34

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.

0

1 Answer 1

66

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.

2
  • 1
    Aha! Thank you. Do these parentheses with a comma cause a higher-level tuple of some kind, or is this another meaning of parentheses? Jun 19, 2012 at 3:42
  • 7
    @Andrew actually they do (now) although you probably should not be worrying yourself about such things just yet. Normall Haskell types have kind (type of type) *, so the tuple type has kind * -> * -> *. In recent versions of GHC there is a new kind Constraint such that Eq :: * -> Constraint, and tuples have been promoted to this new kind.
    – Philip JF
    Jun 19, 2012 at 3:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.