我想创建一个元组,里面有一个箭头和一个描述箭头的字符串。哈斯克尔箭头里面的箭头
funTimes10 = (*10)
describe10 = "times 10"
tuple10 :: (Num b) => ((b -> b), String)
tuple10 = (,) funTimes10 describe10
我可以fst
访问功能,并与snd
我得到的功能的描述字符串:如果我和功能(而不是箭头),以下的作品像预期的这样做。
但是,如果我在下面的交换功能有一个箭头,如:
aTuple10 :: (Arrow a, Num b) => (a b b, String)
aTuple10 = (,) (arr funTimes10) describe10
fst
仍然有效,并返回我的箭,但- 我没有得到任何描述字符串
snd
。
我只得到了此错误消息:
Ambiguous type variable `a0' in the constraint:
(Arrow a0) arising from a use of `aTuple10'
Probable fix: add a type signature that fixes these type variable(s)
In the first argument of `snd', namely `aTuple10'
In the expression: (snd aTuple10)
In an equation for `it': it = (snd aTuple10)
为什么我得到这个错误,而我应该怎么做,才能避免呢?
就是这样;)thx – frosch03 2012-02-27 14:07:02
有多刺激。人们会期望类型系统能够得出结论:'snd aTuple10'的类型是'String';这可以被视为实施中的错误吗?当然,Haskell 2010没有指定这样的行为。有人可能会争辩说,如果实现不知道第一个东西是什么类型的,它不会知道第二个东西在内存中的什么位置,但是因为我们在这里处理盒装元组,所以应该总是有两个指针,因此无论第一个元素的类型如何,第二个元素都可以轻松定位。 – 2012-02-27 21:41:57
像'class Boolish a where toBool :: a - > Bool; foo :: Boolish a =>(a,b) - > b; foo(a,b)=如果toBool a然后b else undefined'是可以想象的,所以函数的结果可以依赖于一个模棱两可的参数。在这种情况下,'snd'的特殊外壳会很奇怪。 – dflemstr 2012-02-27 21:54:59