Documentation

CodeLayer.Code.BitGates

A native bit-logic gate library #

The native cost model charges one step per AST unfolding and runs every bounded recursion for its full iteration count, so arithmetic on a magnitude-v operand costs Θ(v) — linear in the value, not in the number of bits. Equality of general natural-number labels is therefore out of reach in polynomial time on this model.

Computing with bits is not. Each gate below recurses on a bit rather than on a value, and each is bit-closed: its val is 0 or 1 whatever the input. Every gate comes with a val law, an RfindFree certificate (no unbounded search: the gates are total), and a tc bound linear in the input value — hence constant on bits, which is what the tc_*_bit bricks at the end state outright.

cShape0 is the reshaping n ↦ ⟨0, n⟩ that lets a bounded recursion recurse on a bare input. cIsPos and cNot are the two gates that recurse; cSwap is a pure rewiring; and cOr, cAnd and cEqBit are composed over those, the last two by De Morgan, so they carry no recursion of their own. The tc bounds all go through the uniform tc_prec_le.

The public surface is the gates cIsPos, cNot, cSwap, cOr, cAnd and cEqBit with their val laws (val_cIsPos, val_cNot, val_cOr, val_cAnd, val_cEqBit, val_cEqBit_bit), their RfindFree certificates and tc bounds (tc_cOr_le, tc_cAnd_le, tc_cEqBit_le), and the prec cores isPosCore, notCore and orCore with the same three laws each.

The module stays deliberately light over the cost model: these are Boolean primitives, and know nothing of the learners and arithmetic codes assembled from them.

Reshape n ↦ ⟨0, n⟩ so a prec recurses on the value n (seed 0).

Equations
Instances For

    cShape0 runs a constant five steps on every input.

    [n ≠ 0] as a prec recursing on the value: base 0, step 1.

    Equations
    Instances For

      Positivity indicator n ↦ [n ≠ 0] (bit-closed).

      Equations
      Instances For

        isPosCore on a packed ⟨a, n⟩ runs at most 4 * n + 2 steps.

        Negation n ↦ [n = 0] (bit-closed).

        Equations
        Instances For

          notCore on a bare input: cShape0 reshapes n ↦ ⟨0, n⟩ so the prec recurses on n itself, giving val cNot n = [n = 0].

          Equations
          Instances For

            notCore on a packed ⟨a, n⟩ runs at most 2 * n + 4 steps.

            Disjunction ⟨a,b⟩ ↦ a ∨ b (bit-closed): recurse on a, seed b.

            Equations
            Instances For

              orCore after cSwap. A prec recurses on its input's second component, so the swap puts a there and cOr ⟨a, b⟩ is the disjunction in the natural argument order.

              Equations
              Instances For
                theorem BitGates.val_orCore (b a : ℕ) :

                The value of orCore on a packed ⟨b, a⟩: a zero a returns the positivity of b, a nonzero a returns 1.

                theorem BitGates.val_cOr (a b : ℕ) :
                TimeCost.val cOr (Nat.pair a b) = if a = 0 then if b = 0 then 0 else 1 else 1
                theorem BitGates.tc_orCore_le (b a : ℕ) :
                TimeCost.tc orCore (Nat.pair b a) ≤ 4 * b + 4 * a + 9

                orCore on a packed ⟨b, a⟩ runs at most 4 * b + 4 * a + 9 steps.

                theorem BitGates.tc_cOr_le (a b : ℕ) :
                TimeCost.tc cOr (Nat.pair a b) ≤ 4 * a + 4 * b + 13

                Conjunction ⟨a,b⟩ ↦ a ∧ b via De Morgan (prec-free, bit-closed).

                Equations
                Instances For
                  theorem BitGates.val_cAnd (a b : ℕ) :

                  Bit-equality ⟨a,b⟩ ↦ [a = b] on bits, via (a ∧ b) ∨ (¬a ∧ ¬b) (prec-free, bit-closed). The general val law is "both zero, or both nonzero"; it coincides with equality exactly on the binary regime a, b ∈ {0,1} (val_cEqBit_bit).

                  Equations
                  Instances For
                    theorem BitGates.val_cEqBit (a b : ℕ) :
                    TimeCost.val cEqBit (Nat.pair a b) = if a = 0 then if b = 0 then 1 else 0 else if b = 0 then 0 else 1
                    theorem BitGates.val_cEqBit_bit {a b : ℕ} (ha : a ≤ 1) (hb : b ≤ 1) :

                    On bits, cEqBit genuinely tests equality.

                    Every gate is bit-closed: cIsPos outputs a bit.

                    theorem BitGates.tc_cAnd_le (a b : ℕ) :
                    TimeCost.tc cAnd (Nat.pair a b) ≤ 2 * a + 2 * b + 200

                    tc of the composite conjunction gate — linear in the input values (O(1) on bits).

                    theorem BitGates.tc_cEqBit_le (a b : ℕ) :
                    TimeCost.tc cEqBit (Nat.pair a b) ≤ 4 * a + 4 * b + 2000

                    tc of the composite bit-equality gate — linear in the input values (O(1) on bits).

                    Gate tc-on-bits bricks #

                    On bit operands every gate runs a constant number of steps: the prec-based gates unfold ≤ 1 times, the De-Morgan compositions are prec-free.

                    theorem BitGates.tc_cNot_bit {x : ℕ} (h : x ≤ 1) :
                    theorem BitGates.tc_cOr_bit {a b : ℕ} (ha : a ≤ 1) (hb : b ≤ 1) :
                    theorem BitGates.tc_cAnd_bit {a b : ℕ} (ha : a ≤ 1) (hb : b ≤ 1) :
                    theorem BitGates.tc_cEqBit_bit {a b : ℕ} (ha : a ≤ 1) (hb : b ≤ 1) :