Indexed CYK primitives

DefinitionCharged list-membership scan#✓fully proved

Full membership scan, charging equality and list/control operations.

def member (eqCost : ℕ) (a : N) : List N → TimeM ℕ Bool
  | [] => ⟨false, 1⟩
  | b :: bs =>
      let rest := member eqCost a bs
      ⟨decide (a = b) || rest.ret, rest.time + eqCost + 3⟩
Used by 8

TheoremCharged membership scan agrees with list membership#✓fully proved

The charged scan has ordinary list-membership semantics.

@[simp] theorem member_ret (eqCost : ℕ) (a : N) (xs : List N) :
    (member eqCost a xs).ret = true ↔ a ∈ xs
Uses 1 · Used by 2

DefinitionCharged scan of CYK split positions#✓fully proved

Scan split positions 1,...,count, with two table reads per split. The 16 units cover four array reads, bounds/default tests, index arithmetic, Boolean combinations and recursion control.

def splits (eqCost : ℕ) (tbl : Array (Array (List N)))
    (i len : ℕ) (b c : N) : ℕ → TimeM ℕ Bool
  | 0 => ⟨false, 1⟩
  | k + 1 =>
      let rest := splits eqCost tbl i len b c k
      let left := member eqCost b (lookup tbl i (k + 1))
      let right := member eqCost c (lookup tbl (i + (k + 1)) (len - (k + 1)))
      ⟨rest.ret || (left.ret && right.ret),
        rest.time + left.time + right.time + 16⟩
Uses 2 · Used by 5

TheoremSuccessful CYK split scans find a split in range#✓fully proved

Every successful scan names a split in exactly the scanned interval.

theorem splits_ret (eqCost : ℕ) (tbl : Array (Array (List N)))
    (i len : ℕ) (b c : N) (count : ℕ) :
    (splits eqCost tbl i len b c count).ret = true ↔
      ∃ k, 0 < k ∧ k ≤ count ∧ b ∈ lookup tbl i k ∧
        c ∈ lookup tbl (i + k) (len - k)
Uses 4 · Used by 1

DefinitionProduction-output fit test for indexed CYK#✓fully proved

Test a production output without constructing or comparing substrings. Pattern inspection is bounded (at most three cons and two symbol tags).

def fits (termCost ntCost : ℕ) (w : Array T)
    (tbl : Array (Array (List N))) (i len : ℕ) :
    List (Symbol T N) → TimeM ℕ Bool
  | [.terminal a] =>
      ⟨decide (len = 1) && decide (w[i]? = some a), termCost + 12⟩
  | [.nonterminal b, .nonterminal c] =>
      let r := splits ntCost tbl i len b c (len - 1)
      ⟨r.ret, r.time + 8⟩
  | _ => ⟨false, 8⟩
Uses 1 · Used by 6

DefinitionCharged computation of one CYK cell#✓fully proved

Scan every production once, retaining its input exactly when it fits. Duplicates are allowed; no set insertion or uncharged deduplication occurs.

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⟩
Uses 1 · Used by 10

TheoremCYK cell members come from scanned productions#✓fully proved

Membership in a computed cell names an actual scanned production.

theorem mem_cell (termCost ntCost : ℕ) (w : Array T)
    (tbl : Array (Array (List N))) (i len : ℕ)
    (rs : List (ContextFreeRule T N)) (a : N) :
    a ∈ (cell termCost ntCost w tbl i len rs).ret ↔
      ∃ r ∈ rs, r.input = a ∧
        (fits termCost ntCost w tbl i len r.output).ret = true
Uses 2 · Used by 1

DefinitionCharged construction of one CYK row#✓fully proved

Build one row in increasing start-position order. Each cell is evaluated once against the already completed shorter rows.

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⟩
Uses 1 · Used by 9

DefinitionCharged construction of the indexed CYK table#✓fully proved

Fill rows 1,...,depth in a fixed outer array of size w.size + 1. Row conversion is linear; the outer update is an indexed primitive.

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⟩
Uses 1 · Used by 9

DefinitionCharged scan for start epsilon productions#✓fully proved

Scan start epsilon rules; output emptiness is a single list-tag test.

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⟩
Used by 5

DefinitionCharged indexed CYK recognizer#✓fully proved

The charged indexed recognizer. The nonempty branch pays for the input length scan, conversion, completed table, final lookup and membership.

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⟩
Uses 4 · Used by 7
12 fully proved

The dependency explorer needs JavaScript and its graph data.

Download the complete graph data.