Grace/Cumulus/Real/Floor.g17

Open in Workbench Download source

Imports

Grace.Cumulus.Real.HarmonicSeries · Grace.Cumulus.Real.RootTwo · Grace.Cumulus.Nat.Least

Declarations

Source

1-- The floor of a nonnegative real as the least natural n with X < n + 1,2-- its characterizing bounds, monotonicity, and the fractional part in3-- [0, 1).4-- Part of Cumulus, the Grace17 mathematics library.5-- Copyright (c) 2026 Grant Passmore.6-- Released under the Apache License 2.0. See LICENSE in the repository root.7-- Authors: Grant Passmore89module Grace.Cumulus.Real.Floor10import Grace.Cumulus.Real.HarmonicSeries11import Grace.Cumulus.Real.RootTwo12import Grace.Cumulus.Nat.Least1314-- The floor as a least natural1516-- The naturals n with X < n + 1; the floor is the least of them.17def floorBounds (X : Real) : Set := { n ∈ ℕ | X < (realNat(n):R) + 1 }1819-- Existence: leastNatural(floorBounds(X)), nonempty by Real.below_natural;20-- uniqueness by realNat.add_one_le.21defunique realFloor (X : Real) (n ∈ ℕ) :=22  (realNat(n):R) ≤ X ∧ X < (realNat(n):R) + 123requires nonnegative : (0:R) ≤ X24proof25  obtain ⟨m, mNat, above⟩ := Real.below_natural at X26  have mBound : m ∈ floorBounds(X)27    apply floorBounds.membership.backward28    refine ⟨nat.omega.forward with mNat, ?_⟩29    linarith30  have inhabited : Nonempty(floorBounds(X)) := ⟨m, mBound⟩31  have naturals : floorBounds(X) ⊆ ℕ by simp32  have member := leastNatural.member with inhabited, naturals33  have parts := floorBounds.membership.forward with member34  have leastNat := nat.omega.backward with parts.left35  witness leastNatural(floorBounds(X))36  refine ⟨parts.left, ?_, parts.right⟩37  -- The lower bound: the predecessor of the least element is not a bound.38  have shape := natural.zero_or_successor at leastNatural(floorBounds(X))39    with leastNat40  cases shape with zero | successor41  . rw [zero, realNat.zero]42    exact nonnegative43  . obtain ⟨k, kNat, equation⟩ := successor44    have notBelow : ¬(X < (realNat(k):R) + 1)45      intro below46      have kBound : k ∈ floorBounds(X)47        apply floorBounds.membership.backward48        exact ⟨nat.omega.forward with kNat, below⟩49      have least := leastNatural.least with inhabited, naturals, kBound50      rw equation at least51      arith52    have lower := realLe.of_not_less with notBelow53    rw [equation, realNat.succ_add]54    exact lower55uniqueness56  intros n nMember nBounds m mMember mBounds57  rcases nBounds with ⟨nLower, nUpper⟩58  rcases mBounds with ⟨mLower, mUpper⟩59  have shape := Less.trichotomy at n, m with *60  rcases shape with less | same | greater61  . exfalso62    have gap := realNat.add_one_le with less63    linarith64  . exact same65  . exfalso66    have gap := realNat.add_one_le with greater67    linarith6869-- The characterizing bounds7071theorem realFloor.natural (X : Real)72  requires nonnegative : (0:R) ≤ X73  shows Nat(realFloor(X))74proof75  have selected := realFloor.selected with nonnegative76  exact nat.omega.backward with selected.left7778theorem realFloor.le (X : Real)79  requires nonnegative : (0:R) ≤ X80  shows (realNat(realFloor(X)):R) ≤ X81proof82  obtain ⟨_, lower, _⟩ := realFloor.selected with nonnegative83  exact lower8485theorem realFloor.lt (X : Real)86  requires nonnegative : (0:R) ≤ X87  shows X < (realNat(realFloor(X)):R) + 188proof89  obtain ⟨_, _, upper⟩ := realFloor.selected with nonnegative90  exact upper9192theorem realFloor.eq_of_bounds (X : Real) (n : Nat)93  requires nonnegative : (0:R) ≤ X94  requires lower : (realNat(n):R) ≤ X95  requires upper : X < (realNat(n):R) + 196  shows realFloor(X) = n97proof98  obtain ⟨member, floorLower, floorUpper⟩ :=99    realFloor.selected with nonnegative100  have nMember := nat.omega.forward at n with *101  have unique := realFloor.unique with nonnegative102  have step := unique with member, ⟨floorLower, floorUpper⟩103  exact step with nMember, ⟨lower, upper⟩104105theorem realFloor.of_natural (n : Nat)106  shows realFloor(realNat(n)) = n107proof108  have nonnegative := realNat.zero_le at n109  have lower := realLe.reflexive at realNat(n) with *110  have upper : (realNat(n):R) < (realNat(n):R) + 1 by linarith111  exact realFloor.eq_of_bounds with nonnegative, lower, upper112113-- Monotonicity and comparison with naturals114115theorem realFloor.monotone (X Y : Real)116  requires nonnegative : (0:R) ≤ X117  requires le : X ≤ Y118  shows realFloor(X) ≤ realFloor(Y)119proof120  have nonnegativeY : (0:R) ≤ Y by linarith121  have xLower := realFloor.le with nonnegative122  have yUpper := realFloor.lt with nonnegativeY123  by_contra notLe124  have greater := Less.of_not_LessEqual with notLe125  have gap := realNat.add_one_le with greater126  linarith127128theorem realFloor.lt_natural (X : Real) (N : Nat)129  requires nonnegative : (0:R) ≤ X130  requires below : X < realNat(N)131  shows realFloor(X) < N132proof133  have lower := realFloor.le with nonnegative134  by_contra notLess135  have le := LessEqual.of_not_Less with notLess136  have mono := realNat.le_of_natural_le with le137  linarith138139theorem realFloor.natural_le (X : Real) (N : Nat)140  requires nonnegative : (0:R) ≤ X141  requires below : realNat(N) ≤ X142  shows N ≤ realFloor(X)143proof144  have upper := realFloor.lt with nonnegative145  by_contra notLe146  have less := Less.of_not_LessEqual with notLe147  have gap := realNat.add_one_le with less148  linarith149150-- The fractional part151152-- Below zero no natural qualifies, so the selected value is the union of153-- the empty candidate set, which is 0: the floor is a natural for every154-- real, and the fractional part is defined everywhere.155theorem realFloor.natural_total (X : Real)156  shows Nat(realFloor(X))157proof158  by_cases nonnegative : (0:R) ≤ X159  . exact realFloor.natural with nonnegative160  . have negative := realLess.of_not_le with nonnegative161    have empty : realFloor.candidates(X) = ∅162      ext n163      constructor164      . intro member165        obtain ⟨nMember, lower, upper⟩ := member166        have nonneg := realNat.zero_le at n with *167        exfalso168        linarith169      . intro member170        exfalso171        exact set.empty with member172    unfold realFloor173    rw [empty, union.empty, ← nat.zero]174    exact Naturals.constructor.zero175176def realFrac (X : Real) : Real := X - realNat(realFloor(X))177proof178  have natural := realFloor.natural_total at X179  unfold realFrac180  typecheck181182theorem realFrac.value (X : Real)183  shows realFrac(X) = X - realNat(realFloor(X))184proof185  unfold realFrac186  rfl187188theorem realFrac.nonnegative (X : Real)189  requires nonnegative : (0:R) ≤ X190  shows (0:R) ≤ realFrac(X)191proof192  have lower := realFloor.le with nonnegative193  rw realFrac.value194  linarith195196theorem realFrac.lt_one (X : Real)197  requires nonnegative : (0:R) ≤ X198  shows realFrac(X) < (1:R)199proof200  have upper := realFloor.lt with nonnegative201  rw realFrac.value202  linarith203204-- The floor and fractional part recover the number.205theorem realFrac.decomposition (X : Real)206  requires nonnegative : (0:R) ≤ X207  shows X = (realNat(realFloor(X)):R) + realFrac(X)208proof209  rw realFrac.value210  ring211