Reputation: 17
I am new to Haskell, but it was really fun until now. Currently I am working on understanding Types and Type Classes
Example: add :: Integer -> Integer -> Integer
. ->
is right associative which means the declaration is similar to Integer -> (Integer -> Integer)
, so far so good. But what does (a->b) -> a -> b
mean? Why do we use parenthesis suddenly?
In my textbook there is an example for this declaration with a function apply::(a->b)-> a->b
with the def. apply f x = f x
. But I don't understand that, isn't (a->b)
a single function?
I know that a and b are Typevariables which indicates that a and b are of different Types.
Upvotes: 1
Views: 134
Reputation: 1365
Whenever you see parenthesis in the type signature you can think of it as one block. So (a -> b) -> a -> b
is the same as c -> a -> b
where c
stands for a -> b
. c
just happens to be a type which is a function itself.
The same way your first example Integer -> (Integer -> Integer)
was a function that takes an Integer
and returns a function Integer -> Integer
; your function (a -> b) -> a -> b
is a function that takes as argument a function a -> b
and an argument a
in order to return a b
.
In the case of this function apply
it is simply function application. If apply
is defined as apply f x = f x
it simply passes argument x
to the function f
. By the way, this function already exists in the Prelude and is called ($)
.
Upvotes: 6