Grace/Cumulus/Nat/BitLength.g17

Open in Workbench Download source

Imports

Log

Declarations

Source

1-- Binary length: the least k with n < 2 ^ k2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: András Salamon67module Grace.Cumulus.Nat.BitLength8import Log910-- The lengths at which n fits in binary.11def bitCandidates (n : Nat) : Set := { k ∈ ℕ | n < 2 ^ k }1213theorem bitCandidates.inhabited (n : Nat)14  shows n ∈ bitCandidates(n)15proof16  have bound := pow.two_gt at n17  have nNat : Nat(n) by typecheck18  have nMember := nat.omega.forward with nNat19  have both : n ∈ ℕ ∧ n < 2 ^ n := ⟨nMember, bound⟩20  exact bitCandidates.membership.backward with both2122theorem bitCandidates.naturals (n : Nat)23  shows bitCandidates(n) ⊆ ℕ24proof25  intros k kCandidate26  obtain ⟨kMember, below⟩ := bitCandidates.membership.forward with27    kCandidate28  exact kMember2930-- The number of binary digits of n, with bitLength(0) = 0: the least k31-- with n < 2 ^ k. Since 2 ^ k grows with k, bitLength(n) ≤ k if and only32-- if n < 2 ^ k, for every k.33def bitLength (n : Nat) : Set := leastNatural(bitCandidates(n))3435-- Section: The defining property36theorem bitLength.member (n : Nat)37  shows bitLength(n) ∈ bitCandidates(n)38proof39  have candidate := bitCandidates.inhabited at n40  have inhabited : Nonempty(bitCandidates(n)) := ⟨n, candidate⟩41  have naturals := bitCandidates.naturals at n42  have least := leastNatural.member with *43  unfold bitLength44  exact least4546theorem bitLength.natural (n : Nat)47  shows Nat(bitLength(n))48proof49  have member := bitLength.member at n50  obtain ⟨kMember, below⟩ := bitCandidates.membership.forward with member51  exact nat.omega.backward with kMember5253-- n < 2 ^ bitLength(n).54theorem bitLength.lt_pow (n : Nat)55  shows n < 2 ^ bitLength(n)56proof57  have member := bitLength.member at n58  obtain ⟨kMember, below⟩ := bitCandidates.membership.forward with member59  exact below6061theorem bitLength.least (n k : Nat)62  requires above : n < 2 ^ k63  shows bitLength(n) ≤ k64proof65  have candidate := bitCandidates.inhabited at n66  have inhabited : Nonempty(bitCandidates(n)) := ⟨n, candidate⟩67  have naturals := bitCandidates.naturals at n68  have kNat : Nat(k) by typecheck69  have kMember := nat.omega.forward with kNat70  have both : k ∈ ℕ ∧ n < 2 ^ k := ⟨kMember, above⟩71  have kCandidate := bitCandidates.membership.backward with both72  have bound := leastNatural.least at bitCandidates(n), k with *73  unfold bitLength74  exact bound7576-- The Galois connection with powers of 2: bitLength(n) ≤ k ↔ n < 2 ^ k.77theorem bitLength.le_iff (n k : Nat)78  shows bitLength(n) ≤ k ↔ n < 2 ^ k79proof80  have natural := bitLength.natural at n81  have two : 1 + 1 = 2 by arith82  have one := LessEqual.add_one at 183  rw two at one84  constructor85  . intro below86    have above := bitLength.lt_pow at n87    have grows := pow.exponent_monotone at 2, bitLength(n), k with one, below88    have lengthPow := pow.closed at 2, bitLength(n)89    have kPow := pow.closed at 2, k90    exact Less.transitive_LessEqual at n, 2 ^ bitLength(n), 2 ^ k with91      above, grows92  . intro above93    exact bitLength.least at n, k with above9495-- k < bitLength(n) ↔ 2 ^ k ≤ n.96theorem bitLength.lt_iff (n k : Nat)97  shows k < bitLength(n) ↔ 2 ^ k ≤ n98proof99  have natural := bitLength.natural at n100  have kPow := pow.closed at 2, k101  have adjunction := bitLength.le_iff at n, k102  constructor103  . intro below104    have notAtMost := Less.not_LessEqual at k, bitLength(n) with below105    have notAbove : ¬n < 2 ^ k106      intro above107      have atMost := adjunction.backward with above108      exact notAtMost with atMost109    exact (LessEqual.iff_not_Less at 2 ^ k, n).backward with notAbove110  . intro atLeast111    have notAtMost : ¬bitLength(n) ≤ k112      intro atMost113      have above := adjunction.forward with atMost114      have notAbove := LessEqual.not_Less at 2 ^ k, n with atLeast115      exact notAbove with above116    exact (Less.iff_not_LessEqual at k, bitLength(n)).backward with notAtMost117118-- Section: Values and bounds119theorem bitLength.zero120  shows bitLength(0) = 0121proof122  have unit := pow.zero at 2123  have positive := Less.zero_one124  rw ← unit at positive125  have atMost := bitLength.least at 0, 0 with positive126  have natural := bitLength.natural at 0127  exact LessEqual.eq_zero at bitLength(0) with atMost128129theorem bitLength.positive (n : Nat)130  requires one : 1 ≤ n131  shows 1 ≤ bitLength(n)132proof133  have natural := bitLength.natural at n134  have unit := pow.zero at 2135  have adjunction := bitLength.lt_iff at n, 0136  rw unit at adjunction137  have positive := adjunction.backward with one138  exact LessEqual.one_of_positive at bitLength(n) with positive139140-- 2 ^ bitLength(n) ≤ 2 · n for 1 ≤ n: the length is exact up to one bit.141theorem bitLength.pow_le (n : Nat)142  requires one : 1 ≤ n143  shows 2 ^ bitLength(n) ≤ 2 * n144proof145  have natural := bitLength.natural at n146  have atLeast := bitLength.positive at n with one147  have positive := (LessEqual.one_iff_positive at bitLength(n)).forward with148    atLeast149  obtain ⟨e, eNat, shape⟩ := Less.exists_add_one at 0, bitLength(n) with150    positive151  have zeroPlus : 0 + e + 1 = e + 1 by arith152  rw zeroPlus at shape153  have below := Less.add_one at e154  rw ← shape at below155  have lower := (bitLength.lt_iff at n, e).forward with below156  have ePow := pow.closed at 2, e157  have doubled := LessEqual.mul_left at 2 ^ e, n, 2 with lower158  have step := pow.add_one at 2, e159  rw [shape, step]160  exact doubled161162theorem bitLength.monotone (n m : Nat)163  requires below : n ≤ m164  shows bitLength(n) ≤ bitLength(m)165proof166  have natural := bitLength.natural at m167  have above := bitLength.lt_pow at m168  have mPow := pow.closed at 2, bitLength(m)169  have nAbove := LessEqual.transitive_Less at n, m, 2 ^ bitLength(m) with170    below, above171  exact bitLength.least at n, bitLength(m) with nAbove172173theorem bitLength.le_self (n : Nat)174  shows bitLength(n) ≤ n175proof176  have bound := pow.two_gt at n177  exact bitLength.least at n, n with bound178179theorem bitLength.pow_two (m : Nat)180  shows bitLength(2 ^ m) = m + 1181proof182  have natural := bitLength.natural at 2 ^ m183  have two : 1 + 1 = 2 by arith184  have oneTwo := Less.add_one at 1185  rw two at oneTwo186  have next := Less.add_one at m187  have grows := pow.exponent_strict at 2, m, m + 1 with oneTwo, next188  have atMost := bitLength.least at 2 ^ m, m + 1 with grows189  have mPow := pow.closed at 2, m190  have self := LessEqual.reflexive at 2 ^ m191  have below := (bitLength.lt_iff at 2 ^ m, m).backward with self192  have atLeast := Less.add_one_le_of_lt at m, bitLength(2 ^ m) with below193  exact LessEqual.antisymmetric at bitLength(2 ^ m), m + 1 with atMost, atLeast194195-- The length of a product is at most the sum of the lengths.196theorem bitLength.mul (x y : Nat)197  shows bitLength(x * y) ≤ bitLength(x) + bitLength(y)198proof199  have xNatural := bitLength.natural at x200  have yNatural := bitLength.natural at y201  have xAbove := bitLength.lt_pow at x202  have yAbove := bitLength.lt_pow at y203  have xPow := pow.closed at 2, bitLength(x)204  have yPow := pow.closed at 2, bitLength(y)205  have both := Less.mul at x, 2 ^ bitLength(x), y, 2 ^ bitLength(y) with206    xAbove, yAbove207  have split := pow.add at 2, bitLength(y), bitLength(x)208  rw ← split at both209  exact bitLength.least at x * y, bitLength(x) + bitLength(y) with both210211-- The length of a power: bitLength(n ^ d) ≤ d · bitLength(n) for 1 ≤ d.212theorem bitLength.pow_bound (n d : Nat)213  requires positiveD : 1 ≤ d214  shows bitLength(n ^ d) ≤ d * bitLength(n)215proof216  have natural := bitLength.natural at n217  have above := bitLength.lt_pow at n218  have positive := (LessEqual.one_iff_positive at d).forward with positiveD219  obtain ⟨e, eNat, shape⟩ := Less.exists_add_one at 0, d with positive220  have zeroPlus : 0 + e + 1 = e + 1 by arith221  rw zeroPlus at shape222  have grows := pow.base_strict at n, 2 ^ bitLength(n), e with above223  rw ← shape at grows224  have regroup := pow.mul_exponent at 2, bitLength(n), d225  rw regroup at grows226  have swap : bitLength(n) * d = d * bitLength(n) by ring227  rw swap at grows228  exact bitLength.least at n ^ d, d * bitLength(n) with grows229230-- Section: The logarithm231-- For 1 ≤ n, bitLength(n) = ⌊log₂ n⌋ + 1.232theorem bitLength.eq_logNat (n : Nat)233  requires one : 1 ≤ n234  shows bitLength(n) = logNat(2, n) + 1235proof236  have natural := bitLength.natural at n237  have base := LessEqual.reflexive at 2238  have logNatural := logNat.natural at 2, n with base, one239  have upper := logNat.lt_pow_succ at 2, n with base, one240  have lower := logNat.pow_le at 2, n with base, one241  have atMost := bitLength.least at n, logNat(2, n) + 1 with upper242  have below := (bitLength.lt_iff at n, logNat(2, n)).backward with lower243  have atLeast := Less.add_one_le_of_lt at logNat(2, n), bitLength(n) with244    below245  exact LessEqual.antisymmetric at bitLength(n), logNat(2, n) + 1 with246    atMost, atLeast247