Zulip Chat Archive

Stream: Is there code for X?

Topic: The minimum of a `Set` if it exists else `bot`


Ayhon (Jan 27 2026 at 10:09):

In particular, I'm searching for a function of signature Set.min: (s: Set α) → s ≠ ∅ → WithBot α, defined as bot if the set is unbounded. Looking around I haven't managed to find it, but in case it is not defined elsewhere I would be interested in knowing how to best define it with the available definitions.

I was thinking something along the lines of

noncomputable
def Set.min{α: Type}[PartialOrder α](s: Set α)(s_nonempty: s  ): WithBot α :=
  if h:  (a: α), a =  s then
    a
  else
    

However, here I'm unable to picka out of the existential h (maybe with some Classical lemma?), and the BigOp doesn't seem to be defined.

Ayhon (Jan 27 2026 at 10:17):

Closest I've gotten so far (playground):

import Mathlib

noncomputable
def Set.min{α: Type}[PartialOrder α](s: Set α)(h: s  ): WithBot α :=
  let lbs: Type :=  {x : α //  z  s, x  z}
  let mins : Type := {x : lbs // IsMin x}
  have : Decidable (Nonempty mins) := Classical.propDecidable _
  if h: Nonempty mins then
    let x := Classical.choice h
    x.val.val
  else
    

Wrenna Robson (Jan 27 2026 at 10:27):

So this is a thing that it is natural to want but is difficult for the reasons you have noted.

Ayhon (Jan 27 2026 at 10:30):

Actually, I've found a way of picking out of the existential with Classical.choose. This is what I have so far

open Classical in
noncomputable
def Set.min{α: Type}[PartialOrder α](s: Set α)(h: s  ): WithBot α :=
  if h:  x, ( z  s, x  z)  ( x', ( z  s, x'  z)  x'  x ) then
    (Classical.choose h: α)
  else
    

I'm just lacking some way of stating that x is a minimum a bit more nicely.

Ayhon (Jan 27 2026 at 10:32):

And I think that might be IsLeast :thinking:

Wrenna Robson (Jan 27 2026 at 10:34):

I think so

Ayhon (Jan 27 2026 at 10:34):

Ok, this is my current best. I opted to go for IsLUB in the end

import Mathlib

open Classical in
noncomputable
def Set.min{α: Type}[PartialOrder α](s: Set α)(_: s  ): WithBot α :=
  if h :  (x: α), IsLUB s x then
    (Classical.choose h: α)
  else
    

Wrenna Robson (Jan 27 2026 at 10:35):

That is not right.

Wrenna Robson (Jan 27 2026 at 10:35):

IsLUB doesn't make it a minimum

Ayhon (Jan 27 2026 at 10:35):

Oh, silly me

Ayhon (Jan 27 2026 at 10:35):

IsGLB

Wrenna Robson (Jan 27 2026 at 10:35):

No, the same

Wrenna Robson (Jan 27 2026 at 10:35):

IsLeast was correct

Wrenna Robson (Jan 27 2026 at 10:35):

IsLeast.csInf_eq

Wrenna Robson (Jan 27 2026 at 10:35):

See this

Wrenna Robson (Jan 27 2026 at 10:36):

which says that in a conditionally complete lattice, least elements are infimums

Wrenna Robson (Jan 27 2026 at 10:36):

IsLeast.csInf_mem

Ayhon (Jan 27 2026 at 10:36):

Hmmm

Wrenna Robson (Jan 27 2026 at 10:36):

And that the infimum is in the set

Wrenna Robson (Jan 27 2026 at 10:36):

Basically a minimum is an infimum which is in the set.

Wrenna Robson (Jan 27 2026 at 10:36):

BUT I do not think "bot" is a good choice for the default

Wrenna Robson (Jan 27 2026 at 10:37):

really what you want to do is either work in a (conditionally) complete lattice, OR embed into the lattice completion

Wrenna Robson (Jan 27 2026 at 10:38):

@Violeta Hernández Sorry to bother you - how do we spell the lattice completion again? We use Concept, right?

Ayhon (Jan 27 2026 at 10:38):

Maybe the issue is with the name then. I only need this to work with integers, picking the least element in the set or -\infty if its unbounded

Wrenna Robson (Jan 27 2026 at 10:38):

OK, that is much more reasonable as an ask :)

Ayhon (Jan 27 2026 at 10:38):

I wanted my definition to be general to hopefully make use of theorems in Mathlib, if present

Ayhon (Jan 27 2026 at 10:39):

But I'm just hoping to reason about intervals of extended integers WithTop (WithBot Int)

Wrenna Robson (Jan 27 2026 at 10:39):

Aye.

Ayhon (Jan 27 2026 at 10:39):

Why is my original ask problematic?

Wrenna Robson (Jan 27 2026 at 10:40):

I guess my advice is that it is somewhat unergonomic - it is not a good way to use existing mathlib stuff. I speak from experience!

Violeta Hernández (Jan 27 2026 at 10:40):

Wrenna Robson said:

Violeta Hernández Sorry to bother you - how do we spell the lattice completion again? We use Concept, right?

Yes, the Dedekind completion is the concept lattice of ≤. I have a sequence of PRs building up to that but the first one has been stuck for months because of some benchmarking shenanigans.

Wrenna Robson (Jan 27 2026 at 10:40):

Alas

Ayhon (Jan 27 2026 at 10:41):

I see

Wrenna Robson (Jan 27 2026 at 10:41):

Once we do have it, I do think a function that maps the inf/sup of a set into it would be good.

Wrenna Robson (Jan 27 2026 at 10:41):

Ayhon said:

I see

We do have ENat but we don't seem to have the extended integers.

Wrenna Robson (Jan 27 2026 at 10:41):

I assume there is a reason for this.

Violeta Hernández (Jan 27 2026 at 10:41):

Ayhon said:

But I'm just hoping to reason about intervals of extended integers WithTop (WithBot Int)

There's an open PR introducing the abbreviation EInt = WithBot (WithTop Int). I can find it once I'm on my computer.

Ayhon (Jan 27 2026 at 10:41):

There's actually a PR on its way

Ayhon (Jan 27 2026 at 10:42):

Yep, it was actually you, @Violeta Hernández, who mentioned it in a Zulip thread I posted a while back

Ayhon (Jan 27 2026 at 10:42):

There's also some added lemmas for WithTop (WithBot ·), if I remember correctly (up to correct ordering of WithTop/WithBot)

Violeta Hernández (Jan 27 2026 at 10:43):

I asked the PR author to move WithBotTop earlier so it could be used in the Order.WithBot file, but I think they've not yet done that

Wrenna Robson (Jan 27 2026 at 10:43):

Ah great.

Violeta Hernández (Jan 27 2026 at 10:43):

Yes, having a canonical spelling of "add both a bot and a top" is part of the reason I want this

Wrenna Robson (Jan 27 2026 at 10:43):

Yeah it makes sense

Wrenna Robson (Jan 27 2026 at 10:44):

you don't want to worry about the order

Ayhon (Jan 27 2026 at 10:44):

Violeta Hernández said:

I asked the PR author to move WithBotTop earlier so it could be used in the Order.WithBot file, but I think they've not yet done that

Are open PRs open to PRs?

Violeta Hernández (Jan 27 2026 at 10:45):

I think it's best to just ask the author directly. Or open a new PR with their permission.

Ayhon (Jan 27 2026 at 10:46):

I'll just leave a message letting them know I'd be willing to look into the final bit then

Ayhon (Jan 27 2026 at 10:48):

Actually, it seems like it's done, no? (#33876)

Violeta Hernández (Jan 27 2026 at 11:00):

I want WithBotTop to be available in the Order.WithBot file, but the PR currently defines it in a new file which then imports that one.

Violeta Hernández (Jan 27 2026 at 11:02):

I'll try this myself

Violeta Hernández (Jan 27 2026 at 11:42):

Of course I immediately run into problems

Violeta Hernández (Jan 27 2026 at 11:42):

I'll open a new thread about this

Ayhon (Feb 04 2026 at 23:34):

About the original question, I'm considering whether I should define my Set.min? as
sInf (WithBot.some '' s) by defining an instance of InfSet (WithBot α) whenever α has [LinearOrder α] [PredOrder α] [IsPredArchimedean α], so that I can apply BddBelow.exists_isLeast_of_nonempty on it. But this would basically mean defining InfSet.sInf to be what I already have, so I'm not sure it's worth it.

I just thought I'd share


Last updated: Feb 28 2026 at 14:05 UTC