Chomsky Normal Form Grammars #
This file contains the definition of a chomsky normal form grammar, which is a context-free grammar with syntactic restriction on the rules. Each rule either rewrites to a single terminal symbol or a pair of nonterminals.
Main definitions #
ChomskyNormalFormGrammar: A chomsky normal form grammar.ChomskyNormalFormGrammar.toCFG: simple translation to a context-free grammar.
Main theorems #
Language.toCFG_correct:g.toCFGgenerates the same language a a context-free grammarg.
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
nto a single terminalt. - node
{T : Type uT}
{N : Type uN}
(n l r : N)
: ChomskyNormalFormRule T N
Second kind of rule, rewriting a nonterminal
nto a pair of nonterminallr.
Instances For
Equations
- One or more equations did not get rendered due to their size.
- instDecidableEqChomskyNormalFormRule.decEq (ChomskyNormalFormRule.leaf n t) (ChomskyNormalFormRule.node n_1 l r) = isFalse ⋯
- instDecidableEqChomskyNormalFormRule.decEq (ChomskyNormalFormRule.node n l r) (ChomskyNormalFormRule.leaf n_1 t) = isFalse ⋯
Instances For
The input of a CNF rule, similar to ContextFreeRule.input
Equations
- (ChomskyNormalFormRule.leaf n t).input = n
- (ChomskyNormalFormRule.node n l r_2).input = n
Instances For
The output of a CNF rule, similar to ContextFreeRule.output
Equations
- (ChomskyNormalFormRule.leaf n t).output = [Symbol.terminal t]
- (ChomskyNormalFormRule.node n l r_2).output = [Symbol.nonterminal l, Symbol.nonterminal r_2]
Instances For
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).
- head_leaf
{T : Type uT}
{N : Type uN}
(n : N)
(t : T)
(s : List (Symbol T N))
: (leaf n t).Rewrites (Symbol.nonterminal n :: s) (Symbol.terminal t :: s)
The replacement is at the start of the remaining string and the rule is a leaf rule.
- head_node
{T : Type uT}
{N : Type uN}
(nᵢ n₁ n₂ : N)
(s : List (Symbol T N))
: (node nᵢ n₁ n₂).Rewrites (Symbol.nonterminal nᵢ :: s) (Symbol.nonterminal n₁ :: Symbol.nonterminal n₂ :: s)
The replacement is at the start of the remaining string and the rule is a node rule.
- cons
{T : Type uT}
{N : Type uN}
(r : ChomskyNormalFormRule T N)
(s : Symbol T N)
{u₁ u₂ : List (Symbol T N)}
(hru : r.Rewrites u₁ u₂)
: r.Rewrites (s :: u₁) (s :: u₂)
The replacement is at the start of the remaining string.
Instances For
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.
Translation of ChomskyNormalFormRule to ContextFreeRule
Equations
- (ChomskyNormalFormRule.leaf n t).toCFGRule = { input := n, output := [Symbol.terminal t] }
- (ChomskyNormalFormRule.node n l r_2).toCFGRule = { input := n, output := [Symbol.nonterminal l, Symbol.nonterminal r_2] }
Instances For
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.
Instances For
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 c s in some number of
rewriting steps.
Instances For
The language (set of words) that can be generated by a given cnf grammar g.
Instances For
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.
ChomskyNormalFormGrammar to ContextFreeGrammar translation and related properties
Translation of ChomskyNormalFormGrammar to ContextFreeGrammar
Equations
Instances For
Alternative definition of ChomskyNormalFormGrammar.Derives which allows to use well-founded
induction on derivations, by explicitely counting the number of steps of the transformation
Given a context-free grammar g, strings u and v, and number n
g.DerivesIn u v n means that g can transform u to v in n rewriting steps.
- refl
{T : Type uT}
{g : ChomskyNormalFormGrammar T}
(w : List (Symbol T g.NT))
: g.DerivesIn w w 0
0 steps entail no transformation
- tail
{T : Type uT}
{g : ChomskyNormalFormGrammar T}
(u v w : List (Symbol T g.NT))
(n : ℕ)
: g.DerivesIn u v n → g.Produces v w → g.DerivesIn u w n.succ
n + 1 steps, if transforms
utovin n steps, andvtowin 1 step