A newbie in Haskell land : -> monad

Today, I am trying to understand the (->) monad ... not so easy without any example available.

1. The Monad (->)

(->) is an instance of Monad. In Control.Monad.Instances you can read the definition of

 Monad ((->) r)

So, a monadic value m a for this monad is a function r -> a since m is (->) r

The bind operator is defined with:

 f >>= k = \ r -> k (f r) r

Where f has type r -> a and k has type a -> (r -> b)

How can we understand this ? This is a Reader monad ! f is a computation returning a value of type a for a given environment of type r.

k is a function whose result is dependent on the current environment of type r.

The bind operator is computing the combination of both computations in the same environment.

So, we have computation implicitly parametrized by an environment of type r.

But, this monad is not just a Reader monad. It is a specific kind of Reader monad and its monadic values can also be seen as a family of objects parametrized by a coordinate of type r.

The Functor fmap is just applying a function to the family and it is equivalent to the function composition.

But, the bind operator is more difficult to understand if we choose this point of view.

If k had the type r -> (a -> b) then it would be easy. The bind would just apply a family of functions to a family of objects.

But, we could no more chain the functions since the bind opeartor is expecting another type.

Using the function flip, I can transform that type into the one needed by the bind operator. Then, I can give a meaning to:

f >>= flip k

And, each time I have

f >>= k 

I can transform it into

f >>= flip k1

so I assume that saying the bind operator is applying a family of functions to a familly of objects is an acceptable interpretation ?

(This post was imported from my old blog. The date is the date of the import. The comments where not imported.)

blog comments powered by Disqus