我现在对于关于“All about monads”所描述的Error monad感到困惑。
它声称Error monad的定义为
class (Monad m) => Monaderror e m | m -> e where
throwError :: e -> m a
catchError :: m a -> (e -> m a) -> m a
其中一个例子是E e。
instance MonadError (Either e) where
throwError = Left
(Left e) `catchError` handler = handler e
a `catchError` _ = a
这是我不明白的。 MonadError类有两个类型参数,(E)取一个,这是怎么回事 实例化工作?这是因为功能依赖吗?我仍然没有得到它。
和!我已经破了GHCi中的这段代码(带-XFunctionalDependencies,-XMultiParamTypeClasses)没编译!无论如何,这段代码是什么?
答案 0 :(得分:8)
这只是一个错字,实例应该是
instance MonadError e (Either e) where
throwError = Left
(Left e) `catchError` handler = handler e
a `catchError` _ = a
有两个类型参数,如您所料。
Either e
是monad,e
是相应的错误类型。