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.
See the syntax docstring for more details.
The configuration for the congr! tactic.
- closePre : Bool
If
closePre := true, then try to close goals before applying congruence lemmas using tactics such asrflandassumption. These tactics are applied with the transparency level specified bypreTransparency, which is.reducible` by default. - closePost : Bool
- transparency : Lean.Meta.TransparencyMode
The transparency level to use when applying a congruence theorem. By default this is
.reducible, which prevents unfolding of most definitions. - preTransparency : 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
rfland using theassumptiontactic. By default this is.reducible, which prevents unfolding of most definitions. - preferLHS : 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
preferLHSistruethen 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 = truethen the RHS can be expanded). - partialApp : Bool
Allow both sides to be partial applications. When false, given an equality
f a b = g x y zthis means we never consider provingf a = g x y.In this case, we might still consider
f = g xif a pass generates a congruence lemma using the left-hand side. UsesameFun := trueto ensure both sides are applications of the same function (making it be similar to thecongrtactic). - sameFun : Bool
Whether to require that both sides of an equality be applications of defeq functions. That is, if true,
f a = g xis only considered iffandgare defeq (making it be similar to thecongrtactic). 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, settingmaxArgs := some 2means it will only consider eitherf a b = g w x yandc = zorf a = g w x,b = y, andc = z. SettingmaxArgs := none(the default) means no limit.When the functions are dependent,
maxArgscan prevent congruence from working at all. InFintype.card α = Fintype.card β, one needs to havemaxArgsat2or higher since there is aFintypeinstance argument that depends on the first.When there aren't such dependency issues, setting
maxArgs := some 1causescongr!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.- typeEqs : 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ₘ. Iffis not definitionally equal togorm ≠ n, thenαandβare not plausibly equal. - If there is some
isuch thataᵢandbᵢ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 asFin n = Fin morSubtype 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
transparencytransparency. - If the types are both propositions, they are plausibly equal (
- etaExpand : Bool
As a last pass, perform eta expansion of both sides of an equality. For example, this transforms a bare
HAdd.hAddintofun x y => x + y. - useCongrSimp : Bool
- beqEq : Bool
Instances For
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.
Equations
- Congr!.Config.unfoldSameFun = { transparency := Lean.Meta.TransparencyMode.default, preTransparency := Lean.Meta.TransparencyMode.default, partialApp := false, sameFun := true }
Instances For
According to the configuration, how many of the arguments in numArgs should be considered.
Equations
- config.maxArgsFor numArgs = min numArgs (config.maxArgs.getD numArgs)
Instances For
Returns whether or not it's reasonable to consider an equality between types ty1 and ty2.
The heuristic is the following:
- If
ty1andty2are inProp, then yes. - If in whnf both
ty1andty2have 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".
Equations
- One or more equations did not get rendered due to their size.
- Congr!.plausiblyEqualTypes ty1 ty2 0 = pure false
Instances For
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.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
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.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Like mkCongrSimp? but takes in a specific arity.
Equations
- One or more equations did not get rendered due to their size.
Instances For
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.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Try to apply pi_congr. This is similar to Lean.MVar.congrImplies?.
Equations
- mvarId.congrPi? = Lean.observing? do let __do_lift ← Lean.Meta.mkConstWithFreshMVarLevels `pi_congr Lean.Meta.withReducible (mvarId.apply __do_lift)
Instances For
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.
Equations
- One or more equations did not get rendered due to their size.
Instances For
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.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A version of Lean.MVarId.congrImplies? that uses implies_congr'
instead of implies_congr.
Equations
- One or more equations did not get rendered due to their size.
Instances For
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 α = β.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Tries to apply lawful_beq_subsingleton to prove that two BEq instances are equal
by synthesizing LawfulBEq instances for both.
Equations
- mvarId.beqInst? = Lean.observing? (Lean.Meta.withReducible (mvarId.applyConst `lawful_beq_subsingleton))
Instances For
A list of all the congruence strategies used by Lean.MVarId.congrCore!.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Conditionally runs a congruence strategy depending on the predicate b applied to the config.
Equations
Instances For
- goals : Array Lean.MVarId
Accumulated goals that
congr!could not handle. - patterns : List (Lean.TSyntax `rcasesPat)
Patterns to use when doing intro.
Instances For
Equations
Instances For
Pop the next pattern from the current state.
Equations
Instances For
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, andx ↔ x. - deleting Prop hypotheses that are already in the local context.
- converting
x ≍ ytox = yif possible. - converting
x = ytox ↔ yif possible.
Equations
- mvarId.introsClean = Lean.MVarId.introsClean.loop mvarId
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
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.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
A pass to clean up after Lean.MVarId.preCongr! and Lean.MVarId.congrCore!.
Equations
- One or more equations did not get rendered due to their size.
Instances For
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.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
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 a list of things it can try:
If
Rin⊢ R x yis a reflexive relation, it will convert the goal to⊢ x = yif possible. The list of reflexive relations is maintained using the@[refl]attribute. As a special case,⊢ p ↔ qis converted to⊢ p = qduring congruence processing and then returned to⊢ p ↔ qform 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
congrandsimp. If there is a subexpression that can be rewritten bysimp, thencongr!should be able to generate an equality for it.It can do congruences of pi types using lemmas like
implies_congrandpi_congr.Before applying congruences, it will run the
introstactic automatically. The introduced variables can be given names using awithclause. 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
funextorFunction.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.elimorproof_irrel_heq, and using theassumptiontactic.
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:
Try
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.
Equations
- One or more equations did not get rendered due to their size.