leanprover / leanprover/lean4

RFC: Enhanced APIs for Float parsing and formatting

Open
#14,659 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

RFC
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 a String directly to a Float.
  • 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.toStringFull exists, but it is still a hassle to manipulate its output further to get the desired output.

Basic proposal:

  • Implement String.toFloat? and String.toFloat! which are like String.toNat? and String.toNat! but for Float
  • Implement, say, Float.to{Fixed|Scientific} (prec := some default value). There may be more detailed formatting needs such as e vs. 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 the toFloat? function.

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.
Community Feedback

I posted the idea of using ryu for this functionality on this Zulip thread but didn't get any response.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.