RFC: Enhanced APIs for Float parsing and formatting
Nobody has claimed this yet.
- Dominant language
- Lean
- Stars
- 9.2k
- Forks
- 990
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 175
Description
Proposal
One of the basic needs for writing a program is parsing and formatting floating-point numbers. Unfortunately, Lean is currently very lacking in this area IMO:
- Parsing: The closest we have is
OfScientific. We don't have any function that parses aStringdirectly to aFloat. - Formatting: The only thing we have is
ToString, which just calls C++std::format, which in turn gives a string with default precision. We cannot choose between fixed/scientific notation, and we cannot specify a custom precision.Batteries.Float.toStringFullexists, but it is still a hassle to manipulate its output further to get the desired output.
Basic proposal:
- Implement
String.toFloat?andString.toFloat!which are likeString.toNat?andString.toNat!but forFloat - Implement, say,
Float.to{Fixed|Scientific} (prec := some default value). There may be more detailed formatting needs such asevs.E, explicit vs. implicit+in exponents; whether and how to support these may be discussed further
Direct benefits:
- These interfaces make life much easier when writing various application programs
- We could simplify a part of Lean infrastructure, e.g. float parsers/formatters for JSON and TOML
- For parsing, I believe the places where a number token can appear are limited in both formats, so we can get away with something like passing a
[0-9eE.+-]+token to thetoFloat?function.
- For parsing, I believe the places where a number token can appear are limited in both formats, so we can get away with something like passing a
Maintainability concerns:
- If we stick to simple pure-Lean implementation, probably not so much?
- If we care about performance and/or correctness, algorithms such as https://github.com/ulfjack/ryu may be used, but these are significantly more complex to re-implement in Lean
- Vendoring
ryu's C implementation could be an option.
- Vendoring
Community Feedback
I posted the idea of using ryu for this functionality on this Zulip thread but didn't get any response.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the existing String.toNat? and String.toNat! APIs, along with Batteries.Float.toStringFull, to understand the proposed parsing and formatting interfaces. Clarify supported notation, precision, exponent syntax, and implementation strategy before determining whether the proposal is complete.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100