leanprover / leanprover/cslib

Proposal: lawful lenses for verified state access

Open
#658 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lean
Stars
709
Forks
200
Avg merge
3d 6h
Merged PRs (30d)
45

Description

Problem

Verified computer-science developments often need structured state updates over records. When modeling interpreters, simulators, or incremental transformations, authors repeatedly write the same pattern: read a field from a state record, compute an updated value, and write it back while preserving unrelated fields. Without a shared abstraction, each project reimplements getters, setters, and the algebraic laws that make those updates composable and trustworthy.

Proposed minimal API

A small lawful lens core for verified state access (not category theory or profunctor optics):

structure Lens (S A : Type u) where
  get : S → A
  set : S → A → S

structure LawfulLens (S A : Type u) extends Lens S A where
  get_set : ∀ s a, get (set s a) = a
  set_get : ∀ s, set s (get s) = s
  set_set : ∀ s a b, set (set s a) b = set s b

Plus:

  • over — update the focused component with a function
  • compose — chain nested field access

Example: interpreter state

structure MachineState where
  pc : Nat
  memory : List Nat
  halted : Bool

A lawful lens for pc, a step function that increments pc when not halted, and proofs that step preserves memory and halted.

This pattern is exercised in lean-optics (Optics.Examples.MachineState) as an extraction candidate; see also docs/upstream/CSLIB_LENS_PROPOSAL.md in that repo.

Why CSLib

CSLib is the right venue for executable state semantics and verified transformations without tactic automation. The API should be small enough to review in one sitting and strong enough to support verified record updates in algorithms, semantics, and systems formalizations.

Explicitly out of scope (for this first PR)

  • Profunctor hierarchy and optic generalizations
  • Prisms, traversals, and other optic variants
  • Macros (lens!, field-syntax sugar)
  • optic_laws! and other tactic automation
  • Telemetry and benchmarks

Suggested placement

Cslib/Foundations/Data/Lens/ (or similar under Foundations), with one self-contained example module.

Mathlib

Not proposed for this slice. Mathlib optimizes for mathematical structure; the immediate need is a tiny, law-carrying API for executable state in CS developments.

Next step

Happy to open a draft PR with only the minimal API + MachineState example if maintainers agree with the framing. Feedback welcome on naming, placement, and whether composition laws should ship in v1.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading docs/upstream/CSLIB_LENS_PROPOSAL.md and the Optics.Examples.MachineState example in lean-optics, then compare the proposed placement under Cslib/Foundations/Data/Lens/. A complete contribution would need maintainer agreement on naming and composition scope, followed by the minimal lens API and one self-contained MachineState example with its stated laws.

Written by the indexing model from the issue text.

Assessment

Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.