Zulip Chat Archive
Stream: mathlib4
Topic: WellFoundedLT Prop is not found when importing too much
Yaël Dillies (Nov 15 2024 at 12:39):
As per the title,
import Mathlib
#synth WellFoundedLT Prop
#synth WellFoundedLT Bool
doesn't work even though the following does
import Mathlib.Data.Fintype.Card
#synth WellFoundedLT Prop
#synth WellFoundedLT Bool
Eric Wieser (Nov 15 2024 at 12:51):
import Mathlib
attribute [-instance]
WellFounded.wellOrderExtension.isWellFounded_lt -- deprecated
IsWellFounded.wellOrderExtension.isWellOrder_lt
IsWellFounded.wellOrderExtension.isWellFounded_lt -- comment this one out to see a failure
set_option trace.Meta.synthInstance true
#synth WellFoundedLT Prop
fixes it
(docs#IsWellFounded.wellOrderExtension.isWellFounded_lt)
Eric Wieser (Nov 15 2024 at 12:54):
I don't understand what the :explosion: in set_option trace.Meta.synthInstance true
is telling me
Jannis Limperg (Nov 15 2024 at 13:08):
This usually indicates that an exception was thrown.
Yury G. Kudryashov (Nov 15 2024 at 14:28):
Should we rewrite wellOrderExtension
to a type synonym WellOrderExtension α r
with LinearOrder
etc instances on this type synonym?
Yaël Dillies (Nov 15 2024 at 14:31):
My personal solution to fixing the issue is adding a shortcut instance for WellFoundedLT Prop
. Here is a first step: #19086
Eric Wieser (Nov 15 2024 at 14:35):
My initial impression is that mathlib is not to blame here
Kim Morrison (Nov 15 2024 at 19:44):
Minimising this would be helpful!
Yury G. Kudryashov (Nov 15 2024 at 20:55):
My guess is that Lean tries isDefEq
on this instance (because it's about LT.lt
), then something goes wrong. Should we remove more instances that make structural assumptions about instance arguments?
Eric Wieser (Nov 15 2024 at 21:58):
For a long time in mathlib we've assumed it's ok to write instances of the form
instance outerInstance : @SomeClass X instSomeNonCanonicalInstanceOnX
with the premise that outerInstance
is about instSomeNonCanonicalInstanceOnX
not X
, and so it does not harm for it to be available globally
Eric Wieser (Nov 15 2024 at 21:59):
(this comes up, for instance, with "this non-canonical MulAction satisfies SMulCommClass with the canonical one")
Yury G. Kudryashov (Nov 16 2024 at 07:25):
But it means that Lean will try it every time and try to unify instSomeNonCanonicalInstanceOnX
with the generated instance.
Yury G. Kudryashov (Nov 16 2024 at 07:26):
(BTW, did it change during Lean3->Lean4 migration?)
Junyan Xu (Nov 16 2024 at 09:53):
One example is IsScalarTower.of_algHom.
Jovan Gerbscheid (Nov 17 2024 at 03:51):
This reminds me of another type class failure. It turns out that turning off unification hints solves the problem. This is a bug where the discrimination tree used in unification hints can throw Meta.throwIsDefEqStuck
. That is why you see the :explosion: in set_option trace.Meta.synthInstance true
.
import Mathlib
open Lean Meta Elab Command
syntax (name := ha) "#mysynth" term : command
@[command_elab ha] def elabSynth : CommandElab := fun stx => do
let term := stx[1]
withoutModifyingEnv <| runTermElabM fun _ => Term.withDeclName `_synth_cmd do
withConfig ({· with unificationHints := false}) do -- turn off unification hints
let inst ← Term.elabTerm term none
Term.synthesizeSyntheticMVarsNoPostponing
let inst ← instantiateMVars inst
let val ← synthInstance inst
logInfo val
pure ()
-- succeeds
#mysynth WellFoundedLT Prop
#mysynth WellFoundedLT Bool
Jovan Gerbscheid (Nov 17 2024 at 03:56):
This should be easy to fix by setting Config.isDefEqStuckEx
to false when looking up in the discrimination tree when using unificationHints.
Violeta Hernández (Nov 17 2024 at 05:49):
Yury G. Kudryashov said:
Should we rewrite
wellOrderExtension
to a type synonymWellOrderExtension α r
withLinearOrder
etc instances on this type synonym?
Note docs#WellOrderExtension already exists
Violeta Hernández (Nov 17 2024 at 05:50):
And yes, I think a type synonym with an actual LinearOrder
instance would be better here. Why use unbundled relations when we don't need to?
Violeta Hernández (Nov 17 2024 at 05:54):
Actually, docs#WellOrderExtension already has a linear order instance
Yaël Dillies (Nov 17 2024 at 09:22):
I also just found #18217, which introduces the following two looping instances:
instance (priority := 100) isWellOrder_lt [LinearOrder α] [WellFoundedLT α] :
IsWellOrder α (· < ·) where
instance (priority := 100) isWellOrder_gt [LinearOrder α] [WellFoundedGT α] :
IsWellOrder α (· > ·) where
Yaël Dillies (Nov 17 2024 at 09:23):
([IsWellOrder α (· < ·)] → WellFoundedLT α
is an instance)
Kevin Buzzard (Nov 17 2024 at 09:28):
Loops are OK in some cases, right?
Yaël Dillies (Nov 17 2024 at 09:33):
Yes but here WellFoundedLT α
and IsWellOrder α (· < ·)
take or refer to a LT
argument which is unlikely to be exactly the same between two rounds of the loop. Indeed, trying #synth WellFoundedLT Prop
in Data.Fintype.Card
I saw the loop do several rounds before stabilising
Violeta Hernández (Nov 18 2024 at 01:56):
That's unfortunate, as these instances have to be instantiated separately in a couple of places otherwise
Violeta Hernández (Nov 18 2024 at 01:58):
Wait, what two distinct LTs are there for Prop
?
Jovan Gerbscheid (Nov 18 2024 at 19:41):
I have minimized the bug, and I was about to open an issue, but as of today, this minimized example does the expected behaviour on the nightly version of lean: it just so happens to have gotten fixed by lean#6053.
Here's the minimization:
class Inhabited' (α : Type) where
default' : α
class A (α : Type) [Inhabited' α]
opaque opaqueProp : Prop
open Classical in
instance {α : Type} [Inhabited α] : Inhabited' α where
default' := if opaqueProp then default else default
instance badInst (α : Type) [Inhabited α] : A α where
instance {α : Type} [Inhabited α] : Inhabited' α where
default' := default
instance (priority := 1) goodInst (α : Type) [Inhabited α] : A α where
set_option trace.Meta.synthInstance true
#synth A Nat
Yaël Dillies (Nov 18 2024 at 20:05):
Thanks Jovan for the minimization, and Leo for always being a step ahead
Violeta Hernández (Nov 19 2024 at 02:49):
Glad to hear I didn't accidentally screw things up
Last updated: May 02 2025 at 03:31 UTC