简体   繁体   English

gdc 7.8.2上的GADT和明确的forall

[英]GADT and explicit forall on ghc 7.8.2

I'm playing with GADTs and explicit forall on ghc 7.8.2. 我在gdc 7.8.2上玩GADTs和明确的forall。 Let's look at the following simple example: 让我们看看下面这个简单的例子:

{-# LANGUAGE GADTs, RankNTypes #-}

data T1 a where
   T1 :: (b -> a) -> b -> T1 a

data T2 a where
   T2 :: forall b. (b -> a) -> b -> T2 a

Here ghc fails with: 这里ghc失败了:

Test.hs:7:26: Not in scope: type variable ‘a’
Test.hs:7:35: Not in scope: type variable ‘a’

When T2 is commented out type checking succeeds. T2被注释掉时,类型检查成功。 But T1 and T2 are seemingly equivalent. 但是T1T2似乎是等价的。 Is this a bug in ghc or some limitation of GADTs? 这是ghc中的错误还是GADT的一些限制? If the latter then what is the difference between the two? 如果后者那么两者有什么区别?

I originally assumed that a in T1 constructor was binded at data T1 a declaration. 我最初假设a T1构造函数在data T1 a处绑定了data T1 a声明。 But it actually is implicitly quantified in a constructor itself. 但它实际上是在构造函数本身中隐式量化的。 Therefore T2 constructor is wrong because it explicitly quantifies b and doesn't quantify a . 因此T2构造函数是错误的,因为它明确地量化了b并且没有量化a

I was struggling with a similar problem. 我正在努力解决类似的问题。 Based on chi's comment, I came up with this solution: 根据chi的评论,我提出了这个解决方案:

{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE GADTs #-}

data T2 :: * -> * where
   T2 :: forall a b. (b -> a) -> b -> T2 a

I would rather have preferred the b to stand out in comparison to the a , but I guess this is still better than an implicit forall for those who prefer it explicit, which includes myself. 我宁愿首选b相比,脱颖而出a ,但我想这仍然比隐式更好forall为那些喜欢谁是明确的,包括我自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM