如何让MonadRandom成为Functor?

yon*_*ong 10 monads haskell

似乎来自random-fu包的MonadRandom不是Functor,因为我得到的错误如下:

Could not deduce (Functor m) arising from a use of ‘_1’
from the context (MonadRandom m)
Run Code Online (Sandbox Code Playgroud)

我尝试添加以下代码:

instance Functor MonadRandom where
    fmap = liftM

instance Applicative MonadRandom where
    pure  = return
    (<*>) = ap
Run Code Online (Sandbox Code Playgroud)

但我得到错误:

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’
Run Code Online (Sandbox Code Playgroud)

Zet*_*eta 17

MonadRandom是一类,而不是用一种类型* -> *,像Maybe例如.通常,你会使用类似的东西

instance MonadRandom m => Functor m where
    fmap = liftM

instance MonadRandom m => Applicative m where
    pure  = return
    (<*>) = ap
Run Code Online (Sandbox Code Playgroud)

但是,在这种情况下,实例MonadRandom已经是仿函数,所以现在实例是模棱两可的!相反,您应该Functor在函数中添加约束:

yourFunction :: (MonadRandom m, Functor m) => ...
-- instead of yourFunction :: (MonadRandom m) => ...
Run Code Online (Sandbox Code Playgroud)