Langlib

Langlib.Classes.ContextFree.Decidability.Characterization

EncodedCFG characterizes the context-free languages #

This file proves that the encoded-grammar encoding EncodedCFG denotes exactly the context-free languages:

∀ L, L ∈ CF ↔ ∃ c : EncodedCFG T, contextFreeLanguageOf c = L

Soundness (every code denotes a context-free language): an EncodedCFG decodes — by construction — to a CF_grammar (toCFGrammar), so its language is is_CF_via_cfg, hence is_CF.

Completeness (every context-free language is encoded): a context-free language has a CF_grammar presentation with finitely many nonterminals (exists_fintype_nt); we reindex those nonterminals to Fin (n+1) and read the grammar off as an EncodedCFG. Reindexing the nonterminals along an equivalence preserves the language (cf_language_eq_of_rename), proved with the lifting machinery (lift_deri / sink_deri).

Renaming nonterminals preserves the language #

theorem ContextFree.EncodedCFG.cf_language_eq_of_rename {T : Type} {g₀ g : CF_grammar T} (e : g₀.nt g.nt) (hinit : g.initial = e g₀.initial) (hfwd : rg₀.rules, lift_rule (⇑e) r g.rules) (hbwd : rg.rules, r₀g₀.rules, lift_rule (⇑e) r₀ = r) :

Renaming the nonterminals of a context-free grammar along an equivalence preserves its language. Both directions go through the lifting machinery: lift_deri pushes a derivation forward along e, and sink_deri pulls one back along e.symm.

The encoding #

def ContextFree.EncodedCFG.encodeSymbol {T N : Type} {k : } (f : NFin (k + 1)) :
symbol T N T

Encode a symbol over Fin (k+1)-valued nonterminals into the raw ℕ ⊕ T format.

Equations
Instances For
    def ContextFree.EncodedCFG.encodeCFG {T : Type} {k : } (g : CF_grammar T) (e : g.nt Fin (k + 1)) :

    Encode a context-free grammar whose nonterminals are indexed by Fin (k+1) (via the equivalence e) as an EncodedCFG.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      theorem ContextFree.EncodedCFG.toNT_val {T : Type} {G : EncodedCFG T} (A : Fin G.ntCount) :
      G.toNT A = A

      toNT is the identity (up to Fin.val) on indices already in range.

      Soundness and completeness #

      Soundness: every code denotes a context-free language.

      The language of the encoding of a finite-nonterminal grammar is the grammar's language.

      Completeness: every context-free language is denoted by some code.

      EncodedCFG characterizes the context-free languages (the library's class CF).