Group: comp.lang.haskell · Group Profile
Author: Gareth McCaughanGareth McCaughan Date: Mar 31, 2008 16:50
"andrea" wrote:
> Given the three monad laws
>
>
> (return x) >>= f == f x
> m >>= return == m
> (m >>= f) >>= g == m >>= (\x -> f x >>= g)
>
> If I create a new monad, should I try to see if they are actually true??
> how could you prove that for any f or m?
>
> I was trying with the simple Maybe monad with some numerical examples,
> but with no luck.
>
> *Main Monad> let t = Just 10
> *Main Monad> (return t) >>= (+1)
So you're trying to apply
(return x) >>= f == f x
in the case where
x == Just 10
f == (+1)
which means the RHS is
(+1) (Just 10)
or
(Just 10) + 1
which doesn't work because (Just 10) is of type Maybe Integer.
As the machine helpfully tells you:
> :1:15:
> No instance for (Num (Maybe Integer))
> arising from a use of `+' at :1:15-18
> Possible fix: add an instance declaration for (Num (Maybe Integer))
> In the second argument of `(>>=)', namely `(+ 1)'
> In the expression: (return t) >>= (+ 1)
> In the definition of `it': it = (return t) >>= (+ 1)
If instead you take x == 10 then you should get the right types.
(In the Maybe monad, return == Just.)
--
Gareth McCaughan
.sig under construc
|