Skip to content

Commit daf3673

Browse files
committed
feat(proofs): grammar structural metatheory — no-left-recursion (+ discrepancy), lexer, classification (Lean + Coq)
Follow-up to #102. Mechanizes the remaining grammar claims from grammar-proofs.md in priority order, with Lean↔Coq parity on the structural results. New, CI-gated (lean-proofs.yml / coq-proofs.yml), sorry/admit-free. docs/proofs/verification/WokeGrammarStructure.lean (Lean 4.30.0): - T2.1 no left recursion, WHOLE grammar: the begins-with-first-nonterminal graph as data + a strictly-decreasing rank ⇒ no nonterminal reaches itself. no_left_recursion depends on NO axioms. - Discrepancy found and machine-checked: the grammar as literally written DOES have one left-recursive production — the guard pattern `pattern = … | pattern "when" expression` — which the parser does not implement (the grammar's own NOTE). The prose §2.1 blanket "no left recursion" is corrected in-place. - T4.1 maximal munch + T4.2 keyword priority: a word scanner with general longest-match and keyword-on-exact-match lemmas + kernel-decided concretes (remember↦keyword, remembering↦one identifier, remember(↦keyword then stop). - T1.2 LL(1)=NO / T2.3 LL(2)=YES: the x vs x(…) FIRST/FIRST conflict and its two-token resolution (mirrors the parser's peek=='(' branch). docs/proofs/verification/WokeGrammarStructure.v (Coq 8.18.0): - Coq mirror of no-left-recursion (+ guard discrepancy) and the LL classification. Print Assumptions: axiom-free ("Closed under the global context"). Docs: GRAMMAR-PROOF-INVENTORY.md records P2/P3/P4 + the Coq mirror, and honestly FLAGS the §7 formal-language-theory claims (T7.1 not-regular, §7.3 CFL closure) as general REG/CFL facts needing an automata library (Mathlib) unavailable in the offline single-file setup — recorded rather than stubbed. grammar-proofs.md gets the in-place §2.1 correction and a §7 mechanization-status note. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015oyMquf4daB6hMhmqB1wAL
1 parent cd4d161 commit daf3673

6 files changed

Lines changed: 457 additions & 4 deletions

File tree

.github/workflows/coq-proofs.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ on:
1212
push:
1313
paths:
1414
- 'docs/proofs/verification/WokeLang.v'
15+
- 'docs/proofs/verification/WokeGrammarStructure.v'
1516
- '.github/workflows/coq-proofs.yml'
1617
pull_request:
1718
paths:
1819
- 'docs/proofs/verification/WokeLang.v'
20+
- 'docs/proofs/verification/WokeGrammarStructure.v'
1921
- '.github/workflows/coq-proofs.yml'
2022

2123
permissions:
@@ -40,3 +42,10 @@ jobs:
4042
cd docs/proofs/verification
4143
coqc WokeLang.v
4244
echo "✅ WokeLang.v verified"
45+
46+
- name: Verify WokeGrammarStructure.v (no-left-recursion + classification; axiom-free)
47+
run: |
48+
set -euo pipefail
49+
cd docs/proofs/verification
50+
coqc WokeGrammarStructure.v
51+
echo "✅ WokeGrammarStructure.v verified"

.github/workflows/lean-proofs.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,9 @@ jobs:
5050
set -euo pipefail
5151
lean docs/proofs/verification/WokeGrammar.lean
5252
echo "✅ WokeGrammar.lean verified"
53+
54+
- name: Verify WokeGrammarStructure.lean (no-left-recursion + lexer + classification)
55+
run: |
56+
set -euo pipefail
57+
lean docs/proofs/verification/WokeGrammarStructure.lean
58+
echo "✅ WokeGrammarStructure.lean verified"

docs/proofs/formal-semantics/grammar-proofs.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ logical_or = logical_and { "or" logical_and }
6464

6565
The `{ }` repetition prevents left-recursion. □
6666

67+
> **Correction (machine-checked, `WokeGrammarStructure.lean/.v`).** This blanket
68+
> claim is **inaccurate for the grammar as literally written**: the `pattern`
69+
> production has the guard alternative `pattern = … | pattern "when" expression`,
70+
> which is *directly left-recursive* (`guard_pattern_is_left_recursive`). No
71+
> left recursion holds for the **implemented** grammar (the parser does not
72+
> implement the guard alternative — see the grammar's own NOTE), which is what
73+
> `no_left_recursion` proves. The table above also omits `pattern`; with the
74+
> guard alternative it is the sole left-recursive production.
75+
6776
### 2.2 No Ambiguity
6877

6978
**Theorem 2.2:** The WokeLang grammar is unambiguous.
@@ -312,6 +321,15 @@ The binding power comparison ensures this:
312321

313322
## 7. Formal Language Theory
314323

324+
> **Mechanization status.** The §7 results (not-regular via pumping; CFL closure /
325+
> non-closure) are general facts about the *classes* REG/CFL, not about the
326+
> WokeLang grammar specifically, and need a general automata/grammar library
327+
> (Mathlib's `Computability.*`) that the repo's deliberately Mathlib-free, offline,
328+
> single-file prover setup does not provide. They are therefore **not machine-checked
329+
> here** — recorded honestly in `../verification/GRAMMAR-PROOF-INVENTORY.md` rather
330+
> than stubbed. The combinatorial kernel (unbounded balanced nesting) *is* exercised
331+
> concretely in `WokeGrammar.lean`.
332+
315333
### 7.1 Chomsky Hierarchy Position
316334

317335
```

docs/proofs/verification/GRAMMAR-PROOF-INVENTORY.md

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ reach here, with reason).
5757

5858
| # | Prose claim (grammar-proofs.md) | Faithful here? | Priority | Mechanization | Status |
5959
|---|---|---|---|---|---|
60-
| T2.1 | §2.1 No left recursion | ✅ yes (whole grammar) | **P2** | Grammar as data; `¬ LeftRecursive G` by decision/structural proof over **all** productions | MACHINE-CHECKED |
60+
| T2.1 | §2.1 No left recursion | ✅ yes (whole grammar) | **P2** | Grammar as data; rank-decreasing `Reach``¬ Reach a a` over **all** productions | MACHINE-CHECKED (**+ found a real discrepancy:** the guard pattern IS left-recursive — see below) |
6161
| T2.2 | §2.2 Unambiguity | ✅ for the core (undecidable in general) | **P1** | Determinism of the verified parser ⇒ unique parse: `Derives ts e₁ → Derives ts e₂ → e₁=e₂` for the expression core | MACHINE-CHECKED (core) |
6262
| T2.3 | §2.3 LL(2) | ◑ partial | **P4** | The one 2-token decision (`ident` vs `ident(`) witnessed; determinism of the core parser. Full LL(2) table for all 94 productions is not built | MACHINE-CHECKED (witness) |
6363
| T3.1 | §3.1 Parser soundness | ✅ for the core | **P1** | `parse ts = some (e, rest) → Derives (consumed) e` | MACHINE-CHECKED (core) |
@@ -69,7 +69,7 @@ reach here, with reason).
6969
| T6.3 | §6.3 Left-associativity || **P1** | `a-b-c ↦ (a-b)-c` from the strict-`<` model + general statement | MACHINE-CHECKED |
7070
| T1.1 | §1.1 CFG membership || **P4** | By construction (the grammar relation is a CFG) | MACHINE-CHECKED |
7171
| T1.2 | §1.1 LL(1) = ✗ || **P4** | Exhibit the FIRST/FIRST conflict (`primary → identifier` vs `identifier "(" …`) — a 1-lookahead non-determinism witness | MACHINE-CHECKED |
72-
| T7.3a | §7.3 CFL closed under ∪, ·, * | | **P5** | Explicit grammar constructions + language-equality proofs | MACHINE-CHECKED |
72+
| T7.3a | §7.3 CFL closed under ∪, ·, * | ⚠️ needs a general CFG/language module | **P5** | Explicit grammar constructions + language-equality proofs (standalone module, not a fact about *this* grammar) | FLAGGED |
7373
| T7.1 | §7.1 Not regular (pumping lemma) | ⚠️ no (needs automata/Mathlib offline) | **P6** | Mechanize the combinatorial kernel (the balanced-paren sublanguage `(ⁿ)ⁿ ⊆ L`); full DFA+pumping non-regularity needs Mathlib's `Computability` (network-fetched, unavailable) | FLAGGED + partial kernel |
7474
| T7.3b | §7.3 CFL **not** closed under ∩, ¬ | ⚠️ no (needs non-CFL-ness of `aⁿbⁿcⁿ`) | **P6** | Requires the pumping lemma *for CFLs* — same Mathlib gap | FLAGGED |
7575

@@ -128,7 +128,64 @@ cleaner than `WokeLang.lean`).
128128
relation) is the one honest extension still open on P1; the rejection battery
129129
covers the no-junk direction concretely.
130130

131+
## Landed — P2/P3/P4 (`WokeGrammarStructure.lean`, Lean 4.30.0)
132+
133+
CI-gated (`lean-proofs.yml`), `sorry`-free; `no_left_recursion` depends on **no
134+
axioms at all** (fully constructive).
135+
136+
- **T2.1 no left recursion (P2)** — the begins-with-first-nonterminal graph of the
137+
*whole* grammar is encoded as data with a strictly-decreasing rank; `Reach`
138+
(transitive first-symbol reachability) ⇒ `no_left_recursion : ∀ a, ¬ Reach a a`.
139+
**Discrepancy found & machine-checked:** the grammar *as literally written* has
140+
one left-recursive production — the guard pattern `pattern = … | pattern "when"
141+
expression` (`guard_pattern_is_left_recursive`). So the prose §2.1 blanket "no
142+
left recursion" is **false as written**; it holds only for the implemented
143+
subset (the parser does not implement the guard alternative — the grammar's own
144+
NOTE). This corrects the prose.
145+
- **T4.1 maximal munch / T4.2 keyword priority (P3)** — a word scanner taking the
146+
maximal identifier run + keyword-on-exact-match: `kw_priority` (general),
147+
`munch_maximal` (general longest-match: the remainder never extends the token),
148+
plus kernel-`decide`d concretes (`remember`↦keyword, `remembering`*one*
149+
identifier not `remember`+`ing`, `remember(`↦keyword then stop).
150+
- **T1.2 LL(1)=✗ / T2.3 LL(2)=✓ (P4)**`not_LL1` (the `x` vs `x(…)` FIRST/FIRST
151+
conflict shares FIRST₁ = identifier) and `LL2_separates` (two tokens decide,
152+
mirroring the parser's `peek == '('` branch). CFG membership holds by
153+
construction.
154+
155+
## Landed — Coq mirror (`WokeGrammarStructure.v`, Coq 8.18.0)
156+
157+
CI-gated (`coq-proofs.yml`); `Print Assumptions` reports **axiom-free** ("Closed
158+
under the global context"). Mirrors **P2 (no-left-recursion + guard discrepancy)**
159+
and **P4 (LL(1)✗/LL(2)✓)** in lockstep with Lean. (The Lean↔Coq relationship for
160+
the *parser metatheory* P1 follows the repo's existing pattern — Lean carries the
161+
universal `prefix_rt`/`completeness_rp` development; a Coq port of that is the
162+
scoped next step, exactly as `WokeLang.{lean,v}` are "complementary, not
163+
identical" per `AUDIT.md`.)
164+
165+
## Flagged — P5/P6 (§7 formal-language-theory claims), honestly not mechanized
166+
167+
These prose claims are **general facts about the *classes* `REG`/`CFL`**, not
168+
properties of the WokeLang grammar, and need a general automata/grammar library:
169+
170+
- **T7.1 not regular (pumping lemma)** and **§7.3 CFL non-closure under ∩ / ¬**
171+
require a formal DFA + pumping-lemma development (or the CFL pumping lemma for
172+
the non-closure direction). Mathlib has this (`Mathlib.Computability.*`), but
173+
the repo's proofs are deliberately **Mathlib-free and offline** (single-file
174+
`lean <file>`), and `lake`/reservoir fetching is blocked by egress policy here.
175+
Building DFA + pumping from scratch in core Lean is a large standalone module.
176+
- **§7.3 CFL *positive* closure (∪, ·, \*)** is mechanizable but needs a general
177+
CFG + language-semantics module (a derivation relation over arbitrary grammars
178+
and the three closure constructions) — also a standalone development, not a fact
179+
about *this* grammar.
180+
181+
Per the agreed "flag, don't fake" rule these are recorded here rather than stubbed.
182+
The combinatorial kernel of non-regularity (unbounded balanced nesting `(ⁿ … )ⁿ`)
183+
*is* exercised concretely — `WokeGrammar.lean` parses `((1))` and arbitrarily
184+
grouped inputs — but the full automata-theoretic theorems remain out of faithful
185+
reach in this setup.
186+
131187
## Status
132188

133-
P1 landed (above). P2–P6 + the Coq mirror proceed in priority order; the
134-
authoritative status is always "does the CI prover check go green."
189+
P1 (merged, #102) + P2/P3/P4 + the structural Coq mirror are machine-checked and
190+
CI-gated. P5/P6 are honestly flagged above. The authoritative status is always
191+
"does the CI prover check go green."

0 commit comments

Comments
 (0)