# Monads in Haskell

Monads and Effects, 4

## Influential papers

- [Wad92a] P. Wadler. Comprehending monads.
- [Wad92b] P. Wadler. The essence of functional programming.
- [Wad95] P. Wadler. Monads for functional programming.

## Implementing Monads in Haskell

The representation of monads in Haskell is based on the *Kleisli triple* formulation:

A

Kleisli tripleover a categroy π is a triple (T,Ξ·,_{β}^{*}), whereTβ:β|π|βββ|π|,βΞ·_{A}β:βAβββTAforAβββ|π|,βf^{*}β:βTAβββTBforfβ:βAβββTBand the following equation hold: . . .

In Haskell, π is the categroy which Haskell types as objects and Haskell functions as arrows, *T* corresponds to a parameterised type, *Ξ·* is called `return`

, and _{β}^{*} is called `>>==`

```
return :: a -> T a
(>>=) :: (a -> T b) -> (T a -> T b)
```

In Haskell,

`return`

is used to wrap data in amonad. When speaking about I/O,`return`

is used to take pure data and bring it into the IO monad.

βReal World Haskell, Chapter 7, I/O, The IO MonadA useful way to link its behavior to its name is that it

injects(returns) a pure value (of type`a`

) into a monad (of type`m a`

).

βReal World Haskell, Chapter 14, Monads, The Monad Typeclass

We can consider `>>=`

to be a currried function of *two* arguments, with type `(a -> T b)`

and `T a`

.

We give `>>=`

the type

`(>>=) :: T a -> (a -> T b) -> T b`

Now the metalanguage notation let *x*βββ*e*_{1} in *e*_{2} can be conveniently expressed as:

`>>= \x -> e2 e1 `

`\x -> e`

means*Ξ»*xe- this bind
`x`

to the result of`e1`

in`e2`

; `>>=`

pronounced as βbindβ`\x`

pronounced as β*Ξ»*xβ

This is Anonymous (lambda) function in Haskell. We introduce an anonymous function with a backslash character (

`\`

) pronouncedlambda. (the backslash was chosen for its visual resemblance to the Greek letterΞ».)

βReal World Haskell, Chapter 4, Functional Programming, Anonymous (lambda) Functions