gpt4 book ai didi

haskell - 如何使 MonadRandom 成为仿函数?

转载 作者:行者123 更新时间:2023-12-04 03:11:28 26 4
gpt4 key购买 nike

似乎来自 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) => ...

关于haskell - 如何使 MonadRandom 成为仿函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25622571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com