inferred type doesn't type-check (using type families)

Max Bolingbroke batterseapower at hotmail.com
Tue Nov 3 15:20:15 EST 2009


2009/11/3 Daniel Fischer <daniel.is.fischer at web.de>:
> Am Dienstag 03 November 2009 19:28:55 schrieb Roland Zumkeller:
>> Hi,
>>
>> Compiling
>>
>> > class WithT a where
>> >   type T a
>> >
>> > f :: T a -> a -> T a
>> > f = undefined
>> >
>> > g x = f x 42
>>
>> with -XTypeFamilies -fwarn-missing-signatures gives:
>>
>>              Inferred type: g :: forall a. (Num a) => T a -> T a
>>
>> Adding
>>
>> > g :: Num a => T a -> T a
>>
>> results in:
>>
>>     Couldn't match expected type `T a' against inferred type `T a1'
>>     In the first argument of `f', namely `x'
>>
>> Is the inferred type not the right one? Is g typeable?
>
> The type function T isn't injective (or, it isn't guaranteed to be), so there's no way to
> determine which type a to use for 42.

I think (untested) that in this particular case you can get around the
problem using scoped type variables:

> g :: forall a. Num a => T a -> T a
> g x = f x (42 :: a)

In fact, this seems to be the general pattern for fixing problems like
this with type families: add extra "witness" arguments which GHC can
use to unify type variables that are "hidden" inside type family
applications.

Cheers,
Max


More information about the Glasgow-haskell-users mailing list