Zulip Chat Archive

Stream: new members

Topic: Base Case for Induction on N+


Alex Gu (Jan 09 2025 at 19:33):

Hi! I want to do induction on the positive natural numbers, but couldn't out the right syntax for the base case. I also couldn't manage to find it through searching Zulip and PNat's library. Thanks!

import Mathlib

theorem sum_first_n_odd_integers (n : +) :
  ( k in Finset.range n, 2*k + 1) = n^2 := by
  induction n with
  | ?? =>
    sorry

Riccardo Brasca (Jan 09 2025 at 19:36):

Not at the computer at the moment, but if you write induction n is there a yellow bulb in VS Code? If it's there click on "generate skeleton..." and it should do it for you.

Alex Gu (Jan 09 2025 at 19:38):

I didn't see it, maybe I didn't install some extension?

image.png

Edward van de Meent (Jan 09 2025 at 19:38):

an issue you'll run into here is that PNat is not an inductive type...

Edward van de Meent (Jan 09 2025 at 19:38):

rather, it's the subtype with 0 < n

Etienne Marion (Jan 09 2025 at 19:39):

Yes technically it is an inductive type but not in the same way as Nat

David Renshaw (Jan 09 2025 at 19:39):

You might try PNat.strongInductionOn

David Renshaw (Jan 09 2025 at 19:40):

induction n using PNat.strongInductionOn ...

Edward van de Meent (Jan 09 2025 at 19:40):

ah, i just found PNat.recOn

Alex Gu (Jan 09 2025 at 19:41):

what would be my base case for strong induction on PNat? how could I find that information?

Edward van de Meent (Jan 09 2025 at 19:43):

i suspect that in this case you are better helped with the following:

import Mathlib

theorem sum_first_n_odd_integers (n : +) :
    ( k in Finset.range n, 2*k + 1) = n^2 := by
  induction n using PNat.recOn with
  | p1 =>
    sorry
  | hp n hi =>
    sorry

Edward van de Meent (Jan 09 2025 at 19:45):

a friendly reminder that sometimes inductive principles are not called induction or similar because it can construct data. Instead those are named rec or similar.

Edward van de Meent (Jan 09 2025 at 19:46):

see also #naming

Etienne Marion (Jan 09 2025 at 19:47):

@loogle PNat, "rec"

loogle (Jan 09 2025 at 19:47):

:search: PNat.recOn, PNat.recOn_one, and 3 more

Etienne Marion (Jan 09 2025 at 19:47):

Indeed I forgot about rec!

Riccardo Brasca (Jan 09 2025 at 20:01):

Out of curiosity, does the yellow bulb work after induction n with...?

Kyle Miller (Jan 09 2025 at 20:04):

If someone were to add @[induction_eliminator] to PNat.recOn then it would automatically be used by the induction tactic. (It would be nice to adjust the names of the parameters too, perhaps one and succ?)

Alex Gu (Jan 09 2025 at 20:07):

thx! this is very helpful and I was able to finish the proof from here.

theorem sum_first_n_odd_integers (n : +) :
    ( k in Finset.range n, (2*k + 1)) = n^2 := by
  induction n using PNat.recOn with
  | p1 =>
    simp
  | hp n hi =>
    have h1 : (n + 1 : +) = (n : ) + 1 := by simp
    rw [h1]
    rw [Finset.sum_range_succ]
    rw [hi]
    ring

could you help me understand why for PNat, it has to be done in this way, while for natural numbers it's normally done with the constructor like zero and succ n ih?

Alex Gu (Jan 09 2025 at 20:07):

@Riccardo Brasca honestly i've never seen the yellow bulb, not sure how to get it

Kyle Miller (Jan 09 2025 at 20:07):

induction looks at the names of the parameters of the induction principle. They happen to be the names of constructors when the induction principle is generated from the inductive type.

Riccardo Brasca (Jan 09 2025 at 20:08):

You can try it with natural numbers, it should work out of the box

Kyle Miller (Jan 09 2025 at 20:10):

Here's where zero and succ come from:

#check Nat.recOn
/-
Nat.recOn.{u} {motive : ℕ → Sort u} (t : ℕ)
  (zero : motive Nat.zero) (succ : (n : ℕ) → motive n → motive n.succ) :
  motive t
-/

(Though more precisely, we use Nat.recAuxOn so that the result is in terms of 0 and n + 1 instead of Nat.zero and Nat.succ n.)

Kyle Miller (Jan 09 2025 at 20:10):

PNat.recOn uses p1 and hp for its parameter names, hence my suggestion that we rename them to something better.

Etienne Marion (Jan 09 2025 at 20:11):

Riccardo Brasca said:

Out of curiosity, does the yellow bulb work after induction n with...?

In the web editor it gives

import Mathlib

example (n : PNat) : True := by
  induction n with
  | mk val property => sorry

so no

Kyle Miller (Jan 09 2025 at 20:14):

Technically that's working. It's using the "induction principle" that comes from the definition of PNat.

Kyle Miller (Jan 09 2025 at 20:15):

val is the natural number, and property is that it's positive.

Kyle Miller (Jan 09 2025 at 20:15):

If you add in the using PNat.recOn clause to induction you can switch it to the more sensible induction principle

Riccardo Brasca (Jan 09 2025 at 20:17):

I mean if you write explicitly the induction principle you want to use

Etienne Marion (Jan 09 2025 at 20:17):

Ah sorry I misunderstood

Etienne Marion (Jan 09 2025 at 20:18):

so yes it works

Alex Gu (Jan 09 2025 at 20:18):

so if i wanted to use natural numbers, but want to start at, say, 3 rather than 0, what would be the way to do that? (e.g. if n>=3 is one of my hypothesis)

Edward van de Meent (Jan 09 2025 at 20:19):

Nat.le_induction

Edward van de Meent (Jan 09 2025 at 20:20):

Nat.leRec

Etienne Marion (Jan 09 2025 at 20:20):

Kyle Miller said:

If someone were to add @[induction_eliminator] to PNat.recOn then it would automatically be used by the induction tactic. (It would be nice to adjust the names of the parameters too, perhaps one and succ?)

#20617

Julian Berman (Jan 09 2025 at 20:53):

Kyle Miller said:

Technically that's working. It's using the "induction principle" that comes from the definition of PNat.

Just to ask explicitly -- it's not easily feasible to make separate code actions for all induction principles in the environment whose type somehow matches the term being passed to induction is it?

Kyle Miller (Jan 09 2025 at 21:00):

Oh, you mean like "populate code actions with a list of all relevant induction principles?"

Kyle Miller (Jan 09 2025 at 21:01):

Worst case maybe there could be an attribute to tag them, like induction_eliminator?

Julian Berman (Jan 09 2025 at 21:21):

Exactly yeah.


Last updated: May 02 2025 at 03:31 UTC