NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Tracking Captured Variables in Types

Aleksander Boruch-Gruszecki, Jonathan Immanuel Brachthäuser, Edward Lee, Ondrej Lhoták, Martin OderskyarXiv 2021

Abstract

This paper introduces $\text{CF}_{<:}$, a formal calculus that augments types with "capture sets" to track free variables. By modeling capabilities as captured variables, the system provides a unified framework for reasoning about memory safety, effect handlers, and non-local returns. We describe the syntax and subtyping rules of the calculus and discuss its formal verification in the Coq proof assistant.

Introduction

In traditional programming language theory, types describe what a value is—such as an integer, a string, or a function. However, they rarely capture what a value knows or what resources it holds onto. This limitation makes it difficult to statically ensure capability safety: the guarantee that sensitive resources, like file handles or memory regions, do not "escape" their intended scope. This paper, authored by a team including Martin Odersky, introduces $\text{CF}_{<:}$, a new calculus that tracks "captured" variables directly within the type system.

The Challenge of Capability Safety

Modern software architecture increasingly relies on capabilities—objects that grant permission to perform sensitive actions. A major safety challenge is "capture": if a function closes over a local capability and is subsequently returned or passed to a different part of the program, that capability has escaped its intended scope. Existing solutions, such as second-class values, are often too restrictive for complex functional programming. $\text{CF}_{<:}$ aims to be a lightweight and conservative extension to standard type theory that precisely tracks these dependencies.

The $\text{CF}_{<:}$ Calculus

The researchers built $\text{CF}_{<:}$ upon System $F_{<:}$, a standard foundation for polymorphic languages with subtyping. The core innovation is the addition of capture sets to every type.

The syntax of the CF<: calculus, defining capture sets (C), terms (t), and types (T).

As shown in the syntax, a type $T$ is composed of a capture set $C$ and a pretype $U$. For example, a type {File} Logger describes a logger that might capture a specific File capability. A "pure" value has an empty capture set {}.

To make this system flexible, the authors introduced subcapturing and capture polymorphism. Subcapturing allows a function requiring a logger that captures at most {File, Console} to safely accept a logger that only captures {File}. Capture polymorphism allows functions to be generic over the sets they capture, enabling a function to return a result that captures exactly the same set as its argument.

Subtyping rules for types and pretypes in CF<:, showing how capture sets integrate into the type hierarchy via the (CAPT) rule.

The subtyping rules integrate these capture sets into the standard hierarchy. The (CAPT) rule is particularly significant, as it allows subtyping of types based on the subcapturing relationship of their constituent sets and the subtyping of their underlying pretypes.

Key Insights and Results

The core insight of this work is that the free variables of a term represent its authority. By tracking these variables in the type, the system becomes a tool for auditing authority. Furthermore, the system is predictive; it tracks what variables the final value will capture after execution, allowing temporary variables to be used without polluting the final type.

This unified theory effectively models three distinct problems:

  1. Non-local returns: Ensuring a return capability doesn't outlive its block.
  2. Region-based memory: Ensuring pointers don't outlive their allocated memory regions.
  3. Effect Handlers: Ensuring capabilities to perform effects are handled by the correct scope.

The authors proved the system's soundness—ensuring well-typed programs do not violate capture constraints—and mechanized these proofs in Coq. They also demonstrated that standard System $F_{<:}$ terms embed naturally into $\text{CF}_{<:}$, preserving existing language expressivity.

Conclusion and Future Work

While $\text{CF}_{<:}$ provides a robust foundation, challenges remain in handling "capture-polymorphic" data types (like lists containing elements with different capture sets) and complex nested memory regions. The next major step is the integration of these concepts into production languages like Scala 3, balancing mathematical precision with developer ergonomics.

Summary updated

BibTeX
@misc{boruchgruszecki2021tracking,
  author = {Aleksander Boruch-Gruszecki and Jonathan Immanuel Brachthäuser and Edward Lee and Ondrej Lhoták and Martin Odersky},
  title = {{Tracking Captured Variables in Types}},
  year = {2021},
  eprint = {2105.11896},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2105.11896},
}
Figure