
On May 9, 2009, at 15:31 , michael rice wrote:
Prelude> Just 3 >>= (1+)
<interactive>:1:0: No instance for (Num (Maybe b)) arising from a use of `it' at <interactive>:1:0-14 Possible fix: add an instance declaration for (Num (Maybe b)) In the first argument of `print', namely `it' In a stmt of a 'do' expression: print it Prelude>
(>>=) must be applied to a function that produces a result in the same monad: (>>=) :: Monad m => m a -> (a -> m b) -> m b That (a -> m b) in the middle is what's failing to typecheck. The error is a bit obtuse because ghci is trying hard to find a way to do what you want, so it assumes "m" is "(-> r)" (the functor/monad representing functions, also known as the Reader monad) which means "b" must be "Maybe x" for some x, but there are no instances of Maybe that are also instances of Num. If ghci had started by fixing m as Maybe (via the "Just 3") the types in the error message would have made more sensen; fixing m is more restrictive The function you're actually looking for is liftm or fmap: liftm :: Monad m => m a -> (a -> b) -> m b fmap :: Functor f => f a -> (a -> b) -> f b which "lifts" the (1+) up inside the monad/functor (which in this case is Maybe, as per the above). -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH