在单子变换器中,我们有
instance (Monad m, Monoid e) => MonadPlus (ExceptT e m)
在 可扩展效果 中,不存在这样的东西。
instance (Monoid e) => MonadPlus (Eff (Exc e :> r))
我已经尝试过实现它,但都失败了。这是我目前的进展:
instance (Monoid e) => MonadPlus (Eff (Exc e :> r)) where
mzero = throwExc mempty
a `mplus` b = undefined $ do
resultA <- runExc a
case resultA of
Left l -> runExc b
Right r -> return $ Right r
有2个问题:
for
mzero
, GHC complains as follows:Could not deduce (Monoid e0) arising from a use of ‘mempty’ from the context (Monad (Eff (Exc e :> r)), Monoid e)
Why doesn't GHC match
e0
withe
?Answer (provided in comment): turn on
ScopedTypeVariables
for
mplus
,undefined
should be replaced with the inverse function ofrunExc
, but I can't find it in the API of extensible-effects. Did I miss something ?
Member (Exc e) r => Eff r a
中写入 a <|> b
,意思是:
- 尝试
a
- 如果
a
抛出ea
,尝试b
- 如果
b
抛出eb
,则抛出mappend ea eb
这需要一个 Alternative
实例,这就是我首先尝试实现 MonadPlus
实例的原因。
注意: 我使用的是 GHC 7.8.3。
非常感谢您的帮助。
ScopedTypeVariables
。 - user2407038