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)14shows n ∈ bitCandidates(n)15proof16have bound := pow.two_gt at n17have nNat : Nat(n) bytypecheck18have nMember := nat.omega.forward with nNat19have both : n ∈ ℕ ∧ n < 2 ^ n :=⟨nMember, bound⟩20exact bitCandidates.membership.backward with both2122theorem bitCandidates.naturals (n : Nat)23shows bitCandidates(n) ⊆ ℕ24proof25intros k kCandidate26obtain⟨kMember, below⟩:= bitCandidates.membership.forward with27 kCandidate28exact 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)37shows bitLength(n) ∈ bitCandidates(n)38proof39have candidate := bitCandidates.inhabited at n40have inhabited : Nonempty(bitCandidates(n)) :=⟨n, candidate⟩41have naturals := bitCandidates.naturals at n42have least := leastNatural.member with*43unfold bitLength44exact least4546theorem bitLength.natural (n : Nat)47showsNat(bitLength(n))48proof49have member := bitLength.member at n50obtain⟨kMember, below⟩:= bitCandidates.membership.forward with member51exact nat.omega.backward with kMember5253-- n < 2 ^ bitLength(n).54theorem bitLength.lt_pow (n : Nat)55shows n < 2 ^ bitLength(n)56proof57have member := bitLength.member at n58obtain⟨kMember, below⟩:= bitCandidates.membership.forward with member59exact below6061theorem bitLength.least (n k : Nat)62requires above : n < 2 ^ k63shows bitLength(n) ≤ k64proof65have candidate := bitCandidates.inhabited at n66have inhabited : Nonempty(bitCandidates(n)) :=⟨n, candidate⟩67have naturals := bitCandidates.naturals at n68have kNat : Nat(k) bytypecheck69have kMember := nat.omega.forward with kNat70have both : k ∈ ℕ ∧ n < 2 ^ k :=⟨kMember, above⟩71have kCandidate := bitCandidates.membership.backward with both72have bound := leastNatural.least at bitCandidates(n), k with*73unfold bitLength74exact bound7576-- The Galois connection with powers of 2: bitLength(n) ≤ k ↔ n < 2 ^ k.77theorem bitLength.le_iff (n k : Nat)78shows bitLength(n) ≤ k ↔ n < 2 ^ k79proof80have natural := bitLength.natural at n81have two : 1+1=2byarith82have one := LessEqual.add_one at183rw two at one84constructor85 . intro below86have above := bitLength.lt_pow at n87have grows := pow.exponent_monotone at2, bitLength(n), k with one, below88have lengthPow := pow.closed at2, bitLength(n)89have kPow := pow.closed at2, k90exact Less.transitive_LessEqual at n, 2 ^ bitLength(n), 2 ^ k with91 above, grows92 . intro above93exact bitLength.least at n, k with above9495-- k < bitLength(n) ↔ 2 ^ k ≤ n.96theorem bitLength.lt_iff (n k : Nat)97shows k < bitLength(n) ↔2 ^ k ≤ n98proof99have natural := bitLength.natural at n100have kPow := pow.closed at2, k101have adjunction := bitLength.le_iff at n, k102constructor103 . intro below104have notAtMost := Less.not_LessEqual at k, bitLength(n) with below105have notAbove : ¬n < 2 ^ k106intro above107have atMost := adjunction.backward with above108exact notAtMost with atMost109exact (LessEqual.iff_not_Less at2 ^ k, n).backward with notAbove110 . intro atLeast111have notAtMost : ¬bitLength(n) ≤ k112intro atMost113have above := adjunction.forward with atMost114have notAbove := LessEqual.not_Less at2 ^ k, n with atLeast115exact notAbove with above116exact (Less.iff_not_LessEqual at k, bitLength(n)).backward with notAtMost117118-- Section: Values and bounds119theorem bitLength.zero120shows bitLength(0) =0121proof122have unit := pow.zero at2123have positive := Less.zero_one124rw← unit at positive125have atMost := bitLength.least at0, 0with positive126have natural := bitLength.natural at0127exact LessEqual.eq_zero at bitLength(0) with atMost128129theorem bitLength.positive (n : Nat)130requires one : 1≤ n131shows1≤ bitLength(n)132proof133have natural := bitLength.natural at n134have unit := pow.zero at2135have adjunction := bitLength.lt_iff at n, 0136rw unit at adjunction137have positive := adjunction.backward with one138exact 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)142requires one : 1≤ n143shows2 ^ bitLength(n) ≤2* n144proof145have natural := bitLength.natural at n146have atLeast := bitLength.positive at n with one147have positive := (LessEqual.one_iff_positive at bitLength(n)).forward with148 atLeast149obtain⟨e, eNat, shape⟩:= Less.exists_add_one at0, bitLength(n) with150 positive151have zeroPlus : 0+ e +1= e +1byarith152rw zeroPlus at shape153have below := Less.add_one at e154rw← shape at below155have lower := (bitLength.lt_iff at n, e).forward with below156have ePow := pow.closed at2, e157have doubled := LessEqual.mul_left at2 ^ e, n, 2with lower158have step := pow.add_one at2, e159rw [shape, step]160exact doubled161162theorem bitLength.monotone (n m : Nat)163requires below : n ≤ m164shows bitLength(n) ≤ bitLength(m)165proof166have natural := bitLength.natural at m167have above := bitLength.lt_pow at m168have mPow := pow.closed at2, bitLength(m)169have nAbove := LessEqual.transitive_Less at n, m, 2 ^ bitLength(m) with170 below, above171exact bitLength.least at n, bitLength(m) with nAbove172173theorem bitLength.le_self (n : Nat)174shows bitLength(n) ≤ n175proof176have bound := pow.two_gt at n177exact bitLength.least at n, n with bound178179theorem bitLength.pow_two (m : Nat)180shows bitLength(2 ^ m) = m +1181proof182have natural := bitLength.natural at2 ^ m183have two : 1+1=2byarith184have oneTwo := Less.add_one at1185rw two at oneTwo186have next := Less.add_one at m187have grows := pow.exponent_strict at2, m, m +1with oneTwo, next188have atMost := bitLength.least at2 ^ m, m +1with grows189have mPow := pow.closed at2, m190haveself:= LessEqual.reflexive at2 ^ m191have below := (bitLength.lt_iff at2 ^ m, m).backward withself192have atLeast := Less.add_one_le_of_lt at m, bitLength(2 ^ m) with below193exact LessEqual.antisymmetric at bitLength(2 ^ m), m +1with atMost, atLeast194195-- The length of a product is at most the sum of the lengths.196theorem bitLength.mul (x y : Nat)197shows bitLength(x * y) ≤ bitLength(x) + bitLength(y)198proof199have xNatural := bitLength.natural at x200have yNatural := bitLength.natural at y201have xAbove := bitLength.lt_pow at x202have yAbove := bitLength.lt_pow at y203have xPow := pow.closed at2, bitLength(x)204have yPow := pow.closed at2, bitLength(y)205have both := Less.mul at x, 2 ^ bitLength(x), y, 2 ^ bitLength(y) with206 xAbove, yAbove207havesplit:= pow.add at2, bitLength(y), bitLength(x)208rw←splitat both209exact 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)213requires positiveD : 1≤ d214shows bitLength(n ^ d) ≤ d * bitLength(n)215proof216have natural := bitLength.natural at n217have above := bitLength.lt_pow at n218have positive := (LessEqual.one_iff_positive at d).forward with positiveD219obtain⟨e, eNat, shape⟩:= Less.exists_add_one at0, d with positive220have zeroPlus : 0+ e +1= e +1byarith221rw zeroPlus at shape222have grows := pow.base_strict at n, 2 ^ bitLength(n), e with above223rw← shape at grows224have regroup := pow.mul_exponent at2, bitLength(n), d225rw regroup at grows226have swap : bitLength(n) * d = d * bitLength(n) byring227rw swap at grows228exact 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)233requires one : 1≤ n234shows bitLength(n) = logNat(2, n) +1235proof236have natural := bitLength.natural at n237have base := LessEqual.reflexive at2238have logNatural := logNat.natural at2, n with base, one239have upper := logNat.lt_pow_succ at2, n with base, one240have lower := logNat.pow_le at2, n with base, one241have atMost := bitLength.least at n, logNat(2, n) +1with upper242have below := (bitLength.lt_iff at n, logNat(2, n)).backward with lower243have atLeast := Less.add_one_le_of_lt at logNat(2, n), bitLength(n) with244 below245exact LessEqual.antisymmetric at bitLength(n), logNat(2, n) +1with246 atMost, atLeast247