Zulip Chat Archive
Stream: new members
Topic: boolean indicator function
Alex Kontorovich (Jun 26 2020 at 14:41):
Really stupid question: why doesn't this work?
def divides (d n :ℕ) := ∃ ℓ , n=d*ℓ
def is_prime : ℕ → bool
:= λ p:ℕ ,
(
(2 ≤ p)
∧
(∀ m:ℕ , 2≤ m → divides m p → m = p)
)
(Something about decidability?...) Thanks!
Kevin Buzzard (Jun 26 2020 at 14:42):
import tactic
open_locale classical
def divides (d n :ℕ) := ∃ ℓ , n=d*ℓ
noncomputable def is_prime : ℕ → bool
:= λ p:ℕ ,
(
(2 ≤ p)
∧
(∀ m:ℕ , 2≤ m → divides m p → m = p)
)
Kevin Buzzard (Jun 26 2020 at 14:42):
It is computable, and things are decidable, but you would have to prove this before you can get rid of the CS boilerplate
Kevin Buzzard (Jun 26 2020 at 14:42):
Probably for Lean's actual definition of divides, this is done
Kevin Buzzard (Jun 26 2020 at 14:43):
But this is better:
def divides (d n :ℕ) := ∃ ℓ , n=d*ℓ
def is_prime : ℕ → Prop
:= λ p:ℕ ,
(
(2 ≤ p)
∧
(∀ m:ℕ , 2≤ m → divides m p → m = p)
)
Jalex Stark (Jun 26 2020 at 14:43):
if you're just doing math, you probably want a Prop
instead of bool
Jalex Stark (Jun 26 2020 at 14:43):
def is_prime (p : ℕ) : Prop :=
2 ≤ p ∧ (∀ m : ℕ, 2 ≤ m → divides m p → m = p)
Kevin Buzzard (Jun 26 2020 at 14:44):
Actually defining a function to bool forces Lean to try and figure out an algorithm
Alex Kontorovich (Jun 26 2020 at 14:44):
Ok but then how do I make an indicator function?
def is_prime_f : ℕ → ℝ
:= λ (n:ℕ ), if (is_prime n) then 1 else 0
Kevin Buzzard (Jun 26 2020 at 14:45):
Back to open_locale classical
Jalex Stark (Jun 26 2020 at 14:46):
import data.real.basic
open_locale classical
noncomputable theory
def divides (d n :ℕ) := ∃ ℓ , n=d*ℓ
def is_prime (p : ℕ) : Prop := (2 ≤ p) ∧ (∀ m:ℕ , 2≤ m → divides m p → m = p)
def is_prime_f : ℕ → ℝ
:= λ (n:ℕ ), if (is_prime n) then 1 else 0
Jalex Stark (Jun 26 2020 at 14:46):
i needed to also add noncomputable theory
Jalex Stark (Jun 26 2020 at 14:47):
but this is where it tops out, once you have both of these, lean shouldn't ask you about decidability or excluded-middle stuff unless you mention them
Jalex Stark (Jun 26 2020 at 14:47):
(i really do recommend putting the argument to the left of the colon, it helps readability a lot
Scott Olson (Jun 26 2020 at 14:48):
I think you could have a generalized def indicator (p : Prop) [decidable p] : ℝ := if p then 1 else 0
. If you use classical
this will accept any proposition.
Scott Morrison (Jun 27 2020 at 02:48):
We have talked previously about adding def boole
with exactly this definition. But I think the if
statement is short and clear so it hasn't happened.
Last updated: Dec 20 2023 at 11:08 UTC