Documentation

Mathlib.Computability.ContextFreeGrammar

Context-Free Grammars #

This file contains the definition of a context-free grammar, which is a grammar that has a single nonterminal symbol on the left-hand side of each rule.

We restrict nonterminals of a context-free grammar to Type because universe polymorphism would be cumbersome and unnecessary; we can always restrict a context-free grammar to the finitely many nonterminal symbols that are referred to by its finitely many rules.

Main definitions #

Main theorems #

inductive Symbol (T : Type u_1) (N : Type u_2) :
Type (max u_1 u_2)

Symbols for use by all kinds of grammars.

  • terminal {T : Type u_1} {N : Type u_2} (t : T) : Symbol T N

    Terminal symbols (of the same type as the language)

  • nonterminal {T : Type u_1} {N : Type u_2} (n : N) : Symbol T N

    Nonterminal symbols (must not be present when the word being generated is finalized)

Instances For
    @[instance_reducible]
    instance instDecidableEqSymbol {T✝ : Type u_1} {N✝ : Type u_2} [DecidableEq T✝] [DecidableEq N✝] :
    DecidableEq (Symbol T✝ N✝)
    Equations
    @[instance_reducible]
    instance instReprSymbol {T✝ : Type u_1} {N✝ : Type u_2} [Repr T✝] [Repr N✝] :
    Repr (Symbol T✝ N✝)
    Equations
    def instReprSymbol.repr {T✝ : Type u_1} {N✝ : Type u_2} [Repr T✝] [Repr N✝] :
    Symbol T✝ N✝ → ℕ → Std.Format
    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      @[instance_reducible]
      instance instFintypeSymbol {T✝ : Type u_1} {N✝ : Type u_2} [Fintype T✝] [Fintype N✝] :
      Fintype (Symbol T✝ N✝)
      Equations
      structure ContextFreeRule (T : Type u_1) (N : Type u_2) :
      Type (max u_1 u_2)

      Rule that rewrites a single nonterminal to any string (a list of symbols).

      • input : N

        Input nonterminal a.k.a. left-hand side.

      • output : List (Symbol T N)

        Output string a.k.a. right-hand side.

      Instances For
        theorem ContextFreeRule.ext {T : Type u_1} {N : Type u_2} {x y : ContextFreeRule T N} (input : x.input = y.input) (output : x.output = y.output) :
        x = y
        theorem ContextFreeRule.ext_iff {T : Type u_1} {N : Type u_2} {x y : ContextFreeRule T N} :
        def instDecidableEqContextFreeRule.decEq {T✝ : Type u_1} {N✝ : Type u_2} [DecidableEq T✝] [DecidableEq N✝] (x✝ x✝¹ : ContextFreeRule T✝ N✝) :
        Decidable (x✝ = x✝¹)
        Equations
        Instances For
          def instReprContextFreeRule.repr {T✝ : Type u_1} {N✝ : Type u_2} [Repr T✝] [Repr N✝] :
          ContextFreeRule T✝ N✝ → ℕ → Std.Format
          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            @[instance_reducible]
            instance instReprContextFreeRule {T✝ : Type u_1} {N✝ : Type u_2} [Repr T✝] [Repr N✝] :
            Repr (ContextFreeRule T✝ N✝)
            Equations
            structure ContextFreeGrammar (T : Type u_1) :
            Type (max 1 u_1)

            Context-free grammar that generates words over the alphabet T (a type of terminals).

            Instances For
              inductive ContextFreeRule.Rewrites {T : Type u_1} {N : Type u_2} (r : ContextFreeRule T N) :
              List (Symbol T N) → List (Symbol T N) → Prop

              Inductive definition of a single application of a given context-free rule r to a string u; r.Rewrites u v means that the r sends u to v (there may be multiple such strings v).

              Instances For
                theorem ContextFreeRule.Rewrites.exists_parts {T : Type u_1} {N : Type u_2} {r : ContextFreeRule T N} {u v : List (Symbol T N)} (hr : r.Rewrites u v) :
                ∃ (p : List (Symbol T N)) (q : List (Symbol T N)), u = p ++ [Symbol.nonterminal r.input] ++ q ∧ v = p ++ r.output ++ q
                theorem ContextFreeRule.rewrites_of_exists_parts {T : Type u_1} {N : Type u_2} (r : ContextFreeRule T N) (p q : List (Symbol T N)) :
                theorem ContextFreeRule.rewrites_iff {T : Type u_1} {N : Type u_2} {r : ContextFreeRule T N} {u v : List (Symbol T N)} :
                r.Rewrites u v ↔ ∃ (p : List (Symbol T N)) (q : List (Symbol T N)), u = p ++ [Symbol.nonterminal r.input] ++ q ∧ v = p ++ r.output ++ q

                Rule r rewrites string u is to string v iff they share both a prefix p and postfix q such that the remaining middle part of u is the input of r and the remaining middle part of u is the output of r.

                theorem ContextFreeRule.Rewrites.append_left {T : Type u_1} {N : Type u_2} {r : ContextFreeRule T N} {u v : List (Symbol T N)} (hvw : r.Rewrites u v) (p : List (Symbol T N)) :
                r.Rewrites (p ++ u) (p ++ v)

                Add extra prefix to context-free rewriting.

                theorem ContextFreeRule.Rewrites.append_right {T : Type u_1} {N : Type u_2} {r : ContextFreeRule T N} {u v : List (Symbol T N)} (hvw : r.Rewrites u v) (p : List (Symbol T N)) :
                r.Rewrites (u ++ p) (v ++ p)

                Add extra postfix to context-free rewriting.

                Given a context-free grammar g and strings u and v g.Produces u v means that one step of a context-free transformation by a rule from g sends u to v.

                Equations
                Instances For
                  @[reducible, inline]
                  abbrev ContextFreeGrammar.Derives {T : Type u_1} (g : ContextFreeGrammar T) :
                  List (Symbol T g.NT) → List (Symbol T g.NT) → Prop

                  Given a context-free grammar g and strings u and v g.Derives u v means that g can transform u to v in some number of rewriting steps.

                  Equations
                  Instances For

                    Given a context-free grammar g and a string s g.Generates s means that g can transform its initial nonterminal to s in some number of rewriting steps.

                    Equations
                    Instances For

                      The language (set of words) that can be generated by a given context-free grammar g.

                      Equations
                      Instances For
                        @[simp]

                        A given word w belongs to the language generated by a given context-free grammar g iff g can derive the word w (wrapped as a string) from the initial nonterminal of g in some number of steps.

                        theorem ContextFreeGrammar.Produces.single {T : Type u_1} {g : ContextFreeGrammar T} {v w : List (Symbol T g.NT)} (hvw : g.Produces v w) :
                        g.Derives v w
                        theorem ContextFreeGrammar.Derives.trans {T : Type u_1} {g : ContextFreeGrammar T} {u v w : List (Symbol T g.NT)} (huv : g.Derives u v) (hvw : g.Derives v w) :
                        g.Derives u w
                        theorem ContextFreeGrammar.Derives.trans_produces {T : Type u_1} {g : ContextFreeGrammar T} {u v w : List (Symbol T g.NT)} (huv : g.Derives u v) (hvw : g.Produces v w) :
                        g.Derives u w
                        theorem ContextFreeGrammar.Produces.trans_derives {T : Type u_1} {g : ContextFreeGrammar T} {u v w : List (Symbol T g.NT)} (huv : g.Produces u v) (hvw : g.Derives v w) :
                        g.Derives u w
                        theorem ContextFreeGrammar.Derives.eq_or_head {T : Type u_1} {g : ContextFreeGrammar T} {u w : List (Symbol T g.NT)} (huw : g.Derives u w) :
                        u = w ∨ ∃ (v : List (Symbol T g.NT)), g.Produces u v ∧ g.Derives v w
                        theorem ContextFreeGrammar.derives_iff_eq_or_head {T : Type u_1} {g : ContextFreeGrammar T} {u w : List (Symbol T g.NT)} :
                        g.Derives u w ↔ u = w ∨ ∃ (v : List (Symbol T g.NT)), g.Produces u v ∧ g.Derives v w
                        theorem ContextFreeGrammar.Derives.eq_or_tail {T : Type u_1} {g : ContextFreeGrammar T} {u w : List (Symbol T g.NT)} (huw : g.Derives u w) :
                        w = u ∨ ∃ (v : List (Symbol T g.NT)), g.Derives u v ∧ g.Produces v w
                        theorem ContextFreeGrammar.derives_iff_eq_or_tail {T : Type u_1} {g : ContextFreeGrammar T} {u w : List (Symbol T g.NT)} :
                        g.Derives u w ↔ w = u ∨ ∃ (v : List (Symbol T g.NT)), g.Derives u v ∧ g.Produces v w
                        theorem ContextFreeGrammar.Produces.append_left {T : Type u_1} {g : ContextFreeGrammar T} {v w : List (Symbol T g.NT)} (hvw : g.Produces v w) (p : List (Symbol T g.NT)) :
                        g.Produces (p ++ v) (p ++ w)

                        Add extra prefix to context-free producing.

                        theorem ContextFreeGrammar.Produces.append_right {T : Type u_1} {g : ContextFreeGrammar T} {v w : List (Symbol T g.NT)} (hvw : g.Produces v w) (p : List (Symbol T g.NT)) :
                        g.Produces (v ++ p) (w ++ p)

                        Add extra postfix to context-free producing.

                        theorem ContextFreeGrammar.Derives.append_left {T : Type u_1} {g : ContextFreeGrammar T} {v w : List (Symbol T g.NT)} (hvw : g.Derives v w) (p : List (Symbol T g.NT)) :
                        g.Derives (p ++ v) (p ++ w)

                        Add extra prefix to context-free deriving.

                        theorem ContextFreeGrammar.Derives.append_right {T : Type u_1} {g : ContextFreeGrammar T} {v w : List (Symbol T g.NT)} (hvw : g.Derives v w) (p : List (Symbol T g.NT)) :
                        g.Derives (v ++ p) (w ++ p)

                        Add extra postfix to context-free deriving.

                        theorem ContextFreeGrammar.derives_nonterminal {T : Type u_1} {g : ContextFreeGrammar T} {t : g.NT} (hgt : ∀ r ∈ g.rules, r.input ≠ t) (s : List (Symbol T g.NT)) (hs : s ≠ [Symbol.nonterminal t]) :
                        def Language.IsContextFree {T : Type u_1} (L : Language T) :

                        Context-free languages are defined by context-free grammars.

                        Equations
                        Instances For
                          def ContextFreeRule.reverse {T : Type u_1} {N : Type u_2} (r : ContextFreeRule T N) :

                          Rules for a grammar for a reversed language.

                          Equations
                          Instances For
                            @[simp]
                            theorem ContextFreeRule.reverse_reverse {T : Type u_1} {N : Type u_2} (r : ContextFreeRule T N) :
                            theorem ContextFreeRule.Rewrites.reverse {T : Type u_1} {N : Type u_2} {r : ContextFreeRule T N} {u v : List (Symbol T N)} :
                            theorem ContextFreeRule.rewrites_reverse {T : Type u_1} {N : Type u_2} {r : ContextFreeRule T N} {u v : List (Symbol T N)} :
                            @[simp]
                            theorem ContextFreeRule.rewrites_reverse_comm {T : Type u_1} {N : Type u_2} {r : ContextFreeRule T N} {u v : List (Symbol T N)} :

                            Grammar for a reversed language.

                            Equations
                            Instances For

                              Alias of the reverse direction of ContextFreeGrammar.produces_reverse.

                              The class of context-free languages is closed under reversal.