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.Linarith.Parsing

22 declarations. Mathlib docs · Source

Parsing input expressions into linear form

linarith computes the linear form of its input expressions, assuming (without justification) that the type of these expressions is a commutative semiring. It identifies atoms up to ring-equivalence: that is, (y*3)*x will be identified 3*(x*y), where the monomial x*y is the linear atom.

  • Variables are represented by natural numbers.
  • Monomials are represented by Monom := TreeMap ℕ ℕ. The monomial 1 is represented by the empty map.
  • Linear combinations of monomials are represented by Sum := TreeMap Monom ℤ.

All input expressions are converted to Sums, preserving the map from expressions to variables. We then discard the monomial information, mapping each distinct monomial to a natural number. The resulting TreeMap ℕ ℤ represents the ring-normalized linear form of the expression. This is ultimately converted into a Linexp in the obvious way.

linearFormsAndMaxVar is the main entry point into this file. Everything else is contained.

Parsing datatypes

Parsing algorithms

def List.findDefeq E1 #

{v : Type} → Lean.Meta.TransparencyMode → List (Lean.Expr × v) → Lean.Expr → Lean.MetaM v

findDefeq red m e looks for a key in m that is defeq to e (up to transparency red), and returns the value associated with this key if it exists. Otherwise, it fails.

def instAddTreeMapOfZeroOfDecidableEq_mathlib E1 #

{α : Type u_1} →
  {β : Type u_2} → {c : α → α → Ordering} → [Add β] → [Zero β] → [DecidableEq β] → Add (Std.TreeMap α β c)

We introduce a local instance allowing addition of TreeMaps, removing any keys with value zero. We don't need to prove anything about this addition, as it is only used in meta code.

abbrev Mathlib.Tactic.Linarith.Monom E1 #

Type

Variables (represented by natural numbers) map to their power.

def Mathlib.Tactic.Linarith.Monom.one E1 #

Mathlib.Tactic.Linarith.Monom

1 is represented by the empty monomial, the product of no variables.

def Mathlib.Tactic.Linarith.Monom.lt E1 #

Mathlib.Tactic.Linarith.Monom → Mathlib.Tactic.Linarith.Monom → Bool

Compare monomials by first comparing their keys and then their powers.

instance Mathlib.Tactic.Linarith.instOrdMonom E0 #

Ord Mathlib.Tactic.Linarith.Monom

abbrev Mathlib.Tactic.Linarith.Sum E1 #

Type

Linear combinations of monomials are represented by mapping monomials to coefficients.

def Mathlib.Tactic.Linarith.Sum.one E1 #

Mathlib.Tactic.Linarith.Sum

1 is represented as the singleton sum of the monomial Monom.one with coefficient 1.

def Mathlib.Tactic.Linarith.Sum.scaleByMonom E1 #

Mathlib.Tactic.Linarith.Sum → Mathlib.Tactic.Linarith.Monom → Mathlib.Tactic.Linarith.Sum

Sum.scaleByMonom s m multiplies every monomial in s by m.

def Mathlib.Tactic.Linarith.Sum.mul E1 #

Mathlib.Tactic.Linarith.Sum → Mathlib.Tactic.Linarith.Sum → Mathlib.Tactic.Linarith.Sum

sum.mul s1 s2 distributes the multiplication of two sums.

opaque Mathlib.Tactic.Linarith.Sum.pow E1 #

Mathlib.Tactic.Linarith.Sum → ℕ → Mathlib.Tactic.Linarith.Sum

The nth power of s : Sum is the n-fold product of s, with s.pow 0 = Sum.one.

def Mathlib.Tactic.Linarith.SumOfMonom E1 #

Mathlib.Tactic.Linarith.Monom → Mathlib.Tactic.Linarith.Sum

SumOfMonom m lifts m to a sum with coefficient 1.

def Mathlib.Tactic.Linarith.one E1 #

Mathlib.Tactic.Linarith.Monom

The unit monomial one is represented by the empty TreeMap.

def Mathlib.Tactic.Linarith.scalar E1 #

ℤ → Mathlib.Tactic.Linarith.Sum

A scalar z is represented by a Sum with coefficient z and monomial one

def Mathlib.Tactic.Linarith.var E1 #

ℕ → Mathlib.Tactic.Linarith.Sum

A single variable n is represented by a sum with coefficient 1 and monomial n.

abbrev Mathlib.Tactic.Linarith.ExprMap E1 #

Type

ExprMap is used to record atomic expressions which have been seen while processing inequality expressions.

def Mathlib.Tactic.Linarith.linearFormOfAtom E1 #

Lean.Meta.TransparencyMode →
  Mathlib.Tactic.Linarith.ExprMap →
    Lean.Expr → Lean.MetaM (Mathlib.Tactic.Linarith.ExprMap × Mathlib.Tactic.Linarith.Sum)

linearFormOfAtom red map e is the atomic case for linear_form_of_expr. If e appears with index k in map, it returns the singleton sum var k. Otherwise it updates map, adding e with index n, and returns the singleton sum var n.

opaque Mathlib.Tactic.Linarith.linearFormOfExpr E1 #

Lean.Meta.TransparencyMode →
  Mathlib.Tactic.Linarith.ExprMap →
    Lean.Expr → Lean.MetaM (Mathlib.Tactic.Linarith.ExprMap × Mathlib.Tactic.Linarith.Sum)

linearFormOfExpr red map e computes the linear form of e.

map is a lookup map from atomic expressions to variable numbers. If a new atomic expression is encountered, it is added to the map with a new number. It matches atomic expressions up to reducibility given by red.

Because it matches up to definitional equality, this function must be in the MetaM monad, and forces some functions that call it into MetaM as well.

def Mathlib.Tactic.Linarith.elimMonom E1 #

Mathlib.Tactic.Linarith.Sum →
  Std.TreeMap Mathlib.Tactic.Linarith.Monom ℕ compare →
    Std.TreeMap Mathlib.Tactic.Linarith.Monom ℕ compare × Std.TreeMap ℕ ℤ compare

elimMonom s map eliminates the monomial level of the Sum s.

map is a lookup map from monomials to variable numbers. The output TreeMap ℕ ℤ has the same structure as s : Sum, but each monomial key is replaced with its index according to map. If any new monomials are encountered, they are assigned variable numbers and map is updated.

def Mathlib.Tactic.Linarith.toComp E1 #

Lean.Meta.TransparencyMode →
  Lean.Expr →
    Mathlib.Tactic.Linarith.ExprMap →
      Std.TreeMap Mathlib.Tactic.Linarith.Monom ℕ compare →
        Lean.MetaM
          (Mathlib.Tactic.Linarith.Comp ×
            Mathlib.Tactic.Linarith.ExprMap × Std.TreeMap Mathlib.Tactic.Linarith.Monom ℕ compare)

toComp red e e_map monom_map converts an expression of the form t < 0, t ≤ 0, or t = 0 into a comp object.

e_map maps atomic expressions to indices; monom_map maps monomials to indices. Both of these are updated during processing and returned.

def Mathlib.Tactic.Linarith.toCompFold E1 #

Lean.Meta.TransparencyMode →
  Mathlib.Tactic.Linarith.ExprMap →
    List Lean.Expr →
      Std.TreeMap Mathlib.Tactic.Linarith.Monom ℕ compare →
        Lean.MetaM
          (List Mathlib.Tactic.Linarith.Comp ×
            Mathlib.Tactic.Linarith.ExprMap × Std.TreeMap Mathlib.Tactic.Linarith.Monom ℕ compare)

toCompFold red e_map exprs monom_map folds toComp over exprs, updating e_map and monom_map as it goes.

def Mathlib.Tactic.Linarith.linearFormsAndMaxVar E1 #

Lean.Meta.TransparencyMode → List Lean.Expr → Lean.MetaM (List Mathlib.Tactic.Linarith.Comp × ℕ)

linearFormsAndMaxVar red pfs is the main interface for computing the linear forms of a list of expressions. Given a list pfs of proofs of comparisons, it produces a list c of Comps of the same length, such that c[i] represents the linear form of the type of pfs[i].

It also returns the largest variable index that appears in comparisons in c.