Haskell类型签名具有多个类别约束 [英] Haskell type signature with multiple class constraints
本文介绍了Haskell类型签名具有多个类别约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎么能有多个类的约束,所以如果 A
是 Eq
和 B
是 Num
,我可以说
f :: Eq a => a - > b`
或
f :: Num b => a - > b
那么,我该如何获得 Eq a =>
和 Num b =>
同时? f :: Eq a =>数字b => a - > b
,
f ::方程式a - >数字b => a - > b
和
f :: Eq a,Num b => a - > b
没有达到我想要的水平。
解决方案
它们通常被称为类约束,如 Eq
和 Num
被称为类型类。
如何?
f ::(等式a,数字b)=> a - > b
括号非常重要。
How can I have multiple class constraints, so if A
is an Eq
and B
is a Num
, I could say either
f :: Eq a => a -> b`
or
f :: Num b => a -> b
So, how can I have Eq a =>
and Num b =>
at the same time?
f :: Eq a => Num b => a -> b
,f :: Eq a -> Num b => a -> b
, andf :: Eq a, Num b => a -> b
didn't do what I wanted.
解决方案
They're usually called class constraints, as Eq
and Num
are called type-classes.
How about this?
f :: (Eq a, Num b) => a -> b
The parentheses are significant.
这篇关于Haskell类型签名具有多个类别约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文