Documentation

Complexity.Algorithms.WordRAM.Queue.Basic

Two stacks implementing a queue in shared RAM #

Two disjoint reserved blocks hold bottom-first stack arrays. Enqueue pushes onto the rear; dequeue transfers the rear into an empty front before popping. All address arithmetic, memory accesses, loop tests and operation dispatch are machine instructions. Empty live prefixes do not require clearing the reserved memory.

Main definitions #

Main results #

References #

@[reducible, inline]

Operation tag: enqueue zero, dequeue one.

Instances For
    @[reducible, inline]

    Enqueued word.

    Instances For
      @[reducible, inline]

      Front stack length.

      Instances For
        @[reducible, inline]

        Front block base.

        Instances For
          @[reducible, inline]

          Rear stack length.

          Instances For
            @[reducible, inline]

            Rear block base.

            Instances For
              @[reducible, inline]

              Temporary address.

              Instances For
                @[reducible, inline]

                Returned word.

                Instances For
                  @[reducible, inline]

                  Returned presence flag.

                  Instances For
                    @[reducible, inline]

                    Constant zero.

                    Instances For
                      @[reducible, inline]

                      Constant one.

                      Instances For
                        structure Cslib.WordRAM.Queue.Rep (w base capacity : ℕ) (front back : List ℕ) (x : Data 11) :

                        Both stack arrays live in one bounded memory, with one common capacity bound.

                        Instances For

                          The fixed reserved region, including currently unused cells.

                          Instances For

                            Enqueue writes the next rear cell and advances the rear length.

                            Instances For

                              Move the top rear word onto the front stack.

                              Instances For

                                Reverse the rear stack onto the front, testing each iteration on machine registers.

                                Instances For

                                  Pop the front, distinguishing the empty queue from a returned zero.

                                  Instances For

                                    Transfer only when the front is empty.

                                    Instances For

                                      One fixed dispatcher, independent of queue contents, capacity, and operation arguments.

                                      Instances For
                                        def Cslib.WordRAM.Queue.implementation (w capacity : ℕ) :
                                        DynamicProgram (spec w capacity) 11

                                        Register calling convention for the fixed queue dispatcher.

                                        Instances For
                                          theorem Cslib.WordRAM.Queue.enqueue_exec {w base capacity v : ℕ} {front back : List ℕ} {x : Data 11} (hc : Fits w (base + 2 * capacity)) (hr : Rep w base capacity front back x) (hs : front.length + back.length < capacity) (hv : x.regs arg = v) :
                                          ∃ (y : Data 11), Exec w enqueue x y 4 ∧ Rep w base capacity front (v :: back) y ∧ Modifies (footprint base capacity) x.mem y.mem

                                          Enqueue body correctness with an exact four-instruction execution.

                                          theorem Cslib.WordRAM.Queue.move_exec {w base capacity v : ℕ} {front back : List ℕ} {x : Data 11} (hc : Fits w (base + 2 * capacity)) (hr : Rep w base capacity front (v :: back) x) :
                                          ∃ (y : Data 11), Exec w move x y 6 ∧ Rep w base capacity (v :: front) back y ∧ Modifies (footprint base capacity) x.mem y.mem

                                          A rear-to-front move performs six instructions and preserves the reserved-region frame.

                                          theorem Cslib.WordRAM.Queue.transfer_exec {w base capacity : ℕ} {front back : List ℕ} {x : Data 11} (hc : Fits w (base + 2 * capacity)) (hr : Rep w base capacity front back x) :
                                          ∃ (y : Data 11), Exec w transfer x y (8 * back.length + 2) ∧ Rep w base capacity (back.reverse ++ front) [] y ∧ Modifies (footprint base capacity) x.mem y.mem

                                          Transfer costs eight instructions per moved word and two instructions to exit the loop.

                                          theorem Cslib.WordRAM.Queue.pop_exec {w base capacity : ℕ} {front back : List ℕ} {x : Data 11} (hc : Fits w (base + 2 * capacity)) (hr : Rep w base capacity front back x) :
                                          ∃ (y : Data 11), Exec w pop x y (if front = [] then 3 else 6) ∧ Rep w base capacity front.tail back y ∧ (if y.regs present = 0 then none else some (y.regs out)) = front.head? ∧ y.mem = x.mem

                                          Front-pop execution counts include the empty/nonempty branch and distinguish returned zero.

                                          Exact instruction count of a dispatched operation, excluding the final halt.

                                          Instances For
                                            theorem Cslib.WordRAM.Queue.arguments_fit {w capacity : ℕ} (h1 : Fits w 1) {q : List ℕ} (o : Algorithms.Lean.Queue.QueueOp ℕ) (hv : (spec w capacity).valid q o) (i : Fin 11) :
                                            Fits w ((implementation w capacity).encode o i)
                                            theorem Cslib.WordRAM.Queue.execute {w base capacity : ℕ} {front back : List ℕ} {x : Data 11} (hc : Fits w (base + 2 * capacity)) (hr : Rep w base capacity front back x) (o : Algorithms.Lean.Queue.QueueOp ℕ) (hv : (spec w capacity).valid (contents (front, back)) o) :
                                            ∃ (y : Data 11), Exec w program ((implementation w capacity).prepare o x) y (bodyCost front back o) ∧ (implementation w capacity).decode o y.regs = some (pairStep (front, back) o).2 ∧ Rep w base capacity (pairStep (front, back) o).1.1 (pairStep (front, back) o).1.2 y ∧ Modifies (footprint base capacity) x.mem y.mem

                                            Every valid operation executes the fixed dispatcher with its exact instruction count, implements the two-stack transition, and preserves every cell outside the reserved region.

                                            Install an empty queue using six register instructions, with no writes to reserved memory.

                                            Instances For
                                              theorem Cslib.WordRAM.Queue.initialize_exec {w base capacity : ℕ} {x : Data 11} (hx : Data.Bounded w x) (h1 : Fits w 1) (hc : Fits w (base + 2 * capacity)) :
                                              ∃ (y : Data 11), Exec w (initializeCode base capacity) x y 6 ∧ Rep w base capacity [] [] y ∧ y.mem = x.mem

                                              Initialization accepts arbitrary bounded registers and memory. Only the six representation registers change; both reserved live prefixes are empty, so memory remains untouched.