[英]How do I make MonadRandom a Functor?
似乎来自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
但是 ,在这种情况下, MonadRandom
的实例已经是MonadRandom
函数,所以现在实例都不明确! 相反,您应该在函数中添加Functor
约束:
yourFunction :: (MonadRandom m, Functor m) => ...
-- instead of yourFunction :: (MonadRandom m) => ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.