Application of Category-Theory Methods to the Design of a System of Modules for a Functional Programming Language
Total Page:16
File Type:pdf, Size:1020Kb
Warsaw University Faculty of Mathematics, Informatics and Mechanics Mikołaj Konarski Application of Category-Theory Methods to the Design of a System of Modules for a Functional Programming Language Supervisor Prof. dr hab. Andrzej Tarlecki Institute of Informatics Warsaw University °c Mikołaj Konarski 2006 Author’s Declaration I declare that I have composed this dissertation myself. Supervisor’s Declaration The dissertation is ready for review. Abstract Module systems embed elements of methodologies, formalisms and tools for programming-in-the-large into programming languages. In our thesis we design a module system, its categorical model and a constructive semantics of the system in the model. The categorical model is simple and general, built using elementary categorical notions, admitting many variants and ex- tensions of the module system. Our module system features programming mechanisms inspired by category theory and enables fine-grained modular programming, as witnessed by the case studies presented. We show how to extend our categorical model with data types express- ible as adjunctions, as well as with (co)inductive constructions and with fully parameterized exponents. We analyze and compare alternative axiom- atizations of the extensions and demonstrate a series of properties concern- ing rewriting of terms denoting morphisms of 2-categories; in particular, the terms expressing general adjunctions. On the basis of the extended model and without introducing recursive types we define the mechanism of mutual dependency among modules, in the form of (co)inductive module construction. The extended module sys- tem contains the essential mechanisms of modular programming, such as type sharing, transparent as well as non-transparent functor application and grouping of related modules. Conventional modular idioms are ex- pressible in new, original ways, enabling modular programming with no headers, no explicit module applications and no global modular errors. ACM Classification Categories and subject descriptors: D.3.3 [Programming Languages]: Lan- guage Constructs and Features—Modules, packages; D.2.2 [Software En- gineering]: Design Tools and Techniques—Modules and interfaces; F.4.1 [Mathematical Logic and Formal Languages]: Mathematical Logic— Lambda calculus and related systems; D.1.1 [Programming Techniques]: Applicative (Functional) Programming. Additional key words and phrases: category theory, adjunctions, inductive types, recursive modules, inductive modules, type sharing. Streszczenie Systemy modułów wbudowują do wewnątrz samych języków programo- wania elementy metodologii, formalizmów i narzędzi, służących programo- waniu w dużej skali. W naszej rozprawie projektujemy taki system modu- łów, jego kategoryjny model i konstruktywną semantykę owego systemu w tym modelu. Nasz kategoryjny model jest prosty i ogólny, zbudowany uży- wając elementarnych pojęć teorii kategorii, dopuszczający wiele wariantów i rozszerzeń systemu modułów. Zaprojektowany system modułów charak- teryzuje się mechanizmami programistycznymi inspirowanymi przez teorię kategorii i umożliwia drobnoziarniste programowanie modularne, jak świad- czą zaprezentowane eksperymenty z jego użyciem. Pokazujemy, jak rozszerzyć nasz kategoryjny model o typy danych wyrażalne jako sprzężenia w 2-kategoriach, jak również o konstrukcje (ko)indukcyjne i w pełni sparametryzowane obiekty wykładnicze. Anali- zujemy i porównujemy alternatywne aksjomatyzacje tych rozszerzeń i po- kazujemy szereg własności przepisywania termów oznaczających morfizmy 2-kategorii; w szczególności termów wyrażających ogólne sprzężenia. Na podstawie rozszerzonego modelu i bez wprowadzania rekurencyjnych typów definiujemy mechanizm wzajemnej zależności pomiędzy modułami w postaci konstrukcji (ko)indukcyjnego modułu. Rozszerzony system modu- łów zawiera zasadnicze mechanizmy modularnego programowania, takie jak dzielenie typów, przezroczystą jak i nieprzezroczystą aplikację funktorów oraz grupowanie powiązanych modułów. Popularne modułowe konstruk- cje są wyrażalne na nowe i oryginalne sposoby, umożliwiając modularne programowanie bez nagłówków, bez aplikacji modułów i bez globalnych modułowych błędów. Contents I Introduction 11 1 Overview 12 1.1 Aim and Results ........................... 12 1.2 Previous work ............................. 17 1.2.1 Module systems ........................ 17 1.2.2 Models of module systems . 19 1.2.3 Categorical models of core language . 19 1.2.4 Term rewriting ........................ 21 1.3 Summary of the thesis ........................ 22 2 Dule tutorial 30 2.1 Core language mechanisms ...................... 30 2.1.1 Identity and types ...................... 32 2.1.2 Basic value operations .................... 34 2.1.3 Functions and derived forms . 45 2.2 Simple modular programming .................... 51 2.2.1 Base modules ......................... 51 2.2.2 Linking ............................ 55 2.3 Advanced modular programming . 59 2.3.1 Specifications ......................... 59 2.3.2 Module operations ...................... 61 2.3.3 Recursive modules ...................... 71 2.4 Conclusion ............................... 77 2.4.1 Absence of standard mechanisms . 78 2.4.2 Additional ways of expressing . 80 2.4.3 New expressible entities ................... 83 3 Preliminaries 86 3.1 Prerequisites .............................. 86 3.2 Formal notation ............................ 87 3.3 Typical identifiers ........................... 94 6 CONTENTS II Semantics of Dule 97 4 Base categorical model 98 4.1 LC — Labeled Cartesian category . 98 4.1.1 Definition ........................... 98 4.1.2 Language ........................... 99 4.2 2-LC — 2-Labeled Cartesian category . 102 4.2.1 Definition based on LC . 102 4.2.2 Language . 103 4.3 2-LC-lc — 2-LC with labeled products . 105 4.3.1 Definition based on 2-LC . 105 4.3.2 Language . 107 4.4 Conclusion ...............................109 5 Semantics of the module system 112 5.1 Simple Category of Modules . 113 5.1.1 Objects and morphisms . 113 5.1.2 Identity and composition . 118 5.1.3 Products ............................121 5.1.4 Equalizers . 123 5.1.5 Conclusion . 130 5.2 W-Dule — module system with specialization . 130 5.2.1 Language . 131 5.2.2 Algebra of modules . 134 5.2.3 Incorporating core language . 137 5.2.4 Cartesian structure . 144 5.2.5 Specialization, instantiation and trimming . 158 5.2.6 Conclusion . 171 5.3 L-Dule — module system with linking . 172 5.3.1 Language . 172 5.3.2 Grouping . 174 5.3.3 Linking ............................177 5.3.4 Conclusion . 180 6 Instances of the base categorical model 182 6.1 p-Core — core language with product type . 183 6.1.1 Syntactic sugar for indexed lists . 183 6.1.2 Rewriting in LC . 185 6.1.3 Equality testing in LC . 189 6.1.4 Rewriting in 2-LC . 193 6.1.5 Rewriting in 2-LC-lc . 199 6.1.6 Conclusion . 203 CONTENTS 7 6.2 s-Core — core language with sum type . 204 6.2.1 Definition based on p-Core . 204 6.2.2 Language . 205 6.2.3 Rewriting . 207 6.2.4 Conclusion . 213 6.3 i-Core — core language with (co)inductive types . 213 6.3.1 Definition based on s-Core . 214 6.3.2 Language . 214 6.3.3 Rewriting . 218 6.3.4 Conclusion . 228 III Variants and extensions 229 7 Variants of the base categorical model 230 7.1 Adjunctions ..............................230 7.1.1 2-LC-A — 2-LC with adjunctions . 230 7.1.2 2-LC-F — 2-LC-A by factorizers . 235 7.1.3 Example adjunctions . 237 7.1.4 Reduction in 2-LC-F . 244 7.1.5 Conclusion . 250 7.2 Opposite categories . 250 7.2.1 2-LCO — 2-LC with opposite categories . 251 7.2.2 Why exponent is contravariant . 254 7.2.3 Attempt to accommodate parameterized adjoints . 256 7.2.4 2-LCX — 2-LCO with self-dualities . 257 7.2.5 Conclusion . 270 7.3 Parameterized adjunctions . 271 7.3.1 2-LCO-A — 2-LCO with opposite adjunctions . 271 7.3.2 2-LCX-A — 2-LCX with adjunctions . 274 7.3.3 2-LCX-F — 2-LCX-A by factorizers . 287 7.3.4 Reduction in 2-LCX-F . 291 7.3.5 Conclusion . 297 8 Extensions to the core language 298 8.1 e-Core — core language with parameterized exponent . 298 8.1.1 Definition based on i-Core and 2-LCX-F . 298 8.1.2 Language . 299 8.1.3 Construction using 2-LCX-F . 300 8.1.4 Moving back to 2-LC-lc . 306 8.1.5 Conclusion . 308 8.2 f-Core — core language with general recursion . 309 8 CONTENTS 8.2.1 Language . 309 8.2.2 Rewriting . 310 8.2.3 Conclusion . 313 8.3 Extended notation for the core language . 315 8.3.1 Bare core language . 315 8.3.2 User core language . 331 8.3.3 Conclusion . 335 9 Extensions to the module language 336 9.1 I-Dule — module system with inductive modules . 336 9.1.1 Language . 337 9.1.2 Simple inductive modules . 339 9.1.3 Conclusion . 348 9.2 Extended notation for modules . 349 9.2.1 Bare module language . 349 9.2.2 Completeness of module typing . 361 9.2.3 Environment-dependent module language . 367 9.2.4 User module language . 374 9.2.5 Conclusion . 377 IV Conclusion 379 10 Final remarks 380 10.1 Theory .................................380 10.2 Pragmatics ..............................382 10.3 Implementation ............................384 10.4 Type reconstruction algorithms . 385 V Appendices 387 A Language summaries 388 A.1 Internal core language . 388 A.1.1 Syntax .............................388 A.1.2 Semantics . 391 A.1.3 Typing rules . 396 A.1.4 Reduction . 400 A.1.5 Equality testing . 404 A.2 Internal module language . 406 A.2.1 Syntax .............................406 A.2.2 Semantics . 407 CONTENTS 9 A.2.3 Typing rules . 409 A.2.4 Simulation of multiplication . 411 A.2.5 Specification stripping . 413 A.3 Syntax of the full user language . 414 B Longer proofs from Chapter 6 and Chapter 7 417 B.1 Proofs for Chapter 6 . 417 B.2 Proofs for Chapter 7 . 433 C Program examples in Dule 442 C.1 Standard prelude . 442 C.1.1 Module of natural numbers . 443 C.1.2 Module of character strings . 445 C.2 Dule compiler written in Dule . 449 C.2.1 Assigning categories to functors .