Haskell does not want to print high-rank polymorphism

I do not understand why this program is not typical:

type Test a = forall z. (a -> z) -> z

cons :: a -> Test a
cons = \a -> \p -> p a

type Identity = forall x. x -> x

t :: Identity
t = \x -> x

c :: Test Identity
c = cons (t :: Identity)

main :: IO ()
main = do{
  print "test"
}

I use the option -XRankNTypeswith GHC.

I have the following error:

Couldn't match type `x0 -> x0' with `forall x. x -> x'
Expected type: Identity
  Actual type: x0 -> x0
In the first argument of `cons', namely `(t :: Identity)'
In the expression: cons (t :: Identity)
In an equation for `c': c = cons (t :: Identity)

Can someone help me?

+4
source share
1 answer

Conclusion c RankNTypesis complicated. Try annotating the function instead of the argument.

c :: Test Identity
c = (cons :: Identity -> Test Identity) t

Why this is so requires an understanding of the intricacies of a type inference algorithm. There is some kind of intuition.

, , x :: forall a. F(a), a . , a "" a0 ( ). , .

:

id id :: ??

id0 id1.

id0 id1 :: ?? 
id0 :: forall a. a->a
id1 :: forall a. a->a

id0 :: a0 -> a0
id1 :: a1 -> a1

: a0 ~ (a1 -> a1).

id0 :: (a1 -> a1) -> (a1 -> a1)
id1 :: a1 -> a1

.

id0 id1 :: a1 -> a1

Re-.

id0 id1 :: forall a. a->a

, , a0 ~ (forall a. a->a) a1. , , , .

, :

(id :: (forall a. a->a) -> (forall a. a->a)) id

. 2 ( -N). GHC , , -: forall - , , . , forall , , .

GHC, , . gory, , . ( , , - , , Hindley-Milner).

+5
source

All Articles