Langlib

Langlib.Classes.ContextFree.NormalForms.ChomskyNormalForm

Chomsky Normal Form Grammars #

This file defines Chomsky-normal-form grammars and their derivation semantics.

Main declarations #

inductive ChomskyNormalFormRule (T : Type uT) (N : Type uN) :
Type (max uN uT)

Rule that rewrites a single nonterminal to a single terminal or a pair of nonterminals.

  • leaf {T : Type uT} {N : Type uN} (n : N) (t : T) : ChomskyNormalFormRule T N

    First kind of rule, rewriting a nonterminal n to a single terminal t.

  • node {T : Type uT} {N : Type uN} (n l r : N) : ChomskyNormalFormRule T N

    Second kind of rule, rewriting a nonterminal n to a pair of nonterminal lr.

Instances For
    def instDecidableEqChomskyNormalFormRule.decEq {T✝ : Type u_1} {N✝ : Type u_2} [DecidableEq T✝] [DecidableEq N✝] (x✝ x✝¹ : ChomskyNormalFormRule T✝ N✝) :
    Decidable (x✝ = x✝¹)
    Equations
    Instances For
      structure ChomskyNormalFormGrammar (T : Type uT) :
      Type (max (uN + 1) uT)
      Instances For
        def ChomskyNormalFormRule.input {T : Type uT} {N : Type uN} (r : ChomskyNormalFormRule T N) :
        N

        The input of a CNF rule, similar to ContextFreeRule.input

        Equations
        Instances For
          inductive ChomskyNormalFormRule.Rewrites {T : Type uT} {N : Type uN} :
          ChomskyNormalFormRule T NList (Symbol T N)List (Symbol T N)Prop

          Inductive definition of a single application of a given cnf 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 ChomskyNormalFormRule.Rewrites.exists_parts {T : Type uT} {N : Type uN} {r : ChomskyNormalFormRule 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 ChomskyNormalFormRule.rewrites_iff {T : Type uT} {N : Type uN} {r : ChomskyNormalFormRule 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 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 v is the output of r.

            theorem ChomskyNormalFormRule.Rewrites.append_left {T : Type uT} {N : Type uN} {r : ChomskyNormalFormRule T N} {u v : List (Symbol T N)} (huv : r.Rewrites u v) (p : List (Symbol T N)) :
            r.Rewrites (p ++ u) (p ++ v)

            Add extra prefix to cnf rewriting.

            theorem ChomskyNormalFormRule.Rewrites.append_right {T : Type uT} {N : Type uN} {r : ChomskyNormalFormRule T N} {u v : List (Symbol T N)} (huv : r.Rewrites u v) (p : List (Symbol T N)) :
            r.Rewrites (u ++ p) (v ++ p)

            Add extra postfix to cnf rewriting.

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

            Equations
            Instances For
              @[reducible, inline]

              Given a cnf 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 cnf 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 cnf grammar g.

                  Equations
                  Instances For
                    @[simp]

                    A given word w belongs to the language generated by a given cnf 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 ChomskyNormalFormGrammar.Derives.trans {T : Type uT} {g : ChomskyNormalFormGrammar T} {u v w : List (Symbol T g.NT)} (huv : g.Derives u v) (hvw : g.Derives v w) :
                    g.Derives u w
                    theorem ChomskyNormalFormGrammar.Derives.trans_produces {T : Type uT} {g : ChomskyNormalFormGrammar T} {u v w : List (Symbol T g.NT)} (huv : g.Derives u v) (hvw : g.Produces v w) :
                    g.Derives u w
                    theorem ChomskyNormalFormGrammar.Produces.trans_derives {T : Type uT} {g : ChomskyNormalFormGrammar T} {u v w : List (Symbol T g.NT)} (huv : g.Produces u v) (hvw : g.Derives v w) :
                    g.Derives u w
                    theorem ChomskyNormalFormGrammar.Derives.eq_or_head {T : Type uT} {g : ChomskyNormalFormGrammar 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 ChomskyNormalFormGrammar.Derives.eq_or_tail {T : Type uT} {g : ChomskyNormalFormGrammar T} {u w : List (Symbol T g.NT)} (huw : g.Derives u w) :
                    u = w ∃ (v : List (Symbol T g.NT)), g.Derives u v g.Produces v w
                    theorem ChomskyNormalFormGrammar.Produces.append_left {T : Type uT} {g : ChomskyNormalFormGrammar T} {u v : List (Symbol T g.NT)} (huv : g.Produces u v) (p : List (Symbol T g.NT)) :
                    g.Produces (p ++ u) (p ++ v)

                    Add extra prefix to cnf producing.

                    theorem ChomskyNormalFormGrammar.Produces.append_right {T : Type uT} {g : ChomskyNormalFormGrammar T} {u v : List (Symbol T g.NT)} (huv : g.Produces u v) (p : List (Symbol T g.NT)) :
                    g.Produces (u ++ p) (v ++ p)

                    Add extra postfix to cnf producing.

                    theorem ChomskyNormalFormGrammar.Derives.append_left {T : Type uT} {g : ChomskyNormalFormGrammar T} {u v : List (Symbol T g.NT)} (huv : g.Derives u v) (p : List (Symbol T g.NT)) :
                    g.Derives (p ++ u) (p ++ v)

                    Add extra prefix to cnf deriving.

                    theorem ChomskyNormalFormGrammar.Derives.append_right {T : Type uT} {g : ChomskyNormalFormGrammar T} {u v : List (Symbol T g.NT)} (huv : g.Derives u v) (p : List (Symbol T g.NT)) :
                    g.Derives (u ++ p) (v ++ p)

                    Add extra postfix to cnf deriving.

                    theorem ChomskyNormalFormGrammar.Derives.head_induction_on {T : Type uT} {g : ChomskyNormalFormGrammar T} {v : List (Symbol T g.NT)} {P : (u : List (Symbol T g.NT)) → g.Derives u vProp} {u : List (Symbol T g.NT)} (huv : g.Derives u v) (refl : P v ) (head : ∀ {u w : List (Symbol T g.NT)} (huw : g.Produces u w) (hwv : g.Derives w v), P w hwvP u ) :
                    P u huv