Extended syntax for literals

From Successor ML

Jump to: navigation, search

Contents

Introduction

We propose to extend the syntax for numeric literals in three ways:

  • allow underscores to group digits,
  • add binary literals,
  • allow the order of letters in multi-letter prefixes like 0wx to be arbitrary.

Motivation and Example

SML currently provides no way to group digits in numeric literals, which makes long numbers hard to read. As a remedy, we suggest allowing underscores within literals, as is done in several other languages. For example, the programmer would be able to write

 val pi = 3.141_592_653_596
 val billion = 1_000_000_000
 val nibbles = 0wx_f300_4588

Moreover, SML lacks a notation for binary literals and hence requires fallback to hexadecimal. A C-style notation for binary literals with a "0b" prefix would enable writing:

 val ten = 0b1010
 val bits = 0wb1101_0010_1111_0010

Note that binary literals particularly benefit from the ability to group digits.

Last, we want to relieve the programmer from the burden to remember the order of the different parts in literal prefixes and support writing "0xw" and "0bw" as synonyms for "0wx" and "0wb".

Assumptions

None.

Syntax

Defined by the following modifications to the Definition:

  • In Section 2.2, extend the first sentence as follows:
[...] and the underscore (_) that neither starts nor ends with an underscore.
  • Extend the second sentence:
[...] and the underscore that does not end with an underscore.
  • Add the following sentence to the end of the paragraph:
An integer constant (in binary notation) is an optional negation symbol followed by a non-empty sequence of binary digits 0,1 and the underscore that does not end with an underscore.
  • Extend the first sentence of the second paragraph as follows:
[...] and the underscore not ending with an underscore.
  • In the second sentence, replace "is 0wx" with "is 0wx or 0xw".
  • Extend the second sentence as follows:
[...] and the underscore not ending with an underscore.
  • After the second sentence, add:
A word constant (in binary notation) is 0wb or 0bw followed by a non-empty sequence of binary digits 0,1 and the underscore not ending with an underscore.
  • Modify the next sentence by replacing "and one or more decimal digits" with:
and a sequence of one or more decimal digits and underscores that contains at least one digit
  • Add to the the list of examples in the next sentence:
   3.141_592_653  3._678_098__E20
  • Add to the list of non-examples:
   1_.5  1._E2

Static Semantics

Unchanged.

Dynamic Semantics

Unchanged.

Interactions

The scanning functions from the Basis library should be extended to reflect the change by supporting underscores in their input.

Compatibility

This extension is not conservative, as it may change the meaning of programs that contain literals and wildcards without separating spaces, as in

 fun f 3_4 = 0

or, likewise, programs that put a literal next to an identifier "xw", "b", "wb", or "bw". However, such programs are highly unlikely to exist in practice.

The extensions are already supported by Alice ML.

Implementation

Straightforward extension of the lexical analyzer.

Personal tools