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''.
6.5
Constructions with matching
6.6
Mutables and assignment
-
The instruction ``
<-'' is written ``:='':
| OCaml | Revised |
| x.f <- y | x.f := y |
- The ``
ref'' type is declared as a record type with one
field named ``val'', instead of ``contents''. The
operator ``!'' does not exist any more, and references are
assigned like the other mutables:
| OCaml | Revised |
| x := !x + y | x.val := x.val + y |
-
The type constructors are before their type parameters, which
are curryfied:
| OCaml | Revised |
| int list | list int |
| ('a, bool) Hashtbl.t | Hashtbl.t 'a bool |
| type 'a foo = | type foo 'a = |
| 'a list list;; | list (list 'a); |
- The abstract types are represented by a unbound type variable:
| OCaml | Revised |
| type 'a foo;; | type foo 'a = 'b; |
| type bar;; | type bar = 'a; |
- Parentheses are mandatory in tuples of types:
| OCaml | Revised |
| int * bool | (int * bool) |
- In declaration of a concrete type, brackets must enclose
the constructor declarations:
| OCaml | Revised |
| type t = A of i | B;; | type t = [ A of i | B ]; |
- It is possible to make the empty type, without constructor:
type foo = [];
- There is a syntax difference between data constructors with
several parameters and data constructors with one parameter of type
tuple.
The declaration of a data constructor with several parameters is
done by separating the types with ``and''. In expressions and
patterns, this constructor parameters must be curryfied:
| OCaml | Revised |
| type t = C of t1 * t2;; | type t = [ C of t1 and t2 ]; |
| C (x, y);; | C x y; |
The declaration of a data constructor with one parameter of type
tuple is done by using a tuple type. In expressions and patterns,
the parameter has not to be curryfied, since it is alone:
| OCaml | Revised |
| type t = D of (t1 * t2);; | type t = [ D of (t1 * t2) ]; |
| D (x, y);; | D (x, y); |
- The predefined constructors ``
True'' and ``False''
start with an uppercase letter.
- In record types, the keyword ``
mutable'' must appear
after the colon:
| OCaml | Revised |
| type t = {mutable x : t1};; | type t = {x : mutable t1}; |
Modules application uses curryfication:
| OCaml | Revised |
| type t = Set.Make(M).t;; | type t = (Set.Make M).t; |
The objects also have a revised syntax. To see it, the simplest way is
to write examples in normal syntax and to convert them into revised
syntax using the command:
camlp4o pr_r.cmo file.ml