Documentation

CodeLayer.Code.SizeArith

Bit-length arithmetic for size accounts #

Bounding how much room a computation needs comes down, over and over, to two facts about Nat.size that Mathlib does not state: what pairing costs in bits, and how a bit-length compares with the value it measures. Mathlib bounds Nat.pair monotonically, but says nothing about its bit-length.

The module stays deliberately light: it is arithmetic on bit-lengths, and knows nothing about the programs whose sizes are being counted.

Square pairing costs at most twice the wider component. Nat.pair x y is Cantor's square pairing (y² + x or x² + x + y), so it lands strictly below (2^M)² where M is the larger of the two bit-lengths: Nat.size (Nat.pair x y) ≤ 2M. The constant 2 is attained (x = 1, y = 0 gives Nat.pair 1 0 = 2, of size 2 = 2·1). Mathlib bounds Nat.pair monotonically but does not state its bit-length.

theorem CodeSizeArith.size_pair_le (a b : ℕ) :
(Nat.pair a b).size ≤ 2 * a.size + 2 * b.size + 2

Nat.size (Nat.pair a b) ≤ 2·Nat.size a + 2·Nat.size b + 2: the pairing's output bit-length is linear in the inputs' bit-lengths (so pairing preserves poly output size).

Widths never exceed the value. Safe at zero, unlike the max-form bounds.

Pairing never shortens its first component: Nat.size a ≤ Nat.size (Nat.pair a b), since a ≤ Nat.pair a b.

Pairing never shortens its second component: Nat.size b ≤ Nat.size (Nat.pair a b), since b ≤ Nat.pair a b.

theorem CodeSizeArith.size_succ_le_size {m n : ℕ} (h : m ≤ n) :
(m + 1).size ≤ n.size + 1

Incrementing below a bound costs at most one bit over the bound's width: if m ≤ n then Nat.size (m + 1) ≤ Nat.size n + 1.

theorem CodeSizeArith.size_two_mul_succ_le (S : ℕ) :
(2 * S + 1).size ≤ S.size + 1

Doubling and adding one costs at most one bit: Nat.size (2 * S + 1) ≤ Nat.size S + 1.

Nat.size (4 * p + 4) ≤ Nat.size p + 3: quadrupling and adding a small constant costs at most three more bits. From p < 2 ^ size p (Nat.lt_size_self), 4 * p + 4 ≤ 4 * 2 ^ size p < 8 * 2 ^ size p = 2 ^ (size p + 3), uniformly — no case on p = 0 is needed here, unlike the + 6 sibling below.

Nat.size (4 * p + 6) ≤ Nat.size p + 3. Splits on p = 0, where Nat.size p = 0 and the bound is the direct numeral fact 6 < 8; otherwise size p ≥ 1 gives 2 ^ size p ≥ 2, and 4 * p + 6 ≤ 4 * 2 ^ size p + 2 < 8 * 2 ^ size p = 2 ^ (size p + 3).