Haskell多上下文 - currying? [英] Haskell Multiple Contexts - currying?

查看:95
本文介绍了Haskell多上下文 - currying?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在类型签名上编写上下文时,通常我会做类似于

  f ::(Enum a,Ord a )=> a  - > a 

但通过纯粹的运气,我发现这个编译和似乎是一样的,至少在GHC 7.8:

  f :: Enum a => Ord a => a  - > a 

这两者之间的理论或实际区别是什么?第二个不太正统? Haskell报告似乎没有提到第二种形式,我从来没有见过它在任何地方使用。 相关问题

解决方案

这两个版本是一样的。限制条件和 forall -s当它们不在那里时会浮动到范围的顶部。例如,以下定义是有效的:

  foo :: a  - > a  - > Num a => a 
foo =(+)

bar :: a - > forall b。 b - > a
bar = const

但是:t foo 打印 Num a => a - > a - > a :t bar 打印 a - > b - > a (相当于 forall a b。a - > b - > a )。

GHC不支持多态返回类型,这就是为什么约束和量词被浮动的原因。我猜这可能也是GHC抛出错误的有效设计选择。


When writing contexts on a type signature, ordinarily I would do something like

f :: (Enum a, Ord a) => a -> a

But through sheer dumb luck I found that this compiles and seems to work identically, at least on GHC 7.8:

f :: Enum a => Ord a => a -> a

What are the theoretical or practical differences between the two? Is the second one less orthodox? The Haskell report seems to make no mention of the second form, and I've never seen it used anywhere. Related question.

解决方案

The two versions are the same. Constraints and forall-s are floated out to the top of the scope when they are not already there. For example, the following definitions are valid:

foo :: a -> a -> Num a => a
foo = (+)

bar :: a -> forall b. b -> a
bar = const

But :t foo prints Num a => a -> a -> a and :t bar prints a -> b -> a (which is equivalent to forall a b. a -> b -> a).

GHC doesn't support polymorphic return types, that's why the constraints and quantifiers are floated out. I guess it could have been also a valid design choice for GHC to throw errors.

这篇关于Haskell多上下文 - currying?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆