究竟什么是“有效的”意思 [英] What exactly does "effectful" mean
问题描述
我一次又一次读到术语 effectiveful ,但我仍然无法给出它的含义的明确定义。我假设正确的上下文是有效的 computations ,但我也看到了有效的 。 EM>。但是在Haskell中没有副作用(除了某种程度上IO)。仍然有各地有效的计算。
然后我读了monads用于创建有效的计算。我可以在 State
Monad的背景下理解这一点。但是我在 Maybe
monad中看不到任何副作用。一般来说,在我看来,包装类似于函数的东西的Monad比包装一个值的Monad更容易被看作是产生副作用。
当它来 Applicative
函子我更加迷失。我总是看到应用函数作为 map
具有多个参数的函数的一种方式。这里我看不到任何副作用。或者效果和与效果有区别吗?
IO
动作:当评估 IO
动作时,不会执行副作用,它们仅被执行当在 IO
值中规定的动作在 main
中执行时。 $ b $然而,当使用与构成计算相关的抽象(如应用函数和单子)时,可以方便地区分实际值和我们常称之为效应的休息。特别是,如果我们有类型 <$ c $的类型
f
c> * - > * ,然后在 fa
a
部分是价值 是效果。 我故意引用这些术语,因为没有准确的定义(据我所知),它只是一个口语定义。在某些情况下,根本没有值或多个值。例如,对于 Maybe
,效果是可能没有值(并且计算被中止),对于 []
效应是有多个(或零)值。对于更复杂的类型,这种区别可能会更加困难。
效果和值之间的区别并不取决于抽象。 Functor
, Applicative
和 Monad
只需给我们工具可以对它们进行操作( Functor
s允许修改里面的值, Applicative
s允许合并效果和 Monad
s允许效果依赖于以前的值)。但是在 Monad
的上下文中,创建一个关于正在发生的事情的心理图更容易一些,因为一次性动作可以看到以前计算的结果值如< / p>
(>> =):: ma - > (a - > m b) - > mb
运算符:第二个函数接收类型为 a
,所以我们可以想象先前的计算有一些效果,现在有它的结果值,我们可以做些什么。
Time and again I read the term effectful, but I am still unable to give a clear definition of what it means. I assume the correct context is effectful computations, but I've also seen the term effectful values)
I used to think that effectful means having side effects. But in Haskell there are no side-effects (except to some extent IO). Still there are effectful computations all over the place.
Then I read that monads are used to create effectful computations. I can somewhat understand this in the context of the State
Monad. But I fail to see any side-effect in the Maybe
monad. In general it seems to me, that Monads which wrap a function-like thing are easier to see as producing side-effects than Monads which just wrap a value.
When it comes to Applicative
functors I am even more lost. I always saw applicative functors as a way to map
a function with more than one argument. I cannot see any side-effect here. Or is there a difference between effectful and with effects?
A side effect is an observable interaction with its environment (apart from computing its result value). In Haskell, we try hard to avoid functions with such side effects. This even applies to IO
actions: when an IO
action is evaluated, no side effects are performed, they are executed only when the actions prescribed in the IO
value are executed within main
.
However, when working with abstractions that are related to composing computations, such as applicative functors and monads, it's convenient to somewhat distinguish between the actual value and the "rest", which we often call an "effect". In particular, if we have a type f
of kind * -> *
, then in f a
the a
part is "the value" and whatever "remains" is "the effect".
I intentionally quoted the terms, as there is no precise definition (as far as I know), it's merely a colloquial definition. In some cases there are no values at all, or multiple values. For example for Maybe
the "effect" is that there might be no value (and the computation is aborted), for []
the "effect" is that there are multiple (or zero) values. For more complex types this distinction can be even more difficult.
The distinction between "effects" and "values" doesn't really depend on the abstraction. Functor
, Applicative
and Monad
just give us tools what we can do with them (Functor
s allow to modify values inside, Applicative
s allow to combine effects and Monad
s allow effects to depend on the previous values). But in the context of Monad
s, it's somewhat easier to create a mental picture of what is going on, because a monadic action can "see" the result value of the previous computation, as witnessed by the
(>>=) :: m a -> (a -> m b) -> m b
operator: The second function receives a value of type a
, so we can imagine "the previous computation had some effect and now there is its result value with which we can do something".
这篇关于究竟什么是“有效的”意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!