Langlib

Langlib.Classes.ContextFree.Decidability.Emptiness

Encoded Context-Free Grammars and Uniform Computability of Emptiness #

Shows a computability theorem for CFG emptiness where the grammar itself is the argument.

Main results #

Part 1: Productive Nonterminals Algorithm #

Initialize the productive nonterminals set: all nonterminals with leaf rules.

Equations
Instances For

    One step of the productive nonterminals fixpoint: add nonterminals whose node rule has both children already in S.

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

      The set of productive nonterminals, computed by iterating productiveStep g.rules.card times.

      Equations
      Instances For

        The set of all nonterminals that appear as inputs of rules.

        Equations
        Instances For

          Part 2: Monotonicity #

          Part 3: Range bound – productive nonterminals are rule inputs #

          Part 4: Fixpoint property #

          Part 5: Soundness #

          theorem ChomskyNormalFormGrammar.canDerive_node {T : Type} (g : ChomskyNormalFormGrammar T) [DecidableEq g.NT] (n c1 c2 : g.NT) (w1 w2 : List T) (hrule : ChomskyNormalFormRule.node n c1 c2 g.rules) (h1 : g.canDerive c1 w1) (h2 : g.canDerive c2 w2) :
          g.canDerive n (w1 ++ w2)
          theorem ChomskyNormalFormGrammar.productiveStep_sound {T : Type} (g : ChomskyNormalFormGrammar T) [DecidableEq g.NT] (S : Finset g.NT) (hS : ntS, ∃ (w : List T), g.canDerive nt w) (nt : g.NT) (h : nt g.productiveStep S) :
          ∃ (w : List T), g.canDerive nt w

          Part 6: Completeness #

          theorem ChomskyNormalFormGrammar.productive_mem_fixpoint {T : Type} (g : ChomskyNormalFormGrammar T) [DecidableEq g.NT] (S : Finset g.NT) (h_init : g.productiveInit S) (h_fix : g.productiveStep S = S) (nt : g.NT) (w : List T) (hw : g.canDerive nt w) :
          nt S

          Part 7: Main Correctness #

          Part 8: Constructive Decidability #

          Emptiness of a CNF grammar's language is decidable (constructively, without Classical.propDecidable).

          Equations
          Instances For

            Part 9: Context-Free Languages – General CFG #

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

              Decidability and Computability #

              Direct Emptiness Check #

              All nonterminal indices are normalized modulo ntCount to ensure the algorithm correctly identifies productive nonterminals regardless of the raw index values used in the encoding.

              def allNTsInListN {T : Type} (nc : ) (rhs : List ( T)) (S : List ) :

              Check whether all nonterminal indices (mod nc) in a rule's RHS appear in the set S (a list of ). Terminals always pass.

              Equations
              Instances For
                def prodStepN {T : Type} (nc : ) (rules : List ( × List ( T))) (S : List ) :

                One step of the productive-nonterminals fixpoint on encoded rules. Each LHS is normalized mod nc before addition.

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  def prodNTsN {T : Type} (nc : ) (rules : List ( × List ( T))) :

                  Compute the productive nonterminals by iterating prodStepN.

                  Equations
                  Instances For
                    def checkCFGEmpty {T : Type} (G : EncodedCFG T) :

                    Check whether the encoded CFG has an empty language.

                    Equations
                    Instances For

                      Correctness #

                      def IsProductive {T : Type} (g : CF_grammar T) (nt : g.nt) :
                      Equations
                      Instances For
                        theorem mem_prodStepN_of_mem {T : Type} (nc : ) (rules : List ( × List ( T))) (S : List ) (x : ) (hx : x S) :
                        x prodStepN nc rules S
                        theorem productive_sentential_form {T : Type} (g : CF_grammar T) (syms : List (symbol T g.nt)) (h : ssyms, match s with | symbol.terminal a => True | symbol.nonterminal nt => IsProductive g nt) :
                        ∃ (w : List T), CF_derives g syms (List.map symbol.terminal w)
                        theorem productive_from_rule {T : Type} (G : EncodedCFG T) (lhs : ) (rhs : List ( T)) (hrule : (lhs, rhs) G.rawRules) (hprod : ∀ (k : ), Sum.inl k rhsIsProductive G.toCFGrammar (G.toNT k)) :
                        theorem prodStepN_sound_step {T : Type} (G : EncodedCFG T) (S : List ) (hS : kS, IsProductive G.toCFGrammar (G.toNT k)) (k : ) :
                        theorem prodNTsN_sound {T : Type} (G : EncodedCFG T) (k : ) (hk : k prodNTsN G.ntCount G.rawRules) :
                        theorem mem_prodStepN_of_rule {T : Type} (nc : ) (rules : List ( × List ( T))) (S : List ) (lhs : ) (rhs : List ( T)) (hrule : (lhs, rhs) rules) (hall : allNTsInListN nc rhs S = true) :
                        lhs % nc prodStepN nc rules S
                        theorem iterate_nodup {T : Type} (nc : ) (rules : List ( × List ( T))) (n : ) :
                        ((prodStepN nc rules)^[n] []).Nodup
                        theorem prodNTsN_fixpoint {T : Type} (nc : ) (rules : List ( × List ( T))) (x : ) :
                        x prodStepN nc rules (prodNTsN nc rules) x prodNTsN nc rules
                        theorem fixpoint_rule_closed {T : Type} (G : EncodedCFG T) (S : List ) (hfix : ∀ (x : ), x prodStepN G.ntCount G.rawRules S x S) (nt : Fin G.ntCount) (rhs : List (symbol T (Fin G.ntCount))) (hrule : (nt, rhs) G.toCFGrammar.rules) (hall : ∀ (nt' : Fin G.ntCount), symbol.nonterminal nt' rhsnt' S) :
                        nt S
                        theorem all_nts_in_S_of_derives {T : Type} (G : EncodedCFG T) (S : List ) (hfix : ∀ (x : ), x prodStepN G.ntCount G.rawRules S x S) (syms : List (symbol T (Fin G.ntCount))) (w : List T) (hd : CF_derives G.toCFGrammar syms (List.map symbol.terminal w)) (nt : Fin G.ntCount) :
                        symbol.nonterminal nt symsnt S
                        theorem fixpoint_captures_productive {T : Type} (G : EncodedCFG T) (S : List ) (hfix : ∀ (x : ), x prodStepN G.ntCount G.rawRules S x S) (nt : Fin G.ntCount) (h : IsProductive G.toCFGrammar nt) :
                        nt S

                        For a fixpoint S, if all nonterminals in a sentential form that derives some terminal string have their indices in S, and a rule (nt, rhs) with rhs = that form is in the grammar, then nt's index is in S.

                        theorem complete_of_fixpoint {T : Type} (G : EncodedCFG T) (S : List ) (hfix : ∀ (x : ), x prodStepN G.ntCount G.rawRules S x S) (k : ) (hk : IsProductive G.toCFGrammar (G.toNT k)) :
                        k % G.ntCount S

                        Computability #

                        Context-free emptiness is uniformly computable for encoded CFGs (raw ComputablePred decider; the packaged ComputableEmptiness over the CF class lives in ContextFree/Decidability/Characterization.lean).

                        Emptiness is uniformly computable for the context-free languages: encoded context-free grammars are an adequate, effective presentation (contextFreeLanguageOf_characterizes) with uniformly decidable emptiness (ComputableEmptiness).