Opened 22 months ago

Closed 20 months ago

Last modified 9 months ago

#14473 closed task (fixed)

Implement Underscores in Numeric Literals Proposal (NumericUnderscores extension)

Reported by: takenobu Owned by: takenobu
Priority: normal Milestone: 8.6.1
Component: Compiler Version: 8.2.1
Keywords: GHCProposal Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: #13126 #9224 Differential Rev(s): Phab:D4235
Wiki Page:


Implement Underscores in Numeric Literals Proposal.

GHC supports various numeric literals such as decimal, octal, hexadecimal, binary, and floating point numbers. However, large numeric literals are hard to read. This proposal improves the readability, quality, expressiveness of numeric literals.

This proposal allows underscores to numeric literals when the NumericUnderscores language extension is enabled. Underscores (_) in numeric literals are simply ignored.

The specification of the feature is available here:

For a discussion:

Change History (12)

comment:1 Changed 22 months ago by takenobu

I am planning to submit a patch. This is the patch of the current version:

About it, Iavor gave me good advice:

That is, I unify the definition of Lexer and add a validation function. After investigating how to implement the function for validation, I will submit the patch.

comment:2 Changed 22 months ago by takenobu

Owner: set to takenobu

comment:3 Changed 22 months ago by takenobu

Differential Rev(s): Phab:D4235
Status: newpatch

comment:4 Changed 22 months ago by takenobu

I submitted a patch Phab:D4235.

comment:5 Changed 21 months ago by takenobu

For the later people, I record about the correspondence status of the syntax highlight on editors and code browsers for BinaryLiterals, HexFloatLiterals and NumericUnderscores extensions.

  • Emacs (haskell-mode)
    • Lowercase literals are already almost correctly displayed.
    • haskell-lexeme.el and haskell-font-lock.el
  • Linguist (which is used from github)
    • Linguis uses language-haskell submodule of atom.
    • Linguis will bump it at new year release.
Last edited 21 months ago by takenobu (previous) (diff)

comment:6 Changed 21 months ago by _recursion

I've submitted an issue for this for SublimeHaskell (Sublime Text Haskell plugin) which can be found here:

comment:7 Changed 21 months ago by takenobu

_recursion,thank you so much!

comment:8 Changed 20 months ago by Ben Gamari <ben@…>

In 4a13c5b1/ghc:

Implement underscores in numeric literals (NumericUnderscores extension)

Implement the proposal of underscores in numeric literals.
Underscores in numeric literals are simply ignored.

The specification of the feature is available here:

For a discussion of the various choices:

Implementation detail:

* Added dynamic flag
  * `NumericUnderscores` extension flag is added for this feature.

* Alex "Regular expression macros" in Lexer.x
  * Add `@numspc` (numeric spacer) macro to represent multiple
  * Modify `@decimal`, `@decimal`, `@binary`, `@octal`, `@hexadecimal`,
    `@exponent`, and `@bin_exponent` macros to include `@numspc`.

* Alex "Rules" in Lexer.x
  * To be simpler, we have only the definitions with underscores.
    And then we have a separate function (`tok_integral` and `tok_frac`)
    that validates the literals.

* Validation functions in Lexer.x
  * `tok_integral` and `tok_frac` functions validate
    whether contain underscores or not.
    If `NumericUnderscores` extensions are not enabled,
    check that there are no underscores.
  * `tok_frac` function is created by merging `strtoken` and
  * `init_strtoken` is deleted. Because it is no longer used.

* Remove underscores from target literal string
  * `parseUnsignedInteger`, `readRational__`, and `readHexRational} use
    the customized `span'` function to remove underscores.

* Added Testcase
  * testcase for NumericUnderscores enabled.
      NumericUnderscores0.hs and NumericUnderscores1.hs
  * testcase for NumericUnderscores disabled.
      NoNumericUnderscores0.hs and NoNumericUnderscores1.hs
  * testcase to invalid pattern for NumericUnderscores enabled.
      NumericUnderscoresFail0.hs and NumericUnderscoresFail1.hs

Test Plan: `validate` including the above testcase

Reviewers: goldfire, bgamari

Reviewed By: bgamari

Subscribers: carter, rwbarton, thomie

GHC Trac Issues: #14473

Differential Revision:

comment:9 Changed 20 months ago by bgamari

Resolution: fixed
Status: patchclosed

Thanks for taking this on, takenobu!

comment:10 Changed 20 months ago by takenobu

It's my pleasure :)

comment:11 Changed 16 months ago by RyanGlScott

Keywords: GHCProposal added

comment:12 Changed 9 months ago by takenobu

Pygments (which is used from trac, readthedocs, pandoc, ...)

This is pending review. ​

The patch for Pygments has been merged. (Development of Pygments was restarted.)

Syntax hightlight for NumericUnderscores, BinaryLiterals and HexFloatLiterals will be available on Pygments, Sphinx, readthedocs and pandoc.

Note: See TracTickets for help on using tickets.