Polymorphic Records for Dynamic Languages

Giuseppe Castagna,Loïc Peyrot
2024-03-30
Abstract:We define and study "row polymorphism" for a type system with set-theoretic types, specifically union, intersection, and negation types. We consider record types that embed row variables and define a subtyping relation by interpreting types into sets of record values and by defining subtyping as the containment of interpretations. We define a functional calculus equipped with operations for field extension, selection, and deletion, its operational semantics, and a type system that we prove to be sound. We provide algorithms for deciding the typing and subtyping relations.
Programming Languages
What problem does this paper attempt to address?
The main problem that this paper attempts to solve is to design and research a row polymorphism type system with set - theoretical types (including union, intersection, and negation types) for dynamic languages (such as Elixir). Specifically, the goals of the paper are: 1. **Define and research row polymorphism with set - theoretical types**: The paper introduces row variables and defines the subtype relationship by interpreting types as sets of values, thereby providing more precise type inference for record types. 2. **Develop a static type system suitable for dynamic languages**: As more and more dynamic languages (such as TypeScript and Flow) begin to introduce static type systems, the paper aims to provide a more flexible and precise type system for these languages, especially for those dynamic languages (such as Luau and Elixir) that have already adopted set - theoretical types. 3. **Solve the deficiencies of existing theories under set - theoretical types**: The existing row polymorphism theories cannot be directly applied to systems with set - theoretical types. Therefore, the paper proposes a new theoretical framework to meet this challenge. 4. **Provide algorithm implementation**: The paper not only makes theoretical contributions but also presents algorithms for determining type and subtype relationships and proves the correctness and completeness of these algorithms. ### Specific Problems and Motivations The paper shows the limitations of the existing type system through a specific example - the `add_elixir_domain` function in the Elixir log module. In this example, the function accepts an argument of a record type and updates or adds fields according to conditions. However, due to the lack of row polymorphism, the current type system cannot accurately preserve the information of other fields in the input record, causing the type checker to be unable to correctly handle certain expressions. For example, for the expression `add_elixir_domain(%{file: "foo.txt", line: 42}).line`, although it is correct, the type checker will reject it because there is no guarantee that the resulting record still contains the `:line` field. By introducing row variables, the behavior of the function can be described more precisely, ensuring the accuracy of the type system. ### Solutions The paper proposes a solution that combines row polymorphism and set - theoretical types, enabling the type system to more precisely describe the extension and deletion operations of record types. Specifically, by using row variables, all fields in the record can be captured, except for specific fields (such as `:domain`), thus avoiding the loss of type information. In addition, the paper also solves the deficiencies of the existing row polymorphism theories in dealing with set - theoretical types, especially the problems in dealing with optional fields and negation types. By introducing new subtype determination algorithms and type substitution rules, the paper provides a more complete theoretical basis for the set - theoretical type system. ### Summary In summary, the main goal of this paper is to develop a more flexible and precise static type system for dynamic languages, especially by introducing row polymorphism and set - theoretical types to solve the limitations of existing type systems in dealing with record types. This not only improves the expressive power of the type system but also provides a solid theoretical basis for static type checking in dynamic languages.