init::optics

init/src/optics.mo

Optics: Lenses, Prisms, and combinators Structural encoding — lenses as getter+setter pairs, prisms as match+construct.

typeLens#
pub type Lens S A {
    mkLens (view : S -> A) (set : A -> S -> S)
}

A lens focuses on a part A within a whole S.

defview#
pub def view (ln : Lens S A) (s : S) : A

Extract the focused value from a structure.

defset#
pub def set (ln : Lens S A) (a : A) (s : S) : S

Replace the focused value within a structure.

defover#
pub def over [Copy S] (ln : Lens S A) (g : A -> A) (s : S) : S

Modify 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.)