Skip to content

Commit 78e98e0

Browse files
committed
chore(Logic/Equiv/Basic): golf using aesop / grind (#30909)
Lemmas about `Equiv.swap` are very heavy in casework, which is precisely what these tactics excel at.
1 parent ac3e932 commit 78e98e0

File tree

3 files changed

+18
-44
lines changed

3 files changed

+18
-44
lines changed

Mathlib/Data/Set/Prod.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Authors: Mario Carneiro, Johannes Hölzl, Patrick Massot
55
-/
66
import Mathlib.Data.Set.Image
77
import Mathlib.Data.SProd
8+
import Mathlib.Data.Sum.Basic
89

910
/-!
1011
# Sets in product and pi types

Mathlib/Logic/Embedding/Basic.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Authors: Johannes Hölzl, Mario Carneiro
66
import Mathlib.Data.Option.Basic
77
import Mathlib.Data.Prod.Basic
88
import Mathlib.Data.Prod.PProd
9+
import Mathlib.Data.Sum.Basic
910
import Mathlib.Logic.Equiv.Basic
1011

1112
/-!

Mathlib/Logic/Equiv/Basic.lean

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Copyright (c) 2015 Microsoft Corporation. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Leonardo de Moura, Mario Carneiro
55
-/
6-
import Mathlib.Data.Sum.Basic
76
import Mathlib.Logic.Equiv.Option
87
import Mathlib.Logic.Equiv.Sum
98
import Mathlib.Logic.Function.Conjugate
@@ -644,6 +643,7 @@ theorem swap_self (a : α) : swap a a = Equiv.refl _ :=
644643
theorem swap_comm (a b : α) : swap a b = swap b a :=
645644
ext fun r => swapCore_comm r _ _
646645

646+
@[aesop simp, grind =]
647647
theorem swap_apply_def (a b x : α) : swap a b x = if x = a then b else if x = b then a else x :=
648648
rfl
649649

@@ -653,10 +653,10 @@ theorem swap_apply_left (a b : α) : swap a b a = b :=
653653

654654
@[simp]
655655
theorem swap_apply_right (a b : α) : swap a b b = a := by
656-
by_cases h : b = a <;> simp [swap_apply_def, h]
656+
grind
657657

658658
theorem swap_apply_of_ne_of_ne {a b x : α} : x ≠ a → x ≠ b → swap a b x = x := by
659-
simp +contextual [swap_apply_def]
659+
grind
660660

661661
theorem eq_or_eq_of_swap_apply_ne_self {a b x : α} (h : swap a b x ≠ x) : x = a ∨ x = b := by
662662
contrapose! h
@@ -680,20 +680,17 @@ theorem swap_comp_apply {a b x : α} (π : Perm α) :
680680
cases π
681681
rfl
682682

683-
theorem swap_eq_update (i j : α) : (Equiv.swap i j : α → α) = update (update id j i) i j :=
684-
funext fun x => by rw [update_apply _ i j, update_apply _ j i, Equiv.swap_apply_def, id]
683+
theorem swap_eq_update (i j : α) : (Equiv.swap i j : α → α) = update (update id j i) i j := by
684+
aesop
685685

686686
theorem comp_swap_eq_update (i j : α) (f : α → β) :
687687
f ∘ Equiv.swap i j = update (update f j (f i)) i (f j) := by
688-
rw [swap_eq_update, comp_update, comp_update, comp_id]
688+
aesop
689689

690690
@[simp]
691691
theorem symm_trans_swap_trans [DecidableEq β] (a b : α) (e : α ≃ β) :
692-
(e.symm.trans (swap a b)).trans e = swap (e a) (e b) :=
693-
Equiv.ext fun x => by
694-
have : ∀ a, e.symm x = a ↔ x = e a := fun a => by grind
695-
simp only [trans_apply, swap_apply_def, this]
696-
split_ifs <;> simp
692+
(e.symm.trans (swap a b)).trans e = swap (e a) (e b) := by
693+
grind
697694

698695
@[simp]
699696
theorem trans_swap_trans_symm [DecidableEq β] (a b : β) (e : α ≃ β) :
@@ -707,45 +704,25 @@ theorem swap_apply_self (i j a : α) : swap i j (swap i j a) = a := by
707704
/-- A function is invariant to a swap if it is equal at both elements -/
708705
theorem apply_swap_eq_self {v : α → β} {i j : α} (hv : v i = v j) (k : α) :
709706
v (swap i j k) = v k := by
710-
by_cases hi : k = i
711-
· rw [hi, swap_apply_left, hv]
712-
by_cases hj : k = j
713-
· rw [hj, swap_apply_right, hv]
714-
rw [swap_apply_of_ne_of_ne hi hj]
707+
grind
715708

716709
theorem swap_apply_eq_iff {x y z w : α} : swap x y z = w ↔ z = swap x y w := by
717710
rw [apply_eq_iff_eq_symm_apply, symm_swap]
718711

719712
theorem swap_apply_ne_self_iff {a b x : α} : swap a b x ≠ x ↔ a ≠ b ∧ (x = a ∨ x = b) := by
720-
by_cases hab : a = b
721-
· simp [hab]
722-
by_cases hax : x = a
723-
· simp [hax, eq_comm]
724-
by_cases hbx : x = b
725-
· simp [hbx]
726-
simp [hab, hax, hbx, swap_apply_of_ne_of_ne]
713+
grind
727714

728715
namespace Perm
729716

730717
@[simp]
731718
theorem sumCongr_swap_refl {α β : Sort _} [DecidableEq α] [DecidableEq β] (i j : α) :
732719
Equiv.Perm.sumCongr (Equiv.swap i j) (Equiv.refl β) = Equiv.swap (Sum.inl i) (Sum.inl j) := by
733-
ext x
734-
cases x
735-
· simp only [Equiv.sumCongr_apply, Sum.map, coe_refl, comp_id, Sum.elim_inl, comp_apply,
736-
swap_apply_def, Sum.inl.injEq]
737-
split_ifs <;> rfl
738-
· simp [Sum.map, swap_apply_of_ne_of_ne]
720+
aesop
739721

740722
@[simp]
741723
theorem sumCongr_refl_swap {α β : Sort _} [DecidableEq α] [DecidableEq β] (i j : β) :
742724
Equiv.Perm.sumCongr (Equiv.refl α) (Equiv.swap i j) = Equiv.swap (Sum.inr i) (Sum.inr j) := by
743-
ext x
744-
cases x
745-
· simp [Sum.map, swap_apply_of_ne_of_ne]
746-
· simp only [Equiv.sumCongr_apply, Sum.map, coe_refl, comp_id, Sum.elim_inr, comp_apply,
747-
swap_apply_def, Sum.inr.injEq]
748-
split_ifs <;> rfl
725+
aesop
749726

750727
end Perm
751728

@@ -805,11 +782,8 @@ section
805782
def piCongrLeft' (P : α → Sort*) (e : α ≃ β) : (∀ a, P a) ≃ ∀ b, P (e.symm b) where
806783
toFun f x := f (e.symm x)
807784
invFun f x := (e.symm_apply_apply x).ndrec (f (e x))
808-
left_inv f := funext fun x =>
809-
(by rintro _ rfl; rfl : ∀ {y} (h : y = x), h.ndrec (f y) = f x) (e.symm_apply_apply x)
810-
right_inv f := funext fun x =>
811-
(by rintro _ rfl; rfl : ∀ {y} (h : y = x), (congr_arg e.symm h).ndrec (f y) = f x)
812-
(e.apply_symm_apply x)
785+
left_inv f := by grind
786+
right_inv f := by grind
813787

814788
/-- Note: the "obvious" statement `(piCongrLeft' P e).symm g a = g (e a)` doesn't typecheck: the
815789
LHS would have type `P a` while the RHS would have type `P (e.symm (e a))`. For that reason,
@@ -881,14 +855,12 @@ lemma piCongrLeft_apply_eq_cast {P : β → Sort v} {e : α ≃ β}
881855
theorem piCongrLeft_sumInl {ι ι' ι''} (π : ι'' → Type*) (e : ι ⊕ ι' ≃ ι'') (f : ∀ i, π (e (inl i)))
882856
(g : ∀ i, π (e (inr i))) (i : ι) :
883857
piCongrLeft π e (sumPiEquivProdPi (fun x => π (e x)) |>.symm (f, g)) (e (inl i)) = f i := by
884-
simp_rw [piCongrLeft_apply_eq_cast, sumPiEquivProdPi_symm_apply,
885-
sum_rec_congr _ _ _ (e.symm_apply_apply (inl i)), cast_cast, cast_eq]
858+
aesop
886859

887860
theorem piCongrLeft_sumInr {ι ι' ι''} (π : ι'' → Type*) (e : ι ⊕ ι' ≃ ι'') (f : ∀ i, π (e (inl i)))
888861
(g : ∀ i, π (e (inr i))) (j : ι') :
889862
piCongrLeft π e (sumPiEquivProdPi (fun x => π (e x)) |>.symm (f, g)) (e (inr j)) = g j := by
890-
simp_rw [piCongrLeft_apply_eq_cast, sumPiEquivProdPi_symm_apply,
891-
sum_rec_congr _ _ _ (e.symm_apply_apply (inr j)), cast_cast, cast_eq]
863+
aesop
892864

893865
end
894866

0 commit comments

Comments
 (0)