Documentation

CslibFrontier.Algorithms.Lean.CYK.Basic

The Cocke–Younger–Kasami algorithm #

Membership in the language of a grammar in Chomsky normal form, decided by dynamic programming over subwords Kasami Younger Hopcroft, Motwani and Ullman, Section 7.4.4. The table holds, for each subword w[i, i + l) of the input, the list of nonterminals deriving it. Cells of length one are read off the rules A → a. A cell of length l ≥ 2 is filled by trying every split point k and every rule A → B C with B in the cell of the first k + 1 letters and C in the cell of the remaining l - k - 1. The input is accepted when the start symbol is in the cell of the whole word, or, for the empty word, when the grammar has the rule S → ε.

This is the algorithm in the proof of Sipser, Theorem 7.16 that every context-free language is in P, for a grammar that is already in Chomsky normal form. Conversion of an arbitrary context-free grammar to Chomsky normal form is not formalized here; a Word-RAM implementation and O((|R| + 1)(|w| + 1)³) running-time bound are proved in CslibFrontier.Algorithms.WordRAM.CYK.

Main definitions #

Main results #

References #

The left-hand side A of a rule A → a with x = some a.

The left-hand side A of a rule A → B C with B ∈ L and C ∈ R.

Scan the rules once, adding the nonterminal of every match to acc unless it is already there (List.insert).

@[reducible, inline]

A CYK table: tbl i l lists nonterminals for the subword of length l starting at i.

The cell of the letter at position i: the nonterminals A with a rule A → w[i].

The cell of the subword of length l ≥ 2 at i, from the cells of shorter subwords: the nonterminals A with a rule A → B C, B in the cell of the first k + 1 letters and C in the cell of the remaining l - k - 1, for some split k < l - 1.

def Cslib.Algorithms.Lean.CYK.cell {T : Type u_1} [DecidableEq T] (G : ChomskyNormalFormGrammar T) [DecidableEq G.NT] (w : List T) (tbl : Table G) (i l : ℕ) :

The cell of the subword of length l at i.

def Cslib.Algorithms.Lean.CYK.setCell {T : Type u_1} (G : ChomskyNormalFormGrammar T) (tbl : Table G) (i l : ℕ) (c : List G.NT) :

Overwrite the cell (i, l) of a table.

Fill the cells of length l at every start position i with i + l ≤ |w|.

The table with the cells of lengths 1, …, L filled, in order of length.

The CYK membership test: accept the empty word when the grammar has the rule S → ε, and a nonempty word when the start symbol is in the cell of the whole word.

Correctness #

theorem Cslib.Algorithms.Lean.CYK.mem_scan {T : Type u_1} {G : ChomskyNormalFormGrammar T} [DecidableEq G.NT] {f : ChomskyNormalFormRule T G.NT → Option G.NT} {acc : List G.NT} {A : G.NT} :
A ∈ scan G f acc ↔ A ∈ acc ∨ ∃ r ∈ G.rules, f r = some A

A scan adds exactly the matches of the rules.

theorem Cslib.Algorithms.Lean.CYK.nodup_scan {T : Type u_1} {G : ChomskyNormalFormGrammar T} [DecidableEq G.NT] {f : ChomskyNormalFormRule T G.NT → Option G.NT} {acc : List G.NT} (h : acc.Nodup) :
(scan G f acc).Nodup

A scan adds no repetitions.

theorem Cslib.Algorithms.Lean.CYK.mem_nodeCell {T : Type u_1} {G : ChomskyNormalFormGrammar T} [DecidableEq G.NT] {tbl : Table G} {i l : ℕ} {A : G.NT} :
A ∈ nodeCell G tbl i l ↔ ∃ k < l - 1, ∃ (B : G.NT) (C : G.NT), ChomskyNormalFormRule.node A B C ∈ G.rules ∧ B ∈ tbl i (k + 1) ∧ C ∈ tbl (i + k + 1) (l - k - 1)
theorem Cslib.Algorithms.Lean.CYK.level_apply {T : Type u_1} [DecidableEq T] {G : ChomskyNormalFormGrammar T} [DecidableEq G.NT] {w : List T} (tbl : Table G) (l i' l' : ℕ) :
level G w tbl l i' l' = if l' = l ∧ i' + l ≤ w.length then cell G w tbl i' l else tbl i' l'

Filling level l sets the cells of length l that fit in the word, and nothing else.

theorem Cslib.Algorithms.Lean.CYK.table_eq_of_le {T : Type u_1} [DecidableEq T] {G : ChomskyNormalFormGrammar T} [DecidableEq G.NT] {w : List T} {l L : ℕ} (hl : l ≤ L) (i : ℕ) :
table G w L i l = table G w l i l

Once level l has been filled (l ≤ L), cells of length l are unchanged by later levels.

theorem Cslib.Algorithms.Lean.CYK.mem_table_iff {T : Type u_1} [DecidableEq T] {G : ChomskyNormalFormGrammar T} [DecidableEq G.NT] {w : List T} {L i l : ℕ} (hl : 0 < l) (hlL : l ≤ L) (hi : i + l ≤ w.length) (A : G.NT) :

Correctness of the CYK table: after filling the levels 1, …, L, the cell of a subword of length l ≤ L lists exactly the nonterminals deriving it.

Correctness of CYK Hopcroft, Motwani and Ullman, Section 7.4.4: the test accepts exactly the words of the language of the grammar.

@[instance_reducible]

Membership in the language of a grammar in Chomsky normal form is decidable, by CYK.

Size of the cells #

theorem Cslib.Algorithms.Lean.CYK.exists_rule_of_mem_cell {T : Type u_1} [DecidableEq T] {G : ChomskyNormalFormGrammar T} [DecidableEq G.NT] {w : List T} {tbl : Table G} {i l : ℕ} {A : G.NT} (h : A ∈ cell G w tbl i l) :
∃ r ∈ G.rules, r.input = A

Every nonterminal in a cell is the left-hand side of a rule.

theorem Cslib.Algorithms.Lean.CYK.nodup_cell {T : Type u_1} [DecidableEq T] {G : ChomskyNormalFormGrammar T} [DecidableEq G.NT] {w : List T} (tbl : Table G) (i l : ℕ) :
(cell G w tbl i l).Nodup
theorem Cslib.Algorithms.Lean.CYK.length_cell_le {T : Type u_1} [DecidableEq T] {G : ChomskyNormalFormGrammar T} [DecidableEq G.NT] {w : List T} (tbl : Table G) (i l : ℕ) :
(cell G w tbl i l).length ≤ G.rules.length

A cell has at most one entry per rule.

Every cell of the table is a list without repetitions of at most |R| nonterminals, for a grammar with |R| rules.