Zulip Chat Archive
Stream: general
Topic: Choice is computable!
Alex Meiburg (Apr 03 2024 at 04:20):
Good news guys -- Choice can now be built in an entirely computable fashion! :) Behold the demonstration below:
import Mathlib
-- Multi-sorted version of `Option`
inductive POption (α : Sort u) where
/-- No value. -/
| none : POption α
/-- Some value of type `α`. -/
| some (val : α) : POption α
instance : Inhabited (POption α) := ⟨POption.none⟩
noncomputable instance aux_1_sound {α : Sort u} (p : { p' : α → Prop // ∃ x, p' x}) :
Inhabited {x // p.val x} :=
let h : Nonempty (Subtype _) := Exists.elim p.property (fun w h => ⟨w, h⟩);
Classical.inhabited_of_nonempty h
partial def aux_1 {α : Sort u} (p : { p' : α → Prop // ∃ x, p' x}) (f₁ : Prop → POption {x // p.val x}
:= (inferInstance : Inhabited _).default) : {x // p.val x} :=
conf (type_of% @proof_irrel_heq) where --proof irrelevance
conf (q : Prop) : {x // p.val x} :=
match f₁ q with
| POption.some x => x
| _ => conf (¬q ∨ q) --law of excluded middle
--and now the main event:
/-computable-/
def computable_indefiniteDescription {α : Sort u} (p : α → Prop) (h : ∃ x, p x) : {x // p x} :=
aux_1 ⟨p,h⟩
/-computable-/
def computable_choose {α : Sort u} {p : α → Prop} (h : ∃ x, p x) : α :=
(computable_indefiniteDescription p h).val
theorem computable_choose_spec {α : Sort u} {p : α → Prop} (h : ∃ x, p x) : p (computable_choose h) :=
(computable_indefiniteDescription p h).property
--double check that the new `computable_choose` is identical in type to `Classical.choose`:
#check (@Classical.choose : type_of% @computable_choose)
#check (@computable_choose : type_of% @Classical.choose)
My computable_choose
isn't marked noncomputable
, and as we can see it has the exact same type signature as that stinky old noncomputable Classical.choose
. And it all typechecks and compiles fine without any sorry
s or extra axioms :tada: !
(I know I'm about two days overdue here, but I only had the idea just now... :relieved: )
EDIT: an older version of this was about the law of the excluded middle, preserved below so that the following few messages make sense, but this was an error.
old text
Kim Morrison (Apr 03 2024 at 04:26):
I'm not sure what you're claiming here. You know you can write def computable_em' := Classical.em
?
Matt Diamond (Apr 03 2024 at 04:27):
it's a belated April Fool's joke, though Scott's point deflates it a bit
Kim Morrison (Apr 03 2024 at 04:28):
Oops, I'm terrible at noticing April. :-)
Alex Meiburg (Apr 03 2024 at 05:15):
Alright, I admit I kind of botched the joke there since the law of the excluded middle is computable. I think the new version is more surprising / entertaining -- that I can make an entirely computable version of Classical.choose
. :)
Mario Carneiro (Apr 03 2024 at 07:43):
the trick
Last updated: May 02 2025 at 03:31 UTC