Dmitriy Matrosov | 9 Feb 12:02
Picon

Relation between monads and computations

Hi, everyone!

Not so long ago i started to learn haskell, and now i have a question
about relation between monads and computations. In fact, i think, that
i understand it and write some explanation for myself, but i'm not
sure whether my explanation is correct or not, and will be very
thankful if someone check it :) Here it is (i don't know category
theory and my math knowledge is poor, so if i accidently use some
terms from them incorrectly - it was just my understanding).

Monads and computations.

Generally computation consists of initial value, computation itself and the
result. So it can be illustrated as:

    type    a         (M b)            b
    data    I ------>   C   <--------- E
                 f            ConstrM

    I :: a
    f :: a -> M b
    data M a = ContrM a | ...
    E :: b

Let's see what happens: i take initial value I of type a and map it to some
computation C (of type (M b)) using function f. Then, exist such value E of
type b, that C = (ConstrM E). This value E will be the result of computation.

Now consider two functions: unitM, which maps value into trivial computation
(trivial computation is a computation, which result is equal to initial
(Continue reading)

Brent Yorgey | 14 Feb 18:16
Favicon

Re: Relation between monads and computations

Hi Dmitriy,

On Thu, Feb 09, 2012 at 02:02:59PM +0300, Dmitriy Matrosov wrote:
> Hi, everyone!
> 
> Not so long ago i started to learn haskell, and now i have a question
> about relation between monads and computations. In fact, i think, that
> i understand it and write some explanation for myself, but i'm not
> sure whether my explanation is correct or not, and will be very
> thankful if someone check it :) Here it is (i don't know category
> theory and my math knowledge is poor, so if i accidently use some
> terms from them incorrectly - it was just my understanding).

You say you have a question, but from reading the below I am not sure
what your question is...

Let me say first that while "monad" has a precise technical
definition, "computation" does not.  So "the relation between monads
and computations" is not well-defined unless it is specified what you
mean by "computation".  There are many ways to model different ideas
of "computation"; one of them is monads, but that is not the only way.

> Monads and computations.
> 
> Generally computation consists of initial value, computation itself and the
> result. So it can be illustrated as:
> 
>     type    a         (M b)            b
>     data    I ------>   C   <--------- E
>                  f            ConstrM
(Continue reading)

Dmitriy Matrosov | 15 Feb 20:31
Picon

Re: Relation between monads and computations

Hi Brent, thanks for the answer!

> On Thu, Feb 09, 2012 at 02:02:59PM +0300, Dmitriy Matrosov wrote:
> > Hi, everyone!
> >
> > Not so long ago i started to learn haskell, and now i have a question
> > about relation between monads and computations. In fact, i think, that
> > i understand it and write some explanation for myself, but i'm not
> > sure whether my explanation is correct or not, and will be very
> > thankful if someone check it :) Here it is (i don't know category
> > theory and my math knowledge is poor, so if i accidently use some
> > terms from them incorrectly - it was just my understanding).
>
> You say you have a question, but from reading the below I am not sure
> what your question is...

The original question was: "Am i right?". I just try to understand what monad
is, and, because it is referred as computations, i try to understand why.
E.g. from [2], end of section 2.1:
"Just as the type Value represents a value, the type M Value can be thought of
as representing a computation. The purpose of unitM is to coerce a value into
computation; the purpose of bindM is to evaluate a computation, yielding a
value."
Similarly, it often referred as computation in [1]. (Though, i don't finish
reading both of these papers).

> Let me say first that while "monad" has a precise technical
> definition, "computation" does not.  So "the relation between monads
> and computations" is not well-defined unless it is specified what you
> mean by "computation".  There are many ways to model different ideas
(Continue reading)

Brent Yorgey | 15 Feb 21:23
Favicon

Re: Relation between monads and computations

On Wed, Feb 15, 2012 at 10:31:03PM +0300, Dmitriy Matrosov wrote:
> Hi Brent, thanks for the answer!
> 
> > On Thu, Feb 09, 2012 at 02:02:59PM +0300, Dmitriy Matrosov wrote:
> > > Hi, everyone!
> > >
> > > Not so long ago i started to learn haskell, and now i have a question
> > > about relation between monads and computations. In fact, i think, that
> > > i understand it and write some explanation for myself, but i'm not
> > > sure whether my explanation is correct or not, and will be very
> > > thankful if someone check it :) Here it is (i don't know category
> > > theory and my math knowledge is poor, so if i accidently use some
> > > terms from them incorrectly - it was just my understanding).
> >
> > You say you have a question, but from reading the below I am not sure
> > what your question is...
> 
> The original question was: "Am i right?". I just try to understand what monad
> is, and, because it is referred as computations, i try to understand why.
> E.g. from [2], end of section 2.1:
> "Just as the type Value represents a value, the type M Value can be thought of
> as representing a computation. The purpose of unitM is to coerce a value into
> computation; the purpose of bindM is to evaluate a computation, yielding a
> value."

"The purpose of bindM is to evaluate a computation, yielding a value"
-- I have the greatest respect for Phil Wadler, but this is simply
wrong.  From this description one would expect bindM to have the type

  bindM :: M a -> a
(Continue reading)

Dmitriy Matrosov | 16 Feb 10:39
Picon

Re: Relation between monads and computations

> > > > Now, using fucntions unitM and bindM there is possibly to convert arbitrary
> > > > function (k :: a -> b), which makes from initial value of type a value of type
> > > > b, into terms of general computation (represented by type M).
> > >
> > > Yes, (k :: a -> b) can be converted into a function of type  (a -> M
> > > b), but I think you have made it more complicated than necessary.  All
> > > you need to do is  (unitM . k).
> >
> > Hmm, so i was wrong here. Initially, i suppose, that the purpose of bindM is
> > to convert function of type (a -> b) into function of type (a -> M b), but now
> > i see it is wrong. Mentioned above quote from [2] says, that "the purpose of
> > bindM is to evaluate a computation, yielding a value.", which sounds a little
> > unfinished to me.  Then may be: the purpose of bindM is to make composition
> > of functions of type (a -> M b) and (b -> M c) possible. Is this
> > right?
>
> Yes!  In fact, there is a standard operator
>
>   (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
>
> which is defined in terms of bind.  Implementing it is a good
> exercise.
>
> By the way, you may be interested in reading the Typeclassopedia:
>
>   http://www.haskell.org/haskellwiki/Typeclassopedia

Thank you very much, Brent! I'll try what you advise and read this link. And
then may be ask again.. :)

(Continue reading)


Gmane