The Ocaml System Release 4.02
Total Page:16
File Type:pdf, Size:1020Kb
The OCaml system release 4.02 Documentation and user's manual Xavier Leroy, Damien Doligez, Alain Frisch, Jacques Garrigue, Didier R´emy and J´er^omeVouillon August 29, 2014 Copyright © 2014 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 and parsing . 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 . 107 6.8 Type and exception definitions . 119 6.9 Classes . 121 6.10 Module types (module specifications) . 128 6.11 Module expressions (module implementations) . 132 6.12 Compilation units . 135 7 Language extensions 137 7.1 Integer literals for types int32, int64 and nativeint . 137 7.2 Streams and stream parsers . 137 7.3 Recursive definitions of values . 138 7.4 Range patterns . 139 7.5 Assertion checking . 139 7.6 Lazy evaluation . 139 7.7 Local modules . 140 7.8 Recursive modules . 141 7.9 Private types . 142 7.10 Local opens . 144 7.11 Record notations . 145 7.12 Explicit polymorphic type annotations . 146 7.13 Locally abstract types . 146 7.14 First-class modules . 147 7.15 Recovering the type of a module . 149 7.16 Substituting inside a signature . 150 3 7.17 Type-level module aliases . 151 7.18 Explicit overriding in class definitions . 153 7.19 Overriding in open statements . 153 7.20 Generalized algebraic datatypes . 154 7.21 Syntax for Bigarray access . 156 7.22 Attributes . 157 7.23 Extension nodes . 161 7.24 Quoted strings . 163 7.25 Exception cases in pattern matching . 163 7.26 Extensible variant types . 164 7.27 Generative functors . 165 III The OCaml tools 167 8 Batch compilation (ocamlc) 169 8.1 Overview of the compiler . 169 8.2 Options . 170 8.3 Modules and the file system . 179 8.4 Common errors . 180 9 The toplevel system (ocaml) 183 9.1 Options . 184 9.2 Toplevel directives . 187 9.3 The toplevel and the module system . 189 9.4 Common errors . 190 9.5 Building custom toplevel systems: ocamlmktop . 190 9.6 Options . 191 10 The runtime system (ocamlrun) 193 10.1 Overview . 193 10.2 Options . 194 10.3 Dynamic.