Documentation

CslibFrontier.DataStructure.PriorityQueue.Spec

Min-priority queue state-transition specifications #

This file defines two abstract state-transition specifications (Cslib.DataStructure.StateTransitionSpec) for min-priority queues over finite handles:

  1. Cslib.DataStructure.PriorityQueue.indexedMinPQSpec n K_max: a deterministic, graph-free specification for an indexed min-priority queue over caller-chosen handles Fin n and natural-number keys bounded by K_max (used by graph algorithms such as Dijkstra's algorithm). Equal-key ties in extractMin are broken deterministically by smallest handle index (IndexedMinPQ.PrioLE).
  2. Cslib.DataStructure.PriorityQueue.minPQSpec n_max U V: a relational specification for a bounded-capacity min-priority queue storing key–value entries (u, v) (u < U, v < V) addressed by handles h : Fin n_max allocated by the data structure (insert u v nondeterministically chooses any unused handle h and extractMin nondeterministically removes any minimum-key entry), together with a deterministic free-list handle allocator (MinPQ.AllocState, MinPQ.stepAlloc) refining minPQSpec.

Main definitions #

Main results #

References #

Operations on an indexed min-priority queue over handles Fin n and natural-number keys.

  • isEmpty {n : ℕ} : Op n

    Test whether the queue holds no handles.

  • getKey {n : ℕ} (v : Fin n) : Op n

    Query the current key of handle v, if present.

  • insert {n : ℕ} (v : Fin n) (k : ℕ) : Op n

    Insert an absent handle v with key k.

  • decreaseKey {n : ℕ} (v : Fin n) (k : ℕ) : Op n

    Lower the key of a present handle v to k.

  • extractMin {n : ℕ} : Op n

    Remove and return the present (handle, key) pair of minimum (key, handle).

Instances For
    @[instance_reducible]
    @[reducible, inline]

    Abstract state of an indexed min-priority queue on Fin n.

    The empty priority queue on Fin n.

    The finite set of handles currently present in state s.

    @[reducible, inline]

    Lexicographic priority comparison on raw (handle, key) : ℕ × ℕ pairs: smaller key first, breaking equal-key ties by smaller handle index.

    Instances For

      Lexicographic priority comparison on (handle, key) pairs: smaller key first, breaking equal-key ties by smaller handle index.

      Instances For
        theorem Cslib.DataStructure.PriorityQueue.IndexedMinPQ.PrioLE.trans {n : ℕ} {a b c : Fin n × ℕ} (hab : PrioLE a b) (hbc : PrioLE b c) :
        PrioLE a c

        Transitivity of PrioLE.

        theorem Cslib.DataStructure.PriorityQueue.IndexedMinPQ.PrioLE.antisymm {n : ℕ} {a b : Fin n × ℕ} (hab : PrioLE a b) (hba : PrioLE b a) :
        a = b

        Antisymmetry of PrioLE.

        Strict lexicographic priority comparison on raw (handle, key) : ℕ × ℕ pairs: smaller key first, breaking equal-key ties by strictly smaller handle index.

        Instances For

          Strict priority order RawPrioLT implies weak priority order RawPrioLE.

          Strict lexicographic priority comparison on (handle, key) pairs: smaller key first, breaking equal-key ties by strictly smaller handle index.

          Instances For
            theorem Cslib.DataStructure.PriorityQueue.IndexedMinPQ.PrioLT.trans {n : ℕ} {a b c : Fin n × ℕ} (hab : PrioLT a b) (hbc : PrioLT b c) :
            PrioLT a c

            Transitivity of PrioLT.

            PrioLT mixed with PrioLE on the left.

            PrioLT mixed with PrioLE on the right.

            theorem Cslib.DataStructure.PriorityQueue.IndexedMinPQ.prioLT_or_prioLT_of_ne {n : ℕ} {u v : Fin n} (huv : u ≠ v) (ku kv : ℕ) :
            PrioLT (u, ku) (v, kv) ∨ PrioLT (v, kv) (u, ku)

            Any two pairs with distinct handles are strictly comparable under PrioLT.

            Pick the lexicographically smaller (handle, key) pair.

            Minimum (handle, key) pair of a list under PrioLE, or none on the empty list.

            Unique present (u, k) minimizing (k, u.val) lexicographically, or none if empty.

            @[reducible, inline]

            Operation return types for Op n.

            Pure transition function of an indexed min-priority queue on Fin n.

            Admissibility predicate for Op n with key upper bound K_max.

            @[reducible, inline]

            Graph-free indexed min-priority queue specification with key upper bound K_max.

            Domain and findMin lemmas #

            theorem Cslib.DataStructure.PriorityQueue.IndexedMinPQ.mem_dom_iff {n : ℕ} {s : State n} {v : Fin n} :
            v ∈ dom s ↔ ∃ (k : ℕ), s v = some k

            A handle belongs to dom s if and only if s v is some k for some key k.

            @[simp]

            A handle is outside dom s if and only if s v = none.

            @[simp]

            The empty priority queue has empty domain.

            @[simp]

            Updating v to some k inserts v into dom s.

            Lowering the key of a handle already in dom s preserves dom s.

            @[simp]

            Clearing u to none erases u from dom s.

            @[simp]

            findMin s returns none if and only if dom s is empty.

            If findMin s = some (u, k), then s u = some k.

            Any handle returned by findMin belongs to dom s.

            theorem Cslib.DataStructure.PriorityQueue.IndexedMinPQ.findMin_le {n : ℕ} {s : State n} {u : Fin n} {k : ℕ} (h : findMin s = some (u, k)) (v : Fin n) (k' : ℕ) :
            s v = some k' → k < k' ∨ k = k' ∧ ↑u ≤ ↑v

            findMin minimizes (k, u.val) lexicographically among all present handles.

            theorem Cslib.DataStructure.PriorityQueue.IndexedMinPQ.findMin_key_le {n : ℕ} {s : State n} {u : Fin n} {k : ℕ} (h : findMin s = some (u, k)) {v : Fin n} {k' : ℕ} (hv : s v = some k') :
            k ≤ k'

            The key returned by findMin is at most the key of every present handle.

            theorem Cslib.DataStructure.PriorityQueue.IndexedMinPQ.findMin_eq_some_iff {n : ℕ} {s : State n} {u : Fin n} {k : ℕ} :
            findMin s = some (u, k) ↔ s u = some k ∧ ∀ (v : Fin n) (k' : ℕ), s v = some k' → k < k' ∨ k = k' ∧ ↑u ≤ ↑v

            Complete characterization of findMin: findMin s = some (u, k) if and only if s u = some k and (u, k) precedes every present (v, k') under PrioLE.

            Domain evolution of extractMin when findMin s = some (u, k).

            Operation counts and per-kind cost bounds #

            Count isEmpty and getKey queries in an operation sequence.

            Count insert operations in an operation sequence.

            Count decreaseKey operations in an operation sequence.

            Count extractMin operations in an operation sequence.

            theorem Cslib.DataStructure.PriorityQueue.IndexedMinPQ.totalCost_le {n K_max : ℕ} (cost : State n → Op n → ℕ) {cQ cI cD cE : ℕ} (hEmpty : ∀ (s : State n), cost s Op.isEmpty ≤ cQ) (hGet : ∀ (s : State n) (v : Fin n), cost s (Op.getKey v) ≤ cQ) (hIns : ∀ (s : State n) (v : Fin n) (k : ℕ), cost s (Op.insert v k) ≤ cI) (hDec : ∀ (s : State n) (v : Fin n) (k : ℕ), cost s (Op.decreaseKey v k) ≤ cD) (hExt : ∀ (s : State n), cost s Op.extractMin ≤ cE) (s : State n) (ops : List (Op n)) :
            (indexedMinPQSpec n K_max).totalCost cost s ops ≤ cQ * numQueries ops + cI * numInserts ops + cD * numDecreaseKeys ops + cE * numExtractMins ops

            Bound totalCost of an indexed priority-queue history by per-kind charge upper bounds.

            @[reducible, inline]

            Abstract state of a min-priority queue of capacity n_max: each handle h : Fin n_max is either unused (none) or stores a key–value pair some (u, v).

            Initial empty priority queue with all handles unused.

            The finite set of currently active handles in s.

            The current number of stored elements in s.

            Operations of a min-priority queue with handles of capacity n_max.

            • isEmpty {n_max : ℕ} : Op n_max

              Test whether the priority queue currently stores no elements.

            • get {n_max : ℕ} (h : Fin n_max) : Op n_max

              Query the (key, value) pair currently stored at handle h (none if h is inactive).

            • insert {n_max : ℕ} (u v : ℕ) : Op n_max

              Insert a new entry with key u and value v, returning its allocated handle some h.

            • decreaseKey {n_max : ℕ} (h : Fin n_max) (u' : ℕ) : Op n_max

              Decrease the key at active handle h to u'.

            • extractMin {n_max : ℕ} : Op n_max

              Remove and return a minimum-key entry some (u, v, h), or none if empty.

            @[instance_reducible]
            def Cslib.DataStructure.PriorityQueue.MinPQ.instDecidableEqOp.decEq {n_max✝ : ℕ} (x✝ x✝¹ : Op n_max✝) :
            Decidable (x✝ = x✝¹)
            @[reducible, inline]

            Operation return types for Op n_max.

            Domain, element count, and minimum-entry lemmas #

            @[simp]
            @[simp]
            theorem Cslib.DataStructure.PriorityQueue.MinPQ.mem_dom {n_max : ℕ} {s : State n_max} {h : Fin n_max} :
            h ∈ dom s ↔ (s h).isSome = true
            theorem Cslib.DataStructure.PriorityQueue.MinPQ.mem_dom_iff {n_max : ℕ} {s : State n_max} {h : Fin n_max} :
            h ∈ dom s ↔ ∃ (u : ℕ) (v : ℕ), s h = some (u, v)
            theorem Cslib.DataStructure.PriorityQueue.MinPQ.notMem_dom_iff {n_max : ℕ} {s : State n_max} {h : Fin n_max} :
            h ∉ dom s ↔ s h = none
            theorem Cslib.DataStructure.PriorityQueue.MinPQ.dom_eq_empty_iff {n_max : ℕ} {s : State n_max} :
            dom s = ∅ ↔ ∀ (h : Fin n_max), s h = none
            theorem Cslib.DataStructure.PriorityQueue.MinPQ.dom_update_some {n_max : ℕ} (s : State n_max) (h : Fin n_max) (u v : ℕ) :
            theorem Cslib.DataStructure.PriorityQueue.MinPQ.size_update_some_of_none {n_max : ℕ} {s : State n_max} {h : Fin n_max} (hh : s h = none) (u v : ℕ) :
            theorem Cslib.DataStructure.PriorityQueue.MinPQ.size_update_some_of_some {n_max : ℕ} {s : State n_max} {h : Fin n_max} {u v : ℕ} (hh : s h = some (u, v)) (u' v' : ℕ) :
            theorem Cslib.DataStructure.PriorityQueue.MinPQ.size_update_none_of_some {n_max : ℕ} {s : State n_max} {h : Fin n_max} {u v : ℕ} (hh : s h = some (u, v)) :
            theorem Cslib.DataStructure.PriorityQueue.MinPQ.exists_none_of_size_lt {n_max : ℕ} {s : State n_max} (h : size s < n_max) :
            ∃ (i : Fin n_max), s i = none

            If the current number of stored elements size s is strictly below n_max, at least one handle h : Fin n_max is unused (s h = none).

            Project a key–value priority-queue state to an IndexedMinPQ.State n_max by retaining only the key at each handle.

            @[simp]
            def Cslib.DataStructure.PriorityQueue.MinPQ.isMinEntry {n_max : ℕ} (s : State n_max) (h : Fin n_max) (u v : ℕ) :

            Handle h is active in s with entry (u, v) and u is less than or equal to the key of every active entry in s.

            Deterministic minimum-entry selector breaking key ties by smallest handle index via IndexedMinPQ.findMin (toIndexed s).

            theorem Cslib.DataStructure.PriorityQueue.MinPQ.findMin_eq_some_of_indexed {n_max : ℕ} {s : State n_max} {h : Fin n_max} {u : ℕ} (hfm : IndexedMinPQ.findMin (toIndexed s) = some (h, u)) :
            ∃ (v : ℕ), s h = some (u, v) ∧ findMin s = some (u, v, h) ∧ isMinEntry s h u v
            theorem Cslib.DataStructure.PriorityQueue.MinPQ.exists_isMinEntry_of_dom_ne_empty {n_max : ℕ} {s : State n_max} (hne : dom s ≠ ∅) :
            ∃ (h : Fin n_max) (u : ℕ) (v : ℕ), isMinEntry s h u v

            Relational specification minPQSpec and totality #

            Pure relational specification of a capacity-n_max min-priority queue with handles storing keys u < U and values v < V:

            • insert u v nondeterministically chooses any unused handle h : Fin n_max (s h = none);
            • extractMin nondeterministically removes and returns any stored entry (u, v, h) attaining the minimum key (MinPQ.isMinEntry s h u v), or none when MinPQ.dom s = ∅.
            Instances For

              Every admissible MinPQ operation has at least one valid relational transition: insert finds an unused handle by pigeonhole (exists_none_of_size_lt), and extractMin on a nonempty queue selects a minimum-key entry (exists_isMinEntry_of_dom_ne_empty).

              Deterministic free-list handle allocator refining minPQSpec #

              Internal deterministic allocator state refining State n_max: combines the active entry map entries with a LIFO recycled-handle stack freeList and a bump pointer fresh ≤ n_max.

              • entries : State n_max

                Active key–value entries indexed by handle h : Fin n_max.

              • freeList : List (Fin n_max)

                Recycled unused handles < fresh, ordered from top of stack (head) to bottom.

              • fresh : ℕ

                Bump pointer: all handles h with fresh ≤ h.val are unused and absent from freeList.

              Initial empty allocator state (entries = empty n_max, freeList = [], fresh = 0).

              Invariant of the free-list + bump-pointer handle allocator:

              1. s.fresh ≤ n_max;
              2. s.freeList.Nodup;
              3. every h ∈ s.freeList has h.val < s.fresh and s.entries h = none;
              4. every h : Fin n_max with s.fresh ≤ h.val has s.entries h = none;
              5. size s.entries + s.freeList.length = s.fresh.

              Deterministic handle allocation: pops the head of s.freeList when nonempty, or allocates the bump-pointer handle ⟨s.fresh, _⟩ and increments fresh when s.freeList = [] and s.fresh < n_max.

              Deterministic transition on AllocState n_max using allocHandle for insert and findMin s.entries (lexicographic (key, handle) tie-breaking) for extractMin.

              theorem Cslib.DataStructure.PriorityQueue.MinPQ.allocHandle_spec {n_max : ℕ} {s : AllocState n_max} (hwf : s.WellFormed) (hsz : size s.entries < n_max) (u v : ℕ) :
              ∃ (h : Fin n_max) (freeList' : List (Fin n_max)) (fresh' : ℕ), allocHandle s = some (h, freeList', fresh') ∧ s.entries h = none ∧ { entries := Function.update s.entries h (some (u, v)), freeList := freeList', fresh := fresh' }.WellFormed

              On any well-formed AllocState with size s.entries < n_max, allocHandle s succeeds, returns an unused handle h (s.entries h = none), and preserves AllocState.WellFormed after storing some (u, v) at h.

              theorem Cslib.DataStructure.PriorityQueue.MinPQ.stepAlloc_refines_spec {n_max U V : ℕ} {s : AllocState n_max} (hwf : s.WellFormed) (o : Op n_max) (hv : (minPQSpec n_max U V).valid s.entries o) :
              (minPQSpec n_max U V).step s.entries o (stepAlloc s o).1.entries (stepAlloc s o).2 ∧ (stepAlloc s o).1.WellFormed

              Every valid operation on a well-formed AllocState stepped by stepAlloc:

              1. satisfies the relational transition (minPQSpec n_max U V).step s.entries o (stepAlloc s o).1.entries (stepAlloc s o).2, and
              2. preserves AllocState.WellFormed.