[Haskellcafe] Explaining monads
Arie Peterson
ariep at xs4all.nl
Tue Aug 14 15:15:45 EDT 2007
Dan Piponi wrote:
 On 8/14/07, Lennart Augustsson <lennart at augustsson.net> wrote:
 > You don't normally call x::Int a computation of an Int because there's
 > nothing that distinguishes the value of the x from what it was before
 > you computed it.

 Can you spell out exactly what you mean by this?
Let's take a formal viewpoint: when writing a haskell program, you specify
what properties your values must satisfy. So 'x = 6' means that I want 'x'
to equal the value 6. To make solution of these equations tractable, we
have agreed that we treat them as (recursive) definitions of the LHS.
Now, the compiler may actually treat 'x = 6' as a specification of a
computation. From this formal point of view, however, that is an
implementation detail, and we don't need to speak about 'computations'.
Does this help at all?
 When someone uses the phrase "something else" it implies that we are
 talking about two things, a "something" and a disjoint "something
 else". For example, if x = [1,2,3] what is the "something" and what is
 the "something else"? What was the x "before [I] computed it" and how
 does it differ from its "value"?

 This is just the terminology people use, not an absolute truth, so
 you're free to think it's wrong. :)

 For something like this I prefer to think in terms of "useful" and
 "not useful". If you find the term "computation" useful, I might find
 it useful too. So I'm jealous as I can't figure out how to use it. :)
 I'm not looking for a formal definition or anything like that. But I
 would like a reliable way to distinguish between things that are
 computations and things that are not.
Well, it depends on the context really, and on how hard you squint. Sorry
to disappoint you :) (friendly apologetic smile, not an evil laugh).
Instead of [1,2,3], let me underhandly take the list [False,True]. Nothing
'computational' about it, right? Just a basic list, with some boolean
entries.
But behold! In the context of the 'power set' function from the other thread:
> power = filterM (const [False,True])
, this innocuous list suddenly reveals its monadic/computational
character. In this context, you may interpret [False,True] as a
'multivalued computation of a boolean', which yields both False and True.
'const [False,True]' is a monadic predicate, which yields both False and
True on any element. filterM then applies this predicate to all elements
of a list, sequencing the computational/multivalued aspects.
Greetings,
Arie
More information about the HaskellCafe
mailing list