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 Labels and variants
4.1 Labels
4.1.1 Optional arguments
4.1.2 Labels and type inference
4.1.3 Suggestions for labeling
4.2 Polymorphic variants
4.2.1 Weaknesses of polymorphic variants
Chapter 5 Polymorphism and its limitations
5.1 Weak polymorphism and mutation
5.1.1 Weakly polymorphic types
5.1.2 The value restriction
5.1.3 The relaxed value restriction
5.1.4 Variance and value restriction
5.1.5 Abstract data types
5.2 Polymorphic recursion
5.2.1 Explicitly polymorphic annotations
5.2.2 More examples
5.3 Higher-rank polymorphic functions
Chapter 6 Advanced examples with classes and modules
6.1 Extended example: bank accounts
6.2 Simple modules as classes
6.2.1 Strings
6.2.2 Hashtbl
6.2.3 Sets
6.3 The subject/observer pattern
Part II The OCaml language
Chapter 7 The OCaml language
7.1 Lexical conventions
7.2 Values
7.2.1 Base values
7.2.2 Tuples
7.2.3 Records
7.2.4 Arrays
7.2.5 Variant values
7.2.6 Polymorphic variants
7.2.7 Functions
7.2.8 Objects
7.3 Names
7.4 Type expressions
7.5 Constants
7.6 Patterns
7.7 Expressions
7.7.1 Precedence and associativity
7.7.2 Basic expressions
7.7.3 Control structures
7.7.4 Operations on data structures
7.7.5 Operators
7.7.6 Objects
7.7.7 Coercions
7.7.8 Other
7.8 Type and exception definitions
7.8.1 Type definitions
7.8.2 Exception definitions
7.9 Classes
7.9.1 Class types
7.9.2 Class expressions
7.9.3 Class definitions
7.9.4 Class specifications
7.9.5 Class type definitions
7.10 Module types (module specifications)
7.10.1 Simple module types
7.10.2 Signatures
7.10.3 Functor types
7.10.4 The
with
operator
7.11 Module expressions (module implementations)
7.11.1 Simple module expressions
7.11.2 Structures
7.11.3 Functors
7.12 Compilation units
Chapter 8 Language extensions
8.1 Recursive definitions of values
8.2 Recursive modules
8.3 Private types
8.3.1 Private variant and record types
8.3.2 Private type abbreviations
8.3.3 Private row types
8.4 Locally abstract types
8.5 First-class modules
8.6 Recovering the type of a module
8.7 Substituting inside a signature
8.7.1 Destructive substitutions
8.7.2 Local substitution declarations
8.8 Type-level module aliases
8.9 Overriding in open statements
8.10 Generalized algebraic datatypes
8.11 Syntax for Bigarray access
8.12 Attributes
8.12.1 Built-in attributes
8.13 Extension nodes
8.13.1 Built-in extension nodes
8.14 Extensible variant types
8.14.1 Private extensible variant types
8.15 Generative functors
8.16 Extension-only syntax
8.16.1 Extension operators
8.16.2 Extension literals
8.17 Inline records
8.18 Documentation comments
8.18.1 Floating comments
8.18.2 Item comments
8.18.3 Label comments
8.19 Extended indexing operators
8.19.1 Multi-index notation
8.20 Empty variant types
8.21 Alerts
8.22 Generalized open statements
8.23 Binding operators
8.23.1 Rationale
Part III The OCaml tools
Chapter 9 Batch compilation (ocamlc)
9.1 Overview of the compiler
9.2 Options
9.3 Modules and the file system
9.4 Common errors
9.5 Warning reference
9.5.1 Warning 9: missing fields in a record pattern
9.5.2 Warning 52: fragile constant pattern
9.5.3 Warning 57: Ambiguous or-pattern variables under guard
Chapter 10 The toplevel system or REPL (ocaml)
10.1 Options
10.2 Toplevel directives
10.3 The toplevel and the module system
10.4 Common errors
10.5 Building custom toplevel systems:
ocamlmktop
10.5.1 Options
10.6 The native toplevel:
ocamlnat
(experimental)
Chapter 11 The runtime system (ocamlrun)
11.1 Overview
11.2 Options
11.3 Dynamic loading of shared libraries
11.4 Common errors
Chapter 12 Native-code compilation (ocamlopt)
12.1 Overview of the compiler
12.2 Options
12.3 Common errors
12.4 Running executables produced by ocamlopt
12.5 Compatibility with the bytecode compiler
Chapter 13 Lexer and parser generators (ocamllex, ocamlyacc)
13.1 Overview of
ocamllex
13.1.1 Options
13.2 Syntax of lexer definitions
13.2.1 Header and trailer
13.2.2 Naming regular expressions
13.2.3 Entry points
13.2.4 Regular expressions
13.2.5 Actions
13.2.6 Variables in regular expressions
13.2.7 Refill handlers
13.2.8 Reserved identifiers
13.3 Overview of
ocamlyacc
13.4 Syntax of grammar definitions
13.4.1 Header and trailer
13.4.2 Declarations
13.4.3 Rules
13.4.4 Error handling
13.5 Options
13.6 A complete example
13.7 Common errors
Chapter 14 Dependency generator (ocamldep)
14.1 Options
14.2 A typical Makefile
Chapter 15 The documentation generator (ocamldoc)
15.1 Usage
15.1.1 Invocation
15.1.2 Merging of module information
15.1.3 Coding rules
15.2 Syntax of documentation comments
15.2.1 Placement of documentation comments
15.2.2 The Stop special comment
15.2.3 Syntax of documentation comments
15.2.4 Text formatting
15.2.5 Documentation tags (@-tags)
15.3 Custom generators
15.3.1 The generator modules
15.3.2 Handling custom tags
15.4 Adding command line options
15.4.1 Compilation and usage
Chapter 16 The debugger (ocamldebug)
16.1 Compiling for debugging
16.2 Invocation
16.2.1 Starting the debugger
16.2.2 Initialization file
16.2.3 Exiting the debugger
16.3 Commands
16.3.1 Getting help
16.3.2 Accessing the debugger state
16.4 Executing a program
16.4.1 Events
16.4.2 Starting the debugged program
16.4.3 Running the program
16.4.4 Time travel
16.4.5 Killing the program
16.5 Breakpoints
16.6 The call stack
16.7 Examining variable values
16.8 Controlling the debugger
16.8.1 Setting the program name and arguments
16.8.2 How programs are loaded
16.8.3 Search path for files
16.8.4 Working directory
16.8.5 Turning reverse execution on and off
16.8.6 Behavior of the debugger with respect to
fork
16.8.7 Stopping execution when new code is loaded
16.8.8 Communication between the debugger and the program
16.8.9 Fine-tuning the debugger
16.8.10 User-defined printers
16.9 Miscellaneous commands
16.10 Running the debugger under Emacs
Chapter 17 Profiling (ocamlprof)
17.1 Compiling for profiling
17.2 Profiling an execution
17.3 Printing profiling information
17.4 Time profiling
Chapter 18 Interfacing C with OCaml
18.1 Overview and compilation information
18.1.1 Declaring primitives
18.1.2 Implementing primitives
18.1.3 Statically linking C code with OCaml code
18.1.4 Dynamically linking C code with OCaml code
18.1.5 Choosing between static linking and dynamic linking
18.1.6 Building standalone custom runtime systems
18.2 The
value
type
18.2.1 Integer values
18.2.2 Blocks
18.2.3 Pointers outside the heap
18.3 Representation of OCaml data types
18.3.1 Atomic types
18.3.2 Tuples and records
18.3.3 Arrays
18.3.4 Concrete data types
18.3.5 Objects
18.3.6 Polymorphic variants
18.4 Operations on values
18.4.1 Kind tests
18.4.2 Operations on integers
18.4.3 Accessing blocks
18.4.4 Allocating blocks
18.4.5 Raising exceptions
18.5 Living in harmony with the garbage collector
18.5.1 Simple interface
18.5.2 Low-level interface
18.5.3 Pending actions and asynchronous exceptions
18.6 A complete example
18.7 Advanced topic: callbacks from C to OCaml
18.7.1 Applying OCaml closures from C
18.7.2 Obtaining or registering OCaml closures for use in C functions
18.7.3 Registering OCaml exceptions for use in C functions
18.7.4 Main program in C
18.7.5 Embedding the OCaml code in the C code
18.8 Advanced example with callbacks
18.9 Advanced topic: custom blocks
18.9.1 The
struct custom_operations
18.9.2 Allocating custom blocks
18.9.3 Accessing custom blocks
18.9.4 Writing custom serialization and deserialization functions
18.9.5 Choosing identifiers
18.9.6 Finalized blocks
18.10 Advanced topic: Bigarrays and the OCaml-C interface
18.10.1 Include file
18.10.2 Accessing an OCaml bigarray from C or Fortran
18.10.3 Wrapping a C or Fortran array as an OCaml Bigarray
18.11 Advanced topic: cheaper C call
18.11.1 Passing unboxed values
18.11.2 Direct C call
18.11.3 Example: calling C library functions without indirection
18.12 Advanced topic: multithreading
18.12.1 Registering threads created from C
18.12.2 Parallel execution of long-running C code
18.13 Advanced topic: interfacing with Windows Unicode APIs
18.14 Building mixed C/OCaml libraries:
ocamlmklib
18.15 Cautionary words: the internal runtime API
18.15.1 Internal variables and CAML_INTERNALS
18.15.2 OCaml version macros
Chapter 19 Optimisation with Flambda
19.1 Overview
19.2 Command-line flags
19.2.1 Specification of optimisation parameters by round
19.3 Inlining
19.3.1 Classic inlining heuristic
19.3.2 Overview of “Flambda” inlining heuristics
19.3.3 Handling of specific language constructs
19.3.4 Inlining reports
19.3.5 Assessment of inlining benefit
19.3.6 Control of speculation
19.4 Specialisation
19.4.1 Assessment of specialisation benefit
19.5 Default settings of parameters
19.5.1 Settings at -O2 optimisation level
19.5.2 Settings at -O3 optimisation level
19.6 Manual control of inlining and specialisation
19.7 Simplification
19.8 Other code motion transformations
19.8.1 Lifting of constants
19.8.2 Lifting of toplevel let bindings
19.9 Unboxing transformations
19.9.1 Unboxing of closure variables
19.9.2 Unboxing of specialised arguments
19.9.3 Unboxing of closures
19.10 Removal of unused code and values
19.10.1 Removal of redundant let expressions
19.10.2 Removal of redundant program constructs
19.10.3 Removal of unused arguments
19.10.4 Removal of unused closure variables
19.11 Other code transformations
19.11.1 Transformation of non-escaping references into mutable variables
19.11.2 Substitution of closure variables for specialised arguments
19.12 Treatment of effects
19.13 Compilation of statically-allocated modules
19.14 Inhibition of optimisation
19.15 Use of unsafe operations
19.16 Glossary
Chapter 20 Fuzzing with afl-fuzz
20.1 Overview
20.2 Generating instrumentation
20.2.1 Advanced options
20.3 Example
Chapter 21 Runtime tracing with the instrumented runtime
21.1 Overview
21.2 Enabling runtime instrumentation
21.3 Reading traces
21.3.1 eventlog-tools
21.3.2 babeltrace
21.4 Controlling instrumentation and limitations
21.4.1 Trace filename
21.4.2 Pausing and resuming tracing
21.4.3 Limitations
Part IV The OCaml library
Chapter 22 The core library
22.1 Built-in types and predefined exceptions
22.2 Module
Stdlib
: the initially opened module
Chapter 23 The standard library
Chapter 24 The compiler front-end
Chapter 25 The unix library: Unix system calls
Chapter 26 The str library: regular expressions and string processing
Chapter 27 The threads library
Chapter 28 The dynlink library: dynamic loading and linking of object files
Chapter 29 Recently removed or moved libraries (Graphics, Bigarray, Num, LablTk)
29.1 The Graphics Library
29.2 The Bigarray Library
29.3 The Num Library
29.4 The Labltk Library and OCamlBrowser
Part V Indexes