Strange type inferencе with RankNTypes extension

梦想与她 提交于 2019-12-08 20:16:07

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/23532659/strange-type-inferenc%d0%b5-with-rankntypes-extension

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!