Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding ghci's inferred type signature causes an error

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!

like image 287
Daniel Wagner Avatar asked Dec 16 '11 09:12

Daniel Wagner


1 Answers

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.

like image 158
ehird Avatar answered Nov 16 '22 14:11

ehird