Reputation: 117
First of all, please be aware that I looked online (in stackoverflow and Hoogle) to see if I could find anything to this topic!
I have been working myself through a "learn yourself a haskell" and there are two things I have been stumbling upon quite often which I don't really understand. The use of => and (Ord a) in several important functions.
As an example:
max :: (Ord a) => a -> a -> a
and is the use of "Ordering" the same as (Ord a), as in:
compareWith :: Int -> Ordering
Thank you for taking your time and helping me with this (probably) simpole question!
Upvotes: 1
Views: 1708
Reputation: 40378
The part (Ord a) =>
is a type constraint, indicating a is an ordinal type (it can be ordered).
You can read max :: (Ord a) => a -> a -> a
as:
where a is an ordinal type, "max" takes 2 arguments of type a and returns type a
Upvotes: 4
Reputation: 36375
The =>
symbol is used to build class constraints on a function. In the max
example, it means that all parameters of type a
need to implement the Ord
typeclass.
Consider the implementation of max
max :: (Ord a) => a -> a -> a
max x y = if x < y then y else x
The only thing we know about x
and y
is that they have to satisfy the Ord
typeclass. That in turn allows us to use the <
function for comparison, whose signature is:
(<) :: Ord a => a -> a -> Bool
If you were to omit the Ord a
constraint from the definition of max
, then it wouldn't compile, because the body of max
wouldn't be able to use the comparison function.
Upvotes: 8