Prove equivalence between AST-level and Expr-level regex semantics (`captures_iff_captures`)
#142 创建于 2025年11月27日
仓库指标
- 星标
- (109 个星标)
- PR 合并指标
- (PR 指标待抓取)
描述
(This issue is AI-generated an may contain an error. Please comment on the issue to discuss what we'll prove)
Difficulty: Intermediate
Background
Two levels of regex semantics are defined in the ast-semantics branch:
- AST-level (
Ast.Capturesincorrectness/RegexCorrectness/Syntax/Semantics.lean): closer to the surface pattern syntax, directly handlesrepeatwith min/max bounds. - Expr-level (
Expr.Capturesincorrectness/RegexCorrectness/Data/Expr/Semantics/Captures.lean): a lowered representation usingstar, more suitable as an NFA compilation source.
The conversion from AST to Expr is done by Ast.toRegexAux in regex/Regex/Syntax/Ast.lean, which uses applyRepetitions to translate (un)bounded repetitions.
Goal
Prove the equivalence statement
theorem captures_iff_captures {currentGroup it it' groups e} :
Ast.Captures currentGroup it it' groups e ↔ Expr.Captures it it' groups (e.toRegexAux currentGroup).2
so that the AST-level semantics of a pattern agrees with the Expr-level semantics.
Key Challenges
- Repetition translation
applyRepetitionsconvertsrepeat min max greedy einto various Expr combinations:starfor unbounded repetitions (max = .none)- nested
concat/alternateviarepeatConcatfor bounded cases
- One needs to show semantic equivalence between the AST-level
repeatrules and these Expr-level encodings, case by case.
- Group numbering consistency
- The AST semantics threads an explicit
currentGroup : Natparameter that is incremented as groups are traversed, while the Expr syntax embeds group tags directly. - It must be shown that
toRegexAuxproduces a consistent tagging scheme, and thatAst.Captures/Expr.Capturesagree on which groups are captured where.
CaptureGroupsstructure
- The inductive
CaptureGroupstype must line up between the two semantics:.groupshould agree on tag, start, and end positions..concatshould agree on how left/right capture groups are combined.
- Alternatively, we can prove a weaker theorem that the groups are equivalent under the "last-write-win" interpretation.
Proof Strategy
Induction on the semantics is expected to be a key proof technique. In particular, it may be necessary to identify and maintain suitable invariants (e.g. about currentGroup ranges) to carry the proof through the group, alternate, concat, and repeat cases.
Notes
- The exact definitions may need adjustment during proof development (especially around
repeatand group indexing). - In particular, the
currentGroupthreading and theCaptureGroupsconstructors might need small tweaks if the current formulation makes invariants hard to state or prove. - The example around lines 43–54 in
Semantics.leanserves as a sanity check thatmin > maxis impossible for the AST-levelrepeatsemantics.
Tasks
- Prove the forward direction (
Ast.Captures → Expr.Captures). - Prove the backward direction (
Expr.Captures → Ast.Captures). - Adjust or refine the definitions as needed to make the proofs go through cleanly (while preserving the intended semantics).