[英]How can Monoid r => Const r of the optic Fold type be generalized to Contravariant + Applicative?
Starting from this Getter
type 从此
Getter
类型开始
type Getter s a = forall r. (a -> Const r a) -> s -> Const r s
we need an additional Monoid
constraint to obtain a Fold
: 我们需要一个额外的
Monoid
约束来获得Fold
:
type Fold s a = forall r. Monoid r => (a -> Const r a) -> s -> Const r s
But Fold
's actual and more general type is 但
Fold
的实际和更一般的类型是
type Fold s a = forall f. (Contravariant f, Applicative f) => (a -> f a) -> s -> f s
I understand that Contravariant
is used to exclude Identity
and thus ensure that we can only get the value. 我理解
Contravariant
用于排除Identity
,从而确保我们只能获得价值。 But I don't understand how Monoid r
corresponds to Applicative
? 但我不明白
Monoid r
如何与Applicative
相对应? Sure, Const
is also an Applicative but where is the monoid hidden? 当然,
Const
也是一个应用,但躲避幺半群在哪里?
Sorry for this confusing question. 对不起这个令人困惑的问题。
The Monoid
instance is hidden in the constraint of the Applicative
instance for Const r
. Monoid
实例隐藏在Const r
的Applicative
实例的约束中。
Const r
is only an instance of Applicative
if r
is an instance of Monoid
: 如果
r
是Monoid
的实例,则Const r
只是 Applicative
一个实例 :
instance Monoid m => Applicative (Const m) where
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.