Edit: Here's a truly simple example. Motivation for this example below.
This compiles:
{-# LANGUAGE TypeFamilies #-}
type family F a b
f :: a -> F a b
f = undefined
f' [a] = f a
And ghci reports that:
*Main> :t f'
f' :: [a] -> F a b
But if we add this type signature to the file above, it complains:
*Main> :r
[1 of 1] Compiling Main ( test.hs, interpreted )
test.hs:9:14:
Couldn't match type `F a b0' with `F a b'
NB: `F' is a type function, and may not be injective
In the return type of a call of `f'
In the expression: f a
In an equation for `f'': f' [a] = f a
Failed, modules loaded: none.
What gives?
Motivation:
After seeing this question, I thought I'd be a smart-alec and write a little joke solution. The plan of attack is to start with type-level numbers (as usual), then write a little type-level function Args n a c
that yields the function type that takes n
copies of a
and yields a c
. Then we can write a little type class for the various n
and be on our way. Here's what I want to write:
{-# LANGUAGE TypeFamilies #-}
data Z = Z
data S a = S a
type family Args n a c
type instance Args Z a c = c
type instance Args (S n) a c = a -> Args n a c
class OnAll n where
onAll :: n -> (b -> a) -> Args n a c -> Args n b c
instance OnAll Z where
onAll Z f c = c
instance OnAll n => OnAll (S n) where
onAll (S n) f g b = onAll n f (g (f b))
I was surprised to discover that this didn't type-check!
This is a GHC bug, as can be demonstrated by the following, even further simplified example:
type family F a
f :: b -> F a
f = undefined
f' :: b -> F a
f' a = f a
I suggest reporting it to GHC HQ.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With