Documentation

CslibFrontier.Foundations.Computability.ContextFreeGrammar

Derivations from concatenations and from single nonterminals #

Mathlib's ContextFreeGrammar.Derives is closed under adding context on either side (Derives.append_left, Derives.append_right). This file proves the converse decomposition: a derivation from a concatenation u ++ v splits into independent derivations from u and from v, the basic fact behind parse trees and the CYK algorithm. It also describes the derivations from a terminal word (there are none but the trivial one) and from a single nonterminal (the trivial one, or a first step by a rule).

Splitting lemmas of this kind appear in unmerged Mathlib work: DerivesIn.append_split, for derivations of bounded length, in Loitzl and Dvořák's leanprover-community/mathlib4#19943 (closed), and the private special case head_tail_split (for s :: ss) in Mündler-Sasahara's open leanprover-community/mathlib4#33601.

Main results #

theorem ContextFreeGrammar.Derives.append_split {T : Type u_1} {g : ContextFreeGrammar T} {u v w : List (Symbol T g.NT)} (h : g.Derives (u ++ v) w) :
∃ (x : List (Symbol T g.NT)) (y : List (Symbol T g.NT)), g.Derives u x ∧ g.Derives v y ∧ w = x ++ y

A derivation from a concatenation splits into derivations from the two parts.

theorem ContextFreeGrammar.Derives.append {T : Type u_1} {g : ContextFreeGrammar T} {u v x y : List (Symbol T g.NT)} (hu : g.Derives u x) (hv : g.Derives v y) :
g.Derives (u ++ v) (x ++ y)

Derivations from two words combine into a derivation from their concatenation.

theorem ContextFreeGrammar.derives_append_iff {T : Type u_1} {g : ContextFreeGrammar T} {u v w : List (Symbol T g.NT)} :
g.Derives (u ++ v) w ↔ ∃ (x : List (Symbol T g.NT)) (y : List (Symbol T g.NT)), g.Derives u x ∧ g.Derives v y ∧ w = x ++ y

u ++ v derives w exactly when w splits into a word derived from u followed by a word derived from v.

A word of terminals derives only itself.

theorem ContextFreeGrammar.produces_nonterminal_iff {T : Type u_1} {g : ContextFreeGrammar T} {A : g.NT} {v : List (Symbol T g.NT)} :
g.Produces [Symbol.nonterminal A] v ↔ ∃ r ∈ g.rules, r.input = A ∧ v = r.output

A single nonterminal A rewrites in one step to v exactly when v is the output of a rule with input A.

A single nonterminal A derives w exactly when w = [A] or some rule with input A has an output deriving w.