From Gallium

Jump to: navigation, search

The revised syntax is an alternative syntax for OCaml. Its purpose is to be simpler, more regular, more logical than the normal syntax, and fix some problems which sometimes result in bugs not always easy to find.

This chapter presents the changes from regular OCaml syntax.


The revised syntax and 3.10

There is new direction chosen for the revised syntax in 3.10. If you want learn about the revised syntax from scratch please starts at the next section.

We want to being closer to the original syntax when it's possible. So when possible we introduce some constructions closer to the original syntax called "preferred style", but kept the backward compatibility as most as possible.

List of changes

A tour of the revised syntax for OCaml

If you want learn about the revised syntax from scratch you'd better put attention mainly on changes marked "preferred style".

Top-level phrases

  • A simple semicolon (;) ends the sentences, in structures and in signatures. The double semicolon (;;) is not a token. There is no ambiguity with the sequence, which has a special construction.
  • The declaration of a global variable is introduced by the keyword value, let being reserved to the construction
Regular syntaxRevised syntax
let x = 23;;value x = 23;
let x = 23 in x + 7;;let x = 23 in x + 7;
  • In interfaces, one must use value, too, instead of val.
Regular syntaxRevised syntax
val x : int;;value x : int;

Imperative constructions

  • In the preferred style there is just some parentheses more.
  • However the revised syntax also have sequences introduced by the keyword do followed by { and terminated by } (it is possible to put a semicolon after the last expression):
Regular syntaxRevised syntax (preferred style)Revised syntax
e1; e2; e3; e4(e1; e2; e3; e4)do { e1; e2; e3; e4 }
  • The body of for and while has the same syntax:
Regular syntax or Revised preferred styleRevised syntax
while e1 dowhile e1 do {
   e2; e3; e4   e2; e3; e4

Tuples and lists

  • Parentheses are mandatory in tuples:
Regular syntaxRevised syntax
1, "hello", World(1, "hello", World)
  • Lists are always enclosed with [ and ]. Their syntax is:
list::=[ elem-list opt-cons ]
elem-list::=expression ; elem-list | expression
opt-cons::=:: expression | (*empty*)

A list is a sequence of expressions separated by semicolons, optionally ended by a :: and an expression, the whole being enclosed by brackets. Examples:

Regular syntaxRevised syntax
x :: y[x :: y]
[x; y; z][x; y; z]
x :: y :: z :: t[x :: [y :: [z :: t]]]
x :: y :: z :: t[x; y; z :: t]

Note the two ways to write the last case.

Irrefutable patterns

There is a notion of "irrefutable patterns" used by some syntactic constructions. Matching against these patterns never fails. An "irrefutable pattern" is either:

  • A variable.
  • The wildcard _.
  • The constructor ().
  • A tuple with irrefutable patterns.
  • A record with irrefutable patterns.
  • An irrefutable pattern with a type constraint.

Note that the term "irrefutable" does not apply to all patterns which never fail: constructors alone in their type declarations, except (), are not said "irrefutable".

Personal tools
Espace privé