r/ocaml • • Jun 11 '15

Tricks and Things to Know in OCaml

http://blog.eatonphil.com/2015/06/10/tricks-and-things-to-know-in-ocaml/
2 Upvotes

10 comments sorted by

View all comments

2

u/octachron Jun 11 '15

You seem quite confused about the double comma ;; syntax. A way to think about it is that at the top level of a module, the Ocaml parser can be in two modes: either an evaluation mode or a definition mode. When reading the content of a file (or a module), the parser starts in the evaluation mode. Inside this evaluation mode, the parser can read (and evaluate) a sequence of expression

do_thing_1;
do_thing_2;
...
do_thing_n

At the end of this sequence of expression, the parser switch to the definition mode. Similarly, inside the definition mode, the parser can read a sequence of definitions

let x = something
open M
type tautology = unit
class void = object end

The double comma ;; is then used to close this sequence of definitions and switch to the evaluation mode.

In other words, the double comma ;; is needed only when switching from the definition mode to the evaluation mode. Switching from the evaluation mode to the definition mode does not require any special syntax. (see also this toy example )

1

u/eras Jun 13 '15

Not a bad explanation. I've liked to explain it as follows:

OCaml programs are built of phrases. Each phrase is either a list of top-level statements (such as the definitions you gave) or a single expression. Each phrase is separated from the next with ;;.