Skip to content

Commit 554783a

Browse files
Octachrongasche
authored andcommitted
Manual: add extended description of warning 57
1 parent 4096508 commit 554783a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

manual/manual/cmds/comp.etex

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ that are currently defined are ignored. The warnings are as follows.
497497
\begin{options}
498498
\input{warnings-help.tex}
499499
\end{options}
500+
Some warnings are described in more detail in section~\ref{s:comp-warnings}.
500501

501502
The default setting is "-w +a-4-6-7-9-27-29-32..39-41..42-44-45".
502503
It is displayed by "ocamlc -help".
@@ -748,3 +749,49 @@ command line, and possibly the "-custom" option.
748749

749750
\end{options}
750751

752+
\section{Warning reference} \label{s:comp-warnings}
753+
754+
This section describes and explains in detail some warnings:
755+
756+
\begin{options}
757+
\item[Warning 57: ambiguous variables in or-patterns]
758+
The semantics of or-patterns in OCaml is specified with
759+
a left-to-right bias: a value \var{v} matches the pattern \var{p} "|" \var{q}
760+
if it matches \var{p} or \var{q}, but if it matches both,
761+
the environment captured by the match is the environment captured by
762+
\var{p}, never the one captured by \var{q}.
763+
764+
While this property is generally intuitive, there is at least one specific
765+
case where a different semantics might be expected.
766+
Consider a pattern followed by a when-guard:
767+
"|"~\var{p}~"when"~\var{g}~"->"~\var{e}, for example:
768+
\begin{verbatim}
769+
| ((Const x, _) | (_, Const x)) when is_neutral x -> branch
770+
\end{verbatim}
771+
The semantics is clear:
772+
match the scrutinee against the pattern, if it matches, test the guard,
773+
and if the guard passes, take the branch.
774+
In particular, consider the input "(Const"~\var{a}", Const"~\var{b}")", where
775+
\var{a} fails the test "is_neutral"~\var{a}, while \var{b} passes the test
776+
"is_neutral"~\var{b}. With the left-to-right semantics, the clause above is
777+
{\em not} taken by its input: matching "(Const"~\var{a}", Const"~\var{b}")"
778+
against the or-pattern succeeds in the left branch, it returns the
779+
environment \var{x}~"->"~\var{a}, and then the guard
780+
"is_neutral"~\var{a} is tested and fails, the branch is not taken.
781+
782+
However, another semantics may be considered more natural here:
783+
any pair that has one side passing the test will take the branch. With this
784+
semantics the previous code fragment would be equivalent to
785+
\begin{verbatim}
786+
| (Const x, _) when is_neutral x -> branch
787+
| (_, Const x) when is_neutral x -> branch
788+
\end{verbatim}
789+
This is {\em not} the semantics adopted by OCaml.
790+
791+
Warning 57 is dedicated to these confusing cases where the
792+
specified left-to-right semantics is not equivalent to a non-deterministic
793+
semantics (any branch can be taken) relatively to a specific guard.
794+
More precisely, it warns when guard uses ``ambiguous'' variables, that are bound
795+
to different parts of the scrutinees by different sides of a or-pattern.
796+
797+
\end{options}

0 commit comments

Comments
 (0)