Zulip Chat Archive
Stream: mathlib4
Topic: InvImage instances
Hannah Fechtner (Feb 07 2025 at 00:10):
Hi, I'm working on PR#20310 which defines the shortlex relation on lists. I'm following the given advice to prove more general theorems first, and then apply them.
The shortlex order is defined as the InvImage of something; where ought I put these instances/theorems?
theorem InvImage.trichotomous {β : Type*} [IsTrichotomous α r] {f : β → α}
(h : Function.Injective f) : ∀ a b, (InvImage r f) a b ∨ a = b ∨ (InvImage r f) b a := by
intro a b
rw [← Function.Injective.eq_iff h]
exact IsTrichotomous.trichotomous (f a) (f b)
instance InvImage.isAsymm {β : Type*} [IsAsymm α r] (f : β → α) : IsAsymm β (InvImage r f) where
asymm := fun a b h h2 => IsAsymm.asymm (f a) (f b) h h2
The most similar place looks to be https://leanprover-community.github.io/mathlib4_docs/Mathlib/Order/Defs/Unbundled.html#InvImage.trans but those lemmas (InvImage.trans
and InvImage.irreflexive
) are listed as deprecated
Eric Wieser (Feb 07 2025 at 00:16):
Can the first one be stated with IsTrichotomous
?
Hannah Fechtner (Feb 07 2025 at 00:32):
It could be, but typeclass inference won't find the hypothesis that f
is injective, so it's easier to use as a theorem. Then I can use it with apply
, and Lean will leave me with a goal to prove f
's infectivity
Eric Wieser (Feb 07 2025 at 04:18):
Usually I think we'd handle this by writing it through the typeclass but not making it an instance; but probably @Yaël Dillies and @Violeta Hernández have opinions here, and I think it's not a big deal either way.
Eric Wieser (Feb 07 2025 at 04:18):
You _could_ make it an instance for embeddings
Violeta Hernández (Feb 07 2025 at 07:25):
We really ought to have one single canonical way of stating that a relation is trichotomous/asymmetric/reflexive/whatever. For better or worse (I'd argue for better) the way most of Mathlib currently writes this down is via the Is*
typeclasses, so my recommendation is to just use those even if your theorem is not an instance.
Yaël Dillies (Feb 07 2025 at 07:33):
I also have to say that InvImage
is a weird name for a perfectly common operation on relations, and I am not sure how I feel about stating theorems using it
Violeta Hernández (Feb 07 2025 at 07:45):
There's some very useful instances on InvImage
, such as "the inverse image of a well-founded relation is well-founded".
Yaël Dillies (Feb 07 2025 at 07:50):
Precisely. That's not very useful because no one finds themselves using InvImage
. Instead in applications you rather have a subrelation s
of InvImage r f
for some r
and f
(where r
and f
are not necessarily obvious!). A lemma would be much better suited here. Back when I was doing arguments of this form, no such lemma existed so I had to resort to some obscure combination of three unfindable lemmas to do the job
Violeta Hernández (Feb 07 2025 at 07:52):
Maybe the solution is to just use InvImage
more? This seems very analogous to the preimage, which is also a very simple definition we all agree to use notation for anyways because of how many nice properties it satisfies.
Yaël Dillies (Feb 07 2025 at 07:54):
I don't see the situations as analogous. f ⁻¹ s
is genuinely a useful object people prove things about. InvImage r f
not so much. As I said, we rather prove things about a relation s
for which there exist some r
and f
such that s ≤ InvImage r f
Eric Wieser (Feb 10 2025 at 00:52):
Is InvImage r f
the same as r on f
? (edit: docs#InvImage suggests it is)
Violeta Hernández (Feb 10 2025 at 00:53):
Ok so now we know for sure one of them must go
Eric Wieser (Feb 10 2025 at 00:54):
I think in practice on
is only every used for Prop
Eric Wieser (Feb 10 2025 at 00:54):
You could check that by restricting the universe of onFun
and seeing if anything breaks
Eric Wieser (Feb 10 2025 at 00:55):
Personally I prefer the on
naming, but the notation has been controversial in the past
Eric Wieser (Feb 10 2025 at 00:55):
If nothing uses it for Sort I propose we drop that support
Eric Wieser (Feb 10 2025 at 00:56):
Nat.choose on Nat.factorial
isn't exactly obvious notation
Yaël Dillies (Feb 10 2025 at 07:35):
Eric Wieser said:
Is
InvImage r f
the same asr on f
? (edit: docs#InvImage suggests it is)
Yes, that's part of my quibble with it
Edward van de Meent (Feb 10 2025 at 13:40):
Eric Wieser said:
I think in practice
on
is only every used for Prop
I'm pretty sure it gets used in API around docs#Set.PairwiseDisjoint ?
Edward van de Meent (Feb 10 2025 at 13:44):
Ah, I was wrong. I was thinking of its definition: s.PairwiseDisjoint f := s.Pairwise (Disjoint on f)
which indeed uses it for prop
Violeta Hernández (Feb 17 2025 at 10:15):
Wait, is docs#InvImage also equal to docs#Order.Preimage ?
Violeta Hernández (Feb 17 2025 at 10:20):
audible sigh
import Mathlib
open Function
variable {α β : Type*} (r : β → β → Prop) (f : α → β)
example : (r on f) = InvImage r f := rfl
example : (r on f) = f ⁻¹'o r := rfl
Violeta Hernández (Feb 17 2025 at 10:23):
So what now?
Violeta Hernández (Feb 17 2025 at 10:54):
I think Order.Preimage
is the one of the three which makes the most sense from a didactic point of view. But unfortunately InvImage
is the one that's in Core.
Violeta Hernández (Feb 17 2025 at 10:54):
I'm testing whether onFun
is only used on Prop
here: #21982
Violeta Hernández (Feb 17 2025 at 19:13):
Yep, save for a single rfl
theorem onFun
is only ever used on relations.
Violeta Hernández (Feb 17 2025 at 19:13):
So to me it's clear two of these need to go.
Eric Wieser (Feb 18 2025 at 01:08):
InvImage works on Sort, Order.Preimage does not
Eric Wieser (Feb 18 2025 at 01:13):
So for the full comparison:
import Mathlib
universe u v
recall InvImage -- no notation
{α : Sort u} {β : Sort v} (r : β → β → Prop) (f : α → β) : α → α → Prop
recall Order.Preimage -- notation `f ⁻¹'o r`
{α : Type u_2} {β : Type u_3} (f : α → β) (r : β → β → Prop) : α → α → Prop
recall Function.onFun -- notation `r on f`
{α : Sort u₁} {β : Sort u₂} {φ : Sort u₃} (r : β → β → φ) (f : α → β) : α → α → φ
Violeta Hernández (Feb 18 2025 at 05:34):
I like the f ⁻¹o r
notation the most: it makes very clear that this is essentially a preimage construction, whereas neither InvImage
nor on
are very obvious.
Violeta Hernández (Feb 18 2025 at 05:35):
That said, if the objective is to be left with a single one of these three definitions, InvImage
probably gives us the least amount of hassle, being defined in Core Lean.
Violeta Hernández (Feb 18 2025 at 05:36):
Could we perhaps make ⁻¹o
notation for InvImage
?
Ruben Van de Velde (Feb 18 2025 at 09:07):
That sounds at least theoretically possible, not having looked at the code
Hannah Fechtner (Feb 20 2025 at 20:10):
Okay, so if I follow correctly, I'll keep using InvImage
for my PR#20310 and I'll use the IsTrichotomous
notation for the theorem
Last updated: May 02 2025 at 03:31 UTC