Re: Cannot understand liftM2

> Using the cool lambdabot "pointless" utility I found out that:
>
> > \x -> snd(x) - fst(x)
>
> is the same as:
>
> > liftM2 (-) snd fst
>
> I like the elegance of this but I cannot reconcile it with its type. I
> can't understand it.
> I check the signature of liftM2 and I get:
>
> Prelude> :t liftM2
> Prelude> liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
>
> Can someone help me understand what's happening here ?
> What does a Monad have to do with a simple subtraction ?
> What is actually the "m" of my example ?

You came across a very nice Monad: “((->) a)”, or the Monad of all
functions that take a parameter of type a.

Do not be confused by the arrow _before_ the a, it is actually behind
the a: A function of type “a -> b” has type “(->) a b”. The same syntax
as for infix operators applies, and can be curried to “((->) a)”.

Re: Cannot understand liftM2

> Hi All,
>
> I'm loving learning Haskell quite a bit.
> It is stretching my brain but in a delightfull way.
>
> I've googled, I've hoogled but I haven't found a clear explanation for
> what exactly liftM2 does in the context below.
>
> Using the cool lambdabot "pointless" utility I found out that:
>
> > \x -> snd(x) - fst(x)
>
> is the same as:
>
> > liftM2 (-) snd fst
>
> I like the elegance of this but I cannot reconcile it with its type. I
> can't understand it.
> I check the signature of liftM2 and I get:
>
> Prelude> :t liftM2
> Prelude> liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
>
> Can someone help me understand what's happening here ?
> What does a Monad have to do with a simple subtraction ?
> What is actually the "m" of my example ?
>
> I am sure if I get this I'll be another step closer to illumination ...
>
> Thanks,
> Nick

Hi Nick!

The monad instance which is being used here is the instance for ((->)
e) -- that is, functions from a fixed type e form a monad.

So in this case:
liftM2 :: (a1 -> a2 -> r) -> (e -> a1) -> (e -> a2) -> (e -> r)
I bet you can guess what this does just by contemplating the type. (If
it's not automatic, then it's good exercise) Now, why does it do that?

Well, in general,
liftM2 f x y = do
u <- x
v <- y
return (f u v)

So, it runs each of the computations you give it to get parameters for
f, and then returns the result of applying f to them.

In the ((->) e) monad, (which is often called the reader monad,
because it's isomorphic to it), running a computation just means
passing it the environment of type e. So in the reader monad, the
environment is passed to each of x and y, to get u and v respectively,
and then the value of (f u v) is returned. To translate, this is like:
liftM2 f x y e = f (x e) (y e)
of course, just for this particular monad.

In the reader monad, join has type (e -> e -> a) -> (e -> a), and it's
somewhat obvious what it must be doing -- it must take the value of
type e that it gets, and use it for both of the parameters of the
function it gets in order to produce a value of type a. You can see by
interpreting the do-notation that this is what happens in a curried
way. First x is passed the environment, then its result (the partially
applied function) is passed that environment.

So, for instance, join (*) 5 will result in 25.

The reader monad and functor instances are interesting, and worth
exploring. There are some rather interesting idioms which can be
obtained in this way. A nice one is:
ap (,) f
being the function (\x -> (x, f x)), which is handy for mapping across
lists of x-coordinates in making plots of functions.

Let us know if you need more detail about anything here. I sort of
skipped over some details in the presentation. (You might want to work
out exactly what return and bind do in this monad in order to
understand things completely -- you can work them out from the types
alone.)

Re: Cannot understand liftM2

> Hi All,
>
> I'm loving learning Haskell quite a bit.
> It is stretching my brain but in a delightfull way.
>
> I've googled, I've hoogled but I haven't found a clear explanation for
> what exactly liftM2 does in the context below.
>
> Using the cool lambdabot "pointless" utility I found out that:
>
> > \x -> snd(x) - fst(x)
>
> is the same as:
>
> > liftM2 (-) snd fst
>
> I like the elegance of this but I cannot reconcile it with its type. I
> can't understand it.
> I check the signature of liftM2 and I get:
>
> Prelude> :t liftM2
> Prelude> liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
>
> Can someone help me understand what's happening here ?
> What does a Monad have to do with a simple subtraction ?
> What is actually the "m" of my example ?
>
> I am sure if I get this I'll be another step closer to illumination ...

Re: Cannot understand liftM2

> The monad instance which is being used here is the instance for ((->)
> e) -- that is, functions from a fixed type e form a monad.
>
> So in this case:
> liftM2 :: (a1 -> a2 -> r) -> (e -> a1) -> (e -> a2) -> (e -> r)
> I bet you can guess what this does just by contemplating the type. (If
> it's not automatic, then it's good exercise) Now, why does it do that?
>
> Well, in general,
> liftM2 f x y = do
> u <- x
> v <- y
> return (f u v)
>
> So, it runs each of the computations you give it to get parameters for
> f, and then returns the result of applying f to them.

> [...]

> Let us know if you need more detail about anything here. I sort of
> skipped over some details in the presentation. (You might want to work
> out exactly what return and bind do in this monad in order to
> understand things completely -- you can work them out from the types
> alone.)

Your answer was very thorough and very clear. I am honestly overwhelmed :D.
Mind bending and rewarding.

Thank you very much.

I will be going through all your examples again slowly and with a
console on the side so that I am sure I grok as much as I can.

Yes, the '(->) c' monad is very handy. One way to think about it is
viewing 'f :: c -> a' as a set of 'a''s, indexed by the set of 'c''s. The
monad operations are then easily understood as doing things 'pointwise':
given some specific index (e.g. 'x'), you use this index to select the
appropriate value of every relevant indexed object (e.g. 'snd' and 'fst'),
and then apply the unlifted function (e.g. '(-)') to those.

Another way to write the above function is 'uncurry (flip (-))', or
'uncurry subtract'.

The m stands for ((->) e), that is like writing (e -> a1): a function
which will take an argument of type e and will return an argument of
type a1.

And so the above line has a signature that reads something like:
liftM2 will takes 3 arguments:
- a function (-) that takes two arguments and returns one result of type r.
- a function (fst) that takes one argument and returns one result.
- a function (snd) that takes one argument and returns one result.
- the result will be a certain function that will return the same type
r of the (-) function.
- Overall to this liftM2 I will actually pass two values of type a1
and a2 and will get a result of type r.

>From the type signature - correct me if I am wrong - I cannot actually
tell that liftM2 will apply (-) to the rest of the expression, I can
only make a guess. I mean I know it now that you showed me:

> liftM2 f x y = do
> u <- x
> v <- y
> return (f u v)

If this is correct and it all makes sense, my next question is:
- How do I know - or how does the interpreter know - that the "m" of
this example is an instance of type ((->) e) ?
- Is it always like that for liftM2 ? Or is it like that only because
I used the function (-) ?

I am trying to understand this bit by bit I am sorry if this is either
very basic and easy stuff, or if all I wrote is completely wrong and I
did not understand anything. :D Feedback welcome.

> Hi All, Hi Cale,
>
> Can you tell me if I understood things right ? Please see below ...
>
> On 12/11/06, Cale Gibbard <[hidden email]> wrote:
> > The monad instance which is being used here is the instance for ((->)
> > e) -- that is, functions from a fixed type e form a monad.
> >
> > So in this case:
> > liftM2 :: (a1 -> a2 -> r) -> (e -> a1) -> (e -> a2) -> (e -> r)
>
> > I bet you can guess what this does just by contemplating the type. (If
> > it's not automatic, then it's good exercise) Now, why does it do that?
>
> So the way I have to reason on the output I get from ghci is:
>
> Prelude> :t liftM2
> liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
>
> The m stands for ((->) e), that is like writing (e -> a1): a function
> which will take an argument of type e and will return an argument of
> type a1.
>
> And so the above line has a signature that reads something like:
> liftM2 will takes 3 arguments:
> - a function (-) that takes two arguments and returns one result of type r.
> - a function (fst) that takes one argument and returns one result.
> - a function (snd) that takes one argument and returns one result.
> - the result will be a certain function that will return the same type
> r of the (-) function.
> - Overall to this liftM2 I will actually pass two values of type a1
> and a2 and will get a result of type r.
>
> >From the type signature - correct me if I am wrong - I cannot actually
> tell that liftM2 will apply (-) to the rest of the expression, I can
> only make a guess. I mean I know it now that you showed me:
>
> > liftM2 f x y = do
> > u <- x
> > v <- y
> > return (f u v)
>
> If this is correct and it all makes sense, my next question is:
> - How do I know - or how does the interpreter know - that the "m" of
> this example is an instance of type ((->) e) ?
> - Is it always like that for liftM2 ? Or is it like that only because
> I used the function (-) ?
>
> I am trying to understand this bit by bit I am sorry if this is either
> very basic and easy stuff, or if all I wrote is completely wrong and I
> did not understand anything. :D Feedback welcome.
>
> Thanks again,
> Regards,
> Nick
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]> http://www.haskell.org/mailman/listinfo/haskell-cafe>

Re: Cannot understand liftM2

> So the way I have to reason on the output I get from ghci is:
>
> Prelude> :t liftM2
> liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
>
> The m stands for ((->) e), that is like writing (e -> a1): a function
> which will take an argument of type e and will return an argument of
> type a1.
>
> And so the above line has a signature that reads something like:
> liftM2 will takes 3 arguments:
> - a function (-) that takes two arguments and returns one result of
> type r.
> - a function (fst) that takes one argument and returns one result.
> - a function (snd) that takes one argument and returns one result.
> - the result will be a certain function that will return the same type
> r of the (-) function.
> - Overall to this liftM2 I will actually pass two values of type a1
> and a2 and will get a result of type r.
>
>> From the type signature - correct me if I am wrong - I cannot
>> actually
> tell that liftM2 will apply (-) to the rest of the expression, I can
> only make a guess. I mean I know it now that you showed me:
>
>> liftM2 f x y = do
>> u <- x
>> v <- y
>> return (f u v)
>
> If this is correct and it all makes sense, my next question is:
> - How do I know - or how does the interpreter know - that the "m" of
> this example is an instance of type ((->) e) ?
> - Is it always like that for liftM2 ? Or is it like that only because
> I used the function (-) ?
>
> I am trying to understand this bit by bit I am sorry if this is either
> very basic and easy stuff, or if all I wrote is completely wrong and I
> did not understand anything. :D Feedback welcome.

You can derive this yourself by assigning types to all parts of the
expression and working things out, i.e., doing the type inference
yourself. For example,

liftM2 :: T1 = T2 -> T3 -> T4 -> T5 because liftM2 consumes three
arguments. Furthermore, ghci gives you the type of liftM2, you know
the type of (-) and the types of snd and fst. Therefore,

Re: Cannot understand liftM2

On Tuesday 12 December 2006 08:57, Nicola Paolucci wrote:
> - How do I know - or how does the interpreter know - that the "m" of
> this example is an instance of type ((->) e) ?
> - Is it always like that for liftM2 ? Or is it like that only because
> I used the function (-) ?

It's the snd that forces the interpreter to infer the ((->) e) monad.

You can guess from the type of liftM2 that the (-) won't supply any more
information/constraints about m because m is is only mentioned in the snd and
fst parts.

If you use different monadic values, instead of snd and fst, then the m will
end up constrained to a different monad

Try these commands in GHCi to see what happens if you use something in the
Maybe monad:

Re: Cannot understand liftM2

On 12/11/06, Nicola Paolucci <[hidden email]> wrote:
> I am trying to understand this bit by bit I am sorry if this is either
> very basic and easy stuff, or if all I wrote is completely wrong and I
> did not understand anything. :D Feedback welcome.

Don't apologise - I, for one, am finding this discussion very
informative, and am reading the responses with interest.

Re: Cannot understand liftM2

> Using the cool lambdabot "pointless" utility I found out that:
>
>> \x -> snd(x) - fst(x)
>
> is the same as:
>
>> liftM2 (-) snd fst
>
> I like the elegance of this but I cannot reconcile it with its type. I
> can't understand it.
> I check the signature of liftM2 and I get:
>
> Prelude> :t liftM2
> Prelude> liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
>
> Can someone help me understand what's happening here ?
> What does a Monad have to do with a simple subtraction ?
> What is actually the "m" of my example ?

I think the simplest way to understand liftM and liftM2 are in
terms of their do-notation:

liftM op act = do
x <- act
return (op x)

that is: perform the action, bind the result to x, compute
(op x) and return that in the monad.

Similarly for liftM2:

liftM2 op act1 act2 = do
x <- act1
y <- act2
return (x `op` y)

in your case:

liftM2 (-) snd fst = do
x <- snd
y <- fst
return (x - y)

this is in the monad of functions that require an argument. Snd is
a function that takes an argument (a pair) and returns a value
(the 2nd member of the pair). Similarly fst is a fnction that takes
an argument. The whole do-block represents a function that takes
an argument (also a pair). As usual, do-blocks combine several
actions (in this case functions of one arguments) into a new action.

The description for this one is: the function that, when given
an argument (say "a") computes the snd item of the pair (snd a)
binds, computes the fst item of the pair (fst a) and subtracts the
two values (snd a - fst a).

The last step, which I leave as an exercise to the reader (I always wanted
to say that), is use the right hand side of the definition of (>>=) for lists
in the right hand side of liftM2 when applied to (,) and two lists.

You can see the type of the function (,) (yes, comma is a function!)
by executing, in ghci:

The last step, which I leave as an exercise to the reader (I always wanted to say that), is use the right hand side of the definition of (>>=) for lists in the right hand side of liftM2 when applied to (,) and two lists.

You can see the type of the function (,) (yes, comma is a function!) by executing, in ghci: