如何使用 (->) Monad 实例和关于 (->) 的混淆 [英] How to use (->) instances of Monad and confusion about (->)
问题描述
在不同的问题中,我在关于使用 (->)
实例的 Monads 实例的评论中找到了提示,例如用于实现无点样式.
At different questions I've found hints in comments concerning using the (->)
instance of Monads e.g. for realizing point-free style.
对我来说,这有点太抽象了.好的,我在 (->)
上看到了 Arrow 实例,在我看来,(->)
可以用在实例符号中,但不能用在类型中声明(这本身就是另一个问题的内容).
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).
有没有使用 (->)
作为 Monad 实例的例子?或者好的链接?
Has anyone examples using (->)
as instance of Monad? Or a good link?
抱歉,如果这里可能已经讨论过这个问题,但是搜索 "(->)
Monad instance" 会给您带来很多您可以想象的点击次数... 因为几乎所有关于 Haskell 的问题都涉及 (->)
或Monad".
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".
推荐答案
对于给定类型 r
,r 类型的函数 ->a
可以被认为是使用类型为 r
的环境传递 a
的计算.给定两个函数 r ->a
和 a ->(r -> b)
,很容易想象在给定环境(再次,r
类型)时可以组合这些.
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
).
等等!这正是 monad 的意义所在!
But wait! That's exactly what monads are about!
所以我们可以为 (->) r
创建一个 Monad 实例,它通过传递 r
到 f
和 g
.这就是 (->) r
的 Monad 实例所做的.
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.
要实际访问环境,可以使用 id :: r ->r
,您现在可以将其视为在环境 r
中运行并提供 r
的计算.要创建本地子环境,您可以使用以下内容:
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)
这种将环境传递给计算然后可以在本地查询和修改它的模式不仅对 (->) r
monad 有用,这就是为什么它被抽象为MonadReader
类,使用比我在这里使用的更合理的名称:
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
基本上,它有两个实例:我们在这里看到的 (->) r
和 ReaderT rm
,它只是一个 newtype
包裹 r ->m a
,所以它与我在这里描述的 (->) r
monad 相同,除了它在其他一些转换后的 monad 中提供计算.
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屋!