Langlib

Langlib.Classes.ContextFree.Closure.PrefixBonus

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 #

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 #

def productive {T : Type} (g : CF_grammar T) (A : g.nt) :

A nonterminal A in grammar g is productive if it can derive some terminal string.

Equations
Instances For
    def fullyProductiveRule {T : Type} (g : CF_grammar T) (r : g.nt × List (symbol T g.nt)) :

    A rule is fully productive if the LHS nonterminal and every nonterminal in the RHS are productive.

    Equations
    Instances For
      theorem productive_of_mem_derives_terminal {T : Type} {g : CF_grammar T} {s : List (symbol T g.nt)} {w : List T} (hs : CF_derives g s (List.map symbol.terminal w)) {B : g.nt} (hB : symbol.nonterminal B s) :

      Productive Grammar #

      noncomputable def productiveGrammar {T : Type} (g : CF_grammar T) :

      Grammar with only fully productive rules (same nonterminal type, fewer rules).

      Equations
      Instances For
        theorem CF_derives_mono {T : Type} {g : CF_grammar T} {rules₁ rules₂ : List (g.nt × List (symbol T g.nt))} (hsub : rrules₁, r rules₂) {s₁ s₂ : List (symbol T g.nt)} (h : CF_derives { nt := g.nt, initial := g.initial, rules := rules₁ } s₁ s₂) :
        CF_derives { nt := g.nt, initial := g.initial, rules := rules₂ } s₁ s₂

        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) #

        def CF_derives_in {T : Type} (g : CF_grammar T) :
        List (symbol T g.nt)List (symbol T g.nt)Prop

        Derivation in exactly n steps.

        Equations
        Instances For
          theorem derives_of_derives_in {T : Type} {g : CF_grammar T} {n : } {w₁ w₂ : List (symbol T g.nt)} :
          CF_derives_in g n w₁ w₂CF_derives g w₁ w₂
          theorem derives_in_of_derives {T : Type} {g : CF_grammar T} {w₁ w₂ : List (symbol T g.nt)} :
          CF_derives g w₁ w₂∃ (n : ), CF_derives_in g n w₁ w₂
          theorem transform_in_append {T : Type} {g : CF_grammar T} {s₁ s₂ w : List (symbol T g.nt)} (h : CF_transforms g (s₁ ++ s₂) w) :
          (∃ (s₁' : List (symbol T g.nt)), CF_transforms g s₁ s₁' w = s₁' ++ s₂) ∃ (s₂' : List (symbol T g.nt)), CF_transforms g s₂ s₂' w = s₁ ++ s₂'
          theorem head_tail_split_in {T : Type} {g : CF_grammar T} (n : ) (s : symbol T g.nt) (ss x : List (symbol T g.nt)) (h : CF_derives_in g n (s :: ss) x) :
          ∃ (n₁ : ) (n₂ : ), n₁ + n₂ n ∃ (u : List (symbol T g.nt)) (v : List (symbol T g.nt)), CF_derives_in g n₁ [s] u CF_derives_in g n₂ ss v u ++ v = x

          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 #

          theorem CF_of_prefix_CF {T : Type} (L : Language T) :

          The class of context-free languages is closed under taking prefixes.