Deriving Dependently-Typed OOP from First Principles -- Extended Version with Additional Appendices

David Binder,Ingo Skupin,Tim Süberkrüb,Klaus Ostermann
DOI: https://doi.org/10.1145/3649846
2024-03-11
Abstract:The expression problem describes how most types can easily be extended with new ways to produce the type or new ways to consume the type, but not both. When abstract syntax trees are defined as an algebraic data type, for example, they can easily be extended with new consumers, such as print or eval, but adding a new constructor requires the modification of all existing pattern matches. The expression problem is one way to elucidate the difference between functional or data-oriented programs (easily extendable by new consumers) and object-oriented programs (easily extendable by new producers). This difference between programs which are extensible by new producers or new consumers also exists for dependently typed programming, but with one core difference: Dependently-typed programming almost exclusively follows the functional programming model and not the object-oriented model, which leaves an interesting space in the programming language landscape unexplored. In this paper, we explore the field of dependently-typed object-oriented programming by deriving it from first principles using the principle of duality. That is, we do not extend an existing object-oriented formalism with dependent types in an ad-hoc fashion, but instead start from a familiar data-oriented language and derive its dual fragment by the systematic use of defunctionalization and refunctionalization. Our central contribution is a dependently typed calculus which contains two dual language fragments. We provide type- and semantics-preserving transformations between these two language fragments: defunctionalization and refunctionalization. We have implemented this language and these transformations and use this implementation to explain the various ways in which constructions in dependently typed programming can be explained as special instances of the phenomenon of duality.
Programming Languages
What problem does this paper attempt to address?
### What problems does this paper attempt to solve? This paper aims to explore how to extend dependently - typed programming to object - oriented programming (OOP), thereby filling the gap in this area in programming language design. Specifically, the authors attempt to answer the following research questions: 1. **Expression Problem**: How can one easily add new data constructors and new operation functions while maintaining the existing code structure? This is especially important in dependently - typed systems because dependently - typed systems allow for more complex type definitions and stronger static checking. 2. **Duality of Functional and Object Paradigms**: The paper proposes that dependently - typed programming almost entirely follows the functional programming model and ignores the advantages of object - oriented programming. The authors hope to show, by introducing the concept of "duality", how to derive an equivalent object - oriented programming model from an existing functional programming model. 3. **OOP Form of Dependent Types**: The goal of the paper is to develop a dependently - typed object - oriented programming language and provide a systematic method to implement this language. To this end, the authors introduce defunctionalization and refunctionalization as tools to derive the dual object - oriented fragments from familiar functional programming language fragments. 4. **Unifying Data Types and Codata Types**: By defining self - parameters and copattern matching, the paper shows how to unify data types and codata types in a dependently - typed system. This enables programs to handle complex data structures and algorithm verification more flexibly. 5. **Supporting Intrinsic and Extrinsic Verification**: The paper not only discusses how to use dependently - typed for intrinsic verification but also explores how to separate objects from their specifications, thereby supporting extrinsic verification. This provides a new method for verifying the behavior of object - oriented classes. ### Specific Problems and Solutions - **Problem**: Most dependently - typed languages follow the functional programming model and cannot fully utilize the advantages of object - oriented programming. **Solution**: By introducing the concept of duality, it is shown that dependently - typed programming can also adopt an object - oriented style, and these models can be systematically derived through defunctionalization and refunctionalization techniques. - **Problem**: The expression problem is more complex in dependently - typed systems because it is necessary to expand data constructors and operation functions simultaneously. **Solution**: By introducing self - parameters and copattern matching, the dependently - typed system can better handle complex expansion requirements. - **Problem**: How to represent and process basic data types such as natural numbers and Boolean values in a dependently - typed system. **Solution**: Through the refunctionalization technique, the corresponding codata type encodings, such as Church encoding and Scott encoding, are derived from the existing data types. In summary, this paper explores the application of dependently - typed programming in object - oriented programming by introducing the concept of duality and systematic language design tools, providing new ideas and methods for future research and practice.