Proposal: lawful lenses for verified state access

未关闭
#658 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Digger-Rockwell 已经在做这个了。

开始于 2026年11月18日。

  • #4 来自 @Digger-Rockwell —— 未关闭
  • #1248 来自 @Digger-Rockwell —— 未关闭

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
冷清
领域
tooling

调研方向

首先阅读 docs/upstream/CSLIB_LENS_PROPOSAL.md 以及 lean-optics 中的 Optics.Examples.MachineState 示例,然后比较提议放置在 Cslib/Foundations/Data/Lens/ 下的方案。完整的贡献需要维护者就命名和组合范围达成一致,随后实现最小的 lens API,并提供一个包含其所述定律的自包含 MachineState 示例。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Lean
星标
847
派生
234
平均合并
2 天 21 小时
30 天内合并 PR
49

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

leanprover/cslib 的其他 Issue

查看 leanprover/cslib 的全部 Issue

相似的 Issue

更多 DevTools Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。