Hi All:

How do you define a function of signature h :: M Int -> M Int -> M Int  so that h ( M x ) ( M y ) = M ( x + y ), but without unwrapping the  value from the monad?

This question is from the article "Trivial Monad" found at http://blog.sigfpe.com/2007/04/trivial-monad.html. The provided answer is


h x y = x >>= (\x -> g x y)

or equivalently ( in context of the article )


h :: M Int -> M Int -> M Int 
h x y = bind ( \x-> g x y ) x

where g is


g :: Int -> W Int -> W Int
g x y = y >>= (return . (+x))

for the monad:

data M a = M a deriving Show

Now I am a little confused, how can you put x in g if it takes an Int as first parameter but x is M Int?


Thanks!