【发布时间】:2025-11-22 09:50:02
【问题描述】:
在“向您学习 Haskell for Great Good!”中作者声称Applicative IO 实例是这样实现的:
instance Applicative IO where
pure = return
a <*> b = do
f <- a
x <- b
return (f x)
我可能错了,但似乎return 和do 特定的构造(一些含糖绑定(>>=))都来自Monad IO。假设这是正确的,我的实际问题是:
为什么Applicative IO 的实现依赖于Monad IO 函数/组合器?
Applicative不如比Monad更强大的概念吗?
编辑(一些说明):
这个实现违背了我的直觉,因为根据 Typeclassopedia 文章,给定类型必须是 Applicative before 它可以被设为 Monad(或者它应该应该 em>理论上)。
【问题讨论】:
-
@monadic:不,
liftM是fmap,ap是(<*>)。你可能会想到liftM2,即liftM2 f x y=f <$> x <*> y。 -
@camccann 是的,抱歉,我没想到 ^_^
标签: haskell monads applicative