I am trying to experiment with System-F types in Haskell and have implemented Church encoding of natural numbers via type
.
When loading this code
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE RankNTypes #-}
type CNat = forall t . (t -> t) -> (t -> t)
c0 :: CNat
c0 _ x = x
type CPair a b = forall t . (a -> b -> t) -> t
cpair :: a -> b -> CPair a b
cpair a b f = f a b
-- pair3 :: CPair CNat String
pair3 = cpair c0 "hello"
into ghci 7.8.2 I get a warning:
λ: :l test.hs
[1 of 1] Compiling Main ( test.hs, interpreted )
test.hs:29:1: Warning:
Top-level binding with no type signature:
pair3 :: forall t t1.
(((t1 -> t1) -> t1 -> t1) -> [Char] -> t) -> t
Ok, modules loaded: Main.
λ:
The question is, shouldn't the type be
pair3 :: forall t.
((forall t1. (t1 -> t1) -> t1 -> t1) -> [Char] -> t) -> t
?
UPD: A simpler example is provided
That's a perfectly good type for it... but it's not the type Hindley-Milner gives it. HM always infers a rank-1 type. Inferring rank-2 types is actually decidable, I believe, but GHC doesn't even try.
But is there any way to specify my type for it so that when I specify it explicitly (uncomment the line in my code), the compiler would not infer it but just check the provided type? Now I am getting error
If you want to instantiate the type variables a,b
occurring in the type of cpair
, you can add the following explicit annotation.
pair3 :: CPair CNat String
pair3 = (cpair :: CNat -> String -> CPair CNat String) c0 "hello"
Without that, cpair
is only instantiated at monomorphic types, I believe.
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