Richard Eisenberg: Using OCaml’s Coercion Operator

A practical tour of subtyping, polymorphic variants and private type abbreviations, with no runtime conversion cost.

RERichard EisenbergVortrag · Jane Street
Kanal
Jane Street
Veröffentlicht
1. Okt. 2024
Video
9:25
Lesezeit
2 Min.
Folien
3
Original
Englisch
Auf YouTube öffnen
Beim Scrollen schrumpft das Video in die rechte untere Ecke und läuft weiter. Untertitel folgen Ihrer Lesesprache.
LesespracheIn 2 Sprachen verfügbar
Diesen Artikel gibt es noch nicht auf Deutsch, daher wird das Original (Englisch) angezeigt.
ZUSAMMENFASSUNG

In 30 Sekunden

Richard Eisenberg demonstrates OCaml’s built-in coercion operator using polymorphic variants, recursive types and lists. He then uses a private integer abbreviation to show how a module can control construction while allowing values to be viewed as integers.

  1. 1Variant subtypingEvery value of a narrower polymorphic variant can be a value of a compatible wider variant.
  2. 2No runtime conversionA list coercion is checked by the type system and does not walk or reallocate the list at runtime.
  3. 3Private representationsA private integer abbreviation lets a module enforce an invariant at construction while allowing coercion back to int.

Das Wichtigste

  1. OCaml can coerce between types when its subtype relation permits it.
  2. Polymorphic variants with compatible tags and payloads provide a subtyping example.
  3. Coercions can work through recursive types and lists without runtime traversal or allocation.
  4. A private abbreviation can expose a representation while restricting construction and preserving an invariant.
VOLLTEXT

Den bearbeiteten Text nach Kapiteln lesen

Nur das Transkript
01

Coercion and polymorphic variants

Eisenberg introduces the built-in coercion syntax with a narrow and a wider variant. He demonstrates why payloads must be compatible.

I want to explore OCaml’s subtyping relation and coercion operator. This is a feature of ordinary OCaml, rather than a Jane Street-specific extension, and it has been available for some time.

Some OCaml types have values that form a subset of another type’s values. Consider polymorphic variants: a type foo containing only the tag `Foo and a type foo_bar containing `Foo or `Bar. Every foo value is also a valid foo_bar value.

The expression `(x : foo :> foo_bar)` asks the compiler to check whether every value of the source type is valid for the target. If the check succeeds, the expression has the target type. When the source type is already known, it can be omitted; I will use the explicit form to make the examples clear.

Coercing a narrower polymorphic variant
FOLIE 1 / 3Coercing a narrower polymorphic variantThe source type contains only `Foo; the target also permits `Bar.

This is a built-in coercion operator, not a user-defined conversion. It only works when the actual definitions satisfy the subtype relationship. If `Foo carries an integer in the source but not in the target, the coercion fails; adding the corresponding integer payload to the target makes the example work again.

In this example the compiler’s error mentions the missing `Bar tag, which is true but is not the reason for failure. The incompatible integer payload is the important issue. The relationship also works recursively when the payloads themselves use compatible recursive types.

02

Recursive types and lists

The subtype relationship can extend through recursive definitions and lists. The coercion is a type check with no runtime conversion.

The coercion can go deeper: a foo list can be viewed as a foo_bar list. Unlike a conventional conversion that traverses and rebuilds a list, this operator is guaranteed to have no runtime effect. The compiler checks and changes the type, but the list is not reallocated or walked.

A list coercion
FOLIE 2 / 3A list coercionThe example widens a list’s variant type without rebuilding the list.
03

Private type abbreviations

A private integer abbreviation supports a construction invariant. Coercion exposes the representation in one direction without permitting arbitrary integers to become the private type.

Another application is a private type abbreviation. A module can declare `type t = private int` while implementing t as an integer. It can provide a constructor that accepts only positive values, giving the module control over an invariant while exposing the underlying representation.

The private type and int are distinct: using a t directly where an int is expected, or an int where a t is expected, does not type-check. But the coercion operator allows a t to be viewed as an int. It works under lists as well, so a list of t can be coerced to an int list without runtime work.

Private integers and list coercion
FOLIE 3 / 3Private integers and list coercionThe module checks positivity when constructing t, while callers can coerce a t list to an int list.

The reverse coercion is rejected. Allowing an arbitrary int to become t would bypass the positive-value invariant. The module controls construction of t values, while callers can inspect them through the exposed integer representation.

The same operator also works with object types. In the next video I will explore how coercion interacts with variance annotations on new data types.

Mehr aus diesem Kanal

1:48:29EN
JSJane Street· 16. Okt. 2026How AI Is Changing Programming: John Krazit and Ron MinskyRon Minsky, John Krazit · Jane StreetJane Street’s AI tooling lead and the Signals and Threads host examine agent infrastructure, security, code review and the changing experience of writing software.Coding-AgentenDeveloper productivityEvals+2
1:36:37EN
JSJane Street· 23. Sept. 2026Aaron Bauer on Teaching Software Engineering in the Age of AIRon Minsky, Aaron Bauer · Jane StreetHow learning goals, active practice and editor design shape engineering education when models can generate the code.AI in EducationSoftware engineeringEducation+2
9 Folien1:05:45EN
JSJane Street· 11. Sept. 2026Cyrus Omar on Live Programming and a Computational Commons for Humans and AICyrus OmarA demonstration of Hazel shows how immediate feedback, structured collaboration and carefully scoped permissions could support people and AI agents working together.Programming LanguagesHuman-AI CollaborationEntwicklungs-Workflows & Tests+2
1:25:00EN
JSJane Street· 2. Sept. 2026Eric Mannes on Turning Messy External Data Into Trading ResearchRon, Eric Mannes · Jane StreetFrom commodity futures and an Excel risk system to point-in-time datasets and AI, Eric Mannes explains the domain knowledge behind data engineering at Jane Street.Data quality & curationEntwicklungs-Workflows & TestsInvesting+2
7 Folien1:16:29EN
JSJane Street· 26. Aug. 2026Grant Sanderson and Alok Puranik: Positional Encodings Through Group TheoryGrant Sanderson, Alok Puranik · 3Blue1BrownStarting with assumptions about attention, they derive matrix exponentials and connect rotations, decay and Jordan blocks to RoPE and ALiBi.Transformers & attentionMathematicsML-Grundlagen+1
27:39EN
JSJane Street· 21. Aug. 20263Blue1Brown and Jane Street on Trading, Machine Learning and CultureGrant Sanderson, Nitya, In Young, Craig, Alok · 3Blue1BrownGrant Sanderson talks with Jane Street colleagues about market making, shared compute, research and the paths that brought them to the firm.Machine LearningFintechCompany Culture+2