By Yaron Minsky, Anil Madhavapeddy, Jason Hickey (510 pages)
Real World OCaml introduces the reader to OCaml, an industrial-strength programming language designed for expressiveness, safety, and speed. Through the book’s many examples, the reader learns how OCaml stands out as a tool for writing fast, succinct, and readable code. The book offers a clear guide to what you need to know to use OCaml effectively in the real world.
The book starts with a gentle introduction to OCaml. Part 2 of the book (“tools and techniques”) demonstrates how to perform practical tasks such as: parse command-lines, read and write JSON formatted data and handle concurrent I/O; while part 3 dives into low-level detail including: interfacing to C and understanding the GCC and compiler toolchain. It’s aimed at programmers who have some experience with conventional programming languages, but not specifically with statically typed functional programming.
- A Guided Tour – gives an overview of OCaml by walking through a series of small examples that cover most of the major features of the language
- Variables and Functions – covers OCaml’s approach to variables and functions in some detail, starting with the basics of how to define a variable, and ending with the intricacies of functions with labeled and optional arguments
- Lists and Patterns – goes into more detail about these two common elements of programming
- Files, Modules, and Programs – shows the reader how to build an OCaml program from a collection of files, as well as the basics of working with modules and module signatures
- Records – in depth treatment covering the details of how records work, as well as advice on how to use them effectively in software designs
- Variants – explores one of the most useful features of OCaml
- Error Handling – discusses some of the different approaches in OCaml to handling errors, and give some advice on how to design interfaces that make error handling easier
- Imperative Programming – walks the reader through OCaml’s imperative features, and help use them to their fullest
- Functors – functions from modules to modules which can be used to solve a variety of code-structuring problems
- First-Class Modules – ordinary values that can be created from and converted back to regular modules
- Objects – introduces the reader to OCaml objects and subtyping
- Classes – introduces the reader to classes and inheritance
- Maps and Hash Tables – a map is an immutable tree-based data structure where most operations take time logarithmic in the size of the map, whereas a hash table is a mutable data structure where most operations have constant time complexity. This chapter describes both of these data structures in detail and provides advice as to how to choose between them
- Command-Line Parsing – construct basic and grouped command-line interfaces, build simple equivalents to the cryptographic md5 and shasum utilities, and shows how functional combinators can be used to declare complex command-line interfaces in a type-safe and elegant way
- Handling JSON Data – introduces the reader to a couple of new techniques
- Parsing with OCamilex and Menhir – OCamilex, replaces lex, and ocamlyacc and menhir, which replace yacc. This chapter explores these tools and the implementation of a parser for the JSON serialization format
- Data Serialization with S-Expressions – goes into s-expressions in more depth
- Concurrent Programming with Async – covers the Async library, which offers a hybrid model that aims to provide the best of both worlds
- Foreign Function Interface – show how to call routines in C libraries directly from OCaml code, how to build higher-level abstractions in OCaml from the low-level C bindings, and work through some full examples for binding a terminal interface and UNIX date/time functions
- Memory Representation of Values – describes the runtime format of individual OCaml variables
- Understanding the Garbage Collector
- The Compiler Frontend: Parsing and Type Checking – compilation pipeline and what each stage represents, source preprocessing via Camlp4 and the intermediate forms, and type-checking process, including module resolution
- The Compiler Backend: Bytecode and Native code – untyped intermediate lambda code where pattern matching is optimized, bytecode ocamlc compiler and ocamlrun interpreter, and native code ocamlopt code generator, and debugging and profiling native code
The online HTML version of the book is available under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
By Allen Downey and Nicholas Monje (142 pages)
How to Think Like a Computer Scientist is an introductory programming textbook based on the OCaml language which teaches the reader to think like a computer scientist. It’s a modified version of Think Python by Allen Downey.
- The way of the program
- Variables and Expressions – includes values and types, variables, expressions, string operations, and debugging
- Functions – includes function calls, math functions, composition, adding new functions, and more
- Program Flow – includes coverage of Boolean expressions, logical operators, chained conditionals
- Recursive Functions – recursion, infinite recursion, mutually recursive functions, tail-end recursion, and debugging
- Algorithms – square roots, algorithms, and debugging
- Strings – string.length, substrings, string traversal, searching, string comparison, and debugging
- Lists – list operations, list iteration, mapping and folding, list sorting, lists and recursion, and debugging
- Case Study: Regular Expressions
- Putting the O in OCaml, Part 1: Imperative programming
- Arrays – making arrays, array operations, array iteration, mapping, and folding, array sorting, and array traversal
- Hashtables – includes folding and hashtables, reverse lookup, memos and more
- Tuples – includes enumerated types, and aggregate types
- Records and Custom Data Structures
- Putting the O in OCaml, Part 2: Objects and Classes
- Case study: data structure selection
Permission is granted to copy, distribute, and/or modify this book under the terms of the GNU Free Documentation License, Version 1.1 or any later version.
By Xavier Leroy, Didier Rémy (143 pages)
This book is an introductory course on Unix system programming, with an emphasis on communications between processes. The main novelty of this work is the use of the OCaml language, a dialect of the ML language, instead of the C language that is customary in systems programming. This gives an unusual perspective on systems programming and on the ML language.
This document describes only the programmatic interface to the Unix system. It presents neither its implementation, neither its internal architecture.
- Generalities – modules Sys and Unix, interface with the calling program, error handling, and library functions
- Files – including opening a file, reading and writing, positioning, locks on files, complete examples: file copy, recursive copy of files, and Tape ARchive
- Proceses – creation of processes, awaiting the termination of a process, launching a program, complete examples: the command leave, a mini-shell
- Signals – includes how to use signals, using signals, how to mask signals, signals and system calls, problems with signals
- Classical inter-process communication: pipes – pipes, named pipes, descriptor redirectors, input/out multiplexing, complete examples: parallel sieve of Eratosthenes, composing N commands
- Modern communication: sockets – sockets, socket creation, addresses, disconnecting sockets, establishing a service, tuning sockets, high-level primitives, complete examples: the universal client, the universal server, HTTP requests
- Threads – creation and termination of threads, waiting, synchronization among threads: locks, conditions, event-based synchronous
Unix System Programming in OCaml is distributed under a Creative Commons by-nc-sa license.
By Didier Rémy (182 pages)
Using, Understanding, and Unraveling The OCaml Language is a multi-dimensional presentation of the OCaml language that combines an informal and intuitive approach to the language with a rigorous definition and a formal semantics of a large subset of the language, including ML.
Throughout this book, the specific concepts and techniques are given rigorous theoretical justification. A sample of programming exercises with different levels of difficulty are included.
- Core ML – with examples, formalize a very small subset of the language, and shows how to derive other constructs remaining in core ML whenever possible
- The core of OCaml – including type abbreviations, record types, mutable storage and side effects, and exceptions
- The object layer – starts with an introduction to objects and classes, then presents the core of the object layer, and ends with a few advanced uses of objects
- The module language – explores OCaml’s powerful module system
- Mixing modules and objects – discusses the overlapping of features and the specificities, and show how to use them in harmony
By Xavier Leroy, Damien Doligez, Alain Frisch, Jacques Garrigue, Didier Rémy and Jérôme Vouillon (657 pages)
The OCaml system release is the official User’s Manual. It serves as a complete reference guide to OCaml. Updated for each version of OCaml, it contains the description of the language, of its extensions, and the documentation of the tools and libraries included in the official distribution.
The book covers:
- An introduction to OCaml – gives an overview of the language
- The OCaml language – the reference description of the language
- The OCaml tools – documents the compilers, top level system, and programming utilities
- The OCaml library – describes the modules provided in the standard library
By Emmanuel Chailloux, Pascal Manoury and Bruno Pagano (757 pages)
Developing Applications With OCaml is divided into four main sections that build upon each other. Each section has a set of chapters that present some related concepts, followed by an “Applications” chapter that uses those concepts to create a few small applications such as a minesweeper game, a graphical interface library, a couple of different two-player games, a distributed robot simulation, and a basic HTTP servlet.
The book explores:
- Language Core – complete presentation of the basic elements of the Objective Caml language looking into the functional core of the languages, a comparison between the “pure” functional and imperative styles, and the graphics library. This section then exhibits three applications: management of a simple database, a mini-Basic interpreter and a well-known single-player game, minesweeper
- Development Tools – describes the various tools for application development including compilation modes, main libraries, garbage collection mechanism, the use of tools for debugging and profiling programs. Later chapters address lexical and syntactic tools, how to interface Objective Caml programs with C, constructing a library and an application
- Application Structure – describes the two parts of organizing a program: with modules, and with objects
- Concurrency and Distribution – introduces concurrent and distributed programs while detailing communication between processes, lightweight or not, and on the internet