Syntax error OCaml

I have a function
let return x = x;
and i need to find a Syntax error in this , but in compiler it doesnt give me any errors at all.

I have few ideas but i dont know if they re correct
First : if it s an unbound value as we use x before declaring it
Second: this one ; shouldnt be here?

Where did you enter this code? In a file? In the REPL? Is this a toplevel function definition? Or is it nested inside some other expression? In any case, most likely it’s the second, there should not be a semi-colon after the binding.

No its a toplevel function definition, it was given exactly like this
I tried it on Try OCaml and even with semi-colon it works fine and doesnt give any errors.


OK so you entered in the REPL:

let return x = x; ;;

Which is actually slightly different from your original posted code,

let return x = x;

The reason you didn’t get a syntax error is I guess because the parser ignored the single semi-colon when it saw that the entry was finished with ;;.

Btw you can enter code samples into messages in this forum by using triple-backticks to begin and end the samples. It’s better to do that than to paste images of the code, because images are not very accessible.

No i typed exactly as i wrote
let return x = x;
but it adds the two semi-colons automatically

This is on the OCaml REPL on the command line? It doesn’t do that for me, and actually I’ve never heard of that happening. If I end the entry with just the single semi-colon it just sits there waiting for me to finish entering the ;;.

I use this website to try my code
But apart from the semi colon this function is absolutely correct , there s nothing wrong with it?

Oh, so Try OCaml works in a slightly different way than the command-line REPL. It has a single-line text box and assumes that the entry is finished as soon as you hit Enter. And so it takes the line, adds the ;; to the end, and sends it off to its version of the REPL, which executes it.

Anyway to answer your question, there is nothing wrong with the function, it’s a fine function :slight_smile:

thank you very much for your help

1 Like

Indeed, there is nothing wrong apart from the semicolon. Think about why did you wrote it. Your intent was likely to state that your statement was finished (it has this meaning in a lot of languages). BUT in ocaml, single semicolon does not have this meaning at all. I do not know how to explain it clearly, so I provide you example to express this:

let x = () ; (* alone: invalid code*)

let x = () ; 7 (* valid code; literally the same that let x = 7 *)

let x = 4 ; 7 (* invalid code, because expression ending with ";"
                 must be of unit type *)

let x = () ; let y = 7 in y + 3 (* valid code, equivalent to let x = 10 *)

let x = 2 ;
let y = 7  (* invalid code because 2 is not of unit type AND
              let y = 7 should be followed by in <expression>.
              Without the ";", it would have been valid code *)

Be careful because improper use of single semicolon in ocaml leads to error messages very unlikely to help you. (syntax error at a place you would not expect as a beginner).

Welcome and have a good day :slight_smile:

I think that the point was that there should have been a syntax error, but OCaml is not complaining (neither the REPL nor the compiler).

Apparently OCaml is accepting weird (for me at least) syntax such as

let x = 1; in x

Maybe I am mistaken, but it does not seem to correspond to the syntax of OCaml - The OCaml language

It is so called trailing semicolon as in fact, OCaml accepts a sequence of expressions that may end with an optional semicolon,

expr ::= 
 <expr1>; <expr2>; ... <exprN> [;]

This is to accommodate an imperative style of programming, e.g.,

let () = 
   print_endline "Hello";
   print_endline "World";

It would be quite surprising (to a typical programmer) if the last semicolon would raise a syntactic error.

So OCaml allows for a semicolon to follow an expression, e.g.,

let x = 1; in x
let x = 1;2 in x
let x = 1;2; in x

are all valid OCaml expressions.

With that said, I think it would be really helpful if OCaml will at least issue warning 10, for trailing semicolons.

1 Like

Maybe the manual should be updated.

I strongly support the fact that OCaml should emit warning 10 in the case when an expression which is not of type unit is followed by a semicolon, whether this semicolon is followed by an expression or not. However, the last semicolon is probably lost during the parsing phase…