Right-regular grammars = Mathlib-regular languages #
This file proves that right-regular grammars (Type-3) generate exactly Mathlib's
Language.IsRegular languages, via explicit conversions in both directions.
Proof outline #
- RG → IsRegular.
NFA_of_RG gis an NFA over the finite state setOption g.FinNT(the nonterminals occurring ing, plus a sinknonemarking a completed word):A → aBgivesA —a→ B,A → agivesA —a→ none, and the accepting states arenonetogether with the nonterminals having anA → εrule. Forward/backward simulation againstRG_derivesyieldsNFA_of_RG_accepts : (NFA_of_RG g).accepts = RG_language g; Mathlib'sNFA.toDFAthen givesisRegular_of_is_RG. - IsRegular → RG.
RG_of_DFA Mreads a DFA's transitions as rulesq → a (M.step q a)and addsq → εfor accepting states; the invariantRG_of_DFA_derives_invgivesRG_of_DFA_language, henceis_RG_of_isRegular.
Main results #
is_RG_iff_isRegular— The Mathlib regular languages are equivalent to the right-regular languages.RG_eq_DFA— equality of the two language classes (over a finite alphabet).
References #
- Hopcroft, Motwani, Ullman. Introduction to Automata Theory, Languages, and Computation, 3rd ed. Section 3.3.
All nonterminals mentioned in a single RG rule.
Equations
- (RG_rule.cons A a B).nonterminals = [A, B]
- (RG_rule.single A a).nonterminals = [A]
- (RG_rule.epsilon A).nonterminals = [A]
Instances For
Finset of all nonterminals appearing in a grammar (including the initial symbol).
Equations
Instances For
theorem
RG_grammar.rule_nt_mem
{T : Type}
(g : RG_grammar T)
{r : RG_rule T g.nt}
(hr : r ∈ g.rules)
{n : g.nt}
(hn : n ∈ r.nonterminals)
:
@[reducible, inline]
Finite nonterminal subtype: only those nonterminals that appear in the grammar.
Instances For
theorem
RG_derives_form
{T : Type}
{g : RG_grammar T}
{A : g.nt}
{s : List (symbol T g.nt)}
(h : RG_derives g [symbol.nonterminal A] s)
:
(∃ (p : List T) (C : g.nt), s = List.map symbol.terminal p ++ [symbol.nonterminal C]) ∨ ∃ (p : List T), s = List.map symbol.terminal p
theorem
RG_transforms_of_terminal_nt
{T : Type}
{g : RG_grammar T}
{p : List T}
{C : g.nt}
{s : List (symbol T g.nt)}
(h : RG_transforms g (List.map symbol.terminal p ++ [symbol.nonterminal C]) s)
:
(∃ (a : T) (D : g.nt),
RG_rule.cons C a D ∈ g.rules ∧ s = List.map symbol.terminal p ++ [symbol.terminal a, symbol.nonterminal D]) ∨ (∃ (a : T), RG_rule.single C a ∈ g.rules ∧ s = List.map symbol.terminal p ++ [symbol.terminal a]) ∨ RG_rule.epsilon C ∈ g.rules ∧ s = List.map symbol.terminal p
theorem
RG_derives_snoc
{T : Type}
{g : RG_grammar T}
{A B : g.nt}
{w : List T}
(hw : w ≠ [])
(h : RG_derives g [symbol.nonterminal A] (List.map symbol.terminal w ++ [symbol.nonterminal B]))
:
∃ (C : g.nt),
RG_derives g [symbol.nonterminal A] (List.map symbol.terminal w.dropLast ++ [symbol.nonterminal C]) ∧ RG_rule.cons C (w.getLast hw) B ∈ g.rules
theorem
NFA_of_RG_some_backward
{T : Type}
{g : RG_grammar T}
(A : g.nt)
(hA : A ∈ g.nonterminalFinset)
(B : g.nt)
(hB : B ∈ g.nonterminalFinset)
(w : List T)
(h : RG_derives g [symbol.nonterminal A] (List.map symbol.terminal w ++ [symbol.nonterminal B]))
:
theorem
RG_generates_last_step
{T : Type}
{g : RG_grammar T}
{A : g.nt}
{w : List T}
(h : RG_derives g [symbol.nonterminal A] (List.map symbol.terminal w))
:
(∃ (C : g.nt) (p : List T),
RG_derives g [symbol.nonterminal A] (List.map symbol.terminal p ++ [symbol.nonterminal C]) ∧ RG_rule.epsilon C ∈ g.rules ∧ w = p) ∨ ∃ (C : g.nt) (p : List T) (a : T),
RG_derives g [symbol.nonterminal A] (List.map symbol.terminal p ++ [symbol.nonterminal C]) ∧ RG_rule.single C a ∈ g.rules ∧ w = p ++ [a]
theorem
RG_of_DFA_derives_simulation
{T : Type}
[Fintype T]
{σ : Type}
[Fintype σ]
(M : DFA T σ)
(q : σ)
(w : List T)
:
RG_derives (RG_of_DFA M) [symbol.nonterminal q] (List.map symbol.terminal w ++ [symbol.nonterminal (M.evalFrom q w)])
theorem
RG_of_DFA_no_single
{T : Type}
[Fintype T]
{σ : Type}
[Fintype σ]
(M : DFA T σ)
(q : σ)
(a : T)
:
RG_rule.single q a ∉ (RG_of_DFA M).rules
theorem
RG_of_DFA_derives_inv
{T : Type}
[Fintype T]
{σ : Type}
[Fintype σ]
(M : DFA T σ)
(q : σ)
(s : List (symbol T σ))
(h : RG_derives (RG_of_DFA M) [symbol.nonterminal q] s)
: