optic Fold 类型的 Monoid r => Const r 如何推广到 Contravariant + Applicative?

How can Monoid r => Const r of the optic Fold type be generalized to Contravariant + Applicative?

从此Getter类型开始

type Getter s a = forall r. (a -> Const r a) -> s -> Const r s

我们需要一个额外的 Monoid 约束来获得 Fold:

type Fold s a = forall r. Monoid r => (a -> Const r a) -> s -> Const r s

但是Fold的实际和更一般的类型是

type Fold s a = forall f. (Contravariant f, Applicative f) => (a -> f a) -> s -> f s

我理解Contravariant是用来排除Identity从而保证我们只能得到值。但是不明白Monoid r怎么对应Applicative?当然,Const 也是一个 Applicative 但幺半群隐藏在哪里?

抱歉提出这个令人困惑的问题。

Monoid 实例隐藏在 Const rApplicative 实例的约束中。

Const r is only an instance of Applicative if r is an instance of Monoid:

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