Langlib

Langlib.Classes.Regular.Decidability.Helper

Encoded Right-Regular Grammars #

This file introduces a concrete encoded representation of right-regular grammars (EncodedRG) that is Primcodable, together with the language it denotes (regularLanguageOf). It is the shared scaffolding for the uniform computability results proved in Decidability/Membership.lean and Decidability/Emptiness.lean.

Every right-regular grammar is in particular a context-free grammar, so an EncodedRG maps (via toCFG, a primitive-recursive translation) into the encoded context-free grammars EncodedCFG, and regularLanguageOf is defined as the context-free language of that image. The two uniform results are then obtained by composing the already-established uniform CF results (contextFree_computableMembership, contextFree_computableEmptiness) with the primitive-recursive map toCFG.

Restricting the rule shape to the right-regular productions A → a B, A → a, A → ε guarantees that every code denotes a regular language, so this is a faithful encoding of the regular class (and conversely every regular language is generated by some right-regular grammar, hence representable).

Main definitions #

An encoded right-regular grammar over terminal alphabet T.

The underlying type is ℕ × ℕ × List (ℕ × Option (T × Option ℕ)):

  • the first is the number of nonterminals minus one (so the nonterminal type is Fin (numNT + 1), always nonempty), matching EncodedCFG;
  • the second is the index of the initial nonterminal;
  • each rule (lhs, body) has body : Option (T × Option ℕ) encoding exactly one of the three right-regular shapes:
    • none — the rule lhs → ε;
    • some (a, none) — the rule lhs → a;
    • some (a, some B) — the rule lhs → a B.

Because each component is built from , Option, × and List, the type inherits Primcodable from the standard Mathlib instances (given Primcodable T).

Equations
Instances For

    Translate an encoded right-regular grammar to an encoded context-free grammar.

    The nonterminal count and initial index are kept; each right-regular rule body is expanded to its context-free right-hand side via bodyToRHS.

    Equations
    Instances For

      The language denoted by an encoded right-regular grammar: the context-free language of its context-free image.

      Equations
      Instances For

        Primitive recursiveness of the translation #

        The some-branch of bodyToRHS (case analysis on the optional next nonterminal) is primitive recursive.

        toCFG is primitive recursive.