Decidability and Algorithmic Analysis of Dependent Object Types (DOT)
Total Page:16
File Type:pdf, Size:1020Kb
Decidability and Algorithmic Analysis of Dependent Object Types (DOT) by Zhong Sheng (Jason) Hu A thesis presented to the University of Waterloo in fulfillment of the thesis requirement for the degree of Master of Mathematics in Computer Science Waterloo, Ontario, Canada, 2019 c Zhong Sheng (Jason) Hu 2019 I hereby declare that I am the sole author of this thesis. This is a true copy of the thesis, including any required final revisions, as accepted by my examiners. I understand that my thesis may be made electronically available to the public. ii Abstract Dependent Object Types, or DOT , is a family of calculi developed to study the Scala programming language. These calculi have path dependent types as a feature, and poten- tially intersection types, union types and recursive types. So far, the study of DOT calculi mostly focuses on the soundness proof, which does not directly contribute to development of compilers. This thesis presents a detailed investigation of decidability and algorithmic properties of the family of DOT calculi. In decidability analysis, the undecidability of subtyping of several calculi is formally established, including the D<: and D^ calculi. Prior to this investigation, the undecidabil- ity of subtyping of all DOT calculi including D<: was open. Decidability analysis puts emphasis on a particular form of subtyping rules, called normal form. It turns out that a normal form definition is not only as expressive, but also more suggestive than the original definition. A conceptual device, called small-step analysis, is introduced to assist convert- ing a usual definition of subtyping to its normal form definition. Moreover, decidability analysis gives direct contributions to the algorithmic analysis, by revealing two decidable fragments of D<: in declarative form, called the kernels. Decidability analysis also suggests a novel subtyping algorithm framework, stare-at subtyping. Stare-at subtyping and an existing algorithm are shown to be sound and complete w.r.t. their corresponding kernels. In algorithmic analysis, stare-at subtyping is extended to other calculi, with more fea- tures than D<:, including D^, µDART and jDOT . In µDART and jDOT , bi-directional type assignment algorithms are developed. The algorithms developed in this thesis are all shown to be sound with respect to their target calculi and terminating. During the development of the algorithms, analysis shows a number of ways in which the Wadlerfest DOT calculus does not directly correspond to the Scala language, while substantially increases the difficulties of algorithmic design. jDOT , therefore, is developed as an alternative formalization of Scala. iii Acknowledgements In my opinion, a thesis is not only a piece of technical work but also a cumulative result of social events preceding it. At the time of concluding this thesis, it is the best moment to look back in my life and appreciate everyone and everything that make this thesis possible. I would like to thank my supervisor Professor OndˇrejLhot´akfor knowledge I have learned and his continuous support since my application for the Master's program and during my research. I feel very grateful for the lab environment. My understanding of DOT had been significantly improved thanks to conversations with Marianna Rapoport, Ifaz Kabir and surely my supervisor. The thesis inherits work from Abel Nieto's project. The discussions with him benefited greatly my work on undecidability proof of D<:, for which I am equally grateful. I would like to thank my readers Professors Prabhakar Ragde and Brad Lushman for their careful review of my thesis. I would also like to thank Professors Yong Wang, Bo Hu, Xiaofeng Wu and Hui Feng for their support for my Master's application after years of my graduation from Bachelor's, and Professors Arie Gurfinkel and Richard Trefler for their support for my PhD application. Last, I must thank my parents and my grandparents for giving me abundant freedom since my childhood. I am grateful to Pierre Grondin. It would probably have been much more difficult to settle in Canada without his help. iv Table of Contents List of Tablesx List of Figures xi 1 Introduction1 1.1 Motivation....................................1 1.1.1 Decidability...............................2 1.1.2 Type checking..............................3 1.2 Methodologies..................................3 1.3 Organization..................................4 1.4 Contributions..................................4 2 Background8 2.1 Conventions...................................8 2.2 Full F<: and Kernel F<: ............................ 10 2.3 Undecidability of Full F<: ........................... 13 2.4 Bi-directional Type Assignment........................ 16 2.5 Scala and DOT Calculi............................. 16 2.5.1 Path Dependent Types......................... 17 2.5.2 Intersection Types........................... 20 2.5.3 Wadlerfest DOT ............................ 22 v 2.5.4 Other DOT Calculi........................... 25 2.6 Related Work.................................. 26 2.6.1 Formalizations of Scala......................... 26 2.6.2 Undecidability of subtyping...................... 27 2.6.3 Algorithmic (sub)typing........................ 27 2.6.4 Formalization of undecidability proofs................ 28 3 The Undecidability of D<: 29 3.1 Definition of D<: ................................ 30 3.2 Definition of Undecidability.......................... 33 3.3 The Incomplete Proof.............................. 34 − 3.4 F<: as Q..................................... 36 3.5 An Attempt at An Undecidability Proof................... 37 3.6 How Was Undecidability of F<: Proved?................... 42 3.7 Small-step Subtyping.............................. 44 3.7.1 An example............................... 47 3.7.2 Rearranging universal types...................... 48 3.7.3 Proofs of transitivity and narrowing in normal form......... 49 − 3.8 Undecidability of F<:> ............................. 52 3.9 Small-step Analysis of D<: ........................... 57 3.10 Undecidability of Type Assignment of D<: .................. 69 3.11 Discussions................................... 70 3.11.1 D<<: ................................... 70 3.11.2 What about DOT ?........................... 72 3.11.3 Calculi in Normal Forms........................ 74 vi 4 Algorithmic Typing of D<: Fragments 75 4.1 Kernel D<: .................................... 76 4.2 Step Subtyping................................. 78 4.3 Step Typing................................... 81 4.4 A Note on Execution of Step Subtyping.................... 85 4.5 Kernel D<: and Step Subtyping........................ 86 4.6 Strong Kernel D<: ............................... 89 4.7 Stare-at Subtyping............................... 92 4.7.1 Limitations of Step Subtyping..................... 93 4.7.2 Definition................................ 94 4.8 Properties of Operations in Stare-at Subtyping................ 97 4.9 Strong Kernel D<: and Stare-at Subtyping.................. 100 4.10 Discussions................................... 101 4.10.1 Convergence of Theories........................ 101 4.10.2 Properties of Kernel Calculi...................... 102 5 D^ 105 5.1 Definition of D^ ................................. 105 5.2 Strong Kernel D^ ................................ 109 5.3 Revealing in D^ ................................ 110 5.4 Stare-at Subtyping............................... 114 5.5 Properties of Stare-at Subtyping and Properties............... 118 5.6 Strong Kernel D^ and Stare-at Subtyping.................. 120 6 µDART 122 6.1 Definition of µDART .............................. 123 6.2 Difficulties of µ Types............................. 126 6.3 Stare-at Subtyping for µDART ........................ 127 vii 6.4 Revealing Reconsidered............................ 129 6.5 µ Types as Cyclic Contexts.......................... 130 6.6 Types Are Resources!.............................. 133 6.7 Revealing in µDART ............................. 134 6.8 An Example of Revealing ........................... 137 6.9 Properties of Operations and Stare-at Subtyping............... 138 6.10 How Large Is the Decidable Fragment?.................... 142 6.10.1 What is the language?......................... 143 6.10.2 How does removal impact the decidable fragment?.......... 143 6.11 An Alternative Treatment........................... 144 6.12 Variable Typing................................. 145 7 jDOT 148 7.1 A Step Back: Algorithmic (Sub)typing Reconsidered............ 149 7.1.1 Subtyping as communication...................... 149 7.1.2 Backtracking due to ^-Traversal ................... 149 7.1.3 Types as resources........................... 149 7.2 A Second Step Back: A Short Review of DOT ................ 151 7.2.1 Uninterpretable types with µ ..................... 151 7.2.2 How are objects encoded?....................... 152 7.2.3 Unexpected recursive path types................... 153 7.2.4 Soundness proof guided definition................... 154 7.3 The Definition of jDOT ............................ 155 7.4 Refining Revealing .............................. 159 7.5 Examples of Revealing ............................ 164 7.5.1 Maintenance of well-formedness.................... 164 7.5.2 Handling object types......................... 164 7.6 Properties of Operations............................ 166 viii 7.7 Stare-at Subtyping............................... 169 7.8 Variable Typing................................. 170 7.8.1 Variable type check........................... 170 7.8.2 Variable type synthesis........................