Skip to content

Grammar inconsistencies

def-lkb edited this page Oct 13, 2014 · 9 revisions

Document known differences between OCaml and Camlp4. Merlin will follow OCaml syntax.

0-uple

# type t = ()

Accepted by ocaml.
Rejected by camlp4.

"()" in an expression context is quite ambiguous after such definitions.

functions in labelled arguments

# let f x = f ~f:fun x -> x;;

Rejected by ocaml.
Accepted by camlp4.

variant extension

# type t = [variants];;

Rejected by ocaml.
Accepted by camlp4.

type annotations on bindings

# let _ : type = value;;

Rejected by ocaml.
Accepted by camlp4.

# let (_ : type) = value;;
# let _id : type = value;;

Accepted by both ocaml and camlp4.

infix operators

# let ( :=> ) = value;;

Rejected by ocaml.
Accepted by camlp4.

precedence of 'if' in rhs of (^)

# "" ^ if true then (); "";;

Rejected by ocaml.
Accepted by camlp4.

not a seq_expr inside Module.(…) notation

# List.(1;2)

Accepted by ocaml.
Rejected by camlp4.

Spotted by obad.

";" and infix operators

# let (>>>) = (+);; 4; >>> 5;;

Accepted by camlp4 (evaluates to 9).
Rejected by ocaml.

But if we use (+) directly…

# 4; + 5;;
Warning 10: this expression should have type unit.
- : int = 5
# ignore 4; + 5;;
- : int = 5
# #camlp4o;;
# 4; + 5;;
… … (* just wait *)
^C
# ignore 4; + 5;;
… … (* just wait *)
^C

And with utop! A challenger appears

utop # 4; + 5;;
- : int = 9
utop # ignore 4; + 5;;
       ^^^^^^^^
Error: This expression has type unit but an expression was expected of type int
utop # 4; + 5; + 6;;
… … (* just wait *)
^C

WTF