“All about monads”中的MonadError部分 [英] MonadError section in "All about monads"

查看:85
本文介绍了“All about monads”中的MonadError部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



它声称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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆