Grace/Cumulus/Nat/Log.g17

Open in Workbench Download source

Imports

Least · PowLemmas

Declarations

Source

1-- Integer logarithms: the exponent k with b ^ k ≤ n < b ^ (k + 1)2-- 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.Log8import Least9import PowLemmas1011-- The exponents whose next power exceeds n.12def logCandidates (b n : Nat) : Set := { k ∈ ℕ | n < b ^ (k + 1) }1314theorem logCandidates.inhabited (b n : Nat)15  requires base : 2 ≤ b16  shows n ∈ logCandidates(b, n)17proof18  have twoBelow := pow.two_gt at n + 119  have baseBelow := pow.base_monotone at 2, b, n + 1 with base20  have nNat : Nat(n) by typecheck21  have nMember := nat.omega.forward with nNat22  have bound : n < b ^ (n + 1) by arith23  simp [logCandidates, nMember, bound]2425-- The least candidate is the integer logarithm. Outside the stated26-- hypotheses no exponent qualifies and the value is 0.27defunique logNat (b n : Nat) (k ∈ ℕ) :=28  b ^ k ≤ n ∧ n < b ^ (k + 1)29requires base : 2 ≤ b30requires positive : 1 ≤ n31proof32  have candidate := logCandidates.inhabited at b, n with base33  have inhabited : Nonempty(logCandidates(b, n)) := ⟨n, candidate⟩34  have naturals : logCandidates(b, n) ⊆ ℕ by simp35  have least := leastNatural.member with *36  obtain ⟨leastMember, above⟩ := logCandidates.membership.forward with least37  have leastNat := nat.omega.backward with leastMember38  have shape := natural.zero_or_successor with leastNat39  witness leastNatural(logCandidates(b, n))40  refine ⟨leastMember, _, above⟩41  cases shape with zero | successor42  . rw zero43    simp [pow]44  . obtain ⟨j, jNat, successorEquation⟩ := successor45    have plusOne := add.right_one at j46    rw ← plusOne at successorEquation47    have equation : leastNatural(logCandidates(b, n)) = j + 148      exact successorEquation49    rw equation50    have split := Less.or_LessEqual at n, b ^ (j + 1)51    cases split with jAbove | below52    . have jMember := nat.omega.forward with jNat53      have jCandidate : j ∈ logCandidates(b, n)54        simp [logCandidates.membership, jMember, jAbove]55      have bound := leastNatural.least at logCandidates(b, n), j with *56      rw equation at bound57      exfalso58      arith59    . exact below60uniqueness61  intros k kMember kBounds m mMember mBounds62  have kNat := nat.omega.backward with kMember63  have mNat := nat.omega.backward with mMember64  rcases kBounds with ⟨kLower, kUpper⟩65  rcases mBounds with ⟨mLower, mUpper⟩66  have one : 1 ≤ b by arith67  have kAtMost : k ≤ m68    have split := Less.or_LessEqual at m, k69    cases split with above | atMost70    . have step : m + 1 ≤ k by arith71      have grows := pow.exponent_monotone at b, m + 1, k with one, step72      exfalso73      arith74    . exact atMost75  have mAtMost : m ≤ k76    have split := Less.or_LessEqual at k, m77    cases split with above | atMost78    . have step : k + 1 ≤ m by arith79      have grows := pow.exponent_monotone at b, k + 1, m with one, step80      exfalso81      arith82    . exact atMost83  arith8485theorem logNat.natural (b n : Nat)86  requires base : 2 ≤ b87  requires positive : 1 ≤ n88  shows Nat(logNat(b, n))89proof90  obtain ⟨member, bounds⟩ := logNat.selected at b, n with *91  exact nat.omega.backward with member9293theorem logNat.pow_le (b n : Nat)94  requires base : 2 ≤ b95  requires positive : 1 ≤ n96  shows b ^ logNat(b, n) ≤ n97proof98  obtain ⟨member, lower, upper⟩ := logNat.selected at b, n with *99  exact lower100101theorem logNat.lt_pow_succ (b n : Nat)102  requires base : 2 ≤ b103  requires positive : 1 ≤ n104  shows n < b ^ (logNat(b, n) + 1)105proof106  obtain ⟨member, lower, upper⟩ := logNat.selected at b, n with *107  exact upper108109-- The bounds characterize the logarithm.110theorem logNat.eq_of_bounds (b n k : Nat)111  requires base : 2 ≤ b112  requires lower : b ^ k ≤ n113  requires upper : n < b ^ (k + 1)114  shows logNat(b, n) = k115proof116  have one : 1 ≤ b by arith117  have powerPositive := pow.ge_one at b, k with one118  have positive : 1 ≤ n by arith119  obtain ⟨member, logLower, logUpper⟩ := logNat.selected at b, n with *120  have kNat : Nat(k) by typecheck121  have kMember := nat.omega.forward with kNat122  have unique := logNat.unique at b, n with base, positive123  have logBounds : b ^ logNat(b, n) ≤ n ∧ n < b ^ (logNat(b, n) + 1)124    exact ⟨logLower, logUpper⟩125  have kBounds : b ^ k ≤ n ∧ n < b ^ (k + 1) := ⟨lower, upper⟩126  exact unique with logNat(b, n), member, logBounds, k, kMember, kBounds127128-- a ≤ ⌊log_b n⌋ exactly when b ^ a ≤ n.129theorem logNat.le_iff (b n a : Nat)130  requires base : 2 ≤ b131  requires positive : 1 ≤ n132  shows a ≤ logNat(b, n) ↔ b ^ a ≤ n133proof134  have natural := logNat.natural with *135  have lower := logNat.pow_le with *136  have upper := logNat.lt_pow_succ with *137  have one : 1 ≤ b by arith138  constructor139  . intro below140    have grows := pow.exponent_monotone at b, a, logNat(b, n) with one, below141    arith142  . intro bound143    have split := Less.or_LessEqual at logNat(b, n), a144    cases split with above | atMost145    . have step : logNat(b, n) + 1 ≤ a by arith146      have grows := pow.exponent_monotone at b, logNat(b, n) + 1, a with147        one, step148      exfalso149      arith150    . exact atMost151152-- ⌊log_b n⌋ < a exactly when n < b ^ a.153theorem logNat.lt_iff (b n a : Nat)154  requires base : 2 ≤ b155  requires positive : 1 ≤ n156  shows logNat(b, n) < a ↔ n < b ^ a157proof158  have natural := logNat.natural with *159  have adjunction := logNat.le_iff at b, n, a with *160  have left := Less.iff_not_LessEqual at logNat(b, n), a161  have right := Less.iff_not_LessEqual at n, b ^ a162  rw [left, right, adjunction]163164theorem logNat.pow (b k : Nat)165  requires base : 2 ≤ b166  shows logNat(b, b ^ k) = k167proof168  have above : 1 < b by arith169  have kNat : Nat(k) by typecheck170  have lessSucc : k < k + 1 by arith171  have upper := pow.exponent_strict at b, k, k + 1 with above, lessSucc172  have lower : b ^ k ≤ b ^ k by arith173  exact logNat.eq_of_bounds with base, lower, upper174175theorem logNat.monotone (b n m : Nat)176  requires base : 2 ≤ b177  requires positive : 1 ≤ n178  requires below : n ≤ m179  shows logNat(b, n) ≤ logNat(b, m)180proof181  have mPositive : 1 ≤ m by arith182  have natural := logNat.natural at b, n with base, positive183  have lower := logNat.pow_le at b, n with base, positive184  have bound : b ^ logNat(b, n) ≤ m by arith185  have adjunction := logNat.le_iff at b, m, logNat(b, n) with base, mPositive186  exact adjunction.backward with bound187188theorem logNat.lt_self (b n : Nat)189  requires base : 2 ≤ b190  requires positive : 1 ≤ n191  shows logNat(b, n) < n192proof193  have natural := logNat.natural with *194  have lower := logNat.pow_le with *195  have twoAbove := pow.two_gt at logNat(b, n)196  have baseAbove := pow.base_monotone at 2, b, logNat(b, n) with base197  arith198