init::optics
init/src/optics.mo
Optics: Lenses, Prisms, and combinators Structural encoding — lenses as getter+setter pairs, prisms as match+construct.
pub type Lens S A {
mkLens (view : S -> A) (set : A -> S -> S)
}A lens focuses on a part A within a whole S.
pub def view (ln : Lens S A) (s : S) : AExtract the focused value from a structure.
pub def set (ln : Lens S A) (a : A) (s : S) : SReplace the focused value within a structure.
pub def over [Copy S] (ln : Lens S A) (g : A -> A) (s : S) : SModify the focused value within a structure.
[Copy S] because a lens reads the focus AND rebuilds the whole, so the
structure is mentioned twice. That is a real second ownership need, not
a borrow: the rebuilt value is a new S. (Reaching for [Copy S] is
honest where Copy (Lens S A) would not be — a lens is a pair of
closures, and function values are deliberately never Copy.)