
Hello all, can someone explain ap = liftM2 id to me? liftM2 wants a binary funcation liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r but id is unary. How does this work?

There's no such thing as a fixed arity of a function in Haskell. The type of id is
id :: a -> a
If you instantiate "a" with "b -> c", then it becomes
id :: (b -> c) -> b -> c
and it suddenly takes two arguments. In fact, it then behaves like function application. Try
id not True
or
not `id` True
and you'll see that it works as if you had used function application or $ instead. So ap lifts function application, and if it helps, you can think of it as instead being defined as
ap = liftM2 ($)
Cheers,
Andres
On Thu, Mar 26, 2015 at 9:18 PM, martin
Hello all,
can someone explain
ap = liftM2 id
to me? liftM2 wants a binary funcation
liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
but id is unary. How does this work? _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- Andres Löh, Haskell Consultant Well-Typed LLP, http://www.well-typed.com Registered in England & Wales, OC335890 250 Ice Wharf, 17 New Wharf Road, London N1 9RF, England

Am 03/26/2015 um 09:33 PM schrieb Andres Löh: I'll have to think about this. But other than that I am laughing my ass off after reading your paper about "Konjunktiv III". I can hardly type.
There's no such thing as a fixed arity of a function in Haskell. The type of id is
id :: a -> a
If you instantiate "a" with "b -> c", then it becomes
id :: (b -> c) -> b -> c
and it suddenly takes two arguments. In fact, it then behaves like function application. Try
id not True
or
not `id` True
and you'll see that it works as if you had used function application or $ instead. So ap lifts function application, and if it helps, you can think of it as instead being defined as
ap = liftM2 ($)
Cheers, Andres
On Thu, Mar 26, 2015 at 9:18 PM, martin
wrote: Hello all,
can someone explain
ap = liftM2 id
to me? liftM2 wants a binary funcation
liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
but id is unary. How does this work? _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Following up on Andres's explanation, we have ap f x = liftM2 id mf ma -- definition of ap = do { f <- mf; a <- ma; return (id f x) } -- definition of liftM2 = do { f <- mf; a <- ma; return (f x) } -- definition of id, f x y = (f x) y We can see that id applied to f gives f which is then applied to a.
participants (4)
-
Andres Löh
-
Andres Löh
-
martin
-
Rein Henrichs