The Ocaml System Release 4.04
Total Page:16
File Type:pdf, Size:1020Kb
The OCaml system release 4.04 Documentation and user's manual Xavier Leroy, Damien Doligez, Alain Frisch, Jacques Garrigue, Didier R´emy and J´er^omeVouillon November 4, 2016 Copyright © 2016 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 . 15 1.4 Records and variants . 16 1.5 Imperative features . 18 1.6 Exceptions . 20 1.7 Symbolic processing of expressions . 21 1.8 Pretty-printing . 22 1.9 Standalone OCaml programs . 23 2 The module system 25 2.1 Structures . 25 2.2 Signatures . 26 2.3 Functors . 27 2.4 Functors and type abstraction . 29 2.5 Modules and separate compilation . 31 3 Objects in OCaml 33 3.1 Classes and objects . 33 3.2 Immediate objects . 36 3.3 Reference to self . 37 3.4 Initializers . 38 3.5 Virtual methods . 38 3.6 Private methods . 40 3.7 Class interfaces . 42 3.8 Inheritance . 43 3.9 Multiple inheritance . 44 3.10 Parameterized classes . 44 3.11 Polymorphic methods . 47 3.12 Using coercions . 50 3.13 Functional objects . 54 3.14 Cloning objects . 55 3.15 Recursive classes . 58 1 2 3.16 Binary methods . 58 3.17 Friends . 60 4 Labels and variants 63 4.1 Labels . 63 4.2 Polymorphic variants . 69 5 Advanced examples with classes and modules 73 5.1 Extended example: bank accounts . 73 5.2 Simple modules as classes . 79 5.3 The subject/observer pattern . 84 II The OCaml language 89 6 The OCaml language 91 6.1 Lexical conventions . 91 6.2 Values . 95 6.3 Names . 97 6.4 Type expressions . 100 6.5 Constants . 103 6.6 Patterns . 104 6.7 Expressions . 108 6.8 Type and exception definitions . 121 6.9 Classes . 123 6.10 Module types (module specifications) . 130 6.11 Module expressions (module implementations) . 134 6.12 Compilation units . 137 7 Language extensions 139 7.1 Integer literals for types int32, int64 and nativeint . 139 7.2 Recursive definitions of values . 139 7.3 Lazy patterns . 141 7.4 Recursive modules . 141 7.5 Private types . 142 7.6 Local opens . 145 7.7 Record and object notations . 146 7.8 Explicit polymorphic type annotations . 147 7.9 Locally abstract types . 147 7.10 First-class modules . 149 7.11 Recovering the type of a module . 151 7.12 Substituting inside a signature . 151 7.13 Type-level module aliases . 152 7.14 Explicit overriding in class definitions . 154 7.15 Overriding in open statements . 154 7.16 Generalized algebraic datatypes . 155 3 7.17 Syntax for Bigarray access . 160 7.18 Attributes . 160 7.19 Extension nodes . 167 7.20 Quoted strings . 169 7.21 Exception cases in pattern matching . 169 7.22 Extensible variant types . 169 7.23 Generative functors . 171 7.24 Extension-only syntax . 171 7.25 Inline records . 172 7.26 Local exceptions . 173 7.27 Documentation comments . 173 III The OCaml tools 177 8 Batch compilation (ocamlc) 179 8.1 Overview of the compiler . 179 8.2 Options . 180 8.3 Modules and the file system . 191 8.4 Common errors . 191 8.5 Warning reference . 194 9 The toplevel system (ocaml) 197 9.1 Options . 198 9.2 Toplevel directives . 201 9.3 The toplevel and the module system . 204 9.4 Common errors . 204 9.5 Building custom toplevel systems: ocamlmktop . 205 9.6 Options . 205 10 The runtime system (ocamlrun) 207 10.1 Overview . 207 10.2 Options . ..