“All about monads”中的MonadError部分 [英] MonadError section in "All about monads"
问题描述
它声称Error monad的定义为
class(Monad m)=> Monaderror e m | m - > e其中
throwError :: e - > m a
catchError :: m a - > (e - > m a) - > ma
其中一个实例是Either e。
instance MonadError(或e)其中
throwError =左
(左e)`catchError`处理程序=处理程序e
a`catchError` _ = a
这是我不明白的地方。
MonadError类带有两个类型参数,(或e)取1,这个
实例化是如何工作的?这是因为函数依赖关系吗?我仍然不明白。
AND!我在GHCi中修改了这段代码(带-XFunctionalDependencies,-XMultiParamTypeClasses)没有编译!什么是这段代码,无论如何?
这只是一个错字,实例应该是
instance MonadError e(e)其中
throwError = Left
(Left e)`catchError`处理程序=处理程序e
a`catchError` _ = a
包含两个类型参数,如您所愿。
e
是monad, e
是相应的错误类型。
I'm now really confused about the Error monad in which "All about monads" describes.
It claims the definition of Error monad as
class (Monad m) => Monaderror e m | m -> e where
throwError :: e -> m a
catchError :: m a -> (e -> m a) -> m a
And one of the instance is Either e.
instance MonadError (Either e) where
throwError = Left
(Left e) `catchError` handler = handler e
a `catchError` _ = a
Here is what I don't understand. The MonadError class take two type parameters, and (Either e) takes one, how is this instantiation work? Is this because the functional dependencies? I still don't get it.
AND! I've runed this code in GHCi(with -XFunctionalDependencies, -XMultiParamTypeClasses) didn't compile! What's is this piece of code, anyway?
It's just a typo, the instance should be
instance MonadError e (Either e) where
throwError = Left
(Left e) `catchError` handler = handler e
a `catchError` _ = a
with two type parameters, as you expected.
Either e
is the monad, and e
is the corresponding error type.
这篇关于“All about monads”中的MonadError部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!