Using, Understanding, and Unraveling the OCaml Language From Practice to Theory and Vice Versa Didier R´emy INRIA-Rocque...
38 downloads
838 Views
6MB Size
Report
This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!
Report copyright / DMCA form
Using, Understanding, and Unraveling the OCaml Language From Practice to Theory and Vice Versa Didier R´emy INRIA-Rocquencourt http://cristal.inria.fr/∼remy
Abstract. These course notes are addressed to a wide audience of people interested in modern programming languages in general, ML-like languages in particular, or simply in OCaml, whether they are programmers or language designers, beginners or knowledgeable readers —little prerequiresite is actually assumed. They provide a formal description of the operational semantics (evaluation) and statics semantics (type checking) of core ML and of several extensions starting from small variations on the core language to end up with the OCaml language —one of the most popular incarnation of ML— including its object-oriented layer. The tight connection between theory and practice is a constant goal: formal definitions are often accompanied by OCaml programs: an interpreter for the operational semantics and an algorithm for type reconstruction are included. Conversely, some practical programming situations taken from modular or object-oriented programming patterns are considered, compared with one another, and explained in terms of typechecking problems. Many exercises with different level of difficulties are proposed all along the way, so that the reader can continuously checks his understanding and trains his skills manipulating the new concepts; soon, he will feel invited to select more advanced exercises and pursue the exploration deeper so as to reach a stage where he can be left on his own.
G. Barthe et al. (Eds.): Applied Semantics, LNCS 2395, pp. 413–536, 2002. c Springer-Verlag Berlin Heidelberg 2002
414
Didier R´emy
Table of Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 2
Core ML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
2.1 2.2 2.3
Discovering Core ML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Syntax of Core ML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Dynamic Semantics of Core ML . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.1 Reduction Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.2 Properties of the Reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.3 Big-Step Operational Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Static Semantics of Core ML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4.1 Types and Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4.2 Type Inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4.3 Unification for Simple Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4.4 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5.1 Fix-Point Combinator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5.2 Recursive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5.3 Type Inference v.s. Type Checking . . . . . . . . . . . . . . . . . . . . . . . . .
2.4
2.5
421 423 424 425 431 434 437 437 439 442 446 449 449 451 452
3
The Core of OCaml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
3.1
3.3
Data Types and Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.1 Examples in OCaml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.2 Formalization of Superficial Pattern Matching . . . . . . . . . . . . . . . . 3.1.3 Recursive Datatype Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.4 Type Abbreviations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.5 Record Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Mutable Storage and Side Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.1 Formalization of the Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.2 Type Soundness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.3 Store and Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.4 Multiple-Field Mutable Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
The Object Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
4.1
Discovering Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1.1 Basic Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1.2 Polymorphism, Subtyping, and Parametric Classes . . . . . . . . . . . . Understanding Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2.1 Type-Checking Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2.2 Typing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Advanced Uses of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2
4.2
4.3
454 454 455 456 457 458 459 459 461 462 462 462
465 465 468 472 474 478 482
Using, Understanding, and Unraveling the OCaml Language
415
5
The Module Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
5.1
5.2 5.3
Using Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.1.1 Basic Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.1.2 Parameterized Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Advanced Uses of Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
Mixing Modules and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 495
6.1 6.2
Overlapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Combining Modules and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2.1 Classes as Module Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2.2 Classes as Pre-modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
487 487 491 491 491
495 496 497 498
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503 A
First Steps in OCaml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
B
Variant Types and Labeled Arguments . . . . . . . . . . . . . . . 510
B.1 B.2 B.3
Variant Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510 Labeled Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512 Optional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
C
Answers to Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530 List of All Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
416
Didier R´emy
!
" #$
%
& #$
' ! () !
* + ,$ * $ $$ $
Using, Understanding, and Unraveling the OCaml Language
417
Æ ! " # $ % & & # " ' " (
( & & ! " ) * ! " + Æ !" & !" ! $ & , ( " " # # &
418
Didier R´emy
! " # $ % & # ' ( ( ) $ *% + , # $ - .% / $& 0% $& 1%2 $& 3% /
, 4 /
(
, ) 4 5 16. , $ *% 5
- . / 4 & 0 7 ( " ( ( / 2 4 / ( 4
8
$ % 9 : ,
Using, Understanding, and Unraveling the OCaml Language
419
! " # $%&' ! ( )
) * + * ,
) ! -.
" / , * 01) 2 * 3 4
0! 4 1 * # 01 4 5 678
,
! ! # # 9&:6 ; # 0 4
!1
) * + , # < 9&=9 " # / " # > ! ?;2 " # ?@ < # ! A * B C
< - < . - . ! 5 2 " # ! C ,
C 2 C A "#
D ! ! E F F< 9&&7 ! *
420
Didier R´emy
! "###! $ % & ' ! () * ! ()
+ ! ,! ( ,! , ! - () *!
*+!
. ! .
/ () ! + ' ! 0 / ()
1 ! ! ,
+ ,
! , 2 / () ! ! , / ! () ! / ! . 3 ! ()4 - ()
( + $ % &! 5 )! 1 6.
Using, Understanding, and Unraveling the OCaml Language
421
! " # $ % & ' " ! ( & '
)
" * & ' + , & ' + - ./ 0 * " & ( 122' # 3 - 4! $
3
½
½
¾
¾
½
¾
422
Didier R´emy
! "
# $ %& ' %&
( ) $ ' $ '
!
" #
!
Using, Understanding, and Unraveling the OCaml Language
423
! "
# $ % &
'
2.2
The Syntax of Core ML
( & ) * + ½ , ½ - - ¾ , ¾ - - ½ , ½ ¾ , ¾ % ·
. / 0 /
424
Didier R´emy
! !
!
" # $ $
%
&
' ( '' ) ( *(
2.3
The Dynamic Semantics of Core ML
+ , $ &
Using, Understanding, and Unraveling the OCaml Language
425
! " #" $ % & ' " ( ) ! # # !
' & Æ ! Æ ' & !
* & '
' + , + -./0 1 + -230
' ' 45 6 6 7 Ú
426
Didier R´emy
Ú ½
½
½ ·½ ½ ·½
! " # $ %& '( '( ½
% & % & % ½ & Æ
#
) Æ
%Æ & *
%Æ· & %+ + &
# ,
- " ! Æ " "
Using, Understanding, and Unraveling the OCaml Language
427
Æ
!
" # $" # $ %
&'
( # $ ( ) #$
# $
#$ # $
*
¼
¼
428
Didier R´emy
Æ ! " Æ
! !
Using, Understanding, and Unraveling the OCaml Language
429
Æ ! Æ ¼
" #Ú $ % # $ #Ú $ & '(
# # $$ ## # ) !$$ *$ + # $, # # $$ ## # ) !$$$ *$ # # $$ #* ) !$ #Ú $ # # $$ #Æ $ #Ú $ #- -$ + #Æ $ & " , £
430
Didier R´emy
Æ
! "#
Æ $ % & $ $ $ $ ' ( ) *+ ) ,
- ( .
/
¾
0 1 ) 2
¿ ¾ 3 . ¼ (
$ ½ ¾ ¿
½ ¾ ¿ ¿ ½ ¾ ¾ ¿ ½
½
¿ ¾
" ½ '
*+ ,
Using, Understanding, and Unraveling the OCaml Language
431
½
¿
¿
½
¾
¾
½
¿
¾
Æ ! " # # $ %$ # & # '
# #
#
#
# (
$ $ )# ¼ $ # Ò Ò # Ò Ò Ò # %$ # ¼ & & * $ & # # # # # #
¼
¼
¼ ¼
¼
¼
¼
½
½
½
&
Ú
&&
Ú Ú
432
Didier R´emy
Æ
! Æ "
# Æ$ % "
½ ½ ·½ & $$
$ !
Æ ¼
¼
¼
¼
! $$ '() $
* ) ' * ) ' * ) ' ' * ) * ) ' ( * ) ' Æ· ( ' * ) ( ( Æ· + Æ
, $$ , % & # - $$ $$
& $$ - $$ $$ -
! .
!
Using, Understanding, and Unraveling the OCaml Language
433
! ! " ¼ ½ " #$
% & '
(
) * +, - --" " ¼
. /
, #
0 % #1 0 ,
% $ # %
&2
½
3 . ) 2 )
)
4 5 1
* ) & ! $
6 ¼
¼¼¼
¼¼
¼¼
¼
¼¼¼
*
434
Didier R´emy
! " # " $ %
& '
! $" % ( & '
))*
½
))* ½ + ))*
½
½ & ) ½ * , '
-. / 0
&
1 / 2 )
+ )
3 )
3 )
Using, Understanding, and Unraveling the OCaml Language
435
½ ½
½ ½
¼
¼
¼
¼
¼
¼
¼
¼
¼
¼
¼
¼
½ ½
½
¼
¼
¼
¼
¼
½
436
Didier R´emy
Ú½ Ú¾
! " Ú
Using, Understanding, and Unraveling the OCaml Language
437
Æ Æ
!
2.4
The Static Semantics of Core ML
"
#
$ %& ' (
" )
*
+
,
-
!
#
" " . ½
438
Didier R´emy
½
¼
¼
¼
¼
¼
¼
¼ ¼ ! " !
" " # $
! " ¼ #
" % & $ ! & ' & & ( '' ! ) ¼ *
¼
¼ !
¼ !
½
¼
+ ,! + ! + * + !
Using, Understanding, and Unraveling the OCaml Language
439
Æ Æ Æ ¼ ½ ½ Æ ! ! " # $ !
½ ¾ ¾ ½ %
½ ½ ½ ¾ ¾ ½ ¾
!
#
# &
'
#
(
Æ "#
' $
#
#
)
¼
%
440
Didier R´emy
½ ¾ ½ ¾ ½ ¾ ½ ¾
½ ¾ ½ ¾
! " # !" ! " # $
! % " & !" ! " '
( ½ ( %) ¡
%) ! "* & + ) , )% % ( ( + ) * ¼ ( - ¼ . ¼ ( ! ¼ " % !
Using, Understanding, and Unraveling the OCaml Language
441
¼
¼
¼
! "
# $ % &'
(
) ½ ¾ ( ¾ ½ (
) ½ ¾ ( ½ ( ( ¾ (
* $
+ $
, -
'
! "
442
Didier R´emy
½ ! " # # $ ! %
# ½ ½ & ' $ # ½ ! ( # $ ) ½ ½ ! Æ ! * ½ ½ ! ½ ¾ ¾
½ ¾ ¾ ½ ½ ! % ½ ¾ ¾ ½ ¾ ! # % # + , % # $ # $
$ ( $ $ - ( # . / # ( $
Using, Understanding, and Unraveling the OCaml Language
443
¡
¡
¡
¡
¡
¾
¾
¡
¡
¾
¾
¡
¼
¡
¡
¡
¡
¼
¼
¡
¡
¡
¡
¡
Æ ! Æ " # $
Æ %
&
%
'
( & )
* # + & # ½ , ¾ , ½ ¾
444
Didier R´emy
! " #
$ % & ' ( ) $ & ( * * + ( & , && & & *
" , &
- ½ ! ¾ ¾ ¾ ! & ½ ¾ ¾ ( ¾ . * % / ( * ( + % * )
+ ( % 0 !
Using, Understanding, and Unraveling the OCaml Language
445
½ ½ ¾ ¾ ½ ¾ ½ ¾ ! ½ ¾ ½ ¾ ½ " # ½ ¾ ¾ ! $ % ½ ¾ ¾ & #
½ ¾ ' ½ ¾ ' ½ ¾
'
! !
! ! ! !
446
Didier R´emy
! "" # $%% &
' ( )* )* + )* , , - . / "0 - . " %" %% ,
" %
" % ,
'
+1 ¼
¼
¼
¼
"% !
¾
"" %%
" + %
Using, Understanding, and Unraveling the OCaml Language
447
Æ !
¼ !
¼ "
¼ ¼ ¼ ¼ # $
%
¼
&
'
( )* + ,
" # $%
)* -
.
Æ
448
Didier R´emy
¼
¡
¼
! ½ ! ½ ¾ ¾ " ¾ ¾
"
#$ ! %
%
&
! ' ( ! Æ
(
! " # # # $ " %
Using, Understanding, and Unraveling the OCaml Language
449
! ! " # $% " # $% " #
!
" " ## " " ##
" # ! "# ! "# " " "$% ### " # " # ! "# " #
" # ! "# ! ! "# $" " " ## ! ! "# $%
½ ¾
½
! ½ " !
# $ "
450
Didier R´emy
½ ¾ ½ ¾ ½ ¾ ½ ¾
Æ
Æ
Æ
"
Æ
! #
$
"
% & ½ ¾ & ½ ¾
¼
¼
¼
'
()
! " ! !#
*
$!
*
#+
" %
½ & ½ ¾ & ¾
¼
¼
½ & ¾ ½ & ½ ¾ ½ ¼
¾ & ¼
¼
¾ & ¾ ½ & ½ ¾ ¾ ¼
¼
Using, Understanding, and Unraveling the OCaml Language
451
½
½
½
½ ¾ ¾ ¿ ¿
!" #$
!" %
" & " '()* " ¼
¼
¼
¼
+ ,-
$
$ . / *"0"*" 1 $
$ " 2 3# $ 4 " 4
$ " 2 .
4 " ! $ Æ $
. " 2 ! $ " ( 567 . ,- $ " 8 $
!!$ .! . "
! ! "
" #
5"'"0 $ 4 " 9 $ " : / .!/
452
Didier R´emy
!"
# $ %
&
ں׺ !" '( ) * +
+
#
+ # , - * + * . # + +
# ) #
# /012 ) # Æ Æ # ! 3+ Æ ) '(
#
# * 4
'(
" '( ' 5 #
+
5 6 6 4
Using, Understanding, and Unraveling the OCaml Language
453
Further Reading ! " # # $ % &' ! %( # " ! ) Æ
# % * + (,-% ) # '
+ -.
454
Didier R´emy
The Core of OCaml
3.1
Data Types and Pattern Matching
! "# $ % # &
' $ # (
$ ) # # # *
# # $ #
(
(
' + )
Using, Understanding, and Unraveling the OCaml Language
455
!
"
#
$ % & '
( Ú %Ú & % &
) % & * +, * - * ,
%& . ½ %- * ' &
456
Didier R´emy
½ Æ Æ ½
½
½ ½
½
!
!
!
!! "# " #
# # $%& ' $% #
(
# )
! ! "
* + ( #
#
Using, Understanding, and Unraveling the OCaml Language
457
Æ Æ ! " #
$ %
!
! " $ %
& "# ' ( ) * + "# ' "# ' " ¼ # ! ,
- . / (. +
( / + / "# ' "
458
Didier R´emy
! ! ! " ! !
# $ % & ' ! ! #
! " ' # $ % "
! "
$ %
¼
¡
¡
¼
¡
¡
½ ½ ¾ ¾ ¾
( ( )*+
! ,
!
-
! " ½ # ½ . ¾ # !
! ! Æ # ½ Æ
! ' "
#
# ½ #
Using, Understanding, and Unraveling the OCaml Language
½ ½ ½
3.2
459
Mutable Storage and Side Effects
! " # # $% & ' ( ) $% & * ) #& $% '
+ &'
, #& - #& & % . Æ# +
,
460
Didier R´emy
¼
¼
¼
!"
!" !" !"
# $
%& %¼ &
¼
#
' !' ( " !' ( " ' !' ( " ' !' ( '" ' !)" ' ) )
*
$
! "
# +
¼ !¼ " ! "
,
Using, Understanding, and Unraveling the OCaml Language
461
! "#$% & ' (
)* ! & +
, ¼
!- &
.
/ ¼ ! & ¼ ¼ !& ¼ !& !¼ & ! ¼ ¼ &
! &! ( ¼
¼
&
¼
462
Didier R´emy
Ú Ú ! !
" # $ % & # ' ( ) ( ( ¼
¼
* )) +
& , -.
&
! "
* %/ 0 0 # 0
1 & Æ( 2 -. 3
33
Using, Understanding, and Unraveling the OCaml Language
463
! " ! #
"
! $ %
& ' ! '
! ! ( & ! &
! ' &
¼
¼
¼
¼
¼
¼
½
¾
½
¾
¼
¼
½
¼
½
¾
¾
)
Further Reading
* !
!
+ " , - .
/% - . ! * ! 0102 & , '
"
464
Didier R´emy
! "
#$%&&%%%&'
#()' * ! " +
,
Æ - ./ #0%' - #%1$0&)02'
Using, Understanding, and Unraveling the OCaml Language
465
The Object Layer
4.1
Discovering Objects and Classes
! "#
$ " % # # ! " & ' ( " % # ) # & ! ' " ) !
* +
466
Didier R´emy
!
"
# $ % & & !
# ' ( $ ) &
*
Using, Understanding, and Unraveling the OCaml Language
467
!
" # ! #
$
#
# ! # %
468
Didier R´emy
Æ
Æ
!
" Æ #
"
" $ " %
& & " '" " &
" " &
! %
#
' & "
%
Using, Understanding, and Unraveling the OCaml Language
469
! " !
"# $ ! % ! & "# ' "# ( ( !
470
Didier R´emy
!
"
#
$
% &' ( " ) " * + ,- , -* . .
& ' +
)
. /
.
+ .
( .
. . &
.
/
Using, Understanding, and Unraveling the OCaml Language
471
! "# $ % &
! '
" #
$ # #
%
#
"¼ "½ " &
( ! $
& ) Æ *+ ! ! , -
472
Didier R´emy
!
" # # $ #
% & ' ¼
$ #
( ) # *
4.2
$
Understanding Objects and Classes
+ # ) & #
Using, Understanding, and Unraveling the OCaml Language
473
! " #
$ %
& "
'
( ) $* ) ) % + , + , -
!
) *
. "
$ / %
! 0 " 1 $% " $% 2 & ½ 2 ½ & ¾ 2 ½
2 & ½ 2 $% ½ & ¾ 2 $% ½ ! 0 *
474
Didier R´emy
! " #
$
" Æ % & &
#
$ # # & ! ' ' ( ) % ) ¼ ) * )
Using, Understanding, and Unraveling the OCaml Language
475
¼
! ¼ ¼ " ¼ ¼ ! ¼ ¼
#$ %
% #& $ % ' (
)**+ , , - ./-
0
% $ % 0 ' 0
0 % ¾Å 1 ( 2
, % ' $ , $
"
! $
476
Didier R´emy
!
½ " ¾ ½ ¾ ½ ½ ½ " ¾ ¾ ¾ " ½ ½ ¾ ¾ " ½ " ¾ ¾ ¾ " ½ ½
!
¾
" #¡
$ $ ¡ ¡
" # " % #
%
&
&
¡ ' # " %
' " % ( #¡ " )
" )*
+
"
, -.
/'
0
1
/ 2 / 2 2
( / *
/ 2 3
Using, Understanding, and Unraveling the OCaml Language
477
! "
# $! % % & '
( '
( )
* * ++ ! , ) - . ' $ /& 0 Æ/ 1 /
1 / 1 / 2 ) , 3 45 ! ,
) $ 1 / 2 & ) $ & )
1 - 1 $ 1 & ( $ $ & &
! ) )
478
Didier R´emy
!
" #
$
# %
&
' (' (
)
* '
( + ' (' (
$
+ + '(
' (' ( # # "
, !
+ # ' (' (
- &
' ( .
' ( .
Using, Understanding, and Unraveling the OCaml Language
479
¼
¼
¼
¼
¼
¼
¼
¼
! "
# Æ $
%
&
' ( )
# *
+
480
Didier R´emy
! " # $
" " ! " " % & ' () "
! !
* + * * , * + * * ' % " " !
- " & ! % & !
-
. " ! % ! !
" ! # " ! / " - ! " ! ! ½ , + + , ½ - . " # ! ! " ! 0 1 2 ½ * ½ + *
Using, Understanding, and Unraveling the OCaml Language
481
½
½
¾
¼
¼
½
¼
¾
¼
¾
½
¼
¼
½
¾
½
¾ ¼ ¾
½
½
¼
½
¾
¾
¼
¾
¾
¼
½
¼
¼
½ ¼ ¼
¼
¾
¼
½
¼
¾
½
Ë
½
¼
! " ½ ¾ ½ ¾ ½ ¾ # ½
¾ ! $
% & ' '
' () & ' * $ % +
$ + $ , ' * -
482
Didier R´emy
4.3
Advanced Uses of Objects
! " # $ " # % & # '()* + + %
# ,
%
!
- &
. # / / . & !/ # +
,
Using, Understanding, and Unraveling the OCaml Language
483
! ! " Æ
#
$ %
#
484
Didier R´emy
!
"
!" # $ $ $ % $ &' $ $ $ ( )!*
Using, Understanding, and Unraveling the OCaml Language
485
! " " # Æ " $ % &
" ' " $ $ ( & )&$ $ $ $ $ $
$
$ Further Reading
* " & (+ , -./0 1 & (+ " $ $1 " " 2 * & (+ , * $
-.!340 5 6 ($ -/40 & " (+ ) $
" $ " $ " -..0
486
Didier R´emy
!" #
$% & % !
Using, Understanding, and Unraveling the OCaml Language
487
The Module Language
! !
!
! !
! "
! # $ ! ! !
% ! "
!
5.1
Using Modules
! ! & " ' "( % ! ! ! ) * ! !
! !
! ! ! !
+
½
, ! !
-
-
.
! ) */
488
Didier R´emy
! "
# $ ½ % # &' ( )
*
!
"
# #
*# # # + # + , # ! ! #-
#- !
Using, Understanding, and Unraveling the OCaml Language
Å
489
Ë
!
"#
$ %
&
'%
( " % )
* +"+ %
490
Didier R´emy
! " #
!
! $
% ! #
# $
! # "
! # " #
&
#
$ ! !
Using, Understanding, and Unraveling the OCaml Language
491
Ë Ì Å
Å ! Ë Ì
Ë
!
"
5.2
Understanding Modules
# $ % &'()')*+(, - &)),
5.3
Advanced Uses of Modules
. !
492
Didier R´emy
!
"
# $ " # % % # " & " % " % " % $ % " '
'
'
'
! " # $ %
Using, Understanding, and Unraveling the OCaml Language
493
!
"# "# $
$
"#
% % &' %
( % % &' % %
!
) *
494
Didier R´emy
Æ
! ! " ¾ ¾ ¾ ! !" !
Æ
! " # $%
Using, Understanding, and Unraveling the OCaml Language
495
Mixing Modules and Objects
! " # $ #
% & ' & ()* &
&# &
+)) ,
, & - &
. Ü × × Ü Ü × . × × ×
/ & 0 ! 1 $
496
Didier R´emy
!" # $ % & ' # ' & ' & ! & $ "
' ( ) ' ! $ * ' ' ' ' ' ' ( ' 6.2
Combining Modules and Classes
" & + ' ' "' '
' # ' , & ' ' , - - '
Using, Understanding, and Unraveling the OCaml Language
497
!
" # # $ %
& " '( ) " * )
+ , )
! & , " , - . , . , ,
498
Didier R´emy
! " # $ ! " ! %
" &
" ! '( )*+ " , %
# - $ " .
' # - $
Using, Understanding, and Unraveling the OCaml Language
499
!
" " " # " $ " " " "
%
!
ººº
!
ººº
500
Didier R´emy
! " # $%
&
"
% " % '
(
&
Æ
")
(
* % (
'
" & %
+
Using, Understanding, and Unraveling the OCaml Language
501
!" # $ % # $ Æ &
502
Didier R´emy
Further Reading
! " #$%&'( ! ) #***+(
! , ! " #$-..%( / ! 0 1 Æ 1 2
Using, Understanding, and Unraveling the OCaml Language
503
Further Reading
!" #
$ % &
'(!!)" * $ #
+ %
* ')"
# , * -.-/"
%
0 1)" )/12"
*
3
4 # - -2 " 4
3 # (-" ,
!''2)'" * * "
+
3 )-" 4
+ "5
3 + #
4 + * !2"5 $
! " ,
3 , * # , $ 6 -.-)" #
!(-!"5 -1)("
* 3 7 3 * # $ ,
$ 8 #
* 97 8 #
: 0 $
* $ #
1 .)2"
'!"
504
Didier R´emy
! " ! # $%
" !
&'()
&*''+) ,! &-.) ! / !
&+*0) 1 !
&.') 2 !
&*3*+)
Æ 1 2 $%
$% &0+) Æ 2 ! $% ! , $% ! !
$%
1 4 !
56
Using, Understanding, and Unraveling the OCaml Language
505
First Steps in OCaml
! " ! ! "
! # $
!
% & # $
" " "
% ' "
506
Didier R´emy
! ! #$ % # % ! $ # % ! & '$(
" # % ½ ½ " ½ # " % ½ " ½ # "
%
) $ $ $ $ $ $ $ $ * '++( $ , $ - $
$ $ ' (
! " ½ ¾ '. $ ( ! $ ½ ! ½ ! ' ( ! '½ ¾ ( ! $ ½ ½ ! ½ ½ !
Using, Understanding, and Unraveling the OCaml Language
¼ ¼
½ ¾ ¿ ¼
507
! ! " ! # ! $ $ ! # ! #
#! $ ! # $
! # # #! ! $ # #!
# $ #
% # & ' ( ) * + , - &. ' (&/ 0 '( ) * + , $ ! " # $ # $ %&
! # ' !' (' ) $ & ' & ' ( /
%& * 1
%$23 # 3 #
4 + ½ ¾ ½ + ¾ 5 $ #
" 6,7 3 # # $ 67 $ $ 6,7 $
508
Didier R´emy
!" ! !" # $ " " !
% &' ( " )*" + ,-,. / ! !
!
Using, Understanding, and Unraveling the OCaml Language
509
510
Didier R´emy
Variant Types and Labeled Arguments
!
! ! " ! !
# Æ $ ! %
B.1
Variant Types
& $ '( ) ! ! " ! ! * ! $ !
+ % ! , - !
. $ ! /
$%
* !
)
! !
) ! ! * ! ! !
!
#
)
! !
!
& !
Using, Understanding, and Unraveling the OCaml Language
511
! !
" !
#
$ %&
$ % ' ( ) &
!
$ % ( !
512
Didier R´emy
B.2
Labeled Arguments
! " # # $ $ %
& %
'
" ( ) * & &
&
#
% + & , '
Using, Understanding, and Unraveling the OCaml Language
B.3
513
Optional Arguments
! "# $ ! $ $ " % & ' $ ( $ $ % ) " ) * +
) ,%
% )
& % ' Ý
# )
514
Didier R´emy
Using, Understanding, and Unraveling the OCaml Language
515
Answers to Exercises
516
Didier R´emy
¼
! " ! # $ % $ $
$ $ ! & $ ! " ! $ ' $ ( ( ! " ! $ ( $ ) $
Using, Understanding, and Unraveling the OCaml Language
Æ
517
518
Didier R´emy
Using, Understanding, and Unraveling the OCaml Language
519
! !
!
520
Didier R´emy
Æ
Using, Understanding, and Unraveling the OCaml Language
½ ¾ ¿ ½ ½ ¾ ¿ ¼
¼
½
¾
¿
¼
¾ ¾ ¿ ½ ½ ¾ ¿ ¼
¼
¾
¿
¼
¿ ¿ ¾ ¾ ¿ ½ ½ ¾ ¿ ¼
¼
¼
¿
521
522
Didier R´emy
Æ
!
"
#
Using, Understanding, and Unraveling the OCaml Language
523
! " ! # #" " #" $ %& $ " ! ' # " (
$ )
*
! ½ ¾
½ ¾
524
Didier R´emy
½ ¾ ½ ¾
½ ¾ ½ ¾
! " # $ $$%
½ ¾ ½ ¾ ¾ ¾ ¾ ½ ¾ ½ ½ ½ ¾
Using, Understanding, and Unraveling the OCaml Language
525
! " # $
% # $ # !
# # !
! $ $
! &' ( &' ( &' &½ ¾' ( &&½' &¾' ) & ' $
526
Didier R´emy
Using, Understanding, and Unraveling the OCaml Language
527
! " # $ ! % # "
¼
¼
528
Didier R´emy
Using, Understanding, and Unraveling the OCaml Language
529
530
Didier R´emy
! "# $ % %& ' ( )*
+
, - . /0 1' 2 '% %'' 3 4 ' +5 '' 5 6 % / 7 &% %3 - %'* "' 6 %%' 8(5)*5 +559 '
8 6 % / 7 6 ' 3% % ' '' 4 :% ;& % ! " 6% ! %3 # ' 9!+ <= 9 >' $ " $ % ! & 6% %3 $ ;%&<>% 85 6 "% 2& ?' > %%% %' %#' 3% 4 ' 4; 4
8 @A%' "%% & B 2 &. &&<% % %& <&%' 4 () ' !+ , C " " :' ' & >%.' D#' :% (,%& /3 % & = 23%%6) : 2 6' "# $ D &%' $ *
()*!! +!5! 9
C " " D'. $& 1 ' '3 6 % 6 ' 4 + * * ,+**- "''' , 8
$ 1 D ' ' '% % %&' " (5)*5 +!! 8
! / &%- $' % "% $% '" " * " DE !
&' 2& * F% G % 2& % << H6' %3 1'&%
5 %I ,%J ' %- 2& ' %- :' C& ' 6 * < 4 . /0 $'' ' 89 '% ' ' 4; 4 '& % <! 89
: %' & /'
9 ' ' % $ <'&' 3% 3% %' 4 ' ! +! ! $'' 8!
& %' @A%' %- $ 1' /-'% % %&' 4 (( ,
Using, Understanding, and Unraveling the OCaml Language
531
!" # $ %% % & '( ) * + ( +, ( ( ,
, $ %% - . ) /) &) 0, +) ( ) ( 1,
2 " % - 2 "
"!2% #
, %%% # 3$4#5 . ) /) &) 0, ' 1, 6 ) ) 71 17
, ! " #$% & --- # / , / 8 9 +) &:: ) / ( 6 1 &; 3 $ 6 ) , ) '( 1 %% &; 3 0<, ' 4 6 ) 7 )) 7 , ) ) * & + ( -!2 1 %%" # 3 , * - / ( + $ %% +)< = # > *< ; :73 *< . ) ) / 0 < %% 01 > ? 4
1 # ,7 ) ) )) 7 6 ) ) ) & !" # $ %%2 / > ) *) &12 $) ) ( ) 56 @ ? , %% & 0 > , & ) $ $ & * ( 4 ) , 1 , $ % $ >? * 3( "4, 1 , $ --- 3< > 5/ /- +)= A< < $ " %" 3< > +) : 6 "BCD2%!2 %%" 4 # & ? &) )
4 6 )
) 1 , B , C %! 61 ) & , % ? $ & 7 2 *), *) 1 , $ 5 1 %%2 ? $ & $, & 4) 6) 1 ( )?
3( '( 1 77%%%7 +) %%% $, & &) >) 0 ) >?
% +)
%%% 3
.) 5 ) * !% %"
532
Didier R´emy
! ! " ! # $ % &'( ) ( * ( +( ,% '--. - /0# 1 * 2 % % # ! % ( '&34"'5''6'5.( %! '--5 5. 7 / % % # % '88 ( 9: 9,( '-- 5' 7 / ;< ( ) 8( '-- 5 7 / ,%% # # % 9 ( % '56' ,1 ( '-- 5 7 / , # % ( &34"&&86&- ( '--& 55 7 / , ( '.34"&-6.( ... 5 7 / 1 1 1/ ( 354"5'65&( '-- 5& = 1> 1 ;# , # # 9 ( ( ! "#( 8
# $ ( % 5.-65 %?( '--5 58 ,! 1 ) 1 , Æ $ ( 534" 6 ( '- 5 ! 1 1 ;# ; 19; ( '--' 5- ! 1( 1 ;#( ! @%( 1> % &'( ; 19; ( '--8 . 1 19; ( '--& ' 1 * 2 /0# % A2 9
)* ( % 56&8( '--& 1 * 2 ( 1 B( 1 C ; % # A % !( 3'4( '--- 1 * 2 ( % C( 1 C , 2 9
+ + ( % '6'5&( '-- 5 1 * 2 ( % D( 1 D % # 9 ( ..' , * , % % % ( '83&4" 556 -( '--& & /A , - ! ) ( '--' 8 =E = $ ! . ( ''3'4"''6'&( '--5 , , F 1( ( !/0 ! 1 2 2 3 319; ( '--54 2 /2 $, 4 =E : ; / % # 9 )5 ( '-- + ! 9 ) 9 ; % 5-
Using, Understanding, and Unraveling the OCaml Language
533
!
"#" $%&' ( ) '*' +, $ -
)( . ( / ( 0,,, +' !1( 23 #4 ( ( 5 6 ( ! ! '7++ " % ! ") 6 !6 8 ', 7* ' 4 ( 3 $ '0 +0 !1( .9 5 #4&! / 3 #4 5 . ( " # :( ; #
. 7* 4 % 0'<=+ ; '= &> + !1( ( ) ) 3 ) #4 " ; #
! " #" '= += !1( ;1 ? >
.9 #4/ @ .9 & 3 #4 =A'B/07<, '* ( ) 0= # ) ) 4 '7 + ; !( ! . ( '* ++ ; ! ( . ' +7 > ! ( ) 2. '* +* #$% &C ( ' + ;1 ? >
. . . ( / .9 5 5 " &
! # 0,,, 7, ;1 ? >
' ( ) &! * D 1 7 . 0,,, 7' #
C ( ) ) .9 " ! 0,7<07+ " %5 E ; '*7 "222 ( 70 C F 4 ( ! ' 7 ; 8 C
(. ( ( ( 6 & .
! *A'<B/'''<'+ ' 7= 5 G C #
$
( ( ''A'B/*<= '=
534
Didier R´emy
List of All Exercises
" % ( * ,
! # $
& ' $ ) ' $ + $ - ' . - '
+ ' / ) 0 $ 1 $ $ - ' + $ - ' ) - '
2 13 . ' 13 45 45 & $ 5 5 6 5 5 67 - 3
" % ( * ,
" % ( * ,
- '
Using, Understanding, and Unraveling the OCaml Language
#
!" $
535
536
Didier R´emy
! ! ! " # " $ $" % $
% %
&
#
'( $ $ #
$