Zulip Chat Archive

Stream: maths

Topic: -1 raised to an integer


Enrico Borba (Jan 20 2020 at 17:28):

What's the best way of writing (-1) ^ n where n : int? You can't just write it as is since has_pow int int doesn't exist (and shouldn't of course). Is there some construct in mathlib for this?

Alex J. Best (Jan 20 2020 at 17:32):

I guess the best way depends on your application, but the reason -1 to a power is allowed is because -1 is a unit in the ring Z so one way would be

variable (n : )
#check (-1 : units ) ^ n
#eval (-1 : units ) ^ (-4 : )

Enrico Borba (Jan 20 2020 at 17:34):

Oh very interesting. I'll try this. Thank you

Enrico Borba (Jan 20 2020 at 20:31):

Hmm, is it obvious why ring_exp can't handle this?

example (n : ℕ) : ((-1 : units ℤ) ^ (n + 1)) = (-1 : units ℤ) * ((-1 : units ℤ) ^ n) := by ring_exp

Mario Carneiro (Jan 20 2020 at 20:39):

units ℤ isn't a ring

Mario Carneiro (Jan 20 2020 at 20:41):

it's also overkill for this example. There is a lemma that says exactly this; use library_search to find it

Enrico Borba (Jan 20 2020 at 20:54):

library_search suggests I just use rfl, which I guess makes sense depending on how pow is implemented on succ n. Thanks!

Scott Carnahan (Jul 28 2023 at 15:46):

I'm not sure if I should make a new topic instead of joining a 3.5 year old conversation, but I thought I should note that I tried the above solution in mathlib4 without success. I was able to make things work with (-1: ℤˣ)^n but multiplying with elements of other rings seemed to require an additional cast, e.g., (((-1: ℤˣ)^n):ℤ)

Anatole Dedecker (Jul 28 2023 at 15:56):

Could you write a #mwe of what fails precisely?

Kyle Miller (Jul 28 2023 at 15:59):

You are probably looking for the local macro_rules | ($x ^ $y) => (HPow.hPow $x $y) workaround. Lean 4 has a system to try to cast everything in an arithmetic expression to a single type, but this doesn't work well for exponentiation at the moment. The macro_rules incantation turns this feature off for exponentiation.

Scott Carnahan (Jul 28 2023 at 16:04):

import Mathlib.Algebra.Module.Basic

#check (-1: ˣ)^(-2: )
#check (-1: units )^(-1: ) -- unknown identifier 'units'

Am I missing an import?

Matthew Ballard (Jul 28 2023 at 16:05):

It is Units in Lean 4

Anatole Dedecker (Jul 28 2023 at 16:05):

ℤˣ is Units ℤ by definition

Scott Carnahan (Jul 28 2023 at 16:05):

Oh, Units works. Thanks a lot!


Last updated: Dec 20 2023 at 11:08 UTC