Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RotationBST.lean
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public import RotationBST.Models.Region.View.Abstract
public import RotationBST.Models.Geometric.Abstract
public import RotationBST.Models.Equivalence.Abstract
public import RotationBST.Models.Dictionary.Abstract
public import RotationBST.Models.RealWorld.Abstract
public import RotationBST.Models.RealWorld.PointerMachine.Abstract
public import RotationBST.Models.RealWorld.Linked.Abstract
public import RotationBST.Models.RealWorld.Compiler.Abstract

public import RotationBST.Models.Labelled.Search.Abstract
public import RotationBST.Models.LinkedSearch.Abstract
Expand Down
36 changes: 36 additions & 0 deletions RotationBST/Models/RealWorld/Abstract.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module

import RotationBST.Audit.Check
public import RotationBST.Models.RealWorld.AbstractBridge

@[expose] public section
namespace RotationBST.RealWorld.Results

theorem physical {w f r l : Nat} {p : Machine f r l}
{s t : State w f r l} {time cost : Nat} (h : Run p s t time cost) :
Pointer.Trace s.cursor.erase t.cursor.erase cost := run_physical h

theorem bst_cost {w f r l : Nat} {p : Machine f r l}
{s t : State w f r l} {time cost : Nat} (h : Run p s t time cost) :
cost ≤ time := cost_le_time h

theorem causal {w f r l : Nat} {p : Machine f r l}
{s t u : State w f r l} {xs : List (BitVec w)} {a b c d : Nat}
(h : Execution p s xs t a b) (k : Execution p s xs u c d) :
t = u ∧ a = c ∧ b = d := execution_unique h k

theorem append {w f r l : Nat} {p : Machine f r l}
{s t u : State w f r l} {xs ys : List (BitVec w)} {a b c d : Nat}
(h : Execution p s xs t a b) (k : Execution p t ys u c d) :
Execution p s (xs ++ ys) u (a + c) (b + d) := execution_append h k

end RotationBST.RealWorld.Results

#check_bst_abstract RotationBST.RealWorld.Results allowing [
RotationBST.Models.RealWorld.Definitions.Abstract,
RotationBST.Models.Labelled.Cursor.Definitions.Abstract,
RotationBST.Models.Multifinger.Program.Definitions.Abstract,
RotationBST.Models.Multifinger.Definitions.Abstract,
RotationBST.Models.Labelled.Abstract,
RotationBST.Models.Pointer.Machine.Abstract,
RotationBST.Models.Rotation.Abstract]
3 changes: 3 additions & 0 deletions RotationBST/Models/RealWorld/AbstractBridge.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module

public import RotationBST.Models.RealWorld.Proofs
62 changes: 62 additions & 0 deletions RotationBST/Models/RealWorld/Compiler/Abstract.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module

import RotationBST.Audit.Check
public import RotationBST.Models.RealWorld.Compiler.AbstractBridge

@[expose] public section
namespace RotationBST.RealWorld.Compiler.Results

/-- Compile every successful source request into one fixed pointer-machine
program, with charged dispatch, request setup and halt. -/
theorem response {w f r l : Nat} (p : Machine f r l)
{s u : State w f r l} {t : Target w f r} {key : BitVec w} {time cost : Nat}
(width : 0 < w) (image : Image s t)
(source : RealWorld.Responds p s key u time cost) :
∃ v actual, actual ≤ 19 * time + 3 ∧ Responds p t key v actual ∧ Image u v :=
response_simulation p width image source

/-- The compiled code is reused unchanged across the whole online sequence. -/
theorem execution {w f r l : Nat} (p : Machine f r l)
{s u : State w f r l} {t : Target w f r} {xs : List (BitVec w)} {time cost : Nat}
(width : 0 < w) (image : Image s t)
(source : RealWorld.Execution p s xs u time cost) :
∃ v actual, actual ≤ 19 * time + 3 * xs.length ∧ Execution p t xs v actual ∧ Image u v :=
execution_simulation p width image source

/-- Source time proportional to BST work transfers with explicit constants. -/
theorem proportional_time {w f r l : Nat} (p : Machine f r l)
{s u : State w f r l} {t : Target w f r} {key : BitVec w} {time cost constant : Nat}
(width : 0 < w) (image : Image s t)
(source : RealWorld.Responds p s key u time cost) (bound : time ≤ constant * (cost + 1)) :
∃ v actual, actual ≤ (19 * constant + 3) * (cost + 1) ∧
Responds p t key v actual ∧ Image u v := proportional_response p width image source bound

/-- No execution of compiled code allocates or removes a record. -/
theorem storage {w f r l : Nat} (p : Machine f r l) {s t : Target w f r} {n : Nat}
(run : Cslib.PointerMachine.Runs (compile p) s t n) : t.heap.length = s.heap.length :=
storage_preserved p run

/-- Code size is fixed by the source controller, independent of tree size. -/
theorem code_size {w f r l : Nat} (p : Machine f r l) :
(compile (w := w) p).length = 24 * l + 4 := compile_length p

/-- Concrete successful requests have unique states and exact instruction counts. -/
theorem deterministic {w f r l : Nat} {p : Machine f r l} {s t u : Target w f r}
{key : BitVec w} {a b : Nat} (h : Responds p s key t a) (k : Responds p s key u b) :
t = u ∧ a = b := response_unique h k

end RotationBST.RealWorld.Compiler.Results

/-- info: abstract audit passed for `RotationBST.RealWorld.Compiler.Results`: 6 root declarations -/
#guard_msgs in
#check_bst_abstract RotationBST.RealWorld.Compiler.Results allowing [
RotationBST.Models.RealWorld.Compiler.Definitions.Abstract,
RotationBST.Models.RealWorld.Definitions.Abstract,
RotationBST.Models.RealWorld.Linked.Definitions.Abstract,
RotationBST.Models.RealWorld.PointerMachine.Definitions.Abstract,
RotationBST.Models.Labelled.Cursor.Definitions.Abstract,
RotationBST.Models.Labelled.Cursor.Map.Definitions.Abstract,
RotationBST.Models.Multifinger.Program.Definitions.Abstract,
RotationBST.Models.Rotation.Abstract,
RotationBST.Models.Labelled.Ordering.Definitions.Abstract,
CslibFrontier.Computability.PointerMachine.Defs]
3 changes: 3 additions & 0 deletions RotationBST/Models/RealWorld/Compiler/AbstractBridge.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module

public import RotationBST.Models.RealWorld.Compiler.Space
88 changes: 88 additions & 0 deletions RotationBST/Models/RealWorld/Compiler/Boundaries.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
module

public import RotationBST.Models.RealWorld.Compiler.Branch

@[expose] public section
namespace RotationBST.RealWorld.Compiler
open Cslib.PointerMachine (Instr Runs)
variable {w f r l : Nat}

theorem mark_block (p : Machine f r l) (pc next : Fin l)
{s : State w f r l} {t : Target w f r} (image : Image s t) (ht : t.pc = address pc)
(source : p.code pc = .mark next)
(record : Linked.Record w f (r + 4)) (read : t.deref 0 = some record)
(key : record.data 0 = s.request) :
∃ v, Runs (compile p) t v 4 ∧ Image { s with seen := true, pc := some next } v := by
obtain ⟨c, rep, hc, hr, hq, hs, hp⟩ := image
have im : Image s t := ⟨c, rep, hc, hr, hq, hs, hp⟩
let v1 : Target w f r := { t.setData (tempReg r) s.request with pc := address pc + 1 }
let v2 : Target w f r := { v1 with pc := address pc + 3 }
let v3 : Target w f r := { v2.setData (seenReg r) (flag w true) with pc := address pc + 4 }
let v4 : Target w f r := { v3 with pc := address next }
have h0 : (compile (w := w) p)[t.pc]? = some (.loadData (tempReg r) 0 0) := by
rw [ht]; simpa [source, block] using block_lookup (w := w) p pc 0 (by decide)
have h1 : (compile (w := w) p)[address pc + 1]? =
some (.ifTest .eq (tempReg r) (queryReg r) (address pc + 3)) := by
simpa [source, block] using block_lookup (w := w) p pc 1 (by decide)
have h3 : (compile (w := w) p)[address pc + 3]? =
some (.op (.literal 1) (seenReg r) (zeroReg r) (zeroReg r)) := by
simpa [source, block] using block_lookup (w := w) p pc 3 (by decide)
have h4 : (compile (w := w) p)[address pc + 4]? = some (.goto (address next)) := by
simpa [source, block] using block_lookup (w := w) p pc 4 (by decide)
have run1 : Runs (compile p) t v1 1 := by
simpa [Instr.effect, Instr.next, read, key, ht, v1] using one (compile p) t _ h0 (by simp)
have hq' := hq
simp only [queryReg] at hq'
have run2 : Runs (compile p) v1 v2 1 := by
simpa [v1, v2, Instr.effect, Instr.next, Cslib.PointerMachine.State.setData,
tempReg, queryReg, Fin.ext_iff, hq'] using one (compile p) v1 _ h1 (by simp)
have run3 : Runs (compile p) v2 v3 1 := by
simpa [v2, v3, Instr.effect, Instr.next, flag] using one (compile p) v2 _ h3 (by simp)
have run4 : Runs (compile p) v3 v4 1 := by
simpa [v3, v4, Instr.effect, Instr.next] using one (compile p) v3 _ h4 (by simp)
refine ⟨v4, ((run1.trans run2).trans run3).trans run4, ?_⟩
apply image_control (u := { s with seen := true, pc := some next }) (v := v4) im rfl rfl rfl
· intro i
simpa [v4, v3, v2, v1, Cslib.PointerMachine.State.setData,
Ne.symm (user_ne_temp i), Ne.symm (user_ne_seen i)] using hr i
· simpa [v4, v3, v2, v1, Cslib.PointerMachine.State.setData,
queryReg, tempReg, seenReg, Fin.ext_iff] using hq
· simp [v4, v3, Cslib.PointerMachine.State.setData]
· rfl

theorem finish_block (p : Machine f r l) (pc : Fin l)
{s : State w f r l} {t : Target w f r} (image : Image s t) (ht : t.pc = address pc)
(source : p.code pc = .finish) (seen : s.seen = true) (hw : 0 < w) :
∃ v, Runs (compile p) t v 3 ∧ Image { s with pc := (none : Option (Fin l)) } v := by
obtain ⟨c, rep, hc, hr, hq, hs, hp⟩ := image
have im : Image s t := ⟨c, rep, hc, hr, hq, hs, hp⟩
let v1 : Target w f r := { t.setData (zeroReg r) (BitVec.ofNat w 0) with pc := address pc + 1 }
let v2 : Target w f r := { v1 with pc := address pc + 2 }
let v3 : Target w f r := { v2 with pc := boundary l }
have h0 : (compile (w := w) p)[t.pc]? =
some (.op (.literal 0) (zeroReg r) (zeroReg r) (zeroReg r)) := by
rw [ht]; simpa [source, block] using block_lookup (w := w) p pc 0 (by decide)
have h1 : (compile (w := w) p)[address pc + 1]? =
some (.ifTest .eq (seenReg r) (zeroReg r) (trap l)) := by
simpa [source, block] using block_lookup (w := w) p pc 1 (by decide)
have h2 : (compile (w := w) p)[address pc + 2]? = some (.goto (boundary l)) := by
simpa [source, block] using block_lookup (w := w) p pc 2 (by decide)
have nonzero : flag w true ≠ BitVec.ofNat w 0 := flag_ne_zero hw
have run1 : Runs (compile p) t v1 1 := by
simpa [Instr.effect, Instr.next, ht, v1] using one (compile p) t _ h0 (by simp)
have hs' := hs
simp only [seenReg, seen] at hs'
have run2 : Runs (compile p) v1 v2 1 := by
simpa [v1, v2, Instr.effect, Instr.next, Cslib.PointerMachine.State.setData,
seenReg, zeroReg, Fin.ext_iff, hs', nonzero, Nat.add_assoc] using one (compile p) v1 _ h1 (by simp)
have run3 : Runs (compile p) v2 v3 1 := by
simpa [v2, v3, Instr.effect, Instr.next] using one (compile p) v2 _ h2 (by simp)
refine ⟨v3, (run1.trans run2).trans run3, ?_⟩
apply image_control (u := { s with pc := (none : Option (Fin l)) }) (v := v3) im rfl rfl rfl
· intro i
simpa [v3, v2, v1, Cslib.PointerMachine.State.setData, Ne.symm (user_ne_zero i)] using hr i
· simpa [v3, v2, v1, Cslib.PointerMachine.State.setData, queryReg, zeroReg, Fin.ext_iff] using hq
· simpa [v3, v2, v1, Cslib.PointerMachine.State.setData, seenReg, zeroReg, Fin.ext_iff] using hs
· rfl

end RotationBST.RealWorld.Compiler
35 changes: 35 additions & 0 deletions RotationBST/Models/RealWorld/Compiler/Branch.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module

public import RotationBST.Models.RealWorld.Compiler.Primitive

@[expose] public section
namespace RotationBST.RealWorld.Compiler
open Cslib.PointerMachine (Instr Runs step)
variable {w f r l : Nat}

theorem branch_block (p : Machine f r l) (pc yes no : Fin l) (a b : Fin r)
{s : State w f r l} {t : Target w f r} (image : Image s t) (ht : t.pc = address pc)
(source : p.code pc = .branch a b yes no) :
∃ v n, n ≤ 19 ∧ Runs (compile p) t v n ∧
Image { s with pc := some (if (s.regs a).toNat < (s.regs b).toNat then yes else no) } v := by
obtain ⟨c, rep, hc, hr, hq, hs, hpc⟩ := image
have im : Image s t := ⟨c, rep, hc, hr, hq, hs, hpc⟩
have hi : (compile (w := w) p)[t.pc]? =
some (.ifTest .lt (userReg a) (userReg b) (address yes)) := by
rw [ht]
simpa [source, block] using block_lookup (w := w) p pc 0 (by decide)
by_cases cond : (s.regs a).toNat < (s.regs b).toNat
· refine ⟨{ t with pc := address yes }, 1, by decide, ?_, ?_⟩
· simpa [Instr.effect, Instr.next, hr, cond] using one (compile p) t _ hi (by simp)
· simpa [cond, location] using image_jump im (some yes)
· have h1 : (compile (w := w) p)[t.pc + 1]? = some (.goto (address no)) := by
rw [ht]
simpa [source, block] using block_lookup (w := w) p pc 1 (by decide)
have first : Runs (compile p) t { t with pc := t.pc + 1 } 1 := by
simpa [Instr.effect, Instr.next, hr, cond] using one (compile p) t _ hi (by simp)
refine ⟨{ t with pc := address no }, 2, by decide, ?_, ?_⟩
· exact .tail _ _ _ _ first
(Cslib.PointerMachine.step_eq_some_iff.mpr ⟨.goto (address no), h1, by simp, rfl⟩)
· simpa [cond, location] using image_jump im (some no)

end RotationBST.RealWorld.Compiler
57 changes: 57 additions & 0 deletions RotationBST/Models/RealWorld/Compiler/Code.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module

public import RotationBST.Models.RealWorld.Compiler.Relocation

@[expose] public section
namespace RotationBST.RealWorld.Compiler
variable {w f r l : Nat}

theorem block_lookup (p : Machine f r l) (pc : Fin l) (k : Nat) (hk : k < stride) :
(compile (w := w) p)[address pc + k]? =
some (((block (w := w) (address pc) (p.code pc))[k]?).getD (.goto (trap l))) := by
have hi : address pc + k < boundary l := by
simp only [address, boundary, stride] at *
have := pc.isLt
omega
have hb : address pc + k < boundary l + 4 := by omega
have hd : (address pc + k) / stride = pc.val := by
simp only [address, stride] at *; omega
have hm : (address pc + k) % stride = k := by
simp only [address, stride] at *; omega
simp only [compile, List.getElem?_ofFn]
simp [hb, hi, hd, hm]

theorem boundary_lookup (p : Machine f r l) :
(compile (w := w) p)[boundary l]? = some .halt := by
simp only [compile, List.getElem?_ofFn]
simp

theorem entry_lookup (p : Machine f r l) :
(compile (w := w) p)[entry l]? =
some (.op (.literal 0) (seenReg r) (zeroReg r) (zeroReg r)) := by
simp only [compile, List.getElem?_ofFn]
simp [entry]

theorem entry_next_lookup (p : Machine f r l) :
(compile (w := w) p)[entry l + 1]? = some (.goto (address p.entry)) := by
simp only [compile, List.getElem?_ofFn]
simp [entry, Nat.add_assoc]

theorem block_length (base : Nat) (i : Instruction f r l) :
(block (w := w) base i).length ≤ stride := by
cases i <;> simp [block, stride]
rename_i op next
cases op <;> simp [Linked.program, Linked.move, Linked.rotate]

/-- The compiler contains every relocated primitive at the correct address. -/
theorem primitive_contains (p : Machine f r l) (pc next : Fin l) (op : Multifinger.Opcode)
(source : p.code pc = .bst op next) :
Contains (compile (w := w) p) (Linked.program op) (address pc) (address next) := by
intro k i hi
have hk := List.getElem?_eq_some_iff.mp hi |>.1
have bound : (Linked.program (w := w) (e := f) (regs := r + 4) op).length ≤ stride := by
cases op <;> simp [Linked.program, Linked.move, Linked.rotate, stride]
have lookup := block_lookup (w := w) p pc k (by omega)
simpa [source, block, List.getElem?_map, hi] using lookup

end RotationBST.RealWorld.Compiler
Loading
Loading