The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V34. The tactics of Mathlib

Mathlib.Tactic.Ring.Compare

20 declarations and 3 deprecated aliases. Mathlib docs · Source

Automation for proving inequalities in commutative (semi)rings

This file provides automation for proving certain kinds of inequalities in commutative semirings: goals of the form A ≤ B and A < B for which the ring-normal forms of A and B differ by a nonnegative (resp. positive) constant.

For example, ⊢ x + 3 + y < y + x + 4 is in scope because the normal forms of the LHS and RHS are, respectively, 3 + (x + y) and 4 + (x + y), which differ by an additive constant.

Main declarations

  • Mathlib.Tactic.Ring.proveLE: prove goals of the form A ≤ B (subject to the scope constraints described)
  • Mathlib.Tactic.Ring.proveLT: prove goals of the form A < B (subject to the scope constraints described)

Implementation notes

The automation is provided in the MetaM monad; that is, these functions are not user-facing. It would not be hard to provide user-facing versions (see the test file), but the scope of this automation is rather specialized and might be confusing to users.

However, this automation serves as the discharger for the linear_combination tactic on inequality goals, so it is available to the user indirectly as the "degenerate" case of that tactic -- that is, by calling linear_combination without arguments.

Rather than having the metaprograms Mathlib.Tactic.Ring.evalLE.lean and Mathlib.Tactic.Ring.evalLT.lean perform all type class inference at the point of use, we record in advance, as abbrevs, a few type class deductions which will certainly be necessary. They add no new information (they can already be proved by inferInstance).

This helps in speeding up the metaprograms in this file substantially -- about a 50% reduction in heartbeat count in representative test cases -- since otherwise a substantial fraction of their runtime is devoted to type class inference.

The lemmas like add_le_add_right in the root namespace are stated under minimal type classes, typically just [AddRightMono α] or similar. Here we restate these lemmas under stronger type class assumptions ([OrderedCommSemiring α] or similar), which helps in speeding up the metaprograms in this file (Mathlib.Tactic.Ring.proveLT.lean and Mathlib.Tactic.Ring.proveLE.lean) substantially -- about a 50% reduction in heartbeat count in representative test cases -- since otherwise a substantial fraction of their runtime is devoted to type class inference.

These metaprograms at least require CommSemiring, LE/LT, and all CovariantClass/ContravariantClass permutations for addition, and in their main use case (in linear_combination) the Preorder type class is also required, so it is rather little loss of generality simply to require OrderedCommSemiring/StrictOrderedCommSemiring.

def Mathlib.Tactic.Ring.ExceptType.ctorIdx E1 #

Mathlib.Tactic.Ring.ExceptType → ℕ

abbrev Mathlib.Tactic.Ring.ExceptType.ctorElimType E1 #

{motive : Mathlib.Tactic.Ring.ExceptType → Sort u} → ℕ → Sort (max 1 u)

abbrev Mathlib.Tactic.Ring.addMonoidWithOneOfCommSemiring E1 #

(α : Type u_1) → [CommSemiring α] → AddMonoidWithOne α

CommSemiring implies AddMonoidWithOne.

abbrev Mathlib.Tactic.Ring.leOfPartialOrder E1 #

(α : Type u_1) → [PartialOrder α] → LE α

PartialOrder implies LE.

abbrev Mathlib.Tactic.Ring.ltOfPartialOrder E1 #

(α : Type u_1) → [PartialOrder α] → LT α

PartialOrder implies LT.

theorem Mathlib.Tactic.Ring.add_le_add_left E1 #

∀ {α : Type u_1} [inst : CommSemiring α] [inst_1 : PartialOrder α] [IsOrderedRing α] {b c : α},
  b ≤ c → ∀ (a : α), b + a ≤ c + a

theorem Mathlib.Tactic.Ring.add_le_of_nonpos_left E1 #

∀ {α : Type u_1} [inst : CommSemiring α] [inst_1 : PartialOrder α] [IsOrderedRing α] (a : α) {b : α}, b ≤ 0 → b + a ≤ a

theorem Mathlib.Tactic.Ring.le_add_of_nonneg_left E1 #

∀ {α : Type u_1} [inst : CommSemiring α] [inst_1 : PartialOrder α] [IsOrderedRing α] (a : α) {b : α}, 0 ≤ b → a ≤ b + a

theorem Mathlib.Tactic.Ring.add_lt_add_left E1 #

∀ {α : Type u_1} [inst : CommSemiring α] [inst_1 : PartialOrder α] [IsStrictOrderedRing α] {b c : α},
  b < c → ∀ (a : α), b + a < c + a

theorem Mathlib.Tactic.Ring.add_lt_of_neg_left E1 #

∀ {α : Type u_1} [inst : CommSemiring α] [inst_1 : PartialOrder α] [IsStrictOrderedRing α] (a : α) {b : α},
  b < 0 → b + a < a

theorem Mathlib.Tactic.Ring.lt_add_of_pos_left E1 #

∀ {α : Type u_1} [inst : CommSemiring α] [inst_1 : PartialOrder α] [IsStrictOrderedRing α] (a : α) {b : α},
  0 < b → a < b + a

inductive Mathlib.Tactic.Ring.ExceptType E1 #

Type

Inductive type carrying the two kinds of errors which can arise in the metaprograms Mathlib.Tactic.Ring.evalLE.lean and Mathlib.Tactic.Ring.evalLT.lean.

ctor Mathlib.Tactic.Ring.ExceptType.tooSmall E0 #

Mathlib.Tactic.Ring.ExceptType

ctor Mathlib.Tactic.Ring.ExceptType.notComparable E0 #

Mathlib.Tactic.Ring.ExceptType

def Mathlib.Tactic.Ring.evalLE E1 #

{v : Lean.Level} →
  {α : Q(Type v)} →
    (ics : Q(CommSemiring «$α»)) →
      (x : Q(PartialOrder «$α»)) →
        Q(IsOrderedRing «$α») →
          {a b : Q(«$α»)} →
            Mathlib.Tactic.Ring.ExSum q(«$ics») a →
              Mathlib.Tactic.Ring.ExSum q(«$ics») b → Lean.MetaM (Except Mathlib.Tactic.Ring.ExceptType Q(«$a» ≤ «$b»))

In a commutative semiring, given Ring.ExSum objects va, vb which differ by a positive (additive) constant, construct a proof of $a < $b, where a (resp. b) is the expression in the semiring to which va (resp. vb) evaluates.

def Mathlib.Tactic.Ring.evalLT E1 #

{v : Lean.Level} →
  {α : Q(Type v)} →
    (ics : Q(CommSemiring «$α»)) →
      (x : Q(PartialOrder «$α»)) →
        Q(IsStrictOrderedRing «$α») →
          {a b : Q(«$α»)} →
            Mathlib.Tactic.Ring.ExSum q(«$ics») a →
              Mathlib.Tactic.Ring.ExSum q(«$ics») b → Lean.MetaM (Except Mathlib.Tactic.Ring.ExceptType Q(«$a» < «$b»))

In a commutative semiring, given Ring.ExSum objects va, vb which differ by a positive (additive) constant, construct a proof of $a < $b, where a (resp. b) is the expression in the semiring to which va (resp. vb) evaluates.

theorem Mathlib.Tactic.Ring.le_congr E1 #

∀ {α : Type u_1} [inst : LE α] {a b c d : α}, a = b → b ≤ c → d = c → a ≤ d

theorem Mathlib.Tactic.Ring.lt_congr E1 #

∀ {α : Type u_1} [inst : LT α] {a b c d : α}, a = b → b < c → d = c → a < d

def Mathlib.Tactic.Ring.proveLE E3 #

Lean.MVarId → Lean.MetaM Unit

Prove goals of the form A ≤ B in an ordered commutative semiring, if the ring-normal forms of A and B differ by a nonnegative (additive) constant.

def Mathlib.Tactic.Ring.proveLT E3 #

Lean.MVarId → Lean.MetaM Unit

Prove goals of the form A < B in an ordered commutative semiring, if the ring-normal forms of A and B differ by a positive (additive) constant.

Deprecated aliases

NameNew nameSince
Mathlib.Tactic.Ring.amwo_of_csMathlib.Tactic.Ring.addMonoidWithOneOfCommSemiring2026-05-27
Mathlib.Tactic.Ring.le_of_poMathlib.Tactic.Ring.leOfPartialOrder2026-05-27
Mathlib.Tactic.Ring.lt_of_poMathlib.Tactic.Ring.ltOfPartialOrder2026-05-27