Documentation

Complexity.Computability.WordRAM.Basis

Operation bases for word RAMs #

Word-RAM models differ in their arithmetic and logical instructions. Hagerup distinguishes the restricted RAM, without multiplication, from the multiplication RAM Hagerup, and the AC⁰ RAM allows only operations computed by polynomial-size constant-depth circuits, which excludes multiplication Andersson, Miltersen and Thorup. Linear-time and data-structure bounds can depend on this choice.

A basis (Cslib.WordRAM.Basis) fixes these operations, as a circuit basis does: a signature of operation symbols with arities (Cslib.Circuits.Signature), and the value of every symbol on natural numbers at each word size w, which may be undefined (none). The machine itself, its registers, memory, control flow and faults are the same for every basis; an operation faults when its value is undefined or does not fit a word. A basis also marks the operations that are exempt from word-size monotonicity: every other operation keeps each value that fits a word at every larger word size, so that a successful run of a program avoiding the marked operations succeeds in the same way at every larger word size.

Bases are compared by meaning-preserving maps of their operations (Cslib.WordRAM.Basis.Hom), built on Cslib.Circuits.Signature.Hom. A program over a basis runs instruction for instruction over a larger one; Cslib.WordRAM.Basis.restrict gives the sub-bases of a basis.

Main definitions #

Main results #

References #

A basis of word operations: a signature of operation symbols, the value of each operation at each word size, none where it is undefined, and the operations that may read the word size. An operation that does not read the word size keeps each value that fits a word at every larger word size. Whether a value fits a word is checked by the machine, not by the basis.

  • Arity : self.Op → ℕ
  • decEq : DecidableEq self.Op

    Operation symbols have decidable equality, so that programs do.

  • ReadsWidth : self.Op → Prop

    The operations exempt from word-size monotonicity (interp_mono_of_not_readsWidth), such as one that returns the word size.

  • decReadsWidth : DecidablePred self.ReadsWidth

    Whether an operation reads the word size is decidable.

  • interp : ℕ → (f : self.Op) → (Fin (self.Arity f) → ℕ) → Option ℕ

    The value of every operation at word size w, or none if it is undefined.

  • interp_mono_of_not_readsWidth (f : self.Op) (hf : ¬self.ReadsWidth f) {w w' : ℕ} (hw : w ≤ w') {args : Fin (self.Arity f) → ℕ} {v : ℕ} (h : self.interp w f args = some v) (hv : v < 2 ^ w) : self.interp w' f args = some v

    An operation that does not read the word size keeps each value that fits a word at every larger word size.

Instances For

    The standard basis #

    The binary operations of the word RAM.

    • add : BinOp

      Addition.

    • sub : BinOp

      Subtraction.

    • mul : BinOp

      Multiplication.

    • div : BinOp

      Division, rounding down.

    • mod : BinOp

      Remainder.

    • and : BinOp

      Bitwise and.

    • or : BinOp

      Bitwise or.

    • xor : BinOp

      Bitwise exclusive or.

    • shiftLeft : BinOp

      Shift left: x <<< y = x * 2 ^ y.

    • shiftRight : BinOp

      Shift right: x >>> y = x / 2 ^ y.

    Instances For
      @[instance_reducible]

      The value of a binary operation on natural numbers. Subtraction is truncated, and division and remainder by zero follow Lean's conventions x / 0 = 0 and x % 0 = x. Whether the value fits a word is checked by the instruction that writes it.

      Instances For

        The operations of the standard basis: the binary operations, and reading the word size.

        Instances For
          Instances For

            The number of arguments of a standard operation.

            Instances For

              The value of a standard operation at word size w.

              Instances For
                @[reducible, inline]

                The standard word-RAM basis: addition, subtraction, multiplication, bitwise operations and shifts as in Hagerup, together with division and remainder, and the word size, which word-RAM algorithms may read Fredman and Willard. Only the word size reads the word size.

                Instances For
                  @[simp]
                  theorem Cslib.WordRAM.Basis.standard_interp_bin (w : ℕ) (f : BinOp) (x : Fin 2 → ℕ) :
                  standard.interp w (StandardOp.bin f) x = some (f.eval (x 0) (x 1))

                  The exact basis #

                  The exact value of a binary operation on natural numbers: subtraction below zero, and division and remainder by zero, are undefined. Elsewhere it is BinOp.eval.

                  Instances For

                    The exact value of a standard operation at word size w.

                    Instances For
                      @[reducible, inline]

                      The exact word-RAM basis: the operations of Basis.standard, where every operation computes its exact value, so that subtraction below zero and division and remainder by zero are undefined and fault. Only the word size reads the word size. The bounded-word RAM of MIT–ULB CompGeom Group does not reject a negative difference or a zero divisor as such: it subtracts signed words, its division and remainder by zero follow Lean's integer conventions x / 0 = 0 and x % 0 = x, and only its usual check that the result fits a signed word applies.

                      Instances For
                        @[simp]
                        theorem Cslib.WordRAM.Basis.exact_interp_bin (w : ℕ) (f : BinOp) (x : Fin 2 → ℕ) :
                        exact.interp w (StandardOp.bin f) x = f.evalExact (x 0) (x 1)

                        Maps of bases #

                        structure Cslib.WordRAM.Basis.Hom (B B' : Basis) extends B.Hom B'.toSignature :

                        A map of bases: a map of signatures (Cslib.Circuits.Signature.Hom) such that at each word size the image of every operation has the operation's value, and that sends an operation not marked as reading the word size to one not so marked. Programs over B then run instruction for instruction over B'.

                        Instances For
                          @[reducible, inline]
                          abbrev Cslib.WordRAM.Basis.Hom.map {B B' : Basis} (φ : B.Hom B') (f : B.Op) :
                          B'.Op

                          The image of an operation under a map of bases.

                          Instances For
                            theorem Cslib.WordRAM.Basis.Hom.arity_map {B B' : Basis} (φ : B.Hom B') (f : B.Op) :
                            B'.Arity (φ.map f) = B.Arity f
                            theorem Cslib.WordRAM.Basis.Hom.interp_map {B B' : Basis} (φ : B.Hom B') (w : ℕ) (f : B.Op) (x : Fin (B.Arity f) → ℕ) :
                            (B'.interp w (φ.map f) fun (a : Fin (B'.Arity (φ.map f))) => x (Fin.cast ⋯ a)) = B.interp w f x

                            The image of an operation, applied to arguments reindexed along arity_map, has the value of the operation.

                            The identity map of a basis.

                            Instances For
                              def Cslib.WordRAM.Basis.Hom.comp {B B' B'' : Basis} (ψ : B'.Hom B'') (φ : B.Hom B') :
                              B.Hom B''

                              The composition of maps of bases.

                              Instances For
                                @[simp]
                                theorem Cslib.WordRAM.Basis.Hom.id_map {B : Basis} (f : B.Op) :
                                (Hom.id B).map f = f
                                @[simp]
                                theorem Cslib.WordRAM.Basis.Hom.comp_map {B B' B'' : Basis} (ψ : B'.Hom B'') (φ : B.Hom B') (f : B.Op) :
                                (ψ.comp φ).map f = ψ.map (φ.map f)

                                Sub-bases #

                                The sub-basis of B consisting of the operations satisfying P.

                                Instances For
                                  def Cslib.WordRAM.Basis.restrictHom (B : Basis) (P : B.Op → Prop) :
                                  (B.restrict P).Hom B

                                  The inclusion of a sub-basis.

                                  Instances For

                                    Addition and truncated subtraction.

                                    Instances For

                                      Addition, truncated subtraction, multiplication, division and remainder: the operation symbols of the bounded-word RAM of MIT–ULB CompGeom Group, without bitwise operations, shifts or the word size. Words here are unsigned and subtraction is truncated, whereas the source subtracts signed words; on the other operations and nonnegative arguments the two agree.

                                      Instances For

                                        The additive operations are arithmetic.

                                        Instances For

                                          The arithmetic operations are standard.

                                          Instances For

                                            The additive operations are standard.

                                            Instances For