Unexpected partial-match warning on a malformed program

OCaml gives me a pattern-match warning on expressions I think are malformed, e.g., let x + 1 = 2:

─( 13:26:24 )─< command 0 >────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # let x + 1 = 2;;

Line 1, characters 6-13:
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
0

val x : int -> int = <fun>
─( 13:26:25 )─< command 1 >────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # x 1;;
- : int = 2
─( 13:26:28 )─< command 2 >────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # x 2;;
Exception: Match_failure ("//toplevel//", 1, 6).
─( 13:26:30 )─< command 3 >────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # let x - 1 = 0;;

Line 1, characters 6-13:
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
0

val x : int -> int = <fun>
─( 13:26:32 )─< command 4 >────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # x (-1);;
- : int = 0
─( 13:26:36 )─< command 5 >────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # x 0;;
Exception: Match_failure ("//toplevel//", 1, 6).

Expressions like let x * 2 = 0 are rejected due to syntax error.
Is this a bug?

2 Likes

I don’t think so: the term + 1 is a valid pattern in OCaml (pattern matching on ints), so let x + 1 = 2 is parsed as a functional binding let x p = e where p is the (non-exhaustive) pattern + 1 and e is 2.

Cheers,
Nicolas

2 Likes

To add to the explanation, I’d add that this is parsed as let x (+1) = 2 which is like let x 1 = 2.

3 Likes

Thanks for clarifying!