Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I read up on the haskell "->" operator?

Tags:

haskell

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?

like image 700
So8res Avatar asked Feb 03 '12 22:02

So8res


People also ask

What does -> mean in Haskell?

(->) 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.

What does A -> B mean in Haskell?

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 .

What does the arrow do in Haskell?

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.

What does the operator do in Haskell?

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).


1 Answers

(->) 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.

like image 160
hammar Avatar answered Sep 20 '22 14:09

hammar