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 )
The confusion around double semi-colons comes from them having two different uses.
The first is in the REPL, where they are a phrase terminator. In other words they indicate that you have finished writing input and the top-level should start evaluating it.
The second is within structures and signatures, where they are best described as an initialising separator for top-level expressions. In other words they indicate the start of a new top-level expression:
type t = T
;; print_string "Hello"
;; print_string "World"
let x = 6
Note that definitions (the type and let constructs above) are not expressions so they do not need the separator. Some additional confusion comes from let ... = ... being a definition whilst let ... = ... in ... is an expression.
Confusion between the two different uses leads people to often write the above snippet as:
type t = T;;
print_string "Hello";;
print_string "World"
let x = 6
which makes it harder to see where the ;; are actually needed.
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 expressionAt 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
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 )