Documentation

Complexity.Algorithms.WordRAM.Stack

A fixed-program bounded stack on the word RAM #

A nine-register program dispatches push, pop, and size operations using an encoded operation tag. The stack occupies a fixed consecutive memory block, with its top at the highest live address. A pop from an empty stack returns none; other pops return a word and decrease the length without clearing the old cell. Push requires spare capacity and a fitting value.

The semantic certificate is independent of the separate instruction certificate. Every call includes its final halt; argument delivery and the initial reserved block are calling-convention assumptions. This realizes a bounded restriction of the mathematical stack, not its unrestricted interface. A native instruction theorem for an arbitrary free-monad client still excludes execution of the client's pure controller.

Main definitions #

Main results #

References #

The abstract stack interface is shared with Cslib.Algorithms.Lean.Queue. Array indexing and machine execution reuse the checked WordRAM.Allocation and WordRAM.Structured substrate.

@[reducible, inline]

A fixed-capacity restriction of the same mathematical stack interface.

Instances For
    @[reducible, inline]

    Encoded operation tag: push = 0, pop = 1, size = 2.

    Instances For
      @[reducible, inline]

      Caller-supplied push value.

      Instances For
        @[reducible, inline]

        Persistent number of live entries.

        Instances For
          @[reducible, inline]

          Persistent base address of the reserved region.

          Instances For
            @[reducible, inline]

            Temporary cell address.

            Instances For
              @[reducible, inline]

              Returned value.

              Instances For
                @[reducible, inline]

                Constant zero.

                Instances For
                  @[reducible, inline]

                  Constant one.

                  Instances For
                    @[reducible, inline]

                    Presence bit distinguishing none from a returned zero word.

                    Instances For

                      Fixed operation dispatch. No code is generated from an operation or its payload.

                      Instances For
                        def Cslib.WordRAM.Stack.implementation (w capacity : ℕ) :
                        DynamicProgram (spec w capacity) 9

                        Calling convention for the fixed dispatch program. Answers inspect only output registers.

                        Instances For
                          structure Cslib.WordRAM.Stack.Rep (w base capacity : ℕ) (vs : List ℕ) (x : Data 9) :

                          Persistent register, bounded-word, and live array invariants. Cells above the live prefix may contain old popped values; they are part of the reserved footprint.

                          Instances For

                            Reserved memory footprint; it stays fixed through pushes and pops.

                            Instances For
                              theorem Cslib.WordRAM.Stack.arguments_fit {w capacity : ℕ} (h2 : Fits w 2) {vs : List ℕ} (o : Algorithms.Lean.Queue.StackOp ℕ) (hv : (spec w capacity).valid vs o) (i : Fin 9) :
                              Fits w ((implementation w capacity).encode o i)

                              Actual instructions excluding the final halt.

                              Instances For
                                theorem Cslib.WordRAM.Stack.execute {w base capacity : ℕ} (h2 : Fits w 2) (hcap : Fits w (base + capacity)) {vs : List ℕ} {x : Data 9} (hr : Rep w base capacity vs x) (o : Algorithms.Lean.Queue.StackOp ℕ) (hv : (spec w capacity).valid vs o) :
                                ∃ (y : Data 9), Exec w program ((implementation w capacity).prepare o x) y (bodyCost vs o) ∧ (implementation w capacity).decode o y.regs = some ((spec w capacity).step vs o).2 ∧ Rep w base capacity ((spec w capacity).step vs o).1 y ∧ Modifies (footprint base capacity) x.mem y.mem

                                Every admissible operation executes the fixed dispatcher with an exact instruction count, returns the specified answer, and preserves memory outside the reserved region.

                                def Cslib.WordRAM.Stack.certificate {w base capacity : ℕ} (h2 : Fits w 2) (hcap : Fits w (base + capacity)) :

                                Semantic realization of the bounded stack, independently of its performance theorem.

                                Instances For
                                  theorem Cslib.WordRAM.Stack.instructions_eq {w base capacity : ℕ} (h2 : Fits w 2) (hcap : Fits w (base + capacity)) {vs : List ℕ} {x y : Data 9} {o : Algorithms.Lean.Queue.StackOp ℕ} {a : (spec w capacity).Result o} {trace : DynamicProgram.OperationTrace} (hr : Rep w base capacity vs x) (hv : (spec w capacity).valid vs o) (he : (implementation w capacity).ExecOp w x o a y trace) :
                                  trace.instructions = bodyCost vs o + 1

                                  Actual instruction count, including dispatch and the final halt, for every native call.

                                  theorem Cslib.WordRAM.Stack.instruction_bound {w base capacity : ℕ} (h2 : Fits w 2) (hcap : Fits w (base + capacity)) :
                                  Stateful.Bound (certificate h2 hcap).realization DynamicProgram.OperationTrace.instructions fun (x : (spec w capacity).State) (x_1 : (spec w capacity).Op) => 12

                                  Every admissible call has a separate worst-case bound of twelve native instructions.

                                  theorem Cslib.WordRAM.Stack.modifies {w base capacity : ℕ} (h2 : Fits w 2) (hcap : Fits w (base + capacity)) {vs : List ℕ} {x y : Data 9} {o : Algorithms.Lean.Queue.StackOp ℕ} {a : (spec w capacity).Result o} {trace : DynamicProgram.OperationTrace} (hr : Rep w base capacity vs x) (hv : (spec w capacity).valid vs o) (he : (implementation w capacity).ExecOp w x o a y trace) :
                                  Modifies (footprint base capacity) x.mem y.mem

                                  Every concrete call preserves all memory outside the same reserved stack block.

                                  theorem Cslib.WordRAM.Stack.frame {w base capacity : ℕ} (h2 : Fits w 2) (hcap : Fits w (base + capacity)) {vs : List ℕ} {x y : Data 9} {o : Algorithms.Lean.Queue.StackOp ℕ} {a : (spec w capacity).Result o} {trace : DynamicProgram.OperationTrace} (hr : Rep w base capacity vs x) (hv : (spec w capacity).valid vs o) (he : (implementation w capacity).ExecOp w x o a y trace) {Q : HeapAssertion} (hQ : Q.holds x.mem) (hd : Disjoint Q.footprint (footprint base capacity)) :

                                  The machine stack preserves any disjoint heap representation.

                                  An empty reserved block, with all memory zero and the persistent pointer installed. This is a calling-convention initial state, not an uncharged allocation instruction.

                                  Instances For
                                    theorem Cslib.WordRAM.Stack.initial_rep {w base capacity : ℕ} (hcap : Fits w (base + capacity)) :
                                    Rep w base capacity [] (initial base)

                                    The initial representation is inhabited for every fitting reserved block.

                                    theorem Cslib.WordRAM.Stack.run_client {w base capacity : ℕ} (h2 : Fits w 2) (hcap : Fits w (base + capacity)) {α : Type} (p : (spec w capacity).Client α) {vs : List ℕ} {x : Data 9} (hr : Rep w base capacity vs x) (hv : (spec w capacity).ValidHistory vs ((spec w capacity).runClient vs p).2.2) :
                                    ∃ (y : Data 9) (traces : List DynamicProgram.OperationTrace), Stateful.Executes ((implementation w capacity).ExecOp w) p x ((spec w capacity).runClient vs p).1 y traces ∧ Rep w base capacity ((spec w capacity).runClient vs p).2.1 y ∧ Stateful.total DynamicProgram.OperationTrace.instructions traces ≤ ((spec w capacity).runClient vs p).2.2.length * 12

                                    One-operation realization and an independent resource certificate compose across arbitrary admissible adaptive clients. The client controller itself is external to the machine code.