Zulip Chat Archive
Stream: new members
Topic: State in Lean that: if set empty, then X, else Y
An Qi Zhang (Jul 18 2025 at 23:10):
Hello, if I have a Set s, and I wanted to define something different in the case s is empty and the case it's not empty, how would I state this in Lean?
As an example, consider if I wanted to map from s to an Option type. Here, if s is empty, I want to map to none. If it's not empty, I want to map to some element.
Here's an MWE:
import Mathlib
def Set.setToOption {α : Type} (s : Set α) : Option α :=
-- How should I write in Lean: "if empty, then none, otherwise, then some single element"
if IsEmpty s then -- This isn't the right way to do this
none
else
-- Want to pick out one element out of `s` (axiom of choice?)
sorry
As a side question: would this def change if I have the hypothesis that s is subsingleton? I imagine I could just use the same def?
Ruben Van de Velde (Jul 18 2025 at 23:21):
if h : s = \empty will work, though you might need to turn on classical logic
An Qi Zhang (Jul 18 2025 at 23:36):
Ah right, thanks!
Kenny Lau (Jul 19 2025 at 10:47):
@An Qi Zhang it might be useful to think in terms of algorithms to understand why this has to be noncomputable:
Set α gives you a true/false statement (i.e. Prop), so you can't really compute anything using it. I can embed Riemann's hypothesis inside a Prop, and there's no way you can decide if it's true or false, so basically anything concrete (i.e. Option α) you want to produce will have no algorithm for it
Kenny Lau (Jul 19 2025 at 10:49):
import Mathlib.Data.Set.Basic
universe u
open Classical in
noncomputable def Set.setToOption {α : Type u} (s : Set α) : Option α :=
if h : s.Nonempty then some h.some else none
Kenny Lau (Jul 19 2025 at 10:50):
I would prefer using Nonempty because it seems to have more support
Last updated: Dec 20 2025 at 21:32 UTC