For Lisa, a believer in the power of words, who helps me find mine. Yaron
For Mum and Dad, who took me to the library and unlocked my imagination. Anil
For Nobu, who takes me on a new journey every day. Jason
If you purchased this ebook directly from oreilly.com, you have the following benefits:
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Why OCaml?
Programming languages matter. They affect the reliability, security, and efficiency of the code you write, as well as how easy it is to read, refactor, and extend. The languages you know can also change how you think, influencing the way you design software even when youre not using them.
We wrote this book because we believe in the importance of programming languages, and that OCaml in particular is an important language to learn. The three of us have been using OCaml in our academic and professional lives for over 15 years, and in that time weve come to see it as a secret weapon for building complex software systems. This book aims to make this secret weapon available to a wider audience, by providing a clear guide to what you need to know to use OCaml effectively in the real world.
What makes OCaml special is that it occupies a sweet spot in the space of programming language designs. It provides a combination of efficiency, expressiveness and practicality that is matched by no other language. That is in large part because OCaml is an elegant combination of a few key language features that have been developed over the last 40 years. These include:
Garbage collection for automatic memory management, now a feature of almost every modern, high-level language.
First-class functions that can be passed around like ordinary values, as seen in JavaScript, Common Lisp, and C#.
Static type-checking to increase performance and reduce the number of runtime errors, as found in Java and C#.
Parametric polymorphism , which enables the construction of abstractions that work across different data types, similar to generics in Java and C# and templates in C++.
Good support for immutable programming , i.e., programming without making destructive updates to data structures. This is present in traditional functional languages like Scheme, and is also found in distributed, big-data frameworks like Hadoop.
Automatic type inference to avoid having to laboriously define the type of every single variable in a program and instead have them inferred based on how a value is used. Available in a limited form in C# with implicitly typed local variables, and in C++11 with its auto
keyword.
Algebraic data types and pattern matching to define and manipulate complex data structures. Available in Scala and F#.
Some of you will know and love all of these features, and for others they will be largely new, but most of you will have seen some of them in other languages that youve used. As well demonstrate over the course of this book, there is something transformative about having them all together and able to interact in a single language. Despite their importance, these ideas have made only limited inroads into mainstream languages, and when they do arrive there, like first-class functions in C# or parametric polymorphism in Java, its typically in a limited and awkward form. The only languages that completely embody these ideas are statically typed, functional programming languages like OCaml, F#, Haskell, Scala, and Standard ML.
Among this worthy set of languages, OCaml stands apart because it manages to provide a great deal of power while remaining highly pragmatic. The compiler has a straightforward compilation strategy that produces performant code without requiring heavy optimization and without the complexities of dynamic just-in-time (JIT) compilation. This, along with OCamls strict evaluation model, makes runtime behavior easy to predict. The garbage collector is incremental , letting you avoid large garbage collection (GC)-related pauses, and precise , meaning it will collect all unreferenced data (unlike many reference-counting collectors), and the runtime is simple and highly portable.
All of this makes OCaml a great choice for programmers who want to step up to a better programming language, and at the same time get practical work done.
A Brief History
OCaml was written in 1996 by Xavier Leroy, Jrme Vouillon, Damien Doligez, and Didier Rmy at INRIA in France. It was inspired by a long line of research into ML starting in the 1960s, and continues to have deep links to the academic community.
ML was originally the meta language of the LCF (Logic for Computable Functions) proof assistant released by Robin Milner in 1972 (at Stanford, and later at Cambridge). ML was turned into a compiler in order to make it easier to use LCF on different machines, and it was gradually turned into a full-fledged system of its own by the 1980s.
The first implementation of Caml appeared in 1987. It was created by Ascnder Surez and later continued by Pierre Weis and Michel Mauny. In 1990, Xavier Leroy and Damien Doligez built a new implementation called Caml Light that was based on a bytecode interpreter with a fast, sequential garbage collector. Over the next few years useful libraries appeared, such as Michel Maunys syntax manipulation tools, and this helped promote the use of Caml in education and research teams.
Xavier Leroy continued extending Caml Light with new features, which resulted in the 1995 release of Caml Special Light. This improved the executable efficiency significantly by adding a fast native code compiler that made Camls performance competitive with mainstream languages such as C++. A module system inspired by Standard ML also provided powerful facilities for abstraction and made larger-scale programs easier to construct.
The modern OCaml emerged in 1996, when a powerful and elegant object system was implemented by Didier Rmy and Jrme Vouillon. This object system was notable for supporting many common object-oriented idioms in a statically type-safe way, whereas the same idioms required runtime checks in languages such as C++ or Java. In 2000, Jacques Garrigue extended OCaml with several new features such as polymorphic methods, variants, and labeled and optional arguments.
The last decade has seen OCaml attract a significant user base, and language improvements have been steadily added to support the growing commercial and academic codebases. First-class modules, Generalized Algebraic Data Types (GADTs), and dynamic linking have improved the flexibility of the language. There is also fast native code support for x86_64, ARM, PowerPC, and Sparc, making OCaml a good choice for systems where resource usage, predictability, and performance all matter.