Arxiv:2010.05167V1 [Cs.PL] 11 Oct 2020
Total Page:16
File Type:pdf, Size:1020Kb
A Categorical Programming Language Tatsuya Hagino arXiv:2010.05167v1 [cs.PL] 11 Oct 2020 Doctor of Philosophy University of Edinburgh 1987 Author’s current address: Tatsuya Hagino Faculty of Environment and Information Studies Keio University Endoh 5322, Fujisawa city, Kanagawa Japan 252-0882 E-mail: [email protected] Abstract A theory of data types and a programming language based on category theory are presented. Data types play a crucial role in programming. They enable us to write programs easily and elegantly. Various programming languages have been developed, each of which may use different kinds of data types. Therefore, it becomes important to organize data types systematically so that we can understand the relationship between one data type and another and investigate future directions which lead us to discover exciting new data types. There have been several approaches to systematically organize data types: alge- braic specification methods using algebras, domain theory using complete par- tially ordered sets and type theory using the connection between logics and data types. Here, we use category theory. Category theory has proved to be remark- ably good at revealing the nature of mathematical objects, and we use it to understand the true nature of data types in programming. We organize data types under a new categorical notion of F,G-dialgebras which is an extension of the notion of adjunctions as well as that of T -algebras. T - algebras are also used in domain theory, but while domain theory needs some primitive data types, like products, to start with, we do not need any. Products, coproducts and exponentiations (i.e. function spaces) are defined exactly like in category theory using adjunctions. F,G-dialgebras also enable us to define the natural number object, the object for finite lists and other familiar data types in programming. Furthermore, their symmetry allows us to have the dual of the natural number object and the object for infinite lists (or lazy lists). We also introduce a functional programming language in a categorical style. It has no primitive data types nor primitive control structures. Data types are declared using F,G-dialgebras and each data type is associated with its own control structure. For example, natural numbers are associated with primitive recursion. We define the meaning of the language operationally by giving a set of reduction rules. We also prove that any computation in this programming language terminates using Tait’s computability method. ii A specification language to describe categories is also included. It is used to give a formal semantics to F,G-dialgebras as well as to give a basis to the categorical programming language we introduce. iii Acknowledgements The greatest thanks go to Professor Rod Burstall who, first of all, accepted me as a Ph. D. student, then, supervised me during my Ph. D. study and, further, gave me an opportunity to continue working in Edinburgh. He gave me not only useful advices but also much-needed mental support. I would also thank to his wife, Sissi Burstall, who invited me to their house often and made my stay in Edinburgh very pleasant. I am also grateful to Professor Reiji Nakajima for having encouraged me to come to Edinburgh and to Professor Heisuke Hironaka for having helped me to solve the financial problem for studying in Edinburgh. Many lectures I attended in the first year enlightened me a lot: domain theory, operational semantics, denotational semantics, algebraic specification, category theory, and so on. I did not know what category theory really is before I came to Edinburgh. Thanks to Andrzej Tarlecki, Edmund Robinson and John Cartmell for having helped me to overcome the initial difficulty of category theory. John Cartmell also helped me a lot through discussions in the early stage of the thesis. I am also in debt to Furio Honsell who introduced me to Tait’s method for proving normalization theorems of lambda calculi when I stuck in the normalization proof of CPL. Many other people in Edinburgh helped me with their comments and through discussions. I would especially like to thank Bob McKay and Paul Taylor for reading the early drafts and discovering some disastrous mistakes. This thesis is written using LATEX on a Sun workstation and printed by an Apple LaserWriter. I would like to thank George Cleland and Hugh Stabler for provid- ing wonderful computing facilities and software to Laboratory of Foundation of Computer Science. My Ph. D. study at the University of Edinburgh has been funded by the Educa- tional Project for Japanese Mathematical Scientists, Harvard University, by the Overseas Research Students Award and by the Science and Engineering Research Council Research Fellowship. iv Contents 1 Introduction 1 1.1 Backgrounds.............................. 2 1.1.1 Algebraic Specification Methods . 2 1.1.2 DomainTheory........................ 5 1.2 BasicCategoryTheory ........................ 7 1.3 DevelopmentofCategoricalDataTypes . 8 1.4 InThisThesis............................. 16 1.5 ComparisonwithOtherWorks. 17 2 Categorical Specification Language 19 2.1 AFunctorialCalculus......................... 21 2.2 Signatures of Categorical Specification Language . 30 2.3 Structures of Categorical Specification Language . 35 2.4 Functorial Calculus (revisit) . 37 2.5 Sentences and Satisfaction Relation of Categorical Specification Language ............................... 43 2.6 FreeCategories ............................ 45 3 Categorical Data Types 49 3.1 WhatareCategoricalDataTypes? . 49 3.2 DataType Declarations in Categorical DataTypes . 59 v 3.3 ExamplesofCategoricalDataTypes . 62 3.3.1 Terminal and Initial Objects . 63 3.3.2 ProductsandCoProducts . 64 3.3.3 Exponentials ......................... 65 3.3.4 NaturalNumberObject . 67 3.3.5 Lists.............................. 70 3.3.6 Final Co-Algebras (Infinite Lists and Co-Natural Number Object) ............................ 71 3.3.7 Automata ........................... 76 3.3.8 ObscureCategoricalDataTypes . 78 3.4 SemanticsofCategoricalDataTypes . 79 3.5 Existence of Left and Right ..................... 83 4 Computation and Categorical Data Types 88 4.1 Reduction Rules for Categorical Programming Language . 89 4.2 AnExampleofusingReductionRules . 103 4.3 Well-Definedness and Normalization Theorem for Reduction Rules 108 4.4 PropertiesofComputableObjects. 122 4.5 Reduction Rules for Full Evaluation . 127 5 Application of Categorical Data Types 130 5.1 An implementation of Categorical Programming Language . 130 5.2 TypedLambdaCalculus . 136 5.3 MLandCategoricalProgrammingLanguage . 141 Conclusions 145 Bibliography 149 vi Chapter 1 Introduction This is an exploration of data types through category theory. It is an attempt to achieve better understanding of data types, their uniform classification, and discovery of a new world of data types. Data types have been with us since the very first programming languages. Even some machine languages now have some concept of data types, but early programming languages had only a fixed number of data types, like integers, reals and strings, and/or a fixed number of data type constructors, like array constructors and record constructors. When we gradually realized how important data types were, programming languages started having richer and richer data types. A number of programming languages now allow us to define our own data types. Some might even say that the richer they are, the better the programming languages are. Programming languages can be classified by the way how they handle data types. There is no question about the importance of data types. Much research in this area has produced various kinds of data types, so varied that one cannot capture them all. We now need to systematically organize data types. We want to know the connection between one data type and another. We want to know the reason why those data types are with us and while some other data types are not. After getting a clear view of data types, we might find the future direction to discover other important data types. There have been already some attempts to organize data types. We can name some of the important ones: Domain theory is one, algebraic specification is an- other and type theory is one where a lot of research is going on at the moment. In this thesis, we will present yet another attempt to organize data types. We do so by using category theory. We call our data types Categorical Data Types (or CDT for short). One might ask “Why category theory?” Category theory is known as highly 1 CHAPTER 1. INTRODUCTION 2 abstract mathematics. Some call it abstract nonsense. It chases abstract arrows and diagrams, proves nothing but about those arrows and diagrams, rarely talks about what arrows are for and often concepts go beyond one’s imagination. How- ever, when this ‘abstract nonsense’ works, it is like magic. One may discover a simple theorem actually means very deep things and some concepts beautifully unify and connect things which are unrelated before. In ordinary mathematics, whether we are aware or not, we are in the world of set theory. Mathematics has been so well developed with set theory that we can hardly do anything without it. Therefore, it is very natural that semantics of programming languages is generally based on set theory. Note that it is often said that most of programming languages do not have set theoretic semantics and, therefore, domain theory has been developed, but this does not contradict with “semantics based on set theory”, because domain theory itself is based on set theory. A domain is a set with certain properties. Set theory is a powerful tool, but sometimes this power disfigures beautiful objects so that we cannot directly see their natural properties. For example, in set theory it is not easy to see either the duality between injective and surjective functions or the duality between cartesian products and disjoint sums. It is in category theory that these dualities come out clearly. Category theory concentrates on the outer behaviour of objects. It does not care what is in an object, whereas set theory is all about what is in an object.