Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
5909d1b
feat(DataStructure/Memory): word-addressed memory oracle and named fi…
SamuelSchlesinger Nov 1, 2026
412f614
doc(bib): add Guéneau, Charguéraud and Pottier (ESOP 2018)
SamuelSchlesinger Nov 1, 2026
16a647f
feat(DataStructure/StateTransition): refinements by abstraction funct…
SamuelSchlesinger Nov 1, 2026
2f12c49
feat(DataStructure/StateTransition): weaken and rescale amortized ref…
SamuelSchlesinger Nov 1, 2026
fba5208
Merge the memory-oracle branch (#2211) into the queue slice
SamuelSchlesinger Nov 1, 2026
98e3290
fix(DataStructure/StateTransition): place productCharge beside produc…
SamuelSchlesinger Nov 1, 2026
02ba9f7
Merge the refinement branch (#2212) into the queue slice
SamuelSchlesinger Nov 1, 2026
56232f9
refactor(DataStructure/StateTransition): make Refinement.Amortized a …
SamuelSchlesinger Nov 1, 2026
1b4e2ea
Merge the refinement branch (#2212) into the queue slice
SamuelSchlesinger Nov 1, 2026
fe7d60d
wip: array stack over memory and queue method lemmas
SamuelSchlesinger Nov 1, 2026
aad5f3e
feat(Foundations): affine cost bounds are O(size)
SamuelSchlesinger Nov 1, 2026
78cf872
Merge the refinement branch (#2212) into the queue slice
SamuelSchlesinger Nov 1, 2026
6698e86
wip: queue over memory
SamuelSchlesinger Nov 1, 2026
7dda248
refactor(DataStructure/StateTransition): product answer types via Sum…
SamuelSchlesinger Nov 1, 2026
93adf90
Merge the refinement branch (#2212) into the queue slice
SamuelSchlesinger Nov 1, 2026
412ad37
feat(DataStructure): queue over two array stacks over the memory oracle
SamuelSchlesinger Nov 1, 2026
4283b8f
feat(WordRAM): certify compositional register-word glue
SamuelSchlesinger Nov 1, 2026
cb0adff
Merge commit '4283b8fb714450e78ba170865bcb54ce8563bce3' into codex/ra…
SamuelSchlesinger Nov 1, 2026
45dd184
feat(WordRAM): realize memory queries with uniform bind and glue
SamuelSchlesinger Nov 1, 2026
d8f6bea
fix(WordRAM): normalize the memory return simp rule
SamuelSchlesinger Nov 1, 2026
4977e9b
feat(WordRAM): realize bounded loops and initialized client programs
SamuelSchlesinger Nov 1, 2026
04a06b5
feat(WordRAM): derive global and framed implementation certificates
SamuelSchlesinger Nov 2, 2026
e768e0a
chore(WordRAM): combine published realization and queue prerequisites
SamuelSchlesinger Nov 2, 2026
5917ae4
feat(WordRAM): realize runtime-layout memory-stack methods
SamuelSchlesinger Nov 2, 2026
a929a76
refactor(WordRAM): centralize realization history and word encodings
SamuelSchlesinger Nov 2, 2026
5b9f10e
Merge commit 'a929a76f23194a8e94428973bca2a317f9f23755' into codex/ra…
SamuelSchlesinger Nov 2, 2026
383a520
refactor(WordRAM): expose shared glue rules and infer stack push
SamuelSchlesinger Nov 2, 2026
a91fb0a
feat(WordRAM): realize tactic for memory-client realizations
SamuelSchlesinger Nov 2, 2026
141d112
feat(WordRAM): loops whose body issues a query in realize
SamuelSchlesinger Nov 2, 2026
de22695
test(WordRAM): a whole stack client to a flat program via realize
SamuelSchlesinger Nov 2, 2026
72dc1e6
refactor(Stack): migrate the stack to its realized implementation
SamuelSchlesinger Nov 2, 2026
5fd685c
Merge remote-tracking branch 'pr/2246' into feat/ram-realization-adapter
SamuelSchlesinger Nov 2, 2026
0a8975c
fix(WordRAM): destructure the loop argument in Realizes.iterate₀
SamuelSchlesinger Nov 2, 2026
4d7e97c
feat(WordRAM): native backends for memory refinements, one realized b…
SamuelSchlesinger Nov 2, 2026
f51bcd8
style(tests): wrap long lines in the backend test
SamuelSchlesinger Nov 2, 2026
7d6ef37
feat(WordRAM): Backend.data, rep_data and fits_appendWords
SamuelSchlesinger Nov 2, 2026
fb0bf09
test(WordRAM): backend test uses fits_appendWords and Backend.data
SamuelSchlesinger Nov 2, 2026
729ac5f
Merge branch 'feat/ram-realization-adapter' into feat/stack-realized-…
SamuelSchlesinger Nov 2, 2026
c9f6302
refactor(Stack): run the native stack on Realization.Backend
SamuelSchlesinger Nov 2, 2026
212e582
perf(WordRAM): state realize certificates per form in the client's terms
SamuelSchlesinger Nov 2, 2026
65c1d5d
refactor(Stack): keep only consumed native API; fold the traversal wo…
SamuelSchlesinger Nov 2, 2026
d456c2a
Merge branch 'perf/realize-cliffs' (#2280) into feat/stack-realized-m…
SamuelSchlesinger Nov 2, 2026
42c2cb4
refactor(Stack): realize the controller in one call
SamuelSchlesinger Nov 2, 2026
27bfd7c
perf(Stack): simplify only the controller's side goals
SamuelSchlesinger Nov 2, 2026
309306a
Merge origin/main into feat/ram-realization-adapter
SamuelSchlesinger Nov 2, 2026
cac0b0e
chore: drop imports the merge duplicated in Complexity.lean
SamuelSchlesinger Nov 2, 2026
f256f87
feat(WordRAM): framed backends - frames, products over one memory, al…
SamuelSchlesinger Nov 2, 2026
ed43a2c
test(WordRAM): allocate a pair of stacks in one body
SamuelSchlesinger Nov 2, 2026
e3b6a91
chore(Stack): merge actual main into reviewed realized migration
SamuelSchlesinger Nov 2, 2026
d39ffc0
feat(WordRAM): retain Backend register frames for live-input execution
SamuelSchlesinger Nov 2, 2026
97bd0f5
chore(Stack): refresh realized migration on final actual suppliers
SamuelSchlesinger Nov 2, 2026
70a2303
chore(WordRAM): merge landed framed provider into live-input API
SamuelSchlesinger Nov 2, 2026
73f64a7
fix(WordRAM): expose Backend extension and preparation metadata
SamuelSchlesinger Nov 2, 2026
fc383e5
Fix Stack migration name resolution and framed proof view
SamuelSchlesinger Nov 2, 2026
0abe1b7
merge: prepare published Backend and Stack parents for queue integration
SamuelSchlesinger Nov 2, 2026
57b267a
refactor(WordRAM): realize the queue with resident register metadata
SamuelSchlesinger Nov 2, 2026
0eb7af1
chore(Queue): merge actual suppliers into reviewed migration
SamuelSchlesinger Nov 2, 2026
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
10 changes: 8 additions & 2 deletions Complexity.lean
Original file line number Diff line number Diff line change
Expand Up @@ -3016,10 +3016,16 @@ public import Complexity.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.Model
public import Complexity.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.Program
public import Complexity.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.Trees
public import Complexity.DataStructure.Queue.Memory
public import Complexity.DataStructure.Queue.Memory.Transfer
public import Complexity.DataStructure.Queue.Spec
public import Complexity.DataStructure.Queue.WordRAM.Basic
public import Complexity.DataStructure.Queue.WordRAM.Dynamic
public import Complexity.DataStructure.Queue.WordRAM.Model
public import Complexity.DataStructure.Queue.WordRAM.RegisterCorrectness
public import Complexity.DataStructure.Queue.WordRAM.RegisterDynamic
public import Complexity.DataStructure.Queue.WordRAM.RegisterExecution
public import Complexity.DataStructure.Queue.WordRAM.RegisterMetadata
public import Complexity.DataStructure.Queue.WordRAM.RegisterMethods
public import Complexity.DataStructure.Queue.WordRAM.RegisterProgram
public import Complexity.DataStructure.Queue.WordRAM.RegisterTransfer
public import Complexity.DataStructure.Retroactive.Basic
public import Complexity.DataStructure.SearchTree.Basic
public import Complexity.DataStructure.SearchTree.Cost
Expand Down
154 changes: 154 additions & 0 deletions Complexity/DataStructure/Queue/Memory/Transfer.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/-
Copyright (c) 2026 Samuel Schlesinger. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Samuel Schlesinger
-/

module
-- upstream: candidate

public import Complexity.Computability.WordRAM.Realization.While
public import Complexity.DataStructure.Queue.Memory

/-!
# Early-exit transfer for the memory queue

The remaining number of rear elements is explicit loop state. A rear pop returning `none`
sets it to zero, so the shared bounded while loop stops immediately. The source equalities
preserve the original linked queue program, including its operation history.

## Main definitions

* `transferBody`: one rear pop and, when successful, one front push.

## Main results

* `transfer_eq_boundedWhile`: the existing transfer program is the shared early-exit loop.
* `run_eq_allocPair_bind`: allocation followed by the original linked queue client.
* `transfer_invariant`: the existing framed product preserves both stacks and moves the rear.

## References

The queue and its algorithmic references are in `Cslib.DataStructure.Queue.Spec`.
-/

@[expose] public section

namespace Cslib.DataStructure.Queue.Memory

open Cslib.WordRAM Cslib.WordRAM.Realization Cslib.DataStructure.Memory

/-- Pop one rear element. An empty rear stops the loop; a successful move decreases the
explicit remaining count. The loop guard excludes a zero count from this body. -/
def transferBody (d : (Stack.Memory.Layout × Stack.Memory.Layout) × ℕ) : Prog ℕ := do
let a ← Stack.Memory.pop d.1.2
match a with
| none => pure 0
| some v => do
Stack.Memory.push d.1.1 v
pure (d.2 - 1)

/-- Forgetting the loop's remaining count leaves exactly one step of the original transfer. -/
theorem transferBody_forget (L₁ L₂ : Stack.Memory.Layout) (w C r : ℕ) :
(transferBody ((L₁, L₂), r)).bind (fun _ => pure ()) =
link (Methods.prod (Stack.Memory.methods L₁ w C) (Stack.Memory.methods L₂ w C))
(Queue.transfer 1) := by
simp only [transferBody, Queue.transfer, link, Methods.prod, Stack.Memory.methods,
PFunctor.FreeM.liftM, PFunctor.FreeM.bind_eq_bind, bind_assoc]
congr 1
funext a
cases a <;> simp [PFunctor.FreeM.liftM, Methods.prod, Stack.Memory.methods]

/-- The bounded while has exactly the original source program, with no inactive tail queries
or extra memory operations after an early empty pop. The cutoff bounds the remaining count. -/
theorem transfer_eq_boundedWhile (L₁ L₂ : Stack.Memory.Layout) (w C fuel n : ℕ)
(hn : n ≤ fuel) :
link (Methods.prod (Stack.Memory.methods L₁ w C) (Stack.Memory.methods L₂ w C))
(Queue.transfer n) =
(boundedWhile (fun _ r => 0 < r) transferBody (L₁, L₂) fuel n).bind
(fun _ => pure ()) := by
induction fuel generalizing n with
| zero =>
have : n = 0 := by omega
subst n
rfl
| succ fuel ih =>
cases n with
| zero => simp [Queue.transfer, boundedWhile, link]
| succ n =>
simp only [Queue.transfer, link, PFunctor.FreeM.liftM, Methods.prod,
Stack.Memory.methods, boundedWhile, Nat.zero_lt_succ, ↓reduceIte, transferBody,
Nat.add_sub_cancel, PFunctor.FreeM.bind_eq_bind, bind_assoc]
congr 1
funext a
cases a with
| none => simp [boundedWhile_of_not]
| some v =>
simp only [PFunctor.FreeM.liftM]
exact congrArg (fun p => (Stack.Memory.push L₁ v).bind (fun _ => p))
(ih n (by omega))

/-- The current allocator allocates its two stack handles and then links the unchanged queue
methods through their product. This is equality of programs, before selecting a memory state. -/
theorem run_eq_allocPair_bind (w C : ℕ) {α : Type}
(p : (Queue.WordRAM.spec w C).Client α) :
run w C p = (Allocator.allocPair (Stack.Memory.allocator w C)
(Stack.Memory.allocator w C)).bind (fun L => link ((allocator w C).methods L) p) := rfl

/-- The source transfer preserves the two existing storage invariants and reverses the rear
into the front. The generic framed product supplies noninterference; no replacement invariant
or instruction-level proof is needed. -/
theorem transfer_invariant {L₁ L₂ : Stack.Memory.Layout} {w C n : ℕ}
(h₁ : L₁.Valid w C) (h₂ : L₂.Valid w C) (hd : Disjoint L₁.footprint L₂.footprint)
{s : State} (hw : WellFormed w s)
(hs₁ : Stack.Memory.Inv L₁ s) (hs₂ : Stack.Memory.Inv L₂ s)
(hn : s.mem L₂.count.base = n)
(hlen : s.mem L₁.count.base + s.mem L₂.count.base ≤ C) :
let p := link (Methods.prod (Stack.Memory.methods L₁ w C)
(Stack.Memory.methods L₂ w C)) (Queue.transfer n)
let s' := (spec w).clientState s p
(spec w).ValidHistory s ((spec w).clientHistory s p) ∧
WellFormed w s' ∧ Stack.Memory.Inv L₁ s' ∧ Stack.Memory.Inv L₂ s' ∧
s'.mem L₂.count.base = 0 ∧ s'.mem L₁.count.base = s.mem L₁.count.base + n ∧
Stack.Memory.abs L₁ s' = (Stack.Memory.abs L₂ s).reverse ++ Stack.Memory.abs L₁ s := by
let M := (Stack.Memory.framed h₁).prod (Stack.Memory.framed h₂)
have hi : M.inv s := ⟨hs₁, hs₂, hd, by
change Cslib.WordRAM.Fits w (s.frontier + (0 + 0))
simpa only [Nat.add_zero] using hw.frontier_fits⟩
have hn' : n = (Stack.Memory.abs L₂ s).length := by
rw [Stack.Memory.length_abs, hn]
have hv : (boundedStacks w C).ValidHistory (M.abs s)
((boundedStacks w C).clientHistory (M.abs s) (Queue.transfer n)) := by
rw [clientHistory_boundedStacks, hn']
change (boundedStacks w C).ValidHistory (Stack.Memory.abs L₁ s, Stack.Memory.abs L₂ s)
((stackPair ℕ).runClient (Stack.Memory.abs L₁ s, Stack.Memory.abs L₂ s)
(Queue.transfer (Stack.Memory.abs L₂ s).length)).2.2
rw [Queue.transfer_run]
apply validHistory_transferTrace
· simpa only [Stack.Memory.length_abs] using hlen
· intro v hv
simp only [Stack.Memory.abs, List.mem_reverse, Cslib.WordRAM.Block.entries,
List.mem_map] at hv
obtain ⟨i, _, rfl⟩ := hv
exact hw.mem_fits _
obtain ⟨hvE, _, habs, hsE⟩ := M.toRefinement.simulates_client (Queue.transfer n) hi hv
have habs' : M.abs ((spec w).clientState s (link M.methods (Queue.transfer n))) =
((Stack.Memory.abs L₂ s).reverse ++ Stack.Memory.abs L₁ s, []) := by
rw [habs, clientState_boundedStacks, hn']
exact congrArg (fun t => t.2.1) (Queue.transfer_run (Stack.Memory.abs L₁ s)
(Stack.Memory.abs L₂ s))
let s' := (spec w).clientState s (link (Methods.prod (Stack.Memory.methods L₁ w C)
(Stack.Memory.methods L₂ w C)) (Queue.transfer n))
have hfront : Stack.Memory.abs L₁ s' =
(Stack.Memory.abs L₂ s).reverse ++ Stack.Memory.abs L₁ s := congrArg Prod.fst habs'
have hrear : Stack.Memory.abs L₂ s' = [] := congrArg Prod.snd habs'
have hfrontLength := congrArg List.length hfront
have hrearLength := congrArg List.length hrear
simp only [Stack.Memory.length_abs, List.length_append, List.length_reverse,
List.length_nil, hn] at hfrontLength hrearLength
refine ⟨hvE, ?_, hsE.1, hsE.2.1, hrearLength, ?_, hfront⟩
· rw [← (spec w).run_runClient]
exact hw.run hvE
· simpa only [Nat.add_comm] using hfrontLength

end Cslib.DataStructure.Queue.Memory
Loading
Loading