Failed to deduce (Functor r) from (MonadRandom r)

Simple following code

import Control.Monad import Control.Monad.Random psum :: (MonadRandom r) => Int -> r Double -> r Double psum nx = fmap sum $ replicateM nx 

gives an error:

 Could not deduce (Functor r) arising from a use of `fmap' from the context (MonadRandom r) 

This is strange to me because of

 class (Monad m) => MonadRandom m where ... 

in the Control.Monad.Random.Class source file, and since monads are functors, GHC should have deduced that r is a functor in my context. I also tried importing Control.Monad.Random.Class without success.

Manually adding Functor constraints on r works, but I find it pretty ugly.

What am I missing here?

+4
source share
1 answer

Theoretically, monads are functors, but, unfortunately, Functor not a Monad superclass for no good reason.

Instead of adding Functor r you can also use liftM instead of fmap .

Edit: Actually, there seems to be no good reason. Classes were merged in Haskell 1.3 , and superclasses already existed and were used for MonadPlus and MonadZero .

+9
source

All Articles