Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell type signature with multiple class constraints

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.

like image 641
Andrew Wonnacott Avatar asked Jun 19 '12 03:06

Andrew Wonnacott


1 Answers

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.

like image 167
Asherah Avatar answered Oct 03 '22 09:10

Asherah