假设我们有两个monad,m和m'.现在,假设我们有变量,
-- in real problems, the restriction is some subclass MyMonad, so don't worry
-- if it's the case here that mx and f must essentially be pure.
mx :: Monad m'' => m'' a
f :: Monad m'' => a -> m'' b
Run Code Online (Sandbox Code Playgroud)
有没有办法创建类似于产品的东西m x m'?我知道这对Arrows来说是可能的,但对monad来说似乎更复杂(不可能?),尤其是在尝试编写mx >>= f应该做的事情时.
要看到这个,请定义
data ProdM a = ProdM (m a) (m' a)
instance Monad ProdM where
return x = ProdM (return x) (return x)
Run Code Online (Sandbox Code Playgroud)
但现在,当我们定义时mx >>= f,不清楚mx传递给哪个值f,
(ProdM mx mx') >>= f
{- result 1 -} = mx >>= f
{- result 2 -} = mx' >>= f
Run Code Online (Sandbox Code Playgroud)
我希望(mx >>= f) :: ProdM与之同构((mx >>= f) :: m) x ((mx >>= f) :: m').
ehi*_*ird 18
是的,这种类型是monad.关键是简单地将两个结果传递给f,并且只保留结果中的匹配字段.也就是说,我们保留传递mx结果的结果中的第一个元素,以及传递结果的结果中的第二个元素mx'.实例看起来像这样:
instance (Monad m, Monad m') => Monad (ProdM m m') where
return a = ProdM (return a) (return a)
ProdM mx mx' >>= f = ProdM (mx >>= fstProd . f) (mx' >>= sndProd . f)
where fstProd (ProdM my _) = my
sndProd (ProdM _ my') = my'
Run Code Online (Sandbox Code Playgroud)
ProdM可以在名称下的monad-products包中找到Product.
| 归档时间: |
|
| 查看次数: |
526 次 |
| 最近记录: |