Skip to content

Commit

Permalink
perf(BundledCats): more explicit universe annotations (#12741)
Browse files Browse the repository at this point in the history
Testing for leanprover/lean4#4085 exposed possible regressions typeclass synthesis which can be ameliorated using explicit universes. We do this and we add references to the issue #12737 tracking these changes.
  • Loading branch information
mattrobball committed May 7, 2024
1 parent 4c7c71d commit 294ff6a
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 116 deletions.
2 changes: 2 additions & 0 deletions Mathlib/AlgebraicGeometry/AffineScheme.lean
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ We also define predicates about affine schemes and affine open sets.
-/

-- Explicit universe annotations were used in this file to improve perfomance #12737

set_option linter.uppercaseLean3 false

noncomputable section
Expand Down
2 changes: 2 additions & 0 deletions Mathlib/AlgebraicGeometry/FunctionField.lean
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ This is a field when the scheme is integral.
field. This map is injective.
-/

-- Explicit universe annotations were used in this file to improve perfomance #12737

set_option linter.uppercaseLean3 false

universe u v
Expand Down
2 changes: 2 additions & 0 deletions Mathlib/AlgebraicGeometry/GammaSpecAdjunction.lean
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ case the unit and the counit would switch to each other.
-/

-- Explicit universe annotations were used in this file to improve perfomance #12737

set_option linter.uppercaseLean3 false

noncomputable section
Expand Down
6 changes: 4 additions & 2 deletions Mathlib/AlgebraicGeometry/Morphisms/RingHomProperties.lean
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Further more, these properties are stable under compositions (resp. base change)
-/

-- Explicit universe annotations were used in this file to improve perfomance #12737

universe u

open CategoryTheory Opposite TopologicalSpace CategoryTheory.Limits AlgebraicGeometry
Expand Down Expand Up @@ -68,7 +70,7 @@ theorem RespectsIso.basicOpen_iff (hP : RespectsIso @P) {X Y : Scheme.{u}} [IsAf
congr 1
#align ring_hom.respects_iso.basic_open_iff RingHom.RespectsIso.basicOpen_iff

theorem RespectsIso.basicOpen_iff_localization (hP : RespectsIso @P) {X Y : Scheme} [IsAffine X]
theorem RespectsIso.basicOpen_iff_localization (hP : RespectsIso @P) {X Y : Scheme.{u}} [IsAffine X]
[IsAffine Y] (f : X ⟶ Y) (r : Y.presheaf.obj (Opposite.op ⊤)) :
P (Scheme.Γ.map (f ∣_ Y.basicOpen r).op) ↔ P (Localization.awayMap (Scheme.Γ.map f.op) r) := by
refine (hP.basicOpen_iff _ _).trans ?_
Expand Down Expand Up @@ -134,7 +136,7 @@ def sourceAffineLocally : AffineTargetMorphismProperty := fun X _ f _ =>
/-- For `P` a property of ring homomorphisms, `affineLocally P` holds for `f : X ⟶ Y` if for each
affine open `U = Spec A ⊆ Y` and `V = Spec B ⊆ f ⁻¹' U`, the ring hom `A ⟶ B` satisfies `P`.
Also see `affineLocally_iff_affineOpens_le`. -/
abbrev affineLocally : MorphismProperty Scheme :=
abbrev affineLocally : MorphismProperty Scheme.{u} :=
targetAffineLocally (sourceAffineLocally @P)
#align algebraic_geometry.affine_locally AlgebraicGeometry.affineLocally

Expand Down
86 changes: 44 additions & 42 deletions Mathlib/AlgebraicGeometry/OpenImmersion.lean

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Mathlib/AlgebraicGeometry/Properties.lean
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ We provide some basic properties of schemes
are reduced.
-/

universe u

-- Explicit universe annotations were used in this file to improve perfomance #12737

universe u

open TopologicalSpace Opposite CategoryTheory CategoryTheory.Limits TopCat

Expand Down
75 changes: 39 additions & 36 deletions Mathlib/AlgebraicGeometry/Restrict.lean

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion Mathlib/AlgebraicGeometry/Scheme.lean
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ A morphism of schemes is just a morphism of the underlying locally ringed spaces
-/

-- Explicit universe annotations were used in this file to improve perfomance #12737

set_option linter.uppercaseLean3 false

universe u

noncomputable section

open TopologicalSpace
Expand Down Expand Up @@ -245,7 +250,7 @@ def Spec : CommRingCatᵒᵖ ⥤ Scheme where
/-- The empty scheme.
-/
@[simps]
def empty.{u} : Scheme.{u} where
def empty : Scheme where
carrier := TopCat.of PEmpty
presheaf := (CategoryTheory.Functor.const _).obj (CommRingCat.of PUnit)
IsSheaf := Presheaf.isSheaf_of_isTerminal _ CommRingCat.punitIsTerminal
Expand Down
3 changes: 3 additions & 0 deletions Mathlib/AlgebraicGeometry/Spec.lean
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ The adjunction `Γ ⊣ Spec` is constructed in `Mathlib/AlgebraicGeometry/GammaS
-/


-- Explicit universe annotations were used in this file to improve perfomance #12737

noncomputable section

universe u v
Expand Down
72 changes: 38 additions & 34 deletions Mathlib/Geometry/RingedSpace/LocallyRingedSpace.lean
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ stalks are local rings), and morphisms between these (morphisms in `SheafedSpace
`is_local_ring_hom` on the stalk maps).
-/

-- Explicit universe annotations were used in this file to improve perfomance #12737

universe u

open CategoryTheory

open TopCat
Expand All @@ -33,7 +37,7 @@ such that all the stalks are local rings.
A morphism of locally ringed spaces is a morphism of ringed spaces
such that the morphisms induced on stalks are local ring homomorphisms. -/
structure LocallyRingedSpace extends SheafedSpace CommRingCat where
structure LocallyRingedSpace extends SheafedSpace CommRingCat.{u} where
/-- Stalks of a locally ringed space are local rings. -/
localRing : ∀ x, LocalRing (presheaf.stalk x)
set_option linter.uppercaseLean3 false in
Expand All @@ -43,7 +47,7 @@ attribute [instance] LocallyRingedSpace.localRing

namespace LocallyRingedSpace

variable (X : LocallyRingedSpace)
variable (X : LocallyRingedSpace.{u})

/-- An alias for `to_SheafedSpace`, where the result type is a `RingedSpace`.
This allows us to use dot-notation for the `RingedSpace` namespace.
Expand All @@ -59,7 +63,7 @@ def toTopCat : TopCat :=
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.to_Top AlgebraicGeometry.LocallyRingedSpace.toTopCat

instance : CoeSort LocallyRingedSpace (Type*) :=
instance : CoeSort LocallyRingedSpace (Type u) :=
fun X : LocallyRingedSpace => (X.toTopCat : Type _)⟩

instance (x : X) : LocalRing (X.stalk x) :=
Expand All @@ -76,7 +80,7 @@ set_option linter.uppercaseLean3 false in
/-- A morphism of locally ringed spaces is a morphism of ringed spaces
such that the morphisms induced on stalks are local ring homomorphisms. -/
@[ext]
structure Hom (X Y : LocallyRingedSpace) : Type _ where
structure Hom (X Y : LocallyRingedSpace.{u}) : Type _ where
/-- the underlying morphism between ringed spaces -/
val : X.toSheafedSpace ⟶ Y.toSheafedSpace
/-- the underlying morphism induces a local ring homomorphism on stalks -/
Expand All @@ -87,14 +91,14 @@ set_option linter.uppercaseLean3 false in
instance : Quiver LocallyRingedSpace :=
⟨Hom⟩

@[ext] lemma Hom.ext' (X Y : LocallyRingedSpace) {f g : X ⟶ Y} (h : f.val = g.val) : f = g :=
@[ext] lemma Hom.ext' (X Y : LocallyRingedSpace.{u}) {f g : X ⟶ Y} (h : f.val = g.val) : f = g :=
Hom.ext _ _ h

-- TODO perhaps we should make a bundled `LocalRing` and return one here?
-- TODO define `sheaf.stalk` so we can write `X.𝒪.stalk` here?
/-- The stalk of a locally ringed space, just as a `CommRing`.
-/
noncomputable def stalk (X : LocallyRingedSpace) (x : X) : CommRingCat :=
noncomputable def stalk (X : LocallyRingedSpace.{u}) (x : X) : CommRingCat :=
X.presheaf.stalk x
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.stalk AlgebraicGeometry.LocallyRingedSpace.stalk
Expand All @@ -106,39 +110,39 @@ instance stalkLocal (x : X) : LocalRing <| X.stalk x := X.localRing x
/-- A morphism of locally ringed spaces `f : X ⟶ Y` induces
a local ring homomorphism from `Y.stalk (f x)` to `X.stalk x` for any `x : X`.
-/
noncomputable def stalkMap {X Y : LocallyRingedSpace} (f : X ⟶ Y) (x : X) :
noncomputable def stalkMap {X Y : LocallyRingedSpace.{u}} (f : X ⟶ Y) (x : X) :
Y.stalk (f.1.1 x) ⟶ X.stalk x :=
PresheafedSpace.stalkMap f.1 x
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.stalk_map AlgebraicGeometry.LocallyRingedSpace.stalkMap

instance {X Y : LocallyRingedSpace} (f : X ⟶ Y) (x : X) : IsLocalRingHom (stalkMap f x) :=
instance {X Y : LocallyRingedSpace.{u}} (f : X ⟶ Y) (x : X) : IsLocalRingHom (stalkMap f x) :=
f.2 x

instance {X Y : LocallyRingedSpace} (f : X ⟶ Y) (x : X) :
instance {X Y : LocallyRingedSpace.{u}} (f : X ⟶ Y) (x : X) :
IsLocalRingHom (PresheafedSpace.stalkMap f.1 x) :=
f.2 x

/-- The identity morphism on a locally ringed space. -/
@[simps]
def id (X : LocallyRingedSpace) : Hom X X :=
def id (X : LocallyRingedSpace.{u}) : Hom X X :=
⟨𝟙 _, fun x => by erw [PresheafedSpace.stalkMap.id]; apply isLocalRingHom_id⟩
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.id AlgebraicGeometry.LocallyRingedSpace.id

instance (X : LocallyRingedSpace) : Inhabited (Hom X X) :=
instance (X : LocallyRingedSpace.{u}) : Inhabited (Hom X X) :=
⟨id X⟩

/-- Composition of morphisms of locally ringed spaces. -/
def comp {X Y Z : LocallyRingedSpace} (f : Hom X Y) (g : Hom Y Z) : Hom X Z :=
def comp {X Y Z : LocallyRingedSpace.{u}} (f : Hom X Y) (g : Hom Y Z) : Hom X Z :=
⟨f.val ≫ g.val, fun x => by
erw [PresheafedSpace.stalkMap.comp]
exact @isLocalRingHom_comp _ _ _ _ _ _ _ _ (f.2 _) (g.2 _)⟩
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.comp AlgebraicGeometry.LocallyRingedSpace.comp

/-- The category of locally ringed spaces. -/
instance : Category LocallyRingedSpace where
instance : Category LocallyRingedSpace.{u} where
Hom := Hom
id := id
comp {X Y Z} f g := comp f g
Expand All @@ -148,7 +152,7 @@ instance : Category LocallyRingedSpace where

/-- The forgetful functor from `LocallyRingedSpace` to `SheafedSpace CommRing`. -/
@[simps]
def forgetToSheafedSpace : LocallyRingedSpace ⥤ SheafedSpace CommRingCat where
def forgetToSheafedSpace : LocallyRingedSpace.{u} ⥤ SheafedSpace CommRingCat.{u} where
obj X := X.toSheafedSpace
map {X Y} f := f.1
set_option linter.uppercaseLean3 false in
Expand All @@ -159,13 +163,13 @@ instance : forgetToSheafedSpace.Faithful where

/-- The forgetful functor from `LocallyRingedSpace` to `Top`. -/
@[simps!]
def forgetToTop : LocallyRingedSpace ⥤ TopCat :=
def forgetToTop : LocallyRingedSpace.{u} ⥤ TopCat.{u} :=
forgetToSheafedSpace ⋙ SheafedSpace.forget _
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.forget_to_Top AlgebraicGeometry.LocallyRingedSpace.forgetToTop

@[simp]
theorem comp_val {X Y Z : LocallyRingedSpace} (f : X ⟶ Y) (g : Y ⟶ Z) :
theorem comp_val {X Y Z : LocallyRingedSpace.{u}} (f : X ⟶ Y) (g : Y ⟶ Z) :
(f ≫ g).val = f.val ≫ g.val :=
rfl
set_option linter.uppercaseLean3 false in
Expand All @@ -174,13 +178,13 @@ set_option linter.uppercaseLean3 false in
-- Porting note: complains that `(f ≫ g).val.c` can be further simplified
-- so changed to its simp normal form `(f.val ≫ g.val).c`
@[simp]
theorem comp_val_c {X Y Z : LocallyRingedSpace} (f : X ⟶ Y) (g : Y ⟶ Z) :
theorem comp_val_c {X Y Z : LocallyRingedSpace.{u}} (f : X ⟶ Y) (g : Y ⟶ Z) :
(f.1 ≫ g.1).c = g.val.c ≫ (Presheaf.pushforward _ g.val.base).map f.val.c :=
rfl
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.comp_val_c AlgebraicGeometry.LocallyRingedSpace.comp_val_c

theorem comp_val_c_app {X Y Z : LocallyRingedSpace} (f : X ⟶ Y) (g : Y ⟶ Z) (U : (Opens Z)ᵒᵖ) :
theorem comp_val_c_app {X Y Z : LocallyRingedSpace.{u}} (f : X ⟶ Y) (g : Y ⟶ Z) (U : (Opens Z)ᵒᵖ) :
(f ≫ g).val.c.app U = g.val.c.app U ≫ f.val.c.app (op <| (Opens.map g.val.base).obj U.unop) :=
rfl
set_option linter.uppercaseLean3 false in
Expand All @@ -192,8 +196,8 @@ spaces can be lifted to a morphism `X ⟶ Y` as locally ringed spaces.
See also `iso_of_SheafedSpace_iso`.
-/
@[simps]
def homOfSheafedSpaceHomOfIsIso {X Y : LocallyRingedSpace} (f : X.toSheafedSpace ⟶ Y.toSheafedSpace)
[IsIso f] : X ⟶ Y :=
def homOfSheafedSpaceHomOfIsIso {X Y : LocallyRingedSpace.{u}}
(f : X.toSheafedSpace ⟶ Y.toSheafedSpace) [IsIso f] : X ⟶ Y :=
Hom.mk f fun x =>
-- Here we need to see that the stalk maps are really local ring homomorphisms.
-- This can be solved by type class inference, because stalk maps of isomorphisms
Expand All @@ -210,7 +214,7 @@ This is related to the property that the functor `forget_to_SheafedSpace` reflec
In fact, it is slightly stronger as we do not require `f` to come from a morphism between
_locally_ ringed spaces.
-/
def isoOfSheafedSpaceIso {X Y : LocallyRingedSpace} (f : X.toSheafedSpace ≅ Y.toSheafedSpace) :
def isoOfSheafedSpaceIso {X Y : LocallyRingedSpace.{u}} (f : X.toSheafedSpace ≅ Y.toSheafedSpace) :
X ≅ Y where
hom := homOfSheafedSpaceHomOfIsIso f.hom
inv := homOfSheafedSpaceHomOfIsIso f.inv
Expand All @@ -224,15 +228,15 @@ instance : forgetToSheafedSpace.ReflectsIsomorphisms where reflects {_ _} f i :=
⟨homOfSheafedSpaceHomOfIsIso (CategoryTheory.inv (forgetToSheafedSpace.map f)),
Hom.ext _ _ (IsIso.hom_inv_id (I := i)), Hom.ext _ _ (IsIso.inv_hom_id (I := i))⟩ }

instance is_sheafedSpace_iso {X Y : LocallyRingedSpace} (f : X ⟶ Y) [IsIso f] : IsIso f.1 :=
instance is_sheafedSpace_iso {X Y : LocallyRingedSpace.{u}} (f : X ⟶ Y) [IsIso f] : IsIso f.1 :=
LocallyRingedSpace.forgetToSheafedSpace.map_isIso f
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.is_SheafedSpace_iso AlgebraicGeometry.LocallyRingedSpace.is_sheafedSpace_iso

/-- The restriction of a locally ringed space along an open embedding.
-/
@[simps!]
def restrict {U : TopCat} (X : LocallyRingedSpace) {f : U ⟶ X.toTopCat} (h : OpenEmbedding f) :
def restrict {U : TopCat} (X : LocallyRingedSpace.{u}) {f : U ⟶ X.toTopCat} (h : OpenEmbedding f) :
LocallyRingedSpace where
localRing := by
intro x
Expand All @@ -244,23 +248,23 @@ set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.restrict AlgebraicGeometry.LocallyRingedSpace.restrict

/-- The canonical map from the restriction to the subspace. -/
def ofRestrict {U : TopCat} (X : LocallyRingedSpace) {f : U ⟶ X.toTopCat} (h : OpenEmbedding f) :
X.restrict h ⟶ X :=
def ofRestrict {U : TopCat} (X : LocallyRingedSpace.{u})
{f : U ⟶ X.toTopCat} (h : OpenEmbedding f) : X.restrict h ⟶ X :=
⟨X.toPresheafedSpace.ofRestrict h, fun _ => inferInstance⟩
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.of_restrict AlgebraicGeometry.LocallyRingedSpace.ofRestrict

/-- The restriction of a locally ringed space `X` to the top subspace is isomorphic to `X` itself.
-/
def restrictTopIso (X : LocallyRingedSpace) :
def restrictTopIso (X : LocallyRingedSpace.{u}) :
X.restrict (Opens.openEmbedding ⊤) ≅ X :=
isoOfSheafedSpaceIso X.toSheafedSpace.restrictTopIso
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.restrict_top_iso AlgebraicGeometry.LocallyRingedSpace.restrictTopIso

/-- The global sections, notated Gamma.
-/
def Γ : LocallyRingedSpaceᵒᵖ ⥤ CommRingCat :=
def Γ : LocallyRingedSpace.{u}ᵒᵖ ⥤ CommRingCat.{u} :=
forgetToSheafedSpace.op ⋙ SheafedSpace.Γ
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.Γ AlgebraicGeometry.LocallyRingedSpace.Γ
Expand All @@ -271,28 +275,28 @@ set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.Γ_def AlgebraicGeometry.LocallyRingedSpace.Γ_def

@[simp]
theorem Γ_obj (X : LocallyRingedSpaceᵒᵖ) : Γ.obj X = X.unop.presheaf.obj (op ⊤) :=
theorem Γ_obj (X : LocallyRingedSpace.{u}ᵒᵖ) : Γ.obj X = X.unop.presheaf.obj (op ⊤) :=
rfl
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.Γ_obj AlgebraicGeometry.LocallyRingedSpace.Γ_obj

theorem Γ_obj_op (X : LocallyRingedSpace) : Γ.obj (op X) = X.presheaf.obj (op ⊤) :=
theorem Γ_obj_op (X : LocallyRingedSpace.{u}) : Γ.obj (op X) = X.presheaf.obj (op ⊤) :=
rfl
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.Γ_obj_op AlgebraicGeometry.LocallyRingedSpace.Γ_obj_op

@[simp]
theorem Γ_map {X Y : LocallyRingedSpaceᵒᵖ} (f : X ⟶ Y) : Γ.map f = f.unop.1.c.app (op ⊤) :=
theorem Γ_map {X Y : LocallyRingedSpace.{u}ᵒᵖ} (f : X ⟶ Y) : Γ.map f = f.unop.1.c.app (op ⊤) :=
rfl
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.Γ_map AlgebraicGeometry.LocallyRingedSpace.Γ_map

theorem Γ_map_op {X Y : LocallyRingedSpace} (f : X ⟶ Y) : Γ.map f.op = f.1.c.app (op ⊤) :=
theorem Γ_map_op {X Y : LocallyRingedSpace.{u}} (f : X ⟶ Y) : Γ.map f.op = f.1.c.app (op ⊤) :=
rfl
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.Γ_map_op AlgebraicGeometry.LocallyRingedSpace.Γ_map_op

theorem preimage_basicOpen {X Y : LocallyRingedSpace} (f : X ⟶ Y) {U : Opens Y}
theorem preimage_basicOpen {X Y : LocallyRingedSpace.{u}} (f : X ⟶ Y) {U : Opens Y}
(s : Y.presheaf.obj (op U)) :
(Opens.map f.1.base).obj (Y.toRingedSpace.basicOpen s) =
@RingedSpace.basicOpen X.toRingedSpace ((Opens.map f.1.base).obj U) (f.1.c.app _ s) := by
Expand All @@ -311,7 +315,7 @@ set_option linter.uppercaseLean3 false in

-- This actually holds for all ringed spaces with nontrivial stalks.
@[simp]
theorem basicOpen_zero (X : LocallyRingedSpace) (U : Opens X.carrier) :
theorem basicOpen_zero (X : LocallyRingedSpace.{u}) (U : Opens X.carrier) :
X.toRingedSpace.basicOpen (0 : X.presheaf.obj <| op U) = ⊥ := by
ext x
simp only [RingedSpace.basicOpen, Opens.coe_mk, Set.mem_image, Set.mem_setOf_eq, Subtype.exists,
Expand All @@ -324,7 +328,7 @@ theorem basicOpen_zero (X : LocallyRingedSpace) (U : Opens X.carrier) :
set_option linter.uppercaseLean3 false in
#align algebraic_geometry.LocallyRingedSpace.basic_open_zero AlgebraicGeometry.LocallyRingedSpace.basicOpen_zero

instance component_nontrivial (X : LocallyRingedSpace) (U : Opens X.carrier) [hU : Nonempty U] :
instance component_nontrivial (X : LocallyRingedSpace.{u}) (U : Opens X.carrier) [hU : Nonempty U] :
Nontrivial (X.presheaf.obj <| op U) :=
(X.presheaf.germ hU.some).domain_nontrivial
set_option linter.uppercaseLean3 false in
Expand Down

0 comments on commit 294ff6a

Please sign in to comment.