Applying an effect to the original stacks to Reading back a list from its cells
DefinitionApplying an effect to the original stacks#✓fully proved
Let \(S_0\) assign a list \(S_0(k)\) to each stack \(k\), and let \(r = (\ell, v, P, c)\) consist of a label \(\ell\) (possibly the halted label), a state \(v\), a list \(P(k)\) for each stack \(k\) and a natural number \(c(k)\) for each stack \(k\). This is the configuration with label \(\ell\), state \(v\), and stack \(k\) equal to \(P(k)\) followed by \(S_0(k)\) with its first \(c(k)\) symbols (from the top) removed.
Applied to an effect, \(P(k)\) is the pushed prefix still on top of stack \(k\) and \(c(k)\) is the net number of original symbols consumed.
TheoremReading the top through the windows#✓fully proved
Let \(d \in \mathbb{N}\), let \(S_0\) and \(P\) assign a list to each stack, let \(c\) assign a natural number to each stack, and let \(k\) be a stack with \(c(k) < d\). Reading the top of stack \(k\) from the depth-\(d\) windows of the lists \(S_0\), with pushed symbols \(P\) and consumption counts \(c\), gives the first symbol of \(P(k)\) followed by \(S_0(k)\) with its first \(c(k)\) symbols removed, or ⊥ if that list is empty.
theorem readTop_eq (d : ℕ) (S0 P : ∀ k, List (Γ k)) (c : K → ℕ) (k : K) (hc : c k < d) :
readTop (fun k => window d (S0 k)) P c k = (P k ++ (S0 k).drop (c k)).head?
def readTop (W : ∀ k, List (Option (Γ k))) (P : ∀ k, List (Γ k)) (c : K → ℕ) (k : K) :
Option (Γ k) :=
match P k with
| x :: _ => some x
| [] => (W k).getD (c k) none
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:70
def window {α : Type} (d : ℕ) (L : List α) : List (Option α) :=
(List.range d).map fun j => L[j]?
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:596
Uses 2 · Used by 1
TheoremThe symbolic effect of a statement agrees with the real step#✓fully proved
Let \(q\) be a statement tree of a multi-stack machine, \(d \in \mathbb{N}\), and \(S_0\) an assignment of a list \(S_0(k)\) to each stack \(k\). Let \(v\) be a state, \(P\) an assignment of a list \(P(k)\) to each stack, and \(c\) an assignment of a natural number \(c(k)\) to each stack with \(c(k) + \operatorname{ops}(q) \le d\) for every \(k\). Then running \(q\) from state \(v\) on the stacks whose stack \(k\) is \(P(k)\) followed by \(S_0(k)\) with its first \(c(k)\) symbols removed gives the configuration obtained by applying to \(S_0\) the effect of \(q\) computed from \(v\), \(P\), \(c\) and the depth-\(d\) windows of the \(S_0(k)\).
Here \(\operatorname{ops}(q)\) is the largest number of push, peek and pop nodes on a path of \(q\), so the hypothesis keeps every read inside the windows.
theorem eff_agree (d : ℕ) (S0 : ∀ k, List (Γ k)) (q : TM2.Stmt Γ Λ σ) :
∀ (v : σ) (P : ∀ k, List (Γ k)) (c : K → ℕ), (∀ k, c k + ops q ≤ d) →
TM2.stepAux q v (fun k => P k ++ (S0 k).drop (c k)) =
applyEff S0 (eff (fun k => window d (S0 k)) q v P c)
def ops : TM2.Stmt Γ Λ σ → ℕ
| .push _ _ q => ops q + 1
| .peek _ _ q => ops q + 1
| .pop _ _ q => ops q + 1
| .load _ q => ops q
| .branch _ q₁ q₂ => max (ops q₁) (ops q₂)
| .goto _ => 0
| .halt => 0
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:92
def applyEff (S0 : ∀ k, List (Γ k)) (r : Option Λ × σ × (∀ k, List (Γ k)) × (K → ℕ)) :
TM2.Cfg Γ Λ σ :=
⟨r.1, r.2.1, fun k => r.2.2.1 k ++ (S0 k).drop (r.2.2.2 k)⟩
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:601
def eff (W : ∀ k, List (Option (Γ k))) :
TM2.Stmt Γ Λ σ → σ → (∀ k, List (Γ k)) → (K → ℕ) →
Option Λ × σ × (∀ k, List (Γ k)) × (K → ℕ)
| .push k f q, v, P, c => eff W q v (update P k (f v :: P k)) c
| .peek k f q, v, P, c => eff W q (f v (readTop W P c k)) P c
| .pop k f q, v, P, c =>
eff W q (f v (readTop W P c k)) (update P k (P k).tail)
(if (P k).isEmpty then update c k (c k + 1) else c)
| .load a q, v, P, c => eff W q (a v) P c
| .branch p q₁ q₂, v, P, c => cond (p v) (eff W q₁ v P c) (eff W q₂ v P c)
| .goto f, v, P, c => (some (f v), v, P, c)
| .halt, v, P, c => (none, v, P, c)
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:78
def window {α : Type} (d : ℕ) (L : List α) : List (Option α) :=
(List.range d).map fun j => L[j]?
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:596
Uses 6 · Used by 1
DefinitionIdle-extended step#✓fully proved
For a finite TM2 machine \(M\) and a configuration \(c\), the idle-extended step of \(M\) at \(c\) is the configuration that one step of \(M\) produces from \(c\) when the label of \(c\) is not the halted label, and is \(c\) itself when it is (a halted configuration stays put).
Used by 11
DefinitionIdle-extended run#✓fully proved
For a finite TM2 machine \(M\) and a configuration \(c\), the idle-extended run of \(M\) from \(c\) is the sequence of configurations whose term at time \(0\) is \(c\) and whose term at time \(t + 1\) is the idle-extended step of \(M\) applied to its term at time \(t\).
def runI (M : FinTM2) (c : M.Cfg) : ℕ → M.Cfg
| 0 => c
| t + 1 => stepI M (runI M c t)
def stepI (M : FinTM2) (c : M.Cfg) : M.Cfg := (TM2.step M.m c).getD c
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:688
Uses 1 · Used by 37
DefinitionWindows of a configuration#✓fully proved
Let \(M\) be a finite TM2 machine with window depth \(d\) and let \(c\) be a configuration. The window of \(c\) on stack \(k\) is the list of length \(d\) whose entry at position \(j < d\) is the symbol at depth \(j\) of stack \(k\) (counted from the top, starting at \(0\)) when the stack has more than \(j\) symbols, and the empty cell \(\bot\) otherwise.
def winC (M : FinTM2) (c : M.Cfg) : ∀ k, List (Option (M.Γ k)) :=
fun k => window (depth M) (c.stk k)
def window {α : Type} (d : ℕ) (L : List α) : List (Option α) :=
(List.range d).map fun j => L[j]?
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:596
def depth (M : FinTM2) : ℕ :=
letI := M.ΛFin
Finset.univ.sup fun l => ops (M.m l)
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:231
TheoremThe idle-extended step is the situation step applied to the stacks#✓fully proved
Let \(M\) be a finite TM2 machine and \(c\) a configuration with label \(l\), state \(v\) and stacks \(S\), and let \((l', v', P, n)\) be the situation step of \(M\) at label \(l\) and state \(v\) on the windows of \(c\). Then the idle-extended step of \(M\) at \(c\) is the configuration with label \(l'\), state \(v'\) and, on each stack \(k\), the list \(P_k\) placed on top of what remains of \(S_k\) after removing its top \(n_k\) symbols.
This is an equality of full configurations and needs no bound on the stack heights.
theorem step_agree (M : FinTM2) (c : M.Cfg) :
stepI M c = applyEff c.stk (sitStep M c.l c.var (winC M c))
def stepI (M : FinTM2) (c : M.Cfg) : M.Cfg := (TM2.step M.m c).getD c
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:688
def applyEff (S0 : ∀ k, List (Γ k)) (r : Option Λ × σ × (∀ k, List (Γ k)) × (K → ℕ)) :
TM2.Cfg Γ Λ σ :=
⟨r.1, r.2.1, fun k => r.2.2.1 k ++ (S0 k).drop (r.2.2.2 k)⟩
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:601
def sitStep (M : FinTM2) (l : Option M.Λ) (v : M.σ) (W : ∀ k, List (Option (M.Γ k))) :
Option M.Λ × M.σ × (∀ k, List (M.Γ k)) × (M.K → ℕ) :=
match l with
| none => (none, v, fun _ => [], fun _ => 0)
| some l => eff W (M.m l) v (fun _ => []) (fun _ => 0)
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:240
def winC (M : FinTM2) (c : M.Cfg) : ∀ k, List (Option (M.Γ k)) :=
fun k => window (depth M) (c.stk k)
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:696
Uses 9 · Used by 3
DefinitionLabel-and-state code of a configuration#✓fully proved
For an encoding \(E\) of a machine \(M\) and a configuration \(c\) of \(M\), this is the number that \(E\)'s (label, state) numbering assigns to the pair formed by the label of \(c\) (possibly the halted label) and the state of \(c\).
def code (c : M.Cfg) : ℕ := E.lc (c.l, c.var)
Uses 1 · Used by 30
DefinitionCell code of a configuration#✓fully proved
For an encoding \(E\) of a machine \(M\), a configuration \(c\) of \(M\) and natural numbers \(k\) and \(j\), this is the code under \(E\) of the symbol at position \(j\) (counting from \(0\) at the top) of the stack that \(E\)'s inverse stack numbering assigns to \(k\), in \(c\); it is \(0\) (the code of ⊥) if that stack has at most \(j\) symbols.
def cell (c : M.Cfg) (k j : ℕ) : ℕ := ((c.stk (E.kd k))[j]?).elim 0 (E.enc (E.kd k))
Uses 1 · Used by 41
DefinitionSituation index of a configuration#✓fully proved
Fix an encoding \(E\) of a finite TM2 machine \(M\), with \(K\) stacks, symbol-code bound \(g\) and window depth \(d\). The situation index of a configuration \(c\) is the number
where \(\mathrm{code}(c)\) is the combined (label, state) code of \(c\) and \(\mathrm{cell}_c(k, j)\) is the cell code of \(c\) on stack number \(k\) at depth \(j\) (\(0\) for the empty cell).
def sitIdx (c : M.Cfg) : ℕ :=
mix E.g (E.code c) (E.KK * depth M) fun n => E.cell c (n / depth M) (n % depth M)
def mix (b c : ℕ) : ℕ → (ℕ → ℕ) → ℕ
| 0, _ => c
| m + 1, D => D 0 + b * mix b c m (fun n => D (n + 1))
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:399
def depth (M : FinTM2) : ℕ :=
letI := M.ΛFin
Finset.univ.sup fun l => ops (M.m l)
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:231
Uses 6 · Used by 27
DefinitionThe table-driven row update#✓fully proved
Fix an encoding \(E\) of a finite TM2 machine, a row height \(H\), a situation \(i\) and an old row \(\mathrm{old}\) giving a code for each stack number \(k\) and depth \(j\). For a stack number \(k\), write \(p\) for the number of symbols situation \(i\) leaves pushed on stack \(k\) and \(n\) for the number of original symbols it consumes from stack \(k\). The updated row at \((k, j)\) is the code of the symbol situation \(i\) leaves at depth \(j\) of stack \(k\) if \(j < p\); otherwise it is \(\mathrm{old}(k, j - p + n)\) if \(j < (H \mathbin{\dot{-}} \max(p, n)) + p\), where \(\dot{-}\) is truncated subtraction; otherwise it is \(0\), the empty cell.
These are the three cases the shift clauses force: pushed fill, in-range shift and overflow.
def tabRow (H i : ℕ) (old : ℕ → ℕ → ℕ) (k j : ℕ) : ℕ :=
if j < E.pl i k then E.pu i k j
else if j < H - max (E.pl i k) (E.cn i k) + E.pl i k then old k (j - E.pl i k + E.cn i k)
else 0
Uses 4 · Used by 14
DefinitionGood configurations#✓fully proved
For an encoding \(E\) of a machine \(M\), a configuration \(c\) of \(M\) is good when every symbol on every stack \(k\) of \(c\) is one of the effective symbols of stack \(k\) listed by \(E\) (so that it has a code).
def Good (c : M.Cfg) : Prop := ∀ k, ∀ x ∈ c.stk k, ∃ n, E.dec k n = x
Uses 1 · Used by 27
TheoremThe situation index matches the code and windows#✓fully proved
Fix an encoding \(E\) of a finite TM2 machine \(M\), with \(K\) stacks, \(N\) situations and window depth \(d\). If every symbol on every stack of a configuration \(c\) lies in the effective alphabet of \(E\), then the situation index \(i\) of \(c\) satisfies \(i < N\), the label/state code of situation \(i\) equals the combined (label, state) code of \(c\), and for all \(k < K\) and \(j < d\) the window symbol code of situation \(i\) on stack \(k\) at depth \(j\) equals the cell code of \(c\) on stack number \(k\) at depth \(j\).
theorem sitIdx_spec (c : M.Cfg) (hc : E.Good c) :
E.sitIdx c < E.N ∧ E.la (E.sitIdx c) = E.code c ∧
∀ k < E.KK, ∀ j < depth M, E.wa (E.sitIdx c) k j = E.cell c k j
def depth (M : FinTM2) : ℕ :=
letI := M.ΛFin
Finset.univ.sup fun l => ops (M.m l)
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:231
Uses 12 · Used by 12
TheoremCells of a pushed prefix over a truncated stack#✓fully proved
Let \(e\) map elements of \(\alpha\) to natural numbers, and for a list \(L\) over \(\alpha\) and \(i \in \mathbb{N}\) let \(L\langle i\rangle\) be \(e\) of the entry of \(L\) at position \(i\) (counting from \(0\)), or \(0\) if \(L\) has at most \(i\) entries. Subtraction of natural numbers is truncated at \(0\). Let \(P\) and \(S\) be lists over \(\alpha\) and \(n, H, d, j \in \mathbb{N}\) with \(|P| \le d\), \(n \le d\) and \(|S| \le H - d\), and let \(Q\) be \(P\) followed by \(S\) with its first \(n\) entries removed. If \(j < |P|\) then \(Q\langle j\rangle = P\langle j\rangle\); otherwise, if \(j < (H - \max(|P|, n)) + |P|\) then \(Q\langle j\rangle = S\langle j - |P| + n\rangle\); and otherwise \(Q\langle j\rangle = 0\).
theorem row_cell {α : Type} (e : α → ℕ) (P S : List α) (n H d j : ℕ) (hP : P.length ≤ d)
(hn : n ≤ d) (hS : S.length ≤ H - d) :
((P ++ S.drop n)[j]?).elim 0 e =
if j < P.length then (P[j]?).elim 0 e
else if j < H - max P.length n + P.length then (S[j - P.length + n]?).elim 0 e
else 0
Used by 1
TheoremThe table-driven step#✓fully proved
Fix an encoding \(E\) of a finite TM2 machine \(M\), with \(K\) stacks and window depth \(d\), and a natural number \(H\). Let \(c\) be a configuration such that every symbol on every stack of \(c\) lies in the effective alphabet of \(E\) and every stack of \(c\) has length at most \(H \mathbin{\dot{-}} d\) (truncated subtraction). Let \(c'\) be the idle-extended step of \(M\) at \(c\) and \(i\) the situation index of \(c\). Then the combined (label, state) code of \(c'\) is the next label/state code after situation \(i\), and for all \(k < K\) and \(j < H\) the cell code of \(c'\) on stack number \(k\) at depth \(j\) is the value at \((k, j)\) of the table-driven row update with height \(H\) and situation \(i\) applied to the cell codes of \(c\).
No hypothesis \(d \le H\) is needed: if \(H < d\), every stack of \(c\) is empty.
theorem table_agree (H : ℕ) (c : M.Cfg) (hc : E.Good c)
(hh : ∀ k, (c.stk k).length ≤ H - depth M) :
E.code (stepI M c) = E.na (E.sitIdx c) ∧
∀ k < E.KK, ∀ j < H, E.cell (stepI M c) k j = E.tabRow H (E.sitIdx c) (E.cell c) k j
def depth (M : FinTM2) : ℕ :=
letI := M.ΛFin
Finset.univ.sup fun l => ops (M.m l)
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:231
def stepI (M : FinTM2) (c : M.Cfg) : M.Cfg := (TM2.step M.m c).getD c
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:688
Uses 28 · Used by 2
TheoremReads of the in-range shift stay in the row#✓fully proved
Fix an encoding \(E\) of a finite TM2 machine and natural numbers \(H\), \(i\), \(k\) and \(j\). Write \(p\) for the number of symbols situation \(i\) leaves pushed on stack number \(k\) and \(n\) for the number of original symbols it consumes from stack number \(k\). If \(p \le j\) and \(j < (H \mathbin{\dot{-}} \max(p, n)) + p\), where \(\dot{-}\) is truncated subtraction, then \(j - p + n < H\).
So every old cell that the table-driven row update reads lies in the row of height \(H\).
theorem tabRow_reads_lt (H i k j : ℕ) (hpl : E.pl i k ≤ j)
(hj : j < H - max (E.pl i k) (E.cn i k) + E.pl i k) :
j - E.pl i k + E.cn i k < H
Uses 3
TheoremThe idle-extended step preserves good configurations#✓fully proved
Let \(E\) be an encoding of a finite TM2 machine \(M\) and let \(c\) be a configuration of \(M\). If \(c\) is good for \(E\), then so is the idle-extended step of \(M\) at \(c\).
theorem good_step (c : M.Cfg) (hc : E.Good c) : E.Good (stepI M c)
def stepI (M : FinTM2) (c : M.Cfg) : M.Cfg := (TM2.step M.m c).getD c
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:688
Uses 10 · Used by 13
TheoremThe table-driven step along a run#✓fully proved
Fix an encoding \(E\) of a finite TM2 machine \(M\), with \(K\) stacks, \(N\) situations and window depth \(d\), a natural number \(H\), an input list \(s\) and a time \(t\). For each time \(u\) let \(c_u\) be the configuration at time \(u\) of the idle-extended run of \(M\) from the initial configuration of \(M\) on input \(s\), and let \(i\) be the situation index of \(c_t\). If every stack of \(c_t\) has length at most \(H \mathbin{\dot{-}} d\) (truncated subtraction), then \(i < N\); the label/state code of situation \(i\) is the combined (label, state) code of \(c_t\); for all \(k < K\) and \(j < d\) the window symbol code of situation \(i\) on stack \(k\) at depth \(j\) is the cell code of \(c_t\) on stack number \(k\) at depth \(j\); the combined (label, state) code of \(c_{t+1}\) is the next label/state code after situation \(i\); and for all \(k < K\) and \(j < H\) the cell code of \(c_{t+1}\) on stack number \(k\) at depth \(j\) is the value at \((k, j)\) of the table-driven row update with height \(H\) and situation \(i\) applied to the cell codes of \(c_t\).
Unlike the single-step version, no alphabet hypothesis is needed: every symbol on a stack of such a run lies in the effective alphabet of \(E\).
theorem table_agree_run (H : ℕ) (s : List (M.Γ M.k₀)) (t : ℕ)
(hh : ∀ k, ((runI M (initList M s) t).stk k).length ≤ H - depth M) :
let c := runI M (initList M s) t
E.sitIdx c < E.N ∧ E.la (E.sitIdx c) = E.code c ∧
(∀ k < E.KK, ∀ j < depth M, E.wa (E.sitIdx c) k j = E.cell c k j) ∧
E.code (runI M (initList M s) (t + 1)) = E.na (E.sitIdx c) ∧
∀ k < E.KK, ∀ j < H,
E.cell (runI M (initList M s) (t + 1)) k j = E.tabRow H (E.sitIdx c) (E.cell c) k j
def runI (M : FinTM2) (c : M.Cfg) : ℕ → M.Cfg
| 0 => c
| t + 1 => stepI M (runI M c t)
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:691
def depth (M : FinTM2) : ℕ :=
letI := M.ΛFin
Finset.univ.sup fun l => ops (M.m l)
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:231
Uses 15 · Used by 7
DefinitionReading a list from a sequence of cells#✓fully proved
For \(f\) mapping each natural number to an element of \(\alpha\) or to ⊥, and \(j, m \in \mathbb{N}\), this is the list \([x_0, \dots, x_{r-1}]\) with \(f(j + i) = x_i\) for each \(i < r\), where \(r = m\) if none of \(f(j), \dots, f(j + m - 1)\) is ⊥, and otherwise \(r\) is the least \(i < m\) with \(f(j + i) = \bot\).
def decS (f : ℕ → Option α) : ℕ → ℕ → List α
| _, 0 => []
| j, m + 1 => match f j with
| none => []
| some x => x :: decS f (j + 1) m
Used by 7
TheoremReading back a list from its cells#✓fully proved
Let \(f\) map each natural number to an element of \(\alpha\) or to ⊥, let \(S\) be a list over \(\alpha\), and let \(j, m \in \mathbb{N}\). Suppose that for every \(i \in \mathbb{N}\), \(f(j + i)\) is the entry of \(S\) at position \(i\) (counting from \(0\)) when \(i\) is less than the length of \(S\), and ⊥ otherwise. If \(S\) has length at most \(m\), then reading \(f\) from position \(j\) for at most \(m\) positions, stopping at the first ⊥, gives \(S\).
theorem decS_of_list (f : ℕ → Option α) : ∀ (S : List α) (j m : ℕ),
(∀ i, f (j + i) = S[i]?) → S.length ≤ m → decS f j m = S
def decS (f : ℕ → Option α) : ℕ → ℕ → List α
| _, 0 => []
| j, m + 1 => match f j with
| none => []
| some x => x :: decS f (j + 1) m
CSLibExt/Computability/Complexity/CookLevin/Situation.lean:869
Uses 1 · Used by 1
Loading dependency explorer…
The dependency explorer needs JavaScript and its graph data.