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.Basify

11 declarations. Mathlib docs · Source

The basify tactic

Mathlib has many types built from a well-behaved type by a construction that makes the resulting arithmetic partial or truncated. The two most common are

  • extensions by a point at infinity, such as ℕ∞ = WithTop ℕ;
  • subtypes cut out by an inequality, such as ℝ≥0 = {r : ℝ // 0 ≤ r}.

Goals about them are painful, because the decision procedures one would like to use (grind, linarith, norm_num) only understand the underlying type. basify ("base" + "ify") peels the construction off, turning the goal into an equivalent one about the base type:

example (a b : ℝ≥0∞) (h : a + b = 0) : a = 0 := by
  basify
  -- goal now: a : ℝ, a_nonneg : 0 ≤ a, b : ℝ, b_nonneg : 0 ≤ b, h : a + b = 0 ⊢ a = 0
  linarith

Implementation

We proceed in three phases.

  1. We traverse the goal and the hypotheses and collect the atoms: the subexpressions of a compound type, the type we are going to shift to its base type. A compound type is one registered with @[basify_elim]. During the search we look through operations that can themselves be translated to the base type, so in a + b with a b : ℝ≥0∞ we collect a and b rather than the sum; such operations are registered with @[basify_op].
  2. We iterate through the atoms, applying cases to each with the eliminator tagged @[basify_elim]. That sometimes leaves several goals, and often (for ℕ∞ and ℝ≥0∞) the ones mentioning an infinity are typically trivial, so we run the basify_simp simp set after every split to discharge them early and keep the branching from blowing up.
  3. We finish the descent with a final simp_all only [basify_simp], which uses the hypotheses to discharge the side conditions of the conditional cast lemmas.

Relation to other tactics

basify achieves what zify/qify/rify achieve -- propositions shifted to a type where the arithmetic is total -- but not in the same way: those leave every variable's type alone, whereas basify destructs the variables, so an ℝ≥0 hypothesis really does become an ℝ one paired with 0 ≤ ·. In that respect it is closer to lift. The name says "the base type" rather than a fixed one because the target depends on what is registered: ℕ for ℕ∞, ℝ for ℝ≥0∞.

  • rify already covers ℝ≥0 → ℝ, and linarith ships a preprocessor doing the same shift with the nonnegativity facts. On a goal stated purely in ℝ≥0 those usually suffice; basify earns its keep there only on truncated subtraction, which it rewrites with NNReal.coe_sub_def.
  • lift is the per-variable version of the interesting branch of a split: lift a to ℝ≥0 using ha is what one writes by hand once a ≠ ⊤ is known. basify splits on it instead, and discharges the other branch.
  • norm_cast removes coercions and therefore lands in the smallest type of a cast tower, which for ℝ≥0∞ is ℝ≥0, not ℝ. Reaching ℝ means travelling down one coercion and up another, which is why basify_simp holds ← lemmas for the extension layer and forward ones for the subtype layer.

The case split has no counterpart elsewhere: ⊤ - a is not a cast-normalisation problem, and no amount of rewriting turns it into one.

Extending the tactic

Three attributes drive the tactic, so that a new construction needs no change to this file:

  • @[basify_elim] marks an eliminator; the type it destructs and the shape of each of its cases are read off from its type.
  • @[basify_op] marks the lemma relating an operation of a compound type to the corresponding operation below, such as ENNReal.coe_add : ↑(a + b) = ↑a + ↑b. It records the operation and adds the lemma to basify_simp, reversed if ← is given -- the direction the coercion has to travel, outwards for a WithTop layer and inwards for a subtype.
  • @[basify_simp] is the simp set described above, for everything that is not an operation: relations such as ENNReal.coe_inj, and the lemmas that clear away infinities.

Mathlib/Tactic/Basify/ENNReal.lean is a worked example using all three.

Propositional cleanup

The basify_simp simp set is run with simp only, so it has to carry the handful of propositional lemmas needed to actually make a contradictory branch disappear.

Atoms

The phases

def Mathlib.Tactic.Basify.elimEntryFor? E1 #

Lean.Expr → Lean.MetaM (Option Mathlib.Tactic.Basify.ElimEntry)

The eliminator registered for the type ty, if any.

def Mathlib.Tactic.Basify.opsFor E1 #

Lean.Expr → Lean.MetaM Lean.NameSet

The operations registered for the type ty, i.e. the applications of type ty that basify looks inside of instead of treating as atoms.

def Mathlib.Tactic.Basify.isRegisteredType E1 #

Lean.Expr → Lean.MetaM Bool

Does basify know anything about the type ty?

def Mathlib.Tactic.Basify.isAtom E1 #

Lean.Expr → Lean.MetaM Bool

Is e an atom, i.e. a term of a registered type that basify cannot see inside of?

A term of a registered type is not an atom when its head is an operation registered with @[basify_op], in which case its arguments are visited instead, and when it is a let, whose value is visited instead. Everything else is opaque and gets generalized and case split as a whole.

opaque Mathlib.Tactic.Basify.collectAtoms E1 #

Lean.Expr → Mathlib.Tactic.AtomM Unit

Collect the atoms of e

def Mathlib.Tactic.Basify.goalAtoms E1 #

Lean.MVarId → Lean.MetaM (Array Lean.Expr)

All the atoms of the goal g, taken from the target and from every hypothesis.

def Mathlib.Tactic.Basify.generalizeAtoms E1 #

Lean.MVarId → Lean.MetaM (Lean.MVarId × Array Lean.FVarId)

Turn every atom of g into a variable that can be case split, generalizing the ones that are not variables already and keeping their defining equation as <name>_eq. Returns those variables.

def Mathlib.Tactic.Basify.casesAtom E1 #

Lean.MVarId →
  Lean.FVarId → Mathlib.Tactic.Basify.ElimEntry → Lean.Elab.Tactic.TacticM (List (Lean.MVarId × Array Lean.FVarId))

Case split fvarId with the eliminator entry.elimName, naming what each alternative introduces after fvarId itself. Returns one goal per alternative, each paired with the variables that alternative introduced.

def Mathlib.Tactic.Basify.clearTrivialHypotheses E1 #

Lean.MVarId → Lean.MetaM Lean.MVarId

Remove the True hypotheses that simp only ... at * leaves behind.

opaque Mathlib.Tactic.Basify.basifyLoop E1 #

Lean.MVarId → List Lean.FVarId → Lean.Elab.Tactic.TacticM (List Lean.MVarId)

The main loop of the basify tactic: case split the variables in varsToElim one at a time, running the basify_simp simp set after each so that the degenerate branches (⊤ + a = ⊤ and friends) die before the branching explodes.

def Mathlib.Tactic.Basify.tacticBasify E1 #

Lean.ParserDescr

basify removes the layers that separate a type from the type its arithmetic really lives in, turning the goal into an equivalent goal about that type: ℕ∞ and ℕ+ become ℕ, ℝ≥0 becomes ℝ, and ℝ≥0∞ becomes ℝ by way of ℝ≥0.

Every value of a registered type is destructed with the eliminator registered for it -- ⊤ or ↑x for an extension such as ℕ∞, n.toPNat' together with 0 < n for a subtype such as ℕ+ -- the degenerate branches are discharged, and the surviving propositions are pushed down along the coercions. The result is then can be finished off by a decision procedure for the underlying type:

example (a b : ℕ∞) (h : a ≤ b) : a - b < b + 1 := by basify; lia
example (a b : ℕ+) (h : a < b) : 1 < b := by basify; lia
example (a b : ℝ≥0) (h : a + b = 0) : a = 0 := by basify; linarith
example (a b c : ℝ≥0∞) (hab : a ≥ b) (hbc : b ≥ c) : a ≥ c := by basify <;> linarith

The cast lemmas for division and inverse are conditional, and are discharged from the context, so a goal using them needs the relevant ≠ 0 to be available; without it the descent stops part-way.

example (a : ℝ≥0∞) (h : a ≠ 0) (h' : a ≠ ⊤) : a * a⁻¹ = 1 := by basify; field_simp

New types are supported by tagging an eliminator with @[basify_elim], its operations with @[basify_op], and the relevant rewrite lemmas with @[basify_simp].