22 CHAPTER 3. FURTHER CAML
For example, x > 0 & x < 1 is parsed as & (> x 0) (< x 1). Note that all the
comparisons, not just the equality relation, are polymorphic. They not only order
integers in the expected way, and strings alphabetically, but all other primitive
types and composite types in a fairly natural way. Once again, however, they are
not in general allowed to be used on functions.
The two boolean operations & and or have their own special evaluation strategy,
like the conditional expression. In fact, they can be regarded as synonyms for
conditional expressions:
p & q
= if p then q else false
p or q
= if p then true else q
Thus, the ‘and’ operation evaluates its first argument, and only if it is true,
evaluates its second. Conversely, the ‘or’ operation evaluates its first argument,
and only if it is false evaluates its second.
3.2 Syntax of CAML phrases
Expressions in CAML can be built up from constants and variables; any identifier
that is not currently bound is treated as a variable. Declarations bind names to
values of expressions, and declarations can occur locally inside expressions. Thus,
the syntax classes of expressions and declarations are mutually recursive. We can
represent this by the following BNF grammar.
2
expression ::= variable
| constant
| expression expression
| expression infix expression
| not expression
| if expression then expression else expression
| fun pattern -> expression
| (expression)
| declaration in expression
declaration ::= let let bindings
| let rec let bindings
let bindings ::= let binding
| let binding and let bindings
let binding ::= pattern = expression
pattern ::= variables
variables ::= variable
| variable variables
The syntax class pattern will be expanded and explained more thoroughly later
on. For the moment, all the cases we are concerned with are either just variable
or variable variable ·· · variable. In the first case we simply bind an expression to
2
We neglect many constructs that we won’t be concerned with. A few will be introduced later.
See the CAML manual for full details.