Zulip Chat Archive

Stream: Is there code for X?

Topic: More powerful version of linarith?


Herman Chau (Jul 11 2024 at 20:47):

Is there a more powerful version of the tactic linarith that incorporates lt_iff_le_and_ne? Here's a minimal example where I can't prove the goal directly with linarith, but can do it once I prove an intermediate inequality using lt_iff_le_and_ne.

import Mathlib.Tactic.Linarith

theorem eq_two_of_one_le_one_ne_lt_three (n:) (h: 1  n) (h': 1  n) (h'': n < 3) : n = 2 := by
  have : 2  n := by exact Nat.lt_iff_le_and_ne.mpr h, h'
  linarith

Eric Wieser (Jul 11 2024 at 20:49):

omega works here

Herman Chau (Jul 11 2024 at 20:50):

Perfect, that's exactly what I'm looking for. Thank you!

Kyle Miller (Jul 11 2024 at 21:28):

There's also a linarith preprocessor you can enable to have it do cases on 1 < n and 1 > n:

theorem eq_two_of_one_le_one_ne_lt_three (n:) (h: 1  n) (h': 1  n) (h'': n < 3) : n = 2 := by
  linarith (config := { splitNe := true })

Herman Chau (Jul 11 2024 at 21:39):

Actually a quick follow up question: Is linarith completely subsumed by omega when trying to prove statements using linear arithmetic on natural numbers?

Kevin Buzzard (Jul 11 2024 at 22:48):

My mental model of those tactics says the answer is yes, but hopefully an expert can chime in. My mental model also says that there might be non-linear inequalities on the naturals which nlinarith can solve but omega cannot, because omega doesn't know what to do with n2n^2.

Herman Chau (Jul 11 2024 at 23:19):

Kyle Miller said:

There's also a linarith preprocessor you can enable to have it do cases on 1 < n and 1 > n:

theorem eq_two_of_one_le_one_ne_lt_three (n:) (h: 1  n) (h': 1  n) (h'': n < 3) : n = 2 := by
  linarith (config := { splitNe := true })

Oh interesting, I didn't know that there were config options for linarith. Thanks!

Kevin Buzzard (Jul 11 2024 at 23:23):

Do you see them when you hover over the tactic in VS Code? If not then it would be interesting to try and get a complete list of them from somewhere and add this to the docstring of the tactic. This bold approach makes mathlib better.

Herman Chau (Jul 11 2024 at 23:29):

Yes, it looks like the docstring does contain the list of arguments to config and what they do, but I had just never read through them.

I have been often treating tactics as black boxes, but I'm learning how useful it can be to peek under the hood a bit :)


Last updated: May 02 2025 at 03:31 UTC