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

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Fri Aug 22 13:24:26 EDT 2008


On 2008 Aug 22, at 13:07, Quergle Quergle wrote:

>  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

That's not quite how types work.  "a -> a", with nothing constraining  
the "a", can be *any* type... including a function type.  And because  
in Haskell a multiple-argument function is identical to a single- 
argument function returning a single-argument function, the type of  
"id" encompasses any number of arguments.

So, "liftM2 id" constrains "id" by usage (as you noted, it requires a  
2-argument function) to have the type "(a -> b) -> (a -> b)" instead  
of "a -> a", and now it works.

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH




More information about the Beginners mailing list