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

48 declarations. Mathlib docs · Source

The congr! tactic

This is a more powerful version of the congr tactic that knows about more congruence lemmas and can apply to more situations. It is similar to the congr' tactic from Mathlib 3.

The congr! tactic is used by the convert and convert_to tactics.

Detailed description

congr! equates pieces of the left-hand side of a goal to corresponding pieces of the right-hand side by recursively applying congruence lemmas. For example, with ⊢ f as = g bs we could get two goals ⊢ f = g and ⊢ as = bs.

Syntax:

congr!
congr! n
congr! with x y z
congr! n with x y z

Here, n is a natural number and x, y, z are rintro patterns (like h, rfl, ⟨x, y⟩, _, -, (h | h), etc.).

The congr! tactic is similar to congr but is more insistent in trying to equate left-hand sides to right-hand sides of goals. Here is an exhaustive list of things it can try:

  • If R in ⊢ R x y is a reflexive relation, it will convert the goal to ⊢ x = y if possible. The list of reflexive relations is maintained using the @[refl] attribute. As a special case, ⊢ p ↔ q is converted to ⊢ p = q during congruence processing and then returned to ⊢ p ↔ q form at the end.

  • If there is a user congruence lemma associated to the goal (for instance, a @[congr]-tagged lemma applying to ⊢ List.map f xs = List.map g ys), then it will use that.

  • It uses a congruence lemma generator at least as capable as the one used by congr and simp. If there is a subexpression that can be rewritten by simp, then congr! should be able to generate an equality for it.

  • It can do congruences of pi types using lemmas like implies_congr and pi_congr.

  • Before applying congruences, it will run the intros tactic automatically. The introduced variables can be given names using a with clause. This helps when congruence lemmas provide additional assumptions in hypotheses.

  • When there is an equality between functions, so long as at least one is obviously a lambda, we apply funext or Function.hfunext, which allows for congruence of lambda bodies.

  • It can try to close goals using a few strategies, including checking definitional equality, trying to apply Subsingleton.elim or proof_irrel_heq, and using the assumption tactic. Discharging is done at default transparency (unless specified otherwise), but this will become reducible transparency in the future.

The optional parameter is the depth of the recursive applications. This is useful when congr! is too aggressive in breaking down the goal. For example, given ⊢ f (g (x + y)) = f (g (y + x)), congr! produces the goals ⊢ x = y and ⊢ y = x, while congr! 2 produces the intended ⊢ x + y = y + x.

The congr! tactic also takes a configuration option, for example

congr! (transparency := .default) 2

This overrides the default, which is to apply congruence lemmas at reducible transparency.

The congr! tactic is aggressive with equating two sides of everything. There is a predefined configuration that uses a different strategy:

congr! (config := .unfoldSameFun)

This only allows congruences between functions applications of definitionally equal functions, and it applies congruence lemmas at default transparency (rather than just reducible). This is somewhat like congr.

See Congr!.Config for all options.

def CongrState.ctorIdx E1 #

CongrState → ℕ

theorem Congr!.Config.mk.injEq E0 #

∀ (closePre closePost : Bool) (transparency preTransparency postTransparency : Lean.Meta.TransparencyMode)
  (preferLHS partialApp sameFun : Bool) (maxArgs : Option ℕ)
  (typeEqs etaExpand useCongrSimp beqEq closePre_1 closePost_1 : Bool)
  (transparency_1 preTransparency_1 postTransparency_1 : Lean.Meta.TransparencyMode)
  (preferLHS_1 partialApp_1 sameFun_1 : Bool) (maxArgs_1 : Option ℕ)
  (typeEqs_1 etaExpand_1 useCongrSimp_1 beqEq_1 : Bool),
  ({ closePre := closePre, closePost := closePost, transparency := transparency, preTransparency := preTransparency,
        postTransparency := postTransparency, preferLHS := preferLHS, partialApp := partialApp, sameFun := sameFun,
        maxArgs := maxArgs, typeEqs := typeEqs, etaExpand := etaExpand, useCongrSimp := useCongrSimp, beqEq := beqEq } =
      { closePre := closePre_1, closePost := closePost_1, transparency := transparency_1,
        preTransparency := preTransparency_1, postTransparency := postTransparency_1, preferLHS := preferLHS_1,
        partialApp := partialApp_1, sameFun := sameFun_1, maxArgs := maxArgs_1, typeEqs := typeEqs_1,
        etaExpand := etaExpand_1, useCongrSimp := useCongrSimp_1, beqEq := beqEq_1 }) =
    (closePre = closePre_1 ∧
      closePost = closePost_1 ∧
        transparency = transparency_1 ∧
          preTransparency = preTransparency_1 ∧
            postTransparency = postTransparency_1 ∧
              preferLHS = preferLHS_1 ∧
                partialApp = partialApp_1 ∧
                  sameFun = sameFun_1 ∧
                    maxArgs = maxArgs_1 ∧
                      typeEqs = typeEqs_1 ∧ etaExpand = etaExpand_1 ∧ useCongrSimp = useCongrSimp_1 ∧ beqEq = beqEq_1)

theorem CongrState.mk.inj E1 #

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

def Congr!.Config.ctorIdx E1 #

Congr!.Config → ℕ

theorem CongrState.mk.injEq E0 #

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

theorem Congr!.Config.mk.inj E1 #

∀ {closePre closePost : Bool} {transparency preTransparency postTransparency : Lean.Meta.TransparencyMode}
  {preferLHS partialApp sameFun : Bool} {maxArgs : Option ℕ}
  {typeEqs etaExpand useCongrSimp beqEq closePre_1 closePost_1 : Bool}
  {transparency_1 preTransparency_1 postTransparency_1 : Lean.Meta.TransparencyMode}
  {preferLHS_1 partialApp_1 sameFun_1 : Bool} {maxArgs_1 : Option ℕ}
  {typeEqs_1 etaExpand_1 useCongrSimp_1 beqEq_1 : Bool},
  { closePre := closePre, closePost := closePost, transparency := transparency, preTransparency := preTransparency,
        postTransparency := postTransparency, preferLHS := preferLHS, partialApp := partialApp, sameFun := sameFun,
        maxArgs := maxArgs, typeEqs := typeEqs, etaExpand := etaExpand, useCongrSimp := useCongrSimp, beqEq := beqEq } =
      { closePre := closePre_1, closePost := closePost_1, transparency := transparency_1,
        preTransparency := preTransparency_1, postTransparency := postTransparency_1, preferLHS := preferLHS_1,
        partialApp := partialApp_1, sameFun := sameFun_1, maxArgs := maxArgs_1, typeEqs := typeEqs_1,
        etaExpand := etaExpand_1, useCongrSimp := useCongrSimp_1, beqEq := beqEq_1 } →
    closePre = closePre_1 ∧
      closePost = closePost_1 ∧
        transparency = transparency_1 ∧
          preTransparency = preTransparency_1 ∧
            postTransparency = postTransparency_1 ∧
              preferLHS = preferLHS_1 ∧
                partialApp = partialApp_1 ∧
                  sameFun = sameFun_1 ∧
                    maxArgs = maxArgs_1 ∧
                      typeEqs = typeEqs_1 ∧ etaExpand = etaExpand_1 ∧ useCongrSimp = useCongrSimp_1 ∧ beqEq = beqEq_1

structure Congr!.Config E1 #

Type

The configuration for the congr! tactic.

ctor Congr!.Config.mk E0 #

Bool →
  Bool →
    Lean.Meta.TransparencyMode →
      Lean.Meta.TransparencyMode →
        Lean.Meta.TransparencyMode → Bool → Bool → Bool → Option ℕ → Bool → Bool → Bool → Bool → Congr!.Config

abbrev Congr!.Config.closePre E1 #

Congr!.Config → Bool

If closePre := true, then try to close goals before applying congruence lemmas using tactics such as rfl and assumption. These tactics are applied with the transparency level specified by preTransparency, which is .reducible by default.

abbrev Congr!.Config.closePost E1 #

Congr!.Config → Bool

If closePost := true, then try to close goals that remain after no more congruence lemmas can be applied, using the same tactics as closePre. These tactics are applied with the transparency level specified by postTransparency, which is currently default transparency but in the future will become .reducible by default.

abbrev Congr!.Config.transparency E1 #

Congr!.Config → Lean.Meta.TransparencyMode

The transparency level to use when applying a congruence theorem. By default this is .reducible, which prevents unfolding of most definitions.

abbrev Congr!.Config.preTransparency E1 #

Congr!.Config → Lean.Meta.TransparencyMode

The transparency level to use when trying to close goals before applying congruence lemmas. This includes trying to prove the goal by rfl and using the assumption tactic. By default this is .reducible, which prevents unfolding of most definitions.

abbrev Congr!.Config.postTransparency E1 #

Congr!.Config → Lean.Meta.TransparencyMode

The transparency level to use when trying to close goals after no more congruence lemmas can be applied. This includes trying to prove the goal by rfl and using the assumption tactic. For backwards compatibility this is set to .default, which will be changed in the future to .reducible.

abbrev Congr!.Config.preferLHS E1 #

Congr!.Config → Bool

For passes that synthesize a congruence lemma using one side of the equality, we run the pass both for the left-hand side and the right-hand side. If preferLHS is true then we start with the left-hand side.

This can be used to control which side's definitions are expanded when applying the congruence lemma (if preferLHS = true then the RHS can be expanded).

abbrev Congr!.Config.partialApp E1 #

Congr!.Config → Bool

Allow both sides to be partial applications, and allow overapplications. When false, given an equality f a b = g x y z this means we never consider proving f a = g x y.

In this case, we might still consider f = g x if a pass generates a congruence lemma using the left-hand side. Use sameFun := true to ensure both sides are applications of the same function (making it be similar to the congr tactic).

abbrev Congr!.Config.sameFun E1 #

Congr!.Config → Bool

Whether to require that both sides of an equality be applications of defeq functions. That is, if true, f a = g x is only considered if f and g are defeq (making it be similar to the congr tactic).

abbrev Congr!.Config.maxArgs E1 #

Congr!.Config → Option ℕ

The maximum number of arguments to consider when doing congruence of function applications. For example, with f a b c = g w x y z, setting maxArgs := some 2 means it will only consider either f a b = g w x y and c = z or f a = g w x, b = y, and c = z. Setting maxArgs := none (the default) means no limit.

When the functions are dependent, maxArgs can prevent congruence from working at all. In Fintype.card α = Fintype.card β, one needs to have maxArgs at 2 or higher since there is a Fintype instance argument that depends on the first.

When there aren't such dependency issues, setting maxArgs := some 1 causes congr! to do congruence on a single argument at a time. This can be used in conjunction with the iteration limit to control exactly how many arguments are to be processed by congruence.

abbrev Congr!.Config.typeEqs E1 #

Congr!.Config → Bool

For type arguments that are implicit or have forward dependencies, whether or not congr! should generate equalities even if the types do not look plausibly equal.

We have a heuristic in the main congruence generator that types α and β are plausibly equal according to the following algorithm:

  • If the types are both propositions, they are plausibly equal (Iffs are plausible).
  • If the types are from different universes, they are not plausibly equal.
  • Suppose in whnf we have α = f a₁ ... aₘ and β = g b₁ ... bₘ. If f is not definitionally equal to g or m ≠ n, then α and β are not plausibly equal.
  • If there is some i such that aᵢ and bᵢ are not plausibly equal, then α and β are not plausibly equal.
  • Otherwise, α and β are plausibly equal.

The purpose of this is to prevent considering equalities like ℕ = ℤ while allowing equalities such as Fin n = Fin m or Subtype p = Subtype q (so long as these are subtypes of the same type).

The way this is implemented is that when the congruence generator is comparing arguments when looking at an equality of function applications, it marks a function parameter as "fixed" if the provided arguments are types that are not plausibly equal. The effect of this is that congruence succeeds only if those arguments are defeq at transparency transparency.

abbrev Congr!.Config.etaExpand E1 #

Congr!.Config → Bool

As a last pass, perform eta expansion of both sides of an equality. For example, this transforms a bare HAdd.hAdd into fun x y => x + y.

abbrev Congr!.Config.useCongrSimp E1 #

Congr!.Config → Bool

Whether to use the congruence generator that is used by simp and congr. This generator is more strict, and it does not respect all configuration settings. It does respect preferLHS, partialApp and maxArgs and transparency settings. It acts as if sameFun := true and it ignores typeEqs.

abbrev Congr!.Config.beqEq E1 #

Congr!.Config → Bool

Whether to use a special congruence lemma for BEq instances. This synthesizes LawfulBEq instances to discharge equalities of BEq instances.

def Congr!.Config.unfoldSameFun E1 #

Congr!.Config

A configuration option that makes congr! do the sorts of aggressive unfoldings that congr does while also similarly preventing congr! from considering partial applications or congruences between different functions being applied.

def Congr!.Config.numArgsOk E1 #

Congr!.Config → ℕ → Bool

Whether the given number of arguments is allowed to be considered.

def Congr!.Config.maxArgsFor E1 #

Congr!.Config → ℕ → ℕ

According to the configuration, how many of the arguments in numArgs should be considered.

def Congr!.plausiblyEqualTypes E1 #

Lean.Expr → Lean.Expr → optParam ℕ 5 → Lean.MetaM Bool

Returns whether or not it's reasonable to consider an equality between types ty1 and ty2. The heuristic is the following:

  • If ty1 and ty2 are in Prop, then yes.
  • If in whnf both ty1 and ty2 have the same head and if (recursively) it's reasonable to consider an equality between corresponding type arguments, then yes.
  • Otherwise, no.

This helps keep congr from going too far and generating hypotheses like ℝ = ℤ.

To keep things from going out of control, there is a maxDepth. Additionally, if we do the check with maxDepth = 0 then the heuristic answers "no".

def Lean.MVarId.smartHCongr? E1 #

Congr!.Config → Lean.MVarId → Lean.MetaM (Option (List Lean.MVarId))

This is like Lean.MVarId.hcongr? but (1) looks at both sides when generating the congruence lemma and (2) inserts additional hypotheses from equalities from previous arguments.

It uses Lean.Meta.mkRichHCongr to generate the congruence lemmas.

If the goal is an Eq, it uses eq_of_heq first.

As a backup strategy, it uses the LHS/RHS method like in Lean.MVarId.congrSimp? (where Congr!.Config.preferLHS determines which side to try first). This uses a particular side of the target, generates the congruence lemma, then tries applying it. This can make progress with higher transparency settings. To help the unifier, in this mode it assumes both sides have the exact same function.

def Lean.MVarId.congrSimp? E1 #

Congr!.Config → Lean.MVarId → Lean.MetaM (Option (List Lean.MVarId))

Like Lean.MVarId.congr? but instead of using only the congruence lemma associated to the LHS, it tries the RHS too, in the order specified by config.preferLHS.

It uses Lean.Meta.mkCongrSimp? to generate a congruence lemma, like in the congr tactic.

Applies the congruence generated congruence lemmas according to config.

def Lean.MVarId.userCongr? E1 #

Congr!.Config → Lean.MVarId → Lean.MetaM (Option (List Lean.MVarId))

Try applying user-provided congruence lemmas. If any are applicable, returns a list of new goals.

Tries a congruence lemma associated to the LHS and then, if that failed, the RHS.

def Lean.MVarId.congrPi? E1 #

Lean.MVarId → Lean.MetaM (Option (List Lean.MVarId))

Try to apply pi_congr. This is similar to Lean.MVar.congrImplies?.

def Lean.MVarId.obviousFunext? E1 #

Lean.MVarId → Lean.MetaM (Option (List Lean.MVarId))

Try to apply funext, but only if it is an equality of two functions where at least one is a lambda expression.

One thing this check prevents is accidentally applying funext to a set equality, but also when doing congruence we don't want to apply funext unnecessarily.

def Lean.MVarId.obviousHfunext? E1 #

Lean.MVarId → Lean.MetaM (Option (List Lean.MVarId))

Try to apply Function.hfunext, returning the new goals if it succeeds. Like Lean.MVarId.obviousFunext?, we only do so if at least one side of the HEq is a lambda. This prevents unfolding of things like Set.

Need to have Mathlib/Logic/Function/Basic.lean imported for this to succeed.

def Lean.MVarId.congrImplies?' E1 #

Lean.MVarId → Lean.MetaM (Option (List Lean.MVarId))

A version of Lean.MVarId.congrImplies? that uses implies_congr' instead of implies_congr.

def Lean.MVarId.subsingletonHelim? E1 #

Lean.MVarId → Lean.MetaM (Option (List Lean.MVarId))

Try to apply Subsingleton.helim if the goal is a HEq. Tries synthesizing a Subsingleton instance for both the LHS and the RHS.

If successful, this reduces proving @HEq α x β y to proving α = β.

def Lean.MVarId.beqInst? E1 #

Lean.MVarId → Lean.MetaM (Option (List Lean.MVarId))

Tries to apply lawful_beq_subsingleton to prove that two BEq instances are equal by synthesizing LawfulBEq instances for both.

def Lean.MVarId.congrPasses! E1 #

List (String × (Congr!.Config → Lean.MVarId → Lean.MetaM (Option (List Lean.MVarId))))

A list of all the congruence strategies used by Lean.MVarId.congrCore!.

ctor CongrState.mk E0 #

Array Lean.MVarId → List (Lean.TSyntax `rintroPat) → CongrState

structure CongrState E1 #

Type

abbrev CongrState.goals E1 #

CongrState → Array Lean.MVarId

Accumulated goals that congr! could not handle.

abbrev CongrState.patterns E1 #

CongrState → List (Lean.TSyntax `rintroPat)

Patterns to use when doing intro.

abbrev CongrMetaM E1 #

Type → Type

def CongrMetaM.nextPattern E1 #

CongrMetaM (Option (Lean.TSyntax `rintroPat))

Pop the next pattern from the current state.

def Lean.MVarId.introsClean E1 #

Lean.MVarId → CongrMetaM (List Lean.MVarId)

Does Lean.MVarId.intros but then cleans up the introduced hypotheses, removing anything that is trivial. If there are any patterns in the current CongrMetaM state then instead of Lean.MVarId.intros it does Lean.Elab..Tactic.RCases.rintro.

Cleaning up includes:

  • deleting hypotheses of the form x ≍ x, x = x, and x ↔ x.
  • deleting Prop hypotheses that are already in the local context.
  • converting x ≍ y to x = y if possible.
  • converting x = y to x ↔ y if possible.

def Lean.MVarId.preCongr! E1 #

Lean.MVarId → Bool → Lean.MetaM (Option Lean.MVarId)

Convert a goal into an Eq goal if possible (since we have a better shot at those). Also, if tryClose := true, then try to close the goal using an assumption, Subsingleton.Elim, or definitional equality.

def Lean.MVarId.congrCore! E1 #

Congr!.Config → Lean.MVarId → Lean.MetaM (Option (List Lean.MVarId))

def Lean.MVarId.postCongr! E1 #

Congr!.Config → Lean.MVarId → Lean.MetaM (Option Lean.MVarId)

A pass to clean up after Lean.MVarId.preCongr! and Lean.MVarId.congrCore!.

def Lean.MVarId.congrN! E1 #

Lean.MVarId →
  optParam (Option ℕ) none →
    optParam Congr!.Config { } → optParam (List (Lean.TSyntax `rintroPat)) [] → Lean.MetaM (List Lean.MVarId)

A more insistent version of Lean.MVarId.congrN. See the documentation on the congr! syntax.

The depth? argument controls the depth of the recursion. If none, then it uses a reasonably large bound that is linear in the expression depth.

def Congr!.elabConfig E1 #

Lean.Syntax → optParam Congr!.Config { } → optParam Bool true → Lean.Elab.Tactic.TacticM Congr!.Config

def Congr!.congr! E1 #

Lean.ParserDescr

congr! tries to prove the main goal by repeatedly applying congruence rules. For example, on a goal of the form ⊢ f a1 a2 ... = f b1 b2 ..., congr! will make new goals ⊢ a1 = b1, ⊢ a2 = b2, ...

congr! is a more powerful version of the congr tactic that uses congruence lemmas (tagged with @[congr]), reflexivity rules (tagged with @[refl]) and proof discharging strategies. The full list of congruence proof strategies is documented in the module Mathlib.Tactic.CongrExclamation. The congr! tactic is used by the convert and convert_to tactics.

  • congr! n, where n is a positive numeral, controls the depth with which congruence is applied. For example, if the main goal is n + n + 1 = 2 * n + 1, then congr! 1 results in one goal, ⊢ n + n = 2 * n, and congr! 2 (or more) results in two (impossible) goals ⊢ HAdd.hAdd = HMul.hMul and ⊢ n = 2. By default, the depth is unlimited.
  • congr! with x ⟨y₁, y₂⟩ (z₁ | z₂) names or pattern-matches the variables introduced by congruence rules, like rintro x ⟨y₁, y₂⟩ (z₁ | z₂) would.
  • congr! (config := cfg) uses the configuration options in cfg to control the congruence rules (see Congr!.Config).