Skip to content

Lesson 1 of 9 · 22m

Why ML Won Compilers and Trading Floors

Understand the historical and practical reasons OCaml remains attractive for compilers, analyzers, trading systems, and correctness-critical infrastructure.

The first time I watched a team port a gnarly financial validation ruleset into OCaml, the surprise was not that the code got shorter. It was that review meetings got shorter. The arguments moved from 'did you remember this edge case?' to 'the compiler rejects this state combination.' That is the ML-family promise when it is used well: make important facts machine-checkable, then let humans spend their time on the facts machines cannot know.

The lineage matters

OCaml comes from the ML family, whose central idea is not merely static typing. It is the combination of algebraic data types, pattern matching, type inference, parametric polymorphism, and a serious module system. Those pieces reinforce each other:

types describe the shape of the domain
pattern matching consumes every shape
inference propagates local decisions through the program
modules hide representation choices behind explicit interfaces

That combination is why ML dialects keep appearing in compiler construction. Compilers are pipelines of tree transformations: parse tree to typed tree, typed tree to intermediate representation, IR to lower IR, then to machine code. Trees with many cases are exactly where algebraic types and exhaustive matching shine.

Compilers choose ML because invalid trees become unrepresentable

Consider a tiny expression language:

type expr =
  | Int of int
  | Add of expr * expr
  | Let of string * expr * expr

type value =
  | VInt of int

The type says what an expression can be. The interpreter can then match every constructor:

let rec eval env = function
  | Int n -> VInt n
  | Add (a, b) ->
      let VInt x = eval env a in
      let VInt y = eval env b in
      VInt (x + y)
  | Let (name, bound, body) ->
      eval ((name, eval env bound) :: env) body

This example is intentionally small, but the architectural lesson scales: a compiler writer can encode each intermediate language explicitly and inspect every transformation as a total or deliberately partial function.

Trading firms choose it for a different reason

Trading systems sit in a hostile place between correctness and latency. A bad order is worse than a slow order, but a correct decision that arrives too late may also lose money. OCaml's appeal is pragmatic:

Predictable native-code performance, strong invariant modeling, fast compile cycles, symbolic manipulation, and a runtime simple enough to reason about all matter when both correctness and latency are expensive.

Jane Street is the famous example, but copying their language choice without copying their engineering discipline misses the point. OCaml did not make systems correct by magic. It gave a disciplined team better tools for encoding assumptions and reviewing behavior.

Field notes

  • OCaml is not automatically fast because it is native; allocation-heavy code can still lose badly in latency-sensitive paths.
  • ADTs do not eliminate business-rule mistakes; they make the shapes explicit enough that mistakes are easier to review and test.
  • A type that models every technically possible state is not necessarily a good type. Model the states your domain intends to distinguish.
  • If your team treats compiler warnings as noise, you will lose a large part of the language's value before writing any production code.

Was this lesson helpful?

Sign in to rate