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

51 declarations. Mathlib docs · Source

Case bash on variables in finite intervals

This file provides the tactic interval_cases. interval_cases n will:

  1. inspect hypotheses looking for lower and upper bounds of the form a ≤ n or a < n and n < b or n ≤ b, including the bound 0 ≤ n for n : ℕ automatically.
  2. call fin_cases on the synthesised hypothesis n ∈ Set.Ico a b, assuming an appropriate Fintype instance can be found for the type of n.

Currently, n must be of type ℕ or ℤ (TODO: generalize).

You can also explicitly specify a lower and upper bound to use, as interval_cases using hl hu, where the hypotheses should be of the form hl : a ≤ n and hu : n < b. In that case, interval_cases calls fin_cases on the resulting hypothesis h : n ∈ Set.Ico a b.

theorem Mathlib.Tactic.IntervalCases.Bound.lt.injEq E0 #

∀ (n n_1 : ℤ), (Mathlib.Tactic.IntervalCases.Bound.lt n = Mathlib.Tactic.IntervalCases.Bound.lt n_1) = (n = n_1)

abbrev Mathlib.Tactic.IntervalCases.Bound.ctorElimType E1 #

{motive : Mathlib.Tactic.IntervalCases.Bound → Sort u} → ℕ → Sort (max 1 u)

theorem Mathlib.Tactic.IntervalCases.Bound.lt.inj E1 #

∀ {n n_1 : ℤ}, Mathlib.Tactic.IntervalCases.Bound.lt n = Mathlib.Tactic.IntervalCases.Bound.lt n_1 → n = n_1

theorem Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal.mk.inj E1 #

∀ {rhs : Lean.Expr} {value : ℤ} {goal : Lean.MVarId} {rhs_1 : Lean.Expr} {value_1 : ℤ} {goal_1 : Lean.MVarId},
  { rhs := rhs, value := value, goal := goal } = { rhs := rhs_1, value := value_1, goal := goal_1 } →
    rhs = rhs_1 ∧ value = value_1 ∧ goal = goal_1

def Mathlib.Tactic.IntervalCases.Methods.ctorIdx E1 #

Mathlib.Tactic.IntervalCases.Methods → ℕ

def Mathlib.Tactic.IntervalCases.Bound.ctorIdx E1 #

Mathlib.Tactic.IntervalCases.Bound → ℕ

def Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal.ctorIdx E1 #

Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal → ℕ

theorem Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal.mk.injEq E0 #

∀ (rhs : Lean.Expr) (value : ℤ) (goal : Lean.MVarId) (rhs_1 : Lean.Expr) (value_1 : ℤ) (goal_1 : Lean.MVarId),
  ({ rhs := rhs, value := value, goal := goal } = { rhs := rhs_1, value := value_1, goal := goal_1 }) =
    (rhs = rhs_1 ∧ value = value_1 ∧ goal = goal_1)

theorem Mathlib.Tactic.IntervalCases.Bound.le.injEq E0 #

∀ (n n_1 : ℤ), (Mathlib.Tactic.IntervalCases.Bound.le n = Mathlib.Tactic.IntervalCases.Bound.le n_1) = (n = n_1)

theorem Mathlib.Tactic.IntervalCases.Methods.mk.inj E1 #

∀ {initLB initUB : Lean.Expr → Lean.MetaM (Mathlib.Tactic.IntervalCases.Bound × Lean.Expr × Lean.Expr)}
  {proveLE proveLT : Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr}
  {roundUp roundDown : Lean.Expr → Lean.Expr → Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr}
  {eval : Lean.Expr → Lean.MetaM (ℤ × Lean.Expr × Lean.Expr)} {mkNumeral : ℤ → Lean.MetaM Lean.Expr}
  {initLB_1 initUB_1 : Lean.Expr → Lean.MetaM (Mathlib.Tactic.IntervalCases.Bound × Lean.Expr × Lean.Expr)}
  {proveLE_1 proveLT_1 : Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr}
  {roundUp_1 roundDown_1 : Lean.Expr → Lean.Expr → Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr}
  {eval_1 : Lean.Expr → Lean.MetaM (ℤ × Lean.Expr × Lean.Expr)} {mkNumeral_1 : ℤ → Lean.MetaM Lean.Expr},
  { initLB := initLB, initUB := initUB, proveLE := proveLE, proveLT := proveLT, roundUp := roundUp,
        roundDown := roundDown, eval := eval, mkNumeral := mkNumeral } =
      { initLB := initLB_1, initUB := initUB_1, proveLE := proveLE_1, proveLT := proveLT_1, roundUp := roundUp_1,
        roundDown := roundDown_1, eval := eval_1, mkNumeral := mkNumeral_1 } →
    initLB = initLB_1 ∧
      initUB = initUB_1 ∧
        proveLE = proveLE_1 ∧
          proveLT = proveLT_1 ∧ roundUp = roundUp_1 ∧ roundDown = roundDown_1 ∧ eval = eval_1 ∧ mkNumeral = mkNumeral_1

theorem Mathlib.Tactic.IntervalCases.Bound.le.inj E1 #

∀ {n n_1 : ℤ}, Mathlib.Tactic.IntervalCases.Bound.le n = Mathlib.Tactic.IntervalCases.Bound.le n_1 → n = n_1

theorem Mathlib.Tactic.IntervalCases.Methods.mk.injEq E0 #

∀ (initLB initUB : Lean.Expr → Lean.MetaM (Mathlib.Tactic.IntervalCases.Bound × Lean.Expr × Lean.Expr))
  (proveLE proveLT : Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr)
  (roundUp roundDown : Lean.Expr → Lean.Expr → Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr)
  (eval : Lean.Expr → Lean.MetaM (ℤ × Lean.Expr × Lean.Expr)) (mkNumeral : ℤ → Lean.MetaM Lean.Expr)
  (initLB_1 initUB_1 : Lean.Expr → Lean.MetaM (Mathlib.Tactic.IntervalCases.Bound × Lean.Expr × Lean.Expr))
  (proveLE_1 proveLT_1 : Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr)
  (roundUp_1 roundDown_1 : Lean.Expr → Lean.Expr → Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr)
  (eval_1 : Lean.Expr → Lean.MetaM (ℤ × Lean.Expr × Lean.Expr)) (mkNumeral_1 : ℤ → Lean.MetaM Lean.Expr),
  ({ initLB := initLB, initUB := initUB, proveLE := proveLE, proveLT := proveLT, roundUp := roundUp,
        roundDown := roundDown, eval := eval, mkNumeral := mkNumeral } =
      { initLB := initLB_1, initUB := initUB_1, proveLE := proveLE_1, proveLT := proveLT_1, roundUp := roundUp_1,
        roundDown := roundDown_1, eval := eval_1, mkNumeral := mkNumeral_1 }) =
    (initLB = initLB_1 ∧
      initUB = initUB_1 ∧
        proveLE = proveLE_1 ∧
          proveLT = proveLT_1 ∧ roundUp = roundUp_1 ∧ roundDown = roundDown_1 ∧ eval = eval_1 ∧ mkNumeral = mkNumeral_1)

structure Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal E1 #

Type

The result of interval_cases is a list of goals, one for each integer value between the bounds.

ctor Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal.mk E0 #

Lean.Expr → ℤ → Lean.MVarId → Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal

abbrev Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal.rhs E1 #

Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal → Lean.Expr

The target expression, a numeral in the input type

abbrev Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal.value E1 #

Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal → ℤ

The numeric value of the target expression

abbrev Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal.goal E1 #

Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal → Lean.MVarId

The new subgoal, of the form ⊢ x = rhs → tgt

inductive Mathlib.Tactic.IntervalCases.Bound E1 #

Type

A Bound represents the result of analyzing a lower or upper bound expression. If e is the scrutinee expression, then a lower bound expression like 3 < e is normalized to ¬e ≤ 3 and represented as .lt 3, and an upper bound expression like e ≤ 5 is represented as .le 5.

ctor Mathlib.Tactic.IntervalCases.Bound.lt E0 #

ℤ → Mathlib.Tactic.IntervalCases.Bound

A strictly less-than lower bound n ≱ e or upper bound e ≱ n. (interval_cases uses less-equal exclusively, so less-than bounds are actually written as not-less-equal with flipped arguments.)

ctor Mathlib.Tactic.IntervalCases.Bound.le E0 #

ℤ → Mathlib.Tactic.IntervalCases.Bound

A less-than-or-equal lower bound n ≤ e or upper bound e ≤ n.

def Mathlib.Tactic.IntervalCases.Bound.asLower E1 #

Mathlib.Tactic.IntervalCases.Bound → ℤ

Assuming Bound represents a lower bound, this returns the (inclusive) least integer value which is allowed. So 3 ≤ e means the lower bound is 3 and 3 < e means the lower bound is 4.

def Mathlib.Tactic.IntervalCases.Bound.asUpper E1 #

Mathlib.Tactic.IntervalCases.Bound → ℤ

Assuming Bound represents an upper bound, this returns the (inclusive) greatest integer value which is allowed. So e ≤ 3 means the lower bound is 3 and e < 3 means the upper bound is 2. Note that in the case of e < 0 on Nat the upper bound is -1, which is not representable as a Nat; this is why we have to treat the .lt and .le cases separately instead of normalizing everything to .le bounds.

def Mathlib.Tactic.IntervalCases.parseBound E1 #

Lean.Expr → Lean.MetaM (Lean.Expr × Lean.Expr × Bool × Bool)

Given a type ty (the type of a hypothesis in the context or a provided expression), attempt to parse it as an inequality, and return (a, b, strict, positive), where positive means it is a negated inequality and strict means it is a strict inequality (a < b or a ≱ b). a is always the lesser argument and b the greater one.

structure Mathlib.Tactic.IntervalCases.Methods E1 #

Type

A "typeclass" (not actually a class) of methods for the type-specific handling of interval_cases. To add support for a new type, you have to implement this interface and add a dispatch case for it in intervalCases.

ctor Mathlib.Tactic.IntervalCases.Methods.mk E0 #

(Lean.Expr → Lean.MetaM (Mathlib.Tactic.IntervalCases.Bound × Lean.Expr × Lean.Expr)) →
  (Lean.Expr → Lean.MetaM (Mathlib.Tactic.IntervalCases.Bound × Lean.Expr × Lean.Expr)) →
    (Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr) →
      (Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr) →
        (Lean.Expr → Lean.Expr → Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr) →
          (Lean.Expr → Lean.Expr → Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr) →
            (Lean.Expr → Lean.MetaM (ℤ × Lean.Expr × Lean.Expr)) →
              (ℤ → Lean.MetaM Lean.Expr) → Mathlib.Tactic.IntervalCases.Methods

abbrev Mathlib.Tactic.IntervalCases.Methods.initLB E1 #

Mathlib.Tactic.IntervalCases.Methods →
  Lean.Expr → Lean.MetaM (Mathlib.Tactic.IntervalCases.Bound × Lean.Expr × Lean.Expr)

Given e, construct (bound, n, p) where p is a proof of n ≤ e or n < e (characterized by bound), or failure if the type is not lower-bounded.

abbrev Mathlib.Tactic.IntervalCases.Methods.initUB E1 #

Mathlib.Tactic.IntervalCases.Methods →
  Lean.Expr → Lean.MetaM (Mathlib.Tactic.IntervalCases.Bound × Lean.Expr × Lean.Expr)

Given e, construct (bound, n, p) where p is a proof of e ≤ n or e < n (characterized by bound), or failure if the type is not upper-bounded.

abbrev Mathlib.Tactic.IntervalCases.Methods.proveLE E1 #

Mathlib.Tactic.IntervalCases.Methods → Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr

Given a, b, prove a ≤ b or fail.

abbrev Mathlib.Tactic.IntervalCases.Methods.proveLT E1 #

Mathlib.Tactic.IntervalCases.Methods → Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr

Given a, b, prove a ≱ b or fail.

abbrev Mathlib.Tactic.IntervalCases.Methods.roundUp E1 #

Mathlib.Tactic.IntervalCases.Methods → Lean.Expr → Lean.Expr → Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr

Given a, b, a', p where p proves a ≱ b and a' := a+1, prove a' ≤ b.

abbrev Mathlib.Tactic.IntervalCases.Methods.roundDown E1 #

Mathlib.Tactic.IntervalCases.Methods → Lean.Expr → Lean.Expr → Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr

Given a, b, b', p where p proves a ≱ b and b' := b-1, prove a ≤ b'.

abbrev Mathlib.Tactic.IntervalCases.Methods.eval E1 #

Mathlib.Tactic.IntervalCases.Methods → Lean.Expr → Lean.MetaM (ℤ × Lean.Expr × Lean.Expr)

Given e, return (z, n, p) where p : e = n and n is a numeral appropriate for the type denoting the integer z.

abbrev Mathlib.Tactic.IntervalCases.Methods.mkNumeral E1 #

Mathlib.Tactic.IntervalCases.Methods → ℤ → Lean.MetaM Lean.Expr

Construct the canonical numeral for integer z, or fail if z is out of range.

theorem Mathlib.Tactic.IntervalCases.of_not_lt_left E1 #

∀ {α : Type u_1} {a b a' : α} [inst : LinearOrder α], ¬a < b → a = a' → b ≤ a'

theorem Mathlib.Tactic.IntervalCases.of_not_lt_right E1 #

∀ {α : Type u_1} {a b b' : α} [inst : LinearOrder α], ¬a < b → b = b' → b' ≤ a

theorem Mathlib.Tactic.IntervalCases.of_not_le_left E1 #

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

theorem Mathlib.Tactic.IntervalCases.of_not_le_right E1 #

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

theorem Mathlib.Tactic.IntervalCases.of_lt_left E1 #

∀ {α : Type u_1} {a b a' : α} [inst : LinearOrder α], a < b → a = a' → ¬b ≤ a'

theorem Mathlib.Tactic.IntervalCases.of_lt_right E1 #

∀ {α : Type u_1} {a b b' : α} [inst : LinearOrder α], a < b → b = b' → ¬b' ≤ a

theorem Mathlib.Tactic.IntervalCases.of_le_left E1 #

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

theorem Mathlib.Tactic.IntervalCases.of_le_right E1 #

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

def Mathlib.Tactic.IntervalCases.Methods.getBound E1 #

Mathlib.Tactic.IntervalCases.Methods →
  Lean.Expr → Lean.Expr → Bool → Lean.MetaM (Mathlib.Tactic.IntervalCases.Bound × Lean.Expr × Lean.Expr)

Given a proof pf, attempts to parse it as an upper (lb = false) or lower (lb = true) bound on n. If successful, it returns (bound, n, pf') where n is a numeral and pf' proves n ≤ e or n ≱ e (as described by bound).

theorem Mathlib.Tactic.IntervalCases.le_of_not_le_of_le E1 #

∀ {α : Type u_1} {hi n lo : α} [inst : LinearOrder α], ¬hi ≤ n → hi ≤ lo → n ≤ lo

def Mathlib.Tactic.IntervalCases.Methods.inconsistentBounds E1 #

Mathlib.Tactic.IntervalCases.Methods →
  Mathlib.Tactic.IntervalCases.Bound →
    Mathlib.Tactic.IntervalCases.Bound →
      Lean.Expr → Lean.Expr → Lean.Expr → Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr

Given (z1, e1, p1) a lower bound on e and (z2, e2, p2) an upper bound on e, such that the distance between the bounds is negative, returns a proof of False.

opaque Mathlib.Tactic.IntervalCases.Methods.bisect E1 #

Mathlib.Tactic.IntervalCases.Methods →
  Lean.MVarId →
    Subarray Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal →
      Mathlib.Tactic.IntervalCases.Bound →
        Mathlib.Tactic.IntervalCases.Bound → Lean.Expr → Lean.Expr → Lean.Expr → Lean.Expr → Lean.Expr → Lean.MetaM Unit

Given (z1, e1, p1) a lower bound on e and (z2, e2, p2) an upper bound on e, such that the distance between the bounds matches the number of cases in the subarray (which must be positive), proves the goal g using the metavariables in the array by recursive bisection. This is the core of the tactic, producing a case tree of if statements which bottoms out at the cases.

def Mathlib.Tactic.IntervalCases.natMethods E1 #

Mathlib.Tactic.IntervalCases.Methods

A Methods implementation for ℕ. This tells interval_cases how to work on natural numbers.

theorem Int.add_one_le_of_not_le E1 #

∀ {a b : ℤ}, ¬b ≤ a → a + 1 ≤ b

theorem Int.le_sub_one_of_not_le E1 #

∀ {a b : ℤ}, ¬b ≤ a → a ≤ b - 1

def Mathlib.Tactic.IntervalCases.intMethods E1 #

Mathlib.Tactic.IntervalCases.Methods

A Methods implementation for ℤ. This tells interval_cases how to work on integers.

def Mathlib.Tactic.IntervalCases.intervalCases E1 #

Lean.MVarId →
  Lean.Expr →
    Lean.Expr →
      Array Lean.Expr →
        Array Lean.Expr → optParam Bool false → Lean.MetaM (Array Mathlib.Tactic.IntervalCases.IntervalCasesSubgoal)

intervalCases proves goal g by splitting into cases for each integer between the given bounds.

Parameters:

  • g: the goal, which can have any type ⊢ tgt (it works in both proofs and programs)
  • e: the scrutinee, the expression we are proving is bounded between integers
  • e': a version of e used for error messages. (This is used by the interval_cases frontend tactic because it uses a fresh variable for e, so it is more helpful to show the pre-generalized expression in error messages.)
  • lbs: A list of candidate lower bound expressions. The tactic will automatically pick the best lower bound it can find from the list.
  • ubs: A list of candidate upper bound expressions. The tactic will automatically pick the best upper bound it can find from the list.
  • mustUseBounds: If true, the tactic will fail if it is unable to parse any of the given ubs or lbs into bounds. If false (the default), these will be silently skipped and an error message is only produced if we could not find any bounds (including those supplied by the type itself, e.g. if we are working over Nat or Fin n).

Returns an array of IntervalCasesSubgoal, one per subgoal. A subgoal has the following fields:

  • rhs: the numeral expression for this case
  • value: the integral value of rhs
  • goal: the subgoal of type ⊢ e = rhs → tgt

Note that this tactic does not perform any substitution or introduction steps - all subgoals are in the same context as goal itself.

def Mathlib.Tactic.intervalCases E1 #

Lean.ParserDescr

interval_cases n searches for upper and lower bounds on a variable n, and if bounds are found, splits into separate cases for each possible value of n.

As an example, in

example (n : ℕ) (w₁ : n ≥ 3) (w₂ : n < 5) : n = 3 ∨ n = 4 := by
  interval_cases n
  all_goals simp

after interval_cases n, the goals are 3 = 3 ∨ 3 = 4 and 4 = 3 ∨ 4 = 4.

You can also explicitly specify a lower and upper bound to use, as interval_cases using hl, hu. The hypotheses should be in the form hl : a ≤ n and hu : n < b, in which case interval_cases calls fin_cases on the resulting fact n ∈ Set.Ico a b.

You can specify a name h for the new hypothesis, as interval_cases h : n or interval_cases h : n using hl, hu.