Documentation

ReflectiveSubsystems.CodeArith

Arithmetic as explicit codes #

Nat.Partrec.Code has eight constructors and no arithmetic primitive beyond succ: addition of two variables, multiplication, truncated subtraction, powers and comparisons must each be built from bounded recursion. This file builds them, each as a named code with its value equation.

What is reused #

The Boolean layer is already in the tree and is not rebuilt here — cIsPos, cNot, cAnd, cOr, cEqBit, cSwap and cShape0 come from the bit-gate library (CodeLayer/Code/BitGates.lean), together with their ≤ 1 bounds, and constCode from the polynomial-time layer. What is new here is the numeric layer above them.

The shape of every construction #

Bounded recursion prec cf cg on input Nat.pair a n runs cf on a and then folds cg over n, each step seeing Nat.pair a (Nat.pair y acc) — the original first argument, the counter, and the accumulator. Every definition below is that fold: addition counts successors, multiplication counts additions, powers count doublings, subtraction counts predecessors. The value laws are the corresponding inductions, and each is stated at Nat.pair-shaped input because that is the shape bounded recursion consumes.

No step-count facts are stated. Bounded recursion charges by the value of its counter in the native step model, so an addition built this way costs steps proportional to the number it adds; that is a property of the cost model, not of these codes, and the workspace account — which is what consumes them — is unaffected.

Projections and identity #

The accumulator of a bounded-recursion step: the third slot of ⟨a, ⟨y, acc⟩⟩.

Equations
Instances For

    The counter of a bounded-recursion step: the second slot of ⟨a, ⟨y, acc⟩⟩.

    Equations
    Instances For

      Addition #

      Predecessor and truncated subtraction #

      Truncated subtraction: ⟨x, y⟩ ↦ x - y, counting y predecessors from x.

      Equations
      Instances For

        Multiplication #

        Powers of two #

        Powers of two, folded: ⟨a, n⟩ ↦ 2 ^ n, counting n doublings from 1.

        Equations
        Instances For

          Comparison and equality #

          Truncated subtraction turns order into a zero test, which the bit-gate library already decides.

          Selection #

          With a {0,1}-valued guard, selection is arithmetic: multiply each branch by its indicator and add. This is the form the machine's branch conditions will take once they are decided by cEq.

          ⟨b, ⟨t, f⟩⟩ ↦ t when b ≠ 0, else f.

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

            Bit-length #

            Nat.size n is the least k with n < 2 ^ k, so it is found by raising an accumulator while 2 ^ acc still fits below n. Folding that step n times is enough, since Nat.size n ≤ n.

            The Nat.size fold's step: raise the accumulator while 2 ^ acc still fits.

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

              Division and remainder #

              n / d is the number of y with (y + 1) * d ≤ n, so it too is a fold that raises an accumulator while the next multiple still fits — guarded by d > 0, which makes the fold agree with the convention n / 0 = 0 rather than running away. Folding n times is enough.

              Inside the division fold, the two components of the original argument ⟨n, d⟩.

              Equations
              Instances For

                The division fold's step: raise the quotient while the next multiple still fits.

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