Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make MonadRandom a Functor?

Tags:

haskell

monads

It seems that MonadRandom from the random-fu package is not a Functor, as I am getting errors like:

Could not deduce (Functor m) arising from a use of ‘_1’
from the context (MonadRandom m)

I've tried adding the following code:

instance Functor MonadRandom where
    fmap = liftM

instance Applicative MonadRandom where
    pure  = return
    (<*>) = ap

but I get the error:

The first argument of ‘Functor’ should have kind ‘* -> *’,
  but ‘MonadRandom’ has kind ‘(* -> *) -> Constraint’
In the instance declaration for ‘Functor MonadRandom’

The first argument of ‘Applicative’ should have kind ‘* -> *’,
  but ‘MonadRandom’ has kind ‘(* -> *) -> Constraint’
In the instance declaration for ‘Applicative MonadRandom’
like image 529
yong Avatar asked Sep 02 '14 11:09

yong


1 Answers

MonadRandom is a class, not a type with kind * -> *, like Maybe for example. Usually, you would use something like

instance MonadRandom m => Functor m where
    fmap = liftM

instance MonadRandom m => Applicative m where
    pure  = return
    (<*>) = ap

However, in this case the instances of MonadRandom are already functors, so now the instances are ambiguous! Instead, you should to add the Functor constraint at your function:

yourFunction :: (MonadRandom m, Functor m) => ...
-- instead of yourFunction :: (MonadRandom m) => ...
like image 68
Zeta Avatar answered Oct 30 '22 05:10

Zeta