Langlib

Langlib.Grammars.Unrestricted.FiniteNonterminals

Every unrestricted grammar is equivalent to one with finitely many nonterminals #

Since an unrestricted grammar stores its rules in a List, only finitely many nonterminals can ever appear in a derivation from the initial symbol. We restrict the nonterminal type to those that actually occur in the grammar, producing an equivalent grammar whose nonterminal type carries a Fintype instance.

Main results #

Extracting nonterminals from symbol lists #

def symbolsNTs {T N : Type} :
List (symbol T N)List N

Extract all nonterminals from a list of symbols.

Equations
Instances For
    @[simp]
    theorem symbolsNTs_nil {T N : Type} :
    @[simp]
    theorem symbolsNTs_cons_terminal {T N : Type} (t : T) (l : List (symbol T N)) :
    @[simp]
    theorem symbolsNTs_cons_nonterminal {T N : Type} (n : N) (l : List (symbol T N)) :
    theorem mem_symbolsNTs_iff {T N : Type} {n : N} {l : List (symbol T N)} :
    theorem symbolsNTs_append {T N : Type} (l₁ l₂ : List (symbol T N)) :
    symbolsNTs (l₁ ++ l₂) = symbolsNTs l₁ ++ symbolsNTs l₂

    Used nonterminals of a grammar #

    def ruleNTs {T N : Type} (r : grule T N) :

    All nonterminals mentioned in a grammar rule.

    Equations
    Instances For
      def usedNTsList {T : Type} (g : grammar T) :

      The list of all nonterminals used in a grammar.

      Equations
      Instances For
        def usedNTs {T : Type} (g : grammar T) :

        The finite set of all nonterminals used in a grammar.

        Equations
        Instances For
          theorem ruleNT_mem_usedNTs {T : Type} {g : grammar T} {r : grule T g.nt} {n : g.nt} (hr : r g.rules) (hn : n ruleNTs r) :
          theorem inputN_mem_ruleNTs {T N : Type} (r : grule T N) :

          The restricted grammar #

          @[reducible, inline]
          abbrev UsedNT {T : Type} (g : grammar T) :

          The subtype of used nonterminals.

          Equations
          Instances For
            instance usedNT_fintype {T : Type} (g : grammar T) :
            Equations
            def restrictNT {T : Type} (g : grammar T) (n : g.nt) :

            Restrict a nonterminal to UsedNT g. Maps unused nonterminals to the initial symbol.

            Equations
            Instances For
              def restrictSym {T : Type} (g : grammar T) :
              symbol T g.ntsymbol T (UsedNT g)

              Restrict a symbol to UsedNT g.

              Equations
              Instances For
                def embedSym {T : Type} (g : grammar T) :
                symbol T (UsedNT g)symbol T g.nt

                Embed a symbol over UsedNT g back into a symbol over g.nt.

                Equations
                Instances For
                  def restrictGrammar {T : Type} (g : grammar T) :

                  The grammar restricted to its used nonterminals.

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

                    Properties of restrictSym and embedSym #

                    theorem restrictNT_of_mem {T : Type} {g : grammar T} {n : g.nt} (h : n usedNTs g) :
                    @[simp]
                    @[simp]
                    theorem embedSym_terminal {T : Type} (g : grammar T) (t : T) :
                    theorem restrictSym_embedSym {T : Type} (g : grammar T) (s : symbol T (UsedNT g)) :
                    theorem embedSym_restrictSym {T : Type} {g : grammar T} {s : symbol T g.nt} (h : ∀ (n : g.nt), s = symbol.nonterminal nn usedNTs g) :

                    The allNTsUsed predicate #

                    def allNTsUsed {T : Type} (g : grammar T) (l : List (symbol T g.nt)) :

                    All nonterminals in a symbol list belong to usedNTs g.

                    Equations
                    Instances For
                      theorem allNTsUsed_nil {T : Type} (g : grammar T) :
                      theorem allNTsUsed_append {T : Type} {g : grammar T} {l₁ l₂ : List (symbol T g.nt)} :
                      allNTsUsed g (l₁ ++ l₂) allNTsUsed g l₁ allNTsUsed g l₂
                      theorem allNTsUsed_of_rule_input_L {T : Type} {g : grammar T} {r : grule T g.nt} (hr : r g.rules) :
                      theorem allNTsUsed_of_rule_input_R {T : Type} {g : grammar T} {r : grule T g.nt} (hr : r g.rules) :
                      theorem allNTsUsed_of_rule_output {T : Type} {g : grammar T} {r : grule T g.nt} (hr : r g.rules) :

                      Roundtrip properties for lists #

                      theorem map_embedSym_map_restrictSym {T : Type} {g : grammar T} {l : List (symbol T g.nt)} (h : allNTsUsed g l) :
                      theorem map_restrictSym_terminal {T : Type} (g : grammar T) (w : List T) :
                      List.map (restrictSym g) (List.map (fun (t : T) => symbol.terminal t) w) = List.map (fun (t : T) => symbol.terminal t) w
                      theorem map_embedSym_terminal {T : Type} (g : grammar T) (w : List T) :
                      List.map (embedSym g) (List.map (fun (t : T) => symbol.terminal t) w) = List.map (fun (t : T) => symbol.terminal t) w

                      Preservation of allNTsUsed through derivations #

                      theorem allNTsUsed_preserved_by_transform {T : Type} {g : grammar T} {u v : List (symbol T g.nt)} (ht : grammar_transforms g u v) (hu : allNTsUsed g u) :
                      theorem allNTsUsed_preserved_by_derives {T : Type} {g : grammar T} {u v : List (symbol T g.nt)} (hd : grammar_derives g u v) (hu : allNTsUsed g u) :

                      Rule membership in the restricted grammar #

                      theorem restrictGrammar_rule_of_mem {T : Type} {g : grammar T} {r : grule T g.nt} (hr : r g.rules) :
                      { input_L := List.map (restrictSym g) r.input_L, input_N := restrictNT g r.input_N, input_R := List.map (restrictSym g) r.input_R, output_string := List.map (restrictSym g) r.output_string } (restrictGrammar g).rules

                      Derivation correspondence #

                      theorem derives_restrict {T : Type} {g : grammar T} {u v : List (symbol T g.nt)} (hd : grammar_derives g u v) (hu : allNTsUsed g u) :
                      theorem derives_embed {T : Type} {g : grammar T} {u v : List (symbol T (UsedNT g))} (hd : grammar_derives (restrictGrammar g) u v) :

                      Main theorem #

                      Every unrestricted grammar is equivalent to one with finitely many nonterminals.