fact
stringlengths
6
3.84k
type
stringclasses
11 values
library
stringclasses
32 values
imports
listlengths
1
14
filename
stringlengths
20
95
symbolic_name
stringlengths
1
90
docstring
stringlengths
7
20k
exception' (msg : MessageData) : TacticM Unit := do try liftMetaTactic (exception (msg := msg)) catch _ => throwError msg set_option quotPrecheck false in
def
Tactic
[ "Mathlib.CategoryTheory.Bicategory.Coherence", "Mathlib.Tactic.CategoryTheory.BicategoricalComp" ]
Mathlib/Tactic/CategoryTheory/BicategoryCoherence.lean
exception'
Helper function for throwing exceptions with respect to the main goal.
bicategory_coherence (g : MVarId) : TermElabM Unit := g.withContext do withOptions (fun opts => synthInstance.maxSize.set opts (max 256 (synthInstance.maxSize.get opts))) do let thms := [``BicategoricalCoherence.iso, ``Iso.trans, ``Iso.symm, ``Iso.refl, ``Bicategory.whiskerRightIso, ``Bicategory.whiskerLeftIso].foldl (·.addDeclToUnfoldCore ·) {} let (ty, _) ← dsimp (← g.getType) (← Simp.mkContext (simpTheorems := #[thms])) let some (_, lhs, rhs) := (← whnfR ty).eq? | exception g "Not an equation of morphisms." let lift_lhs ← mkLiftMap₂LiftExpr lhs let lift_rhs ← mkLiftMap₂LiftExpr rhs let g₁ ← g.change (← mkEq lift_lhs lift_rhs) let [g₂] ← g₁.applyConst ``congrArg | exception g "congrArg failed in coherence" let [] ← g₂.applyConst ``Subsingleton.elim | exception g "This shouldn't happen; Subsingleton.elim does not create goals."
def
Tactic
[ "Mathlib.CategoryTheory.Bicategory.Coherence", "Mathlib.Tactic.CategoryTheory.BicategoricalComp" ]
Mathlib/Tactic/CategoryTheory/BicategoryCoherence.lean
bicategory_coherence
Auxiliary definition for `bicategorical_coherence`. -/ -- We could construct this expression directly without using `elabTerm`, -- but it would require preparing many implicit arguments by hand. def mkLiftMap₂LiftExpr (e : Expr) : TermElabM Expr := do Term.elabTerm (← ``((FreeBicategory.lift (Prefunctor.id _)).map₂ (LiftHom₂.lift $(← Term.exprToSyntax e)))) none /-- Coherence tactic for bicategories.
@[nolint unusedArguments] assoc_liftHom₂ {f g h i : a ⟶ b} [LiftHom f] [LiftHom g] [LiftHom h] (η : f ⟶ g) (θ : g ⟶ h) (ι : h ⟶ i) [LiftHom₂ η] [LiftHom₂ θ] : η ≫ θ ≫ ι = (η ≫ θ) ≫ ι := (Category.assoc _ _ _).symm
theorem
Tactic
[ "Mathlib.CategoryTheory.Bicategory.Coherence", "Mathlib.Tactic.CategoryTheory.BicategoricalComp" ]
Mathlib/Tactic/CategoryTheory/BicategoryCoherence.lean
assoc_liftHom₂
Coherence tactic for bicategories. Use `pure_coherence` instead, which is a frontend to this one. -/ elab "bicategory_coherence" : tactic => do bicategory_coherence (← getMainGoal) open Lean.Parser.Tactic /-- Simp lemmas for rewriting a 2-morphism into a normal form. -/ syntax (name := whisker_simps) "whisker_simps" optConfig : tactic @[inherit_doc whisker_simps] elab_rules : tactic | `(tactic| whisker_simps $cfg) => do evalTactic (← `(tactic| simp $cfg only [Category.assoc, Bicategory.comp_whiskerLeft, Bicategory.id_whiskerLeft, Bicategory.whiskerRight_comp, Bicategory.whiskerRight_id, Bicategory.whiskerLeft_comp, Bicategory.whiskerLeft_id, Bicategory.comp_whiskerRight, Bicategory.id_whiskerRight, Bicategory.whisker_assoc] )) -- We have unused typeclass arguments here. -- They are intentional, to ensure that `simp only [assoc_liftHom₂]` only left associates -- bicategorical structural morphisms. /-- Auxiliary simp lemma for the `coherence` tactic: this move brackets to the left in order to expose a maximal prefix built out of unitors and associators.
forEachComposition (e : Expr) (f : Expr → MetaM Unit) : MetaM Unit := do e.forEach (fun e ↦ if e.isAppOfArity ``CategoryStruct.comp 7 then f e else pure ())
def
Tactic
[ "Mathlib.CategoryTheory.Category.Basic" ]
Mathlib/Tactic/CategoryTheory/CheckCompositions.lean
forEachComposition
Find appearances of `CategoryStruct.comp C inst X Y Z f g`, and apply `f` to each.
checkComposition (e : Expr) : MetaM Unit := do match_expr e with | CategoryStruct.comp _ _ X Y Z f g => match_expr ← inferType f with | Quiver.Hom _ _ X' Y' => withReducibleAndInstances do if !(← isDefEq X' X) then logInfo m!"In composition\n {e}\nthe source of\n {f}\nis\n {X'}\nbut should be\n {X}" if !(← isDefEq Y' Y) then logInfo m!"In composition\n {e}\nthe target of\n {f}\nis\n {Y'}\nbut should be\n {Y}" | _ => throwError "In composition\n {e}\nthe type of\n {f}\nis not a morphism." match_expr ← inferType g with | Quiver.Hom _ _ Y' Z' => withReducibleAndInstances do if !(← isDefEq Y' Y) then logInfo m!"In composition\n {e}\nthe source of\n {g}\nis\n {Y'}\nbut should be\n {Y}" if !(← isDefEq Z' Z) then logInfo m!"In composition\n {e}\nthe target of\n {g}\nis\n {Z'}\nbut should be\n {Z}" | _ => throwError "In composition\n {e}\nthe type of\n {g}\nis not a morphism." | _ => throwError "{e} is not a composition."
def
Tactic
[ "Mathlib.CategoryTheory.Category.Basic" ]
Mathlib/Tactic/CategoryTheory/CheckCompositions.lean
checkComposition
Given a composition `CategoryStruct.comp _ _ X Y Z f g`, infer the types of `f` and `g` and check whether their sources and targets agree, at "instances and reducible" transparency, with `X`, `Y`, and `Z`, reporting any discrepancies.
checkCompositions (e : Expr) : MetaM Unit := do forEachComposition e checkComposition
def
Tactic
[ "Mathlib.CategoryTheory.Category.Basic" ]
Mathlib/Tactic/CategoryTheory/CheckCompositions.lean
checkCompositions
Check the typing of categorical compositions in an expression.
checkCompositionsTac : TacticM Unit := withMainContext do let e ← getMainTarget checkCompositions e
def
Tactic
[ "Mathlib.CategoryTheory.Category.Basic" ]
Mathlib/Tactic/CategoryTheory/CheckCompositions.lean
checkCompositionsTac
Check the typing of categorical compositions in the goal.
LiftObj (X : C) where protected lift : FreeMonoidalCategory C
class
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
LiftObj
A typeclass carrying a choice of lift of an object from `C` to `FreeMonoidalCategory C`. It must be the case that `projectObj id (LiftObj.lift x) = x` by defeq.
LiftObj_unit : LiftObj (𝟙_ C) := ⟨unit⟩
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
LiftObj_unit
null
LiftObj_tensor (X Y : C) [LiftObj X] [LiftObj Y] : LiftObj (X ⊗ Y) where lift := LiftObj.lift X ⊗ LiftObj.lift Y
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
LiftObj_tensor
null
LiftHom {X Y : C} [LiftObj X] [LiftObj Y] (f : X ⟶ Y) where protected lift : LiftObj.lift X ⟶ LiftObj.lift Y
class
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
LiftHom
A typeclass carrying a choice of lift of a morphism from `C` to `FreeMonoidalCategory C`. It must be the case that `projectMap id _ _ (LiftHom.lift f) = f` by defeq.
LiftHom_id (X : C) [LiftObj X] : LiftHom (𝟙 X) := ⟨𝟙 _⟩
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
LiftHom_id
null
LiftHom_left_unitor_hom (X : C) [LiftObj X] : LiftHom (λ_ X).hom where lift := (λ_ (LiftObj.lift X)).hom
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
LiftHom_left_unitor_hom
null
LiftHom_left_unitor_inv (X : C) [LiftObj X] : LiftHom (λ_ X).inv where lift := (λ_ (LiftObj.lift X)).inv
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
LiftHom_left_unitor_inv
null
LiftHom_right_unitor_hom (X : C) [LiftObj X] : LiftHom (ρ_ X).hom where lift := (ρ_ (LiftObj.lift X)).hom
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
LiftHom_right_unitor_hom
null
LiftHom_right_unitor_inv (X : C) [LiftObj X] : LiftHom (ρ_ X).inv where lift := (ρ_ (LiftObj.lift X)).inv
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
LiftHom_right_unitor_inv
null
LiftHom_associator_hom (X Y Z : C) [LiftObj X] [LiftObj Y] [LiftObj Z] : LiftHom (α_ X Y Z).hom where lift := (α_ (LiftObj.lift X) (LiftObj.lift Y) (LiftObj.lift Z)).hom
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
LiftHom_associator_hom
null
LiftHom_associator_inv (X Y Z : C) [LiftObj X] [LiftObj Y] [LiftObj Z] : LiftHom (α_ X Y Z).inv where lift := (α_ (LiftObj.lift X) (LiftObj.lift Y) (LiftObj.lift Z)).inv
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
LiftHom_associator_inv
null
LiftHom_comp {X Y Z : C} [LiftObj X] [LiftObj Y] [LiftObj Z] (f : X ⟶ Y) (g : Y ⟶ Z) [LiftHom f] [LiftHom g] : LiftHom (f ≫ g) where lift := LiftHom.lift f ≫ LiftHom.lift g
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
LiftHom_comp
null
liftHom_WhiskerLeft (X : C) [LiftObj X] {Y Z : C} [LiftObj Y] [LiftObj Z] (f : Y ⟶ Z) [LiftHom f] : LiftHom (X ◁ f) where lift := LiftObj.lift X ◁ LiftHom.lift f
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
liftHom_WhiskerLeft
null
liftHom_WhiskerRight {X Y : C} (f : X ⟶ Y) [LiftObj X] [LiftObj Y] [LiftHom f] {Z : C} [LiftObj Z] : LiftHom (f ▷ Z) where lift := LiftHom.lift f ▷ LiftObj.lift Z
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
liftHom_WhiskerRight
null
LiftHom_tensor {W X Y Z : C} [LiftObj W] [LiftObj X] [LiftObj Y] [LiftObj Z] (f : W ⟶ X) (g : Y ⟶ Z) [LiftHom f] [LiftHom g] : LiftHom (f ⊗ₘ g) where lift := LiftHom.lift f ⊗ₘ LiftHom.lift g
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
LiftHom_tensor
null
exception {α : Type} (g : MVarId) (msg : MessageData) : MetaM α := throwTacticEx `monoidal_coherence g msg
def
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
exception
Helper function for throwing exceptions.
exception' (msg : MessageData) : TacticM Unit := do try liftMetaTactic (exception (msg := msg)) catch _ => throwError msg
def
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
exception'
Helper function for throwing exceptions with respect to the main goal.
monoidal_coherence (g : MVarId) : TermElabM Unit := g.withContext do withOptions (fun opts => synthInstance.maxSize.set opts (max 512 (synthInstance.maxSize.get opts))) do let thms := [``MonoidalCoherence.iso, ``Iso.trans, ``Iso.symm, ``Iso.refl, ``MonoidalCategory.whiskerRightIso, ``MonoidalCategory.whiskerLeftIso].foldl (·.addDeclToUnfoldCore ·) {} let (ty, _) ← dsimp (← g.getType) (← Simp.mkContext (simpTheorems := #[thms])) let some (_, lhs, rhs) := (← whnfR ty).eq? | exception g "Not an equation of morphisms." let projectMap_lhs ← mkProjectMapExpr lhs let projectMap_rhs ← mkProjectMapExpr rhs let g₁ ← g.change (← mkEq projectMap_lhs projectMap_rhs) let [g₂] ← g₁.applyConst ``congrArg | exception g "congrArg failed in coherence" let [] ← g₂.applyConst ``Subsingleton.elim | exception g "This shouldn't happen; Subsingleton.elim does not create goals."
def
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
monoidal_coherence
Auxiliary definition for `monoidal_coherence`. -/ -- We could construct this expression directly without using `elabTerm`, -- but it would require preparing many implicit arguments by hand. def mkProjectMapExpr (e : Expr) : TermElabM Expr := do Term.elabTerm (← ``(FreeMonoidalCategory.projectMap _root_.id _ _ (LiftHom.lift $(← Term.exprToSyntax e)))) none /-- Coherence tactic for monoidal categories.
insertTrailingIds (g : MVarId) : MetaM MVarId := do let some (_, lhs, rhs) := (← withReducible g.getType').eq? | exception g "Not an equality." let mut g := g if !(lhs.isAppOf ``CategoryStruct.comp) then let [g'] ← g.applyConst ``insert_id_lhs | exception g "failed to apply insert_id_lhs" g := g' if !(rhs.isAppOf ``CategoryStruct.comp) then let [g'] ← g.applyConst ``insert_id_rhs | exception g "failed to apply insert_id_rhs" g := g' return g
def
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
insertTrailingIds
Coherence tactic for monoidal categories. Use `pure_coherence` instead, which is a frontend to this one. -/ elab "monoidal_coherence" : tactic => do monoidal_coherence (← getMainGoal) open Mathlib.Tactic.BicategoryCoherence /-- `pure_coherence` uses the coherence theorem for monoidal categories to prove the goal. It can prove any equality made up only of associators, unitors, and identities. ```lean example {C : Type} [Category C] [MonoidalCategory C] : (λ_ (𝟙_ C)).hom = (ρ_ (𝟙_ C)).hom := by pure_coherence ``` Users will typically just use the `coherence` tactic, which can also cope with identities of the form `a ≫ f ≫ b ≫ g ≫ c = a' ≫ f ≫ b' ≫ g ≫ c'` where `a = a'`, `b = b'`, and `c = c'` can be proved using `pure_coherence` -/ elab (name := pure_coherence) "pure_coherence" : tactic => do let g ← getMainGoal monoidal_coherence g <|> bicategory_coherence g /-- Auxiliary simp lemma for the `coherence` tactic: this moves brackets to the left in order to expose a maximal prefix built out of unitors and associators. -/ -- We have unused typeclass arguments here. -- They are intentional, to ensure that `simp only [assoc_LiftHom]` only left associates -- monoidal structural morphisms. @[nolint unusedArguments] lemma assoc_liftHom {W X Y Z : C} [LiftObj W] [LiftObj X] [LiftObj Y] (f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z) [LiftHom f] [LiftHom g] : f ≫ (g ≫ h) = (f ≫ g) ≫ h := (Category.assoc _ _ _).symm /-- Internal tactic used in `coherence`. Rewrites an equation `f = g` as `f₀ ≫ f₁ = g₀ ≫ g₁`, where `f₀` and `g₀` are maximal prefixes of `f` and `g` (possibly after reassociating) which are "liftable" (i.e. expressible as compositions of unitors and associators). -/ elab (name := liftable_prefixes) "liftable_prefixes" : tactic => do withOptions (fun opts => synthInstance.maxSize.set opts (max 256 (synthInstance.maxSize.get opts))) do evalTactic (← `(tactic| (simp -failIfUnchanged only [monoidalComp, bicategoricalComp, Category.assoc, BicategoricalCoherence.iso, MonoidalCoherence.iso, Iso.trans, Iso.symm, Iso.refl, MonoidalCategory.whiskerRightIso, MonoidalCategory.whiskerLeftIso, Bicategory.whiskerRightIso, Bicategory.whiskerLeftIso]) <;> (apply (cancel_epi (𝟙 _)).1 <;> try infer_instance) <;> (simp -failIfUnchanged only [assoc_liftHom, Mathlib.Tactic.BicategoryCoherence.assoc_liftHom₂]))) lemma insert_id_lhs {C : Type*} [Category C] {X Y : C} (f g : X ⟶ Y) (w : f ≫ 𝟙 _ = g) : f = g := by simpa using w lemma insert_id_rhs {C : Type*} [Category C] {X Y : C} (f g : X ⟶ Y) (w : f = g ≫ 𝟙 _) : f = g := by simpa using w /-- If either the lhs or rhs is not a composition, compose it on the right with an identity.
coherence_loop (maxSteps := 37) : TacticM Unit := match maxSteps with | 0 => exception' "`coherence` tactic reached iteration limit" | maxSteps' + 1 => do evalTactic (← `(tactic| pure_coherence)) <|> do evalTactic (← `(tactic| liftable_prefixes)) <|> exception' "Something went wrong in the `coherence` tactic: \ is the target an equation in a monoidal category?" liftMetaTactic MVarId.congrCore evalTactic (← `(tactic| { pure_coherence })) <|> exception' "`coherence` tactic failed, subgoal not true in the free monoidal category" evalTactic (← `(tactic| rfl)) <|> do liftMetaTactic' insertTrailingIds liftMetaTactic MVarId.congrCore evalTactic (← `(tactic| rfl)) <|> exception' "`coherence` tactic failed, non-structural morphisms don't match" coherence_loop maxSteps' open Lean.Parser.Tactic
def
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Free.Coherence", "Mathlib.Lean.Meta", "Mathlib.Tactic.CategoryTheory.BicategoryCoherence", "Mathlib.Tactic.CategoryTheory.MonoidalComp" ]
Mathlib/Tactic/CategoryTheory/Coherence.lean
coherence_loop
null
forall_congr_forget_Type (α : Type u) (p : α → Prop) : (∀ (x : (forget (Type u)).obj α), p x) ↔ ∀ (x : α), p x := Iff.rfl attribute [local instance] HasForget.instFunLike HasForget.hasCoeToSort
theorem
Tactic
[ "Mathlib.CategoryTheory.ConcreteCategory.Basic", "Mathlib.Util.AddRelatedDecl", "Batteries.Tactic.Lint" ]
Mathlib/Tactic/CategoryTheory/Elementwise.lean
forall_congr_forget_Type
null
forget_hom_Type (α β : Type u) (f : α ⟶ β) : DFunLike.coe f = f := rfl
theorem
Tactic
[ "Mathlib.CategoryTheory.ConcreteCategory.Basic", "Mathlib.Util.AddRelatedDecl", "Batteries.Tactic.Lint" ]
Mathlib/Tactic/CategoryTheory/Elementwise.lean
forget_hom_Type
null
hom_elementwise {C : Type*} [Category C] [HasForget C] {X Y : C} {f g : X ⟶ Y} (h : f = g) (x : X) : f x = g x := by rw [h]
theorem
Tactic
[ "Mathlib.CategoryTheory.ConcreteCategory.Basic", "Mathlib.Util.AddRelatedDecl", "Batteries.Tactic.Lint" ]
Mathlib/Tactic/CategoryTheory/Elementwise.lean
hom_elementwise
null
elementwiseThms : List Name := [ -- HasForget lemmas ``CategoryTheory.coe_id, ``CategoryTheory.coe_comp, ``CategoryTheory.comp_apply, ``CategoryTheory.id_apply, ``CategoryTheory.hom_id, ``CategoryTheory.hom_comp, ``id_eq, ``Function.comp_apply, ``forget_hom_Type, ``forall_congr_forget_Type, ``types_comp_apply, ``types_id_apply, ``forget_obj, ``ConcreteCategory.forget_map_eq_coe, ``coe_toHasForget_instFunLike, ``implies_true]
def
Tactic
[ "Mathlib.CategoryTheory.ConcreteCategory.Basic", "Mathlib.Util.AddRelatedDecl", "Batteries.Tactic.Lint" ]
Mathlib/Tactic/CategoryTheory/Elementwise.lean
elementwiseThms
List of simp lemmas to apply to the elementwise theorem.
elementwiseExpr (src : Name) (type pf : Expr) (simpSides := true) : MetaM (Expr × Option (Level × Level)) := do let type := (← instantiateMVars type).cleanupAnnotations forallTelescope type fun fvars type' => do mkHomElementwise type' (← mkExpectedTypeHint (mkAppN pf fvars) type') fun eqPf instConcr? => do let mut eqPf' ← simpType (simpOnlyNames elementwiseThms (config := { decide := false })) eqPf if (← inferType eqPf') == .const ``True [] then throwError "elementwise lemma for {src} is trivial after applying HasForget \ lemmas, which can be caused by how applications are unfolded. \ Using elementwise is unnecessary." if simpSides then let ctx ← Simp.Context.mkDefault let (ty', eqPf'') ← simpEq (fun e => return (← simp e ctx).1) (← inferType eqPf') eqPf' forallTelescope ty' fun _ ty' => do if let some (_, lhs, rhs) := ty'.eq? then if ← Batteries.Tactic.Lint.isSimpEq lhs rhs then throwError "applying simp to both sides reduces elementwise lemma for {src} \ to the trivial equality {ty'}. \ Either add `nosimp` or remove the `elementwise` attribute." eqPf' ← mkExpectedTypeHint eqPf'' ty' if let some (w, uF, insts) := instConcr? then return (← Meta.mkLambdaFVars (fvars.append insts) eqPf', (w, uF)) else return (← Meta.mkLambdaFVars fvars eqPf', none) where /-- Given an equality, extract a `Category` instance from it or raise an error. Returns the name of the category and its instance. -/ extractCatInstance (eqTy : Expr) : MetaM (Expr × Expr) := do let some (α, _, _) := eqTy.cleanupAnnotations.eq? | failure let (``Quiver.Hom, #[_, instQuiv, _, _]) := α.getAppFnArgs | failure let (``CategoryTheory.CategoryStruct.toQuiver, #[_, instCS]) := instQuiv.getAppFnArgs | failure let (``CategoryTheory.Category.toCategoryStruct, #[C, instC]) := instCS.getAppFnArgs | failure return (C, instC) mkHomElementwise {α} [Inhabited α] (eqTy eqPf : Expr) (k : Expr → Option (Level × Level × Array Expr) → MetaM α) : MetaM α := do let (C, instC) ← try extractCatInstance eqTy catch _ => throwError "elementwise expects equality of morphisms in a category" if let some eqPf' ← observing? (mkAppM ``hom_elementwise #[eqPf]) then k eqPf' none else let .app (.const ``Category [v, u]) _ ← inferType instC | throwError "internal error in elementwise: {← inferType instC}" let w ← mkFreshLevelMVar let uF ← mkFreshLevelMVar let fty (F carrier : Expr) : Expr := .forallE `X C (.forallE `Y C (mkApp3 (.const ``FunLike [.succ uF, .succ w, .succ w]) (mkApp2 F (.bvar 1) (.bvar 0)) (mkApp carrier (.bvar 1)) (mkApp carrier (.bvar 0))) default) default let cty (F carrier instFunLike : Expr) : Expr := ...
def
Tactic
[ "Mathlib.CategoryTheory.ConcreteCategory.Basic", "Mathlib.Util.AddRelatedDecl", "Batteries.Tactic.Lint" ]
Mathlib/Tactic/CategoryTheory/Elementwise.lean
elementwiseExpr
Given an equation `f = g` between morphisms `X ⟶ Y` in a category `C` (possibly after a `∀` binder), produce the equation `∀ (x : X), f x = g x` or `∀ [HasForget C] (x : X), f x = g x` as needed (after the `∀` binder), but with compositions fully right associated and identities removed. Returns the proof of the new theorem along with (optionally) a new level metavariable for the first universe parameter to `HasForget`. The `simpSides` option controls whether to simplify both sides of the equality, for simpNF purposes.
private partial mkUnusedName (names : List Name) (baseName : Name) : Name := if not (names.contains baseName) then baseName else let rec loop (i : Nat := 0) : Name := let w := Name.appendIndexAfter baseName i if names.contains w then loop (i + 1) else w loop 1
def
Tactic
[ "Mathlib.CategoryTheory.ConcreteCategory.Basic", "Mathlib.Util.AddRelatedDecl", "Batteries.Tactic.Lint" ]
Mathlib/Tactic/CategoryTheory/Elementwise.lean
mkUnusedName
Gives a name based on `baseName` that's not already in the list.
@[elementwise] some_lemma {C : Type*} [Category C] {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) (h : X ⟶ Z) (w : ...) : f ≫ g = h := ... ``` produces ```lean
lemma
Tactic
[ "Mathlib.CategoryTheory.ConcreteCategory.Basic", "Mathlib.Util.AddRelatedDecl", "Batteries.Tactic.Lint" ]
Mathlib/Tactic/CategoryTheory/Elementwise.lean
some_lemma
null
some_lemma_apply {C : Type*} [Category C] {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) (h : X ⟶ Z) (w : ...) [HasForget C] (x : X) : g (f x) = h x := ... ``` Here `X` is being coerced to a type via `CategoryTheory.HasForget.hasCoeToSort` and `f`, `g`, and `h` are being coerced to functions via `CategoryTheory.HasForget.hasCoeToFun`. Further, we simplify the type using `CategoryTheory.coe_id : ((𝟙 X) : X → X) x = x` and `CategoryTheory.coe_comp : (f ≫ g) x = g (f x)`, replacing morphism composition with function composition. The `[HasForget C]` argument will be omitted if it is possible to synthesize an instance. The name of the produced lemma can be specified with `@[elementwise other_lemma_name]`. If `simp` is added first, the generated lemma will also have the `simp` attribute. -/ syntax (name := elementwise) "elementwise" " nosimp"? (" (" &"attr" " := " Parser.Term.attrInstance,* ")")? : attr initialize registerBuiltinAttribute { name := `elementwise descr := "" applicationTime := .afterCompilation add := fun src ref kind => match ref with | `(attr| elementwise $[nosimp%$nosimp?]? $[(attr := $stx?,*)]?) => MetaM.run' do if (kind != AttributeKind.global) then throwError "`elementwise` can only be used as a global attribute" addRelatedDecl src "_apply" ref stx? fun type value levels => do let (newValue, level?) ← elementwiseExpr src type value (simpSides := nosimp?.isNone) let newLevels ← if let some (levelW, levelUF) := level? then do let w := mkUnusedName levels `w let uF := mkUnusedName levels `uF unless ← isLevelDefEq levelW (mkLevelParam w) do throwError "Could not create level parameter `w` for ConcreteCategory instance" unless ← isLevelDefEq levelUF (mkLevelParam uF) do throwError "Could not create level parameter `uF` for ConcreteCategory instance" pure <| uF :: w :: levels else pure levels pure (newValue, newLevels) | _ => throwUnsupportedSyntax }
lemma
Tactic
[ "Mathlib.CategoryTheory.ConcreteCategory.Basic", "Mathlib.Util.AddRelatedDecl", "Batteries.Tactic.Lint" ]
Mathlib/Tactic/CategoryTheory/Elementwise.lean
some_lemma_apply
null
argument if it can't synthesize a relevant `HasForget` instance. (Technical note: The forgetful functor's universe variable is instantiated with a fresh level metavariable in this case.) One difference between `elementwise_of%` and `@[elementwise]` is that `@[elementwise]` by default applies `simp` to both sides of the generated lemma to get something that is in simp normal form. `elementwise_of%` does not do this. -/ elab "elementwise_of% " t:term : term => do let e ← Term.elabTerm t none let (pf, _) ← elementwiseExpr .anonymous (← inferType e) e (simpSides := false) return pf syntax "elementwise" (ppSpace colGt ident)* : tactic syntax "elementwise!" (ppSpace colGt ident)* : tactic
instance
Tactic
[ "Mathlib.CategoryTheory.ConcreteCategory.Basic", "Mathlib.Util.AddRelatedDecl", "Batteries.Tactic.Lint" ]
Mathlib/Tactic/CategoryTheory/Elementwise.lean
argument
null
Iso.eq_whisker {C : Type*} [Category C] {X Y : C} {f g : X ≅ Y} (w : f = g) {Z : C} (h : Y ≅ Z) : f ≪≫ h = g ≪≫ h := by rw [w]
theorem
Tactic
[ "Mathlib.CategoryTheory.Iso" ]
Mathlib/Tactic/CategoryTheory/IsoReassoc.lean
Iso.eq_whisker
null
categoryIsoSimp (e : Expr) : MetaM Simp.Result := simpOnlyNames [``Iso.trans_symm, ``Iso.trans_refl, ``Iso.refl_trans, ``Iso.trans_assoc, ``Iso.symm_self_id, ``Iso.self_symm_id, ``Iso.symm_self_id_assoc, ``Iso.self_symm_id_assoc, ``Functor.mapIso_trans, ``Functor.mapIso_symm, ``Functor.mapIso_refl, ``Functor.id_obj] e (config := { decide := false })
def
Tactic
[ "Mathlib.CategoryTheory.Iso" ]
Mathlib/Tactic/CategoryTheory/IsoReassoc.lean
categoryIsoSimp
Simplify an expression using only the axioms of a groupoid.
reassocExprIso (e : Expr) : MetaM (Expr × Array MVarId) := do let lem₀ ← mkConstWithFreshMVarLevels ``Iso.eq_whisker let (args, _, _) ← forallMetaBoundedTelescope (← inferType lem₀) 7 let inst := args[1]! inst.mvarId!.setKind .synthetic let w := args[6]! w.mvarId!.assignIfDefEq e withEnsuringLocalInstance inst.mvarId! do return (← simpType categoryIsoSimp (mkAppN lem₀ args), #[inst.mvarId!]) initialize registerReassocExpr reassocExprIso
def
Tactic
[ "Mathlib.CategoryTheory.Iso" ]
Mathlib/Tactic/CategoryTheory/IsoReassoc.lean
reassocExprIso
Given an equation `f = g` between isomorphisms `X ≅ Y` in a category, produce the equation `∀ {Z} (h : Y ≅ Z), f ≪≫ h = g ≪≫ h`, but with compositions fully right associated, identities removed, and functors applied.
monoidalIso (X Y : C) [MonoidalCoherence X Y] : X ≅ Y := MonoidalCoherence.iso
abbrev
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
monoidalIso
A typeclass carrying a choice of monoidal structural isomorphism between two objects. Used by the `⊗≫` monoidal composition operator, and the `coherence` tactic. -/ -- We could likely turn this into a `Prop`-valued existential if that proves useful. class MonoidalCoherence (X Y : C) where /-- A monoidal structural isomorphism between two objects. -/ iso : X ≅ Y /-- Notation for identities up to unitors and associators. -/ scoped[CategoryTheory.MonoidalCategory] notation " ⊗𝟙 " => MonoidalCoherence.iso -- type as \ot 𝟙 /-- Construct an isomorphism between two objects in a monoidal category out of unitors and associators.
monoidalComp {W X Y Z : C} [MonoidalCoherence X Y] (f : W ⟶ X) (g : Y ⟶ Z) : W ⟶ Z := f ≫ ⊗𝟙.hom ≫ g @[inherit_doc monoidalComp] scoped[CategoryTheory.MonoidalCategory] infixr:80 " ⊗≫ " => monoidalComp -- type as \ot \gg
def
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
monoidalComp
Compose two morphisms in a monoidal category, inserting unitors and associators between as necessary.
monoidalIsoComp {W X Y Z : C} [MonoidalCoherence X Y] (f : W ≅ X) (g : Y ≅ Z) : W ≅ Z := f ≪≫ ⊗𝟙 ≪≫ g @[inherit_doc monoidalIsoComp] scoped[CategoryTheory.MonoidalCategory] infixr:80 " ≪⊗≫ " => monoidalIsoComp -- type as \ll \ot \gg
def
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
monoidalIsoComp
Compose two isomorphisms in a monoidal category, inserting unitors and associators between as necessary.
@[simps] refl (X : C) : MonoidalCoherence X X := ⟨Iso.refl _⟩ @[simps]
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
refl
null
whiskerLeft (X Y Z : C) [MonoidalCoherence Y Z] : MonoidalCoherence (X ⊗ Y) (X ⊗ Z) := ⟨whiskerLeftIso X ⊗𝟙⟩ @[simps]
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
whiskerLeft
null
whiskerRight (X Y Z : C) [MonoidalCoherence X Y] : MonoidalCoherence (X ⊗ Z) (Y ⊗ Z) := ⟨whiskerRightIso ⊗𝟙 Z⟩ @[simps]
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
whiskerRight
null
tensor_right (X Y : C) [MonoidalCoherence (𝟙_ C) Y] : MonoidalCoherence X (X ⊗ Y) := ⟨(ρ_ X).symm ≪≫ (whiskerLeftIso X ⊗𝟙)⟩ @[simps]
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
tensor_right
null
tensor_right' (X Y : C) [MonoidalCoherence Y (𝟙_ C)] : MonoidalCoherence (X ⊗ Y) X := ⟨whiskerLeftIso X ⊗𝟙 ≪≫ (ρ_ X)⟩ @[simps]
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
tensor_right'
null
left (X Y : C) [MonoidalCoherence X Y] : MonoidalCoherence (𝟙_ C ⊗ X) Y := ⟨λ_ X ≪≫ ⊗𝟙⟩ @[simps]
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
left
null
left' (X Y : C) [MonoidalCoherence X Y] : MonoidalCoherence X (𝟙_ C ⊗ Y) := ⟨⊗𝟙 ≪≫ (λ_ Y).symm⟩ @[simps]
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
left'
null
right (X Y : C) [MonoidalCoherence X Y] : MonoidalCoherence (X ⊗ 𝟙_ C) Y := ⟨ρ_ X ≪≫ ⊗𝟙⟩ @[simps]
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
right
null
right' (X Y : C) [MonoidalCoherence X Y] : MonoidalCoherence X (Y ⊗ 𝟙_ C) := ⟨⊗𝟙 ≪≫ (ρ_ Y).symm⟩ @[simps]
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
right'
null
assoc (X Y Z W : C) [MonoidalCoherence (X ⊗ (Y ⊗ Z)) W] : MonoidalCoherence ((X ⊗ Y) ⊗ Z) W := ⟨α_ X Y Z ≪≫ ⊗𝟙⟩ @[simps]
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
assoc
null
assoc' (W X Y Z : C) [MonoidalCoherence W (X ⊗ (Y ⊗ Z))] : MonoidalCoherence W ((X ⊗ Y) ⊗ Z) := ⟨⊗𝟙 ≪≫ (α_ X Y Z).symm⟩
instance
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
assoc'
null
@[simp] monoidalComp_refl {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) : f ⊗≫ g = f ≫ g := by simp [monoidalComp]
lemma
Tactic
[ "Mathlib.CategoryTheory.Monoidal.Category" ]
Mathlib/Tactic/CategoryTheory/MonoidalComp.lean
monoidalComp_refl
null
eq_whisker' {C : Type*} [Category C] {X Y : C} {f g : X ⟶ Y} (w : f = g) {Z : C} (h : Y ⟶ Z) : f ≫ h = g ≫ h := by rw [w]
theorem
Tactic
[ "Mathlib.CategoryTheory.Functor.Basic", "Mathlib.Lean.Meta.Simp", "Mathlib.Tactic.Simps.Basic", "Mathlib.Util.AddRelatedDecl" ]
Mathlib/Tactic/CategoryTheory/Reassoc.lean
eq_whisker'
A variant of `eq_whisker` with a more convenient argument order for use in tactics.
categorySimp (e : Expr) : MetaM Simp.Result := simpOnlyNames [``Category.comp_id, ``Category.id_comp, ``Category.assoc, ``Functor.id_obj, ``Functor.id_map, ``Functor.comp_obj, ``Functor.comp_map] e (config := { decide := false })
def
Tactic
[ "Mathlib.CategoryTheory.Functor.Basic", "Mathlib.Lean.Meta.Simp", "Mathlib.Tactic.Simps.Basic", "Mathlib.Util.AddRelatedDecl" ]
Mathlib/Tactic/CategoryTheory/Reassoc.lean
categorySimp
Simplify an expression using only the axioms of a category.
reassocExprHom (e : Expr) : MetaM (Expr × Array MVarId) := do let lem₀ ← mkConstWithFreshMVarLevels ``eq_whisker' let (args, _, _) ← forallMetaBoundedTelescope (← inferType lem₀) 7 let inst := args[1]! inst.mvarId!.setKind .synthetic let w := args[6]! w.mvarId!.assignIfDefEq e withEnsuringLocalInstance inst.mvarId! do return (← simpType categorySimp (mkAppN lem₀ args), #[inst.mvarId!])
def
Tactic
[ "Mathlib.CategoryTheory.Functor.Basic", "Mathlib.Lean.Meta.Simp", "Mathlib.Tactic.Simps.Basic", "Mathlib.Util.AddRelatedDecl" ]
Mathlib/Tactic/CategoryTheory/Reassoc.lean
reassocExprHom
Given an equation `f = g` between morphisms `X ⟶ Y` in a category, produce the equation `∀ {Z} (h : Y ⟶ Z), f ≫ h = g ≫ h`, but with compositions fully right associated and identities removed. Also returns the category `C` and any instance metavariables that need to be solved for.
registerReassocExpr (f : Expr → MetaM (Expr × Array MVarId)) : IO Unit := do reassocImplRef.modify (·.push f)
def
Tactic
[ "Mathlib.CategoryTheory.Functor.Basic", "Mathlib.Lean.Meta.Simp", "Mathlib.Tactic.Simps.Basic", "Mathlib.Util.AddRelatedDecl" ]
Mathlib/Tactic/CategoryTheory/Reassoc.lean
registerReassocExpr
Adding `@[reassoc]` to a lemma named `F` of shape `∀ .., f = g`, where `f g : X ⟶ Y` are morphisms in some category, will create a new lemma named `F_assoc` of shape `∀ .. {Z : C} (h : Y ⟶ Z), f ≫ h = g ≫ h` but with the conclusions simplified using the axioms for a category (`Category.comp_id`, `Category.id_comp`, and `Category.assoc`). So, for example, if the conclusion of `F` is `a ≫ b = g` then the conclusion of `F_assoc` will be `a ≫ (b ≫ h) = g ≫ h` (note that `≫` reassociates to the right so the brackets will not appear in the statement). This attribute is useful for generating lemmas which the simplifier can use even on expressions that are already right associated. Note that if you want both the lemma and the reassociated lemma to be `simp` lemmas, you should tag the lemma `@[reassoc (attr := simp)]`. The variant `@[simp, reassoc]` on a lemma `F` will tag `F` with `@[simp]`, but not `F_assoc` (this is sometimes useful). This attribute also works for lemmas of shape `∀ .., f = g` where `f g : X ≅ Y` are isomorphisms, provided that `Tactic.CategoryTheory.IsoReassoc` has been imported. -/ syntax (name := reassoc) "reassoc" (" (" &"attr" " := " Parser.Term.attrInstance,* ")")? : attr /-- IO ref for reassociation handlers `reassoc` attribute, so that it can be extended with additional handlers. Handlers take a proof of the equation. The default handler is `reassocExprHom` for morphism reassociation. This will be extended in `Tactic.CategoryTheory.IsoReassoc` for isomorphism reassociation. -/ private initialize reassocImplRef : IO.Ref (Array (Expr → MetaM (Expr × Array MVarId))) ← IO.mkRef #[reassocExprHom] /-- Registers a handler for `reassocExpr`. The handler takes a proof of an equation and returns a proof of the reassociation lemma. Handlers are considered in order of registration. They are applied directly to the equation in the body of the forall.
reassocExpr (pf : Expr) (type? : Option Expr) : MetaM (Expr × Array MVarId) := do let pf ← if let some type := type? then mkExpectedTypeHint pf type else pure pf forallTelescopeReducing (← inferType pf) fun xs _ => do let pf := mkAppN pf xs let handlers ← reassocImplRef.get let (pf, insts) ← handlers.firstM (fun h => h pf) <|> do throwError "`reassoc` can only be used on terms about equality of (iso)morphisms" return (← mkLambdaFVars xs pf, insts)
def
Tactic
[ "Mathlib.CategoryTheory.Functor.Basic", "Mathlib.Lean.Meta.Simp", "Mathlib.Tactic.Simps.Basic", "Mathlib.Util.AddRelatedDecl" ]
Mathlib/Tactic/CategoryTheory/Reassoc.lean
reassocExpr
Reassociates the morphisms in `type?` using the registered handlers, using `reassocExprHom` as the default. If `type?` is not given, it is assumed to be the type of `pf`. Returns the proof of the lemma along with instance metavariables that need synthesis.
reassocExpr' (pf : Expr) (type? : Option Expr) : TermElabM Expr := do let (e, insts) ← reassocExpr pf type? for inst in insts do inst.withContext do unless ← Term.synthesizeInstMVarCore inst do Term.registerSyntheticMVarWithCurrRef inst (.typeClass none) return e initialize registerBuiltinAttribute { name := `reassoc descr := "" applicationTime := .afterCompilation add := fun src ref kind => match ref with | `(attr| reassoc $[(attr := $stx?,*)]?) => MetaM.run' do if (kind != AttributeKind.global) then throwError "`reassoc` can only be used as a global attribute" addRelatedDecl src "_assoc" ref stx? fun type value levels => do Term.TermElabM.run' <| Term.withSynthesize do let pf ← reassocExpr' value type pure (pf, levels) | _ => throwUnsupportedSyntax }
def
Tactic
[ "Mathlib.CategoryTheory.Functor.Basic", "Mathlib.Lean.Meta.Simp", "Mathlib.Tactic.Simps.Basic", "Mathlib.Util.AddRelatedDecl" ]
Mathlib/Tactic/CategoryTheory/Reassoc.lean
reassocExpr'
Version of `reassocExpr` for the `TermElabM` monad. Handles instance metavariables automatically.
evalSlice (a b : Nat) : TacticM Unit := do let _ ← iterateUntilFailureWithResults do evalTactic (← `(conv| rw [Category.assoc])) iterateRange (a - 1) (a - 1) do evalTactic (← `(conv| congr)) evalTactic (← `(tactic| rotate_left)) let k ← iterateUntilFailureCount <| evalTactic (← `(conv| rw [← Category.assoc])) let c := k+1+a-b iterateRange c c <| evalTactic (← `(conv| congr)) let _ ← iterateUntilFailureWithResults do evalTactic (← `(conv| rw [Category.assoc]))
def
Tactic
[ "Mathlib.CategoryTheory.Category.Basic", "Mathlib.Tactic.Conv" ]
Mathlib/Tactic/CategoryTheory/Slice.lean
evalSlice
`slice` is a conv tactic; if the current focus is a composition of several morphisms, `slice a b` reassociates as needed, and zooms in on the `a`-th through `b`-th morphisms. Thus if the current focus is `(a ≫ b) ≫ ((c ≫ d) ≫ e)`, then `slice 2 3` zooms to `b ≫ c`. -/ syntax (name := slice) "slice " num ppSpace num : conv /-- `evalSlice` - rewrites the target expression using `Category.assoc`. - uses `congr` to split off the first `a-1` terms and rotates to `a`-th (last) term - counts the number `k` of rewrites as it uses `←Category.assoc` to bring the target to left associated form; from the first step this is the total number of remaining terms from `C` - it now splits off `b-a` terms from target using `congr` leaving the desired subterm - finally, it rewrites it once more using `Category.assoc` to bring it to right-associated normal form
catAppSimp (e : Expr) : MetaM Simp.Result := simpOnlyNames [ ``Cat.whiskerLeft_app, ``Cat.whiskerRight_app, ``Cat.id_app, ``Cat.comp_app, ``Cat.eqToHom_app] e (config := { decide := false })
def
Tactic
[ "Mathlib.CategoryTheory.Category.Cat", "Mathlib.Util.AddRelatedDecl" ]
Mathlib/Tactic/CategoryTheory/ToApp.lean
catAppSimp
Simplify an expression in `Cat` using basic properties of `NatTrans.app`.
toCatExpr (e : Expr) : MetaM Expr := do let (args, binderInfos, conclusion) ← forallMetaTelescope (← inferType e) let B ← match conclusion.getAppFnArgs with | (`Eq, #[_, η, _]) => match (← inferType η).getAppFnArgs with | (`Quiver.Hom, #[_, _, f, _]) => match (← inferType f).getAppFnArgs with | (`Quiver.Hom, #[_, _, a, _]) => inferType a | _ => throwError "The conclusion {conclusion} is not an equality of 2-morphisms!" | _ => throwError "The conclusion {conclusion} is not an equality of 2-morphisms!" | _ => throwError "The conclusion {conclusion} is not an equality!" let u ← mkFreshLevelMVar let v ← mkFreshLevelMVar let _ ← isDefEq B (.const ``Cat [v, u]) let some inst ← args.findM? fun x => do return (← inferType x).getAppFnArgs == (`CategoryTheory.Bicategory, #[B]) | throwError "Cannot find the argument for the bicategory instance of the bicategory in which \ the equality is taking place." let _ ← isDefEq inst (.const ``CategoryTheory.Cat.bicategory [v, u]) let value := mkAppN e args let rec /-- Recursive function which applies `mkLambdaFVars` stepwise (so that each step can have different binderinfos) -/ apprec (i : Nat) (e : Expr) : MetaM Expr := do if h : i < args.size then let arg := args[i] let bi := binderInfos[i]! let e' ← apprec (i + 1) e unless arg != B && arg != inst do return e' mkLambdaFVars #[arg] e' (binderInfoForMVars := bi) else return e let value ← apprec 0 value return value
def
Tactic
[ "Mathlib.CategoryTheory.Category.Cat", "Mathlib.Util.AddRelatedDecl" ]
Mathlib/Tactic/CategoryTheory/ToApp.lean
toCatExpr
Given a term of type `∀ ..., η = θ`, where `η θ : f ⟶ g` are 2-morphisms in some bicategory `B`, which is bound by the `∀` binder, get the corresponding equation in the bicategory `Cat`. It is important here that the levels in the term are level metavariables, as otherwise these will not be reassignable to the corresponding levels of `Cat`.
toAppExpr (e : Expr) : MetaM Expr := do mapForallTelescope (fun e => do simpType catAppSimp (← mkAppM ``NatTrans.congr_app #[e])) e
def
Tactic
[ "Mathlib.CategoryTheory.Category.Cat", "Mathlib.Util.AddRelatedDecl" ]
Mathlib/Tactic/CategoryTheory/ToApp.lean
toAppExpr
Given morphisms `f g : C ⟶ D` in the bicategory `Cat`, and an equation `η = θ` between 2-morphisms (possibly after a `∀` binder), produce the equation `∀ (X : C), f.app X = g.app X`, and simplify it using basic lemmas about `NatTrans.app`.
@[inline] modifyTodo (f : Array TodoEntry → Array TodoEntry) : CCM Unit := modify fun cc => { cc with todo := f cc.todo }
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
modifyTodo
Update the `todo` field of the state.
@[inline] modifyACTodo (f : Array ACTodoEntry → Array ACTodoEntry) : CCM Unit := modify fun cc => { cc with acTodo := f cc.acTodo }
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
modifyACTodo
Update the `acTodo` field of the state.
@[inline] getTodo : CCM (Array TodoEntry) := do return (← get).todo
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
getTodo
Read the `todo` field of the state.
@[inline] getACTodo : CCM (Array ACTodoEntry) := do return (← get).acTodo
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
getACTodo
Read the `acTodo` field of the state.
pushTodo (lhs rhs : Expr) (H : EntryExpr) (heqProof : Bool) : CCM Unit := do modifyTodo fun todo => todo.push (lhs, rhs, H, heqProof)
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
pushTodo
Add a new entry to the end of the todo list. See also `pushEq`, `pushHEq` and `pushReflEq`.
@[inline] pushEq (lhs rhs : Expr) (H : EntryExpr) : CCM Unit := pushTodo lhs rhs H false
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
pushEq
Add the equality proof `H : lhs = rhs` to the end of the todo list.
@[inline] pushHEq (lhs rhs : Expr) (H : EntryExpr) : CCM Unit := pushTodo lhs rhs H true
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
pushHEq
Add the heterogeneous equality proof `H : lhs ≍ rhs` to the end of the todo list.
@[inline] pushReflEq (lhs rhs : Expr) : CCM Unit := pushEq lhs rhs .refl
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
pushReflEq
Add `rfl : lhs = rhs` to the todo list.
addOccurrence (parent child : Expr) (symmTable : Bool) : CCM Unit := do let childRoot ← getRoot child modify fun ccs => { ccs with parents := ccs.parents.alter childRoot fun ps? => let ps := ps?.getD ∅ ps.insert { expr := parent, symmTable } }
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
addOccurrence
Update the `child` so its parent becomes `parent`.
propagateInstImplicit (e : Expr) : CCM Unit := do let type ← inferType e let type ← normalize type match (← get).instImplicitReprs[type]? with | some l => for e' in l do if ← pureIsDefEq e e' then pushReflEq e e' return modify fun ccs => { ccs with instImplicitReprs := ccs.instImplicitReprs.insert type (e :: l) } | none => modify fun ccs => { ccs with instImplicitReprs := ccs.instImplicitReprs.insert type [e] }
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
propagateInstImplicit
Record the instance `e` and add it to the set of known defeq instances.
mkCongruencesKey (e : Expr) : CCM CongruencesKey := do let .app f a := e | failure if (← getEntry e).any Entry.fo then e.withApp fun fn args => do return .fo (← getRoot fn) (← args.mapM getRoot) else return .ho (← getRoot f) (← getRoot a)
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
mkCongruencesKey
Return the `CongruencesKey` associated with an expression of the form `f a`.
mkSymmCongruencesKey (lhs rhs : Expr) : CCM SymmCongruencesKey := do let lhs ← getRoot lhs let rhs ← getRoot rhs if hash lhs > hash rhs then return { h₁ := rhs, h₂ := lhs } else return { h₁ := lhs, h₂ := rhs }
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
mkSymmCongruencesKey
Return the `SymmCongruencesKey` associated with the equality `lhs = rhs`.
compareSymmAux (lhs₁ rhs₁ lhs₂ rhs₂ : Expr) : CCM Bool := do let lhs₁ ← getRoot lhs₁ let rhs₁ ← getRoot rhs₁ let lhs₂ ← getRoot lhs₂ let rhs₂ ← getRoot rhs₂ let (lhs₁, rhs₁) := if rhs₁.lt lhs₁ then (rhs₁, lhs₁) else (lhs₁, rhs₁) let (lhs₂, rhs₂) := if rhs₂.lt lhs₂ then (rhs₂, lhs₂) else (lhs₂, rhs₂) return lhs₁ == lhs₂ && rhs₁ == rhs₂
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
compareSymmAux
Auxiliary function for comparing `lhs₁ ~ rhs₁` and `lhs₂ ~ rhs₂`, when `~` is symmetric/commutative. It returns `true` (equal) for `a ~ b` `b ~ a`.
compareSymm : (k₁ k₂ : Expr × Name) → CCM Bool | (e₁, n₁), (e₂, n₂) => do if n₁ != n₂ then return false if n₁ == ``Eq || n₁ == ``Iff then compareSymmAux e₁.appFn!.appArg! e₁.appArg! e₂.appFn!.appArg! e₂.appArg! else let some (_, lhs₁, rhs₁) ← e₁.relSidesIfSymm? | failure let some (_, lhs₂, rhs₂) ← e₂.relSidesIfSymm? | failure compareSymmAux lhs₁ rhs₁ lhs₂ rhs₂
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
compareSymm
Given ``k₁ := (R₁ lhs₁ rhs₁, `R₁)`` and ``k₂ := (R₂ lhs₂ rhs₂, `R₂)``, return `true` if `R₁ lhs₁ rhs₁` is equivalent to `R₂ lhs₂ rhs₂` modulo the symmetry of `R₁` and `R₂`.
checkEqTrue (e : Expr) : CCM Unit := do let some (_, lhs, rhs) ← e.relSidesIfRefl? | return if ← isEqv e (.const ``True []) then return -- it is already equivalent to `True` let lhsR ← getRoot lhs let rhsR ← getRoot rhs if lhsR != rhsR then return pushEq e (.const ``True []) .eqTrue
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
checkEqTrue
Given `e := R lhs rhs`, if `R` is a reflexive relation and `lhs` is equivalent to `rhs`, add equality `e = True`.
addCongruenceTable (e : Expr) : CCM Unit := do guard e.isApp let k ← mkCongruencesKey e if let some es := (← get).congruences[k]? then for oldE in es do if ← isCongruent e oldE then let some currEntry ← getEntry e | failure let newEntry := { currEntry with cgRoot := oldE } modify fun ccs => { ccs with entries := ccs.entries.insert e newEntry } let heqProof ← (!·) <$> pureIsDefEq (← inferType e) (← inferType oldE) pushTodo e oldE .congr heqProof return modify fun ccs => { ccs with congruences := ccs.congruences.insert k (e :: es) } else modify fun ccs => { ccs with congruences := ccs.congruences.insert k [e] }
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
addCongruenceTable
If the congruence table (`congruences` field) has congruent expression to `e`, add the equality to the todo list. If not, add `e` to the congruence table.
addSymmCongruenceTable (e : Expr) : CCM Unit := do let some (rel, lhs, rhs) ← e.relSidesIfSymm? | failure let k ← mkSymmCongruencesKey lhs rhs let newP := (e, rel) if let some ps := (← get).symmCongruences[k]? then for p in ps do if ← compareSymm newP p then let some currEntry ← getEntry e | failure let newEntry := { currEntry with cgRoot := p.1 } modify fun ccs => { ccs with entries := ccs.entries.insert e newEntry } if rel == ``Eq || e.getAppNumArgs == 2 then pushEq e p.1 .congr checkEqTrue e return modify fun ccs => { ccs with symmCongruences := ccs.symmCongruences.insert k (newP :: ps) } checkEqTrue e else modify fun ccs => { ccs with symmCongruences := ccs.symmCongruences.insert k [newP] } checkEqTrue e
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
addSymmCongruenceTable
If the symm congruence table (`symmCongruences` field) has congruent expression to `e`, add the equality to the todo list. If not, add `e` to the symm congruence table.
pushSubsingletonEq (a b : Expr) : CCM Unit := do let A ← normalize (← inferType a) let B ← normalize (← inferType b) if ← pureIsDefEq A B then let proof ← mkAppM ``FastSubsingleton.elim #[a, b] pushEq a b proof else let some AEqB ← getEqProof A B | failure let proof ← mkAppM ``FastSubsingleton.helim #[AEqB, a, b] pushHEq a b proof
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
pushSubsingletonEq
Given subsingleton elements `a` and `b` which are not necessarily of the same type, if the types of `a` and `b` are equivalent, add the (heterogeneous) equality proof between `a` and `b` to the todo list.
checkNewSubsingletonEq (oldRoot newRoot : Expr) : CCM Unit := do guard (← isEqv oldRoot newRoot) guard ((← getRoot oldRoot) == newRoot) let some it₁ := (← get).subsingletonReprs[oldRoot]? | return if let some it₂ := (← get).subsingletonReprs[newRoot]? then pushSubsingletonEq it₁ it₂ else modify fun ccs => { ccs with subsingletonReprs := ccs.subsingletonReprs.insert newRoot it₁ }
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
checkNewSubsingletonEq
Given the equivalent expressions `oldRoot` and `newRoot` the root of `oldRoot` is `newRoot`, if `oldRoot` has root representative of subsingletons, try to push the equality proof between their root representatives to the todo list, or update the root representative to `newRoot`.
getEqcLambdas (e : Expr) (r : Array Expr := #[]) : CCM (Array Expr) := do guard ((← getRoot e) == e) let mut r := r let some ee ← getEntry e | failure unless ee.hasLambdas do return r let mut it := e repeat if it.isLambda then r := r.push it let some itN ← getEntry it | failure it := itN.next until it == e return r
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
getEqcLambdas
Get all lambda expressions in the equivalence class of `e` and append to `r`. `e` must be the root of its equivalence class.
propagateBeta (fn : Expr) (revArgs : Array Expr) (lambdas : Array Expr) (newLambdaApps : Array Expr := #[]) : CCM (Array Expr) := do let mut newLambdaApps := newLambdaApps for lambda in lambdas do guard lambda.isLambda if fn != lambda then if ← pureIsDefEq (← inferType fn) (← inferType lambda) then let newApp := mkAppRev lambda revArgs newLambdaApps := newLambdaApps.push newApp return newLambdaApps
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
propagateBeta
Remove `fn` and expressions whose type isn't def-eq to `fn`'s type out from `lambdas`, return the remaining lambdas applied to the reversed arguments.
dbgTraceACEq (header : String) (lhs rhs : ACApps) : CCM Unit := do let ccs ← get trace[Debug.Meta.Tactic.cc.ac] group (ofFormat (header ++ .line) ++ ccs.ppACApps lhs ++ ofFormat (.line ++ "=" ++ .line) ++ ccs.ppACApps rhs)
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
dbgTraceACEq
Given `lhs`, `rhs`, and `header := "my header:"`, Trace `my header: lhs = rhs`.
dbgTraceACState : CCM Unit := do let ccs ← get trace[Debug.Meta.Tactic.cc.ac] group ("state: " ++ nest 6 ccs.ppAC)
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
dbgTraceACState
Trace the state of AC module.
insertEraseROcc (arg : Expr) (lhs : ACApps) (inLHS isInsert : Bool) : CCM Unit := do let some entry := (← get).acEntries[arg]? | failure let occs := entry.ROccs inLHS let newOccs := if isInsert then occs.insert lhs else occs.erase lhs let newEntry := if inLHS then { entry with RLHSOccs := newOccs } else { entry with RRHSOccs := newOccs } modify fun ccs => { ccs with acEntries := ccs.acEntries.insert arg newEntry }
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
insertEraseROcc
Insert or erase `lhs` to the occurrences of `arg` on an equality in `acR`.
insertEraseROccs (e lhs : ACApps) (inLHS isInsert : Bool) : CCM Unit := do match e with | .apps _ args => insertEraseROcc args[0]! lhs inLHS isInsert for h : i in [1:args.size] do if args[i] != args[i - 1]! then insertEraseROcc args[i] lhs inLHS isInsert | .ofExpr e => insertEraseROcc e lhs inLHS isInsert
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
insertEraseROccs
Insert or erase `lhs` to the occurrences of arguments of `e` on an equality in `acR`.
@[inline] insertROccs (e lhs : ACApps) (inLHS : Bool) : CCM Unit := insertEraseROccs e lhs inLHS true
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
insertROccs
Insert `lhs` to the occurrences of arguments of `e` on an equality in `acR`.
@[inline] eraseROccs (e lhs : ACApps) (inLHS : Bool) : CCM Unit := insertEraseROccs e lhs inLHS false
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
eraseROccs
Erase `lhs` to the occurrences of arguments of `e` on an equality in `acR`.
@[inline] insertRBHSOccs (lhs rhs : ACApps) : CCM Unit := do insertROccs lhs lhs true insertROccs rhs lhs false
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
insertRBHSOccs
Insert `lhs` to the occurrences on an equality in `acR` corresponding to the equality `lhs := rhs`.
@[inline] eraseRBHSOccs (lhs rhs : ACApps) : CCM Unit := do eraseROccs lhs lhs true eraseROccs rhs lhs false
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
eraseRBHSOccs
Erase `lhs` to the occurrences on an equality in `acR` corresponding to the equality `lhs := rhs`.
@[inline] insertRRHSOccs (e lhs : ACApps) : CCM Unit := insertROccs e lhs false
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
insertRRHSOccs
Insert `lhs` to the occurrences of arguments of `e` on the right-hand side of an equality in `acR`.
@[inline] eraseRRHSOccs (e lhs : ACApps) : CCM Unit := eraseROccs e lhs false
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
eraseRRHSOccs
Erase `lhs` to the occurrences of arguments of `e` on the right-hand side of an equality in `acR`.
composeAC (lhs rhs : ACApps) (H : DelayedExpr) : CCM Unit := do let some x := (← get).getVarWithLeastRHSOccs lhs | failure let some ent := (← get).acEntries[x]? | failure let occs := ent.RRHSOccs for Rlhs in occs do let some (Rrhs, RH) := (← get).acR[Rlhs]? | failure if lhs.isSubset Rrhs then let (newRrhs, RrhsEqNewRrhs) ← simplifyACCore Rrhs lhs rhs H let newRH := DelayedExpr.eqTransOpt Rlhs Rrhs newRrhs RH RrhsEqNewRrhs modify fun ccs => { ccs with acR := ccs.acR.insert Rlhs (newRrhs, newRH) } eraseRRHSOccs Rrhs Rlhs insertRRHSOccs newRrhs Rlhs let ccs ← get trace[Debug.Meta.Tactic.cc.ac] group <| let oldRw := paren (ccs.ppACApps Rlhs ++ ofFormat (Format.line ++ "-->" ++ .line) ++ ccs.ppACApps Rrhs) let newRw := paren (ccs.ppACApps lhs ++ ofFormat (Format.line ++ "-->" ++ .line) ++ ccs.ppACApps rhs) "compose: " ++ nest 9 (group (oldRw ++ ofFormat (Format.line ++ "with" ++ .line) ++ newRw) ++ ofFormat (Format.line ++ ":=" ++ .line) ++ ccs.ppACApps newRrhs)
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
composeAC
Try to simplify the right-hand sides of equalities in `acR` by `H : lhs = rhs`.
collapseAC (lhs rhs : ACApps) (H : DelayedExpr) : CCM Unit := do let some x := (← get).getVarWithLeastLHSOccs lhs | failure let some ent := (← get).acEntries[x]? | failure let occs := ent.RLHSOccs for Rlhs in occs do if lhs.isSubset Rlhs then let some (Rrhs, RH) := (← get).acR[Rlhs]? | failure eraseRBHSOccs Rlhs Rrhs modify fun ccs => { ccs with acR := ccs.acR.erase Rlhs } let (newRlhs, RlhsEqNewRlhs) ← simplifyACCore Rlhs lhs rhs H let newRlhsEqRlhs := DelayedExpr.eqSymmOpt Rlhs newRlhs RlhsEqNewRlhs let newRH := DelayedExpr.eqTransOpt newRlhs Rlhs Rrhs newRlhsEqRlhs RH modifyACTodo fun todo => todo.push (newRlhs, Rrhs, newRH) let ccs ← get trace[Debug.Meta.Tactic.cc.ac] group <| let newRw := paren (ccs.ppACApps lhs ++ ofFormat (Format.line ++ "-->" ++ .line) ++ ccs.ppACApps rhs) let oldRw := paren (ccs.ppACApps Rrhs ++ ofFormat (Format.line ++ "<--" ++ .line) ++ ccs.ppACApps Rlhs) "collapse: " ++ nest 10 (group (newRw ++ ofFormat (Format.line ++ "at" ++ .line) ++ oldRw) ++ ofFormat (Format.line ++ ":=" ++ .line) ++ ccs.ppACApps newRlhs)
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
collapseAC
Try to simplify the left-hand sides of equalities in `acR` by `H : lhs = rhs`.
mkACSuperposeProof (ra sb a b r s ts tr : ACApps) (tsEqa trEqb : DelayedExpr) : MetaM DelayedExpr := do let .apps _ _ := tr | failure let .apps op _ := ts | failure let some tse := ts.toExpr | failure let some re := r.toExpr | failure let some tre := tr.toExpr | failure let some se := s.toExpr | failure let some ae := a.toExpr | failure let some be := b.toExpr | failure let some rae := ra.toExpr | failure let some sbe := sb.toExpr | failure let tsrEqar := DelayedExpr.congrFun (.congrArg op tsEqa) r -- `(t * s) * r = a * r` let trsEqbs := DelayedExpr.congrFun (.congrArg op trEqb) s -- `(t * r) * s = b * s` let tsr := mkApp2 op tse re -- `(t * s) * r` let trs := mkApp2 op tre se -- `(t * r) * s` let ar := mkApp2 op ae re -- `a * r` let bs := mkApp2 op be se -- `b * r` let tsrEqtrs ← mkACProof tsr trs -- `(t * s) * r = (t * r) * s` let raEqar ← mkACProof rae ar -- `r * a = a * r` let bsEqsb ← mkACProof bs sbe -- `b * s = s * b` return .eqTrans raEqar (.eqTrans (.eqSymm tsrEqar) (.eqTrans tsrEqtrs (.eqTrans trsEqbs bsEqsb)))
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
mkACSuperposeProof
Given `ra := a*r` `sb := b*s` `ts := t*s` `tr := t*r` `tsEqa : t*s = a` `trEqb : t*r = b`, return a proof for `ra = sb`. We use `a*b` to denote an AC application. That is, `(a*b)*(c*a)` is the term `a*a*b*c`.
superposeAC (ts a : ACApps) (tsEqa : DelayedExpr) : CCM Unit := do let .apps op args := ts | return for hi : i in [:args.size] do if i == 0 || args[i] != (args[i - 1]'(Nat.lt_of_le_of_lt (i.sub_le 1) hi.2.1)) then let some ent := (← get).acEntries[args[i]]? | failure let occs := ent.RLHSOccs for tr in occs do let .apps optr _ := tr | continue unless optr == op do continue let some (b, trEqb) := (← get).acR[tr]? | failure let tArgs := ts.intersection tr guard !tArgs.isEmpty let t := ACApps.mkApps op tArgs let sArgs := ts.diff t guard !sArgs.isEmpty let rArgs := tr.diff t guard !rArgs.isEmpty let s := ACApps.mkApps op sArgs let r := ACApps.mkApps op rArgs let ra := ACApps.mkFlatApps op r a let sb := ACApps.mkFlatApps op s b let some true := (← get).opInfo[op]? | failure let raEqsb ← mkACSuperposeProof ra sb a b r s ts tr tsEqa trEqb modifyACTodo fun todo => todo.push (ra, sb, raEqsb) let ccs ← get trace[Debug.Meta.Tactic.cc.ac] group <| let rw₁ := paren (ccs.ppACApps ts ++ ofFormat (Format.line ++ "-->" ++ .line) ++ ccs.ppACApps a) let rw₂ := paren (ccs.ppACApps tr ++ ofFormat (Format.line ++ "-->" ++ .line) ++ ccs.ppACApps b) let eq := paren (ccs.ppACApps ra ++ ofFormat (Format.line ++ "-->" ++ .line) ++ ccs.ppACApps sb) "superpose: " ++ nest 11 (group (rw₁ ++ ofFormat (Format.line ++ "with" ++ .line) ++ rw₂) ++ ofFormat (Format.line ++ ":=" ++ .line) ++ eq)
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
superposeAC
Given `tsEqa : ts = a`, for each equality `trEqb : tr = b` in `acR` where the intersection `t` of `ts` and `tr` is nonempty, let `ts = t*s` and `tr := t*r`, add a new equality `r*a = s*b`.
processAC : CCM Unit := do repeat let acTodo ← getACTodo let mut some (lhs, rhs, H) := acTodo.back? | break modifyACTodo fun _ => acTodo.pop let lhs₀ := lhs let rhs₀ := rhs dbgTraceACEq "process eq:" lhs rhs if let some p ← simplifyAC lhs then H := .eqTransOpt p.1 lhs rhs (.eqSymmOpt lhs p.1 p.2) H lhs := p.1 if let some p ← simplifyAC rhs then H := .eqTransOpt lhs rhs p.1 H p.2 rhs := p.1 if lhs != lhs₀ || rhs != rhs₀ then dbgTraceACEq "after simp:" lhs rhs if lhs == rhs then trace[Debug.Meta.Tactic.cc.ac] "trivial" continue if let .ofExpr lhse := lhs then if let .ofExpr rhse := rhs then if (← getRoot lhse) != (← getRoot rhse) then pushEq lhse rhse (.ofDExpr H) if compare lhs rhs == .lt then H := .eqSymmOpt lhs rhs H (lhs, rhs) := (rhs, lhs) composeAC lhs rhs H collapseAC lhs rhs H superposeAC lhs rhs H modify fun ccs => { ccs with acR := ccs.acR.insert lhs (rhs, H) } insertRBHSOccs lhs rhs let ccs ← get trace[Debug.Meta.Tactic.cc.ac] group <| "new rw: " ++ group (ccs.ppACApps lhs ++ ofFormat (Format.line ++ "-->" ++ .line) ++ ccs.ppACApps rhs)
def
Tactic
[ "Mathlib.Data.Option.Defs", "Mathlib.Tactic.CC.MkProof" ]
Mathlib/Tactic/CC/Addition.lean
processAC
Process the tasks in the `acTodo` field.