Examining Structural Subtyping and Parametric Polymorphism
A study on the relationship between structural subtyping and parametric polymorphism in programming languages.
― 6 min read
Table of Contents
Programming languages have different ways to manage Types, which define what kind of data can be used in various operations. Two important concepts in this area are Structural Subtyping and Parametric Polymorphism. Each provides unique advantages to programmers, particularly when it comes to writing reusable code.
What is Structural Subtyping?
Structural subtyping allows for a relationship between types based on their structure rather than their name. For example, if one type has all the elements of another type plus some extras, it can be used wherever the other type is expected. This relationship makes code more flexible because it doesn't require exact matches in names; as long as the structures are compatible, one can substitute for the other.
What is Parametric Polymorphism?
Parametric polymorphism allows functions to operate on values of any type. By using placeholders, a function can work with many different types without specifying which type it will use in advance. This feature enhances code reuse because a single function can handle various types of data.
Comparing Structural Subtyping and Parametric Polymorphism
Both structural subtyping and parametric polymorphism offer a way to write more versatile and reusable code. However, they achieve this in different ways. Structural subtyping focuses on the shapes and structures of types, while parametric polymorphism uses generic types that can stand in for any type.
While these two features may seem similar, they have distinct implications for how programming languages operate. Understanding the precise relationship between them remains an area of ongoing inquiry.
Context of the Study
Despite their importance, the connection between parametric polymorphism and structural subtyping has not been extensively examined in academic literature. Most discussions are based on a general intuition rather than solid evidence. This study aims to systematically explore the expressive power of both concepts and establish a clearer understanding of how they relate.
Key Concepts in the Investigation
To investigate the relationship between structural subtyping and parametric polymorphism, this paper focuses on:
- Row Types: A mechanism used to define types with variable fields, allowing for more flexible structures.
- Variations in Subtyping: Different approaches to structuring types, such as nominal and structural subtyping.
- Polymorphism Variants: Different forms of polymorphism, including row and presence polymorphism.
The Framework of the Study
The study employs various calculi to characterize expressiveness in programming language design. By examining the relationships among types through this framework, we aim to clarify the dynamics of structural subtyping and parametric polymorphism.
Types, Terms, and Rows
- Types: Classes of data that indicate the kind of values that can be assigned. For example, integers, strings, and more complex structures like records.
- Terms: Specific instances of types used in programming. These can include variables, functions, and combinations of both.
- Rows: A special way to define types that allows for dynamic field presence, making it easier to add or remove fields in records.
Practical Examples
Simple Example: Variants
Consider a function that retrieves an age from a record. The record represents a person with a year value. If a function expects a narrower type but is given a broader one (which includes more fields), structural subtyping allows this without causing an error.
In a programming language with structural subtyping, one can easily upcast a narrower type to a broader one. This flexibility makes the code cleaner and more maintainable.
Simple Example: Records
For record types, the same principle applies. If there is a function expecting a record with fewer fields, a record with additional fields can be passed to the function. This is particularly beneficial when working with complex data structures where different functions may expect variations of a similar type.
The Importance of Expressiveness
Understanding the expressiveness of different systems is crucial for designing programming languages that effectively support both structural subtyping and parametric polymorphism. Expressiveness refers to a system's ability to represent and manage a wide range of types and operations.
How to Measure Expressiveness
Expressiveness can be measured by showing how different types can be composed or transformed into each other. This involves using translations between various calculi and proving that the transformations preserve the meaning of types.
Core Theoretical Foundations
The theoretical foundation of this investigation is based on several well-established principles in type theory.
Type Preservation
Type preservation ensures that if a term is well-typed in one system, it remains well-typed in the translated version. This property is vital for establishing the correctness of any encoding between subtyping and polymorphism.
Operational Correspondence
This principle states that the operational behavior of systems remains consistent when transitioning between different representations of types. For example, if a term behaves in a certain way in one calculus, it should behave similarly in another calculus through proper translations.
Examination of Subtyping and Polymorphism
Structural Subtyping in Detail
Structural subtyping is generally established through a series of rules that define when one type can be considered a subtype of another. The key feature here is that it focuses on the actual structure and contents of types rather than their names.
Parametric Polymorphism in Detail
Parametric polymorphism leverages type variables to allow a function to be used with different types. For example, a function that operates on a list can work with lists of integers, strings, or any other type without needing to be rewritten for each type.
Investigating Translations Between Systems
To understand the relationship between subtyping and polymorphism, we will analyze translations between different systems.
Local vs. Global Translations
Local Translations: These restrict changes to only those terms and types directly involved in the translation. This method is useful for understanding which features can be expressed in a given system without extensive changes to the overall program.
Global Translations: These allow for more extensive changes that may involve the entire program. This approach can lead to a broader understanding of how different systems can interact and the potential overlaps between features.
Non-Existence Results
Throughout our exploration, we will present several non-existence results, showing situations where certain types of translations cannot be achieved.
Implications of Non-Existence Results
The implications of these results shed light on the limitations of current systems and highlight the need for careful design considerations in programming language development. Understanding what cannot be achieved is as crucial as understanding what can be done.
Conclusion
In conclusion, this study systematically examines the interplay between structural subtyping and parametric polymorphism. By providing clear examples and theoretical foundations, we aim to clarify how the two features relate and how each can be utilized effectively in programming language design.
Future work will extend this investigation further, focusing on advanced forms of polymorphism and their interactions with subtyping, as well as exploring how these concepts can be applied in practical programming scenarios.
We believe these insights will contribute significantly to the ongoing dialogue in the field of programming languages and type theory, benefiting both academic research and practical software development.
Title: Structural Subtyping as Parametric Polymorphism
Abstract: Structural subtyping and parametric polymorphism provide similar flexibility and reusability to programmers. For example, both features enable the programmer to provide a wider record as an argument to a function that expects a narrower one. However, the means by which they do so differs substantially, and the precise details of the relationship between them exists, at best, as folklore in literature. In this paper, we systematically study the relative expressive power of structural subtyping and parametric polymorphism. We focus our investigation on establishing the extent to which parametric polymorphism, in the form of row and presence polymorphism, can encode structural subtyping for variant and record types. We base our study on various Church-style $\lambda$-calculi extended with records and variants, different forms of structural subtyping, and row and presence polymorphism. We characterise expressiveness by exhibiting compositional translations between calculi. For each translation we prove a type preservation and operational correspondence result. We also prove a number of non-existence results. By imposing restrictions on both source and target types, we reveal further subtleties in the expressiveness landscape, the restrictions enabling otherwise impossible translations to be defined. More specifically, we prove that full subtyping cannot be encoded via polymorphism, but we show that several restricted forms of subtyping can be encoded via particular forms of polymorphism.
Authors: Wenhao Tang, Daniel Hillerström, James McKinna, Michel Steuwer, Ornela Dardha, Rongxiao Fu, Sam Lindley
Last Update: 2023-09-11 00:00:00
Language: English
Source URL: https://arxiv.org/abs/2304.08267
Source PDF: https://arxiv.org/pdf/2304.08267
Licence: https://creativecommons.org/licenses/by/4.0/
Changes: This summary was created with assistance from AI and may have inaccuracies. For accurate information, please refer to the original source documents linked here.
Thank you to arxiv for use of its open access interoperability.