本文介绍了具有monad身份的Monad变压器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 使用带有Identity monad的Monad变压器而不仅仅是使用变压器的标准版本有什么意义? 它更灵活吗? p> 解决方案 返回 mtl 1.0我们都有 newtype State sa = State {runState :: s - > (a,s)} 和 newtype StateT sma = StateT {runStateT :: s - >然而,这意味着任何必须实现像 MonadState 结束重复工作。 $ b $ c $ c>变形金刚(和现在已经不存在的 monads-fd 和 monads-tf )Ross Paterson决定采用更简单的方法,只提供后者并使用 Identity 作为基本monad。 这导致维护 mtl ,并删除了实现 State monad有两种不同方式的事实。不过,它确实使得 mtl 的内部更难教,因为你需要理解变形金刚版本出门,并没有获得简化版作为训练轮。 当旧的 mtl 退役和 monads-fd 变成 mtl 2.0,使用现有的转换器这个设计决定是继续进行的。 我个人喜欢为了教学目的至少有单独的单子,但是在辩论的另一边有更多的人。 What's the point in using a Monad transformer with the Identity monad rather than just using the "standard" version of the transformer?Is it more flexible? 解决方案 Back in mtl 1.0 we had bothnewtype State s a = State { runState :: s -> (a, s) }andnewtype StateT s m a = StateT { runStateT :: s -> m (a, s) }However, this meant anybody who had to implement instances for things like MonadState wound up duplicating effort.In transformers (and the now defunct monads-fd and monads-tf) Ross Paterson decided to use the simpler approach of only offering the latter and using Identity as the base monad.This led to reduced implementation effort in maintaining the mtl and removed the fact that there were two different ways to implement the State monad. It did, however, make the internals of the mtl harder to teach, because you need to understand the transformers versions right out of the gate and don't get the simplified version as training wheels.When the old mtl was retired and monads-fd became mtl 2.0, using the existing transformers this design decision was carried over.I personally liked having the separate simple monads for pedagogical purposes at least, but there were far more people on the other side of the debate. 这篇关于具有monad身份的Monad变压器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-14 15:00