[Haskell-beginners] Could anyone explain what this means? (Haskell wiki entry on monads)

Gesh gesh at gesh.uni.cx
Tue Aug 30 22:56:08 UTC 2016


I agree with you that the phrasing could be better.

On 2016-08-30 22:47, umptious wrote:
>
> >>Each monad, or computation type<<
>
> That seems to be saying that monad and "computation type" are 
> synonyms. But surely this is just bad writing? A monad is a CT, but a 
> CT doesn't have to be a monad? And what is the point of adding the 
> verbiage of "computation type" in what will be a massive sentence anyway?
What they seem to be trying to convey here is that each monad represents 
a type of computation.
E.g. the list monad represents nondeterministic computation, the Maybe 
monad represents failable computation,
the Either monad represents computations that may throw exceptions, etc.
Basically, the point here is to present the weird and abstract "monad" 
in terms of the more familiar and intuitive notion
of "computation". The word "type" here refers to the intuitive sense, 
and not the type-theoretic/programmer's sense IIUC.

> >>provides means, subject to /*Monad Laws*/,<<
>
> I think it's expected that monads will follow monad laws and provide 
> means to do something..
That's clear post priori. That is, obviously, if there are "Foo Laws", 
then we'd expect Foos to satisfy them.

If it was clear to you at the onset, good for you!
You're well on your way to developing the mindset used in many libraries 
(e.g. pipes/conduit, lens, etc.) which is to approach
any new concept with the question "What laws must it satisfy?"

I assume the second half of your remark is pure snark. (Statement 
necessary due to difficulty distinguishing snark from questions)

> >>> to /*(a)*/ /create/ a description of computation action that will 
> produce (a.k.a. "return") a given Haskell value<<<
>
> This is just bizarre. The monad can create a description of a 
> computation action? What does that mean - a comment is a description - 
> does it mean that? And does it mean the description will do the 
> returning or that the computation action will? And why say "means" 
> instead of something more specific and meaningful (for example, I'm 
> guessing the means might be a function..)
An example would make it easier to see what is meant, in my opinion.

Consider the definitions
 > data State s a = Return a | Get (s -> State s a)  | Set s (State s a)
 > instance Functor (State s) where
 >  fmap f (Return a) = Return a
 >  fmap f (Get t) = Get (\s -> fmap f (t s))
 >  fmap f (Set s n) = Set s (fmap f n)
 > instance Monad (State s) where
 >   return = Return
 >   Return a >>= f = f a
 >   m >>= f = fmap (>>= f) m
 > get = Get Return
 > set s = Set s (Return ())
 > modify f = get >>= \s -> set (f s)
 > runState (Return a) _ = a
 > runState (Get t) s = runState (t s)
 > runState (Set s n) _ = runState n s

With these definitions in hand, we can write programs such as:
 > sum = runState go 0
 >   where go [] = get
 >         go (x:xs) = modify (+x) >> go xs
Note, however, that `State` only *describes* a stateful computation.
We could do other things with the value returned by `go` (e.g., count 
the amount of modifications to the global state).

In other words, the point is that monads can be seen as describing a 
program in a language with semantics wildly different
from Haskell's, where at the end of the day we interpret the program 
using whichever interpreter is most useful to us.
Hence, despite Haskell being pure and functional, we can write code that 
makes use of e.g. pointer manipulation,
randomness, nondeterminism, etc.
In fact, the fact that in Haskell we have `main :: IO a` means that the 
value of `main` is the "program text" of some imperative
program that gets passed to GHC's runtime system, which is capable of 
interpreting this imperative program.
It is in this sense, if I'm not mistaken, that Simon Peyton-Jones refers 
to Haskell as the "world's finest imperative programming
language"[0].

I hope this will reduce confusion, rather than create it.

Gesh

[0] - 
https://www.microsoft.com/en-us/research/wp-content/uploads/2016/07/mark.pdf


More information about the Beginners mailing list