Documentation

CslibFrontier.DataStructure.Memory.Spec

Word-addressed memory as a state-transition specification #

Memory.spec w is a memory oracle: the interface through which a data-structure implementation reaches storage. Its state is the contents of every cell together with an allocation frontier. Every address below the frontier is allocated, and allocation hands out the block that starts at the frontier. The operations read a cell, write a cell, and allocate a block, either zeroed (alloc, charged per cell) or with its old contents (allocDirty, charged one unit).

Admissibility carries the preconditions a word RAM needs. Accessed addresses must be allocated, written values must fit a w-bit word, and the frontier after an allocation must fit. Under these preconditions the frontier and every cell keep fitting a word (WellFormed.step), so an allocated address fits too (WellFormed.fits_of_lt_frontier).

An implementation over this interface is an ordinary Memory.Prog program. Its correctness and its operation charges are proved against step and charge, without registers or instruction counts. charge is the memory-level currency; executing such a program on the word RAM is a separate step that exchanges it into instructions. Allocation never reuses addresses, as in Cslib.WordRAM.alloc, so blocks allocated at different times are disjoint (Cslib.WordRAM.Block.disjoint_of_le). A program reuses memory by keeping a block it allocated.

Main definitions #

Main results #

Memory: the contents of every cell, and the frontier below which cells are allocated.

  • mem : ℕ → ℕ

    The contents of the cell at each address.

  • frontier : ℕ

    Every address below the frontier is allocated; the next block starts here.

Instances For
    theorem Cslib.DataStructure.Memory.State.ext {x y : State} (mem : x.mem = y.mem) (frontier : x.frontier = y.frontier) :
    x = y

    Operations on memory. Addresses are words computed by the caller.

    • read (addr : ℕ) : Op

      Read the cell at an address.

    • write (addr value : ℕ) : Op

      Write a value to the cell at an address.

    • alloc (count : ℕ) : Op

      Allocate count zeroed cells at the frontier and return their base address.

    • allocDirty (count : ℕ) : Op

      Allocate count cells at the frontier without clearing them and return their base address.

    Instances For
      Instances For
        @[reducible, inline]

        The answer type of each operation: a read and an allocation return a word.

        Instances For

          The transition of each operation. An allocation returns the old frontier.

          Instances For

            Admissibility at word size w: accessed addresses are allocated, written values fit a word, and an allocation leaves a fitting frontier.

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

              Word-addressed memory at word size w.

              Instances For

                The memory-level currency: one unit per read, write and dirty allocation, and k + 1 units for a zeroed block of k cells.

                Instances For
                  @[reducible, inline]

                  Programs over the memory oracle. They are the clients of Memory.spec w at every word size.

                  Instances For
                    @[reducible, inline]

                    Read the cell at address a.

                    Instances For
                      @[reducible, inline]

                      Write v to the cell at address a.

                      Instances For
                        @[reducible, inline]

                        Allocate k zeroed cells and return their base address.

                        Instances For
                          @[reducible, inline]

                          Allocate k cells without clearing them and return their base address.

                          Instances For
                            @[simp]
                            @[simp]
                            theorem Cslib.DataStructure.Memory.step_write (s : State) (a v : ℕ) :
                            step s (Op.write a v) = ({ mem := Function.update s.mem a v, frontier := s.frontier }, ())
                            @[simp]
                            theorem Cslib.DataStructure.Memory.step_alloc (s : State) (k : ℕ) :
                            step s (Op.alloc k) = ({ mem := WordRAM.clear s.mem s.frontier k, frontier := s.frontier + k }, s.frontier)
                            @[simp]
                            theorem Cslib.DataStructure.Memory.step_allocDirty (s : State) (k : ℕ) :
                            step s (Op.allocDirty k) = ({ mem := s.mem, frontier := s.frontier + k }, s.frontier)
                            @[simp]
                            @[simp]

                            Every memory operation costs at least one unit, so the number of operations, hence the glue run after them, is paid for by their charges.

                            @[instance_reducible]

                            Admissibility of a concrete history is decidable.

                            Frontier and frame #

                            No operation lowers the frontier.

                            No sequence of operations lowers the frontier.

                            theorem Cslib.DataStructure.Memory.mem_step_of_lt_frontier (s : State) (o : Op) {a : ℕ} (ha : a < s.frontier) (hw : ∀ (v : ℕ), o ≠ Op.write a v) :
                            (step s o).1.mem a = s.mem a

                            An operation changes an allocated cell only by writing to it. Allocation clears cells at or above the frontier only.

                            Word fit #

                            The frontier and every cell fit a w-bit word. Admissible operations preserve this.

                            Instances For

                              Allocated addresses fit a word.

                              theorem Cslib.DataStructure.Memory.WellFormed.step {w : ℕ} {s : State} (h : WellFormed w s) {o : Op} (hv : valid w s o) :

                              Admissible operations preserve well-formedness.

                              theorem Cslib.DataStructure.Memory.WellFormed.run {w : ℕ} {s : State} (h : WellFormed w s) {os : List Op} (hv : (spec w).ValidHistory s os) :
                              WellFormed w ((spec w).run s os).1

                              Admissible histories preserve well-formedness.

                              The input convention #

                              The word RAM's input convention (Cslib.WordRAM.Data.init) as a memory state: the input length at address 0, the input words at addresses 1, …, n, and everything up to the input allocated.

                              Instances For
                                theorem Cslib.DataStructure.Memory.wellFormed_init {w : ℕ} {input : List ℕ} (hin : WordRAM.InputFits w input) (hf : WordRAM.Fits w (input.length + 1)) :

                                The input convention is well formed when the input and its frontier fit a word.