简体   繁体   English

如何将Monoid r =>光学折叠类型的Const r推广到Contravariant + Applicative?

[英]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 rApplicative实例的约束中。

Const r is only an instance of Applicative if r is an instance of Monoid : 如果rMonoid的实例,则Const r 只是 Applicative一个实例

instance Monoid m => Applicative (Const m) where
   ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM