Repository navigation
Description
Prerequisites
- Put an X between the brackets on this line if you have done all of the following:
- Checked that your issue isn't already filed.
- Reduced the issue to a self-contained, reproducible test case.
Description
We're building the mathlib structure "tower" and some instances are taking a lot longer to compile in Lean 4 than in Lean 3. Here is a rather large MWE, where I build a bunch of the order hierarchy and then at the very bottom I construct an instance in two ways, one very quick and one very slow. I know other examples where I don't have an analogue of the "quick" way because the mk constructor is not so convenient. The slowness is really striking: there are three instances of these in the mathlib4 file Mathlib.Order.CompleteBooleanAlgebra and the entire file compiles essentially instantaneously apart from these instances which are super-slow. Here I give three super-quick workarounds for the instances in Mathlib4 but this has come up again in another file and no doubt it will show up more.
universe u v
instance {ι : Type u} {α : ι → Type v} [∀ i, LE (α i)] : LE (∀ i, α i) where
le x y := ∀ i, x i ≤ y i
class Top (α : Type u) where
/-- The top (`⊤`, `\top`) element -/
top : α
/-- Typeclass for the `⊥` (`\bot`) notation -/
class Bot (α : Type u) where
/-- The bot (`⊥`, `\bot`) element -/
bot : α
/-- The top (`⊤`, `\top`) element -/
notation "⊤" => Top.top
/-- The bot (`⊥`, `\bot`) element -/
notation "⊥" => Bot.bot
class Preorder (α : Type u) extends LE α, LT α where
le_refl : ∀ a : α, a ≤ a
le_trans : ∀ a b c : α, a ≤ b → b ≤ c → a ≤ c
lt := λ a b => a ≤ b ∧ ¬ b ≤ a
lt_iff_le_not_le : ∀ a b : α, a < b ↔ (a ≤ b ∧ ¬ b ≤ a)
class PartialOrder (α : Type u) extends Preorder α :=
(le_antisymm : ∀ a b : α, a ≤ b → b ≤ a → a = b)
def Set (α : Type u) := α → Prop
def setOf {α : Type u} (p : α → Prop) : Set α :=
p
namespace Set
variable {α ι : Type _}
/-- Membership in a set -/
protected def Mem (a : α) (s : Set α) : Prop :=
s a
instance : Membership α (Set α) :=
⟨Set.Mem⟩
def range (f : ι → α) : Set α :=
setOf (λ x => ∃ y, f y = x)
end Set
class InfSet (α : Type _) where
infₛ : Set α → α
class SupSet (α : Type _) where
supₛ : Set α → α
export SupSet (supₛ)
export InfSet (infₛ)
open Set
def supᵢ {α : Type _} [SupSet α] {ι} (s : ι → α) : α :=
supₛ (range s)
def infᵢ {α : Type _} [InfSet α] {ι} (s : ι → α) : α :=
infₛ (range s)
--notation3 "⨅ "(...)", "r:(scoped f => infᵢ f) => r
class HasSup (α : Type u) where
/-- Least upper bound (`\lub` notation) -/
sup : α → α → α
class HasInf (α : Type u) where
/-- Greatest lower bound (`\glb` notation) -/
inf : α → α → α
@[inherit_doc]
infixl:68 " ⊔ " => HasSup.sup
@[inherit_doc]
infixl:69 " ⊓ " => HasInf.inf
class SemilatticeSup (α : Type u) extends HasSup α, PartialOrder α where
/-- The supremum is an upper bound on the first argument -/
protected le_sup_left : ∀ a b : α, a ≤ a ⊔ b
/-- The supremum is an upper bound on the second argument -/
protected le_sup_right : ∀ a b : α, b ≤ a ⊔ b
/-- The supremum is the *least* upper bound -/
protected sup_le : ∀ a b c : α, a ≤ c → b ≤ c → a ⊔ b ≤ c
class SemilatticeInf (α : Type u) extends HasInf α, PartialOrder α where
/-- The infimum is a lower bound on the first argument -/
protected inf_le_left : ∀ a b : α, a ⊓ b ≤ a
/-- The infimum is a lower bound on the second argument -/
protected inf_le_right : ∀ a b : α, a ⊓ b ≤ b
/-- The infimum is the *greatest* lower bound -/
protected le_inf : ∀ a b c : α, a ≤ b → a ≤ c → a ≤ b ⊓ c
class Lattice (α : Type u) extends SemilatticeSup α, SemilatticeInf α
class CompleteSemilatticeInf (α : Type _) extends PartialOrder α, InfSet α where
/-- Any element of a set is more than the set infimum. -/
infₛ_le : ∀ s, ∀ a, a ∈ s → infₛ s ≤ a
/-- Any lower bound is less than the set infimum. -/
le_infₛ : ∀ s a, (∀ b, b ∈ s → a ≤ b) → a ≤ infₛ s
class CompleteSemilatticeSup (α : Type _) extends PartialOrder α, SupSet α where
/-- Any element of a set is less than the set supremum. -/
le_supₛ : ∀ s, ∀ a, a ∈ s → a ≤ supₛ s
/-- Any upper bound is more than the set supremum. -/
supₛ_le : ∀ s a, (∀ b, b ∈ s → b ≤ a) → supₛ s ≤ a
class CompleteLattice (α : Type _) extends Lattice α, CompleteSemilatticeSup α,
CompleteSemilatticeInf α, Top α, Bot α where
/-- Any element is less than the top one. -/
protected le_top : ∀ x : α, x ≤ ⊤
/-- Any element is more than the bottom one. -/
protected bot_le : ∀ x : α, ⊥ ≤ x
class Frame (α : Type _) extends CompleteLattice α where
class Coframe (α : Type _) extends CompleteLattice α where
infᵢ_sup_le_sup_infₛ (a : α) (s : Set α) : (infᵢ (λ b => a ⊔ b)) ≤ a ⊔ infₛ s
-- should be ⨅ b ∈ s but I had problems with notation
class CompleteDistribLattice (α : Type _) extends Frame α where
infᵢ_sup_le_sup_infₛ : ∀ a s, (infᵢ (λ b => a ⊔ b)) ≤ a ⊔ infₛ s
-- similarly this is not quite right mathematically but this doesn't matter
-- See note [lower instance priority]
instance (priority := 100) CompleteDistribLattice.toCoframe {α : Type _} [CompleteDistribLattice α] :
Coframe α :=
{ ‹CompleteDistribLattice α› with }
class OrderTop (α : Type u) [LE α] extends Top α where
/-- `⊤` is the greatest element -/
le_top : ∀ a : α, a ≤ ⊤
class OrderBot (α : Type u) [LE α] extends Bot α where
/-- `⊥` is the least element -/
bot_le : ∀ a : α, ⊥ ≤ a
class BoundedOrder (α : Type u) [LE α] extends OrderTop α, OrderBot α
instance(priority := 100) CompleteLattice.toBoundedOrder {α : Type _} [h : CompleteLattice α] :
BoundedOrder α :=
{ h with }
namespace Pi
variable {ι : Type _} {α' : ι → Type _}
instance [∀ i, Bot (α' i)] : Bot (∀ i, α' i) :=
⟨fun _ => ⊥⟩
instance [∀ i, Top (α' i)] : Top (∀ i, α' i) :=
⟨fun _ => ⊤⟩
end Pi
section SemilatticeInf
variable {α : Type _} [SemilatticeInf α] {a b c d : α}
theorem inf_le_left : a ⊓ b ≤ a :=
SemilatticeInf.inf_le_left a b
theorem inf_le_right : a ⊓ b ≤ b :=
SemilatticeInf.inf_le_right a b
theorem le_inf : a ≤ b → a ≤ c → a ≤ b ⊓ c :=
SemilatticeInf.le_inf a b c
end SemilatticeInf
section SemilatticeSup
variable {α : Type _} [SemilatticeSup α] {a b c d : α}
theorem le_sup_left : a ≤ a ⊔ b :=
SemilatticeSup.le_sup_left a b
theorem le_sup_right : b ≤ a ⊔ b :=
SemilatticeSup.le_sup_right a b
theorem sup_le : a ≤ c → b ≤ c → a ⊔ b ≤ c :=
SemilatticeSup.sup_le a b c
end SemilatticeSup
namespace Pi
variable {ι : Type _} {α' : ι → Type _}
protected instance LE {ι : Type u} {α : ι → Type v} [∀ i, LE (α i)] : LE (∀ i, α i) where
le x y := ∀ i, x i ≤ y i
instance Preorder {ι : Type u} {α : ι → Type v} [∀ i, Preorder (α i)] : Preorder (∀ i, α i) :=
{ Pi.LE with
le_refl := sorry
le_trans := sorry
lt_iff_le_not_le := sorry }
instance PartialOrder {ι : Type u} {α : ι → Type v} [∀ i, PartialOrder (α i)] :
PartialOrder (∀ i, α i) :=
{ Pi.Preorder with
le_antisymm := sorry }
instance semilatticeSup [∀ i, SemilatticeSup (α' i)] : SemilatticeSup (∀ i, α' i) where
le_sup_left _ _ _ := le_sup_left
le_sup_right _ _ _ := le_sup_right
sup_le _ _ _ ac bc i := sup_le (ac i) (bc i)
instance semilatticeInf [∀ i, SemilatticeInf (α' i)] : SemilatticeInf (∀ i, α' i) where
inf_le_left _ _ _ := inf_le_left
inf_le_right _ _ _ := inf_le_right
le_inf _ _ _ ac bc i := le_inf (ac i) (bc i)
instance lattice [∀ i, Lattice (α' i)] : Lattice (∀ i, α' i) :=
{ Pi.semilatticeSup, Pi.semilatticeInf with }
instance orderTop [∀ i, LE (α' i)] [∀ i, OrderTop (α' i)] : OrderTop (∀ i, α' i) :=
{ inferInstanceAs (Top (∀ i, α' i)) with le_top := sorry }
instance orderBot [∀ i, LE (α' i)] [∀ i, OrderBot (α' i)] : OrderBot (∀ i, α' i) :=
{ inferInstanceAs (Bot (∀ i, α' i)) with bot_le := sorry }
instance boundedOrder [∀ i, LE (α' i)] [∀ i, BoundedOrder (α' i)] : BoundedOrder (∀ i, α' i) :=
{ Pi.orderTop, Pi.orderBot with }
instance SupSet {α : Type _} {β : α → Type _} [∀ i, SupSet (β i)] : SupSet (∀ i, β i) :=
sorry
instance InfSet {α : Type _} {β : α → Type _} [∀ i, InfSet (β i)] : InfSet (∀ i, β i) :=
sorry
instance completeLattice {α : Type _} {β : α → Type _} [∀ i, CompleteLattice (β i)] :
CompleteLattice (∀ i, β i) :=
{ Pi.boundedOrder, Pi.lattice with
le_supₛ := sorry
infₛ_le := sorry
supₛ_le := sorry
le_infₛ := sorry
}
instance frame {ι : Type _} {π : ι → Type _} [∀ i, Frame (π i)] : Frame (∀ i, π i) :=
{ Pi.completeLattice with }
instance coframe {ι : Type _} {π : ι → Type _} [∀ i, Coframe (π i)] : Coframe (∀ i, π i) :=
{ Pi.completeLattice with infᵢ_sup_le_sup_infₛ := sorry }
end Pi
-- very quick (instantaneous) in Lean 4
instance Pi.completeDistribLattice' {ι : Type _} {π : ι → Type _}
[∀ i, CompleteDistribLattice (π i)] : CompleteDistribLattice (∀ i, π i) :=
CompleteDistribLattice.mk (Pi.coframe.infᵢ_sup_le_sup_infₛ)
-- takes a long time in Lean 4 (5 seconds wall clock time on my PC) (but very quick in Lean 3)
instance Pi.completeDistribLattice'' {ι : Type _} {π : ι → Type _}
[∀ i, CompleteDistribLattice (π i)] : CompleteDistribLattice (∀ i, π i) :=
{ Pi.frame, Pi.coframe with }
-- quick Lean 3 version:
-- https://github.com/leanprover-community/mathlib/blob/b26e15a46f1a713ce7410e016d50575bb0bc3aa4/src/order/complete_boolean_algebra.lean#L210
Activity
kbuzzard commentedon Dec 24, 2022
There is some sorried data in the above; unsorrying it seems to make elaboration quicker, but it's still noticeably slow (a couple of seconds on a fast machine). I also removed everything which was a theorem, so it's only data now.
kbuzzard commentedon Dec 26, 2022
#print Pi.completeDistribLattice''shows that this is the slow declaration:Kha commentedon Dec 27, 2022
A partially unfolded trace with timings (edit: ...milliseconds, not seconds):
Kha commentedon Dec 27, 2022
The tragic line is

where we're so close
kbuzzard commentedon Dec 28, 2022
Gabriel points out on Zulip that changing the definitions of
FrameandCoframeso that they also extendLElike so:makes both instances very quick (both in the example above and in
Mathlib.Order.CompleteBooleanAlgebra, where there were three instances which were taking around 5 seconds of wall clock time each -- sorry I don't know how to profile them properly in Lean 4).kbuzzard commentedon Dec 29, 2022
Here's another one: in mathlib (where the classes are actually slightly more complex -- I didn't want to have to minimise a bunch of stuff about
Rat) this actually times out unless you increase max heartbeats.