如何使用( - >)Monad实例以及有关( - >)的混淆 [英] How to use (->) instances of Monad and confusion about (->)
问题描述
在不同的问题中,我发现了有关使用Monads的( - >)
实例的注释提示,为实现无点风格。
至于我,这有些太抽象了。好的,我在( - >)
上看到了Arrow实例,在我看来,( - >)
可以用在实例符号中,但不能用在类型声明中(这只会是另一个问题的原因)。
有任何使用的例子 - >)
作为Monad的实例?或者一个很好的链接?
对不起,如果这个问题可能已经在这里讨论过了,但是搜索( - >)
Monad实例为您提供了许多点击,因为几乎所有关于Haskell的问题都涉及( - >)
或Monad。
对于给定类型 r
,类型 r - >的功能。一个
可以被认为是使用类型为 r
的环境传递 a
的计算。给定两个函数 r - > a
和 a - > (r - > b)
,很容易想象当给定一个环境时(也可以是 r
类型))。
但是等等!所以我们可以为( - >)r
创建一个Monad实例,通过将 r
传递给 f <>来实现
f>>> g
code>和 g
。这就是( - >)r
的Monad实例。
要实际访问环境,可以使用 id :: r - > r
,您现在可以将其视为运行在 r
环境中的计算,并提供 r
。要创建本地子环境,您可以使用以下内容:
inLocalEnvironment ::(r - > r) - > (r→a)→> (r - > a)
inLocalEnvironment xform f = \ env - > f(xform env)
这种将环境传递给计算的模式可以随后查询并修改它本地不仅适用于( - >)r
monad,这就是为什么它被抽象为 MonadReader
类,使用比我在这里使用的更明智的名称:
http://hackage.haskell.org/packages/archive/mtl/2.0.1.0/doc/html/Control-Monad-Reader -Class.html
基本上它有两个实例:( - >)r
我们已经在这里看到了,> ReaderT rm
,它只是 newtype
围绕 r的包装 - > ma
,所以它和我在这里描述的( - >)r
monad是一样的,除了它在其他一些地方提供了计算,转化单子。
At different questions I've found hints in comments concerning using the (->)
instance of Monads e.g. for realizing point-free style.
As for me, this is a little too abstract. Ok, I've seen Arrow instances on (->)
and it seems to me, that (->)
can be used in instance notations but not in type declarations (that would alone be stuff for another question).
Has anyone examples using (->)
as instance of Monad? Or a good link?
Sorry if this question may already have been discussed here, but searching for "(->)
Monad instance" gives you many many hits as you can imagine ... since nearly every question about Haskell somewhere involves (->)
or "Monad".
For a given type r
, the function of type r -> a
can be thought of as a computation delivering an a
using an environment typed r
. Given two functions r -> a
and a -> (r -> b)
, it's easy to imagine that one can compose these when given an environment (again, of type r
).
But wait! That's exactly what monads are about!
So we can create an instance of Monad for (->) r
that implements f >>= g
by passing the r
to both f
and g
. This is what the Monad instance for (->) r
does.
To actually access the environment, you can use id :: r -> r
, which you can now think of as a computation running in an environment r
and delivering an r
. To create local sub-environments, you can use the following:
inLocalEnvironment :: (r -> r) -> (r -> a) -> (r -> a)
inLocalEnvironment xform f = \env -> f (xform env)
This pattern of having an environment passed to computations that can then query it and modify it locally is useful for not just the (->) r
monad, which is why it is abstracted into the MonadReader
class, using much more sensible names than what I've used here:
http://hackage.haskell.org/packages/archive/mtl/2.0.1.0/doc/html/Control-Monad-Reader-Class.html
Basically, it has two instances: (->) r
that we've seen here, and ReaderT r m
, which is just a newtype
wrapper around r -> m a
, so it's the same thing as the (->) r
monad I've described here, except it delivers computations in some other, transformed monad.
这篇关于如何使用( - >)Monad实例以及有关( - >)的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!