Zulip Chat Archive

Stream: new members

Topic: Lifting Nat lemmas to Int


Gareth Ma (May 02 2024 at 00:06):

How should I prove the following?

example (a b : ) (h : a  a / b * b + b) :
    ofNat a  ofNat a / ofNat b * ofNat b + ofNat b := by
  sorry

Eric Wieser (May 02 2024 at 00:07):

exact h

Gareth Ma (May 02 2024 at 00:07):

Nope :(

Gareth Ma (May 02 2024 at 00:07):

ofNat is Int.ofNat

Gareth Ma (May 02 2024 at 00:07):

Let me edit

Eric Wieser (May 02 2024 at 00:08):

Gareth Ma said:

ofNat is Int.ofNat

The preferred spelling is Nat.cast

Gareth Ma (May 02 2024 at 00:08):

I got this from cases on an Int :(

Eric Wieser (May 02 2024 at 00:08):

Well that was your first mistake :)

Eric Wieser (May 02 2024 at 00:09):

obtain ⟨n, rfl | rfl⟩ := z.eq_nat_or_neg is usually a better choice

Gareth Ma (May 02 2024 at 00:09):

Lmao. How should I prove the following then? Or at least the first steps I guess

lemma lt_div_mul_add_pos (a b : ) (hb : 0 < b) : a < a / b * b + b := by
  nth_rw 2 [ Nat.div_add_mod' a b, mul_comm _ b]
  rw [Nat.mul_add_div hb (a / b) (a % b), add_mul, add_rotate]
  nth_rw 1 [ Nat.div_add_mod' a b, add_comm]
  apply add_lt_add_right $ Nat.lt_add_left _ $ Nat.mod_lt _ hb

example (a b : ) (hb : 0 < b) : a < a / b * b + b := by
  slim_check

Gareth Ma (May 02 2024 at 00:10):

Ohh okay I will try

Gareth Ma (May 02 2024 at 00:13):

Ahha so I use that to get Nat.cast, from which norm_cast can simplify all of that away

Gareth Ma (May 02 2024 at 00:13):

amazing

Gareth Ma (May 02 2024 at 00:44):

Any hint on closing this?

theorem Int.mul_add_div {m : Int} (hm : m  0) (x y : Int) : (m * x + y) / m = x + y / m := by
  sorry

Gareth Ma (May 02 2024 at 00:45):

It seems the Int part is missing a ton of lemmas from Nat, and I can't prove them even with the entire Mathlib. Or is it my skill issue

Rida Hamadani (May 02 2024 at 01:47):

Assuming you are using the division from Data.Int.Basic, this is not true due to rounding, right?

Gareth Ma (May 02 2024 at 01:48):

Why?

theorem Int.mul_add_div {m : Int} (hm : m  0) (x y : Int) : (m * x + y) / m = x + y / m := by
  slim_check (config:={numInst:=1000,maxSize:=1000}) /- passes -/

Gareth Ma (May 02 2024 at 01:48):

If m stays the same, a -> a / m rounds the same way

Gareth Ma (May 02 2024 at 01:48):

If that's what you mean

Gareth Ma (May 02 2024 at 01:48):

I think

Gareth Ma (May 02 2024 at 02:06):

lemma Int.fdiv_eq_floor {a b : } : Int.fdiv a b = (a : ) / (b : ) := by
  cases' a with a a <;> cases' b with b b
  · cases' a with a <;> simp [fdiv]
    have : 0  ((a : ) + 1) / (b : ) := by apply div_nonneg <;> norm_cast <;> omega
    rw [ ofNat_floor_eq_floor this, show (1 : ) = (1 : ) by rfl,  Nat.cast_add,
      Nat.floor_div_eq_div]
    norm_cast
  · cases' a with a <;> simp [fdiv]
    rw [negSucc_eq, Nat.succ_eq_add_one, neg_add,  sub_eq_add_neg]
    rw [ neg_add, div_neg, sub_eq_iff_eq_add,  floor_add_one]

I am now stuck on this. Int is painful...

Gareth Ma (May 02 2024 at 02:07):

Like fdiv is defined in core Lean (?) but never used again

Rida Hamadani (May 02 2024 at 02:07):

Gareth Ma said:

If m stays the same, a -> a / m rounds the same way

Ah I see, sorry!

Gareth Ma (May 02 2024 at 02:18):

Okay I got mul_add_div down:

theorem Int.mul_add_div {m : Int} (hm : m  0) (x y : Int) : (m * x + y) / m = x + y / m := by
  rw [add_comm, add_mul_ediv_left _ _ hm, add_comm]

Gareth Ma (May 02 2024 at 02:19):

https://github.com/leanprover/std4/commit/9ccf392be03aa7b3b80c9e5b2c5e24272d36ee27#diff-efd89f01e8fa474be03044698b70ca62e27ffaddc9440e5570fe97daaf5cbc70R132 there's no way I can figure this out myself


Last updated: May 02 2025 at 03:31 UTC