Law and Order for Typestate with Borrowing

Hannes Saffrich,Yuki Nishida,Peter Thiemann
2024-09-26
Abstract:Typestate systems are notoriously complex as they require sophisticated machinery for tracking aliasing. We propose a new, transition-oriented foundation for typestate in the setting of impure functional programming. Our approach relies on ordered types for simple alias tracking and its formalization draws on work on bunched implications. Yet, we support a flexible notion of borrowing in the presence of typestate. Our core calculus comes with a notion of resource types indexed by an ordered partial monoid that models abstract state transitions. We prove syntactic type soundness with respect to a resource-instrumented semantics. We give an algorithmic version of our type system and prove its soundness. Algorithmic typing facilitates a simple surface language that does not expose tedious details of ordered types. We implemented a typechecker for the surface language along with an interpreter for the core language.
Programming Languages
What problem does this paper attempt to address?
The problem that this paper attempts to solve is: how to design a basic framework in a typestate system with a borrowing mechanism that can both track aliasing and flexibly support borrowing operations. Specifically, the author proposes a transformation - oriented basic framework based on ordered types for typestate management in an impure functional programming environment. ### Main problems and challenges 1. **Complexity**: - Typestate systems are usually very complex because they require complex mechanisms to track alias relationships between objects. 2. **Alias tracking**: - Traditional typestate systems have difficulty dealing with aliasing (multiple references pointing to the same object), which may lead to uncertain or incorrect program behavior. 3. **Borrowing mechanism**: - After introducing the borrowing mechanism, it is a challenge to ensure that the borrowed object is not misused and that the state changes of the object during borrowing can be correctly reflected in its type. ### Solutions The author proposes a new perspective to solve these problems, and the main contributions include: 1. **Ordered Partial Monoid (OPM)**: - Proposes using the ordered partial monoid as the minimum specification to describe the typestate system with borrowing. This abstraction can better manage resources and handle borrowing operations. 2. **Typed λ - calculus**: - Defines a typed λ - calculus that supports intuition, linear, and ordered constructs. Different types (such as function spaces and products) have different order properties, which makes type - checking more strict and accurate. 3. **Semantics and metatheory**: - Establishes a syntactic metatheory to ensure type safety and combines object typestate and borrowing protocol compliance. 4. **Algorithmic version of the type system**: - Provides a syntactic - based version of the type system as the basis for implementing a type - checker prototype. If certain operations on the underlying OPM are decidable, then type - checking is also decidable. ### Example illustration Taking a file handle as an example, the author shows how to use a regular language to represent different states of a file (open, read, write, close). In this way, the typestate can accurately describe the operation sequence of the file handle and ensure that these operations are carried out in the correct order. For example: - After opening a file, the file handle is in a state where it can be read from or written to. - After reading or writing, the file handle can still continue to be read from, written to, or closed. - After closing, the file handle can no longer perform any operations. In addition, the author also introduces the `split` operation, which allows temporary borrowing of resources without transferring ownership. For example, for a file handle, a complete handle can be divided into two parts by `split`: one for borrowing operations and the other for retaining ownership, ensuring that resources can be correctly recycled after borrowing ends. ### Conclusion The main goal of this paper is to provide a more concise, flexible, and safe way to handle typestate systems with borrowing mechanisms by introducing ordered partial monoids and a new typed λ - calculus. This not only improves the security and maintainability of the code but also provides strong support for modeling complex object behaviors.