What does the => symbol mean in Haskell?

n00b picture n00b · Feb 4, 2012 · Viewed 17.4k times · Source

I'm new to Haskell and, in general, to functional programming, and I'm a bit uncomfortable with its syntax.

In the following code what does the => denote? And also (Num a, Ord a)?

loop :: (Num a, Ord a) => a -> (t -> t) -> t -> t

Answer

ehird picture ehird · Feb 4, 2012

This is a typeclass constraint; (Num a, Ord a) => ... means that loop works with any type a that is an instance of the Num and Ord typeclasses, corresponding to numeric types and ordered types respectively. Basically, you can think of loop as having the type on the right hand side of the =>, except that a is required to be an instance of Num and Ord.

You can think of typeclasses as basically similar to OOP interfaces (but they're not the same thing!) — they encapsulate a set of definitions which any instance must support, and generic code can be written using these definitions. For instance, Num includes numeric operations like addition and multiplication, while Ord includes less than, greater than, and so on.

For more information on typeclasses, see this introduction from Learn You a Haskell.