OCaml
A powerful functional programming language combining static typing, type inference, and a sophisticated module system - used at Jane Street, Meta, and in formal verification.
Created by Xavier Leroy, Jérôme Vouillon, Damien Doligez, Didier Rémy (INRIA)
OCaml is a general-purpose, industrial-strength programming language that combines functional, imperative, and object-oriented programming in a statically-typed language with type inference. It occupies a unique “sweet spot” in language design - expressive enough for complex software yet practical enough for real-world production systems.
History & Origins
OCaml’s story begins with ML (Meta Language), created by Robin Milner in 1972 at Stanford for the LCF proof assistant. ML introduced revolutionary concepts: type inference, pattern matching, and algebraic data types - features that would influence decades of language design.
From Caml to OCaml
In 1987, the Formel project at INRIA (the French national research institute) created Caml, the first implementation of the ML dialect that would evolve into OCaml. The journey continued:
Caml Light (1990) - Xavier Leroy and Damien Doligez created a lightweight, portable implementation with a bytecode compiler and fast garbage collector. This made ML-family languages practical for everyday use.
Caml Special Light (1995) - Added a native code compiler, dramatically improving performance. Programs could now compete with C in execution speed while maintaining type safety.
Objective Caml (1996) - Didier Rémy and Jérôme Vouillon added a powerful object system, enabling object-oriented programming with full static type safety. The name was later shortened to “OCaml” in 2011.
Modern OCaml
OCaml 5.0 (2022) marked a major milestone with multicore support and effect handlers, enabling efficient parallel programming while maintaining the language’s signature safety guarantees.
What Makes OCaml Different
1. Type Inference Without Annotations
OCaml’s Hindley-Milner type system can infer types for entire programs without explicit annotations:
| |
2. Algebraic Data Types and Pattern Matching
Define complex data structures and destructure them elegantly:
| |
3. Powerful Module System
OCaml’s module system enables sophisticated abstraction and code organization:
| |
Functors (modules parameterized by modules) enable powerful generic programming:
| |
4. First-Class Functions and Closures
Functions are values that can be passed around, returned, and composed:
| |
5. Imperative Features When Needed
Unlike purely functional languages, OCaml pragmatically supports mutation:
| |
6. Object-Oriented Programming
Full object-oriented features with structural typing:
| |
Why OCaml?
Performance
OCaml’s native code compiler produces executables that rival C in speed:
- No runtime type checks (types are erased after compilation)
- Efficient unboxed representations for numeric types
- Generational garbage collector with very low pause times
- Whole-program optimization through cross-module inlining
Safety
The type system catches errors before they become bugs:
- No null pointer exceptions (use
optiontypes) - Exhaustive pattern matching (compiler warns about missing cases)
- Immutability by default (mutation is explicit)
- Strong encapsulation through module signatures
Expressiveness
Complex ideas map directly to code:
| |
The OCaml Ecosystem
Build Tools
- opam - Package manager with dependency resolution and version constraints
- dune - Modern build system with declarative configuration
- utop - Enhanced REPL with auto-completion
Popular Libraries
- Core / Base - Jane Street’s standard library alternatives
- Lwt / Async - Cooperative threading/concurrency
- Cohttp - HTTP client/server library
- Yojson - JSON parsing and generation
- Dream - Web framework
- Js_of_ocaml - Compile OCaml to JavaScript
- Eio - Modern effect-based I/O (OCaml 5+)
Development Tools
- Merlin - Editor integration for code completion and type information
- ocamlformat - Automatic code formatting
- odoc - Documentation generator
- LSP - Language Server Protocol support
Getting Started
OCaml files use the .ml extension (with .mli for interface files). A basic structure:
| |
The REPL
OCaml has an excellent interactive environment:
$ ocaml
OCaml version 5.2.0
# 1 + 2;;
- : int = 3
# let double x = x * 2;;
val double : int -> int = <fun>
# double 21;;
- : int = 42
# List.map double [1; 2; 3; 4];;
- : int list = [2; 4; 6; 8]
Note: Lines must end with ;; in the REPL (but not in source files).
Compilation
| |
A Complete Example
Here’s a more complete OCaml program demonstrating various features:
| |
The OCaml Community
Resources
- ocaml.org - Official site with documentation and tutorials
- Real World OCaml - Free online book (realworldocaml.org)
- OCaml Manual - Comprehensive language reference
- OCamlverse - Community wiki
Learning Path
- OCaml from the Very Beginning - Beginner-friendly introduction
- Real World OCaml - Practical, comprehensive guide
- More OCaml - Advanced algorithms and techniques
- Types and Programming Languages - Theoretical foundations
Community
- OCaml Discuss forum
- r/ocaml subreddit
- OCaml Discord
- Functional Programming Slack (#ocaml)
OCaml vs Similar Languages
| Feature | OCaml | Haskell | F# | Rust |
|---|---|---|---|---|
| Evaluation | Strict | Lazy | Strict | Strict |
| Mutability | Opt-in | Opt-in | Opt-in | Opt-in |
| OOP | Yes | No | Yes | No |
| Module System | Sophisticated | Type classes | .NET based | Traits |
| GC | Yes | Yes | Yes | No |
| Multicore | Yes (5.0+) | Yes | Yes | Yes |
Continue to the Hello World tutorial to write your first OCaml program.
Timeline
Notable Uses & Legacy
Jane Street
Quantitative trading firm uses OCaml for almost all of their trading infrastructure, processing billions of dollars daily.
Meta (Facebook)
Flow (JavaScript type checker), Hack (PHP successor), and Infer (static analyzer) are all written in OCaml.
Docker
The original Docker for Mac and Windows used the MirageOS unikernel library written in OCaml.
Airbus
Safety-critical software verification using ASTRÉE analyzer, proving absence of runtime errors in A340 flight control software.
Tezos
Blockchain platform written entirely in OCaml for its formal verification capabilities.
Coq Proof Assistant
Major formal verification tool written in OCaml, used to verify mathematical theorems and software correctness.
Language Influence
Influenced By
Influenced
Running Today
Run examples using the official Docker image:
docker pull ocaml/opam:alpineExample usage:
docker run --rm -v $(pwd):/app -w /app ocaml/opam:alpine ocaml hello.ml