Introducing Local Parameters in OCaml

A demonstration of how Jane Street's experimental local mode restricts escaping values and makes function interfaces easier to reason about.

チャンネル
Jane Street
公開日
2024年1月5日
動画
8:52
読了時間
3 分
スライド
4
原文
英語
YouTube で開く
スクロールすると動画は右下に小さくなって再生を続けます。字幕は閲覧言語に合わせて切り替わります。
閲覧言語2 言語で読めます
この記事はまだ日本語に翻訳されていないため、英語の原文を表示しています。
要約

30 秒でわかる

A Jane Street speaker introduces local mode in the company's experimental OCaml compiler branch. Through examples involving returned values, mutable references, and file callbacks, he explains the promise a local parameter makes to callers and sets up a later discussion of allocation.

  1. 1An experimental modeLocal mode is an idea developed in Jane Street's OCaml compiler branch. The speaker introduces its type-system behavior before addressing its allocation benefits.
  2. 2A parameter that cannot escapeMarking a function parameter local promises that it cannot escape its region. Returning that parameter produces a compiler error.
  3. 3Storage is another escapePutting a local string into an external reference also lets it escape and is rejected, even if the function returns another value.
  4. 4Reasoning about mutationA caller can use the local annotation to reason that the function has not retained its first argument when it later changes that argument.
  5. 5A file callbackA callback accepting a local file handle cannot retain it, allowing the enclosing function to close the file after the callback returns.

ポイント

  1. Local mode was demonstrated in Jane Street's experimental compiler branch, with the goal of eventually upstreaming the work.
  2. A local parameter cannot escape its region, for example by being returned or stored in an external reference.
  3. Local mode restricts the function implementation while permitting callers to pass either local or global arguments.
  4. A non-escaping parameter helps callers reason about later mutations and callback resource lifetimes.
  5. The series will later connect local mode to allocation behavior and latency-sensitive performance.
全文

チャプターごとに整えた全文を読む

文字起こしのみ
01

The local mode experiment

The speaker introduces local mode in Jane Street's OCaml compiler branch and explains why this episode starts with type-system behavior. The longer-term goal includes better allocation for latency-sensitive code.

I want to start a series about local mode, an idea developed in Jane Street's experimental branch of the OCaml compiler. This branch is where we try and refine ideas with the goal of eventually upstreaming them into the main compiler. For now, local mode is available in the Jane Street compiler for us to explore.

We'll work through how local affects the type system, piece by piece. The eventual payoff is allocation behavior that can make latency-sensitive code faster, but I think understanding the type-level behavior first is a better way to approach it than jumping straight to allocation.

I'm using Jane Street's version of OCaml 4.14.1. Local is a mode, not exactly a type, so I don't want to call these local types. In the examples I'll write full type annotations on my let bindings: inference works with local mode, but explicit signatures give us more compact examples and better error messages.

02

Parameters and escaping regions

A local argument cannot escape its region by being returned or stored elsewhere. The speaker demonstrates compiler errors and notes that the simplified rule that every function defines a region has an exception.

Start with a simple function and add the local annotation to its parameter. This says the parameter does not escape the function. If the function returns that local parameter, the compiler reports that the value escapes its region: returning it breaks the promise made by the annotation.

A local parameter and an attempted return
スライド 1 / 4A local parameter and an attempted returnThe example annotates a parameter as local and illustrates the non-escape promise.

With a function of two arguments, the annotation can apply only to the first, x. Returning x causes the escape error, but returning the unannotated second argument y compiles. We have made a promise about x, not about y.

A region is an area of code. For now, we can think of every function as defining a region, although there is an exception that I'll explain in a later video. A local parameter cannot escape its region, and returning it is one way to do that.

Storing a local value elsewhere is another way to escape. For example, if x is a local string parameter and I assign it to an external string reference, the compiler rejects the assignment even when the function returns y. The local annotation promises callers that the argument will not escape.

Storing a local parameter in a reference
スライド 2 / 4Storing a local parameter in a referenceThe code attempts to assign the local argument to an external reference, another way the value could escape.
03

Reasoning about mutation and callbacks

A non-escaping argument helps callers reason about later mutation. A file callback demonstrates how a local handle can be prevented from escaping its intended lifetime.

Why is that useful? Suppose a type has a mutable string field and a function f takes its first argument locally. A caller can call f with t1 and t2 and later mutate t1. Since f cannot retain t1, the caller can reason that the later mutation won't affect f's retained relationship to that argument or a value returned by f.

A mutable value passed to a local parameter
スライド 3 / 4A mutable value passed to a local parameterThe example sets up reasoning about a mutable record after a call to a function with a local first argument.

Local parameters are also useful for callbacks. Imagine a with_file function taking a filename and a callback whose file-handle parameter is local. Once the callback finishes, the enclosing function knows the handle has not been stored or returned by it, so it can close the file. Without this guarantee, we would have to trust the callback not to keep the handle.

A local file handle in a callback
スライド 4 / 4A local file handle in a callbackThe callback signature marks its handle parameter local so it cannot be retained after the callback.
04

A promise to callers

Local mode constrains a function's implementation rather than the kinds of arguments a caller can pass. Both local and global arguments are allowed.

The annotation restricts the implementation of the function, not what a caller may pass. A local parameter can receive a local or a global argument. The restriction against returning or storing that argument is the promise the function makes to every caller; this is only the beginning of the exploration.

同じチャンネルの記事

1:48:29EN
JSJane Street· 2026年10月16日How 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.コーディングエージェントDeveloper productivity評価(Evals)+2
1:36:37EN
JSJane Street· 2026年9月23日Aaron 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 枚1:05:45EN
JSJane Street· 2026年9月11日Cyrus 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 Collaboration開発ワークフローとテスト+2
1:25:00EN
JSJane Street· 2026年9月2日Eric 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 & curation開発ワークフローとテストInvesting+2
スライド 7 枚1:16:29EN
JSJane Street· 2026年8月26日Grant 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 & attentionMathematics機械学習の基礎+1
4:41EN
JSJane Street· 2026年7月6日Inside Jane Street’s Internship: Projects, Mentorship and LearningCody · Jane StreetInterns and employees describe collaborative interviews, learning finance from the basics, and working on projects used by the firm.CareersCompany CultureMachine Learning+1