Why am I able to add any number of ; at the end on the line in OCaml top-level?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Why am I able to add any number of ; at the end on the line in OCaml top-level?



I'm new to OCaml and I wonder how this statement works:


# 1 + 1 ;;;;
- : int = 2
# 1 + 1 ;;;;;;;;
- : int = 2



Is the ;;;; considered as combining 1+1 with blankas in (((1 + 1);););;? Or is ;;;; treated as a multi-character in some other way?


;;;;


1+1


blank


(((1 + 1);););;


;;;;




1 Answer
1



As discussed here, ;; in the top-level REPL ignores everything after it. The double semicolon isn't required when writing code to files and compiling them. To quote Daniil Baturin's What I wish I knew when learning OCaml,


;;



The answer to the question “When do I need the ;; within OCaml source code?” is never.


;;



It's not a part of the language and is only used by the interpreter as an end of input mark.



The revised syntax of Ocaml gets rid of ;; with the following argument:


;;



The double semicolon in OCaml exists for historical reasons: the first parsers were driven by the tokens, not by the rules: all constructions needed to have a specific token.



But because of the introduction of modules in OCaml, the double semicolon, which was mandatory in Caml Light to end sentences, became optional: the reason is that in OCaml, a 'phrase' and a 'structure item' are actually the same notion. The problem is that the double semicolon is associated with the idea of 'terminating' something: for a phrase, it is exact, but not for a structure item inside a structure, since other structure items and the keyword 'end' follow.



That choice of letting the double semicolon be optional in normal syntax has introduced several problems:



A structure item is actually ended by the beginning of the next structure item; it means that all structure items must start with a keyword; otherwise there is an ambiguity. For example, you cannot write:


print_string "hello, world"
print_newline ()



because it is interpreted as a call to print_string with 3 parameters (and typing error). The advocated solution is to write:


let _ = print_string "hello, world"
let _ = print_newline ()



Mmm....



But this solution does not work interactively: in the toplevel, you cannot ask people to type the beginning of the next sentence to see the result of the current one. Therefore the double semicolon still remains! The property that we write in the toplevel like in source files has been lost.



In structures and objects, the fact that you don't end the structure items and object items make the programs more difficult to read. If you write a short object or structure item in one only line, it is very difficult to see where the items start and end.



My opinion is that the structure items should end with a token in a context where there is never need to read another token. This ensures a correct behavior in the interactive toplevel. The fact that the sequence is closed, in the revised syntax, frees the simple semicolon. And a simple semicolon is perfectly acceptable inside structures and objects, to end their item, the same way they close a record item. In the revised syntax, this ending semicolon is mandatory.



It is easier to treat a language whose all phrases end with a token: at end of the sentences, the characters and the tokens streams are synchronized (no need to read an extra token to be sure that the phrase is ended). This property can bring simplifications in other treatments (extraction of comments or code for documentation, indentation, editors modes, interactive tools).



[...]





I guess this is a different question, but (((1 + 1);););; also gives - : int = 2. Why is this so? I was expecting it to return unit.
– Santi Santichaivekin
Aug 13 at 8:27



(((1 + 1);););;


- : int = 2


unit





It's a trailing ; after an expression and is ignored. If you want to return unit then you must add a unit value, like (1+1);();;
– Goswin von Brederlow
4 hours ago


(1+1);();;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard