[Haskell-beginners] Question in "Monads for Functional Programming"

Daniel Fischer daniel.is.fischer at googlemail.com
Fri Jan 14 01:47:52 CET 2011


On Friday 14 January 2011 01:10:39, Aggelos Mpimpoudis wrote:
> Hi all,
>
>
>
> I am referring to P. Wadler, "Monads for functional programming,"
> Advanced Functional Programming, pp. 24-52, 1995. I cannot understand in
> chapter 2.6, why a*k = k a.
>

Wadler uses (*) in that paper for what is (>>=) in Haskell (and unit for 
return).
In 2.6, he treats the identity monad (in Haskell, you have to wrap it in a 
newtype, Wadler uses a type synonym).

If partial application of type synonyms were possible in Haskell, the 
identity monad would be

type Id a = a

instance Monad Id where
  -- return :: Monad m => a -> m a
  return x = x
  -- (>>=) :: Monad m => m a -> (a -> m b) -> m b
  m >>= f = f m

For the unwrapped identity monad, you'd have

return :: a -> a {- = Id a -}
(>>=) :: a -> (a -> b) -> b

A function of type a -> (a -> b) -> b can basically only be the flipped 
application (ignoring seq and undefineds).

Actually (with the newtype), it's

newtype Identity a = Identity { runIdentity :: a }

instance Monad Identity where
  return x = Identity x
  -- f :: a -> Identity b
  (Identity x) >>= f = f x

>
>
> I cannot ask anyone else this time of night (3am here J), thus I posted
> here!
>

You can also ask on #haskell, it's always *not* 3 am somewhere ;)

HTH,
Daniel



More information about the Beginners mailing list