[英]What GHC/Haskell specification says that free type constructors match rightmost types?
I was caught off guard recently when I tried to pass a constructor for a type of kind * -> * -> *
, with one bound type var, to a function expecting a constructor for * -> *
.最近,当我试图将带有一个绑定类型 var 的类型
* -> * -> *
的构造函数传递给期望* -> *
的构造函数的 function 时,我措手不及。 Concretely, it was along the lines of passing (\x -> (x, 42)):: (forall a. a -> (a, Int))
to a function of type forall c. (forall a. a -> c a) ->...
具体来说,它是沿着将
(\x -> (x, 42)):: (forall a. a -> (a, Int))
传递到forall c. (forall a. a -> c a) ->...
forall c. (forall a. a -> c a) ->...
. forall c. (forall a. a -> c a) ->...
。 This is ambiguous but not an error in GHC: (,)
cast to * -> *
could be interpreted as a constructor for either the left or righthand argument, and GHC appears to just default to the righthand argument.这是模棱两可的,但在 GHC 中不是错误:
(,)
强制转换为* -> *
可以解释为左侧或右侧参数的构造函数,而 GHC 似乎只是默认为右侧参数。 For higher-kinded matches, it'll take the right most arguments.对于更高种类的匹配,它将采用最右边的 arguments。 To see this, just test the inferred type:
要看到这一点,只需测试推断的类型:
foo :: c a b -> a -> b -> ()
foo _ _ _ = ()
bar = foo ((), 42, 'a') 42 'a' -- typechecks
I wrongly believed instead that it would match the free variables in order, but this higher-rank case is the only time where that would obviously be preferred, and other times it's a wash.相反,我错误地认为它会按顺序匹配自由变量,但这种更高等级的情况是唯一明显首选的情况,而其他时候则是洗牌。 Is there official documentation describing this rule?
是否有描述此规则的官方文档? I'm slightly annoyed but also understanding of the fact that this is not an error, because I can foresee that saves wrapping a lot of things in newtypes.
我有点恼火,但也理解这不是错误的事实,因为我可以预见这可以节省在新类型中包装很多东西。
Unless I misunderstand the question, this simply follows from type application being left-associative, just like function application.除非我误解了这个问题,否则这只是因为类型应用程序是左关联的,就像 function 应用程序一样。
(a, b)
is (,) ab
is ((,) a) b
. (a, b)
是(,) ab
是((,) a) b
。 So (Int, a)
is ((,) Int) a
, but (a, Int)
isn't <something> a
.所以
(Int, a)
是((,) Int) a
,但(a, Int)
不是<something> a
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.