[Haskell-beginners] How does the type of "ap = liftM2 id" work?

Felipe Lessa felipe.lessa at gmail.com
Fri Aug 22 13:18:58 EDT 2008


On Fri, Aug 22, 2008 at 2:07 PM, Quergle Quergle <quergle at googlemail.com> wrote:
> Hello,
>
> I'm a bit perplexed by what it means to do something like "ap = liftM2 id"
>
>  liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
>  id :: a -> a
>  liftM2 id :: (Monad m) => m (a2 -> r) -> m a2 -> m r
>
> My intuitive understanding is that liftM2 is expecting a two-argument
> function as its first argument, so it seems a little unexpected to
> pass it "id", a one-argument function. And I can't really see how the
> type signature of "liftM2 id" is derived from "liftM2" and "id". Any
> help much appreciated!

Just remember that 'a1 -> a2 -> r' is equivalent to '(a1) -> (a2 ->
r)', and unifying this type with 'a -> a' gives

a1 = a2 -> r

So, liftM2's type becomes

((a2 -> r) -> a2 -> r) -> m (a2 -> r) -> m a2 -> m r

Is it clearer now?

HTH,

-- 
Felipe.


More information about the Beginners mailing list