Zulip Chat Archive

Stream: Is there code for X?

Topic: Integer Divisors?


Sabbir Rahman (May 19 2025 at 15:20):

Is there something akin to Nat.divisors for Int? Generally if I know a dividies a constant, then using Nat.divisors I can easily limit a to cases. But I don't know how to do the same for Ints.

import Mathlib

example (a : ) (h : a  10) : a = 1  a = 2  a = 5  a = 10 := by
  replace h : a  Nat.divisors 10 := Nat.mem_divisors.mpr h, by norm_num
  change a  {1, 2, 5, 10} at h
  simpa using h

example (a : ) (h : a  10) : a = 1  a = 2  a = 5  a = 10  a = -1  a = -2  a = -5  a = -10 := by
  sorry

If there isn't any Int divisors, what's the easy way to do the second example? I know one way is to use interval_cases but was hoping for something more elementary.

Luigi Massacci (May 19 2025 at 15:45):

@loogle Int, "Divisors"

loogle (May 19 2025 at 15:45):

:search: Lean.Grind.no_int_zero_divisors, Lean.Grind.instNoNatZeroDivisorsInt, and 37 more

Luigi Massacci (May 19 2025 at 15:46):

I guess you could use docs#Int.divisorsAntidiag

Luigi Massacci (May 19 2025 at 15:47):

Though whether you should I do not really know

Sabbir Rahman (May 19 2025 at 15:57):

I see that Int.divisorsAntidiag is made from Nat.divisorsAntidiag. Well to me it feels like similarly Int.divisors should've been made from Nat.divisors :/

Sabbir Rahman (May 25 2025 at 15:38):

The more I think about it, I feel like there should be Int.divisors. can I try to implement and create pr to add it? Not sure if there is any existing work for it though.

Yaël Dillies (May 25 2025 at 16:15):

@Paul Lezeau, I am losing track here, but don't we have Int.divisors somewhere?

Arend Mellendijk (May 25 2025 at 17:12):

There's docs#Int.divisorsAntidiag

Arend Mellendijk (May 25 2025 at 17:13):

Oops, I see Luigi already suggested that

Niklas Halonen (May 25 2025 at 17:17):

I happened to stumble on a similar question yesterday. This solution from formalising-mathematics-2024 might be useful

The key theorem used there is

theorem int_dvd_iff (x : ) (n : ) (hn : n  0) : x  n  x.natAbs  n.natAbs.divisors := by
  simp [hn]

In my opinion this suffices and there is no need for Int.divisors, but maybe int_dvd_iff should be in mathlib (with a more suitable name like Int.dvd_iff_natAbs_mem_divisors).

Sabbir Rahman (May 25 2025 at 17:38):

The main usecase I see is that one should be able to jump form a ∣ 9797 to a ∈ {1, -1, 97, -97, 101, -101, 9797, -9797} easily. Because this is typically a trivial step in any proof. So int_dvd_iff trick is helpful, but still a bit boilerplate-y

Paul Lezeau (May 25 2025 at 17:43):

Yaël Dillies said:

Paul Lezeau, I am losing track here, but don't we have Int.divisors somewhere?

Hm I think I’ve never seen it before

Sabbir Rahman (May 25 2025 at 17:47):

in fact, even if Int.divisors is not created, following is also good I think

import Mathlib

theorem mythm (a b : ) : a  b  a  b.natAbs.divisors.map (), Nat.cast_injective
     -a  b.natAbs.divisors.map (), Nat.cast_injective := by sorry

example (a : ) : a  123  a = 1  a = 3  a = 41  a = 123 
    -a = 1  -a = 3  -a = 41  -a = 123 := by
  rw [mythm]
  repeat
    conv in Finset.map _ _ =>
      equals ({1, 3, 41, 123} : Finset ) => decide
  simp
  tauto

Eric Wieser (May 25 2025 at 17:47):

I agree that we should define it

Paul Lezeau (May 25 2025 at 18:20):

Do you want to have a go at this @Sabbir Rahman ?

Sabbir Rahman (May 25 2025 at 18:25):

Yes, I'd love to. I assume the same file as Nat.divisors is ok, as it already contains both Nat and Int theorems for divisorsAntidiag

Paul Lezeau (May 25 2025 at 20:48):

Sounds good - feel free to ping me for a review;)

Sabbir Rahman (May 26 2025 at 13:14):

created PR 25209 to add int divisors :raised_hands:

Yongshun Ye (Aug 03 2025 at 09:10):

Hi I came across a case that needs these defs and theorems and thanks for all the work. One simple question on Int.divisorsAntidiag here: why is the name Int.divisorsAntidiag not consistent with Nat.divisorsAntidiagonal?

Kevin Buzzard (Aug 03 2025 at 10:39):

Probably just because mathlib grows organically and things sometimes get merged by people who miss little things. Probably a PR normalising conventions would be welcome!

Yaël Dillies (Aug 03 2025 at 11:10):

The plan here is to rename divisorsAntidiagonal to divisorsAntidiag. I was waiting on #21915 to be done before doing so, but I think it's not going to be ready anytime soon, so feel free to rename docs#Nat.divisorsAntidiagonal now!

Eric Wieser (Aug 03 2025 at 14:07):

What about docs#Finset.antidiagonal ?

Weiyi Wang (Aug 03 2025 at 14:13):

I understand naming is hard, but random abbreviation in mathlib still gets me sometimes

Aaron Liu (Aug 03 2025 at 14:18):

Yaël Dillies said:

The plan here is to rename divisorsAntidiagonal to divisorsAntidiag. I was waiting on #21915 to be done before doing so, but I think it's not going to be ready anytime soon, so feel free to rename docs#Nat.divisorsAntidiagonal now!

Why?

Aaron Liu (Aug 03 2025 at 14:21):

It's already hard enough finding stuff (it's divisors and proper divisors but when they're prime it becomes prime factors)

Aaron Liu (Aug 03 2025 at 14:21):

Do we need to introduce an abbreviation on top of that?

Eric Wieser (Aug 03 2025 at 15:34):

There's maybe an argument we should instead merge it with docs#Finset.HasMulAntidiagonal

Aaron Liu (Aug 03 2025 at 15:40):

Oh did we finally add mul antidiagonals?

Eric Wieser (Aug 03 2025 at 15:54):

We have docs#Finset.mulAntidiagonal but it's different

Aaron Liu (Aug 03 2025 at 15:55):

yet another way to spell everything

Eric Wieser (Aug 03 2025 at 16:09):

Either way, this points to the fact that antidiagonal is pretty established as a full name (for admittedly a bunch of related but different things) and we should match it for the time being

Yaël Dillies (Aug 03 2025 at 16:16):

I've started doing the renames to antidiag...

Eric Wieser (Aug 03 2025 at 16:17):

I would suggest not doing so without a Zulip poll (that is about every antidiagonal in mathlib, after listing all of them)


Last updated: Dec 20 2025 at 21:32 UTC