introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
5.84 kB
import OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.StructuredSpace
import OAI.Algorithms.SubexponentialSAT.StackRoutines
/-! Every-prefix storage of the actual preserving copy table. Empty destination
and temporary stacks permit the original per-stack envelope to be reused. -/
namespace OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.CopySpace
open BitProgram
private def copyWeight {r s : ℕ} (src dst tmp : Fin s) (present : Fin r)
(a : Cfg r s 8) : ℕ :=
let x := (a.stacks src).length
let y := (a.stacks dst).length
let z := (a.stacks tmp).length
match a.pc with
| none => max x y + z
| some pc =>
if pc = 0 then x + y + z
else if pc = 1 then x + y + z + (if a.regs present then 1 else 0)
else if pc = 2 then x + y + z + 1
else if pc = 4 then max x y + z + (if a.regs present then 1 else 0)
else if pc = 5 then max x y + z + 1
else if pc = 6 then max x (y + 1) + z
else max x y + z
private theorem copyWeight_step {r s : ℕ} (src dst tmp : Fin s)
(value present : Fin r) (hsd : src ≠ dst) (hst : src ≠ tmp) (htd : tmp ≠ dst)
{a b : Cfg r s 8}
(hstep : step (StackRoutines.copyProgram src dst tmp value present) a = some b) :
copyWeight src dst tmp present b ≤ copyWeight src dst tmp present a := by
cases hpc : a.pc with
| none => simp [step, hpc] at hstep
| some pc =>
simp only [step, hpc, Option.map, Option.some.injEq] at hstep
rw [← hstep]
fin_cases pc
· cases hx : a.stacks src <;>
simp [copyWeight, execInstr, hpc, hx, readRegs, hsd.symm, hst.symm]
· cases hr : a.regs present <;>
simp [copyWeight, execInstr, hpc, hr]
· simp [copyWeight, execInstr, hpc, hst, htd.symm]
omega
· cases hx : a.stacks tmp <;>
simp [copyWeight, execInstr, hpc, hx, readRegs, hst, htd.symm]
· cases hr : a.regs present <;>
simp [copyWeight, execInstr, hpc, hr]
· simp [copyWeight, execInstr, hpc, hsd.symm, hst.symm]
omega
· simp [copyWeight, execInstr, hpc, hsd, htd]
· simp [copyWeight, execInstr, hpc]
private theorem copyWeight_trace {r s steps : ℕ} (src dst tmp : Fin s)
(value present : Fin r) (hsd : src ≠ dst) (hst : src ≠ tmp) (htd : tmp ≠ dst)
{a b : Cfg r s 8}
(ht : Trace (StackRoutines.copyProgram src dst tmp value present) steps a b) :
copyWeight src dst tmp present b ≤ copyWeight src dst tmp present a := by
induction ht with
| refl => exact le_rfl
| cons hs _ ih => exact ih.trans (copyWeight_step src dst tmp value present hsd hst htd hs)
theorem copyProgram_frame {r s steps : ℕ} (src dst tmp : Fin s)
(value present : Fin r) {a b : Cfg r s 8}
(ht : Trace (StackRoutines.copyProgram src dst tmp value present) steps a b)
(i : Fin s) (his : i ≠ src) (hid : i ≠ dst) (hit : i ≠ tmp) :
b.stacks i = a.stacks i := by
have hsingle {a b : Cfg r s 8}
(hs : step (StackRoutines.copyProgram src dst tmp value present) a = some b) :
b.stacks i = a.stacks i := by
cases hpc : a.pc with
| none => simp [step, hpc] at hs
| some pc =>
simp only [step, hpc, Option.map, Option.some.injEq] at hs
rw [← hs]
fin_cases pc <;> simp [execInstr, his, hid, hit]
induction ht with
| refl => rfl
| cons hs _ ih => exact ih.trans (hsingle hs)
private theorem ports_le_copyWeight {r s : ℕ} (src dst tmp : Fin s) (present : Fin r)
(a : Cfg r s 8) :
(a.stacks src).length ≤ copyWeight src dst tmp present a ∧
(a.stacks dst).length ≤ copyWeight src dst tmp present a ∧
(a.stacks tmp).length ≤ copyWeight src dst tmp present a := by
unfold copyWeight
cases a.pc with
| none => dsimp; omega
| some pc => dsimp; split_ifs <;> omega
/-- Exact local copying never exceeds the source's original word length;
every off-port caller word retains its own independent bound. -/
theorem copyProgram_stack_bound {r s steps frameBound : ℕ} (src dst tmp : Fin s)
(value present : Fin r) (hsd : src ≠ dst) (hst : src ≠ tmp) (htd : tmp ≠ dst)
{a b : Cfg r s 8} (hentry : a.pc = some 0)
(hdst : a.stacks dst = []) (htmp : a.stacks tmp = [])
(hframe : ∀ i, i ≠ src → i ≠ dst → i ≠ tmp → (a.stacks i).length ≤ frameBound)
(ht : Trace (StackRoutines.copyProgram src dst tmp value present) steps a b) :
∀ i, (b.stacks i).length ≤ max (a.stacks src).length frameBound := by
have hweight : copyWeight src dst tmp present b ≤ (a.stacks src).length := by
have h := copyWeight_trace src dst tmp value present hsd hst htd ht
simpa [copyWeight, hentry, hdst, htmp] using h
obtain ⟨hsrc, hdst', htmp'⟩ := ports_le_copyWeight src dst tmp present b
intro i
by_cases hi : i = src
· subst i
exact (hsrc.trans hweight).trans (Nat.le_max_left _ _)
by_cases hd : i = dst
· subst i
exact (hdst'.trans hweight).trans (Nat.le_max_left _ _)
by_cases hw : i = tmp
· subst i
exact (htmp'.trans hweight).trans (Nat.le_max_left _ _)
rw [copyProgram_frame src dst tmp value present ht i hi hd hw]
exact (hframe i hi hd hw).trans (Nat.le_max_right _ _)
theorem copyProgram_no_growth {r s steps bound : ℕ} (src dst tmp : Fin s)
(value present : Fin r) (hsd : src ≠ dst) (hst : src ≠ tmp) (htd : tmp ≠ dst)
{a b : Cfg r s 8} (hentry : a.pc = some 0)
(hdst : a.stacks dst = []) (htmp : a.stacks tmp = [])
(ha : ∀ i, (a.stacks i).length ≤ bound)
(ht : Trace (StackRoutines.copyProgram src dst tmp value present) steps a b) :
∀ i, (b.stacks i).length ≤ bound := by
intro i
exact (copyProgram_stack_bound src dst tmp value present hsd hst htd hentry hdst htmp
(fun j _ _ _ => ha j) ht i).trans (max_le (ha src) le_rfl)
end OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.CopySpace