Zulip Chat Archive
Stream: new members
Topic: How to tell Lean to interpret all numbers as `Real`s?
Ilmārs Cīrulis (Dec 09 2024 at 19:45):
Is it possible to tell Lean (with some command) that I want all numbers to be interpreted as ℝ
?
Ilmārs Cīrulis (Dec 09 2024 at 19:46):
With exceptions marked with specific type, if necessary.
Ilmārs Cīrulis (Dec 09 2024 at 20:27):
Ok, I looked at all commands here and found nothing like that.
So, probably answer is no. At least if no metaprogramming (?) is used.
Notification Bot (Dec 09 2024 at 20:27):
Lessness has marked this topic as resolved.
Kyle Miller (Dec 09 2024 at 20:41):
You can add an OfNat default instance for reals, but this cannot be scoped to a particular module — it will affect all modules that import your module too.
Ilmārs Cīrulis (Dec 11 2024 at 09:44):
Kyle Miller said:
You can add an OfNat default instance for reals, but this cannot be scoped to a particular module — it will affect all modules that import your module too.
Maybe completely noob question, but how I do that? :sweat_smile:
Notification Bot (Dec 11 2024 at 09:44):
Lessness has marked this topic as unresolved.
Ilmārs Cīrulis (Dec 11 2024 at 09:46):
And, is there something similar for interpreting all numbers with decimal points (1.23
etc.) as Real
?
Ilmārs Cīrulis (Dec 11 2024 at 14:49):
Thanks, discorders. This works for all natural numbers equal to at least 2.
import Mathlib
@[default_instance]
instance NatToReal (n : Nat) [n.AtLeastTwo]: OfNat Real n := ⟨↑n⟩
Daniel Weber (Dec 11 2024 at 15:01):
Lessness said:
And, is there something similar for interpreting all numbers with decimal points (
1.23
etc.) asReal
?
I think you can do the same for docs#OfScientific
Ilmārs Cīrulis (Dec 11 2024 at 17:44):
Yes, thank you! It seems that my try was successful. :partying_face:
(I kinda cheated a bit, but now everything works.)
import Mathlib
@[default_instance]
instance NatToReal (n: Nat) [n.AtLeastTwo]: OfNat Real n := ⟨↑n⟩
@[default_instance]
noncomputable instance ScientificToReal: OfScientific Real where
ofScientific m s e := ↑(Rat.ofScientific (OfNat.ofNat m) s (OfNat.ofNat e))
def ex: - 37 / 2 = - 18.5 := by
norm_num
Eric Wieser (Dec 11 2024 at 18:34):
Should mathlib override the default meaning of 0.5
to be Rat
/NNRat
instead of Float
?
Daniel Weber (Dec 11 2024 at 18:37):
I think that's a good idea for mathlib itself, but this might hurt dependencies
Ilmārs Cīrulis (Dec 11 2024 at 21:00):
Just in case, if someone wants to know how to do instances for 0 and 1 too:
import Mathlib
@[default_instance]
instance NatToReal (n: Nat) [n.AtLeastTwo]: OfNat Real n := ⟨↑n⟩
@[default_instance]
instance ZeroToReal: Zero Real where
zero := @OfNat.ofNat ℝ 0 Zero.toOfNat0
@[default_instance]
instance OneToReal: One Real where
one := @OfNat.ofNat ℝ 1 One.toOfNat1
@[default_instance]
noncomputable local instance ScientificToReal: OfScientific Real where
ofScientific m s e := ↑(Rat.ofScientific (OfNat.ofNat m) s (OfNat.ofNat e))
Notification Bot (Dec 11 2024 at 21:00):
Lessness has marked this topic as resolved.
Notification Bot (Dec 11 2024 at 21:29):
Lessness has marked this topic as unresolved.
Ilmārs Cīrulis (Dec 11 2024 at 21:30):
Yeah, no. I'm still dumb. :| The 0 and 1 doesn't work if without neighboring Real numbers that force them to be Real.
import Mathlib
@[default_instance]
instance NatToReal (n: Nat) [n.AtLeastTwo]: OfNat Real n := ⟨↑n⟩
@[default_instance]
instance ZeroToReal: Zero Real where
zero := @OfNat.ofNat ℝ 0 Zero.toOfNat0
@[default_instance]
instance OneToReal: One Real where
one := @OfNat.ofNat ℝ 1 One.toOfNat1
@[default_instance]
noncomputable instance ScientificToReal: OfScientific Real where
ofScientific m s e := ↑(Rat.ofScientific (OfNat.ofNat m) s (OfNat.ofNat e))
def p1: Prop := (- 1.0) ^ (- 0.0) = 1.0
def p2: Prop := (- 1) ^ (- 0) = 1
-- causes error
Kyle Miller (Dec 11 2024 at 21:43):
Maybe you could give a HomogeneousPow Real
instance?
Ilmārs Cīrulis (Dec 11 2024 at 21:44):
Yes, it seems that ^
is the reason. Now trying to make such instance, hopefully, it will be success.
Ilmārs Cīrulis (Dec 11 2024 at 21:50):
@[default_instance]
noncomputable instance RealPow: HPow Real Real Real where
hPow := Real.rpow
Success!
Ilmārs Cīrulis (Dec 11 2024 at 21:55):
For now everything seems to be working correctly, yay :partying_face:
Last updated: May 02 2025 at 03:31 UTC