Zulip Chat Archive

Stream: new members

Topic: simp/ring/other should work here?


Newell Jensen (Sep 14 2022 at 17:48):

Hey all, I am confused on why simp, ring etc. don't work here. This seems like something that should be easy for the tactics to figure out. Yes I am aware that lean already has finset.sum_range_id but I wanted to play around with actually summing over the first n positive integers.

import tactic -- import all tactics

import data.nat.basic
import data.finset.basic

open_locale big_operators

example :  (n : ), ( i in finset.range (n + 1), i) = (n * (n + 1)) / 2 :=
begin
    intro n,
    induction n with n hn,
    dec_trivial,
    change ( i in finset.range (n + 2), i) = (n + 1) * ((n + 1) + 1) / 2,
    have h1 : ( i in finset.range (n + 2), i) = ( i in finset.range (n + 1), i) + (n + 1),
    exact finset.sum_range_succ (λ (x : ), x) (n + 1),
    rw [h1, hn],
    -- all manner (at least what I tried) of rw, simp, ring, ring_nf etc. doesn't seem
    -- to work here... which is confusing so think there is something more going on here
    sorry,
end

Damiano Testa (Sep 14 2022 at 17:59):

My guess, untested, is that if you coerce the inner n to , some tactic may work...

Newell Jensen (Sep 14 2022 at 18:02):

Would have thought the tactics would handle that. Thanks for the suggestion.

Damiano Testa (Sep 14 2022 at 18:04):

No, since, if the statement you wrote is true as stated, it is a fluke of how associativity works with multiplication and division. Note that you have nat-division, not actual division

Newell Jensen (Sep 14 2022 at 18:07):

Yeah understood. Need to dig into the tactics more to understand how they work as well so I am not just assuming what they should or should not be doing.

Damiano Testa (Sep 14 2022 at 18:31):

@Newell Jensen working with ℕ, somewhere along the way, a proof of the fact that n*(n+1) is even should have appeared with nat-division. This is not something any of those tactics would do.

Damiano Testa (Sep 14 2022 at 18:34):

When working with ℚ, division is the usual one and the need for evenness is no longer required. In fact, even is not really well-defined/meaningful for rational numbers.

Damiano Testa (Sep 14 2022 at 18:55):

Here is a proof using rational numbers:

example :  (n : ), ( i in finset.range (n + 1), (i : )) = (n * (n + 1)) / 2 :=
begin
  intro n,
  induction n with n hn,
  { simp },
  { rw [finset.sum_range_succ, hn],
    field_simp,
    ring },
end

Damiano Testa (Sep 14 2022 at 19:08):

Here you can see where division by 2 shows up, continuing your approach:

example :  (n : ), ( i in finset.range (n + 1), i) = (n * (n + 1)) / 2 :=
begin
    intro n,
    induction n with n hn,
    dec_trivial,
    change ( i in finset.range (n + 2), i) = (n + 1) * ((n + 1) + 1) / 2,
    have h1 : ( i in finset.range (n + 2), i) = ( i in finset.range (n + 1), i) + (n + 1),
    exact finset.sum_range_succ (λ (x : ), x) (n + 1),
    rw [h1, hn],
    --  keep massaging a little the equation
    rw [add_assoc, mul_add _ _ (1 + 1), nat.add_div_of_dvd_left, nat.mul_div_cancel],
    { ring_nf },  -- now `ring` works
    { exact zero_lt_two },  -- clean up the mess
    { exact dvd_mul_left _ _ },  -- division by 2!!
end

Last updated: Dec 20 2023 at 11:08 UTC