Zulip Chat Archive

Stream: Is there code for X?

Topic: 2 is the first prime


Ralf Stephan (May 05 2024 at 16:49):

Is there code for it? If not, I suspect it should be possible to prove

import Mathlib

example : Nat.nth Nat.Prime 0 = 2 := by
    sorry

using not_prime_zero, not_prime_one, and prime_two, but I have no idea how. Can you please help?

Yaël Dillies (May 05 2024 at 16:50):

Does rfl do it?

Yaël Dillies (May 05 2024 at 16:50):

I would check myself, but you haven't provided a #mwe

Ralf Stephan (May 05 2024 at 16:51):

type mismatch
  Iff.rfl
has type
  ?m.7574  ?m.7574 : Prop
but is expected to have type
  nth Nat.Prime 0 = 2 : Prop

Ralf Stephan (May 05 2024 at 16:52):

added the import above, sorry

Kyle Miller (May 05 2024 at 16:59):

Here's getting it into a different form that's more computational, but rfl still doesn't work:

import Mathlib

example : Nat.nth Nat.Prime 0 = 2 := by
  rw [Nat.nth_zero]
  have : Set.Nonempty {n | Nat.Prime n} := 2, Nat.prime_two
  rw [Nat.sInf_def this]
  -- ⊢ Nat.find this = 2
  sorry

Mitchell Lee (May 05 2024 at 17:19):

Here's one way

import Mathlib

example : Nat.nth Nat.Prime 0 = 2 := by
    suffices 2  Nat.nth Nat.Prime 0  Nat.nth Nat.Prime 0 < 3 by linarith
    rw [ Nat.count_le_iff_le_nth Nat.infinite_setOf_prime,  Nat.lt_nth_iff_count_lt Nat.infinite_setOf_prime]
    decide

Ralf Stephan (May 05 2024 at 17:28):

Thanks. I guess this is worth a PR? I just have the time.

Ruben Van de Velde (May 05 2024 at 17:32):

Yes, this seems fine to add

Mitchell Lee (May 05 2024 at 17:35):

Here's a potentially better way to do it

import Mathlib

theorem nth_eq_iff_of_ne_zero {p :   Prop} [DecidablePred p] {n m : } (hm : m  0) :
    Nat.nth p n = m  Nat.count p m = n  p m := by
  sorry

example : Nat.nth Nat.Prime 0 = 2 := by
  rw [nth_eq_iff_of_ne_zero]
  · decide
  · decide

Kyle Miller (May 05 2024 at 17:42):

Another approach along those lines:

theorem Nat.nth_eq_iff_of_infinite {p :   Prop} [DecidablePred p] (hi : (setOf p).Infinite) {n m : } :
    Nat.nth p n = m  Nat.count p m = n  p m := by
  constructor
  · rintro rfl
    simp [Nat.count_nth_of_infinite hi, Nat.nth_mem_of_infinite hi]
  · rintro rfl, h
    exact Nat.nth_count h

example : Nat.nth Nat.Prime 0 = 2 := by
  rw [Nat.nth_eq_iff_of_infinite Nat.infinite_setOf_prime]
  decide

Kyle Miller (May 05 2024 at 17:46):

It turns out that's more powerful than needed. This suffices:

example : Nat.nth Nat.Prime 0 = 2 := by
  convert_to Nat.nth Nat.Prime (Nat.count Nat.Prime 2) = 2
  exact Nat.nth_count Nat.prime_two

Kyle Miller (May 05 2024 at 17:47):

Or rather

example : Nat.nth Nat.Prime 0 = 2 := Nat.nth_count Nat.prime_two

Mitchell Lee (May 05 2024 at 17:53):

Here's a way to make it all decidable

import Mathlib

instance {p :   Prop} [DecidablePred p] [hp : Fact (setOf p).Infinite] {n m : } :
    Decidable (Nat.nth p n = m) :=
  decidable_of_iff (Nat.count p m  n  n < Nat.count p (m + 1)) (by
    rw [Nat.lt_nth_iff_count_lt hp.elim, Nat.count_le_iff_le_nth hp.elim,
      Nat.lt_succ_iff,  Nat.le_antisymm_iff, eq_comm])

instance : Fact (setOf Nat.Prime).Infinite := Nat.infinite_setOf_prime

example : Nat.nth Nat.Prime 0 = 2 := by decide
example : Nat.nth Nat.Prime 1 = 3 := by decide
example : Nat.nth Nat.Prime 2 = 5 := by decide
example : Nat.nth Nat.Prime 3 = 7 := by decide

Ralf Stephan (May 05 2024 at 17:55):

So this is more general, thus more preferred I guess.

Kyle Miller (May 05 2024 at 17:56):

That kind of decidable instance isn't the best, since it only applies to Nat.nth Nat.Prime n = m, and it can't be used to evaluate Nat.nth Nat.prime n inside of other expressions. (Plus, we generally don't want to add global Facts if we can help it. That Fact might be ok since it has no hypotheses.)

Ralf Stephan (May 05 2024 at 18:00):

The zeroth case suffices for me so'll first PR the one-liner.

Kyle Miller (May 05 2024 at 18:08):

There could one day either be a norm_num plugin or a simproc to calculate Nat.nth Nat.prime n in general

Ralf Stephan (May 05 2024 at 18:10):

Kyle Miller said:

Or rather

example : Nat.nth Nat.Prime 0 = 2 := Nat.nth_count Nat.prime_two

failed to synthesize instance
  DecidablePred Prime

Ralf Stephan (May 05 2024 at 18:17):

Hmm.. simply adding it and an import of Nth to Nat/Prime will give me this error, but a plain mwe will not.
Ah, I inserted too high in the file.

Mario Carneiro (May 05 2024 at 18:18):

I think a good way to write the decidable instance would be to have a class Productive p like this:

import Mathlib.Data.Nat.Nth

class Productive (p :   Prop) where
  next :   
  next_least {i n} (h : i  next n) : next n = i  p i  n  i

def Nat.nth' (p :   Prop) [Productive p] (n : ) :  :=
  Productive.next p <|
    match n with
    | 0 => 0
    | i+1 => Nat.nth' p i + 1

theorem nth_eq_nth' (p :   Prop) [Productive p] (n : ) : Nat.nth p n = Nat.nth' p n := sorry

instance (p :   Prop) [Productive p] (n a : ) : Decidable (Nat.nth p n = a) :=
  decidable_of_iff (Nat.nth' p n = a) <| by rw [nth_eq_nth']

Timo Carlin-Burns (May 07 2024 at 15:25):

Is it possible to write an efficient implementation of Productive Nat.Prime? next only gives you the largest prime you've generated so far, but you want to test divisibility against all the primes you've generated so far.


Last updated: May 02 2025 at 03:31 UTC