我想让我的 monad 转换器成为一个实例MonadError
如果转换后的单子是一个实例。基本上我希望我的变压器的行为与内置变压器一样,例如有一个MonadError
实例为StateT
:
MonadError e m => MonadError e (StateT s m)
我尝试这样做:
instance MonadError e m => MonadError e (MyMonadT m)
但是 GHC 开始抱怨不可判定的实例,显然 MTL 库只是启用了不可判定的实例,但是有什么方法可以避免这种情况吗?或者在这种情况下就可以了,不会造成任何问题?
This is basically fine. UndecidableInstances
isn't all that scary; all it means is that the compiler could, instead of finding an instance, go into an infinite loop. That sounds pretty bad, until you realise that GHC actually has a limit to the number of steps it'll take to find an instance; nothing will go wrong unless you write a bad instance, and the error messages you get will generally make it very obvious what's gone wrong.1 Certainly it's a lot less scary than things like OverlappingInstances
(or worse, IncoherentInstances
).
它抱怨的原因是因为MonadError
具有函数依赖性m
to e
。这意味着选择m
决定什么e
必须是;即每个m
仅与一个相关联e
。对此(覆盖条件)的检查是保守的,因此很容易遇到这样的问题,您尝试“向下递归一个级别”来指定e
.
1 It'll list all the instances it looked at to try and find the one it's looking for, so you'll see a bunch of repeating lines. But usually you won't even run into trouble like this in the first place.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)