The OCaml system release 4.05 Documentation and user's manual Xavier Leroy, Damien Doligez, Alain Frisch, Jacques Garrigue, Didier Rémy and Jérôme Vouillon July 13, 2017 Copyright © 2013 Institut National de Recherche en Informatique et en Automatique --------------------------------------------------------------------------- This manual is also available in PDF (1). Postscript (2), DVI (3), plain text (4), as a bundle of HTML files (5), and as a bundle of Emacs Info files (6). --------------------------------------------------------------------------- --------------------------------------- (1) http://caml.inria.fr/distrib/ocaml-4.05/ocaml-4.05-refman.pdf (2) http://caml.inria.fr/distrib/ocaml-4.05/ocaml-4.05-refman.ps.gz (3) http://caml.inria.fr/distrib/ocaml-4.05/ocaml-4.05-refman.dvi.gz (4) http://caml.inria.fr/distrib/ocaml-4.05/ocaml-4.05-refman.txt (5) http://caml.inria.fr/distrib/ocaml-4.05/ocaml-4.05-refman-html.tar.gz (6) http://caml.inria.fr/distrib/ocaml-4.05/ocaml-4.05-refman.info.tar.gz 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.5 Imperative features - 1.6 Exceptions - 1.7 Symbolic processing of expressions - 1.8 Pretty-printing - 1.9 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 Advanced examples with classes and modules - 5.1 Extended example: bank accounts - 5.2 Simple modules as classes - 5.2.1 Strings - 5.2.2 Hashtbl - 5.2.3 Sets - 5.3 The subject/observer pattern - Part: II The OCaml language - Chapter 6 The OCaml language - 6.1 Lexical conventions - 6.2 Values - 6.2.1 Base values - 6.2.2 Tuples - 6.2.3 Records - 6.2.4 Arrays - 6.2.5 Variant values - 6.2.6 Polymorphic variants - 6.2.7 Functions - 6.2.8 Objects - 6.3 Names - 6.4 Type expressions - 6.5 Constants - 6.6 Patterns - 6.7 Expressions - 6.7.1 Basic expressions - 6.7.2 Control structures - 6.7.3 Operations on data structures - 6.7.4 Operators - 6.7.5 Objects - 6.7.6 Coercions - 6.7.7 Other - 6.8 Type and exception definitions - 6.8.1 Type definitions - 6.8.2 Exception definitions - 6.9 Classes - 6.9.1 Class types - 6.9.2 Class expressions - 6.9.3 Class definitions - 6.9.4 Class specifications - 6.9.5 Class type definitions - 6.10 Module types (module specifications) - 6.10.1 Simple module types - 6.10.2 Signatures - 6.10.3 Functor types - 6.10.4 The with operator - 6.11 Module expressions (module implementations) - 6.11.1 Simple module expressions - 6.11.2 Structures - 6.11.3 Functors - 6.12 Compilation units - Chapter 7 Language extensions - 7.1 Integer literals for types int32, int64 and nativeint - 7.2 Recursive definitions of values - 7.3 Lazy patterns - 7.4 Recursive modules - 7.5 Private types - 7.5.1 Private variant and record types - 7.5.2 Private type abbreviations - 7.5.3 Private row types - 7.6 Local opens - 7.7 Record and object notations - 7.8 Explicit polymorphic type annotations - 7.9 Locally abstract types - 7.10 First-class modules - 7.11 Recovering the type of a module - 7.12 Substituting inside a signature - 7.13 Type-level module aliases - 7.14 Explicit overriding in class definitions - 7.15 Overriding in open statements - 7.16 Generalized algebraic datatypes - 7.17 Syntax for Bigarray access - 7.18 Attributes - 7.18.1 Built-in attributes - 7.19 Extension nodes - 7.19.1 Built-in extension nodes - 7.20 Quoted strings - 7.21 Exception cases in pattern matching - 7.22 Extensible variant types - 7.23 Generative functors - 7.24 Extension-only syntax - 7.24.1 Extension operators - 7.24.2 Extension literals - 7.25 Inline records - 7.26 Local exceptions - 7.27 Documentation comments - 7.27.1 Floating comments - 7.27.2 Item comments - 7.27.3 Label comments - Part: III The OCaml tools - Chapter 8 Batch compilation (ocamlc) - 8.1 Overview of the compiler - 8.2 Options - 8.3 Modules and the file system - 8.4 Common errors - 8.5 Warning reference - 8.5.1 Warning 52: fragile constant pattern - 8.5.2 Warning 57: Ambiguous or-pattern variables under guard - Chapter 9 The toplevel system (ocaml) - 9.1 Options - 9.2 Toplevel directives - 9.3 The toplevel and the module system - 9.4 Common errors - 9.5 Building custom toplevel systems: ocamlmktop - 9.6 Options - Chapter 10 The runtime system (ocamlrun) - 10.1 Overview - 10.2 Options - 10.3 Dynamic loading of shared libraries - 10.4 Common errors - Chapter 11 Native-code compilation (ocamlopt) - 11.1 Overview of the compiler - 11.2 Options - 11.3 Common errors - 11.4 Running executables produced by ocamlopt - 11.5 Compatibility with the bytecode compiler - Chapter 12 Lexer and parser generators (ocamllex, ocamlyacc) - 12.1 Overview of ocamllex - 12.1.1 Options - 12.2 Syntax of lexer definitions - 12.2.1 Header and trailer - 12.2.2 Naming regular expressions - 12.2.3 Entry points - 12.2.4 Regular expressions - 12.2.5 Actions - 12.2.6 Variables in regular expressions - 12.2.7 Refill handlers - 12.2.8 Reserved identifiers - 12.3 Overview of ocamlyacc - 12.4 Syntax of grammar definitions - 12.4.1 Header and trailer - 12.4.2 Declarations - 12.4.3 Rules - 12.4.4 Error handling - 12.5 Options - 12.6 A complete example - 12.7 Common errors - Chapter 13 Dependency generator (ocamldep) - 13.1 Options - 13.2 A typical Makefile - Chapter 14 The browser/editor (ocamlbrowser) - 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 Communication between the debugger and the program - 16.8.7 Fine-tuning the debugger - 16.8.8 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 The ocamlbuild compilation manager - Chapter 19 Interfacing C with OCaml - 19.1 Overview and compilation information - 19.1.1 Declaring primitives - 19.1.2 Implementing primitives - 19.1.3 Statically linking C code with OCaml code - 19.1.4 Dynamically linking C code with OCaml code - 19.1.5 Choosing between static linking and dynamic linking - 19.1.6 Building standalone custom runtime systems - 19.2 The value type - 19.2.1 Integer values - 19.2.2 Blocks - 19.2.3 Pointers outside the heap - 19.3 Representation of OCaml data types - 19.3.1 Atomic types - 19.3.2 Tuples and records - 19.3.3 Arrays - 19.3.4 Concrete data types - 19.3.5 Objects - 19.3.6 Polymorphic variants - 19.4 Operations on values - 19.4.1 Kind tests - 19.4.2 Operations on integers - 19.4.3 Accessing blocks - 19.4.4 Allocating blocks - 19.4.5 Raising exceptions - 19.5 Living in harmony with the garbage collector - 19.5.1 Simple interface - 19.5.2 Low-level interface - 19.6 A complete example - 19.7 Advanced topic: callbacks from C to OCaml - 19.7.1 Applying OCaml closures from C - 19.7.2 Obtaining or registering OCaml closures for use in C functions - 19.7.3 Registering OCaml exceptions for use in C functions - 19.7.4 Main program in C - 19.7.5 Embedding the OCaml code in the C code - 19.8 Advanced example with callbacks - 19.9 Advanced topic: custom blocks - 19.9.1 The struct custom_operations - 19.9.2 Allocating custom blocks - 19.9.3 Accessing custom blocks - 19.9.4 Writing custom serialization and deserialization functions - 19.9.5 Choosing identifiers - 19.9.6 Finalized blocks - 19.10 Advanced topic: cheaper C call - 19.10.1 Passing unboxed values - 19.10.2 Direct C call - 19.10.3 Example: calling C library functions without indirection - 19.11 Advanced topic: multithreading - 19.11.1 Registering threads created from C - 19.11.2 Parallel execution of long-running C code - 19.12 Building mixed C/OCaml libraries: ocamlmklib - Chapter 20 Optimisation with Flambda - 20.1 Overview - 20.2 Command-line flags - 20.2.1 Specification of optimisation parameters by round - 20.3 Inlining - 20.3.1 Classic inlining heuristic - 20.3.2 Overview of "Flambda" inlining heuristics - 20.3.3 Handling of specific language constructs - 20.3.4 Inlining reports - 20.3.5 Assessment of inlining benefit - 20.3.6 Control of speculation - 20.4 Specialisation - 20.4.1 Assessment of specialisation benefit - 20.5 Default settings of parameters - 20.5.1 Settings at -O2 optimisation level - 20.5.2 Settings at -O3 optimisation level - 20.6 Manual control of inlining and specialisation - 20.7 Simplification - 20.8 Other code motion transformations - 20.8.1 Lifting of constants - 20.8.2 Lifting of toplevel let bindings - 20.9 Unboxing transformations - 20.9.1 Unboxing of closure variables - 20.9.2 Unboxing of specialised arguments - 20.9.3 Unboxing of closures - 20.10 Removal of unused code and values - 20.10.1 Removal of redundant let expressions - 20.10.2 Removal of redundant program constructs - 20.10.3 Removal of unused arguments - 20.10.4 Removal of unused closure variables - 20.11 Other code transformations - 20.11.1 Transformation of non-escaping references into mutable variables - 20.11.2 Substitution of closure variables for specialised arguments - 20.12 Treatment of effects - 20.13 Compilation of statically-allocated modules - 20.14 Inhibition of optimisation - 20.15 Use of unsafe operations - 20.16 Glossary - Chapter 21 Memory profiling with Spacetime - 21.1 Overview - 21.2 How to use it - 21.2.1 Building - 21.2.2 Running - 21.2.3 Analysis - 21.3 Runtime overhead - 21.4 For developers - Chapter 22 Fuzzing with afl-fuzz - 22.1 Overview - 22.2 Generating instrumentation - 22.2.1 Advanced options - 22.3 Example - Part: IV The OCaml library - Chapter 23 The core library - 23.1 Built-in types and predefined exceptions - 23.2 Module Pervasives : The initially opened module. - Chapter 24 The standard library - 24.1 Module Arg : Parsing of command line arguments. - 24.2 Module Array : Array operations. - 24.3 Module ArrayLabels : Array operations. - 24.4 Module Buffer : Extensible buffers. - 24.5 Module Bytes : Byte sequence operations. - 24.6 Module BytesLabels : Byte sequence operations. - 24.7 Module Callback : Registering OCaml values with the C runtime. - 24.8 Module Char : Character operations. - 24.9 Module Complex : Complex numbers. - 24.10 Module Digest : MD5 message digest. - 24.11 Module Ephemeron : Ephemerons and weak hash table - 24.12 Module Filename : Operations on file names. - 24.13 Module Format : Pretty printing. - 24.14 Module Gc : Memory management control and statistics; finalised values. - 24.15 Module Genlex : A generic lexical analyzer. - 24.16 Module Hashtbl : Hash tables and hash functions. - 24.17 Module Int32 : 32-bit integers. - 24.18 Module Int64 : 64-bit integers. - 24.19 Module Lazy : Deferred computations. - 24.20 Module Lexing : The run-time library for lexers generated by ocamllex. - 24.21 Module List : List operations. - 24.22 Module ListLabels : List operations. - 24.23 Module Map : Association tables over ordered types. - 24.24 Module Marshal : Marshaling of data structures. - 24.25 Module MoreLabels : Extra labeled libraries. - 24.26 Module Nativeint : Processor-native integers. - 24.27 Module Oo : Operations on objects - 24.28 Module Parsing : The run-time library for parsers generated by ocamlyacc. - 24.29 Module Printexc : Facilities for printing exceptions and inspecting current call stack. - 24.30 Module Printf : Formatted output functions. - 24.31 Module Queue : First-in first-out queues. - 24.32 Module Random : Pseudo-random number generators (PRNG). - 24.33 Module Scanf : Formatted input functions. - 24.34 Module Set : Sets over ordered types. - 24.35 Module Sort : Sorting and merging lists. - Chapter 25 Memory profiling with Spacetime - 25.1 Overview - 25.2 How to use it - 25.2.1 Building - 25.2.2 Running - 25.2.3 Analysis - 25.3 Runtime overhead - 25.4 For developers - 25.5 Module Stack : Last-in first-out stacks. - 25.6 Module StdLabels : Standard labeled libraries. - 25.7 Module Stream : Streams and parsers. - 25.8 Module String : String operations. - 25.9 Module StringLabels : String operations. - 25.10 Module Sys : System interface. - 25.11 Module Uchar : Unicode characters. - 25.12 Module Weak : Arrays of weak pointers and hash sets of weak pointers. - Chapter 26 The compiler front-end - 26.1 Module Ast_mapper : The interface of a -ppx rewriter - 26.1.1 A generic Parsetree mapper - 26.1.2 Apply mappers to compilation units - 26.1.3 Registration API - 26.1.4 Convenience functions to write mappers - 26.1.5 Helper functions to call external mappers - 26.1.6 Cookies - 26.2 Module Asttypes : Auxiliary AST types used by parsetree and typedtree. - 26.3 Module Location : Source code locations (ranges of positions), used in parsetree. - 26.4 Module Longident : Long identifiers, used in parsetree. - 26.5 Module Parse : Entry points in the parser - 26.6 Module Parsetree : Abstract syntax tree produced by parsing - 26.6.1 Extension points - 26.6.2 Core language - 26.6.3 Class language - 26.6.4 Module language - 26.6.5 Toplevel - 26.7 Module Pprintast - Chapter 27 The unix library: Unix system calls - 27.1 Module Unix : Interface to the Unix system. - 27.2 Module UnixLabels: labelized version of the interface - Chapter 28 The num library: arbitrary-precision rational arithmetic - 28.1 Module Num : Operation on arbitrary-precision numbers. - 28.2 Module Big_int : Operations on arbitrary-precision integers. - 28.3 Module Arith_status : Flags that control rational arithmetic. - Chapter 29 The str library: regular expressions and string processing - 29.1 Module Str : Regular expressions and high-level string processing - Chapter 30 The threads library - 30.1 Module Thread : Lightweight threads for Posix 1003.1c and Win32. - 30.2 Module Mutex : Locks for mutual exclusion. - 30.3 Module Condition : Condition variables to synchronize between threads. - 30.4 Module Event : First-class synchronous communication. - 30.5 Module ThreadUnix : Thread-compatible system calls. - Chapter 31 The graphics library - 31.1 Module Graphics : Machine-independent graphics primitives. - Chapter 32 The dynlink library: dynamic loading and linking of object files - 32.1 Module Dynlink : Dynamic loading of object files. - Chapter 33 The bigarray library - 33.1 Module Bigarray : Large, multi-dimensional, numerical arrays. - 33.2 Big arrays in the OCaml-C interface - 33.2.1 Include file - 33.2.2 Accessing an OCaml bigarray from C or Fortran - 33.2.3 Wrapping a C or Fortran array as an OCaml big array - Part: V Appendix - Chapter 34 Index to the library - Chapter 35 Index of keywords Foreword ******** This manual documents the release 4.05 of the OCaml system. It is organized as follows. - Part I, "An introduction to OCaml", gives an overview of the language. - Part II, "The OCaml language", is the reference description of the language. - Part III, "The OCaml tools", documents the compilers, toplevel system, and programming utilities. - Part IV, "The OCaml library", describes the modules provided in the standard library. Conventions *=*=*=*=*=* OCaml runs on several operating systems. The parts of this manual that are specific to one operating system are presented as shown below: Unix: This is material specific to the Unix family of operating systems, including Linux and MacOS X. Windows: This is material specific to Microsoft Windows (XP, Vista, 7, 8, 10). License *=*=*=* The OCaml system is copyright © 1996--2013 Institut National de Recherche en Informatique et en Automatique (INRIA). INRIA holds all ownership rights to the OCaml system. The OCaml system is open source and can be freely redistributed. See the file LICENSE in the distribution for licensing information. The present documentation is copyright © 2013 Institut National de Recherche en Informatique et en Automatique (INRIA). The OCaml documentation and user's manual may be reproduced and distributed in whole or in part, subject to the following conditions: - The copyright notice above and this permission notice must be preserved complete on all complete or partial copies. - Any translation or derivative work of the OCaml documentation and user's manual must be approved by the authors in writing before distribution. - If you distribute the OCaml documentation and user's manual in part, instructions for obtaining the complete version of this manual must be included, and a means for obtaining a complete version provided. - Small portions may be reproduced as illustrations for reviews or quotes in other works without this permission notice if proper citation is given. Availability *=*=*=*=*=*= The complete OCaml distribution can be accessed via the community Caml Web site (7) and the older Caml Web site (8). The community Caml Web site (9) contains a lot of additional information on OCaml. --------------------------------------- (7) http://www.ocaml.org/ (8) http://caml.inria.fr/ (9) http://www.ocaml.org/ Part: I ******* An introduction to OCaml ************************ Chapter 1 The core language ****************************** This part of the manual is a tutorial introduction to the OCaml language. A good familiarity with programming in a conventional languages (say, Pascal or C) is assumed, but no prior exposure to functional languages is required. The present chapter introduces the core language. Chapter 2 deals with the module system, chapter 3 with the object-oriented features, chapter 4 with extensions to the core language (labeled arguments and polymorphic variants), and chapter 5 gives some advanced examples. 1.1 Basics *=*=*=*=*=* For this overview of OCaml, we use the interactive system, which is started by running ocaml from the Unix shell, or by launching the OCamlwin.exe application under Windows. This tutorial is presented as the transcript of a session with the interactive system: lines starting with # represent user input; the system responses are printed below, without a leading #. Under the interactive system, the user types OCaml phrases terminated by ;; in response to the # prompt, and the system compiles them on the fly, executes them, and prints the outcome of evaluation. Phrases are either simple expressions, or let definitions of identifiers (either values or functions). <<#1+2*3;; - : int = 7 >> <<#let pi = 4.0 *. atan 1.0;; val pi : float = 3.14159265358979312 >> <<#let square x = x *. x;; val square : float -> float = >> <<#square (sin pi) +. square (cos pi);; - : float = 1. >> The OCaml system computes both the value and the type for each phrase. Even function parameters need no explicit type declaration: the system infers their types from their usage in the function. Notice also that integers and floating-point numbers are distinct types, with distinct operators: + and * operate on integers, but +. and *. operate on floats. <<#1.0 * 2;; Error: This expression has type float but an expression was expected of type int >> Recursive functions are defined with the let rec binding: <<#let rec fib n = # if n < 2 then n else fib (n-1) + fib (n-2);; val fib : int -> int = >> <<#fib 10;; - : int = 55 >> 1.2 Data types *=*=*=*=*=*=*=* In addition to integers and floating-point numbers, OCaml offers the usual basic data types: booleans, characters, and immutable character strings. <<#(1 < 2) = false;; - : bool = false >> <<#'a';; - : char = 'a' >> <<#"Hello world";; - : string = "Hello world" >> Predefined data structures include tuples, arrays, and lists. General mechanisms for defining your own data structures are also provided. They will be covered in more details later; for now, we concentrate on lists. Lists are either given in extension as a bracketed list of semicolon-separated elements, or built from the empty list [] (pronounce "nil") by adding elements in front using the :: ("cons") operator. <<#let l = ["is"; "a"; "tale"; "told"; "etc."];; val l : string list = ["is"; "a"; "tale"; "told"; "etc."] >> <<#"Life" :: l;; - : string list = ["Life"; "is"; "a"; "tale"; "told"; "etc."] >> As with all other OCaml data structures, lists do not need to be explicitly allocated and deallocated from memory: all memory management is entirely automatic in OCaml. Similarly, there is no explicit handling of pointers: the OCaml compiler silently introduces pointers where necessary. As with most OCaml data structures, inspecting and destructuring lists is performed by pattern-matching. List patterns have the exact same shape as list expressions, with identifier representing unspecified parts of the list. As an example, here is insertion sort on a list: <<#let rec sort lst = # match lst with # [] -> [] # | head :: tail -> insert head (sort tail) #and insert elt lst = # match lst with # [] -> [elt] # | head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail #;; val sort : 'a list -> 'a list = val insert : 'a -> 'a list -> 'a list = >> <<#sort l;; - : string list = ["a"; "etc."; "is"; "tale"; "told"] >> The type inferred for sort, 'a list -> 'a list, means that sort can actually apply to lists of any type, and returns a list of the same type. The type 'a is a type variable, and stands for any given type. The reason why sort can apply to lists of any type is that the comparisons (=, <=, etc.) are polymorphic in OCaml: they operate between any two values of the same type. This makes sort itself polymorphic over all list types. <<#sort [6;2;5;3];; - : int list = [2; 3; 5; 6] >> <<#sort [3.14; 2.718];; - : float list = [2.718; 3.14] >> The sort function above does not modify its input list: it builds and returns a new list containing the same elements as the input list, in ascending order. There is actually no way in OCaml to modify in-place a list once it is built: we say that lists are immutable data structures. Most OCaml data structures are immutable, but a few (most notably arrays) are mutable, meaning that they can be modified in-place at any time. 1.3 Functions as values *=*=*=*=*=*=*=*=*=*=*=*= OCaml is a functional language: functions in the full mathematical sense are supported and can be passed around freely just as any other piece of data. For instance, here is a deriv function that takes any float function as argument and returns an approximation of its derivative function: <<#let deriv f dx = function x -> (f (x +. dx) -. f x) /. dx;; val deriv : (float -> float) -> float -> float -> float = >> <<#let sin' = deriv sin 1e-6;; val sin' : float -> float = >> <<#sin' pi;; - : float = -1.00000000013961143 >> Even function composition is definable: <<#let compose f g = function x -> f (g x);; val compose : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b = >> <<#let cos2 = compose square cos;; val cos2 : float -> float = >> Functions that take other functions as arguments are called "functionals", or "higher-order functions". Functionals are especially useful to provide iterators or similar generic operations over a data structure. For instance, the standard OCaml library provides a List.map functional that applies a given function to each element of a list, and returns the list of the results: <<#List.map (function n -> n * 2 + 1) [0;1;2;3;4];; - : int list = [1; 3; 5; 7; 9] >> This functional, along with a number of other list and array functionals, is predefined because it is often useful, but there is nothing magic with it: it can easily be defined as follows. <<#let rec map f l = # match l with # [] -> [] # | hd :: tl -> f hd :: map f tl;; val map : ('a -> 'b) -> 'a list -> 'b list = >> 1.4 Records and variants *=*=*=*=*=*=*=*=*=*=*=*=* User-defined data structures include records and variants. Both are defined with the type declaration. Here, we declare a record type to represent rational numbers. <<#type ratio = {num: int; denom: int};; type ratio = { num : int; denom : int; } >> <<#let add_ratio r1 r2 = # {num = r1.num * r2.denom + r2.num * r1.denom; # denom = r1.denom * r2.denom};; val add_ratio : ratio -> ratio -> ratio = >> <<#add_ratio {num=1; denom=3} {num=2; denom=5};; - : ratio = {num = 11; denom = 15} >> The declaration of a variant type lists all possible shapes for values of that type. Each case is identified by a name, called a constructor, which serves both for constructing values of the variant type and inspecting them by pattern-matching. Constructor names are capitalized to distinguish them from variable names (which must start with a lowercase letter). For instance, here is a variant type for doing mixed arithmetic (integers and floats): <<#type number = Int of int | Float of float | Error;; type number = Int of int | Float of float | Error >> This declaration expresses that a value of type number is either an integer, a floating-point number, or the constant Error representing the result of an invalid operation (e.g. a division by zero). Enumerated types are a special case of variant types, where all alternatives are constants: <<#type sign = Positive | Negative;; type sign = Positive | Negative >> <<#let sign_int n = if n >= 0 then Positive else Negative;; val sign_int : int -> sign = >> To define arithmetic operations for the number type, we use pattern-matching on the two numbers involved: <<#let add_num n1 n2 = # match (n1, n2) with # (Int i1, Int i2) -> # (* Check for overflow of integer addition *) # if sign_int i1 = sign_int i2 && sign_int (i1 + i2) <> sign_int i1 # then Float(float i1 +. float i2) # else Int(i1 + i2) # | (Int i1, Float f2) -> Float(float i1 +. f2) # | (Float f1, Int i2) -> Float(f1 +. float i2) # | (Float f1, Float f2) -> Float(f1 +. f2) # | (Error, _) -> Error # | (_, Error) -> Error;; val add_num : number -> number -> number = >> <<#add_num (Int 123) (Float 3.14159);; - : number = Float 126.14159 >> The most common usage of variant types is to describe recursive data structures. Consider for example the type of binary trees: <<#type 'a btree = Empty | Node of 'a * 'a btree * 'a btree;; type 'a btree = Empty | Node of 'a * 'a btree * 'a btree >> This definition reads as follows: a binary tree containing values of type 'a (an arbitrary type) is either empty, or is a node containing one value of type 'a and two subtrees containing also values of type 'a, that is, two 'a btree. Operations on binary trees are naturally expressed as recursive functions following the same structure as the type definition itself. For instance, here are functions performing lookup and insertion in ordered binary trees (elements increase from left to right): <<#let rec member x btree = # match btree with # Empty -> false # | Node(y, left, right) -> # if x = y then true else # if x < y then member x left else member x right;; val member : 'a -> 'a btree -> bool = >> <<#let rec insert x btree = # match btree with # Empty -> Node(x, Empty, Empty) # | Node(y, left, right) -> # if x <= y then Node(y, insert x left, right) # else Node(y, left, insert x right);; val insert : 'a -> 'a btree -> 'a btree = >> 1.5 Imperative features *=*=*=*=*=*=*=*=*=*=*=*= Though all examples so far were written in purely applicative style, OCaml is also equipped with full imperative features. This includes the usual while and for loops, as well as mutable data structures such as arrays. Arrays are either given in extension between [| and |] brackets, or allocated and initialized with the Array.make function, then filled up later by assignments. For instance, the function below sums two vectors (represented as float arrays) componentwise. <<#let add_vect v1 v2 = # let len = min (Array.length v1) (Array.length v2) in # let res = Array.make len 0.0 in # for i = 0 to len - 1 do # res.(i) <- v1.(i) +. v2.(i) # done; # res;; val add_vect : float array -> float array -> float array = >> <<#add_vect [| 1.0; 2.0 |] [| 3.0; 4.0 |];; - : float array = [|4.; 6.|] >> Record fields can also be modified by assignment, provided they are declared mutable in the definition of the record type: <<#type mutable_point = { mutable x: float; mutable y: float };; type mutable_point = { mutable x : float; mutable y : float; } >> <<#let translate p dx dy = # p.x <- p.x +. dx; p.y <- p.y +. dy;; val translate : mutable_point -> float -> float -> unit = >> <<#let mypoint = { x = 0.0; y = 0.0 };; val mypoint : mutable_point = {x = 0.; y = 0.} >> <<#translate mypoint 1.0 2.0;; - : unit = () >> <<#mypoint;; - : mutable_point = {x = 1.; y = 2.} >> OCaml has no built-in notion of variable -- identifiers whose current value can be changed by assignment. (The let binding is not an assignment, it introduces a new identifier with a new scope.) However, the standard library provides references, which are mutable indirection cells (or one-element arrays), with operators ! to fetch the current contents of the reference and := to assign the contents. Variables can then be emulated by let-binding a reference. For instance, here is an in-place insertion sort over arrays: <<#let insertion_sort a = # for i = 1 to Array.length a - 1 do # let val_i = a.(i) in # let j = ref i in # while !j > 0 && val_i < a.(!j - 1) do # a.(!j) <- a.(!j - 1); # j := !j - 1 # done; # a.(!j) <- val_i # done;; val insertion_sort : 'a array -> unit = >> References are also useful to write functions that maintain a current state between two calls to the function. For instance, the following pseudo-random number generator keeps the last returned number in a reference: <<#let current_rand = ref 0;; val current_rand : int ref = {contents = 0} >> <<#let random () = # current_rand := !current_rand * 25713 + 1345; # !current_rand;; val random : unit -> int = >> Again, there is nothing magical with references: they are implemented as a single-field mutable record, as follows. <<#type 'a ref = { mutable contents: 'a };; type 'a ref = { mutable contents : 'a; } >> <<#let ( ! ) r = r.contents;; val ( ! ) : 'a ref -> 'a = >> <<#let ( := ) r newval = r.contents <- newval;; val ( := ) : 'a ref -> 'a -> unit = >> In some special cases, you may need to store a polymorphic function in a data structure, keeping its polymorphism. Without user-provided type annotations, this is not allowed, as polymorphism is only introduced on a global level. However, you can give explicitly polymorphic types to record fields. <<#type idref = { mutable id: 'a. 'a -> 'a };; type idref = { mutable id : 'a. 'a -> 'a; } >> <<#let r = {id = fun x -> x};; val r : idref = {id = } >> <<#let g s = (s.id 1, s.id true);; val g : idref -> int * bool = >> <<#r.id <- (fun x -> print_string "called id\n"; x);; - : unit = () >> <<#g r;; called id called id - : int * bool = (1, true) >> 1.6 Exceptions *=*=*=*=*=*=*=* OCaml provides exceptions for signalling and handling exceptional conditions. Exceptions can also be used as a general-purpose non-local control structure. Exceptions are declared with the exception construct, and signalled with the raise operator. For instance, the function below for taking the head of a list uses an exception to signal the case where an empty list is given. <<#exception Empty_list;; exception Empty_list >> <<#let head l = # match l with # [] -> raise Empty_list # | hd :: tl -> hd;; val head : 'a list -> 'a = >> <<#head [1;2];; - : int = 1 >> <<#head [];; Exception: Empty_list. >> Exceptions are used throughout the standard library to signal cases where the library functions cannot complete normally. For instance, the List.assoc function, which returns the data associated with a given key in a list of (key, data) pairs, raises the predefined exception Not_found when the key does not appear in the list: <<#List.assoc 1 [(0, "zero"); (1, "one")];; - : string = "one" >> <<#List.assoc 2 [(0, "zero"); (1, "one")];; Exception: Not_found. >> Exceptions can be trapped with the try...with construct: <<#let name_of_binary_digit digit = # try # List.assoc digit [0, "zero"; 1, "one"] # with Not_found -> # "not a binary digit";; val name_of_binary_digit : int -> string = >> <<#name_of_binary_digit 0;; - : string = "zero" >> <<#name_of_binary_digit (-1);; - : string = "not a binary digit" >> The with part is actually a regular pattern-matching on the exception value. Thus, several exceptions can be caught by one try...with construct. Also, finalization can be performed by trapping all exceptions, performing the finalization, then raising again the exception: <<#let temporarily_set_reference ref newval funct = # let oldval = !ref in # try # ref := newval; # let res = funct () in # ref := oldval; # res # with x -> # ref := oldval; # raise x;; val temporarily_set_reference : 'a ref -> 'a -> (unit -> 'b) -> 'b = >> 1.7 Symbolic processing of expressions *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* We finish this introduction with a more complete example representative of the use of OCaml for symbolic processing: formal manipulations of arithmetic expressions containing variables. The following variant type describes the expressions we shall manipulate: <<#type expression = # Const of float # | Var of string # | Sum of expression * expression (* e1 + e2 *) # | Diff of expression * expression (* e1 - e2 *) # | Prod of expression * expression (* e1 * e2 *) # | Quot of expression * expression (* e1 / e2 *) #;; type expression = Const of float | Var of string | Sum of expression * expression | Diff of expression * expression | Prod of expression * expression | Quot of expression * expression >> We first define a function to evaluate an expression given an environment that maps variable names to their values. For simplicity, the environment is represented as an association list. <<#exception Unbound_variable of string;; exception Unbound_variable of string >> <<#let rec eval env exp = # match exp with # Const c -> c # | Var v -> # (try List.assoc v env with Not_found -> raise (Unbound_variable v)) # | Sum(f, g) -> eval env f +. eval env g # | Diff(f, g) -> eval env f -. eval env g # | Prod(f, g) -> eval env f *. eval env g # | Quot(f, g) -> eval env f /. eval env g;; val eval : (string * float) list -> expression -> float = >> <<#eval [("x", 1.0); ("y", 3.14)] (Prod(Sum(Var "x", Const 2.0), Var "y"));; - : float = 9.42 >> Now for a real symbolic processing, we define the derivative of an expression with respect to a variable dv: <<#let rec deriv exp dv = # match exp with # Const c -> Const 0.0 # | Var v -> if v = dv then Const 1.0 else Const 0.0 # | Sum(f, g) -> Sum(deriv f dv, deriv g dv) # | Diff(f, g) -> Diff(deriv f dv, deriv g dv) # | Prod(f, g) -> Sum(Prod(f, deriv g dv), Prod(deriv f dv, g)) # | Quot(f, g) -> Quot(Diff(Prod(deriv f dv, g), Prod(f, deriv g dv)), # Prod(g, g)) #;; val deriv : expression -> string -> expression = >> <<#deriv (Quot(Const 1.0, Var "x")) "x";; - : expression = Quot (Diff (Prod (Const 0., Var "x"), Prod (Const 1., Const 1.)), Prod (Var "x", Var "x")) >> 1.8 Pretty-printing *=*=*=*=*=*=*=*=*=*= As shown in the examples above, the internal representation (also called abstract syntax) of expressions quickly becomes hard to read and write as the expressions get larger. We need a printer and a parser to go back and forth between the abstract syntax and the concrete syntax, which in the case of expressions is the familiar algebraic notation (e.g. 2*x+1). For the printing function, we take into account the usual precedence rules (i.e. * binds tighter than +) to avoid printing unnecessary parentheses. To this end, we maintain the current operator precedence and print parentheses around an operator only if its precedence is less than the current precedence. <<#let print_expr exp = # (* Local function definitions *) # let open_paren prec op_prec = # if prec > op_prec then print_string "(" in # let close_paren prec op_prec = # if prec > op_prec then print_string ")" in # let rec print prec exp = (* prec is the current precedence *) # match exp with # Const c -> print_float c # | Var v -> print_string v # | Sum(f, g) -> # open_paren prec 0; # print 0 f; print_string " + "; print 0 g; # close_paren prec 0 # | Diff(f, g) -> # open_paren prec 0; # print 0 f; print_string " - "; print 1 g; # close_paren prec 0 # | Prod(f, g) -> # open_paren prec 2; # print 2 f; print_string " * "; print 2 g; # close_paren prec 2 # | Quot(f, g) -> # open_paren prec 2; # print 2 f; print_string " / "; print 3 g; # close_paren prec 2 # in print 0 exp;; val print_expr : expression -> unit = >> <<#let e = Sum(Prod(Const 2.0, Var "x"), Const 1.0);; val e : expression = Sum (Prod (Const 2., Var "x"), Const 1.) >> <<#print_expr e; print_newline ();; 2. * x + 1. - : unit = () >> <<#print_expr (deriv e "x"); print_newline ();; 2. * 1. + 0. * x + 0. - : unit = () >> 1.9 Standalone OCaml programs *=*=*=*=*=*=*=*=*=*=*=*=*=*=*= All examples given so far were executed under the interactive system. OCaml code can also be compiled separately and executed non-interactively using the batch compilers ocamlc and ocamlopt. The source code must be put in a file with extension .ml. It consists of a sequence of phrases, which will be evaluated at runtime in their order of appearance in the source file. Unlike in interactive mode, types and values are not printed automatically; the program must call printing functions explicitly to produce some output. Here is a sample standalone program to print Fibonacci numbers: <<(* File fib.ml *) let rec fib n = if n < 2 then 1 else fib (n-1) + fib (n-2);; let main () = let arg = int_of_string Sys.argv.(1) in print_int (fib arg); print_newline (); exit 0;; main ();; >> Sys.argv is an array of strings containing the command-line parameters. Sys.argv.(1) is thus the first command-line parameter. The program above is compiled and executed with the following shell commands: <<$ ocamlc -o fib fib.ml $ ./fib 10 89 $ ./fib 20 10946 >> More complex standalone OCaml programs are typically composed of multiple source files, and can link with precompiled libraries. Chapters 8 and 11 explain how to use the batch compilers ocamlc and ocamlopt. Recompilation of multi-file OCaml projects can be automated using third-party build systems, such as the ocamlbuild (1) compilation manager. --------------------------------------- (1) https://github.com/ocaml/ocamlbuild/ Chapter 2 The module system ****************************** This chapter introduces the module system of OCaml. 2.1 Structures *=*=*=*=*=*=*=* A primary motivation for modules is to package together related definitions (such as the definitions of a data type and associated operations over that type) and enforce a consistent naming scheme for these definitions. This avoids running out of names or accidentally confusing names. Such a package is called a structure and is introduced by the struct...end construct, which contains an arbitrary sequence of definitions. The structure is usually given a name with the module binding. Here is for instance a structure packaging together a type of priority queues and their operations: <<#module PrioQueue = # struct # type priority = int # type 'a queue = Empty | Node of priority * 'a * 'a queue * 'a queue # let empty = Empty # let rec insert queue prio elt = # match queue with # Empty -> Node(prio, elt, Empty, Empty) # | Node(p, e, left, right) -> # if prio <= p # then Node(prio, elt, insert right p e, left) # else Node(p, e, insert right prio elt, left) # exception Queue_is_empty # let rec remove_top = function # Empty -> raise Queue_is_empty # | Node(prio, elt, left, Empty) -> left # | Node(prio, elt, Empty, right) -> right # | Node(prio, elt, (Node(lprio, lelt, _, _) as left), # (Node(rprio, relt, _, _) as right)) -> # if lprio <= rprio # then Node(lprio, lelt, remove_top left, right) # else Node(rprio, relt, left, remove_top right) # let extract = function # Empty -> raise Queue_is_empty # | Node(prio, elt, _, _) as queue -> (prio, elt, remove_top queue) # end;; module PrioQueue : sig type priority = int type 'a queue = Empty | Node of priority * 'a * 'a queue * 'a queue val empty : 'a queue val insert : 'a queue -> priority -> 'a -> 'a queue exception Queue_is_empty val remove_top : 'a queue -> 'a queue val extract : 'a queue -> priority * 'a * 'a queue end >> Outside the structure, its components can be referred to using the "dot notation", that is, identifiers qualified by a structure name. For instance, PrioQueue.insert is the function insert defined inside the structure PrioQueue and PrioQueue.queue is the type queue defined in PrioQueue. <<#PrioQueue.insert PrioQueue.empty 1 "hello";; - : string PrioQueue.queue = PrioQueue.Node (1, "hello", PrioQueue.Empty, PrioQueue.Empty) >> 2.2 Signatures *=*=*=*=*=*=*=* Signatures are interfaces for structures. A signature specifies which components of a structure are accessible from the outside, and with which type. It can be used to hide some components of a structure (e.g. local function definitions) or export some components with a restricted type. For instance, the signature below specifies the three priority queue operations empty, insert and extract, but not the auxiliary function remove_top. Similarly, it makes the queue type abstract (by not providing its actual representation as a concrete type). <<#module type PRIOQUEUE = # sig # type priority = int (* still concrete *) # type 'a queue (* now abstract *) # val empty : 'a queue # val insert : 'a queue -> int -> 'a -> 'a queue # val extract : 'a queue -> int * 'a * 'a queue # exception Queue_is_empty # end;; module type PRIOQUEUE = sig type priority = int type 'a queue val empty : 'a queue val insert : 'a queue -> int -> 'a -> 'a queue val extract : 'a queue -> int * 'a * 'a queue exception Queue_is_empty end >> Restricting the PrioQueue structure by this signature results in another view of the PrioQueue structure where the remove_top function is not accessible and the actual representation of priority queues is hidden: <<#module AbstractPrioQueue = (PrioQueue : PRIOQUEUE);; module AbstractPrioQueue : PRIOQUEUE >> <<#AbstractPrioQueue.remove_top ;; Error: Unbound value AbstractPrioQueue.remove_top >> <<#AbstractPrioQueue.insert AbstractPrioQueue.empty 1 "hello";; - : string AbstractPrioQueue.queue = >> The restriction can also be performed during the definition of the structure, as in <> An alternate syntax is provided for the above: <> 2.3 Functors *=*=*=*=*=*=* Functors are "functions" from structures to structures. They are used to express parameterized structures: a structure A parameterized by a structure B is simply a functor F with a formal parameter B (along with the expected signature for B) which returns the actual structure A itself. The functor F can then be applied to one or several implementations B_1 ...B_n of B, yielding the corresponding structures A_1 ...A_n. For instance, here is a structure implementing sets as sorted lists, parameterized by a structure providing the type of the set elements and an ordering function over this type (used to keep the sets sorted): <<#type comparison = Less | Equal | Greater;; type comparison = Less | Equal | Greater >> <<#module type ORDERED_TYPE = # sig # type t # val compare: t -> t -> comparison # end;; module type ORDERED_TYPE = sig type t val compare : t -> t -> comparison end >> <<#module Set = # functor (Elt: ORDERED_TYPE) -> # struct # type element = Elt.t # type set = element list # let empty = [] # let rec add x s = # match s with # [] -> [x] # | hd::tl -> # match Elt.compare x hd with # Equal -> s (* x is already in s *) # | Less -> x :: s (* x is smaller than all elements of s *) # | Greater -> hd :: add x tl # let rec member x s = # match s with # [] -> false # | hd::tl -> # match Elt.compare x hd with # Equal -> true (* x belongs to s *) # | Less -> false (* x is smaller than all elements of s *) # | Greater -> member x tl # end;; module Set : functor (Elt : ORDERED_TYPE) -> sig type element = Elt.t type set = element list val empty : 'a list val add : Elt.t -> Elt.t list -> Elt.t list val member : Elt.t -> Elt.t list -> bool end >> By applying the Set functor to a structure implementing an ordered type, we obtain set operations for this type: <<#module OrderedString = # struct # type t = string # let compare x y = if x = y then Equal else if x < y then Less else Greater # end;; module OrderedString : sig type t = string val compare : 'a -> 'a -> comparison end >> <<#module StringSet = Set(OrderedString);; module StringSet : sig type element = OrderedString.t type set = element list val empty : 'a list val add : OrderedString.t -> OrderedString.t list -> OrderedString.t list val member : OrderedString.t -> OrderedString.t list -> bool end >> <<#StringSet.member "bar" (StringSet.add "foo" StringSet.empty);; - : bool = false >> 2.4 Functors and type abstraction *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= As in the PrioQueue example, it would be good style to hide the actual implementation of the type set, so that users of the structure will not rely on sets being lists, and we can switch later to another, more efficient representation of sets without breaking their code. This can be achieved by restricting Set by a suitable functor signature: <<#module type SETFUNCTOR = # functor (Elt: ORDERED_TYPE) -> # sig # type element = Elt.t (* concrete *) # type set (* abstract *) # val empty : set # val add : element -> set -> set # val member : element -> set -> bool # end;; module type SETFUNCTOR = functor (Elt : ORDERED_TYPE) -> sig type element = Elt.t type set val empty : set val add : element -> set -> set val member : element -> set -> bool end >> <<#module AbstractSet = (Set : SETFUNCTOR);; module AbstractSet : SETFUNCTOR >> <<#module AbstractStringSet = AbstractSet(OrderedString);; module AbstractStringSet : sig type element = OrderedString.t type set = AbstractSet(OrderedString).set val empty : set val add : element -> set -> set val member : element -> set -> bool end >> <<#AbstractStringSet.add "gee" AbstractStringSet.empty;; - : AbstractStringSet.set = >> In an attempt to write the type constraint above more elegantly, one may wish to name the signature of the structure returned by the functor, then use that signature in the constraint: <<#module type SET = # sig # type element # type set # val empty : set # val add : element -> set -> set # val member : element -> set -> bool # end;; module type SET = sig type element type set val empty : set val add : element -> set -> set val member : element -> set -> bool end >> <<#module WrongSet = (Set : functor(Elt: ORDERED_TYPE) -> SET);; module WrongSet : functor (Elt : ORDERED_TYPE) -> SET >> <<#module WrongStringSet = WrongSet(OrderedString);; module WrongStringSet : sig type element = WrongSet(OrderedString).element type set = WrongSet(OrderedString).set val empty : set val add : element -> set -> set val member : element -> set -> bool end >> <<#WrongStringSet.add "gee" WrongStringSet.empty ;; Error: This expression has type string but an expression was expected of type WrongStringSet.element = WrongSet(OrderedString).element >> The problem here is that SET specifies the type element abstractly, so that the type equality between element in the result of the functor and t in its argument is forgotten. Consequently, WrongStringSet.element is not the same type as string, and the operations of WrongStringSet cannot be applied to strings. As demonstrated above, it is important that the type element in the signature SET be declared equal to Elt.t; unfortunately, this is impossible above since SET is defined in a context where Elt does not exist. To overcome this difficulty, OCaml provides a with type construct over signatures that allows enriching a signature with extra type equalities: <<#module AbstractSet2 = # (Set : functor(Elt: ORDERED_TYPE) -> (SET with type element = Elt.t));; module AbstractSet2 : functor (Elt : ORDERED_TYPE) -> sig type element = Elt.t type set val empty : set val add : element -> set -> set val member : element -> set -> bool end >> As in the case of simple structures, an alternate syntax is provided for defining functors and restricting their result: <> Abstracting a type component in a functor result is a powerful technique that provides a high degree of type safety, as we now illustrate. Consider an ordering over character strings that is different from the standard ordering implemented in the OrderedString structure. For instance, we compare strings without distinguishing upper and lower case. <<#module NoCaseString = # struct # type t = string # let compare s1 s2 = # OrderedString.compare (String.lowercase_ascii s1) (String.lowercase_ascii s2) # end;; module NoCaseString : sig type t = string val compare : string -> string -> comparison end >> <<#module NoCaseStringSet = AbstractSet(NoCaseString);; module NoCaseStringSet : sig type element = NoCaseString.t type set = AbstractSet(NoCaseString).set val empty : set val add : element -> set -> set val member : element -> set -> bool end >> <<#NoCaseStringSet.add "FOO" AbstractStringSet.empty ;; Error: This expression has type AbstractStringSet.set = AbstractSet(OrderedString).set but an expression was expected of type NoCaseStringSet.set = AbstractSet(NoCaseString).set >> Note that the two types AbstractStringSet.set and NoCaseStringSet.set are not compatible, and values of these two types do not match. This is the correct behavior: even though both set types contain elements of the same type (strings), they are built upon different orderings of that type, and different invariants need to be maintained by the operations (being strictly increasing for the standard ordering and for the case-insensitive ordering). Applying operations from AbstractStringSet to values of type NoCaseStringSet.set could give incorrect results, or build lists that violate the invariants of NoCaseStringSet. 2.5 Modules and separate compilation *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* All examples of modules so far have been given in the context of the interactive system. However, modules are most useful for large, batch-compiled programs. For these programs, it is a practical necessity to split the source into several files, called compilation units, that can be compiled separately, thus minimizing recompilation after changes. In OCaml, compilation units are special cases of structures and signatures, and the relationship between the units can be explained easily in terms of the module system. A compilation unit A comprises two files: - the implementation file A.ml, which contains a sequence of definitions, analogous to the inside of a struct...end construct; - the interface file A.mli, which contains a sequence of specifications, analogous to the inside of a sig...end construct. These two files together define a structure named A as if the following definition was entered at top-level: << module A: sig (* contents of file A.mli *) end = struct (* contents of file A.ml *) end;; >> The files that define the compilation units can be compiled separately using the ocamlc -c command (the -c option means "compile only, do not try to link"); this produces compiled interface files (with extension .cmi) and compiled object code files (with extension .cmo). When all units have been compiled, their .cmo files are linked together using the ocamlc command. For instance, the following commands compile and link a program composed of two compilation units Aux and Main: <<$ ocamlc -c Aux.mli # produces aux.cmi $ ocamlc -c Aux.ml # produces aux.cmo $ ocamlc -c Main.mli # produces main.cmi $ ocamlc -c Main.ml # produces main.cmo $ ocamlc -o theprogram Aux.cmo Main.cmo >> The program behaves exactly as if the following phrases were entered at top-level: << module Aux: sig (* contents of Aux.mli *) end = struct (* contents of Aux.ml *) end;; module Main: sig (* contents of Main.mli *) end = struct (* contents of Main.ml *) end;; >> In particular, Main can refer to Aux: the definitions and declarations contained in Main.ml and Main.mli can refer to definition in Aux.ml, using the Aux.ident notation, provided these definitions are exported in Aux.mli. The order in which the .cmo files are given to ocamlc during the linking phase determines the order in which the module definitions occur. Hence, in the example above, Aux appears first and Main can refer to it, but Aux cannot refer to Main. Note that only top-level structures can be mapped to separately-compiled files, but neither functors nor module types. However, all module-class objects can appear as components of a structure, so the solution is to put the functor or module type inside a structure, which can then be mapped to a file. Chapter 3 Objects in OCaml ***************************** (Chapter written by Jérôme Vouillon, Didier Rémy and Jacques Garrigue) This chapter gives an overview of the object-oriented features of OCaml. Note that the relation between object, class and type in OCaml is very different from that in mainstream object-oriented languages like Java or C++, so that you should not assume that similar keywords mean the same thing. 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 3.1 Classes and objects *=*=*=*=*=*=*=*=*=*=*=*= The class point below defines one instance variable x and two methods get_x and move. The initial value of the instance variable is 0. The variable x is declared mutable, so the method move can change its value. <<#class point = # object # val mutable x = 0 # method get_x = x # method move d = x <- x + d # end;; class point : object val mutable x : int method get_x : int method move : int -> unit end >> We now create a new point p, instance of the point class. <<#let p = new point;; val p : point = >> Note that the type of p is point. This is an abbreviation automatically defined by the class definition above. It stands for the object type unit>, listing the methods of class point along with their types. We now invoke some methods to p: <<#p#get_x;; - : int = 0 >> <<#p#move 3;; - : unit = () >> <<#p#get_x;; - : int = 3 >> The evaluation of the body of a class only takes place at object creation time. Therefore, in the following example, the instance variable x is initialized to different values for two different objects. <<#let x0 = ref 0;; val x0 : int ref = {contents = 0} >> <<#class point = # object # val mutable x = incr x0; !x0 # method get_x = x # method move d = x <- x + d # end;; class point : object val mutable x : int method get_x : int method move : int -> unit end >> <<#new point#get_x;; - : int = 1 >> <<#new point#get_x;; - : int = 2 >> The class point can also be abstracted over the initial values of the x coordinate. <<#class point = fun x_init -> # object # val mutable x = x_init # method get_x = x # method move d = x <- x + d # end;; class point : int -> object val mutable x : int method get_x : int method move : int -> unit end >> Like in function definitions, the definition above can be abbreviated as: <<#class point x_init = # object # val mutable x = x_init # method get_x = x # method move d = x <- x + d # end;; class point : int -> object val mutable x : int method get_x : int method move : int -> unit end >> An instance of the class point is now a function that expects an initial parameter to create a point object: <<#new point;; - : int -> point = >> <<#let p = new point 7;; val p : point = >> The parameter x_init is, of course, visible in the whole body of the definition, including methods. For instance, the method get_offset in the class below returns the position of the object relative to its initial position. <<#class point x_init = # object # val mutable x = x_init # method get_x = x # method get_offset = x - x_init # method move d = x <- x + d # end;; class point : int -> object val mutable x : int method get_offset : int method get_x : int method move : int -> unit end >> Expressions can be evaluated and bound before defining the object body of the class. This is useful to enforce invariants. For instance, points can be automatically adjusted to the nearest point on a grid, as follows: <<#class adjusted_point x_init = # let origin = (x_init / 10) * 10 in # object # val mutable x = origin # method get_x = x # method get_offset = x - origin # method move d = x <- x + d # end;; class adjusted_point : int -> object val mutable x : int method get_offset : int method get_x : int method move : int -> unit end >> (One could also raise an exception if the x_init coordinate is not on the grid.) In fact, the same effect could here be obtained by calling the definition of class point with the value of the origin. <<#class adjusted_point x_init = point ((x_init / 10) * 10);; class adjusted_point : int -> point >> An alternate solution would have been to define the adjustment in a special allocation function: <<#let new_adjusted_point x_init = new point ((x_init / 10) * 10);; val new_adjusted_point : int -> point = >> However, the former pattern is generally more appropriate, since the code for adjustment is part of the definition of the class and will be inherited. This ability provides class constructors as can be found in other languages. Several constructors can be defined this way to build objects of the same class but with different initialization patterns; an alternative is to use initializers, as described below in section 3.4. 3.2 Immediate objects *=*=*=*=*=*=*=*=*=*=*= There is another, more direct way to create an object: create it without going through a class. The syntax is exactly the same as for class expressions, but the result is a single object rather than a class. All the constructs described in the rest of this section also apply to immediate objects. <<#let p = # object # val mutable x = 0 # method get_x = x # method move d = x <- x + d # end;; val p : < get_x : int; move : int -> unit > = >> <<#p#get_x;; - : int = 0 >> <<#p#move 3;; - : unit = () >> <<#p#get_x;; - : int = 3 >> Unlike classes, which cannot be defined inside an expression, immediate objects can appear anywhere, using variables from their environment. <<#let minmax x y = # if x < y then object method min = x method max = y end # else object method min = y method max = x end;; val minmax : 'a -> 'a -> < max : 'a; min : 'a > = >> Immediate objects have two weaknesses compared to classes: their types are not abbreviated, and you cannot inherit from them. But these two weaknesses can be advantages in some situations, as we will see in sections 3.3 and 3.10. 3.3 Reference to self *=*=*=*=*=*=*=*=*=*=*= A method or an initializer can send messages to self (that is, the current object). For that, self must be explicitly bound, here to the variable s (s could be any identifier, even though we will often choose the name self.) <<#class printable_point x_init = # object (s) # val mutable x = x_init # method get_x = x # method move d = x <- x + d # method print = print_int s#get_x # end;; class printable_point : int -> object val mutable x : int method get_x : int method move : int -> unit method print : unit end >> <<#let p = new printable_point 7;; val p : printable_point = >> <<#p#print;; 7- : unit = () >> Dynamically, the variable s is bound at the invocation of a method. In particular, when the class printable_point is inherited, the variable s will be correctly bound to the object of the subclass. A common problem with self is that, as its type may be extended in subclasses, you cannot fix it in advance. Here is a simple example. <<#let ints = ref [];; val ints : '_a list ref = {contents = []} >> <<#class my_int = # object (self) # method n = 1 # method register = ints := self :: !ints # end ;; Error: This expression has type < n : int; register : 'a; .. > but an expression was expected of type 'b Self type cannot escape its class >> You can ignore the first two lines of the error message. What matters is the last one: putting self into an external reference would make it impossible to extend it through inheritance. We will see in section 3.12 a workaround to this problem. Note however that, since immediate objects are not extensible, the problem does not occur with them. <<#let my_int = # object (self) # method n = 1 # method register = ints := self :: !ints # end;; val my_int : < n : int; register : unit > = >> 3.4 Initializers *=*=*=*=*=*=*=*=* Let-bindings within class definitions are evaluated before the object is constructed. It is also possible to evaluate an expression immediately after the object has been built. Such code is written as an anonymous hidden method called an initializer. Therefore, it can access self and the instance variables. <<#class printable_point x_init = # let origin = (x_init / 10) * 10 in # object (self) # val mutable x = origin # method get_x = x # method move d = x <- x + d # method print = print_int self#get_x # initializer print_string "new point at "; self#print; print_newline () # end;; class printable_point : int -> object val mutable x : int method get_x : int method move : int -> unit method print : unit end >> <<#let p = new printable_point 17;; new point at 10 val p : printable_point = >> Initializers cannot be overridden. On the contrary, all initializers are evaluated sequentially. Initializers are particularly useful to enforce invariants. Another example can be seen in section 5.1. 3.5 Virtual methods *=*=*=*=*=*=*=*=*=*= It is possible to declare a method without actually defining it, using the keyword virtual. This method will be provided later in subclasses. A class containing virtual methods must be flagged virtual, and cannot be instantiated (that is, no object of this class can be created). It still defines type abbreviations (treating virtual methods as other methods.) <<#class virtual abstract_point x_init = # object (self) # method virtual get_x : int # method get_offset = self#get_x - x_init # method virtual move : int -> unit # end;; class virtual abstract_point : int -> object method get_offset : int method virtual get_x : int method virtual move : int -> unit end >> <<#class point x_init = # object # inherit abstract_point x_init # val mutable x = x_init # method get_x = x # method move d = x <- x + d # end;; class point : int -> object val mutable x : int method get_offset : int method get_x : int method move : int -> unit end >> Instance variables can also be declared as virtual, with the same effect as with methods. <<#class virtual abstract_point2 = # object # val mutable virtual x : int # method move d = x <- x + d # end;; class virtual abstract_point2 : object val mutable virtual x : int method move : int -> unit end >> <<#class point2 x_init = # object # inherit abstract_point2 # val mutable x = x_init # method get_offset = x - x_init # end;; class point2 : int -> object val mutable x : int method get_offset : int method move : int -> unit end >> 3.6 Private methods *=*=*=*=*=*=*=*=*=*= Private methods are methods that do not appear in object interfaces. They can only be invoked from other methods of the same object. <<#class restricted_point x_init = # object (self) # val mutable x = x_init # method get_x = x # method private move d = x <- x + d # method bump = self#move 1 # end;; class restricted_point : int -> object val mutable x : int method bump : unit method get_x : int method private move : int -> unit end >> <<#let p = new restricted_point 0;; val p : restricted_point = >> <<#p#move 10 ;; Error: This expression has type restricted_point It has no method move >> <<#p#bump;; - : unit = () >> Note that this is not the same thing as private and protected methods in Java or C++, which can be called from other objects of the same class. This is a direct consequence of the independence between types and classes in OCaml: two unrelated classes may produce objects of the same type, and there is no way at the type level to ensure that an object comes from a specific class. However a possible encoding of friend methods is given in section 3.17. Private methods are inherited (they are by default visible in subclasses), unless they are hidden by signature matching, as described below. Private methods can be made public in a subclass. <<#class point_again x = # object (self) # inherit restricted_point x # method virtual move : _ # end;; class point_again : int -> object val mutable x : int method bump : unit method get_x : int method move : int -> unit end >> The annotation virtual here is only used to mention a method without providing its definition. Since we didn't add the private annotation, this makes the method public, keeping the original definition. An alternative definition is <<#class point_again x = # object (self : < move : _; ..> ) # inherit restricted_point x # end;; class point_again : int -> object val mutable x : int method bump : unit method get_x : int method move : int -> unit end >> The constraint on self's type is requiring a public move method, and this is sufficient to override private. One could think that a private method should remain private in a subclass. However, since the method is visible in a subclass, it is always possible to pick its code and define a method of the same name that runs that code, so yet another (heavier) solution would be: <<#class point_again x = # object # inherit restricted_point x as super # method move = super#move # end;; class point_again : int -> object val mutable x : int method bump : unit method get_x : int method move : int -> unit end >> Of course, private methods can also be virtual. Then, the keywords must appear in this order method private virtual. 3.7 Class interfaces *=*=*=*=*=*=*=*=*=*=* Class interfaces are inferred from class definitions. They may also be defined directly and used to restrict the type of a class. Like class declarations, they also define a new type abbreviation. <<#class type restricted_point_type = # object # method get_x : int # method bump : unit #end;; class type restricted_point_type = object method bump : unit method get_x : int end >> <<#fun (x : restricted_point_type) -> x;; - : restricted_point_type -> restricted_point_type = >> In addition to program documentation, class interfaces can be used to constrain the type of a class. Both concrete instance variables and concrete private methods can be hidden by a class type constraint. Public methods and virtual members, however, cannot. <<#class restricted_point' x = (restricted_point x : restricted_point_type);; class restricted_point' : int -> restricted_point_type >> Or, equivalently: <<#class restricted_point' = (restricted_point : int -> restricted_point_type);; class restricted_point' : int -> restricted_point_type >> The interface of a class can also be specified in a module signature, and used to restrict the inferred signature of a module. <<#module type POINT = sig # class restricted_point' : int -> # object # method get_x : int # method bump : unit # end #end;; module type POINT = sig class restricted_point' : int -> object method bump : unit method get_x : int end end >> <<#module Point : POINT = struct # class restricted_point' = restricted_point #end;; module Point : POINT >> 3.8 Inheritance *=*=*=*=*=*=*=*= We illustrate inheritance by defining a class of colored points that inherits from the class of points. This class has all instance variables and all methods of class point, plus a new instance variable c and a new method color. <<#class colored_point x (c : string) = # object # inherit point x # val c = c # method color = c # end;; class colored_point : int -> string -> object val c : string val mutable x : int method color : string method get_offset : int method get_x : int method move : int -> unit end >> <<#let p' = new colored_point 5 "red";; val p' : colored_point = >> <<#p'#get_x, p'#color;; - : int * string = (5, "red") >> A point and a colored point have incompatible types, since a point has no method color. However, the function get_x below is a generic function applying method get_x to any object p that has this method (and possibly some others, which are represented by an ellipsis in the type). Thus, it applies to both points and colored points. <<#let get_succ_x p = p#get_x + 1;; val get_succ_x : < get_x : int; .. > -> int = >> <<#get_succ_x p + get_succ_x p';; - : int = 8 >> Methods need not be declared previously, as shown by the example: <<#let set_x p = p#set_x;; val set_x : < set_x : 'a; .. > -> 'a = >> <<#let incr p = set_x p (get_succ_x p);; val incr : < get_x : int; set_x : int -> 'a; .. > -> 'a = >> 3.9 Multiple inheritance *=*=*=*=*=*=*=*=*=*=*=*=* Multiple inheritance is allowed. Only the last definition of a method is kept: the redefinition in a subclass of a method that was visible in the parent class overrides the definition in the parent class. Previous definitions of a method can be reused by binding the related ancestor. Below, super is bound to the ancestor printable_point. The name super is a pseudo value identifier that can only be used to invoke a super-class method, as in super#print. <<#class printable_colored_point y c = # object (self) # val c = c # method color = c # inherit printable_point y as super # method print = # print_string "("; # super#print; # print_string ", "; # print_string (self#color); # print_string ")" # end;; class printable_colored_point : int -> string -> object val c : string val mutable x : int method color : string method get_x : int method move : int -> unit method print : unit end >> <<#let p' = new printable_colored_point 17 "red";; new point at (10, red) val p' : printable_colored_point = >> <<#p'#print;; (10, red)- : unit = () >> A private method that has been hidden in the parent class is no longer visible, and is thus not overridden. Since initializers are treated as private methods, all initializers along the class hierarchy are evaluated, in the order they are introduced. 3.10 Parameterized classes *=*=*=*=*=*=*=*=*=*=*=*=*=* Reference cells can be implemented as objects. The naive definition fails to typecheck: <<#class oref x_init = # object # val mutable x = x_init # method get = x # method set y = x <- y # end;; Error: Some type variables are unbound in this type: class oref : 'a -> object val mutable x : 'a method get : 'a method set : 'a -> unit end The method get has type 'a where 'a is unbound >> The reason is that at least one of the methods has a polymorphic type (here, the type of the value stored in the reference cell), thus either the class should be parametric, or the method type should be constrained to a monomorphic type. A monomorphic instance of the class could be defined by: <<#class oref (x_init:int) = # object # val mutable x = x_init # method get = x # method set y = x <- y # end;; class oref : int -> object val mutable x : int method get : int method set : int -> unit end >> Note that since immediate objects do not define a class type, they have no such restriction. <<#let new_oref x_init = # object # val mutable x = x_init # method get = x # method set y = x <- y # end;; val new_oref : 'a -> < get : 'a; set : 'a -> unit > = >> On the other hand, a class for polymorphic references must explicitly list the type parameters in its declaration. Class type parameters are listed between [ and ]. The type parameters must also be bound somewhere in the class body by a type constraint. <<#class ['a] oref x_init = # object # val mutable x = (x_init : 'a) # method get = x # method set y = x <- y # end;; class ['a] oref : 'a -> object val mutable x : 'a method get : 'a method set : 'a -> unit end >> <<#let r = new oref 1 in r#set 2; (r#get);; - : int = 2 >> The type parameter in the declaration may actually be constrained in the body of the class definition. In the class type, the actual value of the type parameter is displayed in the constraint clause. <<#class ['a] oref_succ (x_init:'a) = # object # val mutable x = x_init + 1 # method get = x # method set y = x <- y # end;; class ['a] oref_succ : 'a -> object constraint 'a = int val mutable x : int method get : int method set : int -> unit end >> Let us consider a more complex example: define a circle, whose center may be any kind of point. We put an additional type constraint in method move, since no free variables must remain unaccounted for by the class type parameters. <<#class ['a] circle (c : 'a) = # object # val mutable center = c # method center = center # method set_center c = center <- c # method move = (center#move : int -> unit) # end;; class ['a] circle : 'a -> object constraint 'a = < move : int -> unit; .. > val mutable center : 'a method center : 'a method move : int -> unit method set_center : 'a -> unit end >> An alternate definition of circle, using a constraint clause in the class definition, is shown below. The type #point used below in the constraint clause is an abbreviation produced by the definition of class point. This abbreviation unifies with the type of any object belonging to a subclass of class point. It actually expands to < get_x : int; move : int -> unit; .. >. This leads to the following alternate definition of circle, which has slightly stronger constraints on its argument, as we now expect center to have a method get_x. <<#class ['a] circle (c : 'a) = # object # constraint 'a = #point # val mutable center = c # method center = center # method set_center c = center <- c # method move = center#move # end;; class ['a] circle : 'a -> object constraint 'a = #point val mutable center : 'a method center : 'a method move : int -> unit method set_center : 'a -> unit end >> The class colored_circle is a specialized version of class circle that requires the type of the center to unify with #colored_point, and adds a method color. Note that when specializing a parameterized class, the instance of type parameter must always be explicitly given. It is again written between [ and ]. <<#class ['a] colored_circle c = # object # constraint 'a = #colored_point # inherit ['a] circle c # method color = center#color # end;; class ['a] colored_circle : 'a -> object constraint 'a = #colored_point val mutable center : 'a method center : 'a method color : string method move : int -> unit method set_center : 'a -> unit end >> 3.11 Polymorphic methods *=*=*=*=*=*=*=*=*=*=*=*=* While parameterized classes may be polymorphic in their contents, they are not enough to allow polymorphism of method use. A classical example is defining an iterator. <<#List.fold_left;; - : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a = >> <<#class ['a] intlist (l : int list) = # object # method empty = (l = []) # method fold f (accu : 'a) = List.fold_left f accu l # end;; class ['a] intlist : int list -> object method empty : bool method fold : ('a -> int -> 'a) -> 'a -> 'a end >> At first look, we seem to have a polymorphic iterator, however this does not work in practice. <<#let l = new intlist [1; 2; 3];; val l : '_a intlist = >> <<#l#fold (fun x y -> x+y) 0;; - : int = 6 >> <<#l;; - : int intlist = >> <<#l#fold (fun s x -> s ^ string_of_int x ^ " ") "" ;; Error: This expression has type int but an expression was expected of type string >> Our iterator works, as shows its first use for summation. However, since objects themselves are not polymorphic (only their constructors are), using the fold method fixes its type for this individual object. Our next attempt to use it as a string iterator fails. The problem here is that quantification was wrongly located: it is not the class we want to be polymorphic, but the fold method. This can be achieved by giving an explicitly polymorphic type in the method definition. <<#class intlist (l : int list) = # object # method empty = (l = []) # method fold : 'a. ('a -> int -> 'a) -> 'a -> 'a = # fun f accu -> List.fold_left f accu l # end;; class intlist : int list -> object method empty : bool method fold : ('a -> int -> 'a) -> 'a -> 'a end >> <<#let l = new intlist [1; 2; 3];; val l : intlist = >> <<#l#fold (fun x y -> x+y) 0;; - : int = 6 >> <<#l#fold (fun s x -> s ^ string_of_int x ^ " ") "";; - : string = "1 2 3 " >> As you can see in the class type shown by the compiler, while polymorphic method types must be fully explicit in class definitions (appearing immediately after the method name), quantified type variables can be left implicit in class descriptions. Why require types to be explicit? The problem is that (int -> int -> int) -> int -> int would also be a valid type for fold, and it happens to be incompatible with the polymorphic type we gave (automatic instantiation only works for toplevel types variables, not for inner quantifiers, where it becomes an undecidable problem.) So the compiler cannot choose between those two types, and must be helped. However, the type can be completely omitted in the class definition if it is already known, through inheritance or type constraints on self. Here is an example of method overriding. <<#class intlist_rev l = # object # inherit intlist l # method fold f accu = List.fold_left f accu (List.rev l) # end;; >> The following idiom separates description and definition. <<#class type ['a] iterator = # object method fold : ('b -> 'a -> 'b) -> 'b -> 'b end;; >> <<#class intlist l = # object (self : int #iterator) # method empty = (l = []) # method fold f accu = List.fold_left f accu l # end;; >> Note here the (self : int #iterator) idiom, which ensures that this object implements the interface iterator. Polymorphic methods are called in exactly the same way as normal methods, but you should be aware of some limitations of type inference. Namely, a polymorphic method can only be called if its type is known at the call site. Otherwise, the method will be assumed to be monomorphic, and given an incompatible type. <<#let sum lst = lst#fold (fun x y -> x+y) 0;; val sum : < fold : (int -> int -> int) -> int -> 'a; .. > -> 'a = >> <<#sum l ;; Error: This expression has type intlist but an expression was expected of type < fold : (int -> int -> int) -> int -> 'a; .. > Types for method fold are incompatible >> The workaround is easy: you should put a type constraint on the parameter. <<#let sum (lst : _ #iterator) = lst#fold (fun x y -> x+y) 0;; val sum : int #iterator -> int = >> Of course the constraint may also be an explicit method type. Only occurences of quantified variables are required. <<#let sum lst = # (lst : < fold : 'a. ('a -> _ -> 'a) -> 'a -> 'a; .. >)#fold (+) 0;; val sum : < fold : 'a. ('a -> int -> 'a) -> 'a -> 'a; .. > -> int = >> Another use of polymorphic methods is to allow some form of implicit subtyping in method arguments. We have already seen in section 3.8 how some functions may be polymorphic in the class of their argument. This can be extended to methods. <<#class type point0 = object method get_x : int end;; class type point0 = object method get_x : int end >> <<#class distance_point x = # object # inherit point x # method distance : 'a. (#point0 as 'a) -> int = # fun other -> abs (other#get_x - x) # end;; class distance_point : int -> object val mutable x : int method distance : #point0 -> int method get_offset : int method get_x : int method move : int -> unit end >> <<#let p = new distance_point 3 in #(p#distance (new point 8), p#distance (new colored_point 1 "blue"));; - : int * int = (5, 2) >> Note here the special syntax (#point0 as 'a) we have to use to quantify the extensible part of #point0. As for the variable binder, it can be omitted in class specifications. If you want polymorphism inside object field it must be quantified independently. <<#class multi_poly = # object # method m1 : 'a. (< n1 : 'b. 'b -> 'b; .. > as 'a) -> _ = # fun o -> o#n1 true, o#n1 "hello" # method m2 : 'a 'b. (< n2 : 'b -> bool; .. > as 'a) -> 'b -> _ = # fun o x -> o#n2 x # end;; class multi_poly : object method m1 : < n1 : 'b. 'b -> 'b; .. > -> bool * string method m2 : < n2 : 'b -> bool; .. > -> 'b -> bool end >> In method m1, o must be an object with at least a method n1, itself polymorphic. In method m2, the argument of n2 and x must have the same type, which is quantified at the same level as 'a. 3.12 Using coercions *=*=*=*=*=*=*=*=*=*=* Subtyping is never implicit. There are, however, two ways to perform subtyping. The most general construction is fully explicit: both the domain and the codomain of the type coercion must be given. We have seen that points and colored points have incompatible types. For instance, they cannot be mixed in the same list. However, a colored point can be coerced to a point, hiding its color method: <<#let colored_point_to_point cp = (cp : colored_point :> point);; val colored_point_to_point : colored_point -> point = >> <<#let p = new point 3 and q = new colored_point 4 "blue";; val p : point = val q : colored_point = >> <<#let l = [p; (colored_point_to_point q)];; val l : point list = [; ] >> An object of type t can be seen as an object of type t' only if t is a subtype of t'. For instance, a point cannot be seen as a colored point. <<#(p : point :> colored_point);; Error: Type point = < get_offset : int; get_x : int; move : int -> unit > is not a subtype of colored_point = < color : string; get_offset : int; get_x : int; move : int -> unit > >> Indeed, narrowing coercions without runtime checks would be unsafe. Runtime type checks might raise exceptions, and they would require the presence of type information at runtime, which is not the case in the OCaml system. For these reasons, there is no such operation available in the language. Be aware that subtyping and inheritance are not related. Inheritance is a syntactic relation between classes while subtyping is a semantic relation between types. For instance, the class of colored points could have been defined directly, without inheriting from the class of points; the type of colored points would remain unchanged and thus still be a subtype of points. The domain of a coercion can often be omitted. For instance, one can define: <<#let to_point cp = (cp :> point);; val to_point : #point -> point = >> In this case, the function colored_point_to_point is an instance of the function to_point. This is not always true, however. The fully explicit coercion is more precise and is sometimes unavoidable. Consider, for example, the following class: <<#class c0 = object method m = {< >} method n = 0 end;; class c0 : object ('a) method m : 'a method n : int end >> The object type c0 is an abbreviation for as 'a. Consider now the type declaration: <<#class type c1 = object method m : c1 end;; class type c1 = object method m : c1 end >> The object type c1 is an abbreviation for the type as 'a. The coercion from an object of type c0 to an object of type c1 is correct: <<#fun (x:c0) -> (x : c0 :> c1);; - : c0 -> c1 = >> However, the domain of the coercion cannot always be omitted. In that case, the solution is to use the explicit form. Sometimes, a change in the class-type definition can also solve the problem <<#class type c2 = object ('a) method m : 'a end;; class type c2 = object ('a) method m : 'a end >> <<#fun (x:c0) -> (x :> c2);; - : c0 -> c2 = >> While class types c1 and c2 are different, both object types c1 and c2 expand to the same object type (same method names and types). Yet, when the domain of a coercion is left implicit and its co-domain is an abbreviation of a known class type, then the class type, rather than the object type, is used to derive the coercion function. This allows leaving the domain implicit in most cases when coercing form a subclass to its superclass. The type of a coercion can always be seen as below: <<#let to_c1 x = (x :> c1);; val to_c1 : < m : #c1; .. > -> c1 = >> <<#let to_c2 x = (x :> c2);; val to_c2 : #c2 -> c2 = >> Note the difference between these two coercions: in the case of to_c2, the type #c2 = < m : 'a; .. > as 'a is polymorphically recursive (according to the explicit recursion in the class type of c2); hence the success of applying this coercion to an object of class c0. On the other hand, in the first case, c1 was only expanded and unrolled twice to obtain < m : < m : c1; .. >; .. > (remember #c1 = < m : c1; .. >), without introducing recursion. You may also note that the type of to_c2 is #c2 -> c2 while the type of to_c1 is more general than #c1 -> c1. This is not always true, since there are class types for which some instances of #c are not subtypes of c, as explained in section 3.16. Yet, for parameterless classes the coercion (_ :> c) is always more general than (_ : #c :> c). A common problem may occur when one tries to define a coercion to a class c while defining class c. The problem is due to the type abbreviation not being completely defined yet, and so its subtypes are not clearly known. Then, a coercion (_ :> c) or (_ : #c :> c) is taken to be the identity function, as in <<#function x -> (x :> 'a);; - : 'a -> 'a = >> As a consequence, if the coercion is applied to self, as in the following example, the type of self is unified with the closed type c (a closed object type is an object type without ellipsis). This would constrain the type of self be closed and is thus rejected. Indeed, the type of self cannot be closed: this would prevent any further extension of the class. Therefore, a type error is generated when the unification of this type with another type would result in a closed object type. <<#class c = object method m = 1 end #and d = object (self) # inherit c # method n = 2 # method as_c = (self :> c) #end;; Error: This expression cannot be coerced to type c = < m : int >; it has type < as_c : c; m : int; n : int; .. > but is here used with type c Self type cannot escape its class >> However, the most common instance of this problem, coercing self to its current class, is detected as a special case by the type checker, and properly typed. <<#class c = object (self) method m = (self :> c) end;; class c : object method m : c end >> This allows the following idiom, keeping a list of all objects belonging to a class or its subclasses: <<#let all_c = ref [];; val all_c : '_a list ref = {contents = []} >> <<#class c (m : int) = # object (self) # method m = m # initializer all_c := (self :> c) :: !all_c # end;; class c : int -> object method m : int end >> This idiom can in turn be used to retrieve an object whose type has been weakened: <<#let rec lookup_obj obj = function [] -> raise Not_found # | obj' :: l -> # if (obj :> < >) = (obj' :> < >) then obj' else lookup_obj obj l ;; val lookup_obj : < .. > -> (< .. > as 'a) list -> 'a = >> <<#let lookup_c obj = lookup_obj obj !all_c;; val lookup_c : < .. > -> < m : int > = >> The type < m : int > we see here is just the expansion of c, due to the use of a reference; we have succeeded in getting back an object of type c. The previous coercion problem can often be avoided by first defining the abbreviation, using a class type: <<#class type c' = object method m : int end;; class type c' = object method m : int end >> <<#class c : c' = object method m = 1 end #and d = object (self) # inherit c # method n = 2 # method as_c = (self :> c') #end;; class c : c' and d : object method as_c : c' method m : int method n : int end >> It is also possible to use a virtual class. Inheriting from this class simultaneously forces all methods of c to have the same type as the methods of c'. <<#class virtual c' = object method virtual m : int end;; class virtual c' : object method virtual m : int end >> <<#class c = object (self) inherit c' method m = 1 end;; class c : object method m : int end >> One could think of defining the type abbreviation directly: <<#type c' = ;; >> However, the abbreviation #c' cannot be defined directly in a similar way. It can only be defined by a class or a class-type definition. This is because a #-abbreviation carries an implicit anonymous variable .. that cannot be explicitly named. The closer you get to it is: <<#type 'a c'_class = 'a constraint 'a = < m : int; .. >;; >> with an extra type variable capturing the open object type. 3.13 Functional objects *=*=*=*=*=*=*=*=*=*=*=*= It is possible to write a version of class point without assignments on the instance variables. The override construct {< ... >} returns a copy of "self" (that is, the current object), possibly changing the value of some instance variables. <<#class functional_point y = # object # val x = y # method get_x = x # method move d = {< x = x + d >} # end;; class functional_point : int -> object ('a) val x : int method get_x : int method move : int -> 'a end >> <<#let p = new functional_point 7;; val p : functional_point = >> <<#p#get_x;; - : int = 7 >> <<#(p#move 3)#get_x;; - : int = 10 >> <<#p#get_x;; - : int = 7 >> Note that the type abbreviation functional_point is recursive, which can be seen in the class type of functional_point: the type of self is 'a and 'a appears inside the type of the method move. The above definition of functional_point is not equivalent to the following: <<#class bad_functional_point y = # object # val x = y # method get_x = x # method move d = new bad_functional_point (x+d) # end;; class bad_functional_point : int -> object val x : int method get_x : int method move : int -> bad_functional_point end >> While objects of either class will behave the same, objects of their subclasses will be different. In a subclass of bad_functional_point, the method move will keep returning an object of the parent class. On the contrary, in a subclass of functional_point, the method move will return an object of the subclass. Functional update is often used in conjunction with binary methods as illustrated in section 5.2.1. 3.14 Cloning objects *=*=*=*=*=*=*=*=*=*=* Objects can also be cloned, whether they are functional or imperative. The library function Oo.copy makes a shallow copy of an object. That is, it returns a new object that has the same methods and instance variables as its argument. The instance variables are copied but their contents are shared. Assigning a new value to an instance variable of the copy (using a method call) will not affect instance variables of the original, and conversely. A deeper assignment (for example if the instance variable is a reference cell) will of course affect both the original and the copy. The type of Oo.copy is the following: <<#Oo.copy;; - : (< .. > as 'a) -> 'a = >> The keyword as in that type binds the type variable 'a to the object type < .. >. Therefore, Oo.copy takes an object with any methods (represented by the ellipsis), and returns an object of the same type. The type of Oo.copy is different from type < .. > -> < .. > as each ellipsis represents a different set of methods. Ellipsis actually behaves as a type variable. <<#let p = new point 5;; val p : point = >> <<#let q = Oo.copy p;; val q : point = >> <<#q#move 7; (p#get_x, q#get_x);; - : int * int = (5, 12) >> In fact, Oo.copy p will behave as p#copy assuming that a public method copy with body {< >} has been defined in the class of p. Objects can be compared using the generic comparison functions = and <>. Two objects are equal if and only if they are physically equal. In particular, an object and its copy are not equal. <<#let q = Oo.copy p;; val q : point = >> <<#p = q, p = p;; - : bool * bool = (false, true) >> Other generic comparisons such as (<, <=, ...) can also be used on objects. The relation < defines an unspecified but strict ordering on objects. The ordering relationship between two objects is fixed once for all after the two objects have been created and it is not affected by mutation of fields. Cloning and override have a non empty intersection. They are interchangeable when used within an object and without overriding any field: <<#class copy = # object # method copy = {< >} # end;; class copy : object ('a) method copy : 'a end >> <<#class copy = # object (self) # method copy = Oo.copy self # end;; class copy : object ('a) method copy : 'a end >> Only the override can be used to actually override fields, and only the Oo.copy primitive can be used externally. Cloning can also be used to provide facilities for saving and restoring the state of objects. <<#class backup = # object (self : 'mytype) # val mutable copy = None # method save = copy <- Some {< copy = None >} # method restore = match copy with Some x -> x | None -> self # end;; class backup : object ('a) val mutable copy : 'a option method restore : 'a method save : unit end >> The above definition will only backup one level. The backup facility can be added to any class by using multiple inheritance. <<#class ['a] backup_ref x = object inherit ['a] oref x inherit backup end;; class ['a] backup_ref : 'a -> object ('b) val mutable copy : 'b option val mutable x : 'a method get : 'a method restore : 'b method save : unit method set : 'a -> unit end >> <<#let rec get p n = if n = 0 then p # get else get (p # restore) (n-1);; val get : (< get : 'b; restore : 'a; .. > as 'a) -> int -> 'b = >> <<#let p = new backup_ref 0 in #p # save; p # set 1; p # save; p # set 2; #[get p 0; get p 1; get p 2; get p 3; get p 4];; - : int list = [2; 1; 1; 1; 1] >> We can define a variant of backup that retains all copies. (We also add a method clear to manually erase all copies.) <<#class backup = # object (self : 'mytype) # val mutable copy = None # method save = copy <- Some {< >} # method restore = match copy with Some x -> x | None -> self # method clear = copy <- None # end;; class backup : object ('a) val mutable copy : 'a option method clear : unit method restore : 'a method save : unit end >> <<#class ['a] backup_ref x = object inherit ['a] oref x inherit backup end;; class ['a] backup_ref : 'a -> object ('b) val mutable copy : 'b option val mutable x : 'a method clear : unit method get : 'a method restore : 'b method save : unit method set : 'a -> unit end >> <<#let p = new backup_ref 0 in #p # save; p # set 1; p # save; p # set 2; #[get p 0; get p 1; get p 2; get p 3; get p 4];; - : int list = [2; 1; 0; 0; 0] >> 3.15 Recursive classes *=*=*=*=*=*=*=*=*=*=*=* Recursive classes can be used to define objects whose types are mutually recursive. <<#class window = # object # val mutable top_widget = (None : widget option) # method top_widget = top_widget # end #and widget (w : window) = # object # val window = w # method window = window # end;; class window : object val mutable top_widget : widget option method top_widget : widget option end and widget : window -> object val window : window method window : window end >> Although their types are mutually recursive, the classes widget and window are themselves independent. 3.16 Binary methods *=*=*=*=*=*=*=*=*=*= A binary method is a method which takes an argument of the same type as self. The class comparable below is a template for classes with a binary method leq of type 'a -> bool where the type variable 'a is bound to the type of self. Therefore, #comparable expands to < leq : 'a -> bool; .. > as 'a. We see here that the binder as also allows writing recursive types. <<#class virtual comparable = # object (_ : 'a) # method virtual leq : 'a -> bool # end;; class virtual comparable : object ('a) method virtual leq : 'a -> bool end >> We then define a subclass money of comparable. The class money simply wraps floats as comparable objects. We will extend it below with more operations. We have to use a type constraint on the class parameter x because the primitive <= is a polymorphic function in OCaml. The inherit clause ensures that the type of objects of this class is an instance of #comparable. <<#class money (x : float) = # object # inherit comparable # val repr = x # method value = repr # method leq p = repr <= p#value # end;; class money : float -> object ('a) val repr : float method leq : 'a -> bool method value : float end >> Note that the type money is not a subtype of type comparable, as the self type appears in contravariant position in the type of method leq. Indeed, an object m of class money has a method leq that expects an argument of type money since it accesses its value method. Considering m of type comparable would allow a call to method leq on m with an argument that does not have a method value, which would be an error. Similarly, the type money2 below is not a subtype of type money. <<#class money2 x = # object # inherit money x # method times k = {< repr = k *. repr >} # end;; class money2 : float -> object ('a) val repr : float method leq : 'a -> bool method times : float -> 'a method value : float end >> It is however possible to define functions that manipulate objects of type either money or money2: the function min will return the minimum of any two objects whose type unifies with #comparable. The type of min is not the same as #comparable -> #comparable -> #comparable, as the abbreviation #comparable hides a type variable (an ellipsis). Each occurrence of this abbreviation generates a new variable. <<#let min (x : #comparable) y = # if x#leq y then x else y;; val min : (#comparable as 'a) -> 'a -> 'a = >> This function can be applied to objects of type money or money2. <<#(min (new money 1.3) (new money 3.1))#value;; - : float = 1.3 >> <<#(min (new money2 5.0) (new money2 3.14))#value;; - : float = 3.14 >> More examples of binary methods can be found in sections 5.2.1 and 5.2.3. Note the use of override for method times. Writing new money2 (k *. repr) instead of {< repr = k *. repr >} would not behave well with inheritance: in a subclass money3 of money2 the times method would return an object of class money2 but not of class money3 as would be expected. The class money could naturally carry another binary method. Here is a direct definition: <<#class money x = # object (self : 'a) # val repr = x # method value = repr # method print = print_float repr # method times k = {< repr = k *. x >} # method leq (p : 'a) = repr <= p#value # method plus (p : 'a) = {< repr = x +. p#value >} # end;; class money : float -> object ('a) val repr : float method leq : 'a -> bool method plus : 'a -> 'a method print : unit method times : float -> 'a method value : float end >> 3.17 Friends *=*=*=*=*=*=* The above class money reveals a problem that often occurs with binary methods. In order to interact with other objects of the same class, the representation of money objects must be revealed, using a method such as value. If we remove all binary methods (here plus and leq), the representation can easily be hidden inside objects by removing the method value as well. However, this is not possible as soon as some binary method requires access to the representation of objects of the same class (other than self). <<#class safe_money x = # object (self : 'a) # val repr = x # method print = print_float repr # method times k = {< repr = k *. x >} # end;; class safe_money : float -> object ('a) val repr : float method print : unit method times : float -> 'a end >> Here, the representation of the object is known only to a particular object. To make it available to other objects of the same class, we are forced to make it available to the whole world. However we can easily restrict the visibility of the representation using the module system. <<#module type MONEY = # sig # type t # class c : float -> # object ('a) # val repr : t # method value : t # method print : unit # method times : float -> 'a # method leq : 'a -> bool # method plus : 'a -> 'a # end # end;; >> <<#module Euro : MONEY = # struct # type t = float # class c x = # object (self : 'a) # val repr = x # method value = repr # method print = print_float repr # method times k = {< repr = k *. x >} # method leq (p : 'a) = repr <= p#value # method plus (p : 'a) = {< repr = x +. p#value >} # end # end;; >> Another example of friend functions may be found in section 5.2.3. These examples occur when a group of objects (here objects of the same class) and functions should see each others internal representation, while their representation should be hidden from the outside. The solution is always to define all friends in the same module, give access to the representation and use a signature constraint to make the representation abstract outside the module. Chapter 4 Labels and variants ******************************** (Chapter written by Jacques Garrigue) This chapter gives an overview of the new features in OCaml 3: labels, and polymorphic variants. 4.1 Labels *=*=*=*=*=* If you have a look at modules ending in Labels in the standard library, you will see that function types have annotations you did not have in the functions you defined yourself. <<#ListLabels.map;; - : f:('a -> 'b) -> 'a list -> 'b list = >> <<#StringLabels.sub;; - : string -> pos:int -> len:int -> string = >> Such annotations of the form name: are called labels. They are meant to document the code, allow more checking, and give more flexibility to function application. You can give such names to arguments in your programs, by prefixing them with a tilde ~. <<#let f ~x ~y = x - y;; val f : x:int -> y:int -> int = >> <<#let x = 3 and y = 2 in f ~x ~y;; - : int = 1 >> When you want to use distinct names for the variable and the label appearing in the type, you can use a naming label of the form ~name:. This also applies when the argument is not a variable. <<#let f ~x:x1 ~y:y1 = x1 - y1;; val f : x:int -> y:int -> int = >> <<#f ~x:3 ~y:2;; - : int = 1 >> Labels obey the same rules as other identifiers in OCaml, that is you cannot use a reserved keyword (like in or to) as label. Formal parameters and arguments are matched according to their respective labels (1), the absence of label being interpreted as the empty label. This allows commuting arguments in applications. One can also partially apply a function on any argument, creating a new function of the remaining parameters. <<#let f ~x ~y = x - y;; val f : x:int -> y:int -> int = >> <<#f ~y:2 ~x:3;; - : int = 1 >> <<#ListLabels.fold_left;; - : f:('a -> 'b -> 'a) -> init:'a -> 'b list -> 'a = >> <<#ListLabels.fold_left [1;2;3] ~init:0 ~f:( + );; - : int = 6 >> <<#ListLabels.fold_left ~init:0;; - : f:(int -> 'a -> int) -> 'a list -> int = >> If several arguments of a function bear the same label (or no label), they will not commute among themselves, and order matters. But they can still commute with other arguments. <<#let hline ~x:x1 ~x:x2 ~y = (x1, x2, y);; val hline : x:'a -> x:'b -> y:'c -> 'a * 'b * 'c = >> <<#hline ~x:3 ~y:2 ~x:5;; - : int * int * int = (3, 5, 2) >> As an exception to the above parameter matching rules, if an application is total (omitting all optional arguments), labels may be omitted. In practice, many applications are total, so that labels can often be omitted. <<#f 3 2;; - : int = 1 >> <<#ListLabels.map succ [1;2;3];; - : int list = [2; 3; 4] >> But beware that functions like ListLabels.fold_left whose result type is a type variable will never be considered as totally applied. <<#ListLabels.fold_left ( + ) 0 [1;2;3];; Error: This expression has type int -> int -> int but an expression was expected of type 'a list >> When a function is passed as an argument to a higher-order function, labels must match in both types. Neither adding nor removing labels are allowed. <<#let h g = g ~x:3 ~y:2;; val h : (x:int -> y:int -> 'a) -> 'a = >> <<#h f;; - : int = 1 >> <<#h ( + ) ;; Error: This expression has type int -> int -> int but an expression was expected of type x:int -> y:int -> 'a >> Note that when you don't need an argument, you can still use a wildcard pattern, but you must prefix it with the label. <<#h (fun ~x:_ ~y -> y+1);; - : int = 3 >> 4.1.1 Optional arguments ========================= An interesting feature of labeled arguments is that they can be made optional. For optional parameters, the question mark ? replaces the tilde ~ of non-optional ones, and the label is also prefixed by ? in the function type. Default values may be given for such optional parameters. <<#let bump ?(step = 1) x = x + step;; val bump : ?step:int -> int -> int = >> <<#bump 2;; - : int = 3 >> <<#bump ~step:3 2;; - : int = 5 >> A function taking some optional arguments must also take at least one non-optional argument. The criterion for deciding whether an optional argument has been omitted is the non-labeled application of an argument appearing after this optional argument in the function type. Note that if that argument is labeled, you will only be able to eliminate optional arguments through the special case for total applications. <<#let test ?(x = 0) ?(y = 0) () ?(z = 0) () = (x, y, z);; val test : ?x:int -> ?y:int -> unit -> ?z:int -> unit -> int * int * int = >> <<#test ();; - : ?z:int -> unit -> int * int * int = >> <<#test ~x:2 () ~z:3 ();; - : int * int * int = (2, 0, 3) >> Optional parameters may also commute with non-optional or unlabeled ones, as long as they are applied simultaneously. By nature, optional arguments do not commute with unlabeled arguments applied independently. <<#test ~y:2 ~x:3 () ();; - : int * int * int = (3, 2, 0) >> <<#test () () ~z:1 ~y:2 ~x:3;; - : int * int * int = (3, 2, 1) >> <<#(test () ()) ~z:1 ;; Error: This expression has type int * int * int This is not a function; it cannot be applied. >> Here (test () ()) is already (0,0,0) and cannot be further applied. Optional arguments are actually implemented as option types. If you do not give a default value, you have access to their internal representation, type 'a option = None | Some of 'a. You can then provide different behaviors when an argument is present or not. <<#let bump ?step x = # match step with # | None -> x * 2 # | Some y -> x + y #;; val bump : ?step:int -> int -> int = >> It may also be useful to relay an optional argument from a function call to another. This can be done by prefixing the applied argument with ?. This question mark disables the wrapping of optional argument in an option type. <<#let test2 ?x ?y () = test ?x ?y () ();; val test2 : ?x:int -> ?y:int -> unit -> int * int * int = >> <<#test2 ?x:None;; - : ?y:int -> unit -> int * int * int = >> 4.1.2 Labels and type inference ================================ While they provide an increased comfort for writing function applications, labels and optional arguments have the pitfall that they cannot be inferred as completely as the rest of the language. You can see it in the following two examples. <<#let h' g = g ~y:2 ~x:3;; val h' : (y:int -> x:int -> 'a) -> 'a = >> <<#h' f ;; Error: This expression has type x:int -> y:int -> int but an expression was expected of type y:int -> x:int -> 'a >> <<#let bump_it bump x = # bump ~step:2 x;; val bump_it : (step:int -> 'a -> 'b) -> 'a -> 'b = >> <<#bump_it bump 1 ;; Error: This expression has type ?step:int -> int -> int but an expression was expected of type step:int -> 'a -> 'b >> The first case is simple: g is passed ~y and then ~x, but f expects ~x and then ~y. This is correctly handled if we know the type of g to be x:int -> y:int -> int in advance, but otherwise this causes the above type clash. The simplest workaround is to apply formal parameters in a standard order. The second example is more subtle: while we intended the argument bump to be of type ?step:int -> int -> int, it is inferred as step:int -> int -> 'a. These two types being incompatible (internally normal and optional arguments are different), a type error occurs when applying bump_it to the real bump. We will not try here to explain in detail how type inference works. One must just understand that there is not enough information in the above program to deduce the correct type of g or bump. That is, there is no way to know whether an argument is optional or not, or which is the correct order, by looking only at how a function is applied. The strategy used by the compiler is to assume that there are no optional arguments, and that applications are done in the right order. The right way to solve this problem for optional parameters is to add a type annotation to the argument bump. <<#let bump_it (bump : ?step:int -> int -> int) x = # bump ~step:2 x;; val bump_it : (?step:int -> int -> int) -> int -> int = >> <<#bump_it bump 1;; - : int = 3 >> In practice, such problems appear mostly when using objects whose methods have optional arguments, so that writing the type of object arguments is often a good idea. Normally the compiler generates a type error if you attempt to pass to a function a parameter whose type is different from the expected one. However, in the specific case where the expected type is a non-labeled function type, and the argument is a function expecting optional parameters, the compiler will attempt to transform the argument to have it match the expected type, by passing None for all optional parameters. <<#let twice f (x : int) = f(f x);; val twice : (int -> int) -> int -> int = >> <<#twice bump 2;; - : int = 8 >> This transformation is coherent with the intended semantics, including side-effects. That is, if the application of optional parameters shall produce side-effects, these are delayed until the received function is really applied to an argument. 4.1.3 Suggestions for labeling =============================== Like for names, choosing labels for functions is not an easy task. A good labeling is a labeling which - makes programs more readable, - is easy to remember, - when possible, allows useful partial applications. We explain here the rules we applied when labeling OCaml libraries. To speak in an "object-oriented" way, one can consider that each function has a main argument, its object, and other arguments related with its action, the parameters. To permit the combination of functions through functionals in commuting label mode, the object will not be labeled. Its role is clear from the function itself. The parameters are labeled with names reminding of their nature or their role. The best labels combine nature and role. When this is not possible the role is to be preferred, since the nature will often be given by the type itself. Obscure abbreviations should be avoided. << ListLabels.map : f:('a -> 'b) -> 'a list -> 'b list UnixLabels.write : file_descr -> buf:bytes -> pos:int -> len:int -> unit >> When there are several objects of same nature and role, they are all left unlabeled. << ListLabels.iter2 : f:('a -> 'b -> 'c) -> 'a list -> 'b list -> unit >> When there is no preferable object, all arguments are labeled. << BytesLabels.blit : src:bytes -> src_pos:int -> dst:bytes -> dst_pos:int -> len:int -> unit >> However, when there is only one argument, it is often left unlabeled. << BytesLabels.create : int -> bytes >> This principle also applies to functions of several arguments whose return type is a type variable, as long as the role of each argument is not ambiguous. Labeling such functions may lead to awkward error messages when one attempts to omit labels in an application, as we have seen with ListLabels.fold_left. Here are some of the label names you will find throughout the libraries. ------------------------------------------------------- |Label| Meaning | ------------------------------------------------------- | f: |a function to be applied | |pos: |a position in a string, array or byte sequence | |len: |a length | |buf: |a byte sequence or string used as buffer | |src: |the source of an operation | |dst: |the destination of an operation | |init:|the initial value for an iterator | |cmp: |a comparison function, e.g. Pervasives.compare | |mode:|an operation mode or a flag list | ------------------------------------------------------- All these are only suggestions, but keep in mind that the choice of labels is essential for readability. Bizarre choices will make the program harder to maintain. In the ideal, the right function name with right labels should be enough to understand the function's meaning. Since one can get this information with OCamlBrowser or the ocaml toplevel, the documentation is only used when a more detailed specification is needed. 4.2 Polymorphic variants *=*=*=*=*=*=*=*=*=*=*=*=* Variants as presented in section 1.4 are a powerful tool to build data structures and algorithms. However they sometimes lack flexibility when used in modular programming. This is due to the fact that every constructor is assigned to an unique type when defined and used. Even if the same name appears in the definition of multiple types, the constructor itself belongs to only one type. Therefore, one cannot decide that a given constructor belongs to multiple types, or consider a value of some type to belong to some other type with more constructors. With polymorphic variants, this original assumption is removed. That is, a variant tag does not belong to any type in particular, the type system will just check that it is an admissible value according to its use. You need not define a type before using a variant tag. A variant type will be inferred independently for each of its uses. Basic use ========= In programs, polymorphic variants work like usual ones. You just have to prefix their names with a backquote character `. <<#[`On; `Off];; - : [> `Off | `On ] list = [`On; `Off] >> <<#`Number 1;; - : [> `Number of int ] = `Number 1 >> <<#let f = function `On -> 1 | `Off -> 0 | `Number n -> n;; val f : [< `Number of int | `Off | `On ] -> int = >> <<#List.map f [`On; `Off];; - : int list = [1; 0] >> [>`Off|`On] list means that to match this list, you should at least be able to match `Off and `On, without argument. [<`On|`Off|`Number of int] means that f may be applied to `Off, `On (both without argument), or `Number n where n is an integer. The > and < inside the variant types show that they may still be refined, either by defining more tags or by allowing less. As such, they contain an implicit type variable. Because each of the variant types appears only once in the whole type, their implicit type variables are not shown. The above variant types were polymorphic, allowing further refinement. When writing type annotations, one will most often describe fixed variant types, that is types that cannot be refined. This is also the case for type abbreviations. Such types do not contain < or >, but just an enumeration of the tags and their associated types, just like in a normal datatype definition. <<#type 'a vlist = [`Nil | `Cons of 'a * 'a vlist];; type 'a vlist = [ `Cons of 'a * 'a vlist | `Nil ] >> <<#let rec map f : 'a vlist -> 'b vlist = function # | `Nil -> `Nil # | `Cons(a, l) -> `Cons(f a, map f l) #;; val map : ('a -> 'b) -> 'a vlist -> 'b vlist = >> Advanced use ============ Type-checking polymorphic variants is a subtle thing, and some expressions may result in more complex type information. <<#let f = function `A -> `C | `B -> `D | x -> x;; val f : ([> `A | `B | `C | `D ] as 'a) -> 'a = >> <<#f `E;; - : [> `A | `B | `C | `D | `E ] = `E >> Here we are seeing two phenomena. First, since this matching is open (the last case catches any tag), we obtain the type [> `A | `B] rather than [< `A | `B] in a closed matching. Then, since x is returned as is, input and return types are identical. The notation as 'a denotes such type sharing. If we apply f to yet another tag `E, it gets added to the list. <<#let f1 = function `A x -> x = 1 | `B -> true | `C -> false #let f2 = function `A x -> x = "a" | `B -> true ;; val f1 : [< `A of int | `B | `C ] -> bool = val f2 : [< `A of string | `B ] -> bool = >> <<#let f x = f1 x && f2 x;; val f : [< `A of string & int | `B ] -> bool = >> Here f1 and f2 both accept the variant tags `A and `B, but the argument of `A is int for f1 and string for f2. In f's type `C, only accepted by f1, disappears, but both argument types appear for `A as int & string. This means that if we pass the variant tag `A to f, its argument should be both int and string. Since there is no such value, f cannot be applied to `A, and `B is the only accepted input. Even if a value has a fixed variant type, one can still give it a larger type through coercions. Coercions are normally written with both the source type and the destination type, but in simple cases the source type may be omitted. <<#type 'a wlist = [`Nil | `Cons of 'a * 'a wlist | `Snoc of 'a wlist * 'a];; type 'a wlist = [ `Cons of 'a * 'a wlist | `Nil | `Snoc of 'a wlist * 'a ] >> <<#let wlist_of_vlist l = (l : 'a vlist :> 'a wlist);; val wlist_of_vlist : 'a vlist -> 'a wlist = >> <<#let open_vlist l = (l : 'a vlist :> [> 'a vlist]);; val open_vlist : 'a vlist -> [> 'a vlist ] = >> <<#fun x -> (x :> [`A|`B|`C]);; - : [< `A | `B | `C ] -> [ `A | `B | `C ] = >> You may also selectively coerce values through pattern matching. <<#let split_cases = function # | `Nil | `Cons _ as x -> `A x # | `Snoc _ as x -> `B x #;; val split_cases : [< `Cons of 'a | `Nil | `Snoc of 'b ] -> [> `A of [> `Cons of 'a | `Nil ] | `B of [> `Snoc of 'b ] ] = >> When an or-pattern composed of variant tags is wrapped inside an alias-pattern, the alias is given a type containing only the tags enumerated in the or-pattern. This allows for many useful idioms, like incremental definition of functions. <<#let num x = `Num x #let eval1 eval (`Num x) = x #let rec eval x = eval1 eval x ;; val num : 'a -> [> `Num of 'a ] = val eval1 : 'a -> [< `Num of 'b ] -> 'b = val eval : [< `Num of 'a ] -> 'a = >> <<#let plus x y = `Plus(x,y) #let eval2 eval = function # | `Plus(x,y) -> eval x + eval y # | `Num _ as x -> eval1 eval x #let rec eval x = eval2 eval x ;; val plus : 'a -> 'b -> [> `Plus of 'a * 'b ] = val eval2 : ('a -> int) -> [< `Num of int | `Plus of 'a * 'a ] -> int = val eval : ([< `Num of int | `Plus of 'a * 'a ] as 'a) -> int = >> To make this even more comfortable, you may use type definitions as abbreviations for or-patterns. That is, if you have defined type myvariant = [`Tag1 of int | `Tag2 of bool], then the pattern #myvariant is equivalent to writing (`Tag1(_ : int) | `Tag2(_ : bool)). Such abbreviations may be used alone, <<#let f = function # | #myvariant -> "myvariant" # | `Tag3 -> "Tag3";; val f : [< `Tag1 of int | `Tag2 of bool | `Tag3 ] -> string = >> or combined with with aliases. <<#let g1 = function `Tag1 _ -> "Tag1" | `Tag2 _ -> "Tag2";; val g1 : [< `Tag1 of 'a | `Tag2 of 'b ] -> string = >> <<#let g = function # | #myvariant as x -> g1 x # | `Tag3 -> "Tag3";; val g : [< `Tag1 of int | `Tag2 of bool | `Tag3 ] -> string = >> 4.2.1 Weaknesses of polymorphic variants ========================================= After seeing the power of polymorphic variants, one may wonder why they were added to core language variants, rather than replacing them. The answer is twofold. One first aspect is that while being pretty efficient, the lack of static type information allows for less optimizations, and makes polymorphic variants slightly heavier than core language ones. However noticeable differences would only appear on huge data structures. More important is the fact that polymorphic variants, while being type-safe, result in a weaker type discipline. That is, core language variants do actually much more than ensuring type-safety, they also check that you use only declared constructors, that all constructors present in a data-structure are compatible, and they enforce typing constraints to their parameters. For this reason, you must be more careful about making types explicit when you use polymorphic variants. When you write a library, this is easy since you can describe exact types in interfaces, but for simple programs you are probably better off with core language variants. Beware also that some idioms make trivial errors very hard to find. For instance, the following code is probably wrong but the compiler has no way to see it. <<#type abc = [`A | `B | `C] ;; type abc = [ `A | `B | `C ] >> <<#let f = function # | `As -> "A" # | #abc -> "other" ;; val f : [< `A | `As | `B | `C ] -> string = >> <<#let f : abc -> string = f ;; val f : abc -> string = >> You can avoid such risks by annotating the definition itself. <<#let f : abc -> string = function # | `As -> "A" # | #abc -> "other" ;; Error: This pattern matches values of type [? `As ] but a pattern was expected which matches values of type abc The second variant type does not allow tag(s) `As >> --------------------------------------- (1) This correspond to the commuting label mode of Objective Caml 3.00 through 3.02, with some additional flexibility on total applications. The so-called classic mode (-nolabels options) is now deprecated for normal use. Chapter 5 Advanced examples with classes and modules ******************************************************* (Chapter written by Didier Rémy) In this chapter, we show some larger examples using objects, classes and modules. We review many of the object features simultaneously on the example of a bank account. We show how modules taken from the standard library can be expressed as classes. Lastly, we describe a programming pattern know of as virtual types through the example of window managers. 5.1 Extended example: bank accounts *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= In this section, we illustrate most aspects of Object and inheritance by refining, debugging, and specializing the following initial naive definition of a simple bank account. (We reuse the module Euro defined at the end of chapter 3.) <<#let euro = new Euro.c;; val euro : float -> Euro.c = >> <<#let zero = euro 0.;; val zero : Euro.c = >> <<#let neg x = x#times (-1.);; val neg : < times : float -> 'a; .. > -> 'a = >> <<#class account = # object # val mutable balance = zero # method balance = balance # method deposit x = balance <- balance # plus x # method withdraw x = # if x#leq balance then (balance <- balance # plus (neg x); x) else zero # end;; class account : object val mutable balance : Euro.c method balance : Euro.c method deposit : Euro.c -> unit method withdraw : Euro.c -> Euro.c end >> <<#let c = new account in c # deposit (euro 100.); c # withdraw (euro 50.);; - : Euro.c = >> We now refine this definition with a method to compute interest. <<#class account_with_interests = # object (self) # inherit account # method private interest = self # deposit (self # balance # times 0.03) # end;; class account_with_interests : object val mutable balance : Euro.c method balance : Euro.c method deposit : Euro.c -> unit method private interest : unit method withdraw : Euro.c -> Euro.c end >> We make the method interest private, since clearly it should not be called freely from the outside. Here, it is only made accessible to subclasses that will manage monthly or yearly updates of the account. We should soon fix a bug in the current definition: the deposit method can be used for withdrawing money by depositing negative amounts. We can fix this directly: <<#class safe_account = # object # inherit account # method deposit x = if zero#leq x then balance <- balance#plus x # end;; class safe_account : object val mutable balance : Euro.c method balance : Euro.c method deposit : Euro.c -> unit method withdraw : Euro.c -> Euro.c end >> However, the bug might be fixed more safely by the following definition: <<#class safe_account = # object # inherit account as unsafe # method deposit x = # if zero#leq x then unsafe # deposit x # else raise (Invalid_argument "deposit") # end;; class safe_account : object val mutable balance : Euro.c method balance : Euro.c method deposit : Euro.c -> unit method withdraw : Euro.c -> Euro.c end >> In particular, this does not require the knowledge of the implementation of the method deposit. To keep track of operations, we extend the class with a mutable field history and a private method trace to add an operation in the log. Then each method to be traced is redefined. <<#type 'a operation = Deposit of 'a | Retrieval of 'a;; type 'a operation = Deposit of 'a | Retrieval of 'a >> <<#class account_with_history = # object (self) # inherit safe_account as super # val mutable history = [] # method private trace x = history <- x :: history # method deposit x = self#trace (Deposit x); super#deposit x # method withdraw x = self#trace (Retrieval x); super#withdraw x # method history = List.rev history # end;; class account_with_history : object val mutable balance : Euro.c val mutable history : Euro.c operation list method balance : Euro.c method deposit : Euro.c -> unit method history : Euro.c operation list method private trace : Euro.c operation -> unit method withdraw : Euro.c -> Euro.c end >> One may wish to open an account and simultaneously deposit some initial amount. Although the initial implementation did not address this requirement, it can be achieved by using an initializer. <<#class account_with_deposit x = # object # inherit account_with_history # initializer balance <- x # end;; class account_with_deposit : Euro.c -> object val mutable balance : Euro.c val mutable history : Euro.c operation list method balance : Euro.c method deposit : Euro.c -> unit method history : Euro.c operation list method private trace : Euro.c operation -> unit method withdraw : Euro.c -> Euro.c end >> A better alternative is: <<#class account_with_deposit x = # object (self) # inherit account_with_history # initializer self#deposit x # end;; class account_with_deposit : Euro.c -> object val mutable balance : Euro.c val mutable history : Euro.c operation list method balance : Euro.c method deposit : Euro.c -> unit method history : Euro.c operation list method private trace : Euro.c operation -> unit method withdraw : Euro.c -> Euro.c end >> Indeed, the latter is safer since the call to deposit will automatically benefit from safety checks and from the trace. Let's test it: <<#let ccp = new account_with_deposit (euro 100.) in #let _balance = ccp#withdraw (euro 50.) in #ccp#history;; - : Euro.c operation list = [Deposit ; Retrieval ] >> Closing an account can be done with the following polymorphic function: <<#let close c = c#withdraw c#balance;; val close : < balance : 'a; withdraw : 'a -> 'b; .. > -> 'b = >> Of course, this applies to all sorts of accounts. Finally, we gather several versions of the account into a module Account abstracted over some currency. <<#let today () = (01,01,2000) (* an approximation *) #module Account (M:MONEY) = # struct # type m = M.c # let m = new M.c # let zero = m 0. # class bank = # object (self) # val mutable balance = zero # method balance = balance # val mutable history = [] # method private trace x = history <- x::history # method deposit x = # self#trace (Deposit x); # if zero#leq x then balance <- balance # plus x # else raise (Invalid_argument "deposit") # method withdraw x = # if x#leq balance then # (balance <- balance # plus (neg x); self#trace (Retrieval x); x) # else zero # method history = List.rev history # end # class type client_view = # object # method deposit : m -> unit # method history : m operation list # method withdraw : m -> m # method balance : m # end # class virtual check_client x = # let y = if (m 100.)#leq x then x # else raise (Failure "Insufficient initial deposit") in # object (self) # initializer self#deposit y # method virtual deposit: m -> unit # end # module Client (B : sig class bank : client_view end) = # struct # class account x : client_view = # object # inherit B.bank # inherit check_client x # end # let discount x = # let c = new account x in # if today() < (1998,10,30) then c # deposit (m 100.); c # end # end;; >> This shows the use of modules to group several class definitions that can in fact be thought of as a single unit. This unit would be provided by a bank for both internal and external uses. This is implemented as a functor that abstracts over the currency so that the same code can be used to provide accounts in different currencies. The class bank is the real implementation of the bank account (it could have been inlined). This is the one that will be used for further extensions, refinements, etc. Conversely, the client will only be given the client view. <<#module Euro_account = Account(Euro);; >> <<#module Client = Euro_account.Client (Euro_account);; >> <<#new Client.account (new Euro.c 100.);; >> Hence, the clients do not have direct access to the balance, nor the history of their own accounts. Their only way to change their balance is to deposit or withdraw money. It is important to give the clients a class and not just the ability to create accounts (such as the promotional discount account), so that they can personalize their account. For instance, a client may refine the deposit and withdraw methods so as to do his own financial bookkeeping, automatically. On the other hand, the function discount is given as such, with no possibility for further personalization. It is important to provide the client's view as a functor Client so that client accounts can still be built after a possible specialization of the bank. The functor Client may remain unchanged and be passed the new definition to initialize a client's view of the extended account. <<#module Investment_account (M : MONEY) = # struct # type m = M.c # module A = Account(M) # class bank = # object # inherit A.bank as super # method deposit x = # if (new M.c 1000.)#leq x then # print_string "Would you like to invest?"; # super#deposit x # end # module Client = A.Client # end;; >> The functor Client may also be redefined when some new features of the account can be given to the client. <<#module Internet_account (M : MONEY) = # struct # type m = M.c # module A = Account(M) # class bank = # object # inherit A.bank # method mail s = print_string s # end # class type client_view = # object # method deposit : m -> unit # method history : m operation list # method withdraw : m -> m # method balance : m # method mail : string -> unit # end # module Client (B : sig class bank : client_view end) = # struct # class account x : client_view = # object # inherit B.bank # inherit A.check_client x # end # end # end;; >> 5.2 Simple modules as classes *=*=*=*=*=*=*=*=*=*=*=*=*=*=*= One may wonder whether it is possible to treat primitive types such as integers and strings as objects. Although this is usually uninteresting for integers or strings, there may be some situations where this is desirable. The class money above is such an example. We show here how to do it for strings. 5.2.1 Strings ============== A naive definition of strings as objects could be: <<#class ostring s = # object # method get n = String.get s n # method print = print_string s # method escaped = new ostring (String.escaped s) # end;; class ostring : string -> object method escaped : ostring method get : int -> char method print : unit end >> However, the method escaped returns an object of the class ostring, and not an object of the current class. Hence, if the class is further extended, the method escaped will only return an object of the parent class. <<#class sub_string s = # object # inherit ostring s # method sub start len = new sub_string (String.sub s start len) # end;; class sub_string : string -> object method escaped : ostring method get : int -> char method print : unit method sub : int -> int -> sub_string end >> As seen in section 3.16, the solution is to use functional update instead. We need to create an instance variable containing the representation s of the string. <<#class better_string s = # object # val repr = s # method get n = String.get repr n # method print = print_string repr # method escaped = {< repr = String.escaped repr >} # method sub start len = {< repr = String.sub s start len >} # end;; class better_string : string -> object ('a) val repr : string method escaped : 'a method get : int -> char method print : unit method sub : int -> int -> 'a end >> As shown in the inferred type, the methods escaped and sub now return objects of the same type as the one of the class. Another difficulty is the implementation of the method concat. In order to concatenate a string with another string of the same class, one must be able to access the instance variable externally. Thus, a method repr returning s must be defined. Here is the correct definition of strings: <<#class ostring s = # object (self : 'mytype) # val repr = s # method repr = repr # method get n = String.get repr n # method print = print_string repr # method escaped = {< repr = String.escaped repr >} # method sub start len = {< repr = String.sub s start len >} # method concat (t : 'mytype) = {< repr = repr ^ t#repr >} # end;; class ostring : string -> object ('a) val repr : string method concat : 'a -> 'a method escaped : 'a method get : int -> char method print : unit method repr : string method sub : int -> int -> 'a end >> Another constructor of the class string can be defined to return a new string of a given length: <<#class cstring n = ostring (String.make n ' ');; class cstring : int -> ostring >> Here, exposing the representation of strings is probably harmless. We do could also hide the representation of strings as we hid the currency in the class money of section 3.17. Stacks ------ There is sometimes an alternative between using modules or classes for parametric data types. Indeed, there are situations when the two approaches are quite similar. For instance, a stack can be straightforwardly implemented as a class: <<#exception Empty;; exception Empty >> <<#class ['a] stack = # object # val mutable l = ([] : 'a list) # method push x = l <- x::l # method pop = match l with [] -> raise Empty | a::l' -> l <- l'; a # method clear = l <- [] # method length = List.length l # end;; class ['a] stack : object val mutable l : 'a list method clear : unit method length : int method pop : 'a method push : 'a -> unit end >> However, writing a method for iterating over a stack is more problematic. A method fold would have type ('b -> 'a -> 'b) -> 'b -> 'b. Here 'a is the parameter of the stack. The parameter 'b is not related to the class 'a stack but to the argument that will be passed to the method fold. A naive approach is to make 'b an extra parameter of class stack: <<#class ['a, 'b] stack2 = # object # inherit ['a] stack # method fold f (x : 'b) = List.fold_left f x l # end;; class ['a, 'b] stack2 : object val mutable l : 'a list method clear : unit method fold : ('b -> 'a -> 'b) -> 'b -> 'b method length : int method pop : 'a method push : 'a -> unit end >> However, the method fold of a given object can only be applied to functions that all have the same type: <<#let s = new stack2;; val s : ('_a, '_b) stack2 = >> <<#s#fold ( + ) 0;; - : int = 0 >> <<#s;; - : (int, int) stack2 = >> A better solution is to use polymorphic methods, which were introduced in OCaml version 3.05. Polymorphic methods makes it possible to treat the type variable 'b in the type of fold as universally quantified, giving fold the polymorphic type Forall 'b. ('b -> 'a -> 'b) -> 'b -> 'b. An explicit type declaration on the method fold is required, since the type checker cannot infer the polymorphic type by itself. <<#class ['a] stack3 = # object # inherit ['a] stack # method fold : 'b. ('b -> 'a -> 'b) -> 'b -> 'b # = fun f x -> List.fold_left f x l # end;; class ['a] stack3 : object val mutable l : 'a list method clear : unit method fold : ('b -> 'a -> 'b) -> 'b -> 'b method length : int method pop : 'a method push : 'a -> unit end >> 5.2.2 Hashtbl ============== A simplified version of object-oriented hash tables should have the following class type. <<#class type ['a, 'b] hash_table = # object # method find : 'a -> 'b # method add : 'a -> 'b -> unit # end;; class type ['a, 'b] hash_table = object method add : 'a -> 'b -> unit method find : 'a -> 'b end >> A simple implementation, which is quite reasonable for small hash tables is to use an association list: <<#class ['a, 'b] small_hashtbl : ['a, 'b] hash_table = # object # val mutable table = [] # method find key = List.assoc key table # method add key valeur = table <- (key, valeur) :: table # end;; class ['a, 'b] small_hashtbl : ['a, 'b] hash_table >> A better implementation, and one that scales up better, is to use a true hash table... whose elements are small hash tables! <<#class ['a, 'b] hashtbl size : ['a, 'b] hash_table = # object (self) # val table = Array.init size (fun i -> new small_hashtbl) # method private hash key = # (Hashtbl.hash key) mod (Array.length table) # method find key = table.(self#hash key) # find key # method add key = table.(self#hash key) # add key # end;; class ['a, 'b] hashtbl : int -> ['a, 'b] hash_table >> 5.2.3 Sets =========== Implementing sets leads to another difficulty. Indeed, the method union needs to be able to access the internal representation of another object of the same class. This is another instance of friend functions as seen in section 3.17. Indeed, this is the same mechanism used in the module Set in the absence of objects. In the object-oriented version of sets, we only need to add an additional method tag to return the representation of a set. Since sets are parametric in the type of elements, the method tag has a parametric type 'a tag, concrete within the module definition but abstract in its signature. From outside, it will then be guaranteed that two objects with a method tag of the same type will share the same representation. <<#module type SET = # sig # type 'a tag # class ['a] c : # object ('b) # method is_empty : bool # method mem : 'a -> bool # method add : 'a -> 'b # method union : 'b -> 'b # method iter : ('a -> unit) -> unit # method tag : 'a tag # end # end;; >> <<#module Set : SET = # struct # let rec merge l1 l2 = # match l1 with # [] -> l2 # | h1 :: t1 -> # match l2 with # [] -> l1 # | h2 :: t2 -> # if h1 < h2 then h1 :: merge t1 l2 # else if h1 > h2 then h2 :: merge l1 t2 # else merge t1 l2 # type 'a tag = 'a list # class ['a] c = # object (_ : 'b) # val repr = ([] : 'a list) # method is_empty = (repr = []) # method mem x = List.exists (( = ) x) repr # method add x = {< repr = merge [x] repr >} # method union (s : 'b) = {< repr = merge repr s#tag >} # method iter (f : 'a -> unit) = List.iter f repr # method tag = repr # end # end;; >> 5.3 The subject/observer pattern *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* The following example, known as the subject/observer pattern, is often presented in the literature as a difficult inheritance problem with inter-connected classes. The general pattern amounts to the definition a pair of two classes that recursively interact with one another. The class observer has a distinguished method notify that requires two arguments, a subject and an event to execute an action. <<#class virtual ['subject, 'event] observer = # object # method virtual notify : 'subject -> 'event -> unit # end;; class virtual ['subject, 'event] observer : object method virtual notify : 'subject -> 'event -> unit end >> The class subject remembers a list of observers in an instance variable, and has a distinguished method notify_observers to broadcast the message notify to all observers with a particular event e. <<#class ['observer, 'event] subject = # object (self) # val mutable observers = ([]:'observer list) # method add_observer obs = observers <- (obs :: observers) # method notify_observers (e : 'event) = # List.iter (fun x -> x#notify self e) observers # end;; class ['a, 'event] subject : object ('b) constraint 'a = < notify : 'b -> 'event -> unit; .. > val mutable observers : 'a list method add_observer : 'a -> unit method notify_observers : 'event -> unit end >> The difficulty usually lies in defining instances of the pattern above by inheritance. This can be done in a natural and obvious manner in OCaml, as shown on the following example manipulating windows. <<#type event = Raise | Resize | Move;; type event = Raise | Resize | Move >> <<#let string_of_event = function # Raise -> "Raise" | Resize -> "Resize" | Move -> "Move";; val string_of_event : event -> string = >> <<#let count = ref 0;; val count : int ref = {contents = 0} >> <<#class ['observer] window_subject = # let id = count := succ !count; !count in # object (self) # inherit ['observer, event] subject # val mutable position = 0 # method identity = id # method move x = position <- position + x; self#notify_observers Move # method draw = Printf.printf "{Position = %d}\n" position; # end;; class ['a] window_subject : object ('b) constraint 'a = < notify : 'b -> event -> unit; .. > val mutable observers : 'a list val mutable position : int method add_observer : 'a -> unit method draw : unit method identity : int method move : int -> unit method notify_observers : event -> unit end >> <<#class ['subject] window_observer = # object # inherit ['subject, event] observer # method notify s e = s#draw # end;; class ['a] window_observer : object constraint 'a = < draw : unit; .. > method notify : 'a -> event -> unit end >> As can be expected, the type of window is recursive. <<#let window = new window_subject;; val window : < notify : 'a -> event -> unit; _.. > window_subject as 'a = >> However, the two classes of window_subject and window_observer are not mutually recursive. <<#let window_observer = new window_observer;; val window_observer : < draw : unit; _.. > window_observer = >> <<#window#add_observer window_observer;; - : unit = () >> <<#window#move 1;; {Position = 1} - : unit = () >> Classes window_observer and window_subject can still be extended by inheritance. For instance, one may enrich the subject with new behaviors and refine the behavior of the observer. <<#class ['observer] richer_window_subject = # object (self) # inherit ['observer] window_subject # val mutable size = 1 # method resize x = size <- size + x; self#notify_observers Resize # val mutable top = false # method raise = top <- true; self#notify_observers Raise # method draw = Printf.printf "{Position = %d; Size = %d}\n" position size; # end;; class ['a] richer_window_subject : object ('b) constraint 'a = < notify : 'b -> event -> unit; .. > val mutable observers : 'a list val mutable position : int val mutable size : int val mutable top : bool method add_observer : 'a -> unit method draw : unit method identity : int method move : int -> unit method notify_observers : event -> unit method raise : unit method resize : int -> unit end >> <<#class ['subject] richer_window_observer = # object # inherit ['subject] window_observer as super # method notify s e = if e <> Raise then s#raise; super#notify s e # end;; class ['a] richer_window_observer : object constraint 'a = < draw : unit; raise : unit; .. > method notify : 'a -> event -> unit end >> We can also create a different kind of observer: <<#class ['subject] trace_observer = # object # inherit ['subject, event] observer # method notify s e = # Printf.printf # "\n" s#identity (string_of_event e) # end;; class ['a] trace_observer : object constraint 'a = < identity : int; .. > method notify : 'a -> event -> unit end >> and attach several observers to the same object: <<#let window = new richer_window_subject;; val window : < notify : 'a -> event -> unit; _.. > richer_window_subject as 'a = >> <<#window#add_observer (new richer_window_observer);; - : unit = () >> <<#window#add_observer (new trace_observer);; - : unit = () >> <<#window#move 1; window#resize 2;; {Position = 1; Size = 1} {Position = 1; Size = 1} {Position = 1; Size = 3} {Position = 1; Size = 3} - : unit = () >> Part: II ******** The OCaml language ****************** Chapter 6 The OCaml language ******************************* Foreword ======== This document is intended as a reference manual for the OCaml language. It lists the language constructs, and gives their precise syntax and informal semantics. It is by no means a tutorial introduction to the language: there is not a single example. A good working knowledge of OCaml is assumed. No attempt has been made at mathematical rigor: words are employed with their intuitive meaning, without further definition. As a consequence, the typing rules have been left out, by lack of the mathematical framework required to express them, while they are definitely part of a full formal definition of the language. Notations ========= The syntax of the language is given in BNF-like notation. Terminal symbols are set in typewriter font (like this). Non-terminal symbols are set in italic font (like that). Square brackets [...] denote optional components. Curly brackets {...} denotes zero, one or several repetitions of the enclosed components. Curly brackets with a trailing plus sign {...}^+ denote one or several repetitions of the enclosed components. Parentheses (...) denote grouping. 6.1 Lexical conventions *=*=*=*=*=*=*=*=*=*=*=*= Blanks ------ The following characters are considered as blanks: space, horizontal tabulation, carriage return, line feed and form feed. Blanks are ignored, but they separate adjacent identifiers, literals and keywords that would otherwise be confused as one single identifier, literal or keyword. Comments -------- Comments are introduced by the two characters (*, with no intervening blanks, and terminated by the characters *), with no intervening blanks. Comments are treated as blank characters. Comments do not occur inside string or character literals. Nested comments are handled correctly. Identifiers ----------- ident ::= ( letter | _ ) { letter | 0 ... 9 | _ | ' } capitalized-ident ::= (A ... Z) { letter | 0 ... 9 | _ | ' } lowercase-ident ::= (a ... z | _) { letter | 0 ... 9 | _ | ' } letter ::= A ... Z | a ... z Identifiers are sequences of letters, digits, _ (the underscore character), and ' (the single quote), starting with a letter or an underscore. Letters contain at least the 52 lowercase and uppercase letters from the ASCII set. The current implementation also recognizes as letters some characters from the ISO 8859-1 set (characters 192--214 and 216--222 as uppercase letters; characters 223--246 and 248--255 as lowercase letters). This feature is deprecated and should be avoided for future compatibility. All characters in an identifier are meaningful. The current implementation accepts identifiers up to 16000000 characters in length. In many places, OCaml makes a distinction between capitalized identifiers and identifiers that begin with a lowercase letter. The underscore character is considered a lowercase letter for this purpose. Integer literals ---------------- integer-literal ::= [-] (0...9) { 0...9 | _ } | [-] (0x| 0X) (0...9| A...F| a...f) { 0...9| A...F| a...f| _ } | [-] (0o| 0O) (0...7) { 0...7| _ } | [-] (0b| 0B) (0...1) { 0...1| _ } An integer literal is a sequence of one or more digits, optionally preceded by a minus sign. By default, integer literals are in decimal (radix 10). The following prefixes select a different radix: --------------------------------- |Prefix | Radix | --------------------------------- | 0x, 0X|hexadecimal (radix 16) | |0o, 0O |octal (radix 8) | |0b, 0B |binary (radix 2) | --------------------------------- (The initial 0 is the digit zero; the O for octal is the letter O.) The interpretation of integer literals that fall outside the range of representable integer values is undefined. For convenience and readability, underscore characters (_) are accepted (and ignored) within integer literals. Floating-point literals ----------------------- float-literal ::= [-] (0...9) { 0...9| _ } [. { 0...9| _ }] [(e| E) [+| -] (0...9) { 0...9| _ }] | [-] (0x| 0X) (0...9| A...F| a...f) { 0...9| A...F| a...f| _ } [. { 0...9| A...F| a...f| _ } [(p| P) [+| -] (0...9) { 0...9| _ }] Floating-point decimal literals consist in an integer part, a fractional part and an exponent part. The integer part is a sequence of one or more digits, optionally preceded by a minus sign. The fractional part is a decimal point followed by zero, one or more digits. The exponent part is the character e or E followed by an optional + or - sign, followed by one or more digits. It is interpreted as a power of 10. The fractional part or the exponent part can be omitted but not both, to avoid ambiguity with integer literals. The interpretation of floating-point literals that fall outside the range of representable floating-point values is undefined. Floating-point hexadecimal literals are denoted with the 0x or 0X prefix. The syntax is similar to that of floating-point decimal literals, with the following differences. The integer part and the fractional part use hexadecimal digits. The exponent part starts with the character p or P. It is written in decimal and interpreted as a power of 2. For convenience and readability, underscore characters (_) are accepted (and ignored) within floating-point literals. Character literals ------------------ char-literal ::= ' regular-char ' | ' escape-sequence ' escape-sequence ::= \ ( \ | " | ' | n | t | b | r | space ) | \ (0...9) (0...9) (0...9) | \x (0...9| A...F| a...f) (0...9| A...F| a...f) | \o (0...3) (0...7) (0...7) Character literals are delimited by ' (single quote) characters. The two single quotes enclose either one character different from ' and \, or one of the escape sequences below: ----------------------------------------------------------- |Sequence| Character denoted | ----------------------------------------------------------- | \\ |backslash (\) | |\" |double quote (") | |\' |single quote (') | |\n |linefeed (LF) | |\r |carriage return (CR) | |\t |horizontal tabulation (TAB) | |\b |backspace (BS) | |\space |space (SPC) | |\ddd |the character with ASCII code ddd in decimal | |\xhh |the character with ASCII code hh in hexadecimal | |\oooo |the character with ASCII code ooo in octal | ----------------------------------------------------------- String literals --------------- string-literal ::= " { string-character } " string-character ::= regular-string-char | escape-sequence | \ newline { space | tab } String literals are delimited by " (double quote) characters. The two double quotes enclose a sequence of either characters different from " and \, or escape sequences from the table given above for character literals. To allow splitting long string literals across lines, the sequence \newline spaces-or-tabs (a backslash at the end of a line followed by any number of spaces and horizontal tabulations at the beginning of the next line) is ignored inside string literals. The current implementation places practically no restrictions on the length of string literals. Naming labels ------------- To avoid ambiguities, naming labels in expressions cannot just be defined syntactically as the sequence of the three tokens ~, ident and :, and have to be defined at the lexical level. label-name ::= lowercase-ident label ::= ~ label-name : optlabel ::= ? label-name : Naming labels come in two flavours: label for normal arguments and optlabel for optional ones. They are simply distinguished by their first character, either ~ or ?. Despite label and optlabel being lexical entities in expressions, their expansions ~ label-name : and ? label-name : will be used in grammars, for the sake of readability. Note also that inside type expressions, this expansion can be taken literally, i.e. there are really 3 tokens, with optional blanks between them. Prefix and infix symbols ------------------------ infix-symbol ::= (= | < | > | @ | ^ | | | & | + | - | * | / | $ | %) { operator-char } + | # { operator-char } prefix-symbol ::= ! { operator-char } + | (? | ~) { operator-char } operator-char ::= ! | $ | % | & | * | + | - | . | / | : | < | = | > | ? | @ | ^ | | | ~ See also the following language extension: extension operators. Sequences of "operator characters", such as <=> or !!, are read as a single token from the infix-symbol or prefix-symbol class. These symbols are parsed as prefix and infix operators inside expressions, but otherwise behave like normal identifiers. Keywords -------- The identifiers below are reserved as keywords, and cannot be employed otherwise: << and as assert asr begin class constraint do done downto else end exception external false for fun function functor if in include inherit initializer land lazy let lor lsl lsr lxor match method mod module mutable new nonrec object of open or private rec sig struct then to true try type val virtual when while with >> The following character sequences are also keywords: << != # & && ' ( ) * + , - -. -> . .. : :: := :> ; ;; < <- = > >] >} ? [ [< [> [| ] _ ` { {< | |] || } ~ >> Note that the following identifiers are keywords of the Camlp4 extensions and should be avoided for compatibility reasons. << parser value $ $$ $: <: << >> ?? >> Ambiguities ----------- Lexical ambiguities are resolved according to the "longest match" rule: when a character sequence can be decomposed into two tokens in several different ways, the decomposition retained is the one with the longest first token. Line number directives ---------------------- + linenum-directive ::= # {0 ... 9} + | # {0 ... 9} " { string-character } " Preprocessors that generate OCaml source code can insert line number directives in their output so that error messages produced by the compiler contain line numbers and file names referring to the source file before preprocessing, instead of after preprocessing. A line number directive is composed of a # (sharp sign), followed by a positive integer (the source line number), optionally followed by a character string (the source file name). Line number directives are treated as blanks during lexical analysis. 6.2 Values *=*=*=*=*=* This section describes the kinds of values that are manipulated by OCaml programs. 6.2.1 Base values ================== Integer numbers --------------- Integer values are integer numbers from -2^30 to 2^30-1, that is -1073741824 to 1073741823. The implementation may support a wider range of integer values: on 64-bit platforms, the current implementation supports integers ranging from -2^62 to 2^62-1. Floating-point numbers ---------------------- Floating-point values are numbers in floating-point representation. The current implementation uses double-precision floating-point numbers conforming to the IEEE 754 standard, with 53 bits of mantissa and an exponent ranging from -1022 to 1023. Characters ---------- Character values are represented as 8-bit integers between 0 and 255. Character codes between 0 and 127 are interpreted following the ASCII standard. The current implementation interprets character codes between 128 and 255 following the ISO 8859-1 standard. Character strings ----------------- String values are finite sequences of characters. The current implementation supports strings containing up to 2^24 - 5 characters (16777211 characters); on 64-bit platforms, the limit is 2^57 - 9. 6.2.2 Tuples ============= Tuples of values are written (v_1, ..., v_n), standing for the n-tuple of values v_1 to v_n. The current implementation supports tuple of up to 2^22 - 1 elements (4194303 elements). 6.2.3 Records ============== Record values are labeled tuples of values. The record value written { field_1 = v_1; ...; field_n = v_n } associates the value v_i to the record field field_i, for i = 1 ... n. The current implementation supports records with up to 2^22 - 1 fields (4194303 fields). 6.2.4 Arrays ============= Arrays are finite, variable-sized sequences of values of the same type. The current implementation supports arrays containing up to 2^22 - 1 elements (4194303 elements) unless the elements are floating-point numbers (2097151 elements in this case); on 64-bit platforms, the limit is 2^54 - 1 for all arrays. 6.2.5 Variant values ===================== Variant values are either a constant constructor, or a non-constant constructor applied to a number of values. The former case is written constr; the latter case is written constr (v_1, ... , v_n ), where the v_i are said to be the arguments of the non-constant constructor constr. The parentheses may be omitted if there is only one argument. The following constants are treated like built-in constant constructors: ----------------------------- |Constant| Constructor | ----------------------------- | false |the boolean false | |true |the boolean true | |() |the "unit" value | |[] |the empty list | ----------------------------- The current implementation limits each variant type to have at most 246 non-constant constructors and 2^30-1 constant constructors. 6.2.6 Polymorphic variants =========================== Polymorphic variants are an alternate form of variant values, not belonging explicitly to a predefined variant type, and following specific typing rules. They can be either constant, written `tag-name, or non-constant, written `tag-name(v). 6.2.7 Functions ================ Functional values are mappings from values to values. 6.2.8 Objects ============== Objects are composed of a hidden internal state which is a record of instance variables, and a set of methods for accessing and modifying these variables. The structure of an object is described by the toplevel class that created it. 6.3 Names *=*=*=*=*= Identifiers are used to give names to several classes of language objects and refer to these objects by name later: - value names (syntactic class value-name), - value constructors and exception constructors (class constr-name), - labels (label-name, defined in section 6.1), - polymorphic variant tags (tag-name), - type constructors (typeconstr-name), - record fields (field-name), - class names (class-name), - method names (method-name), - instance variable names (inst-var-name), - module names (module-name), - module type names (modtype-name). These eleven name spaces are distinguished both by the context and by the capitalization of the identifier: whether the first letter of the identifier is in lowercase (written lowercase-ident below) or in uppercase (written capitalized-ident). Underscore is considered a lowercase letter for this purpose. Naming objects -------------- value-name ::= lowercase-ident | ( operator-name ) operator-name ::= prefix-symbol | infix-op infix-op ::= infix-symbol | * | + | - | -. | = | != | < | > | or | || | & | && | := | mod | land | lor | lxor | lsl | lsr | asr constr-name ::= capitalized-ident tag-name ::= capitalized-ident typeconstr-name ::= lowercase-ident field-name ::= lowercase-ident module-name ::= capitalized-ident modtype-name ::= ident class-name ::= lowercase-ident inst-var-name ::= lowercase-ident method-name ::= lowercase-ident As shown above, prefix and infix symbols as well as some keywords can be used as value names, provided they are written between parentheses. The capitalization rules are summarized in the table below. ------------------------------------------------ | Name space |Case of first letter | ------------------------------------------------ | Values |lowercase | |Constructors |uppercase | |Labels |lowercase | |Polymorphic variant tags|uppercase | |Exceptions |uppercase | |Type constructors |lowercase | |Record fields |lowercase | |Classes |lowercase | |Instance variables |lowercase | |Methods |lowercase | |Modules |uppercase | |Module types |any | ------------------------------------------------ Note on polymorphic variant tags: the current implementation accepts lowercase variant tags in addition to capitalized variant tags, but we suggest you avoid lowercase variant tags for portability and compatibility with future OCaml versions. Referring to named objects -------------------------- value-path ::= [ module-path . ] value-name constr ::= [ module-path . ] constr-name typeconstr ::= [ extended-module-path . ] typeconstr-name field ::= [ module-path . ] field-name modtype-path ::= [ extended-module-path . ] modtype-name class-path ::= [ module-path . ] class-name classtype-path ::= [ extended-module-path . ] class-name module-path ::= module-name { . module-name } extended-module-path ::= extended-module-name { . extended-module-name } extended-module-name ::= module-name { ( extended-module-path ) } A named object can be referred to either by its name (following the usual static scoping rules for names) or by an access path prefix . name, where prefix designates a module and name is the name of an object defined in that module. The first component of the path, prefix, is either a simple module name or an access path name_1 . name_2 ..., in case the defining module is itself nested inside other modules. For referring to type constructors, module types, or class types, the prefix can also contain simple functor applications (as in the syntactic class extended-module-path above) in case the defining module is the result of a functor application. Label names, tag names, method names and instance variable names need not be qualified: the former three are global labels, while the latter are local to a class. 6.4 Type expressions *=*=*=*=*=*=*=*=*=*=* typexpr ::= ' ident | _ | ( typexpr ) | [[?]label-name:] typexpr -> typexpr + | typexpr { * typexpr } | typeconstr | typexpr typeconstr | ( typexpr { , typexpr } ) typeconstr | typexpr as ' ident | polymorphic-variant-type | < [..] > | < method-type { ; method-type } [; | ; ..] > | # class-path | typexpr # class-path | ( typexpr { , typexpr } ) # class-path poly-typexpr ::= typexpr + | { ' ident } . typexpr method-type ::= method-name : poly-typexpr See also the following language extensions: first-class modules, attributes and extension nodes. The table below shows the relative precedences and associativity of operators and non-closed type constructions. The constructions with higher precedences come first. ---------------------------------------------- | Operator |Associativity | ---------------------------------------------- | Type constructor application|-- | |# |-- | |* |-- | |-> |right | |as |-- | ---------------------------------------------- Type expressions denote types in definitions of data types as well as in type constraints over patterns and expressions. Type variables -------------- The type expression ' ident stands for the type variable named ident. The type expression _ stands for either an anonymous type variable or anonymous type parameters. In data type definitions, type variables are names for the data type parameters. In type constraints, they represent unspecified types that can be instantiated by any type to satisfy the type constraint. In general the scope of a named type variable is the whole top-level phrase where it appears, and it can only be generalized when leaving this scope. Anonymous variables have no such restriction. In the following cases, the scope of named type variables is restricted to the type expression where they appear: 1) for universal (explicitly polymorphic) type variables; 2) for type variables that only appear in public method specifications (as those variables will be made universal, as described in section 6.9.1); 3) for variables used as aliases, when the type they are aliased to would be invalid in the scope of the enclosing definition (i.e. when it contains free universal type variables, or locally defined types.) Parenthesized types ------------------- The type expression ( typexpr ) denotes the same type as typexpr. Function types -------------- The type expression typexpr_1 -> typexpr_2 denotes the type of functions mapping arguments of type typexpr_1 to results of type typexpr_2. label-name : typexpr_1 -> typexpr_2 denotes the same function type, but the argument is labeled label. ? label-name : typexpr_1 -> typexpr_2 denotes the type of functions mapping an optional labeled argument of type typexpr_1 to results of type typexpr_2. That is, the physical type of the function will be typexpr_1 option -> typexpr_2. Tuple types ----------- The type expression typexpr_1 * ... * typexpr_n denotes the type of tuples whose elements belong to types typexpr_1, ... typexpr_n respectively. Constructed types ----------------- Type constructors with no parameter, as in typeconstr, are type expressions. The type expression typexpr typeconstr, where typeconstr is a type constructor with one parameter, denotes the application of the unary type constructor typeconstr to the type typexpr. The type expression (typexpr_1,..., typexpr_n) typeconstr, where typeconstr is a type constructor with n parameters, denotes the application of the n-ary type constructor typeconstr to the types typexpr_1 through typexpr_n. In the type expression _ typeconstr , the anonymous type expression _ stands in for anonymous type parameters and is equivalent to (_, ...,_) with as many repetitions of _ as the arity of typeconstr. Aliased and recursive types --------------------------- The type expression typexpr as ' ident denotes the same type as typexpr, and also binds the type variable ident to type typexpr both in typexpr and in other types. In general the scope of an alias is the same as for a named type variable, and covers the whole enclosing definition. If the type variable ident actually occurs in typexpr, a recursive type is created. Recursive types for which there exists a recursive path that does not contain an object or polymorphic variant type constructor are rejected, except when the -rectypes mode is selected. If ' ident denotes an explicit polymorphic variable, and typexpr denotes either an object or polymorphic variant type, the row variable of typexpr is captured by ' ident, and quantified upon. Polymorphic variant types ------------------------- polymorphic-variant-type ::= [ tag-spec-first { | tag-spec } ] | [> [ tag-spec ] { | tag-spec } ] + | [< [|] tag-spec-full { | tag-spec-full } [ > { `tag-name } ] ] tag-spec-first ::= `tag-name [ of typexpr ] | [ typexpr ] | tag-spec tag-spec ::= `tag-name [ of typexpr ] | typexpr tag-spec-full ::= `tag-name [ of [&] typexpr { & typexpr } ] | typexpr Polymorphic variant types describe the values a polymorphic variant may take. The first case is an exact variant type: all possible tags are known, with their associated types, and they can all be present. Its structure is fully known. The second case is an open variant type, describing a polymorphic variant value: it gives the list of all tags the value could take, with their associated types. This type is still compatible with a variant type containing more tags. A special case is the unknown type, which does not define any tag, and is compatible with any variant type. The third case is a closed variant type. It gives information about all the possible tags and their associated types, and which tags are known to potentially appear in values. The exact variant type (first case) is just an abbreviation for a closed variant type where all possible tags are also potentially present. In all three cases, tags may be either specified directly in the `tag-name [of typexpr] form, or indirectly through a type expression, which must expand to an exact variant type, whose tag specifications are inserted in its place. Full specifications of variant tags are only used for non-exact closed types. They can be understood as a conjunctive type for the argument: it is intended to have all the types enumerated in the specification. Such conjunctive constraints may be unsatisfiable. In such a case the corresponding tag may not be used in a value of this type. This does not mean that the whole type is not valid: one can still use other available tags. Conjunctive constraints are mainly intended as output from the type checker. When they are used in source programs, unsolvable constraints may cause early failures. Object types ------------ An object type < [method-type { ; method-type }] > is a record of method types. Each method may have an explicit polymorphic type: { ' ident }^+ . typexpr. Explicit polymorphic variables have a local scope, and an explicit polymorphic type can only be unified to an equivalent one, where only the order and names of polymorphic variables may change. The type < {method-type ;} .. > is the type of an object whose method names and types are described by method-type_1, ..., method-type_n, and possibly some other methods represented by the ellipsis. This ellipsis actually is a special kind of type variable (called row variable in the literature) that stands for any number of extra method types. #-types ------- The type # class-path is a special kind of abbreviation. This abbreviation unifies with the type of any object belonging to a subclass of class class-path. It is handled in a special way as it usually hides a type variable (an ellipsis, representing the methods that may be added in a subclass). In particular, it vanishes when the ellipsis gets instantiated. Each type expression # class-path defines a new type variable, so type # class-path -> # class-path is usually not the same as type (# class-path as ' ident) -> ' ident. Use of #-types to abbreviate polymorphic variant types is deprecated. If t is an exact variant type then #t translates to [< t], and #t[> `tag_1 ...` tag_k] translates to [< t > `tag_1 ...` tag_k] Variant and record types ------------------------ There are no type expressions describing (defined) variant types nor record types, since those are always named, i.e. defined before use and referred to by name. Type definitions are described in section 6.8.1. 6.5 Constants *=*=*=*=*=*=*= constant ::= integer-literal | float-literal | char-literal | string-literal | constr | false | true | () | begin end | [] | [||] | `tag-name See also the following language extensions: integer literals for types int32, int64 and nativeint, quoted strings and extension literals. The syntactic class of constants comprises literals from the four base types (integers, floating-point numbers, characters, character strings), and constant constructors from both normal and polymorphic variants, as well as the special constants false, true, (), [], and [||], which behave like constant constructors, and begin end, which is equivalent to (). 6.6 Patterns *=*=*=*=*=*=* pattern ::= value-name | _ | constant | pattern as value-name | ( pattern ) | ( pattern : typexpr ) | pattern | pattern | constr pattern | `tag-name pattern | #typeconstr + | pattern { , pattern } | { field [: typexpr] = pattern { ; field [: typexpr] = pattern } [ ; ] } | [ pattern { ; pattern } [ ; ] ] | pattern :: pattern | [| pattern { ; pattern } [ ; ] |] | char-literal .. char-literal See also the following language extensions: lazy patterns, local opens, first-class modules, attributes, extension nodes and exception cases in pattern matching. The table below shows the relative precedences and associativity of operators and non-closed pattern constructions. The constructions with higher precedences come first. --------------------------------------------------------- | Operator |Associativity | --------------------------------------------------------- | .. |-- | |lazy (see section 7.3) |-- | |Constructor application, Tag application|right | |:: |right | |, |-- | || |left | |as |-- | --------------------------------------------------------- Patterns are templates that allow selecting data structures of a given shape, and binding identifiers to components of the data structure. This selection operation is called pattern matching; its outcome is either "this value does not match this pattern", or "this value matches this pattern, resulting in the following bindings of names to values". Variable patterns ----------------- A pattern that consists in a value name matches any value, binding the name to the value. The pattern _ also matches any value, but does not bind any name. Patterns are linear: a variable cannot be bound several times by a given pattern. In particular, there is no way to test for equality between two parts of a data structure using only a pattern (but when guards can be used for this purpose). Constant patterns ----------------- A pattern consisting in a constant matches the values that are equal to this constant. Alias patterns -------------- The pattern pattern_1 as value-name matches the same values as pattern_1. If the matching against pattern_1 is successful, the name value-name is bound to the matched value, in addition to the bindings performed by the matching against pattern_1. Parenthesized patterns ---------------------- The pattern ( pattern_1 ) matches the same values as pattern_1. A type constraint can appear in a parenthesized pattern, as in ( pattern_1 : typexpr ). This constraint forces the type of pattern_1 to be compatible with typexpr. "Or" patterns ------------- The pattern pattern_1 | pattern_2 represents the logical "or" of the two patterns pattern_1 and pattern_2. A value matches pattern_1 | pattern_2 if it matches pattern_1 or pattern_2. The two sub-patterns pattern_1 and pattern_2 must bind exactly the same identifiers to values having the same types. Matching is performed from left to right. More precisely, in case some value v matches pattern_1 | pattern_2, the bindings performed are those of pattern_1 when v matches pattern_1. Otherwise, value v matches pattern_2 whose bindings are performed. Variant patterns ---------------- The pattern constr ( pattern_1 , ... , pattern_n ) matches all variants whose constructor is equal to constr, and whose arguments match pattern_1 ... pattern_n. It is a type error if n is not the number of arguments expected by the constructor. The pattern constr _ matches all variants whose constructor is constr. The pattern pattern_1 :: pattern_2 matches non-empty lists whose heads match pattern_1, and whose tails match pattern_2. The pattern [ pattern_1 ; ... ; pattern_n ] matches lists of length n whose elements match pattern_1 ...pattern_n, respectively. This pattern behaves like pattern_1 :: ... :: pattern_n :: []. Polymorphic variant patterns ---------------------------- The pattern `tag-name pattern_1 matches all polymorphic variants whose tag is equal to tag-name, and whose argument matches pattern_1. Polymorphic variant abbreviation patterns ----------------------------------------- If the type [('a,'b,...)] typeconstr = [ ` tag-name_1 typexpr_1 | ... | ` tag-name_n typexpr_n] is defined, then the pattern #typeconstr is a shorthand for the following or-pattern: ( `tag-name_1(_ : typexpr_1) | ... | ` tag-name_n(_ : typexpr_n)). It matches all values of type [< typeconstr ]. Tuple patterns -------------- The pattern pattern_1 , ... , pattern_n matches n-tuples whose components match the patterns pattern_1 through pattern_n. That is, the pattern matches the tuple values (v_1, ..., v_n) such that pattern_i matches v_i for i = 1,... , n. Record patterns --------------- The pattern { field_1 = pattern_1 ; ... ; field_n = pattern_n } matches records that define at least the fields field_1 through field_n, and such that the value associated to field_i matches the pattern pattern_i, for i = 1,... , n. The record value can define more fields than field_1 ...field_n; the values associated to these extra fields are not taken into account for matching. Optional type constraints can be added field by field with { field_1 : typexpr_1 = pattern_1 ;... ; field_n : typexpr_n = pattern_n } to force the type of field_k to be compatible with typexpr_k. Array patterns -------------- The pattern [| pattern_1 ; ... ; pattern_n |] matches arrays of length n such that the i-th array element matches the pattern pattern_i, for i = 1,... , n. Range patterns -------------- The pattern ' c ' .. ' d ' is a shorthand for the pattern ' c ' | ' c_1 ' | ' c_2 ' | ... | ' c_n ' | ' d ' where c_1, c_2, ..., c_n are the characters that occur between c and d in the ASCII character set. For instance, the pattern '0'..'9' matches all characters that are digits. 6.7 Expressions *=*=*=*=*=*=*=*= expr ::= value-path | constant | ( expr ) | begin expr end | ( expr : typexpr ) + | expr {, expr} | constr expr | `tag-name expr | expr :: expr | [ expr { ; expr } [;] ] | [| expr { ; expr } [;] |] | { field [: typexpr] = expr { ; field [: typexpr] = expr } [;] } | { expr with field [: typexpr] = expr { ; field [: typexpr] = expr } [;] } + | expr { argument } | prefix-symbol expr | - expr | -. expr | expr infix-op expr | expr . field | expr . field <- expr | expr .( expr ) | expr .( expr ) <- expr | expr .[ expr ] | expr .[ expr ] <- expr | if expr then expr [ else expr ] | while expr do expr done | for value-name = expr ( to | downto ) expr do expr done | expr ; expr | match expr with pattern-matching | function pattern-matching + | fun { parameter } [ : typexpr ] -> expr | try expr with pattern-matching | let [rec] let-binding { and let-binding } in expr | new class-path | object class-body end | expr # method-name | inst-var-name | inst-var-name <- expr | ( expr :> typexpr ) | ( expr : typexpr :> typexpr ) | {< [ inst-var-name = expr { ; inst-var-name = expr } [;] ] >} | assert expr | lazy expr | let module module-name { ( module-name : module-type ) } [ : module-type ] = module-expr in expr argument ::= expr | ~ label-name | ~ label-name : expr | ? label-name | ? label-name : expr pattern-matching ::= [ | ] pattern [when expr] -> expr { | pattern [when expr] -> expr } let-binding ::= pattern = expr | value-name { parameter } [: typexpr] [:> typexpr] = expr parameter ::= pattern | ~ label-name | ~ ( label-name [: typexpr] ) | ~ label-name : pattern | ? label-name | ? ( label-name [: typexpr] [= expr] ) | ? label-name : pattern | ? label-name : ( pattern [: typexpr] [= expr] ) See also the following language extensions: local opens, record and object notations, explicit polymorphic type annotations, first-class modules, overriding in open statements, syntax for Bigarray access, attributes, extension nodes and local exceptions. The table below shows the relative precedences and associativity of operators and non-closed constructions. The constructions with higher precedence come first. For infix and prefix symbols, we write "*..." to mean "any symbol starting with *". ------------------------------------------------------------------------------- -------------- | Construction or operator |Associativity | ------------------------------------------------------------------------------- -------------- | prefix-symbol |-- | |. .( .[ .{ (see section 7.17) |-- | |#... |-- | |function application, constructor application, tag application, assert, lazy|left | |- -. (prefix) |-- | |**... lsl lsr asr |right | |*... /... %... mod land lor lxor |left | | +... -... |left | |:: |right | |@... ^... |right | |=... <... >... |... &... $... != |left | |& && |right | |or || |right | |, |-- | |<- := |right | |if |-- | |; |right | |let match fun function try |-- | ------------------------------------------------------------------------------- -------------- 6.7.1 Basic expressions ======================== Constants --------- An expression consisting in a constant evaluates to this constant. Value paths ----------- An expression consisting in an access path evaluates to the value bound to this path in the current evaluation environment. The path can be either a value name or an access path to a value component of a module. Parenthesized expressions ------------------------- The expressions ( expr ) and begin expr end have the same value as expr. The two constructs are semantically equivalent, but it is good style to use begin ... end inside control structures: << if ... then begin ... ; ... end else begin ... ; ... end >> and ( ... ) for the other grouping situations. Parenthesized expressions can contain a type constraint, as in ( expr : typexpr ). This constraint forces the type of expr to be compatible with typexpr. Parenthesized expressions can also contain coercions ( expr [: typexpr] :> typexpr) (see subsection 6.7.6 below). Function application -------------------- Function application is denoted by juxtaposition of (possibly labeled) expressions. The expression expr argument_1 ... argument_n evaluates the expression expr and those appearing in argument_1 to argument_n. The expression expr must evaluate to a functional value f, which is then applied to the values of argument_1, ..., argument_n. The order in which the expressions expr, argument_1, ..., argument_n are evaluated is not specified. Arguments and parameters are matched according to their respective labels. Argument order is irrelevant, except among arguments with the same label, or no label. If a parameter is specified as optional (label prefixed by ?) in the type of expr, the corresponding argument will be automatically wrapped with the constructor Some, except if the argument itself is also prefixed by ?, in which case it is passed as is. If a non-labeled argument is passed, and its corresponding parameter is preceded by one or several optional parameters, then these parameters are defaulted, i.e. the value None will be passed for them. All other missing parameters (without corresponding argument), both optional and non-optional, will be kept, and the result of the function will still be a function of these missing parameters to the body of f. As a special case, if the function has a known arity, all the arguments are unlabeled, and their number matches the number of non-optional parameters, then labels are ignored and non-optional parameters are matched in their definition order. Optional arguments are defaulted. In all cases but exact match of order and labels, without optional parameters, the function type should be known at the application point. This can be ensured by adding a type constraint. Principality of the derivation can be checked in the -principal mode. Function definition ------------------- Two syntactic forms are provided to define functions. The first form is introduced by the keyword function: function pattern -> expr 1 1 | ... | pattern -> expr n n This expression evaluates to a functional value with one argument. When this function is applied to a value v, this value is matched against each pattern pattern_1 to pattern_n. If one of these matchings succeeds, that is, if the value v matches the pattern pattern_i for some i, then the expression expr_i associated to the selected pattern is evaluated, and its value becomes the value of the function application. The evaluation of expr_i takes place in an environment enriched by the bindings performed during the matching. If several patterns match the argument v, the one that occurs first in the function definition is selected. If none of the patterns matches the argument, the exception Match_failure is raised. The other form of function definition is introduced by the keyword fun: fun parameter_1 ... parameter_n -> expr This expression is equivalent to: fun parameter_1 -> ... fun parameter_n -> expr An optional type constraint typexpr can be added before -> to enforce the type of the result to be compatible with the constraint typexpr: fun parameter_1 ... parameter_n : typexpr -> expr is equivalent to fun parameter_1 -> ... fun parameter_n -> (expr : typexpr ) Beware of the small syntactic difference between a type constraint on the last parameter fun parameter_1 ... (parameter_n: typexpr)-> expr and one on the result fun parameter_1 ... parameter_n: typexpr -> expr The parameter patterns ~lab and ~(lab [: typ]) are shorthands for respectively ~lab: lab and ~lab:( lab [: typ]), and similarly for their optional counterparts. A function of the form fun ? lab :( pattern = expr_0 ) -> expr is equivalent to fun ? lab : ident -> let pattern = match ident with Some ident -> ident | None -> expr_0 in expr where ident is a fresh variable, except that it is unspecified when expr_0 is evaluated. After these two transformations, expressions are of the form fun [label_1] pattern_1 -> ... fun [label_n] pattern_n -> expr If we ignore labels, which will only be meaningful at function application, this is equivalent to function pattern_1 -> ... function pattern_n -> expr That is, the fun expression above evaluates to a curried function with n arguments: after applying this function n times to the values v_1 ... v_n, the values will be matched in parallel against the patterns pattern_1 ... pattern_n. If the matching succeeds, the function returns the value of expr in an environment enriched by the bindings performed during the matchings. If the matching fails, the exception Match_failure is raised. Guards in pattern-matchings --------------------------- The cases of a pattern matching (in the function, match and try constructs) can include guard expressions, which are arbitrary boolean expressions that must evaluate to true for the match case to be selected. Guards occur just before the -> token and are introduced by the when keyword: function pattern [when cond ] -> expr 1 1 1 | ... | pattern [when cond ] -> expr n n n Matching proceeds as described before, except that if the value matches some pattern pattern_i which has a guard cond_i, then the expression cond_i is evaluated (in an environment enriched by the bindings performed during matching). If cond_i evaluates to true, then expr_i is evaluated and its value returned as the result of the matching, as usual. But if cond_i evaluates to false, the matching is resumed against the patterns following pattern_i. Local definitions ----------------- The let and let rec constructs bind value names locally. The construct let pattern_1 = expr_1 and ... and pattern_n = expr_n in expr evaluates expr_1 ... expr_n in some unspecified order and matches their values against the patterns pattern_1 ... pattern_n. If the matchings succeed, expr is evaluated in the environment enriched by the bindings performed during matching, and the value of expr is returned as the value of the whole let expression. If one of the matchings fails, the exception Match_failure is raised. An alternate syntax is provided to bind variables to functional values: instead of writing let ident = fun parameter_1 ... parameter_m -> expr in a let expression, one may instead write let ident parameter_1 ... parameter_m = expr Recursive definitions of names are introduced by let rec: let rec pattern_1 = expr_1 and ... and pattern_n = expr_n in expr The only difference with the let construct described above is that the bindings of names to values performed by the pattern-matching are considered already performed when the expressions expr_1 to expr_n are evaluated. That is, the expressions expr_1 to expr_n can reference identifiers that are bound by one of the patterns pattern_1, ..., pattern_n, and expect them to have the same value as in expr, the body of the let rec construct. The recursive definition is guaranteed to behave as described above if the expressions expr_1 to expr_n are function definitions (fun ... or function ...), and the patterns pattern_1 ... pattern_n are just value names, as in: let rec name_1 = fun ... and ... and name_n = fun ... in expr This defines name_1 ... name_n as mutually recursive functions local to expr. The behavior of other forms of let rec definitions is implementation-dependent. The current implementation also supports a certain class of recursive definitions of non-functional values, as explained in section 7.2. 6.7.2 Control structures ========================= Sequence -------- The expression expr_1 ; expr_2 evaluates expr_1 first, then expr_2, and returns the value of expr_2. Conditional ----------- The expression if expr_1 then expr_2 else expr_3 evaluates to the value of expr_2 if expr_1 evaluates to the boolean true, and to the value of expr_3 if expr_1 evaluates to the boolean false. The else expr_3 part can be omitted, in which case it defaults to else (). Case expression --------------- The expression match expr with pattern -> expr 1 1 | ... | pattern -> expr n n matches the value of expr against the patterns pattern_1 to pattern_n. If the matching against pattern_i succeeds, the associated expression expr_i is evaluated, and its value becomes the value of the whole match expression. The evaluation of expr_i takes place in an environment enriched by the bindings performed during matching. If several patterns match the value of expr, the one that occurs first in the match expression is selected. If none of the patterns match the value of expr, the exception Match_failure is raised. Boolean operators ----------------- The expression expr_1 && expr_2 evaluates to true if both expr_1 and expr_2 evaluate to true; otherwise, it evaluates to false. The first component, expr_1, is evaluated first. The second component, expr_2, is not evaluated if the first component evaluates to false. Hence, the expression expr_1 && expr_2 behaves exactly as if expr_1 then expr_2 else false. The expression expr_1 || expr_2 evaluates to true if one of the expressions expr_1 and expr_2 evaluates to true; otherwise, it evaluates to false. The first component, expr_1, is evaluated first. The second component, expr_2, is not evaluated if the first component evaluates to true. Hence, the expression expr_1 || expr_2 behaves exactly as if expr_1 then true else expr_2. The boolean operators & and or are deprecated synonyms for (respectively) && and ||. Loops ----- The expression while expr_1 do expr_2 done repeatedly evaluates expr_2 while expr_1 evaluates to true. The loop condition expr_1 is evaluated and tested at the beginning of each iteration. The whole while ... done expression evaluates to the unit value (). The expression for name = expr_1 to expr_2 do expr_3 done first evaluates the expressions expr_1 and expr_2 (the boundaries) into integer values n and p. Then, the loop body expr_3 is repeatedly evaluated in an environment where name is successively bound to the values n, n+1, ..., p-1, p. The loop body is never evaluated if n > p. The expression for name = expr_1 downto expr_2 do expr_3 done evaluates similarly, except that name is successively bound to the values n, n-1, ..., p+1, p. The loop body is never evaluated if n < p. In both cases, the whole for expression evaluates to the unit value (). Exception handling ------------------ The expression try expr with pattern -> expr 1 1 | ... | pattern -> expr n n evaluates the expression expr and returns its value if the evaluation of expr does not raise any exception. If the evaluation of expr raises an exception, the exception value is matched against the patterns pattern_1 to pattern_n. If the matching against pattern_i succeeds, the associated expression expr_i is evaluated, and its value becomes the value of the whole try expression. The evaluation of expr_i takes place in an environment enriched by the bindings performed during matching. If several patterns match the value of expr, the one that occurs first in the try expression is selected. If none of the patterns matches the value of expr, the exception value is raised again, thereby transparently "passing through" the try construct. 6.7.3 Operations on data structures ==================================== Products -------- The expression expr_1 , ... , expr_n evaluates to the n-tuple of the values of expressions expr_1 to expr_n. The evaluation order of the subexpressions is not specified. Variants -------- The expression constr expr evaluates to the unary variant value whose constructor is constr, and whose argument is the value of expr. Similarly, the expression constr ( expr_1 , ... , expr_n ) evaluates to the n-ary variant value whose constructor is constr and whose arguments are the values of expr_1, ..., expr_n. The expression constr ( expr_1, ..., expr_n) evaluates to the variant value whose constructor is constr, and whose arguments are the values of expr_1 ... expr_n. For lists, some syntactic sugar is provided. The expression expr_1 :: expr_2 stands for the constructor ( :: ) applied to the arguments ( expr_1 , expr_2 ), and therefore evaluates to the list whose head is the value of expr_1 and whose tail is the value of expr_2. The expression [ expr_1 ; ... ; expr_n ] is equivalent to expr_1 :: ... :: expr_n :: [], and therefore evaluates to the list whose elements are the values of expr_1 to expr_n. Polymorphic variants -------------------- The expression `tag-name expr evaluates to the polymorphic variant value whose tag is tag-name, and whose argument is the value of expr. Records ------- The expression { field_1 = expr_1 ; ... ; field_n = expr_n } evaluates to the record value { field_1 = v_1; ...; field_n = v_n } where v_i is the value of expr_i for i = 1,... , n. The fields field_1 to field_n must all belong to the same record type; each field of this record type must appear exactly once in the record expression, though they can appear in any order. The order in which expr_1 to expr_n are evaluated is not specified. Optional type constraints can be added after each field { field_1 : typexpr_1 = expr_1 ;... ; field_n : typexpr_n = expr_n } to force the type of field_k to be compatible with typexpr_k. The expression { expr with field_1 = expr_1 ; ... ; field_n = expr_n } builds a fresh record with fields field_1 ... field_n equal to expr_1 ... expr_n, and all other fields having the same value as in the record expr. In other terms, it returns a shallow copy of the record expr, except for the fields field_1 ... field_n, which are initialized to expr_1 ... expr_n. As previously, it is possible to add an optional type constraint on each field being updated with { expr with field_1 : typexpr_1 = expr_1 ; ... ; field_n : typexpr_n = expr_n }. The expression expr_1 . field evaluates expr_1 to a record value, and returns the value associated to field in this record value. The expression expr_1 . field <- expr_2 evaluates expr_1 to a record value, which is then modified in-place by replacing the value associated to field in this record by the value of expr_2. This operation is permitted only if field has been declared mutable in the definition of the record type. The whole expression expr_1 . field <- expr_2 evaluates to the unit value (). Arrays ------ The expression [| expr_1 ; ... ; expr_n |] evaluates to a n-element array, whose elements are initialized with the values of expr_1 to expr_n respectively. The order in which these expressions are evaluated is unspecified. The expression expr_1 .( expr_2 ) returns the value of element number expr_2 in the array denoted by expr_1. The first element has number 0; the last element has number n-1, where n is the size of the array. The exception Invalid_argument is raised if the access is out of bounds. The expression expr_1 .( expr_2 ) <- expr_3 modifies in-place the array denoted by expr_1, replacing element number expr_2 by the value of expr_3. The exception Invalid_argument is raised if the access is out of bounds. The value of the whole expression is (). Strings ------- The expression expr_1 .[ expr_2 ] returns the value of character number expr_2 in the string denoted by expr_1. The first character has number 0; the last character has number n-1, where n is the length of the string. The exception Invalid_argument is raised if the access is out of bounds. The expression expr_1 .[ expr_2 ] <- expr_3 modifies in-place the string denoted by expr_1, replacing character number expr_2 by the value of expr_3. The exception Invalid_argument is raised if the access is out of bounds. The value of the whole expression is (). Note: this possibility is offered only for backward compatibility with older versions of OCaml and will be removed in a future version. New code should use byte sequences and the Bytes.set function. 6.7.4 Operators ================ Symbols from the class infix-symbol, as well as the keywords *, +, -, -., =, !=, <, >, or, ||, &, &&, :=, mod, land, lor, lxor, lsl, lsr, and asr can appear in infix position (between two expressions). Symbols from the class prefix-symbol, as well as the keywords - and -. can appear in prefix position (in front of an expression). Infix and prefix symbols do not have a fixed meaning: they are simply interpreted as applications of functions bound to the names corresponding to the symbols. The expression prefix-symbol expr is interpreted as the application ( prefix-symbol ) expr. Similarly, the expression expr_1 infix-symbol expr_2 is interpreted as the application ( infix-symbol ) expr_1 expr_2. The table below lists the symbols defined in the initial environment and their initial meaning. (See the description of the core library module Pervasives in chapter 23 for more details). Their meaning may be changed at any time using let ( infix-op ) name_1 name_2 = ... Note: the operators &&, ||, and ~- are handled specially and it is not advisable to change their meaning. The keywords - and -. can appear both as infix and prefix operators. When they appear as prefix operators, they are interpreted respectively as the functions (~-) and (~-.). ------------------------------------------------------ | Operator | Initial meaning | ------------------------------------------------------ | + |Integer addition. | |- (infix) |Integer subtraction. | |~- - (prefix) |Integer negation. | |* |Integer multiplication. | |/ |Integer division. Raise | | |Division_by_zero if second | | |argument is zero. | |mod |Integer modulus. Raise | | |Division_by_zero if second | | |argument is zero. | |land |Bitwise logical "and" on integers.| | | | |lor |Bitwise logical "or" on integers. | |lxor |Bitwise logical "exclusive or" on | | |integers. | |lsl |Bitwise logical shift left on | | |integers. | |lsr |Bitwise logical shift right on | | |integers. | |asr |Bitwise arithmetic shift right on | | |integers. | |+. |Floating-point addition. | |-. (infix) |Floating-point subtraction. | |~-. -. (prefix)|Floating-point negation. | |*. |Floating-point multiplication. | |/. |Floating-point division. | |** |Floating-point exponentiation. | |@ |List concatenation. | |^ |String concatenation. | |! |Dereferencing (return the current | | |contents of a reference). | |:= |Reference assignment (update the | | |reference given as first argument | | |with the value of the second | | |argument). | |= |Structural equality test. | |<> |Structural inequality test. | |== |Physical equality test. | |!= |Physical inequality test. | |< |Test "less than". | |<= |Test "less than or equal". | |> |Test "greater than". | |>= |Test "greater than or equal". | |&& & |Boolean conjunction. | ||| or |Boolean disjunction. | ------------------------------------------------------ 6.7.5 Objects ============== Object creation --------------- When class-path evaluates to a class body, new class-path evaluates to a new object containing the instance variables and methods of this class. When class-path evaluates to a class function, new class-path evaluates to a function expecting the same number of arguments and returning a new object of this class. Immediate object creation ------------------------- Creating directly an object through the object class-body end construct is operationally equivalent to defining locally a class class-name = object class-body end ---see sections 6.9.2 and following for the syntax of class-body--- and immediately creating a single object from it by new class-name. The typing of immediate objects is slightly different from explicitly defining a class in two respects. First, the inferred object type may contain free type variables. Second, since the class body of an immediate object will never be extended, its self type can be unified with a closed object type. Method invocation ----------------- The expression expr # method-name invokes the method method-name of the object denoted by expr. If method-name is a polymorphic method, its type should be known at the invocation site. This is true for instance if expr is the name of a fresh object (let ident = new class-path ... ) or if there is a type constraint. Principality of the derivation can be checked in the -principal mode. Accessing and modifying instance variables ------------------------------------------ The instance variables of a class are visible only in the body of the methods defined in the same class or a class that inherits from the class defining the instance variables. The expression inst-var-name evaluates to the value of the given instance variable. The expression inst-var-name <- expr assigns the value of expr to the instance variable inst-var-name, which must be mutable. The whole expression inst-var-name <- expr evaluates to (). Object duplication ------------------ An object can be duplicated using the library function Oo.copy (see section 24.27). Inside a method, the expression {< inst-var-name = expr { ; inst-var-name = expr } >} returns a copy of self with the given instance variables replaced by the values of the associated expressions; other instance variables have the same value in the returned object as in self. 6.7.6 Coercions ================ Expressions whose type contains object or polymorphic variant types can be explicitly coerced (weakened) to a supertype. The expression (expr :> typexpr) coerces the expression expr to type typexpr. The expression (expr : typexpr_1 :> typexpr_2) coerces the expression expr from type typexpr_1 to type typexpr_2. The former operator will sometimes fail to coerce an expression expr from a type typ_1 to a type typ_2 even if type typ_1 is a subtype of type typ_2: in the current implementation it only expands two levels of type abbreviations containing objects and/or polymorphic variants, keeping only recursion when it is explicit in the class type (for objects). As an exception to the above algorithm, if both the inferred type of expr and typ are ground (i.e. do not contain type variables), the former operator behaves as the latter one, taking the inferred type of expr as typ_1. In case of failure with the former operator, the latter one should be used. It is only possible to coerce an expression expr from type typ_1 to type typ_2, if the type of expr is an instance of typ_1 (like for a type annotation), and typ_1 is a subtype of typ_2. The type of the coerced expression is an instance of typ_2. If the types contain variables, they may be instantiated by the subtyping algorithm, but this is only done after determining whether typ_1 is a potential subtype of typ_2. This means that typing may fail during this latter unification step, even if some instance of typ_1 is a subtype of some instance of typ_2. In the following paragraphs we describe the subtyping relation used. Object types ------------ A fixed object type admits as subtype any object type that includes all its methods. The types of the methods shall be subtypes of those in the supertype. Namely, < met_1 : typ_1 ; ... ; met_n : typ_n > is a supertype of < met_1 : typ'_1 ; ... ; met_n : typ'_n ; met_n+1 : typ'_n+1 ; ... ; met_n+m : typ'_n+m [; ..] > which may contain an ellipsis .. if every typ_i is a supertype of the corresponding typ'_i. A monomorphic method type can be a supertype of a polymorphic method type. Namely, if typ is an instance of typ', then 'a_1 ... 'a_n . typ' is a subtype of typ. Inside a class definition, newly defined types are not available for subtyping, as the type abbreviations are not yet completely defined. There is an exception for coercing self to the (exact) type of its class: this is allowed if the type of self does not appear in a contravariant position in the class type, i.e. if there are no binary methods. Polymorphic variant types ------------------------- A polymorphic variant type typ is a subtype of another polymorphic variant type typ' if the upper bound of typ (i.e. the maximum set of constructors that may appear in an instance of typ) is included in the lower bound of typ', and the types of arguments for the constructors of typ are subtypes of those in typ'. Namely, [[<] `C_1 of typ_1 | ... | ` C_n of typ_n ] which may be a shrinkable type, is a subtype of [[>] `C_1 of typ'_1 | ... | `C_n of typ'_n | `C_n+1 of typ'_n+1 | ... | `C_n+m of typ'_n+m ] which may be an extensible type, if every typ_i is a subtype of typ'_i. Variance -------- Other types do not introduce new subtyping, but they may propagate the subtyping of their arguments. For instance, typ_1 * typ_2 is a subtype of typ'_1 * typ'_2 when typ_1 and typ_2 are respectively subtypes of typ'_1 and typ'_2. For function types, the relation is more subtle: typ_1 -> typ_2 is a subtype of typ'_1 -> typ'_2 if typ_1 is a supertype of typ'_1 and typ_2 is a subtype of typ'_2. For this reason, function types are covariant in their second argument (like tuples), but contravariant in their first argument. Mutable types, like array or ref are neither covariant nor contravariant, they are nonvariant, that is they do not propagate subtyping. For user-defined types, the variance is automatically inferred: a parameter is covariant if it has only covariant occurrences, contravariant if it has only contravariant occurrences, variance-free if it has no occurrences, and nonvariant otherwise. A variance-free parameter may change freely through subtyping, it does not have to be a subtype or a supertype. For abstract and private types, the variance must be given explicitly (see section 6.8.1), otherwise the default is nonvariant. This is also the case for constrained arguments in type definitions. 6.7.7 Other ============ Assertion checking ------------------ OCaml supports the assert construct to check debugging assertions. The expression assert expr evaluates the expression expr and returns () if expr evaluates to true. If it evaluates to false the exception Assert_failure is raised with the source file name and the location of expr as arguments. Assertion checking can be turned off with the -noassert compiler option. In this case, expr is not evaluated at all. As a special case, assert false is reduced to raise (Assert_failure ...), which gives it a polymorphic type. This means that it can be used in place of any expression (for example as a branch of any pattern-matching). It also means that the assert false "assertions" cannot be turned off by the -noassert option. Lazy expressions ---------------- The expression lazy expr returns a value v of type Lazy.t that encapsulates the computation of expr. The argument expr is not evaluated at this point in the program. Instead, its evaluation will be performed the first time the function Lazy.force is applied to the value v, returning the actual value of expr. Subsequent applications of Lazy.force to v do not evaluate expr again. Applications of Lazy.force may be implicit through pattern matching (see 7.3). Local modules ------------- The expression let module module-name = module-expr in expr locally binds the module expression module-expr to the identifier module-name during the evaluation of the expression expr. It then returns the value of expr. For example: << let remove_duplicates comparison_fun string_list = let module StringSet = Set.Make(struct type t = string let compare = comparison_fun end) in StringSet.elements (List.fold_right StringSet.add string_list StringSet.empty) >> 6.8 Type and exception definitions *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* 6.8.1 Type definitions ======================= Type definitions bind type constructors to data types: either variant types, record types, type abbreviations, or abstract data types. They also bind the value constructors and record fields associated with the definition. type-definition ::= type [nonrec] typedef { and typedef } typedef ::= [type-params] typeconstr-name type-information type-information ::= [type-equation] [type-representation] { type-constraint } type-equation ::= = typexpr type-representation ::= = [|] constr-decl { | constr-decl } | = record-decl type-params ::= type-param | ( type-param { , type-param } ) type-param ::= [variance] ' ident variance ::= + | - record-decl ::= { field-decl { ; field-decl } [;] } constr-decl ::= (constr-name | [] | (::)) [ of constr-args ] constr-args ::= typexpr { * typexpr } field-decl ::= [mutable] field-name : poly-typexpr type-constraint ::= constraint ' ident = typexpr See also the following language extensions: private types, generalized algebraic datatypes, attributes, extension nodes, extensible variant types and inline records. Type definitions are introduced by the type keyword, and consist in one or several simple definitions, possibly mutually recursive, separated by the and keyword. Each simple definition defines one type constructor. A simple definition consists in a lowercase identifier, possibly preceded by one or several type parameters, and followed by an optional type equation, then an optional type representation, and then a constraint clause. The identifier is the name of the type constructor being defined. In the right-hand side of type definitions, references to one of the type constructor name being defined are considered as recursive, unless type is followed by nonrec. The nonrec keyword was introduced in OCaml 4.02.2. The optional type parameters are either one type variable ' ident, for type constructors with one parameter, or a list of type variables ('ident_1,...,' ident_n), for type constructors with several parameters. Each type parameter may be prefixed by a variance constraint + (resp. -) indicating that the parameter is covariant (resp. contravariant). These type parameters can appear in the type expressions of the right-hand side of the definition, optionally restricted by a variance constraint ; i.e. a covariant parameter may only appear on the right side of a functional arrow (more precisely, follow the left branch of an even number of arrows), and a contravariant parameter only the left side (left branch of an odd number of arrows). If the type has a representation or an equation, and the parameter is free (i.e. not bound via a type constraint to a constructed type), its variance constraint is checked but subtyping etc. will use the inferred variance of the parameter, which may be less restrictive; otherwise (i.e. for abstract types or non-free parameters), the variance must be given explicitly, and the parameter is invariant if no variance is given. The optional type equation = typexpr makes the defined type equivalent to the type expression typexpr: one can be substituted for the other during typing. If no type equation is given, a new type is generated: the defined type is incompatible with any other type. The optional type representation describes the data structure representing the defined type, by giving the list of associated constructors (if it is a variant type) or associated fields (if it is a record type). If no type representation is given, nothing is assumed on the structure of the type besides what is stated in the optional type equation. The type representation = [|] constr-decl { | constr-decl } describes a variant type. The constructor declarations constr-decl_1, ..., constr-decl_n describe the constructors associated to this variant type. The constructor declaration constr-name of typexpr_1 * ... * typexpr_n declares the name constr-name as a non-constant constructor, whose arguments have types typexpr_1 ...typexpr_n. The constructor declaration constr-name declares the name constr-name as a constant constructor. Constructor names must be capitalized. The type representation = { field-decl { ; field-decl } [;] } describes a record type. The field declarations field-decl_1, ..., field-decl_n describe the fields associated to this record type. The field declaration field-name : poly-typexpr declares field-name as a field whose argument has type poly-typexpr. The field declaration mutable field-name : poly-typexpr behaves similarly; in addition, it allows physical modification of this field. Immutable fields are covariant, mutable fields are non-variant. Both mutable and immutable fields may have a explicitly polymorphic types. The polymorphism of the contents is statically checked whenever a record value is created or modified. Extracted values may have their types instantiated. The two components of a type definition, the optional equation and the optional representation, can be combined independently, giving rise to four typical situations: Abstract type: no equation, no representation. When appearing in a module signature, this definition specifies nothing on the type constructor, besides its number of parameters: its representation is hidden and it is assumed incompatible with any other type. Type abbreviation: an equation, no representation. This defines the type constructor as an abbreviation for the type expression on the right of the = sign. New variant type or record type: no equation, a representation. This generates a new type constructor and defines associated constructors or fields, through which values of that type can be directly built or inspected. Re-exported variant type or record type: an equation, a representation. In this case, the type constructor is defined as an abbreviation for the type expression given in the equation, but in addition the constructors or fields given in the representation remain attached to the defined type constructor. The type expression in the equation part must agree with the representation: it must be of the same kind (record or variant) and have exactly the same constructors or fields, in the same order, with the same arguments. The type variables appearing as type parameters can optionally be prefixed by + or - to indicate that the type constructor is covariant or contravariant with respect to this parameter. This variance information is used to decide subtyping relations when checking the validity of :> coercions (see section 6.7.6). For instance, type +'a t declares t as an abstract type that is covariant in its parameter; this means that if the type tau is a subtype of the type sigma, then tau t is a subtype of sigma t. Similarly, type -'a t declares that the abstract type t is contravariant in its parameter: if tau is a subtype of sigma, then sigma t is a subtype of tau t. If no + or - variance annotation is given, the type constructor is assumed non-variant in the corresponding parameter. For instance, the abstract type declaration type 'a t means that tau t is neither a subtype nor a supertype of sigma t if tau is subtype of sigma. The variance indicated by the + and - annotations on parameters is enforced only for abstract and private types, or when there are type constraints. Otherwise, for abbreviations, variant and record types without type constraints, the variance properties of the type constructor are inferred from its definition, and the variance annotations are only checked for conformance with the definition. The construct constraint ' ident = typexpr allows the specification of type parameters. Any actual type argument corresponding to the type parameter ident has to be an instance of typexpr (more precisely, ident and typexpr are unified). Type variables of typexpr can appear in the type equation and the type declaration. 6.8.2 Exception definitions ============================ exception-definition ::= exception constr-decl | exception constr-name = constr Exception definitions add new constructors to the built-in variant type exn of exception values. The constructors are declared as for a definition of a variant type. The form exception constr-decl generates a new exception, distinct from all other exceptions in the system. The form exception constr-name = constr gives an alternate name to an existing exception. 6.9 Classes *=*=*=*=*=*= Classes are defined using a small language, similar to the module language. 6.9.1 Class types ================== Class types are the class-level equivalent of type expressions: they specify the general shape and type properties of classes. class-type ::= [[?]label-name:] typexpr -> class-type | class-body-type class-body-type ::= object [( typexpr )] {class-field-spec} end | [[ typexpr {, typexpr} ]] classtype-path class-field-spec ::= inherit class-body-type | val [mutable] [virtual] inst-var-name : typexpr | val virtual mutable inst-var-name : typexpr | method [private] [virtual] method-name : poly-typexpr | method virtual private method-name : poly-typexpr | constraint typexpr = typexpr See also the following language extensions: attributes and extension nodes. Simple class expressions ------------------------ The expression classtype-path is equivalent to the class type bound to the name classtype-path. Similarly, the expression [ typexpr_1 , ... typexpr_n ] classtype-path is equivalent to the parametric class type bound to the name classtype-path, in which type parameters have been instantiated to respectively typexpr_1, ...typexpr_n. Class function type ------------------- The class type expression typexpr -> class-type is the type of class functions (functions from values to classes) that take as argument a value of type typexpr and return as result a class of type class-type. Class body type --------------- The class type expression object [( typexpr )] {class-field-spec} end is the type of a class body. It specifies its instance variables and methods. In this type, typexpr is matched against the self type, therefore providing a name for the self type. A class body will match a class body type if it provides definitions for all the components specified in the class body type, and these definitions meet the type requirements given in the class body type. Furthermore, all methods either virtual or public present in the class body must also be present in the class body type (on the other hand, some instance variables and concrete private methods may be omitted). A virtual method will match a concrete method, which makes it possible to forget its implementation. An immutable instance variable will match a mutable instance variable. Inheritance ----------- The inheritance construct inherit class-body-type provides for inclusion of methods and instance variables from other class types. The instance variable and method types from class-body-type are added into the current class type. Instance variable specification ------------------------------- A specification of an instance variable is written val [mutable] [virtual] inst-var-name : typexpr, where inst-var-name is the name of the instance variable and typexpr its expected type. The flag mutable indicates whether this instance variable can be physically modified. The flag virtual indicates that this instance variable is not initialized. It can be initialized later through inheritance. An instance variable specification will hide any previous specification of an instance variable of the same name. Method specification -------------------- The specification of a method is written method [private] method-name : poly-typexpr, where method-name is the name of the method and poly-typexpr its expected type, possibly polymorphic. The flag private indicates that the method cannot be accessed from outside the object. The polymorphism may be left implicit in public method specifications: any type variable which is not bound to a class parameter and does not appear elsewhere inside the class specification will be assumed to be universal, and made polymorphic in the resulting method type. Writing an explicit polymorphic type will disable this behaviour. If several specifications are present for the same method, they must have compatible types. Any non-private specification of a method forces it to be public. Virtual method specification ---------------------------- A virtual method specification is written method [private] virtual method-name : poly-typexpr, where method-name is the name of the method and poly-typexpr its expected type. Constraints on type parameters ------------------------------ The construct constraint typexpr_1 = typexpr_2 forces the two type expressions to be equal. This is typically used to specify type parameters: in this way, they can be bound to specific type expressions. 6.9.2 Class expressions ======================== Class expressions are the class-level equivalent of value expressions: they evaluate to classes, thus providing implementations for the specifications expressed in class types. class-expr ::= class-path | [ typexpr {, typexpr} ] class-path | ( class-expr ) | ( class-expr : class-type ) + | class-expr {argument} + | fun {parameter} -> class-expr | let [rec] let-binding {and let-binding} in class-expr | object class-body end class-field ::= inherit class-expr [as lowercase-ident] | val [mutable] inst-var-name [: typexpr] = expr | val [mutable] virtual inst-var-name : typexpr | val virtual mutable inst-var-name : typexpr | method [private] method-name {parameter} [: typexpr] = expr | method [private] method-name : poly-typexpr = expr | method [private] virtual method-name : poly-typexpr | method virtual private method-name : poly-typexpr | constraint typexpr = typexpr | initializer expr See also the following language extensions: locally abstract types, explicit overriding in class definitions, attributes and extension nodes. Simple class expressions ------------------------ The expression class-path evaluates to the class bound to the name class-path. Similarly, the expression [ typexpr_1 , ... typexpr_n ] class-path evaluates to the parametric class bound to the name class-path, in which type parameters have been instantiated respectively to typexpr_1, ...typexpr_n. The expression ( class-expr ) evaluates to the same module as class-expr. The expression ( class-expr : class-type ) checks that class-type matches the type of class-expr (that is, that the implementation class-expr meets the type specification class-type). The whole expression evaluates to the same class as class-expr, except that all components not specified in class-type are hidden and can no longer be accessed. Class application ----------------- Class application is denoted by juxtaposition of (possibly labeled) expressions. It denotes the class whose constructor is the first expression applied to the given arguments. The arguments are evaluated as for expression application, but the constructor itself will only be evaluated when objects are created. In particular, side-effects caused by the application of the constructor will only occur at object creation time. Class function -------------- The expression fun [[?]label-name:] pattern -> class-expr evaluates to a function from values to classes. When this function is applied to a value v, this value is matched against the pattern pattern and the result is the result of the evaluation of class-expr in the extended environment. Conversion from functions with default values to functions with patterns only works identically for class functions as for normal functions. The expression fun parameter_1 ... parameter_n -> class-expr is a short form for fun parameter_1 -> ... fun parameter_n -> expr Local definitions ----------------- The let and let rec constructs bind value names locally, as for the core language expressions. If a local definition occurs at the very beginning of a class definition, it will be evaluated when the class is created (just as if the definition was outside of the class). Otherwise, it will be evaluated when the object constructor is called. Class body ---------- class-body ::= [( pattern [: typexpr] )] { class-field } The expression object class-body end denotes a class body. This is the prototype for an object : it lists the instance variables and methods of an objet of this class. A class body is a class value: it is not evaluated at once. Rather, its components are evaluated each time an object is created. In a class body, the pattern ( pattern [: typexpr] ) is matched against self, therefore providing a binding for self and self type. Self can only be used in method and initializers. Self type cannot be a closed object type, so that the class remains extensible. Since OCaml 4.01, it is an error if the same method or instance variable name is defined several times in the same class body. Inheritance ----------- The inheritance construct inherit class-expr allows reusing methods and instance variables from other classes. The class expression class-expr must evaluate to a class body. The instance variables, methods and initializers from this class body are added into the current class. The addition of a method will override any previously defined method of the same name. An ancestor can be bound by appending as lowercase-ident to the inheritance construct. lowercase-ident is not a true variable and can only be used to select a method, i.e. in an expression lowercase-ident # method-name. This gives access to the method method-name as it was defined in the parent class even if it is redefined in the current class. The scope of this ancestor binding is limited to the current class. The ancestor method may be called from a subclass but only indirectly. Instance variable definition ---------------------------- The definition val [mutable] inst-var-name = expr adds an instance variable inst-var-name whose initial value is the value of expression expr. The flag mutable allows physical modification of this variable by methods. An instance variable can only be used in the methods and initializers that follow its definition. Since version 3.10, redefinitions of a visible instance variable with the same name do not create a new variable, but are merged, using the last value for initialization. They must have identical types and mutability. However, if an instance variable is hidden by omitting it from an interface, it will be kept distinct from other instance variables with the same name. Virtual instance variable definition ------------------------------------ A variable specification is written val [mutable] virtual inst-var-name : typexpr. It specifies whether the variable is modifiable, and gives its type. Virtual instance variables were added in version 3.10. Method definition ----------------- A method definition is written method method-name = expr. The definition of a method overrides any previous definition of this method. The method will be public (that is, not private) if any of the definition states so. A private method, method private method-name = expr, is a method that can only be invoked on self (from other methods of the same object, defined in this class or one of its subclasses). This invocation is performed using the expression value-name # method-name, where value-name is directly bound to self at the beginning of the class definition. Private methods do not appear in object types. A method may have both public and private definitions, but as soon as there is a public one, all subsequent definitions will be made public. Methods may have an explicitly polymorphic type, allowing them to be used polymorphically in programs (even for the same object). The explicit declaration may be done in one of three ways: (1) by giving an explicit polymorphic type in the method definition, immediately after the method name, i.e. method [private] method-name : {' ident}^+ . typexpr = expr; (2) by a forward declaration of the explicit polymorphic type through a virtual method definition; (3) by importing such a declaration through inheritance and/or constraining the type of self. Some special expressions are available in method bodies for manipulating instance variables and duplicating self: expr ::= ... | inst-var-name <- expr | {< [ inst-var-name = expr { ; inst-var-name = expr } [;] ] >} The expression inst-var-name <- expr modifies in-place the current object by replacing the value associated to inst-var-name by the value of expr. Of course, this instance variable must have been declared mutable. The expression {< inst-var-name_1 = expr_1 ; ... ; inst-var-name_n = expr_n >} evaluates to a copy of the current object in which the values of instance variables inst-var-name_1, ..., inst-var-name_n have been replaced by the values of the corresponding expressions expr_1, ..., expr_n. Virtual method definition ------------------------- A method specification is written method [private] virtual method-name : poly-typexpr. It specifies whether the method is public or private, and gives its type. If the method is intended to be polymorphic, the type must be explicitly polymorphic. Constraints on type parameters ------------------------------ The construct constraint typexpr_1 = typexpr_2 forces the two type expressions to be equals. This is typically used to specify type parameters: in that way they can be bound to specific type expressions. Initializers ------------ A class initializer initializer expr specifies an expression that will be evaluated whenever an object is created from the class, once all its instance variables have been initialized. 6.9.3 Class definitions ======================== class-definition ::= class class-binding { and class-binding } class-binding ::= [virtual] [[ type-parameters ]] class-name {parameter} [: class-type] = class-expr type-parameters ::= ' ident { , ' ident } A class definition class class-binding { and class-binding } is recursive. Each class-binding defines a class-name that can be used in the whole expression except for inheritance. It can also be used for inheritance, but only in the definitions that follow its own. A class binding binds the class name class-name to the value of expression class-expr. It also binds the class type class-name to the type of the class, and defines two type abbreviations : class-name and # class-name. The first one is the type of objects of this class, while the second is more general as it unifies with the type of any object belonging to a subclass (see section 6.4). Virtual class ------------- A class must be flagged virtual if one of its methods is virtual (that is, appears in the class type, but is not actually defined). Objects cannot be created from a virtual class. Type parameters --------------- The class type parameters correspond to the ones of the class type and of the two type abbreviations defined by the class binding. They must be bound to actual types in the class definition using type constraints. So that the abbreviations are well-formed, type variables of the inferred type of the class must either be type parameters or be bound in the constraint clause. 6.9.4 Class specifications =========================== class-specification ::= class class-spec { and class-spec } class-spec ::= [virtual] [[ type-parameters ]] class-name : class-type This is the counterpart in signatures of class definitions. A class specification matches a class definition if they have the same type parameters and their types match. 6.9.5 Class type definitions ============================= classtype-definition ::= class type classtype-def { and classtype-def } classtype-def ::= [virtual] [[ type-parameters ]] class-name = class-body-type A class type definition class class-name = class-body-type defines an abbreviation class-name for the class body type class-body-type. As for class definitions, two type abbreviations class-name and # class-name are also defined. The definition can be parameterized by some type parameters. If any method in the class type body is virtual, the definition must be flagged virtual. Two class type definitions match if they have the same type parameters and they expand to matching types. 6.10 Module types (module specifications) *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= Module types are the module-level equivalent of type expressions: they specify the general shape and type properties of modules. module-type ::= modtype-path | sig { specification [;;] } end | functor ( module-name : module-type ) -> module-type | module-type -> module-type | module-type with mod-constraint { and mod-constraint } | ( module-type ) mod-constraint ::= type [type-params] typeconstr type-equation { type-constraint } | module module-path = extended-module-path specification ::= val value-name : typexpr | external value-name : typexpr = external-declaration | type-definition | exception constr-decl | class-specification | classtype-definition | module module-name : module-type | module module-name { ( module-name : module-type ) } : module-type | module type modtype-name | module type modtype-name = module-type | open module-path | include module-type See also the following language extensions: recovering the type of a module, substitution inside a signature, type-level module aliases, attributes, extension nodes and generative functors. 6.10.1 Simple module types =========================== The expression modtype-path is equivalent to the module type bound to the name modtype-path. The expression ( module-type ) denotes the same type as module-type. 6.10.2 Signatures ================== Signatures are type specifications for structures. Signatures sig ... end are collections of type specifications for value names, type names, exceptions, module names and module type names. A structure will match a signature if the structure provides definitions (implementations) for all the names specified in the signature (and possibly more), and these definitions meet the type requirements given in the signature. An optional ;; is allowed after each specification in a signature. It serves as a syntactic separator with no semantic meaning. Value specifications -------------------- A specification of a value component in a signature is written val value-name : typexpr, where value-name is the name of the value and typexpr its expected type. The form external value-name : typexpr = external-declaration is similar, except that it requires in addition the name to be implemented as the external function specified in external-declaration (see chapter 19). Type specifications ------------------- A specification of one or several type components in a signature is written type typedef { and typedef } and consists of a sequence of mutually recursive definitions of type names. Each type definition in the signature specifies an optional type equation = typexpr and an optional type representation = constr-decl ... or = { field-decl ... }. The implementation of the type name in a matching structure must be compatible with the type expression specified in the equation (if given), and have the specified representation (if given). Conversely, users of that signature will be able to rely on the type equation or type representation, if given. More precisely, we have the following four situations: Abstract type: no equation, no representation. Names that are defined as abstract types in a signature can be implemented in a matching structure by any kind of type definition (provided it has the same number of type parameters). The exact implementation of the type will be hidden to the users of the structure. In particular, if the type is implemented as a variant type or record type, the associated constructors and fields will not be accessible to the users; if the type is implemented as an abbreviation, the type equality between the type name and the right-hand side of the abbreviation will be hidden from the users of the structure. Users of the structure consider that type as incompatible with any other type: a fresh type has been generated. Type abbreviation: an equation = typexpr, no representation. The type name must be implemented by a type compatible with typexpr. All users of the structure know that the type name is compatible with typexpr. New variant type or record type: no equation, a representation. The type name must be implemented by a variant type or record type with exactly the constructors or fields specified. All users of the structure have access to the constructors or fields, and can use them to create or inspect values of that type. However, users of the structure consider that type as incompatible with any other type: a fresh type has been generated. Re-exported variant type or record type: an equation, a representation. This case combines the previous two: the representation of the type is made visible to all users, and no fresh type is generated. Exception specification ----------------------- The specification exception constr-decl in a signature requires the matching structure to provide an exception with the name and arguments specified in the definition, and makes the exception available to all users of the structure. Class specifications -------------------- A specification of one or several classes in a signature is written class class-spec { and class-spec } and consists of a sequence of mutually recursive definitions of class names. Class specifications are described more precisely in section 6.9.4. Class type specifications ------------------------- A specification of one or several classe types in a signature is written class type classtype-def { and classtype-def } and consists of a sequence of mutually recursive definitions of class type names. Class type specifications are described more precisely in section 6.9.5. Module specifications --------------------- A specification of a module component in a signature is written module module-name : module-type, where module-name is the name of the module component and module-type its expected type. Modules can be nested arbitrarily; in particular, functors can appear as components of structures and functor types as components of signatures. For specifying a module component that is a functor, one may write module module-name ( name_1 : module-type_1 ) ... ( name_n : module-type_n ) : module-type instead of module module-name : functor ( name_1 : module-type_1 ) -> ... -> module-type Module type specifications -------------------------- A module type component of a signature can be specified either as a manifest module type or as an abstract module type. An abstract module type specification module type modtype-name allows the name modtype-name to be implemented by any module type in a matching signature, but hides the implementation of the module type to all users of the signature. A manifest module type specification module type modtype-name = module-type requires the name modtype-name to be implemented by the module type module-type in a matching signature, but makes the equality between modtype-name and module-type apparent to all users of the signature. Opening a module path --------------------- The expression open module-path in a signature does not specify any components. It simply affects the parsing of the following items of the signature, allowing components of the module denoted by module-path to be referred to by their simple names name instead of path accesses module-path . name. The scope of the open stops at the end of the signature expression. Including a signature --------------------- The expression include module-type in a signature performs textual inclusion of the components of the signature denoted by module-type. It behaves as if the components of the included signature were copied at the location of the include. The module-type argument must refer to a module type that is a signature, not a functor type. 6.10.3 Functor types ===================== The module type expression functor ( module-name : module-type_1 ) -> module-type_2 is the type of functors (functions from modules to modules) that take as argument a module of type module-type_1 and return as result a module of type module-type_2. The module type module-type_2 can use the name module-name to refer to type components of the actual argument of the functor. If the type module-type_2 does not depend on type components of module-name, the module type expression can be simplified with the alternative short syntax module-type_1 -> module-type_2 . No restrictions are placed on the type of the functor argument; in particular, a functor may take another functor as argument ("higher-order" functor). 6.10.4 The with operator ========================= Assuming module-type denotes a signature, the expression module-type with mod-constraint { and mod-constraint } denotes the same signature where type equations have been added to some of the type specifications, as described by the constraints following the with keyword. The constraint type [type-parameters] typeconstr = typexpr adds the type equation = typexpr to the specification of the type component named typeconstr of the constrained signature. The constraint module module-path = extended-module-path adds type equations to all type components of the sub-structure denoted by module-path, making them equivalent to the corresponding type components of the structure denoted by extended-module-path. For instance, if the module type name S is bound to the signature << sig type t module M: (sig type u end) end >> then S with type t=int denotes the signature << sig type t=int module M: (sig type u end) end >> and S with module M = N denotes the signature << sig type t module M: (sig type u=N.u end) end >> A functor taking two arguments of type S that share their t component is written << functor (A: S) (B: S with type t = A.t) ... >> Constraints are added left to right. After each constraint has been applied, the resulting signature must be a subtype of the signature before the constraint was applied. Thus, the with operator can only add information on the type components of a signature, but never remove information. 6.11 Module expressions (module implementations) *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* Module expressions are the module-level equivalent of value expressions: they evaluate to modules, thus providing implementations for the specifications expressed in module types. module-expr ::= module-path | struct [ module-items ] end | functor ( module-name : module-type ) -> module-expr | module-expr ( module-expr ) | ( module-expr ) | ( module-expr : module-type ) module-items ::= {;;} ( definition | expr ) { {;;} ( definition | ;; expr) } {;;} definition ::= let [rec] let-binding { and let-binding } | external value-name : typexpr = external-declaration | type-definition | exception-definition | class-definition | classtype-definition | module module-name { ( module-name : module-type ) } [ : module-type ] = module-expr | module type modtype-name = module-type | open module-path | include module-expr See also the following language extensions: recursive modules, first-class modules, overriding in open statements, attributes, extension nodes and generative functors. 6.11.1 Simple module expressions ================================= The expression module-path evaluates to the module bound to the name module-path. The expression ( module-expr ) evaluates to the same module as module-expr. The expression ( module-expr : module-type ) checks that the type of module-expr is a subtype of module-type, that is, that all components specified in module-type are implemented in module-expr, and their implementation meets the requirements given in module-type. In other terms, it checks that the implementation module-expr meets the type specification module-type. The whole expression evaluates to the same module as module-expr, except that all components not specified in module-type are hidden and can no longer be accessed. 6.11.2 Structures ================== Structures struct ... end are collections of definitions for value names, type names, exceptions, module names and module type names. The definitions are evaluated in the order in which they appear in the structure. The scopes of the bindings performed by the definitions extend to the end of the structure. As a consequence, a definition may refer to names bound by earlier definitions in the same structure. For compatibility with toplevel phrases (chapter 9), optional ;; are allowed after and before each definition in a structure. These ;; have no semantic meanings. Similarly, an expr preceded by ;; is allowed as a component of a structure. It is equivalent to let _ = expr, i.e. expr is evaluated for its side-effects but is not bound to any identifier. If expr is the first component of a structure, the preceding ;; can be omitted. Value definitions ----------------- A value definition let [rec] let-binding { and let-binding } bind value names in the same way as a let ... in ... expression (see section 6.7.1). The value names appearing in the left-hand sides of the bindings are bound to the corresponding values in the right-hand sides. A value definition external value-name : typexpr = external-declaration implements value-name as the external function specified in external-declaration (see chapter 19). Type definitions ---------------- A definition of one or several type components is written type typedef { and typedef } and consists of a sequence of mutually recursive definitions of type names. Exception definitions --------------------- Exceptions are defined with the syntax exception constr-decl or exception constr-name = constr. Class definitions ----------------- A definition of one or several classes is written class class-binding { and class-binding } and consists of a sequence of mutually recursive definitions of class names. Class definitions are described more precisely in section 6.9.3. Class type definitions ---------------------- A definition of one or several classes is written class type classtype-def { and classtype-def } and consists of a sequence of mutually recursive definitions of class type names. Class type definitions are described more precisely in section 6.9.5. Module definitions ------------------ The basic form for defining a module component is module module-name = module-expr, which evaluates module-expr and binds the result to the name module-name. One can write module module-name : module-type = module-expr instead of module module-name = ( module-expr : module-type ). Another derived form is module module-name ( name_1 : module-type_1 ) ... ( name_n : module-type_n ) = module-expr which is equivalent to module module-name = functor ( name_1 : module-type_1 ) -> ... -> module-expr Module type definitions ----------------------- A definition for a module type is written module type modtype-name = module-type. It binds the name modtype-name to the module type denoted by the expression module-type. Opening a module path --------------------- The expression open module-path in a structure does not define any components nor perform any bindings. It simply affects the parsing of the following items of the structure, allowing components of the module denoted by module-path to be referred to by their simple names name instead of path accesses module-path . name. The scope of the open stops at the end of the structure expression. Including the components of another structure --------------------------------------------- The expression include module-expr in a structure re-exports in the current structure all definitions of the structure denoted by module-expr. For instance, if the identifier S is bound to the module << struct type t = int let x = 2 end >> the module expression << struct include S let y = (x + 1 : t) end >> is equivalent to the module expression << struct type t = S.t let x = S.x let y = (x + 1 : t) end >> The difference between open and include is that open simply provides short names for the components of the opened structure, without defining any components of the current structure, while include also adds definitions for the components of the included structure. 6.11.3 Functors ================ Functor definition ------------------ The expression functor ( module-name : module-type ) -> module-expr evaluates to a functor that takes as argument modules of the type module-type_1, binds module-name to these modules, evaluates module-expr in the extended environment, and returns the resulting modules as results. No restrictions are placed on the type of the functor argument; in particular, a functor may take another functor as argument ("higher-order" functor). Functor application ------------------- The expression module-expr_1 ( module-expr_2 ) evaluates module-expr_1 to a functor and module-expr_2 to a module, and applies the former to the latter. The type of module-expr_2 must match the type expected for the arguments of the functor module-expr_1. 6.12 Compilation units *=*=*=*=*=*=*=*=*=*=*=* unit-interface ::= { specification [;;] } unit-implementation ::= [ module-items ] Compilation units bridge the module system and the separate compilation system. A compilation unit is composed of two parts: an interface and an implementation. The interface contains a sequence of specifications, just as the inside of a sig ... end signature expression. The implementation contains a sequence of definitions and expressions, just as the inside of a struct ... end module expression. A compilation unit also has a name unit-name, derived from the names of the files containing the interface and the implementation (see chapter 8 for more details). A compilation unit behaves roughly as the module definition module unit-name : sig unit-interface end = struct unit-implementation end A compilation unit can refer to other compilation units by their names, as if they were regular modules. For instance, if U is a compilation unit that defines a type t, other compilation units can refer to that type under the name U.t; they can also refer to U as a whole structure. Except for names of other compilation units, a unit interface or unit implementation must not have any other free variables. In other terms, the type-checking and compilation of an interface or implementation proceeds in the initial environment name_1 : sig specification_1 end ... name_n : sig specification_n end where name_1 ... name_n are the names of the other compilation units available in the search path (see chapter 8 for more details) and specification_1 ... specification_n are their respective interfaces. Chapter 7 Language extensions ******************************** This chapter describes language extensions and convenience features that are implemented in OCaml, but not described in the OCaml reference manual. 7.1 Integer literals for types int32, int64 and nativeint *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= (Introduced in Objective Caml 3.07) constant ::= ... | int32-literal | int64-literal | nativeint-literal int32-literal ::= integer-literal l int64-literal ::= integer-literal L nativeint-literal ::= integer-literal n An integer literal can be followed by one of the letters l, L or n to indicate that this integer has type int32, int64 or nativeint respectively, instead of the default type int for integer literals. The library modules Int32[], Int64[] and Nativeint[] provide operations on these integer types. 7.2 Recursive definitions of values *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= (Introduced in Objective Caml 1.00) As mentioned in section 6.7.1, the let rec binding construct, in addition to the definition of recursive functions, also supports a certain class of recursive definitions of non-functional values, such as let rec name_1 = 1 :: name_2 and name_2 = 2 :: name_1 in expr which binds name_1 to the cyclic list 1::2::1::2::..., and name_2 to the cyclic list 2::1::2::1::...Informally, the class of accepted definitions consists of those definitions where the defined names occur only inside function bodies or as argument to a data constructor. More precisely, consider the expression: let rec name_1 = expr_1 and ... and name_n = expr_n in expr It will be accepted if each one of expr_1 ... expr_n is statically constructive with respect to name_1 ... name_n, is not immediately linked to any of name_1 ... name_n, and is not an array constructor whose arguments have abstract type. An expression e is said to be statically constructive with respect to the variables name_1 ... name_n if at least one of the following conditions is true: - e has no free occurrence of any of name_1 ... name_n - e is a variable - e has the form fun ... -> ... - e has the form function ... -> ... - e has the form lazy ( ... ) - e has one of the following forms, where each one of expr_1 ... expr_m is statically constructive with respect to name_1 ... name_n, and expr_0 is statically constructive with respect to name_1 ... name_n, xname_1 ... xname_m: - let [rec] xname_1 = expr_1 and ... and xname_m = expr_m in expr_0 - let module ... in expr_1 - constr ( expr_1, ... , expr_m) - `tag-name ( expr_1, ... , expr_m) - [| expr_1; ... ; expr_m |] - { field_1 = expr_1; ... ; field_m = expr_m } - { expr_1 with field_2 = expr_2; ... ; field_m = expr_m } where expr_1 is not immediately linked to name_1 ... name_n - ( expr_1, ... , expr_m ) - expr_1; ... ; expr_m An expression e is said to be immediately linked to the variable name in the following cases: - e is name - e has the form expr_1; ... ; expr_m where expr_m is immediately linked to name - e has the form let [rec] xname_1 = expr_1 and ... and xname_m = expr_m in expr_0 where expr_0 is immediately linked to name or to one of the xname_i such that expr_i is immediately linked to name. 7.3 Lazy patterns *=*=*=*=*=*=*=*=*= (Introduced in Objective Caml 3.11) pattern ::= ... | lazy pattern The pattern lazy pattern matches a value v of type Lazy.t, provided pattern matches the result of forcing v with Lazy.force. A successful match of a pattern containing lazy sub-patterns forces the corresponding parts of the value being matched, even those that imply no test such as lazy value-name or lazy _. Matching a value with a pattern-matching where some patterns contain lazy sub-patterns may imply forcing parts of the value, even when the pattern selected in the end has no lazy sub-pattern. For more information, see the description of module Lazy in the standard library ( section 24.19). 7.4 Recursive modules *=*=*=*=*=*=*=*=*=*=*= (Introduced in Objective Caml 3.07) definition ::= ... | module rec module-name : module-type = module-expr { and module-name : module-type = module-expr } specification ::= ... | module rec module-name : module-type { and module-name: module-type } Recursive module definitions, introduced by the module rec ...and ... construction, generalize regular module definitions module module-name = module-expr and module specifications module module-name : module-type by allowing the defining module-expr and the module-type to refer recursively to the module identifiers being defined. A typical example of a recursive module definition is: << module rec A : sig type t = Leaf of string | Node of ASet.t val compare: t -> t -> int end = struct type t = Leaf of string | Node of ASet.t let compare t1 t2 = match (t1, t2) with (Leaf s1, Leaf s2) -> Pervasives.compare s1 s2 | (Leaf _, Node _) -> 1 | (Node _, Leaf _) -> -1 | (Node n1, Node n2) -> ASet.compare n1 n2 end and ASet : Set.S with type elt = A.t = Set.Make(A) >> It can be given the following specification: << module rec A : sig type t = Leaf of string | Node of ASet.t val compare: t -> t -> int end and ASet : Set.S with type elt = A.t >> This is an experimental extension of OCaml: the class of recursive definitions accepted, as well as its dynamic semantics are not final and subject to change in future releases. Currently, the compiler requires that all dependency cycles between the recursively-defined module identifiers go through at least one "safe" module. A module is "safe" if all value definitions that it contains have function types typexpr_1 -> typexpr_2. Evaluation of a recursive module definition proceeds by building initial values for the safe modules involved, binding all (functional) values to fun _ -> raise Undefined_recursive_module. The defining module expressions are then evaluated, and the initial values for the safe modules are replaced by the values thus computed. If a function component of a safe module is applied during this computation (which corresponds to an ill-founded recursive definition), the Undefined_recursive_module exception is raised. Note that, in the specification case, the module-types must be parenthesized if they use the with mod-constraint construct. 7.5 Private types *=*=*=*=*=*=*=*=*= Private type declarations in module signatures, of the form type t = private ..., enable libraries to reveal some, but not all aspects of the implementation of a type to clients of the library. In this respect, they strike a middle ground between abstract type declarations, where no information is revealed on the type implementation, and data type definitions and type abbreviations, where all aspects of the type implementation are publicized. Private type declarations come in three flavors: for variant and record types (section 7.5.1), for type abbreviations (section 7.5.2), and for row types (section 7.5.3). 7.5.1 Private variant and record types ======================================= (Introduced in Objective Caml 3.07) type-representation ::= ... | = private [ | ] constr-decl { | constr-decl } | = private record-decl Values of a variant or record type declared private can be de-structured normally in pattern-matching or via the expr . field notation for record accesses. However, values of these types cannot be constructed directly by constructor application or record construction. Moreover, assignment on a mutable field of a private record type is not allowed. The typical use of private types is in the export signature of a module, to ensure that construction of values of the private type always go through the functions provided by the module, while still allowing pattern-matching outside the defining module. For example: << module M : sig type t = private A | B of int val a : t val b : int -> t end = struct type t = A | B of int let a = A let b n = assert (n > 0); B n end >> Here, the private declaration ensures that in any value of type M.t, the argument to the B constructor is always a positive integer. With respect to the variance of their parameters, private types are handled like abstract types. That is, if a private type has parameters, their variance is the one explicitly given by prefixing the parameter by a `+' or a `-', it is invariant otherwise. 7.5.2 Private type abbreviations ================================= (Introduced in Objective Caml 3.11) type-equation ::= ... | = private typexpr Unlike a regular type abbreviation, a private type abbreviation declares a type that is distinct from its implementation type typexpr. However, coercions from the type to typexpr are permitted. Moreover, the compiler "knows" the implementation type and can take advantage of this knowledge to perform type-directed optimizations. For ambiguity reasons, typexpr cannot be an object or polymorphic variant type, but a similar behaviour can be obtained through private row types. The following example uses a private type abbreviation to define a module of nonnegative integers: << module N : sig type t = private int val of_int: int -> t val to_int: t -> int end = struct type t = int let of_int n = assert (n >= 0); n let to_int n = n end >> The type N.t is incompatible with int, ensuring that nonnegative integers and regular integers are not confused. However, if x has type N.t, the coercion (x :> int) is legal and returns the underlying integer, just like N.to_int x. Deep coercions are also supported: if l has type N.t list, the coercion (l :> int list) returns the list of underlying integers, like List.map N.to_int l but without copying the list l. Note that the coercion ( expr :> typexpr ) is actually an abbreviated form, and will only work in presence of private abbreviations if neither the type of expr nor typexpr contain any type variables. If they do, you must use the full form ( expr : typexpr_1 :> typexpr_2 ) where typexpr_1 is the expected type of expr. Concretely, this would be (x : N.t :> int) and (l : N.t list :> int list) for the above examples. 7.5.3 Private row types ======================== (Introduced in Objective Caml 3.09) type-equation ::= ... | = private typexpr Private row types are type abbreviations where part of the structure of the type is left abstract. Concretely typexpr in the above should denote either an object type or a polymorphic variant type, with some possibility of refinement left. If the private declaration is used in an interface, the corresponding implementation may either provide a ground instance, or a refined private type. << module M : sig type c = private < x : int; .. > val o : c end = struct class c = object method x = 3 method y = 2 end let o = new c end >> This declaration does more than hiding the y method, it also makes the type c incompatible with any other closed object type, meaning that only o will be of type c. In that respect it behaves similarly to private record types. But private row types are more flexible with respect to incremental refinement. This feature can be used in combination with functors. << module F(X : sig type c = private < x : int; .. > end) = struct let get_x (o : X.c) = o#x end module G(X : sig type c = private < x : int; y : int; .. > end) = struct include F(X) let get_y (o : X.c) = o#y end >> A polymorphic variant type [t], for example << type t = [ `A of int | `B of bool ] >> can be refined in two ways. A definition [u] may add new field to [t], and the declaration << type u = private [> t] >> will keep those new fields abstract. Construction of values of type [u] is possible using the known variants of [t], but any pattern-matching will require a default case to handle the potential extra fields. Dually, a declaration [u] may restrict the fields of [t] through abstraction: the declaration << type v = private [< t > `A] >> corresponds to private variant types. One cannot create a value of the private type [v], except using the constructors that are explicitly listed as present, (`A n) in this example; yet, when patter-matching on a [v], one should assume that any of the constructors of [t] could be present. Similarly to abstract types, the variance of type parameters is not inferred, and must be given explicitly. 7.6 Local opens *=*=*=*=*=*=*=*= (Introduced in OCaml 3.12, extended to patterns in 4.04) expr ::= ... | let open module-path in expr | module-path .( expr ) pattern ::= ... | module-path .( pattern ) The expressions let open module-path in expr and module-path.( expr) are strictly equivalent. On the pattern side, only the pattern module-path.( pattern) is available. These constructions locally open the module referred to by the module path module-path in the respective scope of the expression expr or pattern pattern. Restricting opening to the scope of a single expression or pattern instead of a whole structure allows one to benefit from shorter syntax to refer to components of the opened module, without polluting the global scope. Also, this can make the code easier to read (the open statement is closer to where it is used) and to refactor (because the code fragment is more self-contained). Local opens for delimited expressions or patterns (Introduced in OCaml 4.02) expr ::= ... | module-path .[ expr ] | module-path .[| expr |] | module-path .{ expr } | module-path .{< expr >} pattern ::= ... | module-path .[ pattern ] | module-path .[| pattern |] | module-path .{ pattern } When the body of a local open expression or pattern is delimited by [ ], [| |], or { }, the parentheses can be omitted. For expression, parentheses can also be omitted for {< >}. For example, module-path.[ expr] is equivalent to module-path.([ expr]), and module-path.[| pattern |] is equivalent to module-path.([| pattern |]). 7.7 Record and object notations *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= (Introduced in OCaml 3.12, object copy notation added in Ocaml 4.03) pattern ::= ... | { field [= pattern] { ; field [= pattern] } [; _ ] [;] } expr ::= ... | { field [= expr] { ; field [= expr] } [;] } | { expr with field [= expr] { ; field [= expr] } [;] } | { < expr with field [= expr] { ; field [= expr] } [;] > } In a record pattern, a record construction expression or an object copy expression, a single identifier id stands for id = id, and a qualified identifier module-path . id stands for module-path . id = id. For example, assuming the record type << type point = { x: float; y: float } >> has been declared, the following expressions are equivalent: << let x = 1. and y = 2. in { x = x; y = y }, let x = 1. and y = 2. in { x; y }, let x = 1. and y = 2. in { x = x; y } >> On the object side, all following methods are equivalent: << object val x=0. val y=0. val z=0. method f_0 x y = {< x; y >} method f_1 x y = {< x = x; y >} method f_2 x y = {< x=x ; y = y >} end >> Likewise, the following functions are equivalent: << fun {x = x; y = y} -> x +. y fun {x; y} -> x +. y >> Optionally, a record pattern can be terminated by ; _ to convey the fact that not all fields of the record type are listed in the record pattern and that it is intentional. By default, the compiler ignores the ; _ annotation. If warning 9 is turned on, the compiler will warn when a record pattern fails to list all fields of the corresponding record type and is not terminated by ; _. Continuing the point example above, << fun {x} -> x +. 1. >> will warn if warning 9 is on, while << fun {x; _} -> x +. 1. >> will not warn. This warning can help spot program points where record patterns may need to be modified after new fields are added to a record type. 7.8 Explicit polymorphic type annotations *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= (Introduced in OCaml 3.12) let-binding ::= ... | value-name : poly-typexpr = expr Polymorphic type annotations in let-definitions behave in a way similar to polymorphic methods: they explicitly require the defined value to be polymorphic, and allow one to use this polymorphism in recursive occurrences (when using let rec). Note however that this is a normal polymorphic type, unifiable with any instance of itself. There are two possible applications of this feature. One is polymorphic recursion: << type 'a t = Leaf of 'a | Node of ('a * 'a) t let rec depth : 'a. 'a t -> 'b = function Leaf _ -> 1 | Node x -> 1 + depth x >> Note that 'b is not explicitly polymorphic here, and it will actually be unified with int. The other application is to ensure that some definition is sufficiently polymorphic: <<#let id: 'a. 'a -> 'a = fun x -> x + 1;; Error: This definition has type int -> int which is less general than 'a. 'a -> 'a >> 7.9 Locally abstract types *=*=*=*=*=*=*=*=*=*=*=*=*=* (Introduced in OCaml 3.12, short syntax added in 4.03) parameter ::= ... + | ( type {typeconstr-name} ) The expression fun ( type typeconstr-name ) -> expr introduces a type constructor named typeconstr-name which is considered abstract in the scope of the sub-expression, but then replaced by a fresh type variable. Note that contrary to what the syntax could suggest, the expression fun ( type typeconstr-name ) -> expr itself does not suspend the evaluation of expr as a regular abstraction would. The syntax has been chosen to fit nicely in the context of function declarations, where it is generally used. It is possible to freely mix regular function parameters with pseudo type parameters, as in: << let f = fun (type t) (foo : t list) -> ... >> and even use the alternative syntax for declaring functions: << let f (type t) (foo : t list) = ... >> If several locally abstract types need to be introduced, it is possible to use the syntax fun ( type typeconstr-name_1 ... typeconstr-name_n ) -> expr as syntactic sugar for fun ( type typeconstr-name_1 ) -> ... -> fun ( type typeconstr-name_n ) -> expr. For instance, << let f = fun (type t u v) -> fun (foo : (t * u * v) list) -> ... let f' (type t u v) (foo : (t * u * v) list) = ... >> This construction is useful because the type constructors it introduces can be used in places where a type variable is not allowed. For instance, one can use it to define an exception in a local module within a polymorphic function. << let f (type t) () = let module M = struct exception E of t end in (fun x -> M.E x), (function M.E x -> Some x | _ -> None) >> Here is another example: << let sort_uniq (type s) (cmp : s -> s -> int) = let module S = Set.Make(struct type t = s let compare = cmp end) in fun l -> S.elements (List.fold_right S.add l S.empty) >> It is also extremely useful for first-class modules (see section 7.10) and generalized algebraic datatypes (GADTs: see section 7.16). Polymorphic syntax (Introduced in OCaml 4.00) let-binding ::= ... + | value-name : type { typeconstr-name } . typexpr = expr class-field ::= ... + | method [private] method-name : type { typeconstr-name } . typexpr = expr + | method! [private] method-name : type { typeconstr-name } . typexpr = expr The (type typeconstr-name) syntax construction by itself does not make polymorphic the type variable it introduces, but it can be combined with explicit polymorphic annotations where needed. The above rule is provided as syntactic sugar to make this easier: << let rec f : type t1 t2. t1 * t2 list -> t1 = ... >> is automatically expanded into << let rec f : 't1 't2. 't1 * 't2 list -> 't1 = fun (type t1) (type t2) -> (... : t1 * t2 list -> t1) >> This syntax can be very useful when defining recursive functions involving GADTs, see the section 7.16 for a more detailed explanation. The same feature is provided for method definitions. The method! form combines this extension with the "explicit overriding" extension described in section 7.14. 7.10 First-class modules *=*=*=*=*=*=*=*=*=*=*=*=* (Introduced in OCaml 3.12; pattern syntax and package type inference introduced in 4.00; structural comparison of package types introduced in 4.02.; fewer parens required starting from 4.05) typexpr ::= ... | (module package-type) module-expr ::= ... | (val expr [: package-type]) expr ::= ... | (module module-expr [: package-type]) pattern ::= ... | (module module-name [: package-type]) package-type ::= modtype-path | modtype-path with package-constraint { and package-constraint } package-constraint ::= type typeconstr = typexpr Modules are typically thought of as static components. This extension makes it possible to pack a module as a first-class value, which can later be dynamically unpacked into a module. The expression ( module module-expr : package-type ) converts the module (structure or functor) denoted by module expression module-expr to a value of the core language that encapsulates this module. The type of this core language value is ( module package-type ). The package-type annotation can be omitted if it can be inferred from the context. Conversely, the module expression ( val expr : package-type ) evaluates the core language expression expr to a value, which must have type module package-type, and extracts the module that was encapsulated in this value. Again package-type can be omitted if the type of expr is known. If the module expression is already parenthesized, like the arguments of functors are, no additional parens are needed: Map.Make(val key). The pattern ( module module-name : package-type ) matches a package with type package-type and binds it to module-name. It is not allowed in toplevel let bindings. Again package-type can be omitted if it can be inferred from the enclosing pattern. The package-type syntactic class appearing in the ( module package-type ) type expression and in the annotated forms represents a subset of module types. This subset consists of named module types with optional constraints of a limited form: only non-parametrized types can be specified. For type-checking purposes (and starting from OCaml 4.02), package types are compared using the structural comparison of module types. In general, the module expression ( val expr : package-type ) cannot be used in the body of a functor, because this could cause unsoundness in conjunction with applicative functors. Since OCaml 4.02, this is relaxed in two ways: if package-type does not contain nominal type declarations (i.e. types that are created with a proper identity), then this expression can be used anywhere, and even if it contains such types it can be used inside the body of a generative functor, described in section 7.23. It can also be used anywhere in the context of a local module binding let module module-name = ( val expr_1 : package-type ) in expr_2. Basic example A typical use of first-class modules is to select at run-time among several implementations of a signature. Each implementation is a structure that we can encapsulate as a first-class module, then store in a data structure such as a hash table: << module type DEVICE = sig ... end let devices : (string, (module DEVICE)) Hashtbl.t = Hashtbl.create 17 module SVG = struct ... end let _ = Hashtbl.add devices "SVG" (module SVG : DEVICE) module PDF = struct ... end let _ = Hashtbl.add devices "PDF" (module PDF: DEVICE) >> We can then select one implementation based on command-line arguments, for instance: << module Device = (val (try Hashtbl.find devices (parse_cmdline()) with Not_found -> eprintf "Unknown device %s\n"; exit 2) : DEVICE) >> Alternatively, the selection can be performed within a function: << let draw_using_device device_name picture = let module Device = (val (Hashtbl.find_devices device_name) : DEVICE) in Device.draw picture >> Advanced examples With first-class modules, it is possible to parametrize some code over the implementation of a module without using a functor. << let sort (type s) (module Set : Set.S with type elt = s) l = Set.elements (List.fold_right Set.add l Set.empty) val sort : (module Set.S with type elt = 'a) -> 'a list -> 'a list >> To use this function, one can wrap the Set.Make functor: << let make_set (type s) cmp = let module S = Set.Make(struct type t = s let compare = cmp end) in (module S : Set.S with type elt = s) val make_set : ('a -> 'a -> int) -> (module Set.S with type elt = 'a) >> 7.11 Recovering the type of a module *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* (Introduced in OCaml 3.12) module-type ::= ... | module type of module-expr The construction module type of module-expr expands to the module type (signature or functor type) inferred for the module expression module-expr. To make this module type reusable in many situations, it is intentionally not strengthened: abstract types and datatypes are not explicitly related with the types of the original module. For the same reason, module aliases in the inferred type are expanded. A typical use, in conjunction with the signature-level include construct, is to extend the signature of an existing structure. In that case, one wants to keep the types equal to types in the original module. This can done using the following idiom. << module type MYHASH = sig include module type of struct include Hashtbl end val replace: ('a, 'b) t -> 'a -> 'b -> unit end >> The signature MYHASH then contains all the fields of the signature of the module Hashtbl (with strengthened type definitions), plus the new field replace. An implementation of this signature can be obtained easily by using the include construct again, but this time at the structure level: << module MyHash : MYHASH = struct include Hashtbl let replace t k v = remove t k; add t k v end >> Another application where the absence of strengthening comes handy, is to provide an alternative implementation for an existing module. << module MySet : module type of Set = struct ... end >> This idiom guarantees that Myset is compatible with Set, but allows it to represent sets internally in a different way. 7.12 Substituting inside a signature *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* (Introduced in OCaml 3.12) mod-constraint ::= ... | type [type-params] typeconstr-name := typexpr | module module-name := extended-module-path "Destructive" substitution (with ... := ...) behaves essentially like normal signature constraints (with ... = ...), but it additionally removes the redefined type or module from the signature. There are a number of restrictions: one can only remove types and modules at the outermost level (not inside submodules), and in the case of with type the definition must be another type constructor with the same type parameters. A natural application of destructive substitution is merging two signatures sharing a type name. <<# module type Printable = sig # type t # val print : Format.formatter -> t -> unit # end # module type Comparable = sig # type t # val compare : t -> t -> int # end # module type PrintableComparable = sig # include Printable # include Comparable with type t := t # end;; >> One can also use this to completely remove a field: <<#module type S = Comparable with type t := int;; module type S = sig val compare : int -> int -> int end >> or to rename one: <<#module type S = sig # type u # include Comparable with type t := u #end;; module type S = sig type u val compare : u -> u -> int end >> Note that you can also remove manifest types, by substituting with the same type. <<#module type ComparableInt = Comparable with type t = int ;; module type ComparableInt = sig type t = int val compare : t -> t -> int end >> <<#module type CompareInt = ComparableInt with type t := int ;; module type CompareInt = sig val compare : int -> int -> int end >> 7.13 Type-level module aliases *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* (Introduced in OCaml 4.02) specification ::= ... | module module-name = module-path The above specification, inside a signature, only matches a module definition equal to module-path. Conversely, a type-level module alias can be matched by itself, or by any supertype of the type of the module it references. There are several restrictions on module-path: 1. it should be of the form M_0.M_1...M_n (i.e. without functor applications); 2. inside the body of a functor, M_0 should not be one of the functor parameters; 3. inside a recursive module definition, M_0 should not be one of the recursively defined modules. Such specifications are also inferred. Namely, when P is a path satisfying the above constraints, <<#module N = P;; >> has type <> Type-level module aliases are used when checking module path equalities. That is, in a context where module name N is known to be an alias for P, not only these two module paths check as equal, but F (N) and F (P) are also recognized as equal. In the default compilation mode, this is the only difference with the previous approach of module aliases having just the same module type as the module they reference. When the compiler flag -no-alias-deps is enabled, type-level module aliases are also exploited to avoid introducing dependencies between compilation units. Namely, a module alias referring to a module inside another compilation unit does not introduce a link-time dependency on that compilation unit, as long as it is not dereferenced; it still introduces a compile-time dependency if the interface needs to be read, i.e. if the module is a submodule of the compilation unit, or if some type components are referred to. Additionally, accessing a module alias introduces a link-time dependency on the compilation unit containing the module referenced by the alias, rather than the compilation unit containing the alias. Note that these differences in link-time behavior may be incompatible with the previous behavior, as some compilation units might not be extracted from libraries, and their side-effects ignored. These weakened dependencies make possible to use module aliases in place of the -pack mechanism. Suppose that you have a library Mylib composed of modules A and B. Using -pack, one would issue the command line << ocamlc -pack a.cmo b.cmo -o mylib.cmo >> and as a result obtain a Mylib compilation unit, containing physically A and B as submodules, and with no dependencies on their respective compilation units. Here is a concrete example of a possible alternative approach: 1. Rename the files containing A and B to Mylib_A and Mylib_B. 2. Create a packing interface Mylib.ml, containing the following lines. << module A = Mylib_A module B = Mylib_B >> 3. Compile Mylib.ml using -no-alias-deps, and the other files using -no-alias-deps and -open Mylib (the last one is equivalent to adding the line open! Mylib at the top of each file). << ocamlc -c -no-alias-deps Mylib.ml ocamlc -c -no-alias-deps -open Mylib Mylib_*.mli Mylib_*.ml >> 4. Finally, create a library containing all the compilation units, and export all the compiled interfaces. << ocamlc -a Mylib*.cmo -o Mylib.cma >> This approach lets you access A and B directly inside the library, and as Mylib.A and Mylib.B from outside. It also has the advantage that Mylib is no longer monolithic: if you use Mylib.A, only Mylib_A will be linked in, not Mylib_B. 7.14 Explicit overriding in class definitions *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= (Introduced in OCaml 3.12) class-field ::= ... | inherit! class-expr [as lowercase-ident] | val! [mutable] inst-var-name [: typexpr] = expr | method! [private] method-name {parameter} [: typexpr] = expr | method! [private] method-name : poly-typexpr = expr The keywords inherit!, val! and method! have the same semantics as inherit, val and method, but they additionally require the definition they introduce to be an overriding. Namely, method! requires method-name to be already defined in this class, val! requires inst-var-name to be already defined in this class, and inherit! requires class-expr to override some definitions. If no such overriding occurs, an error is signaled. As a side-effect, these 3 keywords avoid the warnings 7 (method override) and 13 (instance variable override). Note that warning 7 is disabled by default. 7.15 Overriding in open statements *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* (Introduced in OCaml 4.01) definition ::= ... | open! module-path specification ::= ... | open! module-path expr ::= ... | let open! module-path in expr Since OCaml 4.01, open statements shadowing an existing identifier (which is later used) trigger the warning 44. Adding a ! character after the open keyword indicates that such a shadowing is intentional and should not trigger the warning. 7.16 Generalized algebraic datatypes *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* (Introduced in OCaml 4.00) constr-decl ::= ... | constr-name : [ constr-args -> ] typexpr type-param ::= ... | [variance] _ Generalized algebraic datatypes, or GADTs, extend usual sum types in two ways: constraints on type parameters may change depending on the value constructor, and some type variables may be existentially quantified. Adding constraints is done by giving an explicit return type (the rightmost typexpr in the above syntax), where type parameters are instantiated. This return type must use the same type constructor as the type being defined, and have the same number of parameters. Variables are made existential when they appear inside a constructor's argument, but not in its return type. Since the use of a return type often eliminates the need to name type parameters in the left-hand side of a type definition, one can replace them with anonymous types _ in that case. The constraints associated to each constructor can be recovered through pattern-matching. Namely, if the type of the scrutinee of a pattern-matching contains a locally abstract type, this type can be refined according to the constructor used. These extra constraints are only valid inside the corresponding branch of the pattern-matching. If a constructor has some existential variables, fresh locally abstract types are generated, and they must not escape the scope of this branch. Recursive functions Here is a concrete example: << type _ term = | Int : int -> int term | Add : (int -> int -> int) term | App : ('b -> 'a) term * 'b term -> 'a term let rec eval : type a. a term -> a = function | Int n -> n (* a = int *) | Add -> (fun x y -> x+y) (* a = int -> int -> int *) | App(f,x) -> (eval f) (eval x) (* eval called at types (b->a) and b for fresh b *) let two = eval (App (App (Add, Int 1), Int 1)) val two : int = 2 >> It is important to remark that the function eval is using the polymorphic syntax for locally abstract types. When defining a recursive function that manipulates a GADT, explicit polymorphic recursion should generally be used. For instance, the following definition fails with a type error: << let rec eval (type a) : a term -> a = function | Int n -> n | Add -> (fun x y -> x+y) | App(f,x) -> (eval f) (eval x) (* ^ Error: This expression has type ($App_'b -> a) term but an expression was expected of type 'a The type constructor $App_'b would escape its scope *) >> In absence of an explicit polymorphic annotation, a monomorphic type is inferred for the recursive function. If a recursive call occurs inside the function definition at a type that involves an existential GADT type variable, this variable flows to the type of the recursive function, and thus escapes its scope. In the above example, this happens in the branch App(f,x) when eval is called with f as an argument. In this branch, the type of f is ($App_ 'b-> a). The prefix $ in $App_ 'b denotes an existential type named by the compiler (see 7.16). Since the type of eval is 'a term -> 'a, the call eval f makes the existential type $App_'b flow to the type variable 'a and escape its scope. This triggers the above error. Type inference Type inference for GADTs is notoriously hard. This is due to the fact some types may become ambiguous when escaping from a branch. For instance, in the Int case above, n could have either type int or a, and they are not equivalent outside of that branch. As a first approximation, type inference will always work if a pattern-matching is annotated with types containing no free type variables (both on the scrutinee and the return type). This is the case in the above example, thanks to the type annotation containing only locally abstract types. In practice, type inference is a bit more clever than that: type annotations do not need to be immediately on the pattern-matching, and the types do not have to be always closed. As a result, it is usually enough to only annotate functions, as in the example above. Type annotations are propagated in two ways: for the scrutinee, they follow the flow of type inference, in a way similar to polymorphic methods; for the return type, they follow the structure of the program, they are split on functions, propagated to all branches of a pattern matching, and go through tuples, records, and sum types. Moreover, the notion of ambiguity used is stronger: a type is only seen as ambiguous if it was mixed with incompatible types (equated by constraints), without type annotations between them. For instance, the following program types correctly. << let rec sum : type a. a term -> _ = fun x -> let y = match x with | Int n -> n | Add -> 0 | App(f,x) -> sum f + sum x in y + 1 val sum : 'a term -> int = >> Here the return type int is never mixed with a, so it is seen as non-ambiguous, and can be inferred. When using such partial type annotations we strongly suggest specifying the -principal mode, to check that inference is principal. The exhaustiveness check is aware of GADT constraints, and can automatically infer that some cases cannot happen. For instance, the following pattern matching is correctly seen as exhaustive (the Add case cannot happen). << let get_int : int term -> int = function | Int n -> n | App(_,_) -> 0 >> Refutation cases and redundancy (Introduced in OCaml 4.03) Usually, the exhaustiveness check only tries to check whether the cases omitted from the pattern matching are typable or not. However, you can force it to try harder by adding refutation cases: matching-case ::= pattern [when expr] -> expr | pattern -> . In presence of a refutation case, the exhaustiveness check will first compute the intersection of the pattern with the complement of the cases preceding it. It then checks whether the resulting patterns can really match any concrete values by trying to type-check them. Wild cards in the generated patterns are handled in a special way: if their type is a variant type with only GADT constructors, then the pattern is split into the different constructors, in order to check whether any of them is possible (this splitting is not done for arguments of these constructors, to avoid non-termination.) We also split tuples and variant types with only one case, since they may contain GADTs inside. For instance, the following code is deemed exhaustive: << type _ t = | Int : int t | Bool : bool t let deep : (char t * int) option -> char = function | None -> 'c' | _ -> . >> Namely, the inferred remaining case is Some _, which is split into Some (Int, _) and Some (Bool, _), which are both untypable. Note that the refutation case could be omitted here, because it is automatically added when there is only one case in the pattern matching. Another addition is that the redundancy check is now aware of GADTs: a case will be detected as redundant if it could be replaced by a refutation case using the same pattern. Advanced examples The term type we have defined above is an indexed type, where a type parameter reflects a property of the value contents. Another use of GADTs is singleton types, where a GADT value represents exactly one type. This value can be used as runtime representation for this type, and a function receiving it can have a polytypic behavior. Here is an example of a polymorphic function that takes the runtime representation of some type t and a value of the same type, then pretty-prints the value as a string: << type _ typ = | Int : int typ | String : string typ | Pair : 'a typ * 'b typ -> ('a * 'b) typ let rec to_string: type t. t typ -> t -> string = fun t x -> match t with | Int -> string_of_int x | String -> Printf.sprintf "%S" x | Pair(t1,t2) -> let (x1, x2) = x in Printf.sprintf "(%s,%s)" (to_string t1 x1) (to_string t2 x2) >> Another frequent application of GADTs is equality witnesses. << type (_,_) eq = Eq : ('a,'a) eq let cast : type a b. (a,b) eq -> a -> b = fun Eq x -> x >> Here type eq has only one constructor, and by matching on it one adds a local constraint allowing the conversion between a and b. By building such equality witnesses, one can make equal types which are syntactically different. Here is an example using both singleton types and equality witnesses to implement dynamic types. << let rec eq_type : type a b. a typ -> b typ -> (a,b) eq option = fun a b -> match a, b with | Int, Int -> Some Eq | String, String -> Some Eq | Pair(a1,a2), Pair(b1,b2) -> begin match eq_type a1 b1, eq_type a2 b2 with | Some Eq, Some Eq -> Some Eq | _ -> None end | _ -> None type dyn = Dyn : 'a typ * 'a -> dyn let get_dyn : type a. a typ -> dyn -> a option = fun a (Dyn(b,x)) -> match eq_type a b with | None -> None | Some Eq -> Some x >> Existential type names in error messages (Updated in OCaml 4.03.0) The typing of pattern matching in presence of GADT can generate many existential types. When necessary, error messages refer to these existential types using compiler-generated names. Currently, the compiler generates these names according to the following nomenclature: - First, types whose name starts with a $ are existentials. - $Constr_'a denotes an existential type introduced for the type variable 'a of the GADT constructor Constr: <<#type any = Any : 'name -> any #let escape (Any x) = x;; Error: This expression has type $Any_'name but an expression was expected of type 'a The type constructor $Any_'name would escape its scope >> - $Constr denotes an existential type introduced for an anonymous type variable in the GADT constructor Constr: <<#type any = Any : _ -> any #let escape (Any x) = x;; Error: This expression has type $Any but an expression was expected of type 'a The type constructor $Any would escape its scope >> - $'a if the existential variable was unified with the type variable 'a during typing: <<#type ('arg,'result,'aux) fn = # | Fun: ('a ->'b) -> ('a,'b,unit) fn # | Mem1: ('a ->'b) * 'a * 'b -> ('a, 'b, 'a * 'b) fn # let apply: ('arg,'result, _ ) fn -> 'arg -> 'result = fun f x -> # match f with # | Fun f -> f x # | Mem1 (f,y,fy) -> if x = y then fy else f x;; Error: This pattern matches values of type ($'arg, 'result, $'arg * 'result) fn but a pattern was expected which matches values of type ($'arg, 'result, unit) fn The type constructor $'arg would escape its scope >> - $n (n a number) is an internally generated existential which could not be named using one of the previous schemes. As shown by the last item, the current behavior is imperfect and may be improved in future versions. Equations on non-local abstract types (Introduced in OCaml 4.04) GADT pattern-matching may also add type equations to non-local abstract types. The behaviour is the same as with local abstract types. Reusing the above eq type, one can write: << module M : sig type t val x : t val e : (t,int) eq end = struct type t = int let x = 33 let e = Eq end let x : int = let Eq = M.e in M.x >> Of course, not all abstract types can be refined, as this would contradict the exhaustiveness check. Namely, builtin types (those defined by the compiler itself, such as int or array), and abstract types defined by the local module, are non-instantiable, and as such cause a type error rather than introduce an equation. 7.17 Syntax for Bigarray access *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= (Introduced in Objective Caml 3.00) expr ::= ... | expr .{ expr { , expr } } | expr .{ expr { , expr } } <- expr This extension provides syntactic sugar for getting and setting elements in the arrays provided by the Bigarray[] library. The short expressions are translated into calls to functions of the Bigarray module as described in the following table. ------------------------------------------------------------------------------- ------------------------------- | expression | translation | ------------------------------------------------------------------------------- ------------------------------- | expr_0.{ expr_1} |Bigarray.Array1.get expr_0 expr_1 | |expr_0.{ expr_1} <- expr |Bigarray.Array1.set expr_0 expr_1 expr | |expr_0.{ expr_1, expr_2} |Bigarray.Array2.get expr_0 expr_1 expr_2 | |expr_0.{ expr_1, expr_2} <- expr |Bigarray.Array2.set expr_0 expr_1 expr_2 expr | |expr_0.{ expr_1, expr_2, expr_3} |Bigarray.Array3.get expr_0 expr_1 expr_2 expr_3 | |expr_0.{ expr_1, expr_2, expr_3} <- expr|Bigarray.Array3.set expr_0 expr_1 expr_2 expr_3 expr | |expr_0.{ expr_1, ..., expr_n} |Bigarray.Genarray.get expr_0 [| expr_1, ... , expr_n |] | |expr_0.{ expr_1, ..., expr_n} <- expr |Bigarray.Genarray.set expr_0 [| expr_1, ... , expr_n |] expr | ------------------------------------------------------------------------------- ------------------------------- The last two entries are valid for any n > 3. 7.18 Attributes *=*=*=*=*=*=*=*= (Introduced in OCaml 4.02, infix notations for constructs other than expressions added in 4.03) Attributes are "decorations" of the syntax tree which are mostly ignored by the type-checker but can be used by external tools. An attribute is made of an identifier and a payload, which can be a structure, a type expression (prefixed with :), a signature (prefixed with :) or a pattern (prefixed with ?) optionally followed by a when clause: attr-id ::= lowercase-ident | capitalized-ident | attr-id . attr-id attr-payload ::= [ module-items ] | : typexpr | : [ specification ] | ? pattern [when expr] The first form of attributes is attached with a postfix notation on "algebraic" categories: attribute ::= [@ attr-id attr-payload ] expr ::= ... | expr attribute typexpr ::= ... | typexpr attribute pattern ::= ... | pattern attribute module-expr ::= ... | module-expr attribute module-type ::= ... | module-type attribute class-expr ::= ... | class-expr attribute class-type ::= ... | class-type attribute This form of attributes can also be inserted after the `tag-name in polymorphic variant type expressions (tag-spec-first, tag-spec, tag-spec-full) or after the method-name in method-type. The same syntactic form is also used to attach attributes to labels and constructors in type declarations: field-decl ::= [mutable] field-name : poly-typexpr {attribute} constr-decl ::= (constr-name | ()) [ of constr-args ] {attribute} Note: when a label declaration is followed by a semi-colon, attributes can also be put after the semi-colon (in which case they are merged to those specified before). The second form of attributes are attached to "blocks" such as type declarations, class fields, etc: item-attribute ::= [@@ attr-id attr-payload ] typedef ::= ... | typedef item-attribute exception-definition ::= exception constr-decl | exception constr-name = constr module-items ::= [;;] ( definition | expr { item-attribute } ) { [;;] definition | ;; expr { item-attribute } } [;;] class-binding ::= ... | class-binding item-attribute class-spec ::= ... | class-spec item-attribute classtype-def ::= ... | classtype-def item-attribute definition ::= let [rec] let-binding { and let-binding } | external value-name : typexpr = external-declaration { item-attribute } | type-definition | exception-definition { item-attribute } | class-definition | classtype-definition | module module-name { ( module-name : module-type ) } [ : module-type ] = module-expr { item-attribute } | module type modtype-name = module-type { item-attribute } | open module-path { item-attribute } | include module-expr { item-attribute } | module rec module-name : module-type = module-expr { item-attribute } { and module-name : module-type = module-expr { item-attribute } } specification ::= val value-name : typexpr { item-attribute } | external value-name : typexpr = external-declaration { item-attribute } | type-definition | exception constr-decl { item-attribute } | class-specification | classtype-definition | module module-name : module-type { item-attribute } | module module-name { ( module-name : module-type ) } : module-type { item-attribute } | module type modtype-name { item-attribute } | module type modtype-name = module-type { item-attribute } | open module-path { item-attribute } | include module-type { item-attribute } class-field-spec ::= ... | class-field-spec item-attribute class-field ::= ... | class-field item-attribute A third form of attributes appears as stand-alone structure or signature items in the module or class sub-languages. They are not attached to any specific node in the syntax tree: floating-attribute ::= [@@@ attr-id attr-payload ] definition ::= ... | floating-attribute specification ::= ... | floating-attribute class-field-spec ::= ... | floating-attribute class-field ::= ... | floating-attribute (Note: contrary to what the grammar above describes, item-attributes cannot be attached to these floating attributes in class-field-spec and class-field.) It is also possible to specify attributes using an infix syntax. For instance: <> For let, the attributes are applied to each bindings: <> 7.18.1 Built-in attributes =========================== Some attributes are understood by the type-checker: - "ocaml.warning" or "warning", with a string literal payload. This can be used as floating attributes in a signature/structure/object/object type. The string is parsed and has the same effect as the -w command-line option, in the scope between the attribute and the end of the current signature/structure/object/object type. The attribute can also be used on an expression, in which case its scope is limited to that expression. Note that it is not well-defined which scope is used for a specific warning. This is implementation dependant and can change between versions. For instance, warnings triggered by the "ppwarning" attribute (see below) are issued using the global warning configuration. - "ocaml.warnerror" or "warnerror", with a string literal payload. Same as "ocaml.warning", for the -warn-error command-line option. - "ocaml.deprecated" or "deprecated". Can be applied to most kind of items in signatures or structures. When the element is later referenced, a warning (3) is triggered. If the payload of the attribute is a string literal, the warning message includes this text. It is also possible to use this "ocaml.deprecated" as a floating attribute on top of an ".mli" file (i.e. before any other non-attribute item) or on top of an ".ml" file without a corresponding interface; this marks the unit itself as being deprecated. - "ocaml.deprecated_mutable" or "deprecated_mutable". Can be applied to a mutable record label. If the label is later used to modify the field (with "expr.l <- expr"), a warning (3) will be triggered. If the payload of the attribute is a string literal, the warning message includes this text. - "ocaml.ppwarning" or "ppwarning", in any context, with a string literal payload. The text is reported as warning (22) by the compiler (currently, the warning location is the location of the string payload). This is mostly useful for preprocessors which need to communicate warnings to the user. This could also be used to mark explicitly some code location for further inspection. - "ocaml.warn_on_literal_pattern" or "warn_on_literal_pattern" annotate constructors in type definition. A warning (52) is then emitted when this constructor is pattern matched with a constant literal as argument. This attribute denotes constructors whose argument is purely informative and may change in the future. Therefore, pattern matching on this argument with a constant literal is unreliable. For instance, all built-in exception constructors are marked as "warn_on_literal_pattern". Note that, due to an implementation limitation, this warning (52) is only triggered for single argument constructor. - "ocaml.tailcall" or "tailcall" can be applied to function application in order to check that the call is tailcall optimized. If it it not the case, a warning (51) is emitted. - "ocaml.inline" or "inline" take either "never", "always" or nothing as payload on a function or functor definition. If no payload is provided, the default value is "always". This payload controls when applications of the annotated functions should be inlined. - "ocaml.inlined" or "inlined" can be applied to any function or functor application to check that the call is inlined by the compiler. If the call is not inlined, a warning (55) is emitted. - "ocaml.noalloc", "ocaml.unboxed"and "ocaml.untagged" or "noalloc", "unboxed" and "untagged" can be used on external definitions to obtain finer control over the C-to-OCaml interface. See 19.10 for more details. - "ocaml.immediate" or "immediate" applied on an abstract type mark the type as having a non-pointer implementation (e.g. "int", "bool", "char" or enumerated types). Mutation of these immediate types does not activate the garbage collector's write barrier, which can significantly boost performance in programs relying heavily on mutable state. - ocaml.unboxed or unboxed can be used on a type definition if the type is a single-field record or a concrete type with a single constructor that has a single argument. It tells the compiler to optimize the representation of the type by removing the block that represents the record or the constructor (i.e. a value of this type is physically equal to its argument). In the case of GADTs, an additional restriction applies: the argument must not be an existential variable, represented by an existential type variable, or an abstract type constructor applied to an existential type variable. - ocaml.boxed or boxed can be used on type definitions to mean the opposite of ocaml.unboxed: keep the unoptimized representation of the type. When there is no annotation, the default is currently boxed but it may change in the future. <= 0) [@ppwarning "TODO: remove this later"]; let rec no_op = function | [] -> () | _ :: q -> (no_op[@tailcall]) q;; let f x = x [@@inline] let () = (f[@inlined]) () type fragile = | Int of int [@warn_on_literal_pattern] | String of string [@warn_on_literal_pattern] let f = function | Int 0 | String "constant" -> () (* trigger warning 52 *) | _ -> () module Immediate: sig type t [@@immediate] val x: t ref end = struct type t = A | B let x = ref 0 end .... >> 7.19 Extension nodes *=*=*=*=*=*=*=*=*=*=* (Introduced in OCaml 4.02, infix notations for constructs other than expressions added in 4.03, infix notation (e1 ;%ext e2) added in 4.04. ) Extension nodes are generic placeholders in the syntax tree. They are rejected by the type-checker and are intended to be "expanded" by external tools such as -ppx rewriters. Extension nodes share the same notion of identifier and payload as attributes 7.18. The first form of extension node is used for "algebraic" categories: extension ::= [% attr-id attr-payload ] expr ::= ... | extension typexpr ::= ... | extension pattern ::= ... | extension module-expr ::= ... | extension module-type ::= ... | extension class-expr ::= ... | extension class-type ::= ... | extension A second form of extension node can be used in structures and signatures, both in the module and object languages: item-extension ::= [%% attr-id attr-payload ] definition ::= ... | item-extension specification ::= ... | item-extension class-field-spec ::= ... | item-extension class-field ::= | item-extension An infix form is available for extension nodes when the payload is of the same kind (expression with expression, pattern with pattern ...). Examples: <> When this form is used together with the infix syntax for attributes, the attributes are considered to apply to the payload: < x + 1 === [%foo (fun x -> x + 1)[@foo ] ]; >> 7.19.1 Built-in extension nodes ================================ (Introduced in OCaml 4.03) Some extension nodes are understood by the compiler itself: - "ocaml.extension_constructor" or "extension_constructor" take as payload a constructor from an extensible variant type (see 7.22) and return its extension constructor slot. <<#type t = .. #type t += X of int | Y of string #let x = [%extension_constructor X] #let y = [%extension_constructor Y];; >> <<# x <> y;; - : bool = true >> 7.20 Quoted strings *=*=*=*=*=*=*=*=*=*= (Introduced in OCaml 4.02) Quoted strings {foo|...|foo} provide a different lexical syntax to write string literals in OCaml code. They are useful to represent strings of arbitrary content without escaping -- as long as the delimiter you chose (here |foo}) does not occur in the string itself. string-literal ::= ... | { quoted-string-id | ........ | quoted-string-id } quoted-string-id ::= { a...z | _ } The opening delimiter has the form {id| where id is a (possibly empty) sequence of lowercase letters and underscores. The corresponding closing delimiter is |id} (with the same identifier). Unlike regular OCaml string literals, quoted strings do not interpret any character in a special way. Example: <> Quoted strings are interesting in particular in conjunction to extension nodes [%foo ...] (see 7.19) to embed foreign syntax fragments to be interpreted by a preprocessor and turned into OCaml code: you can use [%sql {|...|}] for example to represent arbitrary SQL statements -- assuming you have a ppx-rewriter that recognizes the %sql extension -- without requiring escaping quotes. Note that the non-extension form, for example {sql|...|sql}, should not be used for this purpose, as the user cannot see in the code that this string literal has a different semantics than they expect, and giving a semantics to a specific delimiter limits the freedom to change the delimiter to avoid escaping issues. 7.21 Exception cases in pattern matching *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* (Introduced in OCaml 4.02) A new form of exception patterns is allowed, only as a toplevel pattern under a match...with pattern-matching (other occurrences are rejected by the type-checker). pattern ::= ... | exception pattern Cases with such a toplevel pattern are called "exception cases", as opposed to regular "value cases". Exception cases are applied when the evaluation of the matched expression raises an exception. The exception value is then matched against all the exception cases and re-raised if none of them accept the exception (as for a try...with block). Since the bodies of all exception and value cases is outside the scope of the exception handler, they are all considered to be in tail-position: if the match...with block itself is in tail position in the current function, any function call in tail position in one of the case bodies results in an actual tail call. It is an error if all cases are exception cases in a given pattern matching. 7.22 Extensible variant types *=*=*=*=*=*=*=*=*=*=*=*=*=*=*= (Introduced in OCaml 4.02) type-representation ::= ... | = .. specification ::= ... | type [type-params] typeconstr type-extension-spec definition ::= ... | type [type-params] typeconstr type-extension-def type-extension-spec ::= += [private] [|] constr-decl { | constr-decl } type-extension-def ::= += [private] [|] constr-def { | constr-def } constr-def ::= constr-decl | constr-name = constr Extensible variant types are variant types which can be extended with new variant constructors. Extensible variant types are defined using ... New variant constructors are added using +=. << type attr = .. type attr += Str of string type attr += | Int of int | Float of float >> Pattern matching on an extensible variant type requires a default case to handle unknown variant constructors: << let to_string = function | Str s -> s | Int i -> string_of_int i | Float f -> string_of_float f | _ -> "?" >> A preexisting example of an extensible variant type is the built-in exn type used for exceptions. Indeed, exception constructors can be declared using the type extension syntax: << type exn += Exc of int >> Extensible variant constructors can be rebound to a different name. This allows exporting variants from another module. << type Expr.attr += Str = Expr.Str >> Extensible variant constructors can be declared private. As with regular variants, this prevents them from being constructed directly by constructor application while still allowing them to be de-structured in pattern-matching. 7.23 Generative functors *=*=*=*=*=*=*=*=*=*=*=*=* (Introduced in OCaml 4.02) module-expr ::= ... | functor () -> module-expr | module-expr () definition ::= ... | module module-name { ( module-name : module-type ) | () } [ : module-type ] = module-expr module-type ::= ... | functor () -> module-type specification ::= ... | module module-name { ( module-name : module-type ) | () } : module-type A generative functor takes a unit () argument. In order to use it, one must necessarily apply it to this unit argument, ensuring that all type components in the result of the functor behave in a generative way, i.e. they are different from types obtained by other applications of the same functor. This is equivalent to taking an argument of signature sig end, and always applying to struct end, but not to some defined module (in the latter case, applying twice to the same module would return identical types). As a side-effect of this generativity, one is allowed to unpack first-class modules in the body of generative functors. 7.24 Extension-only syntax *=*=*=*=*=*=*=*=*=*=*=*=*=* (Introduced in OCaml 4.02.2, extended in 4.03) Some syntactic constructions are accepted during parsing and rejected during type checking. These syntactic constructions can therefore not be used directly in vanilla OCaml. However, -ppx rewriters and other external tools can exploit this parser leniency to extend the language with these new syntactic constructions by rewriting them to vanilla constructions. 7.24.1 Extension operators =========================== (Introduced in OCaml 4.02.2) infix-symbol ::= ... | # {operator-chars} # {operator-char | #} Operator names starting with a # character and containing more than one # character are reserved for extensions. 7.24.2 Extension literals ========================== (Introduced in OCaml 4.03) float-literal ::= ... | [-] (0...9) { 0...9| _ } [. { 0...9| _ }] [(e| E) [+| -] (0...9) { 0...9| _ }] [g...z| G...Z] | [-] (0x| 0X) (0...9| A...F| a...f) { 0...9| A...F| a...f| _ } [. { 0...9| A...F| a...f| _ } [(p| P) [+| -] (0...9) { 0...9| _ }] [g...z| G...Z] int-literal ::= ... | [-] (0...9) { 0...9 | _ }[g...z| G...Z] | [-] (0x| 0X) (0...9| A...F| a...f) { 0...9| A...F| a...f| _ } [g...z| G...Z] | [-] (0o| 0O) (0...7) { 0...7| _ } [g...z| G...Z] | [-] (0b| 0B) (0...1) { 0...1| _ } [g...z| G...Z] Int and float literals followed by an one-letter identifier in the range [g..z| G..Z] are extension-only literals. 7.25 Inline records *=*=*=*=*=*=*=*=*=*= (Introduced in OCaml 4.03) constr-args ::= ... | record-decl The arguments of a sum-type constructors can now be defined using the same syntax as records. Mutable and polymorphic fields are allowed. GADT syntax is supported. Attributes can be specified on individual fields. Syntactically, building or matching constructors with such an inline record argument is similar to working with a unary constructor whose unique argument is a declared record type. A pattern can bind the inline record as a pseudo-value, but the record cannot escape the scope of the binding and can only be used with the dot-notation to extract or modify fields or to build new constructor values. < Point {p with x = l *. p.x; y = l *. p.y} | .... let print = function | Point {x; y; _} -> Printf.printf "%f/%f" x y | .... let reset = function | Point p -> p.x <- 0.; p.y <- 0. | ... let invalid = function | Point p -> p (* INVALID *) | ... >> 7.26 Local exceptions *=*=*=*=*=*=*=*=*=*=*= (Introduced in OCaml 4.04) It is possible to define local exceptions in expressions: expr ::= ... | let exception constr-decl in expr The syntactic scope of the exception constructor is the inner expression, but nothing prevents exception values created with this constructor from escaping this scope. Two executions of the definition above result in two incompatible exception constructors (as for any exception definition). 7.27 Documentation comments *=*=*=*=*=*=*=*=*=*=*=*=*=*= (Introduced in OCaml 4.03) Comments which start with ** are treated specially by the compiler. They are automatically converted during parsing into attributes (see 7.18) to allow tools to process them as documentation. Such comments can take three forms: floating comments, item comments and label comments. Any comment starting with ** which does not match one of these forms will cause the compiler to emit warning 50. Comments which start with ** are also used by the ocamldoc documentation generator (see 15). The three comment forms recognised by the compiler are a subset of the forms accepted by ocamldoc (see 15.2). 7.27.1 Floating comments ========================= Comments surrounded by blank lines that appear within structures, signatures, classes or class types are converted into floating-attributes. For example: <> will be converted to: <> 7.27.2 Item comments ===================== Comments which appear immediately before or immediately after a structure item, signature item, class item or class type item are converted into item-attributes. Immediately before or immediately after means that there must be no blank lines, ;;, or other documentation comments between them. For example: <> or << (** A description of [t] *) type t = T >> will be converted to: <> Note that, if a comment appears immediately next to multiple items, as in: <> then it will be attached to both items: <> and the compiler will emit warning 50. 7.27.3 Label comments ====================== Comments which appear immediately after a labelled argument, record field, variant constructor, object method or polymorphic variant constructor are are converted into attributes. Immediately after means that there must be no blank lines or other documentation comments between them. For example: < unit type t2 = { fld: int; (** Record field *) fld2: float; } type t3 = | Cstr of string (** Variant constructor *) | Cstr2 of string type t4 = < meth: int * int; (** Object method *) > type t5 = [ `PCstr (** Polymorphic variant constructor *) ] >> will be converted to: < unit type t2 = { fld: int [@ocaml.doc " Record field "]; fld2: float; } type t3 = | Cstr of string [@ocaml.doc " Variant constructor "] | Cstr2 of string type t4 = < meth : int * int [@ocaml.doc " Object method "] > type t5 = [ `PCstr [@ocaml.doc " Polymorphic variant constructor "] ] >> Note that label comments take precedence over item comments, so: <> will be converted to: <> whilst: <> will be converted to: <> In the absence of meaningful comment on the last constructor of a type, an empty comment (**) can be used instead: <> will be converted directly to <> Part: III ********* The OCaml tools *************** Chapter 8 Batch compilation (ocamlc) *************************************** This chapter describes the OCaml batch compiler ocamlc, which compiles OCaml source files to bytecode object files and links these object files to produce standalone bytecode executable files. These executable files are then run by the bytecode interpreter ocamlrun. 8.1 Overview of the compiler *=*=*=*=*=*=*=*=*=*=*=*=*=*=* The ocamlc command has a command-line interface similar to the one of most C compilers. It accepts several types of arguments and processes them sequentially, after all options have been processed: - Arguments ending in .mli are taken to be source files for compilation unit interfaces. Interfaces specify the names exported by compilation units: they declare value names with their types, define public data types, declare abstract data types, and so on. From the file x.mli, the ocamlc compiler produces a compiled interface in the file x.cmi. - Arguments ending in .ml are taken to be source files for compilation unit implementations. Implementations provide definitions for the names exported by the unit, and also contain expressions to be evaluated for their side-effects. From the file x.ml, the ocamlc compiler produces compiled object bytecode in the file x.cmo. If the interface file x.mli exists, the implementation x.ml is checked against the corresponding compiled interface x.cmi, which is assumed to exist. If no interface x.mli is provided, the compilation of x.ml produces a compiled interface file x.cmi in addition to the compiled object code file x.cmo. The file x.cmi produced corresponds to an interface that exports everything that is defined in the implementation x.ml. - Arguments ending in .cmo are taken to be compiled object bytecode. These files are linked together, along with the object files obtained by compiling .ml arguments (if any), and the OCaml standard library, to produce a standalone executable program. The order in which .cmo and .ml arguments are presented on the command line is relevant: compilation units are initialized in that order at run-time, and it is a link-time error to use a component of a unit before having initialized it. Hence, a given x.cmo file must come before all .cmo files that refer to the unit x. - Arguments ending in .cma are taken to be libraries of object bytecode. A library of object bytecode packs in a single file a set of object bytecode files (.cmo files). Libraries are built with ocamlc -a (see the description of the -a option below). The object files contained in the library are linked as regular .cmo files (see above), in the order specified when the .cma file was built. The only difference is that if an object file contained in a library is not referenced anywhere in the program, then it is not linked in. - Arguments ending in .c are passed to the C compiler, which generates a .o object file (.obj under Windows). This object file is linked with the program if the -custom flag is set (see the description of -custom below). - Arguments ending in .o or .a (.obj or .lib under Windows) are assumed to be C object files and libraries. They are passed to the C linker when linking in -custom mode (see the description of -custom below). - Arguments ending in .so (.dll under Windows) are assumed to be C shared libraries (DLLs). During linking, they are searched for external C functions referenced from the OCaml code, and their names are written in the generated bytecode executable. The run-time system ocamlrun then loads them dynamically at program start-up time. The output of the linking phase is a file containing compiled bytecode that can be executed by the OCaml bytecode interpreter: the command named ocamlrun. If a.out is the name of the file produced by the linking phase, the command << ocamlrun a.out arg_1 arg_2 ... arg_n >> executes the compiled code contained in a.out, passing it as arguments the character strings arg_1 to arg_n. (See chapter 10 for more details.) On most systems, the file produced by the linking phase can be run directly, as in: << ./a.out arg_1 arg_2 ... arg_n >> The produced file has the executable bit set, and it manages to launch the bytecode interpreter by itself. 8.2 Options *=*=*=*=*=*= The following command-line options are recognized by ocamlc. The options -pack, -a, -c and -output-obj are mutually exclusive. -a Build a library(.cma file) with the object files ( .cmo files) given on the command line, instead of linking them into an executable file. The name of the library must be set with the -o option. If -custom, -cclib or -ccopt options are passed on the command line, these options are stored in the resulting .cmalibrary. Then, linking with this library automatically adds back the -custom, -cclib and -ccopt options as if they had been provided on the command line, unless the -noautolink option is given. -absname Force error messages to show absolute paths for file names. -annot Dump detailed information about the compilation (types, bindings, tail-calls, etc). The information for file src.ml is put into file src.annot. In case of a type error, dump all the information inferred by the type-checker before the error. The src.annot file can be used with the emacs commands given in emacs/caml-types.el to display types and other annotations interactively. -args filename Read additional newline-terminated command line arguments from filename. -args0 filename Read additional null character terminated command line arguments from filename. -bin-annot Dump detailed information about the compilation (types, bindings, tail-calls, etc) in binary format. The information for file src.ml is put into file src.cmt. In case of a type error, dump all the information inferred by the type-checker before the error. The *.cmt files produced by -bin-annot contain more information and are much more compact than the files produced by -annot. -c Compile only. Suppress the linking phase of the compilation. Source code files are turned into compiled files, but no executable file is produced. This option is useful to compile modules separately. -cc ccomp Use ccomp as the C linker when linking in "custom runtime" mode (see the -custom option) and as the C compiler for compiling .c source files. -cclib -llibname Pass the -llibname option to the C linker when linking in "custom runtime" mode (see the -custom option). This causes the given C library to be linked with the program. -ccopt option Pass the given option to the C compiler and linker. When linking in "custom runtime" mode, for instance-ccopt -Ldir causes the C linker to search for C libraries in directory dir.(See the -custom option.) -color mode Enable or disable colors in compiler messages (especially warnings and errors). The following modes are supported: auto use heuristics to enable colors only if the output supports them (an ANSI-compatible tty terminal); always enable colors unconditionally; never disable color output. The default setting is 'auto', and the current heuristic checks that the TERM environment variable exists and is not empty or dumb, and that 'isatty(stderr)' holds. The environment variable OCAML_COLOR is considered if -color is not provided. Its values are auto/always/never as above. -compat-32 Check that the generated bytecode executable can run on 32-bit platforms and signal an error if it cannot. This is useful when compiling bytecode on a 64-bit machine. -config Print the version number of ocamlc and a detailed summary of its configuration, then exit. -custom Link in "custom runtime" mode. In the default linking mode, the linker produces bytecode that is intended to be executed with the shared runtime system, ocamlrun. In the custom runtime mode, the linker produces an output file that contains both the runtime system and the bytecode for the program. The resulting file is larger, but it can be executed directly, even if the ocamlrun command is not installed. Moreover, the "custom runtime" mode enables static linking of OCaml code with user-defined C functions, as described in chapter 19. Unix: Never use the strip command on executables produced by ocamlc -custom, this would remove the bytecode part of the executable. Unix: Security warning: never set the "setuid" or "setgid" bits on executables produced by ocamlc -custom, this would make them vulnerable to attacks. -dllib -llibname Arrange for the C shared library dlllibname.so (dlllibname.dll under Windows) to be loaded dynamically by the run-time system ocamlrun at program start-up time. -dllpath dir Adds the directory dir to the run-time search path for shared C libraries. At link-time, shared libraries are searched in the standard search path (the one corresponding to the -I option). The -dllpath option simply stores dir in the produced executable file, where ocamlrun can find it and use it as described in section 10.3. -for-pack module-path Generate an object file (.cmo) that can later be included as a sub-module (with the given access path) of a compilation unit constructed with -pack. For instance, ocamlc -for-pack P -c A.ml will generate a..cmo that can later be used with ocamlc -pack -o P.cmo a.cmo. Note: you can still pack a module that was compiled without -for-pack but in this case exceptions will be printed with the wrong names. -g Add debugging information while compiling and linking. This option is required in order to be able to debug the program with ocamldebug (see chapter 16), and to produce stack backtraces when the program terminates on an uncaught exception (see section 10.2). -i Cause the compiler to print all defined names (with their inferred types or their definitions) when compiling an implementation (.ml file). No compiled files (.cmo and .cmi files) are produced. This can be useful to check the types inferred by the compiler. Also, since the output follows the syntax of interfaces, it can help in writing an explicit interface (.mli file) for a file: just redirect the standard output of the compiler to a .mli file, and edit that file to remove all declarations of unexported names. -I directory Add the given directory to the list of directories searched for compiled interface files (.cmi), compiled object code files .cmo, libraries (.cma) and C libraries specified with -cclib -lxxx. By default, the current directory is searched first, then the standard library directory. Directories added with -I are searched after the current directory, in the order in which they were given on the command line, but before the standard library directory. See also option -nostdlib. If the given directory starts with +, it is taken relative to the standard library directory. For instance, -I +labltk adds the subdirectory labltk of the standard library to the search path. -impl filename Compile the file filename as an implementation file, even if its extension is not .ml. -intf filename Compile the file filename as an interface file, even if its extension is not .mli. -intf-suffix string Recognize file names ending with string as interface files (instead of the default .mli). -labels Labels are not ignored in types, labels may be used in applications, and labelled parameters can be given in any order. This is the default. -linkall Force all modules contained in libraries to be linked in. If this flag is not given, unreferenced modules are not linked in. When building a library (option -a), setting the -linkall option forces all subsequent links of programs involving that library to link all the modules contained in the library. When compiling a module (option -c), setting the -linkall option ensures that this module will always be linked if it is put in a library and this library is linked. -make-runtime Build a custom runtime system (in the file specified by option -o) incorporating the C object files and libraries given on the command line. This custom runtime system can be used later to execute bytecode executables produced with the ocamlc -use-runtime runtime-name option. See section 19.1.6 for more information. -no-alias-deps Do not record dependencies for module aliases. See section 7.13 for more information. -no-app-funct Deactivates the applicative behaviour of functors. With this option, each functor application generates new types in its result and applying the same functor twice to the same argument yields two incompatible structures. -noassert Do not compile assertion checks. Note that the special form assert false is always compiled because it is typed specially. This flag has no effect when linking already-compiled files. -noautolink When linking .cmalibraries, ignore -custom, -cclib and -ccopt options potentially contained in the libraries (if these options were given when building the libraries). This can be useful if a library contains incorrect specifications of C libraries or C options; in this case, during linking, set -noautolink and pass the correct C libraries and options on the command line. -nolabels Ignore non-optional labels in types. Labels cannot be used in applications, and parameter order becomes strict. -nostdlib Do not include the standard library directory in the list of directories searched for compiled interface files (.cmi), compiled object code files (.cmo), libraries (.cma), and C libraries specified with -cclib -lxxx. See also option -I. -o exec-file Specify the name of the output file produced by the compiler. The default output name is a.out under Unix and camlprog.exe under Windows. If the -a option is given, specify the name of the library produced. If the -pack option is given, specify the name of the packed object file produced. If the -output-obj option is given, specify the name of the output file produced. If the -c option is given, specify the name of the object file produced for the next source file that appears on the command line. -opaque When the native compiler compiles an implementation, by default it produces a .cmx file containing information for cross-module optimization. It also expects .cmx files to be present for the dependencies of the currently compiled source, and uses them for optimization. Since OCaml 4.03, the compiler will emit a warning if it is unable to locate the .cmx file of one of those dependencies. The -opaque option, available since 4.04, disables cross-module optimization information for the currently compiled unit. When compiling .mli interface, using -opaque marks the compiled .cmi interface so that subsequent compilations of modules that depend on it will not rely on the corresponding .cmx file, nor warn if it is absent. When the native compiler compiles a .ml implementation, using -opaque generates a .cmx that does not contain any cross-module optimization information. Using this option may degrade the quality of generated code, but it reduces compilation time, both on clean and incremental builds. Indeed, with the native compiler, when the implementation of a compilation unit changes, all the units that depend on it may need to be recompiled -- because the cross-module information may have changed. If the compilation unit whose implementation changed was compiled with -opaque, no such recompilation needs to occur. This option can thus be used, for example, to get faster edit-compile-test feedback loops. -open Module Opens the given module before processing the interface or implementation files. If several -open options are given, they are processed in order, just as if the statements open! Module1;; ... open! ModuleN;; were added at the top of each file. -output-obj Cause the linker to produce a C object file instead of a bytecode executable file. This is useful to wrap OCaml code as a C library, callable from any C program. See chapter 19, section 19.7.5. The name of the output object file must be set with the -o option. This option can also be used to produce a C source file (.c extension) or a compiled shared/dynamic library (.so extension, .dll under Windows). -pack Build a bytecode object file (.cmo file) and its associated compiled interface (.cmi) that combines the object files given on the command line, making them appear as sub-modules of the output .cmo file. The name of the output .cmo file must be given with the -o option. For instance, << ocamlc -pack -o p.cmo a.cmo b.cmo c.cmo >> generates compiled files p.cmo and p.cmi describing a compilation unit having three sub-modules A, B and C, corresponding to the contents of the object files a.cmo, b.cmo and c.cmo. These contents can be referenced as P.A, P.B and P.C in the remainder of the program. -plugin plugin Dynamically load the code of the given plugin (a .cmo, .cma or .cmxs file) in the compiler. plugin must exist in the same kind of code as the compiler (ocamlc.byte must load bytecode plugins, while ocamlc.opt must load native code plugins), and extension adaptation is done automatically for .cma files (to .cmxs files if the compiler is compiled in native code). -pp command Cause the compiler to call the given command as a preprocessor for each source file. The output of command is redirected to an intermediate file, which is compiled. If there are no compilation errors, the intermediate file is deleted afterwards. -ppx command After parsing, pipe the abstract syntax tree through the preprocessor command. The module Ast_mapper, described in section 26.1, implements the external interface of a preprocessor. -principal Check information path during type-checking, to make sure that all types are derived in a principal way. When using labelled arguments and/or polymorphic methods, this flag is required to ensure future versions of the compiler will be able to infer types correctly, even if internal algorithms change. All programs accepted in -principal mode are also accepted in the default mode with equivalent types, but different binary signatures, and this may slow down type checking; yet it is a good idea to use it once before publishing source code. -rectypes Allow arbitrary recursive types during type-checking. By default, only recursive types where the recursion goes through an object type are supported.Note that once you have created an interface using this flag, you must use it again for all dependencies. -runtime-variant suffix Add the suffix string to the name of the runtime library used by the program. Currently, only one such suffix is supported: d, and only if the OCaml compiler was configured with option -with-debug-runtime. This suffix gives the debug version of the runtime, which is useful for debugging pointer problems in low-level code such as C stubs. -safe-string Enforce the separation between types string and bytes, thereby making strings read-only. This will become the default in a future version of OCaml. -short-paths When a type is visible under several module-paths, use the shortest one when printing the type's name in inferred interfaces and error and warning messages. Identifier names starting with an underscore _ or containing double underscores __ incur a penalty of +10 when computing their length. -strict-sequence Force the left-hand part of each sequence to have type unit. -strict-formats Reject invalid formats that were accepted in legacy format implementations. You should use this flag to detect and fix such invalid formats, as they will be rejected by future OCaml versions. -thread Compile or link multithreaded programs, in combination with the system threads library described in chapter 30. -unboxed-types When a type is unboxable (i.e. a record with a single argument or a concrete datatype with a single constructor of one argument) it will be unboxed unless annotated with [@@ocaml.boxed]. -no-unboxed-types When a type is unboxable it will be boxed unless annotated with [@@ocaml.unboxed]. This is the default. -unsafe Turn bound checking off for array and string accesses (the v.(i) and s.[i] constructs). Programs compiled with -unsafe are therefore slightly faster, but unsafe: anything can happen if the program accesses an array or string outside of its bounds. Additionally, turn off the check for zero divisor in integer division and modulus operations. With -unsafe, an integer division (or modulus) by zero can halt the program or continue with an unspecified result instead of raising a Division_by_zero exception. -unsafe-string Identify the types string and bytes, thereby making strings writable. For reasons of backward compatibility, this is the default setting for the moment, but this will change in a future version of OCaml. -use-runtime runtime-name Generate a bytecode executable file that can be executed on the custom runtime system runtime-name, built earlier with ocamlc -make-runtime runtime-name. See section 19.1.6 for more information. -v Print the version number of the compiler and the location of the standard library directory, then exit. -verbose Print all external commands before they are executed, in particular invocations of the C compiler and linker in -custom mode. Useful to debug C library problems. -vmthread Compile or link multithreaded programs, in combination with the VM-level threads library described in chapter 30. -version or -vnum Print the version number of the compiler in short form (e.g. 3.11.0), then exit. -w warning-list Enable, disable, or mark as fatal the warnings specified by the argument warning-list. Each warning can be enabled or disabled, and each warning can be fatal or non-fatal. If a warning is disabled, it isn't displayed and doesn't affect compilation in any way (even if it is fatal). If a warning is enabled, it is displayed normally by the compiler whenever the source code triggers it. If it is enabled and fatal, the compiler will also stop with an error after displaying it. The warning-list argument is a sequence of warning specifiers, with no separators between them. A warning specifier is one of the following: +num Enable warning number num. -num Disable warning number num. @num Enable and mark as fatal warning number num. +num1..num2 Enable warnings in the given range. -num1..num2 Disable warnings in the given range. @num1..num2 Enable and mark as fatal warnings in the given range. +letter Enable the set of warnings corresponding to letter. The letter may be uppercase or lowercase. -letter Disable the set of warnings corresponding to letter. The letter may be uppercase or lowercase. @letter Enable and mark as fatal the set of warnings corresponding to letter. The letter may be uppercase or lowercase. uppercase-letter Enable the set of warnings corresponding to uppercase-letter. lowercase-letter Disable the set of warnings corresponding to lowercase-letter. Warning numbers and letters which are out of the range of warnings that are currently defined are ignored. The warnings are as follows. 1 Suspicious-looking start-of-comment mark. 2 Suspicious-looking end-of-comment mark. 3 Deprecated feature. 4 Fragile pattern matching: matching that will remain complete even if additional constructors are added to one of the variant types matched. 5 Partially applied function: expression whose result has function type and is ignored. 6 Label omitted in function application. 7 Method overridden. 8 Partial match: missing cases in pattern-matching. 9 Missing fields in a record pattern. 10 Expression on the left-hand side of a sequence that doesn't have type unit (and that is not a function, see warning number 5). 11 Redundant case in a pattern matching (unused match case). 12 Redundant sub-pattern in a pattern-matching. 13 Instance variable overridden. 14 Illegal backslash escape in a string constant. 15 Private method made public implicitly. 16 Unerasable optional argument. 17 Undeclared virtual method. 18 Non-principal type. 19 Type without principality. 20 Unused function argument. 21 Non-returning statement. 22 Preprocessor warning. 23 Useless record with clause. 24 Bad module name: the source file name is not a valid OCaml module name. 26 Suspicious unused variable: unused variable that is bound with let or as, and doesn't start with an underscore (_) character. 27 Innocuous unused variable: unused variable that is not bound with let nor as, and doesn't start with an underscore (_) character. 28 Wildcard pattern given as argument to a constant constructor. 29 Unescaped end-of-line in a string constant (non-portable code). 30 Two labels or constructors of the same name are defined in two mutually recursive types. 31 A module is linked twice in the same executable. 32 Unused value declaration. 33 Unused open statement. 34 Unused type declaration. 35 Unused for-loop index. 36 Unused ancestor variable. 37 Unused constructor. 38 Unused extension constructor. 39 Unused rec flag. 40 Constructor or label name used out of scope. 41 Ambiguous constructor or label name. 42 Disambiguated constructor or label name (compatibility warning). 43 Nonoptional label applied as optional. 44 Open statement shadows an already defined identifier. 45 Open statement shadows an already defined label or constructor. 46 Error in environment variable. 47 Illegal attribute payload. 48 Implicit elimination of optional arguments. 49 Absent cmi file when looking up module alias. 50 Unexpected documentation comment. 51 Warning on non-tail calls if @tailcall present. 52 (see 8.5.1) Fragile constant pattern. 53 Attribute cannot appear in this context 54 Attribute used more than once on an expression 55 Inlining impossible 56 Unreachable case in a pattern-matching (based on type information). 57 (see 8.5.2) Ambiguous or-pattern variables under guard 58 Missing cmx file 59 Assignment to non-mutable value 60 Unused module declaration A all warnings C warnings 1, 2. D Alias for warning 3. E Alias for warning 4. F Alias for warning 5. K warnings 32, 33, 34, 35, 36, 37, 38, 39. L Alias for warning 6. M Alias for warning 7. P Alias for warning 8. R Alias for warning 9. S Alias for warning 10. U warnings 11, 12. V Alias for warning 13. X warnings 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 30. Y Alias for warning 26. Z Alias for warning 27. The default setting is -w +a-4-6-7-9-27-29-32..39-41..42-44-45-48-50. It is displayed by ocamlc -help. Note that warnings 5 and 10 are not always triggered, depending on the internals of the type checker. -warn-error warning-list Mark as fatal the warnings specified in the argument warning-list. The compiler will stop with an error when one of these warnings is emitted. The warning-list has the same meaning as for the -w option: a + sign (or an uppercase letter) marks the corresponding warnings as fatal, a - sign (or a lowercase letter) turns them back into non-fatal warnings, and a @ sign both enables and marks as fatal the corresponding warnings. Note: it is not recommended to use warning sets (i.e. letters) as arguments to -warn-error in production code, because this can break your build when future versions of OCaml add some new warnings. The default setting is -warn-error -a+31 (only warning 31 is fatal). -warn-help Show the description of all available warning numbers. -where Print the location of the standard library, then exit. - file Process file as a file name, even if it starts with a dash (-) character. -help or --help Display a short usage summary and exit. Contextual control of command-line options The compiler command line can be modified "from the outside" with the following mechanisms. These are experimental and subject to change. They should be used only for experimental and development work, not in released packages. OCAMLPARAM (environment variable) Arguments that will be inserted before or after the arguments from the command line. ocaml_compiler_internal_params (file in the stdlib directory) A mapping of file names to lists of arguments that will be added to the command line (and OCAMLPARAM) arguments. OCAML_FLEXLINK (environment variable) Alternative executable to use on native Windows for flexlink instead of the configured value. Primarily used for bootstrapping. 8.3 Modules and the file system *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= This short section is intended to clarify the relationship between the names of the modules corresponding to compilation units and the names of the files that contain their compiled interface and compiled implementation. The compiler always derives the module name by taking the capitalized base name of the source file (.ml or .mli file). That is, it strips the leading directory name, if any, as well as the .ml or .mli suffix; then, it set the first letter to uppercase, in order to comply with the requirement that module names must be capitalized. For instance, compiling the file mylib/misc.ml provides an implementation for the module named Misc. Other compilation units may refer to components defined in mylib/misc.ml under the names Misc.name; they can also do open Misc, then use unqualified names name. The .cmi and .cmo files produced by the compiler have the same base name as the source file. Hence, the compiled files always have their base name equal (modulo capitalization of the first letter) to the name of the module they describe (for .cmi files) or implement (for .cmo files). When the compiler encounters a reference to a free module identifier Mod, it looks in the search path for a file named Mod.cmi or mod.cmi and loads the compiled interface contained in that file. As a consequence, renaming .cmi files is not advised: the name of a .cmi file must always correspond to the name of the compilation unit it implements. It is admissible to move them to another directory, if their base name is preserved, and the correct -I options are given to the compiler. The compiler will flag an error if it loads a .cmi file that has been renamed. Compiled bytecode files (.cmo files), on the other hand, can be freely renamed once created. That's because the linker never attempts to find by itself the .cmo file that implements a module with a given name: it relies instead on the user providing the list of .cmo files by hand. 8.4 Common errors *=*=*=*=*=*=*=*=*= This section describes and explains the most frequently encountered error messages. Cannot find file filename The named file could not be found in the current directory, nor in the directories of the search path. The filename is either a compiled interface file (.cmi file), or a compiled bytecode file (.cmo file). If filename has the format mod.cmi, this means you are trying to compile a file that references identifiers from module mod, but you have not yet compiled an interface for module mod. Fix: compile mod.mli or mod.ml first, to create the compiled interface mod.cmi. If filename has the format mod.cmo, this means you are trying to link a bytecode object file that does not exist yet. Fix: compile mod.ml first. If your program spans several directories, this error can also appear because you haven't specified the directories to look into. Fix: add the correct -I options to the command line. Corrupted compiled interface filename The compiler produces this error when it tries to read a compiled interface file (.cmi file) that has the wrong structure. This means something went wrong when this .cmi file was written: the disk was full, the compiler was interrupted in the middle of the file creation, and so on. This error can also appear if a .cmi file is modified after its creation by the compiler. Fix: remove the corrupted .cmi file, and rebuild it. This expression has type t_1, but is used with type t_2 This is by far the most common type error in programs. Type t_1 is the type inferred for the expression (the part of the program that is displayed in the error message), by looking at the expression itself. Type t_2 is the type expected by the context of the expression; it is deduced by looking at how the value of this expression is used in the rest of the program. If the two types t_1 and t_2 are not compatible, then the error above is produced. In some cases, it is hard to understand why the two types t_1 and t_2 are incompatible. For instance, the compiler can report that "expression of type foo cannot be used with type foo", and it really seems that the two types foo are compatible. This is not always true. Two type constructors can have the same name, but actually represent different types. This can happen if a type constructor is redefined. Example: << type foo = A | B let f = function A -> 0 | B -> 1 type foo = C | D f C >> This result in the error message "expression C of type foo cannot be used with type foo". The type of this expression, t, contains type variables that cannot be generalized Type variables ('a, 'b, ...) in a type t can be in either of two states: generalized (which means that the type t is valid for all possible instantiations of the variables) and not generalized (which means that the type t is valid only for one instantiation of the variables). In a let binding let name = expr, the type-checker normally generalizes as many type variables as possible in the type of expr. However, this leads to unsoundness (a well-typed program can crash) in conjunction with polymorphic mutable data structures. To avoid this, generalization is performed at let bindings only if the bound expression expr belongs to the class of "syntactic values", which includes constants, identifiers, functions, tuples of syntactic values, etc. In all other cases (for instance, expr is a function application), a polymorphic mutable could have been created and generalization is therefore turned off for all variables occurring in contravariant or non-variant branches of the type. For instance, if the type of a non-value is 'a list the variable is generalizable (list is a covariant type constructor), but not in 'a list -> 'a list (the left branch of -> is contravariant) or 'a ref (ref is non-variant). Non-generalized type variables in a type cause no difficulties inside a given structure or compilation unit (the contents of a .ml file, or an interactive session), but they cannot be allowed inside signatures nor in compiled interfaces (.cmi file), because they could be used inconsistently later. Therefore, the compiler flags an error when a structure or compilation unit defines a value name whose type contains non-generalized type variables. There are two ways to fix this error: - Add a type constraint or a .mli file to give a monomorphic type (without type variables) to name. For instance, instead of writing << let sort_int_list = Sort.list (<) (* inferred type 'a list -> 'a list, with 'a not generalized *) >> write << let sort_int_list = (Sort.list (<) : int list -> int list);; >> - If you really need name to have a polymorphic type, turn its defining expression into a function by adding an extra parameter. For instance, instead of writing << let map_length = List.map Array.length (* inferred type 'a array list -> int list, with 'a not generalized *) >> write << let map_length lv = List.map Array.length lv >> Reference to undefined global mod This error appears when trying to link an incomplete or incorrectly ordered set of files. Either you have forgotten to provide an implementation for the compilation unit named mod on the command line (typically, the file named mod.cmo, or a library containing that file). Fix: add the missing .ml or .cmo file to the command line. Or, you have provided an implementation for the module named mod, but it comes too late on the command line: the implementation of mod must come before all bytecode object files that reference mod. Fix: change the order of .ml and .cmo files on the command line. Of course, you will always encounter this error if you have mutually recursive functions across modules. That is, function Mod1.f calls function Mod2.g, and function Mod2.g calls function Mod1.f. In this case, no matter what permutations you perform on the command line, the program will be rejected at link-time. Fixes: - Put f and g in the same module. - Parameterize one function by the other. That is, instead of having <> define <> and link mod1.cmo before mod2.cmo. - Use a reference to hold one of the two functions, as in : < failwith "forward_g") : ) let f x = ... !forward_g ... mod2.ml: let g y = ... Mod1.f ... let _ = Mod1.forward_g := g >> The external function f is not available This error appears when trying to link code that calls external functions written in C. As explained in chapter 19, such code must be linked with C libraries that implement the required f C function. If the C libraries in question are not shared libraries (DLLs), the code must be linked in "custom runtime" mode. Fix: add the required C libraries to the command line, and possibly the -custom option. 8.5 Warning reference *=*=*=*=*=*=*=*=*=*=*= This section describes and explains in detail some warnings: 8.5.1 Warning 52: fragile constant pattern =========================================== Some constructors, such as the exception constructors Failure and Invalid_argument, take as parameter a string value holding a text message intended for the user. These text messages are usually not stable over time: call sites building these constructors may refine the message in a future version to make it more explicit, etc. Therefore, it is dangerous to match over the precise value of the message. For example, until OCaml 4.02, Array.iter2 would raise the exception << Invalid_argument "arrays must have the same length" >> Since 4.03 it raises the more helpful message << Invalid_argument "Array.iter2: arrays must have the same length" >> but this means that any code of the form << try ... with Invalid_argument "arrays must have the same length" -> ... >> is now broken and may suffer from uncaught exceptions. Warning 52 is there to prevent users from writing such fragile code in the first place. It does not occur on every matching on a literal string, but only in the case in which library authors expressed their intent to possibly change the constructor parameter value in the future, by using the attribute ocaml.warn_on_literal_pattern (see the manual section on builtin attributes in 7.18.1): << type t = | Foo of string [@ocaml.warn_on_literal_pattern] | Bar of string let no_warning = function | Bar "specific value" -> 0 | _ -> 1 let warning = function | Foo "specific value" -> 0 | _ -> 1 > | Foo "specific value" -> 0 > ^^^^^^^^^^^^^^^^ > Warning 52: Code should not depend on the actual values of > this constructor's arguments. They are only for information > and may change in future versions. (See manual section 8.5) >> In particular, all built-in exceptions with a string argument have this attribute set: Invalid_argument, Failure, Sys_error will all raise this warning if you match for a specific string argument. If your code raises this warning, you should not change the way you test for the specific string to avoid the warning (for example using a string equality inside the right-hand-side instead of a literal pattern), as your code would remain fragile. You should instead enlarge the scope of the pattern by matching on all possible values. << let warning = function | Foo _ -> 0 | _ -> 1 >> This may require some care: if the scrutinee may return several different cases of the same pattern, or raise distinct instances of the same exception, you may need to modify your code to separate those several cases. For example, < (0, true) | Failure "bool_of_string" -> (-1, false) >> should be rewritten into more atomic tests. For example, using the exception patterns documented in Section 7.21, one can write: < (0, true) | count -> begin match bool_of_string choice_str with | exception (Failure _) -> (-1, false) | choice -> (count, choice) end >> The only case where that transformation is not possible is if a given function call may raises distinct exceptions with the same constructor but different string values. In this case, you will have to check for specific string values. This is dangerous API design and it should be discouraged: it's better to define more precise exception constructors than store useful information in strings. 8.5.2 Warning 57: Ambiguous or-pattern variables under guard ============================================================= The semantics of or-patterns in OCaml is specified with a left-to-right bias: a value v matches the pattern p | q if it matches p or q, but if it matches both, the environment captured by the match is the environment captured by p, never the one captured by q. While this property is generally intuitive, there is at least one specific case where a different semantics might be expected. Consider a pattern followed by a when-guard: | p when g -> e, for example: << | ((Const x, _) | (_, Const x)) when is_neutral x -> branch >> The semantics is clear: match the scrutinee against the pattern, if it matches, test the guard, and if the guard passes, take the branch. In particular, consider the input (Const a, Const b), where a fails the test is_neutral a, while b passes the test is_neutral b. With the left-to-right semantics, the clause above is not taken by its input: matching (Const a, Const b) against the or-pattern succeeds in the left branch, it returns the environment x -> a, and then the guard is_neutral a is tested and fails, the branch is not taken. However, another semantics may be considered more natural here: any pair that has one side passing the test will take the branch. With this semantics the previous code fragment would be equivalent to << | (Const x, _) when is_neutral x -> branch | (_, Const x) when is_neutral x -> branch >> This is not the semantics adopted by OCaml. Warning 57 is dedicated to these confusing cases where the specified left-to-right semantics is not equivalent to a non-deterministic semantics (any branch can be taken) relatively to a specific guard. More precisely, it warns when guard uses "ambiguous" variables, that are bound to different parts of the scrutinees by different sides of a or-pattern. Chapter 9 The toplevel system (ocaml) **************************************** This chapter describes the toplevel system for OCaml, that permits interactive use of the OCaml system through a read-eval-print loop. In this mode, the system repeatedly reads OCaml phrases from the input, then typechecks, compile and evaluate them, then prints the inferred type and result value, if any. The system prints a # (sharp) prompt before reading each phrase. Input to the toplevel can span several lines. It is terminated by ;; (a double-semicolon). The toplevel input consists in one or several toplevel phrases, with the following syntax: + toplevel-input ::= { definition } ;; | expr ;; | # ident [ directive-argument ] ;; directive-argument ::= string-literal | integer-literal | value-path | true | false A phrase can consist of a definition, like those found in implementations of compilation units or in struct ... end module expressions. The definition can bind value names, type names, an exception, a module name, or a module type name. The toplevel system performs the bindings, then prints the types and values (if any) for the names thus defined. A phrase may also consist in a value expression (section 6.7). It is simply evaluated without performing any bindings, and its value is printed. Finally, a phrase can also consist in a toplevel directive, starting with # (the sharp sign). These directives control the behavior of the toplevel; they are listed below in section 9.2. Unix: The toplevel system is started by the command ocaml, as follows: << ocaml options objects # interactive mode ocaml options objects scriptfile # script mode >> options are described below. objects are filenames ending in .cmo or .cma; they are loaded into the interpreter immediately after options are set. scriptfile is any file name not ending in .cmo or .cma. If no scriptfile is given on the command line, the toplevel system enters interactive mode: phrases are read on standard input, results are printed on standard output, errors on standard error. End-of-file on standard input terminates ocaml (see also the #quit directive in section 9.2). On start-up (before the first phrase is read), if the file .ocamlinit exists in the current directory, its contents are read as a sequence of OCaml phrases and executed as per the #use directive described in section 9.2. The evaluation outcode for each phrase are not displayed. If the current directory does not contain an .ocamlinit file, but the user's home directory (environment variable HOME) does, the latter is read and executed as described below. The toplevel system does not perform line editing, but it can easily be used in conjunction with an external line editor such as ledit, ocaml2 or rlwrap (see the Caml Hump (1)). Another option is to use ocaml under Gnu Emacs, which gives the full editing power of Emacs (command run-caml from library inf-caml). At any point, the parsing, compilation or evaluation of the current phrase can be interrupted by pressing ctrl-C (or, more precisely, by sending the INTR signal to the ocaml process). The toplevel then immediately returns to the # prompt. If scriptfile is given on the command-line to ocaml, the toplevel system enters script mode: the contents of the file are read as a sequence of OCaml phrases and executed, as per the #use directive (section 9.2). The outcome of the evaluation is not printed. On reaching the end of file, the ocaml command exits immediately. No commands are read from standard input. Sys.argv is transformed, ignoring all OCaml parameters, and starting with the script file name in Sys.argv.(0). In script mode, the first line of the script is ignored if it starts with #!. Thus, it should be possible to make the script itself executable and put as first line #!/usr/local/bin/ocaml, thus calling the toplevel system automatically when the script is run. However, ocaml itself is a #! script on most installations of OCaml, and Unix kernels usually do not handle nested #! scripts. A better solution is to put the following as the first line of the script: << #!/usr/local/bin/ocamlrun /usr/local/bin/ocaml >> 9.1 Options *=*=*=*=*=*= The following command-line options are recognized by the ocaml command. -absname Force error messages to show absolute paths for file names. -args filename Read additional newline-terminated command line arguments from filename. It is not possible to pass a scriptfile via file to the toplevel. -args0 filename Read additional null character terminated command line arguments from filename. It is not possible to pass a scriptfile via file to the toplevel. -config Print the version number of and a detailed summary of its configuration, then exit. -I directory Add the given directory to the list of directories searched for source and compiled files. By default, the current directory is searched first, then the standard library directory. Directories added with -I are searched after the current directory, in the order in which they were given on the command line, but before the standard library directory. See also option -nostdlib. If the given directory starts with +, it is taken relative to the standard library directory. For instance, -I +labltk adds the subdirectory labltk of the standard library to the search path. Directories can also be added to the list once the toplevel is running with the #directory directive (section 9.2). -init file Load the given file instead of the default initialization file. The default file is .ocamlinit in the current directory if it exists, otherwise .ocamlinit in the user's home directory. -labels Labels are not ignored in types, labels may be used in applications, and labelled parameters can be given in any order. This is the default. -no-app-funct Deactivates the applicative behaviour of functors. With this option, each functor application generates new types in its result and applying the same functor twice to the same argument yields two incompatible structures. -noassert Do not compile assertion checks. Note that the special form assert false is always compiled because it is typed specially. -nolabels Ignore non-optional labels in types. Labels cannot be used in applications, and parameter order becomes strict. -noprompt Do not display any prompt when waiting for input. -nopromptcont Do not display the secondary prompt when waiting for continuation lines in multi-line inputs. This should be used e.g. when running ocaml in an emacs window. -nostdlib Do not include the standard library directory in the list of directories searched for source and compiled files. -ppx command After parsing, pipe the abstract syntax tree through the preprocessor command. The module Ast_mapper, described in section 26.1, implements the external interface of a preprocessor. -principal Check information path during type-checking, to make sure that all types are derived in a principal way. When using labelled arguments and/or polymorphic methods, this flag is required to ensure future versions of the compiler will be able to infer types correctly, even if internal algorithms change. All programs accepted in -principal mode are also accepted in the default mode with equivalent types, but different binary signatures, and this may slow down type checking; yet it is a good idea to use it once before publishing source code. -rectypes Allow arbitrary recursive types during type-checking. By default, only recursive types where the recursion goes through an object type are supported. -safe-string Enforce the separation between types string and bytes, thereby making strings read-only. This will become the default in a future version of OCaml. -short-paths When a type is visible under several module-paths, use the shortest one when printing the type's name in inferred interfaces and error and warning messages. Identifier names starting with an underscore _ or containing double underscores __ incur a penalty of +10 when computing their length. -stdin Read the standard input as a script file rather than starting an interactive session. -strict-sequence Force the left-hand part of each sequence to have type unit. -strict-formats Reject invalid formats that were accepted in legacy format implementations. You should use this flag to detect and fix such invalid formats, as they will be rejected by future OCaml versions. -unsafe Turn bound checking off for array and string accesses (the v.(i) and s.[i] constructs). Programs compiled with -unsafe are therefore faster, but unsafe: anything can happen if the program accesses an array or string outside of its bounds. -unsafe-string Identify the types string and bytes, thereby making strings writable. For reasons of backward compatibility, this is the default setting for the moment, but this will change in a future version of OCaml. -v Print the version number of the compiler and the location of the standard library directory, then exit. -verbose Print all external commands before they are executed, Useful to debug C library problems. -version Print version string and exit. -vnum Print short version number and exit. -no-version Do not print the version banner at startup. -w warning-list Enable, disable, or mark as fatal the warnings specified by the argument warning-list. Each warning can be enabled or disabled, and each warning can be fatal or non-fatal. If a warning is disabled, it isn't displayed and doesn't affect compilation in any way (even if it is fatal). If a warning is enabled, it is displayed normally by the compiler whenever the source code triggers it. If it is enabled and fatal, the compiler will also stop with an error after displaying it. The warning-list argument is a sequence of warning specifiers, with no separators between them. A warning specifier is one of the following: +num Enable warning number num. -num Disable warning number num. @num Enable and mark as fatal warning number num. +num1..num2 Enable warnings in the given range. -num1..num2 Disable warnings in the given range. @num1..num2 Enable and mark as fatal warnings in the given range. +letter Enable the set of warnings corresponding to letter. The letter may be uppercase or lowercase. -letter Disable the set of warnings corresponding to letter. The letter may be uppercase or lowercase. @letter Enable and mark as fatal the set of warnings corresponding to letter. The letter may be uppercase or lowercase. uppercase-letter Enable the set of warnings corresponding to uppercase-letter. lowercase-letter Disable the set of warnings corresponding to lowercase-letter. Warning numbers and letters which are out of the range of warnings that are currently defined are ignored. The warnings are as follows. 1 Suspicious-looking start-of-comment mark. 2 Suspicious-looking end-of-comment mark. 3 Deprecated feature. 4 Fragile pattern matching: matching that will remain complete even if additional constructors are added to one of the variant types matched. 5 Partially applied function: expression whose result has function type and is ignored. 6 Label omitted in function application. 7 Method overridden. 8 Partial match: missing cases in pattern-matching. 9 Missing fields in a record pattern. 10 Expression on the left-hand side of a sequence that doesn't have type unit (and that is not a function, see warning number 5). 11 Redundant case in a pattern matching (unused match case). 12 Redundant sub-pattern in a pattern-matching. 13 Instance variable overridden. 14 Illegal backslash escape in a string constant. 15 Private method made public implicitly. 16 Unerasable optional argument. 17 Undeclared virtual method. 18 Non-principal type. 19 Type without principality. 20 Unused function argument. 21 Non-returning statement. 22 Preprocessor warning. 23 Useless record with clause. 24 Bad module name: the source file name is not a valid OCaml module name. 26 Suspicious unused variable: unused variable that is bound with let or as, and doesn't start with an underscore (_) character. 27 Innocuous unused variable: unused variable that is not bound with let nor as, and doesn't start with an underscore (_) character. 28 Wildcard pattern given as argument to a constant constructor. 29 Unescaped end-of-line in a string constant (non-portable code). 30 Two labels or constructors of the same name are defined in two mutually recursive types. 31 A module is linked twice in the same executable. 32 Unused value declaration. 33 Unused open statement. 34 Unused type declaration. 35 Unused for-loop index. 36 Unused ancestor variable. 37 Unused constructor. 38 Unused extension constructor. 39 Unused rec flag. 40 Constructor or label name used out of scope. 41 Ambiguous constructor or label name. 42 Disambiguated constructor or label name (compatibility warning). 43 Nonoptional label applied as optional. 44 Open statement shadows an already defined identifier. 45 Open statement shadows an already defined label or constructor. 46 Error in environment variable. 47 Illegal attribute payload. 48 Implicit elimination of optional arguments. 49 Absent cmi file when looking up module alias. 50 Unexpected documentation comment. 51 Warning on non-tail calls if @tailcall present. 52 (see 8.5.1) Fragile constant pattern. 53 Attribute cannot appear in this context 54 Attribute used more than once on an expression 55 Inlining impossible 56 Unreachable case in a pattern-matching (based on type information). 57 (see 8.5.2) Ambiguous or-pattern variables under guard 58 Missing cmx file 59 Assignment to non-mutable value 60 Unused module declaration A all warnings C warnings 1, 2. D Alias for warning 3. E Alias for warning 4. F Alias for warning 5. K warnings 32, 33, 34, 35, 36, 37, 38, 39. L Alias for warning 6. M Alias for warning 7. P Alias for warning 8. R Alias for warning 9. S Alias for warning 10. U warnings 11, 12. V Alias for warning 13. X warnings 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 30. Y Alias for warning 26. Z Alias for warning 27. The default setting is -w +a-4-6-7-9-27-29-32..39-41..42-44-45-48-50. It is displayed by -help. Note that warnings 5 and 10 are not always triggered, depending on the internals of the type checker. -warn-error warning-list Mark as fatal the warnings specified in the argument warning-list. The compiler will stop with an error when one of these warnings is emitted. The warning-list has the same meaning as for the -w option: a + sign (or an uppercase letter) marks the corresponding warnings as fatal, a - sign (or a lowercase letter) turns them back into non-fatal warnings, and a @ sign both enables and marks as fatal the corresponding warnings. Note: it is not recommended to use warning sets (i.e. letters) as arguments to -warn-error in production code, because this can break your build when future versions of OCaml add some new warnings. The default setting is -warn-error -a+31 (only warning 31 is fatal). -warn-help Show the description of all available warning numbers. - file Use file as a script file name, even when it starts with a hyphen (-). -help or --help Display a short usage summary and exit. Unix: The following environment variables are also consulted: TERM When printing error messages, the toplevel system attempts to underline visually the location of the error. It consults the TERM variable to determines the type of output terminal and look up its capabilities in the terminal database. HOME Directory where the .ocamlinit file is searched. 9.2 Toplevel directives *=*=*=*=*=*=*=*=*=*=*=*= The following directives control the toplevel behavior, load files in memory, and trace program execution. Note: all directives start with a # (sharp) symbol. This # must be typed before the directive, and must not be confused with the # prompt displayed by the interactive loop. For instance, typing #quit;; will exit the toplevel loop, but typing quit;; will result in an "unbound value quit" error. General #help;; Prints a list of all available directives, with corresponding argument type if appropriate. #quit;; Exit the toplevel loop and terminate the ocaml command. Loading codes #cd "dir-name";; Change the current working directory. #directory "dir-name";; Add the given directory to the list of directories searched for source and compiled files. #remove_directory "dir-name";; Remove the given directory from the list of directories searched for source and compiled files. Do nothing if the list does not contain the given directory. #load "file-name";; Load in memory a bytecode object file (.cmo file) or library file (.cma file) produced by the batch compiler ocamlc. #load_rec "file-name";; Load in memory a bytecode object file (.cmo file) or library file (.cma file) produced by the batch compiler ocamlc. When loading an object file that depends on other modules which have not been loaded yet, the .cmo files for these modules are searched and loaded as well, recursively. The loading order is not specified. #use "file-name";; Read, compile and execute source phrases from the given file. This is textual inclusion: phrases are processed just as if they were typed on standard input. The reading of the file stops at the first error encountered. #mod_use "file-name";; Similar to #use but also wrap the code into a top-level module of the same name as capitalized file name without extensions, following semantics of the compiler. For directives that take file names as arguments, if the given file name specifies no directory, the file is searched in the following directories: 1. In script mode, the directory containing the script currently executing; in interactive mode, the current working directory. 2. Directories added with the #directory directive. 3. Directories given on the command line with -I options. 4. The standard library directory. Environment queries #show_class class-path;; #show_class_type class-path;; #show_exception ident;; #show_module module-path;; #show_module_type modtype-path;; #show_type typeconstr;; #show_val value-path;; Print the signature of the corresponding component. #show ident;; Print the signatures of components with name ident in all the above categories. Pretty-printing #install_printer printer-name;; This directive registers the function named printer-name (a value path) as a printer for values whose types match the argument type of the function. That is, the toplevel loop will call printer-name when it has such a value to print. The printing function printer-name should have type Format.formatter -> t -> unit, where t is the type for the values to be printed, and should output its textual representation for the value of type t on the given formatter, using the functions provided by the Format library. For backward compatibility, printer-name can also have type t -> unit and should then output on the standard formatter, but this usage is deprecated. #print_depth n;; Limit the printing of values to a maximal depth of n. The parts of values whose depth exceeds n are printed as ... (ellipsis). #print_length n;; Limit the number of value nodes printed to at most n. Remaining parts of values are printed as ... (ellipsis). #remove_printer printer-name;; Remove the named function from the table of toplevel printers. Tracing #trace function-name;; After executing this directive, all calls to the function named function-name will be "traced". That is, the argument and the result are displayed for each call, as well as the exceptions escaping out of the function, raised either by the function itself or by another function it calls. If the function is curried, each argument is printed as it is passed to the function. #untrace function-name;; Stop tracing the given function. #untrace_all;; Stop tracing all functions traced so far. Compiler options #labels bool;; Ignore labels in function types if argument is false, or switch back to default behaviour (commuting style) if argument is true. #ppx "file-name";; After parsing, pipe the abstract syntax tree through the preprocessor command. #principal bool;; If the argument is true, check information paths during type-checking, to make sure that all types are derived in a principal way. If the argument is false, do not check information paths. #rectypes;; Allow arbitrary recursive types during type-checking. Note: once enabled, this option cannot be disabled because that would lead to unsoundness of the type system. #warn_error "warning-list";; Treat as errors the warnings enabled by the argument and as normal warnings the warnings disabled by the argument. #warnings "warning-list";; Enable or disable warnings according to the argument. 9.3 The toplevel and the module system *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* Toplevel phrases can refer to identifiers defined in compilation units with the same mechanisms as for separately compiled units: either by using qualified names (Modulename.localname), or by using the open construct and unqualified names (see section 6.3). However, before referencing another compilation unit, an implementation of that unit must be present in memory. At start-up, the toplevel system contains implementations for all the modules in the the standard library. Implementations for user modules can be entered with the #load directive described above. Referencing a unit for which no implementation has been provided results in the error Reference to undefined global `...'. Note that entering open Mod merely accesses the compiled interface (.cmi file) for Mod, but does not load the implementation of Mod, and does not cause any error if no implementation of Mod has been loaded. The error "reference to undefined global Mod" will occur only when executing a value or module definition that refers to Mod. 9.4 Common errors *=*=*=*=*=*=*=*=*= This section describes and explains the most frequently encountered error messages. Cannot find file filename The named file could not be found in the current directory, nor in the directories of the search path. If filename has the format mod.cmi, this means you have referenced the compilation unit mod, but its compiled interface could not be found. Fix: compile mod.mli or mod.ml first, to create the compiled interface mod.cmi. If filename has the format mod.cmo, this means you are trying to load with #load a bytecode object file that does not exist yet. Fix: compile mod.ml first. If your program spans several directories, this error can also appear because you haven't specified the directories to look into. Fix: use the #directory directive to add the correct directories to the search path. This expression has type t_1, but is used with type t_2 See section 8.4. Reference to undefined global mod You have neglected to load in memory an implementation for a module with #load. See section 9.3 above. 9.5 Building custom toplevel systems: ocamlmktop *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* The ocamlmktop command builds OCaml toplevels that contain user code preloaded at start-up. The ocamlmktop command takes as argument a set of .cmo and .cma files, and links them with the object files that implement the OCaml toplevel. The typical use is: << ocamlmktop -o mytoplevel foo.cmo bar.cmo gee.cmo >> This creates the bytecode file mytoplevel, containing the OCaml toplevel system, plus the code from the three .cmo files. This toplevel is directly executable and is started by: << ./mytoplevel >> This enters a regular toplevel loop, except that the code from foo.cmo, bar.cmo and gee.cmo is already loaded in memory, just as if you had typed: << #load "foo.cmo";; #load "bar.cmo";; #load "gee.cmo";; >> on entrance to the toplevel. The modules Foo, Bar and Gee are not opened, though; you still have to do << open Foo;; >> yourself, if this is what you wish. 9.6 Options *=*=*=*=*=*= The following command-line options are recognized by ocamlmktop. -cclib libname Pass the -llibname option to the C linker when linking in "custom runtime" mode. See the corresponding option for ocamlc, in chapter 8. -ccopt option Pass the given option to the C compiler and linker, when linking in "custom runtime" mode. See the corresponding option for ocamlc, in chapter 8. -custom Link in "custom runtime" mode. See the corresponding option for ocamlc, in chapter 8. -I directory Add the given directory to the list of directories searched for compiled object code files (.cmo and .cma). -o exec-file Specify the name of the toplevel file produced by the linker. The default is a.out. --------------------------------------- (1) http://caml.inria.fr/humps/index_framed_caml.html Chapter 10 The runtime system (ocamlrun) ******************************************* The ocamlrun command executes bytecode files produced by the linking phase of the ocamlc command. 10.1 Overview *=*=*=*=*=*=*= The ocamlrun command comprises three main parts: the bytecode interpreter, that actually executes bytecode files; the memory allocator and garbage collector; and a set of C functions that implement primitive operations such as input/output. The usage for ocamlrun is: << ocamlrun options bytecode-executable arg_1 ... arg_n >> The first non-option argument is taken to be the name of the file containing the executable bytecode. (That file is searched in the executable path as well as in the current directory.) The remaining arguments are passed to the OCaml program, in the string array Sys.argv. Element 0 of this array is the name of the bytecode executable file; elements 1 to n are the remaining arguments arg_1 to arg_n. As mentioned in chapter 8, the bytecode executable files produced by the ocamlc command are self-executable, and manage to launch the ocamlrun command on themselves automatically. That is, assuming a.out is a bytecode executable file, << a.out arg_1 ... arg_n >> works exactly as << ocamlrun a.out arg_1 ... arg_n >> Notice that it is not possible to pass options to ocamlrun when invoking a.out directly. Windows: Under several versions of Windows, bytecode executable files are self-executable only if their name ends in .exe. It is recommended to always give .exe names to bytecode executables, e.g. compile with ocamlc -o myprog.exe ... rather than ocamlc -o myprog .... 10.2 Options *=*=*=*=*=*=* The following command-line options are recognized by ocamlrun. -b When the program aborts due to an uncaught exception, print a detailed "back trace" of the execution, showing where the exception was raised and which function calls were outstanding at this point. The back trace is printed only if the bytecode executable contains debugging information, i.e. was compiled and linked with the -g option to ocamlc set. This is equivalent to setting the b flag in the OCAMLRUNPARAM environment variable (see below). -I dir Search the directory dir for dynamically-loaded libraries, in addition to the standard search path (see section 10.3). -p Print the names of the primitives known to this version of ocamlrun and exit. -v Direct the memory manager to print some progress messages on standard error. This is equivalent to setting v=63 in the OCAMLRUNPARAM environment variable (see below). -version Print version string and exit. -vnum Print short version number and exit. The following environment variables are also consulted: CAML_LD_LIBRARY_PATH Additional directories to search for dynamically-loaded libraries (see section 10.3). OCAMLLIB The directory containing the OCaml standard library. (If OCAMLLIB is not set, CAMLLIB will be used instead.) Used to locate the ld.conf configuration file for dynamic loading (see section 10.3). If not set, default to the library directory specified when compiling OCaml. OCAMLRUNPARAM Set the runtime system options and garbage collection parameters. (If OCAMLRUNPARAM is not set, CAMLRUNPARAM will be used instead.) This variable must be a sequence of parameter specifications separated by commas. A parameter specification is an option letter followed by an = sign, a decimal number (or an hexadecimal number prefixed by 0x), and an optional multiplier. The options are documented below; the last six correspond to the fields of the control record documented in section 24.14. b (backtrace) Trigger the printing of a stack backtrace when an uncaught exception aborts the program. This option takes no argument. p (parser trace) Turn on debugging support for ocamlyacc-generated parsers. When this option is on, the pushdown automaton that executes the parsers prints a trace of its actions. This option takes no argument. R (randomize) Turn on randomization of all hash tables by default (see section 24.16). This option takes no argument. h The initial size of the major heap (in words). a (allocation_policy) The policy used for allocating in the OCaml heap. Possible values are 0 for the next-fit policy, and 1 for the first-fit policy. Next-fit is usually faster, but first-fit is better for avoiding fragmentation and the associated heap compactions. s (minor_heap_size) Size of the minor heap. (in words) i (major_heap_increment) Default size increment for the major heap. (in words) o (space_overhead) The major GC speed setting. O (max_overhead) The heap compaction trigger setting. l (stack_limit) The limit (in words) of the stack size. v (verbose) What GC messages to print to stderr. This is a sum of values selected from the following: 1 (= 0x001) Start of major GC cycle. 2 (= 0x002) Minor collection and major GC slice. 4 (= 0x004) Growing and shrinking of the heap. 8 (= 0x008) Resizing of stacks and memory manager tables. 16 (= 0x010) Heap compaction. 32 (= 0x020) Change of GC parameters. 64 (= 0x040) Computation of major GC slice size. 128 (= 0x080) Calling of finalization functions 256 (= 0x100) Startup messages (loading the bytecode executable file, resolving shared libraries). 512 (= 0x200) Computation of compaction-triggering condition. 1024 (= 0x400) Output GC statistics at program exit. The multiplier is k, M, or G, for multiplication by 2^10, 2^20, and 2^30 respectively. If the option letter is not recognized, the whole parameter is ignored; if the equal sign or the number is missing, the value is taken as 1; if the multiplier is not recognized, it is ignored. For example, on a 32-bit machine, under bash the command << export OCAMLRUNPARAM='b,s=256k,v=0x015' >> tells a subsequent ocamlrun to print backtraces for uncaught exceptions, set its initial minor heap size to 1 megabyte and print a message at the start of each major GC cycle, when the heap size changes, and when compaction is triggered. CAMLRUNPARAM If OCAMLRUNPARAM is not found in the environment, then CAMLRUNPARAM will be used instead. If CAMLRUNPARAM is also not found, then the default values will be used. PATH List of directories searched to find the bytecode executable file. 10.3 Dynamic loading of shared libraries *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* On platforms that support dynamic loading, ocamlrun can link dynamically with C shared libraries (DLLs) providing additional C primitives beyond those provided by the standard runtime system. The names for these libraries are provided at link time as described in section 19.1.4), and recorded in the bytecode executable file; ocamlrun, then, locates these libraries and resolves references to their primitives when the bytecode executable program starts. The ocamlrun command searches shared libraries in the following directories, in the order indicated: 1. Directories specified on the ocamlrun command line with the -I option. 2. Directories specified in the CAML_LD_LIBRARY_PATH environment variable. 3. Directories specified at link-time via the -dllpath option to ocamlc. (These directories are recorded in the bytecode executable file.) 4. Directories specified in the file ld.conf. This file resides in the OCaml standard library directory, and lists directory names (one per line) to be searched. Typically, it contains only one line naming the stublibs subdirectory of the OCaml standard library directory. Users can add there the names of other directories containing frequently-used shared libraries; however, for consistency of installation, we recommend that shared libraries are installed directly in the system stublibs directory, rather than adding lines to the ld.conf file. 5. Default directories searched by the system dynamic loader. Under Unix, these generally include /lib and /usr/lib, plus the directories listed in the file /etc/ld.so.conf and the environment variable LD_LIBRARY_PATH. Under Windows, these include the Windows system directories, plus the directories listed in the PATH environment variable. 10.4 Common errors *=*=*=*=*=*=*=*=*=* This section describes and explains the most frequently encountered error messages. filename: no such file or directory If filename is the name of a self-executable bytecode file, this means that either that file does not exist, or that it failed to run the ocamlrun bytecode interpreter on itself. The second possibility indicates that OCaml has not been properly installed on your system. Cannot exec ocamlrun (When launching a self-executable bytecode file.) The ocamlrun could not be found in the executable path. Check that OCaml has been properly installed on your system. Cannot find the bytecode file The file that ocamlrun is trying to execute (e.g. the file given as first non-option argument to ocamlrun) either does not exist, or is not a valid executable bytecode file. Truncated bytecode file The file that ocamlrun is trying to execute is not a valid executable bytecode file. Probably it has been truncated or mangled since created. Erase and rebuild it. Uncaught exception The program being executed contains a "stray" exception. That is, it raises an exception at some point, and this exception is never caught. This causes immediate termination of the program. The name of the exception is printed, along with its string, byte sequence, and integer arguments (arguments of more complex types are not correctly printed). To locate the context of the uncaught exception, compile the program with the -g option and either run it again under the ocamldebug debugger (see chapter 16), or run it with ocamlrun -b or with the OCAMLRUNPARAM environment variable set to b=1. Out of memory The program being executed requires more memory than available. Either the program builds excessively large data structures; or the program contains too many nested function calls, and the stack overflows. In some cases, your program is perfectly correct, it just requires more memory than your machine provides. In other cases, the "out of memory" message reveals an error in your program: non-terminating recursive function, allocation of an excessively large array, string or byte sequence, attempts to build an infinite list or other data structure, ... To help you diagnose this error, run your program with the -v option to ocamlrun, or with the OCAMLRUNPARAM environment variable set to v=63. If it displays lots of "Growing stack..." messages, this is probably a looping recursive function. If it displays lots of "Growing heap..." messages, with the heap size growing slowly, this is probably an attempt to construct a data structure with too many (infinitely many?) cells. If it displays few "Growing heap..." messages, but with a huge increment in the heap size, this is probably an attempt to build an excessively large array, string or byte sequence. Chapter 11 Native-code compilation (ocamlopt) ************************************************ This chapter describes the OCaml high-performance native-code compiler ocamlopt, which compiles OCaml source files to native code object files and link these object files to produce standalone executables. The native-code compiler is only available on certain platforms. It produces code that runs faster than the bytecode produced by ocamlc, at the cost of increased compilation time and executable code size. Compatibility with the bytecode compiler is extremely high: the same source code should run identically when compiled with ocamlc and ocamlopt. It is not possible to mix native-code object files produced by ocamlopt with bytecode object files produced by ocamlc: a program must be compiled entirely with ocamlopt or entirely with ocamlc. Native-code object files produced by ocamlopt cannot be loaded in the toplevel system ocaml. 11.1 Overview of the compiler *=*=*=*=*=*=*=*=*=*=*=*=*=*=*= The ocamlopt command has a command-line interface very close to that of ocamlc. It accepts the same types of arguments, and processes them sequentially, after all options have been processed: - Arguments ending in .mli are taken to be source files for compilation unit interfaces. Interfaces specify the names exported by compilation units: they declare value names with their types, define public data types, declare abstract data types, and so on. From the file x.mli, the ocamlopt compiler produces a compiled interface in the file x.cmi. The interface produced is identical to that produced by the bytecode compiler ocamlc. - Arguments ending in .ml are taken to be source files for compilation unit implementations. Implementations provide definitions for the names exported by the unit, and also contain expressions to be evaluated for their side-effects. From the file x.ml, the ocamlopt compiler produces two files: x.o, containing native object code, and x.cmx, containing extra information for linking and optimization of the clients of the unit. The compiled implementation should always be referred to under the name x.cmx (when given a .o or .obj file, ocamlopt assumes that it contains code compiled from C, not from OCaml). The implementation is checked against the interface file x.mli (if it exists) as described in the manual for ocamlc (chapter 8). - Arguments ending in .cmx are taken to be compiled object code. These files are linked together, along with the object files obtained by compiling .ml arguments (if any), and the OCaml standard library, to produce a native-code executable program. The order in which .cmx and .ml arguments are presented on the command line is relevant: compilation units are initialized in that order at run-time, and it is a link-time error to use a component of a unit before having initialized it. Hence, a given x.cmx file must come before all .cmx files that refer to the unit x. - Arguments ending in .cmxa are taken to be libraries of object code. Such a library packs in two files (lib.cmxa and lib.a/.lib) a set of object files (.cmx and .o/.obj files). Libraries are build with ocamlopt -a (see the description of the -a option below). The object files contained in the library are linked as regular .cmx files (see above), in the order specified when the library was built. The only difference is that if an object file contained in a library is not referenced anywhere in the program, then it is not linked in. - Arguments ending in .c are passed to the C compiler, which generates a .o/.obj object file. This object file is linked with the program. - Arguments ending in .o, .a or .so (.obj, .lib and .dll under Windows) are assumed to be C object files and libraries. They are linked with the program. The output of the linking phase is a regular Unix or Windows executable file. It does not need ocamlrun to run. 11.2 Options *=*=*=*=*=*=* The following command-line options are recognized by ocamlopt. The options -pack, -a, -shared, -c and -output-obj are mutually exclusive. -a Build a library(.cmxa and .a/.lib files) with the object files (.cmx and .o/.obj files) given on the command line, instead of linking them into an executable file. The name of the library must be set with the -o option. If -cclib or -ccopt options are passed on the command line, these options are stored in the resulting .cmxalibrary. Then, linking with this library automatically adds back the -cclib and -ccopt options as if they had been provided on the command line, unless the -noautolink option is given. -absname Force error messages to show absolute paths for file names. -annot Dump detailed information about the compilation (types, bindings, tail-calls, etc). The information for file src.ml is put into file src.annot. In case of a type error, dump all the information inferred by the type-checker before the error. The src.annot file can be used with the emacs commands given in emacs/caml-types.el to display types and other annotations interactively. -args filename Read additional newline-terminated command line arguments from filename. -args0 filename Read additional null character terminated command line arguments from filename. -bin-annot Dump detailed information about the compilation (types, bindings, tail-calls, etc) in binary format. The information for file src.ml is put into file src.cmt. In case of a type error, dump all the information inferred by the type-checker before the error. The *.cmt files produced by -bin-annot contain more information and are much more compact than the files produced by -annot. -c Compile only. Suppress the linking phase of the compilation. Source code files are turned into compiled files, but no executable file is produced. This option is useful to compile modules separately. -cc ccomp Use ccomp as the C linker called to build the final executable and as the C compiler for compiling .c source files. -cclib -llibname Pass the -llibname option to the linker . This causes the given C library to be linked with the program. -ccopt option Pass the given option to the C compiler and linker. For instance,-ccopt -Ldir causes the C linker to search for C libraries in directory dir. -color mode Enable or disable colors in compiler messages (especially warnings and errors). The following modes are supported: auto use heuristics to enable colors only if the output supports them (an ANSI-compatible tty terminal); always enable colors unconditionally; never disable color output. The default setting is 'auto', and the current heuristic checks that the TERM environment variable exists and is not empty or dumb, and that 'isatty(stderr)' holds. The environment variable OCAML_COLOR is considered if -color is not provided. Its values are auto/always/never as above. -compact Optimize the produced code for space rather than for time. This results in slightly smaller but slightly slower programs. The default is to optimize for speed. -config Print the version number of ocamlopt and a detailed summary of its configuration, then exit. -for-pack module-path Generate an object file (.cmx and .o/.obj files) that can later be included as a sub-module (with the given access path) of a compilation unit constructed with -pack. For instance, ocamlopt -for-pack P -c A.ml will generate a..cmx and a.o files that can later be used with ocamlopt -pack -o P.cmx a.cmx. Note: you can still pack a module that was compiled without -for-pack but in this case exceptions will be printed with the wrong names. -g Add debugging information while compiling and linking. This option is required in order to produce stack backtraces when the program terminates on an uncaught exception (see section 10.2). -i Cause the compiler to print all defined names (with their inferred types or their definitions) when compiling an implementation (.ml file). No compiled files (.cmo and .cmi files) are produced. This can be useful to check the types inferred by the compiler. Also, since the output follows the syntax of interfaces, it can help in writing an explicit interface (.mli file) for a file: just redirect the standard output of the compiler to a .mli file, and edit that file to remove all declarations of unexported names. -I directory Add the given directory to the list of directories searched for compiled interface files (.cmi), compiled object code files (.cmx), and libraries (.cmxa). By default, the current directory is searched first, then the standard library directory. Directories added with -I are searched after the current directory, in the order in which they were given on the command line, but before the standard library directory. See also option -nostdlib. If the given directory starts with +, it is taken relative to the standard library directory. For instance, -I +labltk adds the subdirectory labltk of the standard library to the search path. -impl filename Compile the file filename as an implementation file, even if its extension is not .ml. -inline n Set aggressiveness of inlining to n, where n is a positive integer. Specifying -inline 0 prevents all functions from being inlined, except those whose body is smaller than the call site. Thus, inlining causes no expansion in code size. The default aggressiveness, -inline 1, allows slightly larger functions to be inlined, resulting in a slight expansion in code size. Higher values for the -inline option cause larger and larger functions to become candidate for inlining, but can result in a serious increase in code size. -intf filename Compile the file filename as an interface file, even if its extension is not .mli. -intf-suffix string Recognize file names ending with string as interface files (instead of the default .mli). -labels Labels are not ignored in types, labels may be used in applications, and labelled parameters can be given in any order. This is the default. -linkall Force all modules contained in libraries to be linked in. If this flag is not given, unreferenced modules are not linked in. When building a library (option -a), setting the -linkall option forces all subsequent links of programs involving that library to link all the modules contained in the library. When compiling a module (option -c), setting the -linkall option ensures that this module will always be linked if it is put in a library and this library is linked. -no-alias-deps Do not record dependencies for module aliases. See section 7.13 for more information. -no-app-funct Deactivates the applicative behaviour of functors. With this option, each functor application generates new types in its result and applying the same functor twice to the same argument yields two incompatible structures. -noassert Do not compile assertion checks. Note that the special form assert false is always compiled because it is typed specially. This flag has no effect when linking already-compiled files. -noautolink When linking .cmxalibraries, ignore -cclib and -ccopt options potentially contained in the libraries (if these options were given when building the libraries). This can be useful if a library contains incorrect specifications of C libraries or C options; in this case, during linking, set -noautolink and pass the correct C libraries and options on the command line. -nodynlink Allow the compiler to use some optimizations that are valid only for code that is never dynlinked. -nolabels Ignore non-optional labels in types. Labels cannot be used in applications, and parameter order becomes strict. -nostdlib Do not automatically add the standard library directory the list of directories searched for compiled interface files (.cmi), compiled object code files (.cmx), and libraries (.cmxa). See also option -I. -o exec-file Specify the name of the output file produced by the linker. The default output name is a.out under Unix and camlprog.exe under Windows. If the -a option is given, specify the name of the library produced. If the -pack option is given, specify the name of the packed object file produced. If the -output-obj option is given, specify the name of the output file produced. If the -shared option is given, specify the name of plugin file produced. -opaque When the native compiler compiles an implementation, by default it produces a .cmx file containing information for cross-module optimization. It also expects .cmx files to be present for the dependencies of the currently compiled source, and uses them for optimization. Since OCaml 4.03, the compiler will emit a warning if it is unable to locate the .cmx file of one of those dependencies. The -opaque option, available since 4.04, disables cross-module optimization information for the currently compiled unit. When compiling .mli interface, using -opaque marks the compiled .cmi interface so that subsequent compilations of modules that depend on it will not rely on the corresponding .cmx file, nor warn if it is absent. When the native compiler compiles a .ml implementation, using -opaque generates a .cmx that does not contain any cross-module optimization information. Using this option may degrade the quality of generated code, but it reduces compilation time, both on clean and incremental builds. Indeed, with the native compiler, when the implementation of a compilation unit changes, all the units that depend on it may need to be recompiled -- because the cross-module information may have changed. If the compilation unit whose implementation changed was compiled with -opaque, no such recompilation needs to occur. This option can thus be used, for example, to get faster edit-compile-test feedback loops. -open Module Opens the given module before processing the interface or implementation files. If several -open options are given, they are processed in order, just as if the statements open! Module1;; ... open! ModuleN;; were added at the top of each file. -output-obj Cause the linker to produce a C object file instead of an executable file. This is useful to wrap OCaml code as a C library, callable from any C program. See chapter 19, section 19.7.5. The name of the output object file must be set with the -o option. This option can also be used to produce a compiled shared/dynamic library (.so extension, .dll under Windows). -p Generate extra code to write profile information when the program is executed. The profile information can then be examined with the analysis program gprof. (See chapter 17 for more information on profiling.) The -p option must be given both at compile-time and at link-time. Linking object files not compiled with -p is possible, but results in less precise profiling. Unix: See the Unix manual page for gprof(1) for more information about the profiles. Full support for gprof is only available for certain platforms (currently: Intel x86 32 and 64 bits under Linux, BSD and MacOS X). On other platforms, the -p option will result in a less precise profile (no call graph information, only a time profile). Windows: The -p option does not work under Windows. -pack Build an object file (.cmx and .o/.obj files) and its associated compiled interface (.cmi) that combines the .cmx object files given on the command line, making them appear as sub-modules of the output .cmx file. The name of the output .cmx file must be given with the -o option. For instance, << ocamlopt -pack -o P.cmx A.cmx B.cmx C.cmx >> generates compiled files P.cmx, P.o and P.cmi describing a compilation unit having three sub-modules A, B and C, corresponding to the contents of the object files A.cmx, B.cmx and C.cmx. These contents can be referenced as P.A, P.B and P.C in the remainder of the program. The .cmx object files being combined must have been compiled with the appropriate -for-pack option. In the example above, A.cmx, B.cmx and C.cmx must have been compiled with ocamlopt -for-pack P. Multiple levels of packing can be achieved by combining -pack with -for-pack. Consider the following example: << ocamlopt -for-pack P.Q -c A.ml ocamlopt -pack -o Q.cmx -for-pack P A.cmx ocamlopt -for-pack P -c B.ml ocamlopt -pack -o P.cmx Q.cmx B.cmx >> The resulting P.cmx object file has sub-modules P.Q, P.Q.A and P.B. -plugin plugin Dynamically load the code of the given plugin (a .cmo, .cma or .cmxs file) in the compiler. plugin must exist in the same kind of code as the compiler (ocamlopt.byte must load bytecode plugins, while ocamlopt.opt must load native code plugins), and extension adaptation is done automatically for .cma files (to .cmxs files if the compiler is compiled in native code). -pp command Cause the compiler to call the given command as a preprocessor for each source file. The output of command is redirected to an intermediate file, which is compiled. If there are no compilation errors, the intermediate file is deleted afterwards. -ppx command After parsing, pipe the abstract syntax tree through the preprocessor command. The module Ast_mapper, described in section 26.1, implements the external interface of a preprocessor. -principal Check information path during type-checking, to make sure that all types are derived in a principal way. When using labelled arguments and/or polymorphic methods, this flag is required to ensure future versions of the compiler will be able to infer types correctly, even if internal algorithms change. All programs accepted in -principal mode are also accepted in the default mode with equivalent types, but different binary signatures, and this may slow down type checking; yet it is a good idea to use it once before publishing source code. -rectypes Allow arbitrary recursive types during type-checking. By default, only recursive types where the recursion goes through an object type are supported.Note that once you have created an interface using this flag, you must use it again for all dependencies. -runtime-variant suffix Add the suffix string to the name of the runtime library used by the program. Currently, only one such suffix is supported: d, and only if the OCaml compiler was configured with option -with-debug-runtime. This suffix gives the debug version of the runtime, which is useful for debugging pointer problems in low-level code such as C stubs. -S Keep the assembly code produced during the compilation. The assembly code for the source file x.ml is saved in the file x.s. -shared Build a plugin (usually .cmxs) that can be dynamically loaded with the Dynlink module. The name of the plugin must be set with the -o option. A plugin can include a number of OCaml modules and libraries, and extra native objects (.o, .obj, .a, .lib files). Building native plugins is only supported for some operating system. Under some systems (currently, only Linux AMD 64), all the OCaml code linked in a plugin must have been compiled without the -nodynlink flag. Some constraints might also apply to the way the extra native objects have been compiled (under Linux AMD 64, they must contain only position-independent code). -safe-string Enforce the separation between types string and bytes, thereby making strings read-only. This will become the default in a future version of OCaml. -short-paths When a type is visible under several module-paths, use the shortest one when printing the type's name in inferred interfaces and error and warning messages. Identifier names starting with an underscore _ or containing double underscores __ incur a penalty of +10 when computing their length. -strict-sequence Force the left-hand part of each sequence to have type unit. -strict-formats Reject invalid formats that were accepted in legacy format implementations. You should use this flag to detect and fix such invalid formats, as they will be rejected by future OCaml versions. -thread Compile or link multithreaded programs, in combination with the system threads library described in chapter 30. -unboxed-types When a type is unboxable (i.e. a record with a single argument or a concrete datatype with a single constructor of one argument) it will be unboxed unless annotated with [@@ocaml.boxed]. -no-unboxed-types When a type is unboxable it will be boxed unless annotated with [@@ocaml.unboxed]. This is the default. -unsafe Turn bound checking off for array and string accesses (the v.(i) and s.[i] constructs). Programs compiled with -unsafe are therefore faster, but unsafe: anything can happen if the program accesses an array or string outside of its bounds. Additionally, turn off the check for zero divisor in integer division and modulus operations. With -unsafe, an integer division (or modulus) by zero can halt the program or continue with an unspecified result instead of raising a Division_by_zero exception. -unsafe-string Identify the types string and bytes, thereby making strings writable. For reasons of backward compatibility, this is the default setting for the moment, but this will change in a future version of OCaml. -v Print the version number of the compiler and the location of the standard library directory, then exit. -verbose Print all external commands before they are executed, in particular invocations of the assembler, C compiler, and linker. Useful to debug C library problems. -version or -vnum Print the version number of the compiler in short form (e.g. 3.11.0), then exit. -w warning-list Enable, disable, or mark as fatal the warnings specified by the argument warning-list. Each warning can be enabled or disabled, and each warning can be fatal or non-fatal. If a warning is disabled, it isn't displayed and doesn't affect compilation in any way (even if it is fatal). If a warning is enabled, it is displayed normally by the compiler whenever the source code triggers it. If it is enabled and fatal, the compiler will also stop with an error after displaying it. The warning-list argument is a sequence of warning specifiers, with no separators between them. A warning specifier is one of the following: +num Enable warning number num. -num Disable warning number num. @num Enable and mark as fatal warning number num. +num1..num2 Enable warnings in the given range. -num1..num2 Disable warnings in the given range. @num1..num2 Enable and mark as fatal warnings in the given range. +letter Enable the set of warnings corresponding to letter. The letter may be uppercase or lowercase. -letter Disable the set of warnings corresponding to letter. The letter may be uppercase or lowercase. @letter Enable and mark as fatal the set of warnings corresponding to letter. The letter may be uppercase or lowercase. uppercase-letter Enable the set of warnings corresponding to uppercase-letter. lowercase-letter Disable the set of warnings corresponding to lowercase-letter. Warning numbers and letters which are out of the range of warnings that are currently defined are ignored. The warnings are as follows. 1 Suspicious-looking start-of-comment mark. 2 Suspicious-looking end-of-comment mark. 3 Deprecated feature. 4 Fragile pattern matching: matching that will remain complete even if additional constructors are added to one of the variant types matched. 5 Partially applied function: expression whose result has function type and is ignored. 6 Label omitted in function application. 7 Method overridden. 8 Partial match: missing cases in pattern-matching. 9 Missing fields in a record pattern. 10 Expression on the left-hand side of a sequence that doesn't have type unit (and that is not a function, see warning number 5). 11 Redundant case in a pattern matching (unused match case). 12 Redundant sub-pattern in a pattern-matching. 13 Instance variable overridden. 14 Illegal backslash escape in a string constant. 15 Private method made public implicitly. 16 Unerasable optional argument. 17 Undeclared virtual method. 18 Non-principal type. 19 Type without principality. 20 Unused function argument. 21 Non-returning statement. 22 Preprocessor warning. 23 Useless record with clause. 24 Bad module name: the source file name is not a valid OCaml module name. 26 Suspicious unused variable: unused variable that is bound with let or as, and doesn't start with an underscore (_) character. 27 Innocuous unused variable: unused variable that is not bound with let nor as, and doesn't start with an underscore (_) character. 28 Wildcard pattern given as argument to a constant constructor. 29 Unescaped end-of-line in a string constant (non-portable code). 30 Two labels or constructors of the same name are defined in two mutually recursive types. 31 A module is linked twice in the same executable. 32 Unused value declaration. 33 Unused open statement. 34 Unused type declaration. 35 Unused for-loop index. 36 Unused ancestor variable. 37 Unused constructor. 38 Unused extension constructor. 39 Unused rec flag. 40 Constructor or label name used out of scope. 41 Ambiguous constructor or label name. 42 Disambiguated constructor or label name (compatibility warning). 43 Nonoptional label applied as optional. 44 Open statement shadows an already defined identifier. 45 Open statement shadows an already defined label or constructor. 46 Error in environment variable. 47 Illegal attribute payload. 48 Implicit elimination of optional arguments. 49 Absent cmi file when looking up module alias. 50 Unexpected documentation comment. 51 Warning on non-tail calls if @tailcall present. 52 (see 8.5.1) Fragile constant pattern. 53 Attribute cannot appear in this context 54 Attribute used more than once on an expression 55 Inlining impossible 56 Unreachable case in a pattern-matching (based on type information). 57 (see 8.5.2) Ambiguous or-pattern variables under guard 58 Missing cmx file 59 Assignment to non-mutable value 60 Unused module declaration A all warnings C warnings 1, 2. D Alias for warning 3. E Alias for warning 4. F Alias for warning 5. K warnings 32, 33, 34, 35, 36, 37, 38, 39. L Alias for warning 6. M Alias for warning 7. P Alias for warning 8. R Alias for warning 9. S Alias for warning 10. U warnings 11, 12. V Alias for warning 13. X warnings 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 30. Y Alias for warning 26. Z Alias for warning 27. The default setting is -w +a-4-6-7-9-27-29-32..39-41..42-44-45-48-50. It is displayed by ocamlopt -help. Note that warnings 5 and 10 are not always triggered, depending on the internals of the type checker. -warn-error warning-list Mark as fatal the warnings specified in the argument warning-list. The compiler will stop with an error when one of these warnings is emitted. The warning-list has the same meaning as for the -w option: a + sign (or an uppercase letter) marks the corresponding warnings as fatal, a - sign (or a lowercase letter) turns them back into non-fatal warnings, and a @ sign both enables and marks as fatal the corresponding warnings. Note: it is not recommended to use warning sets (i.e. letters) as arguments to -warn-error in production code, because this can break your build when future versions of OCaml add some new warnings. The default setting is -warn-error -a+31 (only warning 31 is fatal). -warn-help Show the description of all available warning numbers. -where Print the location of the standard library, then exit. - file Process file as a file name, even if it starts with a dash (-) character. -help or --help Display a short usage summary and exit. Options for the IA32 architecture The IA32 code generator (Intel Pentium, AMD Athlon) supports the following additional option: -ffast-math Use the IA32 instructions to compute trigonometric and exponential functions, instead of calling the corresponding library routines. The functions affected are: atan, atan2, cos, log, log10, sin, sqrt and tan. The resulting code runs faster, but the range of supported arguments and the precision of the result can be reduced. In particular, trigonometric operations cos, sin, tan have their range reduced to [-2^64, 2^64]. Options for the AMD64 architecture The AMD64 code generator (64-bit versions of Intel Pentium and AMD Athlon) supports the following additional options: -fPIC Generate position-independent machine code. This is the default. -fno-PIC Generate position-dependent machine code. Options for the Sparc architecture The Sparc code generator supports the following additional options: -march=v8 Generate SPARC version 8 code. -march=v9 Generate SPARC version 9 code. The default is to generate code for SPARC version 7, which runs on all SPARC processors. Contextual control of command-line options The compiler command line can be modified "from the outside" with the following mechanisms. These are experimental and subject to change. They should be used only for experimental and development work, not in released packages. OCAMLPARAM (environment variable) Arguments that will be inserted before or after the arguments from the command line. ocaml_compiler_internal_params (file in the stdlib directory) A mapping of file names to lists of arguments that will be added to the command line (and OCAMLPARAM) arguments. OCAML_FLEXLINK (environment variable) Alternative executable to use on native Windows for flexlink instead of the configured value. Primarily used for bootstrapping. 11.3 Common errors *=*=*=*=*=*=*=*=*=* The error messages are almost identical to those of ocamlc. See section 8.4. 11.4 Running executables produced by ocamlopt *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= Executables generated by ocamlopt are native, stand-alone executable files that can be invoked directly. They do not depend on the ocamlrun bytecode runtime system nor on dynamically-loaded C/OCaml stub libraries. During execution of an ocamlopt-generated executable, the following environment variables are also consulted: OCAMLRUNPARAM Same usage as in ocamlrun (see section 10.2), except that option l is ignored (the operating system's stack size limit is used instead). CAMLRUNPARAM If OCAMLRUNPARAM is not found in the environment, then CAMLRUNPARAM will be used instead. If CAMLRUNPARAM is not found, then the default values will be used. 11.5 Compatibility with the bytecode compiler *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= This section lists the known incompatibilities between the bytecode compiler and the native-code compiler. Except on those points, the two compilers should generate code that behave identically. - Signals are detected only when the program performs an allocation in the heap. That is, if a signal is delivered while in a piece of code that does not allocate, its handler will not be called until the next heap allocation. - Stack overflow, typically caused by excessively deep recursion, is handled in one of the following ways, depending on the platform used: - By raising a Stack_overflow exception, like the bytecode compiler does. (IA32/Linux, AMD64/Linux, PowerPC/MacOSX, MS Windows 32-bit ports). - By aborting the program on a "segmentation fault" signal. (All other Unix systems.) - By terminating the program silently. (MS Windows 64 bits). - On IA32 processors only (Intel and AMD x86 processors in 32-bit mode), some intermediate results in floating-point computations are kept in extended precision rather than being rounded to double precision like the bytecode compiler always does. Floating-point results can therefore differ slightly between bytecode and native code. Chapter 12 Lexer and parser generators (ocamllex, ocamlyacc) *************************************************************** This chapter describes two program generators: ocamllex, that produces a lexical analyzer from a set of regular expressions with associated semantic actions, and ocamlyacc, that produces a parser from a grammar with associated semantic actions. These program generators are very close to the well-known lex and yacc commands that can be found in most C programming environments. This chapter assumes a working knowledge of lex and yacc: while it describes the input syntax for ocamllex and ocamlyacc and the main differences with lex and yacc, it does not explain the basics of writing a lexer or parser description in lex and yacc. Readers unfamiliar with lex and yacc are referred to "Compilers: principles, techniques, and tools" by Aho, Sethi and Ullman (Addison-Wesley, 1986), or "Lex & Yacc", by Levine, Mason and Brown (O'Reilly, 1992). 12.1 Overview of ocamllex *=*=*=*=*=*=*=*=*=*=*=*=*= The ocamllex command produces a lexical analyzer from a set of regular expressions with attached semantic actions, in the style of lex. Assuming the input file is lexer.mll, executing << ocamllex lexer.mll >> produces OCaml code for a lexical analyzer in file lexer.ml. This file defines one lexing function per entry point in the lexer definition. These functions have the same names as the entry points. Lexing functions take as argument a lexer buffer, and return the semantic attribute of the corresponding entry point. Lexer buffers are an abstract data type implemented in the standard library module Lexing. The functions Lexing.from_channel, Lexing.from_string and Lexing.from_function create lexer buffers that read from an input channel, a character string, or any reading function, respectively. (See the description of module Lexing in chapter 24.) When used in conjunction with a parser generated by ocamlyacc, the semantic actions compute a value belonging to the type token defined by the generated parsing module. (See the description of ocamlyacc below.) 12.1.1 Options =============== The following command-line options are recognized by ocamllex. -ml Output code that does not use OCaml's built-in automata interpreter. Instead, the automaton is encoded by OCaml functions. This option mainly is useful for debugging ocamllex, using it for production lexers is not recommended. -o output-file Specify the name of the output file produced by ocamllex. The default is the input file name with its extension replaced by .ml. -q Quiet mode. ocamllex normally outputs informational messages to standard output. They are suppressed if option -q is used. -v or -version Print version string and exit. -vnum Print short version number and exit. -help or --help Display a short usage summary and exit. 12.2 Syntax of lexer definitions *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* The format of lexer definitions is as follows: << { header } let ident = regexp ... [refill { refill-handler }] rule entrypoint [arg_1... arg_n] = parse regexp { action } | ... | regexp { action } and entrypoint [arg_1... arg_n] = parse ... and ... { trailer } >> Comments are delimited by (* and *), as in OCaml. The parse keyword, can be replaced by the shortest keyword, with the semantic consequences explained below. Refill handlers are a recent (optional) feature introduced in 4.02, documented below in subsection 12.2.7. 12.2.1 Header and trailer ========================== The header and trailer sections are arbitrary OCaml text enclosed in curly braces. Either or both can be omitted. If present, the header text is copied as is at the beginning of the output file and the trailer text at the end. Typically, the header section contains the open directives required by the actions, and possibly some auxiliary functions used in the actions. 12.2.2 Naming regular expressions ================================== Between the header and the entry points, one can give names to frequently-occurring regular expressions. This is written let ident = regexp. In regular expressions that follow this declaration, the identifier ident can be used as shorthand for regexp. 12.2.3 Entry points ==================== The names of the entry points must be valid identifiers for OCaml values (starting with a lowercase letter). Similarily, the arguments arg_1... arg_n must be valid identifiers for OCaml. Each entry point becomes an OCaml function that takes n+1 arguments, the extra implicit last argument being of type Lexing.lexbuf. Characters are read from the Lexing.lexbuf argument and matched against the regular expressions provided in the rule, until a prefix of the input matches one of the rule. The corresponding action is then evaluated and returned as the result of the function. If several regular expressions match a prefix of the input, the "longest match" rule applies: the regular expression that matches the longest prefix of the input is selected. In case of tie, the regular expression that occurs earlier in the rule is selected. However, if lexer rules are introduced with the shortest keyword in place of the parse keyword, then the "shortest match" rule applies: the shortest prefix of the input is selected. In case of tie, the regular expression that occurs earlier in the rule is still selected. This feature is not intended for use in ordinary lexical analyzers, it may facilitate the use of ocamllex as a simple text processing tool. 12.2.4 Regular expressions =========================== The regular expressions are in the style of lex, with a more OCaml-like syntax. regexp ::= ... ' regular-char | escape-sequence ' A character constant, with the same syntax as OCaml character constants. Match the denoted character. _ (underscore) Match any character. eof Match the end of the lexer input. Note: On some systems, with interactive input, an end-of-file may be followed by more characters. However, ocamllex will not correctly handle regular expressions that contain eof followed by something else. " { string-character } " A string constant, with the same syntax as OCaml string constants. Match the corresponding sequence of characters. [ character-set ] Match any single character belonging to the given character set. Valid character sets are: single character constants ' c '; ranges of characters ' c_1 ' - ' c_2 ' (all characters between c_1 and c_2, inclusive); and the union of two or more character sets, denoted by concatenation. [ ^ character-set ] Match any single character not belonging to the given character set. regexp_1 # regexp_2 (difference of character sets) Regular expressions regexp_1 and regexp_2 must be character sets defined with [... ] (or a a single character expression or underscore _). Match the difference of the two specified character sets. regexp * (repetition) Match the concatenation of zero or more strings that match regexp. regexp + (strict repetition) Match the concatenation of one or more strings that match regexp. regexp ? (option) Match the empty string, or a string matching regexp. regexp_1 | regexp_2 (alternative) Match any string that matches regexp_1 or regexp_2 regexp_1 regexp_2 (concatenation) Match the concatenation of two strings, the first matching regexp_1, the second matching regexp_2. ( regexp ) Match the same strings as regexp. ident Reference the regular expression bound to ident by an earlier let ident = regexp definition. regexp as ident Bind the substring matched by regexp to identifier ident. Concerning the precedences of operators, # has the highest precedence, followed by *, + and ?, then concatenation, then | (alternation), then as. 12.2.5 Actions =============== The actions are arbitrary OCaml expressions. They are evaluated in a context where the identifiers defined by using the as construct are bound to subparts of the matched string. Additionally, lexbuf is bound to the current lexer buffer. Some typical uses for lexbuf, in conjunction with the operations on lexer buffers provided by the Lexing standard library module, are listed below. Lexing.lexeme lexbuf Return the matched string. Lexing.lexeme_char lexbuf n Return the n^th character in the matched string. The first character corresponds to n = 0. Lexing.lexeme_start lexbuf Return the absolute position in the input text of the beginning of the matched string (i.e. the offset of the first character of the matched string). The first character read from the input text has offset 0. Lexing.lexeme_end lexbuf Return the absolute position in the input text of the end of the matched string (i.e. the offset of the first character after the matched string). The first character read from the input text has offset 0. entrypoint [exp_1... exp_n] lexbuf (Where entrypoint is the name of another entry point in the same lexer definition.) Recursively call the lexer on the given entry point. Notice that lexbuf is the last argument. Useful for lexing nested comments, for example. 12.2.6 Variables in regular expressions ======================================== The as construct is similar to "groups" as provided by numerous regular expression packages. The type of these variables can be string, char, string option or char option. We first consider the case of linear patterns, that is the case when all as bound variables are distinct. In regexp as ident, the type of ident normally is string (or string option) except when regexp is a character constant, an underscore, a string constant of length one, a character set specification, or an alternation of those. Then, the type of ident is char (or char option). Option types are introduced when overall rule matching does not imply matching of the bound sub-pattern. This is in particular the case of ( regexp as ident ) ? and of regexp_1 | ( regexp_2 as ident ). There is no linearity restriction over as bound variables. When a variable is bound more than once, the previous rules are to be extended as follows: - A variable is a char variable when all its occurrences bind char occurrences in the previous sense. - A variable is an option variable when the overall expression can be matched without binding this variable. For instance, in ('a' as x) | ( 'a' (_ as x) ) the variable x is of type char, whereas in ("ab" as x) | ( 'a' (_ as x) ? ) the variable x is of type string option. In some cases, a successful match may not yield a unique set of bindings. For instance the matching of aba by the regular expression (('a'|"ab") as x) (("ba"|'a') as y) may result in binding either x to "ab" and y to "a", or x to "a" and y to "ba". The automata produced ocamllex on such ambiguous regular expressions will select one of the possible resulting sets of bindings. The selected set of bindings is purposely left unspecified. 12.2.7 Refill handlers ======================= By default, when ocamllex reaches the end of its lexing buffer, it will silently call the refill_buff function of lexbuf structure and continue lexing. It is sometimes useful to be able to take control of refilling action; typically, if you use a library for asynchronous computation, you may want to wrap the refilling action in a delaying function to avoid blocking synchronous operations. Since OCaml 4.02, it is possible to specify a refill-handler, a function that will be called when refill happens. It is passed the continuation of the lexing, on which it has total control. The OCaml expression used as refill action should have a type that is an instance of << (Lexing.lexbuf -> 'a) -> Lexing.lexbuf -> 'a >> where the first argument is the continuation which captures the processing ocamllex would usually perform (refilling the buffer, then calling the lexing function again), and the result type that instantiates ['a] should unify with the result type of all lexing rules. As an example, consider the following lexer that is parametrized over an arbitrary monad: <<{ type token = EOL | INT of int | PLUS module Make (M : sig type 'a t val return: 'a -> 'a t val bind: 'a t -> ('a -> 'b t) -> 'b t val fail : string -> 'a t (* Set up lexbuf *) val on_refill : Lexing.lexbuf -> unit t end) = struct let refill_handler k lexbuf = M.bind (M.on_refill lexbuf) (fun () -> k lexbuf) } refill {refill_handler} rule token = parse | [' ' '\t'] { token lexbuf } | '\n' { M.return EOL } | ['0'-'9']+ as i { M.return (INT (int_of_string i)) } | '+' { M.return PLUS } | _ { M.fail "unexpected character" } { end } >> 12.2.8 Reserved identifiers ============================ All identifiers starting with __ocaml_lex are reserved for use by ocamllex; do not use any such identifier in your programs. 12.3 Overview of ocamlyacc *=*=*=*=*=*=*=*=*=*=*=*=*=* The ocamlyacc command produces a parser from a context-free grammar specification with attached semantic actions, in the style of yacc. Assuming the input file is grammar.mly, executing << ocamlyacc options grammar.mly >> produces OCaml code for a parser in the file grammar.ml, and its interface in file grammar.mli. The generated module defines one parsing function per entry point in the grammar. These functions have the same names as the entry points. Parsing functions take as arguments a lexical analyzer (a function from lexer buffers to tokens) and a lexer buffer, and return the semantic attribute of the corresponding entry point. Lexical analyzer functions are usually generated from a lexer specification by the ocamllex program. Lexer buffers are an abstract data type implemented in the standard library module Lexing. Tokens are values from the concrete type token, defined in the interface file grammar.mli produced by ocamlyacc. 12.4 Syntax of grammar definitions *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* Grammar definitions have the following format: << %{ header %} declarations %% rules %% trailer >> Comments are enclosed between /* and */ (as in C) in the "declarations" and "rules" sections, and between (* and *) (as in OCaml) in the "header" and "trailer" sections. 12.4.1 Header and trailer ========================== The header and the trailer sections are OCaml code that is copied as is into file grammar.ml. Both sections are optional. The header goes at the beginning of the output file; it usually contains open directives and auxiliary functions required by the semantic actions of the rules. The trailer goes at the end of the output file. 12.4.2 Declarations ==================== Declarations are given one per line. They all start with a % sign. %token constr ... constr Declare the given symbols constr ... constr as tokens (terminal symbols). These symbols are added as constant constructors for the token concrete type. %token < typexpr > constr ... constr Declare the given symbols constr ... constr as tokens with an attached attribute of the given type. These symbols are added as constructors with arguments of the given type for the token concrete type. The typexpr part is an arbitrary OCaml type expression, except that all type constructor names must be fully qualified (e.g. Modname.typename) for all types except standard built-in types, even if the proper open directives (e.g. open Modname) were given in the header section. That's because the header is copied only to the .ml output file, but not to the .mli output file, while the typexpr part of a %token declaration is copied to both. %start symbol ... symbol Declare the given symbols as entry points for the grammar. For each entry point, a parsing function with the same name is defined in the output module. Non-terminals that are not declared as entry points have no such parsing function. Start symbols must be given a type with the %type directive below. %type < typexpr > symbol ... symbol Specify the type of the semantic attributes for the given symbols. This is mandatory for start symbols only. Other nonterminal symbols need not be given types by hand: these types will be inferred when running the output files through the OCaml compiler (unless the -s option is in effect). The typexpr part is an arbitrary OCaml type expression, except that all type constructor names must be fully qualified, as explained above for %token. %left symbol ... symbol %right symbol ... symbol %nonassoc symbol ... symbol Associate precedences and associativities to the given symbols. All symbols on the same line are given the same precedence. They have higher precedence than symbols declared before in a %left, %right or %nonassoc line. They have lower precedence than symbols declared after in a %left, %right or %nonassoc line. The symbols are declared to associate to the left (%left), to the right (%right), or to be non-associative (%nonassoc). The symbols are usually tokens. They can also be dummy nonterminals, for use with the %prec directive inside the rules. The precedence declarations are used in the following way to resolve reduce/reduce and shift/reduce conflicts: - Tokens and rules have precedences. By default, the precedence of a rule is the precedence of its rightmost terminal. You can override this default by using the %prec directive in the rule. - A reduce/reduce conflict is resolved in favor of the first rule (in the order given by the source file), and ocamlyacc outputs a warning. - A shift/reduce conflict is resolved by comparing the precedence of the rule to be reduced with the precedence of the token to be shifted. If the precedence of the rule is higher, then the rule will be reduced; if the precedence of the token is higher, then the token will be shifted. - A shift/reduce conflict between a rule and a token with the same precedence will be resolved using the associativity: if the token is left-associative, then the parser will reduce; if the token is right-associative, then the parser will shift. If the token is non-associative, then the parser will declare a syntax error. - When a shift/reduce conflict cannot be resolved using the above method, then ocamlyacc will output a warning and the parser will always shift. 12.4.3 Rules ============= The syntax for rules is as usual: << nonterminal : symbol ... symbol { semantic-action } | ... | symbol ... symbol { semantic-action } ; >> Rules can also contain the %prec symbol directive in the right-hand side part, to override the default precedence and associativity of the rule with the precedence and associativity of the given symbol. Semantic actions are arbitrary OCaml expressions, that are evaluated to produce the semantic attribute attached to the defined nonterminal. The semantic actions can access the semantic attributes of the symbols in the right-hand side of the rule with the $ notation: $1 is the attribute for the first (leftmost) symbol, $2 is the attribute for the second symbol, etc. The rules may contain the special symbol error to indicate resynchronization points, as in yacc. Actions occurring in the middle of rules are not supported. Nonterminal symbols are like regular OCaml symbols, except that they cannot end with ' (single quote). 12.4.4 Error handling ====================== Error recovery is supported as follows: when the parser reaches an error state (no grammar rules can apply), it calls a function named parse_error with the string "syntax error" as argument. The default parse_error function does nothing and returns, thus initiating error recovery (see below). The user can define a customized parse_error function in the header section of the grammar file. The parser also enters error recovery mode if one of the grammar actions raises the Parsing.Parse_error exception. In error recovery mode, the parser discards states from the stack until it reaches a place where the error token can be shifted. It then discards tokens from the input until it finds three successive tokens that can be accepted, and starts processing with the first of these. If no state can be uncovered where the error token can be shifted, then the parser aborts by raising the Parsing.Parse_error exception. Refer to documentation on yacc for more details and guidance in how to use error recovery. 12.5 Options *=*=*=*=*=*=* The ocamlyacc command recognizes the following options: -bprefix Name the output files prefix.ml, prefix.mli, prefix.output, instead of the default naming convention. -q This option has no effect. -v Generate a description of the parsing tables and a report on conflicts resulting from ambiguities in the grammar. The description is put in file grammar.output. -version Print version string and exit. -vnum Print short version number and exit. - Read the grammar specification from standard input. The default output file names are stdin.ml and stdin.mli. -- file Process file as the grammar specification, even if its name starts with a dash (-) character. This option must be the last on the command line. At run-time, the ocamlyacc-generated parser can be debugged by setting the p option in the OCAMLRUNPARAM environment variable (see section 10.2). This causes the pushdown automaton executing the parser to print a trace of its action (tokens shifted, rules reduced, etc). The trace mentions rule numbers and state numbers that can be interpreted by looking at the file grammar.output generated by ocamlyacc -v. 12.6 A complete example *=*=*=*=*=*=*=*=*=*=*=*= The all-time favorite: a desk calculator. This program reads arithmetic expressions on standard input, one per line, and prints their values. Here is the grammar definition: << /* File parser.mly */ %token INT %token PLUS MINUS TIMES DIV %token LPAREN RPAREN %token EOL %left PLUS MINUS /* lowest precedence */ %left TIMES DIV /* medium precedence */ %nonassoc UMINUS /* highest precedence */ %start main /* the entry point */ %type main %% main: expr EOL { $1 } ; expr: INT { $1 } | LPAREN expr RPAREN { $2 } | expr PLUS expr { $1 + $3 } | expr MINUS expr { $1 - $3 } | expr TIMES expr { $1 * $3 } | expr DIV expr { $1 / $3 } | MINUS expr %prec UMINUS { - $2 } ; >> Here is the definition for the corresponding lexer: << (* File lexer.mll *) { open Parser (* The type token is defined in parser.mli *) exception Eof } rule token = parse [' ' '\t'] { token lexbuf } (* skip blanks *) | ['\n' ] { EOL } | ['0'-'9']+ as lxm { INT(int_of_string lxm) } | '+' { PLUS } | '-' { MINUS } | '*' { TIMES } | '/' { DIV } | '(' { LPAREN } | ')' { RPAREN } | eof { raise Eof } >> Here is the main program, that combines the parser with the lexer: << (* File calc.ml *) let _ = try let lexbuf = Lexing.from_channel stdin in while true do let result = Parser.main Lexer.token lexbuf in print_int result; print_newline(); flush stdout done with Lexer.Eof -> exit 0 >> To compile everything, execute: << ocamllex lexer.mll # generates lexer.ml ocamlyacc parser.mly # generates parser.ml and parser.mli ocamlc -c parser.mli ocamlc -c lexer.ml ocamlc -c parser.ml ocamlc -c calc.ml ocamlc -o calc lexer.cmo parser.cmo calc.cmo >> 12.7 Common errors *=*=*=*=*=*=*=*=*=* ocamllex: transition table overflow, automaton is too big The deterministic automata generated by ocamllex are limited to at most 32767 transitions. The message above indicates that your lexer definition is too complex and overflows this limit. This is commonly caused by lexer definitions that have separate rules for each of the alphabetic keywords of the language, as in the following example. <> To keep the generated automata small, rewrite those definitions with only one general "identifier" rule, followed by a hashtable lookup to separate keywords from identifiers: <<{ let keyword_table = Hashtbl.create 53 let _ = List.iter (fun (kwd, tok) -> Hashtbl.add keyword_table kwd tok) [ "keyword1", KWD1; "keyword2", KWD2; ... "keyword100", KWD100 ] } rule token = parse ['A'-'Z' 'a'-'z'] ['A'-'Z' 'a'-'z' '0'-'9' '_'] * as id { try Hashtbl.find keyword_table id with Not_found -> IDENT id } >> ocamllex: Position memory overflow, too many bindings The deterministic automata generated by ocamllex maintain a table of positions inside the scanned lexer buffer. The size of this table is limited to at most 255 cells. This error should not show up in normal situations. Chapter 13 Dependency generator (ocamldep) ********************************************* The ocamldep command scans a set of OCaml source files (.ml and .mli files) for references to external compilation units, and outputs dependency lines in a format suitable for the make utility. This ensures that make will compile the source files in the correct order, and recompile those files that need to when a source file is modified. The typical usage is: << ocamldep options *.mli *.ml > .depend >> where *.mli *.ml expands to all source files in the current directory and .depend is the file that should contain the dependencies. (See below for a typical Makefile.) Dependencies are generated both for compiling with the bytecode compiler ocamlc and with the native-code compiler ocamlopt. 13.1 Options *=*=*=*=*=*=* The following command-line options are recognized by ocamldep. -absname Show absolute filenames in error messages. -all Generate dependencies on all required files, rather than assuming implicit dependencies. -allow-approx Allow falling back on a lexer-based approximation when parsing fails. -args filename Read additional newline-terminated command line arguments from filename. -args0 filename Read additional null character terminated command line arguments from filename. -as-map For the following files, do not include delayed dependencies for module aliases. This option assumes that they are compiled using options -no-alias-deps -w -49, and that those files or their interface are passed with the -map option when computing dependencies for other files. Note also that for dependencies to be correct in the implementation of a map file, its interface should not coerce any of the aliases it contains. -debug-map Dump the delayed dependency map for each map file. -I directory Add the given directory to the list of directories searched for source files. If a source file foo.ml mentions an external compilation unit Bar, a dependency on that unit's interface bar.cmi is generated only if the source for bar is found in the current directory or in one of the directories specified with -I. Otherwise, Bar is assumed to be a module from the standard library, and no dependencies are generated. For programs that span multiple directories, it is recommended to pass ocamldep the same -I options that are passed to the compiler. -impl file Process file as a .ml file. -intf file Process file as a .mli file. -map file Read an propagate the delayed dependencies for module aliases in file, so that the following files will depend on the exported aliased modules if they use them. See the example below. -ml-synonym .ext Consider the given extension (with leading dot) to be a synonym for .ml. -mli-synonym .ext Consider the given extension (with leading dot) to be a synonym for .mli. -modules Output raw dependencies of the form << filename: Module1 Module2 ... ModuleN >> where Module1, ..., ModuleN are the names of the compilation units referenced within the file filename, but these names are not resolved to source file names. Such raw dependencies cannot be used by make, but can be post-processed by other tools such as Omake. -native Generate dependencies for a pure native-code program (no bytecode version). When an implementation file (.ml file) has no explicit interface file (.mli file), ocamldep generates dependencies on the bytecode compiled file (.cmo file) to reflect interface changes. This can cause unnecessary bytecode recompilations for programs that are compiled to native-code only. The flag -native causes dependencies on native compiled files (.cmx) to be generated instead of on .cmo files. (This flag makes no difference if all source files have explicit .mli interface files.) -one-line Output one line per file, regardless of the length. -open module Assume that module module is opened before parsing each of the following files. -plugin plugin Dynamically load the code of the given plugin (a .cmo, .cma or .cmxs file) in ocamldep. plugin must exist in the same kind of code as ocamldep (ocamldep.byte must load bytecode plugins, while ocamldep.opt must load native code plugins), and extension adaptation is done automatically for .cma files (to .cmxs files if ocamldep is compiled in native code). -pp command Cause ocamldep to call the given command as a preprocessor for each source file. -ppx command Pipe abstract syntax trees through preprocessor command. -slash Under Windows, use a forward slash (/) as the path separator instead of the usual backward slash (\). Under Unix, this option does nothing. -sort Sort files according to their dependencies. -version Print version string and exit. -vnum Print short version number and exit. -help or --help Display a short usage summary and exit. 13.2 A typical Makefile *=*=*=*=*=*=*=*=*=*=*=*= Here is a template Makefile for a OCaml program. < .depend include .depend >> If you use module aliases to give shorter names to modules, you need to change the above definitions. Assuming that your map file is called mylib.mli, here are minimal modifications. < .depend >> Note that in this case you should not compute dependencies for mylib.mli together with the other files, hence the need to pass explicitly the list of files to process. If mylib.mli itself has dependencies, you should compute them using -as-map. Chapter 14 The browser/editor (ocamlbrowser) *********************************************** Since OCaml version 4.02, the OCamlBrowser tool and the Labltk library are distributed separately from the OCaml compiler. The project is now hosted at https://forge.ocamlcore.org/projects/labltk/. Chapter 15 The documentation generator (ocamldoc) **************************************************** This chapter describes OCamldoc, a tool that generates documentation from special comments embedded in source files. The comments used by OCamldoc are of the form (**...*) and follow the format described in section 15.2. OCamldoc can produce documentation in various formats: HTML, LaTeX, TeXinfo, Unix man pages, and dot dependency graphs. Moreover, users can add their own custom generators, as explained in section 15.3. In this chapter, we use the word element to refer to any of the following parts of an OCaml source file: a type declaration, a value, a module, an exception, a module type, a type constructor, a record field, a class, a class type, a class method, a class value or a class inheritance clause. 15.1 Usage *=*=*=*=*=* 15.1.1 Invocation ================== OCamldoc is invoked via the command ocamldoc, as follows: << ocamldoc options sourcefiles >> Options for choosing the output format -------------------------------------- The following options determine the format for the generated documentation. -html Generate documentation in HTML default format. The generated HTML pages are stored in the current directory, or in the directory specified with the -d option. You can customize the style of the generated pages by editing the generated style.css file, or by providing your own style sheet using option -css-style. The file style.css is not generated if it already exists or if -css-style is used. -latex Generate documentation in LaTeX default format. The generated LaTeX document is saved in file ocamldoc.out, or in the file specified with the -o option. The document uses the style file ocamldoc.sty. This file is generated when using the -latex option, if it does not already exist. You can change this file to customize the style of your LaTeX documentation. -texi Generate documentation in TeXinfo default format. The generated LaTeX document is saved in file ocamldoc.out, or in the file specified with the -o option. -man Generate documentation as a set of Unix man pages. The generated pages are stored in the current directory, or in the directory specified with the -d option. -dot Generate a dependency graph for the toplevel modules, in a format suitable for displaying and processing by dot. The dot tool is available from http://www.research.att.com/sw/tools/graphviz/. The textual representation of the graph is written to the file ocamldoc.out, or to the file specified with the -o option. Use dot ocamldoc.out to display it. -g file.cm[o,a,xs] Dynamically load the given file, which defines a custom documentation generator. See section 15.4.1. This option is supported by the ocamldoc command (to load .cmo and .cma files) and by its native-code version ocamldoc.opt (to load .cmxs files). If the given file is a simple one and does not exist in the current directory, then ocamldoc looks for it in the custom generators default directory, and in the directories specified with optional -i options. -customdir Display the custom generators default directory. -i directory Add the given directory to the path where to look for custom generators. General options --------------- -d dir Generate files in directory dir, rather than the current directory. -dump file Dump collected information into file. This information can be read with the -load option in a subsequent invocation of ocamldoc. -hide modules Hide the given complete module names in the generated documentation. modules is a list of complete module names separated by ',', without blanks. For instance: Pervasives,M2.M3. -inv-merge-ml-mli Reverse the precedence of implementations and interfaces when merging. All elements in implementation files are kept, and the -m option indicates which parts of the comments in interface files are merged with the comments in implementation files. -keep-code Always keep the source code for values, methods and instance variables, when available. The source code is always kept when a .ml file is given, but is by default discarded when a .mli is given. This option keeps the source code in all cases. -load file Load information from file, which has been produced by ocamldoc -dump. Several -load options can be given. -m flags Specify merge options between interfaces and implementations. (see section 15.1.2 for details). flags can be one or several of the following characters: d merge description a merge @author v merge @version l merge @see s merge @since b merge @before o merge @deprecated p merge @param e merge @raise r merge @return A merge everything -no-custom-tags Do not allow custom @-tags (see section 15.2.5). -no-stop Keep elements placed after/between the (**/**) special comment(s) (see section 15.2). -o file Output the generated documentation to file instead of ocamldoc.out. This option is meaningful only in conjunction with the -latex, -texi, or -dot options. -pp command Pipe sources through preprocessor command. -impl filename Process the file filename as an implementation file, even if its extension is not .ml. -intf filename Process the file filename as an interface file, even if its extension is not .mli. -text filename Process the file filename as a text file, even if its extension is not .txt. -sort Sort the list of top-level modules before generating the documentation. -stars Remove blank characters until the first asterisk ('*') in each line of comments. -t title Use title as the title for the generated documentation. -intro file Use content of file as ocamldoc text to use as introduction (HTML, LaTeX and TeXinfo only). For HTML, the file is used to create the whole index.html file. -v Verbose mode. Display progress information. -version Print version string and exit. -vnum Print short version number and exit. -warn-error Treat Ocamldoc warnings as errors. -hide-warnings Do not print OCamldoc warnings. -help or --help Display a short usage summary and exit. Type-checking options --------------------- OCamldoc calls the OCaml type-checker to obtain type information. The following options impact the type-checking phase. They have the same meaning as for the ocamlc and ocamlopt commands. -I directory Add directory to the list of directories search for compiled interface files (.cmi files). -nolabels Ignore non-optional labels in types. -rectypes Allow arbitrary recursive types. (See the -rectypes option to ocamlc.) Options for generating HTML pages --------------------------------- The following options apply in conjunction with the -html option: -all-params Display the complete list of parameters for functions and methods. -charset charset Add information about character encoding being charset (default is iso-8859-1). -colorize-code Colorize the OCaml code enclosed in [ ] and {[ ]}, using colors to emphasize keywords, etc. If the code fragments are not syntactically correct, no color is added. -css-style filename Use filename as the Cascading Style Sheet file. -index-only Generate only index files. -short-functors Use a short form to display functors: << module M : functor (A:Module) -> functor (B:Module2) -> sig .. end >> is displayed as: << module M (A:Module) (B:Module2) : sig .. end >> Options for generating LaTeX files ---------------------------------- The following options apply in conjunction with the -latex option: -latex-value-prefix prefix Give a prefix to use for the labels of the values in the generated LaTeX document. The default prefix is the empty string. You can also use the options -latex-type-prefix, -latex-exception-prefix, -latex-module-prefix, -latex-module-type-prefix, -latex-class-prefix, -latex-class-type-prefix, -latex-attribute-prefix and -latex-method-prefix. These options are useful when you have, for example, a type and a value with the same name. If you do not specify prefixes, LaTeX will complain about multiply defined labels. -latextitle n,style Associate style number n to the given LaTeX sectioning command style, e.g. section or subsection. (LaTeX only.) This is useful when including the generated document in another LaTeX document, at a given sectioning level. The default association is 1 for section, 2 for subsection, 3 for subsubsection, 4 for paragraph and 5 for subparagraph. -noheader Suppress header in generated documentation. -notoc Do not generate a table of contents. -notrailer Suppress trailer in generated documentation. -sepfiles Generate one .tex file per toplevel module, instead of the global ocamldoc.out file. Options for generating TeXinfo files ------------------------------------ The following options apply in conjunction with the -texi option: -esc8 Escape accented characters in Info files. -info-entry Specify Info directory entry. -info-section Specify section of Info directory. -noheader Suppress header in generated documentation. -noindex Do not build index for Info files. -notrailer Suppress trailer in generated documentation. Options for generating dot graphs --------------------------------- The following options apply in conjunction with the -dot option: -dot-colors colors Specify the colors to use in the generated dot code. When generating module dependencies, ocamldoc uses different colors for modules, depending on the directories in which they reside. When generating types dependencies, ocamldoc uses different colors for types, depending on the modules in which they are defined. colors is a list of color names separated by ',', as in Red,Blue,Green. The available colors are the ones supported by the dot tool. -dot-include-all Include all modules in the dot output, not only modules given on the command line or loaded with the -load option. -dot-reduce Perform a transitive reduction of the dependency graph before outputting the dot code. This can be useful if there are a lot of transitive dependencies that clutter the graph. -dot-types Output dot code describing the type dependency graph instead of the module dependency graph. Options for generating man files -------------------------------- The following options apply in conjunction with the -man option: -man-mini Generate man pages only for modules, module types, classes and class types, instead of pages for all elements. -man-suffix suffix Set the suffix used for generated man filenames. Default is '3o', as in List.3o. -man-section section Set the section number used for generated man filenames. Default is '3'. 15.1.2 Merging of module information ===================================== Information on a module can be extracted either from the .mli or .ml file, or both, depending on the files given on the command line. When both .mli and .ml files are given for the same module, information extracted from these files is merged according to the following rules: - Only elements (values, types, classes, ...) declared in the .mli file are kept. In other terms, definitions from the .ml file that are not exported in the .mli file are not documented. - Descriptions of elements and descriptions in @-tags are handled as follows. If a description for the same element or in the same @-tag of the same element is present in both files, then the description of the .ml file is concatenated to the one in the .mli file, if the corresponding -m flag is given on the command line. If a description is present in the .ml file and not in the .mli file, the .ml description is kept. In either case, all the information given in the .mli file is kept. 15.1.3 Coding rules ==================== The following rules must be respected in order to avoid name clashes resulting in cross-reference errors: - In a module, there must not be two modules, two module types or a module and a module type with the same name. In the default HTML generator, modules ab and AB will be printed to the same file on case insensitive file systems. - In a module, there must not be two classes, two class types or a class and a class type with the same name. - In a module, there must not be two values, two types, or two exceptions with the same name. - Values defined in tuple, as in let (x,y,z) = (1,2,3) are not kept by OCamldoc. - Avoid the following construction: <> In this case, OCamldoc will associate Bar.x to the x of module Foo defined just above, instead of to the Bar.x defined in the opened module Foo. 15.2 Syntax of documentation comments *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= Comments containing documentation material are called special comments and are written between (** and *). Special comments must start exactly with (**. Comments beginning with ( and more than two * are ignored. 15.2.1 Placement of documentation comments =========================================== OCamldoc can associate comments to some elements of the language encountered in the source files. The association is made according to the locations of comments with respect to the language elements. The locations of comments in .mli and .ml files are different. Comments in .mli files ---------------------- A special comment is associated to an element if it is placed before or after the element. A special comment before an element is associated to this element if : - There is no blank line or another special comment between the special comment and the element. However, a regular comment can occur between the special comment and the element. - The special comment is not already associated to the previous element. - The special comment is not the first one of a toplevel module. A special comment after an element is associated to this element if there is no blank line or comment between the special comment and the element. There are two exceptions: for constructors and record fields in type definitions, the associated comment can only be placed after the constructor or field definition, without blank lines or other comments between them. The special comment for a constructor with another constructor following must be placed before the '|' character separating the two constructors. The following sample interface file foo.mli illustrates the placement rules for comments in .mli files. <<(** The first special comment of the file is the comment associated with the whole module.*) (** Special comments can be placed between elements and are kept by the OCamldoc tool, but are not associated to any element. @-tags in these comments are ignored.*) (*******************************************************************) (** Comments like the one above, with more than two asterisks, are ignored. *) (** The comment for function f. *) val f : int -> int -> int (** The continuation of the comment for function f. *) (** Comment for exception My_exception, even with a simple comment between the special comment and the exception.*) (* Hello, I'm a simple comment :-) *) exception My_exception of (int -> int) * int (** Comment for type weather *) type weather = | Rain of int (** The comment for constructor Rain *) | Sun (** The comment for constructor Sun *) (** Comment for type weather2 *) type weather2 = | Rain of int (** The comment for constructor Rain *) | Sun (** The comment for constructor Sun *) (** I can continue the comment for type weather2 here because there is already a comment associated to the last constructor.*) (** The comment for type my_record *) type my_record = { val foo : int ; (** Comment for field foo *) val bar : string ; (** Comment for field bar *) } (** Continuation of comment for type my_record *) (** Comment for foo *) val foo : string (** This comment is associated to foo and not to bar. *) val bar : string (** This comment is associated to bar. *) (** The comment for class my_class *) class my_class : object (** A comment to describe inheritance from cl *) inherit cl (** The comment for attribute tutu *) val mutable tutu : string (** The comment for attribute toto. *) val toto : int (** This comment is not attached to titi since there is a blank line before titi, but is kept as a comment in the class. *) val titi : string (** Comment for method toto *) method toto : string (** Comment for method m *) method m : float -> int end (** The comment for the class type my_class_type *) class type my_class_type = object (** The comment for variable x. *) val mutable x : int (** The commend for method m. *) method m : int -> int end (** The comment for module Foo *) module Foo = struct (** The comment for x *) val x : int (** A special comment that is kept but not associated to any element *) end (** The comment for module type my_module_type. *) module type my_module_type = sig (** The comment for value x. *) val x : int (** The comment for module M. *) module M = struct (** The comment for value y. *) val y : int (* ... *) end end >> Comments in .ml files --------------------- A special comment is associated to an element if it is placed before the element and there is no blank line between the comment and the element. Meanwhile, there can be a simple comment between the special comment and the element. There are two exceptions, for constructors and record fields in type definitions, whose associated comment must be placed after the constructor or field definition, without blank line between them. The special comment for a constructor with another constructor following must be placed before the '|' character separating the two constructors. The following example of file toto.ml shows where to place comments in a .ml file. <<(** The first special comment of the file is the comment associated to the whole module. *) (** The comment for function f *) let f x y = x + y (** This comment is not attached to any element since there is another special comment just before the next element. *) (** Comment for exception My_exception, even with a simple comment between the special comment and the exception.*) (* A simple comment. *) exception My_exception of (int -> int) * int (** Comment for type weather *) type weather = | Rain of int (** The comment for constructor Rain *) | Sun (** The comment for constructor Sun *) (** The comment for type my_record *) type my_record = { val foo : int ; (** Comment for field foo *) val bar : string ; (** Comment for field bar *) } (** The comment for class my_class *) class my_class = object (** A comment to describe inheritance from cl *) inherit cl (** The comment for the instance variable tutu *) val mutable tutu = "tutu" (** The comment for toto *) val toto = 1 val titi = "titi" (** Comment for method toto *) method toto = tutu ^ "!" (** Comment for method m *) method m (f : float) = 1 end (** The comment for class type my_class_type *) class type my_class_type = object (** The comment for the instance variable x. *) val mutable x : int (** The commend for method m. *) method m : int -> int end (** The comment for module Foo *) module Foo = struct (** The comment for x *) val x : int (** A special comment in the class, but not associated to any element. *) end (** The comment for module type my_module_type. *) module type my_module_type = sig (* Comment for value x. *) val x : int (* ... *) end >> 15.2.2 The Stop special comment ================================ The special comment (**/**) tells OCamldoc to discard elements placed after this comment, up to the end of the current class, class type, module or module type, or up to the next stop comment. For instance: <> The -no-stop option to ocamldoc causes the Stop special comments to be ignored. 15.2.3 Syntax of documentation comments ======================================== The inside of documentation comments (**...*) consists of free-form text with optional formatting annotations, followed by optional tags giving more specific information about parameters, version, authors, ... The tags are distinguished by a leading @ character. Thus, a documentation comment has the following shape: <<(** The comment begins with a description, which is text formatted according to the rules described in the next section. The description continues until the first non-escaped '@' character. @author Mr Smith @param x description for parameter x *) >> Some elements support only a subset of all @-tags. Tags that are not relevant to the documented element are simply ignored. For instance, all tags are ignored when documenting type constructors, record fields, and class inheritance clauses. Similarly, a @param tag on a class instance variable is ignored. At last, (**) is the empty documentation comment. 15.2.4 Text formatting ======================= Here is the BNF grammar for the simple markup language used to format text descriptions. + text ::= {text-element} text-element ::= | { { 0 ... 9 }^+ text } format text as a section header; the integer following { indicates the sectioning level. | { { 0 ... 9 }^+ : label text } same, but also associate the name label to the current point. This point can be referenced by its fully-qualified label in a {! command, just like any other element. | {b text } set text in bold. | {i text } set text in italic. | {e text } emphasize text. | {C text } center text. | {L text } left align text. | {R text } right align text. | {ul list } build a list. | {ol list } build an enumerated list. | {{: string } text } put a link to the given address (given as string) on the given text. | [ string ] set the given string in source code style. | {[ string ]} set the given string in preformatted source code style. | {v string v} set the given string in verbatim style. | {% string %} target-specific content (LaTeX code by default, see details in 15.2.4.4) | {! string } insert a cross-reference to an element (see section 15.2.4.2 for the syntax of cross-references). | {!modules: string string ... } insert an index table for the given module names. Used in HTML only. | {!indexlist} insert a table of links to the various indexes (types, values, modules, ...). Used in HTML only. | {^ text } set text in superscript. | {_ text } set text in subscript. | escaped-string typeset the given string as is; special characters ('{', '}', '[', ']' and '@') must be escaped by a '\' | blank-line force a new line. 15.2.4.1 List formatting ------------------------- list ::= + | { {- text } } + | { {li text } } A shortcut syntax exists for lists and enumerated lists: <<(** Here is a {b list} - item 1 - item 2 - item 3 The list is ended by the blank line.*) >> is equivalent to: <<(** Here is a {b list} {ul {- item 1} {- item 2} {- item 3}} The list is ended by the blank line.*) >> The same shortcut is available for enumerated lists, using '+' instead of '-'. Note that only one list can be defined by this shortcut in nested lists. 15.2.4.2 Cross-reference formatting ------------------------------------ Cross-references are fully qualified element names, as in the example {!Foo.Bar.t}. This is an ambiguous reference as it may designate a type name, a value name, a class name, etc. It is possible to make explicit the intended syntactic class, using {!type:Foo.Bar.t} to designate a type, and {!val:Foo.Bar.t} a value of the same name. The list of possible syntactic class is as follows: tag syntactic class --------------------------------- module: module modtype: module type class: class classtype: class type val: value type: type exception: exception attribute: attribute method: class method section: ocamldoc section const: variant constructor recfield: record field In the case of variant constructors or record field, the constructor or field name should be preceded by the name of the correspond type -- to avoid the ambiguity of several types having the same constructor names. For example, the constructor Node of the type tree will be referenced as {!tree.Node} or {!const:tree.Node}, or possibly {!Mod1.Mod2.tree.Node} from outside the module. 15.2.4.3 First sentence ------------------------ In the description of a value, type, exception, module, module type, class or class type, the first sentence is sometimes used in indexes, or when just a part of the description is needed. The first sentence is composed of the first characters of the description, until - the first dot followed by a blank, or - the first blank line outside of the following text formatting : {ul list } , {ol list } , [ string ] , {[ string ]} , {v string v} , {% string %} , {! string } , {^ text } , {_ text } . 15.2.4.4 Target-specific formatting ------------------------------------ The content inside {%foo: ... %} is target-specific and will only be interpreted by the backend foo, and ignored by the others. The backends of the distribution are latex, html, texi and man. If no target is specified (syntax {% ... %}), latex is chosen by default. Custom generators may support their own target prefix. 15.2.4.5 Recognized HTML tags ------------------------------ The HTML tags .., .., ..,
    ..
,
    ..
,
  • ..
  • ,
    ..
    and .. can be used instead of, respectively, {b ..} , [..] , {i ..} , {ul ..} , {ol ..} , {li ..} , {C ..} and {[0-9] ..}. 15.2.5 Documentation tags (@-tags) =================================== Predefined tags --------------- The following table gives the list of predefined @-tags, with their syntax and meaning. --------------------------------------------- | @author |The author of the element. | |string |One author per @author tag. | | |There may be several @author| | |tags for the same element. | --------------------------------------------- | @deprecated |The text should describe | |text |when the element was | | |deprecated, what to use as a| | |replacement, and possibly | | |the reason for deprecation. | --------------------------------------------- | @param id |Associate the given | | text |description (text) to the | | |given parameter name id. | | |This tag is used for | | |functions, methods, classes | | |and functors. | --------------------------------------------- | @raise Exc |Explain that the element may| | text |raise the exception Exc. | --------------------------------------------- | @return text|Describe the return value | | |and its possible values. | | |This tag is used for | | |functions and methods. | --------------------------------------------- | @see < URL >|Add a reference to the URL | | text |with the given text as | | |comment. | --------------------------------------------- | @see |Add a reference to the given| |'filename' |file name (written between | |text |single quotes), with the | | |given text as comment. | --------------------------------------------- | @see |Add a reference to the given| |"document-name|document name (written | |" text |between double quotes), with| | |the given text as comment. | --------------------------------------------- | @since |Indicate when the element | |string |was introduced. | --------------------------------------------- | @before |Associate the given | |version text |description (text) to the | | |given version in order to | | |document compatibility | | |issues. | --------------------------------------------- | @version |The version number for the | |string |element. | --------------------------------------------- Custom tags ----------- You can use custom tags in the documentation comments, but they will have no effect if the generator used does not handle them. To use a custom tag, for example foo, just put @foo with some text in your comment, as in: <<(** My comment to show you a custom tag. @foo this is the text argument to the [foo] custom tag. *) >> To handle custom tags, you need to define a custom generator, as explained in section 15.3.2. 15.3 Custom generators *=*=*=*=*=*=*=*=*=*=*=* OCamldoc operates in two steps: 1. analysis of the source files; 2. generation of documentation, through a documentation generator, which is an object of class Odoc_args.class_generator. Users can provide their own documentation generator to be used during step 2 instead of the default generators. All the information retrieved during the analysis step is available through the Odoc_info module, which gives access to all the types and functions representing the elements found in the given modules, with their associated description. The files you can use to define custom generators are installed in the ocamldoc sub-directory of the OCaml standard library. 15.3.1 The generator modules ============================= The type of a generator module depends on the kind of generated documentation. Here is the list of generator module types, with the name of the generator class in the module : - for HTML : Odoc_html.Html_generator (class html), - for LaTeX : Odoc_latex.Latex_generator (class latex), - for TeXinfo : Odoc_texi.Texi_generator (class texi), - for man pages : Odoc_man.Man_generator (class man), - for graphviz (dot) : Odoc_dot.Dot_generator (class dot), - for other kinds : Odoc_gen.Base (class generator). That is, to define a new generator, one must implement a module with the expected signature, and with the given generator class, providing the generate method as entry point to make the generator generates documentation for a given list of modules : << method generate : Odoc_info.Module.t_module list -> unit >> This method will be called with the list of analysed and possibly merged Odoc_info.t_module structures. It is recommended to inherit from the current generator of the same kind as the one you want to define. Doing so, it is possible to load various custom generators to combine improvements brought by each one. This is done using first class modules (see chapter 7.10). The easiest way to define a custom generator is the following this example, here extending the current HTML generator. We don't have to know if this is the original HTML generator defined in ocamldoc or if it has been extended already by a previously loaded custom generator : <> To know which methods to override and/or which methods are available, have a look at the different base implementations, depending on the kind of generator you are extending : - for HTML : odoc_html.ml (1), - for LaTeX : odoc_latex.ml (2), - for TeXinfo : odoc_texi.ml (3), - for man pages : odoc_man.ml (4), - for graphviz (dot) : odoc_dot.ml (5). 15.3.2 Handling custom tags ============================ Making a custom generator handle custom tags (see 15.2.5) is very simple. For HTML -------- Here is how to develop a HTML generator handling your custom tags. The class Odoc_html.Generator.html inherits from the class Odoc_html.info, containing a field tag_functions which is a list pairs composed of a custom tag (e.g. "foo") and a function taking a text and returning HTML code (of type string). To handle a new tag bar, extend the current HTML generator and complete the tag_functions field: <> Another method of the class Odoc_html.info will look for the function associated to a custom tag and apply it to the text given to the tag. If no function is associated to a custom tag, then the method prints a warning message on stderr. For other generators -------------------- You can act the same way for other kinds of generators. 15.4 Adding command line options *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* The command line analysis is performed after loading the module containing the documentation generator, thus allowing command line options to be added to the list of existing ones. Adding an option can be done with the function << Odoc_args.add_option : string * Arg.spec * string -> unit >> Note: Existing command line options can be redefined using this function. 15.4.1 Compilation and usage ============================= Defining a custom generator class in one file --------------------------------------------- Let custom.ml be the file defining a new generator class. Compilation of custom.ml can be performed by the following command : << ocamlc -I +ocamldoc -c custom.ml >> The file custom.cmo is created and can be used this way : << ocamldoc -g custom.cmo other-options source-files >> Options selecting a built-in generator to ocamldoc, such as -html, have no effect if a custom generator of the same kind is provided using -g. If the kinds do not match, the selected built-in generator is used and the custom one is ignored. Defining a custom generator class in several files -------------------------------------------------- It is possible to define a generator class in several modules, which are defined in several files file_1.ml[i], file_2.ml[i], ..., file_n.ml[i]. A .cma library file must be created, including all these files. The following commands create the custom.cma file from files file_1.ml[i], ..., file_n.ml[i] : << ocamlc -I +ocamldoc -c file_1.ml[i] ocamlc -I +ocamldoc -c file_2.ml[i] ... ocamlc -I +ocamldoc -c file_n.ml[i] ocamlc -o custom.cma -a file_1.cmo file_2.cmo ... file_n.cmo >> Then, the following command uses custom.cma as custom generator: << ocamldoc -g custom.cma other-options source-files >> --------------------------------------- (1) https://github.com/ocaml/ocaml/blob/4.05/ocamldoc/odoc_html.ml (2) https://github.com/ocaml/ocaml/blob/4.05/ocamldoc/odoc_latex.ml (3) https://github.com/ocaml/ocaml/blob/4.05/ocamldoc/odoc_texi.ml (4) https://github.com/ocaml/ocaml/blob/4.05/ocamldoc/odoc_man.ml (5) https://github.com/ocaml/ocaml/blob/4.05/ocamldoc/odoc_dot.ml Chapter 16 The debugger (ocamldebug) *************************************** This chapter describes the OCaml source-level replay debugger ocamldebug. Unix: The debugger is available on Unix systems that provide BSD sockets. Windows: The debugger is available under the Cygwin port of OCaml, but not under the native Win32 ports. 16.1 Compiling for debugging *=*=*=*=*=*=*=*=*=*=*=*=*=*=* Before the debugger can be used, the program must be compiled and linked with the -g option: all .cmo and .cma files that are part of the program should have been created with ocamlc -g, and they must be linked together with ocamlc -g. Compiling with -g entails no penalty on the running time of programs: object files and bytecode executable files are bigger and take longer to produce, but the executable files run at exactly the same speed as if they had been compiled without -g. 16.2 Invocation *=*=*=*=*=*=*=*= 16.2.1 Starting the debugger ============================= The OCaml debugger is invoked by running the program ocamldebug with the name of the bytecode executable file as first argument: << ocamldebug [options] program [arguments] >> The arguments following program are optional, and are passed as command-line arguments to the program being debugged. (See also the set arguments command.) The following command-line options are recognized: -c count Set the maximum number of simultaneously live checkpoints to count. -cd dir Run the debugger program from the working directory dir, instead of the current directory. (See also the cd command.) -emacs Tell the debugger it is executed under Emacs. (See section 16.10 for information on how to run the debugger under Emacs.) -I directory Add directory to the list of directories searched for source files and compiled files. (See also the directory command.) -s socket Use socket for communicating with the debugged program. See the description of the command set socket (section 16.8.6) for the format of socket. -version Print version string and exit. -vnum Print short version number and exit. -help or --help Display a short usage summary and exit. 16.2.2 Initialization file =========================== On start-up, the debugger will read commands from an initialization file before giving control to the user. The default file is .ocamldebug in the current directory if it exists, otherwise .ocamldebug in the user's home directory. 16.2.3 Exiting the debugger ============================ The command quit exits the debugger. You can also exit the debugger by typing an end-of-file character (usually ctrl-D). Typing an interrupt character (usually ctrl-C) will not exit the debugger, but will terminate the action of any debugger command that is in progress and return to the debugger command level. 16.3 Commands *=*=*=*=*=*=*= A debugger command is a single line of input. It starts with a command name, which is followed by arguments depending on this name. Examples: << run goto 1000 set arguments arg1 arg2 >> A command name can be truncated as long as there is no ambiguity. For instance, go 1000 is understood as goto 1000, since there are no other commands whose name starts with go. For the most frequently used commands, ambiguous abbreviations are allowed. For instance, r stands for run even though there are others commands starting with r. You can test the validity of an abbreviation using the help command. If the previous command has been successful, a blank line (typing just RET) will repeat it. 16.3.1 Getting help ==================== The OCaml debugger has a simple on-line help system, which gives a brief description of each command and variable. help Print the list of commands. help command Give help about the command command. help set variable, help show variable Give help about the variable variable. The list of all debugger variables can be obtained with help set. help info topic Give help about topic. Use help info to get a list of known topics. 16.3.2 Accessing the debugger state ==================================== set variable value Set the debugger variable variable to the value value. show variable Print the value of the debugger variable variable. info subject Give information about the given subject. For instance, info breakpoints will print the list of all breakpoints. 16.4 Executing a program *=*=*=*=*=*=*=*=*=*=*=*=* 16.4.1 Events ============== Events are "interesting" locations in the source code, corresponding to the beginning or end of evaluation of "interesting" sub-expressions. Events are the unit of single-stepping (stepping goes to the next or previous event encountered in the program execution). Also, breakpoints can only be set at events. Thus, events play the role of line numbers in debuggers for conventional languages. During program execution, a counter is incremented at each event encountered. The value of this counter is referred as the current time. Thanks to reverse execution, it is possible to jump back and forth to any time of the execution. Here is where the debugger events (written §§) are located in the source code: - Following a function application: << (f arg)§§ >> - On entrance to a function: << fun x y z -> §§ ... >> - On each case of a pattern-matching definition (function, match...with construct, try...with construct): << function pat1 -> §§ expr1 | ... | patN -> §§ exprN >> - Between subexpressions of a sequence: << expr1; §§ expr2; §§ ...; §§ exprN >> - In the two branches of a conditional expression: << if cond then §§ expr1 else §§ expr2 >> - At the beginning of each iteration of a loop: << while cond do §§ body done for i = a to b do §§ body done >> Exceptions: A function application followed by a function return is replaced by the compiler by a jump (tail-call optimization). In this case, no event is put after the function application. 16.4.2 Starting the debugged program ===================================== The debugger starts executing the debugged program only when needed. This allows setting breakpoints or assigning debugger variables before execution starts. There are several ways to start execution: run Run the program until a breakpoint is hit, or the program terminates. goto 0 Load the program and stop on the first event. goto time Load the program and execute it until the given time. Useful when you already know approximately at what time the problem appears. Also useful to set breakpoints on function values that have not been computed at time 0 (see section 16.5). The execution of a program is affected by certain information it receives when the debugger starts it, such as the command-line arguments to the program and its working directory. The debugger provides commands to specify this information (set arguments and cd). These commands must be used before program execution starts. If you try to change the arguments or the working directory after starting your program, the debugger will kill the program (after asking for confirmation). 16.4.3 Running the program =========================== The following commands execute the program forward or backward, starting at the current time. The execution will stop either when specified by the command or when a breakpoint is encountered. run Execute the program forward from current time. Stops at next breakpoint or when the program terminates. reverse Execute the program backward from current time. Mostly useful to go to the last breakpoint encountered before the current time. step [count] Run the program and stop at the next event. With an argument, do it count times. If count is 0, run until the program terminates or a breakpoint is hit. backstep [count] Run the program backward and stop at the previous event. With an argument, do it count times. next [count] Run the program and stop at the next event, skipping over function calls. With an argument, do it count times. previous [count] Run the program backward and stop at the previous event, skipping over function calls. With an argument, do it count times. finish Run the program until the current function returns. start Run the program backward and stop at the first event before the current function invocation. 16.4.4 Time travel =================== You can jump directly to a given time, without stopping on breakpoints, using the goto command. As you move through the program, the debugger maintains an history of the successive times you stop at. The last command can be used to revisit these times: each last command moves one step back through the history. That is useful mainly to undo commands such as step and next. goto time Jump to the given time. last [count] Go back to the latest time recorded in the execution history. With an argument, do it count times. set history size Set the size of the execution history. 16.4.5 Killing the program =========================== kill Kill the program being executed. This command is mainly useful if you wish to recompile the program without leaving the debugger. 16.5 Breakpoints *=*=*=*=*=*=*=*=* A breakpoint causes the program to stop whenever a certain point in the program is reached. It can be set in several ways using the break command. Breakpoints are assigned numbers when set, for further reference. The most comfortable way to set breakpoints is through the Emacs interface (see section 16.10). break Set a breakpoint at the current position in the program execution. The current position must be on an event (i.e., neither at the beginning, nor at the end of the program). break function Set a breakpoint at the beginning of function. This works only when the functional value of the identifier function has been computed and assigned to the identifier. Hence this command cannot be used at the very beginning of the program execution, when all identifiers are still undefined; use goto time to advance execution until the functional value is available. break @ [module] line Set a breakpoint in module module (or in the current module if module is not given), at the first event of line line. break @ [module] line column Set a breakpoint in module module (or in the current module if module is not given), at the event closest to line line, column column. break @ [module] # character Set a breakpoint in module module at the event closest to character number character. break address Set a breakpoint at the code address address. delete [breakpoint-numbers] Delete the specified breakpoints. Without argument, all breakpoints are deleted (after asking for confirmation). info breakpoints Print the list of all breakpoints. 16.6 The call stack *=*=*=*=*=*=*=*=*=*= Each time the program performs a function application, it saves the location of the application (the return address) in a block of data called a stack frame. The frame also contains the local variables of the caller function. All the frames are allocated in a region of memory called the call stack. The command backtrace (or bt) displays parts of the call stack. At any time, one of the stack frames is "selected" by the debugger; several debugger commands refer implicitly to the selected frame. In particular, whenever you ask the debugger for the value of a local variable, the value is found in the selected frame. The commands frame, up and down select whichever frame you are interested in. When the program stops, the debugger automatically selects the currently executing frame and describes it briefly as the frame command does. frame Describe the currently selected stack frame. frame frame-number Select a stack frame by number and describe it. The frame currently executing when the program stopped has number 0; its caller has number 1; and so on up the call stack. backtrace [count], bt [count] Print the call stack. This is useful to see which sequence of function calls led to the currently executing frame. With a positive argument, print only the innermost count frames. With a negative argument, print only the outermost -count frames. up [count] Select and display the stack frame just "above" the selected frame, that is, the frame that called the selected frame. An argument says how many frames to go up. down [count] Select and display the stack frame just "below" the selected frame, that is, the frame that was called by the selected frame. An argument says how many frames to go down. 16.7 Examining variable values *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* The debugger can print the current value of simple expressions. The expressions can involve program variables: all the identifiers that are in scope at the selected program point can be accessed. Expressions that can be printed are a subset of OCaml expressions, as described by the following grammar: simple-expr ::= lowercase-ident | { capitalized-ident . } lowercase-ident | * | $ integer | simple-expr . lowercase-ident | simple-expr .( integer ) | simple-expr .[ integer ] | ! simple-expr | ( simple-expr ) The first two cases refer to a value identifier, either unqualified or qualified by the path to the structure that define it. * refers to the result just computed (typically, the value of a function application), and is valid only if the selected event is an "after" event (typically, a function application). $ integer refer to a previously printed value. The remaining four forms select part of an expression: respectively, a record field, an array element, a string element, and the current contents of a reference. print variables Print the values of the given variables. print can be abbreviated as p. display variables Same as print, but limit the depth of printing to 1. Useful to browse large data structures without printing them in full. display can be abbreviated as d. When printing a complex expression, a name of the form $integer is automatically assigned to its value. Such names are also assigned to parts of the value that cannot be printed because the maximal printing depth is exceeded. Named values can be printed later on with the commands p $integer or d $integer. Named values are valid only as long as the program is stopped. They are forgotten as soon as the program resumes execution. set print_depth d Limit the printing of values to a maximal depth of d. set print_length l Limit the printing of values to at most l nodes printed. 16.8 Controlling the debugger *=*=*=*=*=*=*=*=*=*=*=*=*=*=*= 16.8.1 Setting the program name and arguments ============================================== set program file Set the program name to file. set arguments arguments Give arguments as command-line arguments for the program. A shell is used to pass the arguments to the debugged program. You can therefore use wildcards, shell variables, and file redirections inside the arguments. To debug programs that read from standard input, it is recommended to redirect their input from a file (using set arguments < input-file), otherwise input to the program and input to the debugger are not properly separated, and inputs are not properly replayed when running the program backwards. 16.8.2 How programs are loaded =============================== The loadingmode variable controls how the program is executed. set loadingmode direct The program is run directly by the debugger. This is the default mode. set loadingmode runtime The debugger execute the OCaml runtime ocamlrun on the program. Rarely useful; moreover it prevents the debugging of programs compiled in "custom runtime" mode. set loadingmode manual The user starts manually the program, when asked by the debugger. Allows remote debugging (see section 16.8.6). 16.8.3 Search path for files ============================= The debugger searches for source files and compiled interface files in a list of directories, the search path. The search path initially contains the current directory . and the standard library directory. The directory command adds directories to the path. Whenever the search path is modified, the debugger will clear any information it may have cached about the files. directory directorynames Add the given directories to the search path. These directories are added at the front, and will therefore be searched first. directory directorynames for modulename Same as directory directorynames, but the given directories will be searched only when looking for the source file of a module that has been packed into modulename. directory Reset the search path. This requires confirmation. 16.8.4 Working directory ========================= Each time a program is started in the debugger, it inherits its working directory from the current working directory of the debugger. This working directory is initially whatever it inherited from its parent process (typically the shell), but you can specify a new working directory in the debugger with the cd command or the -cd command-line option. cd directory Set the working directory for ocamldebug to directory. pwd Print the working directory for ocamldebug. 16.8.5 Turning reverse execution on and off ============================================ In some cases, you may want to turn reverse execution off. This speeds up the program execution, and is also sometimes useful for interactive programs. Normally, the debugger takes checkpoints of the program state from time to time. That is, it makes a copy of the current state of the program (using the Unix system call fork). If the variable checkpoints is set to off, the debugger will not take any checkpoints. set checkpoints on/off Select whether the debugger makes checkpoints or not. 16.8.6 Communication between the debugger and the program ========================================================== The debugger communicate with the program being debugged through a Unix socket. You may need to change the socket name, for example if you need to run the debugger on a machine and your program on another. set socket socket Use socket for communication with the program. socket can be either a file name, or an Internet port specification host:port, where host is a host name or an Internet address in dot notation, and port is a port number on the host. On the debugged program side, the socket name is passed through the CAML_DEBUG_SOCKET environment variable. 16.8.7 Fine-tuning the debugger ================================ Several variables enables to fine-tune the debugger. Reasonable defaults are provided, and you should normally not have to change them. set processcount count Set the maximum number of checkpoints to count. More checkpoints facilitate going far back in time, but use more memory and create more Unix processes. As checkpointing is quite expensive, it must not be done too often. On the other hand, backward execution is faster when checkpoints are taken more often. In particular, backward single-stepping is more responsive when many checkpoints have been taken just before the current time. To fine-tune the checkpointing strategy, the debugger does not take checkpoints at the same frequency for long displacements (e.g. run) and small ones (e.g. step). The two variables bigstep and smallstep contain the number of events between two checkpoints in each case. set bigstep count Set the number of events between two checkpoints for long displacements. set smallstep count Set the number of events between two checkpoints for small displacements. The following commands display information on checkpoints and events: info checkpoints Print a list of checkpoints. info events [module] Print the list of events in the given module (the current module, by default). 16.8.8 User-defined printers ============================= Just as in the toplevel system (section 9.2), the user can register functions for printing values of certain types. For technical reasons, the debugger cannot call printing functions that reside in the program being debugged. The code for the printing functions must therefore be loaded explicitly in the debugger. load_printer "file-name" Load in the debugger the indicated .cmo or .cma object file. The file is loaded in an environment consisting only of the OCaml standard library plus the definitions provided by object files previously loaded using load_printer. If this file depends on other object files not yet loaded, the debugger automatically loads them if it is able to find them in the search path. The loaded file does not have direct access to the modules of the program being debugged. install_printer printer-name Register the function named printer-name (a value path) as a printer for objects whose types match the argument type of the function. That is, the debugger will call printer-name when it has such an object to print. The printing function printer-name must use the Format library module to produce its output, otherwise its output will not be correctly located in the values printed by the toplevel loop. The value path printer-name must refer to one of the functions defined by the object files loaded using load_printer. It cannot reference the functions of the program being debugged. remove_printer printer-name Remove the named function from the table of value printers. 16.9 Miscellaneous commands *=*=*=*=*=*=*=*=*=*=*=*=*=*= list [module] [beginning] [end] List the source of module module, from line number beginning to line number end. By default, 20 lines of the current module are displayed, starting 10 lines before the current position. source filename Read debugger commands from the script filename. 16.10 Running the debugger under Emacs *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* The most user-friendly way to use the debugger is to run it under Emacs. See the file emacs/README in the distribution for information on how to load the Emacs Lisp files for OCaml support. The OCaml debugger is started under Emacs by the command M-x camldebug, with argument the name of the executable file progname to debug. Communication with the debugger takes place in an Emacs buffer named *camldebug-progname*. The editing and history facilities of Shell mode are available for interacting with the debugger. In addition, Emacs displays the source files containing the current event (the current position in the program execution) and highlights the location of the event. This display is updated synchronously with the debugger action. The following bindings for the most common debugger commands are available in the *camldebug-progname* buffer: C-c C-s (command step): execute the program one step forward. C-c C-k (command backstep): execute the program one step backward. C-c C-n (command next): execute the program one step forward, skipping over function calls. Middle mouse button (command display): display named value. $n under mouse cursor (support incremental browsing of large data structures). C-c C-p (command print): print value of identifier at point. C-c C-d (command display): display value of identifier at point. C-c C-r (command run): execute the program forward to next breakpoint. C-c C-v (command reverse): execute the program backward to latest breakpoint. C-c C-l (command last): go back one step in the command history. C-c C-t (command backtrace): display backtrace of function calls. C-c C-f (command finish): run forward till the current function returns. C-c < (command up): select the stack frame below the current frame. C-c > (command down): select the stack frame above the current frame. In all buffers in OCaml editing mode, the following debugger commands are also available: C-x C-a C-b (command break): set a breakpoint at event closest to point C-x C-a C-p (command print): print value of identifier at point C-x C-a C-d (command display): display value of identifier at point Chapter 17 Profiling (ocamlprof) *********************************** This chapter describes how the execution of OCaml programs can be profiled, by recording how many times functions are called, branches of conditionals are taken, ... 17.1 Compiling for profiling *=*=*=*=*=*=*=*=*=*=*=*=*=*=* Before profiling an execution, the program must be compiled in profiling mode, using the ocamlcp front-end to the ocamlc compiler (see chapter 8) or the ocamloptp front-end to the ocamlopt compiler (see chapter 11). When compiling modules separately, ocamlcp or ocamloptp must be used when compiling the modules (production of .cmo or .cmx files), and can also be used (though this is not strictly necessary) when linking them together. Note If a module (.ml file) doesn't have a corresponding interface (.mli file), then compiling it with ocamlcp will produce object files (.cmi and .cmo) that are not compatible with the ones produced by ocamlc, which may lead to problems (if the .cmi or .cmo is still around) when switching between profiling and non-profiling compilations. To avoid this problem, you should always have a .mli file for each .ml file. The same problem exists with ocamloptp. Note To make sure your programs can be compiled in profiling mode, avoid using any identifier that begins with __ocaml_prof. The amount of profiling information can be controlled through the -P option to ocamlcp or ocamloptp, followed by one or several letters indicating which parts of the program should be profiled: a all options f function calls : a count point is set at the beginning of each function body i if ...then ...else ... : count points are set in both then branch and else branch l while, for loops: a count point is set at the beginning of the loop body m match branches: a count point is set at the beginning of the body of each branch t try ...with ... branches: a count point is set at the beginning of the body of each branch For instance, compiling with ocamlcp -P film profiles function calls, if...then...else..., loops and pattern matching. Calling ocamlcp or ocamloptp without the -P option defaults to -P fm, meaning that only function calls and pattern matching are profiled. Note For compatibility with previous releases, ocamlcp also accepts the -p option, with the same arguments and behaviour as -P. The ocamlcp and ocamloptp commands also accept all the options of the corresponding ocamlc or ocamlopt compiler, except the -pp (preprocessing) option. 17.2 Profiling an execution *=*=*=*=*=*=*=*=*=*=*=*=*=*= Running an executable that has been compiled with ocamlcp or ocamloptp records the execution counts for the specified parts of the program and saves them in a file called ocamlprof.dump in the current directory. If the environment variable OCAMLPROF_DUMP is set when the program exits, its value is used as the file name instead of ocamlprof.dump. The dump file is written only if the program terminates normally (by calling exit or by falling through). It is not written if the program terminates with an uncaught exception. If a compatible dump file already exists in the current directory, then the profiling information is accumulated in this dump file. This allows, for instance, the profiling of several executions of a program on different inputs. Note that dump files produced by byte-code executables (compiled with ocamlcp) are compatible with the dump files produced by native executables (compiled with ocamloptp). 17.3 Printing profiling information *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= The ocamlprof command produces a source listing of the program modules where execution counts have been inserted as comments. For instance, << ocamlprof foo.ml >> prints the source code for the foo module, with comments indicating how many times the functions in this module have been called. Naturally, this information is accurate only if the source file has not been modified after it was compiled. The following options are recognized by ocamlprof: -args filename Read additional newline-terminated command line arguments from filename. -args0 filename Read additional null character terminated command line arguments from filename. -f dumpfile Specifies an alternate dump file of profiling information to be read. -F string Specifies an additional string to be output with profiling information. By default, ocamlprof will annotate programs with comments of the form (* n *) where n is the counter value for a profiling point. With option -F s, the annotation will be (* sn *). -impl filename Process the file filename as an implementation file, even if its extension is not .ml. -intf filename Process the file filename as an interface file, even if its extension is not .mli. -version Print version string and exit. -vnum Print short version number and exit. -help or --help Display a short usage summary and exit. 17.4 Time profiling *=*=*=*=*=*=*=*=*=*= Profiling with ocamlprof only records execution counts, not the actual time spent within each function. There is currently no way to perform time profiling on bytecode programs generated by ocamlc. Native-code programs generated by ocamlopt can be profiled for time and execution counts using the -p option and the standard Unix profiler gprof. Just add the -p option when compiling and linking the program: << ocamlopt -o myprog -p other-options files ./myprog gprof myprog >> OCaml function names in the output of gprof have the following format: << Module-name_function-name_unique-number >> Other functions shown are either parts of the OCaml run-time system or external C functions linked with the program. The output of gprof is described in the Unix manual page for gprof(1). It generally consists of two parts: a "flat" profile showing the time spent in each function and the number of invocation of each function, and a "hierarchical" profile based on the call graph. Currently, only the Intel x86 ports of ocamlopt under Linux, BSD and MacOS X support the two profiles. On other platforms, gprof will report only the "flat" profile with just time information. When reading the output of gprof, keep in mind that the accumulated times computed by gprof are based on heuristics and may not be exact. Note The ocamloptp command also accepts the -p option. In that case, both kinds of profiling are performed by the program, and you can display the results with the gprof and ocamlprof commands, respectively. Chapter 18 The ocamlbuild compilation manager ************************************************ Since OCaml version 4.03, the ocamlbuild compilation manager is distributed separately from the OCaml compiler. The project is now hosted at https://github.com/ocaml/ocamlbuild/. Chapter 19 Interfacing C with OCaml ************************************** This chapter describes how user-defined primitives, written in C, can be linked with OCaml code and called from OCaml functions, and how these C functions can call back to OCaml code. 19.1 Overview and compilation information *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= 19.1.1 Declaring primitives ============================ definition ::= ... | external value-name : typexpr = external-declaration external-declaration ::= string-literal [ string-literal [ string-literal ] ] User primitives are declared in an implementation file or struct...end module expression using the external keyword: << external name : type = C-function-name >> This defines the value name name as a function with type type that executes by calling the given C function. For instance, here is how the input primitive is declared in the standard library module Pervasives: << external input : in_channel -> bytes -> int -> int -> int = "input" >> Primitives with several arguments are always curried. The C function does not necessarily have the same name as the ML function. External functions thus defined can be specified in interface files or sig...end signatures either as regular values << val name : type >> thus hiding their implementation as C functions, or explicitly as "manifest" external functions << external name : type = C-function-name >> The latter is slightly more efficient, as it allows clients of the module to call directly the C function instead of going through the corresponding OCaml function. On the other hand, it should not be used in library modules if they have side-effects at toplevel, as this direct call interferes with the linker's algorithm for removing unused modules from libraries at link-time. The arity (number of arguments) of a primitive is automatically determined from its OCaml type in the external declaration, by counting the number of function arrows in the type. For instance, input above has arity 4, and the input C function is called with four arguments. Similarly, << external input2 : in_channel * bytes * int * int -> int = "input2" >> has arity 1, and the input2 C function receives one argument (which is a quadruple of OCaml values). Type abbreviations are not expanded when determining the arity of a primitive. For instance, << type int_endo = int -> int external f : int_endo -> int_endo = "f" external g : (int -> int) -> (int -> int) = "f" >> f has arity 1, but g has arity 2. This allows a primitive to return a functional value (as in the f example above): just remember to name the functional return type in a type abbreviation. The language accepts external declarations with one or two flag strings in addition to the C function's name. These flags are reserved for the implementation of the standard library. 19.1.2 Implementing primitives =============================== User primitives with arity n <= 5 are implemented by C functions that take n arguments of type value, and return a result of type value. The type value is the type of the representations for OCaml values. It encodes objects of several base types (integers, floating-point numbers, strings, ...) as well as OCaml data structures. The type value and the associated conversion functions and macros are described in detail below. For instance, here is the declaration for the C function implementing the input primitive: <> When the primitive function is applied in an OCaml program, the C function is called with the values of the expressions to which the primitive is applied as arguments. The value returned by the function is passed back to the OCaml program as the result of the function application. User primitives with arity greater than 5 should be implemented by two C functions. The first function, to be used in conjunction with the bytecode compiler ocamlc, receives two arguments: a pointer to an array of OCaml values (the values for the arguments), and an integer which is the number of arguments provided. The other function, to be used in conjunction with the native-code compiler ocamlopt, takes its arguments directly. For instance, here are the two C functions for the 7-argument primitive Nat.add_nat: <> The names of the two C functions must be given in the primitive declaration, as follows: << external name : type = bytecode-C-function-name native-code-C-function-name >> For instance, in the case of add_nat, the declaration is: << external add_nat: nat -> int -> int -> nat -> int -> int -> int -> int = "add_nat_bytecode" "add_nat_native" >> Implementing a user primitive is actually two separate tasks: on the one hand, decoding the arguments to extract C values from the given OCaml values, and encoding the return value as an OCaml value; on the other hand, actually computing the result from the arguments. Except for very simple primitives, it is often preferable to have two distinct C functions to implement these two tasks. The first function actually implements the primitive, taking native C values as arguments and returning a native C value. The second function, often called the "stub code", is a simple wrapper around the first function that converts its arguments from OCaml values to C values, call the first function, and convert the returned C value to OCaml value. For instance, here is the stub code for the input primitive: <> (Here, Val_long, Long_val and so on are conversion macros for the type value, that will be described later. The CAMLprim macro expands to the required compiler directives to ensure that the function is exported and accessible from OCaml.) The hard work is performed by the function getblock, which is declared as: <> To write C code that operates on OCaml values, the following include files are provided: ----------------------------------------------------- | Include file | Provides | ----------------------------------------------------- | caml/mlvalues.h|definition of the value type, and | | |conversion macros | |caml/alloc.h |allocation functions (to create | | |structured OCaml objects) | |caml/memory.h |miscellaneous memory-related | | |functions and macros (for GC | | |interface, in-place modification | | |of structures, etc). | |caml/fail.h |functions for raising exceptions | | |(see section 19.4.5) | |caml/callback.h |callback from C to OCaml (see | | |section 19.7). | |caml/custom.h |operations on custom blocks (see | | |section 19.9). | |caml/intext.h |operations for writing | | |user-defined serialization and | | |deserialization functions for | | |custom blocks (see section 19.9). | |caml/threads.h |operations for interfacing in the | | |presence of multiple threads (see | | |section 19.11). | ----------------------------------------------------- These files reside in the caml/ subdirectory of the OCaml standard library directory, which is returned by the command ocamlc -where (usually /usr/local/lib/ocaml or /usr/lib/ocaml). By default, header files in the caml/ subdirectory give only access to the public interface of the OCaml runtime. It is possible to define the macro CAML_INTERNALS to get access to a lower-level interface, but this lower-level interface is more likely to change and break programs that use it. Note: It is recommended to define the macro CAML_NAME_SPACE before including these header files. If you do not define it, the header files will also define short names (without the caml_ prefix) for most functions, which usually produce clashes with names defined by other C libraries that you might use. Including the header files without CAML_NAME_SPACE is only supported for backward compatibility. 19.1.3 Statically linking C code with OCaml code ================================================= The OCaml runtime system comprises three main parts: the bytecode interpreter, the memory manager, and a set of C functions that implement the primitive operations. Some bytecode instructions are provided to call these C functions, designated by their offset in a table of functions (the table of primitives). In the default mode, the OCaml linker produces bytecode for the standard runtime system, with a standard set of primitives. References to primitives that are not in this standard set result in the "unavailable C primitive" error. (Unless dynamic loading of C libraries is supported -- see section 19.1.4 below.) In the "custom runtime" mode, the OCaml linker scans the object files and determines the set of required primitives. Then, it builds a suitable runtime system, by calling the native code linker with: - the table of the required primitives; - a library that provides the bytecode interpreter, the memory manager, and the standard primitives; - libraries and object code files (.o files) mentioned on the command line for the OCaml linker, that provide implementations for the user's primitives. This builds a runtime system with the required primitives. The OCaml linker generates bytecode for this custom runtime system. The bytecode is appended to the end of the custom runtime system, so that it will be automatically executed when the output file (custom runtime + bytecode) is launched. To link in "custom runtime" mode, execute the ocamlc command with: - the -custom option; - the names of the desired OCaml object files (.cmo and .cma files) ; - the names of the C object files and libraries (.o and .a files) that implement the required primitives. Under Unix and Windows, a library named libname.a (respectively, .lib) residing in one of the standard library directories can also be specified as -cclib -lname. If you are using the native-code compiler ocamlopt, the -custom flag is not needed, as the final linking phase of ocamlopt always builds a standalone executable. To build a mixed OCaml/C executable, execute the ocamlopt command with: - the names of the desired OCaml native object files (.cmx and .cmxa files); - the names of the C object files and libraries (.o, .a, .so or .dll files) that implement the required primitives. Starting with Objective Caml 3.00, it is possible to record the -custom option as well as the names of C libraries in an OCaml library file .cma or .cmxa. For instance, consider an OCaml library mylib.cma, built from the OCaml object files a.cmo and b.cmo, which reference C code in libmylib.a. If the library is built as follows: << ocamlc -a -o mylib.cma -custom a.cmo b.cmo -cclib -lmylib >> users of the library can simply link with mylib.cma: << ocamlc -o myprog mylib.cma ... >> and the system will automatically add the -custom and -cclib -lmylib options, achieving the same effect as << ocamlc -o myprog -custom a.cmo b.cmo ... -cclib -lmylib >> The alternative is of course to build the library without extra options: << ocamlc -a -o mylib.cma a.cmo b.cmo >> and then ask users to provide the -custom and -cclib -lmylib options themselves at link-time: << ocamlc -o myprog -custom mylib.cma ... -cclib -lmylib >> The former alternative is more convenient for the final users of the library, however. 19.1.4 Dynamically linking C code with OCaml code ================================================== Starting with Objective Caml 3.03, an alternative to static linking of C code using the -custom code is provided. In this mode, the OCaml linker generates a pure bytecode executable (no embedded custom runtime system) that simply records the names of dynamically-loaded libraries containing the C code. The standard OCaml runtime system ocamlrun then loads dynamically these libraries, and resolves references to the required primitives, before executing the bytecode. This facility is currently supported and known to work well under Linux, MacOS X, and Windows. It is supported, but not fully tested yet, under FreeBSD, Tru64, Solaris and Irix. It is not supported yet under other Unixes. To dynamically link C code with OCaml code, the C code must first be compiled into a shared library (under Unix) or DLL (under Windows). This involves 1- compiling the C files with appropriate C compiler flags for producing position-independent code (when required by the operating system), and 2- building a shared library from the resulting object files. The resulting shared library or DLL file must be installed in a place where ocamlrun can find it later at program start-up time (see section 10.3). Finally (step 3), execute the ocamlc command with - the names of the desired OCaml object files (.cmo and .cma files) ; - the names of the C shared libraries (.so or .dll files) that implement the required primitives. Under Unix and Windows, a library named dllname.so (respectively, .dll) residing in one of the standard library directories can also be specified as -dllib -lname. Do not set the -custom flag, otherwise you're back to static linking as described in section 19.1.3. The ocamlmklib tool (see section 19.12) automates steps 2 and 3. As in the case of static linking, it is possible (and recommended) to record the names of C libraries in an OCaml .cma library archive. Consider again an OCaml library mylib.cma, built from the OCaml object files a.cmo and b.cmo, which reference C code in dllmylib.so. If the library is built as follows: << ocamlc -a -o mylib.cma a.cmo b.cmo -dllib -lmylib >> users of the library can simply link with mylib.cma: << ocamlc -o myprog mylib.cma ... >> and the system will automatically add the -dllib -lmylib option, achieving the same effect as << ocamlc -o myprog a.cmo b.cmo ... -dllib -lmylib >> Using this mechanism, users of the library mylib.cma do not need to known that it references C code, nor whether this C code must be statically linked (using -custom) or dynamically linked. 19.1.5 Choosing between static linking and dynamic linking =========================================================== After having described two different ways of linking C code with OCaml code, we now review the pros and cons of each, to help developers of mixed OCaml/C libraries decide. The main advantage of dynamic linking is that it preserves the platform-independence of bytecode executables. That is, the bytecode executable contains no machine code, and can therefore be compiled on platform A and executed on other platforms B, C, ..., as long as the required shared libraries are available on all these platforms. In contrast, executables generated by ocamlc -custom run only on the platform on which they were created, because they embark a custom-tailored runtime system specific to that platform. In addition, dynamic linking results in smaller executables. Another advantage of dynamic linking is that the final users of the library do not need to have a C compiler, C linker, and C runtime libraries installed on their machines. This is no big deal under Unix and Cygwin, but many Windows users are reluctant to install Microsoft Visual C just to be able to do ocamlc -custom. There are two drawbacks to dynamic linking. The first is that the resulting executable is not stand-alone: it requires the shared libraries, as well as ocamlrun, to be installed on the machine executing the code. If you wish to distribute a stand-alone executable, it is better to link it statically, using ocamlc -custom -ccopt -static or ocamlopt -ccopt -static. Dynamic linking also raises the "DLL hell" problem: some care must be taken to ensure that the right versions of the shared libraries are found at start-up time. The second drawback of dynamic linking is that it complicates the construction of the library. The C compiler and linker flags to compile to position-independent code and build a shared library vary wildly between different Unix systems. Also, dynamic linking is not supported on all Unix systems, requiring a fall-back case to static linking in the Makefile for the library. The ocamlmklib command (see section 19.12) tries to hide some of these system dependencies. In conclusion: dynamic linking is highly recommended under the native Windows port, because there are no portability problems and it is much more convenient for the end users. Under Unix, dynamic linking should be considered for mature, frequently used libraries because it enhances platform-independence of bytecode executables. For new or rarely-used libraries, static linking is much simpler to set up in a portable way. 19.1.6 Building standalone custom runtime systems ================================================== It is sometimes inconvenient to build a custom runtime system each time OCaml code is linked with C libraries, like ocamlc -custom does. For one thing, the building of the runtime system is slow on some systems (that have bad linkers or slow remote file systems); for another thing, the platform-independence of bytecode files is lost, forcing to perform one ocamlc -custom link per platform of interest. An alternative to ocamlc -custom is to build separately a custom runtime system integrating the desired C libraries, then generate "pure" bytecode executables (not containing their own runtime system) that can run on this custom runtime. This is achieved by the -make-runtime and -use-runtime flags to ocamlc. For example, to build a custom runtime system integrating the C parts of the "Unix" and "Threads" libraries, do: << ocamlc -make-runtime -o /home/me/ocamlunixrun unix.cma threads.cma >> To generate a bytecode executable that runs on this runtime system, do: << ocamlc -use-runtime /home/me/ocamlunixrun -o myprog \ unix.cma threads.cma your .cmo and .cma files >> The bytecode executable myprog can then be launched as usual: myprog args or /home/me/ocamlunixrun myprog args. Notice that the bytecode libraries unix.cma and threads.cma must be given twice: when building the runtime system (so that ocamlc knows which C primitives are required) and also when building the bytecode executable (so that the bytecode from unix.cma and threads.cma is actually linked in). 19.2 The value type *=*=*=*=*=*=*=*=*=*= All OCaml objects are represented by the C type value, defined in the include file caml/mlvalues.h, along with macros to manipulate values of that type. An object of type value is either: - an unboxed integer; - a pointer to a block inside the heap (such as the blocks allocated through one of the caml_alloc_* functions below); - a pointer to an object outside the heap (e.g., a pointer to a block allocated by malloc, or to a C variable). 19.2.1 Integer values ====================== Integer values encode 63-bit signed integers (31-bit on 32-bit architectures). They are unboxed (unallocated). 19.2.2 Blocks ============== Blocks in the heap are garbage-collected, and therefore have strict structure constraints. Each block includes a header containing the size of the block (in words), and the tag of the block. The tag governs how the contents of the blocks are structured. A tag lower than No_scan_tag indicates a structured block, containing well-formed values, which is recursively traversed by the garbage collector. A tag greater than or equal to No_scan_tag indicates a raw block, whose contents are not scanned by the garbage collector. For the benefit of ad-hoc polymorphic primitives such as equality and structured input-output, structured and raw blocks are further classified according to their tags as follows: -------------------------------------------------- | Tag | Contents of the block | -------------------------------------------------- | 0 to No_scan_tag-1|A structured block (an array| | |of OCaml objects). Each | | |field is a value. | |Closure_tag |A closure representing a | | |functional value. The first | | |word is a pointer to a piece| | |of code, the remaining words| | |are value containing the | | |environment. | |String_tag |A character string or a byte| | |sequence. | |Double_tag |A double-precision | | |floating-point number. | |Double_array_tag |An array or record of | | |double-precision | | |floating-point numbers. | |Abstract_tag |A block representing an | | |abstract datatype. | |Custom_tag |A block representing an | | |abstract datatype with | | |user-defined finalization, | | |comparison, hashing, | | |serialization and | | |deserialization functions | | |atttached. | -------------------------------------------------- 19.2.3 Pointers outside the heap ================================= Any word-aligned pointer to an address outside the heap can be safely cast to and from the type value. This includes pointers returned by malloc, and pointers to C variables (of size at least one word) obtained with the & operator. Caution: if a pointer returned by malloc is cast to the type value and returned to OCaml, explicit deallocation of the pointer using free is potentially dangerous, because the pointer may still be accessible from the OCaml world. Worse, the memory space deallocated by free can later be reallocated as part of the OCaml heap; the pointer, formerly pointing outside the OCaml heap, now points inside the OCaml heap, and this can crash the garbage collector. To avoid these problems, it is preferable to wrap the pointer in a OCaml block with tag Abstract_tag or Custom_tag. 19.3 Representation of OCaml data types *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= This section describes how OCaml data types are encoded in the value type. 19.3.1 Atomic types ==================== -------------------------------------------------- |OCaml type| Encoding | -------------------------------------------------- | int |Unboxed integer values. | |char |Unboxed integer values (ASCII code). | |float |Blocks with tag Double_tag. | |bytes |Blocks with tag String_tag. | |string |Blocks with tag String_tag. | |int32 |Blocks with tag Custom_tag. | |int64 |Blocks with tag Custom_tag. | |nativeint |Blocks with tag Custom_tag. | -------------------------------------------------- 19.3.2 Tuples and records ========================== Tuples are represented by pointers to blocks, with tag 0. Records are also represented by zero-tagged blocks. The ordering of labels in the record type declaration determines the layout of the record fields: the value associated to the label declared first is stored in field 0 of the block, the value associated to the second label goes in field 1, and so on. As an optimization, records whose fields all have static type float are represented as arrays of floating-point numbers, with tag Double_array_tag. (See the section below on arrays.) As another optimization, unboxable record types are represented specially; unboxable record types are the immutable record types that have only one field. An unboxable type will be represented in one of two ways: boxed or unboxed. Boxed record types are represented as described above (by a block with tag 0 or Double_array_tag). An unboxed record type is represented directly by the value of its field (i.e. there is no block to represent the record itself). The representation is chosen according to the following, in decreasing order of priority: - An attribute ([@@boxed] or [@@unboxed]) on the type declaration. - A compiler option (-unboxed-types or -no-unboxed-types). - The default representation. In the present version of OCaml, the default is the boxed representation. 19.3.3 Arrays ============== Arrays of integers and pointers are represented like tuples, that is, as pointers to blocks tagged 0. They are accessed with the Field macro for reading and the caml_modify function for writing. Arrays of floating-point numbers (type float array) have a special, unboxed, more efficient representation. These arrays are represented by pointers to blocks with tag Double_array_tag. They should be accessed with the Double_field and Store_double_field macros. 19.3.4 Concrete data types =========================== Constructed terms are represented either by unboxed integers (for constant constructors) or by blocks whose tag encode the constructor (for non-constant constructors). The constant constructors and the non-constant constructors for a given concrete type are numbered separately, starting from 0, in the order in which they appear in the concrete type declaration. A constant constructor is represented by the unboxed integer equal to its constructor number. A non-constant constructor declared with n arguments is represented by a block of size n, tagged with the constructor number; the n fields contain its arguments. Example: ------------------------------------------ |Constructed term| Representation | ------------------------------------------ | () |Val_int(0) | |false |Val_int(0) | |true |Val_int(1) | |[] |Val_int(0) | |h::t |Block with size = 2 and| | |tag = 0; first field | | |contains h, second | | |field t. | ------------------------------------------ As a convenience, caml/mlvalues.h defines the macros Val_unit, Val_false and Val_true to refer to (), false and true. The following example illustrates the assignment of integers and block tags to constructors: < integer "Val_int(0)" *) | B of string (* First non-constant constructor -> block with tag 0 *) | C (* Second constant constructor -> integer "Val_int(1)" *) | D of bool (* Second non-constant constructor -> block with tag 1 *) | E of t * t (* Third non-constant constructor -> block with tag 2 *) >> As an optimization, unboxable concrete data types are represented specially; a concrete data type is unboxable if it has exactly one constructor and this constructor has exactly one argument. Unboxable concrete data types are represented in the same ways as unboxable record types: see the description in section 19.3.2. 19.3.5 Objects =============== Objects are represented as blocks with tag Object_tag. The first field of the block refers to the object's class and associated method suite, in a format that cannot easily be exploited from C. The second field contains a unique object ID, used for comparisons. The remaining fields of the object contain the values of the instance variables of the object. It is unsafe to access directly instance variables, as the type system provides no guarantee about the instance variables contained by an object. One may extract a public method from an object using the C function caml_get_public_method (declared in .) Since public method tags are hashed in the same way as variant tags, and methods are functions taking self as first argument, if you want to do the method call foo#bar from the C side, you should call: << callback(caml_get_public_method(foo, hash_variant("bar")), foo); >> 19.3.6 Polymorphic variants ============================ Like constructed terms, polymorphic variant values are represented either as integers (for polymorphic variants without argument), or as blocks (for polymorphic variants with an argument). Unlike constructed terms, variant constructors are not numbered starting from 0, but identified by a hash value (an OCaml integer), as computed by the C function hash_variant (declared in ): the hash value for a variant constructor named, say, VConstr is hash_variant("VConstr"). The variant value `VConstr is represented by hash_variant("VConstr"). The variant value `VConstr(v) is represented by a block of size 2 and tag 0, with field number 0 containing hash_variant("VConstr") and field number 1 containing v. Unlike constructed values, polymorphic variant values taking several arguments are not flattened. That is, `VConstr(v, w) is represented by a block of size 2, whose field number 1 contains the representation of the pair (v, w), rather than a block of size 3 containing v and w in fields 1 and 2. 19.4 Operations on values *=*=*=*=*=*=*=*=*=*=*=*=*= 19.4.1 Kind tests ================== - Is_long(v) is true if value v is an immediate integer, false otherwise - Is_block(v) is true if value v is a pointer to a block, and false if it is an immediate integer. 19.4.2 Operations on integers ============================== - Val_long(l) returns the value encoding the long int l. - Long_val(v) returns the long int encoded in value v. - Val_int(i) returns the value encoding the int i. - Int_val(v) returns the int encoded in value v. - Val_bool(x) returns the OCaml boolean representing the truth value of the C integer x. - Bool_val(v) returns 0 if v is the OCaml boolean false, 1 if v is true. - Val_true, Val_false represent the OCaml booleans true and false. 19.4.3 Accessing blocks ======================== - Wosize_val(v) returns the size of the block v, in words, excluding the header. - Tag_val(v) returns the tag of the block v. - Field(v, n) returns the value contained in the n^th field of the structured block v. Fields are numbered from 0 to Wosize_val(v)-1. - Store_field(b, n, v) stores the value v in the field number n of value b, which must be a structured block. - Code_val(v) returns the code part of the closure v. - caml_string_length(v) returns the length (number of bytes) of the string or byte sequence v. - Byte(v, n) returns the n^th byte of the string or byte sequence v, with type char. Bytes are numbered from 0 to string_length(v)-1. - Byte_u(v, n) returns the n^th byte of the string or byte sequence v, with type unsigned char. Bytes are numbered from 0 to string_length(v)-1. - String_val(v) returns a pointer to the first byte of the string or byte sequence v, with type char *. This pointer is a valid C string: there is a null byte after the last byte in the string. However, OCaml strings and byte sequences can contain embedded null bytes, which will confuse the usual C functions over strings. - Double_val(v) returns the floating-point number contained in value v, with type double. - Double_field(v, n) returns the n^th element of the array of floating-point numbers v (a block tagged Double_array_tag). - Store_double_field(v, n, d) stores the double precision floating-point number d in the n^th element of the array of floating-point numbers v. - Data_custom_val(v) returns a pointer to the data part of the custom block v. This pointer has type void * and must be cast to the type of the data contained in the custom block. - Int32_val(v) returns the 32-bit integer contained in the int32 v. - Int64_val(v) returns the 64-bit integer contained in the int64 v. - Nativeint_val(v) returns the long integer contained in the nativeint v. - caml_field_unboxed(v) returns the value of the field of a value v of any unboxed type (record or concrete data type). - caml_field_boxed(v) returns the value of the field of a value v of any boxed type (record or concrete data type). - caml_field_unboxable(v) calls either caml_field_unboxed or caml_field_boxed according to the default representation of unboxable types in the current version of OCaml. The expressions Field(v, n), Byte(v, n) and Byte_u(v, n) are valid l-values. Hence, they can be assigned to, resulting in an in-place modification of value v. Assigning directly to Field(v, n) must be done with care to avoid confusing the garbage collector (see below). 19.4.4 Allocating blocks ========================= Simple interface ---------------- - Atom(t) returns an "atom" (zero-sized block) with tag t. Zero-sized blocks are preallocated outside of the heap. It is incorrect to try and allocate a zero-sized block using the functions below. For instance, Atom(0) represents the empty array. - caml_alloc(n, t) returns a fresh block of size n with tag t. If t is less than No_scan_tag, then the fields of the block are initialized with a valid value in order to satisfy the GC constraints. - caml_alloc_tuple(n) returns a fresh block of size n words, with tag 0. - caml_alloc_string(n) returns a byte sequence (or string) value of length n bytes. The sequence initially contains uninitialized bytes. - caml_copy_string(s) returns a string or byte sequence value containing a copy of the null-terminated C string s (a char *). - caml_copy_double(d) returns a floating-point value initialized with the double d. - caml_copy_int32(i), caml_copy_int64(i) and caml_copy_nativeint(i) return a value of OCaml type int32, int64 and nativeint, respectively, initialized with the integer i. - caml_alloc_array(f, a) allocates an array of values, calling function f over each element of the input array a to transform it into a value. The array a is an array of pointers terminated by the null pointer. The function f receives each pointer as argument, and returns a value. The zero-tagged block returned by alloc_array(f, a) is filled with the values returned by the successive calls to f. (This function must not be used to build an array of floating-point numbers.) - caml_copy_string_array(p) allocates an array of strings or byte sequences, copied from the pointer to a string array p (a char **). p must be NULL-terminated. - caml_alloc_float_array(n) allocates an array of floating point numbers of size n. The array initially contains uninitialized values. - caml_alloc_unboxed(v) returns the value (of any unboxed type) whose field is the value v. - caml_alloc_boxed(v) allocates and returns a value (of any boxed type) whose field is the value v. - caml_alloc_unboxable(v) calls either caml_alloc_unboxed or caml_alloc_boxed according to the default representation of unboxable types in the current version of OCaml. Low-level interface ------------------- The following functions are slightly more efficient than caml_alloc, but also much more difficult to use. From the standpoint of the allocation functions, blocks are divided according to their size as zero-sized blocks, small blocks (with size less than or equal to Max_young_wosize), and large blocks (with size greater than Max_young_wosize). The constant Max_young_wosize is declared in the include file mlvalues.h. It is guaranteed to be at least 64 (words), so that any block with constant size less than or equal to 64 can be assumed to be small. For blocks whose size is computed at run-time, the size must be compared against Max_young_wosize to determine the correct allocation procedure. - caml_alloc_small(n, t) returns a fresh small block of size n <= Max_young_wosize words, with tag t. If this block is a structured block (i.e. if t < No_scan_tag), then the fields of the block (initially containing garbage) must be initialized with legal values (using direct assignment to the fields of the block) before the next allocation. - caml_alloc_shr(n, t) returns a fresh block of size n, with tag t. The size of the block can be greater than Max_young_wosize. (It can also be smaller, but in this case it is more efficient to call caml_alloc_small instead of caml_alloc_shr.) If this block is a structured block (i.e. if t < No_scan_tag), then the fields of the block (initially containing garbage) must be initialized with legal values (using the caml_initialize function described below) before the next allocation. 19.4.5 Raising exceptions ========================== Two functions are provided to raise two standard exceptions: - caml_failwith(s), where s is a null-terminated C string (with type char *), raises exception Failure with argument s. - caml_invalid_argument(s), where s is a null-terminated C string (with type char *), raises exception Invalid_argument with argument s. Raising arbitrary exceptions from C is more delicate: the exception identifier is dynamically allocated by the OCaml program, and therefore must be communicated to the C function using the registration facility described below in section 19.7.3. Once the exception identifier is recovered in C, the following functions actually raise the exception: - caml_raise_constant(id) raises the exception id with no argument; - caml_raise_with_arg(id, v) raises the exception id with the OCaml value v as argument; - caml_raise_with_args(id, n, v) raises the exception id with the OCaml values v[0], ..., v[n-1] as arguments; - caml_raise_with_string(id, s), where s is a null-terminated C string, raises the exception id with a copy of the C string s as argument. 19.5 Living in harmony with the garbage collector *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= Unused blocks in the heap are automatically reclaimed by the garbage collector. This requires some cooperation from C code that manipulates heap-allocated blocks. 19.5.1 Simple interface ======================== All the macros described in this section are declared in the memory.h header file. Rule 1 A function that has parameters or local variables of type value must begin with a call to one of the CAMLparam macros and return with CAMLreturn, CAMLreturn0, or CAMLreturnT. In particular, CAMLlocal and CAMLxparam can only be called after CAMLparam. There are six CAMLparam macros: CAMLparam0 to CAMLparam5, which take zero to five arguments respectively. If your function has no more than 5 parameters of type value, use the corresponding macros with these parameters as arguments. If your function has more than 5 parameters of type value, use CAMLparam5 with five of these parameters, and use one or more calls to the CAMLxparam macros for the remaining parameters (CAMLxparam1 to CAMLxparam5). The macros CAMLreturn, CAMLreturn0, and CAMLreturnT are used to replace the C keyword return. Every occurrence of return x must be replaced by CAMLreturn (x) if x has type value, or CAMLreturnT (t, x) (where t is the type of x); every occurrence of return without argument must be replaced by CAMLreturn0. If your C function is a procedure (i.e. if it returns void), you must insert CAMLreturn0 at the end (to replace C's implicit return). Note: some C compilers give bogus warnings about unused variables caml__dummy_xxx at each use of CAMLparam and CAMLlocal. You should ignore them. Example: <> Note: if your function is a primitive with more than 5 arguments for use with the byte-code runtime, its arguments are not values and must not be declared (they have types value * and int). Rule 2 Local variables of type value must be declared with one of the CAMLlocal macros. Arrays of values are declared with CAMLlocalN. These macros must be used at the beginning of the function, not in a nested block. The macros CAMLlocal1 to CAMLlocal5 declare and initialize one to five local variables of type value. The variable names are given as arguments to the macros. CAMLlocalN(x, n) declares and initializes a local variable of type value [n]. You can use several calls to these macros if you have more than 5 local variables. Example: <> Rule 3 Assignments to the fields of structured blocks must be done with the Store_field macro (for normal blocks) or Store_double_field macro (for arrays and records of floating-point numbers). Other assignments must not use Store_field nor Store_double_field. Store_field (b, n, v) stores the value v in the field number n of value b, which must be a block (i.e. Is_block(b) must be true). Example: <> Warning: The first argument of Store_field and Store_double_field must be a variable declared by CAMLparam* or a parameter declared by CAMLlocal* to ensure that a garbage collection triggered by the evaluation of the other arguments will not invalidate the first argument after it is computed. Use with CAMLlocalN: Arrays of values declared using CAMLlocalN must not be written to using Store_field. Use the normal C array syntax instead. Rule 4 Global variables containing values must be registered with the garbage collector using the caml_register_global_root function. Registration of a global variable v is achieved by calling caml_register_global_root(&v) just before or just after a valid value is stored in v for the first time. You must not call any of the OCaml runtime functions or macros between registering and storing the value. A registered global variable v can be un-registered by calling caml_remove_global_root(&v). If the contents of the global variable v are seldom modified after registration, better performance can be achieved by calling caml_register_generational_global_root(&v) to register v (after its initialization with a valid value, but before any allocation or call to the GC functions), and caml_remove_generational_global_root(&v) to un-register it. In this case, you must not modify the value of v directly, but you must use caml_modify_generational_global_root(&v,x) to set it to x. The garbage collector takes advantage of the guarantee that v is not modified between calls to caml_modify_generational_global_root to scan it less often. This improves performance if the modifications of v happen less often than minor collections. Note: The CAML macros use identifiers (local variables, type identifiers, structure tags) that start with caml__. Do not use any identifier starting with caml__ in your programs. 19.5.2 Low-level interface =========================== We now give the GC rules corresponding to the low-level allocation functions caml_alloc_small and caml_alloc_shr. You can ignore those rules if you stick to the simplified allocation function caml_alloc. Rule 5 After a structured block (a block with tag less than No_scan_tag) is allocated with the low-level functions, all fields of this block must be filled with well-formed values before the next allocation operation. If the block has been allocated with caml_alloc_small, filling is performed by direct assignment to the fields of the block: << Field(v, n) = v_n; >> If the block has been allocated with caml_alloc_shr, filling is performed through the caml_initialize function: << caml_initialize(&Field(v, n), v_n); >> The next allocation can trigger a garbage collection. The garbage collector assumes that all structured blocks contain well-formed values. Newly created blocks contain random data, which generally do not represent well-formed values. If you really need to allocate before the fields can receive their final value, first initialize with a constant value (e.g. Val_unit), then allocate, then modify the fields with the correct value (see rule 6). Rule 6 Direct assignment to a field of a block, as in << Field(v, n) = w; >> is safe only if v is a block newly allocated by caml_alloc_small; that is, if no allocation took place between the allocation of v and the assignment to the field. In all other cases, never assign directly. If the block has just been allocated by caml_alloc_shr, use caml_initialize to assign a value to a field for the first time: << caml_initialize(&Field(v, n), w); >> Otherwise, you are updating a field that previously contained a well-formed value; then, call the caml_modify function: << caml_modify(&Field(v, n), w); >> To illustrate the rules above, here is a C function that builds and returns a list containing the two integers given as parameters. First, we write it using the simplified allocation functions: <> Here, the registering of result is not strictly needed, because no allocation takes place after it gets its value, but it's easier and safer to simply register all the local variables that have type value. Here is the same function written using the low-level allocation functions. We notice that the cons cells are small blocks and can be allocated with caml_alloc_small, and filled by direct assignments on their fields. <> In the two examples above, the list is built bottom-up. Here is an alternate way, that proceeds top-down. It is less efficient, but illustrates the use of caml_modify. <> It would be incorrect to perform Field(r, 1) = tail directly, because the allocation of tail has taken place since r was allocated. 19.6 A complete example *=*=*=*=*=*=*=*=*=*=*=*= This section outlines how the functions from the Unix curses library can be made available to OCaml programs. First of all, here is the interface curses.mli that declares the curses primitives and data types: <<(* File curses.ml -- declaration of primitives and data types *) type window (* The type "window" remains abstract *) external initscr: unit -> window = "caml_curses_initscr" external endwin: unit -> unit = "caml_curses_endwin" external refresh: unit -> unit = "caml_curses_refresh" external wrefresh : window -> unit = "caml_curses_wrefresh" external newwin: int -> int -> int -> int -> window = "caml_curses_newwin" external addch: char -> unit = "caml_curses_addch" external mvwaddch: window -> int -> int -> char -> unit = "caml_curses_mvwaddch" external addstr: string -> unit = "caml_curses_addstr" external mvwaddstr: window -> int -> int -> string -> unit = "caml_curses_mvwaddstr" (* lots more omitted *) >> To compile this interface: << ocamlc -c curses.ml >> To implement these functions, we just have to provide the stub code; the core functions are already implemented in the curses library. The stub code file, curses_stubs.c, looks like this: < #include #include #include #include /* Encapsulation of opaque window handles (of type WINDOW *) as OCaml custom blocks. */ static struct custom_operations curses_window_ops = { "fr.inria.caml.curses_windows", custom_finalize_default, custom_compare_default, custom_hash_default, custom_serialize_default, custom_deserialize_default, custom_compare_ext_default }; /* Accessing the WINDOW * part of an OCaml custom block */ #define Window_val(v) (*((WINDOW **) Data_custom_val(v))) /* Allocating an OCaml custom block to hold the given WINDOW * */ static value alloc_window(WINDOW * w) { value v = alloc_custom(&curses_window_ops, sizeof(WINDOW *), 0, 1); Window_val(v) = w; return v; } value caml_curses_initscr(value unit) { CAMLparam1 (unit); CAMLreturn (alloc_window(initscr())); } value caml_curses_endwin(value unit) { CAMLparam1 (unit); endwin(); CAMLreturn (Val_unit); } value caml_curses_refresh(value unit) { CAMLparam1 (unit); refresh(); CAMLreturn (Val_unit); } value caml_curses_wrefresh(value win) { CAMLparam1 (win); wrefresh(Window_val(win)); CAMLreturn (Val_unit); } value caml_curses_newwin(value nlines, value ncols, value x0, value y0) { CAMLparam4 (nlines, ncols, x0, y0); CAMLreturn (alloc_window(newwin(Int_val(nlines), Int_val(ncols), Int_val(x0), Int_val(y0)))); } value caml_curses_addch(value c) { CAMLparam1 (c); addch(Int_val(c)); /* Characters are encoded like integers */ CAMLreturn (Val_unit); } value caml_curses_mvwaddch(value win, value x, value y, value c) { CAMLparam4 (win, x, y, c); mvwaddch(Window_val(win), Int_val(x), Int_val(y), Int_val(c)); CAMLreturn (Val_unit); } value caml_curses_addstr(value s) { CAMLparam1 (s); addstr(String_val(s)); CAMLreturn (Val_unit); } value caml_curses_mvwaddstr(value win, value x, value y, value s) { CAMLparam4 (win, x, y, s); mvwaddstr(Window_val(win), Int_val(x), Int_val(y), String_val(s)); CAMLreturn (Val_unit); } /* This goes on for pages. */ >> The file curses_stubs.c can be compiled with: << cc -c -I`ocamlc -where` curses_stubs.c >> or, even simpler, << ocamlc -c curses_stubs.c >> (When passed a .c file, the ocamlc command simply calls the C compiler on that file, with the right -I option.) Now, here is a sample OCaml program prog.ml that uses the curses module: <<(* File prog.ml -- main program using curses *) open Curses;; let main_window = initscr () in let small_window = newwin 10 5 20 10 in mvwaddstr main_window 10 2 "Hello"; mvwaddstr small_window 4 3 "world"; refresh(); Unix.sleep 5; endwin() >> To compile and link this program, run: << ocamlc -custom -o prog unix.cma curses.cmo prog.ml curses_stubs.o -cclib -lcurses >> (On some machines, you may need to put -cclib -lcurses -cclib -ltermcap or -cclib -ltermcap instead of -cclib -lcurses.) 19.7 Advanced topic: callbacks from C to OCaml *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* So far, we have described how to call C functions from OCaml. In this section, we show how C functions can call OCaml functions, either as callbacks (OCaml calls C which calls OCaml), or with the main program written in C. 19.7.1 Applying OCaml closures from C ====================================== C functions can apply OCaml function values (closures) to OCaml values. The following functions are provided to perform the applications: - caml_callback(f, a) applies the functional value f to the value a and returns the value returned by f. - caml_callback2(f, a, b) applies the functional value f (which is assumed to be a curried OCaml function with two arguments) to a and b. - caml_callback3(f, a, b, c) applies the functional value f (a curried OCaml function with three arguments) to a, b and c. - caml_callbackN(f, n, args) applies the functional value f to the n arguments contained in the array of values args. If the function f does not return, but raises an exception that escapes the scope of the application, then this exception is propagated to the next enclosing OCaml code, skipping over the C code. That is, if an OCaml function f calls a C function g that calls back an OCaml function h that raises a stray exception, then the execution of g is interrupted and the exception is propagated back into f. If the C code wishes to catch exceptions escaping the OCaml function, it can use the functions caml_callback_exn, caml_callback2_exn, caml_callback3_exn, caml_callbackN_exn. These functions take the same arguments as their non-_exn counterparts, but catch escaping exceptions and return them to the C code. The return value v of the caml_callback*_exn functions must be tested with the macro Is_exception_result(v). If the macro returns "false", no exception occured, and v is the value returned by the OCaml function. If Is_exception_result(v) returns "true", an exception escaped, and its value (the exception descriptor) can be recovered using Extract_exception(v). Warning: If the OCaml function returned with an exception, Extract_exception should be applied to the exception result prior to calling a function that may trigger garbage collection. Otherwise, if v is reachable during garbage collection, the runtime can crash since v does not contain a valid value. Example: << value call_caml_f_ex(value closure, value arg) { CAMLparam2(closure, arg); CAMLlocal2(res, tmp); res = caml_callback_exn(closure, arg); if(Is_exception_result(res)) { res = Extract_exception(res); tmp = caml_alloc(3, 0); /* Safe to allocate: res contains valid value. */ ... } CAMLreturn (res); } >> 19.7.2 Obtaining or registering OCaml closures for use in C functions ====================================================================== There are two ways to obtain OCaml function values (closures) to be passed to the callback functions described above. One way is to pass the OCaml function as an argument to a primitive function. For example, if the OCaml code contains the declaration << external apply : ('a -> 'b) -> 'a -> 'b = "caml_apply" >> the corresponding C stub can be written as follows: << CAMLprim value caml_apply(value vf, value vx) { CAMLparam2(vf, vx); CAMLlocal1(vy); vy = caml_callback(vf, vx); CAMLreturn(vy); } >> Another possibility is to use the registration mechanism provided by OCaml. This registration mechanism enables OCaml code to register OCaml functions under some global name, and C code to retrieve the corresponding closure by this global name. On the OCaml side, registration is performed by evaluating Callback.register n v. Here, n is the global name (an arbitrary string) and v the OCaml value. For instance: << let f x = print_string "f is applied to "; print_int x; print_newline() let _ = Callback.register "test function" f >> On the C side, a pointer to the value registered under name n is obtained by calling caml_named_value(n). The returned pointer must then be dereferenced to recover the actual OCaml value. If no value is registered under the name n, the null pointer is returned. For example, here is a C wrapper that calls the OCaml function f above: << void call_caml_f(int arg) { caml_callback(*caml_named_value("test function"), Val_int(arg)); } >> The pointer returned by caml_named_value is constant and can safely be cached in a C variable to avoid repeated name lookups. On the other hand, the value pointed to can change during garbage collection and must always be recomputed at the point of use. Here is a more efficient variant of call_caml_f above that calls caml_named_value only once: << void call_caml_f(int arg) { static value * closure_f = NULL; if (closure_f == NULL) { /* First time around, look up by name */ closure_f = caml_named_value("test function"); } caml_callback(*closure_f, Val_int(arg)); } >> 19.7.3 Registering OCaml exceptions for use in C functions =========================================================== The registration mechanism described above can also be used to communicate exception identifiers from OCaml to C. The OCaml code registers the exception by evaluating Callback.register_exception n exn, where n is an arbitrary name and exn is an exception value of the exception to register. For example: << exception Error of string let _ = Callback.register_exception "test exception" (Error "any string") >> The C code can then recover the exception identifier using caml_named_value and pass it as first argument to the functions raise_constant, raise_with_arg, and raise_with_string (described in section 19.4.5) to actually raise the exception. For example, here is a C function that raises the Error exception with the given argument: << void raise_error(char * msg) { caml_raise_with_string(*caml_named_value("test exception"), msg); } >> 19.7.4 Main program in C ========================= In normal operation, a mixed OCaml/C program starts by executing the OCaml initialization code, which then may proceed to call C functions. We say that the main program is the OCaml code. In some applications, it is desirable that the C code plays the role of the main program, calling OCaml functions when needed. This can be achieved as follows: - The C part of the program must provide a main function, which will override the default main function provided by the OCaml runtime system. Execution will start in the user-defined main function just like for a regular C program. - At some point, the C code must call caml_main(argv) to initialize the OCaml code. The argv argument is a C array of strings (type char **), terminated with a NULL pointer, which represents the command-line arguments, as passed as second argument to main. The OCaml array Sys.argv will be initialized from this parameter. For the bytecode compiler, argv[0] and argv[1] are also consulted to find the file containing the bytecode. - The call to caml_main initializes the OCaml runtime system, loads the bytecode (in the case of the bytecode compiler), and executes the initialization code of the OCaml program. Typically, this initialization code registers callback functions using Callback.register. Once the OCaml initialization code is complete, control returns to the C code that called caml_main. - The C code can then invoke OCaml functions using the callback mechanism (see section 19.7.1). 19.7.5 Embedding the OCaml code in the C code ============================================== The bytecode compiler in custom runtime mode (ocamlc -custom) normally appends the bytecode to the executable file containing the custom runtime. This has two consequences. First, the final linking step must be performed by ocamlc. Second, the OCaml runtime library must be able to find the name of the executable file from the command-line arguments. When using caml_main(argv) as in section 19.7.4, this means that argv[0] or argv[1] must contain the executable file name. An alternative is to embed the bytecode in the C code. The -output-obj option to ocamlc is provided for this purpose. It causes the ocamlc compiler to output a C object file (.o file, .obj under Windows) containing the bytecode for the OCaml part of the program, as well as a caml_startup function. The C object file produced by ocamlc -output-obj can then be linked with C code using the standard C compiler, or stored in a C library. The caml_startup function must be called from the main C program in order to initialize the OCaml runtime and execute the OCaml initialization code. Just like caml_main, it takes one argv parameter containing the command-line parameters. Unlike caml_main, this argv parameter is used only to initialize Sys.argv, but not for finding the name of the executable file. The caml_startup function calls the uncaught exception handler (or enters the debugger, if running under ocamldebug) if an exception escapes from a top-level module initialiser. Such exceptions may be caught in the C code by instead using the caml_startup_exn function and testing the result using Is_exception_result (followed by Extract_exception if appropriate). The -output-obj option can also be used to obtain the C source file. More interestingly, the same option can also produce directly a shared library (.so file, .dll under Windows) that contains the OCaml code, the OCaml runtime system and any other static C code given to ocamlc (.o, .a, respectively, .obj, .lib). This use of -output-obj is very similar to a normal linking step, but instead of producing a main program that automatically runs the OCaml code, it produces a shared library that can run the OCaml code on demand. The three possible behaviors of -output-obj are selected according to the extension of the resulting file (given with -o). The native-code compiler ocamlopt also supports the -output-obj option, causing it to output a C object file or a shared library containing the native code for all OCaml modules on the command-line, as well as the OCaml startup code. Initialization is performed by calling caml_startup (or caml_startup_exn) as in the case of the bytecode compiler. For the final linking phase, in addition to the object file produced by -output-obj, you will have to provide the OCaml runtime library (libcamlrun.a for bytecode, libasmrun.a for native-code), as well as all C libraries that are required by the OCaml libraries used. For instance, assume the OCaml part of your program uses the Unix library. With ocamlc, you should do: << ocamlc -output-obj -o camlcode.o unix.cma other .cmo and .cma files cc -o myprog C objects and libraries \ camlcode.o -L`ocamlc -where` -lunix -lcamlrun >> With ocamlopt, you should do: << ocamlopt -output-obj -o camlcode.o unix.cmxa other .cmx and .cmxa files cc -o myprog C objects and libraries \ camlcode.o -L`ocamlc -where` -lunix -lasmrun >> Warning: On some ports, special options are required on the final linking phase that links together the object file produced by the -output-obj option and the remainder of the program. Those options are shown in the configuration file config/Makefile generated during compilation of OCaml, as the variables BYTECCLINKOPTS (for object files produced by ocamlc -output-obj) and NATIVECCLINKOPTS (for object files produced by ocamlopt -output-obj). - Windows with the MSVC compiler: the object file produced by OCaml have been compiled with the /MD flag, and therefore all other object files linked with it should also be compiled with /MD. - other systems: you may have to add one or more of -lcurses, -lm, -ldl, depending on your OS and C compiler. Stack backtraces. When OCaml bytecode produced by ocamlc -g is embedded in a C program, no debugging information is included, and therefore it is impossible to print stack backtraces on uncaught exceptions. This is not the case when native code produced by ocamlopt -g is embedded in a C program: stack backtrace information is available, but the backtrace mechanism needs to be turned on programmatically. This can be achieved from the OCaml side by calling Printexc.record_backtrace true in the initialization of one of the OCaml modules. This can also be achieved from the C side by calling caml_record_backtrace(Val_int(1)); in the OCaml-C glue code. 19.8 Advanced example with callbacks *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* This section illustrates the callback facilities described in section 19.7. We are going to package some OCaml functions in such a way that they can be linked with C code and called from C just like any C functions. The OCaml functions are defined in the following mod.ml OCaml source: <<(* File mod.ml -- some "useful" OCaml functions *) let rec fib n = if n < 2 then 1 else fib(n-1) + fib(n-2) let format_result n = Printf.sprintf "Result is: %d\n" n (* Export those two functions to C *) let _ = Callback.register "fib" fib let _ = Callback.register "format_result" format_result >> Here is the C stub code for calling these functions from C: < #include #include #include int fib(int n) { static value * fib_closure = NULL; if (fib_closure == NULL) fib_closure = caml_named_value("fib"); return Int_val(caml_callback(*fib_closure, Val_int(n))); } char * format_result(int n) { static value * format_result_closure = NULL; if (format_result_closure == NULL) format_result_closure = caml_named_value("format_result"); return strdup(String_val(caml_callback(*format_result_closure, Val_int(n)))); /* We copy the C string returned by String_val to the C heap so that it remains valid after garbage collection. */ } >> We now compile the OCaml code to a C object file and put it in a C library along with the stub code in modwrap.c and the OCaml runtime system: << ocamlc -custom -output-obj -o modcaml.o mod.ml ocamlc -c modwrap.c cp `ocamlc -where`/libcamlrun.a mod.a && chmod +w mod.a ar r mod.a modcaml.o modwrap.o >> (One can also use ocamlopt -output-obj instead of ocamlc -custom -output-obj. In this case, replace libcamlrun.a (the bytecode runtime library) by libasmrun.a (the native-code runtime library).) Now, we can use the two functions fib and format_result in any C program, just like regular C functions. Just remember to call caml_startup (or caml_startup_exn) once before. < #include extern int fib(int n); extern char * format_result(int n); int main(int argc, char ** argv) { int result; /* Initialize OCaml code */ caml_startup(argv); /* Do some computation */ result = fib(10); printf("fib(10) = %s\n", format_result(result)); return 0; } >> To build the whole program, just invoke the C compiler as follows: << cc -o prog -I `ocamlc -where` main.c mod.a -lcurses >> (On some machines, you may need to put -ltermcap or -lcurses -ltermcap instead of -lcurses.) 19.9 Advanced topic: custom blocks *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* Blocks with tag Custom_tag contain both arbitrary user data and a pointer to a C struct, with type struct custom_operations, that associates user-provided finalization, comparison, hashing, serialization and deserialization functions to this block. 19.9.1 The struct custom_operations ==================================== The struct custom_operations is defined in and contains the following fields: - char *identifier A zero-terminated character string serving as an identifier for serialization and deserialization operations. - void (*finalize)(value v) The finalize field contains a pointer to a C function that is called when the block becomes unreachable and is about to be reclaimed. The block is passed as first argument to the function. The finalize field can also be custom_finalize_default to indicate that no finalization function is associated with the block. - int (*compare)(value v1, value v2) The compare field contains a pointer to a C function that is called whenever two custom blocks are compared using OCaml's generic comparison operators (=, <>, <=, >=, <, > and compare). The C function should return 0 if the data contained in the two blocks are structurally equal, a negative integer if the data from the first block is less than the data from the second block, and a positive integer if the data from the first block is greater than the data from the second block. The compare field can be set to custom_compare_default; this default comparison function simply raises Failure. - int (*compare_ext)(value v1, value v2) (Since 3.12.1) The compare_ext field contains a pointer to a C function that is called whenever one custom block and one unboxed integer are compared using OCaml's generic comparison operators (=, <>, <=, >=, <, > and compare). As in the case of the compare field, the C function should return 0 if the two arguments are structurally equal, a negative integer if the first argument compares less than the second argument, and a positive integer if the first argument compares greater than the second argument. The compare_ext field can be set to custom_compare_ext_default; this default comparison function simply raises Failure. - intnat (*hash)(value v) The hash field contains a pointer to a C function that is called whenever OCaml's generic hash operator (see module Hashtbl) is applied to a custom block. The C function can return an arbitrary integer representing the hash value of the data contained in the given custom block. The hash value must be compatible with the compare function, in the sense that two structurally equal data (that is, two custom blocks for which compare returns 0) must have the same hash value. The hash field can be set to custom_hash_default, in which case the custom block is ignored during hash computation. - void (*serialize)(value v, uintnat * wsize_32, uintnat * wsize_64) The serialize field contains a pointer to a C function that is called whenever the custom block needs to be serialized (marshaled) using the OCaml functions output_value or Marshal.to_.... For a custom block, those functions first write the identifier of the block (as given by the identifier field) to the output stream, then call the user-provided serialize function. That function is responsible for writing the data contained in the custom block, using the serialize_... functions defined in and listed below. The user-provided serialize function must then store in its wsize_32 and wsize_64 parameters the sizes in bytes of the data part of the custom block on a 32-bit architecture and on a 64-bit architecture, respectively. The serialize field can be set to custom_serialize_default, in which case the Failure exception is raised when attempting to serialize the custom block. - uintnat (*deserialize)(void * dst) The deserialize field contains a pointer to a C function that is called whenever a custom block with identifier identifier needs to be deserialized (un-marshaled) using the OCaml functions input_value or Marshal.from_.... This user-provided function is responsible for reading back the data written by the serialize operation, using the deserialize_... functions defined in and listed below. It must then rebuild the data part of the custom block and store it at the pointer given as the dst argument. Finally, it returns the size in bytes of the data part of the custom block. This size must be identical to the wsize_32 result of the serialize operation if the architecture is 32 bits, or wsize_64 if the architecture is 64 bits. The deserialize field can be set to custom_deserialize_default to indicate that deserialization is not supported. In this case, do not register the struct custom_operations with the deserializer using register_custom_operations (see below). Note: the finalize, compare, hash, serialize and deserialize functions attached to custom block descriptors must never trigger a garbage collection. Within these functions, do not call any of the OCaml allocation functions, and do not perform a callback into OCaml code. Do not use CAMLparam to register the parameters to these functions, and do not use CAMLreturn to return the result. 19.9.2 Allocating custom blocks ================================ Custom blocks must be allocated via the caml_alloc_custom function: caml_alloc_custom(ops, size, used, max) returns a fresh custom block, with room for size bytes of user data, and whose associated operations are given by ops (a pointer to a struct custom_operations, usually statically allocated as a C global variable). The two parameters used and max are used to control the speed of garbage collection when the finalized object contains pointers to out-of-heap resources. Generally speaking, the OCaml incremental major collector adjusts its speed relative to the allocation rate of the program. The faster the program allocates, the harder the GC works in order to reclaim quickly unreachable blocks and avoid having large amount of "floating garbage" (unreferenced objects that the GC has not yet collected). Normally, the allocation rate is measured by counting the in-heap size of allocated blocks. However, it often happens that finalized objects contain pointers to out-of-heap memory blocks and other resources (such as file descriptors, X Windows bitmaps, etc.). For those blocks, the in-heap size of blocks is not a good measure of the quantity of resources allocated by the program. The two arguments used and max give the GC an idea of how much out-of-heap resources are consumed by the finalized block being allocated: you give the amount of resources allocated to this object as parameter used, and the maximum amount that you want to see in floating garbage as parameter max. The units are arbitrary: the GC cares only about the ratio used / max. For instance, if you are allocating a finalized block holding an X Windows bitmap of w by h pixels, and you'd rather not have more than 1 mega-pixels of unreclaimed bitmaps, specify used = w * h and max = 1000000. Another way to describe the effect of the used and max parameters is in terms of full GC cycles. If you allocate many custom blocks with used / max = 1 / N, the GC will then do one full cycle (examining every object in the heap and calling finalization functions on those that are unreachable) every N allocations. For instance, if used = 1 and max = 1000, the GC will do one full cycle at least every 1000 allocations of custom blocks. If your finalized blocks contain no pointers to out-of-heap resources, or if the previous discussion made little sense to you, just take used = 0 and max = 1. But if you later find that the finalization functions are not called "often enough", consider increasing the used / max ratio. 19.9.3 Accessing custom blocks =============================== The data part of a custom block v can be accessed via the pointer Data_custom_val(v). This pointer has type void * and should be cast to the actual type of the data stored in the custom block. The contents of custom blocks are not scanned by the garbage collector, and must therefore not contain any pointer inside the OCaml heap. In other terms, never store an OCaml value in a custom block, and do not use Field, Store_field nor caml_modify to access the data part of a custom block. Conversely, any C data structure (not containing heap pointers) can be stored in a custom block. 19.9.4 Writing custom serialization and deserialization functions ================================================================== The following functions, defined in , are provided to write and read back the contents of custom blocks in a portable way. Those functions handle endianness conversions when e.g. data is written on a little-endian machine and read back on a big-endian machine. ------------------------------------------------------- | Function | Action | ------------------------------------------------------- | caml_serialize_int_1 |Write a 1-byte integer | |caml_serialize_int_2 |Write a 2-byte integer | |caml_serialize_int_4 |Write a 4-byte integer | |caml_serialize_int_8 |Write a 8-byte integer | |caml_serialize_float_4 |Write a 4-byte float | |caml_serialize_float_8 |Write a 8-byte float | |caml_serialize_block_1 |Write an array of 1-byte | | |quantities | |caml_serialize_block_2 |Write an array of 2-byte | | |quantities | |caml_serialize_block_4 |Write an array of 4-byte | | |quantities | |caml_serialize_block_8 |Write an array of 8-byte | | |quantities | |caml_deserialize_uint_1 |Read an unsigned 1-byte | | |integer | |caml_deserialize_sint_1 |Read a signed 1-byte integer| | | | |caml_deserialize_uint_2 |Read an unsigned 2-byte | | |integer | |caml_deserialize_sint_2 |Read a signed 2-byte integer| | | | |caml_deserialize_uint_4 |Read an unsigned 4-byte | | |integer | |caml_deserialize_sint_4 |Read a signed 4-byte integer| | | | |caml_deserialize_uint_8 |Read an unsigned 8-byte | | |integer | |caml_deserialize_sint_8 |Read a signed 8-byte integer| | | | |caml_deserialize_float_4|Read a 4-byte float | |caml_deserialize_float_8|Read an 8-byte float | |caml_deserialize_block_1|Read an array of 1-byte | | |quantities | |caml_deserialize_block_2|Read an array of 2-byte | | |quantities | |caml_deserialize_block_4|Read an array of 4-byte | | |quantities | |caml_deserialize_block_8|Read an array of 8-byte | | |quantities | |caml_deserialize_error |Signal an error during | | |deserialization; input_value| | |or Marshal.from_... raise a | | |Failure exception after | | |cleaning up their internal | | |data structures | ------------------------------------------------------- Serialization functions are attached to the custom blocks to which they apply. Obviously, deserialization functions cannot be attached this way, since the custom block does not exist yet when deserialization begins! Thus, the struct custom_operations that contain deserialization functions must be registered with the deserializer in advance, using the register_custom_operations function declared in . Deserialization proceeds by reading the identifier off the input stream, allocating a custom block of the size specified in the input stream, searching the registered struct custom_operation blocks for one with the same identifier, and calling its deserialize function to fill the data part of the custom block. 19.9.5 Choosing identifiers ============================ Identifiers in struct custom_operations must be chosen carefully, since they must identify uniquely the data structure for serialization and deserialization operations. In particular, consider including a version number in the identifier; this way, the format of the data can be changed later, yet backward-compatible deserialisation functions can be provided. Identifiers starting with _ (an underscore character) are reserved for the OCaml runtime system; do not use them for your custom data. We recommend to use a URL (http://mymachine.mydomain.com/mylibrary/version-number) or a Java-style package name (com.mydomain.mymachine.mylibrary.version-number) as identifiers, to minimize the risk of identifier collision. 19.9.6 Finalized blocks ======================== Custom blocks generalize the finalized blocks that were present in OCaml prior to version 3.00. For backward compatibility, the format of custom blocks is compatible with that of finalized blocks, and the alloc_final function is still available to allocate a custom block with a given finalization function, but default comparison, hashing and serialization functions. caml_alloc_final(n, f, used, max) returns a fresh custom block of size n words, with finalization function f. The first word is reserved for storing the custom operations; the other n-1 words are available for your data. The two parameters used and max are used to control the speed of garbage collection, as described for caml_alloc_custom. 19.10 Advanced topic: cheaper C call *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* This section describe how to make calling C functions cheaper. Note: this only applies to the native compiler. So whenever you use any of these methods, you have to provide an alternative byte-code stub that ignores all the special annotations. 19.10.1 Passing unboxed values =============================== We said earlier that all OCaml objects are represented by the C type value, and one has to use macros such as Int_val to decode data from the value type. It is however possible to tell OCaml to do this for us and pass arguments unboxed to the C function. Similarly it is possible to tell OCaml to expect the result unboxed and box it for us. The motivation is that, by letting the OCaml compiler deal with boxing, it can often decide to suppress it entirely. For instance let's consider this example: < float -> float = "foo" let f a b = let len = Array.length a in assert (Array.length b = len); let res = Array.make len 0. in for i = 0 to len - 1 do res.(i) <- foo a.(i) b.(i) done >> Float arrays are unboxed in OCaml, however the C function foo expect its arguments as boxed floats and returns a boxed float. Hence the OCaml compiler has no choice but to box a.(i) and b.(i) and unbox the result of foo. This results in the allocation of 3 * len temporary float values. Now if we annotate the arguments and result with [@unboxed], the compiler will be able to avoid all these allocations: < (float [@unboxed]) -> (float [@unboxed]) = "foo_byte" "foo" >> In this case the C functions must look like: <> For convenicence, when all arguments and the result are annotated with [@unboxed], it is possible to put the attribute only once on the declaration itself. So we can also write instead: < float -> float = "foo_byte" "foo" [@@unboxed] >> The following table summarize what OCaml types can be unboxed, and what C types should be used in correspondence: --------------------- |OCaml type|C type | --------------------- | float |double | |int32 |int32_t | |int64 |int64_t | |nativeint |intnat | --------------------- Similarly, it is possible to pass untagged OCaml integers between OCaml and C. This is done by annotating the arguments and/or result with [@untagged]: < (int [@untagged]) = "f_byte" "f" >> The corresponding C type must be intnat. Note: do not use the C int type in correspondence with (int [@untagged]). This is because they often differ in size. 19.10.2 Direct C call ====================== In order to be able to run the garbage collector in the middle of a C function, the OCaml compiler generates some bookkeeping code around C calls. Technically it wraps every C call with the C function caml_c_call which is part of the OCaml runtime. For small functions that are called repeatedly, this indirection can have a big impact on performances. However this is not needed if we know that the C function doesn't allocate and doesn't raise exceptions. We can instruct the OCaml compiler of this fact by annotating the external declaration with the attribute [@@noalloc]: < int -> int = "foo" [@@noalloc] >> In this case calling bar from OCaml is as cheap as calling any other OCaml function, except for the fact that the OCaml compiler can't inline C functions... 19.10.3 Example: calling C library functions without indirection ================================================================= Using these attributes, it is possible to call C library functions with no indirection. For instance many math functions are defined this way in the OCaml standard library: < float = "caml_sqrt_float" "sqrt" [@@unboxed] [@@noalloc] (** Square root. *) external exp : float -> float = "caml_exp_float" "exp" [@@unboxed] [@@noalloc] (** Exponential. *) external log : float -> float = "caml_log_float" "log" [@@unboxed] [@@noalloc] (** Natural logarithm. *) >> 19.11 Advanced topic: multithreading *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* Using multiple threads (shared-memory concurrency) in a mixed OCaml/C application requires special precautions, which are described in this section. 19.11.1 Registering threads created from C =========================================== Callbacks from C to OCaml are possible only if the calling thread is known to the OCaml run-time system. Threads created from OCaml (through the Thread.create function of the system threads library) are automatically known to the run-time system. If the application creates additional threads from C and wishes to callback into OCaml code from these threads, it must first register them with the run-time system. The following functions are declared in the include file . - caml_c_thread_register() registers the calling thread with the OCaml run-time system. Returns 1 on success, 0 on error. Registering an already-register thread does nothing and returns 0. - caml_c_thread_unregister() must be called before the thread terminates, to unregister it from the OCaml run-time system. Returns 1 on success, 0 on error. If the calling thread was not previously registered, does nothing and returns 0. 19.11.2 Parallel execution of long-running C code ================================================== The OCaml run-time system is not reentrant: at any time, at most one thread can be executing OCaml code or C code that uses the OCaml run-time system. Technically, this is enforced by a "master lock" that any thread must hold while executing such code. When OCaml calls the C code implementing a primitive, the master lock is held, therefore the C code has full access to the facilities of the run-time system. However, no other thread can execute OCaml code concurrently with the C code of the primitive. If a C primitive runs for a long time or performs potentially blocking input-output operations, it can explicitly release the master lock, enabling other OCaml threads to run concurrently with its operations. The C code must re-acquire the master lock before returning to OCaml. This is achieved with the following functions, declared in the include file . - caml_release_runtime_system() The calling thread releases the master lock and other OCaml resources, enabling other threads to run OCaml code in parallel with the execution of the calling thread. - caml_acquire_runtime_system() The calling thread re-acquires the master lock and other OCaml resources. It may block until no other thread uses the OCaml run-time system. After caml_release_runtime_system() was called and until caml_acquire_runtime_system() is called, the C code must not access any OCaml data, nor call any function of the run-time system, nor call back into OCaml code. Consequently, arguments provided by OCaml to the C primitive must be copied into C data structures before calling caml_release_runtime_system(), and results to be returned to OCaml must be encoded as OCaml values after caml_acquire_runtime_system() returns. Example: the following C primitive invokes gethostbyname to find the IP address of a host name. The gethostbyname function can block for a long time, so we choose to release the OCaml run-time system while it is running. <> Callbacks from C to OCaml must be performed while holding the master lock to the OCaml run-time system. This is naturally the case if the callback is performed by a C primitive that did not release the run-time system. If the C primitive released the run-time system previously, or the callback is performed from other C code that was not invoked from OCaml (e.g. an event loop in a GUI application), the run-time system must be acquired before the callback and released after: << caml_acquire_runtime_system(); /* Resolve OCaml function vfun to be invoked */ /* Build OCaml argument varg to the callback */ vres = callback(vfun, varg); /* Copy relevant parts of result vres to C data structures */ caml_release_runtime_system(); >> Note: the acquire and release functions described above were introduced in OCaml 3.12. Older code uses the following historical names, declared in : - caml_enter_blocking_section as an alias for caml_release_runtime_system - caml_leave_blocking_section as an alias for caml_acquire_runtime_system Intuition: a "blocking section" is a piece of C code that does not use the OCaml run-time system, typically a blocking input/output operation. 19.12 Building mixed C/OCaml libraries: ocamlmklib *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* The ocamlmklib command facilitates the construction of libraries containing both OCaml code and C code, and usable both in static linking and dynamic linking modes. This command is available under Windows since Objective Caml 3.11 and under other operating systems since Objective Caml 3.03. The ocamlmklib command takes three kinds of arguments: - OCaml source files and object files (.cmo, .cmx, .ml) comprising the OCaml part of the library; - C object files (.o, .a, respectively, .obj, .lib) comprising the C part of the library; - Support libraries for the C part (-llib). It generates the following outputs: - An OCaml bytecode library .cma incorporating the .cmo and .ml OCaml files given as arguments, and automatically referencing the C library generated with the C object files. - An OCaml native-code library .cmxa incorporating the .cmx and .ml OCaml files given as arguments, and automatically referencing the C library generated with the C object files. - If dynamic linking is supported on the target platform, a .so (respectively, .dll) shared library built from the C object files given as arguments, and automatically referencing the support libraries. - A C static library .a(respectively, .lib) built from the C object files. In addition, the following options are recognized: -cclib, -ccopt, -I, -linkall These options are passed as is to ocamlc or ocamlopt. See the documentation of these commands. -rpath, -R, -Wl,-rpath, -Wl,-R These options are passed as is to the C compiler. Refer to the documentation of the C compiler. -custom Force the construction of a statically linked library only, even if dynamic linking is supported. -failsafe Fall back to building a statically linked library if a problem occurs while building the shared library (e.g. some of the support libraries are not available as shared libraries). -Ldir Add dir to the search path for support libraries (-llib). -ocamlc cmd Use cmd instead of ocamlc to call the bytecode compiler. -ocamlopt cmd Use cmd instead of ocamlopt to call the native-code compiler. -o output Set the name of the generated OCaml library. ocamlmklib will generate output.cma and/or output.cmxa. If not specified, defaults to a. -oc outputc Set the name of the generated C library. ocamlmklib will generate liboutputc.so (if shared libraries are supported) and liboutputc.a. If not specified, defaults to the output name given with -o. On native Windows, the following environment variable is also consulted: OCAML_FLEXLINK Alternative executable to use instead of the configured value. Primarily used for bootstrapping. Example Consider an OCaml interface to the standard libz C library for reading and writing compressed files. Assume this library resides in /usr/local/zlib. This interface is composed of an OCaml part zip.cmo/zip.cmx and a C part zipstubs.o containing the stub code around the libz entry points. The following command builds the OCaml libraries zip.cma and zip.cmxa, as well as the companion C libraries dllzip.so and libzip.a: <> If shared libraries are supported, this performs the following commands: <> Note: This example is on a Unix system. The exact command lines may be different on other systems. If shared libraries are not supported, the following commands are performed instead: <> Instead of building simultaneously the bytecode library, the native-code library and the C libraries, ocamlmklib can be called three times to build each separately. Thus, <> builds the bytecode library zip.cma, and <> builds the native-code library zip.cmxa, and <> builds the C libraries dllzip.so and libzip.a. Notice that the support libraries (-lz) and the corresponding options (-L/usr/local/zlib) must be given on all three invocations of ocamlmklib, because they are needed at different times depending on whether shared libraries are supported. Chapter 20 Optimisation with Flambda *************************************** 20.1 Overview *=*=*=*=*=*=*= Flambda is the term used to describe a series of optimisation passes provided by the native code compilers as of OCaml 4.03. Flambda aims to make it easier to write idiomatic OCaml code without incurring performance penalties. To use the Flambda optimisers it is necessary to pass the -flambda option to the OCaml configure script. (There is no support for a single compiler that can operate in both Flambda and non-Flambda modes.) Code compiled with Flambda cannot be linked into the same program as code compiled without Flambda. Attempting to do this will result in a compiler error. Whether or not a particular ocamlopt uses Flambda may be determined by invoking it with the -config option and looking for any line starting with "flambda:". If such a line is present and says "true", then Flambda is supported, otherwise it is not. Flambda provides full optimisation across different compilation units, so long as the .cmx files for the dependencies of the unit currently being compiled are available. (A compilation unit corresponds to a single .ml source file.) However it does not yet act entirely as a whole-program compiler: for example, elimination of dead code across a complete set of compilation units is not supported. Optimisation with Flambda is not currently supported when generating bytecode. Flambda should not in general affect the semantics of existing programs. Two exceptions to this rule are: possible elimination of pure code that is being benchmarked (see section 20.14) and changes in behaviour of code using unsafe operations (see section 20.15). Flambda does not yet optimise array or string bounds checks. Neither does it take hints for optimisation from any assertions written by the user in the code. Consult the Glossary at the end of this chapter for definitions of technical terms used below. 20.2 Command-line flags *=*=*=*=*=*=*=*=*=*=*=*= The Flambda optimisers provide a variety of command-line flags that may be used to control their behaviour. Detailed descriptions of each flag are given in the referenced sections. Those sections also describe any arguments which the particular flags take. Commonly-used options: -O2 Perform more optimisation than usual. Compilation times may be lengthened. (This flag is an abbreviation for a certain set of parameters described in section 20.5.) -O3 Perform even more optimisation than usual, possibly including unrolling of recursive functions. Compilation times may be significantly lengthened. -Oclassic Make inlining decisions at the point of definition of a function rather than at the call site(s). This mirrors the behaviour of OCaml compilers not using Flambda. Compared to compilation using the new Flambda inlining heuristics (for example at -O2) it produces smaller .cmx files, shorter compilation times and code that probably runs rather slower. When using -Oclassic, only the following options described in this section are relevant: -inlining-report and -inline. If any other of the options described in this section are used, the behaviour is undefined and may cause an error in future versions of the compiler. -inlining-report Emit .inlining files (one per round of optimisation) showing all of the inliner's decisions. Less commonly-used options: -remove-unused-arguments Remove unused function arguments even when the argument is not specialised. This may have a small performance penalty. See section 20.10.3. -unbox-closures Pass free variables via specialised arguments rather than closures (an optimisation for reducing allocation). See section 20.9.3. This may have a small performance penalty. Advanced options, only needed for detailed tuning: -inline The behaviour depends on whether -Oclassic is used. - When not in -Oclassic mode, -inline limits the total size of functions considered for inlining during any speculative inlining search. (See section 20.3.6.) Note that this parameter does not control the assessment as to whether any particular function may be inlined. Raising it to excessive amounts will not necessarily cause more functions to be inlined. - When in -Oclassic mode, -inline behaves as in previous versions of the compiler: it is the maximum size of function to be considered for inlining. See section 20.3.1. -inline-toplevel The equivalent of -inline but used when speculative inlining starts at toplevel. See section 20.3.6. Not used in -Oclassic mode. -inline-branch-factor Controls how the inliner assesses whether a code path is likely to be hot or cold. See section 20.3.5. -inline-alloc-cost, -inline-branch-cost, -inline-call-cost Controls how the inliner assesses the runtime performance penalties associated with various operations. See section 20.3.5. -inline-indirect-cost, -inline-prim-cost Likewise. -inline-lifting-benefit Controls inlining of functors at toplevel. See section 20.3.5. -inline-max-depth The maximum depth of any speculative inlining search. See section 20.3.6. -inline-max-unroll The maximum depth of any unrolling of recursive functions during any speculative inlining search. See section 20.3.6. -no-unbox-free-vars-of-closures Do not unbox closure variables. See section 20.9.1. -no-unbox-specialised-args Do not unbox arguments to which functions have been specialised. See section 20.9.2. -rounds How many rounds of optimisation to perform. See section 20.2.1. -unbox-closures-factor Scaling factor for benefit calculation when using -unbox-closures. See section 20.9.3. Notes - The set of command line flags relating to optimisation should typically be specified to be the same across an entire project. Flambda does not currently record the requested flags in the .cmx files. As such, inlining of functions from previously-compiled units will subject their code to the optimisation parameters of the unit currently being compiled, rather than those specified when they were previously compiled. It is hoped to rectify this deficiency in the future. - Flambda-specific flags do not affect linking with the exception of affecting the optimisation of code in the startup file (containing generated functions such as currying helpers). Typically such optimisation will not be significant, so eliding such flags at link time might be reasonable. - Flambda-specific flags are silently accepted even when the -flambda option was not provided to the configure script. (There is no means provided to change this behaviour.) This is intended to make it more straightforward to run benchmarks with and without the Flambda optimisers in effect. - Some of the Flambda flags may be subject to change in future releases. 20.2.1 Specification of optimisation parameters by round ========================================================= Flambda operates in rounds: one round consists of a certain sequence of transformations that may then be repeated in order to achieve more satisfactory results. The number of rounds can be set manually using the -rounds parameter (although this is not necessary when using predefined optimisation levels such as with -O2 and -O3). For high optimisation the number of rounds might be set at 3 or 4. Command-line flags that may apply per round, for example those with -cost in the name, accept arguments of the form: n | round=n[,...] - If the first form is used, with a single integer specified, the value will apply to all rounds. - If the second form is used, zero-based round integers specify values which are to be used only for those rounds. The flags -Oclassic, -O2 and -O3 are applied before all other flags, meaning that certain parameters may be overridden without having to specify every parameter usually invoked by the given optimisation level. 20.3 Inlining *=*=*=*=*=*=*= Inlining refers to the copying of the code of a function to a place where the function is called. The code of the function will be surrounded by bindings of its parameters to the corresponding arguments. The aims of inlining are: - to reduce the runtime overhead caused by function calls (including setting up for such calls and returning afterwards); - to reduce instruction cache misses by expressing frequently-taken paths through the program using fewer machine instructions; and - to reduce the amount of allocation (especially of closures). These goals are often reached not just by inlining itself but also by other optimisations that the compiler is able to perform as a result of inlining. When a recursive call to a function (within the definition of that function or another in the same mutually-recursive group) is inlined, the procedure is also known as unrolling. This is somewhat akin to loop peeling. For example, given the following code: <> unrolling once at the call site fact 4 produces (with the body of fact unchanged): <> This simplifies to: <> Flambda provides significantly enhanced inlining capabilities relative to previous versions of the compiler. Aside: when inlining is performed --------------------------------- Inlining is performed together with all of the other Flambda optimisation passes, that is to say, after closure conversion. This has three particular advantages over a potentially more straightforward implementation prior to closure conversion: - It permits higher-order inlining, for example when a non-inlinable function always returns the same function yet with different environments of definition. Not all such cases are supported yet, but it is intended that such support will be improved in future. - It is easier to integrate with cross-module optimisation, since imported information about other modules is already in the correct intermediate language. - It becomes more straightforward to optimise closure allocations since the layout of closures does not have to be estimated in any way: it is known. Similarly, it becomes more straightforward to control which variables end up in which closures, helping to avoid closure bloat. 20.3.1 Classic inlining heuristic ================================== In -Oclassic mode the behaviour of the Flambda inliner mimics previous versions of the compiler. (Code may still be subject to further optimisations not performed by previous versions of the compiler: functors may be inlined, constants are lifted and unused code is eliminated all as described elsewhere in this chapter. See sections 20.3.3, 20.8.1 and 20.10. At the definition site of a function, the body of the function is measured. It will then be marked as eligible for inlining (and hence inlined at every direct call site) if: - the measured size (in unspecified units) is smaller than that of a function call plus the argument of the -inline command-line flag; and - the function is not recursive. Non-Flambda versions of the compiler cannot inline functions that contain a definition of another function. However -Oclassic does permit this. Further, non-Flambda versions also cannot inline functions that are only themselves exposed as a result of a previous pass of inlining, but again this is permitted by -Oclassic. For example: <> All of this contrasts with the normal Flambda mode, that is to say without -Oclassic, where: - the inlining decision is made at the call site; and - recursive functions can be handled, by specialisation (see below). The Flambda mode is described in the next section. 20.3.2 Overview of "Flambda" inlining heuristics ================================================= The Flambda inlining heuristics, used whenever the compiler is configured for Flambda and -Oclassic was not specified, make inlining decisions at call sites. This helps in situations where the context is important. For example: <> In this case, we would like to inline f into g, because a conditional jump can be eliminated and the code size should reduce. If the inlining decision has been made after the declaration of f without seeing the use, its size would have probably made it ineligible for inlining; but at the call site, its final size can be known. Further, this function should probably not be inlined systematically: if b is unknown, or indeed false, there is little benefit to trade off against a large increase in code size. In the existing non-Flambda inliner this isn't a great problem because chains of inlining were cut off fairly quickly. However it has led to excessive use of overly-large inlining parameters such as -inline 10000. In more detail, at each call site the following procedure is followed: - Determine whether it is clear that inlining would be beneficial without, for the moment, doing any inlining within the function itself. (The exact assessment of benefit is described below.) If so, the function is inlined. - If inlining the function is not clearly beneficial, then inlining will be performed speculatively inside the function itself. The search for speculative inlining possibilities is controlled by two parameters: the inlining threshold and the inlining depth. (These are described in more detail below.) - If such speculation shows that performing some inlining inside the function would be beneficial, then such inlining is performed and the resulting function inlined at the original call site. - Otherwise, nothing happens. Inlining within recursive functions of calls to other functions in the same mutually-recursive group is kept in check by an unrolling depth, described below. This ensures that functions are not unrolled to excess. (Unrolling is only enabled if -O3 optimisation level is selected and/or the -inline-max-unroll flag is passed with an argument greater than zero.) 20.3.3 Handling of specific language constructs ================================================ Functors -------- There is nothing particular about functors that inhibits inlining compared to normal functions. To the inliner, these both look the same, except that functors are marked as such. Applications of functors at toplevel are biased in favour of inlining. (This bias may be adjusted: see the documentation for -inline-lifting-benefit below.) Applications of functors not at toplevel, for example in a local module inside some other expression, are treated by the inliner identically to normal function calls. First-class modules ------------------- The inliner will be able to consider inlining a call to a function in a first class module if it knows which particular function is going to be called. The presence of the first-class module record that wraps the set of functions in the module does not per se inhibit inlining. Objects ------- Method calls to objects are not at present inlined by Flambda. 20.3.4 Inlining reports ======================== If the -inlining-report option is provided to the compiler then a file will be emitted corresponding to each round of optimisation. For the OCaml source file basename.ml the files are named basename.round.inlining.org, with round a zero-based integer. Inside the files, which are formatted as "org mode", will be found English prose describing the decisions that the inliner took. 20.3.5 Assessment of inlining benefit ====================================== Inlining typically results in an increase in code size, which if left unchecked, may not only lead to grossly large executables and excessive compilation times but also a decrease in performance due to worse locality. As such, the Flambda inliner trades off the change in code size against the expected runtime performance benefit, with the benefit being computed based on the number of operations that the compiler observes may be removed as a result of inlining. For example given the following code: <> it would be observed that inlining of f would remove: - one direct call; - one conditional branch. Formally, an estimate of runtime performance benefit is computed by first summing the cost of the operations that are known to be removed as a result of the inlining and subsequent simplification of the inlined body. The individual costs for the various kinds of operations may be adjusted using the various -inline-...-cost flags as follows. Costs are specified as integers. All of these flags accept a single argument describing such integers using the conventions detailed in section 20.2.1. -inline-alloc-cost The cost of an allocation. -inline-branch-cost The cost of a branch. -inline-call-cost The cost of a direct function call. -inline-indirect-cost The cost of an indirect function call. -inline-prim-cost The cost of a primitive. Primitives encompass operations including arithmetic and memory access. (Default values are described in section 20.5 below.) The initial benefit value is then scaled by a factor that attempts to compensate for the fact that the current point in the code, if under some number of conditional branches, may be cold. (Flambda does not currently compute hot and cold paths.) The factor---the estimated probability that the inliner really is on a hot path---is calculated as (1/1 + f)^d, where f is set by -inline-branch-factor and d is the nesting depth of branches at the current point. As the inliner descends into more deeply-nested branches, the benefit of inlining thus lessens. The resulting benefit value is known as the estimated benefit. The change in code size is also estimated: morally speaking it should be the change in machine code size, but since that is not available to the inliner, an approximation is used. If the estimated benefit exceeds the increase in code size then the inlined version of the function will be kept. Otherwise the function will not be inlined. Applications of functors at toplevel will be given an additional benefit (which may be controlled by the -inline-lifting-benefit flag) to bias inlining in such situations towards keeping the inlined version. 20.3.6 Control of speculation ============================== As described above, there are three parameters that restrict the search for inlining opportunities during speculation: - the inlining threshold; - the inlining depth; - the unrolling depth. These parameters are ultimately bounded by the arguments provided to the corresponding command-line flags (or their default values): - -inline (or, if the call site that triggered speculation is at toplevel, -inline-toplevel); - -inline-max-depth; - -inline-max-unroll. Note in particular that -inline does not have the meaning that it has in the previous compiler or in -Oclassic mode. In both of those situations -inline was effectively some kind of basic assessment of inlining benefit. However in Flambda inlining mode it corresponds to a constraint on the search; the assessment of benefit is independent, as described above. When speculation starts the inlining threshold starts at the value set by -inline (or -inline-toplevel if appropriate, see above). Upon making a speculative inlining decision the threshold is reduced by the code size of the function being inlined. If the threshold becomes exhausted, at or below zero, no further speculation will be performed. The inlining depth starts at zero and is increased by one every time the inliner descends into another function. It is then decreased by one every time the inliner leaves such function. If the depth exceeds the value set by -inline-max-depth then speculation stops. This parameter is intended as a general backstop for situations where the inlining threshold does not control the search sufficiently. The unrolling depth applies to calls within the same mutually-recursive group of functions. Each time an inlining of such a call is performed the depth is incremented by one when examining the resulting body. If the depth reaches the limit set by -inline-max-unroll then speculation stops. 20.4 Specialisation *=*=*=*=*=*=*=*=*=*= The inliner may discover a call site to a recursive function where something is known about the arguments: for example, they may be equal to some other variables currently in scope. In this situation it may be beneficial to specialise the function to those arguments. This is done by copying the declaration of the function (and any others involved in any same mutually-recursive declaration) and noting the extra information about the arguments. The arguments augmented by this information are known as specialised arguments. In order to try to ensure that specialisation is not performed uselessly, arguments are only specialised if it can be shown that they are invariant: in other words, during the execution of the recursive function(s) themselves, the arguments never change. Unless overridden by an attribute (see below), specialisation of a function will not be attempted if: - the compiler is in -Oclassic mode; - the function is not obviously recursive; - the function is not closed. The compiler can prove invariance of function arguments across multiple functions within a recursive group (although this has some limitations, as shown by the example below). It should be noted that the unboxing of closures pass (see below) can introduce specialised arguments on non-recursive functions. (No other place in the compiler currently does this.) Example: the well-known List.iter function This function might be written like so: < () | h :: t -> f h; iter f t >> and used like this: <> The argument f to iter is invariant so the function may be specialised: < () | h :: t -> f h; iter' f t in iter' print_int (List.rev xs) >> The compiler notes down that for the function iter', the argument f is specialised to the constant closure print_int. This means that the body of iter' may be simplified: < () | h :: t -> print_int h; (* this is now a direct call *) iter' f t in iter' print_int (List.rev xs) >> The call to print_int can indeed be inlined: < () | h :: t -> print_endline (string_of_int h); iter' f t in iter' print_int (List.rev xs) >> The unused specialised argument f may now be removed, leaving: < () | h :: t -> print_endline (string_of_int h); iter' t in iter' (List.rev xs) >> Aside on invariant parameters. The compiler cannot currently detect invariance in cases such as the following. < () | 0 :: t -> iter_swap g f l | h :: t -> f h; iter_swap f g t >> 20.4.1 Assessment of specialisation benefit ============================================ The benefit of specialisation is assessed in a similar way as for inlining. Specialised argument information may mean that the body of the function being specialised can be simplified: the removed operations are accumulated into a benefit. This, together with the size of the duplicated (specialised) function declaration, is then assessed against the size of the call to the original function. 20.5 Default settings of parameters *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= The default settings (when not using -Oclassic) are for one round of optimisation using the following parameters. ---------------------------------- | Parameter |Setting | ---------------------------------- | -inline |10 | |-inline-branch-factor |0.1 | |-inline-alloc-cost |7 | |-inline-branch-cost |5 | |-inline-call-cost |5 | |-inline-indirect-cost |4 | |-inline-prim-cost |3 | |-inline-lifting-benefit|1300 | |-inline-toplevel |160 | |-inline-max-depth |1 | |-inline-max-unroll |0 | |-unbox-closures-factor |10 | ---------------------------------- 20.5.1 Settings at -O2 optimisation level ========================================== When -O2 is specified two rounds of optimisation are performed. The first round uses the default parameters (see above). The second uses the following parameters. --------------------------------------------- | Parameter | Setting | --------------------------------------------- | -inline |25 | |-inline-branch-factor |Same as default | |-inline-alloc-cost |Double the default | |-inline-branch-cost |Double the default | |-inline-call-cost |Double the default | |-inline-indirect-cost |Double the default | |-inline-prim-cost |Double the default | |-inline-lifting-benefit|Same as default | |-inline-toplevel |400 | |-inline-max-depth |2 | |-inline-max-unroll |Same as default | |-unbox-closures-factor |Same as default | --------------------------------------------- 20.5.2 Settings at -O3 optimisation level ========================================== When -O3 is specified three rounds of optimisation are performed. The first two rounds are as for -O2. The third round uses the following parameters. --------------------------------------------- | Parameter | Setting | --------------------------------------------- | -inline |50 | |-inline-branch-factor |Same as default | |-inline-alloc-cost |Triple the default | |-inline-branch-cost |Triple the default | |-inline-call-cost |Triple the default | |-inline-indirect-cost |Triple the default | |-inline-prim-cost |Triple the default | |-inline-lifting-benefit|Same as default | |-inline-toplevel |800 | |-inline-max-depth |3 | |-inline-max-unroll |1 | |-unbox-closures-factor |Same as default | --------------------------------------------- 20.6 Manual control of inlining and specialisation *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* Should the inliner prove recalcitrant and refuse to inline a particular function, or if the observed inlining decisions are not to the programmer's satisfaction for some other reason, inlining behaviour can be dictated by the programmer directly in the source code. One example where this might be appropriate is when the programmer, but not the compiler, knows that a particular function call is on a cold code path. It might be desirable to prevent inlining of the function so that the code size along the hot path is kept smaller, so as to increase locality. The inliner is directed using attributes. For non-recursive functions (and one-step unrolling of recursive functions, although @unroll is more clear for this purpose) the following are supported: @@inline always or @@inline never Attached to a declaration of a function or functor, these direct the inliner to either always or never inline, irrespective of the size/benefit calculation. (If the function is recursive then the body is substituted and no special action is taken for the recursive call site(s).) @@inline with no argument is equivalent to @@inline always. @inlined always or @inlined never Attached to a function application, these direct the inliner likewise. These attributes at call sites override any other attribute that may be present on the corresponding declaration. @inlined with no argument is equivalent to @inlined always. For recursive functions the relevant attributes are: @@specialise always or @@specialise never Attached to a declaration of a function or functor, this directs the inliner to either always or never specialise the function so long as it has appropriate contextual knowledge, irrespective of the size/benefit calculation. @@specialise with no argument is equivalent to @@specialise always. @specialised always or @specialised never Attached to a function application, this directs the inliner likewise. This attribute at a call site overrides any other attribute that may be present on the corresponding declaration. (Note that the function will still only be specialised if there exist one or more invariant parameters whose values are known.) @specialised with no argument is equivalent to @specialised always. @unrolled n This attribute is attached to a function application and always takes an integer argument. Each time the inliner sees the attribute it behaves as follows: - If n is zero or less, nothing happens. - Otherwise the function being called is substituted at the call site with its body having been rewritten such that any recursive calls to that function or any others in the same mutually-recursive group are annotated with the attribute unrolled(n - 1). Inlining may continue on that body. As such, n behaves as the "maximum depth of unrolling". A compiler warning will be emitted if it was found impossible to obey an annotation from an @inlined or @specialised attribute. Example showing correct placement of attributes <> 20.7 Simplification *=*=*=*=*=*=*=*=*=*= Simplification, which is run in conjunction with inlining, propagates information (known as approximations) about which variables hold what values at runtime. Certain relationships between variables and symbols are also tracked: for example, some variable may be known to always hold the same value as some other variable; or perhaps some variable may be known to always hold the value pointed to by some symbol. The propagation can help to eliminate allocations in cases such as: <> The projections from p may be replaced by uses of the variables x and y, potentially meaning that p becomes unused. The propagation performed by the simplification pass is also important for discovering which functions flow to indirect call sites. This can enable the transformation of such call sites into direct call sites, which makes them eligible for an inlining transformation. Note that no information is propagated about the contents of strings, even in safe-string mode, because it cannot yet be guaranteed that they are immutable throughout a given program. 20.8 Other code motion transformations *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* 20.8.1 Lifting of constants ============================ Expressions found to be constant will be lifted to symbol bindings---that is to say, they will be statically allocated in the object file---when they evaluate to boxed values. Such constants may be straightforward numeric constants, such as the floating-point number 42.0, or more complicated values such as constant closures. Lifting of constants to toplevel reduces allocation at runtime. The compiler aims to share constants lifted to toplevel such that there are no duplicate definitions. However if .cmx files are hidden from the compiler then maximal sharing may not be possible. Notes about float arrays The following language semantics apply specifically to constant float arrays. (By "constant float array" is meant an array consisting entirely of floating point numbers that are known at compile time. A common case is a literal such as [| 42.0; 43.0; |]. - Constant float arrays at the toplevel are mutable and never shared. (That is to say, for each such definition there is a distinct symbol in the data section of the object file pointing at the array.) - Constant float arrays not at toplevel are mutable and are created each time the expression is evaluated. This can be thought of as an operation that takes an immutable array (which in the source code has no associated name; let us call it the initialising array) and duplicates it into a fresh mutable array. - If the array is of size four or less, the expression will create a fresh block and write the values into it one by one. There is no reference to the initialising array as a whole. - Otherwise, the initialising array is lifted out and subject to the normal constant sharing procedure; creation of the array consists of bulk copying the initialising array into a fresh value on the OCaml heap. 20.8.2 Lifting of toplevel let bindings ======================================== Toplevel let-expressions may be lifted to symbol bindings to ensure that the corresponding bound variables are not captured by closures. If the defining expression of a given binding is found to be constant, it is bound as such (the technical term is a let-symbol binding). Otherwise, the symbol is bound to a (statically-allocated) preallocated block containing one field. At runtime, the defining expression will be evaluated and the first field of the block filled with the resulting value. This initialise-symbol binding causes one extra indirection but ensures, by virtue of the symbol's address being known at compile time, that uses of the value are not captured by closures. It should be noted that the blocks corresponding to initialise-symbol bindings are kept alive forever, by virtue of them occurring in a static table of GC roots within the object file. This extended lifetime of expressions may on occasion be surprising. If it is desired to create some non-constant value (for example when writing GC tests) that does not have this extended lifetime, then it may be created and used inside a function, with the application point of that function (perhaps at toplevel)---or indeed the function declaration itself---marked as to never be inlined. This technique prevents lifting of the definition of the value in question (assuming of course that it is not constant). 20.9 Unboxing transformations *=*=*=*=*=*=*=*=*=*=*=*=*=*=*= The transformations in this section relate to the splitting apart of boxed (that is to say, non-immediate) values. They are largely intended to reduce allocation, which tends to result in a runtime performance profile with lower variance and smaller tails. 20.9.1 Unboxing of closure variables ===================================== This transformation is enabled unless -no-unbox-free-vars-of-closures is provided. Variables that appear in closure environments may themselves be boxed values. As such, they may be split into further closure variables, each of which corresponds to some projection from the original closure variable(s). This transformation is called unboxing of closure variables or unboxing of free variables of closures. It is only applied when there is reasonable certainty that there are no uses of the boxed free variable itself within the corresponding function bodies. Example: In the following code, the compiler observes that the closure returned from the function f contains a variable pair (free in the body of f) that may be split into two separate variables. < fst pair + snd pair + y >> After some simplification one obtains: < pair_0 + pair_1 + y >> and then: < x0 + x1 + y >> The allocation of the pair has been eliminated. This transformation does not operate if it would cause the closure to contain more than twice as many closure variables as it did beforehand. 20.9.2 Unboxing of specialised arguments ========================================= This transformation is enabled unless -no-unbox-specialised-args is provided. It may become the case during compilation that one or more invariant arguments to a function become specialised to a particular value. When such values are themselves boxed the corresponding specialised arguments may be split into more specialised arguments corresponding to the projections out of the boxed value that occur within the function body. This transformation is called unboxing of specialised arguments. It is only applied when there is reasonable certainty that the boxed argument itself is unused within the function. If the function in question is involved in a recursive group then unboxing of specialised arguments may be immediately replicated across the group based on the dataflow between invariant arguments. Example: Having been given the following code, the compiler will inline loop into f, and then observe inv being invariant and always the pair formed by adding 42 and 43 to the argument x of the function f. < fst inv + snd inv | x::xs -> x + loop2 xs inv and loop2 ys inv = match ys with | [] -> 4 | y::ys -> y - loop inv ys let f x = Printf.printf "%d\n" (loop (x + 42, x + 43) [1; 2; 3]) >> Since the functions have sufficiently few arguments, more specialised arguments will be added. After some simplification one obtains: < inv_0 + inv_1 | x::xs -> x + loop2' xs inv_0 inv_1 and loop2' ys inv_0 inv_1 = match ys with | [] -> 4 | y::ys -> y - loop' ys inv_0 inv_1 in Printf.printf "%d\n" (loop' [1; 2; 3] (x + 42) (x + 43)) >> The allocation of the pair within f has been removed. (Since the two closures for loop' and loop2' are constant they will also be lifted to toplevel with no runtime allocation penalty. This would also happen without having run the transformation to unbox specialise arguments.) The transformation to unbox specialised arguments never introduces extra allocation. The transformation will not unbox arguments if it would result in the original function having sufficiently many arguments so as to inhibit tail-call optimisation. The transformation is implemented by creating a wrapper function that accepts the original arguments. Meanwhile, the original function is renamed and extra arguments are added corresponding to the unboxed specialised arguments; this new function is called from the wrapper. The wrapper will then be inlined at direct call sites. Indeed, all call sites will be direct unless -unbox-closures is being used, since they will have been generated by the compiler when originally specialising the function. (In the case of -unbox-closures other functions may appear with specialised arguments; in this case there may be indirect calls and these will incur a small penalty owing to having to bounce through the wrapper. The technique of direct call surrogates used for -unbox-closures is not used by the transformation to unbox specialised arguments.) 20.9.3 Unboxing of closures ============================ This transformation is not enabled by default. It may be enabled using the -unbox-closures flag. The transformation replaces closure variables by specialised arguments. The aim is to cause more closures to become closed. It is particularly applicable, as a means of reducing allocation, where the function concerned cannot be inlined or specialised. For example, some non-recursive function might be too large to inline; or some recursive function might offer no opportunities for specialisation perhaps because its only argument is one of type unit. At present there may be a small penalty in terms of actual runtime performance when this transformation is enabled, although more stable performance may be obtained due to reduced allocation. It is recommended that developers experiment to determine whether the option is beneficial for their code. (It is expected that in the future it will be possible for the performance degradation to be removed.) Simple example: In the following code (which might typically occur when g is too large to inline) the value of x would usually be communicated to the application of the + function via the closure of g. <> Unboxing of the closure causes the value for x inside g to be passed as an argument to g rather than through its closure. This means that the closure of g becomes constant and may be lifted to toplevel, eliminating the runtime allocation. The transformation is implemented by adding a new wrapper function in the manner of that used when unboxing specialised arguments. The closure variables are still free in the wrapper, but the intention is that when the wrapper is inlined at direct call sites, the relevant values are passed directly to the main function via the new specialised arguments. Adding such a wrapper will penalise indirect calls to the function (which might exist in arbitrary places; remember that this transformation is not for example applied only on functions the compiler has produced as a result of specialisation) since such calls will bounce through the wrapper. To mitigate this, if a function is small enough when weighed up against the number of free variables being removed, it will be duplicated by the transformation to obtain two versions: the original (used for indirect calls, since we can do no better) and the wrapper/rewritten function pair as described in the previous paragraph. The wrapper/rewritten function pair will only be used at direct call sites of the function. (The wrapper in this case is known as a direct call surrogate, since it takes the place of another function---the unchanged version used for indirect calls---at direct call sites.) The -unbox-closures-factor command line flag, which takes an integer, may be used to adjust the point at which a function is deemed large enough to be ineligible for duplication. The benefit of duplication is scaled by the integer before being evaluated against the size. Harder example: In the following code, there are two closure variables that would typically cause closure allocations. One is called fv and occurs inside the function baz; the other is called z and occurs inside the function bar. In this toy (yet sophisticated) example we again use an attribute to simulate the typical situation where the first argument of baz is too large to inline. < [] | z::zs -> let rec baz f = function | [] -> [] | a::l -> let r = fv + ((f [@inlined never]) a) in r :: baz f l in (map2 (fun y -> z + y) [z; 2; 3; 4]) @ bar zs fv in Printf.printf "%d" (List.length (bar [1; 2; 3; 4] c)) >> The code resulting from applying -O3 -unbox-closures to this code passes the free variables via function arguments in order to eliminate all closure allocation in this example (aside from any that might be performed inside printf). 20.10 Removal of unused code and values *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= 20.10.1 Removal of redundant let expressions ============================================= The simplification pass removes unused let bindings so long as their corresponding defining expressions have "no effects". See the section "Treatment of effects" below for the precise definition of this term. 20.10.2 Removal of redundant program constructs ================================================ This transformation is analogous to the removal of let-expressions whose defining expressions have no effects. It operates instead on symbol bindings, removing those that have no effects. 20.10.3 Removal of unused arguments ==================================== This transformation is only enabled by default for specialised arguments. It may be enabled for all arguments using the -remove-unused-arguments flag. The pass analyses functions to determine which arguments are unused. Removal is effected by creating a wrapper function, which will be inlined at every direct call site, that accepts the original arguments and then discards the unused ones before calling the original function. As a consequence, this transformation may be detrimental if the original function is usually indirectly called, since such calls will now bounce through the wrapper. (The technique of direct call surrogates used to reduce this penalty during unboxing of closure variables (see above) does not yet apply to the pass that removes unused arguments.) 20.10.4 Removal of unused closure variables ============================================ This transformation performs an analysis across the whole compilation unit to determine whether there exist closure variables that are never used. Such closure variables are then eliminated. (Note that this has to be a whole-unit analysis because a projection of a closure variable from some particular closure may have propagated to an arbitrary location within the code due to inlining.) 20.11 Other code transformations *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* 20.11.1 Transformation of non-escaping references into mutable variables ========================================================================= Flambda performs a simple analysis analogous to that performed elsewhere in the compiler that can transform refs into mutable variables that may then be held in registers (or on the stack as appropriate) rather than being allocated on the OCaml heap. This only happens so long as the reference concerned can be shown to not escape from its defining scope. 20.11.2 Substitution of closure variables for specialised arguments ==================================================================== This transformation discovers closure variables that are known to be equal to specialised arguments. Such closure variables are replaced by the specialised arguments; the closure variables may then be removed by the "removal of unused closure variables" pass (see below). 20.12 Treatment of effects *=*=*=*=*=*=*=*=*=*=*=*=*=* The Flambda optimisers classify expressions in order to determine whether an expression: - does not need to be evaluated at all; and/or - may be duplicated. This is done by forming judgements on the effects and the coeffects that might be performed were the expression to be executed. Effects talk about how the expression might affect the world; coeffects talk about how the world might affect the expression. Effects are classified as follows: No effects: The expression does not change the observable state of the world. For example, it must not write to any mutable storage, call arbitrary external functions or change control flow (e.g. by raising an exception). Note that allocation is not classed as having "no effects" (see below). - It is assumed in the compiler that expressions with no effects, whose results are not used, may be eliminated. (This typically happens where the expression in question is the defining expression of a let; in such cases the let-expression will be eliminated.) It is further assumed that such expressions with no effects may be duplicated (and thus possibly executed more than once). - Exceptions arising from allocation points, for example "out of memory" or exceptions propagated from finalizers or signal handlers, are treated as "effects out of the ether" and thus ignored for our determination here of effectfulness. The same goes for floating point operations that may cause hardware traps on some platforms. Only generative effects: The expression does not change the observable state of the world save for possibly affecting the state of the garbage collector by performing an allocation. Expressions that only have generative effects and whose results are unused may be eliminated by the compiler. However, unlike expressions with "no effects", such expressions will never be eligible for duplication. Arbitrary effects: All other expressions. There is a single classification for coeffects: No coeffects: The expression does not observe the effects (in the sense described above) of other expressions. For example, it must not read from any mutable storage or call arbitrary external functions. It is assumed in the compiler that, subject to data dependencies, expressions with neither effects nor coeffects may be reordered with respect to other expressions. 20.13 Compilation of statically-allocated modules *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= Compilation of modules that are able to be statically allocated (for example, the module corresponding to an entire compilation unit, as opposed to a first class module dependent on values computed at runtime) initially follows the strategy used for bytecode. A sequence of let-bindings, which may be interspersed with arbitrary effects, surrounds a record creation that becomes the module block. The Flambda-specific transformation follows: these bindings are lifted to toplevel symbols, as described above. 20.14 Inhibition of optimisation *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* Especially when writing benchmarking suites that run non-side-effecting algorithms in loops, it may be found that the optimiser entirely elides the code being benchmarked. This behaviour can be prevented by using the Sys.opaque_identity function (which indeed behaves as a normal OCaml function and does not possess any "magic" semantics). The documentation of the Sys module should be consulted for further details. 20.15 Use of unsafe operations *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* The behaviour of the Flambda simplification pass means that certain unsafe operations, which may without Flambda or when using previous versions of the compiler be safe, must not be used. This specifically refers to functions found in the Obj module. In particular, it is forbidden to change any value (for example using Obj.set_field or Obj.set_tag) that is not mutable. (Values returned from C stubs are always treated as mutable.) The compiler will emit warning 59 if it detects such a write---but it cannot warn in all cases. Here is an example of code that will trigger the warning: <> The reason this is unsafe is because the simplification pass believes that fst a holds the value 42; and indeed it must, unless type soundness has been broken via unsafe operations. If it must be the case that code has to be written that triggers warning 59, but the code is known to actually be correct (for some definition of correct), then Sys.opaque_identity may be used to wrap the value before unsafe operations are performed upon it. Great care must be taken when doing this to ensure that the opacity is added at the correct place. It must be emphasised that this use of Sys.opaque_identity is only for exceptional cases. It should not be used in normal code or to try to guide the optimiser. As an example, this code will return the integer 1: <> However the following code will still return 42: <> High levels of inlining performed by Flambda may expose bugs in code thought previously to be correct. Take care, for example, not to add type annotations that claim some mutable value is always immediate if it might be possible for an unsafe operation to update it to a boxed value. 20.16 Glossary *=*=*=*=*=*=*=* The following terminology is used in this chapter of the manual. Call site See direct call site and indirect call site below. Closed function A function whose body has no free variables except its parameters and any to which are bound other functions within the same (possibly mutually-recursive) declaration. Closure The runtime representation of a function. This includes pointers to the code of the function together with the values of any variables that are used in the body of the function but actually defined outside of the function, in the enclosing scope. The values of such variables, collectively known as the environment, are required because the function may be invoked from a place where the original bindings of such variables are no longer in scope. A group of possibly mutually-recursive functions defined using let rec all share a single closure. (Note to developers: in the Flambda source code a closure always corresponds to a single function; a set of closures refers to a group of such.) Closure variable A member of the environment held within the closure of a given function. Constant Some entity (typically an expression) the value of which is known by the compiler at compile time. Constantness may be explicit from the source code or inferred by the Flambda optimisers. Constant closure A closure that is statically allocated in an object file. It is almost always the case that the environment portion of such a closure is empty. Defining expression The expression e in let x = e in e'. Direct call site A place in a program's code where a function is called and it is known at compile time which function it will always be. Indirect call site A place in a program's code where a function is called but is not known to be a direct call site. Program A collection of symbol bindings forming the definition of a single compilation unit (i.e. .cmx file). Specialised argument An argument to a function that is known to always hold a particular value at runtime. These are introduced by the inliner when specialising recursive functions; and the unbox-closures pass. (See section 20.4.) Symbol A name referencing a particular place in an object file or executable image. At that particular place will be some constant value. Symbols may be examined using operating system-specific tools (for example objdump on Linux). Symbol binding Analogous to a let-expression but working at the level of symbols defined in the object file. The address of a symbol is fixed, but it may be bound to both constant and non-constant expressions. Toplevel An expression in the current program which is not enclosed within any function declaration. Variable A named entity to which some OCaml value is bound by a let expression, pattern-matching construction, or similar. Chapter 21 Memory profiling with Spacetime ********************************************* 21.1 Overview *=*=*=*=*=*=*= Spacetime is the name given to functionality within the OCaml compiler that provides for accurate profiling of the memory behaviour of a program. Using Spacetime it is possible to determine the source of memory leaks and excess memory allocation quickly and easily. Excess allocation slows programs down both by imposing a higher load on the garbage collector and reducing the cache locality of the program's code. Spacetime provides full backtraces for every allocation that occurred on the OCaml heap during the lifetime of the program including those in C stubs. Spacetime only analyses the memory behaviour of a program with respect to the OCaml heap allocators and garbage collector. It does not analyse allocation on the C heap. Spacetime does not affect the memory behaviour of a program being profiled with the exception of any change caused by the overhead of profiling (see section 21.3)---for example the program running slower might cause it to allocate less memory in total. Spacetime is currently only available for x86-64 targets and has only been tested on Linux systems (although it is expected to work on most modern Unix-like systems and provision has been made for running under Windows). It is expected that the set of supported platforms will be extended in the future. 21.2 How to use it *=*=*=*=*=*=*=*=*=* 21.2.1 Building ================ To use Spacetime it is necessary to use an OCaml compiler that was configured with the -spacetime option. It is not possible to select Spacetime on a per-source-file basis or for a subset of files in a project; all files involved in the executable being profiled must be built with the Spacetime compiler. Only native code compilation is supported (not bytecode). If the libunwind library is not available on the system then it will not be possible for Spacetime to profile allocations occurring within C stubs. If the libunwind library is available but in an unusual location then that location may be specified to the configure script using the -libunwinddir option (or alternatively, using separate -libunwindinclude and -libunwindlib options). OPAM switches will be provided for Spacetime-configured compilers. Once the appropriate compiler has been selected the program should be built as normal (ensuring that all files are built with the Spacetime compiler---there is currently no protection to ensure this is the case, but it is essential). For many uses it will not be necessary to change the code of the program to use the profiler. Spacetime-configured compilers run slower and occupy more memory than their counterparts. It is hoped this will be fixed in the future as part of improved cross compilation support. 21.2.2 Running =============== Programs built with Spacetime instrumentation have a dependency on the libunwind library unless that was unavailable at configure time or the -disable-libunwind option was specified (see section 21.3). Setting the OCAML_SPACETIME_INTERVAL environment variable to an integer representing a number of milliseconds before running a program built with Spacetime will cause memory profiling to be in operation when the program is started. The contents of the OCaml heap will be sampled each time the number of milliseconds that the program has spent executing since the last sample exceeds the given number. (Note that the time base is combined user plus system time---not wall clock time. This peculiarity may be changed in future.) The program being profiled must exit normally or be caused to exit using the SIGINT signal (e.g. by pressing Ctrl+C). When the program exits files will be written in the directory that was the working directory when the program was started. One Spacetime file will be written for each process that was involved, indexed by process ID; there will normally only be one such. The Spacetime files may be substantial. The directory to which they are written may be overridden by setting the OCAML_SPACETIME_SNAPSHOT_DIR environment variable before the program is started. Instead of using the automatic snapshot facility described above it is also possible to manually control Spacetime profiling. (The environment variables OCAML_SPACETIME_INTERVAL and OCAML_SPACETIME_SNAPSHOT_DIR are then not relevant.) Full documentation as regards this method of profiling is provided in the standard library documentation (section 24) for the Spacetime module. 21.2.3 Analysis ================ The compiler distribution does not itself provide the facility for analysing Spacetime output files; this is left to external tools. The first such tool will appear in OPAM as a package called prof_spacetime. That tool will provide interactive graphical and terminal-based visualisation of the results of profiling. 21.3 Runtime overhead *=*=*=*=*=*=*=*=*=*=*= The runtime overhead imposed by Spacetime varies considerably depending on the particular program being profiled. The overhead may be as low as ten percent---but more usually programs should be expected to run at perhaps a third or quarter of their normal speed. It is expected that this overhead will be reduced in future versions of the compiler. Execution speed of instrumented programs may be increased by using a compiler configured with the -disable-libunwind option. This prevents collection of profiling information from C stubs. Programs running with Spacetime instrumentation consume significantly more memory than their non-instrumented counterparts. It is expected that this memory overhead will also be reduced in the future. 21.4 For developers *=*=*=*=*=*=*=*=*=*= The compiler distribution provides an "otherlibs" library called raw_spacetime_lib for decoding Spacetime files. This library provides facilities to read not only memory profiling information but also the full dynamic call graph of the profiled program which is written into Spacetime output files. A library package spacetime_lib will be provided in OPAM to provide an interface for decoding profiling information at a higher level than that provided by raw_spacetime_lib. Chapter 22 Fuzzing with afl-fuzz *********************************** 22.1 Overview *=*=*=*=*=*=*= American fuzzy lop ("afl-fuzz") is a fuzzer, a tool for testing software by providing randomly-generated inputs, searching for those inputs which cause the program to crash. Unlike most fuzzers, afl-fuzz observes the internal behaviour of the program being tested, and adjusts the test cases it generates to trigger unexplored execution paths. As a result, test cases generated by afl-fuzz cover more of the possible behaviours of the tested program than other fuzzers. This requires that programs to be tested are instrumented to communicate with afl-fuzz. The native-code compiler "ocamlopt" can generate such instrumentation, allowing afl-fuzz to be used against programs written in OCaml. For more information on afl-fuzz, see the website at http://lcamtuf.coredump.cx/afl/ 22.2 Generating instrumentation *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= The instrumentation that afl-fuzz requires is not generated by default, and must be explicitly enabled, by passing the -afl-instrument option to ocamlopt. To fuzz a large system without modifying build tools, OCaml's configure script also accepts the afl-instrument option. If OCaml is configured with afl-instrument, then all programs compiled by ocamlopt will be instrumented. 22.2.1 Advanced options ======================== In rare cases, it is useful to control the amount of instrumentation generated. By passing the -afl-inst-ratio N argument to ocamlopt with N less than 100, instrumentation can be generated for only N% of branches. (See the afl-fuzz documentation on the parameter AFL_INST_RATIO for the precise effect of this). 22.3 Example *=*=*=*=*=*=* As an example, we fuzz-test the following program, readline.ml: < failwith "uh oh" | _ -> () >> There is a single input (the string "secret code") which causes this program to crash, but finding it by blind random search is infeasible. Instead, we compile with afl-fuzz instrumentation enabled: <> Next, we run the program under afl-fuzz: < input/testcase mkdir output afl-fuzz -i input -o output ./readline >> By inspecting instrumentation output, the fuzzer finds the crashing input quickly. Part: IV ******** The OCaml library ***************** Chapter 23 The core library ****************************** This chapter describes the OCaml core library, which is composed of declarations for built-in types and exceptions, plus the module Pervasives that provides basic operations on these built-in types. The Pervasives module is special in two ways: - It is automatically linked with the user's object code files by the ocamlc command (chapter 8). - It is automatically "opened" when a compilation starts, or when the toplevel system is launched. Hence, it is possible to use unqualified identifiers to refer to the functions provided by the Pervasives module, without adding a open Pervasives directive. Conventions *=*=*=*=*=* The declarations of the built-in types and the components of module Pervasives are printed one by one in typewriter font, followed by a short comment. All library modules and the components they provide are indexed at the end of this report. 23.1 Built-in types and predefined exceptions *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= The following built-in types and predefined exceptions are always defined in the compilation environment, but are not part of any module. As a consequence, they can only be referred by their short names. Built-in types ============== << type int >> The type of integer numbers. << type char >> The type of characters. << type bytes >> The type of (writable) byte sequences. << type string >> The type of (read-only) character strings. << type float >> The type of floating-point numbers. << type bool = false | true >> The type of booleans (truth values). << type unit = () >> The type of the unit value. << type exn >> The type of exception values. << type 'a array >> The type of arrays whose elements have type 'a. << type 'a list = [] | :: of 'a * 'a list >> The type of lists whose elements have type 'a. <> The type of optional values of type 'a. <> The type of signed 32-bit integers. See the Int32[] module. <> The type of signed 64-bit integers. See the Int64[] module. <> The type of signed, platform-native integers (32 bits on 32-bit processors, 64 bits on 64-bit processors). See the Nativeint[] module. <> The type of format strings. 'a is the type of the parameters of the format, 'f is the result type for the printf-style functions, 'b is the type of the first argument given to %a and %t printing functions (see module Printf[]), 'c is the result type of these functions, and also the type of the argument transmitted to the first argument of kprintf-style functions, 'd is the result type for the scanf-style functions (see module Scanf[]), and 'e is the type of the receiver function for the scanf-style functions. <> This type is used to implement the Lazy[] module. It should not be used directly. Predefined exceptions ===================== <> Exception raised when none of the cases of a pattern-matching apply. The arguments are the location of the match keyword in the source code (file name, line number, column number). <> Exception raised when an assertion fails. The arguments are the location of the assert keyword in the source code (file name, line number, column number). <> Exception raised by library functions to signal that the given arguments do not make sense. The string gives some information to the programmer. As a general rule, this exception should not be caught, it denotes a programming error and the code should be modified not to trigger it. <> Exception raised by library functions to signal that they are undefined on the given arguments. The string is meant to give some information to the programmer; you must not pattern match on the string literal because it may change in future versions (use Failure _ instead). <> Exception raised by search functions when the desired object could not be found. <> Exception raised by the garbage collector when there is insufficient memory to complete the computation. <> Exception raised by the bytecode interpreter when the evaluation stack reaches its maximal size. This often indicates infinite or excessively deep recursion in the user's program. (Not fully implemented by the native-code compiler; see section 11.5.) <> Exception raised by the input/output functions to report an operating system error. The string is meant to give some information to the programmer; you must not pattern match on the string literal because it may change in future versions (use Sys_error _ instead). <> Exception raised by input functions to signal that the end of file has been reached. <> Exception raised by integer division and remainder operations when their second argument is zero. <> A special case of Sys_error raised when no I/O is possible on a non-blocking I/O channel. <> Exception raised when an ill-founded recursive module definition is evaluated. (See section 7.4.) The arguments are the location of the definition in the source code (file name, line number, column number). 23.2 Module Pervasives : The initially opened module. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= This module provides the basic operations over the built-in types (numbers, booleans, byte sequences, strings, exceptions, references, lists, arrays, input-output channels, ...). This module is automatically opened at the beginning of each compilation. All components of this module can therefore be referred by their short name, without prefixing them by Pervasives. Exceptions ========== << val raise : exn -> 'a >> Raise the given exception value << val raise_notrace : exn -> 'a >> A faster version raise which does not record the backtrace. Since: 4.02.0 << val invalid_arg : string -> 'a >> Raise exception Invalid_argument with the given string. << val failwith : string -> 'a >> Raise exception Failure with the given string. << exception Exit >> The Exit exception is not raised by any library function. It is provided for use in your programs. Comparisons =========== << val (=) : 'a -> 'a -> bool >> e1 = e2 tests for structural equality of e1 and e2. Mutable structures (e.g. references and arrays) are equal if and only if their current contents are structurally equal, even if the two mutable objects are not the same physical object. Equality between functional values raises Invalid_argument. Equality between cyclic data structures may not terminate. << val (<>) : 'a -> 'a -> bool >> Negation of Pervasives.(=)[23.2]. << val (<) : 'a -> 'a -> bool >> See Pervasives.(>=)[23.2]. << val (>) : 'a -> 'a -> bool >> See Pervasives.(>=)[23.2]. << val (<=) : 'a -> 'a -> bool >> See Pervasives.(>=)[23.2]. << val (>=) : 'a -> 'a -> bool >> Structural ordering functions. These functions coincide with the usual orderings over integers, characters, strings, byte sequences and floating-point numbers, and extend them to a total ordering over all types. The ordering is compatible with ( = ). As in the case of ( = ), mutable structures are compared by contents. Comparison between functional values raises Invalid_argument. Comparison between cyclic structures may not terminate. << val compare : 'a -> 'a -> int >> compare x y returns 0 if x is equal to y, a negative integer if x is less than y, and a positive integer if x is greater than y. The ordering implemented by compare is compatible with the comparison predicates =, < and > defined above, with one difference on the treatment of the float value Pervasives.nan[23.2]. Namely, the comparison predicates treat nan as different from any other float value, including itself; while compare treats nan as equal to itself and less than any other float value. This treatment of nan ensures that compare defines a total ordering relation. compare applied to functional values may raise Invalid_argument. compare applied to cyclic structures may not terminate. The compare function can be used as the comparison function required by the Set.Make[24.34] and Map.Make[24.23] functors, as well as the List.sort[24.21] and Array.sort[24.2] functions. << val min : 'a -> 'a -> 'a >> Return the smaller of the two arguments. The result is unspecified if one of the arguments contains the float value nan. << val max : 'a -> 'a -> 'a >> Return the greater of the two arguments. The result is unspecified if one of the arguments contains the float value nan. << val (==) : 'a -> 'a -> bool >> e1 == e2 tests for physical equality of e1 and e2. On mutable types such as references, arrays, byte sequences, records with mutable fields and objects with mutable instance variables, e1 == e2 is true if and only if physical modification of e1 also affects e2. On non-mutable types, the behavior of ( == ) is implementation-dependent; however, it is guaranteed that e1 == e2 implies compare e1 e2 = 0. << val (!=) : 'a -> 'a -> bool >> Negation of Pervasives.(==)[23.2]. Boolean operations ================== << val not : bool -> bool >> The boolean negation. << val (&&) : bool -> bool -> bool >> The boolean 'and'. Evaluation is sequential, left-to-right: in e1 && e2, e1 is evaluated first, and if it returns false, e2 is not evaluated at all. << val (&) : bool -> bool -> bool >> Deprecated. Pervasives.(&&)[23.2] should be used instead. << val (||) : bool -> bool -> bool >> The boolean 'or'. Evaluation is sequential, left-to-right: in e1 || e2, e1 is evaluated first, and if it returns true, e2 is not evaluated at all. << val (or) : bool -> bool -> bool >> Deprecated. Pervasives.(||)[23.2] should be used instead. Debugging ========= << val __LOC__ : string >> __LOC__ returns the location at which this expression appears in the file currently being parsed by the compiler, with the standard error format of OCaml: "File %S, line %d, characters %d-%d". Since: 4.02.0 << val __FILE__ : string >> __FILE__ returns the name of the file currently being parsed by the compiler. Since: 4.02.0 << val __LINE__ : int >> __LINE__ returns the line number at which this expression appears in the file currently being parsed by the compiler. Since: 4.02.0 << val __MODULE__ : string >> __MODULE__ returns the module name of the file being parsed by the compiler. Since: 4.02.0 << val __POS__ : string * int * int * int >> __POS__ returns a tuple (file,lnum,cnum,enum), corresponding to the location at which this expression appears in the file currently being parsed by the compiler. file is the current filename, lnum the line number, cnum the character position in the line and enum the last character position in the line. Since: 4.02.0 << val __LOC_OF__ : 'a -> string * 'a >> __LOC_OF__ expr returns a pair (loc, expr) where loc is the location of expr in the file currently being parsed by the compiler, with the standard error format of OCaml: "File %S, line %d, characters %d-%d". Since: 4.02.0 << val __LINE_OF__ : 'a -> int * 'a >> __LINE__ expr returns a pair (line, expr), where line is the line number at which the expression expr appears in the file currently being parsed by the compiler. Since: 4.02.0 << val __POS_OF__ : 'a -> (string * int * int * int) * 'a >> __POS_OF__ expr returns a pair (loc,expr), where loc is a tuple (file,lnum,cnum,enum) corresponding to the location at which the expression expr appears in the file currently being parsed by the compiler. file is the current filename, lnum the line number, cnum the character position in the line and enum the last character position in the line. Since: 4.02.0 Composition operators ===================== << val (|>) : 'a -> ('a -> 'b) -> 'b >> Reverse-application operator: x |> f |> g is exactly equivalent to g (f (x)). Since: 4.01 << val (@@) : ('a -> 'b) -> 'a -> 'b >> Application operator: g @@ f @@ x is exactly equivalent to g (f (x)). Since: 4.01 Integer arithmetic ================== Integers are 31 bits wide (or 63 bits on 64-bit processors). All operations are taken modulo 2^31 (or 2^63). They do not fail on overflow. << val (~-) : int -> int >> Unary negation. You can also write - e instead of ~- e. << val (~+) : int -> int >> Unary addition. You can also write + e instead of ~+ e. Since: 3.12.0 << val succ : int -> int >> succ x is x + 1. << val pred : int -> int >> pred x is x - 1. << val (+) : int -> int -> int >> Integer addition. << val (-) : int -> int -> int >> Integer subtraction. << val ( * ) : int -> int -> int >> Integer multiplication. << val (/) : int -> int -> int >> Integer division. Raise Division_by_zero if the second argument is 0. Integer division rounds the real quotient of its arguments towards zero. More precisely, if x >= 0 and y > 0, x / y is the greatest integer less than or equal to the real quotient of x by y. Moreover, (- x) / y = x / (- y) = - (x / y). << val (mod) : int -> int -> int >> Integer remainder. If y is not zero, the result of x mod y satisfies the following properties: x = (x / y) * y + x mod y and abs(x mod y) <= abs(y) - 1. If y = 0, x mod y raises Division_by_zero. Note that x mod y is negative only if x < 0. Raise Division_by_zero if y is zero. << val abs : int -> int >> Return the absolute value of the argument. Note that this may be negative if the argument is min_int. << val max_int : int >> The greatest representable integer. << val min_int : int >> The smallest representable integer. Bitwise operations ------------------ << val (land) : int -> int -> int >> Bitwise logical and. << val (lor) : int -> int -> int >> Bitwise logical or. << val (lxor) : int -> int -> int >> Bitwise logical exclusive or. << val lnot : int -> int >> Bitwise logical negation. << val (lsl) : int -> int -> int >> n lsl m shifts n to the left by m bits. The result is unspecified if m < 0 or m >= bitsize, where bitsize is 32 on a 32-bit platform and 64 on a 64-bit platform. << val (lsr) : int -> int -> int >> n lsr m shifts n to the right by m bits. This is a logical shift: zeroes are inserted regardless of the sign of n. The result is unspecified if m < 0 or m >= bitsize. << val (asr) : int -> int -> int >> n asr m shifts n to the right by m bits. This is an arithmetic shift: the sign bit of n is replicated. The result is unspecified if m < 0 or m >= bitsize. Floating-point arithmetic ========================= OCaml's floating-point numbers follow the IEEE 754 standard, using double precision (64 bits) numbers. Floating-point operations never raise an exception on overflow, underflow, division by zero, etc. Instead, special IEEE numbers are returned as appropriate, such as infinity for 1.0 /. 0.0, neg_infinity for -1.0 /. 0.0, and nan ('not a number') for 0.0 /. 0.0. These special numbers then propagate through floating-point computations as expected: for instance, 1.0 /. infinity is 0.0, and any arithmetic operation with nan as argument returns nan as result. << val (~-.) : float -> float >> Unary negation. You can also write -. e instead of ~-. e. << val (~+.) : float -> float >> Unary addition. You can also write +. e instead of ~+. e. Since: 3.12.0 << val (+.) : float -> float -> float >> Floating-point addition << val (-.) : float -> float -> float >> Floating-point subtraction << val ( *. ) : float -> float -> float >> Floating-point multiplication << val (/.) : float -> float -> float >> Floating-point division. << val ( ** ) : float -> float -> float >> Exponentiation. << val sqrt : float -> float >> Square root. << val exp : float -> float >> Exponential. << val log : float -> float >> Natural logarithm. << val log10 : float -> float >> Base 10 logarithm. << val expm1 : float -> float >> expm1 x computes exp x -. 1.0, giving numerically-accurate results even if x is close to 0.0. Since: 3.12.0 << val log1p : float -> float >> log1p x computes log(1.0 +. x) (natural logarithm), giving numerically-accurate results even if x is close to 0.0. Since: 3.12.0 << val cos : float -> float >> Cosine. Argument is in radians. << val sin : float -> float >> Sine. Argument is in radians. << val tan : float -> float >> Tangent. Argument is in radians. << val acos : float -> float >> Arc cosine. The argument must fall within the range [-1.0, 1.0]. Result is in radians and is between 0.0 and pi. << val asin : float -> float >> Arc sine. The argument must fall within the range [-1.0, 1.0]. Result is in radians and is between -pi/2 and pi/2. << val atan : float -> float >> Arc tangent. Result is in radians and is between -pi/2 and pi/2. << val atan2 : float -> float -> float >> atan2 y x returns the arc tangent of y /. x. The signs of x and y are used to determine the quadrant of the result. Result is in radians and is between -pi and pi. << val hypot : float -> float -> float >> hypot x y returns sqrt(x *. x + y *. y), that is, the length of the hypotenuse of a right-angled triangle with sides of length x and y, or, equivalently, the distance of the point (x,y) to origin. If one of x or y is infinite, returns infinity even if the other is nan. Since: 4.00.0 << val cosh : float -> float >> Hyperbolic cosine. Argument is in radians. << val sinh : float -> float >> Hyperbolic sine. Argument is in radians. << val tanh : float -> float >> Hyperbolic tangent. Argument is in radians. << val ceil : float -> float >> Round above to an integer value. ceil f returns the least integer value greater than or equal to f. The result is returned as a float. << val floor : float -> float >> Round below to an integer value. floor f returns the greatest integer value less than or equal to f. The result is returned as a float. << val abs_float : float -> float >> abs_float f returns the absolute value of f. << val copysign : float -> float -> float >> copysign x y returns a float whose absolute value is that of x and whose sign is that of y. If x is nan, returns nan. If y is nan, returns either x or -. x, but it is not specified which. Since: 4.00.0 << val mod_float : float -> float -> float >> mod_float a b returns the remainder of a with respect to b. The returned value is a -. n *. b, where n is the quotient a /. b rounded towards zero to an integer. << val frexp : float -> float * int >> frexp f returns the pair of the significant and the exponent of f. When f is zero, the significant x and the exponent n of f are equal to zero. When f is non-zero, they are defined by f = x *. 2 ** n and 0.5 <= x < 1.0. << val ldexp : float -> int -> float >> ldexp x n returns x *. 2 ** n. << val modf : float -> float * float >> modf f returns the pair of the fractional and integral part of f. << val float : int -> float >> Same as Pervasives.float_of_int[23.2]. << val float_of_int : int -> float >> Convert an integer to floating-point. << val truncate : float -> int >> Same as Pervasives.int_of_float[23.2]. << val int_of_float : float -> int >> Truncate the given floating-point number to an integer. The result is unspecified if the argument is nan or falls outside the range of representable integers. << val infinity : float >> Positive infinity. << val neg_infinity : float >> Negative infinity. << val nan : float >> A special floating-point value denoting the result of an undefined operation such as 0.0 /. 0.0. Stands for 'not a number'. Any floating-point operation with nan as argument returns nan as result. As for floating-point comparisons, =, <, <=, > and >= return false and <> returns true if one or both of their arguments is nan. << val max_float : float >> The largest positive finite value of type float. << val min_float : float >> The smallest positive, non-zero, non-denormalized value of type float. << val epsilon_float : float >> The difference between 1.0 and the smallest exactly representable floating-point number greater than 1.0. << type fpclass = | FP_normal >> Normal number, none of the below << | FP_subnormal >> Number very close to 0.0, has reduced precision << | FP_zero >> Number is 0.0 or -0.0 << | FP_infinite >> Number is positive or negative infinity << | FP_nan >> Not a number: result of an undefined operation The five classes of floating-point numbers, as determined by the Pervasives.classify_float[23.2] function. << val classify_float : float -> fpclass >> Return the class of the given floating-point number: normal, subnormal, zero, infinite, or not a number. String operations ================= More string operations are provided in module String[25.8]. << val (^) : string -> string -> string >> String concatenation. Character operations ==================== More character operations are provided in module Char[24.8]. << val int_of_char : char -> int >> Return the ASCII code of the argument. << val char_of_int : int -> char >> Return the character with the given ASCII code. Raise Invalid_argument "char_of_int" if the argument is outside the range 0--255. Unit operations =============== << val ignore : 'a -> unit >> Discard the value of its argument and return (). For instance, ignore(f x) discards the result of the side-effecting function f. It is equivalent to f x; (), except that the latter may generate a compiler warning; writing ignore(f x) instead avoids the warning. String conversion functions =========================== << val string_of_bool : bool -> string >> Return the string representation of a boolean. As the returned values may be shared, the user should not modify them directly. << val bool_of_string : string -> bool >> Convert the given string to a boolean. Raise Invalid_argument "bool_of_string" if the string is not "true" or "false". << val bool_of_string_opt : string -> bool option >> Convert the given string to a boolean. Return None if the string is not "true" or "false". Since: 4.05 << val string_of_int : int -> string >> Return the string representation of an integer, in decimal. << val int_of_string : string -> int >> Convert the given string to an integer. The string is read in decimal (by default), in hexadecimal (if it begins with 0x or 0X), in octal (if it begins with 0o or 0O), or in binary (if it begins with 0b or 0B). The _ (underscore) character can appear anywhere in the string and is ignored. Raise Failure "int_of_string" if the given string is not a valid representation of an integer, or if the integer represented exceeds the range of integers representable in type int. << val int_of_string_opt : string -> int option >> Same as int_of_string, but returs None instead of raising. Since: 4.05 << val string_of_float : float -> string >> Return the string representation of a floating-point number. << val float_of_string : string -> float >> Convert the given string to a float. The string is read in decimal (by default) or in hexadecimal (marked by 0x or 0X). The format of decimal floating-point numbers is [-] dd.ddd (e|E) [+|-] dd , where d stands for a decimal digit. The format of hexadecimal floating-point numbers is [-] 0(x|X) hh.hhh (p|P) [+|-] dd , where h stands for an hexadecimal digit and d for a decimal digit. In both cases, at least one of the integer and fractional parts must be given; the exponent part is optional. The _ (underscore) character can appear anywhere in the string and is ignored. Depending on the execution platforms, other representations of floating-point numbers can be accepted, but should not be relied upon. Raise Failure "float_of_string" if the given string is not a valid representation of a float. << val float_of_string_opt : string -> float option >> Same as float_of_string, but returns None instead of raising. Since: 4.05 Pair operations =============== << val fst : 'a * 'b -> 'a >> Return the first component of a pair. << val snd : 'a * 'b -> 'b >> Return the second component of a pair. List operations =============== More list operations are provided in module List[24.21]. << val (@) : 'a list -> 'a list -> 'a list >> List concatenation. Not tail-recursive (length of the first argument). Input/output ============ Note: all input/output functions can raise Sys_error when the system calls they invoke fail. << type in_channel >> The type of input channel. << type out_channel >> The type of output channel. << val stdin : in_channel >> The standard input for the process. << val stdout : out_channel >> The standard output for the process. << val stderr : out_channel >> The standard error output for the process. Output functions on standard output ----------------------------------- << val print_char : char -> unit >> Print a character on standard output. << val print_string : string -> unit >> Print a string on standard output. << val print_bytes : bytes -> unit >> Print a byte sequence on standard output. Since: 4.02.0 << val print_int : int -> unit >> Print an integer, in decimal, on standard output. << val print_float : float -> unit >> Print a floating-point number, in decimal, on standard output. << val print_endline : string -> unit >> Print a string, followed by a newline character, on standard output and flush standard output. << val print_newline : unit -> unit >> Print a newline character on standard output, and flush standard output. This can be used to simulate line buffering of standard output. Output functions on standard error ---------------------------------- << val prerr_char : char -> unit >> Print a character on standard error. << val prerr_string : string -> unit >> Print a string on standard error. << val prerr_bytes : bytes -> unit >> Print a byte sequence on standard error. Since: 4.02.0 << val prerr_int : int -> unit >> Print an integer, in decimal, on standard error. << val prerr_float : float -> unit >> Print a floating-point number, in decimal, on standard error. << val prerr_endline : string -> unit >> Print a string, followed by a newline character on standard error and flush standard error. << val prerr_newline : unit -> unit >> Print a newline character on standard error, and flush standard error. Input functions on standard input --------------------------------- << val read_line : unit -> string >> Flush standard output, then read characters from standard input until a newline character is encountered. Return the string of all characters read, without the newline character at the end. << val read_int : unit -> int >> Flush standard output, then read one line from standard input and convert it to an integer. Raise Failure "int_of_string" if the line read is not a valid representation of an integer. << val read_int_opt : unit -> int option >> Same as read_int_opt, but returs None instead of raising. Since: 4.05 << val read_float : unit -> float >> Flush standard output, then read one line from standard input and convert it to a floating-point number. The result is unspecified if the line read is not a valid representation of a floating-point number. << val read_float_opt : unit -> float option >> Flush standard output, then read one line from standard input and convert it to a floating-point number. Returns None if the line read is not a valid representation of a floating-point number. Since: 4.05.0 General output functions ------------------------ << type open_flag = | Open_rdonly >> open for reading. << | Open_wronly >> open for writing. << | Open_append >> open for appending: always write at end of file. << | Open_creat >> create the file if it does not exist. << | Open_trunc >> empty the file if it already exists. << | Open_excl >> fail if Open_creat and the file already exists. << | Open_binary >> open in binary mode (no conversion). << | Open_text >> open in text mode (may perform conversions). << | Open_nonblock >> open in non-blocking mode. Opening modes for Pervasives.open_out_gen[23.2] and Pervasives.open_in_gen[23.2]. << val open_out : string -> out_channel >> Open the named file for writing, and return a new output channel on that file, positioned at the beginning of the file. The file is truncated to zero length if it already exists. It is created if it does not already exists. << val open_out_bin : string -> out_channel >> Same as Pervasives.open_out[23.2], but the file is opened in binary mode, so that no translation takes place during writes. On operating systems that do not distinguish between text mode and binary mode, this function behaves like Pervasives.open_out[23.2]. << val open_out_gen : open_flag list -> int -> string -> out_channel >> open_out_gen mode perm filename opens the named file for writing, as described above. The extra argument mode specifies the opening mode. The extra argument perm specifies the file permissions, in case the file must be created. Pervasives.open_out[23.2] and Pervasives.open_out_bin[23.2] are special cases of this function. << val flush : out_channel -> unit >> Flush the buffer associated with the given output channel, performing all pending writes on that channel. Interactive programs must be careful about flushing standard output and standard error at the right time. << val flush_all : unit -> unit >> Flush all open output channels; ignore errors. << val output_char : out_channel -> char -> unit >> Write the character on the given output channel. << val output_string : out_channel -> string -> unit >> Write the string on the given output channel. << val output_bytes : out_channel -> bytes -> unit >> Write the byte sequence on the given output channel. Since: 4.02.0 << val output : out_channel -> bytes -> int -> int -> unit >> output oc buf pos len writes len characters from byte sequence buf, starting at offset pos, to the given output channel oc. Raise Invalid_argument "output" if pos and len do not designate a valid range of buf. << val output_substring : out_channel -> string -> int -> int -> unit >> Same as output but take a string as argument instead of a byte sequence. Since: 4.02.0 << val output_byte : out_channel -> int -> unit >> Write one 8-bit integer (as the single character with that code) on the given output channel. The given integer is taken modulo 256. << val output_binary_int : out_channel -> int -> unit >> Write one integer in binary format (4 bytes, big-endian) on the given output channel. The given integer is taken modulo 2^32. The only reliable way to read it back is through the Pervasives.input_binary_int[23.2] function. The format is compatible across all machines for a given version of OCaml. << val output_value : out_channel -> 'a -> unit >> Write the representation of a structured value of any type to a channel. Circularities and sharing inside the value are detected and preserved. The object can be read back, by the function Pervasives.input_value[23.2]. See the description of module Marshal[24.24] for more information. Pervasives.output_value[23.2] is equivalent to Marshal.to_channel[24.24] with an empty list of flags. << val seek_out : out_channel -> int -> unit >> seek_out chan pos sets the current writing position to pos for channel chan. This works only for regular files. On files of other kinds (such as terminals, pipes and sockets), the behavior is unspecified. << val pos_out : out_channel -> int >> Return the current writing position for the given channel. Does not work on channels opened with the Open_append flag (returns unspecified results). << val out_channel_length : out_channel -> int >> Return the size (number of characters) of the regular file on which the given channel is opened. If the channel is opened on a file that is not a regular file, the result is meaningless. << val close_out : out_channel -> unit >> Close the given channel, flushing all buffered write operations. Output functions raise a Sys_error exception when they are applied to a closed output channel, except close_out and flush, which do nothing when applied to an already closed channel. Note that close_out may raise Sys_error if the operating system signals an error when flushing or closing. << val close_out_noerr : out_channel -> unit >> Same as close_out, but ignore all errors. << val set_binary_mode_out : out_channel -> bool -> unit >> set_binary_mode_out oc true sets the channel oc to binary mode: no translations take place during output. set_binary_mode_out oc false sets the channel oc to text mode: depending on the operating system, some translations may take place during output. For instance, under Windows, end-of-lines will be translated from \n to \r\n. This function has no effect under operating systems that do not distinguish between text mode and binary mode. General input functions ----------------------- << val open_in : string -> in_channel >> Open the named file for reading, and return a new input channel on that file, positioned at the beginning of the file. << val open_in_bin : string -> in_channel >> Same as Pervasives.open_in[23.2], but the file is opened in binary mode, so that no translation takes place during reads. On operating systems that do not distinguish between text mode and binary mode, this function behaves like Pervasives.open_in[23.2]. << val open_in_gen : open_flag list -> int -> string -> in_channel >> open_in_gen mode perm filename opens the named file for reading, as described above. The extra arguments mode and perm specify the opening mode and file permissions. Pervasives.open_in[23.2] and Pervasives.open_in_bin[23.2] are special cases of this function. << val input_char : in_channel -> char >> Read one character from the given input channel. Raise End_of_file if there are no more characters to read. << val input_line : in_channel -> string >> Read characters from the given input channel, until a newline character is encountered. Return the string of all characters read, without the newline character at the end. Raise End_of_file if the end of the file is reached at the beginning of line. << val input : in_channel -> bytes -> int -> int -> int >> input ic buf pos len reads up to len characters from the given channel ic, storing them in byte sequence buf, starting at character number pos. It returns the actual number of characters read, between 0 and len (inclusive). A return value of 0 means that the end of file was reached. A return value between 0 and len exclusive means that not all requested len characters were read, either because no more characters were available at that time, or because the implementation found it convenient to do a partial read; input must be called again to read the remaining characters, if desired. (See also Pervasives.really_input[23.2] for reading exactly len characters.) Exception Invalid_argument "input" is raised if pos and len do not designate a valid range of buf. << val really_input : in_channel -> bytes -> int -> int -> unit >> really_input ic buf pos len reads len characters from channel ic, storing them in byte sequence buf, starting at character number pos. Raise End_of_file if the end of file is reached before len characters have been read. Raise Invalid_argument "really_input" if pos and len do not designate a valid range of buf. << val really_input_string : in_channel -> int -> string >> really_input_string ic len reads len characters from channel ic and returns them in a new string. Raise End_of_file if the end of file is reached before len characters have been read. Since: 4.02.0 << val input_byte : in_channel -> int >> Same as Pervasives.input_char[23.2], but return the 8-bit integer representing the character. Raise End_of_file if an end of file was reached. << val input_binary_int : in_channel -> int >> Read an integer encoded in binary format (4 bytes, big-endian) from the given input channel. See Pervasives.output_binary_int[23.2]. Raise End_of_file if an end of file was reached while reading the integer. << val input_value : in_channel -> 'a >> Read the representation of a structured value, as produced by Pervasives.output_value[23.2], and return the corresponding value. This function is identical to Marshal.from_channel[24.24]; see the description of module Marshal[24.24] for more information, in particular concerning the lack of type safety. << val seek_in : in_channel -> int -> unit >> seek_in chan pos sets the current reading position to pos for channel chan. This works only for regular files. On files of other kinds, the behavior is unspecified. << val pos_in : in_channel -> int >> Return the current reading position for the given channel. << val in_channel_length : in_channel -> int >> Return the size (number of characters) of the regular file on which the given channel is opened. If the channel is opened on a file that is not a regular file, the result is meaningless. The returned size does not take into account the end-of-line translations that can be performed when reading from a channel opened in text mode. << val close_in : in_channel -> unit >> Close the given channel. Input functions raise a Sys_error exception when they are applied to a closed input channel, except close_in, which does nothing when applied to an already closed channel. << val close_in_noerr : in_channel -> unit >> Same as close_in, but ignore all errors. << val set_binary_mode_in : in_channel -> bool -> unit >> set_binary_mode_in ic true sets the channel ic to binary mode: no translations take place during input. set_binary_mode_out ic false sets the channel ic to text mode: depending on the operating system, some translations may take place during input. For instance, under Windows, end-of-lines will be translated from \r\n to \n. This function has no effect under operating systems that do not distinguish between text mode and binary mode. Operations on large files ------------------------- << module LargeFile : >> sig << val seek_out : Pervasives.out_channel -> int64 -> unit >> << val pos_out : Pervasives.out_channel -> int64 >> << val out_channel_length : Pervasives.out_channel -> int64 >> << val seek_in : Pervasives.in_channel -> int64 -> unit >> << val pos_in : Pervasives.in_channel -> int64 >> << val in_channel_length : Pervasives.in_channel -> int64 >> end Operations on large files. This sub-module provides 64-bit variants of the channel functions that manipulate file positions and file sizes. By representing positions and sizes by 64-bit integers (type int64) instead of regular integers (type int), these alternate functions allow operating on files whose sizes are greater than max_int. References ========== << type 'a ref = { mutable contents : 'a ; } >> The type of references (mutable indirection cells) containing a value of type 'a. << val ref : 'a -> 'a ref >> Return a fresh reference containing the given value. << val (!) : 'a ref -> 'a >> !r returns the current contents of reference r. Equivalent to fun r -> r.contents. << val (:=) : 'a ref -> 'a -> unit >> r := a stores the value of a in reference r. Equivalent to fun r v -> r.contents <- v. << val incr : int ref -> unit >> Increment the integer contained in the given reference. Equivalent to fun r -> r := succ !r. << val decr : int ref -> unit >> Decrement the integer contained in the given reference. Equivalent to fun r -> r := pred !r. Result type =========== << type ('a, 'b) result = | Ok of 'a | Error of 'b >> Since: 4.03.0 Operations on format strings ============================ Format strings are character strings with special lexical conventions that defines the functionality of formatted input/output functions. Format strings are used to read data with formatted input functions from module Scanf[24.33] and to print data with formatted output functions from modules Printf[24.30] and Format[24.13]. Format strings are made of three kinds of entities: - conversions specifications, introduced by the special character '%' followed by one or more characters specifying what kind of argument to read or print, - formatting indications, introduced by the special character '@' followed by one or more characters specifying how to read or print the argument, - plain characters that are regular characters with usual lexical conventions. Plain characters specify string literals to be read in the input or printed in the output. There is an additional lexical rule to escape the special characters '%' and '@' in format strings: if a special character follows a '%' character, it is treated as a plain character. In other words, "%%" is considered as a plain '%' and "%@" as a plain '@'. For more information about conversion specifications and formatting indications available, read the documentation of modules Scanf[24.33], Printf[24.30] and Format[24.13]. Format strings have a general and highly polymorphic type ('a, 'b, 'c, 'd, 'e, 'f) format6. The two simplified types, format and format4 below are included for backward compatibility with earlier releases of OCaml. The meaning of format string type parameters is as follows: - 'a is the type of the parameters of the format for formatted output functions (printf-style functions); 'a is the type of the values read by the format for formatted input functions (scanf-style functions). - 'b is the type of input source for formatted input functions and the type of output target for formatted output functions. For printf-style functions from module Printf[24.30], 'b is typically out_channel; for printf-style functions from module Format[24.13], 'b is typically Format.formatter[24.13]; for scanf-style functions from module Scanf[24.33], 'b is typically Scanf.Scanning.in_channel[24.33]. Type argument 'b is also the type of the first argument given to user's defined printing functions for %a and %t conversions, and user's defined reading functions for %r conversion. - 'c is the type of the result of the %a and %t printing functions, and also the type of the argument transmitted to the first argument of kprintf-style functions or to the kscanf-style functions. - 'd is the type of parameters for the scanf-style functions. - 'e is the type of the receiver function for the scanf-style functions. - 'f is the final result type of a formatted input/output function invocation: for the printf-style functions, it is typically unit; for the scanf-style functions, it is typically the result type of the receiver function. << type ('a, 'b, 'c, 'd, 'e, 'f) format6 = ('a, 'b, 'c, 'd, 'e, 'f) CamlinternalFormatBasics.format6 >> << type ('a, 'b, 'c, 'd) format4 = ('a, 'b, 'c, 'c, 'c, 'd) format6 >> << type ('a, 'b, 'c) format = ('a, 'b, 'c, 'c) format4 >> << val string_of_format : ('a, 'b, 'c, 'd, 'e, 'f) format6 -> string >> Converts a format string into a string. << val format_of_string : ('a, 'b, 'c, 'd, 'e, 'f) format6 -> ('a, 'b, 'c, 'd, 'e, 'f) format6 >> format_of_string s returns a format string read from the string literal s. Note: format_of_string can not convert a string argument that is not a literal. If you need this functionality, use the more general Scanf.format_from_string[24.33] function. << val (^^) : ('a, 'b, 'c, 'd, 'e, 'f) format6 -> ('f, 'b, 'c, 'e, 'g, 'h) format6 -> ('a, 'b, 'c, 'd, 'g, 'h) format6 >> f1 ^^ f2 catenates format strings f1 and f2. The result is a format string that behaves as the concatenation of format strings f1 and f2: in case of formatted output, it accepts arguments from f1, then arguments from f2; in case of formatted input, it returns results from f1, then results from f2. Program termination =================== << val exit : int -> 'a >> Terminate the process, returning the given status code to the operating system: usually 0 to indicate no errors, and a small positive integer to indicate failure. All open output channels are flushed with flush_all. An implicit exit 0 is performed each time a program terminates normally. An implicit exit 2 is performed if the program terminates early because of an uncaught exception. << val at_exit : (unit -> unit) -> unit >> Register the given function to be called at program termination time. The functions registered with at_exit will be called when the program executes Pervasives.exit[23.2], or terminates, either normally or because of an uncaught exception. The functions are called in 'last in, first out' order: the function most recently added with at_exit is called first. Chapter 24 The standard library ********************************** This chapter describes the functions provided by the OCaml standard library. The modules from the standard library are automatically linked with the user's object code files by the ocamlc command. Hence, these modules can be used in standalone programs without having to add any .cmo file on the command line for the linking phase. Similarly, in interactive use, these globals can be used in toplevel phrases without having to load any .cmo file in memory. Unlike the Pervasives module from the core library, the modules from the standard library are not automatically "opened" when a compilation starts, or when the toplevel system is launched. Hence it is necessary to use qualified identifiers to refer to the functions provided by these modules, or to add open directives. Conventions *=*=*=*=*=* For easy reference, the modules are listed below in alphabetical order of module names. For each module, the declarations from its signature are printed one by one in typewriter font, followed by a short comment. All modules and the identifiers they export are indexed at the end of this report. 24.1 Module Arg : Parsing of command line arguments. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* This module provides a general mechanism for extracting options and arguments from the command line to the program. Syntax of command lines: A keyword is a character string starting with a -. An option is a keyword alone or followed by an argument. The types of keywords are: Unit, Bool, Set, Clear, String, Set_string, Int, Set_int, Float, Set_float, Tuple, Symbol, and Rest. Unit, Set and Clear keywords take no argument. A Rest keyword takes the remaining of the command line as arguments. Every other keyword takes the following word on the command line as argument. For compatibility with GNU getopt_long, keyword=arg is also allowed. Arguments not preceded by a keyword are called anonymous arguments. Examples (cmd is assumed to be the command name): - cmd -flag (a unit option) - cmd -int 1 (an int option with argument 1) - cmd -string foobar (a string option with argument "foobar") - cmd -float 12.34 (a float option with argument 12.34) - cmd a b c (three anonymous arguments: "a", "b", and "c") - cmd a b -- c d (two anonymous arguments and a rest option with two arguments) << type spec = | Unit of (unit -> unit) >> Call the function with unit argument << | Bool of (bool -> unit) >> Call the function with a bool argument << | Set of bool Pervasives.ref >> Set the reference to true << | Clear of bool Pervasives.ref >> Set the reference to false << | String of (string -> unit) >> Call the function with a string argument << | Set_string of string Pervasives.ref >> Set the reference to the string argument << | Int of (int -> unit) >> Call the function with an int argument << | Set_int of int Pervasives.ref >> Set the reference to the int argument << | Float of (float -> unit) >> Call the function with a float argument << | Set_float of float Pervasives.ref >> Set the reference to the float argument << | Tuple of spec list >> Take several arguments according to the spec list << | Symbol of string list * (string -> unit) >> Take one of the symbols as argument and call the function with the symbol << | Rest of (string -> unit) >> Stop interpreting keywords and call the function with each remaining argument << | Expand of (string -> string array) >> If the remaining arguments to process are of the form ["-foo"; "arg"] @ rest where "foo" is registered as Expand f, then the arguments f "arg" @ rest are processed. Only allowed in parse_and_expand_argv_dynamic. The concrete type describing the behavior associated with a keyword. << type key = string >> << type doc = string >> << type usage_msg = string >> << type anon_fun = string -> unit >> << val parse : (key * spec * doc) list -> anon_fun -> usage_msg -> unit >> Arg.parse speclist anon_fun usage_msg parses the command line. speclist is a list of triples (key, spec, doc). key is the option keyword, it must start with a '-' character. spec gives the option type and the function to call when this option is found on the command line. doc is a one-line description of this option. anon_fun is called on anonymous arguments. The functions in spec and anon_fun are called in the same order as their arguments appear on the command line. If an error occurs, Arg.parse exits the program, after printing to standard error an error message as follows: - The reason for the error: unknown option, invalid or missing argument, etc. - usage_msg - The list of options, each followed by the corresponding doc string. Beware: options that have an empty doc string will not be included in the list. For the user to be able to specify anonymous arguments starting with a -, include for example ("-", String anon_fun, doc) in speclist. By default, parse recognizes two unit options, -help and --help, which will print to standard output usage_msg and the list of options, and exit the program. You can override this behaviour by specifying your own -help and --help options in speclist. << val parse_dynamic : (key * spec * doc) list Pervasives.ref -> anon_fun -> usage_msg -> unit >> Same as Arg.parse[24.1], except that the speclist argument is a reference and may be updated during the parsing. A typical use for this feature is to parse command lines of the form: - command subcommand options where the list of options depends on the value of the subcommand argument. Since: 4.01.0 << val parse_argv : ?current:int Pervasives.ref -> string array -> (key * spec * doc) list -> anon_fun -> usage_msg -> unit >> Arg.parse_argv ~current args speclist anon_fun usage_msg parses the array args as if it were the command line. It uses and updates the value of ~current (if given), or Arg.current[24.1]. You must set it before calling parse_argv. The initial value of current is the index of the program name (argument 0) in the array. If an error occurs, Arg.parse_argv raises Arg.Bad[??] with the error message as argument. If option -help or --help is given, Arg.parse_argv raises Arg.Help[??] with the help message as argument. << val parse_argv_dynamic : ?current:int Pervasives.ref -> string array -> (key * spec * doc) list Pervasives.ref -> anon_fun -> string -> unit >> Same as Arg.parse_argv[24.1], except that the speclist argument is a reference and may be updated during the parsing. See Arg.parse_dynamic[24.1]. Since: 4.01.0 << val parse_and_expand_argv_dynamic : int Pervasives.ref -> string array Pervasives.ref -> (key * spec * doc) list Pervasives.ref -> anon_fun -> string -> unit >> Same as Arg.parse_argv_dynamic[24.1], except that the argv argument is a reference and may be updated during the parsing of Expand arguments. See Arg.parse_argv_dynamic[24.1]. Since: 4.05.0 << val parse_expand : (key * spec * doc) list -> anon_fun -> usage_msg -> unit >> Same as Arg.parse[24.1], except that the Expand arguments are allowed and the Arg.current[24.1] reference is not updated. Since: 4.05.0 << exception Help of string >> Raised by Arg.parse_argv when the user asks for help. << exception Bad of string >> Functions in spec or anon_fun can raise Arg.Bad with an error message to reject invalid arguments. Arg.Bad is also raised by Arg.parse_argv[24.1] in case of an error. << val usage : (key * spec * doc) list -> usage_msg -> unit >> Arg.usage speclist usage_msg prints to standard error an error message that includes the list of valid options. This is the same message that Arg.parse[24.1] prints in case of error. speclist and usage_msg are the same as for Arg.parse[24.1]. << val usage_string : (key * spec * doc) list -> usage_msg -> string >> Returns the message that would have been printed by Arg.usage[24.1], if provided with the same parameters. << val align : ?limit:int -> (key * spec * doc) list -> (key * spec * doc) list >> Align the documentation strings by inserting spaces at the first space, according to the length of the keyword. Use a space as the first character in a doc string if you want to align the whole string. The doc strings corresponding to Symbol arguments are aligned on the next line. << val current : int Pervasives.ref >> Position (in Sys.argv[25.10]) of the argument being processed. You can change this value, e.g. to force Arg.parse[24.1] to skip some arguments. Arg.parse[24.1] uses the initial value of Arg.current[24.1] as the index of argument 0 (the program name) and starts parsing arguments at the next element. << val read_arg : string -> string array >> Arg.read_arg file reads newline-terminated command line arguments from file file. Since: 4.05.0 << val read_arg0 : string -> string array >> Identical to Arg.read_arg[24.1] but assumes null character terminated command line arguments. Since: 4.05.0 << val write_arg : string -> string array -> unit >> Arg.write_arg file args writes the arguments args newline-terminated into the file file. If the any of the arguments in args contains a newline, use Arg.write_arg0[24.1] instead. Since: 4.05.0 << val write_arg0 : string -> string array -> unit >> Identical to Arg.write_arg[24.1] but uses the null character for terminator instead of newline. Since: 4.05.0 24.2 Module Array : Array operations. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= << val length : 'a array -> int >> Return the length (number of elements) of the given array. << val get : 'a array -> int -> 'a >> Array.get a n returns the element number n of array a. The first element has number 0. The last element has number Array.length a - 1. You can also write a.(n) instead of Array.get a n. Raise Invalid_argument "index out of bounds" if n is outside the range 0 to (Array.length a - 1). << val set : 'a array -> int -> 'a -> unit >> Array.set a n x modifies array a in place, replacing element number n with x. You can also write a.(n) <- x instead of Array.set a n x. Raise Invalid_argument "index out of bounds" if n is outside the range 0 to Array.length a - 1. << val make : int -> 'a -> 'a array >> Array.make n x returns a fresh array of length n, initialized with x. All the elements of this new array are initially physically equal to x (in the sense of the == predicate). Consequently, if x is mutable, it is shared among all elements of the array, and modifying x through one of the array entries will modify all other entries at the same time. Raise Invalid_argument if n < 0 or n > Sys.max_array_length. If the value of x is a floating-point number, then the maximum size is only Sys.max_array_length / 2. << val create : int -> 'a -> 'a array >> Deprecated. Array.create is an alias for Array.make[24.2]. << val create_float : int -> float array >> Array.create_float n returns a fresh float array of length n, with uninitialized data. Since: 4.03 << val make_float : int -> float array >> Deprecated. Array.make_float is an alias for Array.create_float[24.2]. << val init : int -> (int -> 'a) -> 'a array >> Array.init n f returns a fresh array of length n, with element number i initialized to the result of f i. In other terms, Array.init n f tabulates the results of f applied to the integers 0 to n-1. Raise Invalid_argument if n < 0 or n > Sys.max_array_length. If the return type of f is float, then the maximum size is only Sys.max_array_length / 2. << val make_matrix : int -> int -> 'a -> 'a array array >> Array.make_matrix dimx dimy e returns a two-dimensional array (an array of arrays) with first dimension dimx and second dimension dimy. All the elements of this new matrix are initially physically equal to e. The element (x,y) of a matrix m is accessed with the notation m.(x).(y). Raise Invalid_argument if dimx or dimy is negative or greater than Sys.max_array_length[25.10]. If the value of e is a floating-point number, then the maximum size is only Sys.max_array_length / 2. << val create_matrix : int -> int -> 'a -> 'a array array >> Deprecated. Array.create_matrix is an alias for Array.make_matrix[24.2]. << val append : 'a array -> 'a array -> 'a array >> Array.append v1 v2 returns a fresh array containing the concatenation of the arrays v1 and v2. << val concat : 'a array list -> 'a array >> Same as Array.append[24.2], but concatenates a list of arrays. << val sub : 'a array -> int -> int -> 'a array >> Array.sub a start len returns a fresh array of length len, containing the elements number start to start + len - 1 of array a. Raise Invalid_argument "Array.sub" if start and len do not designate a valid subarray of a; that is, if start < 0, or len < 0, or start + len > Array.length a. << val copy : 'a array -> 'a array >> Array.copy a returns a copy of a, that is, a fresh array containing the same elements as a. << val fill : 'a array -> int -> int -> 'a -> unit >> Array.fill a ofs len x modifies the array a in place, storing x in elements number ofs to ofs + len - 1. Raise Invalid_argument "Array.fill" if ofs and len do not designate a valid subarray of a. << val blit : 'a array -> int -> 'a array -> int -> int -> unit >> Array.blit v1 o1 v2 o2 len copies len elements from array v1, starting at element number o1, to array v2, starting at element number o2. It works correctly even if v1 and v2 are the same array, and the source and destination chunks overlap. Raise Invalid_argument "Array.blit" if o1 and len do not designate a valid subarray of v1, or if o2 and len do not designate a valid subarray of v2. << val to_list : 'a array -> 'a list >> Array.to_list a returns the list of all the elements of a. << val of_list : 'a list -> 'a array >> Array.of_list l returns a fresh array containing the elements of l. Iterators ========= << val iter : ('a -> unit) -> 'a array -> unit >> Array.iter f a applies function f in turn to all the elements of a. It is equivalent to f a.(0); f a.(1); ...; f a.(Array.length a - 1); (). << val iteri : (int -> 'a -> unit) -> 'a array -> unit >> Same as Array.iter[24.2], but the function is applied with the index of the element as first argument, and the element itself as second argument. << val map : ('a -> 'b) -> 'a array -> 'b array >> Array.map f a applies function f to all the elements of a, and builds an array with the results returned by f: [| f a.(0); f a.(1); ...; f a.(Array.length a - 1) |]. << val mapi : (int -> 'a -> 'b) -> 'a array -> 'b array >> Same as Array.map[24.2], but the function is applied to the index of the element as first argument, and the element itself as second argument. << val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b array -> 'a >> Array.fold_left f x a computes f (... (f (f x a.(0)) a.(1)) ...) a.(n-1), where n is the length of the array a. << val fold_right : ('b -> 'a -> 'a) -> 'b array -> 'a -> 'a >> Array.fold_right f a x computes f a.(0) (f a.(1) ( ... (f a.(n-1) x) ...)), where n is the length of the array a. Iterators on two arrays ======================= << val iter2 : ('a -> 'b -> unit) -> 'a array -> 'b array -> unit >> Array.iter2 f a b applies function f to all the elements of a and b. Raise Invalid_argument if the arrays are not the same size. Since: 4.03.0 << val map2 : ('a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array >> Array.map2 f a b applies function f to all the elements of a and b, and builds an array with the results returned by f: [| f a.(0) b.(0); ...; f a.(Array.length a - 1) b.(Array.length b - 1)|]. Raise Invalid_argument if the arrays are not the same size. Since: 4.03.0 Array scanning ============== << val for_all : ('a -> bool) -> 'a array -> bool >> Array.for_all p [|a1; ...; an|] checks if all elements of the array satisfy the predicate p. That is, it returns (p a1) && (p a2) && ... && (p an). Since: 4.03.0 << val exists : ('a -> bool) -> 'a array -> bool >> Array.exists p [|a1; ...; an|] checks if at least one element of the array satisfies the predicate p. That is, it returns (p a1) || (p a2) || ... || (p an). Since: 4.03.0 << val mem : 'a -> 'a array -> bool >> mem a l is true if and only if a is equal to an element of l. Since: 4.03.0 << val memq : 'a -> 'a array -> bool >> Same as Array.mem[24.2], but uses physical equality instead of structural equality to compare array elements. Since: 4.03.0 Sorting ======= << val sort : ('a -> 'a -> int) -> 'a array -> unit >> Sort an array in increasing order according to a comparison function. The comparison function must return 0 if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller (see below for a complete specification). For example, Pervasives.compare[23.2] is a suitable comparison function, provided there are no floating-point NaN values in the data. After calling Array.sort, the array is sorted in place in increasing order. Array.sort is guaranteed to run in constant heap space and (at most) logarithmic stack space. The current implementation uses Heap Sort. It runs in constant stack space. Specification of the comparison function: Let a be the array and cmp the comparison function. The following must be true for all x, y, z in a : - cmp x y > 0 if and only if cmp y x < 0 - if cmp x y >= 0 and cmp y z >= 0 then cmp x z >= 0 When Array.sort returns, a contains the same elements as before, reordered in such a way that for all i and j valid indices of a : - cmp a.(i) a.(j) >= 0 if and only if i >= j << val stable_sort : ('a -> 'a -> int) -> 'a array -> unit >> Same as Array.sort[24.2], but the sorting algorithm is stable (i.e. elements that compare equal are kept in their original order) and not guaranteed to run in constant heap space. The current implementation uses Merge Sort. It uses n/2 words of heap space, where n is the length of the array. It is usually faster than the current implementation of Array.sort[24.2]. << val fast_sort : ('a -> 'a -> int) -> 'a array -> unit >> Same as Array.sort[24.2] or Array.stable_sort[24.2], whichever is faster on typical input. 24.3 Module ArrayLabels : Array operations. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= << val length : 'a array -> int >> Return the length (number of elements) of the given array. << val get : 'a array -> int -> 'a >> Array.get a n returns the element number n of array a. The first element has number 0. The last element has number Array.length a - 1. You can also write a.(n) instead of Array.get a n. Raise Invalid_argument "index out of bounds" if n is outside the range 0 to (Array.length a - 1). << val set : 'a array -> int -> 'a -> unit >> Array.set a n x modifies array a in place, replacing element number n with x. You can also write a.(n) <- x instead of Array.set a n x. Raise Invalid_argument "index out of bounds" if n is outside the range 0 to Array.length a - 1. << val make : int -> 'a -> 'a array >> Array.make n x returns a fresh array of length n, initialized with x. All the elements of this new array are initially physically equal to x (in the sense of the == predicate). Consequently, if x is mutable, it is shared among all elements of the array, and modifying x through one of the array entries will modify all other entries at the same time. Raise Invalid_argument if n < 0 or n > Sys.max_array_length. If the value of x is a floating-point number, then the maximum size is only Sys.max_array_length / 2. << val create : int -> 'a -> 'a array >> Deprecated. Array.create is an alias for Array.make[24.2]. << val init : int -> f:(int -> 'a) -> 'a array >> Array.init n f returns a fresh array of length n, with element number i initialized to the result of f i. In other terms, Array.init n f tabulates the results of f applied to the integers 0 to n-1. Raise Invalid_argument if n < 0 or n > Sys.max_array_length. If the return type of f is float, then the maximum size is only Sys.max_array_length / 2. << val make_matrix : dimx:int -> dimy:int -> 'a -> 'a array array >> Array.make_matrix dimx dimy e returns a two-dimensional array (an array of arrays) with first dimension dimx and second dimension dimy. All the elements of this new matrix are initially physically equal to e. The element (x,y) of a matrix m is accessed with the notation m.(x).(y). Raise Invalid_argument if dimx or dimy is negative or greater than Sys.max_array_length[25.10]. If the value of e is a floating-point number, then the maximum size is only Sys.max_array_length / 2. << val create_matrix : dimx:int -> dimy:int -> 'a -> 'a array array >> Deprecated. Array.create_matrix is an alias for Array.make_matrix[24.2]. << val append : 'a array -> 'a array -> 'a array >> Array.append v1 v2 returns a fresh array containing the concatenation of the arrays v1 and v2. << val concat : 'a array list -> 'a array >> Same as Array.append[24.2], but concatenates a list of arrays. << val sub : 'a array -> pos:int -> len:int -> 'a array >> Array.sub a start len returns a fresh array of length len, containing the elements number start to start + len - 1 of array a. Raise Invalid_argument "Array.sub" if start and len do not designate a valid subarray of a; that is, if start < 0, or len < 0, or start + len > Array.length a. << val copy : 'a array -> 'a array >> Array.copy a returns a copy of a, that is, a fresh array containing the same elements as a. << val fill : 'a array -> pos:int -> len:int -> 'a -> unit >> Array.fill a ofs len x modifies the array a in place, storing x in elements number ofs to ofs + len - 1. Raise Invalid_argument "Array.fill" if ofs and len do not designate a valid subarray of a. << val blit : src:'a array -> src_pos:int -> dst:'a array -> dst_pos:int -> len:int -> unit >> Array.blit v1 o1 v2 o2 len copies len elements from array v1, starting at element number o1, to array v2, starting at element number o2. It works correctly even if v1 and v2 are the same array, and the source and destination chunks overlap. Raise Invalid_argument "Array.blit" if o1 and len do not designate a valid subarray of v1, or if o2 and len do not designate a valid subarray of v2. << val to_list : 'a array -> 'a list >> Array.to_list a returns the list of all the elements of a. << val of_list : 'a list -> 'a array >> Array.of_list l returns a fresh array containing the elements of l. << val iter : f:('a -> unit) -> 'a array -> unit >> Array.iter f a applies function f in turn to all the elements of a. It is equivalent to f a.(0); f a.(1); ...; f a.(Array.length a - 1); (). << val map : f:('a -> 'b) -> 'a array -> 'b array >> Array.map f a applies function f to all the elements of a, and builds an array with the results returned by f: [| f a.(0); f a.(1); ...; f a.(Array.length a - 1) |]. << val iteri : f:(int -> 'a -> unit) -> 'a array -> unit >> Same as Array.iter[24.2], but the function is applied to the index of the element as first argument, and the element itself as second argument. << val mapi : f:(int -> 'a -> 'b) -> 'a array -> 'b array >> Same as Array.map[24.2], but the function is applied to the index of the element as first argument, and the element itself as second argument. << val fold_left : f:('a -> 'b -> 'a) -> init:'a -> 'b array -> 'a >> Array.fold_left f x a computes f (... (f (f x a.(0)) a.(1)) ...) a.(n-1), where n is the length of the array a. << val fold_right : f:('b -> 'a -> 'a) -> 'b array -> init:'a -> 'a >> Array.fold_right f a x computes f a.(0) (f a.(1) ( ... (f a.(n-1) x) ...)), where n is the length of the array a. Iterators on two arrays ======================= << val iter2 : f:('a -> 'b -> unit) -> 'a array -> 'b array -> unit >> Array.iter2 f a b applies function f to all the elements of a and b. Raise Invalid_argument if the arrays are not the same size. Since: 4.05.0 << val map2 : f:('a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array >> Array.map2 f a b applies function f to all the elements of a and b, and builds an array with the results returned by f: [| f a.(0) b.(0); ...; f a.(Array.length a - 1) b.(Array.length b - 1)|]. Raise Invalid_argument if the arrays are not the same size. Since: 4.05.0 Array scanning ============== << val exists : f:('a -> bool) -> 'a array -> bool >> Array.exists p [|a1; ...; an|] checks if at least one element of the array satisfies the predicate p. That is, it returns (p a1) || (p a2) || ... || (p an). Since: 4.03.0 << val for_all : f:('a -> bool) -> 'a array -> bool >> Array.for_all p [|a1; ...; an|] checks if all elements of the array satisfy the predicate p. That is, it returns (p a1) && (p a2) && ... && (p an). Since: 4.03.0 << val mem : 'a -> set:'a array -> bool >> mem x a is true if and only if x is equal to an element of a. Since: 4.03.0 << val memq : 'a -> set:'a array -> bool >> Same as Array.mem[24.2], but uses physical equality instead of structural equality to compare list elements. Since: 4.03.0 << val create_float : int -> float array >> Array.create_float n returns a fresh float array of length n, with uninitialized data. Since: 4.03 << val make_float : int -> float array >> Deprecated. Array.make_float is an alias for Array.create_float[24.2]. Sorting ======= << val sort : cmp:('a -> 'a -> int) -> 'a array -> unit >> Sort an array in increasing order according to a comparison function. The comparison function must return 0 if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller (see below for a complete specification). For example, Pervasives.compare[23.2] is a suitable comparison function, provided there are no floating-point NaN values in the data. After calling Array.sort, the array is sorted in place in increasing order. Array.sort is guaranteed to run in constant heap space and (at most) logarithmic stack space. The current implementation uses Heap Sort. It runs in constant stack space. Specification of the comparison function: Let a be the array and cmp the comparison function. The following must be true for all x, y, z in a : - cmp x y > 0 if and only if cmp y x < 0 - if cmp x y >= 0 and cmp y z >= 0 then cmp x z >= 0 When Array.sort returns, a contains the same elements as before, reordered in such a way that for all i and j valid indices of a : - cmp a.(i) a.(j) >= 0 if and only if i >= j << val stable_sort : cmp:('a -> 'a -> int) -> 'a array -> unit >> Same as Array.sort[24.2], but the sorting algorithm is stable (i.e. elements that compare equal are kept in their original order) and not guaranteed to run in constant heap space. The current implementation uses Merge Sort. It uses n/2 words of heap space, where n is the length of the array. It is usually faster than the current implementation of Array.sort[24.2]. << val fast_sort : cmp:('a -> 'a -> int) -> 'a array -> unit >> Same as Array.sort[24.2] or Array.stable_sort[24.2], whichever is faster on typical input. 24.4 Module Buffer : Extensible buffers. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* This module implements buffers that automatically expand as necessary. It provides accumulative concatenation of strings in quasi-linear time (instead of quadratic time when strings are concatenated pairwise). << type t >> The abstract type of buffers. << val create : int -> t >> create n returns a fresh buffer, initially empty. The n parameter is the initial size of the internal byte sequence that holds the buffer contents. That byte sequence is automatically reallocated when more than n characters are stored in the buffer, but shrinks back to n characters when reset is called. For best performance, n should be of the same order of magnitude as the number of characters that are expected to be stored in the buffer (for instance, 80 for a buffer that holds one output line). Nothing bad will happen if the buffer grows beyond that limit, however. In doubt, take n = 16 for instance. If n is not between 1 and Sys.max_string_length[25.10], it will be clipped to that interval. << val contents : t -> string >> Return a copy of the current contents of the buffer. The buffer itself is unchanged. << val to_bytes : t -> bytes >> Return a copy of the current contents of the buffer. The buffer itself is unchanged. Since: 4.02 << val sub : t -> int -> int -> string >> Buffer.sub b off len returns a copy of len bytes from the current contents of the buffer b, starting at offset off. Raise Invalid_argument if srcoff and len do not designate a valid range of b. << val blit : t -> int -> bytes -> int -> int -> unit >> Buffer.blit src srcoff dst dstoff len copies len characters from the current contents of the buffer src, starting at offset srcoff to dst, starting at character dstoff. Raise Invalid_argument if srcoff and len do not designate a valid range of src, or if dstoff and len do not designate a valid range of dst. Since: 3.11.2 << val nth : t -> int -> char >> Get the n-th character of the buffer. Raise Invalid_argument if index out of bounds << val length : t -> int >> Return the number of characters currently contained in the buffer. << val clear : t -> unit >> Empty the buffer. << val reset : t -> unit >> Empty the buffer and deallocate the internal byte sequence holding the buffer contents, replacing it with the initial internal byte sequence of length n that was allocated by Buffer.create[24.4] n. For long-lived buffers that may have grown a lot, reset allows faster reclamation of the space used by the buffer. << val add_char : t -> char -> unit >> add_char b c appends the character c at the end of buffer b. << val add_string : t -> string -> unit >> add_string b s appends the string s at the end of buffer b. << val add_bytes : t -> bytes -> unit >> add_bytes b s appends the byte sequence s at the end of buffer b. Since: 4.02 << val add_substring : t -> string -> int -> int -> unit >> add_substring b s ofs len takes len characters from offset ofs in string s and appends them at the end of buffer b. << val add_subbytes : t -> bytes -> int -> int -> unit >> add_subbytes b s ofs len takes len characters from offset ofs in byte sequence s and appends them at the end of buffer b. Since: 4.02 << val add_substitute : t -> (string -> string) -> string -> unit >> add_substitute b f s appends the string pattern s at the end of buffer b with substitution. The substitution process looks for variables into the pattern and substitutes each variable name by its value, as obtained by applying the mapping f to the variable name. Inside the string pattern, a variable name immediately follows a non-escaped $ character and is one of the following: - a non empty sequence of alphanumeric or _ characters, - an arbitrary sequence of characters enclosed by a pair of matching parentheses or curly brackets. An escaped $ character is a $ that immediately follows a backslash character; it then stands for a plain $. Raise Not_found if the closing character of a parenthesized variable cannot be found. << val add_buffer : t -> t -> unit >> add_buffer b1 b2 appends the current contents of buffer b2 at the end of buffer b1. b2 is not modified. << val add_channel : t -> Pervasives.in_channel -> int -> unit >> add_channel b ic n reads at most n characters from the input channel ic and stores them at the end of buffer b. Raise End_of_file if the channel contains fewer than n characters. In this case, the characters are still added to the buffer, so as to avoid loss of data. << val output_buffer : Pervasives.out_channel -> t -> unit >> output_buffer oc b writes the current contents of buffer b on the output channel oc. << val truncate : t -> int -> unit >> truncate b len truncates the length of b to len Note: the internal byte sequence is not shortened. Raise Invalid_argument if len < 0 or len > length b. Since: 4.05.0 24.5 Module Bytes : Byte sequence operations. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= A byte sequence is a mutable data structure that contains a fixed-length sequence of bytes. Each byte can be indexed in constant time for reading or writing. Given a byte sequence s of length l, we can access each of the l bytes of s via its index in the sequence. Indexes start at 0, and we will call an index valid in s if it falls within the range [0...l-1] (inclusive). A position is the point between two bytes or at the beginning or end of the sequence. We call a position valid in s if it falls within the range [0...l] (inclusive). Note that the byte at index n is between positions n and n+1. Two parameters start and len are said to designate a valid range of s if len >= 0 and start and start+len are valid positions in s. Byte sequences can be modified in place, for instance via the set and blit functions described below. See also strings (module String[25.8]), which are almost the same data structure, but cannot be modified in place. Bytes are represented by the OCaml type char. Since: 4.02.0 << val length : bytes -> int >> Return the length (number of bytes) of the argument. << val get : bytes -> int -> char >> get s n returns the byte at index n in argument s. Raise Invalid_argument if n is not a valid index in s. << val set : bytes -> int -> char -> unit >> set s n c modifies s in place, replacing the byte at index n with c. Raise Invalid_argument if n is not a valid index in s. << val create : int -> bytes >> create n returns a new byte sequence of length n. The sequence is uninitialized and contains arbitrary bytes. Raise Invalid_argument if n < 0 or n > Sys.max_string_length[25.10]. << val make : int -> char -> bytes >> make n c returns a new byte sequence of length n, filled with the byte c. Raise Invalid_argument if n < 0 or n > Sys.max_string_length[25.10]. << val init : int -> (int -> char) -> bytes >> Bytes.init n f returns a fresh byte sequence of length n, with character i initialized to the result of f i (in increasing index order). Raise Invalid_argument if n < 0 or n > Sys.max_string_length[25.10]. << val empty : bytes >> A byte sequence of size 0. << val copy : bytes -> bytes >> Return a new byte sequence that contains the same bytes as the argument. << val of_string : string -> bytes >> Return a new byte sequence that contains the same bytes as the given string. << val to_string : bytes -> string >> Return a new string that contains the same bytes as the given byte sequence. << val sub : bytes -> int -> int -> bytes >> sub s start len returns a new byte sequence of length len, containing the subsequence of s that starts at position start and has length len. Raise Invalid_argument if start and len do not designate a valid range of s. << val sub_string : bytes -> int -> int -> string >> Same as sub but return a string instead of a byte sequence. << val extend : bytes -> int -> int -> bytes >> extend s left right returns a new byte sequence that contains the bytes of s, with left uninitialized bytes prepended and right uninitialized bytes appended to it. If left or right is negative, then bytes are removed (instead of appended) from the corresponding side of s. Raise Invalid_argument if the result length is negative or longer than Sys.max_string_length[25.10] bytes. << val fill : bytes -> int -> int -> char -> unit >> fill s start len c modifies s in place, replacing len characters with c, starting at start. Raise Invalid_argument if start and len do not designate a valid range of s. << val blit : bytes -> int -> bytes -> int -> int -> unit >> blit src srcoff dst dstoff len copies len bytes from sequence src, starting at index srcoff, to sequence dst, starting at index dstoff. It works correctly even if src and dst are the same byte sequence, and the source and destination intervals overlap. Raise Invalid_argument if srcoff and len do not designate a valid range of src, or if dstoff and len do not designate a valid range of dst. << val blit_string : string -> int -> bytes -> int -> int -> unit >> blit src srcoff dst dstoff len copies len bytes from string src, starting at index srcoff, to byte sequence dst, starting at index dstoff. Raise Invalid_argument if srcoff and len do not designate a valid range of src, or if dstoff and len do not designate a valid range of dst. << val concat : bytes -> bytes list -> bytes >> concat sep sl concatenates the list of byte sequences sl, inserting the separator byte sequence sep between each, and returns the result as a new byte sequence. Raise Invalid_argument if the result is longer than Sys.max_string_length[25.10] bytes. << val cat : bytes -> bytes -> bytes >> cat s1 s2 concatenates s1 and s2 and returns the result as new byte sequence. Raise Invalid_argument if the result is longer than Sys.max_string_length[25.10] bytes. << val iter : (char -> unit) -> bytes -> unit >> iter f s applies function f in turn to all the bytes of s. It is equivalent to f (get s 0); f (get s 1); ...; f (get s (length s - 1)); (). << val iteri : (int -> char -> unit) -> bytes -> unit >> Same as Bytes.iter[24.5], but the function is applied to the index of the byte as first argument and the byte itself as second argument. << val map : (char -> char) -> bytes -> bytes >> map f s applies function f in turn to all the bytes of s (in increasing index order) and stores the resulting bytes in a new sequence that is returned as the result. << val mapi : (int -> char -> char) -> bytes -> bytes >> mapi f s calls f with each character of s and its index (in increasing index order) and stores the resulting bytes in a new sequence that is returned as the result. << val trim : bytes -> bytes >> Return a copy of the argument, without leading and trailing whitespace. The bytes regarded as whitespace are the ASCII characters ' ', '\012', '\n', '\r', and '\t'. << val escaped : bytes -> bytes >> Return a copy of the argument, with special characters represented by escape sequences, following the lexical conventions of OCaml. All characters outside the ASCII printable range (32..126) are escaped, as well as backslash and double-quote. Raise Invalid_argument if the result is longer than Sys.max_string_length[25.10] bytes. << val index : bytes -> char -> int >> index s c returns the index of the first occurrence of byte c in s. Raise Not_found if c does not occur in s. << val index_opt : bytes -> char -> int option >> index_opt s c returns the index of the first occurrence of byte c in s or None if c does not occur in s. Since: 4.05 << val rindex : bytes -> char -> int >> rindex s c returns the index of the last occurrence of byte c in s. Raise Not_found if c does not occur in s. << val rindex_opt : bytes -> char -> int option >> rindex_opt s c returns the index of the last occurrence of byte c in s or None if c does not occur in s. Since: 4.05 << val index_from : bytes -> int -> char -> int >> index_from s i c returns the index of the first occurrence of byte c in s after position i. Bytes.index s c is equivalent to Bytes.index_from s 0 c. Raise Invalid_argument if i is not a valid position in s. Raise Not_found if c does not occur in s after position i. << val index_from_opt : bytes -> int -> char -> int option >> index_from _opts i c returns the index of the first occurrence of byte c in s after position i or None if c does not occur in s after position i. Bytes.index_opt s c is equivalent to Bytes.index_from_opt s 0 c. Raise Invalid_argument if i is not a valid position in s. Since: 4.05 << val rindex_from : bytes -> int -> char -> int >> rindex_from s i c returns the index of the last occurrence of byte c in s before position i+1. rindex s c is equivalent to rindex_from s (Bytes.length s - 1) c. Raise Invalid_argument if i+1 is not a valid position in s. Raise Not_found if c does not occur in s before position i+1. << val rindex_from_opt : bytes -> int -> char -> int option >> rindex_from_opt s i c returns the index of the last occurrence of byte c in s before position i+1 or None if c does not occur in s before position i+1. rindex_opt s c is equivalent to rindex_from s (Bytes.length s - 1) c. Raise Invalid_argument if i+1 is not a valid position in s. Since: 4.05 << val contains : bytes -> char -> bool >> contains s c tests if byte c appears in s. << val contains_from : bytes -> int -> char -> bool >> contains_from s start c tests if byte c appears in s after position start. contains s c is equivalent to contains_from s 0 c. Raise Invalid_argument if start is not a valid position in s. << val rcontains_from : bytes -> int -> char -> bool >> rcontains_from s stop c tests if byte c appears in s before position stop+1. Raise Invalid_argument if stop < 0 or stop+1 is not a valid position in s. << val uppercase : bytes -> bytes >> Deprecated. Functions operating on Latin-1 character set are deprecated.Return a copy of the argument, with all lowercase letters translated to uppercase, including accented letters of the ISO Latin-1 (8859-1) character set. << val lowercase : bytes -> bytes >> Deprecated. Functions operating on Latin-1 character set are deprecated.Return a copy of the argument, with all uppercase letters translated to lowercase, including accented letters of the ISO Latin-1 (8859-1) character set. << val capitalize : bytes -> bytes >> Deprecated. Functions operating on Latin-1 character set are deprecated.Return a copy of the argument, with the first character set to uppercase, using the ISO Latin-1 (8859-1) character set.. << val uncapitalize : bytes -> bytes >> Deprecated. Functions operating on Latin-1 character set are deprecated.Return a copy of the argument, with the first character set to lowercase, using the ISO Latin-1 (8859-1) character set.. << val uppercase_ascii : bytes -> bytes >> Return a copy of the argument, with all lowercase letters translated to uppercase, using the US-ASCII character set. Since: 4.03.0 << val lowercase_ascii : bytes -> bytes >> Return a copy of the argument, with all uppercase letters translated to lowercase, using the US-ASCII character set. Since: 4.03.0 << val capitalize_ascii : bytes -> bytes >> Return a copy of the argument, with the first character set to uppercase, using the US-ASCII character set. Since: 4.03.0 << val uncapitalize_ascii : bytes -> bytes >> Return a copy of the argument, with the first character set to lowercase, using the US-ASCII character set. Since: 4.03.0 << type t = bytes >> An alias for the type of byte sequences. << val compare : t -> t -> int >> The comparison function for byte sequences, with the same specification as Pervasives.compare[23.2]. Along with the type t, this function compare allows the module Bytes to be passed as argument to the functors Set.Make[24.34] and Map.Make[24.23]. << val equal : t -> t -> bool >> The equality function for byte sequences. Since: 4.03.0 Unsafe conversions (for advanced users) This section describes unsafe, low-level conversion functions between bytes and string. They do not copy the internal data; used improperly, they can break the immutability invariant on strings provided by the -safe-string option. They are available for expert library authors, but for most purposes you should use the always-correct Bytes.to_string[24.5] and Bytes.of_string[24.5] instead. << val unsafe_to_string : bytes -> string >> Unsafely convert a byte sequence into a string. To reason about the use of unsafe_to_string, it is convenient to consider an "ownership" discipline. A piece of code that manipulates some data "owns" it; there are several disjoint ownership modes, including: - Unique ownership: the data may be accessed and mutated - Shared ownership: the data has several owners, that may only access it, not mutate it. Unique ownership is linear: passing the data to another piece of code means giving up ownership (we cannot write the data again). A unique owner may decide to make the data shared (giving up mutation rights on it), but shared data may not become uniquely-owned again. unsafe_to_string s can only be used when the caller owns the byte sequence s -- either uniquely or as shared immutable data. The caller gives up ownership of s, and gains ownership of the returned string. There are two valid use-cases that respect this ownership discipline: 1. Creating a string by initializing and mutating a byte sequence that is never changed after initialization is performed. << let string_init len f : string = let s = Bytes.create len in for i = 0 to len - 1 do Bytes.set s i (f i) done; Bytes.unsafe_to_string s >> This function is safe because the byte sequence s will never be accessed or mutated after unsafe_to_string is called. The string_init code gives up ownership of s, and returns the ownership of the resulting string to its caller. Note that it would be unsafe if s was passed as an additional parameter to the function f as it could escape this way and be mutated in the future -- string_init would give up ownership of s to pass it to f, and could not call unsafe_to_string safely. We have provided the String.init[25.8], String.map[25.8] and String.mapi[25.8] functions to cover most cases of building new strings. You should prefer those over to_string or unsafe_to_string whenever applicable. 2. Temporarily giving ownership of a byte sequence to a function that expects a uniquely owned string and returns ownership back, so that we can mutate the sequence again after the call ended. << let bytes_length (s : bytes) = String.length (Bytes.unsafe_to_string s) >> In this use-case, we do not promise that s will never be mutated after the call to bytes_length s. The String.length[25.8] function temporarily borrows unique ownership of the byte sequence (and sees it as a string), but returns this ownership back to the caller, which may assume that s is still a valid byte sequence after the call. Note that this is only correct because we know that String.length[25.8] does not capture its argument -- it could escape by a side-channel such as a memoization combinator. The caller may not mutate s while the string is borrowed (it has temporarily given up ownership). This affects concurrent programs, but also higher-order functions: if String.length[25.8] returned a closure to be called later, s should not be mutated until this closure is fully applied and returns ownership. << val unsafe_of_string : string -> bytes >> Unsafely convert a shared string to a byte sequence that should not be mutated. The same ownership discipline that makes unsafe_to_string correct applies to unsafe_of_string: you may use it if you were the owner of the string value, and you will own the return bytes in the same mode. In practice, unique ownership of string values is extremely difficult to reason about correctly. You should always assume strings are shared, never uniquely owned. For example, string literals are implicitly shared by the compiler, so you never uniquely own them. << let incorrect = Bytes.unsafe_of_string "hello" let s = Bytes.of_string "hello" >> The first declaration is incorrect, because the string literal "hello" could be shared by the compiler with other parts of the program, and mutating incorrect is a bug. You must always use the second version, which performs a copy and is thus correct. Assuming unique ownership of strings that are not string literals, but are (partly) built from string literals, is also incorrect. For example, mutating unsafe_of_string ("foo" ^ s) could mutate the shared string "foo" -- assuming a rope-like representation of strings. More generally, functions operating on strings will assume shared ownership, they do not preserve unique ownership. It is thus incorrect to assume unique ownership of the result of unsafe_of_string. The only case we have reasonable confidence is safe is if the produced bytes is shared -- used as an immutable byte sequence. This is possibly useful for incremental migration of low-level programs that manipulate immutable sequences of bytes (for example Marshal.from_bytes[24.24]) and previously used the string type for this purpose. 24.6 Module BytesLabels : Byte sequence operations. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= Since: 4.02.0 << val length : bytes -> int >> Return the length (number of bytes) of the argument. << val get : bytes -> int -> char >> get s n returns the byte at index n in argument s. Raise Invalid_argument if n is not a valid index in s. << val set : bytes -> int -> char -> unit >> set s n c modifies s in place, replacing the byte at index n with c. Raise Invalid_argument if n is not a valid index in s. << val create : int -> bytes >> create n returns a new byte sequence of length n. The sequence is uninitialized and contains arbitrary bytes. Raise Invalid_argument if n < 0 or n > Sys.max_string_length[25.10]. << val make : int -> char -> bytes >> make n c returns a new byte sequence of length n, filled with the byte c. Raise Invalid_argument if n < 0 or n > Sys.max_string_length[25.10]. << val init : int -> f:(int -> char) -> bytes >> init n f returns a fresh byte sequence of length n, with character i initialized to the result of f i. Raise Invalid_argument if n < 0 or n > Sys.max_string_length[25.10]. << val empty : bytes >> A byte sequence of size 0. << val copy : bytes -> bytes >> Return a new byte sequence that contains the same bytes as the argument. << val of_string : string -> bytes >> Return a new byte sequence that contains the same bytes as the given string. << val to_string : bytes -> string >> Return a new string that contains the same bytes as the given byte sequence. << val sub : bytes -> pos:int -> len:int -> bytes >> sub s start len returns a new byte sequence of length len, containing the subsequence of s that starts at position start and has length len. Raise Invalid_argument if start and len do not designate a valid range of s. << val sub_string : bytes -> int -> int -> string >> Same as sub but return a string instead of a byte sequence. << val extend : bytes -> left:int -> right:int -> bytes >> extend s left right returns a new byte sequence that contains the bytes of s, with left uninitialized bytes prepended and right uninitialized bytes appended to it. If left or right is negative, then bytes are removed (instead of appended) from the corresponding side of s. Raise Invalid_argument if the result length is negative or longer than Sys.max_string_length[25.10] bytes. Since: 4.05.0 << val fill : bytes -> pos:int -> len:int -> char -> unit >> fill s start len c modifies s in place, replacing len characters with c, starting at start. Raise Invalid_argument if start and len do not designate a valid range of s. << val blit : src:bytes -> src_pos:int -> dst:bytes -> dst_pos:int -> len:int -> unit >> blit src srcoff dst dstoff len copies len bytes from sequence src, starting at index srcoff, to sequence dst, starting at index dstoff. It works correctly even if src and dst are the same byte sequence, and the source and destination intervals overlap. Raise Invalid_argument if srcoff and len do not designate a valid range of src, or if dstoff and len do not designate a valid range of dst. << val blit_string : src:string -> src_pos:int -> dst:bytes -> dst_pos:int -> len:int -> unit >> blit src srcoff dst dstoff len copies len bytes from string src, starting at index srcoff, to byte sequence dst, starting at index dstoff. Raise Invalid_argument if srcoff and len do not designate a valid range of src, or if dstoff and len do not designate a valid range of dst. Since: 4.05.0 << val concat : sep:bytes -> bytes list -> bytes >> concat sep sl concatenates the list of byte sequences sl, inserting the separator byte sequence sep between each, and returns the result as a new byte sequence. << val cat : bytes -> bytes -> bytes >> cat s1 s2 concatenates s1 and s2 and returns the result as new byte sequence. Raise Invalid_argument if the result is longer than Sys.max_string_length[25.10] bytes. Since: 4.05.0 << val iter : f:(char -> unit) -> bytes -> unit >> iter f s applies function f in turn to all the bytes of s. It is equivalent to f (get s 0); f (get s 1); ...; f (get s (length s - 1)); (). << val iteri : f:(int -> char -> unit) -> bytes -> unit >> Same as Bytes.iter[24.5], but the function is applied to the index of the byte as first argument and the byte itself as second argument. << val map : f:(char -> char) -> bytes -> bytes >> map f s applies function f in turn to all the bytes of s and stores the resulting bytes in a new sequence that is returned as the result. << val mapi : f:(int -> char -> char) -> bytes -> bytes >> mapi f s calls f with each character of s and its index (in increasing index order) and stores the resulting bytes in a new sequence that is returned as the result. << val trim : bytes -> bytes >> Return a copy of the argument, without leading and trailing whitespace. The bytes regarded as whitespace are the ASCII characters ' ', '\012', '\n', '\r', and '\t'. << val escaped : bytes -> bytes >> Return a copy of the argument, with special characters represented by escape sequences, following the lexical conventions of OCaml. << val index : bytes -> char -> int >> index s c returns the index of the first occurrence of byte c in s. Raise Not_found if c does not occur in s. << val index_opt : bytes -> char -> int option >> index_opt s c returns the index of the first occurrence of byte c in s or None if c does not occur in s. Since: 4.05 << val rindex : bytes -> char -> int >> rindex s c returns the index of the last occurrence of byte c in s. Raise Not_found if c does not occur in s. << val rindex_opt : bytes -> char -> int option >> rindex_opt s c returns the index of the last occurrence of byte c in s or None if c does not occur in s. Since: 4.05 << val index_from : bytes -> int -> char -> int >> index_from s i c returns the index of the first occurrence of byte c in s after position i. Bytes.index s c is equivalent to Bytes.index_from s 0 c. Raise Invalid_argument if i is not a valid position in s. Raise Not_found if c does not occur in s after position i. << val index_from_opt : bytes -> int -> char -> int option >> index_from _opts i c returns the index of the first occurrence of byte c in s after position i or None if c does not occur in s after position i. Bytes.index_opt s c is equivalent to Bytes.index_from_opt s 0 c. Raise Invalid_argument if i is not a valid position in s. Since: 4.05 << val rindex_from : bytes -> int -> char -> int >> rindex_from s i c returns the index of the last occurrence of byte c in s before position i+1. rindex s c is equivalent to rindex_from s (Bytes.length s - 1) c. Raise Invalid_argument if i+1 is not a valid position in s. Raise Not_found if c does not occur in s before position i+1. << val rindex_from_opt : bytes -> int -> char -> int option >> rindex_from_opt s i c returns the index of the last occurrence of byte c in s before position i+1 or None if c does not occur in s before position i+1. rindex_opt s c is equivalent to rindex_from s (Bytes.length s - 1) c. Raise Invalid_argument if i+1 is not a valid position in s. Since: 4.05 << val contains : bytes -> char -> bool >> contains s c tests if byte c appears in s. << val contains_from : bytes -> int -> char -> bool >> contains_from s start c tests if byte c appears in s after position start. contains s c is equivalent to contains_from s 0 c. Raise Invalid_argument if start is not a valid position in s. << val rcontains_from : bytes -> int -> char -> bool >> rcontains_from s stop c tests if byte c appears in s before position stop+1. Raise Invalid_argument if stop < 0 or stop+1 is not a valid position in s. << val uppercase : bytes -> bytes >> Deprecated. Functions operating on Latin-1 character set are deprecated.Return a copy of the argument, with all lowercase letters translated to uppercase, including accented letters of the ISO Latin-1 (8859-1) character set. << val lowercase : bytes -> bytes >> Deprecated. Functions operating on Latin-1 character set are deprecated.Return a copy of the argument, with all uppercase letters translated to lowercase, including accented letters of the ISO Latin-1 (8859-1) character set. << val capitalize : bytes -> bytes >> Deprecated. Functions operating on Latin-1 character set are deprecated.Return a copy of the argument, with the first character set to uppercase, using the ISO Latin-1 (8859-1) character set.. << val uncapitalize : bytes -> bytes >> Deprecated. Functions operating on Latin-1 character set are deprecated.Return a copy of the argument, with the first character set to lowercase, using the ISO Latin-1 (8859-1) character set.. << val uppercase_ascii : bytes -> bytes >> Return a copy of the argument, with all lowercase letters translated to uppercase, using the US-ASCII character set. Since: 4.05.0 << val lowercase_ascii : bytes -> bytes >> Return a copy of the argument, with all uppercase letters translated to lowercase, using the US-ASCII character set. Since: 4.05.0 << val capitalize_ascii : bytes -> bytes >> Return a copy of the argument, with the first character set to uppercase, using the US-ASCII character set. Since: 4.05.0 << val uncapitalize_ascii : bytes -> bytes >> Return a copy of the argument, with the first character set to lowercase, using the US-ASCII character set. Since: 4.05.0 << type t = bytes >> An alias for the type of byte sequences. << val compare : t -> t -> int >> The comparison function for byte sequences, with the same specification as Pervasives.compare[23.2]. Along with the type t, this function compare allows the module Bytes to be passed as argument to the functors Set.Make[24.34] and Map.Make[24.23]. << val equal : t -> t -> bool >> The equality function for byte sequences. Since: 4.05.0 24.7 Module Callback : Registering OCaml values with the C runtime. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= This module allows OCaml values to be registered with the C runtime under a symbolic name, so that C code can later call back registered OCaml functions, or raise registered OCaml exceptions. << val register : string -> 'a -> unit >> Callback.register n v registers the value v under the name n. C code can later retrieve a handle to v by calling caml_named_value(n). << val register_exception : string -> exn -> unit >> Callback.register_exception n exn registers the exception contained in the exception value exn under the name n. C code can later retrieve a handle to the exception by calling caml_named_value(n). The exception value thus obtained is suitable for passing as first argument to raise_constant or raise_with_arg. 24.8 Module Char : Character operations. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* << val code : char -> int >> Return the ASCII code of the argument. << val chr : int -> char >> Return the character with the given ASCII code. Raise Invalid_argument "Char.chr" if the argument is outside the range 0--255. << val escaped : char -> string >> Return a string representing the given character, with special characters escaped following the lexical conventions of OCaml. All characters outside the ASCII printable range (32..126) are escaped, as well as backslash, double-quote, and single-quote. << val lowercase : char -> char >> Deprecated. Functions operating on Latin-1 character set are deprecated.Convert the given character to its equivalent lowercase character, using the ISO Latin-1 (8859-1) character set. << val uppercase : char -> char >> Deprecated. Functions operating on Latin-1 character set are deprecated.Convert the given character to its equivalent uppercase character, using the ISO Latin-1 (8859-1) character set. << val lowercase_ascii : char -> char >> Convert the given character to its equivalent lowercase character, using the US-ASCII character set. Since: 4.03.0 << val uppercase_ascii : char -> char >> Convert the given character to its equivalent uppercase character, using the US-ASCII character set. Since: 4.03.0 << type t = char >> An alias for the type of characters. << val compare : t -> t -> int >> The comparison function for characters, with the same specification as Pervasives.compare[23.2]. Along with the type t, this function compare allows the module Char to be passed as argument to the functors Set.Make[24.34] and Map.Make[24.23]. << val equal : t -> t -> bool >> The equal function for chars. Since: 4.03.0 24.9 Module Complex : Complex numbers. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* This module provides arithmetic operations on complex numbers. Complex numbers are represented by their real and imaginary parts (cartesian representation). Each part is represented by a double-precision floating-point number (type float). << type t = { re : float ; im : float ; } >> The type of complex numbers. re is the real part and im the imaginary part. << val zero : t >> The complex number 0. << val one : t >> The complex number 1. << val i : t >> The complex number i. << val neg : t -> t >> Unary negation. << val conj : t -> t >> Conjugate: given the complex x + i.y, returns x - i.y. << val add : t -> t -> t >> Addition << val sub : t -> t -> t >> Subtraction << val mul : t -> t -> t >> Multiplication << val inv : t -> t >> Multiplicative inverse (1/z). << val div : t -> t -> t >> Division << val sqrt : t -> t >> Square root. The result x + i.y is such that x > 0 or x = 0 and y >= 0. This function has a discontinuity along the negative real axis. << val norm2 : t -> float >> Norm squared: given x + i.y, returns x^2 + y^2. << val norm : t -> float >> Norm: given x + i.y, returns sqrt(x^2 + y^2). << val arg : t -> float >> Argument. The argument of a complex number is the angle in the complex plane between the positive real axis and a line passing through zero and the number. This angle ranges from -pi to pi. This function has a discontinuity along the negative real axis. << val polar : float -> float -> t >> polar norm arg returns the complex having norm norm and argument arg. << val exp : t -> t >> Exponentiation. exp z returns e to the z power. << val log : t -> t >> Natural logarithm (in base e). << val pow : t -> t -> t >> Power function. pow z1 z2 returns z1 to the z2 power. 24.10 Module Digest : MD5 message digest. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= This module provides functions to compute 128-bit 'digests' of arbitrary-length strings or files. The digests are of cryptographic quality: it is very hard, given a digest, to forge a string having that digest. The algorithm used is MD5. This module should not be used for secure and sensitive cryptographic applications. For these kind of applications more recent and stronger cryptographic primitives should be used instead. << type t = string >> The type of digests: 16-character strings. << val compare : t -> t -> int >> The comparison function for 16-character digest, with the same specification as Pervasives.compare[23.2] and the implementation shared with String.compare[25.8]. Along with the type t, this function compare allows the module Digest to be passed as argument to the functors Set.Make[24.34] and Map.Make[24.23]. Since: 4.00.0 << val equal : t -> t -> bool >> The equal function for 16-character digest. Since: 4.03.0 << val string : string -> t >> Return the digest of the given string. << val bytes : bytes -> t >> Return the digest of the given byte sequence. Since: 4.02.0 << val substring : string -> int -> int -> t >> Digest.substring s ofs len returns the digest of the substring of s starting at index ofs and containing len characters. << val subbytes : bytes -> int -> int -> t >> Digest.subbytes s ofs len returns the digest of the subsequence of s starting at index ofs and containing len bytes. Since: 4.02.0 << val channel : Pervasives.in_channel -> int -> t >> If len is nonnegative, Digest.channel ic len reads len characters from channel ic and returns their digest, or raises End_of_file if end-of-file is reached before len characters are read. If len is negative, Digest.channel ic len reads all characters from ic until end-of-file is reached and return their digest. << val file : string -> t >> Return the digest of the file whose name is given. << val output : Pervasives.out_channel -> t -> unit >> Write a digest on the given output channel. << val input : Pervasives.in_channel -> t >> Read a digest from the given input channel. << val to_hex : t -> string >> Return the printable hexadecimal representation of the given digest. Raise Invalid_argument if the argument is not exactly 16 bytes. << val from_hex : string -> t >> Convert a hexadecimal representation back into the corresponding digest. Raise Invalid_argument if the argument is not exactly 32 hexadecimal characters. Since: 4.00.0 24.11 Module Ephemeron : Ephemerons and weak hash table *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= Ephemerons and weak hash table Ephemerons and weak hash table are useful when one wants to cache or memorize the computation of a function, as long as the arguments and the function are used, without creating memory leaks by continuously keeping old computation results that are not useful anymore because one argument or the function is freed. An implementation using is not suitable because all associations would keep in memory the arguments and the result. Ephemerons can also be used for "adding" a field to an arbitrary boxed ocaml value: you can attach an information to a value created by an external library without memory leaks. Ephemerons hold some keys and one or no data. They are all boxed ocaml values. The keys of an ephemeron have the same behavior than weak pointers according to the garbage collector. In fact ocaml weak pointers are implemented as ephemerons without data. The keys and data of an ephemeron are said to be full if they point to a value, empty if the value have never been set, have been unset, or was erased by the GC. In the function that accesses the keys or data these two states are represented by the option type. The data is considered by the garbage collector alive if all the full keys are alive and if the ephemeron is alive. When one of the keys is not considered alive anymore by the GC, the data is emptied from the ephemeron. The data could be alive for another reason and in that case the GC will not free it, but the ephemeron will not hold the data anymore. The ephemerons complicate the notion of liveness of values, because it is not anymore an equivalence with the reachability from root value by usual pointers (not weak and not ephemerons). With ephemerons the notion of liveness is constructed by the least fixpoint of: A value is alive if: - it is a root value - it is reachable from alive value by usual pointers - it is the data of an alive ephemeron with all its full keys alive Notes: - All the types defined in this module cannot be marshaled using Pervasives.output_value[23.2] or the functions of the Marshal[24.24] module. Ephemerons are defined in a language agnostic way in this paper: B. Hayes, Ephemerons: a New Finalization Mechanism, OOPSLA'9 << module type S = >> sig Propose the same interface as usual hash table. However since the bindings are weak, even if mem h k is true, a subsequent find h k may raise Not_found because the garbage collector can run between the two. Moreover, the table shouldn't be modified during a call to iter. Use filter_map_inplace in this case. include Hashtbl.S << val clean : 'a t -> unit >> remove all dead bindings. Done automatically during automatic resizing. << val stats_alive : 'a t -> Hashtbl.statistics >> same as Hashtbl.SeededS.stats[24.16] but only count the alive bindings end The output signature of the functor Ephemeron.K1.Make[24.11] and Ephemeron.K2.Make[24.11]. These hash tables are weak in the keys. If all the keys of a binding are alive the binding is kept, but if one of the keys of the binding is dead then the binding is removed. << module type SeededS = >> sig include Hashtbl.SeededS << val clean : 'a t -> unit >> remove all dead bindings. Done automatically during automatic resizing. << val stats_alive : 'a t -> Hashtbl.statistics >> same as Hashtbl.SeededS.stats[24.16] but only count the alive bindings end The output signature of the functor Ephemeron.K1.MakeSeeded[24.11] and Ephemeron.K2.MakeSeeded[24.11]. << module K1 : >> sig << type ('k, 'd) t >> an ephemeron with one key << val create : unit -> ('k, 'd) t >> Ephemeron.K1.create () creates an ephemeron with one key. The data and the key are empty << val get_key : ('k, 'd) t -> 'k option >> Ephemeron.K1.get_key eph returns None if the key of eph is empty, Some x (where x is the key) if it is full. << val get_key_copy : ('k, 'd) t -> 'k option >> Ephemeron.K1.get_key_copy eph returns None if the key of eph is empty, Some x (where x is a (shallow) copy of the key) if it is full. This function has the same GC friendliness as Weak.get_copy[25.12] If the element is a custom block it is not copied. << val set_key : ('k, 'd) t -> 'k -> unit >> Ephemeron.K1.set_key eph el sets the key of eph to be a (full) key to el << val unset_key : ('k, 'd) t -> unit >> Ephemeron.K1.unset_key eph el sets the key of eph to be an empty key. Since there is only one key, the ephemeron starts behaving like a reference on the data. << val check_key : ('k, 'd) t -> bool >> Ephemeron.K1.check_key eph returns true if the key of the eph is full, false if it is empty. Note that even if Ephemeron.K1.check_key eph returns true, a subsequent Ephemeron.K1.get_key[24.11]eph can return None. << val blit_key : ('k, 'a) t -> ('k, 'b) t -> unit >> Ephemeron.K1.blit_key eph1 eph2 sets the key of eph2 with the key of eph1. Contrary to using Ephemeron.K1.get_key[24.11] followed by Ephemeron.K1.set_key[24.11] or Ephemeron.K1.unset_key[24.11] this function does not prevent the incremental GC from erasing the value in its current cycle. << val get_data : ('k, 'd) t -> 'd option >> Ephemeron.K1.get_data eph returns None if the data of eph is empty, Some x (where x is the data) if it is full. << val get_data_copy : ('k, 'd) t -> 'd option >> Ephemeron.K1.get_data_copy eph returns None if the data of eph is empty, Some x (where x is a (shallow) copy of the data) if it is full. This function has the same GC friendliness as Weak.get_copy[25.12] If the element is a custom block it is not copied. << val set_data : ('k, 'd) t -> 'd -> unit >> Ephemeron.K1.set_data eph el sets the data of eph to be a (full) data to el << val unset_data : ('k, 'd) t -> unit >> Ephemeron.K1.unset_data eph el sets the key of eph to be an empty key. The ephemeron starts behaving like a weak pointer. << val check_data : ('k, 'd) t -> bool >> Ephemeron.K1.check_data eph returns true if the data of the eph is full, false if it is empty. Note that even if Ephemeron.K1.check_data eph returns true, a subsequent Ephemeron.K1.get_data[24.11]eph can return None. << val blit_data : ('a, 'd) t -> ('b, 'd) t -> unit >> Ephemeron.K1.blit_data eph1 eph2 sets the data of eph2 with the data of eph1. Contrary to using Ephemeron.K1.get_data[24.11] followed by Ephemeron.K1.set_data[24.11] or Ephemeron.K1.unset_data[24.11] this function does not prevent the incremental GC from erasing the value in its current cycle. << module Make : >> functor (H : Hashtbl.HashedType) -> Ephemeron.S with type key = H.t Functor building an implementation of a weak hash table << module MakeSeeded : >> functor (H : Hashtbl.SeededHashedType) -> Ephemeron.SeededS with type key = H.t Functor building an implementation of a weak hash table. The seed is similar to the one of Hashtbl.MakeSeeded[24.16]. end << module K2 : >> sig << type ('k1, 'k2, 'd) t >> an ephemeron with two keys << val create : unit -> ('k1, 'k2, 'd) t >> Same as Ephemeron.K1.create[24.11] << val get_key1 : ('k1, 'k2, 'd) t -> 'k1 option >> Same as Ephemeron.K1.get_key[24.11] << val get_key1_copy : ('k1, 'k2, 'd) t -> 'k1 option >> Same as Ephemeron.K1.get_key_copy[24.11] << val set_key1 : ('k1, 'k2, 'd) t -> 'k1 -> unit >> Same as Ephemeron.K1.set_key[24.11] << val unset_key1 : ('k1, 'k2, 'd) t -> unit >> Same as Ephemeron.K1.unset_key[24.11] << val check_key1 : ('k1, 'k2, 'd) t -> bool >> Same as Ephemeron.K1.check_key[24.11] << val get_key2 : ('k1, 'k2, 'd) t -> 'k2 option >> Same as Ephemeron.K1.get_key[24.11] << val get_key2_copy : ('k1, 'k2, 'd) t -> 'k2 option >> Same as Ephemeron.K1.get_key_copy[24.11] << val set_key2 : ('k1, 'k2, 'd) t -> 'k2 -> unit >> Same as Ephemeron.K1.set_key[24.11] << val unset_key2 : ('k1, 'k2, 'd) t -> unit >> Same as Ephemeron.K1.unset_key[24.11] << val check_key2 : ('k1, 'k2, 'd) t -> bool >> Same as Ephemeron.K1.check_key[24.11] << val blit_key1 : ('k1, 'a, 'b) t -> ('k1, 'c, 'd) t -> unit >> Same as Ephemeron.K1.blit_key[24.11] << val blit_key2 : ('a, 'k2, 'b) t -> ('c, 'k2, 'd) t -> unit >> Same as Ephemeron.K1.blit_key[24.11] << val blit_key12 : ('k1, 'k2, 'a) t -> ('k1, 'k2, 'b) t -> unit >> Same as Ephemeron.K1.blit_key[24.11] << val get_data : ('k1, 'k2, 'd) t -> 'd option >> Same as Ephemeron.K1.get_data[24.11] << val get_data_copy : ('k1, 'k2, 'd) t -> 'd option >> Same as Ephemeron.K1.get_data_copy[24.11] << val set_data : ('k1, 'k2, 'd) t -> 'd -> unit >> Same as Ephemeron.K1.set_data[24.11] << val unset_data : ('k1, 'k2, 'd) t -> unit >> Same as Ephemeron.K1.unset_data[24.11] << val check_data : ('k1, 'k2, 'd) t -> bool >> Same as Ephemeron.K1.check_data[24.11] << val blit_data : ('k1, 'k2, 'd) t -> ('k1, 'k2, 'd) t -> unit >> Same as Ephemeron.K1.blit_data[24.11] << module Make : >> functor (H1 : Hashtbl.HashedType) -> functor (H2 : Hashtbl.HashedType) -> Ephemeron.S with type key = H1.t * H2.t Functor building an implementation of a weak hash table << module MakeSeeded : >> functor (H1 : Hashtbl.SeededHashedType) -> functor (H2 : Hashtbl.SeededHashedType) -> Ephemeron.SeededS with type key = H1.t * H2.t Functor building an implementation of a weak hash table. The seed is similar to the one of Hashtbl.MakeSeeded[24.16]. end << module Kn : >> sig << type ('k, 'd) t >> an ephemeron with an arbitrary number of keys of the same type << val create : int -> ('k, 'd) t >> Same as Ephemeron.K1.create[24.11] << val get_key : ('k, 'd) t -> int -> 'k option >> Same as Ephemeron.K1.get_key[24.11] << val get_key_copy : ('k, 'd) t -> int -> 'k option >> Same as Ephemeron.K1.get_key_copy[24.11] << val set_key : ('k, 'd) t -> int -> 'k -> unit >> Same as Ephemeron.K1.set_key[24.11] << val unset_key : ('k, 'd) t -> int -> unit >> Same as Ephemeron.K1.unset_key[24.11] << val check_key : ('k, 'd) t -> int -> bool >> Same as Ephemeron.K1.check_key[24.11] << val blit_key : ('k, 'a) t -> int -> ('k, 'b) t -> int -> int -> unit >> Same as Ephemeron.K1.blit_key[24.11] << val get_data : ('k, 'd) t -> 'd option >> Same as Ephemeron.K1.get_data[24.11] << val get_data_copy : ('k, 'd) t -> 'd option >> Same as Ephemeron.K1.get_data_copy[24.11] << val set_data : ('k, 'd) t -> 'd -> unit >> Same as Ephemeron.K1.set_data[24.11] << val unset_data : ('k, 'd) t -> unit >> Same as Ephemeron.K1.unset_data[24.11] << val check_data : ('k, 'd) t -> bool >> Same as Ephemeron.K1.check_data[24.11] << val blit_data : ('k, 'd) t -> ('k, 'd) t -> unit >> Same as Ephemeron.K1.blit_data[24.11] << module Make : >> functor (H : Hashtbl.HashedType) -> Ephemeron.S with type key = H.t array Functor building an implementation of a weak hash table << module MakeSeeded : >> functor (H : Hashtbl.SeededHashedType) -> Ephemeron.SeededS with type key = H.t array Functor building an implementation of a weak hash table. The seed is similar to the one of Hashtbl.MakeSeeded[24.16]. end << module GenHashTable : >> sig Define a hash table on generic containers which have a notion of "death" and aliveness. If a binding is dead the hash table can automatically remove it. << type equal = | ETrue | EFalse | EDead >> the container is dead << module MakeSeeded : >> functor (H : sig << type t >> keys << type 'a container >> contains keys and the associated data << val hash : int -> t -> int >> same as Hashtbl.SeededHashedType << val equal : 'a container -> t -> Ephemeron.GenHashTable.equal >> equality predicate used to compare a key with the one in a container. Can return EDead if the keys in the container are dead << val create : t -> 'a -> 'a container >> create key data creates a container from some initials keys and one data << val get_key : 'a container -> t option >> get_key cont returns the keys if they are all alive << val get_data : 'a container -> 'a option >> get_data cont returns the data if it is alive << val set_key_data : 'a container -> t -> 'a -> unit >> set_key_data cont modifies the key and data << val check_key : 'a container -> bool >> check_key cont checks if all the keys contained in the data are alive end ) -> Ephemeron.SeededS with type key = H.t Functor building an implementation of an hash table that use the container for keeping the information given end 24.12 Module Filename : Operations on file names. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= << val current_dir_name : string >> The conventional name for the current directory (e.g. . in Unix). << val parent_dir_name : string >> The conventional name for the parent of the current directory (e.g. .. in Unix). << val dir_sep : string >> The directory separator (e.g. / in Unix). Since: 3.11.2 << val concat : string -> string -> string >> concat dir file returns a file name that designates file file in directory dir. << val is_relative : string -> bool >> Return true if the file name is relative to the current directory, false if it is absolute (i.e. in Unix, starts with /). << val is_implicit : string -> bool >> Return true if the file name is relative and does not start with an explicit reference to the current directory (./ or ../ in Unix), false if it starts with an explicit reference to the root directory or the current directory. << val check_suffix : string -> string -> bool >> check_suffix name suff returns true if the filename name ends with the suffix suff. << val chop_suffix : string -> string -> string >> chop_suffix name suff removes the suffix suff from the filename name. The behavior is undefined if name does not end with the suffix suff. << val extension : string -> string >> extension name is the shortest suffix ext of name0 where: - name0 is the longest suffix of name that does not contain a directory separator; - ext starts with a period; - ext is preceded by at least one non-period character in name0. If such a suffix does not exist, extension name is the empty string. Since: 4.04 << val remove_extension : string -> string >> Return the given file name without its extension, as defined in Filename.extension[24.12]. If the extension is empty, the function returns the given file name. The following invariant holds for any file name s: remove_extension s ^ extension s = s Since: 4.04 << val chop_extension : string -> string >> Same as Filename.remove_extension[24.12], but raise Invalid_argument if the given name has an empty extension. << val basename : string -> string >> Split a file name into directory name / base file name. If name is a valid file name, then concat (dirname name) (basename name) returns a file name which is equivalent to name. Moreover, after setting the current directory to dirname name (with Sys.chdir[25.10]), references to basename name (which is a relative file name) designate the same file as name before the call to Sys.chdir[25.10]. This function conforms to the specification of POSIX.1-2008 for the basename utility. << val dirname : string -> string >> See Filename.basename[24.12]. This function conforms to the specification of POSIX.1-2008 for the dirname utility. << val temp_file : ?temp_dir:string -> string -> string -> string >> temp_file prefix suffix returns the name of a fresh temporary file in the temporary directory. The base name of the temporary file is formed by concatenating prefix, then a suitably chosen integer number, then suffix. The optional argument temp_dir indicates the temporary directory to use, defaulting to the current result of Filename.get_temp_dir_name[24.12]. The temporary file is created empty, with permissions 0o600 (readable and writable only by the file owner). The file is guaranteed to be different from any other file that existed when temp_file was called. Raise Sys_error if the file could not be created. Before 3.11.2 no ?temp_dir optional argument << val open_temp_file : ?mode:Pervasives.open_flag list -> ?perms:int -> ?temp_dir:string -> string -> string -> string * Pervasives.out_channel >> Same as Filename.temp_file[24.12], but returns both the name of a fresh temporary file, and an output channel opened (atomically) on this file. This function is more secure than temp_file: there is no risk that the temporary file will be modified (e.g. replaced by a symbolic link) before the program opens it. The optional argument mode is a list of additional flags to control the opening of the file. It can contain one or several of Open_append, Open_binary, and Open_text. The default is [Open_text] (open in text mode). The file is created with permissions perms (defaults to readable and writable only by the file owner, 0o600). Before 4.03.0 no ?perms optional argument Before 3.11.2 no ?temp_dir optional argument Raises Sys_error if the file could not be opened. << val get_temp_dir_name : unit -> string >> The name of the temporary directory: Under Unix, the value of the TMPDIR environment variable, or "/tmp" if the variable is not set. Under Windows, the value of the TEMP environment variable, or "." if the variable is not set. The temporary directory can be changed with Filename.set_temp_dir_name[24.12]. Since: 4.00.0 << val set_temp_dir_name : string -> unit >> Change the temporary directory returned by Filename.get_temp_dir_name[24.12] and used by Filename.temp_file[24.12] and Filename.open_temp_file[24.12]. Since: 4.00.0 << val temp_dir_name : string >> Deprecated. You should use Filename.get_temp_dir_name[24.12] instead.The name of the initial temporary directory: Under Unix, the value of the TMPDIR environment variable, or "/tmp" if the variable is not set. Under Windows, the value of the TEMP environment variable, or "." if the variable is not set. Since: 3.09.1 << val quote : string -> string >> Return a quoted version of a file name, suitable for use as one argument in a command line, escaping all meta-characters. Warning: under Windows, the output is only suitable for use with programs that follow the standard Windows quoting conventions. 24.13 Module Format : Pretty printing. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* This module implements a pretty-printing facility to format values within 'pretty-printing boxes'. The pretty-printer splits lines at specified break hints, and indents lines according to the box structure. For a gentle introduction to the basics of pretty-printing using Format, read http://caml.inria.fr/resources/doc/guides/format.en.html[http://caml.inria.fr /resources/doc/guides/format.en.html]. You may consider this module as providing an extension to the printf facility to provide automatic line splitting. The addition of pretty-printing annotations to your regular printf formats gives you fancy indentation and line breaks. Pretty-printing annotations are described below in the documentation of the function Format.fprintf[24.13]. You may also use the explicit box management and printing functions provided by this module. This style is more basic but more verbose than the fprintf concise formats. For instance, the sequence open_box 0; print_string "x ="; print_space (); print_int 1; close_box (); print_newline () that prints x = 1 within a pretty-printing box, can be abbreviated as printf "@[%s@ %i@]@." "x =" 1, or even shorter printf "@[x =@ %i@]@." 1. Rule of thumb for casual users of this library: - use simple boxes (as obtained by open_box 0); - use simple break hints (as obtained by print_cut () that outputs a simple break hint, or by print_space () that outputs a space indicating a break hint); - once a box is opened, display its material with basic printing functions (e. g. print_int and print_string); - when the material for a box has been printed, call close_box () to close the box; - at the end of your routine, flush the pretty-printer to display all the remaining material, e.g. evaluate print_newline (). The behaviour of pretty-printing commands is unspecified if there is no opened pretty-printing box. Each box opened via one of the open_ functions below must be closed using close_box for proper formatting. Otherwise, some of the material printed in the boxes may not be output, or may be formatted incorrectly. In case of interactive use, the system closes all opened boxes and flushes all pending text (as with the print_newline function) after each phrase. Each phrase is therefore executed in the initial state of the pretty-printer. Warning: the material output by the following functions is delayed in the pretty-printer queue in order to compute the proper line splitting. Hence, you should not mix calls to the printing functions of the basic I/O system with calls to the functions of this module: this could result in some strange output seemingly unrelated with the evaluation order of printing commands. Boxes ===== << val open_box : int -> unit >> open_box d opens a new pretty-printing box with offset d. This box prints material as much as possible on every line. A break hint splits the line if there is no more room on the line to print the remainder of the box. A break hint also splits the line if the splitting "moves to the left" (i.e. it gives an indentation smaller than the one of the current line). This box is the general purpose pretty-printing box. If the pretty-printer splits the line in the box, offset d is added to the current indentation. << val close_box : unit -> unit >> Closes the most recently opened pretty-printing box. Formatting functions ==================== << val print_string : string -> unit >> print_string str prints str in the current box. << val print_as : int -> string -> unit >> print_as len str prints str in the current box. The pretty-printer formats str as if it were of length len. << val print_int : int -> unit >> Prints an integer in the current box. << val print_float : float -> unit >> Prints a floating point number in the current box. << val print_char : char -> unit >> Prints a character in the current box. << val print_bool : bool -> unit >> Prints a boolean in the current box. Break hints =========== A 'break hint' tells the pretty-printer to output some space or split the line whichever way is more appropriate to the current box splitting rules. Break hints are used to separate printing items and are mandatory to let the pretty-printer correctly split lines and indent items. Simple break hints are: - the 'space': output a space or split the line if appropriate, - the 'cut': split the line if appropriate. Note: the notions of space and line splitting are abstract for the pretty-printing engine, since those notions can be completely defined by the programmer. However, in the pretty-printer default setting, "output a space" simply means printing a space character (ASCII code 32) and "split the line" is printing a newline character (ASCII code 10). << val print_space : unit -> unit >> print_space () the 'space' break hint: the pretty-printer may split the line at this point, otherwise it prints one space. It is equivalent to print_break 1 0. << val print_cut : unit -> unit >> print_cut () the 'cut' break hint: the pretty-printer may split the line at this point, otherwise it prints nothing. It is equivalent to print_break 0 0. << val print_break : int -> int -> unit >> print_break nspaces offset the 'full' break hint: the pretty-printer may split the line at this point, otherwise it prints nspaces spaces. If the pretty-printer splits the line, offset is added to the current indentation. << val print_flush : unit -> unit >> Flushes the pretty printer: all opened boxes are closed, and all pending text is displayed. << val print_newline : unit -> unit >> Equivalent to print_flush followed by a new line. << val force_newline : unit -> unit >> Forces a new line in the current box. Not the normal way of pretty-printing, since the new line does not reset the current line count. You should prefer using break hints within a vertcal box. << val print_if_newline : unit -> unit >> Executes the next formatting command if the preceding line has just been split. Otherwise, ignore the next formatting command. Margin ====== << val set_margin : int -> unit >> set_margin d sets the right margin to d (in characters): the pretty-printer splits lines that overflow the right margin according to the break hints given. Nothing happens if d is smaller than 2. If d is too large, the right margin is set to the maximum admissible value (which is greater than 10^9). << val get_margin : unit -> int >> Returns the position of the right margin. Maximum indentation limit ========================= << val set_max_indent : int -> unit >> set_max_indent d sets the maximum indentation limit of lines to d (in characters): once this limit is reached, new boxes are rejected to the left, if they do not fit on the current line. Nothing happens if d is smaller than 2. If d is too large, the limit is set to the maximum admissible value (which is greater than 10 ^ 9). << val get_max_indent : unit -> int >> Return the maximum indentation limit (in characters). Formatting depth: maximum number of boxes allowed before ellipsis ================================================================= << val set_max_boxes : int -> unit >> set_max_boxes max sets the maximum number of boxes simultaneously opened. Material inside boxes nested deeper is printed as an ellipsis (more precisely as the text returned by get_ellipsis_text ()). Nothing happens if max is smaller than 2. << val get_max_boxes : unit -> int >> Returns the maximum number of boxes allowed before ellipsis. << val over_max_boxes : unit -> bool >> Tests if the maximum number of boxes allowed have already been opened. Advanced formatting =================== << val open_hbox : unit -> unit >> open_hbox () opens a new 'horizontal' pretty-printing box. This box prints material on a single line. Break hints in a horizontal box never split the line. (Line splitting may still occur inside boxes nested deeper). << val open_vbox : int -> unit >> open_vbox d opens a new 'vertical' pretty-printing box with offset d. This box prints material on as many lines as break hints in the box. Every break hint in a vertical box splits the line. If the pretty-printer splits the line in the box, d is added to the current indentation. << val open_hvbox : int -> unit >> open_hvbox d opens a new 'horizontal-vertical' pretty-printing box with offset d. This box behaves as an horizontal box if it fits on a single line, otherwise it behaves as a vertical box. If the pretty-printer splits the line in the box, d is added to the current indentation. << val open_hovbox : int -> unit >> open_hovbox d opens a new 'horizontal-or-vertical' pretty-printing box with offset d. This box prints material as much as possible on every line. A break hint splits the line if there is no more room on the line to print the remainder of the box. If the pretty-printer splits the line in the box, d is added to the current indentation. Ellipsis ======== << val set_ellipsis_text : string -> unit >> Set the text of the ellipsis printed when too many boxes are opened (a single dot, ., by default). << val get_ellipsis_text : unit -> string >> Return the text of the ellipsis. Semantic Tags ============= << type tag = string >> Semantic tags (or simply tags) are used to decorate printed entities for user's defined purposes, e.g. setting font and giving size indications for a display device, or marking delimitation of semantic entities (e.g. HTML or TeX elements or terminal escape sequences). By default, those tags do not influence line splitting calculation: the tag 'markers' are not considered as part of the printing material that drives line splitting (in other words, the length of those strings is considered as zero for line splitting). Thus, tag handling is in some sense transparent to pretty-printing and does not interfere with usual indentation. Hence, a single pretty printing routine can output both simple 'verbatim' material or richer decorated output depending on the treatment of tags. By default, tags are not active, hence the output is not decorated with tag information. Once set_tags is set to true, the pretty printer engine honours tags and decorates the output accordingly. When a tag has been opened (or closed), it is both and successively 'printed' and 'marked'. Printing a tag means calling a formatter specific function with the name of the tag as argument: that 'tag printing' function can then print any regular material to the formatter (so that this material is enqueued as usual in the formatter queue for further line splitting computation). Marking a tag means to output an arbitrary string (the 'tag marker'), directly into the output device of the formatter. Hence, the formatter specific 'tag marking' function must return the tag marker string associated to its tag argument. Being flushed directly into the output device of the formatter, tag marker strings are not considered as part of the printing material that drives line splitting (in other words, the length of the strings corresponding to tag markers is considered as zero for line splitting). In addition, advanced users may take advantage of the specificity of tag markers to be precisely output when the pretty printer has already decided where to split the lines, and precisely when the queue is flushed into the output device. In the spirit of HTML tags, the default tag marking functions output tags enclosed in "<" and ">": hence, the opening marker of tag t is "" and the closing marker "". Default tag printing functions just do nothing. Tag marking and tag printing functions are user definable and can be set by calling set_formatter_tag_functions. << val open_tag : tag -> unit >> open_tag t opens the tag named t; the print_open_tag function of the formatter is called with t as argument; the tag marker mark_open_tag t will be flushed into the output device of the formatter. << val close_tag : unit -> unit >> close_tag () closes the most recently opened tag t. In addition, the print_close_tag function of the formatter is called with t as argument. The marker mark_close_tag t will be flushed into the output device of the formatter. << val set_tags : bool -> unit >> set_tags b turns on or off the treatment of tags (default is off). << val set_print_tags : bool -> unit >> set_print_tags b turns on or off the printing of tags. << val set_mark_tags : bool -> unit >> set_mark_tags b turns on or off the output of tag markers. << val get_print_tags : unit -> bool >> Return the current status of tags printing. << val get_mark_tags : unit -> bool >> Return the current status of tags marking. Redirecting the standard formatter output ========================================= << val set_formatter_out_channel : Pervasives.out_channel -> unit >> Redirect the pretty-printer output to the given channel. (All the output functions of the standard formatter are set to the default output functions printing to the given channel.) << val set_formatter_output_functions : (string -> int -> int -> unit) -> (unit -> unit) -> unit >> set_formatter_output_functions out flush redirects the pretty-printer output functions to the functions out and flush. The out function performs all the pretty-printer string output. It is called with a string s, a start position p, and a number of characters n; it is supposed to output characters p to p + n - 1 of s. The flush function is called whenever the pretty-printer is flushed (via conversion %!, or pretty-printing indications @? or @., or using low level functions print_flush or print_newline). << val get_formatter_output_functions : unit -> (string -> int -> int -> unit) * (unit -> unit) >> Return the current output functions of the pretty-printer. Changing the meaning of standard formatter pretty printing ========================================================== The Format module is versatile enough to let you completely redefine the meaning of pretty printing: you may provide your own functions to define how to handle indentation, line splitting, and even printing of all the characters that have to be printed! << type formatter_out_functions = { out_string : string -> int -> int -> unit ; out_flush : unit -> unit ; out_newline : unit -> unit ; out_spaces : int -> unit ; } >> Since: 4.01.0 << val set_formatter_out_functions : formatter_out_functions -> unit >> set_formatter_out_functions f Redirect the pretty-printer output to the functions f.out_string and f.out_flush as described in set_formatter_output_functions. In addition, the pretty-printer function that outputs a newline is set to the function f.out_newline and the function that outputs indentation spaces is set to the function f.out_spaces. This way, you can change the meaning of indentation (which can be something else than just printing space characters) and the meaning of new lines opening (which can be connected to any other action needed by the application at hand). The two functions f.out_spaces and f.out_newline are normally connected to f.out_string and f.out_flush: respective default values for f.out_space and f.out_newline are f.out_string (String.make n ' ') 0 n and f.out_string "\n" 0 1. Since: 4.01.0 << val get_formatter_out_functions : unit -> formatter_out_functions >> Return the current output functions of the pretty-printer, including line splitting and indentation functions. Useful to record the current setting and restore it afterwards. Since: 4.01.0 Changing the meaning of printing semantic tags ============================================== << type formatter_tag_functions = { mark_open_tag : tag -> string ; mark_close_tag : tag -> string ; print_open_tag : tag -> unit ; print_close_tag : tag -> unit ; } >> The tag handling functions specific to a formatter: mark versions are the 'tag marking' functions that associate a string marker to a tag in order for the pretty-printing engine to flush those markers as 0 length tokens in the output device of the formatter. print versions are the 'tag printing' functions that can perform regular printing when a tag is closed or opened. << val set_formatter_tag_functions : formatter_tag_functions -> unit >> set_formatter_tag_functions tag_funs changes the meaning of opening and closing tags to use the functions in tag_funs. When opening a tag name t, the string t is passed to the opening tag marking function (the mark_open_tag field of the record tag_funs), that must return the opening tag marker for that name. When the next call to close_tag () happens, the tag name t is sent back to the closing tag marking function (the mark_close_tag field of record tag_funs), that must return a closing tag marker for that name. The print_ field of the record contains the functions that are called at tag opening and tag closing time, to output regular material in the pretty-printer queue. << val get_formatter_tag_functions : unit -> formatter_tag_functions >> Return the current tag functions of the pretty-printer. Multiple formatted output ========================= << type formatter >> Abstract data corresponding to a pretty-printer (also called a formatter) and all its machinery. Defining new pretty-printers permits unrelated output of material in parallel on several output channels. All the parameters of a pretty-printer are local to a formatter: margin, maximum indentation limit, maximum number of boxes simultaneously opened, ellipsis, and so on, are specific to each pretty-printer and may be fixed independently. Given a Pervasives.out_channel[23.2] output channel oc, a new formatter writing to that channel is simply obtained by calling formatter_of_out_channel oc. Alternatively, the make_formatter function allocates a new formatter with explicit output and flushing functions (convenient to output material to strings for instance). << val formatter_of_out_channel : Pervasives.out_channel -> formatter >> formatter_of_out_channel oc returns a new formatter that writes to the corresponding channel oc. << val std_formatter : formatter >> The standard formatter used by the formatting functions above. It is defined as formatter_of_out_channel stdout. << val err_formatter : formatter >> A formatter to use with formatting functions below for output to standard error. It is defined as formatter_of_out_channel stderr. << val formatter_of_buffer : Buffer.t -> formatter >> formatter_of_buffer b returns a new formatter writing to buffer b. As usual, the formatter has to be flushed at the end of pretty printing, using pp_print_flush or pp_print_newline, to display all the pending material. << val stdbuf : Buffer.t >> The string buffer in which str_formatter writes. << val str_formatter : formatter >> A formatter to use with formatting functions below for output to the stdbuf string buffer. str_formatter is defined as formatter_of_buffer stdbuf. << val flush_str_formatter : unit -> string >> Returns the material printed with str_formatter, flushes the formatter and resets the corresponding buffer. << val make_formatter : (string -> int -> int -> unit) -> (unit -> unit) -> formatter >> make_formatter out flush returns a new formatter that writes according to the output function out, and the flushing function flush. For instance, a formatter to the Pervasives.out_channel[23.2] oc is returned by make_formatter (Pervasives.output oc) (fun () -> Pervasives.flush oc). Basic functions to use with formatters ====================================== << val pp_open_hbox : formatter -> unit -> unit >> << val pp_open_vbox : formatter -> int -> unit >> << val pp_open_hvbox : formatter -> int -> unit >> << val pp_open_hovbox : formatter -> int -> unit >> << val pp_open_box : formatter -> int -> unit >> << val pp_close_box : formatter -> unit -> unit >> << val pp_open_tag : formatter -> string -> unit >> << val pp_close_tag : formatter -> unit -> unit >> << val pp_print_string : formatter -> string -> unit >> << val pp_print_as : formatter -> int -> string -> unit >> << val pp_print_int : formatter -> int -> unit >> << val pp_print_float : formatter -> float -> unit >> << val pp_print_char : formatter -> char -> unit >> << val pp_print_bool : formatter -> bool -> unit >> << val pp_print_break : formatter -> int -> int -> unit >> << val pp_print_cut : formatter -> unit -> unit >> << val pp_print_space : formatter -> unit -> unit >> << val pp_force_newline : formatter -> unit -> unit >> << val pp_print_flush : formatter -> unit -> unit >> << val pp_print_newline : formatter -> unit -> unit >> << val pp_print_if_newline : formatter -> unit -> unit >> << val pp_set_tags : formatter -> bool -> unit >> << val pp_set_print_tags : formatter -> bool -> unit >> << val pp_set_mark_tags : formatter -> bool -> unit >> << val pp_get_print_tags : formatter -> unit -> bool >> << val pp_get_mark_tags : formatter -> unit -> bool >> << val pp_set_margin : formatter -> int -> unit >> << val pp_get_margin : formatter -> unit -> int >> << val pp_set_max_indent : formatter -> int -> unit >> << val pp_get_max_indent : formatter -> unit -> int >> << val pp_set_max_boxes : formatter -> int -> unit >> << val pp_get_max_boxes : formatter -> unit -> int >> << val pp_over_max_boxes : formatter -> unit -> bool >> << val pp_set_ellipsis_text : formatter -> string -> unit >> << val pp_get_ellipsis_text : formatter -> unit -> string >> << val pp_set_formatter_out_channel : formatter -> Pervasives.out_channel -> unit >> << val pp_set_formatter_output_functions : formatter -> (string -> int -> int -> unit) -> (unit -> unit) -> unit >> << val pp_get_formatter_output_functions : formatter -> unit -> (string -> int -> int -> unit) * (unit -> unit) >> << val pp_set_formatter_tag_functions : formatter -> formatter_tag_functions -> unit >> << val pp_get_formatter_tag_functions : formatter -> unit -> formatter_tag_functions >> << val pp_set_formatter_out_functions : formatter -> formatter_out_functions -> unit >> Since: 4.01.0 << val pp_get_formatter_out_functions : formatter -> unit -> formatter_out_functions >> These functions are the basic ones: usual functions operating on the standard formatter are defined via partial evaluation of these primitives. For instance, print_string is equal to pp_print_string std_formatter. Since: 4.01.0 << val pp_flush_formatter : formatter -> unit >> pp_flush_formatter fmt flushes fmt's internal queue, ensuring that all the printing and flushing actions have been performed. In addition, this operation will close all boxes and reset the state of the formatter. This will not flush fmt's output. In most cases, the user may want to use Format.pp_print_flush[24.13] instead. Since: 4.04.0 Convenience formatting functions. ================================= << val pp_print_list : ?pp_sep:(formatter -> unit -> unit) -> (formatter -> 'a -> unit) -> formatter -> 'a list -> unit >> pp_print_list ?pp_sep pp_v ppf l prints items of list l, using pp_v to print each item, and calling pp_sep between items (pp_sep defaults to Format.pp_print_cut[24.13]). Does nothing on empty lists. Since: 4.02.0 << val pp_print_text : formatter -> string -> unit >> pp_print_text ppf s prints s with spaces and newlines respectively printed with Format.pp_print_space[24.13] and Format.pp_force_newline[24.13]. Since: 4.02.0 printf like functions for pretty-printing. ========================================== << val fprintf : formatter -> ('a, formatter, unit) Pervasives.format -> 'a >> fprintf ff fmt arg1 ... argN formats the arguments arg1 to argN according to the format string fmt, and outputs the resulting string on the formatter ff. The format fmt is a character string which contains three types of objects: plain characters and conversion specifications as specified in the Printf[24.30] module, and pretty-printing indications specific to the Format module. The pretty-printing indication characters are introduced by a @ character, and their meanings are: - @[: open a pretty-printing box. The type and offset of the box may be optionally specified with the following syntax: the < character, followed by an optional box type indication, then an optional integer offset, and the closing > character. Box type is one of h, v, hv, b, or hov. 'h' stands for an 'horizontal' box, 'v' stands for a 'vertical' box, 'hv' stands for an 'horizontal-vertical' box, 'b' stands for an 'horizontal-or-vertical' box demonstrating indentation, 'hov' stands a simple 'horizontal-or-vertical' box. For instance, @[ opens an 'horizontal-or-vertical' box with indentation 2 as obtained with open_hovbox 2. For more details about boxes, see the various box opening functions open_*box. - @]: close the most recently opened pretty-printing box. - @,: output a 'cut' break hint, as with print_cut (). - @ : output a 'space' break hint, as with print_space (). - @;: output a 'full' break hint as with print_break. The nspaces and offset parameters of the break hint may be optionally specified with the following syntax: the < character, followed by an integer nspaces value, then an integer offset, and a closing > character. If no parameters are provided, the good break defaults to a 'space' break hint. - @.: flush the pretty printer and split the line, as with print_newline (). - @: print the following item as if it were of length n. Hence, printf "@<0>%s" arg prints arg as a zero length string. If @ is not followed by a conversion specification, then the following character of the format is printed as if it were of length n. - @{: open a tag. The name of the tag may be optionally specified with the following syntax: the < character, followed by an optional string specification, and the closing > character. The string specification is any character string that does not contain the closing character '>'. If omitted, the tag name defaults to the empty string. For more details about tags, see the functions open_tag and close_tag. - @}: close the most recently opened tag. - @?: flush the pretty printer as with print_flush (). This is equivalent to the conversion %!. - @\n: force a newline, as with force_newline (), not the normal way of pretty-printing, you should prefer using break hints inside a vertical box. Note: If you need to prevent the interpretation of a @ character as a pretty-printing indication, you must escape it with a % character. Old quotation mode @@ is deprecated since it is not compatible with formatted input interpretation of character '@'. Example: printf "@[%s@ %d@]@." "x =" 1 is equivalent to open_box (); print_string "x ="; print_space (); print_int 1; close_box (); print_newline (). It prints x = 1 within a pretty-printing 'horizontal-or-vertical' box. << val printf : ('a, formatter, unit) Pervasives.format -> 'a >> Same as fprintf above, but output on std_formatter. << val eprintf : ('a, formatter, unit) Pervasives.format -> 'a >> Same as fprintf above, but output on err_formatter. << val sprintf : ('a, unit, string) Pervasives.format -> 'a >> Same as printf above, but instead of printing on a formatter, returns a string containing the result of formatting the arguments. Note that the pretty-printer queue is flushed at the end of each call to sprintf. In case of multiple and related calls to sprintf to output material on a single string, you should consider using fprintf with the predefined formatter str_formatter and call flush_str_formatter () to get the final result. Alternatively, you can use Format.fprintf with a formatter writing to a buffer of your own: flushing the formatter and the buffer at the end of pretty-printing returns the desired string. << val asprintf : ('a, formatter, unit, string) Pervasives.format4 -> 'a >> Same as printf above, but instead of printing on a formatter, returns a string containing the result of formatting the arguments. The type of asprintf is general enough to interact nicely with %a conversions. Since: 4.01.0 << val ifprintf : formatter -> ('a, formatter, unit) Pervasives.format -> 'a >> Same as fprintf above, but does not print anything. Useful to ignore some material when conditionally printing. Since: 3.10.0 Formatted output functions with continuations. << val kfprintf : (formatter -> 'a) -> formatter -> ('b, formatter, unit, 'a) Pervasives.format4 -> 'b >> Same as fprintf above, but instead of returning immediately, passes the formatter to its first argument at the end of printing. << val ikfprintf : (formatter -> 'a) -> formatter -> ('b, formatter, unit, 'a) Pervasives.format4 -> 'b >> Same as kfprintf above, but does not print anything. Useful to ignore some material when conditionally printing. Since: 3.12.0 << val ksprintf : (string -> 'a) -> ('b, unit, string, 'a) Pervasives.format4 -> 'b >> Same as sprintf above, but instead of returning the string, passes it to the first argument. << val kasprintf : (string -> 'a) -> ('b, formatter, unit, 'a) Pervasives.format4 -> 'b >> Same as asprintf above, but instead of returning the string, passes it to the first argument. Since: 4.03 Deprecated ========== << val bprintf : Buffer.t -> ('a, formatter, unit) Pervasives.format -> 'a >> Deprecated. This function is error prone. Do not use it. If you need to print to some buffer b, you must first define a formatter writing to b, using let to_b = formatter_of_buffer b; then use regular calls to Format.fprintf on formatter to_b. << val kprintf : (string -> 'a) -> ('b, unit, string, 'a) Pervasives.format4 -> 'b >> Deprecated. An alias for ksprintf. << val set_all_formatter_output_functions : out:(string -> int -> int -> unit) -> flush:(unit -> unit) -> newline:(unit -> unit) -> spaces:(int -> unit) -> unit >> Deprecated. Subsumed by set_formatter_out_functions. << val get_all_formatter_output_functions : unit -> (string -> int -> int -> unit) * (unit -> unit) * (unit -> unit) * (int -> unit) >> Deprecated. Subsumed by get_formatter_out_functions. << val pp_set_all_formatter_output_functions : formatter -> out:(string -> int -> int -> unit) -> flush:(unit -> unit) -> newline:(unit -> unit) -> spaces:(int -> unit) -> unit >> Deprecated. Subsumed by pp_set_formatter_out_functions. << val pp_get_all_formatter_output_functions : formatter -> unit -> (string -> int -> int -> unit) * (unit -> unit) * (unit -> unit) * (int -> unit) >> Deprecated. Subsumed by pp_get_formatter_out_functions. Tabulation boxes are deprecated. << val pp_open_tbox : formatter -> unit -> unit >> Deprecated. since 4.03.0 << val pp_close_tbox : formatter -> unit -> unit >> Deprecated. since 4.03.0 << val pp_print_tbreak : formatter -> int -> int -> unit >> Deprecated. since 4.03.0 << val pp_set_tab : formatter -> unit -> unit >> Deprecated. since 4.03.0 << val pp_print_tab : formatter -> unit -> unit >> Deprecated. since 4.03.0 << val open_tbox : unit -> unit >> Deprecated. since 4.03.0 << val close_tbox : unit -> unit >> Deprecated. since 4.03.0 << val print_tbreak : int -> int -> unit >> Deprecated. since 4.03.0 << val set_tab : unit -> unit >> Deprecated. since 4.03.0 << val print_tab : unit -> unit >> Deprecated. since 4.03.0 24.14 Module Gc : Memory management control and statistics; finalised values. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= << type stat = { minor_words : float ; >> Number of words allocated in the minor heap since the program was started. This number is accurate in byte-code programs, but only an approximation in programs compiled to native code. << promoted_words : float ; >> Number of words allocated in the minor heap that survived a minor collection and were moved to the major heap since the program was started. << major_words : float ; >> Number of words allocated in the major heap, including the promoted words, since the program was started. << minor_collections : int ; >> Number of minor collections since the program was started. << major_collections : int ; >> Number of major collection cycles completed since the program was started. << heap_words : int ; >> Total size of the major heap, in words. << heap_chunks : int ; >> Number of contiguous pieces of memory that make up the major heap. << live_words : int ; >> Number of words of live data in the major heap, including the header words. << live_blocks : int ; >> Number of live blocks in the major heap. << free_words : int ; >> Number of words in the free list. << free_blocks : int ; >> Number of blocks in the free list. << largest_free : int ; >> Size (in words) of the largest block in the free list. << fragments : int ; >> Number of wasted words due to fragmentation. These are 1-words free blocks placed between two live blocks. They are not available for allocation. << compactions : int ; >> Number of heap compactions since the program was started. << top_heap_words : int ; >> Maximum size reached by the major heap, in words. << stack_size : int ; >> Current size of the stack, in words. Since: 3.12.0 << } >> The memory management counters are returned in a stat record. The total amount of memory allocated by the program since it was started is (in words) minor_words + major_words - promoted_words. Multiply by the word size (4 on a 32-bit machine, 8 on a 64-bit machine) to get the number of bytes. << type control = { mutable minor_heap_size : int ; >> The size (in words) of the minor heap. Changing this parameter will trigger a minor collection. Default: 256k. << mutable major_heap_increment : int ; >> How much to add to the major heap when increasing it. If this number is less than or equal to 1000, it is a percentage of the current heap size (i.e. setting it to 100 will double the heap size at each increase). If it is more than 1000, it is a fixed number of words that will be added to the heap. Default: 15. << mutable space_overhead : int ; >> The major GC speed is computed from this parameter. This is the memory that will be "wasted" because the GC does not immediatly collect unreachable blocks. It is expressed as a percentage of the memory used for live data. The GC will work more (use more CPU time and collect blocks more eagerly) if space_overhead is smaller. Default: 80. << mutable verbose : int ; >> This value controls the GC messages on standard error output. It is a sum of some of the following flags, to print messages on the corresponding events: - 0x001 Start of major GC cycle. - 0x002 Minor collection and major GC slice. - 0x004 Growing and shrinking of the heap. - 0x008 Resizing of stacks and memory manager tables. - 0x010 Heap compaction. - 0x020 Change of GC parameters. - 0x040 Computation of major GC slice size. - 0x080 Calling of finalisation functions. - 0x100 Bytecode executable and shared library search at start-up. - 0x200 Computation of compaction-triggering condition. - 0x400 Output GC statistics at program exit. Default: 0. << mutable max_overhead : int ; >> Heap compaction is triggered when the estimated amount of "wasted" memory is more than max_overhead percent of the amount of live data. If max_overhead is set to 0, heap compaction is triggered at the end of each major GC cycle (this setting is intended for testing purposes only). If max_overhead >= 1000000, compaction is never triggered. If compaction is permanently disabled, it is strongly suggested to set allocation_policy to 1. Default: 500. << mutable stack_limit : int ; >> The maximum size of the stack (in words). This is only relevant to the byte-code runtime, as the native code runtime uses the operating system's stack. Default: 1024k. << mutable allocation_policy : int ; >> The policy used for allocating in the heap. Possible values are 0 and 1. 0 is the next-fit policy, which is quite fast but can result in fragmentation. 1 is the first-fit policy, which can be slower in some cases but can be better for programs with fragmentation problems. Default: 0. Since: 3.11.0 << window_size : int ; >> The size of the window used by the major GC for smoothing out variations in its workload. This is an integer between 1 and 50. Default: 1. Since: 4.03.0 << } >> The GC parameters are given as a control record. Note that these parameters can also be initialised by setting the OCAMLRUNPARAM environment variable. See the documentation of ocamlrun. << val stat : unit -> stat >> Return the current values of the memory management counters in a stat record. This function examines every heap block to get the statistics. << val quick_stat : unit -> stat >> Same as stat except that live_words, live_blocks, free_words, free_blocks, largest_free, and fragments are set to 0. This function is much faster than stat because it does not need to go through the heap. << val counters : unit -> float * float * float >> Return (minor_words, promoted_words, major_words). This function is as fast as quick_stat. << val minor_words : unit -> float >> Number of words allocated in the minor heap since the program was started. This number is accurate in byte-code programs, but only an approximation in programs compiled to native code. In native code this function does not allocate. Since: 4.04 << val get : unit -> control >> Return the current values of the GC parameters in a control record. << val set : control -> unit >> set r changes the GC parameters according to the control record r. The normal usage is: Gc.set { (Gc.get()) with Gc.verbose = 0x00d } << val minor : unit -> unit >> Trigger a minor collection. << val major_slice : int -> int >> major_slice n Do a minor collection and a slice of major collection. n is the size of the slice: the GC will do enough work to free (on average) n words of memory. If n = 0, the GC will try to do enough work to ensure that the next automatic slice has no work to do. This function returns an unspecified integer (currently: 0). << val major : unit -> unit >> Do a minor collection and finish the current major collection cycle. << val full_major : unit -> unit >> Do a minor collection, finish the current major collection cycle, and perform a complete new cycle. This will collect all currently unreachable blocks. << val compact : unit -> unit >> Perform a full major collection and compact the heap. Note that heap compaction is a lengthy operation. << val print_stat : Pervasives.out_channel -> unit >> Print the current values of the memory management counters (in human-readable form) into the channel argument. << val allocated_bytes : unit -> float >> Return the total number of bytes allocated since the program was started. It is returned as a float to avoid overflow problems with int on 32-bit machines. << val get_minor_free : unit -> int >> Return the current size of the free space inside the minor heap. Since: 4.03.0 << val get_bucket : int -> int >> get_bucket n returns the current size of the n-th future bucket of the GC smoothing system. The unit is one millionth of a full GC. Raise Invalid_argument if n is negative, return 0 if n is larger than the smoothing window. Since: 4.03.0 << val get_credit : unit -> int >> get_credit () returns the current size of the "work done in advance" counter of the GC smoothing system. The unit is one millionth of a full GC. Since: 4.03.0 << val huge_fallback_count : unit -> int >> Return the number of times we tried to map huge pages and had to fall back to small pages. This is always 0 if OCAMLRUNPARAM contains H=1. Since: 4.03.0 << val finalise : ('a -> unit) -> 'a -> unit >> finalise f v registers f as a finalisation function for v. v must be heap-allocated. f will be called with v as argument at some point between the first time v becomes unreachable (including through weak pointers) and the time v is collected by the GC. Several functions can be registered for the same value, or even several instances of the same function. Each instance will be called once (or never, if the program terminates before v becomes unreachable). The GC will call the finalisation functions in the order of deallocation. When several values become unreachable at the same time (i.e. during the same GC cycle), the finalisation functions will be called in the reverse order of the corresponding calls to finalise. If finalise is called in the same order as the values are allocated, that means each value is finalised before the values it depends upon. Of course, this becomes false if additional dependencies are introduced by assignments. In the presence of multiple OCaml threads it should be assumed that any particular finaliser may be executed in any of the threads. Anything reachable from the closure of finalisation functions is considered reachable, so the following code will not work as expected: - let v = ... in Gc.finalise (fun _ -> ...v...) v Instead you should make sure that v is not in the closure of the finalisation function by writing: - let f = fun x -> ... let v = ... in Gc.finalise f v The f function can use all features of OCaml, including assignments that make the value reachable again. It can also loop forever (in this case, the other finalisation functions will not be called during the execution of f, unless it calls finalise_release). It can call finalise on v or other values to register other functions or even itself. It can raise an exception; in this case the exception will interrupt whatever the program was doing when the function was called. finalise will raise Invalid_argument if v is not guaranteed to be heap-allocated. Some examples of values that are not heap-allocated are integers, constant constructors, booleans, the empty array, the empty list, the unit value. The exact list of what is heap-allocated or not is implementation-dependent. Some constant values can be heap-allocated but never deallocated during the lifetime of the program, for example a list of integer constants; this is also implementation-dependent. Note that values of types float are sometimes allocated and sometimes not, so finalising them is unsafe, and finalise will also raise Invalid_argument for them. Values of type 'a Lazy.t (for any 'a) are like float in this respect, except that the compiler sometimes optimizes them in a way that prevents finalise from detecting them. In this case, it will not raise Invalid_argument, but you should still avoid calling finalise on lazy values. The results of calling String.make[25.8], Bytes.make[24.5], Bytes.create[24.5], Array.make[24.2], and Pervasives.ref[23.2] are guaranteed to be heap-allocated and non-constant except when the length argument is 0. << val finalise_last : (unit -> unit) -> 'a -> unit >> same as Gc.finalise[24.14] except the value is not given as argument. So you can't use the given value for the computation of the finalisation function. The benefit is that the function is called after the value is unreachable for the last time instead of the first time. So contrary to Gc.finalise[24.14] the value will never be reachable again or used again. In particular every weak pointer and ephemeron that contained this value as key or data is unset before running the finalisation function. Moreover the finalisation function attached with `GC.finalise` are always called before the finalisation function attached with `GC.finalise_last`. Since: 4.04 << val finalise_release : unit -> unit >> A finalisation function may call finalise_release to tell the GC that it can launch the next finalisation function without waiting for the current one to return. << type alarm >> An alarm is a piece of data that calls a user function at the end of each major GC cycle. The following functions are provided to create and delete alarms. << val create_alarm : (unit -> unit) -> alarm >> create_alarm f will arrange for f to be called at the end of each major GC cycle, starting with the current cycle or the next one. A value of type alarm is returned that you can use to call delete_alarm. << val delete_alarm : alarm -> unit >> delete_alarm a will stop the calls to the function associated to a. Calling delete_alarm a again has no effect. 24.15 Module Genlex : A generic lexical analyzer. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= This module implements a simple 'standard' lexical analyzer, presented as a function from character streams to token streams. It implements roughly the lexical conventions of OCaml, but is parameterized by the set of keywords of your language. Example: a lexer suitable for a desk calculator is obtained by << let lexer = make_lexer ["+";"-";"*";"/";"let";"="; "("; ")"] >> The associated parser would be a function from token stream to, for instance, int, and would have rules such as: << let rec parse_expr = parser | [< n1 = parse_atom; n2 = parse_remainder n1 >] -> n2 and parse_atom = parser | [< 'Int n >] -> n | [< 'Kwd "("; n = parse_expr; 'Kwd ")" >] -> n and parse_remainder n1 = parser | [< 'Kwd "+"; n2 = parse_expr >] -> n1+n2 | [< >] -> n1 >> One should notice that the use of the parser keyword and associated notation for streams are only available through camlp4 extensions. This means that one has to preprocess its sources e. g. by using the "-pp" command-line switch of the compilers. << type token = | Kwd of string | Ident of string | Int of int | Float of float | String of string | Char of char >> The type of tokens. The lexical classes are: Int and Float for integer and floating-point numbers; String for string literals, enclosed in double quotes; Char for character literals, enclosed in single quotes; Ident for identifiers (either sequences of letters, digits, underscores and quotes, or sequences of 'operator characters' such as +, *, etc); and Kwd for keywords (either identifiers or single 'special characters' such as (, }, etc). << val make_lexer : string list -> char Stream.t -> token Stream.t >> Construct the lexer function. The first argument is the list of keywords. An identifier s is returned as Kwd s if s belongs to this list, and as Ident s otherwise. A special character s is returned as Kwd s if s belongs to this list, and cause a lexical error (exception Stream.Error[??] with the offending lexeme as its parameter) otherwise. Blanks and newlines are skipped. Comments delimited by (* and *) are skipped as well, and can be nested. A Stream.Failure[??] exception is raised if end of stream is unexpectedly reached. 24.16 Module Hashtbl : Hash tables and hash functions. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* Hash tables are hashed association tables, with in-place modification. Generic interface ================= << type ('a, 'b) t >> The type of hash tables from type 'a to type 'b. << val create : ?random:bool -> int -> ('a, 'b) t >> Hashtbl.create n creates a new, empty hash table, with initial size n. For best results, n should be on the order of the expected number of elements that will be in the table. The table grows as needed, so n is just an initial guess. The optional random parameter (a boolean) controls whether the internal organization of the hash table is randomized at each execution of Hashtbl.create or deterministic over all executions. A hash table that is created with ~random:false uses a fixed hash function (Hashtbl.hash[24.16]) to distribute keys among buckets. As a consequence, collisions between keys happen deterministically. In Web-facing applications or other security-sensitive applications, the deterministic collision patterns can be exploited by a malicious user to create a denial-of-service attack: the attacker sends input crafted to create many collisions in the table, slowing the application down. A hash table that is created with ~random:true uses the seeded hash function Hashtbl.seeded_hash[24.16] with a seed that is randomly chosen at hash table creation time. In effect, the hash function used is randomly selected among 2^{30} different hash functions. All these hash functions have different collision patterns, rendering ineffective the denial-of-service attack described above. However, because of randomization, enumerating all elements of the hash table using Hashtbl.fold[24.16] or Hashtbl.iter[24.16] is no longer deterministic: elements are enumerated in different orders at different runs of the program. If no ~random parameter is given, hash tables are created in non-random mode by default. This default can be changed either programmatically by calling Hashtbl.randomize[24.16] or by setting the R flag in the OCAMLRUNPARAM environment variable. Before 4.00.0 the random parameter was not present and all hash tables were created in non-randomized mode. << val clear : ('a, 'b) t -> unit >> Empty a hash table. Use reset instead of clear to shrink the size of the bucket table to its initial size. << val reset : ('a, 'b) t -> unit >> Empty a hash table and shrink the size of the bucket table to its initial size. Since: 4.00.0 << val copy : ('a, 'b) t -> ('a, 'b) t >> Return a copy of the given hashtable. << val add : ('a, 'b) t -> 'a -> 'b -> unit >> Hashtbl.add tbl x y adds a binding of x to y in table tbl. Previous bindings for x are not removed, but simply hidden. That is, after performing Hashtbl.remove[24.16] tbl x, the previous binding for x, if any, is restored. (Same behavior as with association lists.) << val find : ('a, 'b) t -> 'a -> 'b >> Hashtbl.find tbl x returns the current binding of x in tbl, or raises Not_found if no such binding exists. << val find_opt : ('a, 'b) t -> 'a -> 'b option >> Hashtbl.find_opt tbl x returns the current binding of x in tbl, or None if no such binding exists. Since: 4.05 << val find_all : ('a, 'b) t -> 'a -> 'b list >> Hashtbl.find_all tbl x returns the list of all data associated with x in tbl. The current binding is returned first, then the previous bindings, in reverse order of introduction in the table. << val mem : ('a, 'b) t -> 'a -> bool >> Hashtbl.mem tbl x checks if x is bound in tbl. << val remove : ('a, 'b) t -> 'a -> unit >> Hashtbl.remove tbl x removes the current binding of x in tbl, restoring the previous binding if it exists. It does nothing if x is not bound in tbl. << val replace : ('a, 'b) t -> 'a -> 'b -> unit >> Hashtbl.replace tbl x y replaces the current binding of x in tbl by a binding of x to y. If x is unbound in tbl, a binding of x to y is added to tbl. This is functionally equivalent to Hashtbl.remove[24.16] tbl x followed by Hashtbl.add[24.16] tbl x y. << val iter : ('a -> 'b -> unit) -> ('a, 'b) t -> unit >> Hashtbl.iter f tbl applies f to all bindings in table tbl. f receives the key as first argument, and the associated value as second argument. Each binding is presented exactly once to f. The order in which the bindings are passed to f is unspecified. However, if the table contains several bindings for the same key, they are passed to f in reverse order of introduction, that is, the most recent binding is passed first. If the hash table was created in non-randomized mode, the order in which the bindings are enumerated is reproducible between successive runs of the program, and even between minor versions of OCaml. For randomized hash tables, the order of enumeration is entirely random. The behavior is not defined if the hash table is modified by f during the iteration. << val filter_map_inplace : ('a -> 'b -> 'b option) -> ('a, 'b) t -> unit >> Hashtbl.filter_map_inplace f tbl applies f to all bindings in table tbl and update each binding depending on the result of f. If f returns None, the binding is discarded. If it returns Some new_val, the binding is update to associate the key to new_val. Other comments for Hashtbl.iter[24.16] apply as well. Since: 4.03.0 << val fold : ('a -> 'b -> 'c -> 'c) -> ('a, 'b) t -> 'c -> 'c >> Hashtbl.fold f tbl init computes (f kN dN ... (f k1 d1 init)...), where k1 ... kN are the keys of all bindings in tbl, and d1 ... dN are the associated values. Each binding is presented exactly once to f. The order in which the bindings are passed to f is unspecified. However, if the table contains several bindings for the same key, they are passed to f in reverse order of introduction, that is, the most recent binding is passed first. If the hash table was created in non-randomized mode, the order in which the bindings are enumerated is reproducible between successive runs of the program, and even between minor versions of OCaml. For randomized hash tables, the order of enumeration is entirely random. The behavior is not defined if the hash table is modified by f during the iteration. << val length : ('a, 'b) t -> int >> Hashtbl.length tbl returns the number of bindings in tbl. It takes constant time. Multiple bindings are counted once each, so Hashtbl.length gives the number of times Hashtbl.iter calls its first argument. << val randomize : unit -> unit >> After a call to Hashtbl.randomize(), hash tables are created in randomized mode by default: Hashtbl.create[24.16] returns randomized hash tables, unless the ~random:false optional parameter is given. The same effect can be achieved by setting the R parameter in the OCAMLRUNPARAM environment variable. It is recommended that applications or Web frameworks that need to protect themselves against the denial-of-service attack described in Hashtbl.create[24.16] call Hashtbl.randomize() at initialization time. Note that once Hashtbl.randomize() was called, there is no way to revert to the non-randomized default behavior of Hashtbl.create[24.16]. This is intentional. Non-randomized hash tables can still be created using Hashtbl.create ~random:false. Since: 4.00.0 << val is_randomized : unit -> bool >> return if the tables are currently created in randomized mode by default Since: 4.03.0 << type statistics = { num_bindings : int ; >> Number of bindings present in the table. Same value as returned by Hashtbl.length[24.16]. << num_buckets : int ; >> Number of buckets in the table. << max_bucket_length : int ; >> Maximal number of bindings per bucket. << bucket_histogram : int array ; >> Histogram of bucket sizes. This array histo has length max_bucket_length + 1. The value of histo.(i) is the number of buckets whose size is i. << } >> Since: 4.00.0 << val stats : ('a, 'b) t -> statistics >> Hashtbl.stats tbl returns statistics about the table tbl: number of buckets, size of the biggest bucket, distribution of buckets by size. Since: 4.00.0 Functorial interface ==================== The functorial interface allows the use of specific comparison and hash functions, either for performance/security concerns, or because keys are not hashable/comparable with the polymorphic builtins. For instance, one might want to specialize a table for integer keys: << module IntHash = struct type t = int let equal i j = i=j let hash i = i land max_int end module IntHashtbl = Hashtbl.Make(IntHash) let h = IntHashtbl.create 17 in IntHashtbl.add h 12 "hello" >> This creates a new module IntHashtbl, with a new type 'a IntHashtbl.t of tables from int to 'a. In this example, h contains string values so its type is string IntHashtbl.t. Note that the new type 'a IntHashtbl.t is not compatible with the type ('a,'b) Hashtbl.t of the generic interface. For example, Hashtbl.length h would not type-check, you must use IntHashtbl.length. << module type HashedType = >> sig << type t >> The type of the hashtable keys. << val equal : t -> t -> bool >> The equality predicate used to compare keys. << val hash : t -> int >> A hashing function on keys. It must be such that if two keys are equal according to equal, then they have identical hash values as computed by hash. Examples: suitable (equal, hash) pairs for arbitrary key types include - ((=), Hashtbl.hash[24.16]) for comparing objects by structure (provided objects do not contain floats) - ((fun x y -> compare x y = 0), Hashtbl.hash[24.16]) for comparing objects by structure and handling Pervasives.nan[23.2] correctly - ((==), Hashtbl.hash[24.16]) for comparing objects by physical equality (e.g. for mutable or cyclic objects). end The input signature of the functor Hashtbl.Make[24.16]. << module type S = >> sig << type key >> << type 'a t >> << val create : int -> 'a t >> << val clear : 'a t -> unit >> << val reset : 'a t -> unit >> Since: 4.00.0 << val copy : 'a t -> 'a t >> << val add : 'a t -> key -> 'a -> unit >> << val remove : 'a t -> key -> unit >> << val find : 'a t -> key -> 'a >> << val find_opt : 'a t -> key -> 'a option >> Since: 4.05.0 << val find_all : 'a t -> key -> 'a list >> << val replace : 'a t -> key -> 'a -> unit >> << val mem : 'a t -> key -> bool >> << val iter : (key -> 'a -> unit) -> 'a t -> unit >> << val filter_map_inplace : (key -> 'a -> 'a option) -> 'a t -> unit >> Since: 4.03.0 << val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b >> << val length : 'a t -> int >> << val stats : 'a t -> Hashtbl.statistics >> Since: 4.00.0 end The output signature of the functor Hashtbl.Make[24.16]. << module Make : >> functor (H : HashedType) -> S with type key = H.t Functor building an implementation of the hashtable structure. The functor Hashtbl.Make returns a structure containing a type key of keys and a type 'a t of hash tables associating data of type 'a to keys of type key. The operations perform similarly to those of the generic interface, but use the hashing and equality functions specified in the functor argument H instead of generic equality and hashing. Since the hash function is not seeded, the create operation of the result structure always returns non-randomized hash tables. << module type SeededHashedType = >> sig << type t >> The type of the hashtable keys. << val equal : t -> t -> bool >> The equality predicate used to compare keys. << val hash : int -> t -> int >> A seeded hashing function on keys. The first argument is the seed. It must be the case that if equal x y is true, then hash seed x = hash seed y for any value of seed. A suitable choice for hash is the function Hashtbl.seeded_hash[24.16] below. end The input signature of the functor Hashtbl.MakeSeeded[24.16]. Since: 4.00.0 << module type SeededS = >> sig << type key >> << type 'a t >> << val create : ?random:bool -> int -> 'a t >> << val clear : 'a t -> unit >> << val reset : 'a t -> unit >> << val copy : 'a t -> 'a t >> << val add : 'a t -> key -> 'a -> unit >> << val remove : 'a t -> key -> unit >> << val find : 'a t -> key -> 'a >> << val find_opt : 'a t -> key -> 'a option >> Since: 4.05.0 << val find_all : 'a t -> key -> 'a list >> << val replace : 'a t -> key -> 'a -> unit >> << val mem : 'a t -> key -> bool >> << val iter : (key -> 'a -> unit) -> 'a t -> unit >> << val filter_map_inplace : (key -> 'a -> 'a option) -> 'a t -> unit >> Since: 4.03.0 << val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b >> << val length : 'a t -> int >> << val stats : 'a t -> Hashtbl.statistics >> end The output signature of the functor Hashtbl.MakeSeeded[24.16]. Since: 4.00.0 << module MakeSeeded : >> functor (H : SeededHashedType) -> SeededS with type key = H.t Functor building an implementation of the hashtable structure. The functor Hashtbl.MakeSeeded returns a structure containing a type key of keys and a type 'a t of hash tables associating data of type 'a to keys of type key. The operations perform similarly to those of the generic interface, but use the seeded hashing and equality functions specified in the functor argument H instead of generic equality and hashing. The create operation of the result structure supports the ~random optional parameter and returns randomized hash tables if ~random:true is passed or if randomization is globally on (see Hashtbl.randomize[24.16]). Since: 4.00.0 The polymorphic hash functions ============================== << val hash : 'a -> int >> Hashtbl.hash x associates a nonnegative integer to any value of any type. It is guaranteed that if x = y or Pervasives.compare x y = 0, then hash x = hash y. Moreover, hash always terminates, even on cyclic structures. << val seeded_hash : int -> 'a -> int >> A variant of Hashtbl.hash[24.16] that is further parameterized by an integer seed. Since: 4.00.0 << val hash_param : int -> int -> 'a -> int >> Hashtbl.hash_param meaningful total x computes a hash value for x, with the same properties as for hash. The two extra integer parameters meaningful and total give more precise control over hashing. Hashing performs a breadth-first, left-to-right traversal of the structure x, stopping after meaningful meaningful nodes were encountered, or total nodes (meaningful or not) were encountered. If total as specified by the user exceeds a certain value, currently 256, then it is capped to that value. Meaningful nodes are: integers; floating-point numbers; strings; characters; booleans; and constant constructors. Larger values of meaningful and total means that more nodes are taken into account to compute the final hash value, and therefore collisions are less likely to happen. However, hashing takes longer. The parameters meaningful and total govern the tradeoff between accuracy and speed. As default choices, Hashtbl.hash[24.16] and Hashtbl.seeded_hash[24.16] take meaningful = 10 and total = 100. << val seeded_hash_param : int -> int -> int -> 'a -> int >> A variant of Hashtbl.hash_param[24.16] that is further parameterized by an integer seed. Usage: Hashtbl.seeded_hash_param meaningful total seed x. Since: 4.00.0 24.17 Module Int32 : 32-bit integers. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= This module provides operations on the type int32 of signed 32-bit integers. Unlike the built-in int type, the type int32 is guaranteed to be exactly 32-bit wide on all platforms. All arithmetic operations over int32 are taken modulo 2^32. Performance notice: values of type int32 occupy more memory space than values of type int, and arithmetic operations on int32 are generally slower than those on int. Use int32 only when the application requires exact 32-bit arithmetic. << val zero : int32 >> The 32-bit integer 0. << val one : int32 >> The 32-bit integer 1. << val minus_one : int32 >> The 32-bit integer -1. << val neg : int32 -> int32 >> Unary negation. << val add : int32 -> int32 -> int32 >> Addition. << val sub : int32 -> int32 -> int32 >> Subtraction. << val mul : int32 -> int32 -> int32 >> Multiplication. << val div : int32 -> int32 -> int32 >> Integer division. Raise Division_by_zero if the second argument is zero. This division rounds the real quotient of its arguments towards zero, as specified for Pervasives.(/)[23.2]. << val rem : int32 -> int32 -> int32 >> Integer remainder. If y is not zero, the result of Int32.rem x y satisfies the following property: x = Int32.add (Int32.mul (Int32.div x y) y) (Int32.rem x y). If y = 0, Int32.rem x y raises Division_by_zero. << val succ : int32 -> int32 >> Successor. Int32.succ x is Int32.add x Int32.one. << val pred : int32 -> int32 >> Predecessor. Int32.pred x is Int32.sub x Int32.one. << val abs : int32 -> int32 >> Return the absolute value of its argument. << val max_int : int32 >> The greatest representable 32-bit integer, 2^31 - 1. << val min_int : int32 >> The smallest representable 32-bit integer, -2^31. << val logand : int32 -> int32 -> int32 >> Bitwise logical and. << val logor : int32 -> int32 -> int32 >> Bitwise logical or. << val logxor : int32 -> int32 -> int32 >> Bitwise logical exclusive or. << val lognot : int32 -> int32 >> Bitwise logical negation << val shift_left : int32 -> int -> int32 >> Int32.shift_left x y shifts x to the left by y bits. The result is unspecified if y < 0 or y >= 32. << val shift_right : int32 -> int -> int32 >> Int32.shift_right x y shifts x to the right by y bits. This is an arithmetic shift: the sign bit of x is replicated and inserted in the vacated bits. The result is unspecified if y < 0 or y >= 32. << val shift_right_logical : int32 -> int -> int32 >> Int32.shift_right_logical x y shifts x to the right by y bits. This is a logical shift: zeroes are inserted in the vacated bits regardless of the sign of x. The result is unspecified if y < 0 or y >= 32. << val of_int : int -> int32 >> Convert the given integer (type int) to a 32-bit integer (type int32). << val to_int : int32 -> int >> Convert the given 32-bit integer (type int32) to an integer (type int). On 32-bit platforms, the 32-bit integer is taken modulo 2^31, i.e. the high-order bit is lost during the conversion. On 64-bit platforms, the conversion is exact. << val of_float : float -> int32 >> Convert the given floating-point number to a 32-bit integer, discarding the fractional part (truncate towards 0). The result of the conversion is undefined if, after truncation, the number is outside the range [Int32.min_int[24.17], Int32.max_int[24.17]]. << val to_float : int32 -> float >> Convert the given 32-bit integer to a floating-point number. << val of_string : string -> int32 >> Convert the given string to a 32-bit integer. The string is read in decimal (by default) or in hexadecimal, octal or binary if the string begins with 0x, 0o or 0b respectively. Raise Failure "int_of_string" if the given string is not a valid representation of an integer, or if the integer represented exceeds the range of integers representable in type int32. << val of_string_opt : string -> int32 option >> Same as of_string, but return None instead of raising. Since: 4.05 << val to_string : int32 -> string >> Return the string representation of its argument, in signed decimal. << val bits_of_float : float -> int32 >> Return the internal representation of the given float according to the IEEE 754 floating-point 'single format' bit layout. Bit 31 of the result represents the sign of the float; bits 30 to 23 represent the (biased) exponent; bits 22 to 0 represent the mantissa. << val float_of_bits : int32 -> float >> Return the floating-point number whose internal representation, according to the IEEE 754 floating-point 'single format' bit layout, is the given int32. << type t = int32 >> An alias for the type of 32-bit integers. << val compare : t -> t -> int >> The comparison function for 32-bit integers, with the same specification as Pervasives.compare[23.2]. Along with the type t, this function compare allows the module Int32 to be passed as argument to the functors Set.Make[24.34] and Map.Make[24.23]. << val equal : t -> t -> bool >> The equal function for int32s. Since: 4.03.0 24.18 Module Int64 : 64-bit integers. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= This module provides operations on the type int64 of signed 64-bit integers. Unlike the built-in int type, the type int64 is guaranteed to be exactly 64-bit wide on all platforms. All arithmetic operations over int64 are taken modulo 2^64 Performance notice: values of type int64 occupy more memory space than values of type int, and arithmetic operations on int64 are generally slower than those on int. Use int64 only when the application requires exact 64-bit arithmetic. << val zero : int64 >> The 64-bit integer 0. << val one : int64 >> The 64-bit integer 1. << val minus_one : int64 >> The 64-bit integer -1. << val neg : int64 -> int64 >> Unary negation. << val add : int64 -> int64 -> int64 >> Addition. << val sub : int64 -> int64 -> int64 >> Subtraction. << val mul : int64 -> int64 -> int64 >> Multiplication. << val div : int64 -> int64 -> int64 >> Integer division. Raise Division_by_zero if the second argument is zero. This division rounds the real quotient of its arguments towards zero, as specified for Pervasives.(/)[23.2]. << val rem : int64 -> int64 -> int64 >> Integer remainder. If y is not zero, the result of Int64.rem x y satisfies the following property: x = Int64.add (Int64.mul (Int64.div x y) y) (Int64.rem x y). If y = 0, Int64.rem x y raises Division_by_zero. << val succ : int64 -> int64 >> Successor. Int64.succ x is Int64.add x Int64.one. << val pred : int64 -> int64 >> Predecessor. Int64.pred x is Int64.sub x Int64.one. << val abs : int64 -> int64 >> Return the absolute value of its argument. << val max_int : int64 >> The greatest representable 64-bit integer, 2^63 - 1. << val min_int : int64 >> The smallest representable 64-bit integer, -2^63. << val logand : int64 -> int64 -> int64 >> Bitwise logical and. << val logor : int64 -> int64 -> int64 >> Bitwise logical or. << val logxor : int64 -> int64 -> int64 >> Bitwise logical exclusive or. << val lognot : int64 -> int64 >> Bitwise logical negation << val shift_left : int64 -> int -> int64 >> Int64.shift_left x y shifts x to the left by y bits. The result is unspecified if y < 0 or y >= 64. << val shift_right : int64 -> int -> int64 >> Int64.shift_right x y shifts x to the right by y bits. This is an arithmetic shift: the sign bit of x is replicated and inserted in the vacated bits. The result is unspecified if y < 0 or y >= 64. << val shift_right_logical : int64 -> int -> int64 >> Int64.shift_right_logical x y shifts x to the right by y bits. This is a logical shift: zeroes are inserted in the vacated bits regardless of the sign of x. The result is unspecified if y < 0 or y >= 64. << val of_int : int -> int64 >> Convert the given integer (type int) to a 64-bit integer (type int64). << val to_int : int64 -> int >> Convert the given 64-bit integer (type int64) to an integer (type int). On 64-bit platforms, the 64-bit integer is taken modulo 2^63, i.e. the high-order bit is lost during the conversion. On 32-bit platforms, the 64-bit integer is taken modulo 2^31, i.e. the top 33 bits are lost during the conversion. << val of_float : float -> int64 >> Convert the given floating-point number to a 64-bit integer, discarding the fractional part (truncate towards 0). The result of the conversion is undefined if, after truncation, the number is outside the range [Int64.min_int[24.18], Int64.max_int[24.18]]. << val to_float : int64 -> float >> Convert the given 64-bit integer to a floating-point number. << val of_int32 : int32 -> int64 >> Convert the given 32-bit integer (type int32) to a 64-bit integer (type int64). << val to_int32 : int64 -> int32 >> Convert the given 64-bit integer (type int64) to a 32-bit integer (type int32). The 64-bit integer is taken modulo 2^32, i.e. the top 32 bits are lost during the conversion. << val of_nativeint : nativeint -> int64 >> Convert the given native integer (type nativeint) to a 64-bit integer (type int64). << val to_nativeint : int64 -> nativeint >> Convert the given 64-bit integer (type int64) to a native integer. On 32-bit platforms, the 64-bit integer is taken modulo 2^32. On 64-bit platforms, the conversion is exact. << val of_string : string -> int64 >> Convert the given string to a 64-bit integer. The string is read in decimal (by default) or in hexadecimal, octal or binary if the string begins with 0x, 0o or 0b respectively. Raise Failure "int_of_string" if the given string is not a valid representation of an integer, or if the integer represented exceeds the range of integers representable in type int64. << val of_string_opt : string -> int64 option >> Same as of_string, but return None instead of raising. Since: 4.05 << val to_string : int64 -> string >> Return the string representation of its argument, in decimal. << val bits_of_float : float -> int64 >> Return the internal representation of the given float according to the IEEE 754 floating-point 'double format' bit layout. Bit 63 of the result represents the sign of the float; bits 62 to 52 represent the (biased) exponent; bits 51 to 0 represent the mantissa. << val float_of_bits : int64 -> float >> Return the floating-point number whose internal representation, according to the IEEE 754 floating-point 'double format' bit layout, is the given int64. << type t = int64 >> An alias for the type of 64-bit integers. << val compare : t -> t -> int >> The comparison function for 64-bit integers, with the same specification as Pervasives.compare[23.2]. Along with the type t, this function compare allows the module Int64 to be passed as argument to the functors Set.Make[24.34] and Map.Make[24.23]. << val equal : t -> t -> bool >> The equal function for int64s. Since: 4.03.0 24.19 Module Lazy : Deferred computations. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* << type 'a t = 'a lazy_t >> A value of type 'a Lazy.t is a deferred computation, called a suspension, that has a result of type 'a. The special expression syntax lazy (expr) makes a suspension of the computation of expr, without computing expr itself yet. "Forcing" the suspension will then compute expr and return its result. Note: lazy_t is the built-in type constructor used by the compiler for the lazy keyword. You should not use it directly. Always use Lazy.t instead. Note: Lazy.force is not thread-safe. If you use this module in a multi-threaded program, you will need to add some locks. Note: if the program is compiled with the -rectypes option, ill-founded recursive definitions of the form let rec x = lazy x or let rec x = lazy(lazy(...(lazy x))) are accepted by the type-checker and lead, when forced, to ill-formed values that trigger infinite loops in the garbage collector and other parts of the run-time system. Without the -rectypes option, such ill-founded recursive definitions are rejected by the type-checker. << exception Undefined >> << val force : 'a t -> 'a >> force x forces the suspension x and returns its result. If x has already been forced, Lazy.force x returns the same value again without recomputing it. If it raised an exception, the same exception is raised again. Raise Lazy.Undefined[??] if the forcing of x tries to force x itself recursively. << val force_val : 'a t -> 'a >> force_val x forces the suspension x and returns its result. If x has already been forced, force_val x returns the same value again without recomputing it. Raise Lazy.Undefined[??] if the forcing of x tries to force x itself recursively. If the computation of x raises an exception, it is unspecified whether force_val x raises the same exception or Lazy.Undefined[??]. << val from_fun : (unit -> 'a) -> 'a t >> from_fun f is the same as lazy (f ()) but slightly more efficient. from_fun should only be used if the function f is already defined. In particular it is always less efficient to write from_fun (fun () -> expr) than lazy expr. Since: 4.00.0 << val from_val : 'a -> 'a t >> from_val v returns an already-forced suspension of v. This is for special purposes only and should not be confused with lazy (v). Since: 4.00.0 << val is_val : 'a t -> bool >> is_val x returns true if x has already been forced and did not raise an exception. Since: 4.00.0 << val lazy_from_fun : (unit -> 'a) -> 'a t >> Deprecated. synonym for from_fun. << val lazy_from_val : 'a -> 'a t >> Deprecated. synonym for from_val. << val lazy_is_val : 'a t -> bool >> Deprecated. synonym for is_val. 24.20 Module Lexing : The run-time library for lexers generated by ocamllex. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* Positions ========= << type position = { pos_fname : string ; pos_lnum : int ; pos_bol : int ; pos_cnum : int ; } >> A value of type position describes a point in a source file. pos_fname is the file name; pos_lnum is the line number; pos_bol is the offset of the beginning of the line (number of characters between the beginning of the lexbuf and the beginning of the line); pos_cnum is the offset of the position (number of characters between the beginning of the lexbuf and the position). The difference between pos_cnum and pos_bol is the character offset within the line (i.e. the column number, assuming each character is one column wide). See the documentation of type lexbuf for information about how the lexing engine will manage positions. << val dummy_pos : position >> A value of type position, guaranteed to be different from any valid position. Lexer buffers ============= << type lexbuf = { refill_buff : lexbuf -> unit ; mutable lex_buffer : bytes ; mutable lex_buffer_len : int ; mutable lex_abs_pos : int ; mutable lex_start_pos : int ; mutable lex_curr_pos : int ; mutable lex_last_pos : int ; mutable lex_last_action : int ; mutable lex_eof_reached : bool ; mutable lex_mem : int array ; mutable lex_start_p : position ; mutable lex_curr_p : position ; } >> The type of lexer buffers. A lexer buffer is the argument passed to the scanning functions defined by the generated scanners. The lexer buffer holds the current state of the scanner, plus a function to refill the buffer from the input. At each token, the lexing engine will copy lex_curr_p to lex_start_p, then change the pos_cnum field of lex_curr_p by updating it with the number of characters read since the start of the lexbuf. The other fields are left unchanged by the lexing engine. In order to keep them accurate, they must be initialised before the first use of the lexbuf, and updated by the relevant lexer actions (i.e. at each end of line -- see also new_line). << val from_channel : Pervasives.in_channel -> lexbuf >> Create a lexer buffer on the given input channel. Lexing.from_channel inchan returns a lexer buffer which reads from the input channel inchan, at the current reading position. << val from_string : string -> lexbuf >> Create a lexer buffer which reads from the given string. Reading starts from the first character in the string. An end-of-input condition is generated when the end of the string is reached. << val from_function : (bytes -> int -> int) -> lexbuf >> Create a lexer buffer with the given function as its reading method. When the scanner needs more characters, it will call the given function, giving it a byte sequence s and a byte count n. The function should put n bytes or fewer in s, starting at index 0, and return the number of bytes provided. A return value of 0 means end of input. Functions for lexer semantic actions ==================================== The following functions can be called from the semantic actions of lexer definitions (the ML code enclosed in braces that computes the value returned by lexing functions). They give access to the character string matched by the regular expression associated with the semantic action. These functions must be applied to the argument lexbuf, which, in the code generated by ocamllex, is bound to the lexer buffer passed to the parsing function. << val lexeme : lexbuf -> string >> Lexing.lexeme lexbuf returns the string matched by the regular expression. << val lexeme_char : lexbuf -> int -> char >> Lexing.lexeme_char lexbuf i returns character number i in the matched string. << val lexeme_start : lexbuf -> int >> Lexing.lexeme_start lexbuf returns the offset in the input stream of the first character of the matched string. The first character of the stream has offset 0. << val lexeme_end : lexbuf -> int >> Lexing.lexeme_end lexbuf returns the offset in the input stream of the character following the last character of the matched string. The first character of the stream has offset 0. << val lexeme_start_p : lexbuf -> position >> Like lexeme_start, but return a complete position instead of an offset. << val lexeme_end_p : lexbuf -> position >> Like lexeme_end, but return a complete position instead of an offset. << val new_line : lexbuf -> unit >> Update the lex_curr_p field of the lexbuf to reflect the start of a new line. You can call this function in the semantic action of the rule that matches the end-of-line character. Since: 3.11.0 Miscellaneous functions ======================= << val flush_input : lexbuf -> unit >> Discard the contents of the buffer and reset the current position to 0. The next use of the lexbuf will trigger a refill. 24.21 Module List : List operations. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* Some functions are flagged as not tail-recursive. A tail-recursive function uses constant stack space, while a non-tail-recursive function uses stack space proportional to the length of its list argument, which can be a problem with very long lists. When the function takes several list arguments, an approximate formula giving stack usage (in some unspecified constant unit) is shown in parentheses. The above considerations can usually be ignored if your lists are not longer than about 10000 elements. << val length : 'a list -> int >> Return the length (number of elements) of the given list. << val compare_lengths : 'a list -> 'b list -> int >> Compare the lengths of two lists. compare_lengths l1 l2 is equivalent to compare (length l1) (length l2), except that the computation stops after itering on the shortest list. Since: 4.05.0 << val compare_length_with : 'a list -> int -> int >> Compare the length of a list to an integer. compare_length_with l n is equivalent to compare (length l) n, except that the computation stops after at most n iterations on the list. Since: 4.05.0 << val cons : 'a -> 'a list -> 'a list >> cons x xs is x :: xs Since: 4.03.0 << val hd : 'a list -> 'a >> Return the first element of the given list. Raise Failure "hd" if the list is empty. << val tl : 'a list -> 'a list >> Return the given list without its first element. Raise Failure "tl" if the list is empty. << val nth : 'a list -> int -> 'a >> Return the n-th element of the given list. The first element (head of the list) is at position 0. Raise Failure "nth" if the list is too short. Raise Invalid_argument "List.nth" if n is negative. << val nth_opt : 'a list -> int -> 'a option >> Return the n-th element of the given list. The first element (head of the list) is at position 0. Return None if the list is too short. Raise Invalid_argument "List.nth" if n is negative. Since: 4.05 << val rev : 'a list -> 'a list >> List reversal. << val append : 'a list -> 'a list -> 'a list >> Concatenate two lists. Same as the infix operator @. Not tail-recursive (length of the first argument). << val rev_append : 'a list -> 'a list -> 'a list >> List.rev_append l1 l2 reverses l1 and concatenates it to l2. This is equivalent to List.rev[24.21] l1 @ l2, but rev_append is tail-recursive and more efficient. << val concat : 'a list list -> 'a list >> Concatenate a list of lists. The elements of the argument are all concatenated together (in the same order) to give the result. Not tail-recursive (length of the argument + length of the longest sub-list). << val flatten : 'a list list -> 'a list >> An alias for concat. Iterators ========= << val iter : ('a -> unit) -> 'a list -> unit >> List.iter f [a1; ...; an] applies function f in turn to a1; ...; an. It is equivalent to begin f a1; f a2; ...; f an; () end. << val iteri : (int -> 'a -> unit) -> 'a list -> unit >> Same as List.iter[24.21], but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument. Since: 4.00.0 << val map : ('a -> 'b) -> 'a list -> 'b list >> List.map f [a1; ...; an] applies function f to a1, ..., an, and builds the list [f a1; ...; f an] with the results returned by f. Not tail-recursive. << val mapi : (int -> 'a -> 'b) -> 'a list -> 'b list >> Same as List.map[24.21], but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument. Not tail-recursive. Since: 4.00.0 << val rev_map : ('a -> 'b) -> 'a list -> 'b list >> List.rev_map f l gives the same result as List.rev[24.21] (List.map[24.21] f l), but is tail-recursive and more efficient. << val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a >> List.fold_left f a [b1; ...; bn] is f (... (f (f a b1) b2) ...) bn. << val fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b >> List.fold_right f [a1; ...; an] b is f a1 (f a2 (... (f an b) ...)). Not tail-recursive. Iterators on two lists ====================== << val iter2 : ('a -> 'b -> unit) -> 'a list -> 'b list -> unit >> List.iter2 f [a1; ...; an] [b1; ...; bn] calls in turn f a1 b1; ...; f an bn. Raise Invalid_argument if the two lists are determined to have different lengths. << val map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list >> List.map2 f [a1; ...; an] [b1; ...; bn] is [f a1 b1; ...; f an bn]. Raise Invalid_argument if the two lists are determined to have different lengths. Not tail-recursive. << val rev_map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list >> List.rev_map2 f l1 l2 gives the same result as List.rev[24.21] (List.map2[24.21] f l1 l2), but is tail-recursive and more efficient. << val fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> 'b list -> 'c list -> 'a >> List.fold_left2 f a [b1; ...; bn] [c1; ...; cn] is f (... (f (f a b1 c1) b2 c2) ...) bn cn. Raise Invalid_argument if the two lists are determined to have different lengths. << val fold_right2 : ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c >> List.fold_right2 f [a1; ...; an] [b1; ...; bn] c is f a1 b1 (f a2 b2 (... (f an bn c) ...)). Raise Invalid_argument if the two lists are determined to have different lengths. Not tail-recursive. List scanning ============= << val for_all : ('a -> bool) -> 'a list -> bool >> for_all p [a1; ...; an] checks if all elements of the list satisfy the predicate p. That is, it returns (p a1) && (p a2) && ... && (p an). << val exists : ('a -> bool) -> 'a list -> bool >> exists p [a1; ...; an] checks if at least one element of the list satisfies the predicate p. That is, it returns (p a1) || (p a2) || ... || (p an). << val for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool >> Same as List.for_all[24.21], but for a two-argument predicate. Raise Invalid_argument if the two lists are determined to have different lengths. << val exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool >> Same as List.exists[24.21], but for a two-argument predicate. Raise Invalid_argument if the two lists are determined to have different lengths. << val mem : 'a -> 'a list -> bool >> mem a l is true if and only if a is equal to an element of l. << val memq : 'a -> 'a list -> bool >> Same as List.mem[24.21], but uses physical equality instead of structural equality to compare list elements. List searching ============== << val find : ('a -> bool) -> 'a list -> 'a >> find p l returns the first element of the list l that satisfies the predicate p. Raise Not_found if there is no value that satisfies p in the list l. << val find_opt : ('a -> bool) -> 'a list -> 'a option >> find_opt p l returns the first element of the list l that satisfies the predicate p, or None if there is no value that satisfies p in the list l. Since: 4.05 << val filter : ('a -> bool) -> 'a list -> 'a list >> filter p l returns all the elements of the list l that satisfy the predicate p. The order of the elements in the input list is preserved. << val find_all : ('a -> bool) -> 'a list -> 'a list >> find_all is another name for List.filter[24.21]. << val partition : ('a -> bool) -> 'a list -> 'a list * 'a list >> partition p l returns a pair of lists (l1, l2), where l1 is the list of all the elements of l that satisfy the predicate p, and l2 is the list of all the elements of l that do not satisfy p. The order of the elements in the input list is preserved. Association lists ================= << val assoc : 'a -> ('a * 'b) list -> 'b >> assoc a l returns the value associated with key a in the list of pairs l. That is, assoc a [ ...; (a,b); ...] = b if (a,b) is the leftmost binding of a in list l. Raise Not_found if there is no value associated with a in the list l. << val assoc_opt : 'a -> ('a * 'b) list -> 'b option >> assoc_opt a l returns the value associated with key a in the list of pairs l. That is, assoc_opt a [ ...; (a,b); ...] = b if (a,b) is the leftmost binding of a in list l. Returns None if there is no value associated with a in the list l. Since: 4.05 << val assq : 'a -> ('a * 'b) list -> 'b >> Same as List.assoc[24.21], but uses physical equality instead of structural equality to compare keys. << val assq_opt : 'a -> ('a * 'b) list -> 'b option >> Same as List.assoc_opt[24.21], but uses physical equality instead of structural equality to compare keys. Since: 4.05 << val mem_assoc : 'a -> ('a * 'b) list -> bool >> Same as List.assoc[24.21], but simply return true if a binding exists, and false if no bindings exist for the given key. << val mem_assq : 'a -> ('a * 'b) list -> bool >> Same as List.mem_assoc[24.21], but uses physical equality instead of structural equality to compare keys. << val remove_assoc : 'a -> ('a * 'b) list -> ('a * 'b) list >> remove_assoc a l returns the list of pairs l without the first pair with key a, if any. Not tail-recursive. << val remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) list >> Same as List.remove_assoc[24.21], but uses physical equality instead of structural equality to compare keys. Not tail-recursive. Lists of pairs ============== << val split : ('a * 'b) list -> 'a list * 'b list >> Transform a list of pairs into a pair of lists: split [(a1,b1); ...; (an,bn)] is ([a1; ...; an], [b1; ...; bn]). Not tail-recursive. << val combine : 'a list -> 'b list -> ('a * 'b) list >> Transform a pair of lists into a list of pairs: combine [a1; ...; an] [b1; ...; bn] is [(a1,b1); ...; (an,bn)]. Raise Invalid_argument if the two lists have different lengths. Not tail-recursive. Sorting ======= << val sort : ('a -> 'a -> int) -> 'a list -> 'a list >> Sort a list in increasing order according to a comparison function. The comparison function must return 0 if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller (see Array.sort for a complete specification). For example, Pervasives.compare[23.2] is a suitable comparison function. The resulting list is sorted in increasing order. List.sort is guaranteed to run in constant heap space (in addition to the size of the result list) and logarithmic stack space. The current implementation uses Merge Sort. It runs in constant heap space and logarithmic stack space. << val stable_sort : ('a -> 'a -> int) -> 'a list -> 'a list >> Same as List.sort[24.21], but the sorting algorithm is guaranteed to be stable (i.e. elements that compare equal are kept in their original order) . The current implementation uses Merge Sort. It runs in constant heap space and logarithmic stack space. << val fast_sort : ('a -> 'a -> int) -> 'a list -> 'a list >> Same as List.sort[24.21] or List.stable_sort[24.21], whichever is faster on typical input. << val sort_uniq : ('a -> 'a -> int) -> 'a list -> 'a list >> Same as List.sort[24.21], but also remove duplicates. Since: 4.02.0 << val merge : ('a -> 'a -> int) -> 'a list -> 'a list -> 'a list >> Merge two lists: Assuming that l1 and l2 are sorted according to the comparison function cmp, merge cmp l1 l2 will return a sorted list containting all the elements of l1 and l2. If several elements compare equal, the elements of l1 will be before the elements of l2. Not tail-recursive (sum of the lengths of the arguments). 24.22 Module ListLabels : List operations. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* Some functions are flagged as not tail-recursive. A tail-recursive function uses constant stack space, while a non-tail-recursive function uses stack space proportional to the length of its list argument, which can be a problem with very long lists. When the function takes several list arguments, an approximate formula giving stack usage (in some unspecified constant unit) is shown in parentheses. The above considerations can usually be ignored if your lists are not longer than about 10000 elements. << val length : 'a list -> int >> Return the length (number of elements) of the given list. << val hd : 'a list -> 'a >> Return the first element of the given list. Raise Failure "hd" if the list is empty. << val compare_lengths : 'a list -> 'b list -> int >> Compare the lengths of two lists. compare_lengths l1 l2 is equivalent to compare (length l1) (length l2), except that the computation stops after itering on the shortest list. Since: 4.05.0 << val compare_length_with : 'a list -> len:int -> int >> Compare the length of a list to an integer. compare_length_with l n is equivalent to compare (length l) n, except that the computation stops after at most n iterations on the list. Since: 4.05.0 << val cons : 'a -> 'a list -> 'a list >> cons x xs is x :: xs Since: 4.05.0 << val tl : 'a list -> 'a list >> Return the given list without its first element. Raise Failure "tl" if the list is empty. << val nth : 'a list -> int -> 'a >> Return the n-th element of the given list. The first element (head of the list) is at position 0. Raise Failure "nth" if the list is too short. Raise Invalid_argument "List.nth" if n is negative. << val nth_opt : 'a list -> int -> 'a option >> Return the n-th element of the given list. The first element (head of the list) is at position 0. Return None if the list is too short. Raise Invalid_argument "List.nth" if n is negative. Since: 4.05 << val rev : 'a list -> 'a list >> List reversal. << val append : 'a list -> 'a list -> 'a list >> Catenate two lists. Same function as the infix operator @. Not tail-recursive (length of the first argument). The @ operator is not tail-recursive either. << val rev_append : 'a list -> 'a list -> 'a list >> List.rev_append l1 l2 reverses l1 and concatenates it to l2. This is equivalent to List.rev[24.21] l1 @ l2, but rev_append is tail-recursive and more efficient. << val concat : 'a list list -> 'a list >> Concatenate a list of lists. The elements of the argument are all concatenated together (in the same order) to give the result. Not tail-recursive (length of the argument + length of the longest sub-list). << val flatten : 'a list list -> 'a list >> Same as concat. Not tail-recursive (length of the argument + length of the longest sub-list). Iterators ========= << val iter : f:('a -> unit) -> 'a list -> unit >> List.iter f [a1; ...; an] applies function f in turn to a1; ...; an. It is equivalent to begin f a1; f a2; ...; f an; () end. << val iteri : f:(int -> 'a -> unit) -> 'a list -> unit >> Same as List.iter[24.21], but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument. Since: 4.00.0 << val map : f:('a -> 'b) -> 'a list -> 'b list >> List.map f [a1; ...; an] applies function f to a1, ..., an, and builds the list [f a1; ...; f an] with the results returned by f. Not tail-recursive. << val mapi : f:(int -> 'a -> 'b) -> 'a list -> 'b list >> Same as List.map[24.21], but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument. Since: 4.00.0 << val rev_map : f:('a -> 'b) -> 'a list -> 'b list >> List.rev_map f l gives the same result as List.rev[24.21] (List.map[24.21] f l), but is tail-recursive and more efficient. << val fold_left : f:('a -> 'b -> 'a) -> init:'a -> 'b list -> 'a >> List.fold_left f a [b1; ...; bn] is f (... (f (f a b1) b2) ...) bn. << val fold_right : f:('a -> 'b -> 'b) -> 'a list -> init:'b -> 'b >> List.fold_right f [a1; ...; an] b is f a1 (f a2 (... (f an b) ...)). Not tail-recursive. Iterators on two lists ====================== << val iter2 : f:('a -> 'b -> unit) -> 'a list -> 'b list -> unit >> List.iter2 f [a1; ...; an] [b1; ...; bn] calls in turn f a1 b1; ...; f an bn. Raise Invalid_argument if the two lists are determined to have different lengths. << val map2 : f:('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list >> List.map2 f [a1; ...; an] [b1; ...; bn] is [f a1 b1; ...; f an bn]. Raise Invalid_argument if the two lists are determined to have different lengths. Not tail-recursive. << val rev_map2 : f:('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list >> List.rev_map2 f l1 l2 gives the same result as List.rev[24.21] (List.map2[24.21] f l1 l2), but is tail-recursive and more efficient. << val fold_left2 : f:('a -> 'b -> 'c -> 'a) -> init:'a -> 'b list -> 'c list -> 'a >> List.fold_left2 f a [b1; ...; bn] [c1; ...; cn] is f (... (f (f a b1 c1) b2 c2) ...) bn cn. Raise Invalid_argument if the two lists are determined to have different lengths. << val fold_right2 : f:('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> init:'c -> 'c >> List.fold_right2 f [a1; ...; an] [b1; ...; bn] c is f a1 b1 (f a2 b2 (... (f an bn c) ...)). Raise Invalid_argument if the two lists are determined to have different lengths. Not tail-recursive. List scanning ============= << val for_all : f:('a -> bool) -> 'a list -> bool >> for_all p [a1; ...; an] checks if all elements of the list satisfy the predicate p. That is, it returns (p a1) && (p a2) && ... && (p an). << val exists : f:('a -> bool) -> 'a list -> bool >> exists p [a1; ...; an] chec