Lumenary CS · Programming languages
OCaml
A fast, statically typed functional language with a powerful module system and, since OCaml 5, multicore parallelism and effect handlers, from a first program to professional practice.
OCaml is a general-purpose programming language from the ML family, developed at Inria in France and first released as Objective Caml in 1996. It combines functional programming (immutable data, pattern matching, higher-order functions) with practical imperative features (mutable references, arrays, exceptions), a type system that infers almost every type without annotations, and a module system that is among the most expressive of any language. It compiles to fast native code with a predictable garbage collector. OCaml is used to write compilers and static analysers, proof assistants such as Rocq (formerly Coq), trading systems at Jane Street, the Tezos blockchain, MirageOS unikernels and many developer tools.
This course is for anyone who already knows what a variable, a loop and a function are: the Start level of Lumenary CS or equivalent experience in any language. No previous functional programming is assumed. It starts with installing OCaml and running a first program, works through expressions, functions, lists, options, records and variants, then modules, signatures and functors, mutability and exceptions, and the standard library, and ends with OCaml 5's domains and effect handlers, the tools OCaml teams use every day, and a complete small project.
To run the code, install OCaml with opam, its package manager, following the instructions at ocaml.org/install; you can also try programs in the browser at the OCaml Playground. Every complete program in these lessons was run with OCaml 5.4 by Lumenary's test suite, and the output shown is the output it printed. The lessons were drafted with AI assistance and are awaiting review by a human computer scientist.
- Chloe · The Designer
“Why is the language built this way?” - Margaret · The Profiler
“What does it print, and what does it cost?” - Gideon · The Language Lawyer
“What exactly does the language guarantee?”
Foundations · 4 lessons
Foundations
Install OCaml with opam, run programs in the toplevel and with dune, and learn the core language: expressions and inferred types, functions, lists, options, results, records and variants, all taken apart with pattern matching.
- 1First steps in OCamlWhere OCaml came from, what it is good at, and your first programs with opam, the toplevel and dune. 45 min
- 2Expressions, types and functionsValues and operators, let-bindings and scope, inferred types, and functions with currying, labels and recursion. 55 min
- 3Lists, pattern matching, options and resultsThe everyday immutable list, taken apart with match; the List module's toolkit; and option and result, OCaml's honest answers to "maybe nothing" and "maybe an error". 55 min
- 4Records and variantsDesign types that say exactly what your data can be, and let the compiler check every function that uses them. 55 min
Practice · 3 lessons
Practice
Organise code with modules, signatures and functors, use mutable state, arrays and exceptions where they help, and do everyday work with the standard library.
- 5Modules, signatures and functorsOCaml's module system, the feature that most sets it apart, from files as modules to abstract types and functions from modules to modules. 60 min
- 6Mutability, arrays and exceptionsWhen and how to change things in place with references, mutable fields, arrays and hash tables, and how OCaml's fast exceptions fit alongside option and result. 55 min
- 7The standard library in practiceText, formatted output, lazy sequences and files with OCaml's own library, and an honest look at the alternatives teams use. 50 min
Professional · 2 lessons
Professional
Use OCaml 5's domains for parallelism and effect handlers for concurrency, work with the tools OCaml teams rely on, see honestly where OCaml is used, and build a complete project.
- 8OCaml 5: domains, effects and concurrencyParallelism on several cores with domains, atomics and mutexes, and effect handlers, the new mechanism behind lightweight threads, generators and direct-style I/O. 65 min
- 9Tools, where OCaml is used, and a complete projectHow OCaml teams build, test, format and document code with dune and opam, where the language is used in industry, and a small compiler written from lexer to virtual machine. 70 min
Further reading and resources
- OCaml.orgThe official home of OCaml: installation, tutorials, the package index and community.
- The OCaml ManualThe reference manual: the language definition, the tools and the standard library, for the latest release.
- Standard library APIDocumentation for every module of the standard library.
- OCaml PlaygroundWrite and run OCaml in the browser.
- Real World OCaml · Yaron Minsky and Anil MadhavapeddyA free online book on using OCaml in practice, with Jane Street's Base and Core libraries.
- OCaml Programming: Correct + Efficient + Beautiful · Michael R. Clarkson and othersCornell's free textbook for its functional programming course, written in OCaml.
- opamThe OCaml package manager and its package repository.
- DuneThe standard build system for OCaml projects.