Zulip Chat Archive
Stream: Is there code for X?
Topic: bdiv and bmod
Mitchell Lee (Dec 12 2024 at 06:44):
The definition of docs#Int.bdiv has this docstring:
Balanced division. This returns the unique integer so that
b * (Int.bdiv a b) + Int.bmod a b = a
.
However, I can't see where the equation b * (Int.bdiv a b) + Int.bmod a b = a
is actually proved. Is there something I am missing?
Ruben Van de Velde (Dec 12 2024 at 06:54):
@loogle Int.bdiv, Int.bmod
loogle (Dec 12 2024 at 06:54):
:shrug: nothing found
Ruben Van de Velde (Dec 12 2024 at 06:54):
Apparently not
Mitchell Lee (Dec 12 2024 at 07:10):
Here's a low-powered proof (only requiring Mathlib.Init
). I'm sure it can be improved.
import Mathlib.Init
namespace Int
theorem bdiv_add_bmod (a : Int) (b : Nat) : b * (a.bdiv b) + a.bmod b = a := by
unfold bdiv bmod
split
· simp_all only [Nat.cast_ofNat_Int, Int.mul_zero, emod_zero, Int.zero_add, Int.sub_zero,
ite_self]
· dsimp only
split
· exact ediv_add_emod a b
· rw [Int.mul_add, Int.mul_one, Int.add_assoc, Int.add_comm b, Int.sub_add_cancel]
exact ediv_add_emod a b
theorem bmod_eq (a : Int) (b : Nat) : a.bmod b = a - b * (a.bdiv b) := by
rw (occs := .pos [2]) [← bdiv_add_bmod a b]
rw [Int.add_comm, Int.add_sub_cancel]
Where would be the best place for a theorem like this to go? One possibility is Mathlib.Data.Int.DivMod
(https://leanprover-community.github.io/mathlib4_docs/Mathlib/Data/Int/DivMod.html) but I am slightly concerned about putting an inessential theorem so early in the import hierarchy.
Yury G. Kudryashov (Dec 12 2024 at 07:16):
@Kim Morrison Should it go to Mathlib or Lean?
Yury G. Kudryashov (Dec 12 2024 at 07:17):
Looks like a basic fact about a definition from core which is never used in Mathlib.
Ruben Van de Velde (Dec 12 2024 at 07:25):
I'm guessing mathlib doesn't care much, so put it in batteries for Kim to upstream into core at their leisure
Kim Morrison (Dec 12 2024 at 08:05):
Yes, either PR to Batteries or Lean. I can collect it from Batteries when I next return to Int/Nat.
Mario Carneiro (Dec 12 2024 at 11:22):
yes, this should go in Data.Int.DivMod
. I think bdiv
did not exist when I wrote most of the theorems in that file (which is now in Init
)
Mitchell Lee (Jan 01 2025 at 20:47):
https://github.com/leanprover/lean4/pull/6494
Last updated: May 02 2025 at 03:31 UTC