10

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’
soulcheck
36.9k6 gold badges95 silver badges91 bronze badges
asked Sep 2, 2014 at 11:41
3
  • MonadRandom is a type-class, you can't make those instances of other type-classes. Commented Sep 2, 2014 at 11:48
  • You should add least add the function's signature, so that we know the context where you want to use _1. Commented Sep 2, 2014 at 11:51
  • @Xeo: actually you can, with {-# LANGUAGE KindSignatures #-} import GHC.Exts try e.g. class Foo (c :: * -> Constraint) instance Foo Num. Whether that makes any sense at all is a different question... Commented Sep 2, 2014 at 20:08

1 Answer 1

17

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) => ...
answered Sep 2, 2014 at 11:50
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.