Reputation: 888
In Haskell, what does ((->) t) mean in the type signature of instances? For example Functor, Applicative and Monad all have an instance along the lines of:
Functor ((->) r)
I can't find any explanation of what this type signature means and it's highly search engine-resistant.
Upvotes: 16
Views: 1414
Reputation: 13677
->
is an infix type constructor. You can compare it with :
- an infix value constructor for list type. To use :
alone we put parentheses around it so it becomes a prefix function application:
(:) a b
is the same as a : b
Similarly, (->) a b
is the same as a -> b
, type of a function from a
to b
.
(->) a
is a partial application of type constructor, and itself a type constructor of kind * -> *
.
You can think of it as "a constructor of types of functions from a". E.g. (->) Int
is a constructor of types of functions from Int
. You can construct full function type by passing another type to it: (->) Int String
is the type of functions from Int
to String
.
instance Functor (->) a
is a functor with fmap
operation transforming an a -> b
function into an a -> c
function. You can compare it with a similar instance Functor (Either a)
which maps Either a b
to Either a c
by applying the fmap
argument to Right
values.
Upvotes: 19
Reputation: 1622
We could use lambda functions and infix functions:
(->) a = \b -> (->) a b --pseudo-Haskell
(->) a = \b -> a -> b --pseudo-Haskell
so, read instance as:
class Functor f where
fmap :: (a->b) -> f a -> f b
instance Functor ((->)r) where
fmap :: (a->b) -> f a -> f b
= (a->b) -> (->)r a -> (->)r b --pseudo-Haskell
= (a->b) -> (r -> a) -> (r -> b) --pseudo-Haskell
Upvotes: 12
Reputation: 3061
You could see it as the set of types r -> a
where r
is fixed.
A functor is a type function m
, which means that for any type a
you have a type m a
. Examples are Maybe
, []
and (->) r
. The latter should perhaps better be written (r ->)
, but I don't know if that's allowed.
Upvotes: 4