与 monad 不同的 monad-transformer 是什么?

发布于 2024-10-18 04:21:41 字数 1288 浏览 6 评论 0原文

这个问题确实说明了一切。我知道(Scala)Monad 看起来像这样:

trait Monad[M[_]] {
  def pure[A](a : A) : M[A]
  def bind[A, B](ma : M[A], f : A => M[B]) : M[B]
}

Monad Transformer 看起来像什么?它们有什么用?


EDIT. Consider the following REPL session: if a monad transformer somehow decorates a monad with reader capabilities (or vice versa)

假设我只想使用 Scalaz 中的 replicateM

scala> import scalaz._; import Scalaz._
import scalaz._
import Scalaz._

scala> some(4).replicateM[List](2)
res20: Option[List[Int]] = Some(List(4, 4))

现在让我们说,我需要从 File 读取 Int 值,而不是拥有 Option[Int]

scala> val f = (_ : java.io.File) => some(1)
f: (java.io.File) => Option[Int] = <function1>

所以,我可以将这个阅读器,好像它是一个 Monad?

scala> ReaderT(f).replicateM[List](2)
<console>:16: error: value replicateM is not a member of scalaz.ReaderT[Option,java.io.File,Int]
       ReaderT(f).replicateM[List](2)
                  ^

呃,不。

如果这一切看起来很愚蠢,我很抱歉,我只是想了解我包装 File =>; 是多么可爱的事情。 ReaderT 中的 Option[Int] 实际上可以买下我。

The question says it all, really. I know a (Scala) Monad looks like this:

trait Monad[M[_]] {
  def pure[A](a : A) : M[A]
  def bind[A, B](ma : M[A], f : A => M[B]) : M[B]
}

What does a Monad Transformer look like? And what are they used for?


EDIT. Consider the following REPL session: if a monad transformer somehow decorates a monad with reader capabilities (or vice versa)

Let's say I just want to use replicateM from Scalaz;

scala> import scalaz._; import Scalaz._
import scalaz._
import Scalaz._

scala> some(4).replicateM[List](2)
res20: Option[List[Int]] = Some(List(4, 4))

Now let's say, rather than having an Option[Int], I need to read an Int value from a File:

scala> val f = (_ : java.io.File) => some(1)
f: (java.io.File) => Option[Int] = <function1>

So, I can treat this reader, as if it was a Monad?

scala> ReaderT(f).replicateM[List](2)
<console>:16: error: value replicateM is not a member of scalaz.ReaderT[Option,java.io.File,Int]
       ReaderT(f).replicateM[List](2)
                  ^

Err, no.

Apologies if this all seems stupid, I'm just trying to understand what lovely goodness my wrapping the File => Option[Int] in a ReaderT can actually buy me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

血之狂魔 2024-10-25 04:21:41

Monad 转换器是类型函数,当应用于 monad 类型时,会生成一个新的 monad,它结合了两个组件的行为。

例如,在 xmonad 窗口管理器中,计算在内部运行:

newtype X a = X (ReaderT XConf (StateT XState IO) a)

即由 StateIO 组成的 Reader代码> 单子。

  • Reader 提供对只读存储器的访问
  • State 提供读写状态的形式
  • IO 允许任意外部效果

请注意,monad 变换因此更高- 等级类型。它们采用一元类型 (* -> *),并生成该类型的新类型。

一如既往,Haskell wiki 有一些有用的内容:

一切开始的地方:

Monad transformers are type functions that when applied to a monad type, generate a new monad, that combines the behavior of both components.

E.g. in the xmonad window manager, the computations run inside:

newtype X a = X (ReaderT XConf (StateT XState IO) a)

that is, a Reader composed with a State and an IO monad.

  • Reader gives access to read-only memory
  • State provides a form of read-write state
  • IO allows arbitrary external effects

Note that monad transforms are thus higher-rank types. They take a monadic type of kind (* -> *), and yield a new type of that kind.

As always, the Haskell wiki has some useful content:

Where it all began:

亢潮 2024-10-25 04:21:41

Monad Transformer 用于组合/扩展 monad(将一个 monad 的功能添加到另一个 monad)。例如,ReaderT(Reader Transformer)用Reader功能丰富给定的monad M(将给定的monad转换为Reader,保留M<的原始功能) /代码>)。

同时,Monad Transformers 是普通的 monad,具有 bindreturn 等操作。

您可以在 Scalaz 中找到 monad 转换器的示例 - 例如,阅读器状态 monad。

Monad Transformers are used for combining / extending monads (add capabilities of one monad to another). E.g., ReaderT (Reader Transformer) enriches given monad M with Reader capabilities (transforms given monad into Reader retaining original features of M).

At the same time, Monad Transformers are normal monads that have bind, return, and other operations.

You can find examples of monad transformers in Scalaz - e.g., for Reader and State monads.

苍暮颜 2024-10-25 04:21:41

我不认为 Reader 用于从文件中读取值。很确定它是用于读取配置值的。我认为它是全局变量、静态变量或动态/线程局部变量(在 Common Lisp 中称为“特殊变量”,有时在 Scheme 中称为“fluid-let”的 Fluid 变量)的替代方案。因此,请使用 Reader/ReaderT,而不是访问全局或动态变量,也不要将参数传递到可能需要访问某些配置选项的每个方法中。当某些非常深的代码突然需要访问新的配置选项时,这可能很有用。您可以从 main() 函数向下传递该选项,偷偷地访问全局或使用 Reader/ReaderT。

I don't think Reader is for reading values from a file. Pretty sure it's for reading values of configuration. I think of it as an alternative to global variables, statics, or dynamic/thread-local variables (called "special variables" in Common Lisp or sometimes fluide variables in Scheme with it's "fluid-let"). So, use Reader/ReaderT rather than access a global or dynamic variable and rather than pass parameters into each of your methods that may require access to some configuration option. This can be useful when some very deep piece of code suddenly requires access to a new configuration option. You can pass the option down from your main() function, sneakily access a global or use Reader/ReaderT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文