Langlib

Langlib.Grammars.Indexed.Definition

Indexed Grammar Definitions #

This file defines indexed grammars (Aho, 1968), their derivation relation, and the induced language predicate.

An indexed grammar extends context-free grammars by attaching a stack of flags (index symbols) to each nonterminal in a sentential form. Productions can push flags onto, or pop flags from, these stacks.

Main declarations #

References #

inductive IRhsSymbol (T N F : Type) :

A symbol on the right-hand side of an indexed production. A nonterminal can optionally have a flag pushed onto its inherited stack.

Instances For
    def instDecidableEqIRhsSymbol.decEq {T✝ N✝ F✝ : Type} [DecidableEq T✝] [DecidableEq N✝] [DecidableEq F✝] (x✝ x✝¹ : IRhsSymbol T✝ N✝ F✝) :
    Decidable (x✝ = x✝¹)
    Equations
    Instances For
      structure IRule (T N F : Type) :

      A production rule of an indexed grammar.

      • lhs : the nonterminal on the left-hand side.
      • consume : if some f, the rule can only fire when f is on top of the nonterminal's stack, and pops it; if none, no flag is consumed.
      • rhs : the right-hand side; each nonterminal inherits the (possibly modified) stack of the left-hand side, with an optional additional flag push.
      Instances For
        structure IndexedGrammar (T : Type) :

        An indexed grammar that generates words over the alphabet T.

        • nt : type of nonterminals
        • flag : type of index symbols (flags)
        • initial : the start nonterminal
        • rules : the list of production rules
        Instances For
          inductive IndexedGrammar.ISym {T : Type} (g : IndexedGrammar T) :

          A symbol in a sentential form of an indexed grammar: either a terminal or a nonterminal equipped with its current flag stack.

          Instances For
            def IndexedGrammar.expandRhs {T : Type} (g : IndexedGrammar T) (rhs : List (IRhsSymbol T g.nt g.flag)) (σ : List g.flag) :

            Expand the right-hand side of a rule, distributing the given stack σ to every nonterminal (and pushing any specified flag on top).

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              def IndexedGrammar.Transforms {T : Type} (g : IndexedGrammar T) (w₁ w₂ : List g.ISym) :

              One step of indexed-grammar transformation.

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

                Any number of steps of indexed-grammar transformation.

                Equations
                Instances For

                  A word (list of terminals) is generated by the grammar if it can be derived from the initial nonterminal (with empty stack).

                  Equations
                  Instances For

                    The language generated by the grammar.

                    Equations
                    Instances For
                      theorem IndexedGrammar.deri_self {T : Type} (g : IndexedGrammar T) (w : List g.ISym) :
                      g.Derives w w
                      theorem IndexedGrammar.deri_of_tran {T : Type} {g : IndexedGrammar T} {w₁ w₂ : List g.ISym} (h : g.Transforms w₁ w₂) :
                      g.Derives w₁ w₂
                      theorem IndexedGrammar.deri_of_deri_deri {T : Type} {g : IndexedGrammar T} {w₁ w₂ w₃ : List g.ISym} (h₁ : g.Derives w₁ w₂) (h₂ : g.Derives w₂ w₃) :
                      g.Derives w₁ w₃
                      theorem IndexedGrammar.deri_of_deri_tran {T : Type} {g : IndexedGrammar T} {w₁ w₂ w₃ : List g.ISym} (h₁ : g.Derives w₁ w₂) (h₂ : g.Transforms w₂ w₃) :
                      g.Derives w₁ w₃
                      theorem IndexedGrammar.deri_of_tran_deri {T : Type} {g : IndexedGrammar T} {w₁ w₂ w₃ : List g.ISym} (h₁ : g.Transforms w₁ w₂) (h₂ : g.Derives w₂ w₃) :
                      g.Derives w₁ w₃
                      theorem IndexedGrammar.deri_with_prefix {T : Type} {g : IndexedGrammar T} (p : List g.ISym) {w₁ w₂ : List g.ISym} (h : g.Derives w₁ w₂) :
                      g.Derives (p ++ w₁) (p ++ w₂)
                      theorem IndexedGrammar.deri_with_suffix {T : Type} {g : IndexedGrammar T} (s : List g.ISym) {w₁ w₂ : List g.ISym} (h : g.Derives w₁ w₂) :
                      g.Derives (w₁ ++ s) (w₂ ++ s)

                      Uniform stack-suffix extension #

                      @[simp]
                      theorem IndexedGrammar.ISym.appendStackSuffix_indexed {T : Type} {g : IndexedGrammar T} (suffix : List g.flag) (A : g.nt) (σ : List g.flag) :
                      appendStackSuffix suffix (indexed A σ) = indexed A (σ ++ suffix)
                      @[simp]
                      theorem IndexedGrammar.appendStackSuffixes_cons {T : Type} {g : IndexedGrammar T} (suffix : List g.flag) (s : g.ISym) (w : List g.ISym) :
                      @[simp]
                      @[simp]
                      theorem IndexedGrammar.appendStackSuffixes_map_terminal {T : Type} {g : IndexedGrammar T} (suffix : List g.flag) (w : List T) :
                      appendStackSuffixes suffix (List.map (fun (a : T) => ISym.terminal a) w) = List.map (fun (a : T) => ISym.terminal a) w
                      @[simp]
                      theorem IndexedGrammar.appendStackSuffixes_expandRhs {T : Type} {g : IndexedGrammar T} (suffix : List g.flag) (rhs : List (IRhsSymbol T g.nt g.flag)) (σ : List g.flag) :
                      appendStackSuffixes suffix (g.expandRhs rhs σ) = g.expandRhs rhs (σ ++ suffix)
                      theorem IndexedGrammar.transforms_appendStackSuffixes {T : Type} {g : IndexedGrammar T} {w₁ w₂ : List g.ISym} (h : g.Transforms w₁ w₂) (suffix : List g.flag) :
                      g.Transforms (appendStackSuffixes suffix w₁) (appendStackSuffixes suffix w₂)
                      theorem IndexedGrammar.derives_appendStackSuffixes {T : Type} {g : IndexedGrammar T} {w₁ w₂ : List g.ISym} (h : g.Derives w₁ w₂) (suffix : List g.flag) :
                      g.Derives (appendStackSuffixes suffix w₁) (appendStackSuffixes suffix w₂)
                      theorem IndexedGrammar.derives_appendStackSuffix_indexed {T : Type} {g : IndexedGrammar T} {A : g.nt} {σ : List g.flag} {w : List g.ISym} (h : g.Derives [ISym.indexed A σ] w) (suffix : List g.flag) :
                      g.Derives [ISym.indexed A (σ ++ suffix)] (appendStackSuffixes suffix w)

                      A grammar has no ε-productions.

                      Equations
                      Instances For
                        theorem IndexedGrammar.transforms_length_le_of_noEpsilon {T : Type} {g : IndexedGrammar T} (hne : g.NoEpsilon') {w₁ w₂ : List g.ISym} (h : g.Transforms w₁ w₂) :
                        w₁.length w₂.length
                        theorem IndexedGrammar.derives_length_le_of_noEpsilon {T : Type} {g : IndexedGrammar T} (hne : g.NoEpsilon') {w₁ w₂ : List g.ISym} (h : g.Derives w₁ w₂) :
                        w₁.length w₂.length

                        A grammar has terminals isolated: each rule's RHS either is a single terminal, or consists entirely of nonterminals (with optional flag annotations).

                        Equations
                        Instances For

                          The start symbol does not appear on the right-hand side of any rule.

                          Equations
                          Instances For

                            A rule has separated flag operations.

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

                              All rules in the grammar have separated flag operations.

                              Equations
                              Instances For
                                def IndexedGrammar.IRule.IsNF {T N F : Type} [DecidableEq N] (r : IRule T N F) (s : N) :

                                A production rule is in normal form with respect to a start symbol s.

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

                                  An indexed grammar is in normal form if every production rule is in normal form with respect to the start symbol.

                                  Equations
                                  Instances For
                                    theorem IndexedGrammar.IRule.rhs_ne_nil_of_isNF {T N F : Type} [DecidableEq N] {r : IRule T N F} {start : N} (h : IsNF r start) :
                                    theorem IndexedGrammar.IRule.terminalsIsolated_of_isNF {T N F : Type} [DecidableEq N] {r : IRule T N F} {start : N} (h : IsNF r start) :
                                    (∃ (a : T), r.rhs = [IRhsSymbol.terminal a]) sr.rhs, ∃ (n : N) (f : Option F), s = IRhsSymbol.nonterminal n f
                                    theorem IndexedGrammar.IRule.flagsSeparated_of_isNF {T N F : Type} [DecidableEq N] {r : IRule T N F} {start : N} (h : IsNF r start) :
                                    theorem IndexedGrammar.IRule.startNotOnRhs_of_isNF {T N F : Type} [DecidableEq N] {r : IRule T N F} {start : N} (h : IsNF r start) (s : IRhsSymbol T N F) :
                                    s r.rhsmatch s with | IRhsSymbol.nonterminal n a => n start | IRhsSymbol.terminal a => True