# Multiple functions applied to a single value

Derek Elkins ddarius at hotpop.com
Tue Dec 9 08:00:45 EST 2003

```On Mon, 08 Dec 2003 16:33:53 +0000
Graham Klyne <GK at ninebynine.org> wrote:

> A little while ago, I sent a long rambling message  containing a
> key question in response to an earlier helpful response.  I guess my
> ramblings were quite reasonably passed over as too much waffle, and
> the question was lost.  Here, I isolate the question.
>
>  From the earlier response, I understand that:
>
>     ((->) r)
>
> is an instance of Monad, but I haven't found a definition of the Monad
> instance functions.  I think these do the job, and they satisfy the
>
> [[
> instance Monad ((->) r) where
>      return a  =  const a
>      g1 >>= f  =  \e -> f (g1 e) e
> ]]
>
> Can anyone confirm, or point me at an actual definition?

well as various papers containing implementations.

And, as long as you didn't make a mistake below, you've proven it.
Well, you've proven it's a monad in this way.

>  Checking the Monad laws for the above definitions
>
> (A)
>    return a >>= k
>      = [g1/const a][f/k] \e -> f (g1 e) e
>      = \e -> k (const a e) e
>      = \e -> k a e
>      = k a    -- as required.
>
> (B)
>    m >>= return
>      = [g1/m][f/const]  \e -> f (g1 e) e
>      = \e -> const (m e) e
>      = \e -> (m e)
>      = m      -- as required.
>
> (C)
>    m >>= (\x -> k x >>= h)
>      = [g1/m][f/(\x -> k x >>= h)] \e -> f (g1 e) e
>      = \e -> (\x -> k x >>= h) (m e) e
>      = \e -> (k (m e) >>= h) e
>      = \e -> ([g1/k (m e)][f/h] \e1 -> f (g1 e1) e1) e
>      = \e -> (\e1 -> h (k (m e) e1) e1) e
>      = \e -> h (k (m e) e) e
>
>    (m >>= k) >>= h
>      = ([g1/m][f/k] \e -> f (g1 e) e) >>= h
>      = (\e -> k (m e) e) >>= h
>      = [g1/(\e -> k (m e) e)][f/h] \e1 -> f (g1 e1) e1
>      = \e1 -> h ((\e -> k (m e) e) e1) e1
>      = \e1 -> h (k (m e1) e1) e1
>      = \e -> h (k (m e) e) e
>
> So both expressions are equivalent, as required.

```