Haskell type signature with multiple class constraints

只愿长相守 提交于 2019-12-04 15:20:27

问题


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, and
  • f :: Eq a, Num b => a -> b

didn't do what I wanted.


回答1:


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.



来源:https://stackoverflow.com/questions/11093847/haskell-type-signature-with-multiple-class-constraints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!