Nothing Special   »   [go: up one dir, main page]

Academia.eduAcademia.edu

A generic theory of datatypes

1997

A generic theory of datatypes Citation for published version (APA): Hoogendijk, P. F. (1997). A generic theory of datatypes. Technische Universiteit Eindhoven. https://doi.org/10.6100/IR496548 DOI: 10.6100/IR496548 Document status and date: Published: 01/01/1997 Document Version: Publisher’s PDF, also known as Version of Record (includes final page, issue and volume numbers) Please check the document version of this publication: • A submitted manuscript is the version of the article upon submission and before peer-review. There can be important differences between the submitted version and the official published version of record. People interested in the research are advised to contact the author for the final version of the publication, or visit the DOI to the publisher's website. • The final author version and the galley proof are versions of the publication after peer review. • The final published version features the final layout of the paper including the volume, issue and page numbers. Link to publication General rights Copyright and moral rights for the publications made accessible in the public portal are retained by the authors and/or other copyright owners and it is a condition of accessing publications that users recognise and abide by the legal requirements associated with these rights. • Users may download and print one copy of any publication from the public portal for the purpose of private study or research. • You may not further distribute the material or use it for any profit-making activity or commercial gain • You may freely distribute the URL identifying the publication in the public portal. If the publication is distributed under the terms of Article 25fa of the Dutch Copyright Act, indicated by the “Taverne” license above, please follow below link for the End User Agreement: www.tue.nl/taverne Take down policy If you believe that this document breaches copyright please contact us at: openaccess@tue.nl providing details and we will investigate your claim. Download date: 10. Dec. 2021 A Generic Theory of Datatypes CIP-DATA LIBRARY TECHNISCHE UNIVERSlTEIT EINDHOVEN Hoogendijk, Paul Ferenc A generic theory of datatypes / Paul Ferenc Hoogendijk Eindhoven: Technische Universiteit Eindhoven, 1997. - 174 p. Proefschrift. - ISBN 90-386-0521-8 NUGI852 Trefw.: transformationeel prograrnmeren / categorieentheorie CR Subject Classification (1991): D.l.m, D.3.3 druk: PrintPartners Ipskamp B.V, Enschede. © 1997 by P.E Hoogendijk, Eindhoven, The Netherlands All rights reserved. No part ofthis publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without prior permission of the author. -----, NWO These investigations were supported by the Netherlands Computer Science Research Foundation (SION) with financial supportfrom the Netherlands Organization for Scientific Research (NWO). They have been carried out under the auspices ofthe research school IPA (Institute for Programming research and Algorithmics). A Generic Theory of Datatypes PROEFSCHRIFT ter verkrijging van de graad van doctor aan de Technische UniversiteitEindhoven, op gezag van de Rector Magnificus, prof.dr. M. Rem, voor een commissie aangewezen door het College van Dekanen in het openbaar te verdedigen op dinsdag 24 juni 1997 om 16.00 um door Paul Ferenc Hoogendijk geboren te Eindhoven Dit proefschrift is goedgekeurd door de promotoren: prof.dr. R. C. Backhouse en prof. R. S. Bird Acknowledgements Many people supported me in preparing this thesis, and their help is gratefully acknowledged. I would like to mention: My Ph.D. supervisor Roland Backhouse, first, for accepting my as an "afstudeer"-student and later on offering me the possibility of doing research in the challenging area of Mathematics of Program Construction. I want to thank him for his guidance, his patience and the effort he put in improving preliminary versions of this thesis. lowe him a lot. Oege de Moor, for his pleasant cooperation on the field of membership and fans. Although we communicated almost exclusively via email it was as if we were sharing an office and working side-by-side. I enjoyed it a lot. My fellow Ph.D. student Henk Doornbos for many fruitful discussions and his willingness to serve as a "klank-bord". The other members and former members of the Friday morning club: Chritiene Aarts, Eerke Boiten, Joop van den Eijnde, Netty van Gasteren, Rik van Geldrop, Frans Rietman, Ed Voermans, and Jaap van der Woude. The external members of the Ph.D. committee: Richard Bird, Wim Hesselink, and Lambert Meertens, for reviewing this thesis and giving a lot of detailed and useful comments. Richard Verhoeven for providing assistance with MathSpad and I5f}3X. My family and friends, for their love and friendship. Contents 1 2 1 Aims and Motivation 1.1 Generic programming 1.2 Theorems for free .. 1.2.1 Polymorphism 1.2.2 Polytypy... 1.2.3 Parametric polymorphism 1.2.4 Parametric polytypy 1.3 Our contribution . . . 1.4 Overview of the thesi s 1 2 2 2 3 4 4 5 Basic Notions 2.1 Categories . . . . . . . . . . . . . 2.1.1 Isomorphisms....... 2.1 .2 Terminal and initial objects 2.2 Datatypes and functors . . . . . 2.2.1 Functors........ 2.2.2 Natural transformations 2.2.3 Category Fun . . . . . 2.2.4 Product and coproduct 2.2.5 Recursive datatypes . 2.3 Theorems for free . 2.3.1 Higher-order naturality . 2.4 Allegories . . . . . . . . . 2.4.1 Galois connections. 2.4.2 Definition . . . . . 2.4.3 Partial identities . . 2.4.4 Simple and total relations 2.4.5 Tabular allegories 2.4.6 Unit . 2.4.7 Range and domain 2.4.8 Power allegories . 2.4.9 Locally complete allegories 2.4.10 Division . . . . . . 2.4.11 Extensionality . . . 2.4.12 Operator precedence iii 7 7 8 8 10 10 11 12 13 16 19 20 21 21 23 24 25 27 27 28 30 31 31 33 33 iv CONTENTS 2.5 3 4 5 Relational extensions. . . . . . 2.5.1 Relators . 2.5.2 Coproduct and product. 2.5.3 Natural transformations 2.5.4 Power relator . . . . 2.5.5 Recursive datatypes 34 35 37 44 47 47 Calculational Preliminaries 3.1 Category "Slok" . 3.1.1 Typing rules . 3.1.2 Uniqueness and naturality 3.1.3 Category CoSlok . . 3.2 The '"[-<'1 calculus 3.2.1 Arbitrary products of a category 3.2.2 Matrix transposition . 3.2.3 Definition of matrix transposition 3.3 Extending to allegories. . 3.3.1 Regular datatypes 51 51 53 54 55 56 57 Membership 4.1 Definition of membership . . . . . . . . . . . . 4.1.1 Uniqueness of membership (endorelators) 4.1.2 Membership for non-endo relators . . . . 4.1.3 Uniqueness of membership (arbitrary relators) . 4.2 Membership for regular relators . . 4.2.1 Membership of projections. . . . 4.2.2 Membership of composition . . . 4.2.3 Membership of the power relator. 4.3 Membership of tree types 4.4 A Counter-example 4.5 Fans . 4.5.1 Fans of non-endo relators 4.5.2 Uniqueness of fan (non-endo) relator 4.5.3 Fans of regular relators. 4.5.4 Recoverable relators 65 66 A Class of Commuting Relators 5.1 Introduction . 5.2 The requirement (endorelators) . 5.2.1 Naturality requirements 5.2.2 Monoid homomorphism 5.2.3 Half zips and commuting relators 5.3 Analysis of the requirement . . . . 5.3.1 Higher-order parametricity . 5.3.2 Shape preservation . . . . . 61 61 63 64 67 69 73 75 75 76 78 81 83 83 86 87 87 89 91 91 92 93 95 96 96 97 98 CONTENTS 5.4 5.5 5.6 5.7 5.8 5.9 5.3.3 Commuting relators .. 5.3.4 Strength . 5.3.5 Structure multiplication The requirement (arbitrary relators) 5.4.1 Multi-valued relators .. The zip-a-dee-doo-dah theorem .. Constructing candidate zips . . . . 5.6.1 Zipping identity, projections, coproduct and product. 5.6.2 Zipping constants .. 5.6.3 Zipping compositions 5.6.4 Zipping tuples .. 5.6.5 Zipping tree types 5.6.6 Summary Vetting the candidates .. 5.7.1 Zips are natural transformations 5.7.2 Zips are higher-order natural. 5.7.3 Zips are compositional. 5.7.4 Zips respect projections Regular relators are commuting Broadcasts . . . . . . . . v 100 101 105 106 109 112 114 115 117 117 118 118 119 119 120 120 121 124 124 125 6 Strength, Copies map and Fan 6.1 Zips . 6.2 Constructing copies map, fan and strength 6.3 One-to-one correspondence . 6.4 Uniqueness of strength and copies map . 6.5 Shapely functors . . . . . . . . . . . . . 6.6 Coherence versus largest natural transformations. 6.7 Conclusion . 129 7 145 Epilogue 130 130 135 137 140 141 143 A Typing rules Al The '['-,1 calculus A2 Membership and fans A3 Zips . 149 Bibliography 151 Index 154 Samenvatting 158 Curriculum Vitae 161 149 149 150 Chapter 1 Aims and Motivation 1.1 Generic programming The attribute "generic" -which means according to a dictionary definition "characteristic of a genus or class; applied to (any individual of) a large class; general, not specific or special"is without doubt a sine qua non for computer software. The computer is, after all, a generalpurpose device and its most successful applications are to software systems which because of their broad applicability can capture a huge market but which can be customized to the needs of specific clients. Examples of such systems are workflow management systems, logistic systems and systems for financial administration. Indeed, Simonyi [42] has argued that the cycle of abstraction and customization is the key to success in the computing market. The macro economics of big business is of course not the concern of this thesis. Our concern is at the micro level of programming. But even at this level the ability to write so-called "generic" code capturing commonly occurring patterns is vital to reusability and thus to programmer productivity. Many programming languages, or programming methodologies, claim to support some kind of genericity. Examples are overloading, the generic class of ADA, polymorphism in functional languages, and inheritance in object oriented programming. Reusability is not the only reason why generic programs are important. Another is the conceptual unification that they offer. As remarked by Meertens [35] "Which is more exciting: to find yet another algorithm, or to discover that two familiar algorithms are instances of one more abstract algorithm? It is the latter that sparks new insights and opens the way for finding further connections, that makes it possible to organize and systematize our knowledge and eventually set as routine exercises problems that once were feats of scientific discovery". Indeed, there are many additional reasons. Two that we regard as particularly important are the increased concision and precision afforded by a higher degree of abstraction, and the reduction of the burden of proof gained by eliminating the need to repeat essentially the same argument for many special cases. Genericity is generally accepted to be important; on the other hand it is extremely difficult to be objective about measures of genericity. Like many buzz words the term "generic" can be applied with impunity to almost everything. The goal of this thesis is to contribute to the development of a mathematical theory of generic programming in which verifiable criteria are given for the notion of genericity. 2 1.2 CHAPTER 1. AIMS AND MOTIVATION Theorems for free The viability of a generic theory of datatypes was made plausible by the pioneering work of Plotkin [41] and Reynolds [43] on the semantics of polymorphism, the importance of which was highlighted by Wadler [52] in a paper entitled "Theorems for free". Since "theorems for free" was the catalyst for our own work we use this section to briefly summarise developments in functional programming both preceding and up to the writing of this thesis. In the next section we present an overview of the thesis. We begin with a discussion of polymorphism in functional programming. 1.2.1 Polymorphism Of the instances of genericity in current programming languages/paradigms mentioned above, one which has a relatively well-developed theoretical basis is the notion of parametric polymorphism first introduced by Strachey [48] and later incorporated in the language ML by Milner [37, 38] (and since then a more or less standard feature of all functional programming languages). The use of parametric polymorphism is one of the major success stories of functional programming since it eliminates the compulsion in languages like Pascal to provide irrelevant type information. For example, it is irrelevant to the computation of the length of a list whether the elements of list are integers, characters, or whatever. In Pascal this information must be supplied, thus enforcing the programmer to write essentially the same code each time a length function is required for a new element type. 1.2.2 Polytypy It has long been observed that data structure influences program structure. For this reason the study of datatypes is an important issue. Bird has developed the theory of lists [8], a set of algebraic transformation rules involving operators like map, reduce, filter etc. Meertens calls this kind of program methodology "algorithmics". [34]. The theory of lists is polymorphic. For instance the map operator is defined irrespective of the type of its argument, i.e. the type of the function that is being mapped over a list. However, the theory was restricted only to lists and thus not applicable to arbitrary datatypes. The theory of lists has been extended by Malcolm [31, 32, 33] to arbitrary datatypes. For instance, he formulated a theorem expressing when two computations could be fused into one computation. Malcolm's fusion theorem was "polytypic" - that is the name given to it nowadays, Malcolm did not use this term - in that it was parameterized by a datatype constructor and so could be instantiated in a variety of ways. So, "polytypic" programs distinguish themselves from polymorphic programs in that the parameter is a datatype constructor like "list" or "tree" - a function from types to types - rather than a type like "boolean", "integer" or "list of integers". One could say that Malcolm developed a "theory of F's" with F being a parameter standing for an arbitrary datatype constructor, as a generalisation of the theory of lists. Malcolm exploited the - polytypic - notion of a "catamorphism" and introduced the "banana bracket" notation which was popularised and extended to the - polytypic - notions of "anamorphism" and "hylomorphism" by Fokkinga, Meijer and Paterson [36]. Since then the theme of polytypy has been explored in a variety ways. Several authors [6, 27,35] have 1.2. THEOREMS FOR FREE 3 explored polytypic generalisations of existing programming problems, Doornbos [12, 13, 14] has developed a polytypic theory of program termination and the recently published book by Bird and De Moor [7] contains a wealth of material in which parameterisation by a datatype constructor plays a central role. Functional programmers have a good intuitive understanding of what it means for a function to be polymorphic. Being able to experiment with the notion by writing and executing polymorphic programs is clearly enormously beneficial to understanding. Nevertheless, an unequivocal formal semantics of "parametric polymorphism" is still an active area of research [15]. The situation with regard to polytypy is worse, the emphasis at the current time being on demonstrating the practicality of the notion [27, 22] with the potential danger of unnecessary complication. Parametric polymorphism is a well-defined concept that provides the basis for a well-defined theory of parametric polytypy. 1.2.3 Parametric polymorphism A polymorphic function is parametric if its behaviour does not depend on the type at which it is instantiated [48]. Reynolds [43] showed for a specific language that any parametrically polymorphic function instantiated at different types behaves in "related" ways. A polymorphic function satisfies a certain (di)naturality property that is derivable from the type of the function. For instance, for the polymorphic reverse function rev on lists it follows that, for f: At- B, map(f) . revB = reVA . map(f) where map (f) denotes the function on lists which maps function f to each element of the list. Wadler [52] calls such a property a "theorem for free"; a property derived for a function by examining its type alone. Note that Reynolds' abstraction theorem is a property of a particular language. For instance, it predicts the following property for a parametrically polymorphic equality function =A: Baal t- A x A. For all functions f, x=y f(x) = fry) . In other words, if Reynolds' theorem holds for a given language then all functions are injective in that language or no polymorphic equality function can exist. Indeed, for some languages it is possible to define a polymorphic equality function using ad hoc polymorphism, i.e. including a clause for every type. Such a polymorphic equality function is then not truly polymorphic, i.e. not parametrically polymorphic. So, although Reynolds' abstraction theorem does not hold for all languages, the notion of parametric polymorphism provides us with a verifiable way of checking whether a function is truly parametrically polymorphic. In other words, we consider a function to be truly parametrically polymorphic if the "free theorem", i.e. the (di)naturality property, predicted by its type holds. 4 1.2.4 CHAPTER 1. AIMS AND MOTIVATION Parametric polytypy Recall that a polytypical function is a parameterised function in which the parameter is a datatype constructor. So, a logical question is: what does a "theorem for free" look like on this higher-level? And is the work of Reynolds applicable on this level? The answer to the second question is negative since we want to consider languages in which it is possible to define higher-order parametric functions inductively over the class of type constructors. The answer to the first question is that in most cases - at least the cases considered in this thesis - we can construct a property which one could call the "free" theorem of a polytypical function. Such a property is a higher-order naturality property expressing that a polytypical function instantiated at different type constructors behaves in related ways. So, although we can not assume Reynolds' abstraction theorem on a higher-level, Reynolds' ideas, suitably generalized, provide us a verifiable way of checking whether a function is truly higher-order parametric or not. In other words, we consider a function to be truly polytypic if the higher-order "free theorem" i.e. a higher-order naturality property, predicted by its type holds. The work of Jeuring and Jansson [27, 21, 22] differs in this respect from our approach since they consider a polytypic function to be a function which is defined by induction on the structure of the class of type constructors. So, although such a function has a type constructor as a parameter, they do not require - at least not explicitly - that the definition of a polytypic function for each of the different clauses behaves in related ways. In other words, one could argue that Jeuring and Jansson's definition of polytypy is "ad hoc" polytypy. Just like ad hoc polymorphism, the behaviour of a polytypic function instantiated at different types may not be related at all. In other words, it is not necessarily the case that the functions which Jeuring and Jansson define are truly higher-order parametric. Also related to our work, is the theory of shape developed by Jay [24, 26, 25]. In the theory of shape, a data stlUcture is separated into its shape and its contents. 1.3 Our contribution In this thesis we try to formulate higher-order parametric characterizations of generic notions and programs instead of giving inductive definitions every time. The main criterion we use to check if a function is truly polytypic is whether the "free theorem" predicted by type considerations holds. An advantage of giving a generic characterization instead of an inductive definition is that if one wants to calculate with these notions and/or programs one can use the characterization directly instead of being forced to give cumbersome proofs inductively over the structure of the class of type constructors every time. However, for the final implementation of a higher-order parametric function we do need an inductive definition of the function on the class of type constructors. Using the polytypic characterization of a program, we can derive a class of programs which is defined by induction on the class of type constructors. For instance, in chapter 5 the notion of higher-order naturality plays a central role in the construction of the polytypic function that commutes two datatypes. We give a non-inductive characterisation of this polytypic function by requiring the higher-order naturality property, i.e. its "free theorem", which we derive from type considerations. 1.4. OVERVIEW OF THE THESIS 5 Normally, the type of a polymorphic function does not imply uniqueness. Many different polymorphic functions of the same type can exist. However, in the case of polytypic functions we get some uniqueness results by investigating the higher-order naturality requirement. These uniqueness results we use for the construction of the polytypic function that commutes two datatypes. 1.4 Overview of the thesis Chapter 4 reports on joint work with Oege de Moor [20]. We introduce the notion of a membership test for a datatype. We give a non-inductive characterisation of a membership test for an arbitrary datatype and show that it is a "good" characterisation in the sense that for a datatype there is at most one membership test. A new result, compared with [20], is that we give a more general definition for membership than given in [20]; we include the so-called non-endo datatypes as well. For this generalisation, we use the 'T-Li calculus as introduced in the second half of chapter 3. After that, we give the inductive definition of a membership test for the inductively defined class of the so-called regular datatypes. In the second half of chapter 4, we introduce the notion of a so-called fan for a datatype. Again, we give a non-recursive characterisation of a fan for all datatypes. The existence of membership implies the existence of a unique fan. One can view a fan as the counterpart of membership: with a fan, one can construct a data structure. We give an inductive definition of the fans of the regular datatypes. In chapter 5 we illustrate the use of the generic notions introduced so far to tackle the problem of "commuting datatypes". We formulate a characterisation of when two datatypes commute. This is done by giving a characterisation of a so-called zip operator which commutes the two datatypes. This characterisation is generic in the sense that the two datatypes are parameters of the program. As an example of the use of the theory of commuting datatypes, we show that a special class of zips, the so-called broadcast operations, are strengths. Furthermore, we tackle the problem of structure multiplication. After that, we give an inductive definition for the zip operations for the class of regular datatypes. In chapter 6 we continue the investigation begun by Oege de Moor [20] of the relationship between fans and so-called strengths and copies maps of a datatype. Furthermore, we investigate the "free theorem" of the membership test and fans. The formal setting in which we conduct our research is introduced in chapter 2. The setup of the theory is based on the book of Bird and de Moor [7] although there are differences in detail which we thought were necessary. Readers familiar with that book may skip chapter 2 on first reading except for subsection 2.2.3 and section 2.3. Furthermore, there are some notational differences for the split (fork) and june (case). Chapter 2 is divided into two parts. First, in order to make this thesis as self-contained as possible, we introduce those elements of category theory that are needed. The category of total functions between sets will be the leading example with which we motivate the concepts we introduce. The most important notion is that of a functor. We argue that functors model datatypes. We define the functors corresponding with product (pairing), coproduct (disjoint union) and recursive datatypes. Connected with 6 CHAPTER 1. AIMSANDMOTIVATION functors is the notion of a natural transformation. A natural transformation is a datatype transformer: it takes a datatype and transforms it into another. The term "natural" reflects the fact that such a transformation may not alter the value of the elements that the datatype carries, it is only a transformation of the structure itself. In the second part of chapter 2, we introduce the notion of a so-called allegory: a point-free axiomatization of the structure of binary relations between sets. The functions, i.e. total and single-valued relations, form a sub-category of an allegory. We extend the notions introduced in the first half to relations. A relational extension of a categorical concept is something which is defined on all relations but if one takes the restriction to functions, one gets the original categorical concept for the sub-category of total functions. The relational extension of a functor is a so-called relator. Not all functors have a relational extension. However, we regard having a relational extension as a healthiness condition on a functor modelling a datatype. In other words, not all functors correspond to a datatype. Chapter 3 is divided into two parts. In the first part we define the so-called "slok" category. This category plays an important role in chapter 5 on commuting relators. In the second half, we introduce the T-6. calculus. The '[-6. calculus is used in the remainder ofthe thesis in order to reason about non-endo relators. A non-endo relator takes a vector of arguments and/or gives as result a vector of arguments. In appendix A we have included a list of the typing rules for the '[-6. calculus, membership, fans, and zips. Chapter 2 Basic Notions The fonnal setting in which we conduct our research is introduced in in this chapter. The setup of the theory is based on the book by Bird and De Moor [7] although there are differences in detail which we thought were necessary. Readers familiar with that book may skip this chapter on first reading except for subsection 2.2.3 and section 2.3. Furthennore, there are some notational differences for the split (fork) and junc (case). In order to make this thesis as self-contained as possible, we introduce in section 2.1 those elements of category theory that are needed. In section 2.4 we introduce the notion of an allegory: a point-free axiomatization of the structure of binary relations between sets. The functions, i.e. total and single-valued relations, fonn a sub-category of an allegory. We extend the notions introduced in section 2.1 to relations. A relational extension of a categorical concept is something which is defined on all relations but if one takes the restriction to the total functions, one gets the original categorical concept defined for the sub-category of total functions. 2.1 Categories A category consists of a class of objects and a class of arrows. Every arrow l' comes equipped with two unique objects, its so-called source and target, denoted by 1't> and 1'<1, respectively. We write l' : A (- B if 1't> = B and 1'<1 = A. For every pair of arrows l' : A (- Band 9 : B (- C with matching target and source, their composition exists: l' . 9 : A (- C. We require composition to be associative, that is, for l' : A (- B, 9 : B (- C and h : C (- D, (1'·g)·h=1'·(g·h) . Furthennore, for every object A there exists a so-called identity arrow idA which is the unit of composition. That is to say, for each l' : A (- B, idA·1' = l' = l' . ids . Functions and relations as categories Two important examples of categories we encounter in this thesis are the category Map, the category of total functions between sets, and category Rei, the category of binary relations between sets. 7 8 CHAPTER 2. BASIC NOTIONS In category Map, the objects are sets and an arrow is a triple (A, f , B) of two sets A and B, target and source of the arrow, and a function f. For such a triple we require that set A contains the range of function f and set B is the domain of f. The identity arrow on A is the triple (A, idA, A) where idA is the identity function on the set A. The composition of (A, f , B) and (B , 9 , C) is (A, f . 9 , C) where f . 9 is the usual composition of functions: (f· g)x = f( 9 (x)). In category Rei, the objects are again sets, and an arrow is a triple of three sets (A, R, B) where R is a subset of the cartesian product A x B. So R is a set of pairs. We write x R y if (x, y) E R. The relation idA is the identity arrow on A, i.e. x idA x iff x E A. The composition of (A, R, B) and (B ,S , C) is (A, R· S ,C) where R· S is the usual composition of binary relations i.e. a (R· S) c iff 3(b:: a R b;\ b S c). In the next section we introduce some additional structures on categories. Although many kinds of categories exist, the category Map will be the leading example with which we motivate the concepts we introduce. Note that although binary relations between sets are indeed a category much more can be said about binary relations. In section 2.4 we introduce so-called allegories. By definition, an allegory is a category with some additional axioms to capture some of the structure of binary relations over sets. We will give the relational extension of some of the categorical notions defined on functions to relations. Again, category Rei will be the leading example with which we motivate the definition of the relational extensions. 2.1.1 Isomorphisms A special kind of arrow is a so-called isomorphism. Arrow f : A there exists an arrow 9 : B f- A such that 9 .f = idB ;\ f· 9 = f- B is an isomorphism if (2.1) idA . If such a 9 exists then it is necessarily unique. For, assume that h· f Then = idB and f . h = idA. 9 { f· h = idA } g·f = idB } 9 ·f· h h In Map, the function 9 is the inverse of f. Hence, in Map the function f is an isomorphism precisely when f is a bijection. 2.1.2 Terminal and initial objects A special kind of object is a so-called terminal object. An object T is terminal if for every object A there exists precisely one arrow of type T f- A. In Map, the singleton sets are terminal. For singleton set {x}, the only function of type {x} f- A is the constant function mapping every element of A to x. 2.1. CATEGORIES 9 Assume the existence of terminal objects. Let 1 denote some fixed terminal object and let !A denote the unique arrow of type 1 f- A. Then terminality of object 1 can equally defined by the following universal property, f =!A == f: 1 f- A . The dual of terminality is initiality. Object I is initial if for every object A there is precisely one arrow of type A f- 1. Let iA denote this unique arrow. Then we have the following universal property, f = iA == f: A f- I . The empty set is the only initial object in Map. Duality Many categorical concepts we encounter have a dual counter-part. We have just seen an example. The dual of terminality is initiality. For category C, we can define its so-called opposite category Cop by turning the arrows around. Formally, the objects of Cop are the objects of C, and f : B f - A is an arrow of Cop if f : A f- B is an arrow of C. Furthermore, the order of composition is swapped, i.e. fog ~ 9 . f is the composition of CoP. So, if we have a categorical concept for a category then this categorical concept defined for category Cop is the dual concept for category C. The dualized concept is derived from the original one by turning the arrows around. Similarly, properties proven for the original concept can be translated for the dual concept by turning the arrows around. In other words, if we have a categorical concept for which we have proven some properties, we get its dual counterpart together with its properties for "free". Diagrams Instead of writing down an equation like, f·g=h·i one could depict the equation in a diagram: 9 B----C f i A-·---D h In the literature about category theory, it is common practice to reason with diagrams instead of using equational reasoning. We will not do so. Occasionally we will give a diagram instead of an equation because a diagram states the type information more explicitly. If any two paths between a pair of objects depict the same arrow, we say that the diagram commutes. 10 CHAPTER 2. BASIC NOTIONS 2.2 Datatypes and functors Since the main objective of this thesis is to develop a theory in which it is possible to abstract from the data structure, we need an abstract notion of data structures. The requirement for this abstraction is that it be sufficiently abstract: we do not want to be bothered with too many details. But on the other hand, the abstraction should capture enough properties of a datatype in general such that it is possible to make other than trivial statements about datatypes. The choice we have made can be summarized by the slogan "datatype = relator". A relator is the relational extension of a so-called functor on functions. Not all functors have a relational extension, but all functors corresponding with datatypes we encounter in daily life do have one. Hence, we will only consider functors which have a relational extension. In section 2.5, we give the formal definition of a relator. For the moment, we forget about the relational . extension and just work with functors. For a specific datatype, we assume the existence of two notions. The first is the notion of a datatype former. Using a datatype former, we can construct, from existing datatypes, new datatypes. An example of a datatype former is pairing or tupling. Having the types A and B, one can construct the type A x B whose elements are pairs of elements of type A and B. Another example is the list former. Having type A, List(A) is the type of all finite lists over A. The second notion, directly connected with the first notion, is the notion of a generalized map, For lists, we have the well-known function map: it takes a function and a list and applies the function to each element of the list. In general, associated with a datatype former we assume a map which takes a function and data structure and applies the function to each of the elements of the data structure. Formalizing both notions for categories in general yields the notion of a functor. 2.2.1 Functors A functor consists of two mappings 1, one mapping objects to objects and one mapping arrows to arrows, which respects identities and distributes over composition. Both mappings we usually denote by the same symbol. Formally, F is afunctor to category C from TJ, i.e. F : C i;----TJ, iff and, for each f : Ai;---- Band 9 : B i;---- C, Ff·Fg=F(f·g) . These two requirements imply the following typing rule: Ff : FAi;---- FB f: Ai;----B . I We adopt the convention that we call functions on the meta level "mappings" and use the tenn "function" only if we mean an arrow of a category. 2.2. DATATYPES AND FUNCTORS 11 Note that we denote functor application by juxtaposition. It is trivial to verify that functors are closed under composition. Functor composition we usually denote also by juxtaposition. Hence, FGf means either F(Gf) or (FG)f. However, if we want to make the composition of functors explicit, we write foG instead of FG. The classical example of a functor is the map operator on lists known for functional programming. As we remarked above, for lists the object map is the type-constructor List, i.e. for type A, List(A) is the type of lists over type A. The function map is the arrow map: it takes a function and a list and applies the function to each element of the list. Hence, for function f : A f- 13, we have map (1') : List (A) f- List(13 ). It is trivial to verify that map indeed respects identities and distributes over composition. Two other more trivial, but nevertheless important examples of a functor are the identity functor and the constant functor. The identity functor Id leaves both the object and arrow unchanged. The constant functor KA, for some object A, maps each object to A, and each arrow to idA. Functors we denote by F, G, ..., or by a capitalized identifier like rd, Outl, etc. F-structures and shapes Elements of FA, for some A, we call F-structures. The interpretation of an F-structure is that it is a data-structure containing elements of type A. Then the interpretation ofH, for f : A f- 13, is that it transforms an F-structure of A's into an F-structure of 13 's by applying the function f to each element of the F-structure. For an F-structure of A's, the function F!A : F1 f- FA yields its so-called shape. That is, F!A replaces each element of the F-structure of A's by the element 1. Now, two F-structures have the same shape if application of F! to both F-structures yields the same F-structure of 1'so Note that H respects shape since using the fact that functors distributes over composition and terminality of 1, we have, F! . H = F(!· f) = F! . Hence, applying H to an F-structure and then computing its shape yields the same shape as the shape of the original F-structure. 2.2.2 Natural transformations Directly related with functors is the notion of a natural transformation. For functors F and G to category C from category D, a collection of arrows (X of C, indexed by objects of D, is a natural transformation of type F f- G if, H· (XB = (XA . Gf for each f: Af- 13. The interpretation of a natural transformation is that (XA : FA f- GA transforms a G-structure of A's into an F-structure of A's without changing the value of the elements. In chapter 4 we give a formal justification for this claim. An example of a natural transformation is the polymorphic reverse function on lists. For each type A, revA is the reverse function on a list of type List(A) . Indeed, for rev we have the property, for function f : A f- 13, map(f) . revB = revA . map(f) , 12 CHAPTER 2. BASIC NOTIONS since first applying function f to each element of the list and then reversing the list yields the same list as if we first reverse the list and then apply f to each element. Hence, rev is a natural transformation of type List f- List. Another example of a natural transformation is the collection of identity arrows id. We have that id is a natural transformation to the identity functor from the identity functor, i.e. id : Id f- Id, since, Id( f) . idB = idA' Id( f) for each f : A f- B. We will suppress the typing information if one can deduce from the context what the index is of a specific natural transformation. Natural transformations we denote either by Greek symbols ex, {3, ..., or by sans serif identifiers like rev, id, etc. 2.2.3 Category Fun As the notation ex : F f- G suggests, it is the case that functors and natural transformations form a category. Thefunctorcategory, denoted by Fun, has as objects functors and as arrows natural transformations between them. The identity arrow on functor F is id F where id F is defined by (idF)A = id FA . Furthermore, the composition of ex: F f- G and {3 : G f- H is ex· {3 where ex· 13 is defined by (ex· I3)A = exA . 13 A. It is not too difficultto verify that ex· 13 : F f- H. Furthermore, composition of natural transformations is associative. Hence, Fun is indeed a category. Note that composition of natural transformations is defined as the pointwise lifting of the composition in the base category. In general, calculation steps involving pointwise liftings will be indicated by the hint "lifting". The sub-category of Fun of functors to C from D we denote by Fun (C ,1)). If F is a functor to category C from D, then functor F induces two functors on natural transformations. IfF : C f- D then (Fa) : Fun(C,E) f- Fun(D, E) for each category E, where (Fa) denotes post-composition of functor F. More precisely, the object map of (Fa) is postcomposition of F, i.e. the application of (Fa) to a functor G is defined to be FG, and the application of (Fa) to a natural transformation ex is defined to be Fex i.e. the composition of (the arrow map of) functor F after the natural transformation ex. (Recall that we view a natural transformation as a mapping from objects to arrows.) It is not too difficult to prove that (Fa) is indeed a functor on natural transfonnations. First of all, (Fa) applied to a natural transformation ex : G f- H is a natural transformation of type FG f- FH since, for f : A f- B, FGf . FexA = F(Gf . exA) = F(exB . Hf) = FexB . FHf . Furthermore, functor (Fa) respects identities, and distributes over composition: (Fex· FI3)A = FexA . FI3A = F(exA ·I3A) = F(ex· (3)A . The second functor on natural transformations induced by functor F: C f- D is (aF) : Fu n(E , D J f- Fu n(E ,C), for each category E, where (a F) is pre-composition off. Specifically, the application of (a F) to a functor G is defined to be GF and the application of (a F) to a natural transformation ex is the mapping that maps object A to exFA. Again, it is not too difficult to prove that (a F) so defined is a functor on natural transformations. First of all, (a F) applied to natural transformation ex : G f- H is a natural transformation of type GF f- HF since, forf: Af- B, exFA . GFf = HFf . exFB . 2.2. DATATYPES AND FUNCTORS 13 Furthermore, functor ( F) respects identities by definition, and distributes over composition since: 0 £X.FA . 13 FA = (£X.·I3)FA . Basically, (oF) distributes over the composition of natural transformations since this composition is defined as the pointwise lifting of the composition in the base category. Like functor composition, we denote the composition of a functor after a natural transformation by juxtaposition. Hence, F£X.A means either F( £X.A) or (F£x')A. However, the composition of a natural transformation after (the object map of) a functor we denote by £X.F instead of £X.F. The reason for this is that the notation lXF is more consistent with the decision we made to suppress type information if the type of the natural transformation can be deduced from the context. 2.2.4 Product and coproduct The first two non-trivial examples of datatype formers we define formally are product and coproduct. Given two types A and B we can construct their product, or tupling, A x B elements of which are pairs of elements of A and B. The coproduct, or disjoint sum, of A and B, denoted by A + B is basically the union of A and B but constructed in such a way that it is always possible to decide whether an element of A + B originates from A or from B. Product We begin with the abstract definition of product. A product of two objects consists of an object and two projection arrows. The object is denoted by A x B and the two arrows by outlA,B : A f- A x Band outrA,B : B f- A x B. Furthermore, for each pair of arrows f : A f- C and 9 : B f- C with the same source we require the existence of the so-called split of f and g, denoted by ftc. 9 : A x B f- C, for which the following universal property holds: h=ftc.g outIA,B·h=f /\ outrA,s·h=g, (2.2) for each h : A x B f- C. We say that a category has products if each pair of objects has a product. Category Map has products: A x B is the cartesian product of the sets A and B, and outlA,B and outrA,B are the normal projection functions. Then ftc. 9 is the function defined by (ftc.g)(x) = (f(x) ,g(x)). As with natural transformations, if one can deduce from the context what the type of a specific projection arrow is, we will suppress the typing information. Taking h := ftc. g, we get from the universal property the so-called computation rules for split: outl . (f tc. g) = f /\ outr· (f tc. g) = 9 . (2.3) Furthermore, taking f := outl . hand 9 := outr . h, yields, for h ; A x B f- C, h = (outl· h) tc. (outr· h) . (2.4) Similarly, the following distribution law is easy to verify, (ftc.g)·h= [f·h)tc.(g·h) . (2.5) 14 CHAPTER 2. BASIC NOTIONS The product functor If category C has products we can define a productfunctor: for all f : A f- C and 9 : B f- D fxg ~ (f·outlc,o)6.(g·outrc,o): AxBf-CxD. For category Map, f x 9 is the function that maps the pair (a, b) to (f( a) , 9 (b)). That "x" is indeed a functor is not too difficult to verify. From equation (2.4) it follows by taking h := idA x B that" x" respects identities. That" x" distributes over composition follows from the so-called product-splitfusion rule: (f x g) . (h6.i) = (f· h) 6. (g ·i) since using the distribution property (2.5) and computation rules (2.3) (fxg). (h6.i) = (f·outl·(h6.i))6.(g·outr·(h6.i)) = (f·h)6.(g·i) Using the product-split fusion rule we can verify that "x" distributes over composition, i.e, (fx g). (hx i) = (f· h) x (g ·i) since h x i = (h· outl) 6. (i· outr). From the computation rule for split (2.3) it follows that the projection arrows distribute through "x", for f : A f- C and 9 : B f- D, outlA,B . (f x g) = f· out1c,O 1\ outrA,B . (f x g) = g. outrc,o since, for example, out/A,B . (f x g) = outlA,B . ((f· out1c,o) 6. (g. outrc,o)) = f . outlc,o In other words, outl and outr are natural transformations of type Outl f- x and Outr f- x, respectively, where Outl and Outr denote the projection functors of type C f- C2 . That is, Outl(f, g) = f and Outr(f, g) = g. Coproduct The definition of coproduct is the dual of product. So, the coproduct of A and B in category C is the product of A and B in the opposite category CoP. Thus, a coproduct of two objects consists of an object and two so-called injection arrows. The object is denoted by A + Band the two arrows by inlA,B : A + B f- A and inrA,B : A + B f- B. Furthermore, for each pair of arrows f : C f- A and 9 : C f- B with the same target we require the existence of the so-called junc of f and g, denoted by f'V 9 : C f- A + B, for which the following universal property holds: h=f'Vg h· inlA,B = f 1\ h· inrA,B = 9 , (2.6) for each h : C f- A + B. We say that a category has coproducts if each pair of objects has a coproduct. Category Map has coproducts: A + B is a disjoint union of the sets A and B, i.e. A+B={(a,O) I aEA}U{(b,l) I bEB}, 15 2.2. DATATYPES AND FUNCTORS ° and the functions inlA.B and inrA,B add a and 1 tag, respectively. Then f'V 9 is the function defined by (f'Vg)(o.,O) =f(o.) and (f'Vg)(b,l) = g(b). Since coproduct is the dual of product, we get the properties for coproduct for "free" by turning the direction of the arrows around. The dual of computation rules (2.3) are the computation rules for june: (fvg) . inlA,B = f 1\ (f'Vg). inrA.B = 9 , (2.7) and, the dual of (2.4) is, for h : C f- A + B, h= (h·inIA,B)'V(h·inrA,B) . (2.8) For june we have the dual distribution law: (2.9) h· (f'Vg) = (h·f)'V(h·g) The coproduct functor Just as for product, if category C has coproducts we can define a coproduct functor. Again, we dualize the results for product. Hence, the coproduct functor is defined by, for f : A f- C and 9 : B f- D f+g ~ (inIA.B·f)'V(inrA,B·g): A+Bf-C+D . For category Map, f + 9 is the function which leaves the tagging of an element unchanged and applies f or 9 to the element if it is tagged with or 1, respectively. ° Since" x" is a functor it follows from duality that "+" is a functor too. Dualising the productsplit fusion rule, we get the so-called june-coproductfusion rule: (f'Vg)· (h+i) = (f·h)'V(g·i) . Like projections, inl and inr are natural transformations, for f : A f- C and 9 : B f- D, (f + g). inlc,D = inIA,B' f 1\ (f + g). inrC,D = inrA,B' 9 So, inl : + f- Outl and inr : + f- Outr. Product and coproduct in category Fun If category C has products then so has category Fun (C ,D). The product of functors F and G is the functor F x G that maps arrow f to Ff x Gf, and the two corresponding projections are outlF,G and outrF,G defined by (outIF,G)A = outIFA,GA and (outrF,G)A = outrFA,GA, respectively. The split operator on natural transformations lX ; F f- G and 13 : H f- G is defined as the pointwise lifting of the split operator "6" in the base category C. That is to say, lX 6 13 is defined by (lX 6 13)A = lXA 613A. It is straightforward to verify that (F x G , outlF,G ,outrF,G) defines a product for the functors F and G. The junc operator and coproduct are lifted similarly. Throughout the remainder of this thesis, we silently lift operations defined on arrows to functors and natural transformations. For instance, for binary functor ®, the functor F®G is defined by (F®G)f = Ff ® Gf. Similarly, the natural transformation lX®13 is defined by (lX® 13 )A = lXA ® 13A. Note that definition of the lifting of an operation defined on arrows to natural transformations is consistent with our convention to occasionally suppress the typing of natural transformations. CHAPTER 2. BASIC NOTIONS 16 2.2.5 Recursive datatypes Until now we have only considered non-recursive datatypes. In this section we describe how one can construct recursive datatypes. The two leading examples will be natural numbers and (cons) lists. A natural number is either zero or the successor of a natural number. Similarly, a list is either the empty list or an element concatenated to a list. That is to say, we have the following constructors, for naturals, zero: Nat f- 1 1\ succ: Nat f- Nat and for lists over type B, denoted by List(B), nill : List(B) f-1 cons : List(B) f- B x List(B) 1\ Note that the constructors of a type have the same target, hence, using coproduct the typing of the individual constructors can be combined in a single statement: zero v succ : Nat f-1 + Nat, and nil I v cons : List(B) f- 1 + B x List(B) . Note that both ')uncs" have the typing A f- FA, for some object A and some functor F. For the naturals, A = Nat and FX = id 1 + X, and for lists over type B, A = List(B) and FX = id 1 + idB X X. An arrow of type A f- FA is called an F-algebra and object A is the socalled carrier of the F-algebra. Connected with F-algebras, is the notion of an F-homomorphism. An F-homomorphism to an algebra f : A f- FA from an algebra 9 : B f- FB is an arrow h : A f- B such that h· 9 = f· Fh . For instance, the function which computes the size of a list of B's is an F-homomorphism where F is the functor used to define lists of B's, i.e. FX = id 1 + idB X X. We have that size : Nat f- List(B) and size satisfies the following recursive equation, where 0 denotes the empty list, size [D) size(cons(a,x)) o succ(size(x)) or, equivalently without points, since succ(size(x)) = (succ' outr· (id x size))( a, x) zero succ . outr . (id x size) size· nill size· cons Again, using coproduct both equations can be combined into one: (size· nill) v (size· cons) = zero v (succ . outr' (id x size)) . (2.10) 2.2. DATATYPES AND FUNCTORS 17 Now, using the rules distribution (2.9) and june-coproduct fusion, equation (2.10) can be rewritten as, size· (nillv-cons) = (zerov-(succ·outr))· (id! +(id B x size)) (2.11 ) Hence, size is an F-homomorphism to F-algebra zero v- (succ· outr) from F-algebra nill v- cons. The size function on lists is an example of a function which follows the structure of the recursively defined data structure. In general, a function which is recursively defined according to the structure of lists has the structure: c h(D) h(cons( a, xl) f(a,h(xl) or, equivalently, without points, h·nill = h· cons c f· (id x h) Again, the two equations can be combined into one: h· (nillv-cons) = (cv-f)· (id! + [idB x hll . (2.12) In other words, 11. is an F-homomorphism to F-algebra c v- f from nil I v- cons. For lists, we know that the recursive equation uniquely defines function h. In functional programming, the function 11. is known to be an instance ofjoldr, specifically, h = joldr( c , f). In general, if a recursive definition of a function according to the structure of a recursively defined data structure always has a unique solution, we say that the algebra corresponding to the data structure is initial. Formally, let in : T f- FT be an F-algebra. Then in is initial ifffor each F-algebra f : A f- FA there exists an F-homomorphism of type A f- T, which we denote by QF; fD, for which the following universal property holds, 11. = QF; fD == h· in =f . Fh (2.13) Hence, QF; fD is the unique F-homomorphism to algebra f from in. Property (2.13) we call the unique extension property. Furthermore, we call QF; fD the F-catamorphism of f. If it is clear from the context which functor is meant, we write QfD instead of QF; fD. Taking 11. := id T and f := in in the unique extension property gives id T = QinD. Similarly, taking 11. := QfD gives the computation rule, QfD . in = f . FQfD . Using the computation rule, we can prove thejusion law, h· QfD since = QgD {= h· f = 9 . Fh , 18 CHAPTER 2. BASIC NOTIONS h· qfD = [gD { unique extension property h· qfD· in = { computation; F-functor = h . f . FqfD {= 9 . Fh . FafD Leibniz} { h·f 9 . F(h· afD) = 9 . Fh We claim that in: T f- FT is an isomorphism. In order to show that in is an isomorphism, we have to construct an arrow ex of type FT f- T such that ex· in = id rr 1\ in· ex = id T (2.14) . Since the source of ex is T, i.e. the carrier of the initial algebra in, we suspect that ex is a for some ~. We continue with the rhs of equation (2.14) catamorphism, i.e. ex = a~D in . a~D {= = id T id T { in . ~ = = ainD, fusion } in . Fin Hence, if we take ~ = Fin, i.e. ex = aFinD, we have in . ex = id T . We verify whether for this choice of ex the first conjunct of (2.14) is also true: aFinD . in { computation Fin· FaFinD { F functor } F(in· aFinD) { above: in· aFinD = id T , F functor id rr Hence, an initial algebra is an isomorphism [29]. This fact we will exploit when we extend catamorphisms to relations. As in Map, a relation is an isomorphism iff it is a bijection. Remark: the use of the term "initial" is because an initial F-algebra is initial in the category of F-algebras. The objects of this category are F-algebras and the arrows are F-homomorphisms. Parameterized datatypes Note that the example of lists is parameterized by an object. Recall that nill 'V cons is an initial F-algebra of type List(B) f- 1 + B x List(B) where F is the functor FX = id 1 + idB X X. In general, and making the parameter B explicit, assume a collection of initial algebras inB of type TB f- B I6i TB where T is some mapping on objects and I6i a binary functor. Hence, inB 2.3. THEOREMS FOR FREE 19 is a (id BC9)-algebra where (id BC9)X = id B\'9X. For the example of lists, we have that T is the datatype former List and i3l is the binary functor defined by X0Y = 1 + X x Y. For lists, the map operator discussed in section (2.2.1) is the arrow map of the List functor. Such a map operator exists in general. For each arrow f : A f- B, we have to construct an arrow a : TA f- TB. Since TB is the carrier of an initial algebra, we suspect that a = ~idBC9; /3D for some f3 : TA f- B i3l TA. Since the target of inA is TA, we take f3 = inA· l' for some l' : A C9 TA f- B i3l TA. Now, f has the type A f- B, hence, fi3lid TA : A I6i TA f- B I6i TA. If we take l' = fi3lid TA , so a = ~idBI6; inA· (fi3lid TA 1D, then a has the type TAf- TB, as desired. Moreover, defines a functor, the tree type functor induced by i3l. Functor T respects identities since ~inAD = idTA . That T distributes over composition follows from the tree type fusion rule ([fD . Tg = ~f· (9C9idH which we prove by ~fD {= . Tg = (gi3lidlD definition T, fusion } { ~fD' ~f· inA· (gi3lid) { = f· (gC9id)· )Df~9Cdi( computation} f· (id 3l~fD)· (g0id) = f· (gi3lid)· (idi3l([fll) 0 binary functor: (id0~fD)· { (gi3lid) = (g0id)· (id0~fD) true Using the tree type fusion rule we can verify that T distributes over composition: Tf· Tg 2.3 = ~in· (fi3lid) . (90idH = ~in . (f· g)@idD = T(f· g) Theorems for free Having the notion of functors and natural transformation, we can formally define the notion of a "free theorem". For a natural transformation a of type F f- G it follows that for each of the individual members of the collection of arrows a, we have aA : FA f- GA. In other words, a:Ff-G =? (aA: FAf-GA foreachA) . One can view a as a polymorphic function: it has a type as parameter. Now, for a collection of arrows - a mapping from objects to arrows - its "free theorem" holds if the other way around is also true. That is to say, if for some a, a:Ff-G {= (aA: FAf-GA foreachA). (2.15) 20 CHAPTER 2. BASIC NOTIONS we say that the "free theorem" predicted by the typing of etA holds. Reynolds [43] proved for a particular language that property (2.15) holds for all polymorphic functions that are definable in that language. He showed that all basic polymorphic functions are natural transformations, and he proved that all the language constructs preserves natural transformations. One can not expect property (2.15) to hold for all polymorphic functions in all languages. For instance, if it is possible to define a polymorphic function in some ad hoc way, e.g. by giving a clause for each type, one can not expect property (2.15) to hold for all functions. However, property (2.15) holds for a polymorphic function which is constructed using constructs that behave "nicely". For instance, if we construct a polymorphic function using natural transformations and constructions that preserve naturality, it follows that this function is a natural transformation. So, we consider property (2.15) as a healthiness condition for a polymorphic function. The informal interpretation of natural transformation is that its behaviour does not depend on the type at which it is instantiated. In other words, we consider a polymorphic function for which property (2.15) holds to be parametrically polymorphic in the sense that the definition of the polymorphic function does not depend crucially on the actual value of the type at which the polymorphic function is being instantiated. 2.3.1 Higher-order naturality Extending Reynolds' idea further we also use the higher-order naturality property predicted by the type of a polytypic function as a healthiness condition. That is, we lift property (2.15) to a higher-level. By a higher-level we mean that we instantiate parameter (object) A with a functor and functors F and G with functors on the functor category. Functor F is a functor on the functor category if it consists of two mappings, a mapping that maps functors to functors -the object map of F- and a mapping that maps natural transformations to natural transformations -the arrow map of F-. Furthermore, F should distribute over composition of natural transformations and respect identities. Now, let ~ denote a polytypic natural transformation such that ~F is a natural transformation of type GF +-- HF for higher-order functors G and H. Then ~ is a higher-order natural transformation of type G +-- H if Get . ~L = ~K . So, if for some polytypic ~ : G +-- H {= (2.16) Het for each et: K +--l (~F: ~ we have, (2.17) GF +-- HF for each F) , we say that the higher-order "free theorem" predicted by the typing of ~F holds. For example, define F~ = Fy for some fixed natural transformation y : G +-- H, i.e. 13 is postcomposition with a functor. Then we have F~ : GF +-- HF,foreach functor F, whereG = (oG) and H = (0 H), i.e. G and H are pre-composition with functors G and H, respectively. So, the 2.4. ALLEGORIES 21 "free theorem" predicted by the typing ~F : GF f- HF, is the higher-order naturality property ~ : G f- H. Expanding the definition of higher-order naturality yields, GlX . ~L = ~K . HlX for each lX: and instantiating with the definition of lXG . LY = Ky· lXH for each ~ lX : K f- L and G and H gives us, K f- L which follows trivially from the typing of y and lX. So, we conclude that post-composition with an arbitrary functor is a truly polytypic operation since the higher-order naturality predicted by the typing of post-composition holds. Of course, post-composition is a trivial example. In Chapter 5 we use (2.17) as a healthiness condition for the polytypic operation that commutes two datatypes. 2.4 Allegories Allegories are to binary relations between sets as categories are to functions between sets. An allegory A is a category with some additional structure to capture the properties of binary relations. This additional structure is inspired by the properties we have for binary relations over sets. Although other allegories than the category Rei exist, we talk about allegories as if we are working with binary relations over sets. That is to say, if we give an interpretation of a property, we give the interpretation with respect to the set theoretical model of relations. Furthermore, we use the same terminology for arbitrary allegories as for the category ReI. In particular, we talk about "relations" instead of "arrows". The reason why we work with relations instead of functions is because it is easier to express partial and nondeterministic operations than working in a strict functional setting. The move from functions to relations increases our powers of expression. For instance, we can use the nondeterministic nature of relations in order to specify nondeterministic problems. Of course, it is always possible to use set-valued functions or binary predicates instead of relations. However, we find it more natural to work with the relations directly. Since we were already working in a categorical setting, i.e. working without elements (points), the price to pay in order to shift from functions to relations is not that high. The calculus of relations has existed for a very long time. (See e.g. [49, 50,47].) Our presentation of the calculus is based on the work of Backhouse et. al. [3, 2, 5, 1] and the book by Bird and De Moor [7]. The latter is based on the theory of allegories as set out in [17]. We use allegories too. 2.4.1 Galois connections Before we introduce the notion of an allegory we first introduce the concept of a Galois connection. As we will see, we introduce several allegorical notions as instances of a Galois connection. The theory of Galois connections is not that deep but it helps us to shape the setup of the theory and our calculations. 22 CHAPTER 2. BASIC NOTIONS Assume two partial orders::; and ~. Then we say that the functions f and 9 form a Galois connection, or are Galois connected if for all x and 1), f(x) ::; 1) == x ~ (2.18) 9(1)) . We call functions f and 9 the lower adjoint and upper adjoint, respectively, of the Galois connection. The first property we prove is that for a given function f there exists at most one upper adjoint. That is, assume that 9 and h are both an upper adjoint of f, then for each x and 1), x ~ 9(1)) { f and 9 are Galois connected } f(x) ::; 1) { ~ x f and h are Galois connected } h(1)) Now, instantiating x:= 9(1)), and instantiating x:= h(1)) gives 9(1)) ~ h(1)) and h(1)) ~ 9 (1)), respectively. By anti-symmetry of ~ it follows that 9 (1)) = h(1)) for each 1), hence, 9 = h. If we know that two functions are Galois connected we get a number of properties for free. For instance, we have the so-called cancellation rules, x ~ g(f(x)) and f(9(1)))::; 1) . which follow from (2.18) by instantiating 1) := f( x), and by instantiating x := 9 (1)) such that the lhs and rhs, respectively, becomes true. Furthermore, the functions of a Galois connection are monotonic. For instance, we have, f(x) ::; f(1)) { x {= ~ f and 9 are Galois connected (2.18) 9(f(1))J { cancellation: 1) ~ 9 (f (1) )); transivity of ~ } X~1) The function f distributes over arbitrary joins and 9 over arbitrary meets provided they exist. In particular, f is bottom-strict and 9 is top-strict. As an example, we prove that f distributes over arbitrary joins. That is, let U and U denote the join of ::; and ~, respectively. Then we have to show that for each set S such that uS exists, f(uS) = u{f(Sll SES} We calculate, for all X, fluS) ::; X f and 9 are Galois connected } 2.4. ALLEGORIES uS ~ 23 g(X) { join} 'v'(S: SES: S ~ { g(X)) f and 9 are Galois connected } 'v'(S: SES: f(S)::::; X) { join} U{ f ( S) I S E S} ::::; X Hence, by anti-symmetry of::::; it follows that fluS) = u{f(S) I SE S}. As mentioned earlier, we define a number of operators by means of a Galois connection. Subsequently, we use the fact that such operators are monotonic, distribute over arbitrary joins or meets, and obey certain cancellation properties as a matter of course. 2.4.2 Definition An allegory is a category satisfying some additional axioms. Because it is a category, for every object A there exists an identity idA, and every pair of relations R : A f - Band S : B f - C, with matching source and target, can be composed: R· S : A f- C. Composition is associative and has id as a unit. The additional axioms are as follows. First of all, relations of the same type are ordered by the partial order <:;:; and composition is monotonic with respect to this order, that is, In Rei, the partial order <:;:; is just set inclusion. Secondly, for every pair of relations R, S : A f- B, its intersection (meet) R n S exists and is defined by the following universal property, for each X : A f- B, X<:;:;R 1\ X<:;:;S X<:;;RnS Using the definition of intersection it is easy to verify that intersection is idempotent, commutative and associative. Note that we do not assume the existence of arbitrary intersections. If a largest relation of type A f - B exists we denote it by TT A, B. In Rei, TT A, B is the cartesian product A x B. From the fact that composition is monotonic and the universal property of intersection, it follows that composition distributes over intersection with an inclusion. That is to say, (RnS)·T<:;;(R·T)n(S·T) . Finally, for each relation R : A f- B its converse RO : B f- A exists. In Rei we have, x RO 1) iff R x. The converse operator is defined by the requirements that it is its own Galois adjoint, that is, 1) 24 CHAPTER 2. BASIC NOTIONS and is contravariant with respect to composition, Since converse is defined using a Galois connection, we have the following cancellation rules R ~ (RO)Oand(RO)O ~ R. Hence, converse is its own inverse: (RO)O = R. Furthermore, from the theory of Galois connections, it follows that converse distributes over arbitrary meets. Specifically, we have, As might be suspected, it is the case that id id = (idT = = (idO . idt ida . (idT = ida since, = ida . id = ida All the three operators of an allegory are connected by the modular law, also known as Dedekind's law [45]. We have as an axiom (R· S) n T (R n (T· SO)) . S . ~ Taking the converse of both sides, we have dually, (R· S) n T ~ R· (S n (Ro. Tll . Note that allegory Rei has more structure than we have captured so far with our axioms. For instance, in Rei we can take arbitrary unions Goins) of relations. However, the basic definition of an allegory has already sufficient structure to define some useful concepts. In subsection 2.4.9 we give the axioms for the union of relations. 2.4.3 Partial identities Relations below an identity relation, i.e. X ~ idA for some A, we call partial identities (sometimes called "coreflexives" [17] or "monotypes" [1]). We use partial identities to represent subsets of object A. In Rei, there is a partial identity on A for every subset B of A. That is to say, for subset B we can define the partial identity X, by b X b == bE B. We say that X is the partial identity corresponding to B. Partial identities are symmetric. First notice that for arbitrary relation R, we have R since using the modular law, ~ R· RO . R Hence, for partial identity X, X ~ X· XO . X ~ XO, so X = XO. Furthermore, intersection and composition of partial identities coincide. Using monotonicity it follows trivially for partial identities X and Y that X . Y ~ X n Y. The other inclusion follows from the modular law, X n Y = (id· X) n Y ~ Hence, X . Y = X n Y. (id n (X· Y°J) . Y ~ X· Y 2.4. ALLEGORIES 2.4.4 25 Simple and total relations A special sub-class of relations is formed by functions. Functions are total and single-valued relations, that is to say, relation f is a function iff for each x there is precisely one lJ such that lJ f x. Formally, relation R : A f- B is total or entire iff idB r;;; RO. R , and relation R is single-valued or simple iff R· RO r;;; idA. Afunction is a relation that is both total and simple. It is easy to verify that total and simple relations are closed under composition. Hence, functions are closed under composition too. In other words, the functions form a sub-category. For an allegory A, we denote the subcategory of functions by Map(A). In particular, we have Map(Rel) = Map. We say that R is surjective iff RO is total, and R is injective iff RO is simple. Furthermore, R is a cofunction if RO is a function, and R is a bijection if R is both a function and a cofunction. Note that arbitrary relations do not distribute over intersection, but simple relations do distribute over intersection from the right. We calculate: (RnS)·f c { distribution } (R·f)n(S·f) c modular law } { (R n (S· f . fO)) . f c assumption: f . fO r;;; id { (RnS) ·f Hence, for f simple (RnS)·f = (R·f) n (S·f) Taking the converse of both sides, we have dually fO . (R n S) = W' R) n W . S) Furthermore, for simple relations we have the modular law with equality called the modular identity. We have, (f·R)nS C { f· (R n c modular law } W' S)) { distribution (f· R) n (f· fO . S) c { (f·R)nS assumption: f . fO r;;; id } 26 CHAPTER 2. BASIC NOTIONS Hence, for f simple (f· R) n 5 = f· (R n W' 5)) Again, taking the converse of both sides, we have dually (R·fO) n 5 = (Rn (S·f))·fo A rule we use frequently is the so-called shunting rule. For function f, we have f· R <;;; 5 (2.19) and dually, R· fO <;;; 5 (2.20) R <;;; 5·f We prove the first one: f· R <;;; 5 =9 { monotonicity } fO . f . R <;;; fO . 5 assumption: id <;;; fO . f } =9 R <;;; fO. 5 monotonicity } =9 f· R <;;; f· fO. 5 =9 assumption: f . fO <;;; id } { f· R <;;; 5 Hence, f . R <;;; 5 == R <;;; fO . 5. As a matter of fact, relation f is a function shunting rule (2.19) holds for each Rand S. if and only if the Note that if we have to prove equality of functions, we only have to show one inclusion, that is, f<;;;g==f=g since using shunting rules (2.19) and (2.20), f <;;; 9 == id <;;; fO . 9 == gO <;;; r == 9 <;;; f By definition, in a category, an arrow f : A 9 : B f- A such that 9.f = ids 1\ f· 9 = idA f- B is an isomorphism if there exists a arrow . In an allegory, the isomorphisms and bijections coincide. It is trivial to verify that if R is a bijection then R is an isomorphism. (Take 9 := RO.) For the other way around assume that 5 . R = ids 1\ R· 5 = idA . We intend to show that 5 = RO, and so R is a function and a cofunction, hence, a bijection. Recall that for arbitrary relation P that P <;;; p. po . P. Combining this with the assumption gives us, RO = 5 . R . W . R . 5 ~ 5· R . 5 = 5 Hence, RO :;;> 5. Similarly, it follows that 5° :;;> R. Hence, 5 = RO. 27 2.4. ALLEGORIES 2.4.5 Tabular allegories We say that an object C and a pair of functions f : A f- C and 9 : B f- C is a tabulation of relation R : A f- B if An allegory is said to be tabular if every relation has a tabulation. Note that in general we do not assume tabularity. However, as we will see in subsection 2.5.2, the definition of relational product is precisely the existence of a tabulation of TT. Allegory Rei is tabular. Given relation R : A f- B, define C to be the subset of the cartesian product A x B containing the pairs of elements for which (x, 1J) E R. Then the pair of projection functions outl : A f - C and Qutr : B f - C is a tabulation of R. In the remainder of this chapter we define the relational extension of some of the categorical concepts defined in section 2.1. Recall that a relational extension of a categorical concept is something which is defined on all relations but if one takes the restriction to functions, one gets the original categorical concept for the sub-category of total functions. Although we do not assume tabularity in general, we show that for the relational extension of functors and natural transformations, tabularity implies that the relational extension is in some sense canonical. That tabularity is the key assumption in order to prove the relationship between total functions and relations is not that strange. It enables one to translate a statement about relations into a statement about functions by replacing relations by their corresponding tabulations. 2.4.6 Unit We say that an object U is a unit if id u is the largest relation of its type, i.e. id u = TT u,u, and for every object A there exists a total relation !A : U f- A. Together with the first requirement it follows that !A is a function: simplicity follows from !A . !~ ~ id u since !A . !~ : U f- U. If an allegory has a unit then all tops TT A,B exist since for all R : A f- B, we have: ! function, shunting !A . R . !B ~ { id u id u = TT u,u } true Hence, TT A,B = !~ . !B. In particular, we have !A ~ TT U,A = !il' !A ~ !A since !u ~ id u . That is !A = TT U,A. In general we do not have the property that TT A,B . TT B,e = TT A,e for arbitrary objects A, Band C. (For instance, in Rei the Ihs is the empty relation if B is the empty set.) However, this property holds if B = U since, TT A,U . TT u,e = !~ . !e = TT A,e . 28 CHAPTER 2. BASIC NOTIONS As a matter of fact, the existence of a unit can equally be guaranteed by the requirement IT A,U . IT U,C = IT A,C /\ idu;;:J IT u,u (2.21) The first conjunct expresses that the object U is not the empty type, i.e. contains at least one element. The second conjunct expresses that U contains at most one element. So in Rei, the units are the singleton sets. We say that an allegory is unitary if it has a unit and we denote this unit by 1. If allegory A has a unit 1 then this unit is terminal in the sub-category Map(A). Since !A = IT I,A it follows for function f : 1 f- A that !A ;2 f. Hence, !A = f since !A is a function too. For a tabular allegory, the other way around is also true: if 1 is terminal in the subcategory Map (A) then 1 is a unit of A. We have to show that id l is the largest relation of its type. Let (f , g) be a tabulation of relation R of type 1 f-1. Then, R <;;; id l { R = f. gO } f. gO <;;; id l { shunting, f, 9 functions } f=g { f ,g : 1 f- C, terminality of 1 } true The definition of a unit is the first example of a relational extension of a categorical concept for functions. Without using tabularity, it follows that if we restrict our attention to functions, i.e. the sub-category Map, we get the normal categorical definition. For the other way around, we have to assume tabularity to show that the relational extension of a categorical concept on functions is in some sense canonical. 2.4.7 Range and domain Associated with every relation R : A f- B there are two partial identities, the domain of R, denoted by R> : B f- B and the range of R, denoted by R< : A f- A. The domain operator is defined by the following universal property [1], R <;;; IT A,B' X == R> <;;; X for each X <;;; idB. (2.22) The interpretation of the domain operator is that it represents the set of values for which there exists an image, i.e. x E R> == :3(lJ:: lJ R x) Note that the universal property is a Galois connection. Hence, we know immediately that ">" is monotonic. Dually, the range operator is defined by the universal property, R <;;; X· IT A,B == R< <;;; X for each X <;;; idA. 2.4. ALLEGORIES 29 Note that R> = (RO)<. From now on we concentrate on the domain operator; the properties of the range operator follow by duality. The domain operator can equally well be defined by the following universal property == R> R = R· X ~ X for each X ~ id B • (2.23) In other words, R> is the least partial identity which is a domain of R. The equivalence between (2.22) and (2.23) follows from the fact that (for all partial identities X) == R=R·X X·Tl~R which is verified using the modular identity. There are closed formulae for the domain and range operator. For the domain operator we have (2.24) R> = (RO . R) n id Again, using the modularity law it is not too difficult to verify that for the closed formula (2.24), the universal property (2.23) holds. For the domain operator, we have the following properties [1, 7], (R·S» = (R>· S» (2.25) , (R·S»~>, (2.26) (2.27) [RnS» = (Ro. S) n id One of the cancellation laws of the Galois connection for domains is R ~ IT A,B . R>. Composing both sides with IT C,A gives us IT C,A . R ~ IT C,B . R>, since IT C,A ·IT A,B ~ IT C,B· The converse inclusion follows from (2.24), IT C,B . R> ~ IT C,B . RO . R ~ IT C,A . R . Hence, we have the property, for R : A f- B and object C, IT C,A . R = IT C,B . R> . (2.28) Partial identities versus conditions Instead of representing subsets of object A by partial identities of idA, they can equally well represented by so-called conditions. A right-condition is a relation below !A, i.e. a relation of type 1 f- A. Dually, a relation R is a left-condition if RO is a right-condition. The order isomorphism between partial identities and conditions is given by the following equation, for all partial identities X : A f- A and (right) conditions C: If-A, X = C> == !A' X = C since we have the properties, 30 CHAPTER 2. BASIC NOTIONS The first conjunct follows from (2.25) and X> and !1 = id 1 · = X, the second conjunct follows from (2.28) So, instead of representing range and domain as partial identities one could represent them using conditions. From the correspondence between partial identities and conditions and property (2.28) it follows that the right-condition representation ofthe domain is ! . R. Sometimes it is more handy to use conditions instead of the partial identities. Note that we have, !. R <;;; !·S 2.4.8 Power allegories Instead of using binary relations, one could work with set-valued functions in view of the theorem that, for each relation R, there exists a set-valued function f such that x R y iff x E f(y). In this section we give a calculational presentation of this isomorphism between binary relations and set-valued functions. Power transpose and membership The object which represents the set of subsets of object A, called the power object, we denote by PA. The set-valued function corresponding to R, called the power transpose, is denoted by AR. That is to say, for each relation R : A f- B, AR is a function of type PA f- B. Tht:< membership relation corresponding with the power-object PA we denote by EA : A f- PA. For these three notions we demand the following universal property, for all R : A f- Band functions f : PA f- B, f = AR == EA· f = R (2.29) The universal property implies the following two cancellation laws: E . AR = R, and for function f : PA f- B, A( EA . f) = f. Hence, A is indeed an isomorphism. Using the first cancellation law, it follows that we have the following distribution property AR . f = A(R· f) for function f, since AR· f = A(R·f) { E· AR· f { f function, hence, AR . f function, (2.29) } = R·f cancellation} true Existential image As the notation PA for power objects already suggests, we can extend P to a functor on relations. We define the existential image functor by, ER ~ A(R·EB): PAf-PB foreachR:Af-B. 2.4. ALLEGORIES 31 The interpretation of ER is aE (ER)(x) =::: 3(b: bEX : a R b) The fact that E distributes over composition follows trivially from the following fusion law, = I\.(R· 5) , ER . 1\.5 whose validity is verified using distribution and cancellation as follows, = I\.(R· E) . 1\.5 = I\.(R· E ·1\.5) = I\.(R· 5) . ER . 1\.5 The fact that E respects identities, i.e. I\.E (2.29). So, E is a functor on relations. 2.4.9 = id, is easily verified using the universal property Locally complete allegories The definition of an allegory only mentions the intersection of two relations. In allegory Rei, the union of arbitrary relations also exists. We say that an allegory is locally complete if for each set S of relations of type A (- B, the union (join) uS : A (- B exists, defined by the following universal property, for all X : A (- B, US<;;;X=:::V(5ES::5<;;;X) , and intersection and composition distribute over arbitrary unions. That is, (uS) n R = u{5 n R I 5 E S} , R· (US) = U{R . 5 I 5 E S} . and (US) . R = U{5 . R I 5 E S} . The union of the empty set, i.e. smallest relation of type A (- B, we denote by ..ilA,B. Furthermore, for the union of two relations we use the infix notion, i.e. U{R, 5} = R U 5. By taking S to be the empty set it follows that ..il is a zero both of composition and intersection. It is trivial to verify that union is idempotent, symmetric and associative. Since converse and domains are both defined as a lower Galois adjoint it follows that they both distribute over arbitrary unions. Specifically, we have ..il 0 = ..il, (R U 5)0 = RO U 5°, ..il> = ..il, and (R uS» = R> uS>. 2.4.10 Division In this section we define the so-called division operators. (Others use the terms "residual" [11], "factor" [10], or "weakest pre-specification" [19].) By definition, we define "\" and "I" as the upper-adjoint of composition on the left and on the right, respectively. That is to say, we assume the following two Galois-connections, for R : A (- B, 5 : B (- C and T : A (- C, R . 5 <;;; T =::: 5 <;;; R\T , 32 CHAPTER 2. BASIC NOTIONS and R· S <;;; T == R <;;; TIS. A relation of the shape R\T or TIS we call a factor of T. Note that R\T : B f- C and f- B. The interpretation of the factors is TIS: A b(R\T)c == 'v'(a:aRb:aTc), and a(T/S)b == 'v'(c:bSc:aTc). For division we have the following cancellation rules, R· (R\T) <;;; T /\ (T/S),S <;;; T . These cancellation rules are the reason why we call the upper Galois adjoint division. Since (R\) and (/S) are the upper adjoints of (R . ) and ( . R), respectively, they distribute over arbitrary intersection. In particular, R\(S n T) = (R\S) n (R\T) /\ (R n T)/S = (RIS) n (TIS) . Furthermore, combining both Galois-connections yields the following Galois-connection: S <;;; R\T == R <;;; TIS. (2.30) To see why this is a Galois-connection, define as, S <;;; R\T == TIS ~ ~ = ;2, then equation (2.30) can be rewritten (2.31) R . Hence, (\T) and (T/) are Galois-connected. Thus (\T) is <;;; to ~ monotonic and thus antii.e. <;;;-joins, to <;;;-meets. The same monotonic, since ~ = ;2. Similarly, (\T) takes ~-mets, holds for (TI). In particular, we have, (R US)\T = (R\T) n (S\T) /\ T/(R US) = (T/R) n (TIS) . Since composition is associative we have the following properties for factors, R\(S\T) = (S· R)\T /\ (RIS)/T R\(S/T) = (R\S)/T . and, = R/(T· S) , 2.4. ALLEGORIES 2.4.11 33 Extensionality The last axiom we define is the so-called extensionality axiom. Note that our axiomatization of the relational calculus was point-free. Only when we gave an interpretation of a relation did we talk about elements (points), e.g. the elements related by a relation. The extensionality axiom enables us to give pointwise reasonings. First, we define the notion of a point. Definition 2.32 We say that p is a point if relation p is a function of type A (- 1 for some A. We write pEA if p is a point of type A (-l. o Recall that the interpretation of the object 1 is the singleton set {1}. So, since we require a point to be function, it means that point p is a function mapping 1 to some fixed element of object A. In Rei, binary relations of the form {(x, 1)}, for some x of the set A, are points. In other words, points correspond to the notion of an element of a set. This explains the notation pEA. We unify an element and its corresponding point. Now we can state the extensionality [46, 44] axiom. That is, we assume, for R, S : A (- B, that R;;2S R· p "2 S· p for all p E B. R. p = or, equivalently, R=S S . p for all p E B. Note that for point x, R . x corresponds to the image set of R with respect to the element x. That is to say, R· x is the set {(y ,1) Iy R x}. In other words, the extensionality axiom expresses that two relations are equal iff the image sets of both relations with respect to all elements are equal. Compare this with the normal version of extensionality for functions, f=g f(x) = g(x) for all x. Trivially, the extensionality axiom holds in ReI. In general, we will not assume the extensionality axiom. We only use the extensionality axiom if we want to calculate with the interpretation of relations. Note that we have the correspondence, 2.4.12 Operator precedence Now that we have introduced all the primitive operators in our calculus it is useful to give each a precedence in order to minimise parentheses. A general rule we use is that metaoperators ("1\", "=", "(-" etc.) have the lowest precedence. Among these operators the precedence rules we use conform to standard mathematical practice. The highest precedence is given to subscripting followed by prefix and postfix operators (converse being the only instance so far) and application and composition of mappings (both denoted by juxtaposition). The object level infix operators have the following precedence from highest to lowest: 34 CHAPTER 2. BASIC NOTIONS • division, • split, junc, product and coproduct, • composition, • intersection and union. For example, the expression f'Vg . h is parsed as (f'Vg) . hand R n 5 . T as R n (5· T). (Note that we endeavour to space the subexpressions in such a way that the correct structure is evident to the human eye.) From time to time parentheses will be introduced even though this is not strictly necessary. In particular, when giving basic laws it is our practice to include parentheses. (See for example (2.41).) 2.5 Relational extensions In this section we extend the notion of functors to so-called relators. A relator is a monotonic functor on relations which commutes with converse. A main property of relators is that they preserve functions. Hence, a relator is a relational extension of a functor on functions. As observed by De Moor [40], this extension is unique for a tabular allegory. That is to say, for every functor on functions, there exists at most one relator which agrees on functions with the original functor. We introduce relational coproduct and product and their corresponding relators. Relational coproduct is the real categorical coproduct whereas this is only the case for relational product if we take the restriction to functions. We also define the relational extension of the notion of a natural transformation between functors. And we define the notion of a relational catamorphism. Furthermore, we give the relational extension of the power functor. For all the relational extensions of categorical constructs we give in this section, it is the case that when we take the restriction to functions, we get the normal categorical constructions on functions. We argued that we wanted to move from functions to relations in order to increase our powers of expression. Furthermore, one can employ relations to specify nondeterministic problems. For instance, consider the case that we have a nondeterministic specification of a problem and we want to find a functional solution. In our calculation we can freely use relations in combination with the relational extension of categorical constructs. Now, if we know that all the components of our final derivation are functions 2 , the "program" can be "implemented" using the normal categorical constructions on the functions. The way we introduce the relational extensions of categorical concepts is inspired by the way it is done in the book by Bird and de Moor [7] although there they assume tabularity. Furthermore, for relational coproduct and relational catamorphisms, they assume the existence of power transpose and membership (see subsection 2.4.8).We will not assume tabularity in general nor the existence of power transpose and membership. 2 and these functions have a straightforward implementation 35 2.5. RELATIONAL EXTENSIONS 2.5.1 Relators We stal1 with the relational extension of a functor on functions, Definition 2.33 (Relator) A relator is a monotonic functor which commutes with converse. That is, let A and B be allegories. Then the mapping F : A f- B is a relator iff, FR . FS = F(R· 5) for each R : A f- Band 5 : B f- C, (2.34) Fid A = id FA for each object A, (2.35) FR (2.36) <:;;; FS {= R <:;;; 5 , (2.37) o Note that it is safe to write FRO which means either (FR)O or F(RO). If allegory B is tabular, a functor is monotonic iff it commutes with converse [7]. So, if we define a relator on a tabular allegory, one has to prove either requirement (2.36) or (2.37). However, since we do not assume tabularity, we prove both properties (2.36) and (2.37) whenever we define a relator. The interpretation of FR : FA f- FB, for R : A f- B, is that when we apply FR to F-structure x, the relation R is applied to all the elements of x. For instance, as we will see, the product and the list functors can be extended to relators. For the interpretation of relational product, we have (a, b) R x 5 (c , d) iff aRc and b 5 d. And for lists, x (ListR) y == #.x = #.y A \i(i:: Xi R yd In general, we have x FR y whenever F-structures x and y have the same shape and the corresponding elements of x and yare related by R. At the end of this section, we will formally prove that FR respects shapes. Preservation of functions In general, a functor between allegories does not preserve functions. However, using the extra requirements for a relator (2.36) and (2.37), it follows that relators preserve simple and total relations, hence, respect functions. For simple relation f, we have, hence, Ff is simple, andpreservation of totality follows from, for total relation R, FRO. FR = F(RO . R) :;2 Fid = id From the fact that a relator respects functions it follows that every relator F : A f - B is a functor between the corresponding sub-category of total functions, i.e. F : Map(A) f- Map (B). In other words, every relator is a relational extension of a functor on functions. If we assume tabulations, this extension is in some sense canonical. That is to say, for every functor on Map, there exists at most one relator which agrees on functions with the original functor. This is expressed by the following lemma: 36 CHAPTER 2. BASIC NOTIONS Lemma 2.38 Let F and G be two relators which agree on functions, i.e. Ff functions f. Then F = G. = Gf for all Proof Let R be an arbitrary relation with tabulation (f , g). Then D As we will see in subsection 2.5.2, the product and coproduct functor on functions can be extended to a relator. Some functors exist for which there is no relational extension. However, all regular functors have a relational extension. A precise condition is given in [40] for when a functor has a relational extension in the case that the allegory is tabular. Shapes Another corollary of the fact that relators respect functions, is that the relation FR respects shapes. That is to say, for R : A f- B, x FR 1J F!A' =} X = F!B . 1J . Recall that the shape of x E FA is defined as F!A . x E Fl. We calculate, for R : A points x E FA, 1J E FB, f- B, and x FR 1J { definition ~ x'1Jo FR monotonicity, R ~ x'1Jo ~ = !A'!B } F(!A' !B) F relator, shunting of functions } { F!A . 1T A,B X ~ F!B . 1J functions } F!A . X = F!B . 1J Thus if x and 1J are related by FR they have the same shape Note that not all functors on relations respect shape. An example is existential image E. Although E is a functor on relations, it is not monotonic, hence, it is not a relator. The shape of sets is given by E! which maps the empty set to the empty set and a non-empty set to the singleton set containing the unit element. Hence, the shape of a set, i.e. an E-structure, is the property whether or not the set is empty. Now, Ell = A(ll . E) = All is an example of a relation which does not preserve shapes: All maps every set to the empty set, hence, the shape of a non-empty set is not preserved. Domains We have proven that relators preserve total relations but we can do better. We can prove that relators commute with the domain operator. That is, F(R» = (FR». From the fact that a 37 2.5. RELATIONAL EXTENSIONS relator respects identities and is monotonic it follows that a relator respects partial identities too. Now, using the isomorphism between partial identities and conditions, it follows that relators commute with the domain operator if we show that, = ! ·F(R» !. (FR». Since! is a function and !FA = TT I,FA it follows that !FA = !n . F!A. Using this, we calculate, for R: B f-A, !FA . F(R» property above, F functor !n . F(!A . R» { domains (2.28): !A . R> = !B . R !n . F(!B' R) property above, F functor } domains (2.28) } !FA . (FR» Hence, relators commute with the domain operator. Since relators commute with converse it follows that relators commute with the range operator as well. Thus it is safe to write FR> meaning either F(R» or (FR». 2.5.2 Coproduct and product In this section we introduce relational coproduct and product and define the corresponding relators. We begin with relational coproduct because this one is easier. Relational coproduct will be the real categorical coproduct on relations. We try to define relational product more or less as the dual of relational coproduct. Relational product will not be a real categorical product. However, if we take the restriction to total functions, relational product is a real categorical product. So, not only is the product relator the relational extension of the product functor on the sub-category Map, for relational product we also have in Map the usual universal property for products. This holds for coproduct too. In other words, the definition of relational coproduct and product are in some sense canonical extensions of the categorical definitions for total functions. The way we introduce coproduct and product is inspired by [17] and [7], although we do not assume tabulations for every relation. In [1] relational coproduct and product are introduced in a different but equivalent way. Coproducts A coproduct of two objects consists of an object and two injection relations. The object is denotedbyA+BandthetworelationsbyinIA,B: A+Bf-AandinrA,B: A+Bf-B. For 38 CHAPTER 2. BASIC NOTIONS the injection relations we require that inl~,B . inlA,B = idA and inr~,B' 'B,~lni (2.39) inrA,B = idB (2.40) inrA,B = ..LlA,B and (inIA,B' )B,~lni U (inrA,B' )B,~rni = id A+B (2.41) Equation (2.39) expresses that inl and inr are injective and total; from (2.41) it follows that they are simple too, hence, they are injective functions. Furthermore, from equation (2.40) it follows that inl and inr have disjoint ranges but from equation (2.41) it follows that the union of both ranges is id A + B . Having the functions inl and inr, we can define the june operator, for R : C f- A and S : C f- B, R\7S ~ )B,~lni·R( U (S·inr~,B) (2.42) : Cf-A+B . Since junc is defined as a union and inl and inr are functions, it follows that we have the following universal property. For all R : C f- A, S : C f- B and X : C f- A + B, (2.43) R <;;; X· inlA,B /\ S <;;; X· inrA,B since for the rhs of above equation we have R <;;; X· inl/\ S <;;; X· inr inl and inr are functions, shunting } { R· inlo <;;; X /\ S . inr <;;; X { universal property of union } R· inlo uS· inro <;;; X Hence, the universal property (2.43) follows from indirect equality. As a matter of fact, R \7 S could be defined by the universal property (2.43) instead of the closed formula (2.42). The universal property (2.43) expresses that R \7 S is the smallest relation for which the rhs holds. Specifically, we have the following computation rules R <;;; R\7S . inlA,B /\ S <;;; R\7S . inrA,B However, we can do better. Using requirement (2.39) and (2.40) it follows that (2.44) R = R\7S . inlA,B /\ S = R\7S . InrA,B since, for instance, R\7S . inl = (R· inlo uS· inrO) . inl = R· inlo . inl uS· inro . inl = R Using this fact, we prove that we have a dual universal property for coproduct: that is to say, the universal property (2.43) with the inclusion the other way around. To begin, since composition distributes over union it follows from (2.41) that for X : C f- A + B, X = X·inl·inlo U X·inr· inro = (X·inl)\7(X·inr) . (2.45) 2.5. RELATIONAL EXTENSIONS 39 Instantiating X := R 'V Sand R := X· inl and S := X· inr in the universal property (2.43) yields Now, using fact (2.45) and the computation rules (2.44) it follows that we have the universal property: X· inlA,B ~ R /\ X· inrA,B ~ S (2.46) In other words, R'V S is the largest relation for which the rhs of (2.46) holds, This suggests that R'V S can be expressed as an intersection: X· inl ~ R /\ X· inr ~ R/inl/\ X { X ~ S factors} { X ~ ~ S/inr intersection R/inl n S/inr Hence, using indirect equality it follows from (2.46) that R 'V S = R/inl n S/inr (2.47) Combining universal properties (2.43) and (2.46), it follows that (A + B , inlA,B , inrA,B) is a real categorical coproduct, i.e. we have the universal property X· inlA,B = R /\ X· inrA,B = S (2.48) As a matter of fact, we could have defined coproduct equivalently starting with the above universal property and deriving properties (2.39) - (2.41) from it [17]. However, since this is not possible for product and we want to see more or less the dual between coproduct and product, we have chosen not to do so. Since relational coproduct is a real categorical coproduct it follows that, for R : A f- C and S: Bf-D, R+S ~ (inIA,B·R)'V(inrA,B·S) : A+Bf-C+D , defines a functor. Furthermore, we have the properties that and Since "'V" is monotonic, it follows that "+" is monotonic too. Furthermore, it is easy to verify that the operator "+" commutes with converse, hence, "+" is a relator. From the fact that "+" is a relator, it follows that "+" respects functions. Furthermore, f'V 9 = (f + g) . (id 'V id) is a function, if id 'V id is a function too. That id 'V id is indeed a function is easy to verify using properties (2.39) - (2.41). Hence, relation coproduct is not only a real categorical coproduct for an allegory, it is also a coproduct for the sub-category Map. 40 CHAPTER 2. BASIC NOTIONS Product A product of two objects consists of an object and two projection arrows. The object is denoted by A x B and the two arrows by outlA,B ; A f- A x Band outrA,B ; B f- A x B. For the arrows we require them to be functions and that = TTA,B outlA,B . outr~,B (2.49) and B,~ltuo . outlA,B n outr~,B' outrA,B = idA x B (2.50) In other words, we require (outl , outr) to be a tabulation of TT. Furthermore, recalling that for a unitary allegory each top exists, we conclude that a unitary tabular allegory has relational products. Note, however, that for the existence of relational product, we do not need to assume the existence of tabulations for every relation. Equation (2.49) expresses that all elements a and b can be paired, and equation (2.50) expresses that such a pairing is unique. Having the projection functions outl and outr, we can define the split operator on relations,for R ; A f- C and S ; B f- C, (2.51) Since split is defined as an intersection and outl and outr are functions, it follows that we have the following universal property, for all X : A x B f- C, R ; A f- C and S : B f- C outIA,B' X ~ R 1\ outrA,B' X ~ S (2.52) In detail, the proof of (2.52) is as follows. We have for the rhs of above equation outl . X R 1\ outr . X ~ ~ S outl, outr functions, shunting } X ~ outlO . R 1\ X { X ~ ~ outre . S universal property intersection } outlO . R n outre . S Hence, the universal property (2.52) follows from indirect equality. As a matter of fact, R e,. S could be defined by the universal property (2.52) instead of the closed formula (2.51). The universal property (2.52) expresses that R e,. S is the largest relation for which the rhs holds. Specifically, we have the following computation rules outl . R e,. S ~ R 1\ outr . R e,. S ~ S For the coproduct, we have computation rules (2.44) with equality. One can not expect equality for product as well. For instance, from the definition of "e,." it follows that R e,...li = ..li, hence, outl . R e,...li = ..li. This means that relational product is not a categorical product. Although in general we do not have computation rules with equality, we do have equality if we add a domain restriction. That is to say, we have the following rules outl . R e,. S = R . S> 1\ outr . R e,. S = S . R> (2.53) 41 2.5. RELATIONAL EXTENSIONS since, for instance, using the modular identity and the fact that outl is a function outl· (outlo. R n outro. S) = R n outl· outro. S = R nIT· S = R . S> So, if we take the restriction to total relations, we have computation rules with equality. That is, for total relations Rand S, outl . R"'S = R !\ outr . R"'S =S (2.54) For product, the dual of property (2.45), i.e. for X X = A x B f- C, (outl· X)"' (outr· X) does not hold in general since split is defined as an intersection and arbitrary relations do not distribute over intersection. However, if X is simple it follows from property (2.50) that the dual of property (2.45) does hold. That is, X = (2.55) (outl· X) "' (outr . X) if X is simple, since using property (2.50) and the fact that simple X distribute over intersection, X = (outlo. outl n outro. outr) . X = outlo. outl . X n outro. outr . X (outl . X) "' (outr· X) For coproduct, the computation rules with equality (2.44) together with property (2.45) were used to prove a dual universal property for coproduct. Hence, if we restrict our attention to total and simple relations, i.e. functions, we can prove in the same way as for coproduct the dual universal property for product using properties (2.54) and (2.55). For functions f, 9 and h, instantiating X ;= f"' 9 and R ;= outl . hand S := outr . h in the universal property (2.52) yields f"'g ~ outl . f"' 9 (outl·h)"'(outr·h) ~ outl . h !\ outr . f"' 9 ~ outr' h Now, using property (2.55), i.e. h = (outl· h)"' (outr· h), and the computation rules (2.53) for f"' g, it follows that we have the universal property, f"'g ~ h f ~ outl . h !\ 9 ~ outr . h (2.56) Combining both universal properties (2.52) and (2.56), we have for functions f, 9 and h, the universal property f"'g =h f = outlA,B' h !\ 9 = outrA,B' h (2.57) Many properties for the relational coproduct follow from the fact that the relational coproduct is a real categorical coproduct. Since this is not the case for relational product, we have to prove some of its properties using requirements (2.49) and (2.50) and the definition of split (2.51). Since split is defined as an intersection, split inherits properties of intersection. For instance, we have the distribution rules (R",S)·T~ (R·T)"'(S·T), (2.58) 42 CHAPTER 2. BASIC NOTIONS = (R'" 5) . f (R· f) '" (5· f) if f is simple. (2.59) And the modular law and modular identity R"'(S·T) <;;; UR·r)"'S)·T (2.60) I = ((R·fO)"'S)·f iffiscosimple. R"'(S·f) (2.61) Next we want to show that, although relational product is not a categorical product, Rx 5 ~ (R· outlA,B) '" (S· outrA,B) for R : C f- A and 5: 0 f- B still defines a binary relator. The most difficult part is to prove that" x" distributes over composition. It follows from the following property: (2.62) We first prove this property for the case that one of the arguments is a function. For the moment assume that U is a function. We calculate: (R"'S)o·T"'U c definition split, distribution over intersection } RO . outl . T '" U n S°· outr . T'" U c computation rules outl, outr } RO. T n S°· U computation outl, U total RO . outl . T'" U n S°· U modular law } C (RO . outl n So. U· (T'" U)O) . T '" U C { (RO . outl C definition split: (T'" U)O <;;; uo . outr } n S°· U· Uo . outr) . T '" U U simple, definition split } (R"'S)o·T"'U So, property (2.62) holds if U is a function. Similarly, this property also holds ifT is a function by duality between left and right, and if either R or 5 is a function by taking converses. Using property (2.62) if at least one of the arguments is a function, we can prove property (2.62) for arbitrary relations: definition split (Ro·outl n SO·outr)· T",U 43 2.5. RELATIONAL EXTENSIONS outl function, property above } (R 6 id)0. outI 6 (5°·outr)· T6U { definition split: outl 6 (5° . outr) = (outl6 (5· outr))O } (R 6 id)0. (outI 6 (5·outrW· T6U outl function, property above } (R6 id)o. (outlo. Tn outrO' 5°· U) outl° . T n outrO . 5° . U = T 6 (5° . U) id function, property above } RO. Tn 5°· U Having the notion of cosplit, denoted by"..." as the converse-conjugate of split, i.e. property (2.62) can be rewritten as (2.63) R... 5·T6U=R·Tn5·U We call property (2.63), cosplit-split elimination. As a matter of fact, we could have defined relational product equivalently using equation (2.62) together with equation (2.50). It is then possible to derive property (2.49) from it [1]. Although this is easier than to derive property (2.63) from our definition of relational product, we have chosen not to do so. The reason for this is that we wanted to show that a unitary tabular allegory has relational products. From property (2.62) the so-called product-splitfusion rule follows: Rx5· (T6U) = (R·T)6(5·U) since R x 5 = ((RO . outl) 6 (5° . outrWand outlo. R . T n outrO . 5 . U = (R· T) 6 (5 . U). Using the product-split fusion rule it is easy to prove that "x" distributes over composition. The requirement that" x" preserves identities is precisely property (2.50), hence, "x" is a functor. Furthermore, it is easy to verify that "x" is monotonic and commutes with converse, hence, "x" is a relator. From the fact that "x" is a relator, it follows that "x" respects functions. Furthermore, f 6 9 = f x 9 . id 6 id is a function if id 6 id is a function too. This is easy to prove: simplicity follows from requirement (2.50): id 6 id . (id 6 id)O = (outlO n outrO). (outl n outr) ~ outl°. outl n outro. outr = id Totality follows from (2.27) and requirement (2.49): (id 6 id» = (outlO n outrO» = id n outl· outr O= id So, although the relational product is not a categorical product, (A x B ,outIA,B ,outrA,B) is a real categorical product for functions, i.e. for the sub-category Map. 44 CHAPTER 2. BASIC NOTIONS We have proven that R v S = R/inl n S/inr and since for function f we have f O. R = f\R, it follows that R 6 S = outlO . R n outre . S = outl\R n outr\S . (2.64) Hence, since (R/S)O = W\SO, it follows that "v" and "6" are in some sense converse duals of each other. This fact we exploit in chapter 4 on membership. 2.5.3 Natural transformations Since relators are by definition functors it follows that the standard definition of a natural transformation between relators makes sense. That is to say, a collection of relations lX indexed by objects (equivalently, a mapping lX of objects to relations) is a natural transformation of type F f- G, for relators F and G iff FR . lXB = lXA . GR for each R : A f- B. Note that we have the normal typing rules for natural transformations: HlX: HFf-HG {= lX:Ff-G (2.65) lXH : FH f- GH {= lX: F f- G (2.66) and However, many collections of relations we will encounter are not natural with equality but with an inclusion. That is why we define two other types of natural transformation denoted by F t----> G and F'----1 G, respectively. We have: ::2 lXA . GR for each R : A f- B. lX : F f-' G ~ FR· lXB lX : F'----1 G ~ FR· lXB <;;; lXA . GR for each R : A f- B. and Note that lX : F f-' G == lXo : F'----1 G and lX : F f- G == lX: F t----> G 1\ lX : F'----1 G. Since natural transformations of type F t----> G are more common than ones of type F f- G, we say that lX is a natural transformation if a : F f-' G and we say that lX is a proper natural transformation if lX : F f- G. If lX : F'----1 G then a is a conatural transformation. Typing rules For natural transformations we still have the same typing rules as for proper natural transformations. That is, (2.67) and (2.68) lXH : FH t----> GH {= lX: F t----> G Property (2.68) is trivial to verify, property (2.67) follows from monotonicity of relator H : HFR . HlXB = H(FR· aB) ::2 H(aA . GR) = HlXA . HGR 2.5. RELATIONAL EXTENSIONS Interpretation of natural transformations lX: F f--' G, i.e, FR· lXB "2 lXA . 45 The interpretation of a natural transformation GR for each R: Af- B, (2.69) is that lXB takes a G-structure containing elements of type B and transforms it into an F-structure without changing the value of the elements. That is to say, lXB can throwaway or duplicate elements of the G-structure but it can not invent new values. In chapter 4 we give a formal justification for this interpretation. Consider the case that lX only duplicates elements. Then for equation (2.69), if R is nondeterministic, the lhs first applies R to an element and duplicates the nondeterministically chosen element. In contrast, the rhs first duplicates the element and then applies R to both copies. Hence, the rhs is more nondeterministic than the lhs. An example of a natural transformation which duplicates elements is fork A ~ idA 6. idA : A x A f- A, i.e. we have R x R . fork "2 fork· R Now, take R = (+ 1) u (-1), i.e. R nondeterministically increments or decrements a natural number. Then if we apply fork· R to 0, we get either the pair (+ 1 ,+ 1) or the pair (-1 , - 1). On the other hand, if we apply R x R . fork to 0, we get one of the four possibilities (+ 1 ,+ 1), (- 1 ,+ 1), (+ 1 ,-1) or (-1 , - 1). Indeed, if we take the restriction to deterministic relations, i.e. simple relations, the inclusion becomes an equality: f x f . fork = fork· f for simple f. Now, consider the case that lX only throws elements away. Then for equation (2.69), if R is not total, the lhs is not defined on G-structures which contain elements on which R is not defined. On the other hand, the rhs is defined on G-structures containing elements which are either thrown away by lX, or are kept by lX and are in the domain of R. Hence, the rhs is more defined than the lhs. An example of a natural transformation which throws elements away is projection, i.e. we have R . outl "2 outl . R x R Now, take R = (0 <) i.e. R is the partial identity on natural numbers which is only defined on the positive numbers. Then outl . R x R is not defined on the pair (+ 1 , - 1), whereas R . outl yields (+ 1) when applied to (+ 1 , - 1). Indeed, if we take the restriction to relations which are defined everywhere, i.e. total relations, we get equality: R . outl = outl . R x R for total R. An arbitrary natural transformation can throwaway some elements and at the same time duplicate some other elements. The combination of both examples suggests that for simple and total relations, i.e. functions, we have equality for arbitrary natural transformations. That is to say, we have the following lemma: If lX : F f--' G then, Lemma 2.70 Ff . lXB = lXA . Gf for each function f : A f- B. 46 CHAPTER 2. BASIC NOTIONS Proof The;;:J part follows from the definition of F f-' G. For the remaining inclusion we calculate: Ff . lX ~ lX . Gf { relators respect functions, shunting } lX . Gfo Ffo . lX ~ { Gf . lXo converse ~ lXo . Ff lX : F f-' G == lXo : G '--1 F } true o If the allegory is tabular, the other way around is also true: Lemma 2.71 lX: F f-' G {= (Ff· lXB = lXA . Gf for each function f : A f- B) . Proof Assume that Ff . lX = lX . Gf for each function f. Let (f , g) be a tabulation of R, then FR . lX ;;:J lX . GR { (f, g) tabulates R, F and G relators } Ff . Fgo . lX ;;:J lX' Gf . Ggo assumption: lX . Gf = Ff . lX } Ff . Fgo . lX ;;:J Ff . lX' Ggo monotonicity } Fgo . lX ;;:J lX . Ggo { relators respect functions, shunting } lX' Gg ;;:J Fg . lX assumption } true o Recall that relators respect functions, i.e. relators are functors on the sub-category Map. From the combination of both lemmas, it follows that for afunctional collection of arrows lX, we have, lX: F f-' G in A == lX: F f- Gin Map(A) for tabular allegory A. In this way, the notion of F f-' G for relations is the natural 3 generalization of the notion of a natural transformation for functions. 3Pun intended 2.5. RELATIONAL EXTENSIONS 2.5.4 47 Power relator From the definition of existential image, i.e. ER = A(R· E), it follows that ER is a function for every R. Hence, E is a functor on functions, i.e. E is a functor on the sub-category Map. However, E is not monotonic, nor does it commute with converse. So, E is not a relator. However, in [7] it is shown that for a tabular allegory, the restriction of E to functions does have a relational extension. This relational extension, called the power relator, we denote by P. In [7], Bird and De Moor give a closed formula for P, i.e. (2.72) but in order to show that this defines a relator, tabularity is assumed. Since P is a relational extension it follows that for all tabulations (f ,g) of relation R, PR = Ef· (Egt PR = (2.73) since P(f·gO) = Pf· (Pgt = Ef· (Egt In this thesis, we use only equation (2.73). So, although we do not assume tabularity in general, for results which mention the power relator, tabularity is assumed. From the closed formula (2.72) the following interpretation for PR follows. For sets x and y, xPRy == V(UEX:::3(bEy::uRb))/\V(bEy:::3(UEx::uRb)) 2.5.5 Recursive datatypes In this subsection we define the relational extension of a tree type functor. Since tree type functors are defined using catamorphisms we first define the relational extension of a catamorphism. Relational catamorphisms Recall that an allegory is a category and a relator is a functor on relations. We define the relational catamorphism as the normal categorical notion of a catarnorphism in the category of relations. So, suppose that relation in : A f- FA is an initial algebra. That is to say, for a relation R : B f- fB, i.e. R is an F-algebra, there exists a unique F-homomorphism to R from in. We denote this unique homomorphism by ~RD. In other words, ~RD is characterized by the universal property, called the unique extension property, that for each relation X : B f- A, X = ~RD == X· in = R· FX (2.74) If the allegory is a power allegory it can be shown that a relational initial F-algebra coincide with the initial f-algebra for the sub-category of total functions. Since relational catamorphism is the standard notion of catamorphism defined on relations, it follows that an initial algebra is an isomorphism. Recall that a relation is an isomorphism 48 CHAPTER 2. BASIC NOTIONS iff it is a bijection. That is to say, for in we have, ina . in the unique extension property (2.74) can be rewritten as = Fid and in . ina = id. Using this, x = QRD == X = R . FX . ina In other words, QRD is the unique, so also the least and greatest, fixed-point of the mapping AX. (R . FX . ina). Since this mapping is monotonic, it follows from Knaster-Tarski that we have, (2.75) and X :2 QRD {= X:2 R . FX . ina . (2.76) For relational catamorphisms we have the normal fusion rule. Furthermore, the equations (2.75) and (2.76) imply two other fusion rules with inclusion. That is to say, we have, R· QTD = QSD {= R· T = S . FR , (2.77) R· [TD c:;; QSD {= R· T c- S . FR (2.78) R· [TD :2 QSD {= R· T :2 S . FR , (2.79) Application of the rules (2.77) - (2.79) will be indicated by the hint "fusion". Note that from (2.78) or (2.79) it follows by taking R := id that the catamorphism operator [-D is monotonic. So, relational catamorphisms are defined using the standard categorical construction in the category of relations. However, the notion of a relational catamorphism is an extension of the notion of a catamorphism on functions, since the mapping [-D respects functions. Simplicity of [fD for simple f follows from, Df~ . [fDa c:;; id { factors} QfD c:;; id/[fr {= { property (2.76) } f . F(id/[fn . ina c:;; id/[fr { factors} f . F(id/Qfn . ina . [fr c:;; id { computation, F functor f . F(id/QfDo . QW) . fO c:;; id { factors, f simple } true That the operator U respects total relations, i.e. [Rr· [RD :2 idA for total R, is not difficult to verify using fusion law (2.79) since idA = QinD. 2.5. RELATIONAL EXTENSIONS 49 Tree type relators Since relational catamorphism is the standard notion of a catamorphism on relations, the relational extension of a type functor suggests itself. We take the same definition as for functions. That is, let 0 be a binary relator and let, for each A, inA : TA ~ A ® TA be an initial algebra of (A®). Then for R ; A ~ B, TR = QAI8I; inB . R0id TBD defines a relator, the tree type relator induced by 181. From subsection 2.2.5 it follows that T is a functor. That T commutes with converse follows from, (TR)O = T(RO) { (TR)o. in definition T, unique extension = in . ROl8lid . idl8l(TR)O converse, ina . TR { TR· in = @ relator } id 0 TR . R0id . ina in isomorphism, 181 functor in . Rl8lid . id0TR definition T } true Finally, monotonicity ofT follows from monotonicity of catamorphism. Hence, T is a relator. Cocatamorphism We define the notion of a cocatamorphism as the converse-conjugate of a catamorphism. That is, for coalgebra R ; FA ~ A, we define We call the mapping K-] a cocatamorphism instead of an anamorphism as done in [1]. An anamorphism [36] is by definition the dual of catamorphism. The reason why we prefer the term cocatamorphism instead of anamorphism is because K-] is not the relational extension of the anamorphism operator on functions. For instance, K_] does not respect functions in general. Chapter 3 Calculational Preliminaries In this chapter we introduce some additional, non-standard concepts which we need in the remainder of this thesis. We start with the introduction of the notion of a slok property. A slok property is a kind of healthiness condition on natural transformations. A naturality requirement does not characterize a natural transformation uniquely whereas, under some conditions, a slok property uniquely characterizes a natural transformation. This fact we exploit in Chapter 5. Here we show that slok properties are preserved by composing natural transformations, and by composing functors and natural transformations. This is done by defining a slok property as a property of an arrow of the slok category and investigating the typing rules of this category. In the second half of this chapter we introduce the so-called 'f-Li calculus. This calculus provides us with a variable-free way of constructing and calculating with relators which take a vector of arguments and/or give as result a vector of arguments. 3.1 Category "Slok" Natural transformations and isomorphisms in the literature are often introduced by just giving their type. In general, however, natural transformations are not uniquely defined by their type. So a phraseology like "the obvious natural transformation" or the "canonical natural transformation" is used, hereby avoiding the need to give the (well-known) closed formula. An example is the "obvious natural isomorphism" swa p : B x A f - A x B The intended natural transformation is: swap = outr 6. out!. Furthermore, one assumes that the "obvious natural isomorphism" has some well-known properties, for instance swap has the property that swa PA, B . f 6. 9 for all f : A f- C and 9 : B f- C. (3.1) and indeed this property can be proven for the formula given above. We call a property like (3.1), a slok property, "slok" being the dutch word for "to gobble". We consider a slok property as a healthiness property of a natural transformation. That is to say, a specific arrow should not only be a natural transformation (or even a natural isomorphism) but it should also 51 52 CHAPTER 3. CALCULATIONAL PRELIMINARIES satisfy a certain slok property. In some categories there are many natural isomorphisms of type B x A (- A x B but there exists only one natural transformation which has slok property (3.1). In this section, we define the category Siok. An arrow in this category is a collection of arrows in the base category which has a slok property. We give typing rules for this category: we give the slok properties of several combinations of arrows from category Siok. Under certain conditions, a slok property has at most one solution. Furthermore, we give a condition under which such a unique arrow in category Siok is a natural transformation i.e. an arrow in category Fun. So, under certain conditions, if one has to construct a natural transformation which should satisfy a corresponding slok property then the slok property predicts the only possible candidate. Having verified that this candidate indeed satisfies the slok property it then follows that the collection of arrows is also a natural transformation. Definition of Siok A mapping to C-arrows from the collection of B-arrows is an object of category Siok if it behaves nicely with respect to typing: mapping F is an object if the target of Ff, for all f, only depends on the target of f, i.e. (Ff)<1 = (Fg)<1 f<1 = g<1 for all f and g. (3.2) Recall that "_<1" denotes the target operator. That is, f<1 = A if f : A (- B. Note that we do not assume that the collection of B-arrows form a category. From (3.2) it follows that mapping F induces a mapping on objects, say F", such that (Ff)<1 = F"f<1. Specifically, F"A = (Fid A )<1, or equivalently, F" = <1oFoid . Examples of objects of Siok are functors; for functor F, the mapping F" is the object mapping of F. Another example is the composition of a functor and a natural transformation, e.g. if a is a natural transformation F (- G, then the mapping Hf ~ Ff· a (= a· Gf) satisfies condition (3.2): H" is again the object mapping of F. Note that the objects of category Siok are closed under function composition; (FG)" is F"G". Recalling (3.1), the mapping "6" is an object of Siok since it is the composition of the product functor and the natural transformation id 6 id. We now define the arrows of Siok in such a way that swap is such an arrow. The arrows between the objects F and G, both mappings to C-arrows from B-arrows, are collections of C-arrows indexed by the objects of B with a so-called slok property i.e. for collection a, a : F (- G iff Ff = aA . Gf for each f : A (- B. (3.3) Note the resemblance with the definition of a natural transformation: with a natural transformation a appears on both sides; with a slok property a is "gobbled up" when being composed with Gf. This explains the term "slok": "slok" being the dutch word for "to gobble". Notice that aA : (FfJ<1 (- (Gf)<1 for each f : A (- B, 53 3.1. CATEGORY "SLOK" or equivalently, since F and G satisfy condition (3.2), CXA : F"A f- G"A for all objects A. The composition of collections of arrows cx : F f- G and 13 : G f- H is defined in the usual way: (cx·I3)A = CXA ·I3A. Then, cx'l3: Ff- H since CXA . I3A . Hf = CXA' Gf = Ffforeach f : A f- B. That the above definition indeed defines a category is easily verified: associativity of composition is immediate from the definition and the identity arrow id F on object F is (idF)A ~ idF"A. Before we state some typing rules, we define two subclasses of objects of the category Siok. Definition 3.4 Ff . G9 Mapping F is fuseable into mapping G iff = G (f . g) for all f and g. o Definition 3.5 Mapping F is recoverable from mapping G with the collection of arrows 13 iff Ff = G (f . 13 B) for each f : A f- B. We say that F is recoverable from G if there exists a collection of arrows with which F can be recovered. o Trivially, a functor is fuseable into itself, and if mapping F is fuseable into mapping Hand mapping G is fuseable into I then FG is fuseable into HI since FGf· HIg = H(Gf . Ig) = HI(f· g) . Similarly, a functor is recoverable from itself with id and if mapping F is recoverable from mapping G with 13 then HF is recoverable from H G with 13 since H Ff = H G (f . 13). A property we use very often is that if functor F is fuseable into mapping G then F is recoverable from G with 13 iff Fid = G f3 since Ff = Ff . Fid = Ff . G f3 = G (f . f3) As an example, product is fusable into split and product is recoverable from split with the pair (outl ,outr). 3.1.1 Typing rules Next we state the typing rules for category Siok. We start with an unconditional one; for all H, CXH< : FH f- GH {= where (CXH")A = CXH"A, since CXH<A . FHf ping H' is fuseable into mapping H, then H'cx:HFf-HG {= (3.6) cx : F f- G cx:Ff-G = GHf for each f : A f- B . Furthermore, if map(3.7) 54 CHAPTER 3. CALCULATIONAL PRELIMINARIES where (H'cxlA = H'(CXA), since H'cxA . HGf = H(CXA . Gf) = Hff for each f: A ~ Now, since a functor is fuseable into itself, it follows that for every functor H, Hcx : HF~ HG ~ cx: ~F G . B. (3.8) Combining the rules (3.6) and (3.7) we get, if mapping G ' is fuseable into mapping G, then CXH" . G 1 ~ : since CXH" : FH ~ ping F, then F'~ CXIO: sinceF'~: FH ~ GH and FH~GI ~ GI G'~ cx : F ~ : GH ~ ~ FH~ IandcxI": G 1\ ~ : H ~ I (3.9) GI. Similarly, if mapping F ' is fuseable into map- cx:F~GI\ H (3.10) FI~G . The reader is encouraged to compare the typing rules for category Siok with the typing rules known for category Fun, i.e. the category with functors as objects and natural transformations as arrows between them. For instance, vertical composition of natural transformations, i.e. composition in category Fun, corresponds to composition in the category Slok, horizontal composition of natural transformations corresponds to the rule (3.9) or (3.10). 3.1.2 Uniqueness and naturality Having the notion of recoverability, we can express a rule which states the uniqueness of arrows in category Slok: Lemma 3.11 If cx : F ~ G and there exists afunctor that is recoverable from G with cx = F ~. Hence, cx is the unique arrow of type F ~ G. Proof Assume it is functor G' that is recoverable from G with G'id A = G ~A, cx:F~G ~, ~ then then functors preserve identity arrows. } } D If we also assume that functor F' is fuseable into mapping F, we get our main lemma: G and functor F' is fuseable into F and functor G 1 is recoverable Lemma 3.12 If cx : F ~ G I. from G then cx is a natural transformation of type F1 ~ Proof Assume functor G 1 is recoverable from G with .~ From lemma (3.11) it then follows For naturality of cx, we calculate, for each f : A ~ B, that CXA = F~A' 3.1. CATEGORY "SLOK" 55 (XA . G'f G'f (XA . G(f· = G(f· ~B) } ~B) { (X:Ff-G { F ' is fuseable into F } F(f'~B) = F'f . F~B { (XB = F~B } F'f . (XB D The above lemma shows that, under certain conditions, an arrow in the category Siok is an arrow in the category Fun, i.e. a natural transformation. For instance, the only possible candidate which has slok property (XA, B . f L. 9 for all f : A f- C and 9 : B f- C. (3.13) is given by lemma (3.11): (XA, B = outrA, B L. outlA, B, i.e. swap, since product is recoverable B = (outIA, B ,outrA, B)' Having verified that this candidate indeed satisfies from "L." with ~A, property (3.13) it follows from lemma (3.12) that (XA,B is a natural transformation. Lemma (3.11) may viewed as a way of predicting (X such that (X : F f- G. If a functor is recoverable from G with ~ then the only candidate for (X is .~F The lemma, however, does not guarantee that F~ has the slok property F f- G. 3.1.3 Category CoSlok In Chapter 5, we need the opposite category ofSlok as well; we denote this category by CoSlok. For reference purposes, we state the definition of category CoS10k and its dual properties explicitly. A mapping F to C from B is an object of the category CoSlok if the source ofFf, for all f, only depends on the source of f, i.e. (Ffle> = (Fg)e> (3.14) fe> = ge> for all f and g. Recall that "J>" denotes the source operator. That is, fe> = B if f : A f- B. From (3.14) it follows that mapping F induces a mapping on objects, say P', such that (Ff)e> = P'fe> i.e. P'A = (FidA)e>, or similarly, F" = e>oFoid . The arrows between the objects F and G, both mappings to C-arrows from B-arrows, are collections of arrows in C indexed by objects of B with a so-called coslok property i.e. for collection (x, (X : F f- G iff Ff· (XA = Gf for each f: Af- B. For category CoSlok we have the dual notions of fuseable and recoverable: (3.15) CHAPTER 3. CALCULATIONAL PRELIMINARIES 56 Definition 3.16 Ff . G9 Mapping G is cofuseable into mapping F iff = F (f . g) for all f and g. o and Definition 3.17 Mapping F is corecoverable from mapping G with the collection of arrows f3 iffFf = G(f3A' f) for each f : A f- 8. o For CoSlok we have the dual typing rules of (3.6), (3.7) and (3.8). For all mappings H, we have, ~HX< : FHf- GH {= <x:Ff-G (3.18) If mapping H' is cofuseable into mapping H, then H'<x:HFf-HG {= <x:Ff-G (3.19) <x:Ff-G. (3.20) For every functor H, H<X: HFf-HG {= Similarly, we have the duals of lemma's (3.11) and (3.12): Lemma 3.21 If <X : F f- G and there exists afunctor that is corecoverable from F with f3 then <X = Gf3. Hence, <X is the unique arrow of type F f- G. o and Lemma 3.22 If <X: F ~ G and functor F' is corecoverable from F and functor G' is cofuseable into G then <X is a natural transformation of type F' ~ G'. o 3.2 The 'T-~ calculus In this section we develop the tools to define and calculate with functors without the use of variables. Often functors (datatypes) that occur in programming are single-valued, but in general they are not. For instance, multi-valued, or vector-valued, functors occur in the definition of single-valued functors. The construct FI8lG is an example of such a definition: the (singlevalued) functor FiSlG is the composition of the functor I8l after the (pair-valued) split (F , G) defined by (F , G) f = (Ff , Gf). A more complicated example is the binary functor which maps the pair (f ,g) to f + 9 x g. We want to be able to define such functors without mentioning the arguments explicitly. We identify a number of constructs with which we can construct these functors. The example of the binary functor, for instance, is constructed using projection as well as repetition (split), product and coproduct. Normally, one is not usually aware 3.2. THE '[-<1 CALCULUS 57 of the use of multi-valued functors and projections because their use is camouflaged by the use of variables. Having identified these constructs, we can give inductive definitions on the whole class of definable functors by giving a clause for each of the constructs. The purpose of this section is thus to develop the tools to define and calculate with functors in a variablefree way. This is achieved by introducing mechanisms for tupling of arbitrary variables and taking projections. 3.2.1 Arbitrary products of a category For two categories C and V we can define the so-called product category C x V. The arrows and the objects are pairs of arrows and objects, respectively, of C and D. Composition is defined componentwise: (f,g)·(h.,k) = (f·h.,g·k). The identity on the object (A, B) is the pair (idA, ids). Furthermore, we have two projection functors Outl and Outr defined by Outl(f, g) =f /\ Outr(f, g) = 9 . Note that if categories C and V are allegories then so is C x D. All operations on allegory C x V are defined componentwise. For instance, the union of (R, 5) and (T, U) is (R u T , 5 U U) and the converse of (R ,5) is (RO ,50). Furthermore, Outl and Outr are relators. Next we generalize the notion of a binary product of categories, to a k-fold product of a category. In the remainder of this thesis we only need vectors of arrows of the same category. So, Ck denotes the kth power of category C. Every object of Ck is a vector of k objects Ai, o S i < k, of C and every arrow of Ck is a vector of k arrows f i , 0 S i < k, of C. We denote these vectors by <1(i: 0 S i < k: Ad and <1( i: 0 S i < k: fd, respectively. Composition in Ck we define componentwise, that is to say, <1(i:osi<k:fd' <1(i:OSi<k:gil = <1(i:OSi<k:fi'gil (3.23) and the identity arrow on <1(i: 0 S i < k: Ad is <1(i: 0 S i < k: idA;)' In other words, we assume that the mapping <1(i: 0 S i < k: _) is a functor. Furthermore, we assume the existence of k projections Proji : C f- Ck , 0 S i < k, such that we have the following universal property, for all k arrows f i , 0 S i < k, and each arrow 9 fromC k , 9 = <1(i: 0 S i < k: fd \f(i: 0 S i < k: Projdg) = fil . (3.24) From now on we abbreviate <1(i: 0 S i < k: fil to <1k fk, and similarly <1(i: 0 S i < k: Ail to <1 k A k . Other vector notations that are sometimes used in the literature are the boldface notation, f, or the notation We have chosen for the notation <1 k f k because in the remainder of this thesis we consider vectors of vectors, i.e. matrices, and this notation shows explicitly how the different variables are being bound. For instance, we consider matrices like <1 k <1 l h, l and <1 l <1k f k , l. Furthermore, the notation <1 k f k enables us to consider the properties of the mapping <1k on its own. r 58 CHAPTER 3. CALCULATIONAL PRELIMINARIES Just as we suppressed the need for the introduction of an index variable in the notation ~kfb we abbreviate \f(i: 0 ::::: i < k: Proj;(g) = fil to Projdg) = f k. In general, we adopt the convention that all free occurrences of variable k within a formula are bound by a universal quantification with the smallest possible scope. So, the universal property (3.24) is abbreviated to Proh(g) = fk . (3.25) From (3.25) follow the following two cancellation rules, Now, since ~k is a functor it follows that Projk is a functor too: A special instance of ~k we use throughout the remainder of this thesis is k~ f, i.e. k~ applied to an argument which does not depend on k. So, L\k f is a vector containing k copies of f. If it is clear from the context what the value of k is, we drop the subscript k. So, we write L\f instead of L\d. Note that the universal property (3.25) for k = 2 is not an instance of the universal property of products (2.2) since Proh( g) is the application of functor Projk to the arrow 9 and not a composition. We can extend L\k to work on functors and natural transformations as well. We start with the extension on functors. That is, we define tupling of k functors Fk : C f-1J by, is a mapping of type Ck f-1J. Note that it is safe to write ~kFf meaning either (~kF )f or ~dF ). The mapping L\kFk takes an arrow f and delivers a vector oflength k, (on arrows of C) is a functor it with each ith component being FJ. From the fact that ~k is a functor too since follows that ~kF So, kF~ and Similarly as for functors, we define tupling of k natural transformations CXk : Fk f- Gk with Fk , Gk : C f-1J by meaning either L\k((CXk)A) or (L\k(CXkllA. We have that Again, it is safe to write ~dCXk)A ~kCX is a natural transformation too, that is, 3.2. THE'r-Li CALCULUS since for R : A f- 59 B, Now, it follows that Lik is a functor on the functor category. The fact that Li k distributes over the composition of natural transformations follows from the fact that Li k distributes over the composition of arrows of C, For the tupling of two functors and two natural transformations, we use the notation (_, _) thus (F , G) is the functor that maps arrow f to the pair of arrows (Ff , Gf), and ((X, S) maps object A to the pair of arrows ((XA , SA). For Li k on functors we have the following universal property, for all k functors Fk : C f- V and all functors G : Ck f- V, (3.26) since = extensionality, definition LikF k } V(f:: Gf = LikFkf { ProjkGf universal property (3.24) = Fkf) } true Note that the universal property is a generalisation of the universal property of (binary) product (2.2) with respect to the functor category. (In contrast to (3.24), ProhG is the composition of Projk and G, not the application of Projk to G).This explains why we have chosen the Li notation: it is the generalisation of the binary split ",:,." operator. Like the universal property of binary products, the universal property (3.26) implies a number of properties. For instance, we obtain the following two cancellation laws: the reflection law, G = LidProhG) , and the computation rule Furthermore, we have the distribution law, (3.27) The proof of all these facts proceeds the same as for binary products. In view of rule (3.27) it is safe to write LikFkG meaning either (LikFk )G or Lik(FkG). If all k-fold vectors exist, we can define a so-called k-fold product functor, for k functors Fk : C f-V, nkF k ~ Lik(FkProjk): Ck f- V k . 60 CHAPTER 3. CALCULATIONAL PRELIMINARIES So, nkF k takes a vector of length k of arrows of V and applies Fi to the ith element of the vector resulting in a vector of length k of arrows of C. The mapping n k is a functor on functors, i.e. we have, For n k and,1b we have the generalized product-split fusion rule, A special instance of n is the so-called kth power of a single functor. We define for a single functor F : C (-- V, Fk Q nkF:ck(--V k . Most often we use the constructions ,1b n k and "_k" on functors which have as source and target a power of C. If it is clear from the context what the base category is, we write k (-- 1 instead of Ck (-- ct. We write hlfor (Ck)t, and (h1)*m for ((ck)t)m, etc. We call k, h1, etc., the arity of a category. So an arity is either a natural number or h 1 where k is an arity and 1 a natural number. Note that * is not associative. We do however identify 1*k and k* 1 with k. If F : k (-- 1, we say that k (-- 1 is the arity of functor F. A functor with arity 1 (-- 1 is called an endofunctor and a functor with arity 1 (-- k for some k is called single-valued. So, for kfunctors Fk with arity 1(-- m, ,1kFk has arity hk(-- m, and nkFk has arity hk (-- m*k. In particular, if F has arity 1(-- m then Fk has arity h k (-- m* k. Recall that a natural transformation (X : F (-- G for some functors F ,G : C (-- V is a mapping of type (X : C (-- V, i.e. (X maps objects of V to arrows of C. Just as for functors, we have a universal property for natural transformations. For all sets of k natural transformations (Xk : C (-- V and natural transformations f3 : Ck (-- V, (3.28) From this universal property follows the same properties for natural transformations as for functors. For instance, we have the rule, (3.29) and we have the functor ,,_k" on natural transformations, i.e. (Xk : Ck (-- 'Ok if (X: C (-- V. In view of rule (3.29) it is safe to write ,1k(Xkf3 meaning either (,1k(Xk)f3 or ,1d(Xkf3). By omitting the brackets, we can use rule (3.29) silently in a calculation. Depending on which calculation rule we want to apply we can choose how we want to interpret a formula. For instance, the formula, may be interpreted in three different, but equivalent, ways: as ,1koFo(X, the composition of the functor ,1k, F and (x, as (,1k. F)o(X, the composition of the k-fold split of F after (x, and as ,1dFo(X], the k-fold split of the natural transformation Fo(X. 3.2. THE't-L1 CALCULUS 61 In the next section, we prove results for general mappings of type C f- V, not necessarily functors or natural transformations. We use the same universal propel1y as for functors and natural transformations but we drop the requirement that the mappings be functors or natural transformations. That is, we define for k mappings Fk : C f- V, which implies the universal property that for all k mappings Fk : C f- V and mappings G : Ckf-V, 3.2.2 Matrix transposition In this section we define matrix transposition. Using the universal properties of "L'." and "y," it is not difficult to show that (f <7 g) L'. (h <7 k) = (f L'. h) v (g L'. k). We can depict this rule by the following diagram f v 9 f 9 h <7 k h k Richard Bird calls such a rule an abide law [8], the contraction of the words "above" and "beside". Rewriting the equation in a variable-free way, we have that, where 't denotes the functor which maps (( f , g) , (h , k)) to (( f , h) , (g , k)). In other words, 't is matrix transposition on a matrix of dimension 2 by 2. In the remainder of this thesis, we need matrix transposition for arbitrary dimensions. In this section we give a formal characterization of matrix transposition and use it to derive several properties. 3.2.3 Definition of matrix transposition We define the functor l 't, 't : k* 1 f- h k, by the following slok property. We require for each k times 1 matrix of mappings Fk,I, Fk,1 : C f- V, that, L11L1kFk ,1 = 'tL1 kL1 1Fk ,1 , or, equivalently, Siok 't : L11L1kFk ,1 - - L1 kL1I Fk ,1 . Note that the functor nkn l is recoverable from L1 kL1 1 with ak,1 L1 kL11PTOhPTOjk = = PTohPTOjk since L1dId1 )PTOjk = (Idl)k 1Nonnally, we denote functors by capital Roman letters but since T will be a natural transfonnation too, we denote it by a Greek letter. 62 CHAPTER 3. CALCULATIONAL PRELIMINARIES (Note that Proh : 1 f-1 and Projk : 1f- hk.) Now, from lemma (3.11) it follows that the only possible candidate for 'T is 'T = £'1 t £'1 k (ProitProjk) Indeed, for this choice of'T we have 'T : £'1 t £'1kFk ,t ~ £'1k£'1 t Fk ,t since 'T£'1k£'1 t Fk ,t = £'1t£'1kProitProjk£'1k£'1tFk,t = £'1 t £'1 kFk ,t Furthermore, since functor ntn k is fusable into £'1 t£'1k it follows from lemma (3.12) that the functor'T is also a natural transformation of type ntn k f- nknt, i.e. (ntnkFk,tl'T = 'T(nknlFk,tl Note that 'T is an isomorphism. The inverse of 'T, 'To, is derived from the definition of'T by interchanging k and 1, i.e. 'TO = £'1 k£'1 l (ProjkProit). Indeed, 'To is the inverse of'T since using the defining slok property of'T and distribution, 'T('T = 0 ) Not only has 'T 'T£'1k£'1lProjkProh = £'1t£'1kProjkProit = (Idk)l its defining slok property, it has also the slok property £'1 t n kFk,l = 'Tn k£'1 t Fk,t I since using the definition of n, 'Tn k£'1 t Fk,t = 'T£'1k£'1tFk,tProjk = £'1l£'1kFk,tProjk = £'1 t n kFk,t Similarly, we have the slok property TI t £'1 kFk,t = 'T£'1 kTI t Fk,t . Since the functor "_k" is an instance of TI, it follows that we have the following naturality property, ((Fk)t)'T = 'T((Ft)k) . and the two slok properties, £'1 t ((Ftl k) = 'T(£'1 t Ftl k 1\ (£'1 kFS = 'T£'1k ((F k)t) . We adopt the convention that we use 'T for arbitrary k and 1. We rely on the context to determine what the dimensions of'T are. Note that if k = 1 or 1 = 1 then 'T is the identity functor on Ct or Ck, respectively. Recall that if functor F has arity 1f- m then Fk has arity h k f- m* k. Dually, we define kF 6 'T(Fk)'T. We write the superscript before its argument in order to facilitate the use of the arity rule: if F has arity 1f- m then kF has arity h 1f- hm. (A disadvantage is that we have to be careful about parenthesisation since FkG can be read as (Fk)G or as F(kG).) Note that kF is also a functor since it is by definition the composition of three functors. Since is an isomorphism we can shunt one or both of the 'T'S to the other side. So, we have, (kF)'T = 'T(F k ) , 'T(kF) = (Fk)'T and Fk = 'T(kF)'T . 'T In this section we have listed a number of properties of'T. Most of these properties can be remembered or reconstructed by arity considerations. For instance, £'1 t ((Ftl k) has arity h 1 f- k and (£'1 t Ftl k has arity l*kf- k. So this suggests that the relationship between the two constructs is matrix transposition 'T. 3.3. EXTENDING TO ALLEGORIES. 3.3 63 Extending to allegories. Just as for the product C x D of two allegories, it is the case that the kth power of an allegory is again an allegory if we define all operations componentwise. That is to say, we postulate that L1k (on relations) is a homomorphism. For instance, we have (L1kRk)O = Llk(R k0), LlkRk n Llk5k = LlklR k n Sk), LlkRk \ Llk5k = Llk(R k\5 k ), etcetera. In the same way as we proved that Proh is a functor it follows from the universal property (3.24) that Projk is a homomorphism too. Now, it follows trivially that Llk and Projk are relators. Furthermore, the mapping Llk on relators, respects relators. That is to say, LlkFk for k relators Fk is again a relator. Hence, the functor'[ defined as L11LlklProhProjk) is a relator too. More specifically, since Llk respects homomorphisms, it follows that '[ is a homomorphism. In particular, '[ distributes over factors, i.e. '[(R\S) = '[R \ '[5. Recall that for binary intersection we have the following universal property, == X~RnS R~X /\ X ~ S Similarly, we can define the intersection of a vector of relations. That is to say, we define the Band R : LlkA~B mapping n : 1 ~ k by, for X: A ~ X~ n R == LlkX ~ R In other words, n is the upper-adjoint of the relator Llk. Note that n is a partial function: it is only defined on a vector of relations each with the same target and same source. Dually, we can define union u : 1 ~ k by uR ~ X == R~ LlkX So, u is the lower-adjoint of the relator Llk. As we mentioned before, the reason why we investigated powers of categories, matrix transposition etc. was because we wanted to have a variable-free calculus of functors. But we can do more. For instance, we can express the statement (3.30) without variables. Recall that for each relation X of Ck, we have X = LlkXk where Xk = ProhX. So, statement (3.30) can be rewritten as, for all X and Y from ck, (3.31) This is the requirement for a binary intersection. For the intersection of three relations of Ck, we have Xn Yn Z = Llk(X k n Yk n Zk) . (3.32) So, for the intersection of 1 relations R1 of ck, we have (3.33) We have included the subscripts k and 1 in (3.33) in order to indicate the correct binding. Note that the arity of the intersection operator on the Ihs is k ~ h 1. With the goal of defining this operator in terms of single-valued intersection operators we continue with the rhs: 64 CHAPTER 3. CALCULATIONAL PRELIMINARIES ~k n ~dRl)k { product-split fusion: k~ { matrix transposition = n (nk)~ } (nk)~dRtl (nk)T~l dRt ~dRlh = R1 } (nk)T~lR Combining this with (3.33) and abstracting from n = ~lR = R yields (nk)'T . (3.34) Note that the n operator of the lhs has arity k f- hi, whereas the n operator of the rhs has arity 1 f- 1. Thus property (3.34) is what we expect from arity considerations. Because T is an isomorphism, equation (3.34) is equivalent to, n'T = n k . (3.35) Similarly, we have for union, U = (uk)'T and UT = Uk (3.36) And we have for t'Toh, PTOh = (PTohk)'T and PTOhT = PTOh k (3.37) where PToh of the lhs has arity k f- hi, whereas PTOh of the rhs has arity 1 f- 1. We prove the second conjunct using the definition of'T, PTOhT = PTOh~lkj = ~kPTOhj = (PTOh)k The last property we conclude this section with is the generalisation of the property that (R n S) n (T n ti) = (R n T) n (S n ti) In other words, n abides with itself. In general, we have the property that, nn = nn'T where the arities of the four intersection operators are, in order from left to right, 1 f- k, kf- hi, 1 f-l, if- hk and 'T is of arity hkf- h1. 3.3.1 Regular datatypes Having the notion of powers of categQries we can define the notion of a regular relator. First, let :F denote the class of relators which contains the basic relators - the constant relators KA for all objects A, coproduct "+", product "x" - , which also contains all the projection and the relators PTOh : k f- hi for 0 s:: i < 1, and is closed under composition, tupling (~k) tree type construction. Note that :F contains all identity relators of type k f- k, for all k, since an identity relator is a projection relator on a vector of length 1. Furthermore:F contains the relator 'T and constructs like Fk and kF. Now, we call the subclass of relators of :F with arity k f- 1 where k and 1 are natural numbers, the class of regular relators. Chapter 4 Membership In section 2.2 we argued that "datatype = relator" and we showed that for coproduct, product and recursively defined datatypes, a corresponding relator exists. For these relators a so-called membership relation exists. When we introduced the notion of a functor and when we generalized it to the notion of a relator, we talked about F-structures, and "F-structures containing elements" etc. In general, one would expect for a relator F the existence of a collection of membership relations mernA : A f- FA for which we have the interpretation that u mernA x iff U is an element of the F-structure x. For instance, the interpretation of the pairing relator FA = A x A is that u is an element of b x c if u = b or u = c. So, outl U outr is the corresponding membership relation. Similarly, for the relator List, we want that u is an element of the list [UO,Ul, ... ,Un-l] when U = Ui for some i , 0 ::; i < n. Several scientists have suggested that the datatypes used in computing cannot be equated with the notion of a relator or functor since these notions are too general. Oege de Moor proposed the idea that a datatype is the combination of a relator and a membership relation. This proposal, summarised by the slogan" datatype = relator + membership" was investigatedjointly by Hoogendijk and De Moor and reported in [20]. In this chapter we extend that investigation further in order to include relators of arbitrary arity. Most often, it is not too difficult to come up with a membership relation for a specific relator. For instance, it is possible to define membership relations inductively for the polynomial relators. However, we want to have a generic characterization of membership. Having a generic characterization, it is possible to prove properties for membership relations in general. If we only have an inductive definition for membership, every time we want to prove a property for membership, we have to consider each of the cases of the inductive definition. Such a case analysis is undesirable. We start with the definition of a membership relation for an endorelator. Later on we generalize this definition to single-valued relators and then to arbitrary relators. 65 66 4.1 CHAPTER 4. MEMBERSHIP Definition of membership What should a generic definition of a collection of membership relations mernA for an arbitrary relator F look like? For the moment, we concentrate on a single element of a collection of membership relations, i.e. we try to find a characterisation of mernA : A +--- FA for some fixed relator F and object A. Recall that the interpretation of FR is a relation between two F-structures of the same shape such that the corresponding elements of the two F-structures are related by R. In particular, the interpretation of FX for partial identity X, X S;; idA, is that FX is the partial identity defined on F-structures containing elements related by X. In other words, the partial identity FX represents the set of F-structures only containing elements of the subset of A which X represents. This interpretation leads to the following two requirements. First of all, for the set of F-structures FX, the membership relation should only return elements of set X. In a formula, (4.1) (mernA· FX)< S;; X for all X, X S;; idA. On the other hand, all F-structures for which the membership relation only returns elements of X, should be elements of FX. That is, (mernA· Y)< S;; X =} Y S;; FX for all X and Y, X S;; idA and Y S;; id FA . Since by monotonicity equation (4.1) is equivalent to (mernA' Y)< S;; X combine both requirements in the following way, (mernA' Y)< S;; X == Y S;; FX for all X and Y, X S;; {= (4.2) Y S;; FX, we can idA and Y S;; id FA . (4.3) Note that requirement (4.3) is a Galois connection. We require relator F restricted to partial identities to be the upper adjoint of the mapping on partial identities "AY. (mernA' Y)< . So, the interpretation of equation (4.3) is that FX is the largest set of F-structures for which membership mernA only returns elements of set X. A factor is by definition the upper Galois-adjoint of composition. Using the isomorphism between partial identities and conditionals we can get rid of the domain operator in equation (4.3) and restate it as an equality using a factor. So, rewriting equation (4.3) in terms of leftconditions, we get Introducing a factor in the lhs this becomes Since left-factors respect left-conditions the above equation (for all Y), is equivalent to mernA \(X· n = FX·!O for all X, X S;; idA. (4.4) For the moment, we take equation (4.4) as the generic definition of membership mernA for all objects A. Next we show that by using extensionality, definition (4.4) can be generalized in the following way: For each R : B +--- A, FR . mernA \id A = memB \R which follows by extensionality from, for all points pEA, (4.5) 4.1. DEFINITION OF MEMBERSHIP 67 FR . memA \id A . P { function p, factors } FR· memA\p { p=p<.!0,(4.4)} FR . Fp< . !O { relator F, domains } F(R.p)< . !O { (4.4),(R·p)<·!0=R·p memB\(R·p) { function p, factors } memB\R· P Since F is a functor, equation (4.5) is equivalent to FR . mem\5 = mem\(R· 5) . (4.6) That equation (4.6) is indeed a generalization of the definition of membership (4.4) follows by taking R := X and 5 := !O and thefact that mem \!O = !o. Since this direction, i.e. showing that equation (4.6) implies equation (4.4), does not depend on extensionality, we take equation (4.5) instead of (4.4) as the definition of membership for relator F: 1 Definition 4.7 (Membership) lator F if for each R : B f- A, A collection of arrows mem is a membership relation of re- (4.8) o Whenever it is necessary to differentiate between membership relations for different relators, we denote the membership relation for relator F by mem.F. Uniqueness of membership (endorelators) 4.1.1 Next we show that definition (4.7) is a "good" definition. We show that a relator has at most one membership relation. To be more precise, we prove that membership memo F is the largest natural transformation of type Id f-' F. That is, we have memo F : Id f-' F, and ex ~ mem.F <= ex: Idf-'F . (4.9) The proof of this fact depends on the so-called identification axiom. The identification axiom states that id is the largest natural transformation of type Id f-' Id. We have not encountered 1There are non-extensional allegories for which equation (4.5) for some relator F is really stronger than equation (4.4). 68 CHAPTER 4. MEMBERSHIP the identification axiom in the literature so far. However, it is not difficult to show that extensionality implies the identification axiom. Furthermore, the identification axiom is a necessary condition for the proof of property (4.9). Since id is a membership of the identity relator Id, property (4.9) for relator Id implies the identification axiom. So, we prove the following lemma assuming identification: Lemma 4.10 If mem is a membership relation of relator F, then mem is the largest natural transformation of type Id t---' F. Proof We first prove that mem : Id t---' F, mem . FR <;;; R· mem { factors} FR <;;; mem\(R· mem) mem membership (4.6) } FR <;;; FR . mem \mem id <;;; mem\mem } true Next, we show that mem is the largest natural transformation of its type. Let ex be a natural transformation of type Id t---' F. It is easy to prove that mem\id : F t---' Id, since for R : A (-- B, (mem\idl A · R <;;; mernA \R = FR . (mem\idl B That ex <;;; mem now follows from the identification axiom: C { id F <;;; mem \mem ex'mem\mem mem membership (4.8) } ex· Fmem . (mem\idl F ex: Idt---' F } C mem· exF' (mem\idJF ex:Idt---'F,mem\id:Ft---'Id,hence,ex·mem\id: Idt---'Id, C id is the largest natural transformation of this type } mem D Note that we assumed that mernA has type A (-- FA. So, we assumed that relator F is an endorelator. In the following subsection we extend the definition of membership to non-endo relators. 69 4.1. DEFINITION OF MEMBERSHIP 4.1.2 Membership for non-endo relators Next we generalize the definition of membership for arbitrary relators. We first derive the definition of membership for single-valued relators, i.e. relators of arity 1 f- k for some k. For the moment, we assume k = 2. What should the definition of the membership relation mem.G for a binary relator is> look like? The interpretation of a binary relator iZI as a datatypeformer is that a structure of type AoiZlA l, for objects A o and A l, contains data at two places: the left and right argument. In other words, the membership relation for @ has two components, memo : A o f- AoiS>Aland mem 1 : A 1 f- AoiZlA l, one for each argument. Just as in the endo case, for all iZI-structures that are elements of the set Xo@X" for partial identities Xo and Xl, the component for the left argument should only return elements of Xo, and the component for the right argument only elements of X,. In formulae, for all Xo ~ id Ao and Xl ~ id Al (memo' XoiZlXJl< ~ Xo and (meml' XoiZlX,)< ~ Xl . On the other hand, all iZI-structures for which the left component and right component of the membership relation only return elements of Xo and Xl, respectively, should be elements of the set XoiZlX l . That is, for all Y ~ idAoC)Al (memo' Y)< ~ Xo 1\ (mem,. Y)< ~ Xl =? Y~ XoiZlX, Combining both equations we demand that for all partial identities Xo Y~ ~ id Ao ' X, ~ idA 1 and idAo0A" (memo' Y)< ~ Xo 1\ (mem,· Y)< ~ Xl (4.11) The lhs of above equation can be rewritten as ~2 ((memo, mem,) . ~ Y)< (Xo , XJl denotes the doubling functor: ~2 Y = (Y, V). Now, writing A = (A o , A,), Recall that ~2 X = (X o , X,) and mem = (memo, mem,), equation (4.11) becomes, for all partial identities X ~ idA and Y ~ id(C91A, == X~<)Y2~'me ( X)IZi(~Y The above equation for a membership relation for a binary relator suggests the equation for an arbitrary single-valued relator F of arity 1 f- k. Specifically, we demand that mem be of arity k f - k and is such that for all objects A, A E Ck, we have for all partial identities X ~ idA and Y ~ id FA , Y ~ FX with memA : A f- ~ k F A . As in the case of endorelators we want to get rid of the domain operator and restate the requirement as an equality using factors. Rewriting the above equation in terms of conditions, we get memA . ~k Y . !O ~ X. !O Y.!O ~ FX . !O (4.12) 70 CHAPTER 4. MEMBERSHIP Let us concentrate on the Ihs. Since all operations in allegory Ck are defined componentwise, it follows that !"'k A = t.k!A. Hence, since t. k is a relator, Using this and introducing a factor, the Ihs of (4.12) can be rewritten as, Exploiting the Galois connection between t. and n, we get Hence, in combination with equation (4.12), our requirement for memA becomes (4.13) Again, by a similar calculation as for the endo case, we can prove using extensionality that equation (4.13) is equivalent to, for each R : B f- t.kA, (4.14) since for all points pEA, FR· n(mem\idlt.kA . P function P, factors, intersection } FR· n(mem"'k A \t.kP) { t.kp = t.kp< . to, (4.13) } FR . Ft.kP< . !o relator F, domains } F(R . t.kP)< . !o (4.13), (R· t.kP)< . !o = R· t.kp n(memB\(R· t.kP)) function P, factors, intersection n(mem"'k A\R) . P We take equation (4.14) as the definition of membership for a single-valued relator F : Definition 4.15 (Membership) A collection of arrows mem of arity k f- k such that, for each A, memA : A f- t.kFA is a membership relation of single-valued relator F : 1 f- k, if for each R : B f- t.kA, FR . n(me\idL~"A o = n(memB\R) (4.16) 4.1. DEFINITION OF MEMBERSHIP 71 Note that definition (4.15) is indeed a generalization of definition (4.7) since,11 = Id and n : 1 \- 1 = Id. Furthermore, using the fact that F is a functor, it follows that, (4.17) FR· n(mem\S) = n(mem\(R· S)) Instantiating the definition of membership for binary relator 0, it follows that the pair of collections of relations memlA, B : A\- A0B and memrA, B : B \- A,,)B is a membership relation of ® iff, for each R : A\- C and S : B \- C, RcZ!S· (memlc,c\idc n memrc,c\idc) = memIA,B\R n memrA,B\S . (4.18) For instance, it follows that the pair (outl, outr) is a membership of product since property (2.64) states that R"'S = outIA,B\R n outrA.B\S. Hence, requirement (4.18) is just the product-split fusion rule: The decomposition lemma The construction of a membership relation for a relator F of arity 1 \- k, k > 1, was guided by the idea that memA should be a vector of length k of membership relations for each of the k arguments of F. Indeed, this can be proven using the definition (4.15). For instance, define relator G by GR = F(R, idA) for some object A E Ck - 1. Hence, G contains the elements of the first argument of F. Let mem denote a membership of F. Assume that mem = (a, (3) for a E C and 13 E Ck - 1 , i.e. a is the first component of mem and 13 the remainder. Now, the claim is that the collection of relations 'Y defined by 'YB = alB ,A) is a membership relation of G. To prove this claim we first observe that for R : C \- B, 'Yc\R { a(c,A)\R definition 'Y, X\ IT n S = IT, nIT = IT, intersection n(f3(C,A)\ITA,L'>k_1 B) n (nX) = n(S , X), \ componentwise } n((a, (3)rc,AJ\(R, ITA,L'>k_1 B)) definition a and 13 } n(memrc,Aj\(R, ITA,L'>k_1 B)) Using this, the proof that 'Y is the membership of G is as follows: YcW { calculation above } n(mem(c,Aj\(R, IT A,L'>k_1 B)) { mem membership off, (4.17): (R, ITA,L'>k_l B) = (R, idA) . (id B , IT A,L'>k_l B) F(R,id A )· n(mem(B,A)\(idB,ITA,L'>k_1 B)) 72 CHAPTER 4. MEMBERSHIP definition G, calculation above GR· ys\id s In particular, we have for binary relator C9 with membership relation mem = (meml, memr) that meml_,A is a membership relation of the sectioning (C9id A). That is to say, we have for R: B f- C, RC9id A . memlC,A\id c = memlS,A \R Multi-valued relators So far we have only considered single-valued relators, i.e. relators of arity 1 f- k. Next we consider the membership relation for relator F of arity 1 f- k. Recall that for F : 1 f- k, mem. F is a vector of membership relations for each of the k arguments of F. Generalizing this property to a relator of arity 1 f- k, we require that mem. F is a vector of length k, each component corresponding to one of the arguments of F and being itself a vector of length 1. Thus we require mem. F to have arity h k f- k. This leads to the following definition, Definition 4.19 defined by, For relator F of arity 1 f- k, the membership relation, mem. F : h k f- k, is mem.F ~ 'l',11(mem.Ftl D For mem. F we have the following property. For F : 1 f- k, we have for each R, RECk and R : B f- ,1 kA, Lemma 4.20 FR· n(mem.F\ (,1tlkidbkA = n((mem.F)s \ (,1tl kR) Proof We first calculate that n((mem.F)s \ (,1tl k R) { definition mem.F, klt~( = 'l',11 where 'l' : hkf- hl n('l',11(mem.FtlB \ 'l',11R) { 'l' and ,11 distribute over \ } n'l',1d (mem.Ftls \R) n'l' = nl, (n 1),11 = ,11n } ,11n (( mem. Ftl s \ R) Using the above calculation the lemma is straightforward to prove: FR· n(mem.F\ (,1tl kid)"'k A = n((mem.Fl s \ (,1tl kR) ~lF1R F = ,11Flo calculation above } ,11n[mem.F1\idl"'k A = ,11n((mem.FtlB\R) (4.21) 4.1. DEFINITION OF MEMBERSHIP ~t ~dFtR 73 functor } . n(mem.Ft\id)""k A) = ~tn(me.FlB\R) for each 1 } FtR· n(mem.Ft\id)""kA = n((mem.FtlB\R) Hence, the lemma follows from the definition of membership (4.15) for each mem. Ft. o As a matter of fact, one can use property (4.21) as the defining equation of membership for an arbitrary relator, and show that the choice mem.F = '"[~dme.Ftl satisfies equation (4.21). 4.1.3 Uniqueness of membership (arbitrary relators) In subsection 4.1.1 we showed that an endorelator has at most one membership relation. In this section we generalise this result for arbitrary relators of arity 1 f- k. We start with singlevalued relators. That is to say, we prove that equation (4.16) has at most one solution. Lemma 4.22 If mem is a membership relation of single-valued relator F, then mem is the largest natural transformation of type Id f---> .F~ Proof First we prove that mem is a natural transformation: mem . { ~FR ~ R· mem ~FR ~ factors} mem\(R· mem) } nH~ ~ FR n(mem\(R . mem)) mem membership (4.17) } FR ~ FR . n(mem\mem) { id ~ mem\mem, nid = id true Next we show that mem is the largest natural transformation of its type. Let Then we have to show that (X ~ mem: transformation of type Id f---> .F~ (X ~ factors, intersection: id M C ~n(me\) (x. ~ n ( m e \ m e ) { (x. ~ F m e c { mem membership (4.17), . (X : ~n(me\id)"F Id f---> F~ } ~ functor } } (X be a natural CHAPTER 4. MEMBERSHIP 74 mem . aL';F . L'in(mem\idlL';F { lifting} mem . (aL'; . L'in(mem\id)",JF claim: aL'; . L'in(mem\idlL'; C ~ L'iid } mem Next we verify the claim. It is easy to prove that n(mem\idlL'; : FL'if--J Id , (4.23) since for R : B f- A, n(mem\idlL';B' R ~ n[(mem\idlL';B . L'iRl ~ n(memL';B \L'iRl = FL'iR . n(mem\idl M Furthermore, notice that it follows from the identification axiom that L'iid is the largest natural transformation of type L'i f--J L'i. Using both facts we calculate aL'; . L'in(mem\idl Ll ~ <= { L'iid identification axiom } all . L'in(mem\idlL'; : L'if--JLi <= typing rule } all : Lif--JL'iFL'i /\ L'in(mem\idlA : L'iFL'if--JLi <= typing rules } a: Idf--JL'iF /\ n(mem\idlL';: FL'if--Jld assumption a, property (4.23) } true o Hence, we have shown that a relator of arity 1 f- k has at most one membership relation. Recall that the membership relation for a relator F of arity 1 f- k is defined by mem.F = rrL'idmem.Ftl. Since mem.F t : Idf--JL'ikF t , we have We claim that mem.F is the largest natural transformation of its type too. First, because Li k is an order-isomorphism it follows that L'idmem.Ftl, where each mem.F t the largest natural transformation oftype Id f--J L'ikFt, is the largest natural transformation of type Lit f--J L'itL'ikF t . Now, since 'T is an isomorphism it follows that mem. F is the largest natural transformation of its type. Hence, every relator of arity 1 f- k has at most one membership relation. 75 4.2. MEMBERSHIP FOR REGULAR RELATORS 4.2 Membership for regular relators All regular relators have membership. In this section we give a definition of the membership relation for the regular relators. The construction of the membership relations is inductive over the structure of the regular relators. We start with the basic relators: Lemma 4.24 mem.Id id mem.K A ..ll_,A (outl , outr) (inlO ,inrO) mem.x mem.+ Proof Note that for R : B f- C, we have idB \R = Rand ..llB,A\R = TTA,C. Using this, the verification of membership for Id and KA is straightforward. Verification of membership for product follows from property (2.64) and from the product-split fusion rule. Similarly, verification of membership for coproduct follows from the converse of property (2.47) and the converse of the coproduct-june fusion rule. D 4.2.1 Membership of projections In this section we give the membership relation for a projection relator Proh : 1 f- k for 0::::: i < k. We have to construct a natural transformation mem.Proji of arity kf- k. Recall that the interpretation of mem. Proh is a vector of length k of membership relations for each of the k arguments of F. Since it is only the ith argument of Proh which contributes to the data that is stored in a Prokstructure, we suspect that mem. Proh is vector of length k with id at position i and ..ll elsewhere. Indeed, we have, For a projection relator Proh : 1 f- k, 0 ::::: i Lemma 4.25 defined by < k its membership relation is mem.Proh !:o. L1k (i = k----) idproj;, ..llProik, Proj.) . Where f (j) = (i = j ----) 9 , h) denotes a so-called conditional such that f (j) = 9 if i = j and f(j) =hifiy!oj. Proof We first calculate for R : B f- L1k A, (L1 k (i = k ----) idproj; , ..llProik ,Proj.) lB\ R B = L1k Bk , conditionals; R = L1 k Rk L1k ( i = k ----) idB, ,..llBk, B.) \ L1k Rk { L1 k distributes over \ } L1d(i = k----) id B, , ..llB k ,B,)\Rkl conditionals, idB, \R i = Ri , ..llBk, B, \R k = TTB, ,A L1di = k----) Ri , TT B, ,A) 76 CHAPTER 4. MEMBERSHIP So, n((Lh(i = k----1 idproi; ,ll..Proh, ProiJ h \Rl = n(lldi = k ----1 Ri TT B, ,All = Ri . Using this, equation (4.16) is trivially verified. I o 4.2.2 Membership of composition In this section we construct the membership relation for the composition of two relators. For a composition of endorelators, the membership relation is trivial. That is to say, mem.FG = mem.G· (mem.F1 G It is easy to verify that mem.FG satisfies equation (4.8). Before constructing the membership relation for the general case, we first look at a concrete example. We take F := x and G := (H , I), then FG = (x 1(H ,I) = H x I. A value stored in a HA x IA structure is stored either in the H-structure or in the I-structure. This suggest that mem.(H x 11 = mem.H· outl(H.I) U mem.!· outr(H,I) More generally, if F : 1 f- k and G : k f- 1, we expect that, mem.FG = Uk(mem.G k · ((mem.FlklGl (where Uk is a shorthand for Ullk), since a value stored in an FGA structure is stored in one of the Gk-structures atthe kth argument off. Recall that (mem.Fh is the membership relation of the kth argument off. Now, since mem.G k = (mem.Gl k and ((mem.FhlG = ((mem.F1Glk, we have Uk(mem.G k · ((mem.FlklGl = Uk((mem.Gl k · ((mem.F)Glkl = U(mem.G· (mem.F1Gl Hence, we define for F : 1 f- k and G : k f- 1, mem.FG = U(mem.G· (mem.F1Gl Next we consider the most general case that F : 1 f- k and G : k f- 1. Since FG has arity 1 f- 1, membership (mem.FG1 A is an element of e l , i.e. a vector of 1 membership relations, one for each of the 1 arguments of FG. Now, because the lth argument of FG is F applied to the lth argument of G, we expect, mem.FG = lllU((mem.Gh· (mem.F1Gl We try to rewrite this definition of mem. FG into a form without quantification: llIU((mem.Gh· (mem.F1Gl lltlul = (u1llll, III functor U1(lll(mem.Gh . lltlmem.F1Gl (4.26) 4.2. MEMBERSHIP FOR REGULAR RELATORS = mem.G, u l = UT where T : hG.me (l~ T~l = (~dk U(T(mem.Gl . hk+- h1, T functor } T~dme.F1Gl U(T(mem.Gl . { 77 } (~tlk)me.FG Hence we define, For relator F : 1 +- k and relator G : k +- 1, Definition 4.27 (4.28) o In order to verify that mem.FG is indeed the membership relation of FG, we first prove the following lemma, For each R : B +- A, Lemma 4.29 n((mem.FGls\Rl = n((mem.F1 GB \n((mem.G1 B\ (~klR) Proof n((mem.FG1 B\R) { definition mem.FG (4.28) } n((U(T(mem.G)B . (~dk)me.FlGs\R { Yl for X E Ck and YE C } factors: (UX)\Y = n(X \ ~k nn((T(mem.Gls· ((l'.dk)(mem.F1GBl lRk~\ { factors: (X· Yl\Z nn( ( ~dk)(me .FlGB { = T~b (~tlk T = Y\(X\Zl } \ (T(mem.Gl B\ L't.kRl 1 ~k = T(L't.kl l where T : hk+- h1, distributes over \ (twice) } nT(~lme.F1GB \ ((mem.Gl B\ (~klR { nnT = nn, n(~lX\ n((mem.FlGB \ n((mem.Gl B\ (~klR Yl = X\nY for YEC l and XEC } 1 o Using lemma (4.29) it is straightforward to verify that equation (4.16) holds for memo FG. We assume that mem.F and mem.G are memberships for F and G. Then for each R : A+- B, FGR . n((mem.FGl A\idAl lemma (4.29) } 78 CHAPTER 4. MEMBERSHIP FGR . n((mem.F)GA \n((mem.G)A \ (~k)lidA { mem.F membership, (4.17) } n((mem.F)GB\(GR· n((mem.G)A \(~SidA) { mem.G membership, (4.21) } n((mem.F)GB \ n((mem.G)B \ (~k)lR { lemma (4.29) } n((mem.FG)B \R) Hence, mem.FG is membership of FG. For binary relator Q9 and endorelators F and G it follows that mem.(F&G) = mem.F· ((mem.iSl)O)(F.G) U mem.G· ((mem.@)l)(F,G) For instance, we have for membership of F x G and F + G that, mem.(F x G) mem.F· outl(F,G) U mem.G· outr(F,G) mem.(F+G) mem.F· inl(F,G) U mem.G· inr(F,G) 4.2.3 Membership of the power relator As one would expect, membership of the power relator P is E. We have to show that PR . E\id = E\R One inclusion follows from naturality of E, E ; Id f- E, or equivalently, E : Id (----' P: PR . E\id c:;; E\R { factors E . PR . E\id c:;; R E : Id(----' P {= R· E . E\id c:;; R factors, monotonicity true For the other inclusion, let (f,g) be a tabulation of R. Then, PR· E\id { (f,g) tabulation of R, relator P extension of E } Ef . [Eg)O . E\id { Eg function, factors Ef· (E' Eg)\id 4.2. MEMBERSHIP FOR REGULAR RELATORS { 79 E : Id f- E } Ef· (g. E)\id { g function, factors Ef· E\gO :J { claim: Ef . E\R ;;;: E\(f· R) } E\(f. gO) { (f,g) tabulation of R } E\R The claim in the penultimate step we prove by a pseudo-pointwise argument. Let (x,z) be a tabulation of E\(f· R). That is: X'ZO = E\(f· R) . (4.30) We aim to construct a function y such that (4.31) then it follows that The first inclusion follows because y is a function and functions are total. The second inclusion follows from (4.31) and monotonicity of composition. Next, we construct function y such that equation (4.31) holds: X·yO <:; ; Ef { /\ y·zo <:; ; E\R shunting of functions and factors } x=Ef·y /\ E·y<:;;;R·z Y := I\.(X, fusion and cancellation x=I\.(f·(X) /\ (X<:;;; R·z { (X := 13 n R . z, intersection } x = I\.(f· (13 n R· z)) { E· x universal property of I\. (2.29) = f· (13 n R· z) 13 := fO . y, modular identity E·x=ynf·R·z { E·x<:;;;f·R·z { shunting of functions and factors x·z°<:;;;E\(f·R) 80 CHAPTER 4. MEMBERSHIP assumption (4.30) } true intersection } E'x=y Thus equation (4.31) is valid with 1J assigned to AW· E· x n R· z). Surprisingly, the above proof is very long. It is very difficult, only using that P is an extension of E and E : Id i- E, to show that E satisfies the defining equation of the membership relation of P. However, in [40] the following property is proved using a similar proof technique: (4.32) E\E:Pi-E Using this property, the verification that E is the membership relation of P is almost trivial. Since AR is a function it follows that E\R = E\( E . AR) = E\E . AR. Using this we prove: PR· E\id { property above PR . E\E . Aid E\E : Pi- E } E\E . ER . Aid { fusion rule: ER . AS = A(R· S) } E\E . AR { property above } E\R The way we proved that E is the membership relation of P is an adaption of the proof in [40] of property (4.32). As a matter of fact, having the fact that E is membership of P, naturality of E\E follows trivially: PR· E\E { E membership of P } E\(R· E) { E : Idi- E } E\(E . ER) { E\E . ER ER function, factors } 4.3. MEMBERSHIP OF TREE TYPES 4.3 81 Membership of tree types In this section we construct the membership relations for tree types. Let @be a binary relator and let (in, T) be its tree type. We try to construct the membership of T. This construction is inductive in the sense that we assume the existence of membership of @ and construct the membership of T in terms of it. Let mem = (meml , memr) denote the membership relation of@. We know that relators T and Id@T are isomorphic: in is a natural isomorphism of type T f- Id@T. It is easy to verify that if F and G are isomorphic, that is, there exists a natural isomorphism (X: F f- G, then mem.G . (X0 and mem.F· (X are memberships of F and G, respectively. Hence, we have, mem.T = mem.(Id®T) ·in° (4.33) From the definition of membership of composition, it follows that mem.(Id®T) = meml(Id,T) U mem.T· memr(Id,T) Instantiating this in equation (4.33) and distributing ina over the composition, yields the following recursive equation: mem.T = meml(Id,T)' ina U mem.T· memr(Id,T)' ina (4.34) Now, define root = meml(Id,T)' ina The natural transformation root: Id (---' T returns an element that occurs at the root of a tree. Similarly, define branch = memr(Id,T)' ina The natural transformation branch: T (---' T returns an immediate subtree of its argument. Using both definitions, equation (4.34) can be rewritten as, mem.T = root U mem.T· branch (4.35) In general, R· 5* is a solution of the equation: X :: X = R U X·S where 5* is the reflexive, transitive closure of S. That is, 5* is by definition the least solution of: X :: X = id U X· 5 . So, equation (4.35) suggests that we should define mem.T !:. root· branch* . The interpretation of root· branch* is that branch* selects an arbitrary subtree and root takes the root element of it. That this definition indeed defines membership of a tree type follows from the following lemma: CHAPTER 4. MEMBERSHIP 82 Let Lemma 4.36 <2) be a binary relator with tree type (in,T). Then (root· branch*)\R = [id,Zi; meml\R n memr\id] Proof We aim to show ina . (root· branch*)\R = id Q9 (root· branch*)\R . (meml\R n memr\id) This may be done as follows in°· (root·branch*)\R in function, factors } (root· branch*· in)\R closure } ((root U root· branch*· branch)· in)\R composition over union, defs root and branch (meml U root· branch*· memr)\R factors } meml\R n memr\((root· branch*)\R) { mem = (meml, memr) membership of binary relator Q9 id Q9 (root· branch*)\R . (meml\R n memr\id) o As a corollary to the above lemma, we have Corollary 4.37 Let Q9 be a binary relator with tree type (in,T). Then mem.T = root· branch* Proof We argue TR· [root· branch*)\id lemma (4.36) } TR· [meml\id { n memr\id] [_] H [R®id . (meml\id { U, tree type fusion n memr\id)] mem = (meml,memr) membership of binary relator ® [meml\R n memr\id] lemma (4.36) } (root· branch*)\R o 4.4. A COUNTER-EXAMPLE 83 4.4 A Counter-example Finally, we mention the fact that not every relator has a membership relation. Consider the case that C = 8 2 for some 8 and define the swapping relator on C: F(R,S) = (S,R) Suppose there is a natural transformation (ao,al) of type Id f-' F. We argue (ao,ad : Idf-'F. { definitions} V(R,S:: (R,S)·(ao,ad:2 (ao,ad·(S,R)) =? { composition in 8 2 } V(R,S:: R·ao:2 ao·S) =? { take R := II and 5 := id } ll:2lXo By symmetry, we also get al = ll, and therefore (ll,ll) is the only transformation of type Id f-' F. Since (ll ,ll)\(R,S) = (TT ,TT) it follows that F does not have membership. It is important to realize that F does not have a membership relation since we consider C = 8 2 to be our base category and thus F to be an endorelator. If we consider 8 to be our base category, so F is a relator of arity 2 +--- 2 defined by (Outr, Outl) , it follows from lemma's (4.19) and (4.25) that F does have a membership relation. 4.5 Fans In section 4.1.1 we have proven that, for single-valued F, mem. F is the largest natural transformation of type Id f-' LiF. This fact can be generalized to any pair of single-valued relators that have membership: Lemma 4.38 Let F and G be single-valued relators with membership mem.F and mem.G respectively. Then the largest natural transformation of type F f-' G is n (mem. F\mem. G). Proof From the definition of membership (4.16) it follows that n(mem.F\mem.G) = Fmem.G . n(mem.F\id)"G Furthermore, mem.G has type Id f-' LiG and n(mem.F\id)" has type FLi f-' Id by equation (4.23). Hence, n(mem.F\mem.G) has type F f-' G. Now, let a: F f-' G, then a ~ n(mem.F\mem.G) LiHn } Lia c:;:: mem.F\mem.G factors } CHAPTER 4. MEMBERSHIP 84 ~ex mem.F ~ c:;; mem.G mem.G largest natural transformation of type Id f-' ~ G mem.F ~ex: Idf-'~G mem.F : Idf-'~F , ~ex : ~Ff-'G } true o So, for endorelators F and G, we have that mem.F\mem.G is the largest natural transformation of type F f-' G. It is illuminating to interpret this result in ReI. If ex : F f-' G then ex c:;; mem.F\mem.G. That is, mem.F· ex c:;; mem.G. This says that ex can never invent new values: if x ex y, then every value stored in the F-structure x is a member of the set of values stored in the G-structure y. An important special case occurs when G is the identity relator: for endorelator F, mem.F\id is the largest natural transformation of type F f-' id. The interpretation of mem. F\id is a relation that holds between an F-structure and a value x such that all values stored in the Fstructure are copies of the value x. Such a relation was called a generator in [4] (where it was first introduced) because it generates (or creates) F-structures from a given seed value x. We now prefer to use the term fan because "generator" is sometimes used with a different meaning and because "fan" is shorter. (The metaphor is a hand-held device for creating a current of air made from several copies of a single blade that can be fanned out or completely closed up.) In this section we give a general definition of a fan (satisfied by memo F\id in the case of an endorelator F) and then show that any relator with membership has a unique fan. The definition of fan is based on the following two observations: first, we have that mem.F\id : F f-' id and second, because AR. (P\R) preserves arbitrary intersections, it follows from the definition of membership that AR . (FR . mem. F\id) preserves arbitrary intersections. For the definition of a fan, we only require preservation ofjinite intersections. This leads to the following definition. Definition 4.39 Afan of endorelator F is a natural transformation fan: F f-' Id such that the mapping A(R : B f- A). (FR· fanA) preserves finite intersections: FlT B.A F(R n S) IT FB,A FR· fanA n FS ·fanA for all R ,S : B f-A. o Corollary 4.40 memo F\id is a fan of endorelator F. o We start with a useful lemma: 4.5. FANS 85 Lemma 4.41 If for endorelator f and a collection of arrows (X, (XA '\(X: B t- A) . (fX . (XA) preserves binary intersections then fR n f$·(X·(X°·fT ~ fAt- A the mapping f(RnS·T) Proof Since id is total it follows from equation (2.53) that for each R : At- B, outlA,B . R 6 id B = R Note that outlA,B is simple, and R 6 id B is cosimpIe since R 6 id B ~ outrA,B' So, although we do not assume tabularity, it follows that if we assume products then for every relation R there exists a pair of simple relations (f, g) such that R = f· gO. Now, using that R = f. gO for some simple f and g, we calculate: f(f·gO) n f$·(X·(X°·fT { modular identity, f and 9 simple } ff· (id n fW·$)·(X·(X°·f(T·g))· fgO { domains: id n p. QO = (P n Q)< ff . (fW' $) . (X n f(T· g)O . (X)< . fgO { '\R. [fR . (X) preserves binary intersections } ff· (fW'S n (T·g)O). (X)<. fgO C { domains: (p. Q)< ~ P<, (fP)< = f(P<) } ff· f(W'S n (T·g)o)<). fgO { domains: (P n Q)< = id n p. QO } ff· f[id n fo·$·T·g) . fgO { modular identity, f and 9 simple f(f'gO n S·T) o As a direct corollary, we have for fans: Corollary 4.42 Let fan be a fan of endorelator f, then FITA,A n fanA' fan~ ~ id FA Proof Follows from (4.41) by taking R = ITA,A, $ = idA and T = idA. o The interpretation of corollary (4.42) is that if we have two f-structures x and y with the same shape, i.e. x fIT y, and x and yare filled with the same value a, i.e. x fan a and 11 fan a then x and yare equal. In other words, for every value a there exists for every f-shape at most one f-structure of that shape containing only copies of a. Not only is mem.f\id a fan of relator f, it is also the unique one. Corollary (4.42) is the keyargument in the proof of this fact. CHAPTER 4. MEMBERSHIP 86 If endorelator F has membership mem.F, then mem.F\id is the unique fan of Lemma 4.43 F. Proof For uniqueness we argue as follows: suppose (X ~ {3, then (X and {3 are both fans of F such that {3 (X fan of F: IT = FIT {3 c n FIT· (X { ({3. (x0 modular law n { C ({3.{30 { C (x, intersection } FIT) . (X } (X~{3 n FIT) . (X {3 fan of F, lemma (4.42) } (X Hence, (X = {3. So if there are two fans then they are incomparable via~. But from theorem (4.38) it follows that memo F\id is the largest transformation of type F (----' Id, hence, mem.F\id is the unique fan of relator F. o We call mem. F\id the canonical fan of relator F. 4.5.1 Fans of non-endo relators Until now we have only considered fans of endorelators. In this section we generalize the definition of a fan to arbitrary relators. We start with single-valued relators. For endorelator F, mem.F\id is its canonical fan. For a single-valued relator F : 1 f- k we want n (mem.F\id )tlk to be its canonical fan. Equation (4.23) states that n(mem.F\id)tlk is a natural transformation of type FL1k (----' Id. Furthermore, we have that the (partial) mapping l\R : B f- L1 k A . (FR· n(mem.F\idltlkAl preserve finite intersections since by definition (4.15), FR· n(mem.F\idl tlkA = n(mem.F\R1 B and l\R : B f- L1 k A . n(mem.F\R1 B preserves finite intersections. This leads to the following definition: Definition 4.44 A fan of single-valued relator F : 1 f- k is a natural transformation fan : FL1k (----' Id such that the mapping l\(R : B f- L1 k Al. (FR· fanAl preserves finite intersections: FIT B.tlA F(R n Sj IT FB,A FR . fanA n FS . fanA for all R, S B f- L1 k A. 4.5. FANS 87 o n(mem.F\idl""k is a fan of endorelator F. Corollary 4.45 o Next we define the fan for arbitrary relators F of arity m ( - k. Just as for membership, we define a fan of a tuple of relators as a tuple of the corresponding fans: For relator F of arity 1 (- k, the fan, fan.F : 1(- 1, is defined by Definition 4.46 fan.F ~ L1 lfan.F l . o Note that fan. F is a natural transformation of type FL1 k L1 lfan.F l : L1 lFlL1 k 4.5.2 (-' L1 l = FL1k (-' (-' L1 l since L1 l . Uniqueness of fan (non-endo) relator Just as for endorelators it is the case that the existence of a membership relation implies that the canonical fan of a non-endo relator is the largest natural transformation of its type. We consider single-valued relators first. From lemma (4.29) and mem.L1 = L1(idl it follows that n(mem.F\idl"" = mem.FL1\id. So, the canonicalfan of a single-valued relator F is the canonical fan ofthe endo (!) relator FL1. Hence, from lemma (4.43) it follows that n( memo F\idl"" is the unique fan of relator F. The canonical fan of an arbitrary relator F of arity 1(- k is defined as L1 l fan. Fl. Each of the components fan.F l is the largest natural transformation of its type, hence, L1 lfan. Fl is the largest natural transformation of type FL1 k (-' L1 l . 4.5.3 Fans of regular relators The definition of a fan as presented here arose more or less as a counterpart of membership, i.e. a membership has type Id (-' F whereas a fan has type F (-' Id . Directly connected with fans we have the notion of the so-calledfanlunction. The fan-function Fof relator F : 1(- k is defined by FR = FR . (fan.F1 A for all R : B (- L1 k A Note that the fan-function is a partial function: it is only defined on a vector of relations with the same source. We call the fan-function canonical if it is constructed using the canonical fan. Note that for an arbitrary relator we have F= since L1 l Fl , 88 CHAPTER 4. MEMBERSHIP For this reason we may restrict our attention to single-valued relators without loss of generality. Having the notion of a (canonical) fan-function, the definition of membership (for a singlevalued relator) can be expressed as the requirement that FR = n((mem.FlB\Rl FR = FR· for all R : Bt-L1k A since (fan.Fl A = FR . (mem.FL1\idl A = FR· n(mem.F\idl M The definition of membership, which is stated as a fusion property, is for product and coproduct the fusion property with split and cojunc. So, membership may be a new concept for relators whereas the fan-function is already an existing notion for product and coproduct. The fan-functions of the regular relators can be constructed inductively. Lemma 4.47 clauses: Id Proj KA The canonical fan-functions of the regular relators are given by the following Id Proj ;\(R: C t- Bl. TT A,B -Ix FG t FG ;\R: At- B . [id A @; R@id B] where "..." denotes the cojunc operator defined by R... S = (W 'V So 1° and T is the tree type induced by binary relator @. Proof The verification of the case KA is straightforward. The verification of the case Proj - which includes Id - follows from lemma (4.25) where we have proved that n(mem.Proj\Rl = ProjR The verification of the definition of the fan-function of "+" and "x" is a direct corollary of properties (2.47) and (2.64). Verification of the case FG follows from lemma (4.29). And finally, the verification of the fan-function of tree type T is a directly corollary oflemma (4.36). o Since we have the identity FidL'.k A Corollary 4.48 (fan.Fl A , we have as a corollary, The (canonical) fan of a regular relator is given by the following clauses: fanJd id fan.Proj id fan.K A = TT A,_ 89 4.5. FANS fan.+ fan.x fan.FG fan.T id ... id id 6 id F(fan.G) . fan.F [id0; fan.0] o Just as for cojunc, cosplit and cocatamorphism, we define the notion of a cofan-function, denoted by F, as the converse-conjugate of the fan-function. That is, we define Fby FR = (F(RO))O for each R : LkB ~ 4.5.4 A. Recoverable relators In this section we show that coproduct, product and the projection relators can be recovered from their fan-functions with their membership relations. This property we later exploit in Chapter 5 and it enables us to use the slok-theory as introduced in Chapter 3. We have the properties, and All three properties are easily to verify. Hence, in the case of coproduct, product and projection relators, the relator can be recovered from its fan-function with its membership relation. Chapter 5 A Class of Commuting Relators 5.1 Introduction The zip function is well known to functional programmers [9]. Informally, zip maps two lists of the same length into a single list of pairs whereby Uu, ,Ul , ...J , [b j ,bl, ...J) H [( U, ,bJl , (Ul' b1l , ...J Zip is just one of a whole family of operations that commute the order of two data structures. Whilst zip commutes a pair of lists into a list of pairs, it is not difficult to imagine generalising zip to a function that commutes m lists each of length n into n lists each of length m. Indeed, this latter function is also well known under the name matrix transposition. With a little bit more imagination several other members of the family suggest themselves. For example, there is a function that commutes a tree of lists all of the same length into a list of trees all of the same shape. There is also a function that "broadcasts" a value to all elements of a list -thus (U, [b, ,b1 , ...J) H [( u, b,l, (u, b 1 ), ...J That is, the datatype an element of type A paired with (a list of elements of type B) is "commuted" to a list of (element of type A paired with an element of type B). This list broadcast is itself an instance of a subfamily of the operations that we discuss in this chapter. In general, a broadcast operation copies a given value to all locations in a given data structure. The construction of individual members of this class of operations (for example the zip function on pairs of lists or matrix transposition) is a straightforward programming exercise. Constructing, in one go, a class of such functions, having an infinite number of elements, is much more exacting, and demands a good theory of datatypes that facilitates the formulation of a compact and workable generic specification of the members of the class of functions. In this chapter we consider the construction of such functions. We specify the class of so-' called "zippable" data-structures and consider in detail a large and significant subclass of this class. For this subclass we show how to construct "zips" and identify several consequences of their being "zippable". A substantial portion of the chapter is devoted simply to formulating requirements for what it is for something to be a generic "zip" of two datatypes. (We use the terminology "require91 92 CHAPTER 5. A CLASS OF COMMUTING RELATORS ment" because, at this stage in our work, we are unable to prove that our "requirements" completely characterise zips. That is, we have no generally valid uniqueness property of solutions to our requirements.) The techniques we use are, to the best of our knowledge, original and, so, perhaps not easy to grasp on a first encounter. To ease the understanding we build up to the requirement for the most general case in successive stages. We also present several non-trivial consequences of the requirement giving greater insight into their nature. The simplification we make in the first instance is that all datatypes are endorelators. That is, we do not consider datatypes having more than one type parameter. This initial formulation is progressively refined to the case that the two datatypes to be "zipped" have arbitrary, not necessarily matching, arities. Another simplification that we make is to base our requirements on the notion of a "half-zip". The latter simplification is of a different nature to the former, it being a generalisation of the notion of a zip rather than a specialisation. Having constructed our requirement we then proceed to show that it is met by the regular datatypes. 5.2 The requirement (endorelators) The first problem we have to tackle is that of formulating a specification of the family of "zip" functions. As stated before, we begin by restricting our attention to endorelators like list and tree. Of course, this is inadequate for our ultimate goal-even the standard example of zipping a pair of lists is excluded by this restriction since product (pairing) is not endo- but we shall see that our requirements in their final form reduce directly to the form given in this subsection if all information on the arity of the relators is hidden from view. Looking again at the examples above, the first step towards an abstract problem specification is clear enough. Replacing "list", "tree" etc. by "relator F" the problem is to construct an operation zip.F.G for given relators F and G that maps F-G-structures into G-F-structures. The first step may be clear enough, subsequent steps are less clear. One complication is that "zips" are typically partial, being defined only on F-structures of (G-structures of the same shape) and not on all F-G-structures. Because of the level of generality that we are seeking it is not immediately evident how to formulate a precise specification. Rather than falling into the trap of basing our specification on operational considerations such as the effect on the shape of the given structures- we base our specification on abstract, algebraic properties. The nature of our requirements is influenced by Reynolds' insights into the relationship between parametric polymorphism and naturality properties [43]. Reynolds' idea as popularised by Wadler [52] is that if a function is parametrically polymorphic in a type then it is possible to derive from its type a property of the function (a "theorem for free" as Wadler called it). For example, any parametrically polymorphic function ex. such that ex.A has type ListA (-- TreeA for all A must satisfy the naturality property ListR . ex.B ;;:> ex.A· TreeR for all relations R oftype A (-- B. This, according to Reynolds, expresses the requirement that ex.A should be independent of the representation of elements A, or that there is a "logical relationship" between the different instances of ex.. 93 5.2. THE REQUIREMENT (ENDORELATORS) Our exploitation of Reynolds' insight takes place at a higher level. We consider the relator F to be fixed and specify a collection of operations zip. F. G indexed by the relator G. (The fact that the index is a relator rather than a type is what we mean by "at a higher level".) Such a family forms what we call a collection of "half-zips". The requirement is that the collection be "parametric" in G. That is, the elements of the family zip.F should be "logically related" to each other. The precise formulation of this idea leads us to three requirements on "half-zips". The symmetry between F and G, lost in the process of fixing F and varying G, is then restored by the simple requirement that a zip is both a half-zip and the converse of a half-zip. The division of our requirements into "half-zips" and "zips" corresponds to the way that we construct zips later in this chapter. We construct zip.F.G for each relator F in the class of the regular relators and an arbitrary relator G. (This construction is inductive over the structure of the regular relators.) That is to say, for each regular relator F we construct the mapping zip.F on relators, which for an arbitrary relator G gives the corresponding zip operation zip.F.G. The mapping is constructed to meet the requirement that it define a collection of half-zips; subsequently we show that if the collection is restricted to regular relators G then each halfzip is in fact a zip. 5.2.1 Naturality requirements Our first requirement is that zip.F.G be natural. That is to say, its application to an F-Gstructure should not in any way depend on the values in that structure. So, we demand (5.1) zip.F.G : GF (- FG Note that we have chosen zip.F.G to be a natural transformation with equality since for a zip operation on a structure no loss or duplication of values should occur. Demanding naturality is not enough. Somehow we want to express that the members of the family zip. F of zip operations for different datatypes G and H are related. For instance, if we have a natural transformation (X: G (- H then zip.F.G and zip.F.H should be coherent with the transformation (x. That is to say, having both zips and (x, there are two ways of transforming F-H-structures into G-F-structures; these should effectively be the same. One way is first transforming an F-H-structure into an F-G-structure using F(X, (i.e. applying the transformation (X to each H-structure inside the F-structure) and then zipping the F-Gstructure into a G-F-structure using zip.F.G. Another way is first zipping an F-H-structure into a H-F-structure with zip.F. H and then transforming this H-structure into a G-structure (both containing F-structures) using (XF. SO, we have the following diagram. FGA • F(XA zip.F.G A FHA zip.F.H A GFA-·--HFA 94 CHAPTER 5. A CLASS OF COMMUTING RELATORS One might suppose that an equality is required, i.e. aF' zip.F.H = zip.F.G· (5.2) Fa for all natural transformations a : G f- H. But this requirement is too severe for two reasons. The first reason is that if a is not functional, i.e. a non-deterministic transformation, the rhs of equation (5.2) may be more non-deterministic than the lhs because of the possible multiple occurrences of a. Take for instance F:= List and G = H := x, i.e. zip.F.G and zip.F.H are both the inverse of the zip function on a pair of lists, and take a:= id u swap, i.e. a nondeterministically swaps the elements of a pair or not. Then aF . zip.F. H unzips a list of pairs into a pair of lists and swaps the lists or not. On the other hand, zip. F. G . Fa first swaps some of the elements of a list of pairs and then unzips it into a pair of lists. The second reason is that, due to the partiality of zips, the domain of the left side of (5.2) may be smaller than that of the right. As a concrete example, suppose listify is a polymorphic function that constructs a list of the elements stored in a tree. The way that the tree is traversed (inorder, preorder etc.) is immaterial; what is important is that listify is a natural transformation of type List f - Tree. Now suppose we are given a list of trees. Then it can be transformed to a list of lists by "listify"ing each tree in the list, i.e. by applying the (appropriate instance of the) function List(listify). If all the trees in the list have the same shape, a list of lists can also be obtained by first "zipping" the list of trees to a tree oflists (all of the same length) and then "listify"ing the tree structure. That is we apply the (appropriate instance of the) function listifYList' zipList.Tree. The two lists of lists will not be the same: if the size of the original list is m and the size of each tree in the list is n then the first method will construct m lists each of length n whilst the second method will construct n lists each of length m. However the two lists of lists are "zips" of each other ("transposes" would be the more conventional terminology). This is expressed by the commutativity of the following diagram in the case that the input type List(TreeA) is restricted to lists of trees of the same shape. List(ListA) (zi pListList) A List(listifY)A List(TreeA) (zipList.Tree)A List(ListA) .., - - - - - - Tree(ListA) listifyListA Note however that if we view both paths through the diagram as partial relations of type List(ListA) f- List(TreeA) then the upper path (via List(ListA)) includes the lower path (via Tree (ListA)). This is because the function List(listifY)A may construct a list of lists all of the same length (as required by the subsequent zip operation) even though all the trees in the given list of trees may not all have the same shape. The requirement on the trees is that they all have the same size, which is weaker than their all having the same shape. Both examples show that we have to relax requirement (5.2) using an inclusion instead of equality. Having this inclusion, the requirement for a can be relaxed as well. So, the require- 5.2. THE REQUIREMENT (ENDORELATORS) 95 ment becomes aF' zip.EH 5.2.2 ~ zip.EG . Fa for all a: G f---' H (5.3) Monoid homomorphism For our next requirement we consider the monoid structure of functors under composition. Fix functor F and consider the collection of zips, zip. EG, indexed by (endo)functor G. Since the (endo)functors form a monoid it is required that the mapping zip.F is a monoid homomorphism. In order to formulate this requirement precisely we let ourselves be driven by type considerations. The requirement is that zip.EGH be some composition ofzip.EG and zip.EH of which zip.Eld is the identity. But the type of zip.EGH, zip.F.GH : GHF f- FGH , demands that the relator F has to be "pushed" through GH leaving the order of G and H unchanged. With zip.EG we can swap the order of F and G, with zip.EH the order of F and H. Thus transforming FGH to GHF can be achieved as shown below. GHF ~(zip.f Hl GFH \zip.F.Gl H FGH So, we demand zip.EGH = G(zip.EH) . (zip.EG)H Formally, define (for fixed relator F) a monoid M as follows. The elements are pairs consisting of a natural transformation, a, and a functor, G, where (a,G)EM == Fun a:GF-FG Define composition in the following way: (a,G)o(I3,H) ~ (GI3' aH, GH) Note that (G 13 . aH , GH) is an element of C since G 13 . aH GHF f - FGH follows from 13 : HF f- FH and a : GF f- FG. It is clear that "0" has unit (id F , ld) and is associative, i.e. ((a,G)o(I3,Hllo(y,l) = (a,G)o((I3,H)o(y,lll since GHy . (GI3 . aHlr = G(Hy . I3r) . aHr. Thus M is indeed a monoid. Now, define fiG) = (zip.EG, G). Then the mapping zip.F is a monoid homomorphism if f( GH) = f( G)of(H) and f(ld) = (id F , ld). Expanding the definition of f, we thus demand zip.EGH = G(zip.EH) . (zip.EG)H (5.4) zip.Eld = id F (5.5) and (Note that id F : IdF f- FId.) CHAPTER 5. A CLASS OF COMMUTING RELATORS 96 5.2.3 Half zips and commuting relators Apart from the very first of our requirements «5.1), the requirement that zip.F.G be natural), all the other requirements have been requirements on the nature of the mapping zip.F. Roughly speaking, (5.3) demands that it be parametric, and (5.4) and (5.5) that it be functorial. We find it useful to bundle these requirements together into the definition of something that we call a "half-zip". Definition 5.6 (Half Zip (Endorelators)) Consider a fixed relator F and a class of relators 9 that includes at least the identity relator and is closed under composition. Then the members of the collection zi p. F. G, where G ranges over 9, are called half-zips iff, (a) zip.F.G : GF ~ (b) aF' zip.F.H (c) zip.F.GH (d) zip.F.Id ~ FG, zip.F.G . Fa for each a: G f-' H, = G(zip.F.H) . (zip.F.G)H, = id F . o Given the notion of a half-zip, we can recover the natural symmetry between the parameters F and G by defining a zip to be something that is such that both it and its converse are half-zips. Definition 5.7 (Commuting Relators (Endo)) The half-zip zip.F.G is said to be a zip of (F, G) if there exist collections of half-zips, zip. F and zip. G, such that zip.F.G = (zip.G.F)O We say that relators F and G commute if there exists a zip for (F , G). o This completes our list of requirements for endorelators. 5.3 Analysis of the requirement Before generalising the requirements on zips to non-endo relators we interpose an analysis of the requirements. At first sight there is little connection between the formal and informal requirements given in the introduction to this chapter. For instance, the formal requirement makes no explicit mention of any shape preservation properties. The purpose ofthis section is to show that such considerations are indeed captured by the formal requirement. The discussion begins at an abstract level and then progresses to concrete examples of the properties of zips. We begin with a more detailed analysis of the parametricity requirement (subsection 5.3.1) , and then discuss how the requirements capture the shape preservation 5.3. ANALYSIS OF THE REQUIREMENT 97 of zips (subsection 5.3.2). This is followed by a technical discussion of some of the consequences of relators being commuting (subsection 5.3.3) in preparation for two concrete examples (sections 5.3.4 and 5.3.5). The message in these last two sections is that zips crop up in many unexpected places; indeed they are everywhere! 5.3.1 Higher-order parametricity In section 5.2 we derived part of the definition of a half-zip from the requirement that zip.F.G and zip.F.H should be coherent with respect to natural transformations of type G f- H. We referred to Reynolds' work on parametric polymorphism and logical relations as the inspiration for the requirement but claimed that our use of the notion was at a higher level. In this section we present requirement (5.3) in a more abstract way in order to provide support for our claim. In particular, we show that (5.3) follows from the requirement that the construction of the class of zip operations zip. F. G should be natural (parametric) in G. In order to see in what sense zip. F. G is natural in G let us denote it according to our convention for denoting natural transformations. Specifically, define J3G = zip.F.G. Thus J3 is a collection of arrows indexed by a relator. Now observe that the naturality property of I3G (property (5.1)) takes the form I3G ; (aF)G f- (Fa)G . (5.8) Recall that (0F) and (Fa) denote pre- and post-composition with relator F. That is, (oF) G = GF and (Fa)G = FG. Property (5.8) states that each individual element I3G of the collection of arrows 13 is a natural transformation. If the collection of arrows is itself a natural transformation then the mappings (0F) and (Fa) are the object maps of thefunctors I between which 13 is a natural transformation. In subsection 2.2.3, we showed that (0 F) and (Fa) are functors on natural transformations. The arrows maps of these functors are (0 F) a (= aF) and (Fa) a (= Fa), respectively. So, from J3G ; (0F) G f- (Fa) G we derive the typing of 13 as a (higher order) natural transformation: 13 : (oF) f- (Fa) Expanding the definition of a natural transformation gives us: (aF)a·I3H = I3G' (Fa)a for each a: Gf-H Rewriting this and expanding 13 = zip.F gives; aF' zip.F.H = zip.F.G . Fa for each a : G f- H (5.9) Equation (5.9), motivated here by type considerations, is not precisely the requirement on half-zips derived in section 5.2. As explained there, the conventional categorical notation of naturality is inadequate. In an allegory there are three kinds of natural transformation and care needs to be taken in deciding which is relevant. However, if all components involved are functions (5.9) is satisfied since inclusion of functions is equality offunctions. The restriction to functions is the framework in which Reynolds' ideas are usually formulated and thus (a specialisation of) our requirement does express the fact that zip.F.G is polymorphic in the (higher order) parameter G. 1That is to say, functors on the functor category. CHAPTER 5. A CLASS OF COMMUTING RELATORS 98 5.3.2 Shape preservation Zips are partial operations: zip.F.G should map F-structures of (G-structures of the same shape) into G-structures of (F-structures of the same shape). This requirement is, however, not explicitly stated in our formalisation of being a zip. In this subsection we show that it is nevertheless a consequence of the formal requirement. In particular we show that a half-zip always constructs G-structures of (F-structures of the same shape). Let us first recall how shape considerations are expressed. Recall that (F!Alx is the shape of the F-structure x, and F!A . f is the shape of the result of applying function f. Now, for a natural transformation (X of type F f-> G, the shape characteristics of (X in general are determined by (XI. since That is, the shape of the result of applying (XA is completely determined by the behaviour of (Xl. The shape characteristics of zip. F.G, in particular, are determined by (zip.F.Gh since GF!A' (zip.F.G1 A = (zip.F.Gh . FG!A Our shape requirement is that a half-zip maps an F-G-shape into a G-F-shape in which all F-shapes equal the original F-shape. This we can express by a single equation relating the behaviour of (zip.F.Gh to that offan.G. Specifically, we note that (fan.Gl fl generates from a given F-shape, x, a G-structure with an arbitrary shape in which all elements equal x, and thus have the same F-shape. On the other hand, F(fan.Gh, when applied to x, generates Fstructures with shape x containing arbitrary G-shapes. The shape requirement is thus satisfied if we can establish the property (fan.Gl fl = (zip.F.Gh· F(fan.Gh (5.10) This property is an immediate consequence of the following lemma. Lemma 5.11 If fa n. G is the canonical fan of relator G then, (fan.Gl F = zip.F.G . F(fan.G) Proof The inclusion ~ follows from the definition of half-zips (definition (5.6)) since fan.G : G f-> Id and zip.F.Id = Fid. For;:;:> we calculate: (fan.Gh ;:;:> zip.F.G . F(fan.Gl fan.G = mem.G\id (mem.G\idl F ;:;:> zip.F.G . F(fan.Gl { factors} id F ;:;:> (mem.Gh . zip.F.G . F(fan.Gl mem.G : id f--' G, definition half-zips zip.F (5.6) {= id F ;:;:> zip.F.id . F(mem.G . fan.Gl { zip.F.id = id F , monotonicity } 5.3. ANALYSIS OF THE REQUIREMENT id :;;;> 99 mem.G . fan.G { fan.G = mem.G\id, factors } true o From equation (5.10) it also follows thatthe range of (zip.F.Gh is the range of (fan.Gl n , i.e. arbitrary G-structures of which all elements are the same, but arbitrary, F-shape. Formally, ((zip.F.Ghl< definition fans (4.39): (fan.Gh = 1T Gl,l, hence, ((fan.Gh)< = Gid 1 } ((zip.F.Gh . F(fan.Gh <)< { domains, (5.10) ((fan.G)n)< Again, it follows from naturality of zip.F. G that the above interpretation of the shape of the range holds for zip.F.G A too: = (GF!A . (zip.F.G)A)< ((zip.F.Gh . FG!Al< = ((zip.F.Gh)< Note that nothing can be deduced about the shape of the G-structure from (5.10). As mentioned in section 5.2, a zip operation on an F-G-structure is apartial operation which is only defined on F-structures of (G-structures of the same shape). The requirement that a zip be the converse of a half-zip is a simple way of meeting this requirement: because zip. G. F is a half-zip, the domain of zip.F.G (which is the range of zip.G. F) consists of F-structures of (Gstructures of the same shape) and zip.F.G respects the shape of the G-structures. Thus a zip of (F , G) is a partial operation which maps an F-structure of (G-structures of the same shape) into a G-structure of (F-structures of the same shape). Furthermore, the G-shape of the result is the same as all the original G-structures, and the shape of all the F-structures contained in the G-structure is the same as the original F-structure. Before leaving lemma (5.11) let us note that it, together with naturality of zip.F.G, implies that zip.F.G has a slok property. Recall that G denotes the fan-function of relator G defined by GR = GR· fan.G B for R : A (- B. Then we have, GFR = zip.F.G A · FGR for each R: A(- B or, equivalently, A Siok A zip.F.G : GF - - FG The proof proceeds as follows (5.12) lOa CHAPTER 5. A CLASS OF COMMUTING RELATORS zip.F.G . FGR definition G zip.F.G . F(GR . fan.G) naturality zip.F.G GFR . zip.F.G . F(fan.G) lemma (5.11) GFR - (fan.G)F definition G} GFR 5.3.3 Commuting relators We say that a class of relators is commuting if the class contains Id, is closed under composition and all its elements are pair-wise commuting. The main concern of this subsection is to explore the naturality properties of zips defined on a commuting class of relators. As a preliminary observation we remark that, although requirement (5.6b) for half-zips is stated with an inclusion, we have an equality in some cases: Lemma 5.13 (a) For cofunction a and a : G '----+ H, aF' zip.F.H ;;:: zip.F.G . Fa , (b) For cofunction a and a : G ~ aF' H, zip.F.H = zip.F.G . Fa . Proof Part (b) follows from combining part (a) with (5.6b). For (a) we calculate: aF -zip.F.H ;;:: zip.F.G . Fa { a cofunction, shunting } zip.F.H . { ;;:: a F· zip.F.G ~ao aO : H f-' G, definition half-zips (5.6b) true D Combining the properties of both half-zips gives Lemma 5.14 in E." (a) zip.F.G Let E., denote a class of commuting relators. Then for all relators F, G and H GF ~ FG, 5.3. ANALYSIS OF THE REQUIREMENT (b) CXF' zip.F.H ~ zip.F.G . Fcx for each cx: G (----> H, (c) Fcx . zip.H.F :2 zip.G.F· (d) zip.F.GH 101 CXF for each cx : G '----1 H, = G(zip.F.H) . (zip.F.G)H, (e) zip.FG.H = (zip.F.H)G . F(zip.G.H), (f) zip.F.Id = id F = zip.Id.F, (g) zip.F.G = (zip.G.F)o. o Note that property (5.14c) implies the dual properties oflemma(5.13) and lemma (5.1 1). That is to say, we have Lemma 5.15 in E" Let E, denote a class of commuting relators. Then for all relators F, G and H (a) for function cx and cx : G f-' H, Fcx· zip.H.F ~ zip.G.F· CXF (b) for function cx and cx : G f- H, Fcx· zip.H.F = zip.G.F· CXF (c) if fan.F is the canonical fan of relator F then G(fan.F)O . zip.F.G = (fan.F)G o Note that property (5.l5c) together with naturality of zip.F. G implies the dual slok property of (5.12). Recall that the cofan function F is defined by FR = (fan.F)A . FR for R : A f - B, so FR = (f:(WW. SO, we havefor commuting relators (F, G) GFR . (zip.F.G)B = FGR for each R : A f- B, or, equivalently, zip.F.G is an arrow having a coslok property, zip.F.G : GF ~ 5.3.4 FG Strength In this section and the next we consider some more specific cases of "zips". Several scientists have argued that the notion of functor is too general to capture the notion of a datatype as understood by programmers. Moggi [39] claims that the notion of "strength" is fundamental to computation, "strength" being defined as follows. CHAPTER 5. A CLASS OF COMMUTING RELATORS 102 Definition 5.16 (Strength) A natural transformation strA,B : F(A x B) f-' FAx B is said to be a strength of relator F iff strA, B is a function that behaves coherently with respect to product in the following sense. First, the diagram F(A xl) . . . _ _s_tr_A.:...,l FAx 1 FA (where rid A : A gram f- A x 1 is the obvious natural isomorphism) commutes. Second, the dia- FAx (B x C) • assFA,B,e (FA x B) x C strA,B x ide F(Ax B) x C strA,Bxe strAxB,e F(A x (B x Cll . FassA,B,e F((A x B) x C) (where assA,B,e : A x (B x C) f- (A x B) x C is the obvious natural isomorphism) commutes as well. A relator that has at least one strength is said to be strong. o We have no idea why the term "strength" has been chosen to name this property of a relator. The idea behind it is however very simple. A relator F is "strong" if, for each pair of types A and B, it is possible to broadcast a given value of type B to every element in an F-structure of A's. The broadcasting operation is what Moggi calls the "strength" of the relator. Note that the requirement FridA' strA,l FoutlA,B . strA,B rid FA is equivalent to = outlFA,B since using rid A = outlA,l and outlA,B (5.17) = outlA,l = FoutlA,l . F(id A x . idFAx !B = outlFA,B FoutlA,B . strA,B = outlFA,l = . idA X !B !B) . strA,B = FoutlA,l . strA,l . idFAX !B The type of the strength strA,B of relator F is the same as the type of (zi p. (x B). F) A, namely F(A x B) f-' FAx B. We shall argue in this section that, iff and the family of relators (x B) 5.3. ANALYSIS OF THE REQUIREMENT 103 are included in a class of commuting relators, then any relation satisfying the requirements of (zip.( x B) .Fh also satisfies the definition of str A,B. Furthermore, we show in chapter 6 that a relator with membership has a unique strength. In other words, a strength of relator F with membership is the half-zip (zip.( x B) .FlA. Let us begin with an informal scrutiny of the definition of strength. In the introduction to this chapter we remarked that a broadcast operation (a "strength") is an example of a (half-)zip. Specifically, a broadcast operation is a zip of the form (zip.( x B). F)A. Paying due attention to the fact that the relator F is a parameter of the definition, we observe that all the natural transformations involved in the definition of strength are special cases of a broadcast operation and thus of zips. In the first diagram there are two occurrences of the canonical isomorphism rid. In general, we recognise a projection of type A f- A x B as a broadcast where the parameter F is instantiated to the constant relator KA. Thus rid A is (zip. (x 1). KA)B for some arbitrary B. In words, rid A commutes the relators (x 1) and KA . Redrawing the first diagram above, using that all the arrows are broadcasts and thus zips, we get the following diagram2 . F(A xl) (zip.(x1).F)KA +-.- - - - - - F';P.(Xl ~ FAx 1 ~Xl .(K'AI FA Comparing with (5.4) we see that the first coherence property of strengths is just an instance of the general requirement on half-zips that they respect composition of functors. Now we turn to the second diagram in the definition of strength. Just as we observed that rid is an instance of a broadcast and thus a zip, we also observe that ass is a broadcast and thus a zip. Specifically, assA,B,C is (zip. (x C). (Ax ))B. Once again, every edge in the diagram involves a zip operation! That is not all. Yet more zips can be added to the diagram. For our purposes it is crucial to observe that the bottom left and middle right nodes -the nodes la- 2To be perfectly correct we should instantiate each of the transfonnations at some arbitrary B. We haven't done so because the choice of which B in this case is truly irrelevant. 104 CHAPTER 5. A CLASS OF COMMUTING RELATORS belled f(A x (B x C)) and f(A x B) x C- are connected by the edge (zip.( x C).f(Ax ))B. fAx(BxC), (zip.(xC).(fAx))B (fAxB)xC (zip.(xB).f)A x ide (zip.(x(BxC)).f)A flAx B) x C / (zip. (x C). f(Ax))B / f(Ax(BxC)) , f(zip.( x C). (AX))B (zip.( x C) .f)AxB f((AxB)xC) This means that we can decompose the original coherence property into a combination of two properties of zips. The first property, represented by the bottom triangle, is just the requirement that zip. (x Cl respect composition of functors (requirement (5.4) once more). zip.(xC).f(Ax) = fzip.(xC).(Ax) . (zip.(xC).f)(Axl The second property is less easy to match with the requirements on zips. Reading off from the diagram (the inner path from the top right node to the bottom left node) we require that (zip.(x(BxC)).f)A· (zip.(xC).((fA)x))B (zip. (x C). f(Ax) lB· (zip.( x B). flAX ide Careful inspection reveals that it is an instance of requirement (5.3). To see this define (X by (XB = (zip.(xB).f)A and instantiate functors f, G and H to (xC), ((fA)x) and flAx), respectively. In order to apply (5.3) we require that (XB be natural in the parameter B. In section 5.9 we show that this is indeed the case. Now, applying (5.3) we obtain the inclusion (zip.(x(BxC)).f)A· (zip.(xC).((fA)x))B c (zip.(xC).f(Ax))B· (zip.(xB).f)Axid e (for all B) rather than equality. In section 5.9 we show that (zip. (x B).f)A is a function. Hence, equality follows because an inclusion between functions is equivalent to their equality. Let us summarise this section with a formal statement of what we have observed. The focus of our discussion has been a family of broadcast operations zip. (x C) indexed by C and some class of relators. We have observed that if this family is natural in the parameter C and, for each C, zip. (x C) defines a class of functional half-zips then each element zip. (x C). f is a "strength" of the relator f. In section 5.6 we show how to construct such a family of half-zips. By focusing on the parameter f in the definition of strength we have obtained a beautiful characterisation of the notion. 5.3. ANALYSIS OF THE REQUIREMENT 5.3.5 105 Structure multiplication Another example3 of the beauty of the "zip" generalisation is afforded by what we shall call "structure multiplication". A simple, concrete example of structure multiplication is the following. Given two lists [aj , az , ... J and [b l , b z , ...J, form a matrix in which the (i, nth element is the pair (ai, bj). We call this "structure multiplication" because the input type is the product ListA x ListB for some types A and B. Given certain basic functions, this task may be completed in one of two ways. The first way has two steps. First, the list of a's is broadcast over the list of b's to form the list [([al ,az, ...J, bd ,([al , az, ...J ,b z ) , ...J Then each b is broadcast over the list of a's. The second way is identical but for an interchange of "a" and "b". Both methods return a list of lists, but the results are not identical. The connection between the two results is that one is the transpose of the other. The two methods and the connection between them are summarised in the following diagram. ListA x ListB List(ListA x B) List(A x ListB) ListList(A x B) ._- .~ ListList(A x B) The generalisation of this procedure is obvious: replace ListA by FA and ListB by GB for some arbitrary relators F and G. Doing so leads to the realisation that every step involves a "zip" operation. This is made explicit in the diagram below. 3This example was communicated to Roland Backhouse by D.J. Lillie in December, 1994. 106 CHAPTER 5. A CLASS OF COMMUTING RELATORS FAx GB (,;P.(FAXI7 ~(XGBI.FlA G(FAx B) F(A x GB) / G(zip.(xB).FJA (zip.F(AxJ.G)B / GF(A x B) • (zip.F.G)AxB F(zip.(Ax).GJB FG(A x B) An extra edge has been added to the diagram in order to show how the commutativity of the diagram can be decomposed into smaller parts 4 . Thus we have reduced our proof obligation into two separate conditions. The first condition, the commutativity of the triangle in the bottom right of the diagram, is: (zip.F(Ax}.G)B = (zip.F.GJAxB . F(zip.(Ax}.G}B This is clearly property (5.l4e). (take G = F and H = (Ax)). The second condition, the commutativity of the remainder of the diagram, is: G(zip.(xB).F)A· (zip.(FAx}.G)B = (zip.F(Ax}.G}B· (zip.(xGB).F}A This is an instance of (5.l5b) (take (XB = (zip.(xB).F)A, F = G , G = F(Ax) and H = (FAx )). (Note that it is required that the broadcast operation (zip. (x B) .FJA is a function and natural in the parameter B. In section 5.9 we verify that this is indeed the case.) Formally the claim is that the diagram headed by the node FAx GB commutes provided that F and G are elements of a class of commuting relators that includes in addition all relators of the form (A x) and (x B) and such that the broadcast operations (zip. (x B).F)A are functions and natural in B. (Commutativity of the symmetric diagram requires that the broadcast operations (zip.(xB}.GJA are also functions.) 5.4 The requirement (arbitrary relators) Until now, we have said nothing about zips for non-endo relators. We assumed that both compositions FG and GF existed, hence F and G are endorelators of the same type. For the regular relators we have to include non-endo relators as well. For example, the standard zip function on a pair of lists, i.e. zip. x .List, mentioned in the introduction has type (zip.x.ListlA,B : List(AxB) ~ListAxListB 4The additional edge together with the removal of the right-pointing edge in the bottom line seem to make the diagram asymmetric. But, of course, there are symmetric edges. Corresponding to the added diagonal edge there is an edge connecting G (FAx B) and FG (A x B) but only one of these edges is needed in the argument that follows. 5.4. THE REQUIREMENT (ARBITRARY RELATORS) 107 So, zi p. x .List is a natural transformation of type List( x ) (- ( x )Lisr. Recall that" _2" denotes the doubling functor on relators, Le. F2 (R , S) = (FR) FS). For the definition of zip.F.G we want to include the case that F and G are single-valued. For the remainder of this section, we assume that F and G are single-valued. Furthermore, we assume for the moment that G is an endorelator as is the case for the example. The definition of zip.F.G for a relator F of type 1 (- k is then derived from the original definition by replacing the composition FG by F( G k ) and doing the same for related compositions of relators and natural transformations. So, for a single-valued relator F the definition of a half-zip becomes: Definition 5.18 For relator F of type 1 (- k and G and H endorelators, (a)zip.F.G: GF(-F(G k ), (b) (XF . zip, F. H ~ zip. F.G . F( (Xk) for each (X : G f--J H, (c) zip.F.GH = G(zip.F.H) . (zip.F.G)w, (d) zip.F.Id = F(id k ). D For instance, take the case of a binary relator (». Filling in the objects, the definition reads (a) (zip.(».G)A,B : G(AI8!B) (- GAI8!GB, (b) (XA@B' (zip.I8!.HlA,B ~ (zip.I8!.G)A,B . (XAI8!(XB for each (X; G f--JH, (c) (zip.I8!.GH)A,B = G(zip.I8!.HlA,B· (zip.I8!.G)HA,HB , Similarly, if we take for F an endorelator and we allow non-endo relators of type 1 (- k for the second component of the zip function we have to replace the composition GF by G (F k ) and do the same for related compositions. Furthermore, since we have included the projection relators as well, we add the requirement that zip.F is also coherent with projections. For a projection relator Proj ; 1 (- k, the projection on a element of a vector of length k, we demand: zip. F. Proj = idF(Proil (5.19) The identity relator is a projection relator on a vector of length I. Hence, requirement (5.19) is a generalisation of the requirement that zip. F respects identities. So, we replace condition (d) by requirement (5.19). The extended definition of zip.F becomes; Definition 5.20 For endorelator F, (a) zip.F.G : G(F k ) (- FG, for G ; 1 (- k, 108 CHAPTER 5. A CLASS OF COMMUTING RELATORS (b) CXFk . zip.F.H C;;; zip.F.G . Fcx for each cx: G f-' Hand G , H : 1 f- k. = G(zip.F.H) (c) zip.F.GH (d) zip. F. Proj = . (zip.F.G)H forendo G and H: 1 f- k, idF(Proj) for each Proj : 1 f- k. o Again, instantiating the second argument of zip with a binary relator and filling in the objects gives: (a) (zip.F.iZI)A,B : FA@FB f- F(AiZlB) (b) CXFA,FB' (zip.F.EB)A,B C;;; (zip.F.iZI)A,B . Fcx for each cx: iZI f-' EB. (d) (zip.F.OutllA,B = id FA and (zip.F.Outr)A,B = id FB Next we give the definition of zip for both arguments being not necessarily endorelators. A first attempt would be to take the combination of both definitions but this is too simple. For instance for F : 1 f- k and G : 1 f- 1 the naturality requirement becomes: but G(F I ) and F( G k ) do not have the same type: the source of the fonner is h1 whereas the source of the latter is h k. Of course, the relationship between both types is matrix transposition'T. In other words, we take G(FI)'T instead of G(F I ). Note that for single-valued F, we have G[Fl)'T = G(lF). Now, the definition of zips for single valued relators is derived from the original one by replacing FG by F( G k ), and the same for related compositions of relators and natural transformations., and replacing GF by G (IF), and the same for related compositions. Definition 5.21 For relator F : 1 f - k, the members ofthe collection zip. F.G, for each singlevalued relator G, are called half-zips iff, (a) zip.F.G : G(IF) f- F(G k ), for each G : 1 f-1 (b) CX(lF) . zip.F.H C;;; zip.F.G . F(cxk ) for each cx: G f-' Hand G, H : 1 f-1, (c) zip.F.GH = G(zip.F.H) . (zip.F.G)Hk for G : 1 f-1 and H : 1 f-1, (d) zip. F. Proj = idF(Projk) for each Proj : 1 f- 1. o Since the definition of zip.F.G for non-endo relators is derived from the original one by replacing relator composition by adding "_k" and "1_" at the appropriate places, results for the non-endo case can be derived from results for the unary one by making the same replacement. These adjustments follow from arity considerations. 5.4. THE REQUIREMENT (ARBITRARY RELATORS) 109 Commuting relators Since we have generalized the definition of half-zips, we have to generalize the definition of commuting relators. Note that for F : 1 f- k and G : 1 f- 1, we have zip.F.G: G(lF)f-F(G k) and (zip.G.F)O : G(Fl)f-F(kG) sofornon-endoFand G they do not have the same type, hence, we can not demand zip.F.G = (zip.G.F)o. However, the relationship between G(lF) and G(F l ), and between F(G k ) and F(kG) is matrix transposition, i.e. the relator 'I", since Hence, we have (zip.GF)~ tion becomes: : G[Fl)H- F(kG)'I" = G(lF) f- F(G k). So, the general defini- Definition 5.22 The half-zip zip.F.G for single-valued relators F and G, is said to be a zip of (F, G) if there exists a half-zip zip.G.F such that zip.F.G = (zip.GF)~ o 5.4.1 Multi-valued relators Until now we have assumed that both relators F and G are single valued, i.e. relators of type 1 f- k, for some k. Next we define zip.F.G for F : mf- k and G : nf-l. We try to extend the definition (5.21). From arity consideration, it follows that we should generalize F( G k) to (nF)(G k). Similarly, we take (GffiJ[lF) instead of G(lF). Making the same adjustments for related compositions of relators and natural transformations yields the following definition. Definition 5.23 For relator F : mf- k, the members of the collection zip.F.G, for each relator G : n f- 1, are called half-zips iff, (a)zip.F.G: (GffiJ[lFlf-(nF)(Gk),foreachG:nf-l (c) zip.F.GH = (Gffi)(zip.F.H) . (zip.F.G)Hk for G : nf-1 and H : If-o, (d) zip.F.Proj = idF(PTojk) for each Proj : 1 f-l. [J From property (5.23c) and (5.23d) it follows that the mapping zip.F is coherent with tupling in the following sense. Lemma 5.24 For relators F : m f- k and G : n f- 1, we have, Proof We aim to use the universal property for arbitrary products, zip.F.G = 'I"L1n zip.F.G n CHAPTER 5. A CLASS OF COMMUTING RELATORS 110 't isomorphism } 'tzip.F.G = L1n zip.F.G n universal property L1n } Proj'tzip.F.G = zip.F.G n Proj't = Projm; (Projm)(nF) = F(Projk), property (5.23d) } (Projm)zip.F.G· (zip.F.Proj)Gk = zip.F.G n compositionality (5.23c) } zip.F.ProjG = zip.F.G n ProjG = Gn } true o So, from lemma (5.24) it follows that zip.F.G can be expressed in terms of the collection of half-zips zip.F.G n , half-zips for which the second argument is a single-valued relator. It is even the case that we can define zip. F. G in this way: For fixed relators F : m Lemma 5.25 zip.F.G ~ f- k and for each relator G : n f- 1, 'tL1n zip.F.G n defines a collection of half-zips given that zip.F. H is a half-zip for each single-valued relator H. Proof Since 't and L1 n are functors it follows from zip.F.G n : (G n m) (tF) 'tL1 n zip.F.G n : 'tL1n (G n m)(tF) f- 'tL1n F(G n k ) , since 'tL1 n (Gn m) = G m and 'tL1n F( G n k) 'tL1n zip.F.G n : (Gm)(tF) as required by (5.23a). ex: G (-> H, (exm)(tFl' zip.F.H n ~ f- = f- F( Gn k) that (nF)(G k ), we have, (nF)(G k ) , For requirement (5.23b) we calculate, for each H : nf-1 and zip.F.G n . F(exk ) we calculate zip.F.G . (nF)(exk ) definition zip.F, exm = 'tL1n (exnm), (nF)( exk ) = 'tL1n F( exnk) } 'tL1n (exn m )(tFl . 'tL1n zip.F.Hn ~ 'tL1n zip.F.G n . 'tL1n F(exn k ) (exm)(tFl ·zip.F.H ~ 'tL1n relator } m 'tL1n ((exn )(tFl . zip.F.H n ) ~ 'tL1n (zip.F.G n · F(exn k )) 111 5.4. THE REQUIREMENT (ARBITRARY RELATORS) { 'T isomorphism; product } (anml(IFl' zip.F.H n S;; zip.F.G n . F(an k ) { H n and Gn single-valued, assumption true Requirement (5.23c) we verify by, for G : n f-1 and H : 1f- 0, zip.F.GH definition zip.F, (GH)n = GnH } { 'T~nzip.FGH = GnH single-valued, assumption: (5.23c) } 'T~n(G m)(zip.F.Hl . (zip.F.GnlHkl 'T~n { 'T~n(Gm)zip.FH functor } . 'T~n(G { 'T~n(zip.FG)Hk m) = Gm , definition zip.F (Gm)(zip.F.H) . (zip.F.G1Hk Finally, requirement (5.23d) holds by assumption since Proj is a single-valued relator. o The importance of lemma (5.25) is that for the construction of zip. F.G we can restrict our attention to the cases that G is single-valued. In the next section we show that zip. F. G is also coherent with tupling in the first argument. Hence, combining both coherence properties it follows that an arbitrary zip.F.G can be defined in terms of the collection of half-zips zip.Fm.G n , half-zips both arguments of which are single-valued relators. Commuting relators Just as for the generalisation for single-valued relators, we have to extend the definition of commuting relators. From arity considerations, we derive that 'T(zip.G F)~ has the same arity as zip.F.G. Hence, we define, Definition 5.26 The half-zip zip.F.G for arbitrary relators F and G, is said to be a zip of (F, G) if there exists a half-zip zip.G.F such that zip.F.G = 'T(zip.GF)~ o Fans of single-valued relators Although we claimed that results for arbitrary relators can be derived from the endo case, one has to be careful when translating results which mention the fan. The fan of an endorelator F is a natural transformation of type F (----' Id. The fan of an (----' Id since the fan of a non-endo relator is by defiarbitrary relator F : 1 f- k has type F~k nition the fan of the endorelator F~k' For instance, from the generalized version of lemma (5.11) it follows that for F : 1 f- k and G : 1 f- 1, (fan.G)F = zip.FG~l . F(fan.G)k 112 CHAPTER 5. A CLASS OF COMMUTING RELATORS Equivalently, since from (5.23c) and (5.24) it follows that zip.F.Gt. 1 = Gt.dzip.F.ld) . (zip.F.G)(t>tlk = (zip.F.G)(t>tlk , we have, (fan.Gh = (zip.F.GJ(t>tlk . F(fan.G)k (5.27) Fortunately, the results for single-valued relators which mention the fan-function are still derived by adding" _k" or "1_" at the right place since a fan-function of a relator has the same type as the relator. However, one has to remember that a fan-function is a partial function: the fan-function 1= for relator F of type 1 f- k is only defined for a vector of relations which all have the same source. For instance, for arbitrary relators the translation of property (5.12), i.e. GFR = zip.F.G . FGR for each R : A f- B and its proof, reads for F : 1 f- k and G : 1 f- 1, G(lF)R = (zip.F.Gl A · F(Gk)R foreachR: Af-((t.tlk)B since, (zip.F.G)A . F(Gk)R { definition G, _k functor } (zip.F.G)A . F((Gk)R . (fan.G)\) { G(lF)R { G(lF)R { naturality zip.F.G } (zip.F.G)((t>tlk)B· F(fan.Gl\ property (5.27) (fan.G1 FB definition G } G(lF)R 5.5 The zip-a-dee-doo-dah theorem We are now ready to formulate a general theorem about the existence of zips. We call the theorem the zip-a-dee-doo-dah theorem after the song "Zip-a-dee-doo-dah, zip-a-dee-ay" because it seemed such a wonderful day when the theorem was first discovered (at least to us anyway)! The inspiration for the theorem came from Fokkinga's Ph.D. thesis [16, chapter 4, p 90 onwards]. Fokkinga considers only one particular case - the construction of a function that transposes a stream of lists into a list of streams - but it was clear from his calculations that they could be made much more general if the step were taken to a higher level of abstraction. Theorem 5.28 (Zip-a-dee-doo-dah) For all regular relators F, there are zip.F.G, for each relator G with membership, such that the collection zip.F.G are half-zips. Furthermore, the regular relators are commuting, i.e. if G is also a regular relator then zip.F.G = -r(zip.G .~)F D 113 5.5. THE ZIP-A-DEE-DOO-DAH THEOREM As we mentioned before, we show how to construct zip.F.G for each regular relator F and an arbitrary relator G. This construction is inductive over the structure of the regular relators. We first construct "candidate" half-zips. The construction of "candidate" half-zips is dictated most of the time by the desire to meet one of the properties which followed from the fact that (F , G) should commute. For instance, for the construction of the candidates zip for the identity and projections relators, product and coproduct, we use the derived coslok property for commuting relators (F , G), for F : 1 f- k and G : 1 f- 1, G(lf)R· zip.F.G B = f(Gk)R foreachR:Af-B. (5.29) For the construction of a zip of a composition, we aim to meet the requirement that (FG , H) should commute. For the construction of a candidate zip for a tree type, we use property (5.15b). Subsequently, we verify that the construction we give satisfies the clauses in the definition of half-zips. After the construction of the half-zips zi p.F. G for all regular relators F, we verify that the regular relators are commuting. Recoverable relator If a relator F : 1 f- k is corecoverable from its cofan-function with (3, i.e. (3A : L1kFA f- A and f(3 = id F, many proofs for the verification of the Zip-a-dee-doodah theorem with respect to zip.F follow for free. Recall the uniqueness property (3.21), that G for mappings F and G, and the functor F' is corecoverable from is to say, if <X : F ~ F with (3 then <X = G(3. Hence, it follows that the only possible candidate for zip.F.G, for F: 1 f-kandG: 1 f-l, which can satisfy equation (5.29), i.e. hastypeG(lf) ~ f(G k), is f(Gk)(l(3) since G(lF) is corecoverable from G(lf) with 1(3: G(lf)(l(3) = G(l(f(3)) = G(l(id F)) = idG(lF) . Note that (l(3)A has type L1 k (lF)A f- A, for each A, since l(L1 kF) = L1kflf) for single-valued F. If the candidate f( G k)(1(3) has indeed the coslok property (5.29) then the naturality of zip. F.G follows directly from lemma (3.22) since relator F( G k) is cofuseable into f( Gk ) since a relator is always cofuseable into its cofan function. Furthermore, from type considerations it follows that zip.F is compositional. Since the category CoSlok obeys the same typing rules as the functor category Fun, it follows that for G : 1 f- 1 and H : 1 f- m ~ G(zip.F.H) . (zip.F.G)Hk : GH(mF) CoSlok +-- ~ F(GH) k but zip.F.GH is the unique arrow with the coslok property GH(mf) f- f( GH)\ hence, zip.F.GH = G(zip.F.H) . (zip.F.G)w . Similarly, we have zip.F.Proj = idf(Projk) since for Proj : 1 f-l 114 CHAPTER 5. A CLASS OF COMMUTING RELATORS From the naturality of a it follows that the candidate F( G k) (113) is higher-order natural, i.e. a(lF)' F(H kWf3) ~ f(G kWf3)' F(ak) foreacha:G(----'HandG,H:lf-l. For the cofan-function we have that R . FS ~ formation of type Id'-1 Ft.. k. Hence, F(t..dR) . S) since (fan.F)O is a natural trans- a(lF) . F(H kWf3) remark above, t..da(lF)) = (ak)t..d1F) } F(akL'.k(lF) . (H k J[lf3)) { a k : Gk(----'H k, (lf3)A : t..k(lF)Af-A { C F((G k )(lf3) . a k ) F-F cofusion k F(G )(l13) . F(a k) Finally, it follows that (F , G) for single-valued relator G commute. From the slok property of half-zips (5.12) it follows that (zip.GF)~ has the coslok property G(mF) ~ F(G k ) too. Hence, from the uniqueness property (3.21) it follows that zip.F.G = (zip.GF)~ . Collecting all the results together, we have the following lemma Lemma 5.30 If relator F is corecoverable from F with 13 the only possible candidate for zip.F.G, for each single-valued relator G, is F( G kJ[l(3). If the candidate half-zips F( Gk )(l13) satisfy the cos10k properties G(mF) ~ F(G k), for each G, then the collection of natural k transformations F( G Wf3) are half-zips off. Furthermore, if the half-zip zip.G. F exists then F and G commute, i.e. zip.F.G = (zip.GF)~ o 5.6 Constructing candidate zips We are ready to embark on the task of constructing zip. F.G for regular relator F and arbitrary relator G. Note that we can restrict ourselves to single-valued relators G; from lemma (5.24) it follows that the zip.F. G for an arbitrary relator G : m f- 1can be constructed in terms of the collection zip. F. G m. Hence, in the remainder of this section we only consider single-valued relators G. The zips we construct are, at this point in time, candidate zips. In the next section we vet them against the clauses in the specification of a zip. The construction is by induction on the structure of the regular relators. That is, we show how to construct zip.Id.G, zip.Proj.G, zip.KA.G, zip.+.G and zip.x.G for single-valued relator G. We show how to construct zip.FG.H by composing the zips zip.F.H and zip.G.H. And we show how to construct zip.t..mFm.G in terms of the collection of zips zip.Fm.G. Finally, we construct zip.T.G for relator T from a tree type induced by the binary relator 0 given that zip.0.G has already been constructed. 115 5.6. CONSTRUCTING CANDIDATE ZIPS 5.6.1 Zipping identity, projections, coproduct and product Recall that the fan-functions of the projections - which includes the identity relator - , coproduct and product are recoverable from their corresponding membership relation. Or, dually, for relator F being a projection, coproduct or product, we have that F is corecoverable from the cofan function F with f3 ~ (mem.F)O since Ff3 = id F • This means that we can use lemma (5.30) to construct the candidate zips for the projections, coproduct and product. Recall that Proj; : 1 (- k has membership relation ,1di = k-+ id proh ,l.lProjk, Proh) , and coproduct and product have membership relations (inlO , inrO) and (outI, outr), respectively. So, lemma (5.30) gives us the following candidate zips, for G : 1 (- 1, Proji( Gk W(,1k(i = k -l id proh ,l.lProjk, ProjJ)) ('V)(G 2 )(l(inl,inr)) (.t.)(G 2 )(l(outIO ,outre)) zip.Proh,G zip.+.G zip.x.G = The candidate for zip.Proh,G can be simplified, Proji( Gk W(,1k(i = k -l id proh ,l.lProik' Proh))) Proh(G k) = G(lproh)' "l_" functor } { G(l(Proji,1k(i = k-l id proh ,l.lProh, Proh))) Proj;,1k Rk = Ri } G (l(idprojJ) identities, G(lproj) = Proj( Gk) } The candidates zips for coproduct and product, we can simplify too, and similarly Note that linl, linr : e l (- e l x e l are injections in the allegory el. We take (ninl ,ninr) as the canonical injections for allegory en, for all n. That is to say, we define coproduct, and similarly product, component wise in en. So, we define inl ~ ninl : en (- en x en, inr~ ninr : en (-en x en and 'V~ n('V) : en (-en x en, and similarly for product. Hence, we have zip.+.G zip.x.G = Ginlto. Ginr GoutlO .t. GoutrO (5.31) (5.32) where (inl, inr) and (outl , outr) denote the injections and projections, respectively, in the allegory of the source of relator G. CHAPTER 5. A CLASS OF COMMUTING RELATORS 116 For the above candidates we have to verify the coslok properties, for each R : L1 k B r- A, G(lproj;)R· idpTOj,(Gk)A = ProUGk)R (5.33) and, for each R : C r- A, S : C r- B, G(R7S) . GinIA,B7GinrA,B GR7GS G(RaS) . GOutIA,BaGoutrA,B GRaGS (5.34) (5.35) Property (5.33) follows from G(IProj,) = ProjdG k ). Verification of (5.34) is straightforward since relational coproduct is the real categorical coproduct. Instead of (5.35) we verify the equivalent for R : A r- C and S : B r- C, GOutIA,B 6 GoutrA,B . G(R6S) = GR6GS (5.36) Note that (5.36) is the dual of (5.34) if we restrict Rand S to functions since product is the real categorical product in the sub-category Map. For arbitrary relations Rand S we calculate: Goutl6Goutr· G(R 6 S) product } Goutl6Goutr· G(Rxid)· G(idxS)· G(id 6 id) { claim: Goutl6 Goutr . G(R x id) = GR x Gid . Goutl 6 Goutr, and GOutl6 Goutr . G(id x S) = Gid x GS . Goutl6 Goutr GR x Gid . Gid x GS . GOutl6 Goutr . G(id 6 id) see remark above, id function, (5.36) } GR x Gid . Gid x GS . Gid 6 Gid { product} GR6GS Next we verify the claim. It is straightforward to prove that Goutl6Goutr· G(Rxid) ~ GRxGid . Goutl 6 Goutr For the other inclusion we calculate GR x Gid . Goutl6 Goutr { product-split fusion } (GR· Goutl) 6 Goutr G functor, computation outl (Goutl . G (R x id)) 6 Goutr C modulaw law } Goutl 6 (Goutr . G(RO x id)) . G(R x id) C computation outr: Goutr . G(RO x id) ~ Goutl6 Goutr . G(R x id) Goutr } } 117 5.6. CONSTRUCTING CANDIDATE ZIPS Dually follows, Goutl'" Goutr . G(id x S) = Gid x GS . Goutl'" Goutr So, indeed the candidates suggested by the uniqueness property (3.11) have the corresponding coslok properties. Hence, from lemma (5.30) it follows that the constructed candidates are half-zips. Next we construct candidate zips for the constant relator, composition of relators and the relator of a tree type. 5.6.2 Zipping constants For the construction of the candidate zip zip.KA.G for the constant relator KA : 1 f- k, for some A, we concentrate on the derived slok property (5.27), for G : 1 f- 1 (fan.G)K A = (zip.KA.G)(t.tlk . KA(fan.G)k But since KA(fan.G)k = id KA it follows that (fan.G)K A = (zip.KA.G)(t.tlk This suggests that we should take zip.KA.G = (fan.G)KA (5.37) as the candidate for zip. KA.G. Note that this choice depends on the fact that there exists a fan for relator G. 5.6.3 Zipping compositions Forthe construction of the candidate zip.FG. H, for F : m f- k, G : k f- 1, H : n ourselves be guide by the requirement that (FG , H) should commute, i.e. f- 0, we let zip.FG.H = -r(zip.HFG)~ Assuming compositionality of zip.H and commutativity of (F, H) and (G , H), we calculate -r(zip.H FG)~ { zip. H compositional, converse, -r functor } -r(zip.H.F)(G0)T . { -r(Fn)zip.HG~ (F, H) and (G , H) are commuting, -r isomorphism (zip.F.H)-r(Go)-r . -r(Fn)-r(zip.G.H) { 'T(GO)'T=oG,sameforF (zip.F.H)oG . (nF)(zip.G.H) Hence, we take as candidate for the half-zip of FG: zip.FG.H = (zip.F.H)oG . (nF)(zip.G.H) (5.38) Later on we exploit the fact that this derivation was based on the requirement of commutativity of (FG, H). CHAPTER 5. A CLASS OF COMMUTING RELATORS 118 5.6.4 Zipping tuples Just as we proved that from the properties (5.23c) and (5.23d) of the definition of a half-zip that zip.F.G is coherent with tupling in its second argument (lemma (5.24)), it follows from the definitions of the candidates zips zip. FG and zip. Proj that zip. F. G is coherent with tupling in the first argument. That is, we have for F : m f- k and G : n f- 1, (5.39) 5.6.5 Zipping tree types The final stage in this analysis of zips is their construction for relators of tree types. Let @ be a binary relator and let (in, T) be its tree type. We consider the construction of zip.T.G for arbitrary relator G. The construction is inductive in the sense that we assume the existence of zip.®.G and construct zip.T.G in terms of it. This time we let ourselves be guided by the generalized version of the derived property (5.15b) for commuting relators, for F : 1 f- k and G , H : 1 f- 1, F(k CX) • zip.H.F = zip.G.F· Instantiating cx with inA : TA f- CXp for function cx, cx: G f- H. A@ TA and F := G gives, for G : 1 f- k G(kin) . zipJd@T.G = zip.T.G ·inG composition (5.38): Id@T= (@)(ld,T) } G(kin) (zip.0.Ghld,T)· (@)(zip.(ld,T).G) = zip.T.G ·inG definition (5.24), zipJd.G = id G } G(kin) . (zip.@.G)k(Id,T) . idG@zip.T.G = zip.T.G· inG unique extension } zip.T.G = qid G@; G(kin) . (zip.@G)kId,T~ So we define, zip.T.G = qid G@; G(kin) . (zip.0Ghld,T)~ Later on we exploit the fact that this candidate was forced upon us. To be more precise, we use that for a given relator G : 1 f- k and tree type (in, T) induced by the binary relator @ if we have a mapping f G mapping relators to natural transformations such that (5.40) and for H : 1 f- m, I : m f- n, (5.41) and fGl@) = zip.@.G (5.42) 5.7. VETTING THE CANDIDATES 119 it follows that that is to say, f G(T) = zip.T.G. This fact follows from a generalisation of the above calculation. We start with property (5.40): G(kin) . f(IdiUTl = f(T) . inG { G (kin) { G(kin) { property (5.41) with H := Gl and I := (Id, T) } f(@ )k(ld, T) . (@)f( (Id, T)) = f(T) . inG property (5.41): f((Id, T) J = (f(Id), fiT)) = (id G , fiT)) } f(@)k(ld,T)' idG@f(T) = f(T)· inG unique extension } f(T) = ~idG@; { G(kin) . f(0)k(ld,T)D property (5.42) } f(T) = ~idG@; G(kin) . (zip.0.Ghld, T)D 5.6.6 Summary For later use we summarise the zips so far constructed. zip.Id.G zip.Proj.G (5.43) (5.44) zip.+.G Ginl 'V Ginr (5.45) zip.x.G GoutlO & GoutrO (5.46) zip.KA.G (fan.Gl KA (5.47) zip.FG.H (zip.F.HltG . F(zip.G.H) for H : 1 (--1 (5.48) llm(zip.Fm.Gl for F : fi(-- k (5.49) ~idG@; (5.50) zip.llmFm. G zip.T.G 5.7 idG idproHGk) for Proj : 1 (-- k G(lin) . (zip.0.G)l(Id,T)D for G : 1 (--1 Vetting the candidates As forewarned, the clauses in the specification of half-zips were postponed until after the construction of suitable candidates. In this section we verify that they are indeed satisfied in each case. Many verifications are entirely straightforward. From lemma (5.30) it follows that the candidate zips for the identity and projection relators, coproduct and product are already vetted since we have shown that the candidate zips indeed have coslok property (5.29). So, we only have to vet the candidate zips for the constant relator, composition of relators, tupling of relators and the relator of a tree type. CHAPTER 5. A CLASS OF COMMUTING RELATORS 120 5.7.1 Zips are natural transformations Verification ofzip.KA.G : GK A f- KAG, i.e. GKAR· (fan.G)A = (fan.G)A . KAGR for each R: Bf-C, is trivial since KAX = idA. Naturality of zip.FG.H follows directly from the naturality of zip.F.H and zip.G.H. Similarly, naturality of zip.L1mFm-G follows from naturality of each of the components zip.Fm.G. To establish that naturality of zip.T.G follows from the naturality of zip.@.G, we calculate, for G : 1 f- k, G(kT)R . zip.T.G = zip.T.G . TGR { definition zip.T.G, tree type fusion G(kT)R· zip.T.G = ~idGZI; {= { G(kin)· (zip.iZI.G)k(Id.T)· GR0idD definition zip.T.G, catamorphism fusion } G(kT)R· G(kin)· (zip.(Si).Ghld,T) =G(kin)· (zip.iZI.G)k(Id,T) . GRC':Jid . id0G(kT)R { G, k_, 0 functors, TR k · in = in . RkiZlTR k = in . (Id0T)Rk } G(kin) . G(k(Id@T))R . (zip.iZI.Ghld,T) =G(kin)' (zip.iZI.G)k(Id,T) . GRiZlG(kT)R {= { definition naturality } (zip.0.Ghld,T) : G(k(Id@T))f-GiZlG(kT) {= { G(k(Id0T)) = G(k@)(k(Id,T)) and G®G(kT) = @(G 2 J[k(Id, T)) } zip.iZI.G : G(k iZl ) f-iZI(G 2 ) 5.7.2 Zips are higher-order natural In this section we verify that the constructed half-zip are higher order natural. That is to say, we verify property (5.23b) of the definition of half-zips, for F : ffif- k and G , H : 1 f- 1, (5.51) For zip.K A we calculate: CX('(KAll' { zip.KA.H ~ lifting, (zip.KA.F)B = fan.h, KAX = idA } CX..... A ·fan.H A ~ { zip.KA.G . KAcx fan.G A cx..... ·fan.H: GL1i-'Id, fan.G is the largest natural transformation of this type } true 5.7. VETTING THE CANDIDATES 121 For the higher-order naturality of zip.FG, we assume that zip.F and zip.G satisfy property (5.51). Assume that cx : H f---" I, then we calculate, for F : m (- I, G : 1(- k, H J I : 1 (- n zip.FG.H . FG(cxk) { definition zip. FG } (zip.F.H)nG . F(zip.G.H) . FG(tXk) => { F relator, property (5.51): (CXl)nG' zip.G.I ~ zip.G.H . G(tXk) } (zip.F.H)nG . F(tXl)nG . Fzip.G.I lifting, property (5.51): (CXffi)(nFl' zip.F.I ~ => zip.F.H . F(cxl ) } (tXffi)(nF)(nGl' (zip.F.l)nG . Fzip.G.I { "n _" functor, definition zip.FG (tXffi)n{FG1' zip.FG.I Verification of the higher-order naturality of Zip.L1ffiFffi follows from a similar proof to that given for lemma (5.25). For the verification of the higher-order naturality of zip.T we assume property (5.23b) for zip.Q9. Then we calculate, for G , H : 1 (- k, tX(kTl ·zip.T.H ~ zip.T.G· TtX definition zip.T.G, tree type fusion } cx(kT)·zip.H~ ~idGQ9; G(kin)· (zip.!Zl.Gh(Id,T)· tXQ9id G(kT1D definition zip.T.G, catamorphism fusion, Q9 functor } {= tX(kT) . H(kin) . (zip.@.Hhld,T) ~ tX:Gf---"H {= G(kin) . (zip.Q9.Ghld,T) . tXQ9tX(kT) } G(kin) . tX(k(Id@T))' (zip.Q9.H)k(Id,T) ~ {= { CX(k(Id@T)) tX(k@) ·zip.Q9.H ~ 5.7.3 = G(kin)· (zip.Q9.G)k(Id,T)· tXQ9tX(kTl tX(k@)(k(Id,T)) , tXQ9CX(kT) zip.0.G . = Q9(tX2hld,T), lifting} @(tX2 ) Zips are compositional In order to verify that zi p. FG is compositional, we have to show that, for F : m (- k, G : k (- I, H : 1 (- n, I : n (- 0, zip.FG.HI = (Hffi)(zip.FG.I) . (zip.FG.Hlrl assuming that zip.F and zip.G are compositional. We first prove a lemma which starts with the rhs and assumes nothing about compositionality. Lemma 5.52 (Hffi)(zip.FG.I) . (zip.FG.Hlrl ((Hffi)(zip.F.I) . (zip.F.Hlrk)OG . F((Hk)(zip.G.I) . (zip.G.Hlrcl 122 CHAPTER 5. A CLASS OF COMMUTING RELATORS Proof (Hm)(zip.FG.I) . (zip.FG.Hlrl { definition zip.FG } (Hm)((zip.F.I)oG . (nF)(zip.G.I)) . ((zip.F.H)nG . F(zip.G.H)lrl { H m functor, lifting } (Hm)(zip.F.I)oG . (Hm)(nF)(zip.G.I) . (zip.F.H)(nGH[l) . F(zip.G.Hlrl { zip.F.H : (Hm)(nF) t- F(H k ) and zip.G.I : [IkWG) t- (nG)(I t ). Hence, (Hm)(nF)(zip.G.I) . (zip.F.H)(nG)([l) =(zip.F.H)(Ik)(oG) . F(Hk)(zip.G.I) } (Hm)(zip.F.I)oG . (zip.F.H)(Ik)(oG) . F(Hk)(zip.G.I) . F(zip.G.H)]l { lifting, F functor } ((Hm)(zip.F.I) . (zip.F.Hlrk)OG . F((Hk)(zip.G.I) . (zip.G.Hlrtl o Using the above lemma, verification of compositionality of zip.FG is straightforward. (Hm)(zip.FG.I) . (zip.FG.Hlrl { lemma (5.52) } ((Hm)(zip.F.l) . (zip.F.Hlrk)nG . F((Hk)(zip.G.I) . (zip.G.Hlrtl { assumption: zip.F and zip.G are compositional } (zip.F.HI)nG . F(zip.G.HI) definition zip.FG } zip.FG.HI Compositionality ofzip.L1mFm follows from a dual proof to that given for lemma (5.25). For the verification of compositionality of zip.T, where T is the tree type corresponding to @, we use that the definition of zip.T.F was forced upon us. We have to prove, for H : 1 t-n, I: nt-o, zip.T.HI = H(zip.T.I) . (zip.T.Hlr If we define mapping f H [ for F : 1 t- k, we have to prove fHI(T) = zip.T.HI 5.7. VETTING THE CANDIDATES 123 In subsection 5.6.5 we showed that this follows if we prove properties (5.40)-(5.42) for f HI . Property (5.42), i.e. f HI (®) = zip.0. HI, or, expanding the definition of f, H(zip.®.I) . (zip.iSJ.H)J2 = zip.0.HI is just the assumption that zip.0 is compositional. Furthermore, we have to check property (5.41), i.e. for F : 1 f- k, G : kf-l, Expanding the definition of f, for H : 1 f- n, I : n f- 0, we have H(zip.Id.I) . (zip.Id.Hh = id HI , and H(zip.FG.I) . (zip.FG.Hhl (H(zip.F.I) . (zip.F.H)]k)OG . F((Hk)(zip.G.I) . (zip.G.Hhl) The first conjunct follows from the definition of zip.Id, the second conjunct follows from the fact that zip.G.F = Lik(zip.Gk.F), and the third conjunct is lemma (5.52). It remains to verify property (5.40), i.e. or, expanding the definition of f, HWin) . H(zip.Id®T.I) . (zip.IdQSiT.H)I = H(zip.T.I) . (zip.T.Hh . inHI Recall that zip.T.F was constructed such that, for F : 1 f- k, (5.53) We calculate H(zip.T.I) . (zip.T.Hh . inHI { lifting, property (5.53) H(zip.T.I) . H(minh . (zip.IdiUT.Hh { HWin) H functor, property (5.53) H(zip.IdQSiT.I)· (zipJd0T.Hh Hence, we have proven that zip.T is compositional. CHAPTER 5. A CLASS OF COMMUTING RELATORS 124 5.7.4 Zips respect projections In this section we verify that each of the candidates respects projections, that is to say, we verify for F : 1 +- k, zip.F.Proj = idF(Projk) for each Proj : 1 +-1. Verification for zip.K A is trivial since fan.Proj = id: zip.KA.Proj = (fan.Proj)KA = id KA = idKA(Projk) We show that for F : m +- k, G : k +-l the candidate zip.FG respects projections assuming that zip.F and zip.G respect projections. We calculate for Proj : 1 +- n, zip.FG.Proj definition zip.FG (zip.F.ProilnG . F(zip.G.Proj) zip.F and zip.G respect projections } idF(Projk)[nGl . F(idk)G(projl) { (Projk)(nG) = G (Projl), identities Verification for zip.L1 m Fm follows from a dual proof to that given for lemma (5.25). For the verification that zip.T respects projections we assume that zip.@ respects projections, then we calculate for Proj : 1 +- k, zip.T.Proj { definition zip.T } Qid proj 0; Proj[kin) . (zip.(8I.Projhld, { d Proj(kin) = inproj, zip.0 respects projections } Qid proj (8l; inproj' id@(proj2)(k(Id, ~)T identities } idT(Proj) 5.8 Regular relators are commuting In this section we prove that the class of regular relators is commuting. That is to say, we prove for regular F and G that zip.F.G = '"[(zip.GF)~ 5.9. BROADCASTS 125 or, equivalently, zip.G.F = T(zip.FG)~ As we have already shown, the pair of relators (F , G) is commuting if F or G is recoverable from its cofan function. This dispenses with the cases that either F or G is the identity relator, a projection relator, coproduct or product. Commutativity of (FG , H) follows by construction: the half-zip zip.FG.H was derived from the requirement of commutativity of (FG , H). We assumed the existence of the half-zip zip.H.FG and commutativity of (F, H) and (G, H). Commutativity of (~mF lemma (5.24), ziP.~mF G m , G) follows from commutativity of (Fm , G) for each m, since using = ~mzip.FG = ~m(zip.GF)'"[ = T(zip.G~mF) Commutativity of (T, G) follows from commutativity of (@, G). We have to show that zip.T.G = (zip.G.T)O, hence we define fG(F) = (zip.GF)~ and we show that properties (5.40)-(5.42) hold for f G. We start with property (5.40), for G : 1 f--- k, G(kin) . fG(Id@T) = fGiT) . inG definition f G, converse, kin = ink } inG' zip.G.T = zip.G.Id@T . G(inO)k which follows from functionality of in, in : T f--- Id@T, and lemma (5.13b). Furthermore, property (5.41) which states that f G is compositional and respects identities, i.e. is the property that zip.G is compositional and respects identities. Similarly, property (5.42), i.e. is just the assumption that (@, G) commutes. It remains to verify that (K A , KB) commutes. We have zip.KA.K B = (fan.KB)K A = IT KB . KA' hence, zip.KA.K B = (zip.KA.KB)o. This concludes the proof of the zip-a-dee-doo-dah theorem. 5.9 Broadcasts In section 5.3.4 on strength we claimed that zips were an instance of strength. We showed that the broadcast zip. (x B) .FA is a strength of endorelator F provided that it is functional and natural in B. In this section we show that this indeed the case for the half-zip constructed in the previous sections. In Chapter 6, we prove that this half-zip is the unique strength for relator F provided F has membership. In other words, it follows that a strength is an instance of a half-zip. First, we construct the half-zip zip. (x B) .F. We calculate, 126 CHAPTER 5. A CLASS OF COMMUTING RELATORS zip.(x B).F (x B) = (x)(Id, KB), zip of composition (5.38) (zip.x.F),Id,K B)· (x)zip.(Id,KB).F { zip of "x" (5.32); zip respect tuples (5.39) } (FoutlO A FoutrO ),Id, Ks) . (x) (zip.Id. F , zip.KB.F) zip respect identities, zip.KB.F = (fan.F)KB (5.37) } (FoutlO A FoutrO)(Id,KB) . id F x (fan.F)KB Hence, (zip.(x B).F)A = Foutl~,B A Foutr~,B . idFAX (fan.Fh . Next, we verify that (zip.(x B).F)A is functional, and natural in B. Lemma 5.54 Let strA,B = Foutl~,B A FoutrA.B . idFAX (fan.Fh, then str is a functional natural transformation of type F(A x B) f--' FAx B. Proof Naturality of strA,B : F(AxB) f--'FAxB follows from naturality of ZiPA. B : F(A x B) r FAx FB and naturality of fanB : FB f--' B. Totality is proved as follows: str> definition str, product, domain of intersection } { id n outl~A, B . F(outlA, B. outrA, B) . fanB . outrFA, B outIA,B' outrA,B = TTA,B, fan: FTTA,B . fanB = TT FA,B } id n outl FA ,B. TT . outrFA, B outl and outr are total id FAxB Simplicity of str we verify by: strA,B' strA,B definition str, product } Foutl~, BA Foutr A,B . idA x (fanB . fan~) product } ·outIA,B) n FoutrA,B' fanB . fan~ F(outl~,B c . FoutlA, Bf', FoutrA, B . FoutrA,B lemma(4.41) } F(outIA,B' outlA,B n outrA,B' outrA,B) B,~ltuo idF(AxBj ·outIA,B n outrA,B 'outrA,B = id AxB } 5.9. BROADCASTS D Combining lemma (5.54) with the results of section 5.3.4, we get as corollary Corollary 5.55 D Foutl:;"', B'" Foutr A,B . idFAX (fan.FJB is a strength of relator F. Hence, the construction given for half-zip zip. (x BJ. F is a strength of relator F. 127 Chapter 6 Strength, Copies map and Fan In chapter 4 on membership, we showed that the existence of membership of relator F implied the existence of the canonical fan. In chapter 5 on commuting relators we showed that the existence of the canonical fan implies the existence of a strength of F. Thus a relator with membership has a strength, i.e. is strong. This chapter reformulates and extends the work of Hoogendijk and De Moor [20]. In [20] an abstract of notion of a fan (satisfied by the canonical fan) is given and it is shown that the fans and strengths of a relator are in one-to-one correspondence. Furthermore, it is shown that the existence of a membership relation implies the existence of a unique fan and (thus) a unique strength. In this chapter we extend this analysis to include Jay's [23] notion of a copies map. The interpretation of a copies map is that it takes an F-shape and a value and generates an F-structure by filling the F-shape with copies of the given value. We show that the existence of a strength implies the existence of a copies map. Furthermore, we show that for a binary intersection preserving relator F, the existence of a copies map implies the existence of a fan of relator F. Finally, we show that the existence of a fan, not necessarily the canonical fan, implies the existence of a strength. Furthermore, we show that all the constructions are injective. In other words, for a binary intersection preserving relator, strengths, fans and copies maps are all in a one-to-one correspondence. Moreover, if we assume that the relator has membership then we can prove, just as for fan, that the relator has a unique strength and a unique copies map. We prove the uniqueness using the fact that a fan is the largest relation of its type. Furthermore, we show that an arbitrary natural transformation of type F f--' G is coherent with respect to the strength and the copies map of F and G. Recall that all regular relators and the power relator have a membership relation. Hence, these relators have a unique fan, strength and copies map. Since the definition of strength is only given for endorelators, we will assume throughout the remainder of this chapter a fixed, but arbitrary endorelator F. 129 CHAPTER 6. STRENGTH, COPIES MAP AND FAN 130 6.1 Zips We define as a shorthand zip = zip. x .F, that is to say, zip = FoutlO ... FoutrO . Recall that we have the coslok properties (5.35), F(R ... S)· zip = FR ... FS . (6.1) Note that zip is a half-zip. For instance, we have for C( : F f-' Id, (6.2) since (zip.x.F)A,B . C(A X C(B ~ C(AxB' (zip.x.Id)A,B = C(AxB Furthermore, for the combination of the assA.B.C: (AxB)xCrAx(BxC)andzip,wehave, ZiPAXB,C . ziPA,B X natural isomorphism id Fc . assFA,FB,FC = FassA,B,C . ziPA,Bxc . idA X ZiPB,C ass, (6.3) or, equivalently, since ass is an isomorphism, zip~,B X id FC . zip~xB,c . FassA,B,C = assFA,FB,FC . idA x zip~,c . Zip~,BxC (6.4) Note that zipo = Foutl '" Foutr , so equation (6.4) is a statement in Map since all components involved are functions. Now, property (6.4) follows by a straightforward calculation using the universal property of product. 6.2 Constructing copies map, fan and strength We start with the definition of a copies map [23] and show that having a strength, we can construct a copies map. And we show that for a binary intersection preserving relator, we can construct a fan from a copies map. Definition 6.5 such that, A copies map cop is a functional natural transformation of type FA F!A . COPA = outlFl,A . f-' F1 x A (6.6) o The interpretation of a copies map is that it takes an F-shape, i.e. an element from FI, and a value and fills the F-shape with copies of the given value. Equation (6.6) expresses that the shape of the result is the same as the given F-shape. Recall that the interpretation of a strength is a so-called broadcast. That is to say, strA, B : F(A x B) r FAx B takes an F-structure of A's and a value of type B and pairs all the elements of the F-structure with the given value of type B. This suggest that strl, B : F(l x B) r F1 x Band COPB : FB r F1 x B are closely related. Indeed, we have, 6.2. CONSTRUCTING COPIES MAp, FAN AND STRENGTH 131 Lemma 6.7 If str is a strength of relator F then Flid A . str1 ,A is a copies map of F (where lid A : A f- 1 x A is the obvious natural isomorphism). Proof F!A . F1id A . str1,A terminality: !A ·lid A = !, x A = outl',A } Foutl',A' str1,A property (5.17) out1n,A o Comparing the interpretation of a fan and a copies map one can say that a copies map is the functional counterpart of the nondeterministic fan. A fan takes a value and generates an arbitrary F-structure filled with copies of the value, whereas the copies map takes a specific Fshape and generates an F-structure of that shape filled with copies of the value. In other words, if we first generate an arbitrary F-shape and we feed it together with a value to a copies map, we should get the same result as with the fan. Indeed, this is true for a binary intersection preserving relator F. In [1] it has been shown that product and coproduct preserves binary intersections. Furthermore, a tree type relator preserves binary intersections if the base relator preserves binary intersections. Hence, it follows that all regular relators preserves binary intersections. Lemma 6.8 If cop is a copies map of binary intersection preserving relator F then is a fan ofF. Proof Naturality offanA : FA f---' A follows from naturality of COPA naturality of outrh,A : F1 x A f- A. Top strictness we verify by, FTT B,A . COPA . outr F1 ,A { TT B,A = !B . !A, copies map (6.6) } F!B . outlA, n . outrF1,A outl . outr = TT, domains (2.28): !B total } TT FB,A For preservation of binary intersection, we prove for each R, S : B f- A, F(R n S) . fanA c { FR . fanA monotonicity n FS . fanA FA f---' F1 x A and 132 CHAPTER 6. STRENGTH, COPIES MAP AND FAN c modular law } (FR n FS· c fanA . fanAO) ·fanA claim: FR (FR n FS . fanA . fanAo C FS } n FS) . fanA c assumption: F preserves binary intersections } F(R n S) . fanA Hence, F(R n S) . fanA = FR . fanA n FS . fanA For the claim, we prove FR n FS . fanA . fan~ definition fa nA } FR n FS . COPA . outrFl,A . outrFl,A . COPAo modular law } C n outrFl,A ·outrFl,A) . copo FSo . FR ~ FTTA,A = F!A ° . F!A, copies map (6.6) (twice) . (outIF1,A ·outIFl,A n outrFl,A ·outrFl,Al . COPAo FS· cop' (copO . FSo . FR· cop C FS . COPA C { Outl F1 ,A' outlFl.A n outrh,A . outrFl ,A } = id, cop simple FS o Note that outrFl ,A = TTFl ,A L:. idA, so the interpretation of outrFl ,A is that it generates from a value of A, a pair consisting of an arbitrary F-shape and the given value. Combining the construction of a copies map from a strength and the construction of a fan from a copies map gives, Corollary 6.9 fanA = Let F be a binary intersection preserving relator with a strength str, then Flid A . strl, A . outrFl,A is a fan of F. o In subsection 5.3.4 and section 5.9 we have shown that (zip. (x A) .FlB is a strength of relator F. Using the inductive construction of the half-zips it followed that (zip.(x A).Fl B = (zip.x.FlA,B . idA x (fan.Fl B where fan.F is the canonical fan of relator F, i.e. the fan constructed using the membership relation ofF. However, we can prove that (zip.( x A).F)B is a strength ofF only assuming that fan. F is a fan of F according to the definition of fan (4.39), 6.2. CONSTRUCTING COPIES MAp, FAN AND STRENGTH 133 Iffan is a fan of relator F then Lemma 6.10 is a strength of F. Proof Naturality and functionality of strA, Bfollow from lemma (5.54). For unit coherence we prove: FridA' ZiPA,l . idFAX fani { rid A = idA'" !A' slok property of zip (6.1) id FA ... F!A . idFAX fani { product} id FA ... (F!A . fanA) fan fan: F!A . fanA = !FA' id FA '" !FA = rid FA Associative coherence follows from: strAxB,C . strA,B x idc . assFA,B,C { definition of str, product ZiPAXB,C . ZiPA,B X id FC . (idFAX fanB) x fanc . assFA,B,C associate } ziPAXB,C . ZiPA,B X id FC . assFA,FB,FC . idFAX (fanB x fane) property (6.3) } FassA,B,C . ZiPA,BXC . idFAX ZiPB,C . id FA x (fanB x fanc) :J { product, property (6.2): fan: F f---" Id } FassA,B,C . ZiPA,BXC . idFAX fanBxc { definition str } FaSSA,B,C . strA,BxC Equality now follows because str . str x id . ass and Fass . str are both functions. o We have shown how one can construct a fan from a copies map, and how one construct a strength from a fan. Combining both constructions gives, Corollary 6.11 is a strength of F. If cop is a copies map of binary intersection preserving relator F then, 134 CHAPTER 6. STRENGTH, COPIES MAP AND FAN Proof Follows by combining the constructions of lemmas (6.8) and (6.10). o The construction of corollary (6.11) is the combination of first constructing a fan, which is nondeterministic, from a copies map, and then constructing a strength from a fan. However, both a copies map and a strength are functions. This suggests that there is a more direct way of constructing a strength from a copies map. Indeed, we have, If cop is a copies map of binary intersection preserving relator F then, Corollary 6.12 ZiPA.B . idFAx (COPB' outrF1,B) ziPA,B . outlFA,B 6. (COPB . F!A x idB) Proof We calculate, ziPA,B . idFAx (COPB .outrF1,B) definition x } ziPA,B . outlFA,B 6. (COPB' outrF1,B' outrFA,B) ::) outr F1 ,B. outrFA, B = TIn, FAX idB ;;;? F!A X idB } ziPA,B' outIFA,B6.(COPB . F!Axid B) Now, equality follows if we prove that ziPFA, B . outlA, B6. (coPB . F!A Xid B) is total: (ZiPA,B . outlFA,B 6. (COPB . F!A X idB))> { definition zip, product } (Foutl~,B . outlFA,B n FoutrA,B . COPB . F!A X idB» { domain intersection (2.27) } id AxB n outl~A,B . FoutlA,B . FoutrA,B . cOPB . F!A X idB FoutlA,B . FoutrA,B idAxB n outl~A,B = FTIA,B = F!A . F!A . F!B . COPB . F!A X idB copies map: F!B . COPB idA xB n outl~A, { . F!B } = outln,B, naturality outl B . F!A . F!A . outlFA, B domain (2.24), F!A . outlA,B total id FAx B o Now, note that OUtIFA,B 6. (COPB . F!A x idB) is a function and ZiPA,B is a partial function (simple) for binary intersection preserving relator F. The interpretation of COPB . F!A XidB is that it takes the shape of the left-argument and fills it with the value of the right argument. Similarly, we can construct a copies map from a fan, 6.3. ONE-TO-ONE CORRESPONDENCE Corollary 6.13 135 If fan is a fan of relator F then, is a copies map of F. Proof constructs of lemma (6.10) and (6.7) F1id A . ZiPI,A . idFl x fanA { definition zip, product Flid A . Foutl~,A { A(Foutrl,A . fanA) lid A = outri ,A, modular identity } F(outrl,A' outl~,A) AfanA outrl,A' outl~,A = ITA,I =!A } o For the interpretation of F!O Afan, we have, x (F!OAfan) (y,z) == yF!x 1\ xfanz Hence, F-structure x has shape y and is filled with copies of the value z. 6.3 One-to-one correspondence Next, we want to prove that all the constructions given are injective for a binary intersection preserving relator. We prove that in the following way. We have shown how one can construct a copies map from a strength, and a fan from a copies map, and a strength from fan. If we prove that the resulting strength equals the original strength it follows that the construction of a copies map or a fan from a strength is injective. Similarly, we prove this for copies map and fan. Lemma 6.14 If str is a strength of binary intersection preserving relator F then, (6.15) Proof ZipA,B . idFAx (Flid B . strl,B . outrFI,B) Foutl~,B definition zip, product } A(F(outrA,B ·lid B) . strl,B . outrFI,B) CHAPTER 6. STRENGTH, COPIES MAP AND FAN 136 product: outr A,B. lid B = !A Foutl~,B'" X idB } (F(!A x idB) . strl,B . outrF1,B) naturality strA, B : F(A x B) f--' FAx B } :J Foutl~,B'" (strA,B . F!A x idB . outrF1,B) F! total, natura1ity of outr } Foutl~, B... (strA, B . outr A ,B) modular identity, str function strA,B' (strA,B' Foutl~,B)"'outrA,B str strength: (5.17) } strA,B . outl~,B ... outrA,B product } Now, equality follows because from lemma's (6.9) and (6.10) it follows the the rhs of (6.15) is also a strength, hence, a function. o And the constructions of a fan or a strength from a copies map is injective, Lemma 6.16 If cop is a copies map of relator F then, Proof F!A ... (COPA . outrFt,A) cop function, modular identity COPA . (COPA . F!A)'" outrF1,A copies map: COPA . F!A = outl~,A' product } o Finally, the constructions of a strength or a copies map from a fan is injective, Lemma 6.17 If fa n is a fan of relator F then, Proof Flid A . ZiPl,A . id Fl x fanA . outrF1,A lid A = !A 0 ... idA, naturality outr 6.4. UNIQUENESS OF STRENGTH AND COPIES MAP 137 F(!AO ... id A) . ZiPl,A . outri\,FA . fanA { property zip (6.1) } F!A ° ... Fid A . outrh, FA . fanA { computation rule outr, F!A total o Corollary 6.18 For a binary intersection preserving relator F, the strengths, copies maps and fans are all in one-to-one correspondence. o 6.4 Uniqueness of strength and copies map Suppose relator F has membership memo Then, as we have shown earlier, mem \id is a fan of F and, assuming the identification axiom, is indeed the unique fan of F. We called mem\id the "canonical fan" of F. Using the constructions of lemmas (6.13) and (6.10) we obtain a canonical copies map and a canonical strength zip· id x fan where fan denotes the canonical fan of relator F. The uniqueness of the canonical fan and the one-to-one correspondence between fans, strengths and copies maps guarantees that the canonical copies map and the canonical strength are also unique. The assumption is, however, that F preserves binary intersections. This assumption is unnecessary as we now show. Lemma 6.19 off. For a relator F with membership, the canonical strength is the unique strength Proof We prove that the canonical strength is the largest strength of F. Then uniqueness follows because inclusion of functions is equality of functions. So, let 8 be a strength of F and str the canonical strength of F. Then, 8 A,B <;;; strA,B definition canonical strength, zip, product 8 A ,B <;;; "'B,~ltuoF (FoutrA,B . fanB) ... H "'-, 8 A,B' outl~A,B { <;;; B.~ltuoF universal propery product (2.52) /\ 8 A,B' outrFA,B <;;; FoutrA,B . fanB shunting of functions } CHAPTER 6. STRENGTH, COPIES MAP AND FAN 138 FoutlA,B . 8 A,B ~ OUtIFA,B 1\ FoutrA,B . 8 A,B . OUtrFA,B ~ fanB property (5.17), fanB largest nat. trans. of type FB f--' B } {= FoutrA,B . 8 A,B ·outrFA,B : FBf--'B naturality outrA,_, 8 A,_, outrFA,_ true o Note, however, that in the proof of lemma (6.19) we made only use of the fact that 8 A,B is a functional natural transformation and property (5.17) which followed from the coherence property with rid. In other words we have proven, Corollary 6.20 If relator F has membership and 8A, Bis a functional natural transformation of type F(A x B) f--' FAx B and is coherent with rid, i.e. then 8A, B is the unique strength of relator f. o The importance of corollary (6.20) is that it provides an easy way to check whether a constructed function is a strength for a specific datatype. For instance, it follows that o str(D,b) str(cons(a, x), b) cons((a, b) ,str(x, b)) defines a strength for (cons) lists. First, since str is a polymorphic function it follows from "Theorems for Free!" that str is a natural transformation. Secondly, it is not difficult to verify that str preserves the original list in the left-argument of all the pairs. Hence, without a proof we know that str is coherent with respect to ass and thus str is a strength of the datatype list. Just as for strength, we can prove uniqueness of copies map, Lemma 6.21 For a relator F with membership, the canonical copies map is the unique copies map of F. Proof Just as for the canonical strength, we prove that the canonical copies map is the largest copies map of F. So, let Y be a copies map and cop the canonical copies map of F, then, YA ~ COPA canonical copies map } YA ~ F!A .fanA • H YA . OUtl~,A ~ L., universal propery product (2.52) } F!A 1\ YA . outrFl ,A ~ shunting of functions } fanA 6.4. UNIQUENESS OF STRENGTH AND COPIES MAP F!A . 'YA ~ {= { Dutln,A 1\ 'YA . DutrFt,A ~ 139 fanA copies map: F!A . 'YA = Dutln,A, fanA largest nat. trans. of type FAf--'A } 'YA . DutrF1,A : FAf--'A { 'YA: FA f--' F1 x A, outrFl,A F1 x A f- A } true o The proofs of lemma's (6.19) and (6.21) are nice examples of the power of the calculus of largest natural transformations. It simplifies many arguments for generic constructs like strength and copies map. Another beautiful example is the following, concerning so-called strong natural transformations, Definition 6.22 Let F and G be strong relators, with strength str.F and str.G respectively. A natural transformation (X : F f--' G is said to be strong if FAx B • (XA X idB GA x B (str.Fh,B (str.G)A,B F(AxB)' (XAxB G(AxB) commutes. o Surprisingly, this condition is always satisfied if relator F has membership, Let F and G be relators with membership. Then any (X : F f--' G is strong. Lemma 6.23 Proof Let str.F and str. G denote the unique, canonical strengths of relators F and G respectively; then we have to prove: (str.F)A,B . (XA x idB = (XAxB . (str.GlA,B The containment:;2 follows from the theory of commuting relators since the canonical strength (str. Fl A,B is the half-zip (zip. (x B) .F)A' For the other containment we reason as follows: (str.F)A,B . (XA x idB ~ (XAxB . (str.G)A,B str function, shunting } (XA x idB . (str.G)A,B ~ { (str.F)A,B· (XAxB converse} (str.GlA,B . (XI.. x idB ~ (XAxB' (str.FlA,B 140 CHAPTER 6. STRENGTH, COPIES MAP AND FAN We continue with the lhs: (str.GlA,B . (XA x idB canonical strength, product } (Goutl~.B C . (XAl. (GoutrA,B . (fan.GlBl { (X:h--'G} ((XAxB' B,ArtuoG(·lB,~ tuoF C { (XAxB . (fan.GlBl modular law } ·B,~ltuoF C ((XAxB . GoutrA,B . (fan.GlBl (X:Ff--'G} (XAxB ·B.ArtuoF(·B,~ltuoF (XB . (fan.GlBl (X. fan.G : F f--' Id, and fan.F largest of thattype } C (XAxB (Foutr A.B . (fan.Fl B) ·B,~ltuoF canonical strength (XA xB. (str. FlA. B o As said before, we find this result is quite surprising. It gives some confidence that the definition of a datatype as "relator with membership" [20] is a proper one. First of all, a relator with membership has a strength, i.e. is strong, which Moggi [39] regards fundamental to computation. Secondly, all natural transformations between two relators with membership are strong too. 6.5 Shapely functors Barry Jay [23] defines the notion of a shapely functor and the notion of copyable natural transformation between shapely functors. By definition a shapely functor is a so-called pullback preserving functor equipped with a copies map. In [40], Oege de Moor shows that a relator preserves pullbacks precisely when it preserves binary intersections. In other words, it follows that a binary intersection preserving relator with membership is shapely. Hence, all regular relators are shapely. A natural transformation (X : F f--' G is said to be copyable if it is coherent with respect to the copies maps of F and G. That is to say, if the following diagram commutes, F1 x A. (cop.Fl A (Xl X idA Gl x A (cop.Gl A FA-·----GA 6.6. COHERENCE VERSUS LARGEST NATURAL TRANSFORMATIONS 141 It follows trivially that, Let F and G be relators with membership. Then any lX : F f--' G is copyable. Lemma 6.24 Proof Follows directly from lemma (6.23). We have for the unique, canonical copies map (cop.F1 A = Foutrl,A . (str.Fh,B. Using this, we prove (cop.F1 A · lXl X idA = lXA' (cop.G1 A remark above } Foutrl,A' (str.Fh,B . lXl { idA = lXA . Goutrl,A . (str.Gh,B lXA' Goutrl,A = Foutrl,A . lXlxA, Leibniz } (str.Fh,B . lXl { X X idA = lXlxA' (str.Gh,B lemma (6.23) } true o 6.6 Coherence versus largest natural transformations In the previous section we showed that every natural transformation of type F f--' G is coherent with respect to the corresponding strengths and copies maps. Both facts we proved using the fact that the canonical fan is the largest natural transformation of its type. Similarly, we have that a membership relation is the largest natural transformation of its type. In this section we show that the property "largest natural transformation of its type" for fans and membership can be expressed as a coherence requirement as well. Furthermore, this coherence requirement can be expressed just as for zip.F as a higher-order naturality requirement. We start with membership. We consider the collection of membership relations mem.F indexed by a relator F. From the fact that fan.G is the largest natural transformation of type Id f--' G it follows that for each lX : F f--' G, mem.F· lX <;;; mem.G (6.25) since mem. F . lX has type Id f--' G. Furthermore, it follows that mem.Id = id (6.26) since the identification axiom states that id is the largest natural transformation of type Id f--' Id. So, equations (6.25) and (6.26) are a consequence of the fact that membership is the largest natural transformation. The other way around is also true. Assume equations (6.25) and (6.26). Then, for each lX : Id f--' F, 142 CHAPTER 6. STRENGTH, COPIES MAP AND FAN (6.25) =} { F:= Id, G := F mem.Id· a <;;; mem.F (6.26): mem.Id = id } ex <;;; mem.F Hence, mem.F is the largest natural transformation of type Id ~ F. Next, we want to express requirement (6.25) as a higher order naturality property. We do this in the same way as we derived the higher order naturality property of zip. F. In order to see in what sense mem.F is natural in F let us denote it according to our convention for denoting natural transformations. Specifically, we write memF instead of memo F. Thus mem is a collection of arrows indexed by a relator. Now observe that the naturality of memF : Id ~ F takes the form (6.27) where KId denotes the constant mapping which maps each relator to the identity relator, and ID denotes the identity mapping on relators. If the collection mem is itself a natural transformation then the mappings KId and ID are the object maps of the functors between which mem is a natural transformation. So, what are the corresponding arrow maps of these functors? The obvious choice is to define for natural transformation a, KId(a) = id and ID(a) = a. It is trivially verified that this defines two functors on the functor category. Now, typing (6.27) of memF for each F suggest the following "free theorem": mem is a higher order natural transformation with typing: (6.28) mem : KId Ie- ID Expanding the definition of a natural transformation gives us: KId(a)· memG = memF ·ID(ex) for each a: Fie-G. And using the definition of KId and ID gives, memG = memF' a for each a : Fie-G. (6.29) Just as equation (5.9) for zip.F, equation (6.29), motivated by type considerations, is not precisely property (6.25). However, if ex : Fie- G is a function we can prove that memF . ex is a membership relation of G, hence, equation (6.29) follows from uniqueness of membership. For an arbitrary natural transformation, demanding equality is too severe. If we take for instance aA := JJ..FA,GA' we have memF' JJ..FA,GA = JJ..A,GA and in general it is the case that memG f JJ..A,GA. SO, if we want to include arbitrary natural transformations we have to relax requirement (6.29) using an inclusion instead of an equality. Having an inclusion, the requirement for a can be relaxed as well. So, equation (6.29) becomes, memG :2 memF' a for each a : F ~ G, 6.7. CONCLUSION 143 which is equation (6.25). In other words, the fact that a membership relation for each relator is the largest natural transformation of its type can more or less be expressed by: mem : KId Ie- ID and memld = id . Just as for membership, we can derive that a fan for each relator is the largest natural transformation of its type can be expressed by, fan: ID Ie- KId and fanld = id . So, we have shown that membership and fan are the largest natural transformations of their types followed from considering their "free property", a higher-order naturality property, which we derived from the typing of the individual members mem.F and fan.F. 6.7 Conclusion In this chapter we proved that for a binary intersection preserving relator the notions of fan, copies map and strength coincide. Furthermore, we proved that for a relator with membership, not necessarily binary intersection preserving, these three concepts are uniquely defined. The copies map and strength are based on a functional paradigm. Since we are working in a relational framework we are able to define a notion like the canonical fan. The notion of a fan is a much simpler concept, despite the nondeterminism involved, than strength and somewhat simpler than copies map. For all three concepts we can express a coherence property: for a copies map the notion of a copyable natural transformation, for strength the notion of a strong natural transformation. For fans, the coherence property is just the fact that the canonical fan is the largest fan of its type, and this fact is predicted by the free theorem derived from the typing of fans. As we have seen, the calculus of largest natural transformations is a powerful tool. We proved the existence and uniqueness of strength and copies map, and we proved that every natural transformation is strong and copyable. In other words, for a natural transformation between relators with membership the requirements "being strong" or "being copyable" are superfluous. It is of course quite likely that we have missed out a number of generic operations on datatypes, i.e. operations that are common to all datatypes. It remains to be seen whether these can be constructed using membership. However, we have shown that this is indeed the cases for fans, strengths and copies maps. Chapter 7 Epilogue In this thesis, we have presented the basis of a theory of datatypes that is truly generic in a mathematically precise sense. We have formulated a precise, verifiable definition of the notion of a polytypic function. Specifically, a polytypic function should satisfy the higherorder naturality property predicted by its type. The higher-order naturality property captures the informal notion that a polytypic function instantiated at different type constructors should behave in related ways. We have given a generic characterization of a membership relation and shown that a membership relation is the largest natural transformation of its type. The calculus of largest natural transformations was shown to be very powerful. In particular, using this calculus we were able to prove several uniqueness results. For instance, for a relator F with membership there exists a unique strength. We showed that the property "being a largest of its type" can be formulated as a higher-order naturality property. This higher-order naturality is predicted by the type of a membership relation. So, indeed the notion of a membership relation is truly polytypic. The same holds for fans, the notion dual to membership. Again, fans are the largest natural transformations of their type, and this property can be formulated as a higher-order naturality property. The notion of higher-order naturality played a central role in the discussion of commuting datatypes and the construction ofthe candidate zips. Normally, the type of a natural transformation does not imply uniqueness. Many different natural transformations of the same type can exist. However, in the case of higher-order naturality we were able to establish several uniqueness results. The higher-order naturality of the zip operation implies a slok-property. In case of coproduct, product and projections this slok-property has at most one solution and predicts the only possible candidates for the corresponding zip operation. Furthermore, the uniqueness of a solution of a slok-property implies that the corresponding zip operations are compositional. We got this result because we proved that we have the same typing rules for the slok-category as for the functor category. In other words, a slok-property is preserved by the normal constructions on natural transformations. Although the theory of slok-properties seems trivial it provided us with a powerful tool for constructing polytypic functions and establishing their properties. The discovery of the higher-order naturality property is a major advance on our earlier work [4] in which the commuting requirement was substantially more operational in flavour and 145 146 CHAPTER 7. EPILOGUE hence ad hoc. In our earlier work we only required the slok-properties expressing the requirement that a zip operation should preserve the shape of the data structures. It was a pleasant surprise to us that the higher-order naturality of the zip operations implied the slok-properties. So, the preservation of shape was a consequence of the higher-order naturality requirement. We showed that a special class of zips, the broadcast functions, coincide with the notion of a strength - a notion which according to Moggi [39] is fundamental to computation. Furthermore, we proved that this strength is unique and that every natural transformation between two relators with membership is coherent with the corresponding strengths. Similarly, we showed that a relator with membership has a unique copies map and all natural transformations between relators with membership are coherent with their corresponding copies maps. In other words, for a natural transformation between relators with membership the requirements "being strong" or "being copyable" are superfluous. A new development in this thesis is that we have considered non-endo relators as well. We have demonstrated how to cope cleanly with non-endo relators thus overcoming a limitation of all other work in this field published to date that we know of (including our own). Our goal has been to calculate programs: that is, to construct programs just by syntactic manipulation instead of being led by operational interpretations and giving informal arguments for their correctness. The calculus we have used is the calculus of relations extended with a few categorical concepts. However, this thesis is not on the calculus of relations nor on category theory. We have used the calculus of relations and category theory only as a mathematicallanguage in order to express our notions and theorems in a precise and concise way. Using this calculus we were able to achieve an optimum level of abstraction enabling us to define generic concepts and reason generically about those concepts. An immediate benefit is a substantial reduction in the burden of proof. An example is the generic definition of a datatype. Were we only able to define the notion of a datatype by giving an inductive definition we would be obliged to give an inductive proof every time we want to prove a property for all datatypes. However, we defined a datatype as a relator with membership and using this definition we were able to prove in one go that all regular relators have a strength. This in contrast to, for example, Tuijnman [51] who established a similar result by giving an inductive definition for the class of regular datatypes. Another example is structure multiplication. Lillie proved this property but only in the case of lists and with the aid of a very long proof cluttered with case distinctions between the two constructors nill and cons of the datatype list. Since we formulated the problem at a higher-level of abstraction we were able to give one proof for all datatypes at once, and at the same time we were not hindered by unnecessary details. Several challenges remain. For instance, having a number of generic, or polytypical concepts such as catamorphism - and its friends anamorphism, hylomorphism etc. - , zips, fans, strengths and copies map, one may wonder: are there others? And more importantly, can they be expressed using the existing polytypical building blocks mentioned earlier? Or is it the case that there are other fundamental polytypical notions yet to be discovered which we can then add to our repertoire of generic constructions? We have argued that a polytypic function should be higher-order natural. So, the question remains: are all the polytypic functions considered by leuring and others [27,28, 35] higher- 147 order natural, and what does the higher-order naturality look like in each of the individual cases? For instance, Jeuring defines a polytypic size function (size.Fl A of type Natf- FA. The intended interpretation of the size function is that it returns the number of elements stored in a data structure. So, the "free theorem" suggested by the type of size. F is, size.G = size.F· ex: ~ ex:: F f- G . (7.1) Indeed, we would expect this property for functional and proper natural transformation ex: since the interpretation of a proper natural transformation is that the number of elements remains the samel. It is not the case that (7.1) completely characterizes the size function. A question is thus, what are the other requirements for the size function such that we can characterize -possible uniquely - the polytypic size function? If we do not have a non-inductive characterization of the size function it will be very complicated to prove the higher-order naturality requirement for size. For instance, if we only have an inductive definition of the size function we have to give a proof by induction on two levels: one for F and one for G. This would yield an explosion in the number of cases to be considered. Another challenge is directly connected with the previous problem and has to do with unicity. A major frustration is that we have been unable to establish a general unicity property of the "zip" operators even though in every individual case that we have studied we can prove unicity. This suggests that our requirements can be made stronger and, in the process, yet simpler and more elegant. The general question is: is it always possible to derive some uniqueness properties for a polytypic program from its higher-order naturality property? Broader questions concern how the notion of polytypy relates to other notions of generic programming. Design patterns [18] have recently attracted substantial interest as a way of categorizing (and reusing) common building blocks in object-oriented programming but we are unable to comment at this stage on whether there is any relation between this work and our own. There does appear, however, to be a close connection to the notion of "adaptive" objectoriented programming [30] which aims to abstract away from the inheritance structure of an object-oriented program. The field of generic programming is new and initial progress likely to be slow but the challenge is worthwhile and inviting. I Note that we have not been able to give a formal proof ofthis interpretation. We were only able to prove that the set of elements remains the same which does not necessarily imply that the number of elements is preserved. Appendix A Typing rules A.I The '"[-~ calculus L1kf k E ck {= f k E C , L1kFk : ck I;-- V {= Fk : C I;-- V , nkF k : ck I;-- v k {= Fk : C I;-- V , Fk : ck I;-- v k {= F: C I;-- V . L1kFk : m*k I;-- n {= Fk : m I;-- n , nkF k : m*kl;--n*kl;--n {= Fk : ml;--n , Fk : m*kl;--n*k {= F: ml;--n , kF : bml;-- bn {= L1k(Xk : L1kFk I;-- L1 kGk F: ml;--n . {= (Xk: Fk I;-- Gk , nk(Xk : nkF k I;-- nkG k {= (Xk: Fk I;-- Gk , (Xk: Fkl;--G k {= (X: FI;--G k(X : kF I;-- kG A.2 {= (X: F I;-- G . Membership and fans mem.F: Idf--'F , mem.F: 1 1;--1 F: 1 1;--1 , {= mem.F: Idf--'L1 kF , mem.F:kl;--k {= F:1I;--k, mem.F: (L1tl k f--'L1 kF , mem.F: hkl;--k {= F:ll;--k. fan.F: Ff--'Id , fan.F: 1 1;--1 {= F: 11;--1 , fan.F : FL1 k f--' Id , fan.F: 1 I;-- 1 {= F: 1 I;-- k , fan.f : Fl-. k f--' L1\ , fan.F: 1 I;-- 1 {= F: 1 I;-- k . 149 APPENDIX A. TYPING RULES 150 A.3 Zips zip.F.G ; GF f- FG , zip.F.G; 1 f-l zip.F.G : G(lF) f- F(G k ) , {= F: 1 f-l , G : 1 f-l , zip.F.G : 1 f-l*k zip.F.G ; (Gm)(lF) f- (nF)(G k ) , {= F: 1 f- k , G : 1 f-l , zip.F.G : mmf- hk {= F: mf- k , G : nf-l . Bibliography [1] c.J. Aarts, RC. Backhouse, P. Hoogendijk, T.S. Voermans, and J. van der Woude. A relational theory of datatypes. Available via World-Wide Web at http://www.win.tue.nl/win/cs/wp/papers. September 1992. [2] RC. Backhouse, P. de Bruin, P. Hoogendijk, G. Malcolm, T.S. Voermans, and J. van der Woude. Polynomial relators. In M. Nivat, C.S. Rattray, T. Rus, and G. Scollo, editors, Proceedings of the 2nd Conference on Algebraic Methodology and Software Technology, AMAST'91, pages 303-326. Springer-Verlag, Workshops in Computing, 1992. [3] R.C. Backhouse, P. de Bruin, G. Malcolm, T.S. Voermans, and J. van der Woude. Relational catamorphisms. In Moller B., editor, Proceedings of the 1F1P TC2/WG2.1 Working Conference on Constructing Programsfrom Specifications, pages 287-318. Elsevier Science Publishers B.Y., 1991. [4] RC. Backhouse, H. Doornbos, and P. Hoogendijk. Commuting relators. Available via World-Wide Web athttp://www.win.tue.nl/win/cs/wp/papers.September 1992. [5] RC. Backhouse and P. Hoogendijk. Elements of a relational theory of datatypes. In B. Moller, H.A. Partsch, and S.A. Schuman, editors, Formal Program Development. Proc. IFJP TC2/WG 2.1 State of the Art Seminar, Rio de Janeiro, Jan. 1992, volume 755 of LNCS, pages 7-42. Springer-Verlag, 1993. [6] Richard Bird, Oege de Moor, and Paul Hoogendijk. Generic functional programming with types and relations. J. of Functional Programming, 6(1): 1-28, January 1996. [7] Richard S. Bird and Oege de Moor. Algebra of Programming. Prentice-Hall International, 1996. [8] RS. Bird. An introduction to the theory of lists. In M. Broy, editor, Logic of Programming and Calculi of Discrete Design. Springer-Verlag, 1987. NATO ASI Series, vol. F36. [9] RS. Bird and P. Wadler. Introduction to Functional Programming. Prentice-Hall, 1988. [10] J.H. Conway. Regular Algebra and Finite Machines. Chapman and Hall, London, 1971. [11] RP. Dilworth. Non-commutative residuated lattices. Transactions of the American Mathematical Society, 46:426-444,1939. 151 152 BIBLIOGRAPHY [12] H. Doornbos. Reductivity arguments and program construction. PhD thesis, Eindhoven University of Technology, Department of Mathematics and Computing Science, June 1996. [13] Henk Doornbos and Roland Backhouse. Induction and recursion on datatypes. In B. Moller, editor, Mathematics ofProgram Construction, 3rd International Conference, volume 947 of LNCS, pages 242-256. Springer-Verlag, July 1995. [14] Henk Doornbos and Roland Backhouse. Reductivity. Science of Computer Programming, 26(1-3):217-236,1996. [15] Achim Jung (Editor). Domains and denotational semantics: History, accomplishments and open problems. Bulletin of the European Associationfor Computer Science, 59:227-256, June 1996. [16] Maarten M. Fokkinga. Law and Order in Algorithmics. PhD thesis, UniversiteitTwente, The Netherlands, 1992. [17] PJ. Freyd and A Scedrov. Categories, Allegories. North-Holland, 1990. [18] Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements ofReusable Object-Oriented Software. Addison-Wesley, Reading, Mass., 1995. [19] CAR. Hoare and Jifeng He. The weakest prespecification. Fundamenta Informaticae, 9:51-84,217-252,1986. [20] Paul Hoogendijk and Oege de Moor. What is a datatype? Technical Report 96/16, Department of Mathematics and Computing Science, Eindhoven University of Technology, 1996. Submitted to Science of Computer Programming. Available via World-Wide Web athttp://www.win.tue.nl/win/cs/wp/papers. [21] P. Jansson. Polytypism and polytypic unification. Master's thesis, Chalmers University of Technology and University of Goteborg, 1995. [22] P. Jansson and J. Jeuring. Polyp - a polytypic programming language extension. In POPL '97: The 24th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, pages 470-482. ACM Press, 1997. [23] CB. Jay. Matrices, monads and the fast fourier transform. Technical Report UTSSOCS-93.13, University of Technology, Sydney, 1993. [24] CB. Jay. Polynomial polymorphism. In R. Kotagiri, editor, Proceedings of the Eighteenth Australasian Computer Science Conference: Glenelg, South Australia 1-3 February, 1995, volume 17, pages 237-243. ACS. Communications, 1995. [25] CB. Jay. A semantics for shape. Science ofComputer Programming, 25:251-283,1995. [26] CB. Jay and J.R.B. Cockett. Shapely types and shape polymorphism. In D. Sannella, editor, Programming Languages and Systems - ESOP '94: 5th European Symposium on Programming, Edinburgh, u.K., April 1994, Proceedings, Lecture Notes in Computer Science, pages 302-316. Springer Verlag, 1994. 153 BIBLIOGRAPHY [27] J. Jeuring. Polytypic pattern matching. In Conference Record ofFPCA '95, SIGPLANSIGARCH-WG2.8 Conference on Functional Programming Languages and Computer Architecture, pages 238-248, 1995. [28] J. Jeuring and P. Jansson. Polytypic programming. In J. Launchbury, E. Meijer, and T. Sheard, editors, Proceedings of the Second International Summer School on Advanced Functional Programming Techniques, pages 68-114. Springer-Verlag, 1996. LNCS 1129. [29] J. Lambek. A fixpoint theorem for complete categories. Mathematische Zeitschrijt, 103:151-161, 1968. [30] Karl J. Lieberherr, Ignacio Silva-Lepe, and Cun Xiao. Adaptive object-oriented programming using graph-based customization. Comm.A.C.M., 37(5):94-101, May 1994. [31] G. Malcolm. Homomorphisms and promotability. In J.L.A. van de Snepscheut, editor, Conference on the Mathematics ofProgram Construction, pages 335-347. SpringerVerlag LNCS 375, 1989. [32] G. Malcolm. Algebraic data types and program transformation. PhD thesis, Groningen University, 1990. [33] G. Malcolm. Data structures and program transformation. Science of Computer Programming, 14(2-3):255-280, October 1990. [34] L. Meertens. Algorithmics - towards programming as a mathematical activity. In Proceedings of the CWI Symposium on Mathematics and Computer Science, pages 289334. North-Holland, 1986. [35] Lambert Meertens. Calculate polytypically! In Herbert Kuchen and S. Doaitse Swierstra, editors, Proceedings of the Eighth International Symposium PLILP '96 Programming Languages: Implementations, Logics and Programs, volume 1140 of Lecture Notes in Computer Science, pages 1-16. Springer Verlag, 1996. [36] E. Meijer, M.M. Fokkinga, and R. Paterson. Functional programming with bananas, lenses, envelopes and barbed wire. In FPCA91: Functional Programming Languages and Computer Architecture, volume 523 of LNCS, pages 124-144. Springer-Verlag, 1991. [37] R. Milner. A theory of type polymorphism in programming. J. Compo Syst. Scs., 17:348375,1977. [38] R. Milner. The standard ML core language. Polymorphism, II(2), October 1985. [39] E. Moggi. Notions of computation and monads. 93(1 ):55-92, 1991. Information and Computation, [40] O. de Moor. Categories, Relations and Dynamic Programming. PhD thesis, Oxford University Laboratory, Programming Research Group, April 1992. 154 BIBLIOGRAPHY [41] Gordon D. Plotkin. Lambda-definability in the full type hierarchy. In J.P. Seldin and J.R. Hindley, editors, To H.E. Curry: Essays on Combinatory Logic, Lambda Calculus and Formalism. Academic Press, London, 1980. [42] B. Randell, G. Ringland, and w.A. Wulf (Eds.). Software 2000: A View of the Future. European Commission, Brussels, 1994. [43] J.e. Reynolds. Types, abstraction and parametric polymorphism. In R.E. Mason, editor, IFIP '83, pages 513-523. Elsevier Science Publishers, 1983. [44] FJ. Rietman. A note on extensionality. In J. van Leeuwen, editor, Proceedings Computer Science in the Netherlands 91, pages 468-483, 1991. [45] J. Riguet. Relations binaires, fermetures, correspondances de Galois. Bulletin de la Societe Mathematique de France, 76: 114-155, 1948. [46] G. Schmidt and T. StrOhlein. Relation algebras: Concept of points and representability. Discrete Mathematics, 54:83-92, 1985. [47] G. Schmidt and T. Strohlein. Relations and Graphs, Discrete Mathematicsfor Computer Scientists. EATCS Monographs on Theoretical Computer Science. Springer-Verlag, Berlin Heidelberg, 1993. [48] e. Strachey. Fundamental concepts in programming languages. Lecture Notes, International Summer School in Computer Programming, Copenhagen, August 1967. [49] A. Tarski. On the calculus of relations. Journal of Symbolic Logic, 6(3):73-89, 1941. [50] Alfred Tarski and Steven Givant. A Formalization of Set Theory without Variables, volume 41 of Colloquium Publications. American Mathematical Society, Providence, Rhode Island, 1987. [51] D. Tuijnman. A Categorical Approach to Functional Programming. PhD thesis, Department of Computer Science, University of Ulm, Germany, 1996. [52] P. Wadler. Theorems for free! In 4'th Symposium on Functional Programming Languages and Computer Architecture, ACM, London, September 1989. Index [F; _], F-cocatamorphism, 49 -D, F-catamorphism, 17 _ <, range, 28 _>, domain, 28 converse, 23 _<1, target, 7 .J>, source, 7 [_], cocatamorphism, 49 U' catamorphism, 17 F, cofan-function, 89 F, fan-function, 87 1, unit, 28 I\, power transpose, 30 ..LL, bottem, 31 IT, top, 23 n, intersection, 23 U, union, 31 composition (meta), 11 ., cojunc, 88 ..., cosplit, 43 f---', natural transformation, 44 Y, conatural transformation, 44 E, membership, 30 /, factor, 32 \, factor, 32 v,junc, 14,38 ", split, 13, 40 <:;;;, partial order, 23 +, coproduct, 15,39 x, product, 14,42 (Fo), post-composition, 12 (oF), pre-composition, 12 bijection, 25 broadcast, 125 ~F; canonical copies map, 137 canonical fan, 86 canonical fan-function, 87 canonical strength, 137 carrier, 16 catamorphism, 17 relational, 47 category, 7 category CoS10k, 55 category Slok, 52 cocatamorphism, 49 cofan-function, 89 cofunction, 25 cofuseable, 56 cojunc,88 commute endorelators, 96 multi-valued relators, 111 single-valued relators, 109 commuting, 100 commuting diagram, 9 composition, 7 relational, 23 computation rule catamorphism, 17 june, 15 june, relational, 38 split, 13 split, relational, 40 conatural transformation, 44 condition, 29 converse, 23 cop, copies map, 130 coproduct, 14 relational, 37 0 _ , 0, adjoint, 22 algebra, 16 allegories, 21 arrows, 7 ass, associate, 102 155 156 coproduct functor, 15 coproduct relator, 39 copyable natural transformation, 140 corecoverable, 56 CoSlok,55 cosplit-split elimination, 43 Dedekind's law, 24 distribution law june, 15 split, 13,41 division, 31 domain, 28 E, existential image, 30 extensionality axiom, 33 F-cocatamorphism, 49 F-algebra, 16 fan canonical, 86 endorelator, 84 multi-valued relator, 87 single-valued relator, 86 fan-function, 87 F-catamorphism, 17 F-homomorphism, 16 free theorem, 19 higher-order, 20 F-shape, 11 F-structure, II Fun, functor category, 12 function, 25 functor, 10 fuseable, 53 fusion catamorphism, 17 catamorphism, relational, 48 june-coproduct, 15 product-split, 14 product-split, relational, 43 Galois connected, 22 Galois connection, 22 half-zip endorelators, 96 INDEX multi-valued relators, 109 single-valued relators, 108 has coproducts, 14 has products, 13 higher-order free theorem, 20 higher-order naturality, 20 higher-order parametricity, 97 homomorphism, 16 Id, identity functor, II id, identity arrow, 7 identification axiom, 67 in, initial algebra, 17 initial, 9 algebra, 17 injection relations, 37 injection arrows, 14 injective, 25 inl, left injection, 14 in r, right injection, 14 intersection, 23 isomorphism, 8 june, 14 relational, 38 KA , constant functor, 11 left-condition, 29 lid, left identity, 131 locally complete, 31 lower adjoint, 22 Map, 25 Map,8 mem, membership, 67 membership endorelator, 67 multi-valued relator, 72 single-valued relator, 70 modular identity, 25, 42 modular law, 24, 42 natural transformation, 11, 44 relational, 44 natural transformation, proper, 44 157 INDEX naturality injections, 15 projections, 14 objects, 7 opposite category, 9 Outl, left projection functor, 14 outl, left projection, 13 Outr, right projection functor, 14 outr, right projection, 13 paramett<rized datatypes, 18 parametrically polymorphic, 20 partial identities, 24 point, 33 parametrically, 20 polymorphism, 2 parametric, 3 polytypic, 2, 20 polytypy, 2 parametric, 4 post-composition, 12 power allegories, 30 power object, 30 power relator, 47 power transpose, 30 pre-composition, 12 product, 13 relational, 40 product functor, 14 product relator, 42 projection arrows, 13 proper natural transformation, 44 range, 28 recoverable from, 53 regular, 64 Rel,8 relational coproduct, 37 relational june, 38 relational product, 40 relational split, 40 relator, 35 rid, right identity, 102 right-condition, 29 shape, 11 shapely functor, 140 shunting rule, 26 simple, 25 Slok,52 slok property, 51, 52 source, 7 split, 13 relational, 40 str, strength, 102 strength, 102 strong natural transformation, 139 relator, 102 structure, II structure multiplication, 105 surjective, 25 tabular allegory, 27 tabulation, 27 target, 7 terminal, 8 theorems for free, 2, 19 total, 25 tree type functor induced by @, 19 tree type fusion rule, 19 tree type relator induced by 161, 49 union, 31 unique extension, 17 unit, 27 unitary, 28 upper adjoint, 22 zip endorelators, 96 multi-valued relators, III single-valued relators, 109 Samenvatting Introductie De eigenschap "generiek" - in de betekenis van "algemeen, niet specifiek of speciaal" is zander meer een noodzakelijke eigenschap voor computer software. Computers zijn immers geschikt voor meerdere doeleinden en de meest succesvolle toepassingen zijn software systemen die vanwege hun brede toepasbaarheid geschikt zijn voor een grote markt en teyens aangepast kunnen worden aan de specifieke eisen van een klant. Voorbeelden van dergelijke systemen zijn workflow management systemen, logistieke systemen en systemen voor financiele administratie. In dit proefschrift gaan we niet in op zulke grootschalige systemen. We zullen ons beperken tot kleine programma's en programma-onderdelen (algoritmen). Ook op dit niveau is de mogelijkheid om zogenaamde "generieke" programmacode te schrijven om veel voorkomende programma-patronen te beschrijven van groot belang voor het hergebruiken van programmacodes en bevorderen van de produktiviteit van de programmeur. Van veel programmeertalen en programmeer-methologieen wordt beweerd dat ze enige vorm van genericiteit ondersteunen. Voorbeelden zijn "overloading", de generieke klasse van ADA, polymorfie van functionele programmeertalen en "inheritance" bij object georienteerd programmeren. Rergebruik is niet de enige reden waarom generieke programma's van belang zijn. Een andere reden is de mogelijkheid om het achterliggende concept bij verschillende maar toch geIijksoortige algoritmen te kunnen unificeren. In [35] vraagt Meertens zich af wat opwindender is: weer een ander algoritme te vinden of te ontdekken dat twee bekende algoritmen voorbeelden zijn van een, abstracter algoritme. Dit laatste kan tot nieuwe inzichten leiden, aanleiding geven voor het vinden van andere verbanden, het mogelijk maken om onze kennis te organiseren en te structureren, en er uiteindelijk voor zorgen dat vraagstukken die ooit wetenschappelijke hoogstandjes waren, routinematige taken worden. Er zijn nog twee andere redenen die we van belang achten. De eerste reden is de toename in compactheid en precisie die een hogere graad van abstractie biedt. De tweede reden is de afname van de bewijsverplichting aangezien het niet nodig is steeds min of meer hetzelfde argument te herhalen voor de vele specifieke gevallen. Genericiteit wordt in het algemeen belangrijk gevonden. Ret is echter moeilijk objectief te zijn over de graad van genericiteit. De term "generiek" kan net zoals andere modewoorden straffeloos worden toegepast op bijna alles. Ret doel van dit proefschrift is om een bijdrage te leveren aan de ontwikkeling van een wiskundige theorie van generiek programmeren, waarbij verifieerbare criteria worden gegeven voor de notie van genericiteit. 159 160 Gegevensstructuren Vee1al bepaalt de structuur van de invoer voor een programma de structuur van het te schrijven programma. Een voorbeeld hiervan is een programma dat de som van een lijst van getallen berekent. Een lijst kan worden opgevat als ofwei een lege lijst, ofwei een getal gevolgd door een (mogelijk lege) lijst van getallen. Het meest voor de hand liggende programma voor het berekenen van de som van een lijst getallen werkt als voIgt. Eerst wordt er gecontroleerd of de lijst leeg is; in dat geval is het antwoord nul. Indien de lijst niet leeg is, wordt het eerste getal opgeteld bij de som van de lijst van de overige getallen. Het ligt voor de hand dat het zo ook mogelijk is een programma te schrijven dat aile getallen optelt die zijn opgeslagen in een ingewikkeldere gegevensstructuur dan bijvoorbeeld lijsten. Het programma voIgt de structuur van een specifieke gegevensstructuur en telt aile getallen op die het onderweg tegenkomt. Op deze manier kun je voor iedere specifieke gegevensstructuur een programma schrijven dat de som van aile getallen berekent. Met de generieke concepten beschreven in dit proefschrift is het mogelijk om een algemeen, abstract programma-schema te schrijven, waarin de specifieke vorm van de gegevensstructuur nog moet worden ingevuld. Door het invullen van de vorm van de gegevensstructuur wordt het bijbehorende programma verkregen. Een programma waarin de specifieke vorm van de gegevensstructuur waarop het programma gaat werken nog moet worden ingevuld, noemen we "polytypiek". In dit proefschrift beschrijven we verschillende polytypieke concepten, waarmee dergelijke polytypieke programma's geschreven kunnen worden. "Element van"-test Een van de belangrijkste polytypieke concepten die we beschouwen in dit proefschrift is de notatie van een "element van"-test. Dit is de test die bepaalt of een waarde is opgeslagen in een gegevensstructuur. We geven een polytypieke karakterisatie van deze test. Dat wil zeggen dat we precies die eigenschappen definieren waaraan een "element van"-test voor een willekeurige gegevensstructuur moet voldoen. We gaan zelfs een stapje verder: we definieren een "gegevensstructuur" als een wiskundige object waarvoor een "element van"-test bestaat. Met andere woorden, de minimale eis voor een gegevensstructuur is dat opgeslagen waarden gei"nspecteerd kunnen worden. De "element van"-test blijkt fundamenteel te zijn. Het is mogelijk om andere, ingewikkelde noties voor een gegevensstructuur uit te drukken in termen van de bijbehorende "element van"- test. Verwisselen van gegevensstructuren Een probleem dat we in detail uitwerken in dit proefschrift is de generalisatie van matrixtranspositie. Als je een matrix van dimensies n bij m beschouwt als een lijst ter lengte n die lijsten ter lengte m bevat, dan is de transpositie van deze matrix een lijst ter lengte m die lijsten ter lengte n bevat. Het algemene probleem is om een programma te schrijven dat een gegevensstructuur van het type F die gegevensstructuren van het type G bevat, te transformeren naar een gegevensstructuur van het type G die gegevensstructuren van het type F bevat. Hiervoor geven we een polytypieke karakterisatie van zo'n transformatie. Dat wil zeggen dat 161 we een definitie geven waarin de specifieke keuze voor de buitenste en de binnenste gegevensstructuur nog niet zijn ingevuld. Door het invullen van een keuze voor de twee soorten gegevensstructuren krijg je de specificatie van de transformatie van die twee gegevensstructuren. Door bijvoorbeeld voor de twee gegevensstructuren lijsten te nemen, wordt matrixtranspositie verkregen. Hogere orde natuurlijkheid Een belangrijk hulpmiddel bij het abstract definieren van gegevensstructuur-transpositie is de notie van "hogere orde natuurlijkheid". Hogere orde natuurlijkheid drukt uit dat de verschillende transposities behorende bij gerelateerde gegevensstructuren ook gerelateerd zijn. Voor elk polytypiek programma is een hogere orde natuurlijkheid eigenschap te bepalen. Ais deze hogere orde natuurlijk eigenschap geldt dan betekent dit dus dat twee verschillende programma's die verkregen zijn door het invullen van twee gerelateerde gegevensstructuren ook gerelateerd zijn. Aangezien we dit een belangrijke eigenschap vinden voor polytypieke programma's eisen we dat voor elke polytypiek programma de bijbehorende hogere orde natuurlijkheid eigenschap moet gelden. Op deze manier wordt gegarandeerd dat een polytypiek programma zich gelijksoortig gedraagt voor verschillende gegevensstructuren. Inderdaad geldt voor gegevensstructuur-transpositie en de "element van"-test de bijbehorende hogere orde natuurlijk eigenschappen. Hogere orde natuurlijkheid voldoet aan de eisen die we ons in het begin hebben gesteld. Het is verifieerbaar en draagt bij aan een toename in compactheid en precisie met name bij bewij svoering. Curriculum Vitae Paul Ferenc Hoogendijk 3 maart 1968 Geboren te Eindhoven juli 1986 Diploma VWO Eindhovens Protestants Lyceum juni 1991 Doctoraal examen Technische Informatica Technische Universiteit Eindoven juli 1991 - juli 1995 Onderzoeker in opleiding in dienst van de Nederlandse Organisatie voor Wetenschappelijk Onderzoek (NWO), verbonden aan de Technische Universiteit Eindhoven 163 STELLINGEN behorende bij het proefschrift A Generic Theory of Data Types van Paul Hoogendijk 1. Een belangrijk ontwerpcriterium bij het ontwerpen van een goede notatie is dat veel voorkomende transformatiestappen impliciet plaats vinden doordat een formule op meerdere, maar gelijkwaardige manieren kan worden gei"nterpreteerd. Een voorbeeld hiervan is het weglaten van de haakjes bij een infix-genoteerde associatieve operator. Helaas is het niet altijd mogelijk om de ideale notatie te verzinnen, omdat zo'n notatie niet hoeft te bestaan of omdat deze in strijd is met andere criteria voor een goede notatie. 2. Als er voor relator F een relatie out bestaat van het type FT (-- T zodanig dat er een functie [ _] op relaties bestaat waar voor geldt: a. [R] : T (--A b. [out] = id T {= R: FA(--A , c. [Rr· [5] = v(X H RO . FX . 51 , dan is out een relationele terminale F-co-algebra en [_] de bijbehorende relationele extensie van de anamorfisme operator. 3. Als out een terminale F-co-algebra is voor de sub-categorie van totaJe functies dan is onder aanname van het bestaan van tabulaties en het keuze-axioma, out een relationele terminale coalgebra als gedefinieerd in stelling 2. 4. Het geeft te denken dat hoewel monaden worden gebruikt voor functioneel programmeren, de monaden-wetten vrijwel nooit worden geverifieerd of worden gebruikt bij het programmeren zelf. Zie bijvoorbeeld [1]. [1] P. Wadler. Monads for Functional Programming in J. Jeuring en E. Meijer, editors, Advanced Functional Programming, Springer-Verlag, 1995. 5. Sommige bewijzen van eigenschappen van relationele producten te vinden in de literatuur zijn onnodig gecompliceerd, doordat de auteurs geen gebruik maken van het feit dat de universele eigenschap voor producten geldt in de sub-categorie van totaJe functies. Zie bijvoorbeeld [2]. [2] Chris Brink, Wolfram Kahl and Gunther Schmidt, editors, Relational Methods in Computer Science. Springer-Verlag, 1997. 6. In [3] wordt op onvoldoende wijze onderscheid gemaakt tussen het definieren van het relationele coproduct en van het relationele catamorfisme enerzijds en het bestaan van deze concepten anderzijds. [3] Richard S. Bird and Oege De Moor. Algebra of Programming. Prentice-Hall International, 1996. 7. Het is opvallend hoe groot de overeenkomst is tussen het schrijven van een proefschrift en het schrijven van een computerprogramma. 8. Gezien de mogelijk te verwachten rampspoed die ons vanaf 1 januari 2000 zal treffen als gevolg van het "jaar 2000"-probleem voor computerprogrammatuur, is het aan te bevelen de millenniumfeesten een jaar uit te stellen. Dit te meer daar het derde millennium pas op I januari 2001 begint. 9. Het zou het imago van frisbee als sport ten goede komen als er aileen frisbeeschijven in omloop zouden worden gebracht waar redelijk mee kan worden overgegooid. Helaas is dit voor frisbees die voor reclamedoeleinden worden weggeven meestal niet het geval.