I am a newbie in the Haskell land. I was lost but found some good maps and discovered there is a tradition in Haskell land : writing a
monad tutorial.

There are so many monad tutorials that writing a new one is getting difficult. And writing a good one if even more difficult. So, I am just
going to explain my own understanding.

The first thing to note is that monads are EASY !!

What's difficult is trying to understand what they have in common because they can look so different. I have identified three kinds of
monads (not exclusive - a monad can belong to more than one kind):

Monad as control of the sequencing ;

Monad as control of side effects ;

Monad as container

1. Monad as control of the sequencing

In a lazy functional programming language like Haskell, the order of evaluation does not matter. It does not mean you cannot control
the order of evaluation. It means you can abstract it and build your own sequencing, your own control.

In imperative languages (like C), you need to extend the language to support new control statements.

In less elegant functional languages (like LISP) you need to have special forms which do not follow the normal rules for evaluation.

In Haskell, you "just" build your own control operators. Let's see some examples:

1.1. Control in IO monad

repeatN0a=return()repeatNna=a>>repeatN(n-1)a

test=repeatN3$doputStrLn"TEST"

And, if you want to pass the loop index to the loop body, you may write:

repeatN0a=return()repeatNna=(an)>>repeatN(n-1)a

test=repeatN3$\i->doputStrLn$"TEST : "++(showi)

1.2. Indeterminism monad also known as List monad

Another example of control of the sequencing is the indeterminism monad:

importControl.Monad.List

-- f is a function returning several possible resultsf::Int->[Int]fx=[1+x,2*x]

test::IO()test=putStrLn.show$doa<-return5b<-fareturnb

Here we apply a function f to the value 5. The function f is returning several possible results.

It is possible to chain indeterminate functions like f:

test2::IO()test2=putStrLn.show$doa<-return5b<-fac<-fbreturnc

but we do not need to give a name to the intermediate results, so let's write it like:

We create a Reader monad to have access to the environment defined by initState. Then in the monad, we can access the fields of
initState.

This state is available whenever we need it in the monad and we do not need to pass it as argument.

runReaderT and lift are explained later. They are not important to understand this example. You just have to know that the line
with lift is used to display a value and the runReaderT is used to initialize the environment.

Now, we can temporarily change the value of one variable and work in this modified environment.

-- Increment vara from the environmentincrementVarA::Int->MyState->MyStateincrementVarAxp=p{vara=(varap)+x}

test=dotheVarA<-asksvaralift.putStrLn$showtheVarA-- computation in the new modified environmentlocal(incrementVarA5)$dotheVarA<-asksvaralift.putStrLn$showtheVarAtheVarA<-asksvaralift.putStrLn$showtheVarA`runReaderT`initState

We have a side effect since the environment is modified and this change is visible in a non local way.
But this change is nevertheless restricted by the local function.

The previous examples are in fact using the Reader monad and the IO monad hence the use of the monad transformer ReaderT and runReaderT.

You may use runReader. With runReader the type of test is no more IO () but Int:

runReaderT is just a bit more complex. It has type: ReaderT r m a -> r -> m a

So, when you're working in ReaderT r IO a, you need to specify if you are working with values of type ReaderT r IO a or IO a. The lift
function is used for this. Its type is m a -> t m a. So it will transform IO a values to ReaderT r IO a.

A different way to look at this (probably a wrong way) is:

If you have a value v of type a, you use return v to inject it in the ReaderT r IO a monad.

return v would not work if v was of type IO a since you would get a value of type ReaderT r IO (IO a).

So, lift is used to inject the value in the monad.

3. Monad as container

In each monad, you have the return function which is injecting an element into the monad. So, any monad can be seen as a kind
of container. For the List monad it is obvious. Seeing a monad as a container can be very useful.

Assume you want to add an integer to the result of a computation which could return no result. You may have to do something like that

result=Just20

test=caseresultofJusta->Just(a+10)_->Nothing

So, you need to extract the value from the container (if there is something to extract), apply your function and package the result in the same container.

Or you can just write:

test=(+10)`fmap`result

fmap is a kind of generalization of map. map is lifting a function a -> b to the container [a] -> [b]

fmap is doing the same for a container m (a monad). So, fmap is transforming the type a -> b to m a -> m b

4. Deriving monad (you have to use -fglasgow-exts)

In a same code you may have to use different Reader monads even if they have the same type since they may be for different uses.

You may create a type synonym :

typeMyEnvironmenta=ReaderInta-- (here the environment is just an Int)

But it would not prevent from mixing two different Reader monads if they have the same type.

So, you need to create a new type:

newtypeMyEnvironmenta=MyEnvironment{runMyEnvironment::ReaderInta}

Then, you want the same behavior. This is just a reader monad (from a behavior point of view)
like newtype Meter = Meter Int is just a number (from a behavior point of view).

So, instead of having to write several instance declarations, you just write:

Then you create an environment . It is just a Reader monad contained in your new type

r::MyEnvironmentIntr=dor<-MyEnvironment$ask-- This is packaging the result of ask-- in MyEnvironment. Hence the work is donereturnr-- in the MyEnvironment monad and not in a -- simple Reader monad r is an Int but -- return r is a MyEnvironment Int and not -- a Reader Int Int

Then, you extract the reader monad and apply it to the initial state

test=putStrLn.show$(runMyEnvironmentr)`runReader`4

5. What's common ?

What do the previous monads have in common ? Nothing ! Or not a lot. Indeed, being a monad is a very general concept and focusing on the part
they have in common (return, >>=) is not the interesting part nor the difficult one. What is interesting is how different they are : a Reader monad
is providing ask and local functions ; an IO monad is providing putStrLn etc...

Each monad has its own personality. Of course, >>= will not be the same in each monad but from a user point of view, it will respect the same
monadic laws :

returna>>=k==ka-- return is a "neutral element" on leftm>>=return==m-- return is a "neutral element" on rightm>>=(\x->kx>>=h)==(m>>=k)>>=h-- a kind of associativity of >>=

The only things shared by all monads : the monadic laws.

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