Correctness of indexed CYK
TheoremCorrectness of one indexed CYK cell#✓fully proved
A cell is correct once all strictly shorter, valid factors are correct. The rule enumeration certificate constrains data only, not computation.
theorem cell_correct {g : ContextFreeGrammar T} [DecidableEq g.NT]
(hg : IsChomskyNormalForm g) (tc nc : ℕ) (w : List T)
(rs : List (ContextFreeRule T g.NT))
(hrs : ∀ r, r ∈ rs ↔ r ∈ g.rules)
(tbl : Array (Array (List g.NT))) (i len : ℕ)
(hpos : 0 < len) (hbound : i + len ≤ w.length)
(htbl : ∀ j m, 0 < m → m < len → j + m ≤ w.length → ∀ a,
a ∈ lookup tbl j m ↔
Yields g [.nonterminal a] ((w.drop j).take m)) (a : g.NT) :
a ∈ (cell tc nc w.toArray tbl i len rs).ret ↔
Yields g [.nonterminal a] ((w.drop i).take len)
def IsChomskyNormalForm (g : ContextFreeGrammar T) : Prop :=
(∀ r ∈ g.rules,
(∃ B C, r.output = [Symbol.nonterminal B, Symbol.nonterminal C]) ∨
(∃ a, r.output = [Symbol.terminal a]) ∨
(r.output = [] ∧ r.input = g.initial)) ∧
((∀ r ∈ g.rules, Symbol.nonterminal g.initial ∉ r.output) ∨
∀ r ∈ g.rules, r.output ≠ [])
CSLibExt/Computability/Languages/ContextFree/ChomskyNormalForm.lean:57
def lookup (tbl : Array (Array (List N))) (i len : ℕ) : List N :=
(tbl[len]?.getD #[])[i]?.getD []
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:68
inductive Yields (g : ContextFreeGrammar T) : List (Symbol T g.NT) → List T → Prop
| nil : Yields g [] []
| terminal (a : T) {u : List (Symbol T g.NT)} {w : List T} (h : Yields g u w) :
Yields g (Symbol.terminal a :: u) (a :: w)
| nonterminal {r : ContextFreeRule T g.NT} (hr : r ∈ g.rules)
{u : List (Symbol T g.NT)} {w₁ w₂ : List T}
(hout : Yields g r.output w₁) (hrest : Yields g u w₂) :
Yields g (Symbol.nonterminal r.input :: u) (w₁ ++ w₂)
CSLibExt/Computability/Languages/ContextFree/Yields.lean:48
def cell (termCost ntCost : ℕ) (w : Array T)
(tbl : Array (Array (List N))) (i len : ℕ) :
List (ContextFreeRule T N) → TimeM ℕ (List N)
| [] => ⟨[], 1⟩
| r :: rs =>
let f := fits termCost ntCost w tbl i len r.output
let rest := cell termCost ntCost w tbl i len rs
⟨if f.ret then r.input :: rest.ret else rest.ret,
f.time + rest.time + 5⟩
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:121
Uses 11 · Used by 1
TheoremIndexing of a constructed CYK row#✓fully proved
Row construction preserves the exact start-position indexing.
theorem row_get (tc nc : ℕ) (w : Array T)
(tbl : Array (Array (List N))) (len : ℕ)
(rs : List (ContextFreeRule T N)) (i count j : ℕ) :
(row tc nc w tbl len rs i count).ret[j]? =
if j < count then some (cell tc nc w tbl (i + j) len rs).ret
else none
def row (termCost ntCost : ℕ) (w : Array T)
(tbl : Array (Array (List N))) (len : ℕ)
(rs : List (ContextFreeRule T N)) (i : ℕ) :
ℕ → TimeM ℕ (List (List N))
| 0 => ⟨[], 1⟩
| count + 1 =>
let c := cell termCost ntCost w tbl i len rs
let rest := row termCost ntCost w tbl len rs (i + 1) count
⟨c.ret :: rest.ret, c.time + rest.time + 4⟩
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:147
def cell (termCost ntCost : ℕ) (w : Array T)
(tbl : Array (Array (List N))) (i len : ℕ) :
List (ContextFreeRule T N) → TimeM ℕ (List N)
| [] => ⟨[], 1⟩
| r :: rs =>
let f := fits termCost ntCost w tbl i len r.output
let rest := cell termCost ntCost w tbl i len rs
⟨if f.ret then r.input :: rest.ret else rest.ret,
f.time + rest.time + 5⟩
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:121
Uses 2 · Used by 2
TheoremIndexed CYK table keeps its allocated size#✓fully proved
The outer table retains its allocated size through every update.
@[simp] theorem table_size (tc nc : ℕ) (w : Array T)
(rs : List (ContextFreeRule T N)) (depth : ℕ) :
(table tc nc w rs depth).ret.size = w.size + 1
def table (termCost ntCost : ℕ) (w : Array T)
(rs : List (ContextFreeRule T N)) :
ℕ → TimeM ℕ (Array (Array (List N)))
| 0 => ⟨Array.replicate (w.size + 1) #[], w.size + 4⟩
| depth + 1 =>
let prev := table termCost ntCost w rs depth
let next := row termCost ntCost w prev.ret (depth + 1) rs 0
(w.size + 1)
⟨prev.ret.set! (depth + 1) next.ret.toArray,
prev.time + next.time + 2 * (w.size + 1) + 8⟩
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:159
Uses 2 · Used by 2
TheoremCYK table update leaves other lengths' cells unchanged#✓fully proved
Updating one length never changes another length's cells.
theorem lookup_table_other (tc nc : ℕ) (w : Array T)
(rs : List (ContextFreeRule T N)) (depth i len : ℕ)
(h : depth + 1 ≠ len) :
lookup (table tc nc w rs (depth + 1)).ret i len =
lookup (table tc nc w rs depth).ret i len
def lookup (tbl : Array (Array (List N))) (i len : ℕ) : List N :=
(tbl[len]?.getD #[])[i]?.getD []
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:68
def table (termCost ntCost : ℕ) (w : Array T)
(rs : List (ContextFreeRule T N)) :
ℕ → TimeM ℕ (Array (Array (List N)))
| 0 => ⟨Array.replicate (w.size + 1) #[], w.size + 4⟩
| depth + 1 =>
let prev := table termCost ntCost w rs depth
let next := row termCost ntCost w prev.ret (depth + 1) rs 0
(w.size + 1)
⟨prev.ret.set! (depth + 1) next.ret.toArray,
prev.time + next.time + 2 * (w.size + 1) + 8⟩
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:159
Uses 3 · Used by 2
TheoremNew CYK table row holds the computed cells#✓fully proved
The new length's row contains exactly the cells just computed.
theorem lookup_table_new (tc nc : ℕ) (w : Array T)
(rs : List (ContextFreeRule T N)) (depth i : ℕ)
(hd : depth < w.size) (hi : i < w.size + 1) :
lookup (table tc nc w rs (depth + 1)).ret i (depth + 1) =
(cell tc nc w (table tc nc w rs depth).ret i (depth + 1) rs).ret
def lookup (tbl : Array (Array (List N))) (i len : ℕ) : List N :=
(tbl[len]?.getD #[])[i]?.getD []
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:68
def table (termCost ntCost : ℕ) (w : Array T)
(rs : List (ContextFreeRule T N)) :
ℕ → TimeM ℕ (Array (Array (List N)))
| 0 => ⟨Array.replicate (w.size + 1) #[], w.size + 4⟩
| depth + 1 =>
let prev := table termCost ntCost w rs depth
let next := row termCost ntCost w prev.ret (depth + 1) rs 0
(w.size + 1)
⟨prev.ret.set! (depth + 1) next.ret.toArray,
prev.time + next.time + 2 * (w.size + 1) + 8⟩
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:159
def cell (termCost ntCost : ℕ) (w : Array T)
(tbl : Array (Array (List N))) (i len : ℕ) :
List (ContextFreeRule T N) → TimeM ℕ (List N)
| [] => ⟨[], 1⟩
| r :: rs =>
let f := fits termCost ntCost w tbl i len r.output
let rest := cell termCost ntCost w tbl i len rs
⟨if f.ret then r.input :: rest.ret else rest.ret,
f.time + rest.time + 5⟩
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:121
TheoremCorrectness of the completed indexed CYK table#✓fully proved
Every completed valid cell agrees with canonical grammar derivations.
theorem table_correct {g : ContextFreeGrammar T} [DecidableEq g.NT]
(hg : IsChomskyNormalForm g) (tc nc : ℕ) (w : List T)
(rs : List (ContextFreeRule T g.NT))
(hrs : ∀ r, r ∈ rs ↔ r ∈ g.rules) (depth : ℕ) :
∀ i len, 0 < len → len ≤ depth → i + len ≤ w.length → ∀ a,
a ∈ lookup (table tc nc w.toArray rs depth).ret i len ↔
Yields g [.nonterminal a] ((w.drop i).take len)
def IsChomskyNormalForm (g : ContextFreeGrammar T) : Prop :=
(∀ r ∈ g.rules,
(∃ B C, r.output = [Symbol.nonterminal B, Symbol.nonterminal C]) ∨
(∃ a, r.output = [Symbol.terminal a]) ∨
(r.output = [] ∧ r.input = g.initial)) ∧
((∀ r ∈ g.rules, Symbol.nonterminal g.initial ∉ r.output) ∨
∀ r ∈ g.rules, r.output ≠ [])
CSLibExt/Computability/Languages/ContextFree/ChomskyNormalForm.lean:57
def lookup (tbl : Array (Array (List N))) (i len : ℕ) : List N :=
(tbl[len]?.getD #[])[i]?.getD []
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:68
def table (termCost ntCost : ℕ) (w : Array T)
(rs : List (ContextFreeRule T N)) :
ℕ → TimeM ℕ (Array (Array (List N)))
| 0 => ⟨Array.replicate (w.size + 1) #[], w.size + 4⟩
| depth + 1 =>
let prev := table termCost ntCost w rs depth
let next := row termCost ntCost w prev.ret (depth + 1) rs 0
(w.size + 1)
⟨prev.ret.set! (depth + 1) next.ret.toArray,
prev.time + next.time + 2 * (w.size + 1) + 8⟩
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:159
inductive Yields (g : ContextFreeGrammar T) : List (Symbol T g.NT) → List T → Prop
| nil : Yields g [] []
| terminal (a : T) {u : List (Symbol T g.NT)} {w : List T} (h : Yields g u w) :
Yields g (Symbol.terminal a :: u) (a :: w)
| nonterminal {r : ContextFreeRule T g.NT} (hr : r ∈ g.rules)
{u : List (Symbol T g.NT)} {w₁ w₂ : List T}
(hout : Yields g r.output w₁) (hrest : Yields g u w₂) :
Yields g (Symbol.nonterminal r.input :: u) (w₁ ++ w₂)
CSLibExt/Computability/Languages/ContextFree/Yields.lean:48
Uses 8 · Used by 1
TheoremCorrectness of the CYK epsilon scan#✓fully proved
Epsilon scanning agrees with existence of a start epsilon production.
@[simp] theorem epsilon_ret (nc : ℕ) (start : N)
(rs : List (ContextFreeRule T N)) :
(epsilon nc start rs).ret = true ↔
∃ r ∈ rs, r.input = start ∧ r.output = []
def epsilon (ntCost : ℕ) (start : N) :
List (ContextFreeRule T N) → TimeM ℕ Bool
| [] => ⟨false, 1⟩
| r :: rs =>
let rest := epsilon ntCost start rs
⟨(decide (r.input = start) && r.output.isEmpty) || rest.ret,
rest.time + ntCost + 6⟩
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:171
Uses 1 · Used by 1
TheoremCorrectness of the indexed CYK recognizer#✓fully proved
The indexed recognizer decides the existing canonical language.
theorem recognizes_iff {g : ContextFreeGrammar T} [DecidableEq g.NT]
(hg : IsChomskyNormalForm g) (tc nc : ℕ)
(rs : List (ContextFreeRule T g.NT))
(hrs : ∀ r, r ∈ rs ↔ r ∈ g.rules) (w : List T) :
(recognizes tc nc g.initial rs w).ret = true ↔ w ∈ g.language
def IsChomskyNormalForm (g : ContextFreeGrammar T) : Prop :=
(∀ r ∈ g.rules,
(∃ B C, r.output = [Symbol.nonterminal B, Symbol.nonterminal C]) ∨
(∃ a, r.output = [Symbol.terminal a]) ∨
(r.output = [] ∧ r.input = g.initial)) ∧
((∀ r ∈ g.rules, Symbol.nonterminal g.initial ∉ r.output) ∨
∀ r ∈ g.rules, r.output ≠ [])
CSLibExt/Computability/Languages/ContextFree/ChomskyNormalForm.lean:57
def recognizes (termCost ntCost : ℕ) (start : N)
(rs : List (ContextFreeRule T N)) (w : List T) : TimeM ℕ Bool :=
match w with
| [] =>
let r := epsilon ntCost start rs
⟨r.ret, r.time + 1⟩
| _ :: _ =>
let a := w.toArray
let tbl := table termCost ntCost a rs a.size
let r := member ntCost start (lookup tbl.ret 0 a.size)
⟨r.ret, tbl.time + r.time + 3 * w.length + 12⟩
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:181
Uses 15 · Used by 2
TheoremIndexed CYK agrees with the list-table CYK recognizer#✓fully proved
Exact Boolean correspondence with the existing list-table recognizer.
theorem recognizes_eq_cykAccepts {g : ContextFreeGrammar T}
[DecidableEq g.NT] (hg : IsChomskyNormalForm g) (tc nc : ℕ)
(rs : List (ContextFreeRule T g.NT))
(hrs : ∀ r, r ∈ rs ↔ r ∈ g.rules) (w : List T) :
(recognizes tc nc g.initial rs w).ret = cykAccepts g w
def IsChomskyNormalForm (g : ContextFreeGrammar T) : Prop :=
(∀ r ∈ g.rules,
(∃ B C, r.output = [Symbol.nonterminal B, Symbol.nonterminal C]) ∨
(∃ a, r.output = [Symbol.terminal a]) ∨
(r.output = [] ∧ r.input = g.initial)) ∧
((∀ r ∈ g.rules, Symbol.nonterminal g.initial ∉ r.output) ∨
∀ r ∈ g.rules, r.output ≠ [])
CSLibExt/Computability/Languages/ContextFree/ChomskyNormalForm.lean:57
def recognizes (termCost ntCost : ℕ) (start : N)
(rs : List (ContextFreeRule T N)) (w : List T) : TimeM ℕ Bool :=
match w with
| [] =>
let r := epsilon ntCost start rs
⟨r.ret, r.time + 1⟩
| _ :: _ =>
let a := w.toArray
let tbl := table termCost ntCost a rs a.size
let r := member ntCost start (lookup tbl.ret 0 a.size)
⟨r.ret, tbl.time + r.time + 3 * w.length + 12⟩
CSLibExt/Computability/Languages/ContextFree/CYKIndexed.lean:181
def cykAccepts (w : List T) : Bool :=
if w = [] then decide (∃ r ∈ g.rules, r.input = g.initial ∧ r.output = [])
else decide (g.initial ∈ cykLookup (cykTable g w w.length) w)
CSLibExt/Computability/Languages/ContextFree/CYK.lean:382
Uses 5
Loading dependency explorer…
The dependency explorer needs JavaScript and its graph data.