具有monad身份的Monad变压器 [英] Monad transformers with identity 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) }
and
newtype 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 defunctmonads-fd
andmonads-tf
) Ross Paterson decided to use the simpler approach of only offering the latter and usingIdentity
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 theState
monad. It did, however, make the internals of themtl
harder to teach, because you need to understand thetransformers
versions right out of the gate and don't get the simplified version as training wheels.When the old
mtl
was retired andmonads-fd
becamemtl
2.0, using the existingtransformers
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变压器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!