Typestate via Revocable Capabilities
Songlin Jia, Craig Liu, Siyuan He, Haotian Deng, Yuyan Bao, Tiark Rompf
TL;DR
This paper tackles the challenge of safely managing stateful resources in the presence of aliasing by unifying scope-based and flow-sensitive approaches. It introduces revocable capabilities and path-dependent types to decouple capability lifetimes from lexical scopes, enabling functions to receive, revoke, and return capabilities in a flow-sensitive manner. The authors provide a Scala 3 prototype that combines a destructive effect system, per-object path-dependent capabilities, and a Sigma-based bundling mechanism with a type-directed ANF transformation to support a wide range of stateful patterns, including file I/O, locking protocols, DOM construction, and session types. Key contributions include the three interactions (receiving, revoking, returning) mediated by implicit resolution, the Sigma abstraction for resource-capability pairing, and practical case studies demonstrating safe and ergonomic typestate programming. The work demonstrates that expressive and safe typestate management can be achieved with minimal extensions to existing capability-based languages, offering a scalable path toward more robust stateful programming in real-world languages like Scala 3.
Abstract
Managing stateful resources safely and expressively is a longstanding challenge in programming languages, especially in the presence of aliasing. While scope-based constructs such as Java's synchronized blocks offer ease of reasoning, they restrict expressiveness and parallelism. Conversely, imperative, flow-sensitive management enables fine-grained control but demands sophisticated typestate analyses and often burdens programmers with explicit state tracking. In this work, we present a novel approach that unifies the strengths of both paradigms by extending flow-insensitive capability mechanisms into flow-sensitive typestate tracking. Our system decouples capability lifetimes from lexical scopes, allowing functions to provide, revoke, and return capabilities in a flow-sensitive manner, based on the existing mechanisms explored for the safety and ergonomics of scoped capability programming. We implement our approach as an extension to the Scala 3 compiler, leveraging path-dependent types and implicit resolution to enable concise, statically safe, and expressive typestate programming. Our prototype generically supports a wide range of stateful patterns, including file operations, advanced locking protocols, DOM construction, and session types. This work demonstrates that expressive and safe typestate management can be achieved with minimal extensions to existing capability-based languages, paving the way for more robust and ergonomic stateful programming.
