Skip to content

[Merged by Bors] - feat(Data/Real): representation of reals from [0, 1] in positional system#26021

Closed
vasnesterov wants to merge 24 commits intoleanprover-community:masterfrom
vasnesterov:ofDigits
Closed

[Merged by Bors] - feat(Data/Real): representation of reals from [0, 1] in positional system#26021
vasnesterov wants to merge 24 commits intoleanprover-community:masterfrom
vasnesterov:ofDigits

Conversation

@vasnesterov
Copy link
Copy Markdown
Collaborator

@vasnesterov vasnesterov commented Jun 17, 2025

  • Introduce ofDigits {b : ℕ} (digits : ℕ → Fin b) : ℝ, representing a real number of the form 0.d₀d₁d₂... (where dᵢ = digits i) as an infinite sum.
  • Prove that this sum converges to a number in the interval [0, 1].
  • Prove ofDigits_close_of_common_prefix: if the first n digits of two numbers are equal, then their difference is bounded by b⁻ⁿ.
  • Introduce Real.digits, which converts a real number into its sequence of digits.
  • Prove that ofDigits (toDigits x b) = x.

Zulip threads:


Open in Gitpod

@vasnesterov vasnesterov added the t-data Data (lists, quotients, numbers, etc) label Jun 17, 2025
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jun 17, 2025

PR summary e0f13842c2

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference
Mathlib.Analysis.Real.OfDigits (new file) 1553

Declarations diff

+ abs_ofDigits_sub_ofDigits_le
+ digits
+ hasSum_ofDigitsTerm_digits
+ ofDigits
+ ofDigitsTerm
+ ofDigitsTerm_le
+ ofDigitsTerm_nonneg
+ ofDigits_digits
+ ofDigits_digits_sum_eq
+ ofDigits_eq_sum_add_ofDigits
+ ofDigits_le_one
+ ofDigits_nonneg
+ sum_ofDigitsTerm_digits_ge
+ sum_ofDigitsTerm_digits_le
+ summable_ofDigitsTerm

You can run this locally as follows
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for script/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@mathlib4-dependent-issues-bot mathlib4-dependent-issues-bot added the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Jun 17, 2025
@wwylele
Copy link
Copy Markdown
Collaborator

wwylele commented Jun 22, 2025

I am not a maintainer, but I wonder that if we add positional system of real to mathlib, it is preferred to add the more general version, which includes

  • Representation of actually all real numbers (or NNReal if we don't want to deal with the sign for now) instead of just Set.Ico 0 1. This could look like Z -> Fin b with a condition on bounded support in one direction
  • Make it a bijection / equivalence by removing duplicates like 1 and 0.999...

Copy link
Copy Markdown
Contributor

@alreadydone alreadydone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can take advantage of the API developed in the PRs mentioned in #Is there code for X?>Base n expansion of real numbers? (sorry, results there are not really easy to use)

Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Co-authored-by: Junyan Xu <[email protected]>
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
· rw [inv_pow]

/-- The number `0.d₀d₁d₂...` in the system with base `b`. -/
noncomputable def ofDigits {b : ℕ} (digits : ℕ → Fin b) : ℝ :=
Copy link
Copy Markdown
Member

@eric-wieser eric-wieser Jun 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try and write something longer later, but for reviewers who didn't see my Zulip message, I'd be inclined to either make digits:

  • PNat-indexed, so that the $n$ th digit represents $b^{-n}$
  • Int-indexed, so that the $n$ th digit represents $b^{n}$

Copy link
Copy Markdown
Contributor

@alreadydone alreadydone Jun 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Int-indexed version would be able to represent all nonnegative reals, but if we want to use LaurentSeries, we have to let the $n$-th digit represent $b^{-n}$.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is using LaurentSeries meaningful here? Does the multiplication do anything sensible?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pointing at LaurentSeries as a way to include the condition that the expansions should start with ...000 (for negative reals they start with ...(b-1)(b-1)(b-1)); The multiplication of course doesn't match. In this case we could also consider including the condition directly so as not to worry about the sign, since it's not hard to state.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My take on these ideas:

  1. Regarding Int: I think that digits in [0,1] is a fine place to start. The behaviour of reals is different when expanding after the decimal point (you can go on forever) and before (you can't) and rather than trying to unify these things in a clever way, which gets messed up because of minus signs, with this hack involving (b-1) digits (which I think is daft, to be honest), I think we should just stick to [0,1]. It's simple, and it's missing from mathlib. The moment we allow Int-valued things we open up a can of worms. I think there is an argument for allowing Int, demanding finite support to the left of the decimal point and then mapping to NNReal but this feels like a different project which could easily coexist with this one. The relevant map from Nat to Int would be the constructor, which is nice.

  2. Regarding PNat; I do think that this suggestion has some merit but the proof of the pudding will be in the eating. It would be a nice exercise to translate this PR into PNat language and to see if it makes it better or worse. One thing which might be missing is the analogue of Finset.range n as a function PNat -> Finset (PNat) plus all the API needed to make it painless. This is worth an experiment but my experience with cyclotomic fields was that Mathlib wasn't quite ready for it yet.

@eric-wieser
Copy link
Copy Markdown
Member

  • Make it a bijection / equivalence by removing duplicates like 1 and 0.999...

I think we want both, so that we can state the theorem that these are the same.

@eric-wieser
Copy link
Copy Markdown
Member

This might also be an argument for why we want the Int-indexed version, since that largely lets us follow the standard informal proof.

@vasnesterov
Copy link
Copy Markdown
Collaborator Author

I think we should have both versions: one for NNReals and one for numbers in [0, 1]. I developed this for #26096, and switching to the more general NNReal version would make the proofs more difficult. Perhaps we could devote this PR to the [0, 1] version and develop the NNReal version in a future PRs?

@vasnesterov vasnesterov changed the title feat(Data/Real): representation of reals in positional system feat(Data/Real): representation of reals from [0, 1] in positional system Jun 26, 2025
@mathlib4-dependent-issues-bot mathlib4-dependent-issues-bot removed the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Jul 2, 2025
@mathlib4-dependent-issues-bot
Copy link
Copy Markdown
Collaborator

This PR/issue depends on:

@grunweg
Copy link
Copy Markdown
Contributor

grunweg commented Aug 23, 2025

Coming here from triage: what's the status of this PR? Is this waiting on the author to refactor the PR, waiting on review, or something else?

Copy link
Copy Markdown
Contributor

@Ruben-VandeVelde Ruben-VandeVelde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - a few comments to match mathlib style more closely below.

Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
@Ruben-VandeVelde Ruben-VandeVelde added the awaiting-author A reviewer has asked the author a question or requested changes. label Aug 23, 2025
@vasnesterov vasnesterov removed the awaiting-author A reviewer has asked the author a question or requested changes. label Aug 23, 2025
@github-actions github-actions bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Sep 1, 2025
@mathlib4-merge-conflict-bot mathlib4-merge-conflict-bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Sep 2, 2025
@mathlib4-merge-conflict-bot
Copy link
Copy Markdown
Collaborator

This pull request has conflicts, please merge master and resolve them.

@kbuzzard
Copy link
Copy Markdown
Member

kbuzzard commented Sep 4, 2025

This job finally got to the top of the pile -- sorry it took so long. Right now there is a trivial conflict and also a complaint that this file is no longer allowed to live in Data. Maybe just move it to Mathlib/Analysis/Real/OfDigits.lean?

Copy link
Copy Markdown
Member

@kbuzzard kbuzzard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I only made it half way through this and now I need to pause for a bit, so I thought I'd submit my half-review just to prove that I am actually looking at the PR now! Apologies for the delay.

Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
Comment thread Mathlib/Data/Real/OfDigits.lean Outdated
@github-actions github-actions bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Sep 5, 2025
Copy link
Copy Markdown
Member

@kbuzzard kbuzzard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wouldn't surprise me if some expert could golf these proofs a lot, but I don't really have the time or the energy to take this any further; I think the proofs are fine as they are (I suggested some more golfs and name changes).

In my mind this is ready for merge modulo the question of whether PNat would be a better indexing set. My gut feeling is that it would make the proofs even worse but do you think that actually this would be a an effective change?

Comment thread Mathlib/Analysis/Real/OfDigits.lean
Comment thread Mathlib/Analysis/Real/OfDigits.lean Outdated
Comment thread Mathlib/Analysis/Real/OfDigits.lean Outdated
Comment thread Mathlib/Analysis/Real/OfDigits.lean Outdated
Comment thread Mathlib/Analysis/Real/OfDigits.lean Outdated
Comment thread Mathlib/Analysis/Real/OfDigits.lean Outdated
Comment thread Mathlib/Analysis/Real/OfDigits.lean Outdated
Comment thread Mathlib/Analysis/Real/OfDigits.lean Outdated
Comment thread Mathlib/Analysis/Real/OfDigits.lean
Comment thread Mathlib/Analysis/Real/OfDigits.lean Outdated
@kbuzzard kbuzzard added the awaiting-author A reviewer has asked the author a question or requested changes. label Sep 7, 2025
@vasnesterov
Copy link
Copy Markdown
Collaborator Author

@kbuzzard Please, respond to my comment above.

@vasnesterov vasnesterov removed the awaiting-author A reviewer has asked the author a question or requested changes. label Sep 7, 2025
Copy link
Copy Markdown
Contributor

@grunweg grunweg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not Kevin, but here's my take on the style questions you raised.

Comment thread Mathlib/Analysis/Real/OfDigits.lean
Comment thread Mathlib/Analysis/Real/OfDigits.lean
@bryangingechen
Copy link
Copy Markdown
Contributor

@vasnesterov Could you say something regarding the suggestion to use PNat indexing? This seems to be the last significant open question (Sorry if I missed it somewhere above).

@bryangingechen bryangingechen added the awaiting-author A reviewer has asked the author a question or requested changes. label Sep 11, 2025
@vasnesterov
Copy link
Copy Markdown
Collaborator Author

I agree with @kbuzzard above that it makes sense, but would be painful due to the lack of PNat API. So I'd keep it with Nat for now.

@vasnesterov vasnesterov removed the awaiting-author A reviewer has asked the author a question or requested changes. label Sep 11, 2025
@bryangingechen
Copy link
Copy Markdown
Contributor

OK, based on #PR reviews > representation of reals in [0, 1] in positional system @ 💬 from Kevin, let's merge.

Thanks!
bors r+

@ghost ghost added the ready-to-merge This PR has been sent to bors. label Sep 11, 2025
mathlib-bors bot pushed a commit that referenced this pull request Sep 11, 2025
…system (#26021)

* Introduce `ofDigits {b : ℕ} (digits : ℕ → Fin b) : ℝ`, representing a real number of the form `0.d₀d₁d₂...` (where `dᵢ = digits i`) as an infinite sum.
* Prove that this sum converges to a number in the interval `[0, 1]`.
* Prove `ofDigits_close_of_common_prefix`: if the first `n` digits of two numbers are equal, then their difference is bounded by `b⁻ⁿ`.
* Introduce `Real.digits`, which converts a real number into its sequence of digits.
* Prove that `ofDigits (toDigits x b) = x`.

Zulip threads: 
- [Zulip thread where this was proposed](https://leanprover.zulipchat.com/#narrow/stream/217875-Is-there-code-for-X.3F/topic/Base.20n.20expansion.20of.20real.20numbers.3F/near/502937293) 
- [#PR reviews > representation of reals in &#91;0, 1&#93; in positional system](https://leanprover.zulipchat.com/#narrow/channel/144837-PR-reviews/topic/representation.20of.20reals.20in.20.5B0.2C.201.5D.20in.20positional.20system/with/537704930)
@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented Sep 11, 2025

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors bot changed the title feat(Data/Real): representation of reals from [0, 1] in positional system [Merged by Bors] - feat(Data/Real): representation of reals from [0, 1] in positional system Sep 11, 2025
@mathlib-bors mathlib-bors bot closed this Sep 11, 2025
namespace Real

/-- `ofDigits` takes a sequence of digits `(d₀, d₁, ...)` in base `b` and returns the
real numnber `0.d₀d₁d₂... = ∑ᵢ(dᵢ/bⁱ)`. This auxiliary definition `ofDigitsTerm` sends the
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a typo, I think? There is a missing +1 here. I'd suggest writing this as $\LaTeX$ too, for clarity.

joelriou pushed a commit to joelriou/mathlib4 that referenced this pull request Oct 2, 2025
…system (leanprover-community#26021)

* Introduce `ofDigits {b : ℕ} (digits : ℕ → Fin b) : ℝ`, representing a real number of the form `0.d₀d₁d₂...` (where `dᵢ = digits i`) as an infinite sum.
* Prove that this sum converges to a number in the interval `[0, 1]`.
* Prove `ofDigits_close_of_common_prefix`: if the first `n` digits of two numbers are equal, then their difference is bounded by `b⁻ⁿ`.
* Introduce `Real.digits`, which converts a real number into its sequence of digits.
* Prove that `ofDigits (toDigits x b) = x`.

Zulip threads: 
- [Zulip thread where this was proposed](https://leanprover.zulipchat.com/#narrow/stream/217875-Is-there-code-for-X.3F/topic/Base.20n.20expansion.20of.20real.20numbers.3F/near/502937293) 
- [#PR reviews > representation of reals in &leanprover-community#91;0, 1&leanprover-community#93; in positional system](https://leanprover.zulipchat.com/#narrow/channel/144837-PR-reviews/topic/representation.20of.20reals.20in.20.5B0.2C.201.5D.20in.20positional.20system/with/537704930)
zhuyizheng pushed a commit to zhuyizheng/mathlib4 that referenced this pull request Oct 2, 2025
…system (leanprover-community#26021)

* Introduce `ofDigits {b : ℕ} (digits : ℕ → Fin b) : ℝ`, representing a real number of the form `0.d₀d₁d₂...` (where `dᵢ = digits i`) as an infinite sum.
* Prove that this sum converges to a number in the interval `[0, 1]`.
* Prove `ofDigits_close_of_common_prefix`: if the first `n` digits of two numbers are equal, then their difference is bounded by `b⁻ⁿ`.
* Introduce `Real.digits`, which converts a real number into its sequence of digits.
* Prove that `ofDigits (toDigits x b) = x`.

Zulip threads: 
- [Zulip thread where this was proposed](https://leanprover.zulipchat.com/#narrow/stream/217875-Is-there-code-for-X.3F/topic/Base.20n.20expansion.20of.20real.20numbers.3F/near/502937293) 
- [#PR reviews > representation of reals in &leanprover-community#91;0, 1&leanprover-community#93; in positional system](https://leanprover.zulipchat.com/#narrow/channel/144837-PR-reviews/topic/representation.20of.20reals.20in.20.5B0.2C.201.5D.20in.20positional.20system/with/537704930)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge This PR has been sent to bors. t-data Data (lists, quotients, numbers, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants