Langlib

Langlib.Classes.ContextFree.Basics.FiniteNT

Every context-free grammar has an equivalent one with finite nonterminals #

Since a ContextFreeGrammar stores its rules in a Finset, only finitely many nonterminals can ever appear in a derivation. We restrict the nonterminal type to those that actually occur in the grammar, producing an equivalent grammar whose nonterminal type is Fintype.

Main results #

Used nonterminals #

noncomputable def ContextFreeGrammar.usedNT {T : Type} (g : ContextFreeGrammar T) :

The finite set of nonterminals occurring in the grammar (rules + start symbol).

Equations
  • One or more equations did not get rendered due to their size.
Instances For

    The restricted nonterminal type: nonterminals actually appearing in the grammar.

    Equations
    Instances For

      Symbol embedding and restriction #

      Embed a symbol over the restricted nonterminals back to the original type.

      Equations
      Instances For

        Embed a word over restricted nonterminals back to the original type.

        Equations
        Instances For
          def ContextFreeGrammar.restrictOneSymbol {T : Type} (g : ContextFreeGrammar T) (s : Symbol T g.NT) (h : ∀ (n : g.NT), s = Symbol.nonterminal nn g.usedNT) :

          Restrict a single symbol, given that any nonterminal in it is used.

          Equations
          Instances For

            All-used predicate #

            All nonterminals in a word are used.

            Equations
            Instances For
              theorem ContextFreeGrammar.allUsed_cons {T : Type} (g : ContextFreeGrammar T) {s : Symbol T g.NT} {w : List (Symbol T g.NT)} :
              g.AllUsed (s :: w) (∀ (n : g.NT), s = Symbol.nonterminal nn g.usedNT) g.AllUsed w
              theorem ContextFreeGrammar.allUsed_produces {T : Type} (g : ContextFreeGrammar T) {u v : List (Symbol T g.NT)} (h : g.Produces u v) (hu : g.AllUsed u) :
              theorem ContextFreeGrammar.allUsed_derives {T : Type} (g : ContextFreeGrammar T) {u v : List (Symbol T g.NT)} (h : g.Derives u v) (hu : g.AllUsed u) :

              Word restriction #

              Restrict a word with all-used nonterminals to the restricted type.

              Equations
              Instances For
                theorem ContextFreeGrammar.restrictWord_append {T : Type} (g : ContextFreeGrammar T) (u v : List (Symbol T g.NT)) (hu : g.AllUsed u) (hv : g.AllUsed v) (huv : g.AllUsed (u ++ v)) :
                g.restrictWord (u ++ v) huv = g.restrictWord u hu ++ g.restrictWord v hv

                The restricted grammar #

                Restrict a rule to used nonterminals.

                Equations
                Instances For

                  The context-free grammar with nonterminals restricted to the finite used set.

                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For
                    theorem ContextFreeGrammar.mem_toFiniteNT_rules {T : Type} (g : ContextFreeGrammar T) (r' : ContextFreeRule T g.UsedNT) (hr' : r' g.toFiniteNT.rules) :
                    ∃ (r : ContextFreeRule T g.NT) (hr : r g.rules), r' = g.restrictRule r hr

                    Direction 1: toFiniteNT derives → original derives #

                    Direction 2: original derives from initial → toFiniteNT derives #

                    theorem ContextFreeGrammar.restrictWord_produces {T : Type} (g : ContextFreeGrammar T) {u v : List (Symbol T g.NT)} (h : g.Produces u v) (hu : g.AllUsed u) (hv : g.AllUsed v) :
                    theorem ContextFreeGrammar.restrictWord_derives {T : Type} (g : ContextFreeGrammar T) {u v : List (Symbol T g.NT)} (h : g.Derives u v) (hu : g.AllUsed u) (hv : g.AllUsed v) :

                    Main theorem #