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.GCongr.Core

87 declarations. Mathlib docs · Source

The gcongr ("generalized congruence") tactic

The gcongr tactic applies "generalized congruence" rules, reducing a relational goal between an LHS and RHS matching the same pattern to relational subgoals between the differing inputs to the pattern. For example,

example {a b x c d : ℝ} (h1 : a + 1 ≤ b + 1) (h2 : c + 2 ≤ d + 2) :
    x ^ 2 * a + c ≤ x ^ 2 * b + d := by
  gcongr
  · linarith
  · linarith

This example has the goal of proving the relation ≤ between an LHS and RHS both of the pattern

x ^ 2 * ?_ + ?_

(with inputs a, c on the left and b, d on the right); after the use of gcongr, we have the simpler goals a ≤ b and c ≤ d.

A depth limit, or a pattern can be provided explicitly; this is useful if a non-maximal match is desired:

example {a b c d x : ℝ} (h : a + c + 1 ≤ b + d + 1) :
    x ^ 2 * (a + c) + 5 ≤ x ^ 2 * (b + d) + 5 := by
  gcongr x ^ 2 * ?_ + 5 -- or `gcongr 2`
  linarith

Sourcing the generalized congruence lemmas

Relevant "generalized congruence" lemmas are declared using the attribute @[gcongr]. For example, the first example constructs the proof term

add_le_add (mul_le_mul_of_nonneg_left _ (pow_bit0_nonneg x 1)) _

using the generalized congruence lemmas add_le_add and mul_le_mul_of_nonneg_left. The term pow_bit0_nonneg x 1 is automatically generated by a discharger (see below).

When a lemma is tagged @[gcongr], it is verified that the lemma is of "generalized congruence" form, f x₁ y z₁ ∼ f x₂ y z₂, that is, a relation between the application of a function to two argument lists, in which the "varying argument" pairs (here x₁/x₂ and z₁/z₂) are all free variables. The gcongr tactic will try a lemma only if it matches the goal in relation ∼, head function f and the arity of f. It prioritizes lemmas with fewer "varying arguments". Thus, for example, all three of the following lemmas are tagged @[gcongr] and are used in different situations according to whether the goal compares constant-left-multiplications, constant-right-multiplications, or fully varying multiplications:

theorem mul_le_mul_of_nonneg_left [Mul α] [Zero α] [Preorder α] [PosMulMono α]
    {a b c : α} (h : b ≤ c) (a0 : 0 ≤ a) :
    a * b ≤ a * c

theorem mul_le_mul_of_nonneg_right [Mul α] [Zero α] [Preorder α] [MulPosMono α]
    {a b c : α} (h : b ≤ c) (a0 : 0 ≤ a) :
    b * a ≤ c * a

theorem mul_le_mul [MulZeroClass α] [Preorder α] [PosMulMono α] [MulPosMono α]
    {a b c d : α} (h₁ : a ≤ b) (h₂ : c ≤ d) (c0 : 0 ≤ c) (b0 : 0 ≤ b) :
    a * c ≤ b * d

The advantage of this approach is that the lemmas with fewer "varying" input pairs typically require fewer side conditions, so the tactic becomes more useful by special-casing them.

There can also be more than one generalized congruence lemma dealing with the same relation and head function, for example with purely notational head functions which have different theories when different typeclass assumptions apply. For example, the following lemma is stored with the same @[gcongr] data as mul_le_mul above, and the two lemmas are simply tried in succession to determine which has the typeclasses relevant to the goal:

theorem mul_le_mul' [Mul α] [Preorder α] [MulLeftMono α]
    [MulRightMono α] {a b c d : α} (h₁ : a ≤ b) (h₂ : c ≤ d) :
    a * c ≤ b * d

Resolving goals

The tactic attempts to discharge side goals to the "generalized congruence" lemmas (such as the side goal 0 ≤ x ^ 2 in the above application of mul_le_mul_of_nonneg_left) using the tactic gcongr_discharger, which wraps positivity but can also be extended. Side goals not discharged in this way are left for the user.

The tactic also attempts to discharge "main" goals using the available hypotheses, as well as a limited amount of forward reasoning. Such attempts are made before descending further into matching by congruence. The built-in forward-reasoning includes reasoning by symmetry and reflexivity, and this can be extended by writing tactic extensions tagged with the @[gcongr_forward] attribute.

Introducing variables and hypotheses

Some natural generalized congruence lemmas have "main" hypotheses which are universally quantified or have the structure of an implication, for example

theorem GCongr.Finset.sum_le_sum [OrderedAddCommMonoid N] {f g : ι → N} {s : Finset ι}
    (h : ∀ (i : ι), i ∈ s → f i ≤ g i) :
    s.sum f ≤ s.sum g

The tactic automatically introduces the variable i✝ : ι and hypothesis hi✝ : i✝ ∈ s in the subgoal ∀ (i : ι), i ∈ s → f i ≤ g i generated by applying this lemma. By default this is done anonymously, so they are inaccessible in the goal state which results. The user can name them if needed using the syntax gcongr with i hi.

Variants

The tactic rel is a variant of gcongr, intended for teaching. Local hypotheses are not used automatically to resolve main goals, but must be invoked by name:

example {a b x c d : ℝ} (h1 : a ≤ b) (h2 : c ≤ d) :
    x ^ 2 * a + c ≤ x ^ 2 * b + d := by
  rel [h1, h2]

The rel tactic is finishing-only: it fails if any main or side goals are not resolved.

Implementation notes

Patterns

When you provide a pattern, such as in gcongr x ^ 2 * ?_ + 5, this is elaborated with a metadata annotation at each ?_ hole. This is then unified with the LHS of the relation in the goal. As a result, the pattern becomes the same as the LHS, but with mdata annotations that indicate where the original ?_ holes were. The LHS is then replaced with this expression so that gcongr can tell based on the LHS how far to continue recursively. We also keep track of when then LHS and RHS swap around, so that we know where to look for the metadata annotation.

The lemma rel_imp_rel is too general to be tagged with @[gcongr], so instead we use relImpRelLemma to look it up.

def Mathlib.Tactic.GCongr.GCongrLemma.ctorIdx E1 #

Mathlib.Tactic.GCongr.GCongrLemma → ℕ

theorem Mathlib.Tactic.GCongr.Context.mk.injEq E0 #

∀ (mainGoalDischarger : Lean.MVarId → Lean.MetaM Bool) (sideGoalDischarger : Lean.MVarId → Lean.MetaM Unit)
  (mainGoalDischarger_1 : Lean.MVarId → Lean.MetaM Bool) (sideGoalDischarger_1 : Lean.MVarId → Lean.MetaM Unit),
  ({ mainGoalDischarger := mainGoalDischarger, sideGoalDischarger := sideGoalDischarger } =
      { mainGoalDischarger := mainGoalDischarger_1, sideGoalDischarger := sideGoalDischarger_1 }) =
    (mainGoalDischarger = mainGoalDischarger_1 ∧ sideGoalDischarger = sideGoalDischarger_1)

theorem Mathlib.Tactic.GCongr.GCongrHyp.mk.inj E1 #

∀ {lhsIdx rhsIdx hypIdx : ℕ} {hypsPos : List (Option ℕ)} {isContra : Bool} {lhsIdx_1 rhsIdx_1 hypIdx_1 : ℕ}
  {hypsPos_1 : List (Option ℕ)} {isContra_1 : Bool},
  { lhsIdx := lhsIdx, rhsIdx := rhsIdx, hypIdx := hypIdx, hypsPos := hypsPos, isContra := isContra } =
      { lhsIdx := lhsIdx_1, rhsIdx := rhsIdx_1, hypIdx := hypIdx_1, hypsPos := hypsPos_1, isContra := isContra_1 } →
    lhsIdx = lhsIdx_1 ∧ rhsIdx = rhsIdx_1 ∧ hypIdx = hypIdx_1 ∧ hypsPos = hypsPos_1 ∧ isContra = isContra_1

theorem Mathlib.Tactic.GCongr.State.mk.inj E1 #

∀ {newGoals : Array Lean.MVarId} {patterns : List (Lean.TSyntax `rintroPat)} {newGoals_1 : Array Lean.MVarId}
  {patterns_1 : List (Lean.TSyntax `rintroPat)},
  { newGoals := newGoals, patterns := patterns } = { newGoals := newGoals_1, patterns := patterns_1 } →
    newGoals = newGoals_1 ∧ patterns = patterns_1

def Mathlib.Tactic.GCongr.instInhabitedGCongrLemma.default E1 #

Mathlib.Tactic.GCongr.GCongrLemma

def Mathlib.Tactic.GCongr.GCongrKey.ctorIdx E1 #

Mathlib.Tactic.GCongr.GCongrKey → ℕ

theorem Mathlib.Tactic.GCongr.State.mk.injEq E0 #

∀ (newGoals : Array Lean.MVarId) (patterns : List (Lean.TSyntax `rintroPat)) (newGoals_1 : Array Lean.MVarId)
  (patterns_1 : List (Lean.TSyntax `rintroPat)),
  ({ newGoals := newGoals, patterns := patterns } = { newGoals := newGoals_1, patterns := patterns_1 }) =
    (newGoals = newGoals_1 ∧ patterns = patterns_1)

def Mathlib.Tactic.GCongr.instInhabitedGCongrKey.default E1 #

Mathlib.Tactic.GCongr.GCongrKey

theorem Mathlib.Tactic.GCongr.GCongrHyp.mk.injEq E0 #

∀ (lhsIdx rhsIdx hypIdx : ℕ) (hypsPos : List (Option ℕ)) (isContra : Bool) (lhsIdx_1 rhsIdx_1 hypIdx_1 : ℕ)
  (hypsPos_1 : List (Option ℕ)) (isContra_1 : Bool),
  ({ lhsIdx := lhsIdx, rhsIdx := rhsIdx, hypIdx := hypIdx, hypsPos := hypsPos, isContra := isContra } =
      { lhsIdx := lhsIdx_1, rhsIdx := rhsIdx_1, hypIdx := hypIdx_1, hypsPos := hypsPos_1, isContra := isContra_1 }) =
    (lhsIdx = lhsIdx_1 ∧ rhsIdx = rhsIdx_1 ∧ hypIdx = hypIdx_1 ∧ hypsPos = hypsPos_1 ∧ isContra = isContra_1)

theorem Mathlib.Tactic.GCongr.GCongrKey.mk.inj E1 #

∀ {relName head : Lean.Name} {arity : ℕ} {relName_1 head_1 : Lean.Name} {arity_1 : ℕ},
  { relName := relName, head := head, arity := arity } = { relName := relName_1, head := head_1, arity := arity_1 } →
    relName = relName_1 ∧ head = head_1 ∧ arity = arity_1

theorem Mathlib.Tactic.GCongr.GCongrLemma.mk.injEq E0 #

∀ (keys : List Mathlib.Tactic.GCongr.GCongrKey) (declName : Lean.Name)
  (mainSubgoals : Array Mathlib.Tactic.GCongr.GCongrHyp) (numHyps prio numVarying : ℕ) (forGrw : Bool)
  (keys_1 : List Mathlib.Tactic.GCongr.GCongrKey) (declName_1 : Lean.Name)
  (mainSubgoals_1 : Array Mathlib.Tactic.GCongr.GCongrHyp) (numHyps_1 prio_1 numVarying_1 : ℕ) (forGrw_1 : Bool),
  ({ keys := keys, declName := declName, mainSubgoals := mainSubgoals, numHyps := numHyps, prio := prio,
        numVarying := numVarying, forGrw := forGrw } =
      { keys := keys_1, declName := declName_1, mainSubgoals := mainSubgoals_1, numHyps := numHyps_1, prio := prio_1,
        numVarying := numVarying_1, forGrw := forGrw_1 }) =
    (keys = keys_1 ∧
      declName = declName_1 ∧
        mainSubgoals = mainSubgoals_1 ∧
          numHyps = numHyps_1 ∧ prio = prio_1 ∧ numVarying = numVarying_1 ∧ forGrw = forGrw_1)

theorem Mathlib.Tactic.GCongr.GCongrLemma.mk.inj E1 #

∀ {keys : List Mathlib.Tactic.GCongr.GCongrKey} {declName : Lean.Name}
  {mainSubgoals : Array Mathlib.Tactic.GCongr.GCongrHyp} {numHyps prio numVarying : ℕ} {forGrw : Bool}
  {keys_1 : List Mathlib.Tactic.GCongr.GCongrKey} {declName_1 : Lean.Name}
  {mainSubgoals_1 : Array Mathlib.Tactic.GCongr.GCongrHyp} {numHyps_1 prio_1 numVarying_1 : ℕ} {forGrw_1 : Bool},
  { keys := keys, declName := declName, mainSubgoals := mainSubgoals, numHyps := numHyps, prio := prio,
        numVarying := numVarying, forGrw := forGrw } =
      { keys := keys_1, declName := declName_1, mainSubgoals := mainSubgoals_1, numHyps := numHyps_1, prio := prio_1,
        numVarying := numVarying_1, forGrw := forGrw_1 } →
    keys = keys_1 ∧
      declName = declName_1 ∧
        mainSubgoals = mainSubgoals_1 ∧
          numHyps = numHyps_1 ∧ prio = prio_1 ∧ numVarying = numVarying_1 ∧ forGrw = forGrw_1

def Mathlib.Tactic.GCongr.GCongrHyp.ctorIdx E1 #

Mathlib.Tactic.GCongr.GCongrHyp → ℕ

theorem Mathlib.Tactic.GCongr.Context.mk.inj E1 #

∀ {mainGoalDischarger : Lean.MVarId → Lean.MetaM Bool} {sideGoalDischarger : Lean.MVarId → Lean.MetaM Unit}
  {mainGoalDischarger_1 : Lean.MVarId → Lean.MetaM Bool} {sideGoalDischarger_1 : Lean.MVarId → Lean.MetaM Unit},
  { mainGoalDischarger := mainGoalDischarger, sideGoalDischarger := sideGoalDischarger } =
      { mainGoalDischarger := mainGoalDischarger_1, sideGoalDischarger := sideGoalDischarger_1 } →
    mainGoalDischarger = mainGoalDischarger_1 ∧ sideGoalDischarger = sideGoalDischarger_1

def Mathlib.Tactic.GCongr.State.ctorIdx E1 #

Mathlib.Tactic.GCongr.State → ℕ

theorem Mathlib.Tactic.GCongr.GCongrKey.mk.injEq E0 #

∀ (relName head : Lean.Name) (arity : ℕ) (relName_1 head_1 : Lean.Name) (arity_1 : ℕ),
  ({ relName := relName, head := head, arity := arity } = { relName := relName_1, head := head_1, arity := arity_1 }) =
    (relName = relName_1 ∧ head = head_1 ∧ arity = arity_1)

def Mathlib.Tactic.GCongr.Context.ctorIdx E1 #

Mathlib.Tactic.GCongr.Context → ℕ

structure Mathlib.Tactic.GCongr.GCongrKey E1 #

Type

GCongrKey is the key used in the dictionary for looking up gcongr lemmas.

ctor Mathlib.Tactic.GCongr.GCongrKey.mk E0 #

Lean.Name → Lean.Name → ℕ → Mathlib.Tactic.GCongr.GCongrKey

abbrev Mathlib.Tactic.GCongr.GCongrKey.relName E1 #

Mathlib.Tactic.GCongr.GCongrKey → Lean.Name

The name of the relation. For example, a + b ≤ a + c has relName := `LE.le.

abbrev Mathlib.Tactic.GCongr.GCongrKey.head E1 #

Mathlib.Tactic.GCongr.GCongrKey → Lean.Name

The name of the head function. For example, a + b ≤ a + c has head := `HAdd.hAdd.

abbrev Mathlib.Tactic.GCongr.GCongrKey.arity E1 #

Mathlib.Tactic.GCongr.GCongrKey → ℕ

The number of arguments that head is applied to. For example, a + b ≤ a + c has arity := 6, because HAdd.hAdd has 6 arguments.

instance Mathlib.Tactic.GCongr.instInhabitedGCongrKey E0 #

Inhabited Mathlib.Tactic.GCongr.GCongrKey

instance Mathlib.Tactic.GCongr.instBEqGCongrKey E0 #

BEq Mathlib.Tactic.GCongr.GCongrKey

def Mathlib.Tactic.GCongr.instBEqGCongrKey.beq E1 #

Mathlib.Tactic.GCongr.GCongrKey → Mathlib.Tactic.GCongr.GCongrKey → Bool

instance Mathlib.Tactic.GCongr.instOrdGCongrKey E0 #

Ord Mathlib.Tactic.GCongr.GCongrKey

structure Mathlib.Tactic.GCongr.GCongrHyp E1 #

Type

The data for a "main" hypothesis of a gcongr lemma. That is a hypothesis of the form ∀ xs, a xs' ~ b xs' where xs' is a subset of xs, and a and b form a "varying pair" in the conclusion of the gcongr lemma.

ctor Mathlib.Tactic.GCongr.GCongrHyp.mk E0 #

ℕ → ℕ → ℕ → List (Option ℕ) → Bool → Mathlib.Tactic.GCongr.GCongrHyp

abbrev Mathlib.Tactic.GCongr.GCongrHyp.lhsIdx E1 #

Mathlib.Tactic.GCongr.GCongrHyp → ℕ

Index of the left varying argument.

abbrev Mathlib.Tactic.GCongr.GCongrHyp.rhsIdx E1 #

Mathlib.Tactic.GCongr.GCongrHyp → ℕ

Index of the right varying argument.

abbrev Mathlib.Tactic.GCongr.GCongrHyp.hypIdx E1 #

Mathlib.Tactic.GCongr.GCongrHyp → ℕ

Index of the hypothesis itself.

abbrev Mathlib.Tactic.GCongr.GCongrHyp.hypsPos E1 #

Mathlib.Tactic.GCongr.GCongrHyp → List (Option ℕ)

In ∀ xs, a xs' ~ b xs', for each x in xs, the position it appears in xs'. This information is used to infer suitable names when introducing these variables.

abbrev Mathlib.Tactic.GCongr.GCongrHyp.isContra E1 #

Mathlib.Tactic.GCongr.GCongrHyp → Bool

Whether the order of the two varying arguments is the opposite from in the conclusion.

structure Mathlib.Tactic.GCongr.GCongrLemma E1 #

Type

Structure recording the data for a "generalized congruence" (gcongr) lemma.

ctor Mathlib.Tactic.GCongr.GCongrLemma.mk E0 #

List Mathlib.Tactic.GCongr.GCongrKey →
  Lean.Name → Array Mathlib.Tactic.GCongr.GCongrHyp → ℕ → ℕ → ℕ → Bool → Mathlib.Tactic.GCongr.GCongrLemma

abbrev Mathlib.Tactic.GCongr.GCongrLemma.keys E1 #

Mathlib.Tactic.GCongr.GCongrLemma → List Mathlib.Tactic.GCongr.GCongrKey

The keys under which the lemma is stored. This is usually one key, but for gcongr strict lemmas, this stores two keys, for the LHS and RHS respectively.

abbrev Mathlib.Tactic.GCongr.GCongrLemma.declName E1 #

Mathlib.Tactic.GCongr.GCongrLemma → Lean.Name

The name of the lemma.

abbrev Mathlib.Tactic.GCongr.GCongrLemma.mainSubgoals E1 #

Mathlib.Tactic.GCongr.GCongrLemma → Array Mathlib.Tactic.GCongr.GCongrHyp

mainSubgoals are the subgoals on which gcongr will be recursively called.

abbrev Mathlib.Tactic.GCongr.GCongrLemma.numHyps E1 #

Mathlib.Tactic.GCongr.GCongrLemma → ℕ

The number of arguments that declName takes when applying it.

abbrev Mathlib.Tactic.GCongr.GCongrLemma.prio E1 #

Mathlib.Tactic.GCongr.GCongrLemma → ℕ

The given priority of the lemma, for example as @[gcongr high].

abbrev Mathlib.Tactic.GCongr.GCongrLemma.numVarying E1 #

Mathlib.Tactic.GCongr.GCongrLemma → ℕ

The number of arguments in the application of head that are different. This is used for sorting the lemmas. For example, a + b ≤ a + c has numVarying := 1.

abbrev Mathlib.Tactic.GCongr.GCongrLemma.forGrw E1 #

Mathlib.Tactic.GCongr.GCongrLemma → Bool

Whether the lemma is suitable for use in grw. For most lemmas this is true, but there are lemmas that aren't quite congruence lemmas, but can still be used in gcongr.

instance Mathlib.Tactic.GCongr.instInhabitedGCongrLemma E0 #

Inhabited Mathlib.Tactic.GCongr.GCongrLemma

abbrev Mathlib.Tactic.GCongr.GCongrLemmas E1 #

Type

A collection of GCongrLemma, to be stored in the environment extension.

def Mathlib.Tactic.GCongr.GCongrLemma.prioLE E1 #

Mathlib.Tactic.GCongr.GCongrLemma → Mathlib.Tactic.GCongr.GCongrLemma → Bool

Return true if the priority of a is less than or equal to the priority of b.

def Mathlib.Tactic.GCongr.addGCongrLemmaEntry E1 #

Mathlib.Tactic.GCongr.GCongrLemmas → Mathlib.Tactic.GCongr.GCongrLemma → Mathlib.Tactic.GCongr.GCongrLemmas

Insert a GCongrLemma in a collection of lemmas, making sure they are sorted by priority.

opaque Mathlib.Tactic.GCongr.gcongrExt E1 #

Lean.SimpleScopedEnvExtension Mathlib.Tactic.GCongr.GCongrLemma Mathlib.Tactic.GCongr.GCongrLemmas

Environment extension for "generalized congruence" (gcongr) lemmas.

def Mathlib.Tactic.GCongr.findGCongrLemmas? E1 #

Lean.Name → Lean.Name → Lean.Name → ℕ → Lean.CoreM (List Mathlib.Tactic.GCongr.GCongrLemma)

Return the gcongr lemmas whose conclusion has the shape relName (lhs ..) (rhs ..), where arity is the number of arguments in ... This is used by the gcongr tactic.

def Mathlib.Tactic.GCongr.findGCongrLemmas?' E1 #

Lean.Name → Lean.Name → Bool → ℕ → Lean.CoreM (List Mathlib.Tactic.GCongr.GCongrLemma)

Get the gcongr lemmas whose conclusion has the shape relName (head ..) _, if forward := true, or rel _ (head ..), if forward := false. arity is the number of arguments in ... This is used by the grw tactic.

def Mathlib.Tactic.GCongr.getCongrAppFnArgs E1 #

Lean.Expr → Option (Lean.Name × Array Lean.Expr)

Given an application f a₁ .. aₙ, return the name of f, and the array of arguments aᵢ.

def Mathlib.Tactic.GCongr.getRel E1 #

Lean.Expr → Option (Lean.Name × Lean.Expr × Lean.Expr)

If e is of the form r a b, return (r, a, b). Note: we assume that e does not have an Expr.mdata annotation.

def Mathlib.Tactic.GCongr.updateRel E1 #

Lean.Expr → Lean.Expr → Bool → Lean.Expr

If r is of the form rel a b, replace either a or b with e.

def Mathlib.Tactic.GCongr.makeGCongrLemma E1 #

Array Lean.Expr → Lean.Expr → Lean.Name → ℕ → Bool → Bool → Lean.MetaM Mathlib.Tactic.GCongr.GCongrLemma

Try to construct the GCongrLemma for a lemma with hypotheses hyps and conclusion target.

def Mathlib.Tactic.GCongr.gcongrAttr E1 #

Lean.ParserDescr

Attribute marking "generalized congruence" (gcongr) lemmas. Such lemmas must have a conclusion of a form such as f x₁ y z₁ ∼ f x₂ y z₂; that is, a relation between the application of a function to two argument lists, in which the "varying argument" pairs (here x₁/x₂ and z₁/z₂) are all free variables. These lemmas are used by the gcongr and grw tactics.

The antecedents of such a lemma are classified as generating "main goals" if they are of the form x₁ ≈ x₂ for some "varying argument" pair x₁/x₂ (and a possibly different relation ≈ to ∼), or more generally of the form ∀ i h h' j h'', f₁ i j ≈ f₂ i j (say) for some "varying argument" pair f₁/f₂, where the arguments of f₁ and f₂ are the same list of variables which have to be bound by the preceding ∀. (Other antecedents are considered to generate "side goals".)

If a lemma such as add_le_add : a ≤ b → c ≤ d → a + c ≤ b + d has been tagged with gcongr, then a direct consequence like a ≤ b → a + c ≤ b + c does not need to be tagged. However, if a more specific lemma has fewer side conditions, it should also be tagged with gcongr. For example, mul_le_mul_of_nonneg_right and mul_le_mul_of_nonneg_left are both tagged.

  • gcongr only relaxes some checks that ensure that the lemma is suitable for use in grw. So, a lemma tagged gcongr only is not used by grw, but it may still be used by gcongr.
  • gcongr strict lets you tag lemmas where the conclusion relates two different constants, instead of a constant with itself. This allows grw to replace one constant with another while doing a rewrite. In particular, we use this to have special support for rewriting with strict inequalities (<). This is done by applying gcongr strict to lt_of_lt_of_le, which has the conclusion b ≤ c → a < c that relates LE.le with LT.lt (and similarly for lt_of_lt_of_le'). As a result, a rewrite with a strict inequality can turn < into ≤, or ≤ into <, depending on whether this appears in positive or negative position.

def Mathlib.Tactic.GCongr.addGCongrLemma E1 #

Lean.Name → Lean.Syntax → Lean.AttributeKind → Lean.MetaM Unit

Mark declName with the gcongr attribute.

def Lean.MVarId.applyRflOrId E1 #

Lean.MVarId → Lean.MetaM Unit

A version of the rfl tactic that also closes goals of the form ⊢ p → p.

def Mathlib.Tactic.GCongr.tacticGcongr_discharger E1 #

Lean.ParserDescr

gcongr_discharger is used by gcongr to discharge side goals.

This is an extensible tactic using macro_rules. By default it calls positivity (after importing the positivity tactic). Example: macro_rules | `(tactic| gcongr_discharger) => `(tactic| positivity).

def Mathlib.Tactic.GCongr.gcongrDischarger E1 #

Lean.MVarId → Lean.MetaM Unit

This is used as the default side-goal discharger, it calls the gcongr_discharger extensible tactic.

def Mathlib.Tactic.GCongr.exactRefl E1 #

Mathlib.Tactic.GCongr.ForwardExt

See if the term is a = b and the goal is a ∼ b or b ∼ a, with ∼ reflexive.

def Mathlib.Tactic.GCongr.exact E1 #

Mathlib.Tactic.GCongr.ForwardExt

def Lean.MVarId.gcongrForward E1 #

Array Lean.Expr → Lean.MVarId → Lean.MetaM Bool

Attempt to resolve an (implicitly) relational goal by one of a provided list of hypotheses, either with such a hypothesis directly or by a limited palette of relational forward-reasoning from these hypotheses.

def Mathlib.Tactic.GCongr.gcongrForwardDischarger E1 #

Lean.MVarId → Lean.MetaM Bool

This is used as the default main-goal discharger, consisting of running Lean.MVarId.gcongrForward (trying a term together with limited forward-reasoning on that term) on each nontrivial hypothesis.

def Mathlib.Tactic.GCongr.mkHoleAnnotation E1 #

Lean.Expr → Lean.Expr

Annotate e with a gcongrHole mdata annotation.

def Mathlib.Tactic.GCongr.hasHoleAnnotation E1 #

Lean.Expr → Bool

Check whether e has a gcongrHole mdata annotation.

def Mathlib.Tactic.GCongr.containsHoleAnnotation E1 #

Lean.Expr → Bool

Determine whether e contains a gcongrHole mdata annotation in any subexpression. This tells gcongr whether to continue applying gcongr lemmas.

def Mathlib.Tactic.GCongr.gcongrHoleExpand E1 #

Lean.ParserDescr

(Internal for gcongr) Elaborates to an expression satisfying hasHoleAnnotation.

def Mathlib.Tactic.GCongr.elabCHoleExpand E1 #

Lean.Elab.Term.TermElab

(Internal for gcongr) Elaborates to an expression satisfying hasHoleAnnotation.

theorem Mathlib.Tactic.GCongr.rel_imp_rel E1 #

∀ {α : Sort u_1} {r : α → α → Prop} [Trans r r r] {a b c d : α}, r c a → r b d → r a b → r c d

def Mathlib.Tactic.GCongr.relImpRelLemma E1 #

ℕ → List Mathlib.Tactic.GCongr.GCongrLemma

Construct a GCongrLemma for gcongr goals of the form a ≺ b → c ≺ d. This will be tried if there is no other available @[gcongr] lemma. For example, the relation a ≡ b [ZMOD n] has an instance of IsTrans, so a congruence of the form a ≡ b [ZMOD n] → c ≡ d [ZMOD n] can be solved with rel_imp_rel.

structure Mathlib.Tactic.GCongr.State E1 #

Type

The state used by GCongrM.

ctor Mathlib.Tactic.GCongr.State.mk E0 #

Array Lean.MVarId → List (Lean.TSyntax `rintroPat) → Mathlib.Tactic.GCongr.State

abbrev Mathlib.Tactic.GCongr.State.newGoals E1 #

Mathlib.Tactic.GCongr.State → Array Lean.MVarId

The new goals produced by gcongr. This includes side-goals and main goals. TODO?: split this into separate side and main goals, so that the side goals can be discharged using a nested gcongr by ... syntax.

abbrev Mathlib.Tactic.GCongr.State.patterns E1 #

Mathlib.Tactic.GCongr.State → List (Lean.TSyntax `rintroPat)

Patterns to use when doing intro.

structure Mathlib.Tactic.GCongr.Context E1 #

Type

The context used by GCongrM.

ctor Mathlib.Tactic.GCongr.Context.mk E0 #

(Lean.MVarId → Lean.MetaM Bool) → (Lean.MVarId → Lean.MetaM Unit) → Mathlib.Tactic.GCongr.Context

abbrev Mathlib.Tactic.GCongr.Context.mainGoalDischarger E1 #

Mathlib.Tactic.GCongr.Context → Lean.MVarId → Lean.MetaM Bool

The discharger for main goals.

abbrev Mathlib.Tactic.GCongr.Context.sideGoalDischarger E1 #

Mathlib.Tactic.GCongr.Context → Lean.MVarId → Lean.MetaM Unit

The discharger for side goals.

abbrev Mathlib.Tactic.GCongr.GCongrM E1 #

Type → Type

The monad used internally in the gcongr tactic.

def Mathlib.Tactic.GCongr.GCongrM.run E1 #

{α : Type} →
  Mathlib.Tactic.GCongr.GCongrM α →
    optParam (List (Lean.TSyntax `rintroPat)) [] →
      optParam (Lean.MVarId → Lean.MetaM Bool) Mathlib.Tactic.GCongr.gcongrForwardDischarger →
        optParam (Lean.MVarId → Lean.MetaM Unit) Mathlib.Tactic.GCongr.gcongrDischarger →
          Lean.MetaM (α × Mathlib.Tactic.GCongr.State)

Run a GCongrM computation in MetaM.

def Mathlib.Tactic.GCongr.pushNewGoal E1 #

Lean.MVarId → Mathlib.Tactic.GCongr.GCongrM Unit

Add an unsolved goal to the newGoals array in the state.

def Mathlib.Tactic.GCongr.dischargeSide E1 #

Lean.MVarId → Mathlib.Tactic.GCongr.GCongrM Unit

Run the discharger for side goals.

def Mathlib.Tactic.GCongr.applyGCongrLemma E1 #

Lean.MVarId →
  Mathlib.Tactic.GCongr.GCongrLemma → Mathlib.Tactic.GCongr.GCongrM (Array (Lean.MVarId × Bool) × Array Lean.MVarId)

Apply generalized congruence lemma lem to goal g, throwing an error if it fails. Return an array of main subgoals and an array of side goals. For each main subgoal, also return whether the sides of the relation are swapped (i.e. whether it is contravariant).

This function is used by both the gcongr and grw tactic. In the case of grw, one of the two sides of the goal is a metavariable that is filled in by this function.

In the main subgoals, the forall binders are introduced. They are named using the with clause of gcongr, or otherwise using binder names in the goal. grw relies on these names to ensure that binder names are preserved in the goal.

opaque Lean.MVarId.gcongr E1 #

Lean.MVarId → Option Bool → optParam ℕ 1000000 → Mathlib.Tactic.GCongr.GCongrM Bool

The core of the gcongr tactic. Parse a goal into the form (f _ ... _) ∼ (f _ ... _), look up any relevant @[gcongr] lemmas, try to apply them, recursively run the tactic itself on "main" goals which are generated, and run the discharger on side goals which are generated. If there is a user-provided template, first check that the template asks us to descend this far into the match.

def Mathlib.Tactic.GCongr.gcongr E1 #

Lean.ParserDescr

gcongr applies "generalized congruence" rules to recursively reduce a goal of form ⊢ R (f a₁ ... aₙ) (f b₁ ... bₙ) to (possibly multiple) goal(s) ⊢ Rᵢ aᵢ bᵢ, keeping only the distinct pairs aᵢ ≠ bᵢ, where Rᵢ is a possibly different relation (depending on the precise rule). The relations R, Rᵢ can be any two-argument relation, including · → ·.

This tactic is extensible: to add a "generalized congruence" rule, tag a theorem with the attribute @[gcongr].

If a "generalized congruence" lemma has a side goal, gcongr will try to discharge it using gcongr_discharger, which is an extensible tactic based on positivity. Side goals not discharged in this way are left for the user.

  • gcongr with x y ... z names the variables that are introduced by descending into binders (for example sums or suprema).
  • gcongr n, where n is a natural number literal, limits the depth of the recursive applications. This is useful if gcongr is too aggressive in breaking down the goal.
  • gcongr t, where t is a term with ?_ holes, performs congruence up to the holes in t. In other words, gcongr f ?_ turns a goal ⊢ R (f x) (f y) into ⊢ R x y (but no further). This is useful if gcongr is too aggressive in breaking down the goal.

Examples:

example {a b x c d : ℝ} (h1 : a + 1 ≤ b + 1) (h2 : c + 2 ≤ d + 2) :
    x ^ 2 * a + c ≤ x ^ 2 * b + d := by
  -- LHS and RHS both have the form x ^ 2 * ?_ + ?_
  gcongr
  · -- New goal: ⊢ a ≤ b
    linarith
  · -- ⊢ New goal: c ≤ d
    linarith
-- Resulting proof term is:
--   add_le_add (mul_le_mul_of_nonneg_left ?_ (Even.pow_nonneg (even_two_mul 1) x)) ?_
-- where `add_le_add` and `mul_le_mul_of_nonneg_left` are generalized congruence lemmas
-- and the side goal `0 ≤ x ^ 2` is discharged by `gcongr_discharger`.
example {a b c d x : ℝ} (h : a + c + 1 ≤ b + d + 1) :
    x ^ 2 * (a + c) + 5 ≤ x ^ 2 * (b + d) + 5 := by
  -- Using a pattern to limit the depth.
  gcongr x ^ 2 * ?_ + 5 -- or `gcongr 2`
  -- New goal: ⊢ a + c ≤ b + d
  linarith
-- Descending into binders (here: ⨆).
example {f g : ℕ → ℝ≥0∞} (h : ∀ n, f n ≤ g n) : ⨆ n, f n ≤ ⨆ n, g n := by
  gcongr with i
  exact h i

def Mathlib.Tactic.GCongr.runGCongr E1 #

Lean.MVarId → Option Lean.Term → Option (Lean.TSyntaxArray `rintroPat) → Lean.Elab.TermElabM Mathlib.Tactic.GCongr.State

Run the gcongr tactic.

def Mathlib.Tactic.GCongr.gconvert E1 #

Lean.ParserDescr

gconvert e, where the term e is inferred to have type t, replaces the main goal ⊢ t' with new goals for proving the implication t → t' using generalized congruence. The goals are created like gcongr would. Like gcongr, gconvert introduces variables while applying generalized congruence rules. Additionally, if a resulting goal is an implication, the hypothesis is introduced using this as the default name for the new hypothesis. These variables can be pattern-matched, like rintro would, using the with keyword.

gconvert can be used to peel matching quantifiers off of a given term and the goal and introduce the relevant variables.

gconvert is a generalized version of convert, in the same way that gcongr and grw are generalized versions of congr/congr! and rw.

  • gconvert e with x y ... z names the variables that are introduced.
  • gconvert e using n where n is a natural number literal, limits the depth of gcongr. This is useful if gcongr is too aggressive in breaking down the goal.
  • gconvert e using t, where t is a term with ?_ holes, makes gcongr perform congruence up to the holes in t. This is useful if gcongr is too aggressive in breaking down the goal.

Example:

example (h : ∀ ε > (0 : ℝ), ∃ N : ℕ, ∀ n ≥ N, 1 / (n + 1 : ℝ) < ε) :
             ∀ ε > (0 : ℝ), ∃ N : ℕ, ∀ n ≥ N, 1 / (n + 1 : ℝ) ≤ ε := by
  gconvert h with ε hε N n hn
  /-
  h : ∀ ε > 0, ∃ N, ∀ n ≥ N, 1 / (↑n + 1) < ε
  ε : ℝ
  hε : ε > 0
  N n : ℕ
  hn : n ≥ N
  this : 1 / (↑n + 1) < ε
  ⊢ 1 / (↑n + 1) ≤ ε
  -/
  exact this.le

def Mathlib.Tactic.GCongr.«tacticRel[_]» E1 #

Lean.ParserDescr

rel [h₁, ..., hₙ] uses "generalized congruence" rules to solve a goal of form ⊢ R (f a₁ ... aₙ) (f b₁ ... bₙ) by substituting with the terms hᵢ : Rᵢ aᵢ bᵢ. The relations R, Rᵢ can be any two-argument relation, including · → ·.

This tactic is extensible: to add a "generalized congruence" rule, tag a theorem with the attribute @[gcongr].

If a "generalized congruence" lemma has a side goal, rel will try to discharge it using gcongr_discharger, which is an extensible tactic based on positivity. If side goals cannot be discharged, or the terms h₁, ..., hₙ cannot solve the goals, the tactic fails.

Examples:

example {a b x c d : ℝ} (h1 : a ≤ b) (h2 : c ≤ d) :
    x ^ 2 * a + c ≤ x ^ 2 * b + d := by
  rel [h1, h2]
-- In this example we "substitute" the hypotheses `a ≤ b` and `c ≤ d` into the LHS `x ^ 2 * a + c`
-- of the goal and obtain the RHS `x ^ 2 * b + d`, thus proving the goal.
-- This constructs the proof term:
--   add_le_add (mul_le_mul_of_nonneg_left h1 (pow_bit0_nonneg x 1)) h2
-- using the generalized congruence lemmas `add_le_add` and `mul_le_mul_of_nonneg_left`.