I'd like to read more about haskell's ->
operator. I'm not really clear on how much it blurs the line between special syntax and some sort of type class, and I'd like to do some poking around. Specifically, I've seen things like this:
instance Monad ((->) r) where ...
That have piqued my interest.
However, when I try to search for "haskell arrow" or "haskell function" or "haskell class function", I run into the obvious problems of getting results for Control.Arrow
or simple type class tutorials.
What is ->
called and where can I read more about it?
(->) 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.
a -> b Bool means... forall (a :: *) (b :: * -> *). a -> b Bool. b is therefore a type constructor taking a single type argument. Examples of single-argument type constructors abound: Maybe , [] , IO are all examples of things which you could use to instantiate b .
The Arrow (either (->) or MyArr ) is an abstraction of a computation. For a function b -> c , b is the input and c is the output. For a MyArr b c , b is the input and c is the output.
Haskell provides special syntax to support infix notation. An operator is a function that can be applied using infix syntax (Section 3.4), or partially applied using a section (Section 3.5).
(->)
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. Just like 2 + 3
is syntactic sugar for (+) 2 3
, so is from -> to
syntactic sugar for (->) from to
. You can think of it like Function from to
if the symbols are confusing.
In other words, the instance you mentioned can be read as
instance Monad (Function from) where ...
which makes it clear that we're talking about functions which take arguments of some arbitrary (but fixed) type. In fact, this monad instance is found in Control.Monad.Instances and it is essentially the same as the Reader monad.
Looking at the source, it's really quite simple:
instance Monad ((->) r) where return = const f >>= k = \ r -> k (f r) r
The trivial values given by return
ignore the argument, and the (>>=)
operator distributes the argument r
to both sides.
It's also interesting to note that in the corresponding Applicative
instance for functions, pure
and (<*>)
correspond to the K and S combinators of the SKI combinator calculus.
(->)
is also generalized by the Arrow type class. An introduction to arrows can be found here.
Finally, note that the symbol ->
also appears in other more or less unrelated parts of the syntax, including lambda abstractions \x -> ...
, case expressions case ... of x -> ...
, etc. The reverse symbol <-
also occurs in several unrelated contexts. Don't confuse those with the function arrow.
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