Langlib

Langlib.Classes.ContextSensitive.Decidability.Membership

Membership in Context-Sensitive Languages #

Membership in a context-sensitive language is uniformly computable: there is a single algorithm that, given an encoded context-sensitive grammar and a word, decides membership.

A context-sensitive (noncontracting) grammar is coded with nonterminals fixed to , as a Code T = List (grule T ℕ) × ℕ (its rule list and initial symbol), which is Primcodable. Membership is decided by a bounded search over derivation sequences (memCode): a candidate sequence of rule applications is replayed from the start symbol and checked against the target word, searching all sequences within a bound that is sufficient for noncontracting grammars.

Main results #

The uniform decider here is also the engine behind CSRecursive (Classes/ContextSensitive/Inclusion/StrictRecursive.lean).

Uniform computability of applyRuleSeq #

The project's computable_applyRuleSeq proves applyRuleSeq rules init computable for a fixed rule list. For the enumeration we need it uniform in the rules (the grammar is runtime data). We re-derive the Primrec facts threading rules/init as projections rather than constants, reusing the uniform building blocks primrec_applyRuleAt.

theorem primrec_applyRuleSeq_uniform {T N : Type} [DecidableEq T] [DecidableEq N] [Primcodable T] [Primcodable N] :
Primrec fun (p : List (grule T N) × List (symbol T N) × List ( × )) => applyRuleSeq p.1 p.2.1 p.2.2

applyRuleSeq is primitive recursive uniformly in the rule list, initial form, and derivation sequence.

A concrete word-indexed enumeration of context-sensitive languages #

We make the enumeration concrete so that the correctness clause becomes definitional and the remaining content splits cleanly into exactly two obligations: uniform computability of the decider (memOracle_computable) and coverage of all CS languages (enum_covers_CS).

A context-sensitive (noncontracting) grammar is coded with nonterminals fixed to , as a Code T = List (grule T ℕ) × ℕ (its rule list and initial symbol). Code T is Primcodable via the existing grulePrimcodable instance, so it is decodable from a word.

Membership is decided by a bounded search over derivation sequences: a derivation sequence seq : List (ℕ × ℕ) (rule index, position) is replayed from [initial] by applyRuleSeq, and we check whether it yields exactly v. We search all sequences of length seqBound c v whose entries are bounded (rule index < |rules|, position ≤ |v| + 1). This is a finite, computable search (via the uniform primrec_applyRuleSeq_uniform), and for a noncontracting grammar the bound is large enough to capture a witnessing derivation whenever v is in the language.

@[reducible, inline]
abbrev Code (T : Type) :

A coded context-sensitive grammar: nonterminals are ; the data is its rule list and initial nonterminal.

Equations
Instances For
    @[reducible]
    def ofCode {T : Type} (c : Code T) :

    Interpret a Code as an (unrestricted) grammar with nonterminals.

    Equations
    Instances For
      def testData {T : Type} [DecidableEq T] (c : Code T) (seq : List ( × )) (v : List T) :

      Replay a derivation sequence from [initial] and check it yields exactly v.

      Equations
      Instances For
        def seqAlphabet {T : Type} (c : Code T) (v : List T) :

        The finite alphabet of derivation steps (rule index, position) considered for c, v.

        Equations
        Instances For
          def listsOfLen (alph : List ( × )) :
          List (List ( × ))

          All lists of length exactly k over the alphabet alph.

          Equations
          Instances For
            def seqBound {T : Type} (c : Code T) (v : List T) :

            Crude over-approximation of the length of a minimal witnessing derivation sequence (the number of distinct bounded sentential forms).

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              def candidateSeqs {T : Type} (c : Code T) (v : List T) :

              All derivation sequences considered for c, v (length seqBound, bounded entries).

              Equations
              Instances For
                def memCode {T : Type} [DecidableEq T] (c : Code T) (v : List T) :

                Bounded derivation-sequence search membership decider for a coded grammar.

                Equations
                Instances For
                  theorem memCode_primrec {T : Type} [DecidableEq T] [Primcodable T] :
                  Primrec fun (p : Code T × List T) => memCode p.1 p.2

                  memCode is primrec jointly in the code and the word.

                  Coverage: every CS language is enumerated #

                  The proof has two easy structural ingredients (here) and one hard combinatorial ingredient (the bounded-witness lemma memCode_complete).

                  theorem memCode_sound {T : Type} [DecidableEq T] (c : Code T) (v : List T) (h : memCode c v = true) :

                  Coverage, soundness side. If the bounded search accepts, the word is in the language.

                  theorem memCode_complete {T : Type} [DecidableEq T] (c : Code T) (v : List T) (hcs : grammar_context_sensitive (ofCode c)) (h : v grammar_language (ofCode c)) :

                  Coverage, completeness side (the hard bounded-witness lemma). For a context-sensitive coded grammar, if v is in the language then there is a witnessing derivation sequence within the search bound.

                  theorem code_of_CS {T : Type} [DecidableEq T] {L : Language T} (hL : is_CS L) :

                  Encoding. Every context-sensitive language is the language of a context-sensitive coded grammar (nonterminals renamed to ).

                  Uniform computable membership #

                  The language a coded grammar accepts via the bounded search. For a context-sensitive code it equals the grammar's language (memCode_sound / memCode_complete); for an arbitrary code it is whatever the bounded search accepts — in all cases a decidable language.

                  The bounded-search language of a coded grammar.

                  Equations
                  Instances For

                    Uniform computability of context-sensitive membership. Membership is decided by a single algorithm taking the encoded grammar and the word jointly.

                    This is the raw ComputablePred decider; the packaged uniform statement ComputableMembership is_CS contextSensitiveLanguageOf' (over the noncontracting-code subtype) is assembled in ContextSensitive/Decidability/Characterization.lean.

                    Computable membership (per language) #

                    Specialising the uniform algorithm to a fixed (encoded) grammar, or to any context-sensitive language, gives the weaker ComputablePred statements.

                    Per-grammar computability: membership in a fixed coded grammar's bounded-search language.

                    theorem exists_code_of_is_CS {T : Type} [DecidableEq T] {L : Language T} (hL : is_CS L) :

                    Every context-sensitive language is exactly the bounded-search language of some code.

                    theorem computablePred_mem_of_is_CS {T : Type} [DecidableEq T] [Primcodable T] {L : Language T} (hL : is_CS L) :
                    ComputablePred fun (w : List T) => w L

                    Membership in any context-sensitive language is computable (ComputablePred), derived from the uniform algorithm.

                    Decidable membership #

                    Decidability is a corollary of computability. For a coded grammar the decision procedure is the genuine bounded search; for an abstract context-sensitive language it is extracted classically.

                    Membership in a coded grammar's bounded-search language is decidable (by the search).

                    Equations
                    noncomputable def decidable_mem_of_is_CS {T : Type} [DecidableEq T] [Primcodable T] {L : Language T} (hL : is_CS L) (v : List T) :

                    Membership in a context-sensitive language is decidable.

                    Equations
                    Instances For