# Difference between revisions of "Monads as containers"

CaleGibbard (talk | contribs) |
CaleGibbard (talk | contribs) |
||

Line 24: | Line 24: | ||

This is expressed in the type of <code>fmap</code>: | This is expressed in the type of <code>fmap</code>: | ||

− | + | <haskell> | |

− | + | fmap :: (Functor f) => (a -> b) -> f a -> f b | |

− | + | </haskell> | |

''If you will give me a blueberry for each apple I give you'' <code>(a -> b)</code>'', | ''If you will give me a blueberry for each apple I give you'' <code>(a -> b)</code>'', | ||

and I have a box of apples'' <code>(f a)</code>'', then I can get a box of blueberries'' | and I have a box of apples'' <code>(f a)</code>'', then I can get a box of blueberries'' | ||

Line 36: | Line 36: | ||

return takes an element of type <code>a</code>, and gives a container of type <code>(m a)</code> | return takes an element of type <code>a</code>, and gives a container of type <code>(m a)</code> | ||

with that element in it. So, its type in Haskell is | with that element in it. So, its type in Haskell is | ||

− | + | <haskell> | |

− | + | return :: (Monad m) => a -> m a | |

− | + | </haskell> | |

''If I have an apple'' <code>(a)</code> ''then I can put it in a box'' <code>(m a)</code>''.'' | ''If I have an apple'' <code>(a)</code> ''then I can put it in a box'' <code>(m a)</code>''.'' | ||

Line 44: | Line 44: | ||

containers <code>m (m a)</code>, and combines them into one <code>m a</code> in some sensible | containers <code>m (m a)</code>, and combines them into one <code>m a</code> in some sensible | ||

fashion. Its Haskell type is | fashion. Its Haskell type is | ||

− | + | <haskell> | |

− | + | join :: (Monad m) => m (m a) -> m a | |

− | + | </haskell> | |

''If I have a box of boxes of apples'' <code>(m (m a))</code> ''then I can take the | ''If I have a box of boxes of apples'' <code>(m (m a))</code> ''then I can take the | ||

apples from each, and put them in a new box'' <code>(m a)</code>''.'' | apples from each, and put them in a new box'' <code>(m a)</code>''.'' | ||

Line 62: | Line 62: | ||

an <code>m (m b)</code>) and then applies join to the result to get a container of type | an <code>m (m b)</code>) and then applies join to the result to get a container of type | ||

<code>(m b)</code>. Its type and definition in Haskell is then | <code>(m b)</code>. Its type and definition in Haskell is then | ||

− | + | <haskell> | |

− | + | (>>=) :: (Monad m) => m a -> (a -> m b) -> m b | |

− | + | xs >>= f = join (fmap f xs) | |

− | + | -- how we would get bind (>>=) in Haskell if it were join and fmap | |

− | + | -- that were chosen to be primitive | |

− | + | </haskell> | |

''If I have a box of apples'' <code>(m a)</code> ''and for each apple, you will give me | ''If I have a box of apples'' <code>(m a)</code> ''and for each apple, you will give me | ||

a box of blueberries'' <code>(a -> m b)</code> ''then I can get a box with all the | a box of blueberries'' <code>(a -> m b)</code> ''then I can get a box with all the | ||

Line 88: | Line 88: | ||

is just the usual <code>map</code>, <code>return</code> is just <code>(\x -> [x])</code> and <code>join</code> is <code>concat</code>. | is just the usual <code>map</code>, <code>return</code> is just <code>(\x -> [x])</code> and <code>join</code> is <code>concat</code>. | ||

− | + | <haskell> | |

− | + | instance Monad [] where | |

− | + | return :: a -> [a] | |

+ | return x = [x] -- make a list containing the one element given | ||

− | + | (>>=) :: [a] -> (a -> [b]) -> [b] | |

− | + | xs >>= f = concat (map f xs) | |

− | + | -- collect up all the results of f (which are lists) | |

− | + | -- and combine them into a new list | |

+ | </haskell> | ||

The list monad, in some sense, models computations which could return any | The list monad, in some sense, models computations which could return any | ||

Line 104: | Line 106: | ||

A couple examples of using this definition of bind: | A couple examples of using this definition of bind: | ||

+ | <haskell> | ||

+ | [10,20,30] >>= \x -> [x, x+1] | ||

+ | -- a function which takes a number and gives both it and its | ||

+ | -- successor at once | ||

+ | = [10,11,20,21,30,31] | ||

− | + | [10,20,30] >>= \x -> [x, x+1] >>= \y -> if y > 20 then [] else [y,y] | |

− | + | = [10,10,11,11,20,20] | |

− | + | </haskell> | |

− | |||

− | |||

− | |||

− | |||

And a simple fractal, exploiting the fact that lists are ordered: | And a simple fractal, exploiting the fact that lists are ordered: | ||

+ | <haskell> | ||

+ | f x | x == '#' = "# #" | ||

+ | | otherwise = " " | ||

− | + | "#" >>= f >>= f >>= f >>= f | |

− | + | = "# # # # # # # # # # # # # # # #" | |

− | + | </haskell> | |

− | |||

− | |||

You might notice a similarity here between bind and function application or | You might notice a similarity here between bind and function application or | ||

Line 134: | Line 138: | ||

The function that does this for any monad in Haskell is called <code>liftM</code> -- it | The function that does this for any monad in Haskell is called <code>liftM</code> -- it | ||

can be written in terms of return and bind as follows: | can be written in terms of return and bind as follows: | ||

− | + | <haskell> | |

− | + | liftM :: (Monad m) => (a -> b) -> m a -> m b | |

− | + | liftM f xs = xs >>= (return . f) | |

− | + | -- take a container full of a's, to each, apply f, | |

− | + | -- put the resulting value of type b in a new container, | |

− | + | -- and then join all the containers together. | |

+ | </haskell> | ||

Joining is equivalent to binding a container with the identity map. This is | Joining is equivalent to binding a container with the identity map. This is | ||

indeed still called <code>join</code> in Haskell: | indeed still called <code>join</code> in Haskell: | ||

− | + | <haskell> | |

− | + | join :: (Monad m) => m (m a) -> m a | |

− | + | join xss = xss >>= id | |

+ | </haskell> | ||

It is common when constructing monadic computations that one ends up with a | It is common when constructing monadic computations that one ends up with a | ||

Line 157: | Line 163: | ||

The above (perhaps somewhat silly) list computations could be written: | The above (perhaps somewhat silly) list computations could be written: | ||

− | + | <haskell> | |

− | + | do x <- [10,20,30] | |

− | + | [x, x+1] | |

− | + | </haskell> | |

and, | and, | ||

− | + | <haskell> | |

− | + | do x <- [10,20,30] | |

− | + | y <- [x, x+1] | |

− | + | if y > 20 then [] else [y,y] | |

+ | </haskell> | ||

The code for liftM could be written: | The code for liftM could be written: | ||

− | + | <haskell> | |

− | + | liftM f xs = do a <- xs | |

− | + | return (f a) | |

+ | </haskell> | ||

---- | ---- | ||

Line 184: | Line 192: | ||

join and return) which make the following tree type a monad. Just keep in | join and return) which make the following tree type a monad. Just keep in | ||

mind what they are supposed to do. | mind what they are supposed to do. | ||

− | + | <haskell> | |

− | + | data Tree a = Leaf a | Branch (Tree a) (Tree a) | |

+ | </haskell> | ||

For more good examples of monads, and lots of explanation see | For more good examples of monads, and lots of explanation see | ||

Line 207: | Line 216: | ||

value out with bind, side effects can occur. Many IO containers don't | value out with bind, side effects can occur. Many IO containers don't | ||

actually contain interesting values. For example, | actually contain interesting values. For example, | ||

− | + | <haskell> | |

− | + | putStrLn "Hello, World!" :: IO () | |

− | + | </haskell> | |

That is, the value returned by <code>putStrLn "Hello, World!"</code> is an IO container | That is, the value returned by <code>putStrLn "Hello, World!"</code> is an IO container | ||

filled with a value of type <code>()</code>, a not so interesting type. However, when | filled with a value of type <code>()</code>, a not so interesting type. However, when | ||

Line 231: | Line 240: | ||

effects and don't care about the values returned by the container on the left | effects and don't care about the values returned by the container on the left | ||

is this: | is this: | ||

− | + | <haskell> | |

− | + | (>>) :: Monad m => m a -> m b -> m b | |

− | + | m >> k = m >>= \_ -> k | |

+ | </haskell> | ||

An example of doing some IO in do notation: | An example of doing some IO in do notation: | ||

− | + | <haskell> | |

− | + | main = do putStrLn "Hello, what is your name?" | |

− | + | name <- getLine | |

− | + | putStrLn ("Hello " ++ name ++ "!") | |

+ | </haskell> | ||

or in terms of bind, making use of the special form: | or in terms of bind, making use of the special form: | ||

− | + | <haskell> | |

− | + | main = putStrLn "Hello, what is your name?" >> | |

− | + | getLine >>= \name -> | |

− | + | putStrLn ("Hello " ++ name ++ "!") | |

+ | </haskell> | ||

or, very primitive, without the special form for bind: | or, very primitive, without the special form for bind: | ||

− | + | <haskell> | |

− | + | main = putStrLn "Hello, what is your name?" >>= \x -> | |

− | + | getLine >>= \name -> | |

− | + | putStrLn ("Hello " ++ name ++ "!") | |

+ | </haskell> | ||

---- | ---- | ||

Line 267: | Line 280: | ||

the index type on our indexed containers. | the index type on our indexed containers. | ||

− | + | <haskell> | |

+ | type Reader e = (->) e -- our monad | ||

+ | </haskell> | ||

Return simply produces the container having a given value at every spot. | Return simply produces the container having a given value at every spot. | ||

− | + | <haskell> | |

− | + | return :: a -> (Reader e a) | |

− | + | return x = (\k -> x) | |

+ | </haskell> | ||

Mapping a function over such a container turns out to be nothing more than | Mapping a function over such a container turns out to be nothing more than | ||

what composition does. | what composition does. | ||

− | + | <haskell> | |

− | + | fmap :: (a -> b) -> Reader e a -> Reader e b | |

− | + | = (a -> b) -> (e -> a) -> (e -> b) | |

+ | -- by definition, (Reader a b) = (a -> b) | ||

− | + | fmap f xs = f . xs | |

+ | </haskell> | ||

How about join? Well, let's have a look at the types. | How about join? Well, let's have a look at the types. | ||

− | + | <haskell> | |

− | + | join :: (Reader e) (Reader e a) -> (Reader e a) | |

− | + | = (e -> e -> a) -> (e -> a) -- by definition of (Reader a) | |

− | + | </haskell> | |

There's only one thing the function of type <code>(e -> a)</code> constructed could | There's only one thing the function of type <code>(e -> a)</code> constructed could | ||

really be doing: | really be doing: | ||

− | + | <haskell> | |

+ | join xss = (\k -> xss k k) | ||

+ | </haskell> | ||

From the container perspective, we are taking an indexed container of indexed | From the container perspective, we are taking an indexed container of indexed | ||

Line 298: | Line 318: | ||

So we can derive what we want bind to do based on this: | So we can derive what we want bind to do based on this: | ||

+ | <haskell> | ||

+ | (>>=) :: (Reader e a) -> (a -> Reader e b) -> (Reader e b) | ||

+ | = (e -> a) -> (a -> (e -> b)) -> (e -> b) -- by definition | ||

− | + | xs >>= f = join (fmap f xs) | |

− | + | = join (f . xs) | |

− | + | = (\k -> (f . xs) k k) | |

− | + | = (\k -> f (xs k) k) | |

− | + | </haskell> | |

− | |||

− | |||

Which is exactly what you'll find in other definitions of the Reader monad. | Which is exactly what you'll find in other definitions of the Reader monad. | ||

Line 338: | Line 359: | ||

this "lookup". | this "lookup". | ||

− | + | {{{#!syntax haskell | |

+ | newtype State s a = State { runState :: (s -> (a,s)) } | ||

+ | }}} | ||

What does return do? It gives a <code>State</code> container with the given element at | What does return do? It gives a <code>State</code> container with the given element at | ||

every index, and with the "address" (a.k.a. state parameter) unchanged. | every index, and with the "address" (a.k.a. state parameter) unchanged. | ||

− | + | <haskell> | |

− | + | return :: a -> State s a | |

− | + | return x = State (\s -> (x,s)) | |

− | + | </haskell> | |

Mapping does the natural thing, applying a function to each of the values of | Mapping does the natural thing, applying a function to each of the values of | ||

type <code>a</code>, throughout the structure. | type <code>a</code>, throughout the structure. | ||

− | + | <haskell> | |

− | + | fmap :: (a -> b) -> (State s a) -> (State s b) | |

− | + | fmap f (State m) = State (onVal f . m) | |

− | + | where onVal f (x, s) = (f x, s) | |

+ | </haskell> | ||

Joining needs a bit more thought. We want to take a value of type | Joining needs a bit more thought. We want to take a value of type | ||

Line 363: | Line 387: | ||

So we get: | So we get: | ||

− | + | <haskell> | |

− | + | join :: (State s (State s a)) -> (State s a) | |

− | + | join xss = State (\s -> uncurry runState (runState xss s)) | |

+ | </haskell> | ||

---- | ---- |

## Revision as of 06:34, 9 May 2006

*There now exists a translation of this article into Russian!*

A *monad* is a container type together with a few methods defined on it.
Monads model different kinds of computations.

Like Haskell lists, all the elements which a monadic container holds at any one time must be the same type (it is homogeneous).

There are a few ways to choose the basic set of functions that one can perform
on these containers to be able to define a monad. Haskell generally uses a
pair of functions called return and bind `(>>=)`

, but it is more natural
sometimes to begin with map (`fmap`

), return and join, as these are simpler
to understand at first. We can later define bind in terms of these.

The first of these three, generally called *map*, (but called `fmap`

in
Haskell 98) actually comes from the definition of a *functor*. We can think
of a functor as a type of container where we are permitted to apply a single
function to every object in the container.

That is, if `f`

is a functor, and we are given a function of type `(a -> b)`

,
and a container of type `(f a)`

, we can get a new container of type `(f b)`

.

This is expressed in the type of `fmap`

:

```
fmap :: (Functor f) => (a -> b) -> f a -> f b
```

*If you will give me a blueberry for each apple I give you* `(a -> b)`

*,*
and I have a box of apples* (f a)*, then I can get a box of blueberries

`(f b)`

*.*

Every monad is a functor.

The second method, *return*, is specific to monads. If `m`

is a monad, then
return takes an element of type `a`

, and gives a container of type `(m a)`

with that element in it. So, its type in Haskell is

```
return :: (Monad m) => a -> m a
```

*If I have an apple* `(a)`

*then I can put it in a box* `(m a)`

*.*

The third method, *join*, also specific to monads, takes a container of
containers `m (m a)`

, and combines them into one `m a`

in some sensible
fashion. Its Haskell type is

```
join :: (Monad m) => m (m a) -> m a
```

*If I have a box of boxes of apples* `(m (m a))`

*then I can take the*
apples from each, and put them in a new box* (m a)*.

From these, we can construct an important operation called *bind* or
*extend*, which is commonly given the symbol `(>>=)`

. When you define your
own monad in Haskell, it will expect you to define just return and bind. It
turns out that mapping and joining come for free from these two. Although only
return and bind are needed to define a monad, it is usually simpler to think
about map, return, and join first, and then get bind from these, as map and
join are in general simpler than bind.

What bind does is to take a container of type `(m a)`

and a function of type
`(a -> m b)`

. It first maps the function over the container, (which would give
an `m (m b)`

) and then applies join to the result to get a container of type
`(m b)`

. Its type and definition in Haskell is then

```
(>>=) :: (Monad m) => m a -> (a -> m b) -> m b
xs >>= f = join (fmap f xs)
-- how we would get bind (>>=) in Haskell if it were join and fmap
-- that were chosen to be primitive
```

*If I have a box of apples* `(m a)`

*and for each apple, you will give me*
a box of blueberries* (a -> m b) *then I can get a box with all the
blueberries together

*.*

`(m b)`

Note that for a given container type, there might be more than one way to define these basic operations (though for obvious reasons, Haskell will only permit one instance of the Monad class per actual type). [Technical side note: The functions return and bind need to satisfy a few laws in order to make a monad, but if you define them in a sensible way given what they are supposed to do, the laws will work out. The laws are only a formal way to give the informal description of the meanings of return and bind I have here.]

It would be good to have a concrete example of a monad at this point, as these functions are not very useful if we cannot find any examples of a type to apply them to.

Lists are most likely the simplest, most illustrative example. Here, `fmap`

is just the usual `map`

, `return`

is just `(\x -> [x])`

and `join`

is `concat`

.

```
instance Monad [] where
return :: a -> [a]
return x = [x] -- make a list containing the one element given
(>>=) :: [a] -> (a -> [b]) -> [b]
xs >>= f = concat (map f xs)
-- collect up all the results of f (which are lists)
-- and combine them into a new list
```

The list monad, in some sense, models computations which could return any
number of values. Bind pumps values in, and catches all the values output.
Such computations are known in computer science as *nondeterministic*.
That is, a list [x,y,z] represents a value which is all of the values x, y,
and z at once.

A couple examples of using this definition of bind:

```
[10,20,30] >>= \x -> [x, x+1]
-- a function which takes a number and gives both it and its
-- successor at once
= [10,11,20,21,30,31]
[10,20,30] >>= \x -> [x, x+1] >>= \y -> if y > 20 then [] else [y,y]
= [10,10,11,11,20,20]
```

And a simple fractal, exploiting the fact that lists are ordered:

```
f x | x == '#' = "# #"
| otherwise = " "
"#" >>= f >>= f >>= f >>= f
= "# # # # # # # # # # # # # # # #"
```

You might notice a similarity here between bind and function application or composition, and this is no coincidence. The reason that bind is so important is that it serves to chain computations on monadic containers together.

You might be interested in how, given just bind and return, we can get back to map and join.

Mapping is equivalent to binding to a function which only returns containers
with a single value in them -- the value that we get from the function of
type `(a -> b)`

which we are handed.

The function that does this for any monad in Haskell is called `liftM`

-- it
can be written in terms of return and bind as follows:

```
liftM :: (Monad m) => (a -> b) -> m a -> m b
liftM f xs = xs >>= (return . f)
-- take a container full of a's, to each, apply f,
-- put the resulting value of type b in a new container,
-- and then join all the containers together.
```

Joining is equivalent to binding a container with the identity map. This is
indeed still called `join`

in Haskell:

```
join :: (Monad m) => m (m a) -> m a
join xss = xss >>= id
```

It is common when constructing monadic computations that one ends up with a large chain of binds and lambdas. For this reason, some syntactic sugar called "do notation" was created to simplify this process, and at the same time, make the computations look somewhat like imperative programs.

Note that in what follows, the syntax emphasizes the fact that the list monad
models nondeterminism: the code `y <- xs`

can be thought of as `y`

taking on
all the values in the list `xs`

at once.

The above (perhaps somewhat silly) list computations could be written:

```
do x <- [10,20,30]
[x, x+1]
```

and,

```
do x <- [10,20,30]
y <- [x, x+1]
if y > 20 then [] else [y,y]
```

The code for liftM could be written:

```
liftM f xs = do a <- xs
return (f a)
```

If you understood the above, then you have a good start on understanding monads in Haskell. Check out Maybe (containers with at most one thing in them, modelling computations that might not return a value) and State (modelling computations that carry around a state parameter using an odd sort of container described below), and you'll start getting the picture as to how things work.

A good exercise is to figure out a definition of bind and return (or fmap, join and return) which make the following tree type a monad. Just keep in mind what they are supposed to do.

```
data Tree a = Leaf a | Branch (Tree a) (Tree a)
```

For more good examples of monads, and lots of explanation see http://www.nomaware.com/monads/html/ which has a catalogue of the commonest ones, more explanation as to why you might be interested in monads, and information about how they work.

The question that many people will be asking at this point is "What does this all have to do with IO?".

Well, in Haskell, IO is a monad. How does this mesh with the notion of a container?

Consider `getChar :: IO Char`

-- that is, an IO container with a character
value in it. The exact character that the container holds is determined when
the program runs by which keys the user presses. Trying to get the character
out of the box will cause a side effect: the program stops and waits for the
user to press a key. This is generally true of IO values - when you get a
value out with bind, side effects can occur. Many IO containers don't
actually contain interesting values. For example,

```
putStrLn "Hello, World!" :: IO ()
```

That is, the value returned by `putStrLn "Hello, World!"`

is an IO container
filled with a value of type `()`

, a not so interesting type. However, when
you pull this value out during a bind operation, the string `Hello, World!`

is printed on the screen. So another way to think about values of type `IO t`

is as computations which when executed, may have side effects before
returning a value of type t.

One thing that you might notice as well, is that there is no ordinary Haskell
function you can call (at least not in standard Haskell) to actually get a
value out of an IO container/computation, other than bind, which puts it right
back in. Such a function of type `IO a -> a`

would be very unsafe in the pure
Haskell world, because the value produced could be different each time it was
called, and the IO computation could have side effects, and there would be no
way to control when it was executed (Haskell is lazy after all). So how do IO
actions ever get run? The IO action called `main`

runs when the program is
executed. It can make use of other IO actions in the process, and everything
starts from there.

When doing IO, a handy special form of bind when you just want the side effects and don't care about the values returned by the container on the left is this:

```
(>>) :: Monad m => m a -> m b -> m b
m >> k = m >>= \_ -> k
```

An example of doing some IO in do notation:

```
main = do putStrLn "Hello, what is your name?"
name <- getLine
putStrLn ("Hello " ++ name ++ "!")
```

or in terms of bind, making use of the special form:

```
main = putStrLn "Hello, what is your name?" >>
getLine >>= \name ->
putStrLn ("Hello " ++ name ++ "!")
```

or, very primitive, without the special form for bind:

```
main = putStrLn "Hello, what is your name?" >>= \x ->
getLine >>= \name ->
putStrLn ("Hello " ++ name ++ "!")
```

Another good example of a monad which perhaps isn't obviously a container at
first, is the Reader monad (see MonadReader). This monad basically consists
of functions from a particular type: `((->) e)`

, which might be written
`(e ->)`

if that were supported syntax. These can be viewed as containers
indexed by values of type `e`

, having one spot for each and every value of
type `e`

. The primitive operations on them follow naturally from thinking
this way.

The Reader monad models computations which read from (depend on) a shared environment. To clear up the correspondence, the type of the environment is the index type on our indexed containers.

```
type Reader e = (->) e -- our monad
```

Return simply produces the container having a given value at every spot.

```
return :: a -> (Reader e a)
return x = (\k -> x)
```

Mapping a function over such a container turns out to be nothing more than what composition does.

```
fmap :: (a -> b) -> Reader e a -> Reader e b
= (a -> b) -> (e -> a) -> (e -> b)
-- by definition, (Reader a b) = (a -> b)
fmap f xs = f . xs
```

How about join? Well, let's have a look at the types.

```
join :: (Reader e) (Reader e a) -> (Reader e a)
= (e -> e -> a) -> (e -> a) -- by definition of (Reader a)
```

There's only one thing the function of type `(e -> a)`

constructed could
really be doing:

```
join xss = (\k -> xss k k)
```

From the container perspective, we are taking an indexed container of indexed
containers and producing a new one which at index `k`

, has the value at index
`k`

in the container at index `k`

.

So we can derive what we want bind to do based on this:

```
(>>=) :: (Reader e a) -> (a -> Reader e b) -> (Reader e b)
= (e -> a) -> (a -> (e -> b)) -> (e -> b) -- by definition
xs >>= f = join (fmap f xs)
= join (f . xs)
= (\k -> (f . xs) k k)
= (\k -> f (xs k) k)
```

Which is exactly what you'll find in other definitions of the Reader monad.
What is it doing? Well, it's taking a container `xs`

, and a function `f`

from
the values in it to new containers, and producing a new container which at
index `k`

, holds the result of looking up the value at `k`

in `xs`

, and then
applying `f`

to it to get a new container, and finally looking up the value
in that container at `k`

.

The MonadsAsComputation perspective makes the purpose of such a monad perhaps
more obvious: bind is taking a computation which may read from the environment
before producing a value of type `a`

, and a function from values of type `a`

to computations which may read from the environment before returning a value
of type `b`

, and composing these together, to get a computation which might
read from the (shared) environment, before returning a value of type `b`

.

How about the StateMonad? (perhaps see MonadState) Although I'll admit that with State and IO in particular, it is generally more natural to take the view of MonadsAsComputation, it is good to see that the container analogy doesn't break down. The state monad is a particular refinement of the reader monad discussed above.

I won't go into huge detail about the state monad here, so if you don't already know what it's for, what follows may seem a bit unnatural. It's perhaps better taken as a secondary way to look at the structure.

For reference to the analogy, a value of type `(State s a)`

is like a
container indexed by values of type `s`

, and at each index, it has a value of
type `a`

and another, new value of type `s`

. The function `runState`

does
this "lookup".

{{{#!syntax haskell newtype State s a = State { runState :: (s -> (a,s)) } }}}

What does return do? It gives a `State`

container with the given element at
every index, and with the "address" (a.k.a. state parameter) unchanged.

```
return :: a -> State s a
return x = State (\s -> (x,s))
```

Mapping does the natural thing, applying a function to each of the values of
type `a`

, throughout the structure.

```
fmap :: (a -> b) -> (State s a) -> (State s b)
fmap f (State m) = State (onVal f . m)
where onVal f (x, s) = (f x, s)
```

Joining needs a bit more thought. We want to take a value of type
`(State s (State s a))`

and turn it into a `(State s a)`

in a natural way.
This is essentially removal of indirection. We take the new address and new
box that we get from looking up a given address in the box, and we do another
lookup -- note that this is almost the same as what we did with the reader
monad, only we use the new address that we get at the location, rather than
the same address as for the first lookup.

So we get:

```
join :: (State s (State s a)) -> (State s a)
join xss = State (\s -> uncurry runState (runState xss s))
```

I hope that the above was a reasonably clear introduction to what monads are about. Feel free to make criticisms and ask questions.

-- CaleGibbard