Skip to content

Lesson 4 of 9 · 25m

Modules, Signatures, and Encapsulation

Use structures and signatures as the unit of architecture: hide representations, expose operations, and make dependencies explicit.

The module system is where OCaml stops being 'a functional language with good types' and becomes a serious engineering tool. Most application languages organize large systems through classes, packages, or conventions. OCaml gives you first-class compile-time interfaces and lets the compiler enforce that implementation details stay implementation details.

A structure is an implementation unit

A module is usually a file. ledger.ml defines module Ledger; ledger.mli can define its signature:

(* ledger.ml *)
type account = {
  id : string;
  mutable balance_cents : int64;
}

let create id = { id; balance_cents = 0L }
let balance a = a.balance_cents

let credit a cents =
  if cents < 0L then invalid_arg "credit: negative amount";
  a.balance_cents <- Int64.add a.balance_cents cents

That is the implementation. The field mutability is an internal decision.

A signature is the contract

(* ledger.mli *)
type account

val create : string -> account
val balance : account -> int64
val credit : account -> int64 -> unit

Because type account is abstract, callers cannot construct an invalid account or mutate balance directly. They only get operations the module exposes. This is stronger than documentation. If a caller pattern-matches on the representation, compilation fails outside the module boundary.

Private types are a middle ground

Sometimes callers should inspect a shape but not forge one. Private types expose reading while restricting construction: type email = private Email of string is useful at validation boundaries. A parser or constructor can produce Email only inside the defining module's rules. Consumers can match enough to use the value without being able to mint arbitrary invalid instances. The exact use varies by design, but the principle is important: distinguish inspection authority from construction authority.

Include and open are not free

open Ledger brings a module namespace into scope. It is convenient in small examples and corrosive in large files. Two modules exporting create, empty, or map become a guessing game. Prefer explicit qualification in production code, or use local opens:

let total = Ledger.(balance account |> Int64.to_int)

The local form makes the dependency visible at the point of use without contaminating the rest of the file.

Field notes

  • .mli files are not redundant ceremony in application code; they are how large OCaml codebases stay navigable.
  • Abstract types prevent clients from depending on representation details, but they also make debugging less transparent. Choose deliberately.
  • Cyclic module dependencies usually mean the conceptual boundaries are wrong, not that the build needs a cleverer hack.
  • Do not export a mutable data structure just because callers 'need flexibility.' Export operations whose invariants you can defend.

Was this lesson helpful?

Sign in to rate