Haskell Type Family Error

Sorry to bother you with this simple problem. I am trying to find out how type family extension works. When I deceived myself, I encountered a mistake, I could not understand why.

class Foo abc where data T abc :: * f :: a -> T abc g :: T abc -> b h :: c -> a -> b hca = g $ fa 

Error message:

  Could not deduce (Foo ab c0) arising from a use of 'g' from the context (Foo abc) bound by the class declaration for 'Foo' at DB/Internal/Typecast.hs:(17,1)-(25,19) The type variable 'c0' is ambiguous Relevant bindings include a :: a (bound at DB/Internal/Typecast.hs:25:9) h :: c -> a -> b (bound at DB/Internal/Typecast.hs:25:5) In the expression: g In the expression: g $ fa In an equation for 'h': hca = g $ fa 

I do not understand how c is ambiguous in T abc for g . Could the compiler get type c from T abc from f ?

I just need composite g . f g . f

+5
source share
1 answer

Note that in the definition

 h :: c -> a -> b hca = g $ fa 

there is no limit to the fact that f and g refer to the same instance that you define h for. (And this flexibility is often useful for specifying instances.)

From type inference, the result of g limited to the same type of b , and the argument f limited to type a , but nothing is said that T abc passed from one to the other uses the same c !

To fix this in this case, you can enable ScopedTypeVariables and do

  hca = g (fa :: T abc) 

Note that this works because data families are “injective” (arguments to the data family type can be inferred from the final type). If you used a type family, even that would not work, since then T abc would not necessarily define c at all.

+4
source

All Articles