Skip to content

Type class instance selector doesn't check the apartness of parameters #3941

@acple

Description

@acple

Description

Type class instance selector should check their parameters are apart.

To Reproduce

Prepare the example type class:

class TwoParams a b where
  func :: a -> b

instance equals :: TwoParams a a where
  func = identity
else
instance any :: TwoParams a b where
  func = unsafeCoerce

And the below is correct behavior:

-- can compile
testEquals :: forall a. a -> a
testEquals = func -- with instance `equals`
testAny :: Unit -> Boolean
testAny = func -- with instance `any`

-- expects compile error, cannot choose instance because `b` can be `Unit`
test :: forall b. Unit -> b
test = func
--     ^^^^
--
--   No type class instance was found for
--
--     Main.TwoParams Unit
--                    b0
--
--
-- while checking that type forall (a :: Type) (b :: Type). TwoParams a b => a -> b
--   is at least as general as type Unit -> b0
-- while checking that expression func
--   has type Unit -> b0
-- in value declaration test

However, the below should be able to compiled but not now.

-- `a` and `m a` are never unifiable unless we have infinite types (and of course not)
-- so expected that the instance `any` is chosen.
thisShouldBeCompiled :: forall m a. a -> m a
thisShouldBeCompiled = func
--                     ^^^^
--
--   No type class instance was found for
--
--     Main.TwoParams a0
--                    (m1 a0)
--
--
-- while checking that type forall (a :: Type) (b :: Type). TwoParams a b => a -> b
--   is at least as general as type a0 -> m1 a0
-- while checking that expression func
--   has type a0 -> m1 a0
-- in value declaration thisShouldBeCompiled

Expected behavior

The second one can be compiled with the instance any.

Additional context

This may be root cause of some scenario preventing instance selection with type inference.

PureScript version

v0.13.8 and v0.14.0-rc2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions