The simple following code
import Control.Monad
import Control.Monad.Random
psum :: (MonadRandom r) => Int -> r Double -> r Double
psum n x = fmap sum $ replicateM n x
yields the error:
Could not deduce (Functor r) arising from a use of `fmap'
from the context (MonadRandom r)
This is weird to me because of
class (Monad m) => MonadRandom m where ...
in 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 to import Control.Monad.Random.Class with no success.
Adding manually the Functor constraint on r works, but I find this quite ugly.
What am I missing here ?
Theoretically monads are functors, but sadly
Functoris not a superclass ofMonadfor no good reason.Instead of adding
Functor ryou can also useliftMinstead offmap.Edit: There really seems to be no good reason. The classes were introduced together in Haskell 1.3, and superclasses already existed and were used for
MonadPlusandMonadZero.