1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
|
\section{s:value-expr}{Expressions}
%HEVEA\cutname{expr.html}
\ikwd{in\@\texttt{in}|see{\texttt{let}}}
\ikwd{and\@\texttt{and}}
\ikwd{rec\@\texttt{rec}|see{\texttt{let}, \texttt{module}}}
\ikwd{let\@\texttt{let}}
\ikwd{try\@\texttt{try}}
\ikwd{function\@\texttt{function}}
\ikwd{fun\@\texttt{fun}}
\ikwd{with\@\texttt{with}}
\ikwd{done\@\texttt{done}|see{\texttt{while}, \texttt{for}}}
\ikwd{do\@\texttt{do}|see{\texttt{while}, \texttt{for}}}
\ikwd{downto\@\texttt{downto}|see{\texttt{for}}}
\ikwd{to\@\texttt{to}|see{\texttt{for}}}
\ikwd{for\@\texttt{for}}
\ikwd{else\@\texttt{else}|see{\texttt{if}}}
\ikwd{then\@\texttt{then}|see{\texttt{if}}}
\ikwd{if\@\texttt{if}}
\ikwd{or\@\texttt{or}}
\ikwd{match\@\texttt{match}}
\ikwd{begin\@\texttt{begin}}
\ikwd{end\@\texttt{end}}
\ikwd{when\@\texttt{when}}
\ikwd{new\@\texttt{new}}
\ikwd{object\@\texttt{object}}
\ikwd{lazy\@\texttt{lazy}}
\begin{syntax}
expr:
value-path
| constant
| '(' expr ')'
| 'begin' expr 'end'
| '(' expr ':' typexpr ')'
| expr {{ ',' expr }}
| constr expr
| "`"tag-name expr
| expr '::' expr
| '[' expr { ';' expr } [';'] ']'
| '[|' expr { ';' expr } [';'] '|]'
| '{' field [':' typexpr] ['=' expr]%
{ ';' field [':' typexpr] ['=' expr] } [';'] '}'
| '{' expr 'with' field [':' typexpr] ['=' expr]%
{ ';' field [':' typexpr] ['=' expr] } [';'] '}'
| expr {{ argument }}
| prefix-symbol expr
| '-' expr
| '-.' expr
| expr infix-op expr
| expr '.' field
| expr '.' field '<-' expr
| expr '.(' expr ')'
| expr '.(' expr ')' '<-' expr
| expr '.[' expr ']'
| expr '.[' expr ']' '<-' expr
| 'if' expr 'then' expr [ 'else' expr ]
| 'while' expr 'do' expr 'done'
| 'for' value-name '=' expr ( 'to' || 'downto' ) expr 'do' expr 'done'
| expr ';' expr
| 'match' expr 'with' pattern-matching
| 'function' pattern-matching
| 'fun' {{ parameter }} [ ':' typexpr ] '->' expr
| 'try' expr 'with' pattern-matching
| 'let' ['rec'] let-binding { 'and' let-binding } 'in' expr
| "let" "exception" constr-decl "in" expr
| 'let' 'module' module-name { '(' module-name ':' module-type ')' }
[ ':' module-type ] \\ '=' module-expr 'in' expr
| '(' expr ':>' typexpr ')'
| '(' expr ':' typexpr ':>' typexpr ')'
| 'assert' expr
| 'lazy' expr
| local-open
| object-expr
;
%BEGIN LATEX
\end{syntax} \begin{syntax}
%END LATEX
argument:
expr
| '~' label-name
| '~' label-name ':' expr
| '?' label-name
| '?' label-name ':' expr
;
%\end{syntax} \begin{syntax}
pattern-matching:
[ '|' ] pattern ['when' expr] '->' expr
{ '|' pattern ['when' expr] '->' expr }
;
let-binding:
pattern '=' expr
| value-name { parameter } [':' typexpr] [':>' typexpr] '=' expr
| value-name ':' poly-typexpr '=' expr %since 3.12
;
parameter:
pattern
| '~' label-name
| '~' '(' label-name [':' typexpr] ')'
| '~' label-name ':' pattern
| '?' label-name
| '?' '(' label-name [':' typexpr] ['=' expr] ')'
| '?' label-name ':' pattern
| '?' label-name ':' '(' pattern [':' typexpr] ['=' expr] ')'
;
local-open:
| "let" "open" module-path "in" expr
| module-path '.(' expr ')'
| module-path '.[' expr ']'
| module-path '.[|' expr '|]'
| module-path '.{' expr '}'
| module-path '.{<' expr '>}'
;
object-expr:
| 'new' class-path
| 'object' class-body 'end'
| expr '#' method-name
| inst-var-name
| inst-var-name '<-' expr
| '{<' [ inst-var-name ['=' expr] { ';' inst-var-name ['=' expr] } [';'] ] '>}'
\end{syntax}
See also the following language extensions:
\hyperref[s:first-class-modules]{first-class modules},
\hyperref[s:explicit-overriding-open]{overriding in open statements},
\hyperref[s:bigarray-access]{syntax for Bigarray access},
\hyperref[s:attributes]{attributes},
\hyperref[s:extension-nodes]{extension nodes} and
\hyperref[s:index-operators]{extended indexing operators}.
\subsection{ss:precedence-and-associativity}{Precedence and associativity}
The table below shows the relative precedences and associativity of
operators and non-closed constructions. The constructions with higher
precedence come first. For infix and prefix symbols, we write
``"*"\ldots'' to mean ``any symbol starting with "*"''.
% Note that this table is duplicated in stdlib/ocaml_operators.mld,
% these tables should be kept in sync with the one below.
\ikwd{or\@\texttt{or}}%
\ikwd{if\@\texttt{if}}%
\ikwd{fun\@\texttt{fun}}%
\ikwd{function\@\texttt{function}}%
\ikwd{match\@\texttt{match}}%
\ikwd{try\@\texttt{try}}%
\ikwd{let\@\texttt{let}}%
\ikwd{mod\@\texttt{mod}}
\ikwd{land\@\texttt{land}}
\ikwd{lor\@\texttt{lor}}
\ikwd{lxor\@\texttt{lxor}}
\ikwd{lsl\@\texttt{lsl}}
\ikwd{lsr\@\texttt{lsr}}
\ikwd{asr\@\texttt{asr}}
\begin{tableau}{|l|l|}{Construction or operator}{Associativity}
\entree{prefix-symbol}{--}
\entree{". .( .[ .{" (see section~\ref{s:bigarray-access})}{--}
\entree{"#"\ldots}{left}
\entree{function application, constructor application, tag
application, "assert",
"lazy"}{left}
\entree{"- -." (prefix)}{--}
\entree{"**"\ldots" lsl lsr asr"}{right}
\entree{"*"\ldots" /"\ldots" %"\ldots" mod land lor lxor"}{left}
%% "`"@ident@"`"
\entree{"+"\ldots" -"\ldots}{left}
\entree{"::"}{right}
\entree{{\tt \char64}\ldots " ^"\ldots}{right}
\entree{"="\ldots" <"\ldots" >"\ldots" |"\ldots" &"\ldots" $"\ldots" !="}{left}
\entree{"& &&"}{right}
\entree{"or ||"}{right}
\entree{","}{--}
\entree{"<- :="}{right}
\entree{"if"}{--}
\entree{";"}{right}
\entree{"let match fun function try"}{--}
\end{tableau}
It is simple to test or refresh one's understanding:
\begin{caml_example}{toplevel}
3 + 3 mod 2, 3 + (3 mod 2), (3 + 3) mod 2;;
\end{caml_example}
\subsection{ss:expr-basic}{Basic expressions}
\subsubsection*{sss:expr-constants}{Constants}
An expression consisting in a constant evaluates to this constant. For example,
\texttt{3.14} or \texttt{[||]}.
\subsubsection*{sss:expr-var}{Value paths}
An expression consisting in an access path evaluates to the value bound to
this path in the current evaluation environment. The path can
be either a value name or an access path to a value component of a module.
\begin{caml_example}{toplevel}
Float.ArrayLabels.to_list;;
\end{caml_example}
\subsubsection*{sss:expr-parenthesized}{Parenthesized expressions}
\ikwd{begin\@\texttt{begin}}
\ikwd{end\@\texttt{end}}
The expressions @'(' expr ')'@ and @'begin' expr 'end'@ have the same
value as @expr@. The two constructs are semantically equivalent, but it
is good style to use @'begin' \ldots 'end'@ inside control structures:
\begin{alltt}
if \ldots then begin \ldots ; \ldots end else begin \ldots ; \ldots end
\end{alltt}
and @'(' \ldots ')'@ for the other grouping situations.
\begin{caml_example}{toplevel}
let x = 1 + 2 * 3
let y = (1 + 2) * 3;;
\end{caml_example}
\begin{caml_example}{toplevel}
let f a b =
if a = b then
print_endline "Equal"
else begin
print_string "Not Equal: ";
print_int a;
print_string " and ";
print_int b;
print_newline ()
end;;
\end{caml_example}
Parenthesized expressions can contain a type constraint, as in @'('
expr ':' typexpr ')'@. This constraint forces the type of @expr@ to be
compatible with @typexpr@.
Parenthesized expressions can also contain coercions
@'(' expr [':' typexpr] ':>' typexpr')'@ (see
subsection~\ref{ss:expr-coercions} below).
\subsubsection*{sss:expr-functions-application}{Function application}
Function application is denoted by juxtaposition of (possibly labeled)
expressions. The expression @expr argument_1 \ldots argument_n@
evaluates the expression @expr@ and those appearing in @argument_1@
to @argument_n@. The expression @expr@ must evaluate to a
functional value $f$, which is then applied to the values of
@argument_1, \ldots, argument_n@.
The order in which the expressions @expr, argument_1, \ldots,
argument_n@ are evaluated is not specified.
\begin{caml_example}{toplevel}
List.fold_left ( + ) 0 [1; 2; 3; 4; 5];;
\end{caml_example}
Arguments and parameters are matched according to their respective
labels. Argument order is irrelevant, except among arguments with the
same label, or no label.
\begin{caml_example}{toplevel}
ListLabels.fold_left ~f:( @ ) ~init:[] [[1; 2; 3]; [4; 5; 6]; [7; 8; 9]];;
\end{caml_example}
If a parameter is specified as optional (label prefixed by @"?"@) in the
type of @expr@, the corresponding argument will be automatically
wrapped with the constructor "Some", except if the argument itself is
also prefixed by @"?"@, in which case it is passed as is.
\begin{caml_example}{toplevel}
let fullname ?title first second =
match title with
| Some t -> t ^ " " ^ first ^ " " ^ second
| None -> first ^ " " ^ second
let name = fullname ~title:"Mrs" "Jane" "Fisher"
let address ?title first second town =
fullname ?title first second ^ "\n" ^ town;;
\end{caml_example}
If a non-labeled argument is passed, and its corresponding parameter
is preceded by one or several optional parameters, then these
parameters are {\em defaulted}, {\em i.e.} the value "None" will be
passed for them.
%
All other missing parameters (without corresponding argument), both
optional and non-optional, will be kept, and the result of the
function will still be a function of these missing parameters to the
body of $f$.
\begin{caml_example}{toplevel}
let fullname ?title first second =
match title with
| Some t -> t ^ " " ^ first ^ " " ^ second
| None -> first ^ " " ^ second
let name = fullname "Jane" "Fisher";;
\end{caml_example}
In all cases but exact match of order and labels, without optional
parameters, the function type should be known at the application
point. This can be ensured by adding a type constraint. Principality
of the derivation can be checked in the "-principal" mode.
As a special case, OCaml supports "labels-omitted" full applications:
if the function has a known arity, all the arguments are unlabeled,
and their number matches the number of non-optional parameters, then
labels are ignored and non-optional parameters are matched in their
definition order. Optional arguments are defaulted. This omission of
labels is discouraged and results in a warning, see \ref{ss:warn6}.
\subsubsection*{sss:expr-function-definition}{Function definition}
Two syntactic forms are provided to define functions. The first form
is introduced by the keyword "function":
\ikwd{function\@\texttt{function}}
$$\begin{array}{rlll}
\token{function} & \textsl{pattern}_1 & \token{->} & \textsl{expr}_1 \\
\token{|} & \ldots \\
\token{|} & \textsl{pattern}_n & \token{->} & \textsl{expr}_n
\end{array}$$
This expression evaluates to a functional value with one argument.
When this function is applied to a value \var{v}, this value is
matched against each pattern @pattern_1@ to @pattern_n@.
If one of these matchings succeeds, that is, if the value \var{v}
matches the pattern @pattern_i@ for some \var{i},
then the expression @expr_i@ associated to the selected pattern
is evaluated, and its value becomes the value of the function
application. The evaluation of @expr_i@ takes place in an
environment enriched by the bindings performed during the matching.
If several patterns match the argument \var{v}, the one that occurs
first in the function definition is selected. If none of the patterns
matches the argument, the exception "Match_failure" is raised.
%
\index{Matchfailure\@\verb`Match_failure`}
\begin{caml_example}{toplevel}
(function (0, 0) -> "both zero"
| (0, _) -> "first only zero"
| (_, 0) -> "second only zero"
| (_, _) -> "neither zero")
(7, 0);;
\end{caml_example}
The other form of function definition is introduced by the keyword "fun":
\ikwd{fun\@\texttt{fun}}
\begin{center}
@"fun" parameter_1 \ldots parameter_n "->" expr@
\end{center}
This expression is equivalent to:
\begin{center}
@"fun" parameter_1 "->" \ldots "fun" parameter_n "->" expr@
\end{center}
\begin{caml_example}{toplevel}
let f = (fun a -> fun b -> fun c -> a + b + c)
let g = (fun a b c -> a + b + c);;
\end{caml_example}
An optional type constraint @typexpr@ can be added before "->" to enforce
the type of the result to be compatible with the constraint @typexpr@:
\begin{center}
@"fun" parameter_1 \ldots parameter_n ":" typexpr "->" expr@
\end{center}
is equivalent to
\begin{center}
@"fun" parameter_1 "->" \ldots "fun" parameter_n "->" %
(expr ":" typexpr )@
\end{center}
Beware of the small syntactic difference between a type constraint on
the last parameter
\begin{center}
@"fun" parameter_1 \ldots (parameter_n":"typexpr)"->" expr @
\end{center}
and one on the result
\begin{center}
@"fun" parameter_1 \ldots parameter_n":" typexpr "->" expr @
\end{center}
\begin{caml_example}{toplevel}
let eq = fun (a : int) (b : int) -> a = b
let eq2 = fun a b : bool -> a = b
let eq3 = fun (a : int) (b : int) : bool -> a = b;;
\end{caml_example}
The parameter patterns @"~"lab@ and @"~("lab [":" typ]")"@
are shorthands for respectively @"~"lab":"lab@ and
@"~"lab":("lab [":" typ]")"@, and similarly for their optional
counterparts.
\begin{caml_example}{toplevel}
let bool_map ~cmp:(cmp : int -> int -> bool) l =
List.map cmp l
let bool_map' ~(cmp : int -> int -> bool) l =
List.map cmp l;;
\end{caml_example}
A function of the form @"fun" "?" lab ":(" pattern '=' expr_0 ')' '->'
expr@ is equivalent to
\begin{center}
@"fun" "?" lab ":" ident '->'
"let" pattern '='
"match" ident "with" "Some" ident "->" ident '|' "None" '->' expr_0
"in" expr@
\end{center}
where @ident@
is a fresh variable, except that it is unspecified when @expr_0@ is evaluated.
\begin{caml_example}{toplevel}
let open_file_for_input ?binary filename =
match binary with
| Some true -> open_in_bin filename
| Some false | None -> open_in filename
let open_file_for_input' ?(binary=false) filename =
if binary then open_in_bin filename else open_in filename;;
\end{caml_example}
After these two transformations, expressions are of the form
\begin{center}
@"fun" [label_1] pattern_1 "->" \ldots "fun" [label_n] pattern_n "->" expr@
\end{center}
If we ignore labels, which will only be meaningful at function
application, this is equivalent to
\begin{center}
@"function" pattern_1 "->" \ldots "function" pattern_n "->" expr@
\end{center}
That is, the @"fun"@ expression above evaluates to a curried function
with \var{n} arguments: after applying this function $n$ times to the
values @@v@_1 \ldots @v@_n@, the values will be matched
in parallel against the patterns @pattern_1 \ldots pattern_n@.
If the matching succeeds, the function returns the value of @expr@ in
an environment enriched by the bindings performed during the matchings.
If the matching fails, the exception "Match_failure" is raised.
\subsubsection*{sss:guards-in-pattern-matchings}{Guards in pattern-matchings}
\ikwd{when\@\texttt{when}}
The cases of a pattern matching (in the @"function"@, @"match"@ and
@"try"@ constructs) can include guard expressions, which are
arbitrary boolean expressions that must evaluate to "true" for the
match case to be selected. Guards occur just before the @"->"@ token and
are introduced by the @"when"@ keyword:
$$\begin{array}{rlll}
\token{function} & \nt{pattern}_1 \; [\token{when} \; \nt{cond}_1] & \token{->} & \nt{expr}_1 \\
\token{|} & \ldots \\
\token{|} & \nt{pattern}_n \; [\token{when} \; \nt{cond}_n] & \token{->} & \nt{expr}_n
\end{array}$$
Matching proceeds as described before, except that if the value
matches some pattern @pattern_i@ which has a guard @@cond@_i@, then the
expression @@cond@_i@ is evaluated (in an environment enriched by the
bindings performed during matching). If @@cond@_i@ evaluates to "true",
then @expr_i@ is evaluated and its value returned as the result of the
matching, as usual. But if @@cond@_i@ evaluates to "false", the matching
is resumed against the patterns following @pattern_i@.
\begin{caml_example}{toplevel}
let rec repeat f = function
| 0 -> ()
| n when n > 0 -> f (); repeat f (n - 1)
| _ -> raise (Invalid_argument "repeat");;
\end{caml_example}
\subsubsection*{sss:expr-localdef}{Local definitions}
\ikwd{let\@\texttt{let}}
The @"let"@ and @"let" "rec"@ constructs bind value names locally.
The construct
\begin{center}
@"let" pattern_1 "=" expr_1 "and" \ldots "and" pattern_n "=" expr_n "in" expr@
\end{center}
evaluates @expr_1 \ldots expr_n@ in some unspecified order and matches
their values against the patterns @pattern_1 \ldots pattern_n@. If the
matchings succeed, @expr@ is evaluated in the environment enriched by
the bindings performed during matching, and the value of @expr@ is
returned as the value of the whole @"let"@ expression. If one of the
matchings fails, the exception "Match_failure" is raised.
%
\index{Matchfailure\@\verb`Match_failure`}
\begin{caml_example}{toplevel}
let v =
let x = 1 in [x; x; x]
let v' =
let a, b = (1, 2) in a + b
let v'' =
let a = 1 and b = 2 in a + b;;
\end{caml_example}
An alternate syntax is provided to bind variables to functional
values: instead of writing
\begin{center}
@"let" ident "=" "fun" parameter_1 \ldots parameter_m "->" expr@
\end{center}
in a @"let"@ expression, one may instead write
\begin{center}
@"let" ident parameter_1 \ldots parameter_m "=" expr@
\end{center}
\begin{caml_example}{toplevel}
let f = fun x -> fun y -> fun z -> x + y + z
let f' = fun x y z -> x + y + z
let f'' x y z = x + y + z;;
\end{caml_example}
\noindent
Recursive definitions of names are introduced by @"let" "rec"@:
\begin{center}
@"let" "rec" pattern_1 "=" expr_1 "and" \ldots "and" pattern_n "=" expr_n
"in" expr@
\end{center}
The only difference with the @"let"@ construct described above is
that the bindings of names to values performed by the
pattern-matching are considered already performed when the expressions
@expr_1@ to @expr_n@ are evaluated. That is, the expressions @expr_1@
to @expr_n@ can reference identifiers that are bound by one of the
patterns @pattern_1, \ldots, pattern_n@, and expect them to have the
same value as in @expr@, the body of the @"let" "rec"@ construct.
\begin{caml_example}{toplevel}
let rec even =
function 0 -> true | n -> odd (n - 1)
and odd =
function 0 -> false | n -> even (n - 1)
in
even 1000;;
\end{caml_example}
The recursive definition is guaranteed to behave as described above if
the expressions @expr_1@ to @expr_n@ are function definitions
(@"fun" \ldots@ or @"function" \ldots@), and the patterns @pattern_1
\ldots pattern_n@ are just value names, as in:
\begin{center}
@"let" "rec" name_1 "=" "fun" \ldots
"and" \ldots
"and" name_n "=" "fun" \ldots
"in" expr@
\end{center}
This defines @name_1 \ldots name_n@ as mutually recursive functions
local to @expr@.
The behavior of other forms of @"let" "rec"@ definitions is
implementation-dependent. The current implementation also supports
a certain class of recursive definitions of non-functional values,
as explained in section~\ref{s:letrecvalues}.
\subsubsection{sss:expr-let-exception}{Local exceptions}
(Introduced in OCaml 4.04)
It is possible to define local exceptions in expressions:
@ "let" exception constr-decl "in" expr @ .
\begin{caml_example}{toplevel}
let map_empty_on_negative f l =
let exception Negative in
let aux x = if x < 0 then raise Negative else f x in
try List.map aux l with Negative -> [];;
\end{caml_example}
The syntactic scope of the exception constructor is the inner
expression, but nothing prevents exception values created with this
constructor from escaping this scope. Two executions of the definition
above result in two incompatible exception constructors (as for any
exception definition). For instance:
\begin{caml_example}{toplevel}
let gen () = let exception A in A
let () = assert(gen () = gen ());;
\end{caml_example}
\subsubsection{sss:expr-explicit-polytype}{Explicit polymorphic type annotations}
(Introduced in OCaml 3.12)
Polymorphic type annotations in @"let"@-definitions behave in a way
similar to polymorphic methods:
\begin{center}
@"let" pattern_1 ":" typ_1 \ldots typ_n "." typexpr "=" expr @
\end{center}
These annotations explicitly require the defined value to be polymorphic,
and allow one to use this polymorphism in recursive occurrences
(when using @"let" "rec"@). Note however that this is a normal polymorphic
type, unifiable with any instance of itself.
\subsection{ss:expr-control}{Control structures}
\subsubsection*{sss:expr-sequence}{Sequence}
The expression @expr_1 ";" expr_2@ evaluates @expr_1@ first, then
@expr_2@, and returns the value of @expr_2@.
\begin{caml_example}{toplevel}
let print_pair (a, b) =
print_string "(";
print_string (string_of_int a);
print_string ",";
print_string (string_of_int b);
print_endline ")";;
\end{caml_example}
\subsubsection*{sss:expr-conditional}{Conditional}
\ikwd{if\@\texttt{if}}
The expression @"if" expr_1 "then" expr_2 "else" expr_3@ evaluates to
the value of @expr_2@ if @expr_1@ evaluates to the boolean @"true"@,
and to the value of @expr_3@ if @expr_1@ evaluates to the boolean
@"false"@.
\begin{caml_example}{toplevel}
let rec factorial x =
if x <= 1 then 1 else x * factorial (x - 1);;
\end{caml_example}
The @"else" expr_3@ part can be omitted, in which case it defaults to
@"else" "()"@.
\begin{caml_example}{toplevel}
let debug = ref false
let log msg =
if !debug then prerr_endline msg;;
\end{caml_example}
\subsubsection*{sss:expr-case}{Case expression}\ikwd{match\@\texttt{match}}
The expression
$$\begin{array}{rlll}
\token{match} & \textsl{expr} \\
\token{with} & \textsl{pattern}_1 & \token{->} & \textsl{expr}_1 \\
\token{|} & \ldots \\
\token{|} & \textsl{pattern}_n & \token{->} & \textsl{expr}_n
\end{array}$$
matches the value of @expr@ against the patterns @pattern_1@ to
@pattern_n@. If the matching against @pattern_i@ succeeds, the
associated expression @expr_i@ is evaluated, and its value becomes the
value of the whole @'match'@ expression. The evaluation of
@expr_i@ takes place in an environment enriched by the bindings
performed during matching. If several patterns match the value of
@expr@, the one that occurs first in the @'match'@ expression is
selected.
\begin{caml_example}{toplevel}
let rec sum l =
match l with
| [] -> 0
| h :: t -> h + sum t;;
\end{caml_example}
If none of the patterns match the value of @expr@, the
exception "Match_failure" is raised.
%
\index{Matchfailure\@\verb`Match_failure`}
\begin{caml_example}{toplevel}[warning=8, error]
let unoption o =
match o with
| Some x -> x
let l = List.map unoption [Some 1; Some 10; None; Some 2];;
\end{caml_example}
\subsubsection*{sss:expr-boolean-operators}{Boolean operators}
The expression @expr_1 '&&' expr_2@ evaluates to @'true'@ if both
@expr_1@ and @expr_2@ evaluate to @'true'@; otherwise, it evaluates to
@'false'@. The first component, @expr_1@, is evaluated first. The
second component, @expr_2@, is not evaluated if the first component
evaluates to @'false'@. Hence, the expression @expr_1 '&&' expr_2@ behaves
exactly as
\begin{center}
@'if' expr_1 'then' expr_2 'else' 'false'@.
\end{center}
The expression @expr_1 '||' expr_2@ evaluates to @'true'@ if one of
the expressions
@expr_1@ and @expr_2@ evaluates to @'true'@; otherwise, it evaluates to
@'false'@. The first component, @expr_1@, is evaluated first. The
second component, @expr_2@, is not evaluated if the first component
evaluates to @'true'@. Hence, the expression @expr_1 '||' expr_2@ behaves
exactly as
\begin{center}
@'if' expr_1 'then' 'true' 'else' expr_2@.
\end{center}
\ikwd{or\@\texttt{or}}
The boolean operators @'&'@ and @'or'@ are deprecated synonyms for
(respectively) @'&&'@ and @'||'@.
\begin{caml_example}{toplevel}
let xor a b =
(a || b) && not (a && b);;
\end{caml_example}
\subsubsection*{sss:expr-loops}{Loops}
\ikwd{while\@\texttt{while}}
The expression @'while' expr_1 'do' expr_2 'done'@ repeatedly
evaluates @expr_2@ while @expr_1@ evaluates to @'true'@. The loop
condition @expr_1@ is evaluated and tested at the beginning of each
iteration. The whole @'while' \ldots 'done'@ expression evaluates to
the unit value @'()'@.
\begin{caml_example}{toplevel}
let chars_of_string s =
let i = ref 0 in
let chars = ref [] in
while !i < String.length s do
chars := s.[!i] :: !chars;
i := !i + 1
done;
List.rev !chars;;
\end{caml_example}
\ikwd{for\@\texttt{for}}
The expression @'for' name '=' expr_1 'to' expr_2 'do' expr_3 'done'@
first evaluates the expressions @expr_1@ and @expr_2@ (the boundaries)
into integer values \var{n} and \var{p}. Then, the loop body @expr_3@ is
repeatedly evaluated in an environment where @name@ is successively
bound to the values
$n$, $n+1$, \ldots, $p-1$, $p$.
The loop body is never evaluated if $n > p$.
\begin{caml_example}{toplevel}
let chars_of_string s =
let l = ref [] in
for p = 0 to String.length s - 1 do
l := s.[p] :: !l
done;
List.rev !l;;
\end{caml_example}
The expression @'for' name '=' expr_1 'downto' expr_2 'do' expr_3 'done'@
evaluates similarly, except that @name@ is successively bound to the values
$n$, $n-1$, \ldots, $p+1$, $p$.
The loop body is never evaluated if $n < p$.
\begin{caml_example}{toplevel}
let chars_of_string s =
let l = ref [] in
for p = String.length s - 1 downto 0 do
l := s.[p] :: !l
done;
!l;;
\end{caml_example}
In both cases, the whole @'for'@ expression evaluates to the unit
value @'()'@.
\subsubsection*{sss:expr-exception-handling}{Exception handling}
\ikwd{try\@\texttt{try}}
The expression
$$\begin{array}{rlll}
\token{try~} & \textsl{expr} \\
\token{with} & \textsl{pattern}_1 & \token{->} & \textsl{expr}_1 \\
\token{|} & \ldots \\
\token{|} & \textsl{pattern}_n & \token{->} & \textsl{expr}_n
\end{array}$$
evaluates the expression @expr@ and returns its value if the
evaluation of @expr@ does not raise any exception. If the evaluation
of @expr@ raises an exception, the exception value is matched against
the patterns @pattern_1@ to @pattern_n@. If the matching against
@pattern_i@ succeeds, the associated expression @expr_i@ is evaluated,
and its value becomes the value of the whole @'try'@ expression. The
evaluation of @expr_i@ takes place in an environment enriched by the
bindings performed during matching. If several patterns match the value of
@expr@, the one that occurs first in the @'try'@ expression is
selected. If none of the patterns matches the value of @expr@, the
exception value is raised again, thereby transparently ``passing
through'' the @'try'@ construct.
\begin{caml_example}{toplevel}
let find_opt p l =
try Some (List.find p l) with Not_found -> None;;
\end{caml_example}
\subsection{ss:expr-ops-on-data}{Operations on data structures}
\subsubsection*{sss:expr-products}{Products}
The expression @expr_1 ',' \ldots ',' expr_n@ evaluates to the
\var{n}-tuple of the values of expressions @expr_1@ to @expr_n@. The
evaluation order of the subexpressions is not specified.
\begin{caml_example}{toplevel}
(1 + 2 * 3, (1 + 2) * 3, 1 + (2 * 3));;
\end{caml_example}
\subsubsection*{sss:expr-variants}{Variants}
The expression @constr expr@ evaluates to the unary variant value
whose constructor is @constr@, and whose argument is the value of
@expr@. Similarly, the expression @constr '(' expr_1 ',' \ldots ','
expr_n ')'@ evaluates to the n-ary variant value whose constructor is
@constr@ and whose arguments are the values of @expr_1, \ldots,
expr_n@.
The expression @constr '('expr_1, \ldots, expr_n')'@ evaluates to the
variant value whose constructor is @constr@, and whose arguments are
the values of @expr_1 \ldots expr_n@.
\begin{caml_example}{toplevel}
type t = Var of string | Not of t | And of t * t | Or of t * t
let test = And (Var "x", Not (Or (Var "y", Var "z")));;
\end{caml_example}
For lists, some syntactic sugar is provided. The expression
@expr_1 '::' expr_2@ stands for the constructor @'(' '::' ')' @
applied to the arguments @'(' expr_1 ',' expr_2 ')'@, and therefore
evaluates to the list whose head is the value of @expr_1@ and whose tail
is the value of @expr_2@. The expression @'[' expr_1 ';' \ldots ';'
expr_n ']'@ is equivalent to @expr_1 '::' \ldots '::' expr_n '::'
'[]'@, and therefore evaluates to the list whose elements are the
values of @expr_1@ to @expr_n@.
\begin{caml_example}{toplevel}
0 :: [1; 2; 3] = 0 :: 1 :: 2 :: 3 :: [];;
\end{caml_example}
\subsubsection*{sss:expr-polyvars}{Polymorphic variants}
The expression @"`"tag-name expr@ evaluates to the polymorphic variant
value whose tag is @tag-name@, and whose argument is the value of @expr@.
\begin{caml_example}{toplevel}
let with_counter x = `V (x, ref 0);;
\end{caml_example}
\subsubsection*{sss:expr-records}{Records}
The expression @'{' field_1 ['=' expr_1] ';' \ldots ';' field_n ['='
expr_n ']}'@ evaluates to the record value
$\{ field_1 = v_1; \ldots; field_n = v_n \}$
where $v_i$ is the value of @expr_i@ for \fromoneto{i}{n}.
A single identifier @field_k@ stands for @field_k '=' field_k@,
and a qualified identifier @module-path '.' field_k@ stands for
@module-path '.' field_k '=' field_k@.
The fields @field_1@ to @field_n@ must all belong to the same record
type; each field of this record type must appear exactly
once in the record expression, though they can appear in any
order. The order in which @expr_1@ to @expr_n@ are evaluated is not
specified. Optional type constraints can be added after each field
@'{' field_1 ':' typexpr_1 '=' expr_1 ';'%
\ldots ';' field_n ':' typexpr_n '=' expr_n '}'@
to force the type of @field_k@ to be compatible with @typexpr_k@.
\begin{caml_example}{toplevel}
type t = {house_no : int; street : string; town : string; postcode : string}
let address x =
Printf.sprintf "The occupier\n%i %s\n%s\n%s"
x.house_no x.street x.town x.postcode;;
\end{caml_example}
The expression
@"{" expr "with" field_1 ["=" expr_1] ";" \ldots ";" field_n ["=" expr_n] "}"@
builds a fresh record with fields @field_1 \ldots field_n@ equal to
@expr_1 \ldots expr_n@, and all other fields having the same value as
in the record @expr@. In other terms, it returns a shallow copy of
the record @expr@, except for the fields @field_1 \ldots field_n@,
which are initialized to @expr_1 \ldots expr_n@. As previously,
single identifier @field_k@ stands for @field_k '=' field_k@,
a qualified identifier @module-path '.' field_k@ stands for
@module-path '.' field_k '=' field_k@ and it is
possible to add an optional type constraint on each field being updated
with
@"{" expr "with" field_1 ':' typexpr_1 "=" expr_1 ";" %
\ldots ";" field_n ':' typexpr_n "=" expr_n "}"@.
\begin{caml_example}{toplevel}
type t = {house_no : int; street : string; town : string; postcode : string}
let uppercase_town address =
{address with town = String.uppercase_ascii address.town};;
\end{caml_example}
The expression @expr_1 '.' field@ evaluates @expr_1@ to a record
value, and returns the value associated to @field@ in this record
value.
The expression @expr_1 '.' field '<-' expr_2@ evaluates @expr_1@ to a record
value, which is then modified in-place by replacing the value
associated to @field@ in this record by the value of
@expr_2@. This operation is permitted only if @field@ has been
declared @'mutable'@ in the definition of the record type. The whole
expression @expr_1 '.' field '<-' expr_2@ evaluates to the unit value
@'()'@.
\begin{caml_example}{toplevel}
type t = {mutable upper : int; mutable lower : int; mutable other : int}
let stats = {upper = 0; lower = 0; other = 0}
let collect =
String.iter
(function
| 'A'..'Z' -> stats.upper <- stats.upper + 1
| 'a'..'z' -> stats.lower <- stats.lower + 1
| _ -> stats.other <- stats.other + 1);;
\end{caml_example}
\subsubsection*{sss:expr-arrays}{Arrays}
The expression @'[|' expr_1 ';' \ldots ';' expr_n '|]'@ evaluates to
a \var{n}-element array, whose elements are initialized with the values of
@expr_1@ to @expr_n@ respectively. The order in which these
expressions are evaluated is unspecified.
The expression @expr_1 '.(' expr_2 ')'@ returns the value of element
number @expr_2@ in the array denoted by @expr_1@. The first element
has number 0; the last element has number $n-1$, where \var{n} is the
size of the array. The exception "Invalid_argument" is raised if the
access is out of bounds.
The expression @expr_1 '.(' expr_2 ')' '<-' expr_3@ modifies in-place
the array denoted by @expr_1@, replacing element number @expr_2@ by
the value of @expr_3@. The exception "Invalid_argument" is raised if
the access is out of bounds. The value of the whole expression is @'()'@.
\begin{caml_example}{toplevel}
let scale arr n =
for x = 0 to Array.length arr - 1 do
arr.(x) <- arr.(x) * n
done
let x = [|1; 10; 100|]
let _ = scale x 2;;
\end{caml_example}
\subsubsection*{sss:expr-strings}{Strings}
The expression @expr_1 '.[' expr_2 ']'@ returns the value of character
number @expr_2@ in the string denoted by @expr_1@. The first character
has number 0; the last character has number $n-1$, where \var{n} is the
length of the string. The exception "Invalid_argument" is raised if the
access is out of bounds.
\begin{caml_example}{toplevel}
let iter f s =
for x = 0 to String.length s - 1 do f s.[x] done;;
\end{caml_example}
The expression @expr_1 '.[' expr_2 ']' '<-' expr_3@ modifies in-place
the string denoted by @expr_1@, replacing character number @expr_2@ by
the value of @expr_3@. The exception "Invalid_argument" is raised if
the access is out of bounds. The value of the whole expression is @'()'@.
{\bf Note:} this possibility is offered only for backward
compatibility with older versions of OCaml and will be removed in a
future version. New code should use byte sequences and the "Bytes.set"
function.
\subsection{ss:expr-operators}{Operators}
\ikwd{mod\@\texttt{mod}}
\ikwd{land\@\texttt{land}}
\ikwd{lor\@\texttt{lor}}
\ikwd{lxor\@\texttt{lxor}}
\ikwd{lsl\@\texttt{lsl}}
\ikwd{lsr\@\texttt{lsr}}
\ikwd{asr\@\texttt{asr}}
Symbols from the class @infix-symbol@, as well as the keywords
@"*"@, @"+"@, @"-"@, @'-.'@, @"="@, @"!="@, @"<"@, @">"@, @"or"@, @"||"@,
@"&"@, @"&&"@, @":="@, @"mod"@, @"land"@, @"lor"@, @"lxor"@, @"lsl"@, @"lsr"@,
and @"asr"@ can appear in infix position (between two
expressions). Symbols from the class @prefix-symbol@, as well as
the keywords @"-"@ and @"-."@
can appear in prefix position (in front of an expression).
\begin{caml_example}{toplevel}
(( * ), ( := ), ( || ));;
\end{caml_example}
Infix and prefix symbols do not have a fixed meaning: they are simply
interpreted as applications of functions bound to the names
corresponding to the symbols. The expression @prefix-symbol expr@ is
interpreted as the application @'(' prefix-symbol ')'
expr@. Similarly, the expression @expr_1 infix-symbol expr_2@ is
interpreted as the application @'(' infix-symbol ')' expr_1 expr_2@.
The table below lists the symbols defined in the initial environment
and their initial meaning. (See the description of the core
library module "Stdlib" in chapter~\ref{c:corelib} for more
details). Their meaning may be changed at any time using
@"let" "(" infix-op ")" name_1 name_2 "=" \ldots@
\begin{caml_example}{toplevel}
let ( + ), ( - ), ( * ), ( / ) = Int64.(add, sub, mul, div);;
\end{caml_example}
Note: the operators @'&&'@, @'||'@, and @'~-'@ are handled specially
and it is not advisable to change their meaning.
The keywords @'-'@ and @'-.'@ can appear both as infix and
prefix operators. When they appear as prefix operators, they are
interpreted respectively as the functions @'(~-)'@ and @'(~-.)'@.
%% Conversely, a regular function identifier can also be used as an infix
%% operator by enclosing it in backquotes: @expr_1 '`' ident '`' expr_2@
%% is interpreted as the application @ident expr_1 expr_2@.
\ikwd{mod\@\texttt{mod}}%
\ikwd{land\@\texttt{land}}%
\ikwd{lor\@\texttt{lor}}%
\ikwd{lxor\@\texttt{lxor}}%
\ikwd{lsl\@\texttt{lsl}}%
\ikwd{lsr\@\texttt{lsr}}%
\ikwd{asr\@\texttt{asr}}%
\begin{tableau}{|l|p{12cm}|}{Operator}{Initial meaning}
\entree{"+"}{Integer addition.}
\entree{"-" (infix)}{Integer subtraction.}
\entree{"~- -" (prefix)}{Integer negation.}
\entree{"*"}{Integer multiplication.}
\entree{"/"}{Integer division.
Raise "Division_by_zero" if second argument is zero.}
\entree{"mod"}{Integer modulus. Raise
"Division_by_zero" if second argument is zero.}
\entree{"land"}{Bitwise logical ``and'' on integers.}
\entree{"lor"}{Bitwise logical ``or'' on integers.}
\entree{"lxor"}{Bitwise logical ``exclusive or'' on integers.}
\entree{"lsl"}{Bitwise logical shift left on integers.}
\entree{"lsr"}{Bitwise logical shift right on integers.}
\entree{"asr"}{Bitwise arithmetic shift right on integers.}
\entree{"+."}{Floating-point addition.}
\entree{"-." (infix)}{Floating-point subtraction.}
\entree{"~-. -." (prefix)}{Floating-point negation.}
\entree{"*."}{Floating-point multiplication.}
\entree{"/."}{Floating-point division.}
\entree{"**"}{Floating-point exponentiation.}
\entree{{\tt\char64} }{List concatenation.}
\entree{"^" }{String concatenation.}
\entree{"!" }{Dereferencing (return the current
contents of a reference).}
\entree{":="}{Reference assignment (update the
reference given as first argument with the value of the second
argument).}
\entree{"=" }{Structural equality test.}
\entree{"<>" }{Structural inequality test.}
\entree{"==" }{Physical equality test.}
\entree{"!=" }{Physical inequality test.}
\entree{"<" }{Test ``less than''.}
\entree{"<=" }{Test ``less than or equal''.}
\entree{">" }{Test ``greater than''.}
\entree{">=" }{Test ``greater than or equal''.}
\entree{"&& &"}{Boolean conjunction.}
\entree{"|| or"}{Boolean disjunction.}
\end{tableau}
\subsection{ss:expr-obj}{Objects} \label{s:objects}
\subsubsection*{sss:expr-obj-creation}{Object creation}
\ikwd{new\@\texttt{new}}
When @class-path@ evaluates to a class body, @'new' class-path@
evaluates to a new object containing the instance variables and
methods of this class.
\begin{caml_example}{toplevel}
class of_list (lst : int list) = object
val mutable l = lst
method next =
match l with
| [] -> raise (Failure "empty list");
| h::t -> l <- t; h
end
let a = new of_list [1; 1; 2; 3; 5; 8; 13]
let b = new of_list;;
\end{caml_example}
When @class-path@ evaluates to a class function, @'new' class-path@
evaluates to a function expecting the same number of arguments and
returning a new object of this class.
\subsubsection*{sss:expr-obj-immediate}{Immediate object creation}
\ikwd{object\@\texttt{object}}
Creating directly an object through the @'object' class-body 'end'@
construct is operationally equivalent to defining locally a @'class'
class-name '=' 'object' class-body 'end'@ ---see sections
\ref{sss:class-body} and following for the syntax of @class-body@---
and immediately creating a single object from it by @'new' class-name@.
\begin{caml_example}{toplevel}
let o =
object
val secret = 99
val password = "unlock"
method get guess = if guess <> password then None else Some secret
end;;
\end{caml_example}
The typing of immediate objects is slightly different from explicitly
defining a class in two respects. First, the inferred object type may
contain free type variables. Second, since the class body of an
immediate object will never be extended, its self type can be unified
with a closed object type.
\subsubsection*{sss:expr-method}{Method invocation}
The expression @expr '#' method-name@ invokes the method
@method-name@ of the object denoted by @expr@.
\begin{caml_example}{toplevel}
class of_list (lst : int list) = object
val mutable l = lst
method next =
match l with
| [] -> raise (Failure "empty list");
| h::t -> l <- t; h
end
let a = new of_list [1; 1; 2; 3; 5; 8; 13]
let third = ignore a#next; ignore a#next; a#next;;
\end{caml_example}
If @method-name@ is a polymorphic method, its type should be known at
the invocation site. This is true for instance if @expr@ is the name
of a fresh object (@'let' ident = 'new' class-path \dots @) or if
there is a type constraint. Principality of the derivation can be
checked in the "-principal" mode.
\subsubsection*{sss:expr-obj-variables}{Accessing and modifying instance variables}
The instance variables of a class are visible only in the body of the
methods defined in the same class or a class that inherits from the
class defining the instance variables. The expression @inst-var-name@
evaluates to the value of the given instance variable. The expression
@inst-var-name '<-' expr@ assigns the value of @expr@ to the instance
variable @inst-var-name@, which must be mutable. The whole expression
@inst-var-name '<-' expr@ evaluates to @"()"@.
\begin{caml_example}{toplevel}
class of_list (lst : int list) = object
val mutable l = lst
method next =
match l with (* access instance variable *)
| [] -> raise (Failure "empty list");
| h::t -> l <- t; h (* modify instance variable *)
end;;
\end{caml_example}
\subsubsection*{sss:expr-obj-duplication}{Object duplication}
An object can be duplicated using the library function "Oo.copy"
(see module \stdmoduleref{Oo}). Inside a method, the expression
@ '{<' [inst-var-name ['=' expr] { ';' inst-var-name ['=' expr] }] '>}'@
returns a copy of self with the given instance variables replaced by
the values of the associated expressions. A single instance variable
name @id@ stands for @id '=' id@. Other instance variables have the same
value in the returned object as in self.
\begin{caml_example}{toplevel}
let o =
object
val secret = 99
val password = "unlock"
method get guess = if guess <> password then None else Some secret
method with_new_secret s = {< secret = s >}
end;;
\end{caml_example}
\subsection{ss:expr-coercions}{Coercions}
Expressions whose type contains object or polymorphic variant types
can be explicitly coerced (weakened) to a supertype.
%
The expression @'('expr ':>' typexpr')'@ coerces the expression @expr@
to type @typexpr@.
%
The expression @'('expr ':' typexpr_1 ':>' typexpr_2')'@ coerces the
expression @expr@ from type @typexpr_1@ to type @typexpr_2@.
The former operator will sometimes fail to coerce an expression @expr@
from a type @typ_1@ to a type @typ_2@
even if type @typ_1@ is a subtype of type
@typ_2@: in the current implementation it only expands two levels of
type abbreviations containing objects and/or polymorphic variants,
keeping only recursion when it is explicit in the class type (for objects).
As an exception to the above algorithm, if both the inferred type of @expr@
and @typ@ are ground ({\em i.e.} do not contain type variables), the
former operator behaves as the latter one, taking the inferred type of
@expr@ as @typ_1@. In case of failure with the former operator,
the latter one should be used.
It is only possible to coerce an expression @expr@ from type
@typ_1@ to type @typ_2@, if the type of @expr@ is an instance of
@typ_1@ (like for a type annotation), and @typ_1@ is a subtype
of @typ_2@. The type of the coerced expression is an
instance of @typ_2@. If the types contain variables,
they may be instantiated by the subtyping algorithm, but this is only
done after determining whether @typ_1@ is a potential subtype of
@typ_2@. This means that typing may fail during this latter
unification step, even if some instance of @typ_1@ is a subtype of
some instance of @typ_2@.
%
In the following paragraphs we describe the subtyping relation used.
\subsubsection*{sss:expr-obj-types}{Object types}
A fixed object type admits as subtype any object type that includes all
its methods. The types of the methods shall be subtypes of those in
the supertype. Namely,
\begin{center}
@ '<' met_1 ':' typ_1 ';' \dots ';' met_n ':' typ_n '>' @
\end{center}
is a supertype of
\begin{center}
@ '<' met_1 ':' typ@$'_1$@ ';' \dots ';' met_n ':' typ@$'_n$@ ';'
met@$_{n+1}$@ ':' typ@$'_{n+1}$@ ';' \dots ';' met@$_{n+m}$@ ':' typ@$'_{n+m}$@
~[';' '..'] '>' @
\end{center}
which may contain an ellipsis ".." if every @typ_i@ is a supertype of
the corresponding @typ@$'_i$.
A monomorphic method type can be a supertype of a polymorphic method
type. Namely, if @typ@ is an instance of @typ@$'$, then @ "'"@a@_1
\dots "'"@a@_n '.' typ@$'$ is a subtype of @typ@.
Inside a class definition, newly defined types are not available for
subtyping, as the type abbreviations are not yet completely
defined. There is an exception for coercing @@self@@ to the (exact)
type of its class: this is allowed if the type of @@self@@ does not
appear in a contravariant position in the class type, {\em i.e.} if
there are no binary methods.
\subsubsection*{sss:expr-polyvar-types}{Polymorphic variant types}
A polymorphic variant type @typ@ is a subtype of another polymorphic
variant type @typ@$'$ if the upper bound of @typ@ ({\em i.e.} the
maximum set of constructors that may appear in an instance of @typ@)
is included in the lower bound of @typ@$'$, and the types of arguments
for the constructors of @typ@ are subtypes of those in
@typ@$'$. Namely,
\begin{center}
@ "["["<"] "`"C_1 "of" typ_1 "|" \dots "|" "`"C_n "of" typ_n "]" @
\end{center}
which may be a shrinkable type, is a subtype of
\begin{center}
@ "["[">"] "`"C_1 "of" typ@$'_1$@ "|" \dots "|" "`"C_n "of" typ@$'_n$@
"|" "`"C@$_{n+1}$@ "of" typ@$'_{n+1}$@ "|" \dots "|" "`"C@$_{n+m}$@ "of"
typ@$'_{n+m}$@ "]" @
\end{center}
which may be an extensible type, if every @typ_i@ is a subtype of @typ@$'_i$.
\subsubsection*{sss:expr-variance}{Variance}
Other types do not introduce new subtyping, but they may propagate the
subtyping of their arguments. For instance, @typ_1 "*" typ_2@ is a
subtype of @typ@$'_1$@ "*" typ@$'_2$ when @typ_1@ and @typ_2@ are
respectively subtypes of @typ@$'_1$ and @typ@$'_2$.
For function types, the relation is more subtle:
@typ_1 "->" typ_2@ is a subtype of @typ@$'_1$@~"->" typ@$'_2$
if @typ_1@ is a supertype of @typ@$'_1$ and @typ_2@ is a
subtype of @typ@$'_2$. For this reason, function types are covariant in
their second argument (like tuples), but contravariant in their first
argument. Mutable types, like "array" or "ref" are neither covariant
nor contravariant, they are nonvariant, that is they do not propagate
subtyping.
For user-defined types, the variance is automatically inferred: a
parameter is covariant if it has only covariant occurrences,
contravariant if it has only contravariant occurrences,
variance-free if it has no occurrences, and nonvariant otherwise.
A variance-free parameter may change freely through subtyping, it does
not have to be a subtype or a supertype.
%
For abstract and private types, the variance must be given explicitly
(see section~\ref{ss:typedefs}),
otherwise the default is nonvariant. This is also the case for
constrained arguments in type definitions.
\subsection{ss:expr-other}{Other}
\subsubsection*{sss:expr-assertion}{Assertion checking}
\ikwd{assert\@\texttt{assert}}
OCaml supports the @"assert"@ construct to check debugging assertions.
The expression @"assert" expr@ evaluates the expression @expr@ and
returns @"()"@ if @expr@ evaluates to @"true"@. If it evaluates to
@"false"@ the exception
"Assert_failure" is raised with the source file name and the
location of @expr@ as arguments. Assertion
checking can be turned off with the "-noassert" compiler option. In
this case, @expr@ is not evaluated at all.
\begin{caml_example}{toplevel}
let f a b c =
assert (a <= b && b <= c);
(b -. a) /. (c -. b);;
\end{caml_example}
As a special case, @"assert false"@ is reduced to
@'raise' '('@"Assert_failure ..."@')'@, which gives it a polymorphic
type. This means that it can be used in place of any expression (for
example as a branch of any pattern-matching). It also means that
the @"assert false"@ ``assertions'' cannot be turned off by the
"-noassert" option.
%
\index{Assertfailure\@\verb`Assert_failure`}
\begin{caml_example}{toplevel}
let min_known_nonempty = function
| [] -> assert false
| l -> List.hd (List.sort compare l);;
\end{caml_example}
\subsubsection*{sss:expr-lazy}{Lazy expressions}
\ikwd{lazy\@\texttt{lazy}}
The expression @"lazy" expr@ returns a value \var{v} of type "Lazy.t" that
encapsulates the computation of @expr@. The argument @expr@ is not
evaluated at this point in the program. Instead, its evaluation will
be performed the first time the function "Lazy.force" is applied to the value
\var{v}, returning the actual value of @expr@. Subsequent applications
of "Lazy.force" to \var{v} do not evaluate @expr@ again. Applications
of "Lazy.force" may be implicit through pattern matching (see~\ref{sss:pat-lazy}).
\begin{caml_example}{toplevel}
let lazy_greeter = lazy (print_string "Hello, World!\n");;
Lazy.force lazy_greeter;;
\end{caml_example}
\subsubsection*{sss:expr-local-modules}{Local modules}
\ikwd{let\@\texttt{let}}
\ikwd{module\@\texttt{module}}
The expression
@"let" "module" module-name "=" module-expr "in" expr@
locally binds the module expression @module-expr@ to the identifier
@module-name@ during the evaluation of the expression @expr@.
It then returns the value of @expr@. For example:
\begin{caml_example}{toplevel}
let remove_duplicates comparison_fun string_list =
let module StringSet =
Set.Make(struct type t = string
let compare = comparison_fun end)
in
StringSet.elements
(List.fold_right StringSet.add string_list StringSet.empty);;
\end{caml_example}
\subsubsection*{sss:local-opens}{Local opens}
\ikwd{let\@\texttt{let}}
\ikwd{module\@\texttt{open}}
The expressions @"let" "open" module-path "in" expr@ and
@module-path'.('expr')'@ are strictly equivalent. These
constructions locally open the module referred to by the module path
@module-path@ in the respective scope of the expression @expr@.
\begin{caml_example}{toplevel}
let map_3d_matrix f m =
let open Array in
map (map (map f)) m
let map_3d_matrix' f =
Array.(map (map (map f)));;
\end{caml_example}
When the body of a local open expression is delimited by
@'[' ']'@, @'[|' '|]'@, or @'{' '}'@, the parentheses can be omitted.
For expression, parentheses can also be omitted for @'{<' '>}'@.
For example, @module-path'.['expr']'@ is equivalent to
@module-path'.(['expr'])'@, and @module-path'.[|' expr '|]'@ is
equivalent to @module-path'.([|' expr '|])'@.
\begin{caml_example}{toplevel}
let vector = Random.[|int 255; int 255; int 255; int 255|];;
\end{caml_example}
%% \newpage
|