Zulip Chat Archive

Stream: new members

Topic: Proving: z.natAbs = z.toNat for non negative z.


Kajani Kaunda (Oct 08 2025 at 12:07):

How do I resolve the following? Any help is much appreciated.

lemma natAbs_eq_toNat_of_nonneg {z : } (hz : 0  z) :  z.natAbs = z.toNat := by sorry

Aaron Liu (Oct 08 2025 at 12:11):

@loogle Int.natAbs, Int.toNat, |- @Eq Nat _ _

loogle (Oct 08 2025 at 12:11):

:search: Int.toNat_add_toNat_neg_eq_natAbs, Int.emod_natAbs_of_nonneg, and 1 more

Kajani Kaunda (Oct 08 2025 at 12:14):

Aaron Liu said:

loogle Int.natAbs, Int.toNat, |- @Eq Nat _ _

How exactly do I code this in a Lemma? Sorry for my ignorance!

Aaron Liu (Oct 08 2025 at 12:21):

I guess you can use docs#Int.toNat_add_toNat_neg_eq_natAbs along with docs#Int.toNat_of_nonpos

Kajani Kaunda (Oct 08 2025 at 12:26):

Aaron Liu said:

I guess you can use docs#Int.toNat_add_toNat_neg_eq_natAbs along with docs#Int.toNat_of_nonpos

Let me try these out... :)

Kajani Kaunda (Oct 08 2025 at 12:33):

mmm. I cant get anything to compile without error... :hurt:

Kajani Kaunda (Oct 08 2025 at 12:35):

import Mathlib

lemma natAbs_eq_toNat_of_nonneg {z : } (hz : 0  z) : z.natAbs = z.toNat := by sorry

Rider Kirkpatrick (Oct 08 2025 at 12:51):

Something like this?

import Mathlib

lemma natAbs_eq_toNat_of_nonneg {z : } (hz : 0  z) : z.natAbs = z.toNat := by
  rw [ Int.toNat_add_toNat_neg_eq_natAbs, Nat.add_eq_left, Int.toNat_of_nonpos]
  linarith

Kevin Buzzard (Oct 08 2025 at 12:57):

lemma natAbs_eq_toNat_of_nonneg {z : } (hz : 0  z) :  z.natAbs = z.toNat := by
  obtain n, rfl :  (n : ), z = n := Int.eq_ofNat_of_zero_le hz -- found with `exact?`
  simp

Kenny Lau (Oct 08 2025 at 13:00):

import Mathlib

lemma natAbs_eq_toNat_of_nonneg {z : } (hz : 0  z) : z.natAbs = z.toNat := by
  lift z to  using hz
  simp

Kajani Kaunda (Oct 08 2025 at 13:00):

Thank you to you all. And at @Rider Kirkpatrick and @Kevin Buzzard , thank you so very much, both of your codes compiled without error!

Kenny Lau (Oct 08 2025 at 13:00):

import Mathlib

lemma natAbs_eq_toNat_of_nonneg {z : } (hz : 0  z) : z.natAbs = z.toNat := by
  grind

Kenny Lau (Oct 08 2025 at 13:01):

import Mathlib

lemma natAbs_eq_toNat_of_nonneg {z : } (hz : 0  z) : z.natAbs = z.toNat := by
  omega

Kajani Kaunda (Oct 08 2025 at 13:01):

Kenny Lau said:

import Mathlib

lemma natAbs_eq_toNat_of_nonneg {z : } (hz : 0  z) : z.natAbs = z.toNat := by
  grind

@Kenny Lau , thank you too! Your code works too!

Alfredo Moreira-Rosa (Oct 08 2025 at 19:44):

As a reminder, for simple lemma like these, you can find tactics that solve the goal with hint, try?,
and for even simpler ones with exact?

Kajani Kaunda (Oct 08 2025 at 23:58):

ecyrbe said:

As a reminder, for simple lemma like these, you can find tactics that solve the goal with hint, try?,
and for even simpler ones with exact?.

Oh!, thanks for the tip @ecyrbe. Will try that first next time.


Last updated: Dec 20 2025 at 21:32 UTC