Contents
Part I An introduction to OCaml
Chapter 1 The core language
1.1 Basics
1.2 Data types
1.3 Functions as values
1.4 Records and variants
1.4.1 Record and variant disambiguation
1.5 Imperative features
1.6 Exceptions
1.7 Lazy expressions
1.8 Symbolic processing of expressions
1.9 Pretty-printing
1.10 Printf formats
1.11 Standalone OCaml programs
Chapter 2 The module system
2.1 Structures
2.2 Signatures
2.3 Functors
2.4 Functors and type abstraction
2.5 Modules and separate compilation
Chapter 3 Objects in OCaml
3.1 Classes and objects
3.2 Immediate objects
3.3 Reference to self
3.4 Initializers
3.5 Virtual methods
3.6 Private methods
3.7 Class interfaces
3.8 Inheritance
3.9 Multiple inheritance
3.10 Parameterized classes
3.11 Polymorphic methods
3.12 Using coercions
3.13 Functional objects
3.14 Cloning objects
3.15 Recursive classes
3.16 Binary methods
3.17 Friends
Chapter 4 Labeled arguments
4.1 Optional arguments
4.2 Labels and type inference
4.3 Suggestions for labeling
Chapter 5 Polymorphic variants
5.1 Basic use
5.2 Advanced use
5.3 Weaknesses of polymorphic variants
Chapter 6 Polymorphism and its limitations
6.1 Weak polymorphism and mutation
6.1.1 Weakly polymorphic types
6.1.2 The value restriction
6.1.3 The relaxed value restriction
6.1.4 Variance and value restriction
6.1.5 Abstract data types
6.2 Polymorphic recursion
6.2.1 Explicitly polymorphic annotations
6.2.2 More examples
6.3 Higher-rank polymorphic functions
Chapter 7 Generalized algebraic datatypes
7.1 Recursive functions
7.2 Type inference
7.3 Refutation cases
7.4 Advanced examples
7.5 Existential type names in error messages
7.6 Explicit naming of existentials
7.7 Equations on non-local abstract types
Chapter 8 Advanced examples with classes and modules
8.1 Extended example: bank accounts
8.2 Simple modules as classes
8.2.1 Strings
8.2.2 Hashtbl
8.2.3 Sets
8.3 The subject/observer pattern
Part II The OCaml language
Chapter 9 The OCaml language
9.1 Lexical conventions
9.2 Values
9.2.1 Base values
9.2.2 Tuples
9.2.3 Records
9.2.4 Arrays
9.2.5 Variant values
9.2.6 Polymorphic variants
9.2.7 Functions
9.2.8 Objects
9.3 Names
9.4 Type expressions
9.5 Constants
9.6 Patterns
9.7 Expressions
9.7.1 Precedence and associativity
9.7.2 Basic expressions
9.7.3 Control structures
9.7.4 Operations on data structures
9.7.5 Operators
9.7.6 Objects
9.7.7 Coercions
9.7.8 Other
9.8 Type and exception definitions
9.8.1 Type definitions
9.8.2 Exception definitions
9.9 Classes
9.9.1 Class types
9.9.2 Class expressions
9.9.3 Class definitions
9.9.4 Class specifications
9.9.5 Class type definitions
9.10 Module types (module specifications)
9.10.1 Simple module types
9.10.2 Signatures
9.10.3 Functor types
9.10.4 The
with
operator
9.11 Module expressions (module implementations)
9.11.1 Simple module expressions
9.11.2 Structures
9.11.3 Functors
9.12 Compilation units
Chapter 10 Language extensions
10.1 Recursive definitions of values
10.2 Recursive modules
10.3 Private types
10.3.1 Private variant and record types
10.3.2 Private type abbreviations
10.3.3 Private row types
10.4 Locally abstract types
10.5 First-class modules
10.6 Recovering the type of a module
10.7 Substituting inside a signature
10.7.1 Destructive substitutions
10.7.2 Local substitution declarations
10.7.3 Module type substitutions
10.8 Type-level module aliases
10.9 Overriding in open statements
10.10 Generalized algebraic datatypes
10.11 Syntax for Bigarray access
10.12 Attributes
10.12.1 Built-in attributes
10.13 Extension nodes
10.13.1 Built-in extension nodes
10.14 Extensible variant types
10.14.1 Private extensible variant types
10.15 Generative functors
10.16 Extension-only syntax
10.16.1 Extension operators
10.16.2 Extension literals
10.17 Inline records
10.18 Documentation comments
10.18.1 Floating comments
10.18.2 Item comments
10.18.3 Label comments
10.19 Extended indexing operators
10.19.1 Multi-index notation
10.20 Empty variant types
10.21 Alerts
10.22 Generalized open statements
10.23 Binding operators
10.23.1 Examples
10.23.2 Conventions
10.23.3 General desugaring rules
10.23.4 Short notation for variable bindings (let-punning)
10.24 Effect handlers
10.24.1 Basics
10.24.2 Concurrency
10.24.3 User-level threads
10.24.4 Control inversion
10.24.5 Semantics
10.24.6 Shallow handlers
Part III The OCaml tools
Chapter 11 Batch compilation (ocamlc)
11.1 Overview of the compiler
11.2 Options
11.3 Modules and the file system
11.4 Common errors
11.5 Warning reference
11.5.1 Warning 6: Label omitted in function application
11.5.2 Warning 9: missing fields in a record pattern
11.5.3 Warning 52: fragile constant pattern
11.5.4 Warning 57: Ambiguous or-pattern variables under guard
Chapter 12 The toplevel system or REPL (ocaml)
12.1 Options
12.2 Toplevel directives
12.3 The toplevel and the module system
12.4 Common errors
12.5 Building custom toplevel systems:
ocamlmktop
12.5.1 Options
12.6 The native toplevel:
ocamlnat
(experimental)
Chapter 13 The runtime system (ocamlrun)
13.1 Overview
13.2 Options
13.3 Dynamic loading of shared libraries
13.4 Common errors
Chapter 14 Native-code compilation (ocamlopt)
14.1 Overview of the compiler
14.2 Options
14.3 Common errors
14.4 Running executables produced by ocamlopt
14.5 Compatibility with the bytecode compiler
Chapter 15 Lexer and parser generators (ocamllex, ocamlyacc)
15.1 Overview of
ocamllex
15.1.1 Options
15.2 Syntax of lexer definitions
15.2.1 Header and trailer
15.2.2 Naming regular expressions
15.2.3 Entry points
15.2.4 Regular expressions
15.2.5 Actions
15.2.6 Variables in regular expressions
15.2.7 Refill handlers
15.2.8 Reserved identifiers
15.3 Overview of
ocamlyacc
15.4 Syntax of grammar definitions
15.4.1 Header and trailer
15.4.2 Declarations
15.4.3 Rules
15.4.4 Error handling
15.5 Options
15.6 A complete example
15.7 Common errors
Chapter 16 Dependency generator (ocamldep)
16.1 Options
16.2 A typical Makefile
Chapter 17 The documentation generator (ocamldoc)
17.1 Usage
17.1.1 Invocation
17.1.2 Merging of module information
17.1.3 Coding rules
17.2 Syntax of documentation comments
17.2.1 Placement of documentation comments
17.2.2 The Stop special comment
17.2.3 Syntax of documentation comments
17.2.4 Text formatting
17.2.5 Documentation tags (@-tags)
17.3 Custom generators
17.3.1 The generator modules
17.3.2 Handling custom tags
17.4 Adding command line options
17.4.1 Compilation and usage
Chapter 18 The debugger (ocamldebug)
18.1 Compiling for debugging
18.2 Invocation
18.2.1 Starting the debugger
18.2.2 Initialization file
18.2.3 Exiting the debugger
18.3 Commands
18.3.1 Getting help
18.3.2 Accessing the debugger state
18.4 Executing a program
18.4.1 Events
18.4.2 Starting the debugged program
18.4.3 Running the program
18.4.4 Time travel
18.4.5 Killing the program
18.5 Breakpoints
18.6 The call stack
18.7 Examining variable values
18.8 Controlling the debugger
18.8.1 Setting the program name and arguments
18.8.2 How programs are loaded
18.8.3 Search path for files
18.8.4 Working directory
18.8.5 Turning reverse execution on and off
18.8.6 Behavior of the debugger with respect to
fork
18.8.7 Stopping execution when new code is loaded
18.8.8 Communication between the debugger and the program
18.8.9 Fine-tuning the debugger
18.8.10 User-defined printers
18.9 Miscellaneous commands
18.10 Running the debugger under Emacs
Chapter 19 Profiling (ocamlprof)
19.1 Compiling for profiling
19.2 Profiling an execution
19.3 Printing profiling information
19.4 Time profiling
Chapter 20 Interfacing C with OCaml
20.1 Overview and compilation information
20.1.1 Declaring primitives
20.1.2 Implementing primitives
20.1.3 Statically linking C code with OCaml code
20.1.4 Dynamically linking C code with OCaml code
20.1.5 Choosing between static linking and dynamic linking
20.1.6 Building standalone custom runtime systems
20.2 The
value
type
20.2.1 Integer values
20.2.2 Blocks
20.2.3 Pointers outside the heap
20.3 Representation of OCaml data types
20.3.1 Atomic types
20.3.2 Tuples and records
20.3.3 Arrays
20.3.4 Concrete data types
20.3.5 Objects
20.3.6 Polymorphic variants
20.4 Operations on values
20.4.1 Kind tests
20.4.2 Operations on integers
20.4.3 Accessing blocks
20.4.4 Allocating blocks
20.4.5 Raising exceptions
20.5 Living in harmony with the garbage collector
20.5.1 Simple interface
20.5.2 Low-level interface
20.5.3 Pending actions and asynchronous exceptions
20.6 A complete example
20.7 Advanced topic: callbacks from C to OCaml
20.7.1 Applying OCaml closures from C
20.7.2 Obtaining or registering OCaml closures for use in C functions
20.7.3 Registering OCaml exceptions for use in C functions
20.7.4 Main program in C
20.7.5 Embedding the OCaml code in the C code
20.8 Advanced example with callbacks
20.9 Advanced topic: custom blocks
20.9.1 The
struct custom_operations
20.9.2 Allocating custom blocks
20.9.3 Accessing custom blocks
20.9.4 Writing custom serialization and deserialization functions
20.9.5 Choosing identifiers
20.9.6 Finalized blocks
20.10 Advanced topic: Bigarrays and the OCaml-C interface
20.10.1 Include file
20.10.2 Accessing an OCaml bigarray from C or Fortran
20.10.3 Wrapping a C or Fortran array as an OCaml Bigarray
20.11 Advanced topic: cheaper C call
20.11.1 Passing unboxed values
20.11.2 Direct C call
20.11.3 Example: calling C library functions without indirection
20.12 Advanced topic: multithreading
20.12.1 Registering threads created from C
20.12.2 Parallel execution of long-running C code
20.13 Advanced topic: interfacing with Windows Unicode APIs
20.14 Building mixed C/OCaml libraries:
ocamlmklib
20.15 Cautionary words: the internal runtime API
20.15.1 Internal variables and CAML_INTERNALS
20.15.2 OCaml version macros
Chapter 21 Optimisation with Flambda
21.1 Overview
21.2 Command-line flags
21.2.1 Specification of optimisation parameters by round
21.3 Inlining
21.3.1 Classic inlining heuristic
21.3.2 Overview of “Flambda” inlining heuristics
21.3.3 Handling of specific language constructs
21.3.4 Inlining reports
21.3.5 Assessment of inlining benefit
21.3.6 Control of speculation
21.4 Specialisation
21.4.1 Assessment of specialisation benefit
21.5 Default settings of parameters
21.5.1 Settings at -O2 optimisation level
21.5.2 Settings at -O3 optimisation level
21.6 Manual control of inlining and specialisation
21.7 Simplification
21.8 Other code motion transformations
21.8.1 Lifting of constants
21.8.2 Lifting of toplevel let bindings
21.9 Unboxing transformations
21.9.1 Unboxing of closure variables
21.9.2 Unboxing of specialised arguments
21.9.3 Unboxing of closures
21.10 Removal of unused code and values
21.10.1 Removal of redundant let expressions
21.10.2 Removal of redundant program constructs
21.10.3 Removal of unused arguments
21.10.4 Removal of unused closure variables
21.11 Other code transformations
21.11.1 Transformation of non-escaping references into mutable variables
21.11.2 Substitution of closure variables for specialised arguments
21.12 Treatment of effects
21.13 Compilation of statically-allocated modules
21.14 Inhibition of optimisation
21.15 Use of unsafe operations
21.16 Glossary
Chapter 22 Fuzzing with afl-fuzz
22.1 Overview
22.2 Generating instrumentation
22.2.1 Advanced options
22.3 Example
Chapter 23 Runtime tracing with the instrumented runtime
23.1 Overview
23.2 Enabling runtime instrumentation
23.3 Reading traces
23.3.1 eventlog-tools
23.3.2 babeltrace
23.4 Controlling instrumentation and limitations
23.4.1 Trace filename
23.4.2 Pausing and resuming tracing
23.4.3 Limitations
Chapter 24 The “Tail Modulo Constructor” program transformation
24.1 Disambiguation
24.2 Danger: getting out of tail-mod-cons
24.3 Details on the transformation
24.4 Current limitations
Part IV The OCaml library
Chapter 25 The core library
25.1 Built-in types and predefined exceptions
25.2 Module
Stdlib
: the initially opened module
Chapter 26 The standard library
Chapter 27 The compiler front-end
Chapter 28 The unix library: Unix system calls
Chapter 29 The str library: regular expressions and string processing
Chapter 30 The threads library
Chapter 31 The dynlink library: dynamic loading and linking of object files
Chapter 32 Recently removed or moved libraries (Graphics, Bigarray, Num, LablTk)
32.1 The Graphics Library
32.2 The Bigarray Library
32.3 The Num Library
32.4 The Labltk Library and OCamlBrowser
Part V Indexes