Words, captures, reads, and repeated substrings
DefinitionBackreferenceMatching_LanguagesAn expression can write the word matched by a subexpression into a named variable and read its current value later. All variables start with the empty word. A successful derivation records the letters it produces, the final variable values, and the total number of reads. An expression is -use if that total is at most on every complete derivation.
Ordinary regular languages use Mathlib's interpretation of regular expressions. The ABCBD condition requires a factorization with the same word at both positions. A normal form is a finite union of such languages and one ordinary regular language. The consecutive-repeat condition likewise requires copies of the same middle word.
import Mathlib.Computability.RegularExpressions
namespace BackreferenceMatching
abbrev Word (q : ℕ) := List (Fin q)
abbrev Reg (q : ℕ) := RegularExpression (Fin q)
abbrev Valuation (q : ℕ) := ℕ → Word q
inductive Expr (q : ℕ) where
| zero | eps
| letter (a : Fin q)
| alt (left right : Expr q)
| seq (left right : Expr q)
| star (body : Expr q)
| capture (x : ℕ) (body : Expr q)
| read (x : ℕ)
variable {q : ℕ}
def Expr.size : Expr q → ℕ
| .zero | .eps | .letter _ | .read _ => 1
| .star e | .capture _ e => e.size + 1
| .alt e f | .seq e f => e.size + f.size + 1
def regSize : Reg q → ℕ
| .zero | .epsilon | .char _ => 1
| .star e => regSize e + 1
| .plus e f | .comp e f => regSize e + regSize f + 1
def Expr.variables : Expr q → List ℕ
| .zero | .eps | .letter _ => []
| .read x => [x]
| .star e => e.variables
| .capture x e => x :: e.variables
| .alt e f | .seq e f => e.variables ++ f.variables
def Expr.variableCount (e : Expr q) : ℕ := e.variables.eraseDups.length
/-- The final index counts reads in this complete derivation. -/
inductive Run : Expr q → Valuation q → Word q → Valuation q → ℕ → Prop where
| eps (ρ : Valuation q) : Run .eps ρ [] ρ 0
| letter (ρ : Valuation q) (a : Fin q) : Run (.letter a) ρ [a] ρ 0
| left {e f : Expr q} {ρ σ : Valuation q} {u : Word q} {i : ℕ}
(h : Run e ρ u σ i) : Run (.alt e f) ρ u σ i
| right {e f : Expr q} {ρ σ : Valuation q} {u : Word q} {i : ℕ}
(h : Run f ρ u σ i) : Run (.alt e f) ρ u σ i
| seq {e f : Expr q} {ρ σ τ : Valuation q} {u v : Word q} {i j : ℕ}
(h₁ : Run e ρ u σ i) (h₂ : Run f σ v τ j) :
Run (.seq e f) ρ (u ++ v) τ (i + j)
| starZero (e : Expr q) (ρ : Valuation q) : Run (.star e) ρ [] ρ 0
| starStep {e : Expr q} {ρ σ τ : Valuation q} {u v : Word q} {i j : ℕ}
(h₁ : Run e ρ u σ i) (h₂ : Run (.star e) σ v τ j) :
Run (.star e) ρ (u ++ v) τ (i + j)
| capture {e : Expr q} {ρ σ : Valuation q} {u : Word q} {i : ℕ}
(x : ℕ) (h : Run e ρ u σ i) :
Run (.capture x e) ρ u (Function.update σ x u) i
| read (x : ℕ) (ρ : Valuation q) : Run (.read x) ρ (ρ x) ρ 1
def initial (q : ℕ) : Valuation q := fun _ => []
def Matches (e : Expr q) (w : Word q) : Prop :=
∃ σ i, Run e (initial q) w σ i
def BoundedUse (r : ℕ) (e : Expr q) : Prop :=
∀ w σ i, Run e (initial q) w σ i → i ≤ r
def UnboundedUse (e : Expr q) : Prop := ∀ r, ¬ BoundedUse r e
structure Branch (q : ℕ) where
first : Reg q
copy : Reg q
middle : Reg q
last : Reg q
def Branch.size (p : Branch q) : ℕ :=
regSize p.first + regSize p.copy + regSize p.middle + regSize p.last
def ABCBD (p : Branch q) (w : Word q) : Prop :=
∃ a b c d, a ∈ p.first.matches' ∧ b ∈ p.copy.matches' ∧
c ∈ p.middle.matches' ∧ d ∈ p.last.matches' ∧ w = a ++ b ++ c ++ b ++ d
structure NormalForm (q : ℕ) where
regular : Reg q
branches : List (Branch q)
def NormalForm.accepts (u : NormalForm q) (w : Word q) : Prop :=
w ∈ u.regular.matches' ∨ ∃ p ∈ u.branches, ABCBD p w
def RepeatedBlock (x y z : Reg q) (α : ℕ) (w : Word q) : Prop :=
∃ a b c, a ∈ x.matches' ∧ b ∈ y.matches' ∧ c ∈ z.matches' ∧
w = a ++ (List.replicate α b).flatten ++ c
end BackreferenceMatchingRead-back
What the Lean code literally says, in plain math · GPT-6
Expressions and words
For , words are finite lists of natural numbers below . Only the empty word exists for . Ordinary regular expressions have empty language, empty word, letter, union, concatenation and star. Extended expressions add capture and read with natural-number names. Both sizes count syntax-tree nodes. The name list records each capture before its body, each read when visited, and the left operand before the right. The variable count is the number of distinct names.
Runs and reads
A run records starting and final valuations, the matched word and a read count. Valuations assign words to all names. The empty language has no run. Empty-word and letter runs match their indicated words with unchanged valuation and zero reads. Union chooses either operand. Concatenation and finite star iteration thread valuations, concatenate words and add read counts. Zero iterations match the empty word with unchanged valuation and zero reads. Capture runs its body and then stores the whole matched word under its name in the resulting valuation. A read matches the current value, preserves the valuation and counts once, even for an empty value.
An expression matches exactly when a run on starts with every name empty, for some final valuation and read count. A bound requires every such complete run to use at most reads. This holds vacuously without a run. Unbounded use means every natural bound fails.
Normal forms and repetition
A branch has size equal to its four regular expressions’ total node count. Let denote the language of . The branch accepts exactly the words with , , and . A normal form's language is the union of its regular language and listed branch languages. For regular expressions and , repetition means with , and . Even at , such a must exist.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.