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.
翻译:安全且富有表现力地管理有状态资源是编程语言中长期存在的挑战,尤其是在存在别名的情况下。虽然基于作用域的构造(如Java的同步块)提供了易于推理的特性,但它们限制了表达能力和并行性。相反,命令式的、流敏感的管理实现了细粒度控制,但需要复杂的类型状态分析,并且常常使程序员承担显式状态跟踪的负担。在这项工作中,我们提出了一种新颖的方法,通过将流不敏感的能力机制扩展为流敏感的类型状态跟踪,统一了两种范式的优势。我们的系统将能力生命周期与词法作用域解耦,允许函数以流敏感的方式提供、撤销和返回能力,这基于已探索的、用于提升作用域能力编程安全性和人体工程学的现有机制。我们将我们的方法实现为Scala 3编译器的扩展,利用路径依赖类型和隐式解析,以实现简洁、静态安全且富有表现力的类型状态编程。我们的原型通用地支持广泛的有状态模式,包括文件操作、高级锁定协议、DOM构造和会话类型。这项工作表明,通过对现有基于能力的语言进行最小扩展,即可实现富有表现力且安全的类型状态管理,为更健壮和符合人体工程学的有状态编程铺平了道路。