chr*_*eyn 3 monads haskell applicative
Haskell的pure功能是一样的return吗?
如果Monad已经是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
Run Code Online (Sandbox Code Playgroud)
作为Monad实例的每个类型都必须return等于pure.
特别是,由于Applicative是一个超类Monad,return不需要定义,因为默认情况下它被定义为以下的同义词pure:参见定义:
此外,Monad和Applicative操作应涉及如下:
pure = return
最小的完整定义
Run Code Online (Sandbox Code Playgroud)(>>=)
请注意,最小定义仅需要>>=(而不是)return要求pure = return(与所有此类"法律"一样,不能由语言强制执行,但必须适用于所有"理智"实现,否则语义将不正确).
但有些类型是适用但不是Monad,因此有一个pure但没有return.ZipList是传统的例子.