Opened 8 years ago

Closed 7 years ago

#3826 closed bug (fixed)

Can't infer type (type family as "element" type)

Reported by: spl Owned by: simonpj
Priority: normal Milestone: 7.0.1
Component: Compiler (Type checker) Version: 6.12.1
Keywords: type families Cc: leather@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC rejects valid program Test Case: indexed-types/should_compile/T3826
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

Suppose I have a class C,

class C a where
  type E a
  c :: E a -> a -> a

a datatype T,

data T a = T a

and an instance of C for T

instance C (T a) where
  type E (T a) = a
  c x (T _) = T x

I would like to write a function such as f

f t@(T x) = c x t

without a type signature. Unfortunately, I can't because GHC tells me

    Couldn't match expected type `t' against inferred type `T (E t)'
    In the second argument of `c', namely `t'
    In the expression: c x t
    In the definition of `f': f (t@(T x)) = c x t

There are at least three possible ways to write the above code such that it works.

(1) Give a type signature for f

f :: T a -> T a

(2) Define the class C using an equality constraint

class C t where
  type E t
  c :: (E t ~ e) => e -> t -> t

(3) Define the class C using functional dependencies

class C t e | t -> e where
  c :: e -> t -> t

But the real question is why don't I get a type for f?

This has been tested in GHC 6.10.1 and 6.12.1.

Attachments (1)

Test.lhs (1.1 KB) - added by spl 8 years ago.
Test case

Download all attachments as: .zip

Change History (6)

Changed 8 years ago by spl

Attachment: Test.lhs added

Test case

comment:1 Changed 8 years ago by igloo

Milestone: 6.14.1
Owner: set to simonpj

This sounds like a question for you, Simon.

comment:2 Changed 8 years ago by simonpj

Thank you! Yet another example that should start working when the new constraint solver is in place. I'll keep track of it, but it'll be a couple of months at least.

Simon

comment:3 Changed 7 years ago by igloo

Blocked By: 4232 added

comment:4 Changed 7 years ago by igloo

Blocked By: 4232 removed

comment:5 Changed 7 years ago by simonpj

Resolution: fixed
Status: newclosed
Test Case: indexed-types/should_compile/T3826

Happily this does now indeed work with the new typechecker, so closing.

Simon

Note: See TracTickets for help on using tickets.