← Back to Pull requests

feat(Automata): Thompson's construction #1148

Closed
unknown closed this pull request without merging on Oct 30, 2026 08:03 UTC
View on GitHub
unknown unknown commented on Oct 19, 2026 02:43 UTC
Author

This PR adds Thompson's construction, which converts regular expressions to ε-NAs: RegularExpression.toEpsilonNA r : εNA.FinAcc r.State α, with RegularExpression.language_toEpsilonNA : language r.toEpsilonNA = r.matches'. It then uses the construction to prove IsRegular.char, IsRegular.mul, IsRegular.kstar and IsRegular.regex.

  • LTS/Simulation.lean: IsSimulationMap (a map of states that sends transitions to transitions) and IsSimulationMap.sim_trace (it sends runs to runs), as a special case of simulation through IsSimulationMap.isSimulation.

  • LTS/Union.lean: the transitions and runs of LTS.unionSum (unionSum_tr_inl_inl, unionSum_mTr_inl_iff, …).

  • LTS/HasTau.lean: τSTr_iff_exists_mTr_replicate and sMTr_iff_exists_mTr_filter (a saturated run along a τ-free list is a plain run whose labels, once the τs are dropped, are that list).

  • Languages/LanguageHom.lean: Language.Hom.reduceOption, dropping the nones of a list as a language homomorphism.

  • EpsilonNA/Basic.lean: εNA.FinAcc.mem_language_iff (acceptance through plain runs) and DecidableEqTau (Option α).

  • EpsilonNA/ToSingleStartAccept.lean (formerly ToSingleAccept.lean): toSingleStartAccept adds a start state without incoming transitions and an accept state without outgoing transitions, on Bool ⊕ State. It replaces toSingleAccept.

  • NA/ToEpsilonNA.lean: NA.toEpsilonNA through LTS.mapHom and Language.Hom.reduceOption, so its ε-transitions are self-loops, and NA.FinAcc.toEpsilonNAFinAcc, with the same language.

  • EpsilonNA/ThompsonConstruction.lean: the building blocks and their languages:

  • - zero, epsilon and char;
    - sum, both automata side by side through LTS.unionSum;
    - union := (sum a b).toSingleStartAccept;
    - concat, with ε-transitions from the accepting states of the first automaton to the initial states of the second;
    - star, with a fresh hub state that is both initial and accepting.
  • EpsilonNA/OfRegularExpression.lean: RegularExpression.State (with a Finite instance), toEpsilonNA and language_toEpsilonNA.

  • Languages/RegularLanguage.lean: IsRegular.iff_epsilon_nfa. IsRegular.char, IsRegular.mul and IsRegular.kstar now come from the blocks above, without a case split on an empty alphabet, and IsRegular.regex from language_toEpsilonNA.
  • Relation to existing code: CSLib has concatenation and star for ε-free NA (NA.FinAcc.finConcat, NA.FinAcc.finLoop), which need [Inhabited Symbol]. Thompson's construction is a specific ε-NA instead: every r.toEpsilonNA has one initial state and at most one accepting state, and each constructor of r adds at most two states. NFA-simulation algorithms work on exactly this automaton. Following the discussion on Zulip, the union is built on toSingleStartAccept. This complements #887 (Kleene's algorithm, the converse direction), and #625 named the construction as a target.

    AI disclosure: written with Anthropic's Claude Code Opus 5.5, which drafted the Lean code under my direction. I chose the design, reviewed every definition and proof, and ran the checks.

    Diff: 414 lines (+310 / -104) across 5 files Reviewer: Lsonic233 Review duration: 16159m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.45) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 414 lines diff across 5 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.361
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1148"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 44.9% (MEDIUM) · Higher = larger blast radius
    44.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.4% · Safe to merge
    19.4%