[英]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
也是一個應用,但躲避幺半群在哪里?
對不起這個令人困惑的問題。
Monoid
實例隱藏在Const r
的Applicative
實例的約束中。
如果r
是Monoid
的實例,則Const r
只是 Applicative
一個實例 :
instance Monoid m => Applicative (Const m) where
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.