Modal Types for Mobile Code
Total Page:16
File Type:pdf, Size:1020Kb
Modal Types for Mobile Code Tom Murphy VII CMU-CS-08-126 May 13, 2008 School of Computer Science Carnegie Mellon University Pittsburgh, PA 15213 Thesis Committee: Robert Harper, Co-Chair Karl Crary, Co-Chair Frank Pfenning Peter Sewell Submitted in partial fulfillment of the requirements for the degree of Doctor of Philosophy. Copyright c 2008 Tom Murphy VII Keywords: modal logic, distributed computing, programming languages, web pro- gramming, compilers, types Abstract In this dissertation I argue that modal type systems provide an elegant and practical means for controlling local resources in spatially distributed computer programs. A distributed program is one that executes in multiple physical or logical places. It usually does so because those places have local resources that can only be used in those locations. Such resources can include processing power, proximity to data, hardware, or the physical presence of a user. Programmers that write distributed applications therefore need to be able to reason about the places in which their programs will execute. This work provides an elegant and practical way to think about such programs in the form of a type system derived from modal logic. Modal logic allows for reasoning about truth from multiple simultaneous perspectives. These perspectives, called “worlds,” are identified with the lo- cations in the distributed program. This enables the programming language to be simultaneously aware of the various hosts involved in a program, their local resources, and their differing perspectives on each other’s code and data. This leads to a clean and general type structure for programs that re- spects locality while permitting high-level language features. To argue that this system is elegant, I present a modal logic formulated for this purpose and then prove its global soundness and completeness and its equivalence to known logics. I then show how a small programming lan- guage can be derived from the logic, and how it can be implemented, prov- ing properties of this abstract compilation procedure. All of these theorems are formalized in Twelf and can be checked by computer. To demonstrate that it is practical, I then extend the modal calculus to a full-fledged programming language based on ML. I implemented a com- piler for this language for the specific case of web applications, a distributed computation involving two hosts with widely different capabilities: the web server and the web browser. I then use the completed implementation to build realistic web applications. iv Contents 1 Introduction 1 1.1 Overview . .1 1.2 Organization . .4 2 Located programming 5 2.1 ConCert and Grid/ML . .5 2.2 Marshaling and location . .8 3 A modal logic for distributed computing 11 3.1 Modal logic . 11 3.1.1 Accessibility . 14 3.1.2 IS5[ ..................................... 15 3.2 Lambda 5 . 17 3.2.1 Action at a distance . 17 3.2.2 Lambda 5 natural deduction . 18 3.2.3 Soundness and completeness . 21 3.2.4 Equivalence to IS5[ ............................ 27 3.3 Dynamic semantics . 28 3.4 C5.......................................... 32 3.4.1 Classical control flow . 32 3.4.2 Classical natural deduction . 33 3.4.3 Classical sequent calculus . 35 3.4.4 Soundness and completeness . 37 3.4.5 Examples . 40 3.4.6 Operational semantics . 42 3.5 Validity . 50 3.5.1 Sequent calculus . 51 3.5.2 Operational semantics . 54 3.5.3 Type safety . 56 3.5.4 Relationship with other connectives . 58 3.6 Summary . 59 v 4 Modal typed compilation 61 4.1 The at modality . 61 4.2 MinML5 external language . 62 4.2.1 Addresses . 62 4.2.2 Syntax and static semantics . 63 4.2.3 Dynamic semantics . 65 4.3 MinML5 internal language . 65 4.3.1 Dynamic semantics . 68 4.4 Elaboration . 71 4.4.1 Elaboration in Twelf . 74 4.4.2 The elaboration relations . 78 4.5 Continuation passing style . 83 4.5.1 Dynamic semantics . 85 4.5.2 Type safety . 87 4.6 CPS conversion . 89 4.6.1 Static correctness . 90 4.6.2 CPS conversion in Twelf . 92 4.7 Closure conversion . 98 4.7.1 Closure conversion in Twelf . 103 4.8 Conclusion . 108 5 ML5 and its implementation 111 5.1 ML5 . 112 5.1.1 Hello, version! . 112 5.1.2 Type and validity inference . 115 5.1.3 Interacting with the environment . 116 5.1.4 ML-like features . 120 5.1.5 Summary . 125 5.2 ML5/pgh . 125 5.2.1 Design concerns . 125 5.3 Front-end . 128 5.3.1 Parsing . 128 5.3.2 The internal language . 130 5.3.3 Elaboration . 137 5.3.4 Optimization . 147 5.4 The CPS language . 147 5.4.1 Return to Oz . 154 5.4.2 CPS conversion . 160 5.4.3 Type-directed translations . 165 5.4.4 Optimizations . 169 5.4.5 Type representation . 170 5.4.6 Closure conversion . 173 5.4.7 Type representation II . 177 5.4.8 Hoisting . 179 vi 5.4.9 Code generation . 182 5.5 Runtime . 190 5.5.1 Server 5 . 190 5.5.2 Communication . 194 5.5.3 Client runtime . 195 5.5.4 Marshaling and unmarshaling . 197 5.6 Summary . 202 6 Applications 205 6.1 Watchkey . 205 6.2 Chat . 207 6.3 Wiki . 211 6.4 Spreadsheet . 214 6.5 Summary . 215 7 Conclusion 217 7.1 Related work . 217 7.1.1 Modal logic in distributed computing . 219 7.1.2 Distributed ML-like languages . 221 7.1.3 Languages for web applications . 222 7.2 Future work . 223 7.2.1 Modal type systems . 224 7.2.2 ML5 and its implementation . 225 7.2.3 Web programming . 229 7.2.4 Conclusion . 230 A Twelf proofs 233 A.1 Equivalence of Lambda 5 natural deduction and sequent calculus . 233 A.2 Equivalence of IS5[ natural deduction and sequent calculus . 241 A.3 Lambda 5 dynamic semantics . 244 A.3.1 The %partial extension . 244 A.3.2 Dynamic semantics . 244 A.4 Soundness and completeness of C5 . 246 A.5 Operational semantics and type safety of C5 . 257 A.5.1 Natural numbers . 258 A.5.2 Operational semantics . ..