Skip to content

Commit 32616be

Browse files
committed
feat: make generalize_proofs handle proofs under binders (#12472)
The `generalize_proofs` tactic had a bug where it would not generalize proofs that appeared under binders correctly, creating terms with fvars from the wrong context. The tactic has been refactored, and it now abstracts the proofs (universally quantifying all the bound variables that appear in the proof) before lifting them to the local context. This feature can be controlled with the `abstract` option. Using `generalize_proofs (config := { abstract := false })` turns off proof abstraction and leaves alone those proofs that refer to bound variables. Other new features: - `generalize_proofs at h` for a let-bound local hypothesis `h` will clear its value. - `generalize_proofs at h` for a duplicate proposition will eliminate `h` from the local context. - Proofs are recursively generalized for each new local hypothesis. This can be turned off with `generalize_proofs (config := { maxDepth := 0 })`. - `generalize_proofs at h` will no longer generalize proofs from the goal. - The type of the generalized proof is carefully computed by propagating expected types, rather than by using `inferType`. This gives local hypotheses in more useful forms. (This PR serves as a followup to [this comment](#447 (comment)).)
1 parent 0e05cbe commit 32616be

File tree

7 files changed

+592
-142
lines changed

7 files changed

+592
-142
lines changed

Mathlib/Algebra/Category/GroupCat/Limits.lean

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,18 +520,14 @@ def kernelIsoKer {G H : AddCommGroupCat.{u}} (f : G ⟶ H) :
520520
refine equalizer.hom_ext ?_
521521
ext x
522522
dsimp
523-
generalize_proofs _ h1 h2
524-
erw [DFunLike.congr_fun (kernel.lift_ι f _ h1) ⟨_, h2⟩]
525-
rfl
523+
apply DFunLike.congr_fun (kernel.lift_ι f _ _)
526524
inv_hom_id := by
527525
apply AddCommGroupCat.ext
528526
simp only [AddMonoidHom.coe_mk, coe_id, coe_comp]
529527
rintro ⟨x, mem⟩
530528
refine Subtype.ext ?_
531529
simp only [ZeroHom.coe_mk, Function.comp_apply, id_eq]
532-
generalize_proofs _ h1 h2
533-
erw [DFunLike.congr_fun (kernel.lift_ι f _ h1) ⟨_, mem⟩]
534-
rfl
530+
apply DFunLike.congr_fun (kernel.lift_ι f _ _)
535531
set_option linter.uppercaseLean3 false in
536532
#align AddCommGroup.kernel_iso_ker AddCommGroupCat.kernelIsoKer
537533

Mathlib/AlgebraicGeometry/Morphisms/RingHomProperties.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ theorem RespectsIso.basicOpen_iff (hP : RespectsIso @P) {X Y : Scheme} [IsAffine
5757
congr
5858
delta IsLocalization.Away.map
5959
refine' IsLocalization.ringHom_ext (Submonoid.powers r) _
60-
generalize_proofs h1 h2 h3
60+
generalize_proofs
6161
haveI i1 := @isLocalization_away_of_isAffine X _ (Scheme.Γ.map f.op r)
6262
-- Porting note: needs to be very explicit here
6363
convert
64-
(@IsLocalization.map_comp (hy := h3) (Y.presheaf.obj <| Opposite.op (Scheme.basicOpen Y r))
64+
(@IsLocalization.map_comp (hy := ‹_ ≤ _›) (Y.presheaf.obj <| Opposite.op (Scheme.basicOpen Y r))
6565
_ _ (isLocalization_away_of_isAffine _) _ _ _ i1).symm using 1
6666
change Y.presheaf.map _ ≫ _ = _ ≫ X.presheaf.map _
6767
rw [f.val.c.naturality_assoc]

Mathlib/Combinatorics/SimpleGraph/Partition.lean

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ theorem partitionable_iff_colorable {n : ℕ} : G.Partitionable n ↔ G.Colorabl
147147
· rintro ⟨C⟩
148148
refine' ⟨C.toPartition, C.colorClasses_finite, le_trans _ (Fintype.card_fin n).le⟩
149149
generalize_proofs h
150-
haveI : Fintype C.colorClasses := C.colorClasses_finite.fintype
150+
change Set.Finite (Coloring.colorClasses C) at h
151+
have : Fintype C.colorClasses := C.colorClasses_finite.fintype
151152
rw [h.card_toFinset]
152153
exact C.card_colorClasses_le
153154
#align simple_graph.partitionable_iff_colorable SimpleGraph.partitionable_iff_colorable

Mathlib/Data/PFun.lean

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,9 @@ theorem fix_fwd {f : α →. Sum β α} {b : β} {a a' : α} (hb : b ∈ f.fix a
328328
def fixInduction {C : α → Sort*} {f : α →. Sum β α} {b : β} {a : α} (h : b ∈ f.fix a)
329329
(H : ∀ a', b ∈ f.fix a' → (∀ a'', Sum.inr a'' ∈ f a' → C a'') → C a') : C a := by
330330
have h₂ := (Part.mem_assert_iff.1 h).snd
331-
-- Porting note: revert/intro trick required to address `generalize_proofs` bug
332-
revert h₂
333-
generalize_proofs h₁
334-
intro h₂; clear h
335-
induction' h₁ with a ha IH
331+
generalize_proofs at h₂
332+
clear h
333+
induction' ‹Acc _ _› with a ha IH
336334
have h : b ∈ f.fix a := Part.mem_assert_iff.2 ⟨⟨a, ha⟩, h₂⟩
337335
exact H a h fun a' fa' => IH a' fa' (Part.mem_assert_iff.1 (fix_fwd h fa')).snd
338336
#align pfun.fix_induction PFun.fixInduction
@@ -341,9 +339,8 @@ theorem fixInduction_spec {C : α → Sort*} {f : α →. Sum β α} {b : β} {a
341339
(H : ∀ a', b ∈ f.fix a' → (∀ a'', Sum.inr a'' ∈ f a' → C a'') → C a') :
342340
@fixInduction _ _ C _ _ _ h H = H a h fun a' h' => fixInduction (fix_fwd h h') H := by
343341
unfold fixInduction
344-
-- Porting note: `generalize` required to address `generalize_proofs` bug
345-
generalize @fixInduction.proof_1 α β f b a h = ha
346-
induction ha
342+
generalize_proofs
343+
induction ‹Acc _ _›
347344
rfl
348345
#align pfun.fix_induction_spec PFun.fixInduction_spec
349346

Mathlib/NumberTheory/Cyclotomic/Gal.lean

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,10 @@ theorem autToPow_injective : Function.Injective <| hμ.autToPow K := by
5656
intro f g hfg
5757
apply_fun Units.val at hfg
5858
simp only [IsPrimitiveRoot.coe_autToPow_apply] at hfg
59-
-- Porting note: was `generalize_proofs hf' hg' at hfg`
60-
revert hfg
61-
generalize_proofs hf' hg'
62-
intro hfg
59+
generalize_proofs hf' hg' at hfg
6360
have hf := hf'.choose_spec
6461
have hg := hg'.choose_spec
65-
-- Porting note: was `generalize_proofs hζ at hf hg`
66-
revert hf hg
67-
generalize_proofs hζ
68-
intro hf hg
62+
generalize_proofs hζ at hf hg
6963
suffices f (hμ.toRootsOfUnity : Lˣ) = g (hμ.toRootsOfUnity : Lˣ) by
7064
apply AlgEquiv.coe_algHom_injective
7165
apply (hμ.powerBasis K).algHom_ext
@@ -154,13 +148,11 @@ noncomputable def fromZetaAut : L ≃ₐ[K] L :=
154148

155149
theorem fromZetaAut_spec : fromZetaAut hμ h (zeta n K L) = μ := by
156150
simp_rw [fromZetaAut, autEquivPow_symm_apply]
157-
-- Porting note: `generalize_proofs` did not generalize the same proofs, making the proof different.
158-
generalize_proofs h1 h2
159-
nth_rewrite 4 [← (zeta_spec n K L).powerBasis_gen K]
160-
have := Exists.choose_spec ((zeta_spec n K L).eq_pow_of_pow_eq_one hμ.pow_eq_one n.pos)
161-
rw [PowerBasis.equivOfMinpoly_gen, h1.powerBasis_gen K, ZMod.coe_unitOfCoprime,
162-
ZMod.val_cast_of_lt this.1]
163-
exact this.2
151+
generalize_proofs hζ h _ hμ _
152+
nth_rewrite 4 [← hζ.powerBasis_gen K]
153+
rw [PowerBasis.equivOfMinpoly_gen, hμ.powerBasis_gen K]
154+
convert h.choose_spec.2
155+
exact ZMod.val_cast_of_lt h.choose_spec.1
164156
#align is_cyclotomic_extension.from_zeta_aut_spec IsCyclotomicExtension.fromZetaAut_spec
165157

166158
end IsCyclotomicExtension

0 commit comments

Comments
 (0)