-- The floor of a nonnegative real as the least natural n with X < n + 1, -- its characterizing bounds, monotonicity, and the fractional part in -- [0, 1). -- Part of Cumulus, the Grace17 mathematics library. -- Copyright (c) 2026 Grant Passmore. -- Released under the Apache License 2.0. See LICENSE in the repository root. -- Authors: Grant Passmore module Grace.Cumulus.Real.Floor import Grace.Cumulus.Real.HarmonicSeries import Grace.Cumulus.Real.RootTwo import Grace.Cumulus.Nat.Least -- The floor as a least natural -- The naturals n with X < n + 1; the floor is the least of them. def floorBounds (X : Real) : Set := { n ∈ ℕ | X < (realNat(n):R) + 1 } -- Existence: leastNatural(floorBounds(X)), nonempty by Real.below_natural; -- uniqueness by realNat.add_one_le. defunique realFloor (X : Real) (n ∈ ℕ) := (realNat(n):R) ≤ X ∧ X < (realNat(n):R) + 1 requires nonnegative : (0:R) ≤ X proof obtain ⟨m, mNat, above⟩ := Real.below_natural at X have mBound : m ∈ floorBounds(X) apply floorBounds.membership.backward refine ⟨nat.omega.forward with mNat, ?_⟩ linarith have inhabited : Nonempty(floorBounds(X)) := ⟨m, mBound⟩ have naturals : floorBounds(X) ⊆ ℕ by simp have member := leastNatural.member with inhabited, naturals have parts := floorBounds.membership.forward with member have leastNat := nat.omega.backward with parts.left witness leastNatural(floorBounds(X)) refine ⟨parts.left, ?_, parts.right⟩ -- The lower bound: the predecessor of the least element is not a bound. have shape := natural.zero_or_successor at leastNatural(floorBounds(X)) with leastNat cases shape with zero | successor . rw [zero, realNat.zero] exact nonnegative . obtain ⟨k, kNat, equation⟩ := successor have notBelow : ¬(X < (realNat(k):R) + 1) intro below have kBound : k ∈ floorBounds(X) apply floorBounds.membership.backward exact ⟨nat.omega.forward with kNat, below⟩ have least := leastNatural.least with inhabited, naturals, kBound rw equation at least arith have lower := realLe.of_not_less with notBelow rw [equation, realNat.succ_add] exact lower uniqueness intros n nMember nBounds m mMember mBounds rcases nBounds with ⟨nLower, nUpper⟩ rcases mBounds with ⟨mLower, mUpper⟩ have shape := Less.trichotomy at n, m with * rcases shape with less | same | greater . exfalso have gap := realNat.add_one_le with less linarith . exact same . exfalso have gap := realNat.add_one_le with greater linarith -- The characterizing bounds theorem realFloor.natural (X : Real) requires nonnegative : (0:R) ≤ X shows Nat(realFloor(X)) proof have selected := realFloor.selected with nonnegative exact nat.omega.backward with selected.left theorem realFloor.le (X : Real) requires nonnegative : (0:R) ≤ X shows (realNat(realFloor(X)):R) ≤ X proof obtain ⟨_, lower, _⟩ := realFloor.selected with nonnegative exact lower theorem realFloor.lt (X : Real) requires nonnegative : (0:R) ≤ X shows X < (realNat(realFloor(X)):R) + 1 proof obtain ⟨_, _, upper⟩ := realFloor.selected with nonnegative exact upper theorem realFloor.eq_of_bounds (X : Real) (n : Nat) requires nonnegative : (0:R) ≤ X requires lower : (realNat(n):R) ≤ X requires upper : X < (realNat(n):R) + 1 shows realFloor(X) = n proof obtain ⟨member, floorLower, floorUpper⟩ := realFloor.selected with nonnegative have nMember := nat.omega.forward at n with * have unique := realFloor.unique with nonnegative have step := unique with member, ⟨floorLower, floorUpper⟩ exact step with nMember, ⟨lower, upper⟩ theorem realFloor.of_natural (n : Nat) shows realFloor(realNat(n)) = n proof have nonnegative := realNat.zero_le at n have lower := realLe.reflexive at realNat(n) with * have upper : (realNat(n):R) < (realNat(n):R) + 1 by linarith exact realFloor.eq_of_bounds with nonnegative, lower, upper -- Monotonicity and comparison with naturals theorem realFloor.monotone (X Y : Real) requires nonnegative : (0:R) ≤ X requires le : X ≤ Y shows realFloor(X) ≤ realFloor(Y) proof have nonnegativeY : (0:R) ≤ Y by linarith have xLower := realFloor.le with nonnegative have yUpper := realFloor.lt with nonnegativeY by_contra notLe have greater := Less.of_not_LessEqual with notLe have gap := realNat.add_one_le with greater linarith theorem realFloor.lt_natural (X : Real) (N : Nat) requires nonnegative : (0:R) ≤ X requires below : X < realNat(N) shows realFloor(X) < N proof have lower := realFloor.le with nonnegative by_contra notLess have le := LessEqual.of_not_Less with notLess have mono := realNat.le_of_natural_le with le linarith theorem realFloor.natural_le (X : Real) (N : Nat) requires nonnegative : (0:R) ≤ X requires below : realNat(N) ≤ X shows N ≤ realFloor(X) proof have upper := realFloor.lt with nonnegative by_contra notLe have less := Less.of_not_LessEqual with notLe have gap := realNat.add_one_le with less linarith -- The fractional part -- Below zero no natural qualifies, so the selected value is the union of -- the empty candidate set, which is 0: the floor is a natural for every -- real, and the fractional part is defined everywhere. theorem realFloor.natural_total (X : Real) shows Nat(realFloor(X)) proof by_cases nonnegative : (0:R) ≤ X . exact realFloor.natural with nonnegative . have negative := realLess.of_not_le with nonnegative have empty : realFloor.candidates(X) = ∅ ext n constructor . intro member obtain ⟨nMember, lower, upper⟩ := member have nonneg := realNat.zero_le at n with * exfalso linarith . intro member exfalso exact set.empty with member unfold realFloor rw [empty, union.empty, ← nat.zero] exact Naturals.constructor.zero def realFrac (X : Real) : Real := X - realNat(realFloor(X)) proof have natural := realFloor.natural_total at X unfold realFrac typecheck theorem realFrac.value (X : Real) shows realFrac(X) = X - realNat(realFloor(X)) proof unfold realFrac rfl theorem realFrac.nonnegative (X : Real) requires nonnegative : (0:R) ≤ X shows (0:R) ≤ realFrac(X) proof have lower := realFloor.le with nonnegative rw realFrac.value linarith theorem realFrac.lt_one (X : Real) requires nonnegative : (0:R) ≤ X shows realFrac(X) < (1:R) proof have upper := realFloor.lt with nonnegative rw realFrac.value linarith -- The floor and fractional part recover the number. theorem realFrac.decomposition (X : Real) requires nonnegative : (0:R) ≤ X shows X = (realNat(realFloor(X)):R) + realFrac(X) proof rw realFrac.value ring