Documentation

LeanPool.OrderMaintenance.Proof.Cost.PQ

The bucket priority queue (OrderMaintenance/Model/PQ.lean) #

QRep s k inQ bl nb: the queue of state s holds exactly the chunks c with inQ c, with key k c > θ. Bucket b is the doubly linked list bl b (from pqHead b along pqNext, back along pqPrev); the non-empty buckets form the increasing doubly linked list nb (from pqLo along nbNext), and pqHi is its last bucket.

Results: adding and deleting keep the representation; the +1 adjustment of a quiet chunk's size never takes the slow path (K2); pqPopMax returns a chunk of largest key; pqEmpty is exact.

structure OrderMaintenance.Proof.QRep (s : Model.Core) (k : ℕ → ℕ) (inQ : ℕ → Bool) (bl : ℕ → List ℕ) (nb : List ℕ) :

The concrete bucket queue represents the given keys, membership and ordered nonempty buckets.

Instances For
    def OrderMaintenance.Proof.QInv (s : Model.Core) (k : ℕ → ℕ) (inQ : ℕ → Bool) :

    The queue holds exactly inQ, keyed by k.

    Equations
    Instances For
      theorem OrderMaintenance.Proof.LList.congr {nxt prv nxt' prv' : ℕ → ℕ} {hd hd' : ℕ} {l : List ℕ} (h : LList nxt prv hd l) (hn : ∀ x ∈ l, nxt' x = nxt x) (hp : ∀ x ∈ l, prv' x = prv x) (hh : hd' = hd) :
      LList nxt' prv' hd' l
      theorem OrderMaintenance.Proof.LList.nil_iff {nxt prv : ℕ → ℕ} {hd : ℕ} {l : List ℕ} (h : LList nxt prv hd l) :

      The operations as functions of the state #

      pqLinkAfter s p: make bucket s non-empty, right after bucket p (0: at the front).

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For

        pqAdd c s p: push c at the head of bucket s.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For

          pqDel c: remove c from its bucket, and the bucket from the list if it became empty.

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For

            Sorted lists #

            theorem OrderMaintenance.Proof.sorted_split {l : List ℕ} (hs : List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) l) (s : ℕ) (hsl : s ∉ l) :
            l = List.filter (fun (x : ℕ) => decide (x < s)) l ++ List.filter (fun (x : ℕ) => decide (s < x)) l
            theorem OrderMaintenance.Proof.sorted_insert {l : List ℕ} (hs : List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) l) (s : ℕ) :
            List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) (List.filter (fun (x : ℕ) => decide (x < s)) l ++ s :: List.filter (fun (x : ℕ) => decide (s < x)) l)

            Linking a bucket into the list of nonempty buckets #

            theorem OrderMaintenance.Proof.linkSt_nbNext (st : Model.Core) (s p b : ℕ) :
            (linkSt st s p).nbNext.getX b = if b = s then if p = 0 then st.pqLo else st.nbNext.getX p else if p ≠ 0 ∧ b = p then s else st.nbNext.getX b
            theorem OrderMaintenance.Proof.linkSt_nbPrev (st : Model.Core) (s p b : ℕ) (hsn : s ≠ if p = 0 then st.pqLo else st.nbNext.getX p) :
            (linkSt st s p).nbPrev.getX b = if (if p = 0 then st.pqLo else st.nbNext.getX p) ≠ 0 ∧ b = if p = 0 then st.pqLo else st.nbNext.getX p then s else if b = s then p else st.nbPrev.getX b
            theorem OrderMaintenance.Proof.linkSt_pqHi (st : Model.Core) (s p : ℕ) :
            (linkSt st s p).pqHi = if (if p = 0 then st.pqLo else st.nbNext.getX p) ≠ 0 then st.pqHi else s
            theorem OrderMaintenance.Proof.linkSt_rest (st : Model.Core) (s p : ℕ) :
            (linkSt st s p).pqHead = st.pqHead ∧ (linkSt st s p).pqNext = st.pqNext ∧ (linkSt st s p).pqPrev = st.pqPrev ∧ (linkSt st s p).pqWhere = st.pqWhere ∧ (linkSt st s p).P = st.P

            Adding a chunk #

            theorem OrderMaintenance.Proof.QRep.disj {st : Model.Core} {k : ℕ → ℕ} {inQ : ℕ → Bool} {bl : ℕ → List ℕ} {nb : List ℕ} (hQ : QRep st k inQ bl nb) {a b c : ℕ} (ha : c ∈ bl a) (hb : c ∈ bl b) :
            a = b
            theorem OrderMaintenance.Proof.QRep.add {st : Model.Core} {k : ℕ → ℕ} {inQ : ℕ → Bool} {bl : ℕ → List ℕ} {nb : List ℕ} (hQ : QRep st k inQ bl nb) {c s p : ℕ} (hc : inQ c = false) (hc0 : c ≠ 0) (hs : st.P.theta < s) (hp : bl s = [] → p = (List.filter (fun (x : ℕ) => decide (x < s)) nb).getLastD 0) :
            QRep (addSt st c s p) (Function.update k c s) (Function.update inQ c true) (Function.update bl s (c :: bl s)) (if bl s = [] then List.filter (fun (x : ℕ) => decide (x < s)) nb ++ s :: List.filter (fun (x : ℕ) => decide (s < x)) nb else nb)

            Deleting a chunk #

            The bucket head after unlinking c, before the bucket list is touched.

            Equations
            Instances For
              theorem OrderMaintenance.Proof.delSt_nb_keep (st : Model.Core) (c : ℕ) (h : (delHead st c).getX (st.pqWhere.getX c) ≠ Model.NIL) :
              (delSt st c).nbNext = st.nbNext ∧ (delSt st c).nbPrev = st.nbPrev ∧ (delSt st c).pqLo = st.pqLo ∧ (delSt st c).pqHi = st.pqHi
              theorem OrderMaintenance.Proof.delSt_nb_drop (st : Model.Core) (c : ℕ) (h : (delHead st c).getX (st.pqWhere.getX c) = Model.NIL) :
              (∀ (b : ℕ), (delSt st c).nbNext.getX b = if st.nbPrev.getX (st.pqWhere.getX c) ≠ 0 ∧ b = st.nbPrev.getX (st.pqWhere.getX c) then st.nbNext.getX (st.pqWhere.getX c) else if b = st.pqWhere.getX c then 0 else st.nbNext.getX b) ∧ (∀ (b : ℕ), (delSt st c).nbPrev.getX b = if st.nbNext.getX (st.pqWhere.getX c) ≠ 0 ∧ b = st.nbNext.getX (st.pqWhere.getX c) then st.nbPrev.getX (st.pqWhere.getX c) else if b = st.pqWhere.getX c then 0 else st.nbPrev.getX b) ∧ ((delSt st c).pqLo = if st.nbPrev.getX (st.pqWhere.getX c) ≠ 0 then st.pqLo else st.nbNext.getX (st.pqWhere.getX c)) ∧ (delSt st c).pqHi = if st.nbNext.getX (st.pqWhere.getX c) ≠ 0 then st.pqHi else st.nbPrev.getX (st.pqWhere.getX c)
              theorem OrderMaintenance.Proof.getLastD_append_ne {l1 l2 : List ℕ} (h : l2 ≠ []) (d : ℕ) :
              (l1 ++ l2).getLastD d = l2.getLastD d
              theorem OrderMaintenance.Proof.delSt_bucket_erase {st : Model.Core} {c bucket : ℕ} {before after : List ℕ} (hw : st.pqWhere.getX c = bucket) (hB : LList (fun (z : ℕ) => st.pqNext.getX z) (fun (z : ℕ) => st.pqPrev.getX z) (st.pqHead.getX bucket) (before ++ c :: after)) :
              LList (fun (z : ℕ) => (delSt st c).pqNext.getX z) (fun (z : ℕ) => (delSt st c).pqPrev.getX z) ((delSt st c).pqHead.getX bucket) (before ++ after)

              Unlinking a queued chunk erases it from its bucket’bucket doubly linked list.

              theorem OrderMaintenance.Proof.QRep.del {st : Model.Core} {k : ℕ → ℕ} {inQ : ℕ → Bool} {bl : ℕ → List ℕ} {nb : List ℕ} (hQ : QRep st k inQ bl nb) {c : ℕ} (hc : inQ c = true) :
              QRep (delSt st c) k (Function.update inQ c false) (Function.update bl (k c) ((bl (k c)).erase c)) (if (bl (k c)).erase c = [] then nb.erase (k c) else nb)

              Only queue fields change #

              st' differs from st at most in the queue's fields.

              Instances For
                theorem OrderMaintenance.Proof.PQOnly.trans {a b c : Model.Core} (h1 : PQOnly a b) (h2 : PQOnly b c) :
                PQOnly a c
                theorem OrderMaintenance.Proof.addSt_only (st : Model.Core) (c s p : ℕ) :
                PQOnly st (addSt st c s p)

                The operations used by the algorithm #

                theorem OrderMaintenance.Proof.filter_lt_succ_erase {nb : List ℕ} (hnd : nb.Nodup) (s : ℕ) :
                List.filter (fun (x : ℕ) => decide (x < s + 1)) (nb.erase s) = List.filter (fun (x : ℕ) => decide (x < s)) nb

                The bucket list's elements below s: all of them are below s + 1 too, minus s.

                theorem OrderMaintenance.Proof.sorted_split_mem {l : List ℕ} (hs : List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) l) {s : ℕ} (hsl : s ∈ l) :
                l = List.filter (fun (x : ℕ) => decide (x < s)) l ++ s :: List.filter (fun (x : ℕ) => decide (s < x)) l
                theorem OrderMaintenance.Proof.pqAdjust_inc {st : Model.Core} {k : ℕ → ℕ} {inQ : ℕ → Bool} {bl : ℕ → List ℕ} {nb : List ℕ} (hQ : QRep st k inQ bl nb) {c old : ℕ} (hc0 : c ≠ 0) (hin : inQ c = decide (st.P.theta < old)) (hk : inQ c = true → k c = old) :
                ∃ (st' : Model.Core), EStateM.run (Model.pqAdjust c old (old + 1)) st = EStateM.Result.ok () st' ∧ PQOnly st st' ∧ QInv st' (Function.update k c (old + 1)) (Function.update inQ c (decide (st.P.theta < old + 1)))

                Lemma K2 for insertions. Adjusting a quiet chunk's key by +1 keeps the representation and never scans.

                theorem OrderMaintenance.Proof.QRep.empty_iff {st : Model.Core} {k : ℕ → ℕ} {inQ : ℕ → Bool} {bl : ℕ → List ℕ} {nb : List ℕ} (hQ : QRep st k inQ bl nb) :
                st.pqHi = 0 ↔ ∀ (d : ℕ), inQ d = false
                theorem OrderMaintenance.Proof.pqPopMax_ok {st : Model.Core} {k : ℕ → ℕ} {inQ : ℕ → Bool} {bl : ℕ → List ℕ} {nb : List ℕ} (hQ : QRep st k inQ bl nb) (hne : st.pqHi ≠ 0) :
                ∃ (c : ℕ), EStateM.run Model.pqPopMax st = EStateM.Result.ok c (delSt st c) ∧ inQ c = true ∧ ∀ (d : ℕ), inQ d = true → k d ≤ k c

                pqPopMax returns a queued chunk of largest key, and deletes it.