是 Haskell 的 pure
功能同return
?
如果一个类型已经是 Applicative 的一个实例,我就可以让一个类型成为 Monad 的一个实例,对吗?所以我想知道 Applicative 的 pure
每次都可以与 Monad 的 return
互换?
有没有他们不一样的例子?
data HelloType a = HelloType { getValue :: a } deriving (Show, Eq)
instance Functor HelloType where
fmap f (HelloType y) = HelloType (f y)
instance Applicative HelloType where
(<*>) (HelloType f) (HelloType x) = HelloType (f x)
pure = HelloType
instance Monad HelloType where
(>>=) (HelloType x) f = f x
-- return = pure
return = HelloType
plus3 :: (Num a) => Maybe a -> HelloType (Maybe a)
plus3 (Just x) = HelloType (Just $ x + 3)
plus3 Nothing = HelloType Nothing
main= do
let withPure = pure (Just 3) >>= plus3 >>= plus3
withReturn = return (Just 3) >>= plus3 >>= plus3
print $ withPure == withReturn -- TRUE
最佳答案
Monad 实例的每个类型都必须有它的 return
等于 pure
.
特别是,由于 Applicative
成为 Monad
的父类(super class), return
不需要定义,因为默认情况下它被定义为 pure
的同义词: 见 the definition :
Furthermore, the Monad and Applicative operations should relate as follows:
pure = return
Minimal complete definition
(>>=)
请注意,最小定义只需要
>>=
, 不是 return
, 以及 pure = return
的要求(与所有此类“法律”一样,语言不能强制执行,但必须适用于所有“合理”实现,否则语义将不正确)。但是有些类型是 Applicative 而不是 Monad,因此有
pure
但没有 return
. ZipList
是传统的例子。
关于纯和返回不可互换的 Haskell 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54750784/