Documentation

CslibFrontier.Computability.Languages.ChomskyNormalForm

Grammars in Chomsky normal form #

In Sipser's definition Sipser, Definition 2.8, a context-free grammar is in Chomsky normal form when every rule has the form A → B C or A → a, where B and C are not the start symbol S, except that the rule S → ε is permitted. Hopcroft, Motwani and Ullman's normal form [HMU06] has no ε-rule and allows S on right-hand sides. Cslib.ChomskyNormalFormGrammar covers both: its rules form a list, so that algorithms can iterate over them, and a Boolean flag records the rule S → ε. Only when that flag is set must S occur on no right-hand side; this is what keeps ε out of the languages of all the other nonterminals. Sipser's form is the case in which S occurs on no right-hand side regardless. ChomskyNormalFormGrammar.toCFG reads the grammar as a Mathlib ContextFreeGrammar, and every statement about derivations and languages is made through it.

The design (rule constructors leaf and node, the field names NT, initial and rules, and the embedding toCFG) follows Loitzl and Dvořák's Mathlib pull request leanprover-community/mathlib4#19943, which was closed unmerged and had no ε-rule.

Main definitions #

Main results #

References #

inductive Cslib.ChomskyNormalFormRule (T : Type u_1) (N : Type u_2) :
Type (max u_1 u_2)

A rule of a grammar in Chomsky normal form, over terminals T and nonterminals N.

Instances For
    def Cslib.instDecidableEqChomskyNormalFormRule.decEq {T✝ : Type u_1} {N✝ : Type u_2} [DecidableEq T✝] [DecidableEq N✝] (x✝ x✝¹ : ChomskyNormalFormRule T✝ N✝) :
    Decidable (x✝ = x✝¹)
    @[instance_reducible]
    def Cslib.instReprChomskyNormalFormRule.repr {T✝ : Type u_1} {N✝ : Type u_2} [Repr T✝] [Repr N✝] :
    @[instance_reducible]
    instance Cslib.instReprChomskyNormalFormRule {T✝ : Type u_1} {N✝ : Type u_2} [Repr T✝] [Repr N✝] :

    The left-hand side of a rule.

    The right-hand side of a rule.

    A rule in Chomsky normal form as a context-free rule.

    structure Cslib.ChomskyNormalFormGrammar (T : Type u_1) :
    Type (max 1 u_1)

    A context-free grammar in Chomsky normal form over terminals T: rules A → a and A → B C, together with the rule S → ε for the start symbol S when nilRule is set. With that rule, S occurs on no right-hand side.

    @[reducible, inline]

    A grammar in Chomsky normal form as a Mathlib ContextFreeGrammar.

    theorem Cslib.ChomskyNormalFormGrammar.mem_toCFG_rules {T : Type u_1} (G : ChomskyNormalFormGrammar T) {r : ContextFreeRule T G.NT} :
    r ∈ G.toCFG.rules ↔ (∃ r' ∈ G.rules, r'.toContextFreeRule = r) ∨ G.nilRule = true ∧ r = { input := G.initial, output := [] }

    The rules of G.toCFG are those of G and, with nilRule, the rule initial → ε.

    Every symbol of a sentential form deriving ε is the start symbol, which then has the rule S → ε.

    A nonterminal derives ε exactly when it is the start symbol and the grammar has the rule S → ε.

    The empty word is in the language exactly when the grammar has the rule S → ε.

    A nonterminal A derives a nonempty word w exactly when w is a letter a with a rule A → a, or w = u ++ v for nonempty u and v and a rule A → B C such that B derives u and C derives v.