似乎来自random-fu软件包的MonadRandom不是Functor,因为我遇到如下错误:
Could not deduce (Functor m) arising from a use of ‘_1’
from the context (MonadRandom m)
我尝试添加以下代码:
instance Functor MonadRandom where
fmap = liftM
instance Applicative MonadRandom where
pure = return
(<*>) = ap
但是我得到了错误:
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’
最佳答案
MonadRandom
是一个类,而不是类型为* -> *
的类型,例如Maybe
。通常,您会使用类似
instance MonadRandom m => Functor m where
fmap = liftM
instance MonadRandom m => Applicative m where
pure = return
(<*>) = ap
但是,在这种情况下,instances of
MonadRandom
已经是仿函数,因此现在的实例是模棱两可的!相反,您应该在函数中添加Functor
约束:yourFunction :: (MonadRandom m, Functor m) => ...
-- instead of yourFunction :: (MonadRandom m) => ...