The Ocaml System Release 4.08
Total Page:16
File Type:pdf, Size:1020Kb
The OCaml system release 4.08 Documentation and user's manual Xavier Leroy, Damien Doligez, Alain Frisch, Jacques Garrigue, Didier R´emy and J´er^ome Vouillon August 6, 2019 Copyright © 2019 Institut National de Recherche en Informatique et en Automatique 2 Contents I An introduction to OCaml 11 1 The core language 13 1.1 Basics........................................... 13 1.2 Data types......................................... 14 1.3 Functions as values.................................... 16 1.4 Records and variants................................... 16 1.5 Imperative features.................................... 21 1.6 Exceptions......................................... 23 1.7 Lazy expressions...................................... 25 1.8 Symbolic processing of expressions........................... 26 1.9 Pretty-printing...................................... 27 1.10 Printf formats....................................... 28 1.11 Standalone OCaml programs............................... 30 2 The module system 33 2.1 Structures......................................... 33 2.2 Signatures......................................... 36 2.3 Functors.......................................... 37 2.4 Functors and type abstraction.............................. 38 2.5 Modules and separate compilation............................ 41 3 Objects in OCaml 43 3.1 Classes and objects.................................... 43 3.2 Immediate objects..................................... 46 3.3 Reference to self...................................... 47 3.4 Initializers......................................... 48 3.5 Virtual methods...................................... 48 3.6 Private methods...................................... 50 3.7 Class interfaces...................................... 52 3.8 Inheritance......................................... 53 3.9 Multiple inheritance.................................... 53 3.10 Parameterized classes................................... 55 3.11 Polymorphic methods................................... 58 3.12 Using coercions...................................... 61 3.13 Functional objects..................................... 65 1 2 3.14 Cloning objects...................................... 66 3.15 Recursive classes..................................... 68 3.16 Binary methods...................................... 69 3.17 Friends........................................... 71 4 Labels and variants 73 4.1 Labels........................................... 73 4.2 Polymorphic variants................................... 79 5 Polymorphism and its limitations 83 5.1 Weak polymorphism and mutation........................... 83 5.2 Polymorphic recursion.................................. 88 5.3 Higher-rank polymorphic functions........................... 91 6 Advanced examples with classes and modules 93 6.1 Extended example: bank accounts............................ 93 6.2 Simple modules as classes................................ 99 6.3 The subject/observer pattern.............................. 104 II The OCaml language 109 7 The OCaml language 111 7.1 Lexical conventions.................................... 111 7.2 Values........................................... 116 7.3 Names........................................... 118 7.4 Type expressions..................................... 121 7.5 Constants......................................... 124 7.6 Patterns.......................................... 125 7.7 Expressions........................................ 130 7.8 Type and exception definitions.............................. 144 7.9 Classes........................................... 147 7.10 Module types (module specifications).......................... 153 7.11 Module expressions (module implementations)..................... 158 7.12 Compilation units..................................... 161 8 Language extensions 163 8.1 Recursive definitions of values.............................. 163 8.2 Recursive modules..................................... 164 8.3 Private types........................................ 166 8.4 Local opens for patterns................................. 168 8.5 Locally abstract types.................................. 169 8.6 First-class modules.................................... 170 8.7 Recovering the type of a module............................. 172 8.8 Substituting inside a signature.............................. 173 8.9 Type-level module aliases................................. 175 3 8.10 Overriding in open statements.............................. 176 8.11 Generalized algebraic datatypes............................. 177 8.12 Syntax for Bigarray access................................ 182 8.13 Attributes......................................... 183 8.14 Extension nodes...................................... 189 8.15 Extensible variant types................................. 191 8.16 Generative functors.................................... 193 8.17 Extension-only syntax.................................. 194 8.18 Inline records....................................... 195 8.19 Documentation comments................................ 196 8.20 Extended indexing operators.............................. 199 8.21 Empty variant types................................... 199 8.22 Alerts........................................... 200 8.23 Generalized open statements............................... 201 8.24 Binding operators.................................... 203 III The OCaml tools 207 9 Batch compilation (ocamlc) 209 9.1 Overview of the compiler................................. 209 9.2 Options........................................... 210 9.3 Modules and the file system............................... 222 9.4 Common errors...................................... 223 9.5 Warning reference..................................... 225 10 The toplevel system or REPL (ocaml) 229 10.1 Options........................................... 230 10.2 Toplevel directives..................................... 237 10.3 The toplevel and the module system.......................... 240 10.4 Common errors...................................... 240 10.5 Building custom toplevel systems: ocamlmktop ..................... 240 10.6 The native toplevel: ocamlnat (experimental)..................... 241 11 The runtime system (ocamlrun) 243 11.1 Overview.......................................... 243 11.2 Options........................................... 244 11.3 Dynamic loading of shared libraries........................... 246 11.4 Common errors...................................... 247 12 Native-code compilation (ocamlopt) 249 12.1 Overview of the compiler................................. 249 12.2 Options........................................... 250 12.3 Common errors...................................... 263 12.4 Running executables produced by ocamlopt...................... 263 12.5 Compatibility with the bytecode compiler....................... 263 4 13 Lexer and parser generators (ocamllex, ocamlyacc) 265 13.1 Overview of ocamllex .................................. 265 13.2 Syntax of lexer definitions................................ 266 13.3 Overview of ocamlyacc .................................. 271 13.4 Syntax of grammar definitions.............................. 271 13.5 Options........................................... 274 13.6 A complete example.................................... 275 13.7 Common errors...................................... 276 14 Dependency generator (ocamldep) 279 14.1 Options........................................... 279 14.2 A typical Makefile..................................... 281 15 The browser/editor (ocamlbrowser) 285 16 The documentation generator (ocamldoc) 287 16.1 Usage............................................ 287 16.2 Syntax of documentation comments........................... 294 16.3 Custom generators.................................... 303 16.4 Adding command line options.............................. 306 17 The debugger (ocamldebug) 307 17.1 Compiling for debugging................................. 307 17.2 Invocation......................................... 307 17.3 Commands......................................... 308 17.4 Executing a program................................... 309 17.5 Breakpoints........................................ 312 17.6 The call stack....................................... 312 17.7 Examining variable values................................ 313 17.8 Controlling the debugger................................. 314 17.9 Miscellaneous commands................................. 317 17.10 Running the debugger under Emacs........................... 317 18 Profiling (ocamlprof) 319 18.1 Compiling for profiling.................................. 319 18.2 Profiling an execution................................... 320 18.3 Printing profiling information.............................. 320 18.4 Time profiling....................................... 321 19 The ocamlbuild compilation manager 323 20 Interfacing C with OCaml 325 20.1 Overview and compilation information......................... 325 20.2 The value type...................................... 332 20.3 Representation of OCaml data types.......................... 333 20.4 Operations on values................................... 336 5 20.5 Living in harmony with the garbage collector..................... 339 20.6 A complete example.................................... 343 20.7 Advanced topic: callbacks from C to OCaml...................... 347 20.8 Advanced example with callbacks............................ 352 20.9 Advanced topic: custom blocks............................