Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#9224 closed feature request (fixed)

Add support for binary integer literals

Reported by: hvr Owned by:
Priority: normal Milestone: 7.10.1
Component: Compiler (Parser) Version:
Keywords: literals Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description (last modified by hvr)

Haskell2010 supports

  • base-10 (prefix-less),
  • base-8 (via 0[oO]-prefix), and
  • base-16 (via 0[xX]-prefix) integer literals.

I hereby propose to add conditional support for base-2 integers literals via a 0[bB]-prefix, disabled by default, and controllable via a new {-# LANGUAGE BinaryLiterals #-} language extension flag/pragma.

The use of a 0b prefix for indicating binary literals is known from popular programming languages such as Python, Ruby, and Java.

Change History (6)

comment:1 Changed 5 years ago by hvr

Initial patch (sans the language-extension) pushed to https://phabricator.haskell.org/D22 for review

Last edited 5 years ago by hvr (previous) (diff)

comment:2 Changed 5 years ago by hvr

Description: modified (diff)

comment:3 Changed 5 years ago by Herbert Valerio Riedel <hvr@…>

In 1c0b5fdc9f2b6ea8166cc565383d4cd20432343c/ghc:

Add -XBinaryLiterals language extension (re #9224)

Haskell2010 supports

- base-10 (prefix-less),
- base-8 (via `0[oO]`-prefix), and
- base-16 (via `0[xX]`-prefix) integer literals.

This commit adds syntax support for base-2 integer literals via the new `0[bB]`
prefix. The use of a `0b` prefix for indicating binary literals is known
from popular programming languages such as C++14, Perl, Python, Ruby, and Java.

This syntax extension is disabled by default and can be enabled via the
new `{-# LANGUAGE BinaryLiterals #-}` pragma and/or the new `-XBinaryLiterals`

This new extensions requires to upgrade the `ExtsBitmap` type from
`Word` to `Word64` as this adds a 33th flag which is not guaranteed to
fit into a `Word`.

Signed-off-by: Herbert Valerio Riedel <hvr@gnu.org>

Differential Revision: https://phabricator.haskell.org/D22

comment:4 Changed 5 years ago by Herbert Valerio Riedel <hvr@…>

In dab0fa06fa33eeb45cef16c06c41d6c45b102451/ghc:

Update Cabal to BinaryLiterals-aware 1.20 version

In 1c0b5fdc9f2b6ea8166cc565383d4cd20432343c (re #9224) `BinaryLiterals`
was temporarily added to T4437's `expectedGhcOnlyExtensions` list. This
can now reverted as Cabal has been made aware of `BinaryLiterals`
(see haskell/cabal#1970 and haskell/cabal#1972).

updates Cabal submodule

Signed-off-by: Herbert Valerio Riedel <hvr@gnu.org>

comment:5 Changed 5 years ago by hvr

Resolution: fixed
Status: newclosed

comment:6 Changed 4 years ago by Lemming

That's pretty useful for bit manipulation!

Note: See TracTickets for help on using tickets.