Documentation

CslibFrontier.DataStructure.PriorityQueue.WordRAM.Basic

Word-RAM interface for indexed min-priority queues #

This file packages uniform Word-RAM calling conventions (PQCode) and parametric specification / resource certificates (PQPackage) for Cslib.DataStructure.PriorityQueue.indexedMinPQSpec.

A PQPackage B r_pq bundles a SubroutineCode B r_pq 1 with:

It also provides basis-polymorphic inline caller helpers (callIsEmpty, callGetKey, callInsert, callDecreaseKey, callExtractMin) built from Op.copy and StateTransitionImpl.call_rename.

Main definitions #

Main results #

References #

PQCode: Uniform priority-queue subroutine code and calling convention #

Uniform machine code and register calling convention for an indexed min-priority queue over basis B using r_pq local registers.

  • allocSpan : Code B r_pq
  • initSetup : Code B r_pq
  • code : Code B r_pq
  • baseReg : Fin r_pq
  • initParams : Fin 1 → Fin r_pq
  • curReg : Fin r_pq
  • zeroReg : Fin r_pq
  • oneReg : Fin r_pq
  • incCur : Op B r_pq
  • input : Fin r_pq → Bool
  • output : Fin r_pq
  • nReg : Fin r_pq

    Register holding the handle-universe size n at initialization (initParams = ![nReg]).

  • tag2 : Fin r_pq

    High tag bit (≠ 0 selects extractMin).

  • tag1 : Fin r_pq

    Middle tag bit (≠ 0 selects insert / decreaseKey when tag2 = 0).

  • tag0 : Fin r_pq

    Low tag bit (0 selects isEmpty / insert; ≠ 0 selects getKey / decreaseKey).

  • argV : Fin r_pq

    Handle argument register (v.val for getKey, insert, decreaseKey).

  • argK : Fin r_pq

    Key argument register (k for insert, decreaseKey).

  • outFlag : Fin r_pq

    Flag output register (1/0 for isEmpty, getKey, extractMin).

  • outV : Fin r_pq

    Extracted handle output register (u.val for non-empty extractMin).

  • outK : Fin r_pq

    Key output register (k for present getKey and non-empty extractMin).

  • initParams_eq : self.initParams = ![self.nReg]

    Initialization takes n in nReg.

  • nReg_ne_frontier : self.nReg ≠ self.frontierReg

    nReg is distinct from frontierReg so callers can pass n and frontier together.

  • args_nodup : [self.tag2, self.tag1, self.tag0, self.argV, self.argK].Nodup

    Tag and argument registers are pairwise distinct.

  • tag2_input : self.input self.tag2 = true

    tag2 is a caller-overwritable input register.

  • tag1_input : self.input self.tag1 = true

    tag1 is a caller-overwritable input register.

  • tag0_input : self.input self.tag0 = true

    tag0 is a caller-overwritable input register.

  • argV_input : self.input self.argV = true

    argV is a caller-overwritable input register.

  • argK_input : self.input self.argK = true

    argK is a caller-overwritable input register.

  • output_eq : self.output = self.outFlag

    Primary halting output register is outFlag.

Translate a PQCode bundle along a basis simulation.

@[reducible, inline]

Lift an exact-basis PQCode to Basis.standard.

Standard register encoding for IndexedMinPQ.Op n using 1-bit tags (tag2, tag1, tag0).

Partial argument-matching predicate for inline priority-queue calls.

def Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.decode {B : WordRAM.Basis} {r_pq : ℕ} (P : PQCode B r_pq) {n K_max : ℕ} (o : (indexedMinPQSpec n K_max).Op) :
(Fin r_pq → ℕ) → Option ((indexedMinPQSpec n K_max).Result o)

Standard output decoder for indexedMinPQSpec n K_max.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.arguments_fit {B : WordRAM.Basis} {r_pq : ℕ} (P : PQCode B r_pq) {n K_max w base space : ℕ} (hsp : n + 1 ≤ space) (hS : WordRAM.Fits w (base + space)) (hK : WordRAM.Fits w K_max) {s : IndexedMinPQ.State n} (o : IndexedMinPQ.Op n) (hv : (indexedMinPQSpec n K_max).valid s o) (i : Fin r_pq) :

Standard encoded arguments fit in w-bit words whenever n + 1 ≤ space, Fits w (base + space), and Fits w K_max.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.encode_matches {B : WordRAM.Basis} {r_pq : ℕ} (P : PQCode B r_pq) {n : ℕ} (o : IndexedMinPQ.Op n) (regs : Fin r_pq → ℕ) :
P.argMatch o fun (i : Fin r_pq) => if P.input i = true then P.encode o i else regs i

Full standalone prepare register delivery satisfies P.argMatch.

Standalone flat-program register conventions and operation-sequence execution #

def Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.initRegs {B : WordRAM.Basis} {r_pq : ℕ} (P : PQCode B r_pq) (n base : ℕ) :
Fin r_pq → ℕ

Initial register assignment supplying n in P.nReg, base in P.frontierReg, and 0 elsewhere.

@[simp]
theorem Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.initRegs_nReg {B : WordRAM.Basis} {r_pq : ℕ} (P : PQCode B r_pq) (n base : ℕ) :
P.initRegs n base P.nReg = n

P.initRegs n base places n in P.nReg.

@[simp]

P.initRegs n base places base in P.frontierReg.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.initRegs_params {B : WordRAM.Basis} {r_pq : ℕ} (P : PQCode B r_pq) (n base : ℕ) (j : Fin 1) :
P.initRegs n base (P.initParams j) = ![n] j

P.initRegs n base supplies the single initialization parameter ![n].

theorem Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.initRegs_bounded {B : WordRAM.Basis} {r_pq : ℕ} (P : PQCode B r_pq) {n base w : ℕ} (hn : WordRAM.Fits w n) (hb : WordRAM.Fits w base) {mem₀ : ℕ → ℕ} (hmem : ∀ (a : ℕ), WordRAM.Fits w (mem₀ a)) :
WordRAM.Data.Bounded w { regs := P.initRegs n base, mem := mem₀ }

Initial data built from P.initRegs n base is w-bounded when n, base, and mem₀ fit.

def Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.prepareRegs {B : WordRAM.Basis} {r_pq : ℕ} (P : PQCode B r_pq) {n : ℕ} (o : IndexedMinPQ.Op n) (regs : Fin r_pq → ℕ) :
Fin r_pq → ℕ

Deliver operation o's 3-bit tag into P.tag2..P.tag0 and its handle/key arguments into P.argV, P.argK, leaving all other registers unchanged.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.prepareRegs_matches {B : WordRAM.Basis} {r_pq : ℕ} (P : PQCode B r_pq) {n : ℕ} (o : IndexedMinPQ.Op n) (regs : Fin r_pq → ℕ) :
P.argMatch o (P.prepareRegs o regs)

Delivering operation o via P.prepareRegs satisfies P.argMatch.

inductive Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.RunsOps {B : WordRAM.Basis} {r_pq : ℕ} (P : PQCode B r_pq) {n K_max : ℕ} (space w base : ℕ) :

Sequential flat-program execution of ops by repeated calls to P.opProg: each call prepares input registers via P.prepareRegs, runs P.opProg from pc = 0 to halt, decodes the exact IndexedMinPQ result via P.decode, modifies memory only within [base, base + space), and accumulates total instructions executed (including each trailing halt).

theorem Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.run_ops_exact {B : WordRAM.Basis} {r_pq : ℕ} {P : PQCode B r_pq} {n K_max w base t_bound : ℕ} (C : WordRAM.StateTransitionImpl P.toSubroutineCode (indexedMinPQSpec n K_max) w base ![n] (IndexedMinPQ.empty n)) (hsp : n + 1 ≤ C.space) (hS : WordRAM.Fits w (base + C.space)) (hK : WordRAM.Fits w K_max) (harg : C.argMatch = P.argMatch) (hdec : C.decode = P.decode) {mem₀ : ℕ → ℕ} (hmem : ∀ (a : ℕ), WordRAM.Fits w (mem₀ a)) {c : WordRAM.Code B r_pq} (hinit : WordRAM.Data.Bounded w { regs := P.initRegs n base, mem := mem₀ } → P.initRegs n base P.frontierReg = base → (∀ (j : Fin 1), P.initRegs n base (P.initParams j) = ![n] j) → ∃ (x₀ : WordRAM.Data r_pq), WordRAM.ExecWithin w c { regs := P.initRegs n base, mem := mem₀ } x₀ t_bound ∧ x₀.regs P.frontierReg = base + C.space ∧ C.rep (IndexedMinPQ.empty n) x₀ ∧ C.potential x₀ ≤ C.initPotential ∧ WordRAM.Modifies { base := base, length := C.space }.footprint mem₀ x₀.mem) (ops : List (IndexedMinPQ.Op n)) (hv : (indexedMinPQSpec n K_max).ValidHistory (IndexedMinPQ.empty n) ops) :
∃ (pc₀ : ℕ) (x₀ : WordRAM.Data r_pq) (t_init : ℕ) (z : WordRAM.Data r_pq) (t_ops : ℕ), WordRAM.Runs w (c.program P.output) { pc := 0, data := { regs := P.initRegs n base, mem := mem₀ } } { pc := pc₀, data := x₀ } t_init ∧ (∃ (out : ℕ), WordRAM.step w (c.program P.output) { pc := pc₀, data := x₀ } = WordRAM.Outcome.halt out) ∧ WordRAM.Modifies { base := base, length := C.space }.footprint mem₀ x₀.mem ∧ P.RunsOps C.space w base (IndexedMinPQ.empty n) x₀ ops z t_ops ∧ C.rep ((indexedMinPQSpec n K_max).run (IndexedMinPQ.empty n) ops).1 z ∧ t_init ≤ t_bound ∧ t_ops + C.potential z ≤ (indexedMinPQSpec n K_max).totalCost (fun (st : (indexedMinPQSpec n K_max).State) (o : (indexedMinPQSpec n K_max).Op) => C.charge st o + 1) (IndexedMinPQ.empty n) ops + C.initPotential

Generic flat-program operation-sequence theorem for any PQCode, StateTransitionImpl certificate C, and initializer block c (such as P.initFresh via C.initFresh_exec or P.initClear via C.initClear_exec).

Basis-polymorphic inline caller blocks and execution lemmas #

def Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.callIsEmpty {B : WordRAM.Basis} {r_pq r_caller : ℕ} (P : PQCode B r_pq) (f : Fin r_pq → Fin r_caller) (zeroReg_c : Fin r_caller) :
WordRAM.Code B r_caller

Relocated inline call to isEmpty, copying 0 from zeroReg_c.

def Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.callExtractMin {B : WordRAM.Basis} {r_pq r_caller : ℕ} (P : PQCode B r_pq) (f : Fin r_pq → Fin r_caller) (oneReg_c : Fin r_caller) :
WordRAM.Code B r_caller

Relocated inline call to extractMin, copying 1 from oneReg_c.

def Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.callGetKey {B : WordRAM.Basis} {r_pq r_caller : ℕ} (P : PQCode B r_pq) (f : Fin r_pq → Fin r_caller) (zeroReg_c oneReg_c vReg_c : Fin r_caller) :
WordRAM.Code B r_caller

Relocated inline call to getKey, copying 0, 1, and handle vReg_c.

def Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.callInsert {B : WordRAM.Basis} {r_pq r_caller : ℕ} (P : PQCode B r_pq) (f : Fin r_pq → Fin r_caller) (zeroReg_c oneReg_c vReg_c kReg_c : Fin r_caller) :
WordRAM.Code B r_caller

Relocated inline call to insert, copying 0, 1, handle vReg_c, and key kReg_c.

def Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.callDecreaseKey {B : WordRAM.Basis} {r_pq r_caller : ℕ} (P : PQCode B r_pq) (f : Fin r_pq → Fin r_caller) (zeroReg_c oneReg_c vReg_c kReg_c : Fin r_caller) :
WordRAM.Code B r_caller

Relocated inline call to decreaseKey, copying 0, 1, handle vReg_c, and key kReg_c.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.callIsEmpty_exec {B : WordRAM.Basis} {r_pq r_caller : ℕ} {P : PQCode B r_pq} {n K_max w base : ℕ} (C : WordRAM.StateTransitionImpl P.toSubroutineCode (indexedMinPQSpec n K_max) w base ![n] (IndexedMinPQ.empty n)) (harg_eq : C.argMatch = P.argMatch) (hdec_eq : C.decode = P.decode) {f : Fin r_pq → Fin r_caller} (hf : Function.Injective f) {zeroReg_c : Fin r_caller} (hz_out : zeroReg_c ∉ Set.range f) {s : IndexedMinPQ.State n} {x : WordRAM.Data r_caller} (hr : C.rep s (WordRAM.Data.comap f x)) (hz0 : x.regs zeroReg_c = 0) :
∃ (z : WordRAM.Data r_caller) (t : ℕ), WordRAM.ExecWithin w (P.callIsEmpty f zeroReg_c) x z t ∧ (z.regs (f P.outFlag) = 0 ↔ IndexedMinPQ.dom s ≠ ∅) ∧ C.rep s (WordRAM.Data.comap f z) ∧ WordRAM.Modifies { base := base, length := C.space }.footprint x.mem z.mem ∧ (∀ j ∉ Set.range f, z.regs j = x.regs j) ∧ t + C.potential (WordRAM.Data.comap f z) ≤ 3 + C.charge s IndexedMinPQ.Op.isEmpty + C.potential (WordRAM.Data.comap f x)

Inline execution of P.callIsEmpty: preserves abstract state s, sets z.regs (f P.outFlag) so z.regs (f P.outFlag) = 0 ↔ IndexedMinPQ.dom s ≠ ∅, and charges 3 + C.charge s .isEmpty.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.callExtractMin_exec {B : WordRAM.Basis} {r_pq r_caller : ℕ} {P : PQCode B r_pq} {n K_max w base : ℕ} (C : WordRAM.StateTransitionImpl P.toSubroutineCode (indexedMinPQSpec n K_max) w base ![n] (IndexedMinPQ.empty n)) (hsp_pos : 1 ≤ C.space) (hS : WordRAM.Fits w (base + C.space)) (harg_eq : C.argMatch = P.argMatch) (hdec_eq : C.decode = P.decode) {f : Fin r_pq → Fin r_caller} (hf : Function.Injective f) {oneReg_c : Fin r_caller} {s : IndexedMinPQ.State n} {x : WordRAM.Data r_caller} (hr : C.rep s (WordRAM.Data.comap f x)) (ho1 : x.regs oneReg_c = 1) :

Inline execution of P.callExtractMin: extracts findMin s and charges 1 + C.charge s .extractMin.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.callInsert_exec {B : WordRAM.Basis} {r_pq r_caller : ℕ} {P : PQCode B r_pq} {n K_max w base : ℕ} (C : WordRAM.StateTransitionImpl P.toSubroutineCode (indexedMinPQSpec n K_max) w base ![n] (IndexedMinPQ.empty n)) (hsp : n + 1 ≤ C.space) (hS : WordRAM.Fits w (base + C.space)) (hK : WordRAM.Fits w K_max) (harg_eq : C.argMatch = P.argMatch) {f : Fin r_pq → Fin r_caller} (hf : Function.Injective f) {zeroReg_c oneReg_c vReg_c kReg_c : Fin r_caller} (hz_out : zeroReg_c ∉ Set.range f) (ho_out : oneReg_c ∉ Set.range f) (hv_out : vReg_c ∉ Set.range f) (hk_out : kReg_c ∉ Set.range f) {s : IndexedMinPQ.State n} {x : WordRAM.Data r_caller} {v : Fin n} {k : ℕ} (hr : C.rep s (WordRAM.Data.comap f x)) (hval : s v = none ∧ k ≤ K_max) (hz0 : x.regs zeroReg_c = 0) (ho1 : x.regs oneReg_c = 1) (hvv : x.regs vReg_c = ↑v) (hvk : x.regs kReg_c = k) :
∃ (z : WordRAM.Data r_caller) (t : ℕ), WordRAM.ExecWithin w (P.callInsert f zeroReg_c oneReg_c vReg_c kReg_c) x z t ∧ C.rep (Function.update s v (some k)) (WordRAM.Data.comap f z) ∧ WordRAM.Modifies { base := base, length := C.space }.footprint x.mem z.mem ∧ (∀ j ∉ Set.range f, z.regs j = x.regs j) ∧ t + C.potential (WordRAM.Data.comap f z) ≤ 5 + C.charge s (IndexedMinPQ.Op.insert v k) + C.potential (WordRAM.Data.comap f x)

Inline execution of P.callInsert: inserts (v, k) and charges 5 + C.charge s (.insert v k).

theorem Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.callDecreaseKey_exec {B : WordRAM.Basis} {r_pq r_caller : ℕ} {P : PQCode B r_pq} {n K_max w base : ℕ} (C : WordRAM.StateTransitionImpl P.toSubroutineCode (indexedMinPQSpec n K_max) w base ![n] (IndexedMinPQ.empty n)) (hsp : n + 1 ≤ C.space) (hS : WordRAM.Fits w (base + C.space)) (hK : WordRAM.Fits w K_max) (harg_eq : C.argMatch = P.argMatch) {f : Fin r_pq → Fin r_caller} (hf : Function.Injective f) {zeroReg_c oneReg_c vReg_c kReg_c : Fin r_caller} (ho_out : oneReg_c ∉ Set.range f) (hv_out : vReg_c ∉ Set.range f) (hk_out : kReg_c ∉ Set.range f) {s : IndexedMinPQ.State n} {x : WordRAM.Data r_caller} {v : Fin n} {k : ℕ} (hr : C.rep s (WordRAM.Data.comap f x)) (hval : (∃ (k₀ : ℕ), s v = some k₀ ∧ k ≤ k₀) ∧ k ≤ K_max) (hz0 : x.regs zeroReg_c = 0) (ho1 : x.regs oneReg_c = 1) (hvv : x.regs vReg_c = ↑v) (hvk : x.regs kReg_c = k) :
∃ (z : WordRAM.Data r_caller) (t : ℕ), WordRAM.ExecWithin w (P.callDecreaseKey f zeroReg_c oneReg_c vReg_c kReg_c) x z t ∧ C.rep (Function.update s v (some k)) (WordRAM.Data.comap f z) ∧ WordRAM.Modifies { base := base, length := C.space }.footprint x.mem z.mem ∧ (∀ j ∉ Set.range f, z.regs j = x.regs j) ∧ t + C.potential (WordRAM.Data.comap f z) ≤ 5 + C.charge s (IndexedMinPQ.Op.decreaseKey v k) + C.potential (WordRAM.Data.comap f x)

Inline execution of P.callDecreaseKey: lowers v's key to k and charges 5 + C.charge s (.decreaseKey v k).

theorem Cslib.DataStructure.PriorityQueue.WordRAM.PQCode.callGetKey_exec {B : WordRAM.Basis} {r_pq r_caller : ℕ} {P : PQCode B r_pq} {n K_max w base : ℕ} (C : WordRAM.StateTransitionImpl P.toSubroutineCode (indexedMinPQSpec n K_max) w base ![n] (IndexedMinPQ.empty n)) (hsp : n + 1 ≤ C.space) (hS : WordRAM.Fits w (base + C.space)) (harg_eq : C.argMatch = P.argMatch) (hdec_eq : C.decode = P.decode) {f : Fin r_pq → Fin r_caller} (hf : Function.Injective f) {zeroReg_c oneReg_c vReg_c : Fin r_caller} (hz_out : zeroReg_c ∉ Set.range f) (ho_out : oneReg_c ∉ Set.range f) (hv_out : vReg_c ∉ Set.range f) {s : IndexedMinPQ.State n} {x : WordRAM.Data r_caller} (v : Fin n) (hr : C.rep s (WordRAM.Data.comap f x)) (hz0 : x.regs zeroReg_c = 0) (ho1 : x.regs oneReg_c = 1) (hvv : x.regs vReg_c = ↑v) :
∃ (z : WordRAM.Data r_caller) (t : ℕ), WordRAM.ExecWithin w (P.callGetKey f zeroReg_c oneReg_c vReg_c) x z t ∧ P.decode (IndexedMinPQ.Op.getKey v) (WordRAM.Data.comap f z).regs = some (s v) ∧ C.rep s (WordRAM.Data.comap f z) ∧ WordRAM.Modifies { base := base, length := C.space }.footprint x.mem z.mem ∧ (∀ j ∉ Set.range f, z.regs j = x.regs j) ∧ t + C.potential (WordRAM.Data.comap f z) ≤ 4 + C.charge s (IndexedMinPQ.Op.getKey v) + C.potential (WordRAM.Data.comap f x)

Inline execution of P.callGetKey: queries s v and charges 4 + C.charge s (.getKey v).

PQPackage: Parametric priority-queue implementation bundle #

A priority-queue implementation over basis B with uniform PQCode B r_pq, memory footprint space n, per-operation amortized instruction charges *Time n, and parametric WordRAM.StateTransitionImpl certificates supporting arbitrary concrete potentials potential : Data r_pq → ℕ.

Translate a PQPackage along any basis simulation.

@[reducible, inline]

Lift an exact-basis PQPackage to Basis.standard.