Zulip Chat Archive

Stream: general

Topic: Need help with a recursive function


Notification Bot (Apr 07 2022 at 21:27):

Aaron Bies has marked this topic as unresolved.

Aaron Bies (Apr 07 2022 at 21:27):

I'm afraid I'm stuck again,

I tried to prove that shift always returns an odd number unless the input was 0, but I keep getting unexpected occurrence of recursive function and I don't know what it wants me to do.

(I'm sorry this is a lot of code)

my code

Reid Barton (Apr 07 2022 at 21:35):

I suspect finish is using the lemma itself for the same n

Reid Barton (Apr 07 2022 at 21:36):

There might be similar issues with simp * at *

Aaron Bies (Apr 07 2022 at 21:42):

Those are good points

Reid Barton (Apr 07 2022 at 21:42):

I never really understood how to mix the equation compiler and tactic proofs, personally I think it's easier to go about it this way:

theorem shift_odd (n : ) : n = 0  (shift n) % 2  0 :=
begin
  apply nat.strong_induction_on n, intros n rec,
  by_cases hb : n > 1  n % 2 = 0,
  {
    cases hb with h1 h2,
    right,

    let k := n / 2,
    have nk : n = 2 * k := begin
      have kd : k = n / 2 := rfl,
      rw kd,
      rw nat.mul_div_cancel',
      rw nat.dvd_iff_mod_eq_zero,
      exact h2,
    end,

    -- aids the proof that recurion is well-founded
    have term : k < n := begin
      refine nat.div_lt_self _ _,
      { nlinarith, },
      { rw [nat.one_lt_bit0_iff], }
    end,

    specialize rec k term,

    simp * at *,

    -- recursion happens here
    rw or_iff_not_imp_left at rec,
    rw shift_mul_2,
    apply rec,

    linarith,
  }, {
    have h := shift_base n hb,
    rw h,
    sorry -- obviously follows from hb somehow
  },
end

Aaron Bies (Apr 07 2022 at 21:48):

oooh I was not aware of nat.strong_induction_on

Aaron Bies (Apr 07 2022 at 21:48):

Thank you very much!

Eric Wieser (Apr 07 2022 at 21:51):

induction n using nat.binary_rec might yield a shorter proof, but I haven't tried


Last updated: Dec 20 2023 at 11:08 UTC