Zulip Chat Archive

Stream: new members

Topic: How to express (-1)^n?


Alex Gu (Oct 30 2024 at 21:29):

I am trying to express (-1)^n in Lean, but having trouble because it seems not to like -1. What's the right way to express this theorem?

theorem thm (n : ) (h : n  0 [MOD 2]) :
  (-1)^n  1 [MOD 2] := by sorry

Error:

failed to synthesize
  Neg 

Yaël Dillies (Oct 30 2024 at 21:35):

[MOD _] notation is for natural number. If you want to deal with integers, you should be using [ZMOD _] notation :smile:

Adam Topaz (Oct 30 2024 at 21:41):

This isn't quite related to the question, but you should be aware that we have a docs#SignType

Adam Topaz (Oct 30 2024 at 21:42):

Depending on what your application is, it may be convenient. But if you're just doing some modular arithmetic, then sticking to integers is probably the way to go.

Alex Gu (Oct 30 2024 at 21:50):

Thanks both :) Getting a bit stuck with the proof now, any good tips here?

theorem thm (n : ) (h : n  0 [ZMOD 2]) :
  (-1)^n  1 [ZMOD 2] := by
  cases Nat.even_or_odd n with
  | inl he =>
    obtain k, hk := he
    rw [hk]
    simp [pow_mul]
  | inr ho =>

Damiano Testa (Oct 30 2024 at 21:54):

aesop can finish from there.

Alex Gu (Oct 30 2024 at 21:59):

Got it! Is there a way to do it by finding the contradiction instead of proving the theorem? (I saw the aesop proof actually ignores the contradcition and goes ahead to prove it)

Damiano Testa (Oct 30 2024 at 22:00):

What contradiction?

Damiano Testa (Oct 30 2024 at 22:01):

Oh, you mean using the redundant hypothesis?

Alex Gu (Oct 30 2024 at 22:01):

That is n = 0 mod 2, then n cannot be odd

Damiano Testa (Oct 30 2024 at 22:09):

You could do this

    exfalso
    rw [Int.modEq_iff_dvd, zero_sub, dvd_neg] at h
    norm_cast at h
    apply Nat.not_even_iff_odd.mpr ho
    refine (even_iff_exists_two_nsmul n).mpr h

Eric Wieser (Oct 30 2024 at 22:29):

You might also just want to work in ZMod 2 rather than using [ZMOD 2]

Alex Gu (Oct 30 2024 at 22:32):

What's the difference?

Floris van Doorn (Oct 30 2024 at 22:39):

With ZMod 2 you work in the type that has only two elements, 0 and 1. Working with [ZMOD 2] means working with integers, but considering an equivalence relation on it with two equivalence classes.

Alex Gu (Oct 30 2024 at 23:05):

I see, what would be the equivalent way to write it in ZMod 2 then?

Eric Wieser (Oct 30 2024 at 23:46):

theorem thm (n : ZMod 2) (h : n = 0) :
    (-1 : ZMod 2)^n.val = 1 := by
  simp [h]

Eric Wieser (Oct 30 2024 at 23:47):

Though the .val there makes it slightly debatable whether this is the same theorem.

Edward van de Meent (Oct 31 2024 at 10:00):

i don't think you even need h here? shouldn't this just be true for all n:Nat?

Edward van de Meent (Oct 31 2024 at 10:00):

because mod 2, 1=1=1n-1=1=1^n?

Edward van de Meent (Oct 31 2024 at 10:01):

or am i missing something?

Alex Mobius (Oct 31 2024 at 10:28):

I think the lower [MOD 2] is unintended, and the TS is trying to express if even n then 1 else -1

Edward van de Meent (Oct 31 2024 at 10:32):

ah... in that case, i think that the idiomatic way to express this would be this:

import Mathlib.Analysis.Normed.Field.Lemmas

def foo {n:Nat} (h:Even n) : (-1:Int) ^ n = 1 := Even.neg_one_pow h

Alex Mobius (Oct 31 2024 at 11:09):

Mathlib: there's a lemma for everything (good luck with looking for it) :)

Edward van de Meent (Oct 31 2024 at 11:30):

exact? was used in this case


Last updated: May 02 2025 at 03:31 UTC