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.
() is very often used as the result of something that has no interesting result. For example, an IO action that is supposed to perform some I/O and terminate without producing a result will typically have type IO () .
(->) is often called the "function arrow" or "function type constructor", and while it does have some special syntax, there's not that much special about it. It's essentially an infix type operator. Give it two types, and it gives you the type of functions between those types.
Internally, Haskell lists are represented as linked cons-cells. A cons-cell is like a C struct with two pointer fields head and tail. The head field points to the first element of the list, the tail field to the rest of the list.
Haskell has first-class functions : functions are values just like integers, lists, etc. They can be passed as arguments, assigned names, etc. … val is value of type Int , and half_of is a value of type Float -> Float .
->
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With