Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get more information out of the Ocaml Compiler

I have been having the hardest time with ocamlc, and it's extremely uninformative error messages. Right now, when I compile one of my files, it gives me

Error: Syntax Error

on the last line of the file, which is empty. So, I'm assuming I'm missing some open parenthesis or something, I'm not sure. Regardless, how do people who use Ocaml in a production setting deal with this? Are there any available tools that give any hint at all as to what character the lexer was expecting, or just better error messages in general?

Specifically, I'm looking for a tool like this, which looks awesome. But it looks like it's no longer under development, however this looks like a tool that compiles Ocaml to Javascript, which isn't the end result that I want.

Any suggestions as to how people usually resolve Ocaml syntax errors would be great.

like image 538
Enrico Borba Avatar asked Jan 28 '26 17:01

Enrico Borba


1 Answers

Given one.ml:

let a = 42 in
let f x = x + a
let g x = x + x

You'll get this error when compiling it:

$ ocamlc one.ml 
File "one.ml", line 3, characters 0-3:
Error: Syntax error

ocamlc gives up on making sense of this at the let on the third line, because at that point the only possible continuation of line 2 is in and another expression (or a continuation of x + x, like + x in ...). The fundamental problem here is that ocamlc didn't throw an error at the end of line 1, because ocamlc tries to allow both proper OCaml and a mess of expressions that you could have pasted into an interactive session. It even allows ;; in files, which is absurd: ;; is only needed interactively because without some extraordinary termination rule (like Python's blank empty line that ends a definition - but only interactively) OCaml can't know that an expression isn't to be continued by an operator and another expression. ocamlc is trying to be forgiving of confusion to the point that confusions are prolonged, instead of corrected.

So what is proper OCaml? For whatever reason it's not documented well. But you can get a feel for it very quickly when you understand that some forms you can put 'leftmost' in a file and some form can only exist as subexpressions of the first form, or if you try to read well-written OCaml while remembering that whitespace is insignificant in OCaml in the same manner as in C: OCaml isn't using the blank lines you between definitions to understand that those definitions are separate, so how is it separating them?

Or, you can start with a few rules. "Don't use let ... in except as a subexpression." "Don't try to perform side effects except as subexpressions of let () =", "Treat semicolons like operators that require a right-hand side, not terminators", etc.

So instead of one.ml, any of these would work:

let a = 42
let f x = x + a
let g x = x + x

or

let f x =
  let a = 42 in
  x + a
let g x = x + x

or (obviously this is bad style):

let f x = let a = 42 in x + a let g x = x + x

two.ml has the other common error:

let () =
  print_endline "hi";

let f x = x + x

You'll get a 'Syntax error' on line 5, characters 0=0. Which is ocamlc reaching the end of the file and still not seeing the in (or a continuation of x + x) that must follow. Because the ; on line 2 means that the let on line 4 must be a subexpression of the let on line 1.

Instead of two.ml, any of these would be fine:

let () =
  print_endline "hi"

let f x = x + x

or (again bad style, but imagine it with the semicolon):

let () = print_endline "hi" let f x = x + x

Although you can also get a 'bare' syntax error with let x = [| ], even someone very new to OCaml will recognize such errors the moment they find the line and character range complained about, so such errors aren't frustrating at the level of one.ml and two.ml

like image 110
Julian Fondren Avatar answered Jan 31 '26 22:01

Julian Fondren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!