Context-Free Closure Under Prefix and Suffix #
This file proves that context-free languages are closed under the prefix and suffix operations. This strategy uses the construction of an explicit prefix grammar.
Main declarations #
CF_of_prefix_CF— CFLs are closed under prefixCF_of_suffix_CF— CFLs are closed under suffix (via reverse, prefix, reverse)
Strategy #
The prefix closure proof constructs an explicit grammar. Given a CF grammar g, we build
a "prefix grammar" whose nonterminals are Bool × g.nt, where (false, A) ("full mode")
behaves like the original A, and (true, A) ("prefix mode") can derive any prefix of
any word derivable from A.
To ensure soundness, we first filter the grammar to keep only "fully productive" rules (rules where every nonterminal in the RHS can derive some terminal string). This filtered grammar generates the same language as the original.
Productivity #
A nonterminal A in grammar g is productive if it can derive some terminal string.
Equations
- productive g A = ∃ (w : List T), CF_derives g [symbol.nonterminal A] (List.map symbol.terminal w)
Instances For
A rule is fully productive if the LHS nonterminal and every nonterminal in the RHS are productive.
Equations
- fullyProductiveRule g r = (productive g r.1 ∧ ∀ (B : g.nt), symbol.nonterminal B ∈ r.2 → productive g B)
Instances For
Productive Grammar #
Grammar with only fully productive rules (same nonterminal type, fewer rules).
Equations
- productiveGrammar g = { nt := g.nt, initial := g.initial, rules := List.filter (fun (r : g.nt × List (symbol T g.nt)) => decide (fullyProductiveRule g r)) g.rules }
Instances For
All rules in the productive grammar are fully productive (with respect to the productive grammar itself).
Prefix Grammar Construction #
Stepped Derivations (for induction on step count) #
Derivation in exactly n steps.
Equations
- CF_derives_in g 0 x✝¹ x✝ = (x✝¹ = x✝)
- CF_derives_in g n.succ x✝¹ x✝ = ∃ (w₂ : List (symbol T g.nt)), CF_transforms g x✝¹ w₂ ∧ CF_derives_in g n w₂ x✝
Instances For
Full Lifting #
Completeness: prefixLang (CF_language g) ⊆ CF_language (prefixGrammar g) #
Soundness: CF_language (prefixGrammar g) ⊆ prefixLang (CF_language g) #
This section assumes all rules in g are fully productive (every nonterminal appearing
in any rule can derive some terminal string).
Main Theorems #
The class of context-free languages is closed under taking prefixes.