A Functional Pattern System for Object-Oriented Design Thomas K ¨uhne } A child does not dis- cover the world by learning abstract rules. Instead it learns by looking at concrete examples. An example contains the rules as well. In contrast to rules, the recognition of examples can be based on tangible reality. The knowledge extracted from an example serves as a Pattern that is used to remember facts and to construct new solutions. When grown-ups are about to learn something or have to apply unknown tools, they are put into a child’s position again. They will favor concrete examples over abstract rules. The rules will happily be gen- erated for automatically, this is how the brain works. } Thesis iii Thesis Design patterns inspired by functional programming concepts can advance object- oriented design. Problem The object-oriented paradigm has undoubtfully raised our ability to design and maintain large complex software systems. However, it does not seem to have meet the high expectations concerning reuse and ease of evolution which have been pro- moted ever since its commercial success. There are many potential reasons for the above observation such as unquali- fied staff, immature languages, inadequate methodologies, inappropriate business processes, etc. The view presented here is that although the object-oriented paradigm is a pow- erful basis, it is incomplete in its inherent concepts and therefore restricts the design space to inappropriate solutions. It is assumed that both software development and language design are restrained from achieving their full potential when restricted to a purely object-oriented world view. Solution Since the complementary paradigm to object-orientation is represented by func- tional programming, I investigate high-level, well-known to work functional con- cepts and examine their suitability to enhance object-oriented design. I explore the software engineering relevance of each concept and present its intent, applicability, implementation, and consequences in the literate form of a design pattern. My approach clearly motivates functional techniques for object-oriented design from a software engineering point of view. This is different to the usual procedure of designing a new language with an “ad-hoc” conglomeration of functional and object-oriented features. The latter case requires excellence in language design and makes it hard to find out and evaluate uses of the new language. In contrast, design patterns are already widely used to improve design. As func- tional concepts constitute a powerful paradigm by themselves, it is more than sug- gestive to assume that design patterns expressing successful functional concepts will enhance the object-oriented paradigm with new capabilities. iv Contribution Feasibility Thesis I demonstrate the feasibility of using functional techniques in object-oriented de- signs which are to be implemented by ordinary object-oriented programming lan- guages. This is done at the level of a calculus comparison and in concrete design pattern implementation descriptions. I demonstrate synergetic effects caused by concept integration, which together with the advantages of functional patterns, thus, show the utility of the approach. Software production Object-oriented practitioners hopefully will use the names of functional design pat- terns as a vocabulary to discuss solutions in a new design space. I present a system of patterns which are connected by relations that describe how individual patterns may interact and collaborate with each other. This system, consisting of state-of- the-art mini-architectures, may allow thinking and designing beyond restrictions imposed by a dogmatic object-oriented approach. As a result, the quality of soft- ware is hoped to improve. Language Design Using functional patterns for object-oriented design can be regarded as dual- paradigm design. In this light, functional design patterns appear as language id- ioms that lift an object-oriented language to a dual paradigm implementation lan- guage. It is very instructive to verify how well an object-oriented language supports the implementation of these idioms, since limiting properties are expected to interfere in other attempts to produce flexible and maintainable software as well. Unless one is restricted to use a certain existing language, it is, however, only natural to consider direct language support in order to avoid the repetitive im- plementation of these idioms. A holistic language that encompasses both object- oriented and functional paradigms should provide more ease of use, increased safety, better initial execution efficiency, and higher optimization potential. I consider each presented design pattern for its contribution to language con- structs that support a dual paradigm language. The software engineering consid- erations, contained in each design pattern description, help to avoid “featurism” in favor of conceptually founded language principles. Ultimately, impulses initiated by the functional pattern system lead to a reeval- uation of the role distribution between a programming language and its associated environment. The result allows transcending the limitations of each paradigm by providing the optimal paradigm view on demand. Preface v Preface Example is the school of mankind, and they will learn at no other. – Edmund Burke A number of people directly or indirectly influenced my work and I am grateful for their contributions. Norbert Ihrig made my early years in school a worthwhile experience and with- out him my way would have been much harder. Gregor Snelting proposed an interesting master thesis to me and, thus, opened up the subsequent opportunity for a research position at the institute for “Praktis- che Informatik” at the Darmstadt University of Technology. I enjoyed his lectures and am thankful for his continued support. Thilo Kielman introduced me to the world of being a scientist. I owe him many hints, paper reviews, and enjoyable hours. Alberto Pardo was my roommate over a long period and shared many stimulat- ing discussions with me. I would like to express my gratitude for his ever present willingness to help and for a lot of advice in the areas of calculi and algebras. He and Nick Dyson helped to improve the chapter on functional programming. John Sargeant invested considerable effort to discuss the Void Value pattern with me and provided useful information on UFO. Dirk Koschorek provided invaluable last minute advice on SMALLTALK. Many scientists, who I had the honor to meet at workshops or conferences, dis- cussed aspects of my work with me and made me reflect about it. Gillian L. Lovegrove and J ¨urgen Ebert accepted to be external examiners and I am indebted to their timely efforts. I would like to thank J ¨urgen Ebert for his invitation to the “Bad Honnef” workshop series and for his personal interest in my work. Finally, I would like to express my gratitude to my thesis supervisor Wolfgang Henhapl. He gave me all the freedom I could wish for and stimulated my work on several occasions. He asked the question to which function objects present an an- swer and challenged me to conceive the Translator design. Numerous discussions with him created an abundance of insights for me. I am honestly grateful to the nameless cosmic particles that hit me when I had the idea of function objects, to replace Nil with a type specific value, to solve the forces of internal and external iteration with multiple consumable intermediate streams, to express functional ideas by devising a pattern system, and to rethink the roles of languages and their environments based on the notion of tiles. vi Preface Many thanks to Cordon Art for granting permission to use the images by M.C. 1998, Cordon “Circle Limit IV”, “Space Filling”, and “Day and Night”. (cid:211) Escher Art B.V., Nieuwstraat 6, P.O.Box 101, 3740 AC Baarn, Holland. Last but not least, my appreciation goes to to Donald E. Knuth who developed TEX and put it into the public domain, Frank Mittelbach for LATEX 2e , Linus Torvald for LINUX, and all their companions and successors who enabled me to produce this document with copylefted software only. (cid:210) Contents Contents Thesis Preface Prologue I Foundation 1 Functional programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functional decomposition . . . . Reduction Semantics . . . . . . . . . . . . . . . . . 1.1 Worldview . . . . . . 1.2 Concepts . . . . 1.2.1 . . . 1.2.2 . . . . 1.2.3 Higher-Order Functions . . . . . . . . Lazy evaluation . 1.2.4 . . . . . . Pattern Matching . 1.2.5 . . . . . . . Type inference . . 1.2.6 . . . . . . . . . . Review . . . . . . . . . . . . . . 1.3.1 Pro . . Programming Discipline . 1.3.1.1 . . . 1.3.1.2 Concise programs . . . . . . . . . Contra . . Functional decomposition . 1.3.2.1 . . 1.3.2.2 . Reduction semantics . . . . 1.3.2.3 Deceptive Clearness . . . . . 1.3.2 . . . . . 1.3 . . . . . . . . . . . . . . . . . 2 Object-orientation 2.1 Worldview . . . 2.2 Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.1 Object-oriented decomposition . . Encapsulation . . . 2.2.2 . . . Inheritance . . 2.2.3 . 2.2.4 . . . Subtyping . . 2.2.5 Dynamic Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii iii v 1 7 9 9 10 10 11 12 14 16 17 17 17 18 18 21 21 21 26 29 29 31 32 32 33 34 35 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viii 2.3 2.2.6 Review . 2.3.1 2.3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Identity . . . . . . . . Pro . . . Real world modeling . 2.3.1.1 . 2.3.1.2 Maintainability . 2.3.1.3 . Contra . . 2.3.2.1 2.3.2.2 Classes for everything . 2.3.2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . Inheritance for everything . . Beyond the imperative . . . Imperative heritage . . . . . . . . . . . . . 3 Calculus comparison Language comparisons . . 3.1 3.2 Opulus . . . 3.3 Opulus within l -calculus . l -calculus within Opulus . 3.4 . 3.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Conflict & Cohabitance . 4.1 Conflict . . . . . . . 4.1.1 Oppositions . . . . . . . . . . . . . . . . . . . . . . . Semantic model 4.1.1.1 . . 4.1.1.2 Decomposition . . . Evaluation . . 4.1.1.3 . . . Encapsulation . 4.1.1.4 . . . . . Redundancy . . . . 4.1.2.1 Parameterization . . 4.1.2.2 Dispatch . . . . . . . . . . Subsumption . . . . . 4.2.1.1 . . 4.2.1.2 Dispatch . . . Integration . . . . 4.2.2.1 . . 4.2.2.2 Closures . . . . . . Synergy . Parameterization . . 4.2.3.1 . . 4.2.3.2 Decomposition . . . . . . . . . . . Pure functions . . . . . . Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1.2 4.2.1 4.2.2 4.2.3 4.3 Conclusion . 5 Design Patterns . . 5.1 Definition . . . . . . 5.1.1 A gentle introduction . . . . 5.1.2 . . 5.2 History . Software Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 Cohabitance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 38 38 38 39 40 41 41 42 42 45 45 46 47 49 51 55 55 55 55 56 56 56 57 57 57 57 57 57 58 59 59 59 59 60 64 68 69 69 69 72 74 Contents . . . . . . . . Promise . . Form . . . . 5.3 5.4 . . 5.5 Diagram notation . . . . . . . . Class diagram notation . . . . 5.5.1 . 5.5.2 Object diagram notation . 5.5.3 . . . . . Interaction diagram notation . . . . . . . . . . . . . . . . . . . . . . . . . . II Pattern System 6 Catalog 6.1 6.2 . . . . . . . Proto-Patterns . Pattern System . 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.2.6 . . . . Function Object . . . . Lazy Object . . Value Object . . . . Transfold . . . . . Void Value . . . . . Translator . . . . . . . 6.3 Why Eiffel? . 7 Function Object . . . . . . . . . . . . . . . Intent . . 7.3.1 7.3.2 7.1 . 7.2 Also Known As . . 7.3 Motivation . . . . . . Implementation . . . . . . . . Problem . . Solution . . . . . . . . . . . . . . . . . . . . . . . . . . 7.4 Applicability . . . . . . Structure . 7.5 . . 7.6 . Participants . . . 7.7 Collaborations . . 7.8 Consequences . 7.9 . . 7.10 Function Object variants . . . 7.11 Known Uses . . . . 7.12 Related Patterns 7.12.1 Categorization . . 7.12.2 Collaboration . . 7.12.3 Implementation . . . . . . . . 8 Lazy Object . . . . Intent . . 8.1 . 8.2 Also Known As . . 8.3 Motivation . . . . . . . . Problem . . 8.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 76 76 79 80 80 81 83 85 85 86 86 87 87 88 88 89 91 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 93 . . 93 . . 93 . . 93 . . 96 . . . . 98 . . 100 . . 100 . . 101 . . 102 . . 105 . . 107 . . 110 . . 111 . . 111 . . 112 . . 113 115 . . 115 . . 115 . . 115 . . 115 x 8.3.2 8.4 Applicability . . Structure . 8.5 . . 8.6 . Participants . 8.7 Collaborations 8.8 Consequences . 8.9 8.10 Sample Code . 8.11 Known Uses . . 8.12 Related Patterns Solution . . . . . . . . . . . . . . . . . . . . . . . . . Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.12.1 Categorization . . 8.12.2 Collaboration . . 8.12.3 Implementation . 9 Value Object . . . . Intent . . 9.3.1 9.3.2 9.1 . 9.2 Also Known As . . 9.3 Motivation . . . . . . . . Problem . . Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.11.1 Categorization . . 9.11.2 Collaboration . . Implementation . 9.11.3 . . . . . Implementation . . 9.4 Applicability . . . . Structure . 9.5 9.6 . Participants . 9.7 Collaborations 9.8 Consequences . 9.9 9.10 Known Uses . . 9.11 Related Patterns 10 Transfold . . . . . 10.1 Intent . . . . 10.2 Motivation . . . . . 10.2.1 Problem . . 10.2.2 Solution . . . . . . . . . . . . . . . . . . . 10.3 Applicability . . . . . 10.4 Structure . . 10.5 Participants . . . 10.6 Collaborations 10.7 Consequences . . 10.8 Implementation . . 10.9 Sample Code . . . 10.10 Known Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 . . 119 . . 121 . . 121 . . 122 . . 122 . . 127 . . 130 . . 143 . . 145 . . 145 . . 145 . . 146 149 . . 149 . . 149 . . 149 . . 149 . . 152 . . 154 . . 154 . . 155 . . 155 . . 155 . . 158 . . 160 . . 161 . . 161 . . 161 . . 162 163 . . 163 . . 163 . . 163 . . 167 . . 173 . . 174 . . 175 . . 175 . . 176 . . 180 . . 182 . . 187 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Contents 10.11 Related Patterns . . . . . 10.11.1 Categorization . . 10.11.2 Collaboration . . 10.11.3 Implementation . 11 Void Value . . . . . 11.1 Intent . . . . 11.2 Motivation . . . . . 11.2.1 Problem . . 11.2.2 Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.10.1 Categorization . . 11.10.2 Collaboration . . 11.10.3 Implementation . . 11.3 Applicability . . . 11.4 Structure . . . . 11.5 Participants . . . 11.6 Collaborations 11.7 Consequences . . 11.8 Implementation . . . 11.9 Known Uses . 11.10 Related Patterns 12 Translator . . . . . 12.1 Intent . . . . 12.2 Motivation . . . . . 12.2.1 Problem . . 12.2.2 Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.10.1 Categorization . . 12.10.2 Collaboration . . 12.10.3 Implementation . . 12.3 Applicability . . . 12.4 Structure . . . . . 12.5 Participants . . 12.6 Collaborations 12.7 Consequences . . 12.8 Implementation . 12.9 Sample Code . . 12.10 Related Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Collaboration 13.1 Example collaboration . . . . . . . . . . . . . . . . . . . . . . . . . xi . . 188 . . 188 . . 188 . . 188 191 . . 191 . . 191 . . 191 . . 192 . . 194 . . 195 . . 195 . . 196 . . 196 . . 197 . . 199 . . 199 . . 199 . . 199 . . 200 201 . . 201 . . 201 . . 202 . . 203 . . 204 . . 206 . . 207 . . 208 . . 209 . . 213 . . 214 . . 217 . . 217 . . 218 . . 219 221 . . 227 xii Contents III Language design . 14 Pattern Driven Language Design . . . . . . . . 14.1 Introduction . . 14.2 Function Object . . 14.3 Translator . . . 14.4 Transfold . . . 14.5 Value Object . . 14.6 Lazy Object . . 14.7 Void Value . . . 14.8 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Epilogue Bibliography Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 233 . . 233 . . 233 . . 238 . . 240 . . 243 . . 246 . . 248 . . 252 261 275 305 ListofFigures xiii List of Figures Functional decomposition . 1.1 . . 1.2 Comparison between imperative and functional style . . . . 1.3 . . 1.4 Functional and imperative LIFE . . I/O feedback model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1 Object-oriented taxonomy . 2.2 . . Ideal inheritance . . 2.3 Classes as versions and variants . . . 2.4 . 2.5 Dynamic method lookup . Subtyping inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1 3.2 3.3 3.4 Screenshot: OPULUS expression . . . Screenshot: OPULUS expression represented in the l -calculus . Screenshot: l -calculus expression . . . . . . Screenshot: l -calculus expression represented in OPULUS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1 Classification of programming paradigms . 4.2 Cooking with object responsibility . . . 4.3 Cooking with single inheritance . . . . 4.4 Cooking with multiple inheritance . . . 4.5 Cooking with repeated inheritance . . . . . 4.6 Cooking with parameterization . . . . . 4.7 Cooking with synergy . . . . . . 4.8 . Restructured dependencies . . . . . . . . . . . . . . . . 5.1 A composer’s pattern . . . Space filling pattern . . . 5.2 Software system of interwoven patterns. 5.3 . . . 5.4 OMT Notation: Class diagram . . . 5.5 OMT Notation: Object diagram . . . . 5.6 OMT Notation: Interaction diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1 7.2 8.1 8.2 Function Object structure diagram . . Function Object interaction diagram . . . . . . . . . Schematic CD-player . . . Plug-in modules in a bitstream architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 19 25 28 30 31 33 34 36 49 49 51 51 56 60 61 61 62 63 63 67 70 73 77 80 80 81 . . 101 . . 102 . . 116 . . 116 xiv . . . . . . . . . . . 8.3 Generating hamming numbers . . . Structure diagram . . . 8.4 8.5 . . Interaction diagram . . . 8.6 Definition of an Anamorphism . . . 8.7 Natural number stream . . . 8.8 Using streams . . . . . 8.9 Suspension of a stream tail . . 8.10 Streamfunction unfolding . . . . 8.11 Streamvalue insertion . . . . . . . . 8.12 Stream of primes . . . . . . . 8.13 Unfolded primes . . . . . . . . 8.14 Expanding the sieve . . . . . . . 8.15 Normalized sieve . . . . 8.16 Sample structure . . . . . . . . 8.17 Stream structure diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . Providing a “const” interface . . . . Structure diagram . . . . Interaction diagram . 9.1 . 9.2 . . 9.3 9.4 Optional mutators for Value Object . 9.5 Copy strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.1 Code example “Goodbye Mariner” . 10.2 Iteration topology with lazy streams and Transfold . . . . 10.3 Iteration of two aggregates in lock-step . . . . . . . 10.4 Transposing lists . . . . . . 10.5 Computing a list of products . . . 10.6 Transmapping equality . . . . . . . 10.7 Transfolding the inner product of a matrix . . . . 10.8 Minor axis matrix transposition . . . . . . 10.9 Transformation chain . . . . . . . . . 10.10 Structure diagram . . . . . . . . . 10.11 Interaction diagram . . 10.12 Inner product application . . . . . 10.13 Matrix multiplication with a magic square . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1 Code example: Robust behavior . . . . 11.2 Empty tree as void value . . . 11.3 Code example: Null iterator . . . 11.4 Code example: Default Behavior . 11.5 Code example: Base case definition . . . 11.6 Code example: Case distribution . 11.7 Void Value structure diagram . . . . 11.8 Automatic reference initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ListofFigures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 . . 121 . . 122 . . 128 . . 130 . . 131 . . 134 . . 135 . . 136 . . 138 . . 139 . . 141 . . 142 . . 147 . . 148 . . 151 . . 154 . . 155 . . 158 . . 159 . . 166 . . 168 . . 169 . . 170 . . 170 . . 171 . . 172 . . 173 . . 173 . . 174 . . 176 . . 182 . . 186 . . 192 . . 192 . . 193 . . 193 . . 193 . . 194 . . 195 . . 198 12.1 Homomorphic translations of trees . . . . . . . . . . . . . . . . . . . 202 ListofFigures 12.2 Generic interpretation on an abstract syntax tree . . . 12.3 Sample structure . . . . 12.4 Structure diagram . . 12.5 Interaction diagram . . . 12.6 Distinct interpretation phases . . . . 12.7 Splitting the interpretation . . . . 12.8 Non-interfering semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.1 Pattern categorization . . . 13.2 Pattern implementation relations . . . 13.3 Pattern collaborations . . . . 13.4 Pattern system relations . 13.5 Functional pattern decomposition of “Samefringe” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.1 Code example: Initialization. . 14.2 Hierarchical tiles editing . . . . . . . . . . . . E.1 E.2 Pattern Cathedral . . . . Paradigm integration with patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv . . 204 . . 205 . . 206 . . 208 . . 209 . . 210 . . 211 . . 222 . . 223 . . 224 . . 226 . . 229 . . 251 . . 258 . . 265 . . 269 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi ListofFigures ListofTables xvii List of Tables . . . . . . . . . . . . . 3.1 Opulus syntax . . . . 3.2 Opulus BNF . Translation of Opulus to l -calculus . 3.3 Translation of l -calculus to Opulus 3.4 . 3.5 Complexity of Opulus in the l -calculus . 3.6 Complexity of l -calculus in Opulus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1 Criteria to evaluate cook and recipe combination . . 4.2 Decomposition matrix . . . 4.3 . . Sensitivity of decomposition types . . . . . . . . . . . . . . . . . . . . . 5.1 5.2 6.1 6.2 6.3 Facets of “pattern” . . . Promise of design patterns . . . . . . . . Functional pattern spectrum . . . . Language levels . . . Functional pattern system . . . . . . . . . . . . . 10.1 External versus internal iteration . . 10.2 Transfold’s arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.1 Simplifications possible while translating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.1 Benefits of the functional pattern design example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.1 Pattern implications on language design . . . . 14.2 Tiles with object-oriented and functional dimensions . . . 14.3 Concrete types with implementation and interface views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 47 48 50 52 52 60 65 65 71 76 90 91 92 . . 167 . . 171 . . 211 . . 228 . . 252 . . 256 . . 257 xviii ListofTables Prologue 1 Prologue A paradigm is not a dogma. – me P aradigms define ways to perceive the world. Is a cannery a hierarchi- cal structure of functions defined on passive data, or a set of interacting active objects, or a web of concurrent processes? In analogy to theories, the value of a paradigm is determined by the usefulness of creating models accord- ing to it. A paradigm, therefore, is a means to an end. In software development the goal is to avoid semantic gaps inbetween analysis, design, and implementation while achieving reusable components, extendable applications, and maintainable software. Motivation During its now three decades spanning history, the object-oriented paradigm has proved to be tremendously useful as a basis for development methodologies and as an implementation strategy. Object-oriented languages are truly general purpose languages since they may conquer any domain by defining the appropriate abstrac- tions. Domain specific libraries or frameworks enable designers to define and use application languages, specifically tailored to model particular domains [Hudak96]. Application languages avoid an impedance mismatch between analysis and imple- mentation. Object-oriented languages to a large extent allow a natural definition of application languages with the help of class abstractions. They, hence, enable a so-called middle-out development [Ward94], where one team implements the appli- cation language while another builds the application on top of it. It appears to be almost certain that object-orientation will not cease as a dead end in the evolution of programming and design paradigms. However, there are signs of a disintegrating kingdom. The fact that the next Kuhnian paradigm shift [Kuhn70] is overdue [Quibeldey-Cirkel94] is not a strong indication, but in 1994 an issue of BYTE magazine devoted to compo- nent software shook the object-oriented community by stating that object-oriented technology failed to keep its promises and that VISUAL BASIC-like, component oriented languages offer higher productivity [Byte94]. Subsequently, the func- tional paradigm scored higher in analysis suitability [Harrison et al.94] and the supremacy of the object-oriented paradigm was found to be “an open research 2 Prologue issue” [Menzies & Haynes95]. Further competition appeared promoting “Subject- oriented programming” [Harrison & Ossher93, ObjectExpert97]. It is not my intent to refute alternative approaches to be futile, but this thesis makes a definite statement that first, object-orientation is still well in the game and second, should be the basis for an evolution rather than a revolution. But why did object-orientation not live up to its promises? A possible answer is given the the August 1995 issue of IEEE COMPUTER: “Poor design is a major culprit in the software crisis.” – Bruce W. Weide Or, more elaborate: “But simply using an object-oriented programming language or environ- ment does not, in itself, guarantee miraculous results. Like any other human endeavor, software design is an art: discipline, hard work, inspiration, and sound technique all play their parts. Object-oriented technology has much to offer, certainly. But how may it best be exploited? [Wirfs-Brock et al.90]” – Rebecca Wirfs-Brock et al. This question is still unanswered as evidenced by the 1997 call for papers of the OOPSLA ’97 workshop “Object-Oriented Design Quality”: “Despite the burst in the availability of OO analysis and design methodolo- gies, languages, database management systems, and tools, relatively little work has been done in the area of OO design quality assurance, assessment, and improvement. We badly need a better understanding of the properties of OO system design, in the small and in the large, and their effect on quality factors – Rudolf K. Keller such as maintainability, evolvability, and reusability.” Design patterns [Gamma et al.94], i.e., the documentation of successful mini- architectures, are hoped to be part of the solution to the persisting design problem. They can be used to hand down design excellence from experts to novices. Indeed, pattern books may contribute to make software engineering a true engineering sci- ence by representing engineering handbooks containing known to work solutions. In fact, patterns can make software more reusable, allow reusing of design, aid in designer communication, and can be used in teaching good design. Paradigm integration While object-orientation and design patterns — the buzzwords of the eighties and nineties respectively — receive a great deal of attention, it got rather quiet around a field which has mostly been of academic interest only: Functional programming is known for its elegance but also has the reputation of being an academic toy only. Quite often the desirable features of functional programming are believed to be inaccessible from other programming language types. Prologue 3 “Functional programming can not, apart from modest attempts, be applied with algorithmic programming languages in common use1 [Schneider91].” – H. J. Hoffmann Especially higher-order functions, i.e., the ability to support closures, are a desir- able feature, believed to be absent in programming languages like C++, EIFFEL, and so on [Baker93, Kofler93, Gamma et al.94]. Continuing attempts to explain potential benefits of functional program- ming [Hughes87], this thesis uses the literate form of design patterns to capture the benefits of functional concepts and idioms. It, therefore, demonstrates the fea- sibility of supporting a functional programming style in the above mentioned lan- guages. Of course, it is not possible to turn stateful languages into pure functional programming languages. This is not intended, however. “This discussion suggests that what is important is the functional program- ming style, in which the above features are manifest and in which side effects are strongly discouraged but not necessarily eliminated. [Hudak89].” – Paul Hudak Although some of the functional patterns to be presented introduce restrictions to gain safety, they are intended to be additions rather than constraints to existing object-oriented practices. Prior to the formulation of patterns an analysis of the two paradigms is per- formed with the view to embed functional programming into the object-oriented paradigm. This is, as such, a valuable endevour. “We need to find, generalize, and integrate similarities in programming languages. Otherwise, there will be a proliferation of concepts which nobody will be able to overview and understand [Reynolds96].” – John C. Reynolds In addition to that, the world of object-oriented design is enriched with the func- tional paradigm. The result is a multi-paradigm design space. The author is in good company in his belief that multi-paradigm programming will play a crucial role in the future: “. . . mixed language working — “integration of programming paradigms” – Chris Hankin et al. is an important theme [Hankin et al.97].” A multi-paradigm toolkit does not only represent an enhanced choice of tools but also enhances the tool user. “Research results from the psychology of programming [Petre89] indicate that expertise in programming is far more strongly related to the number of dif- ferent programming styles understood by an individual that it is to the number – Timothy Budd years experience in programming [Budd95].” 1Translated from German. 4 Prologue This can be explained by the fact that the available notions of a particular program- ming language restrict the solution space of a designer who is familiar with this language only. “Wir m ¨ussen den starken und unbestreitbaren Einfluß unserer Sprache auf die Art unseres Denkens erkennen. Mit ihr wird in der Tat der abstrakte Raum definiert und begrenzt, in dem wir unsere Gedanken formulieren, ihnen eine – Nicklaus Wirth Form geben.” In the context of this thesis it is appropriate to interpret the term “Sprache” (lan- guage) as “paradigm”. The single paradigm possibly implied by even a group of programming languages, thus, restricts designers that are familiar with this paradigm only. The introduction of functional patterns, hence, enlarges the de- sign space of formerly pure object-oriented designers. Interestingly, this is possible without requiring them to learn a new language, since the functional patterns are expressed with well-known object-oriented concepts. The attempt to capture functional concepts as patterns, evaluate their software engineering properties and then document them as parts of a engineering hand- book for design, is in tune with the primal definition of science. “It is my aim to first enumerate experience and then to proof with reason – Leonardo da Vinci why it must act in this way.” Design patterns can be a blessing for design practitioners but they are also often testimony to the weaknesses of implementation languages or underlying paradigms [Baumgartner et al.96, Seiter et al.96, Norvig96]. A further lesson to be learned from the functional pattern system is, therefore, how to better support it in future languages. Hints on reading The “Newsreader” approach to this thesis is to read chapter “Thesis” on page iii only. A more comprehensive quicktour is obtained by adding the prologue on page 1 and epilogue on page 261. Anybody interested in a glance at the patterns may take a pattern quicktour by reading chapters catalog on page 85 and collab- oration on page 221. Design practitioners, however, will savor the whole pattern system part starting on page 85. Chapters of interest to language designers are the calculus comparison on page 45, the analysis of paradigm integration on page 55, and the whole language design part beginning on page 233. Chapters functional programming on page 9, object-orientation on page 29, and design patterns on page 69 are meant as an introduction to the uninitiated but also serve to establish and clarify terminology. Prologue 5 Newsreader Thesis Prologue FOUNDATION Design Practitioner r u o t k c i u Q n r e t t a P r u o t k c i u Q r e n g i s e D e g a u g n a L Functional Programming Object-Orientation Calculus Comparison Conflict & Cohabitance Design Patterns PATTERN SYSTEM Catalog Function Object Lazy Object Value Object Transfold Void Value Translator Collaboration LANGUAGE DESIGN Epilogue 6 Prologue 7 Part I Foundation 8 9 1 Functional programming his chapter introduces functional programming and defines some terms that are used in later discussions. I explain the underlying worldview (section 1.1) and the most important concepts (section 1.2 on the next page). Finally, section 1.3 on page 17 enumerates pros and cons of functional pro- gramming. T 1.1 Worldview The functional paradigm suggests to regard everything as an expression. This can be seen as the logical conclusion from FORTRAN (mathematical flavor through expressions), to functions in PASCAL, to expression oriented style in SCHEME [Hudak89]. Since the value of mathematical expressions does not change over time, there is no need for any notion of state. Also, there is no need for control flow, because the order of expression evaluation has no impact on the final result (as long as partiality is excluded). The most important type of expression is called function application, i.e., some data is fed into a function and the function produces a result. We might say “The functional paradigm suggests to regard everything as a function” and loose only a little bit of truth. In fact, it is possible to model everything with functions only (e.g., with the pure l -calculus [Barendregt84, Hankin94]), but functional programming languages typically provide built-in primitive data structures and operations. Apparently, functional programming corresponds to two outdated software paradigms: On a small scale the transformation of data with functions corresponds to the Input/Processing/Output model [Pepper92], which has been replaced by more modern views such as distributed computation and event-triggered behav- ior. On a large scale the structuring of a program into a hierarchy of functions corresponds to structured analysis and design, which has been replaced by entity- relationship or object-oriented decomposition techniques. The above should not create the impression that functional programming is of no use anymore. On the contrary, we will see that data transformation and functional decomposition are still very useful concepts. Also, apart from the on- going academic interest in functional programming languages (e.g., [Hudak96, Gostanza et al.96, L¨aufer96]) the rise of new parallel computer architectures adds importance to languages with a strong potential for parallel execution models. 10 1 Functionalprogramming 1.2 Concepts The following sections are meant to briefly introduce concepts in functional pro- gramming. For a more thorough discussion and also for a history of functional programming languages the reader is referred to [Ebert87] and [Hudak89]. Some readers may miss the concepts of polymorphism, strong static typing, data abstrac- tion, and garbage collection. These are not discussed since they can be regarded as orthogonal to language paradigms. Equally, all these concepts have been adopted by both functional and object-oriented languages. Type inference is not paradigm specific either but has not been successfully applied to object-oriented languages yet and therefore will be a stimulus for the extension of object-oriented languages (see part III starting at page 233). Subsequently, I will use the syntax of the functional language HASKELL to de- pict functional concepts. The notation is very intuitive, but you may want to refer to an introduction like [Hudak & Fasel92]. 1.2.1 Functional decomposition I already mentioned the correspondence between functional decomposition and structured analysis and design. A program is viewed as a main function, that is decomposed into more functions with less work to accomplish (see figure 1.1). Main Function Decomposition Function Decomposition Function Library Function Decomposition Function Operations Operations Figure 1.1: Functional decomposition By functional decomposition I also include the separation between functions and data, i.e., the tools and materials metaphor. New functionality typically does not require to change existing data structures. It is simply added with external functions operating on top of existing data structures. However, changes to data structures often require to change existing functions. As functions depend on data, they must be adapted to properly react to, e.g., new constructors. See [Cook90] for a technical and Chapter 12 of [Meyer88] for a software engineering discussion of functional versus object-oriented decomposition. 1.2 Concepts 11 1.2.2 Reduction Semantics Computations within a functional programming language are performed just like reductions in a calculus. Instead of altering an implicit state with instructions, com- putation is defined as the reduction of expressions. Intuitively, we can think of a reduction step as a replacement of a term with a simpler term1. For instance: 1 + 2 + 4 =) 3 + 4 =) 7 Reduction does not always mean simplification in a na¨ıve sense, i.e., expressions may as well grow in size in intermediate steps: (1 + 2)2 =) (1 + 2) (cid:3) (1 + 2) =) 3 (cid:3) 3 =) 9 (1.1) Yet, reduction is guaranteed to produce a unique so-called normal form, if it ter- minates at all2. An expression in normal form cannot be reduced any further and is what we intuitively think of as the value of an expression. Uniqueness of nor- mal forms for the untyped l -calculus is guaranteed by the Church-Rosser Theorem I [Hudak89]. In addition, the normal form may be reached through every possible reduction order, e.g., (1 + 2)2 =) (3)2 =) (3) (cid:3) (3) =) 9 (1.2) yields the same result as the above calculation. We have to be more precise if errors (e.g., division by zero) or non-termination may also occur. For instance, False ^ (1=0 = 1) =) False but only if logical conjunction (^) is non-strict, that is, does not evaluate its second argument unless necessary. Here, the reduction order used in reduction 1.2 called applicative-order reduction3 would not produce the normal form “False”, but pro- duce a division by zero error. Fortunately, normal-order reduction4, the strategy used in reduction 1.1, always yields the normal form, if it exists. This is stated by the Church-Rosser Theorem II for the untyped l -calculus [Hudak89]. Reduction results, as well as any other expression, are immutable values. Adding an element to a list produces a new list copy with that element appended. Furthermore, the result is not distinguishable from a second list with identical el- ement order that was obtained in a different way (e.g., by removing an element), that is, values have no identity. This is a remarkable property. To see why, consider the following (non-HASKELL) code: 1Though this view is a crude oversimplification it suffices for our purposes here. 2Termination is not guaranteed in general. However, there are special systems like the simply typed l -calculus, the polymorphic typed l -calculus, system F, and languages like FP that even guarantee termination. 3Evaluate arguments first; then apply function (just like call-by-value parameter passing). 4Evaluate function first; then arguments (akin to call-by-name parameter passing). 12 1 Functionalprogramming x := 1+6 y := 2+5 Here are some questions (adapted from [Appel93]): (cid:15) Is x the same 7 as y? (cid:15) If we modify x does y change? (cid:15) Do we need a copy of 7 to implement z := x? (cid:15) When we no longer need x how do we dispose of the 7? You rightfully consider these questions as silly but what about posing them again in a different context? x := aList.add(anItem) y := aList.add(anItem) We can conclude that numbers have value semantics5 and many “silly” questions arise when reference semantics, i.e., references, mutable objects, and identity come into play. For our purposes we may recall reduction semantics to mean: 1. No implicit state ! No side-effects ! Great freedom in reduction order. 2. No mutable objects nor identity ! Aliasing is not an issue. 1.2.3 Higher-Order Functions Functions are first-class values often called first-class citizens. A function can be an argument to another function (downward-funarg) as well as be the result of another function (upward-funarg). An example for passing a function as an argument is the map function: map f [ ] = [ ] map f (x : xs) = ( f x) : map f xs (1.3) (1.4) It applies a function to all elements of a list. Hence, map (+ 1) [1; 2; 3] =) [2; 3; 4] and [”House”; ”Boat”] map (”my” ++)6 =) [”myHouse”; ”myBoat”]: 5Even in imperative languages! 1.2 Concepts 13 Thus, downward-funargs are a perfect means to achieve behavior parameteriza- tion. Let us look at a very basic function for an example of returning a function, namely function composition: compose f g = l x ! f (g x)7: From two functions f and g a new function is produced that applies g to its argu- ment and then applies f to the result of the former application. So, compose (+ 21) ((cid:3) 3) =) 7 42; since the result of 7 multiplied by 3 added to 21 yields 42. The compose function is an example for a functional, i.e., higher-order function, since it takes functions as arguments and produces a new function. Returning functions makes it easy to define new functions by a combination of existing functions. An interesting varia- tion of creating a new function by supplying less parameters than needed is called partial application and uses a curried8 function: Given several functions can be defined by supplying only one of two parameters: add x y = x + y inc = add 1 dec = add (−1) addTen = add 10 inc 98 =) 99 dec 3 =) 2 addTen 1 =) 11: In fact, currying is possible with runtime values. We may create a function addX by supplying add with a number from user input. The function inc actually represents a closure. It represents function add but carries the value of x with it. Another typical way to create a closure is by capturing the value of a free (or global) variable as in let y = 13 in l x ! add x y: Here we created a function that will add its argument (x) to 13. Variable y is said to be a free variable in the scope of the l abstraction, as its value is determined by the function’s environment rather than by a function parameter (Functions with no free variables are also called combinators). Closures are truly functions created at 6(+1) and (”my” ++) (append) are called sections and denote the partial application of “+” to 1 and append to ”my” respectively. 7l builds a function abstraction and binds a variable (in this case x) to the value that will be received by a later function application. 8Named after the logician Haskell B. Curry, who used the notation f x y to denote ( f x) y, previ- ously written as f (x; y). 14 1 Functionalprogramming runtime which is also documented by the fact that they cannot be optimized by partial evaluation [Davies & Pfenning96]. The essence of the above is that higher-order functions provide more possibil- ities than functions normally do in imperative languages. In C, for instance, one can pass and return function pointers, but without a means to capture environment values. PASCAL allows closures as downward-funargs but (for reasons of language implementation) not as upward-funargs. With true first-class functions, however, the modularity of programs can be enhanced significantly [Hughes87]. 1.2.4 Lazy evaluation Most programming languages have strict semantics. We already got to know this type of evaluation strategy in section 1.2.2 on page 11 as applicative-order reduction and it is also known as call-by-value parameter passing. Yet another name for it is eager evaluation. Here is why: Given select x y = x we may obtain a reduction sequence like select 1 (2 + 3) =) select 1 5 =) 1: The intermediate result 5 was computed although it was subject to be thrown away immediately. Even worse, eagerness may not only cause unnecessary work to be Just imagine a done it can also prevent the computation of meaningful results. non-terminating expression or (1=0) in place of (2 + 3). The result would be non- termination or a division-by-zero error, though the result 1 would be perfectly rea- sonable. The solution is to use normal-order reduction, akin to call-by-name. With a non-strict select function the reduction sequence becomes select 1 (2 + 3) =) 1; which is both faster and safer. Indeed, almost any programming language contains at least one non-strict operator/statement in order to allow recursion or choice of side-effects. Typically, it is an if-statement that does not evaluate its branching ar- guments until one can be chosen. Nevertheless, we already have seen an example when lazy evaluation causes If you compare reduction sequence 1.2 on more work than eager-evaluation. page 11 using eager evaluation to reduction sequence 1.1 you will note that it needs one addition operation less. The problem was created by duplicating an expression (by unfolding exponentiation to multiplication) which also duplicated the compu- tation amount. Alas, all we need to do is to record the duplication and to share com- putation results. Speaking in calculi terms we replace string reduction by graph reduction. Then, reduction sequence 1.1, becomes 1.2 Concepts 15 (1 + 2)2 =) ((cid:15) (cid:3) (cid:15)) & . z }| { (1 + 2) =) ((cid:15) (cid:3) (cid:15)) =) 9 & . z}|{ 3 causing (1 + 2) to be evaluated only once. The combination of normal-order reduc- tion and sharing of expression results is known as call-by-need. With call-by-need we may not only save some computations we may even re- duce the computational complexity of algorithms. Let us obtain the minimum of a list by first sorting the list and then retrieving the first element: min xs = hd (cid:14) 9sort xs When we implement sorting with insertion-sort [Bird & Wadler88] the complexity of algorithm min with eager evaluation is O(n2), n being the number of elements in the input list. With lazy evaluation the complexity of min is reduced to O(n), since insertion-sort finds the first element of the sorted list in O(n) time and sorting of the rest of the list does not take place, as it is thrown away by the application of hd. In effect, what is thrown away is a function closure that could produce the rest of the list — item by item — if demanded. We can directly transfer the above observation for the implementation of Kruskal’s algorithm for minimal spanning trees. This algorithm uses a sorted list of edges to construct a minimal spanning tree of a graph [Aho & Ullmann92]. If the number of edges in the original graph is large compared to the number of edges needed to constitute the resulting spanning tree it pays off to use lazy sorting (e.g., lazy quicksort) that just sorts as much as is requested by the algorithm [Okasaki95a]. Besides efficiency improvements, lazy evaluation opens up the world of infinite data structures. A definition like ones = 1 : ones is not possible with a strict version of cons (:)10. There would be no reasonable use of ones, in that every function accessing it would not terminate. With a lazy cons, however, the above definition provides an infinite number of ones. Only as much ones as needed are produced, e.g., take 5 ones =) [1; 1; 1; 1; 1]: We can even perform infinite transformations as in twos = map (+1) ones ( =) [2; 2; 2; : : :]): A more useful definition is the list of all prime numbers: primes = sieve [2; : : :] sieve (p : xs) = p : sieve (filter ((6= 0) (cid:14) (mod p)) xs) (1.5) (1.6) 9Infix notation for the compose function of section 1.2.3 on page 12. 10This operator produces a list by appending the second argument to the first. 16 1 Functionalprogramming The algorithm uses the famous Sieve of Erathostenes11 and filters all non-prime numbers by testing an infinite amount of generated numbers ([2; : : :]) against all already found prime numbers. Most importantly, it relies on an infinite supply of numbers to test against and produces an inexhaustible list of prime numbers. There is no need to speculate about the highest number a client could possibly request for. Summarizing, we keep in mind that lazy evaluation avoids unnecessary non- termination or errors by creating a network of data dependencies and evaluating just that and nothing more. A considerable amount of computation can be spared this way to the amount of reducing the complexity of algorithms. Computation complexity, thus, may not depend on an algorithm’s nature, but on its usage, i.e., which data in what order is requested. Finally, lazy constructors allow for infinite data structures [Friedman & Wise76]. Lazy functions imply lazy data but there are languages like HOPE that provide lazy data only in order to avoid non-strict seman- tics for functions [Burstall et al.80]. The reason for this is to avoid closure creation for suspended function applications and to retain an applicative order reduction, e.g., for side-effects or easier debbuging. Anyway, lazy evaluation, similar to higher-order functions, can significantly enhance the modularity of programs [Hughes87], as they free data generators from knowing about the specifics of consumers. Termination control can be shifted to the consumer as opposed to a conventional mix of generation and control. 1.2.5 Pattern Matching Pattern matching is a typical example for syntactic sugar. Instead of testing for argument values within the function body, e.g., fac n = if n == 0 then 1 else n (cid:3) fac (n − 1) or fac n = case n of n == 0 ! 1 n > 0 ! n (cid:3) fac (n − 1) we may write: fac 0 = 1 fac (n + 1) = (n + 1) (cid:3) fac n: Along with the idea of using equations as part of the syntax, pattern matching adds much to the declarative nature of functional programs. All case discriminations are clearly separated from each other and the implicit assembly of partial function def- initions is very appealing. Note, however, besides a possibly psychological factor there is no real software engineering significance associated with pattern matching. The expressive power of a language [Felleisen91] is not augmented with pattern matching in any way. 11Eratosthenes was a Alexandrian Greek philosopher in the third century B.C. 1.3 Review 1.2.6 Type inference 17 Among the many innovations of ML [Milner et al.90, Wikstr ¨om87] was a type sys- tem that liberated a programmer from explicitly declaring the types of expressions. For instance, the type system would automatically infer the type of function map (see definition 1.3 on page 12) to be map :: (a ! b) ! [a] ! [b]; (1.7) that is, a function from type a to b and a list of elements of type a is used to produce a list with elements of type b. This is an example of parametric polymorphism, that is, a single function definition works in the same way for many types. A strongly and statically typed language with type inference ensures the ab- sence of any runtime type errors without putting any declaration burden on the programmer. For each expression the most general (so-called principle) type is computed. With explicit type declarations a programmer might over-specify types and thereby unintentionally narrow the use of a function. For instance, someone programming functions on integer lists may type map as map :: (Int ! Int) ! [Int] ! [Int]; thus, excluding many other possible applications of map. There is a different view on this matter that argues that type declarations are not just a service to compilers but constitute a part of the programs documenta- tion and even constitutes an albeit weak specification contribution. The nice thing about type inference is that it allows a mixed style, i.e., explicit type declarations overrule computed types and affect the type inference of associated expressions. In effect, in the presence of type declarations the type system performs type checking, comparing the inferred with the declared type. Like pattern matching there is no real impact of type inference in comparison with type checking on software designs. Notwithstanding, the impact of the read- ability of programs should not be underestimated. An example how type declara- tions can get in the way of an otherwise clean syntax is LEDA [Budd95]. 1.3 Review The following two sections give a subjective assessment of functional programming from a software engineering point of view. 1.3.1 Pro Pointers are like jumps, leading wildly from one part of the data structure to another. Their introduction into high-level languages has been a step backwards from which we may never recover. – C.A.R. Hoare 18 1 Functionalprogramming We already highlighted functional concepts (e.g., higher-order functions and lazy evaluation) and their ability to improve software properties (e.g., modularity of programs) in the preceding sections. Now, we turn to the foundations of func- tional programming and its positive implications. 1.3.1.1 Programming Discipline We can think of functions as a disciplined use of gotos [Dijkstra76] and parameter binding as a disciplined use of assignment. In this light, the absence of imperative control structures and side-effects is not regarded as inhibiting expressiveness but as a discipline for good programming [Hudak89]. Referential Transparency Representing computations in terms of expressions promotes the specification of problems and establishes data dependencies rather than giving an over-specified algorithm and execution order. That is why func- tional languages are considered to be declarative. Maintaining programs is facil- itated by the fact that equals may be replaced by equals, i.e., equivalent expres- sions can be interchanged without regard for introducing interferences through side-effects. Furthermore, the absence of side-effects makes programs much more amenable to parallelization. With regard to the multi-processor and massive parallel architec- ture designs trend in hardware there is a great demand for languages with a high potential for parallel execution. Finally, correctness proofs are much easier in a language with reduction seman- tics. Imperative languages require an additional calculus such as Dijkstra’s pred- icate calculus or Hoare’s weakest preconditions. Program transformations can be proven to be correct almost as easily as manipulating mathematical formulas. 1.3.1.2 Concise programs Functional programs are typically shorter than their imperative counterparts; at least given an appropriate problem domain which is amenable to a mathematical description [Harrison et al.94]. Short Syntax The most important operation in functional programming — func- tion application — is denoted as juxtaposition, that is, it does not need any syn- tactical elements! This economy in syntax pays back in short programs. Also, the frequent use of manifest constants reduces the amount for preliminary initializa- tions. In take 5 (filter even [1::]) (1.8) we need five characters to denote an infinite list of integers. The creation of a new function by composing two existing functions (filter and even) solely requires a space. There is even no need for bracketing, since function application associates 1.3 Review 19 to the left. For the very same reason, we actually need to bracket the list argu- ment for take, i.e., this time we need a space and parentheses in order to express the sequencing of functions12. The fact that the above short program involves the concepts of (cid:15) infinite data structures, (cid:15) lazy evaluation (by list construction, filter and take), (cid:15) higher-order functions (passing the predicate even), and (cid:15) type-safe instantiation of polymorphic functions (especially specialization of take and filter to integers) clearly witnesses the expressiveness of functional programming syntax. Another useful syntactical shortcut, inspired by mathematical notations to de- fine the contents of sets, are list comprehensions: [x (cid:3) y j x [6; 9::]; y [1::9]; odd y] denotes a list of all products between two lists ranging from one to nine (but odd numbers only) and from six to whatever number is reached by picking numbers in steps of three. Note that due to the nesting semantics of ‘,’ in list comprehensions the first list produces unique numbers only while the second recycles again and again. Compositionality As there is just one world of expressions in functional pro- gramming rather than two worlds of expressions and statements like in imperative programming [Backus78] the composition of program parts is especially easy. The result of calling filter in program 1.8 on the facing page is simply passed on to take without a need for a communication device. Compare the program fragments in figure 1.2. The functional version much more clearly expresses the intent of the l1.add(’a’) l2.add(’b’) l1.concat(l2); Result:=l1; versus (0a0 : l1) ++ (0b0 : l2) Figure 1.2: Comparison between imperative and functional style program, does not need to pass the result via l1, and leaves l1 and l2 intact for other uses. During the examination of program 1.8 on the facing page we have already seen that not only values but also functions may be “glued” together13. A nice 12Another way for writing the same function is (take 5) (cid:14) (filter even) [1::]. 13By the simple fact that functions are values too, i.e., first-class expressions. 20 1 Functionalprogramming demonstration of composing programs (parsers) with higher-order functions is given in [Hutton92]. Yet another type of gluing is enabled by the use of lists as a paradigmatic data structure. Many types of data structures in functional programming are modeled with lists, e.g., sequences, sets, flattened trees, etc14. An instructive example is the modeling of a chessboard by a list of numbers in the eight queens problem: Find a configuration of eight queens on a chessboard such that no queen threatens an- other [Bird & Wadler88]. The full program incrementally produces solutions with increasing board sizes [Wadler85], but we only look at the function that checks whether a new queen can safely be put on column n given a board configuration p (list of taken columns). safe p n = and [not (check (i; j)(m; n)) j (i; j) zip ([1::#p]; p)] (1.9) where m = #p + 1 For the reason that only column positions are stored, whereas rows are implicitly encoded in list positions, safe has to construct one position for the new queen (col- umn n & row m) and reconstruct one position tuple for all old queens respectively. The latter part is accomplished by (zip) that joins the column positions with a gener- ated list of row positions. All thus reconstructed position tuples are checked against the new position, yielding a list of safeness indicators. This list of booleans is then reduced to a single boolean result by and. The use of zip and and was possible only by using a list to model column positions and using a list of safeness indicators. Many standard functions take and produce lists and, therefore, can be often reused for purposes as above. The list effectively serves as a lingua franca between functions. As a final example for the elegance of functional programming have a look at the definition of the famous quicksort algorithm. quicksort [ ] = [ ] quicksort (x : xs) = quicksort (filter (< x) xs) ++ [x] ++ quicksort (filter (>= x) xs) The idea of the algorithm (to conquer by dividing the problem into the concatena- tion of two lists and a pivot element) almost springs to the eye of the reader. A typ- ical imperative solution (using nested while-statements to pre-sort the list in-place) almost gives no clue about the design idea. Assuming that concise programs are faster to write the compactness of func- tional programs translates to increased programmer productivity [Ebert87]. Despite their shortness functional programs are often said to execute ineffi- ciently. However, experiments with a heavily optimizing compiler for the strict 14Lists are used as the single and universal data structure in John Backus’ FP language [Backus78]. Many functions are just there to navigate values to their right position in lists. 1.3 Review 21 functional language SISAL show that functional programs can be faster than FOR- TRAN [Cann92] and programs that require complicated and expensive storage man- agement in C may run faster in a ML implementation with a good garbage collec- tor [Clinger & Hansen92]. 1.3.2 Contra Purely applicative languages are poorly applicable. – Alan J. Perlis The more light there is, the more shadow one might expect. Indeed, a part of the same aspects that we mentioned in favor of functional programming can be turned into counterarguments from a different perspective. 1.3.2.1 Functional decomposition Functional decomposition works fine for the tools and materials metaphor when programming in the small. Also, a decomposition based on actions and trans- formations can often be more intuitive than some entity-relationship organiza- tion [Moynihan94]. On the other hand, it is difficult to assign a “main function” to any large system and any choice is likely to be invalidated some time. A func- tional decomposition is inherently instable, since the topmost functions are not grounded in the problem domain and are subject to change whenever new re- quirements occur. Additionally, often systems are expected to cope with new data which demands to accommodate all affected functions. Precisely these reasons led to the redemption of structured analysis and design by object-oriented methodolo- gies [Meyer88]. 1.3.2.2 Reduction semantics The very same feature — renunciation of state — that gives functional program- ming its strength (absence of side effects) is responsible for its main weakness (lack of updates). “How can someone program in a language that does not have a notion of state? The answer, of course, is that we cannot. . . [Hudak89].” – Paul Hudak Of course, Hudak goes on explaining that functional languages treat state explicitly rather than implicitly. Passing around state for clarity and modeling references by indirection is fine but some serious problems remain. Essential State Felder sind physikalische Zust¨ande des Raumes. – Albert Einstein 22 1 Functionalprogramming We perceive the real world as consisting of objects which change over time. Real objects have identity and state. There is a great impedance mismatch between re- ality and software solution if we have to, say, deal with copies of a pressure tank every time pressure or temperature changes. Modeling a bank account with fair ac- cess to multiple users is a problem in functional languages [Abelson & Sussman87]. In fact, the problems to model state with a feedback model [Abelson & Sussman87] (see also figure 1.4 on page 28) and the prospect to incorporate functional pro- gramming into an object-oriented approach with a functional design pattern sys- tem caused the MUSE project to cancel the use of the functional programming language SAMPl E [Henhapl et al.91, Deegener et al.94, J¨ager et al.88, K ¨uhnapfel93, K ¨uhnapfel & Große94]. Something fundamental and innocent looking as simple I/O has been a hard problem for functional languages. Many models, such lazy streams, continuation semantics, and systems model [Hudak & Sundaresh88, Gordon93b, Gordon93a] have been proposed to overcome the discrepancy between a referential trans- parent language and I/O side effects. Hudak gives examples of the above I/O styles [Hudak89] and although imperative syntax is mimicked they look very un-intuitive and cumbersome to someone used to plain I/O statements. Recently, a step forward has been made by employing so-called monads for I/O [Jones & Wadler93, Carlsson & Hallgren93]. This model has also been adopted for the latest revision for HASKELL. It remains a matter of taste whether such a treatment of I/O state is conceived as natural or unacceptable. A related area, also suffering from lack of updates, are cyclic data structures (e.g., cyclic graphs). While these are easily and elegantly expressible in functional programming updating them is very inefficient, since there is no way around re- building the whole cycle15. One circumvention is to emulate pointers with mutable arrays (e.g., in HASKELL) [van Yzendoor95], but then we start to experience a mis- match between paradigm (imperative) and language (functional) used. Especially reactive systems, furthermore, demand a notion of event. An image recognizing robot must stop improving its assumptions when some movement is absolutely necessary. Functional languages cannot express such time-dependent planning, since an event interrupt implies execution order, which has no meaning in a functional program [Meunier95a]. Howbeit the tasteful use of a goto may improve a program [Knuth74] it is not difficult to do without goto altogether. To renounce implicit state, how- ever, appears much harder. Although, monads may emulate backtracking, non- determinism, exceptions, and state, in sum — “. . . it is true that programming with updates is a proven technology, and programming entirely without them is still ‘research’ [Appel93]16.” – Andrew W. Appel Monads especially do not seem to allow decoupling and structuring state in a sys- tem. 15Unless the whole data structure is single-threaded which can be difficult to prove. 16Appel already knew about Monads and cites [Wadler92]. 1.3 Review 23 Desirable State Arrays are often used due to efficiency considerations. Yet, func- tional programming languages must conceptually copy whole arrays for single up- dates. Only if the compiler can detect a single threaded array use it can use efficient destructive updates [Wadler90, Odersky91, Jones & Wadler93]. Monadic arrays are guaranteed to be single threaded, though. It is very convenient to side-effect a result cache, e.g., to reduce the runtime complexity of fib 1 = 0 fib 2 = 1 fib n = fib (n − 1) + fib (n − 2) from O(en) down to O(n). There are purely applicative version of memoiza- tion [Keller & Sleep86], but to overcome limitations like expensive quality checks on lists more advanced approaches are necessary. Hughes’ so-called lazy memo- functions are not syntactic sugar anymore, since an identity predicate — normally not provided by a functional language — is required [Hughes85]. “The interesting thing about memoization in general is that it begins to touch on some of the limitations of functional languages — in particular, the inability to side effect global objects such as caches — and solutions such as lazy memo-functions represent useful compromises. It remains to be seen whether more general solutions can be found that eliminate the need for these special- purpose features [Hudak89].” – Paul Hudak There are more function definitions like the fibonacci function definition above that look beautiful but are hopelessly inefficient. Often the culprit is the ++ operator which takes time linear to the size of its left argument. So, instead of reverse [ ] = [ ] reverse (x : xs) = (reverse xs) ++ [x] should apply one Bird & Wadler88] and use the accumulator technique [Burstall & Darlington77, reverse = rev [ ] rev acc [ ] = acc rev acc (x : xs) = rev (x : acc) xs: This version of reverse is O(n) instead of O(n2) but undoubtedly lost clarity as well. For the very same reason the nice formulation of displaying a tree showTree (Leaf x) = show x showTree (Branch lr) = 00 <00 ++ showTree l ++ 00j00 ++ showTree r ++ 00 >00 should be replaced by a less nice version using shows [Hudak & Fasel92]. 24 1 Functionalprogramming Transformations aimed at efficiency like the accumulator technique or tail- recursive functions often destroy the clarity and beauty of initial solutions17: One cannot really speak of declarative programming, concise programs, and mathe- matical specifications in view of the tweaked definition of a function to produce permutations: perms [ ] tail res = tail ++ res perms (x : xr) tail res = cycle [ ] x xr tail res cycle left mid [ ] tail res = perms left (mid : tail) res cycle left mid right@(r : rr) tail res = cycle (mid : left) r rr tail (perms (left ++ right) mid : tail res ) fastperms xs = perms xs [ ] [ ] “Functional languages etc. do of course abstract away from more of the machine-level details than do imperative ones, but real programs in any lan- guage contain a great deal of ‘how’. The nearest thing I know to a declarative language in this sense is Prolog, as described in lecture 1. Unfortunately, lec- tures 2. . . n follow :-) [Sargeant96a].” – John Sargeant So, unfortunately a lot of the over-specification of “how” to do things that could be left out in nice formulations must be given later in case more efficient version are needed. Note that the definition of quicksort (see definition 1.10 on page 20) compares each element twice for creating two new input lists. A more efficient version using an auxiliary split function already appears less clear. Beyond this, the combination of result lists with append (++) is very inefficient again in time (adding an O(n) factor) and cannot compete with the in-place sort of the imperative version (zero space overhead). Beyond aesthetical considerations there appears to be a class of algorithms that are inherently imperative causing one to end up asymptotically worse without im- perative features [Ponder88, Hunt & Szymanski77]. Just to achieve O(1) complexity for insertion and deletion of elements in list — a triviality in imperative programming — some tricky machinery has to be employed in functional programming [Okasaki95c]. Also, some algorithms have a very intuitive imperative form. In John Horton Conway’s Game of Life cells need to count the number of inhabited neighbor cells. One approach is to iterate all cells and determine their neighbor count by testing all adjacent cells (see functional version on the left of figure 1.3 on the next page, needing eight tests). 17Evil imperative programmers might remark that they do not care whether their programs are amenable to transformations, since they get acceptable efficency at once and functional programs need to be transformable because of the poor performance of initial versions. 1.3 Review 25 Alternatively, one may list inhabited cells only and in- crease (by a side-effect) the neighbor count of all adjacent cells (see imperative version on the right of figure 1.3, needing just three18 updates). ? ! Unavoidable State Again, it is not possible to circumvent state. Ordering of actions in imperative solutions translates to ordering of data dependen- cies or nesting in functional Consider Joseph solutions. Weizenbaum’s Eliza program. One part of it needs to replace words in order to use User input for Eliza’s questions. Given the input list inps and the replacement list reps — Figure 1.3: Functional and imperative LIFE inps = [”i”; ”am”; ”i”; ”says”; ”Eliza”] reps = [(”you”; ”i”); (”i”; ”you”); (”am”; ”are”)] — you are invited to convince yourself which of the following two expressions (substitute1 or substitute2) does the correct substitutions: replace org (from; to) xs = if org == from then to else xs 0 (from; to) org = if org == from then to else org replace substitute1 = map (l w ! foldr (replace w) w reps) inps 0 substitute2 = foldr (l p ! map (replace p)) inps reps: The above problem is akin to the considerations to be made when you have to determine the order of list generators in a list comprehension. Unless the correct nesting is specified the result will not be as desired. While one of the above alternatives is wrong there is another example of two correct but nevertheless different solutions: To calculate all possible chessboard configurations of eight queens that do not threaten each other (see the correspond- ing ‘safe’ function in definition 1.9 on page 20) both programs queens and sneeuq, with the relation queens m = map reverse (sneeuq m); need the same time. Notwithstanding, sneeuq is ten times slower in producing just one solution [Bird & Wadler88]. This effect is due to the order relevance of steps 18In general, the number of neighbors, which is usually low. 26 1 Functionalprogramming that produce potential solutions to be tested. An unfavorable order causes much more invalid configurations to be tested19. Another example how ordering corresponds to nesting are monads used in con- junction with folds [Meijer & Jeuring95]. The functions foldr and foldl replace list constructors with functions. When an associative function (together with an iden- tity element) is used the choice of foldl and foldr does not affect the result20 but is made according to runtime characteristics. However, when the function is, say, a state monad the result will be affected, akin to an imperative left or right traversal of a sequence with side-effects. Finally, if one needs to model references, e.g., to represent sharing of nodes in a graph, of course aliasing may occur just as in imperative languages. In this case, the danger is much more concentrated and explicit in a functional program, but nevertheless, unavoidable. In conclusion, treating state explicitly does not make reasoning about state eas- ier. It only may make reasoning about programs easier, which — though of course desirable in itself — is not the same as the former. 1.3.2.3 Deceptive Clearness Functional programs often look very nice but a deeper inspection frequently re- veals unexpected behavior. Inefficient lists Given the often weak performance of algorithms using lists it appears unfortunate that much reuse is gained by representing data structures with lists and using standard list functions for processing and interconnection (see paragraph Compositionality in section 1.3.1 on page 17). Moreover, often abstract datatypes are more appropriate for encapsulating data and providing natural op- erations. For instance, a representation of a list of columns for a chessboard con- figuration (see definition 1.9 on page 20) is fine but should be encapsulated by an abstract data type. Machine lurks below Albeit transformations such as (a+b)+c =) a+(b+c) are possible and valid one should not be surprised if floating point results are affected in case machine arithmetic rounding effects come into place. Also, the illusion of a mathematical world is destroyed when recursion does not terminate and, thus, causes the control stack to overflow. In fact, the problems aligned with uncontrolled user defined recursion (unproved termination, necessity for inductive proofs, un- known time and space complexity, hindrance of automatic optimization) have been a motivation for the so-called Squiggol school to allow a fixed set of recursive com- 19The author once wrote a backtracking program to generate a solution for the well-known soli- taire game. A different ordering of north, east, south, and west moves caused a one megahertz 6502 processor to conclude in 30 seconds while an eight megahertz 68000 processor calculated for 2 days. 20First duality theorem of folds [Bird & Wadler88]. 1.3 Review 27 binators only [Bird & de Moor92, Meijer et al.91]. This research direction towards a more algebraic notion of functional programming demonstrates that 1. reasoning about general recursion is not easy and 2. there is a need to optimize the time and space efficiency of functional pro- grams. Pattern Matching Often, nice definitions as pred 0 = 0 pred (n + 1) = n are shown but one should not be mislead to believe that the automatic inverse calculation of operands is possible in general. The n + k pattern is just a special case and often one will need to decompose arguments with functions as opposed to patterns. In lazy languages subtle effects may occur: behaves differently than f 1 1 = 1 f 2 = 2 g 1 1 = 1 g 2 = 2 because f 2 ? 21 yields 2 but g ? 2 yields ? [Hudak89], i.e., contrary to intuition pattern matching depends on the ordering of arguments. The same applies to the ordering of patterns which is crucial for the correct definition of the function. By exchanging the first two patterns of take take 0 = [ ] take [ ] = [ ] take (n + 1) (x : xs) = x : take n xs one can tune a preference for possibly getting a result though the numeric argu- ment (or alternatively the list argument) is not available (i.e., ?). Other subtle ef- fects of pattern and argument ordering caused by pattern matching are discussed in [Hudak89]. HASKELL uses a top-to-bottom left-to-right ordering of patterns and arguments. Note that this imposes restrictions for compilers. They are no longer free to evaluate arguments in any order. 21“Bottom” is used to denote “error” or non-termination. 28 1 Functionalprogramming Laziness meets pattern matching also in case of circular dependencies: HASKELL’s I/O model was based on a feedback loop defined by reqs = client init resps resps = server reqs The associated are [Hudak & Fasel92]: function definitions reqs resps server client init Figure 1.4: I/O feedback model client init (resp : resps) = init : client (next resp) resps server (req : reqs) = process req : server reqs: Before client has issued a request it already “pattern matches” for a response. Pat- tern matching causes evaluation and subsequently a deadlock. Note that the more conventional version without pattern matching client init resps = init : client (next (head resps)) (tail resps) would work! For these and other cases HASKELL provides a lazy pattern matching operator. Sometimes pattern matching works but causes unexpected inefficiencies: The definition of merge sort merge [ ] ys = [ ] merge xs [ ] = [ ] merge (x : xs) (y : ys) = x : merge xs (y : ys); x <= y y : merge (x : xs) ys; x > y splits the heads from input lists only to reassemble them in two branches respec- tively [Buckley95]. HASKELL allows naming patterns, e.g., merge xs0@(x : xs) ys0@(y : ys) in order to use xs0 for a recursive call for merge, thereby adding another special option to pattern matching. This option introduces a further subtlety: Actually, the type (given in parentheses) of data Sum a b = L a j R b copyr (R b) = R b (:: Sum a b ! Sum a c) is different to copyr r@(R b) = r (:: Sum a b ! Sum a b); since the former definition amounts to a reconstruction [Okasaki95b]. An interesting collection of typical problems students encounter when learning functional programming is given in [Clack & Myers95]. 29 2 Object-orientation Computing is viewed as an intrinsic capability of objects that can be uniformly invoked by sending messages. – Adele Goldberg his chapter introduces object-orientation and defines some terms that are used in later discussions. I explain the underlying worldview (sec- tion 2.1) and the most important concepts (section 2.2 on page 31). Fi- T nally, section 2.3 on page 38 enumerates pros and cons of object-orientation. In this dissertation I restrict myself to class based languages like C++, EIFFEL, and SMALLTALK. There is another interesting class of so-called delegation based or prototyping languages like SELF which use a dynamic type of inheritance and renounce classes. However, classless languages are not in widespread use and one of my aims is to improve the practice in object-oriented design. 2.1 Worldview The object-oriented paradigm suggests to decompose systems in autonomous ob- jects. Autonomous means an object 1. represents a complete entity. It is not just material or tools but both at once. 2. has a self-supporting state. It manages and keeps its state without needing support. 3. provides self-sustained operations. In case it refers to other objects it does not matter to clients. In terms of real world modeling objects represent real world objects and capture their identity, state, and behavior. In terms of software decomposition each object can be regarded as a little computer inside the computer. “For the first time I thought of the whole as the entire computer and wondered why anyone would want to divide it up into weaker things called data structures and procedures. Why not divide it up into little comput- ers. . . ? [Kay96]” – Alan Kay 30 2 Object-orientation So, object-oriented systems have a less hierarchical but a more collaborative nature. Even when reducing complexity by decomposition it is not done at the expense of the power of the parts. “The basic principle of recursive design is to make the parts have the same – Bob Barton power as the whole.” In a purely object-oriented languages there are no free functions that are defined on objects. Every function must be part of a data abstraction. Hence, an object- oriented systems bears no resemblance to an Input/Processing/Output model but constitutes a network of interactive events and responses. Objects can be thought of as abstract datatypes. This correspondence is fine with regard to real world modeling but from a software engineering perspective we have to take a closer look. Abstract datatypes define functions on constructors, i.e., provide data abstraction. Objects, distribute operations to constructors, i.e., provide procedural abstraction [Cook90]. An object operation implicitly knows the constructor it operates on, since it is tied to its definition. A function on an abstract data type has to distinguish between constructors first. That is why, it is hard to introduce new constructors to abstract datatypes (all functions must be extended) and easy to just add a new object. Conversely, it is hard to add a function to objects (all objects must be changed) and easy just to define a new function for all constructors. Note, that most object-oriented languages allow using classes for both abstract datatypes (hide representation of implementation) and procedural abstraction (dis- tribute constructors to subclasses and bind dynamically). In any case, objects are accessible only via the operations they provide. These are invoked by so-called message sends (or method calls). Object-orientation not only decomposes complex systems into smaller parts cor- responding to real world entities, but also captures these entities in a taxonomy of inheritance relationships. Inheritance may denote: ANIMAL MAMMAL BIRD WHALE PIG VULTURE Figure 2.1: Object-oriented taxonomy 2.2 Concepts 31 Is-a The heir is of the kind of its ancestor (also referred to as specialization inher- This relationship classifies entities, similar to the classification of itance). animals in zoology1 (e.g., Whale is a Mammal). Subtype The heir can be used whenever the use of its ancestor is appropriate Heirs conform in interface and behavior and provide monotonic extensions only. This is also known as the Liskov Substitution principle [Liskov & Wing93] (e.g., a Vulture can be used whenever a Bird is expected). Code reuse Heirs inherit code from their ancestors (also known as subclassing). Either through directly re-exporting inherited features or by their use for the definition of new features heirs may exploit ancestor code (e.g., Pig may reuse code templates of Mammal). Ideally, these three types of inheritance coincide (see fig- ure 2.2), but normally they are in conflict with each other. For instance, a IntegerSet is-a Set but it is not a subtype, since clients may want to insert strings. Furthermore, Set may in- herit code from HashArray but neither is-a nor subtyping are accurate [LaLonde & Pugh91]. The object-oriented worldview can be said to rule the soft- ware engineering world. Since its birth through SIMULA in 1967 it became the programming language and methodology paradigm [Rumbaugh et al.97] of choice. There was a 60% in- crease in SMALLTALK uses in 1993–1994 [Shan95] and today it is even used in embedded systems like oscilloscopes [Thomas95]. VEHICLE CAR SPORTSCAR Figure 2.2: Ideal inheritance 2.2 Concepts The following sections are meant to briefly introduce concepts of object-oriented languages. For a more thorough discussion the reader is referred to [Wegner87, Wegner90, Beaudouin-Lafon94] for technical matters, to [Nelson91] for a clarifica- tion of terms, and to [Meyer88, Nierstrasz89, Jacobson et al.94, Booch94] for soft- ware engineering and system development relevance. Subsequently, I will use the syntax of the object-oriented language EIFFEL in order to depict object-oriented concepts. The syntax is very clean and intuitive, but you may want to refer to a definition [Meyer92], introduction [Racko94], textbook [Rist & Terwilliger95, Thomas & Weedon95], or application development books [Walden & Nerson95, J´ez´equel96]. EIFFEL’s garbage collection avoids dis- tractions from memory management and its static type system allows typing issues to be discussed. EIFFEL’s clear syntax, simple semantics, and support of correctness through the use of assertions made it the premier choice for an education language for many universities throughout the world [Meyer93]. 1With multiple inheritance one may even let whales inherit from mammals and fishes, thus, avoiding duplications caused by single inheritance. 32 2 Object-orientation 2.2.1 Object-oriented decomposition The idea to draw subsystem boundaries around data and associated functions is one of the most important aspect of object-orientation. Actually, it is a simple ap- plication of information hiding [Parnas72]. That way, the representation of data and the implementation of functions is hidden from other subsystems. Object- orientation supports this modularity by encapsulation (see section 2.2.2) and boosts it by allowing multiple instances of modules. The latter lifts a tool to control soft- ware complexity to a paradigm for real world modeling. Right in the spirit of SIMULA object-oriented systems simulate real world pro- cesses. Each participant in the real world can be represented by a software artifact In an object-oriented bureau we would not see functions like “file called object. content of incoming letter into drawer”, but objects like Letter (supporting retrieval of content) and Drawer (supporting addition of information). Clearly, objects offer services and do not prescribe the order of actions. The object-oriented secretary be- comes a conductor of object capabilities as opposed to a hierarchical manipulator of materials. As a result, the design is much more stable, because even if processes in the bureau are drastically changed the basic participants (objects) are likely to stay. This promises less software maintenance in case of change but also greater potential for reuse of objects in other domains (e.g., Letter will be useful in other domains as well). Again, the reason for this reuse potential is the absence of a rigid top-down conceived function call structure. The bottom-up provision of general services is more suited to be reused in altered conditions. The recursive decomposition of data works especially fine due to the concept of procedural abstraction (see section 2.2.5 on page 35). In the object-oriented bu- reau objects issue goals to each other. The secretary may ask a drawer to file an information. The drawer in turn forwards the goal to one of its sub-compartments and so on until the goal is achieved. The secretary does not need to know about the internal organization of drawers which also do not care what types of sub- compartments exist. Clients never care about the types of their servers. Conse- quently, servers may be exchanged without need to alter clients. This would not be the case if, e.g., a secretary took different actions depending on drawer types. 2.2.2 Encapsulation We already noted above that encapsulation supports the mutual protection of in- dependent software entities. This observation is important for programming in the large. For programming in the small it is important to recognize the support of encapsulation for data integrity. An object is responsible for its own initialization after creation. It is ought to establish a consistent state at this point. Later ma- nipulations to the object’s state may only occur through the associated procedures. These, however, are designed to keep the object’s state consistent as well. It is not possible to accidentally add an element to the bottom of a Stack because one has access to the stack’s List representation and choose an invalid operation. A client of the chessboard data structure from section 1.3.2 on page 21 may insert an integer 2.2 Concepts 33 out of range which does not correspond to a column. Encapsulation would prevent a chessboard to get in such an invalid state. For the reason that procedures and functions (i.e., methods) are defined on an encapsulated object it is reasonable to allow an implicit access to the object’s fea- tures (methods and attributes). Hence, object descriptions get shorter and the tight binding of features to an object is emphasized. One can think of methods to always have one2 (the object reference) implicit argument. The combination of encapsulation and procedural abstraction (see section 2.2.5 on page 35) allows for a nice treatment of non-free abstract data-types. For in- stance, a rational number object may keep its numerator and denominator always in normalized form and clients cannot be confused by comparing non-normalized instances with seemingly unequal normalized instances. Object-oriented languages differ in their encapsulation scope. A language with class encapsulation — such as C++ — allows methods to access internals of entities of the same class. A Stack class may access the representation of a stack argument in the implementation of a pushAllFromStack method. In a language with object encapsulation — such as EIFFEL — objects are protected against each other even if they are instances of the same class. The pushAllFromStack from above would have to use the public Stack interface (e.g., to pop all elements one by one) only. 2.2.3 Inheritance COMMON_BASE In comparison to languages like PASCAL or C, object-oriented languages empower the programmer much more, since it is not only possible to define new types but these can be made to appear just like built-in types3. Even beyond, types can be incrementally derived from other types by inheritance. An heir inherits both interface (method signatures) and code (attributes and method bodies) from its ancestor. A typical use of inheritance is to spe- cialize a type, e.g., derive a sports- car from a car. Other uses of inher- itance are discussed in section 2.3.2 Inheritance can be use on page 41. to classify both data (see figure 2.1 on page 30) and algorithms [Schmitz92]. Therefore, it can be used as an orga- nization principle for libraries. Different refinements yield variants s n o i s r e v d l e i y e m s e g n a h C VARIANT2 VARIANT1 VERSION i t n i Figure 2.3: Classes as versions and variants There is also an interesting corre- spondence between Inheritance and 2CLOS methods may break the encapsulation of more than one object. 3C++ and EIFFEL even allow user defined infix operators. 34 2 Object-orientation version/variant control4 (see figure 2.3 on the preceding page). When inheritance is used to factor out code, that is, a set of subclasses uses and shares superclass code, it establishes a system of variants. Changes that should affect all classes are made in the common superclass. Changes meant for individual variants only are made to subclasses only. A subclass can be regarded as being a variant to its siblings or to be a version of its ancestor. When object-oriented languages began to compete for market-shares their prominently advertised feature was inheritance. As a mechanism directly support- ing reuse it was promoted as a unique and most important feature. The presence or absence of inheritance was used to distinguish between truly object-oriented or just object-based languages [Wegner90]. After all, inheritance turned out to be of less significance. The author rates data centered design, encapsulation, and dynamic binding to be of much more value. In fact, until today inheritance is not fully understood yet (see critique in section 2.3.2 on page 41). My view is supported by the fact that SMALLTALK-72 did not even have inheritance [Kay96], though its paragon SIMULA67 did. 2.2.4 Subtyping scale FIGURE We already got subtyping to know as a special kind of inheritance in section 2.1 on page 29. Yet, Subtyping has less to do with incremental class definitions but rather denotes a particular form of polymorphism called inclusion poly- morphism [Cardelli & Wegner85]. Fig- uratively, one can assume the type Fig- ure (see figure 2.4) to include the type Circle, since a Circle will behave ex- actly as a Figure when viewed and manipulated through a Figure inter- face. For subtyping, or the Liskov Sub- stitution principle [Liskov & Wing93], to hold true it is important that not only argument types vary contravariantly and result types vary covariantly, but also the pre- and post-conditions must obey the same principle. That is, pre-conditions may be weakened and post-conditions can be be strengthened. Subtyping, hence, works especially well with a programming- by-contract model, which is supported by EIFFEL. Its language support for pre- and post-conditions promotes the verification of behavioral subtyping as well. Figure 2.4: Subtyping inheritance scale radius1 radius2 scale radius ELLIPSE CIRCLE Inclusion polymorphism is one of the distinguished features of object-oriented languages in comparison to functional languages. Functional languages, typically offer so-called parametric polymorphism only5. For instance, in the type of map 4Consult [Schroeder95] for terminology, history, and classification of version controlling. 5Of course, subtyping is desirable for functional languages too and its integration into functional 2.2 Concepts 35 (see typing judgment 1.7 on page 17) there is an implicit all-quantifier in front of the judgment. This type corresponds to unconstrained generic classes in object- oriented languages [Meyer92]. A variable of type Figure, however, is existentially quantified. The difference is that a parametric polymorphic function does the same for all instantiations of the type variable. Inclusion polymorphism just states that there is a concrete type which will behave individually. Subtyping is of special value for the user of a library. It guarantees the sub- stitutability of classes. A piece of code will work for all subtypes, if it works for the supertype. Library writers, on the other hand, often long for subclassing (for code reuse) and is-a relationships (for classification). For the reason of these diverging goals subtyping should not be tied to subclassing as it is the case in C++ or EIFFEL. Rare exceptions to this rule are POOL [America & v. Linden90] and SATHER [Murer et al.93a]. In sum, subtyping can be regarded as putting a discipline on inheritance (al- lowing conforming subclasses only) and polymorphism (excluding ad-hoc poly- morphism, which allows no assumption about behavioral commonalities). 2.2.5 Dynamic Binding Just as structure programming hid the functionality of goto behind if/else, while and do; OOP has hidden the functionality of indirect goto behind polymorphism. – Robert C. Martin Subtyping without existential qualification of type variables would be akin to HASKELL’s type classes. This is a fine mechanism to make ad-hoc polymorphism less ad-hoc [Wadler & Blott89]. Contrary to an occasional misconception [Berger91] type classes do not allow for true object-oriented programming. Most important for true object-oriented programming is the concept of dynamic binding. Subtyping just restricts dynamic binding “vertically” to the inheritance hierarchy. Now, a routine call to an object variable is not compiled as a standard subroutine call. The code to be executed is not determined before runtime. That is why, dynamic binding is often also called late binding. The code selection depends on the actual object contained in the variable at runtime. Consider the code figure : FIGURE; !CIRCLE!figure; figure.display; figure.typeMessage; Although figure is of type Figure it refers to an object of type Circle. In figure 2.5 on the next page the lookup of methods is depicted. All method searches start at Circle. As method display is defined by Circle it is used in spite the presence of a general display method in Figure. As innocent as this languages is a important research topic [L¨aufer96]. 36 2 Object-orientation FIGURE typeMessage printType display CIRCLE printType display print "I am a "; printType; print " object."; print "Circle"; "typeMessage" "display" Figure 2.5: Dynamic method lookup looks it is most important for the reuse promise of object-oriented programming, for it allows old code to use new code. For instance, a drawing editor may use an iterator to display all visible objects. If the editor uses the Figure interface to invoke display one can add new figure types without any need to change the iterator code. This inversion of control (don’t call us — we call you), or Hollywood-principle, is typically not possible in procedural languages like C6. Indeed, however, it lifts reuse from plain library reuse to the reuse of design, that is, prefabricated applica- tion skeletons (called frameworks). The prefabricated code can be tailored through the use of dynamic binding. For this to work properly it is most important that self calls (implicit calls to methods of the object itself) are also subject to dynamic binding. To see why, con- sider the typeMessage call in figure 2.5. First, it is redirected to Figure as there is no redefined version in Circle. The implementation of typeMessage prints a string tem- plate and calls printType to output the actual information. Now, instead of invoking the implementation in Figure this self-call is also late bound to the implementation in Circle. This leads to the desired result of printing “I am a Circle object”. Late bind- ing of self-calls, ergo, enable this factoring of code into code templates7 that can be altered and refined by new classes. Dynamic binding effectively defers behavior distinction from the main program to the datatypes. This opens up the possibility to build frameworks that do not 6One may exploit function pointers for this but it is not a common programming style. 7Method typeMessage is indeed part of the Template Method pattern [Gamma et al.94]. 2.2 Concepts 37 need to know about new types to come. Dynamic binding also makes it reasonable to operate with lists of heterogeneous types. Functional lists are homogenous, i.e., allow one element type only. One may use a sum type for homogenous lists but in order to differentiate actions according to individual element types type cases become necessary. Dynamic binding assigns the responsibility to differentiate to the elements and, therefore, avoids the type cases. Actually, the main “trick” used is the data centered approach, i.e., distributing operations to their data. Hence, case statements — otherwise contained in functions scattered around the whole program – are concentrated at data abstractions. Dynamic binding “merely” hides the indirection used to access the distributed operations. In this light it appears as language support for data centered programming which can be pursued also (by the albeit clumsy use of function pointers) in C. Note that functional languages also allow using old code with new code. For instance, map can be used with any new function with appropriate type. Notwith- standing, this type of parameterization does not amount to the same flexibility, since the variable parts (e.g., function to map) must be specified in advance. An object-oriented framework, however, can be adapted in many unforeseen ways, as it is possible to override any methods — not just those intended for parameteriza- tion. For instance, Circle may also redefine typeMessage to print “Circle, I am”. Since the latter form of reuse requires knowledge about the internals of the reused classes it is called “white-box reuse”. The corresponding name for com- posing opaque parts — just like higher-order functions — is “black-box reuse”. Akin to lazy evaluation (see section 1.2.4 on page 14) dynamic binding sup- ports modularization. Analog to the separation of data generation and control the separation between method selection and method invocation decouples the client from the server. The client does not need to prescribe the server’s action. It just declares a goal (through a message send) and the server is free to choose whatever appropriate action. Almost all object-oriented languages use single-dispatch, i.e., dynamic binding is used for the receiver of a message send only. If dynamic binding is extended to the arguments of the message send it is called multi-dispatch. The languages CECIL [Chambers92b] and CLOS [Bobrow et al.86a] hereby allow implementations to be selected with respect to all argument types. 2.2.6 Identity All objects are created unequal. – me The concept of object identity [Khoshafian & Copeland86] is easily overlooked due to the presence of more prominent concepts like inheritance, encapsulation, and polymorphism. Nevertheless it also significantly contributes to the expressive- ness of the object-oriented paradigm. What does object identity mean? One way to look at it is to observe that objects are mutable. When an object changes, e.g., an element is inserted into a list, there is no new list being created. The list remains the 38 2 Object-orientation same but has a different value. This property gives rise to easy real world model- ing but also may induce problems due to unwanted aliasing. Aliasing occurs when two or more variables refer to one object and some interference takes place, e.g., a client with access to an object is not aware of other accesses and is invalidated by external changes made to its referenced object. In contrast values (from functional programming) are immutable and lack iden- tity [MacLennan82, Eckert & Kempe94]. It does not make sense to make a differ- ence between two numbers of the same value or two lists with the same elements. This perspective, however, leads to the second way to look at identity: Consider personal data, such as name, age, city of birth, etc., held in records. Let us assume Ivan Smith exists twice with the same age. One Ivan is born in St. Petersburg (Rus- sia) and the other is born in St. Petersburg (Florida). Unless we include the state of birth the two Ivan’s have the same value, i.e., they are the same person in a func- tional language. Represented as objects, there is no problem at all. If one compares for data equality one will find out about the curiosity of the coincidental birth date. Yet, if one compares for identity it is clear that we have two distinct persons and not just one person that we somehow received twice. Objects may join their values but their identity remains [Harrison & Ossher93]. Summarizing, identity is very closely related to state, but one should emphasize its role in modeling real world objects with identity (A tank does not change only because its pressure does) and its role to distinguish between objects that happen to have the same value (Ivan is not Ivan). 2.3 Review The following two sections give a subjective assessment of object-oriented pro- gramming from a software engineering point of view. 2.3.1 Pro Why does the combination of the above presented concepts work well? This section examines the positive implications of the object-oriented paradigm. 2.3.1.1 Real world modeling Whereas Turing machine behavior can be expressed by mathematical models, the observable behavior of interaction machines corresponds to that of empirical systems in the natural sciences. – Peter Wegner Two properties allow for real world modeling without impedance mismatch8: 8When a modulated current flows through a conductor which is not properly terminated some energy will be lost due to reflections. Similarly, “energy” will be lost if the modeling domain cannot adequately express the things to be modeled. 2.3 Review 39 1. The emphasis on a collaborative set of self-sustained objects and 2. direct support for the notions of state and identity. Real world systems are often composed of a collaborative set of self-sustained ob- jects themselves and their structure provides a natural starting point for an object- oriented design. Maintaining the system, thus, will be possible without recon- structing too many transformation and abstraction steps. An insufficient under- standing of the system would lead to faulty changes or bug fixes. With regard to state support it is instructive to note that all of the arguments against reduction semantics (in section 1.3.2 on page 21) vanish. Furthermore, the usually as negative discredited implications of state support like references and aliasing are by no means just a crude copy of an underlying primitive von Neu- mann hardware. Consider an office software where you may embed pictures into word-processor texts. Often we want to share images between multiple texts. A correction to the image should be reflected in all uses of the image9. This is a truly useful use of aliasing. Also, we may have a link to our office software represented by an icon to click on in order to start the software. The link allows changing the location or version of the software without bothering the user with a new place to start from or a new icon to use. This is a truly useful use of referencing. Changes to documents are modifications to mutable objects with an identity. A model re- flecting this employs a truly useful use of state. Given these useful applications of identity and state it seems worthwhile to assign them direct language support. 2.3.1.2 Maintainability “Software systems are long-lived and must survive many modifications in order to prove useful over their intended life span. The primary linguis- tic mechanisms for managing complexity are modularity (separating a system into parts) and abstraction (hiding details that are only relevant to the internal structure of each part). A challenge for future language design is to support modularity and abstraction in a manner that allows incremental changes to be made as easily as possible. Object-oriented concepts have much to offer and are the topic of much on-going investigation [Hankin et al.97].” – Chris Hankin et al. Seamless development All phases of a software development (requirements analysis, analysis model, design, implementation) are based on the same founda- tion, hence, avoiding paradigm shifts inbetween. Phase transitions are considered seamless [Walden & Nerson95] and, ergo, backward or forward changes have less implications compared to a methodology involving paradigm shifts, i.e., redesigns. 9A individual change can be realized by first making a copy of the image. 40 2 Object-orientation Object-oriented decomposition Building up on a base of reusable self-sustained domain or business objects yields more stable systems. There is no main function that can be invalidated by a series of requirement changes. The introduction of new data — i.e., the “know how” of a system is applied to a new domain — is easy, for it amounts to a local addition of code only. Similarly to extensions, changes should occur more localized too. The distribution of functionality from a hierar- chy of functions to the former “dead data”, consequently, can be thought of as a complexity reducing mechanism. Class definitions create a small working space and make the first argument of their methods implicit. Changes, ergo, become a matter of altering a small encapsulated system fraction with short access to local data. Usually, there is no need to teach binding environments and techniques like display tables or static links anymore due to the simple structure of classes10. Modularity We already explained in the preceding concept sections how encap- sulation (section 2.2.2 on page 32) and procedural abstraction (section 2.2.5 on page 35 and section 2.2.1 on page 32) aid the modularity of programs. Clients do not care how servers fulfill their services and can rely on consistent and self- managing objects at any time. 2.3.1.3 Beyond the imperative Although the object-oriented paradigm is closely related to an imperative style of programming it has been argued that there are important differences [K ¨uhne96a]. Objects successfully hide the use of state and represent explicit substates to change and to handle, hence, escape the von Neumann bottleneck. The famous critique of John Backus [Backus78] towards imperative languages like FORTRAN and ALGOL must be re-evaluated with regard to the object-oriented paradigm. Let us recall the quotation of Alan Kay on page 29: He viewed objects as a decomposition of the whole computer with equal power. Now, the whole computer and its decom- position objects do not need to be von Neumann architectures. It is possible to use objects in a referential transparent way [Meyer94a] or as nodes in a dataflow graph. “What I got from Simula was that you could now replace bindings and assignment with goals. The last thing you wanted any programmer to do is mess with internal state even if presented figuratively. . . . It is unfortu- nate that much of what is called ‘object-oriented programming’ today is simply old style programming with fancier constructs. Many programs are loaded with ‘assignment-style’ operations now done by more expensive attached pro- cedures. [Kay96]” – Alan Kay In particular, the improved control abstraction facilities of object-oriented lan- guages compared to early imperative languages weaken Backus’ critique. We will return to the object-oriented possibilities to escape the limitations of plain impera- tive languages in section 4.2 on page 57. 10C++and JAVA allow nested classes though. 2.3 Review 2.3.2 Contra 41 Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. – Rich Cook 2.3.2.1 Imperative heritage Most object-oriented languages are based on ALGOL-like languages (C++, SIMULA, EIFFEL) and some on LISP (SMALLTALK, CLOS, DYLAN). All share an imperative base trough their ancestors. While we argued in section 2.3.1.2 on page 39 that object-oriented languages reach beyond their imperative roots, they still suffer from their imperative heritage. One example are system shut-downs due to method calls on void references. This is clearly an initialization problem which in general has already been criticized by John Backus [Backus78] and which we will tackle in chapter 11 on page 191. Aliasing Consider a program fragment using a complex number library: !!a.make(2,0); b:=a; c:=a+b; !!a.make(0,2); d:=a+b; We would expect d to have the value 2+2i but if complex numbers are implemented with reference semantics — which is quite likely to be the case due to efficiency considerations — the result will be 4i. The reason for this is the unexpected aliasing of b with a. Though object-oriented languages typically provide support for value semantics (e.g., expanded classes in EIFFEL, part-objects in BETA, non-reference variables in C++) there is no agreed system how to choose between reference and value semantics. Another source of aliasing are interferences between parameters and results. A method does not represent a good abstraction if the effects of o.m(x, y) differ to o.m(x, x). Also, aMatrix.multiply(aMatrix2) possibly yields the wrong result when aMatrix2 happens to reference aMatrix, since then result elements will override still to be used argument elements. 42 2 Object-orientation Broken encapsulation When object state is distributed over several sub-objects and these objects are accessible from outside the object the state-encapsulation of the object is broken. For instance, a careful bank customer, aiming at never having less than $2000 on his account, will be invalidated by his wife, if she has access to the account too [Duke95]. Similarly, a balanced tree can get unbalanced if one of its element is changed by a side-effect. A solution for these problems has been proposed by the use of so-called Islands [Hogg91]. 2.3.2.2 Classes for everything The class concept is used for representing modules, encapsulation borders, in- stance generation, code pools, types, visibility mechanism, and interfaces. Al- though the strive for unification, i.e., to capture many things with a single ab- straction, is desirable, the wealth of a class’ functions appears too much. There has been arguments to separate modules and classes [Szyperski92], code pools and types [Murer et al.93a], and encapsulation and classes [Limberghen & Mens94]. So- called Kinds have been proposed to aid classes with typing [Yu & Zhuang95] and “namespaces” are supposed to cluster C++ classes. Obviously, there will be more research necessary to remove the overloading of concepts on classes. 2.3.2.3 Inheritance for everything A modeling tool is more than a passive medium for recording our view of reality. It shapes that view, and limits our perceptions. If a mind is committed to a certain [tool], then it will perform amazing feats of distortion to see things structured that way, and it will simply be blind to the things which don’t fit that structure [Kent78]. – W. Kent Inheritance has been the “hammer” for many problems (be they “nails” or not) for both language designers and language users. Though inheritance is primarily an incremental modification operator on classes it fulfills many purposes: Language designers use inheritance for Subtyping An heir is implicitly assumed to be a subtype to its ancestor and, thus, is allowed to be substituted for it. Code reuse Code reuse happens through inheriting methods which continue to work in the heir but also by deliberately importing methods only meant to be used for the implementation of the heir. Interfaces So-called pure, abstract, or deferred classes only specify interfaces and do not contain any code. They are used to define an interface that implemen- tations may adhere to. 2.3 Review 43 Modules The collection of mathematical constants in the EIFFEL library is an ex- ample for solely using a class as a namespace and container of data without intending to use it as a instance generator. Mixins Languages with multiple inheritance suggest a LEGO approach to the com- position of code. Each superclass mixin provides a facet of a class like per- sistence, observeability, etc. This style is also referred to as facility inheri- tance [Meyer94b]. Language users exploit inheritance for Classification Libraries are sometimes organized as a taxonomy of concepts (see figure 2.1 on page 30). This helps to understand classes in terms of others. Also, algorithms may be subject to classification [Schmitz92]. Each branch in the inheritance hierarchy then represents a design decision. Specialization Heirs narrow the scope of ancestors, e.g., a Student is a special Per- son, typically by adding information (e.g., student identification). Special- ization may conform to subtyping (like in the SportsCar and Car example in figure 2.2 on page 31) but does not necessarily (a 3DPoint is not a subtype of Point due to the covariant equality method). Generalization Heirs cancel limitations of their ancestors. For instance an all pur- pose vehicle may be subclassed to obtain an all purpose vehicle that allows steering of the rear axle. This usage is typically due to unforeseen extensions and coincides with code reuse. Versioning As explained earlier (see figure 2.3 on page 33) it is possible to use heirs as versions of their ancestors. This allows extending data records in a system while reusing all of the control code unchanged. Parameterization Partially abstract classes are used as ancestors to concrete sub- classes that specify the missing code. This type of code parameterization has been described as the Template Method pattern [Gamma et al.94]. The problem with all these uses for inheritance is that always the same rules for visibility, substitutability, and namespaces are applied. While breaking en- capsulation is necessary for white-box reuse it leads to fragile base classes in gen- eral [Snyder86, K ¨uhne95b]. Also, (cid:15) inheritance used for code reuse or covariant specialization should not enable substitutability [LaLonde & Pugh91]. (cid:15) Inheritance used for parameterization should not break encapsula- tion [K ¨uhne95b]. (cid:15) Interface inheritance should not allow covariant redefinitions of argu- ments [Cook89b], etc. 44 2 Object-orientation Consult section 2.3.2.2 on page 42 for a list of suggestions to escape the “one-trick- pony” mentality towards inheritance in object-oriented languages. Especially multiple inheritance causes additional problems like aggravating en- capsulation, modularity and the open-closed principle [Carr´e & Geib90], which is why many languages do not provide it (e.g., SMALLTALK, BETA, JAVA). It can be argued about whether it is positive or negative that inheritance typ- ically is a static mechanism. There is no doubt, however, that inheritance should not be used when more dynamic solutions are appropriate. We will return to this subject in chapter 7 on page 93. 45 3 Calculus comparison s we are going to subsume functional concepts with the object-oriented paradigm in chapter 4 on page 55 and aim at capturing functional tech- niques with object-oriented design patterns in part II starting at page 85 A this chapter shall assure us about the validity of this approach. Of course, all functional and object-oriented languages are Turing complete with regard to their computation power. There is no doubt that any of these lan- guages can emulate one of the other. In spite of that it is quite interesting to ask whether one emulation is easier to accomplish than the other. If it is easier to cap- ture object-oriented programming with a functional language we should write pat- terns facilitating object-oriented programming in a functional language. The goal of this chapter is to prove the opposite. 3.1 Language comparisons Looking for the computational difference between two languages is like comparing apples to oranges only to find out that both are fruit. – me What is left if we beware of falling into to the Turing tarpit, i.e., try to find a difference in computational power that is simply not there? What other indica- tors for expressiveness are available? One interesting approach is to evaluate the amount of restructuring a program necessary to emulate the addition of new con- struct [Felleisen91]. If local changes are necessary only then the new construct can be judged to add no further expressiveness. Unfortunately, the theoretical frame- work used for this approach does work for conservative extensions to languages only, i.e., cannot be used to compare two fundamentally different languages. For the purpose of this chapter my idea was to define a translation of one lan- guage into the other and vice versa and then compare the translation complexities. The rational is that a language should emulate a less expressive language with ease while a considerable amount of machinery would be necessary the other way round. In order to compare the fundamental mechanisms instead of technicalities of specific languages it appeared most reasonable to compare calculi expressing the nature of the two paradigms respectively. The calculus representing functional pro- gramming is easily found and is referred to as the l -calculus. You might want to 46 3 Calculuscomparison refer to an introduction for its syntax and semantics [Barendregt84, Hankin94]. It is much less obvious to choose a calculus representing the object-oriented paradigm. Many approaches to model object-oriented principles with calculi use an extended version of the l -calculus or do contain first-order functions [Abadi94, Bruce94, Cardelli84, Cook89a, Fisher & Mitchel95, Pierce & Turner94, Reddy88]. A com- parison with such a calculus would be ridiculous because of the trivial emula- tion of the l -calculus within it. When I made the decision which calculus to choose there where two candidates that did not model objects as records in a functional language but attempted to model object-oriented languages from first principles. One was OPUS [Mens et al.94, Mens et al.95] and the other was the s - calculus [Abadi & Cardelli94]. In fact, the latter calculus was inappropriate too for it allowed to bind the self reference of an object to an arbitrary name and, thus, enabled to access variables of arbitrary nesting depth. In a comparison carried out by Dirk Thierbach this feature was found out to be very “functional” in na- ture [Thierbach96] and also would have prevented a fair comparison. Thierbach also identified several weaknesses of OPUS and developed an improvement called OPULUS. He proved OPULUS to have the Church-Rosser property and gave a com- parison to both OPUS and s -calculus [Thierbach96]. 3.2 Opulus OPULUS is a simple calculus that denotes (cid:15) encapsulation of multiple methods to an object by : : : : : : : : : : : : : : : [E], (cid:15) referencing “Self” as : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : $, (cid:15) sending a message N with Argument F to object E with : : : : : E N : F, (cid:15) referencing the parameter of a message send as : : : : : : : : : : : : : : : : : : : #, (cid:15) incremental modification (E is subclassed by F) by : : : : : : : : : : : E + F, (cid:15) declaration of attributes as : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : N = E, (cid:15) declaration of methods (with Name N and body E) as : : : : : : l N = E, (cid:15) the empty object by : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : , (cid:15) and finally, hiding of methods (N1–Nk) with : : : : : : : : : : : EfN1; : : : ; Nkg. Table 3.1: Opulus syntax Its grammar (with start-symbol “Expression”), therefore, is as given in table 3.2 on the facing page. For a more thorough definition please consult [Thierbach96]. For our purposes we will be satisfied with just one example showing how the calculus works. Con- 3.3 Opuluswithin l -calculus 47 Expression ::= ”[” Expression ”]” j Expression ”+” Expression j Expression ”f” Namelist ”g” j Expression Name ”:” Expression j Name ”=” Expression j ”l ” Name ”=” Expression j ”#” j ”$” j ” ”(” Expression ”)” ” j Namelist Name Character ::= Name ”,” Name ::= Character f Character g ::= ”a” j ”b” j . . . j ”z” Table 3.2: Opulus BNF sider an object POINT: POINT (cid:17) [x = 7 + y = 13 + l setx = [$ + x = #]]: It has two attributes x and y and one method setx that changes the value of x by producing a new object. The new object is the old object (referenced by $) modified by an attribute x that has the value of the setx parameter. The reduction occurs as follows: POINT setx : 42 ; 42) app(POINT; setx; !b (cid:17) app(l setx = [$ + x = #]; setx; x = 7 + y = 13 + l setx = ($ + x = #); 42) (cid:17) [x = 7 + y = 13 + l setx = ($ + x = #) + x = 42] !!h !h [x = 7 + (y = 13 + (l setx = ($ + x = #) + x = 42))] [y = 13 + (l setx = ($ + x = #) + x = 42))] The result is a new object with x possesing the value 42. It is one of the distin- guishing features of OPULUS to be able to get rid of the overriden “x = 7” attribute by h -reduction. 3.3 Opulus within l -calculus A standard translation of OPULUS into the l -calculus represents OPULUS terms in a tree structure and uses programmed reduction rules to manipulate the tree [Thierbach96]. The fixpoint combinator Y can then be employed to, e.g., re- peatedly apply b -reduction. A more efficient translation is achievable (cid:15) by recognizing that substitution in OPULUS (e.g., replacing # with the argu- ment value) can be captured with l -abstraction variable bindings, 48 3 Calculuscomparison (cid:15) using a continuation passing style for method calling that might “backtrack” to search in A after B in the case of (A + B) message : , (cid:15) and by directly translating OPULUS terms into structure evaluating func- tions instead of separating term representation and reduction rule applica- tion [Thierbach96]. The complete (fast) translation of OPULUS to the l -calculus is given in table 3.3 (parameter d refers to the self-reference, e to the value of an argument, n to the name of a method, and f to the continuation to be used in case a message must be redirected to the modified operand, e.g., A in A + B). (cid:28) $ (cid:29) (cid:28) # (cid:29) (cid:28) (cid:29) (cid:28) [A] (cid:29) (cid:17) self (cid:17) arg (cid:17) Y l y:l d e n f :y (cid:17) l d e n f :((l x:x x) (cid:28) A (cid:29)) e n f (cid:17) l d:(l x:x x)1 (cid:28) A (cid:29) (cid:28) A + B (cid:29) (cid:17) l d e n f : (cid:28) B (cid:29) d e n ((cid:28) A (cid:29) d e n f ) (cid:28) A M : B (cid:29) (cid:17) (cid:28) A (cid:29)(cid:28) (cid:29)(cid:28) B (cid:29)(cid:28) M (cid:29) (cid:28) l N = A (cid:29) (cid:17) l d e n f : (cid:28) N (cid:29) ((l self :l arg (cid:28) A (cid:29)) d e) f n (cid:28) N = A (cid:29) (cid:17) l d e n f : (cid:28) N (cid:29)(cid:28) A (cid:29) f n (cid:28) AfNg (cid:29) (cid:17) l d e n f : (cid:28) N (cid:29) f ((cid:28) A (cid:29) d e n f ) n Table 3.3: Translation of Opulus to l -calculus It should be noted that h -reductions are not considered anymore and that a translation for OPUS would not be possible in this manner. The translation appears straightforward (e.g., message send corresponds to function application) but we should pay attention to the fact that OPULUS names (such as method names) must be encoded in the l -calculus, since it does not sup- port a similar notion. Thierbach translated names to an encoding of numbers in the l -calculus, because all one really needs is to compare names for equality. One can, alternatively, also think of an extended l -calculus with d -reductions for numerals or names. In our case, however, comparing names adds a complexity factor of O(v), where v is the number of distinct names used in the OPULUS term. Figures 3.1 and 3.2 on the facing page show an OPULUS term and its mechani- cally2 derived translation to the l -calculus respectively. Both are screenshots taken of a system generated by CENTAUR [Despeyroux88, Jager et al.91, Centaur92] from specifications written by Dirk Thierbach [Thierbach96]. The M and N macros in figure 3.2 on the next page represent the encodings of OPULUS names. 1An object has itself as an implicit argument. 2Some minor modifications have been made by hand to enhance clarity. 3.4 l -calculuswithinOpulus 49 Figure 3.1: Screenshot: OPULUS expression Figure 3.2: Screenshot: OPULUS expression represented in the l -calculus 3.4 l -calculus within Opulus Again a standard translation is easily obtained by encoding l -terms into objects which understand application and substitution messages [Thierbach96]. The diffi- culties with a -conversions necessary for b -reductions are easily avoided by calcu- lating weak-head-normal-forms only [Field & Harrison88]. A cleverer translation is obtained by exploiting the closure like nature of objects. Closures are functions that carry their variable environment with them [Field & Harrison88]. How can we make use of closures then? It is easy to 50 3 Calculuscomparison translate the identity function (l x:x) to OPULUS as [l app = #]: Then the application to an argument [l app = #] app : yields the argument itself, i.e., . However, we need a more sophisticated approach in the case of l x:l y:x. When l y:x is applied to a value we need to remember the value of x. This is precisely the case for a closure which can remember earlier variable bindings. Hence, the result of applying l x: : : : must be a closure representing the function l y:x but remembering the value of x. A closure, howbeit, is most easily represented by an object storing the variable binding in its state, ergo [l app = [x = # + l app = ($ x : )]] represents the function l x:l y:x (the sub-term “$ x : ”retrieves the function body value from the variable environment). Note that if the lambda term had been l x:l y:y the variable access of the innermost function body translation would have been “#” instead of “$ x : ”. As a result, we need a context dependent translation of parameter access [Thierbach96]. In table 3.4 the parameter context is denoted with subscript indices. (cid:28) l X:B (cid:29) (cid:17) l app =(cid:28) B (cid:29)X (cid:28) l X:B (cid:29)Y (cid:17) [$ +Y = # + l app =(cid:28) B (cid:29)X ] (cid:28) L R (cid:29) (cid:17) (cid:28) L (cid:29) app :(cid:28) R (cid:29) (cid:28) L R (cid:29)Y (cid:17) (cid:28) L (cid:29)Y app :(cid:28) R (cid:29)Y (cid:28) X (cid:29)X (cid:17) # (cid:28) X (cid:29)Y (cid:17) ($ X : (cid:28) X (cid:29) (cid:17) ($ X : if X 6(cid:17) Y ); ) (3.1) (3.2) (3.3) (3.4) (3.5) (3.6) (3.7) Table 3.4: Translation of l -calculus to Opulus The subscript index is defined and respected by lambda abstraction (rules 3.1 and 3.2 of table 3.4) and distributed over application (rules 3.3 and 3.4). If the cur- rent binding variable coincides with the variable to be accessed then the parameter symbol is used (rule 3.5). Otherwise, the variable is accessed via the variable envi- ronment (rules 3.6 and 3.7). Interestingly, in contrast to the standard translation the fast translation above even allows to dispose the restriction of calculating the weak-head-normal-form only. Figure 3.3 on the next page shows a lambda expression in a CENTAUR win- dow. Figure 3.4 on the facing page shows the corresponding OPULUS term which was derived by automatic translation. 3.5 Conclusion 51 Figure 3.3: Screenshot: l -calculus expression Figure 3.4: Screenshot: l -calculus expression represented in OPULUS 3.5 Conclusion It did not come as a surprise that either translation was possible at all but how about the results of comparing the complexity of performing one calculus in the other? Table 3.5 on the next page summarizes the results for emulating OPULUS in the l -calculus and table 3.6 on the following page shows the results for emulating l - calculus in OPULUS. Ranges of complexity denote the best and worst case, whereas the average case is always closer to the lower bound. Let us first regard the standard translation which we did not explicate in the preceding sections but that we remember as using a straightforward term repre- sentation with associated reduction functions. We note that the derivation of emu- lating the l -calculus in OPULUS was done within two pages [Thierbach96] resulting in three conceptually easy object definitions all implementing two messages each (substitution and b -reduction). Deriving the opposite standard translation took more than three pages and resulted in nine translation rules needing nine construc- tors with nine associated selection functions. In addition, this does not include the emulation of OPULUS names in the l -calculus. The difference in verbosity of the two emulations can be explained by two rea- sons: 1. The l -calculus is somewhat weaker in its basic operations causing the need for a name comparison emulation and 52 3 Calculuscomparison 2. OPULUS is a somewhat bigger calculus with a larger grammar causing the need for more (nine compared to three) emulation rules. On the one hand the size of OPULUS may appear arbitrary given the current ma- turity of object-oriented calculi and one may easily excuse the l -calculus for its efforts to emulate such a “clumsy” formalism. On the other hand, OPULUS does a very good job of capturing the basic mechanisms of class based object-oriented languages. Capturing the others calculus’ richness is part of the game and the l - calculus has obviously a harder job. The first of the above argument also shows up in the complexity comparison: One full reduction step in OPULUS needs O(nv) steps in the l -calculus (see table 3.5), whereas v is the number of distinct names in the OPULUS term. Fast translation Standard translation Substitution Application Redex contraction Redex recognition O(1) O(nv) O(nv) O(1) O(n) O(nv) O(nv) O(1)–O(nv) O(nv)–O(n2v)† O(nv) Reduction-step † It easy to reduce this to O(nv) by combining the functions that search a redex and perform a reduction. Table 3.5: Complexity of Opulus in the l -calculus Fast translation Standard translation Substitution Redex contraction Redex recognition Reduction-step O(n) O(n) O(1) O(n) O(n) O(n) O(1)–O(n) O(n) Table 3.6: Complexity of l -calculus in Opulus It is interesting to note that the built-in name comparison of OPULUS causes extra work when emulated in the l -calculus. Yet, it should not be given to much importance, since the l -calculus can easily be extended with d -reductions that ac- complish the required comparisons with O(1) complexity. So, abstracting from the name evaluation overhead the two standard transla- tions essentially do not differ (compare table 3.5 and table 3.6). Let us turn to the fast translations: It immediately strikes our attention that redex recognition is O(1) in either translation. This is due to the fact that redexes are directly translated into 3.5 Conclusion 53 redexes of the other calculus. Besides this exception, there is the usual overhead for name comparisons in the l -calculus. Notwithstanding, substitution in OPULUS (re- placing $ and #) is just O(1) in the l -calculus. This is achieved by using b -reduction, i.e., the application of l sel f :l arg: (cid:28) A (cid:29) to the values of $ and #, reduces in two (constant) steps. Substitution of l -calculus terms in OPULUS has linear complexity, because vari- ables have to be looked up in the variable environment. In an object-oriented pro- gramming language this step is constant too, since it simply requires an attribute access or — in the worst case — an access through an indirection table in case of an overridden attribute. So, on the one hand substituting l -calculus terms takes linear time in OPULUS but on the other hand note that looking up OPULUS methods takes linear time in the l -calculus (see row “Application” in table 3.5 on the facing page). There is no equivalent to looking up method names in the l -calculus which is why there is no such entry in table 3.6 on the preceding page. De facto, these prominent dif- ference in complexity of emulations point out a crucial difference of the underly- ing paradigms: Object-orientation is characterized by the atomicity of looking up a name and performing the corresponding method. Functional programming is characterized by having access to function parameters at any place in any nesting depth. Overall, the two calculi appear to be almost equal in expressiveness3, since their translations involve linear complexity respectively (excluding the overhead for comparing names in the l -calculus). Where differences are present (method lookup and variable substitution) they point out paradigm fundamentals. Especially, the fast translations — we might also say native translations, since idiomatic constructs were used — demonstrated a very direct way of translating calculi constructs: Functions in the l -calculus could be captured by objects operat- ing as closures. Objects in OPULUS could be represented by l -terms and selector functions, e.g., an object with the fields a, b, and c — l s:s a b c — receives a message (selector) “b” by being applied to l x y z:y, yielding field b. Finally, one may note that fast and standard emulation of the l -calculus are very close to each other (see table 3.6 on the facing page). This may hint to the fact that objects are a good abstraction for simulation. The direct simulation of l -calculus was almost as efficient as the version that exploited native features of OPULUS. Also, we may recall that an object-oriented languages would not need linear complexity to look up l -variables in the environment. In contrast, an implementa- tion of OPULUS in a functional programming language will still need linear com- plexity to look up names, unless further efforts like introducing method lookup arrays or similar machinery is introduced. Before we derive further conclusions from the translation complexities found here, a small investigation is in order. Apparently, translation complexities depend 3You may want to consider [Thierbach96] for a full account including h -reductions. 54 3 Calculuscomparison on the translation strategies. What if a better scheme, say for translating OPU- LUS into the l -calculus, could be found? Is it not possible that one calculus is still superior but we have not found the right translation scheme yet? The fact that both translations exhibit mostly linear characteristics gives us confidence that we found optimal translations and future translation schemes will not affect the over- all result. But what about sub-linear translations? Are they possible? Consider, a translation from a RAM model with just numerical addition into one that features multiplication also. When addition loops are converted into multiplications this could result into a sub-linear translation. However, the reverse translation would obviously be super-linear. If one our translations could be optimized to a sub-linear translation, the other one could not possibly have linear characteristics right now. Proof by false assumption: Assume the forth translation to be sub-linear and the back translation to be linear. With repeated back and forth translations a program could be made infinitely faster. As the conclusion is obviously false, so must be the assumption [Thierbach97]. In summary, it is certainly not a reverse logic approach to aim at subsuming functions with objects. While the other way round is not impossible either it ap- pears less natural. Moreover, when leaving the level of calculi, it appears more rea- sonable to subsume reduction semantics with state than to “ruin” the basic assump- tion of referential transparency with the introduction of stateful objects. Note, how- ever, the striking correspondence of some well-known combinators with impera- tive statements: S (cid:17) “:=”, K (cid:17) “const”, I (cid:17) “goto”, and CB (cid:17) “;” [Hudak89], sug- gesting how to emulate imperative features in a declarative language. I will con- tinue the discussion about the conflict between state and referential transparency in section 4.1.1 on the next page and section 4.2.1 on page 57. 55 4 Conflict & Cohabitance When two worlds collide the question is whether they cancel out or enlighten each other. – me N ow that we know the foundations of both functional and object-oriented programming and convinced ourselves that subsuming functional pro- gramming with an object-oriented language works fine at the calculus level it is time to refocus our overall goal. Part II starting at page 85 is going to present functional concepts made amenable to object-oriented design by captur- ing them in design patterns. But which functional concepts are appropriate? This chapter discusses functional and object-oriented concepts that seem to be at odds with each other (section 4.1) and shows a path of possible integration (section 4.2 on page 57) partly reconciling conflicts discovered in the following section. 4.1 Conflict Although, the preceding chapter gave us confidence that an integration of func- tional concepts into an object-oriented language is viable it might be the case that incommensurable properties prevent an integration at the programming language level. Indeed, the following sections reveal some immediate oppositions and re- dundancies. 4.1.1 Oppositions This section discusses diametral oppositions of the functional and the object- oriented paradigm. 4.1.1.1 Semantic model As elaborated in section 1.2.2 on page 11 functional programming is founded on reduction semantics, i.e., excludes the presence of side-effects. Object-orientation, however, relies on stateful objects (see section 2.2.6 on page 37). This is a serious conflict. Abandoning either reduction semantics or stateful objects seems to de- stroy one of the paradigm foundation piles respectively. Actually, the integration of functional and object-oriented features amounts to combining both declarative and algorithmic language paradigms (see figure 4.1 on the next page). 56 4 Conflict&Cohabitance Programming Paradigms declarative algorithmic logic functional object-oriented procedural Figure 4.1: Classification of programming paradigms 4.1.1.2 Decomposition There is a fundamental dichotomy between the decomposition strategies in func- tional and object-oriented programming on two levels: On a large scale we must decide whether to decompose a software system into functions (section 1.2.1 on page 10) or into objects (section 2.2.1 on page 32) [Meyer88]. On a small scale we have to choose between data abstraction (section 1.2.1 on page 10) or procedural ab- straction (section 2.2.1 on page 32) [Cook90]. Either we package data constructors and use functions that dispatch on those or we package functions and distribute them to their respective constructors. 4.1.1.3 Evaluation In section 1.2.2 on page 11 and section 1.2.4 on page 14 we have learned that normal- order reduction or lazy evaluation has desirable properties. This is the case as long as side-effects are not allowed. Side-effects do not fit with lazy evaluation with its unintuitive and data dependent evaluation order. For this reason, functional languages with side-effects (e.g., ML or UFO) use eager evaluation. Then, side- effects of the function arguments will appear prior to that of the function itself. It has also been said that laziness can conflict with dynamic binding. In order to dynamically bind on an argument it needs to be evaluated anyway without any option of delay [Sargeant95]. 4.1.1.4 Encapsulation Data abstraction, i.e., exposing data constructors to functions of an abstract datatype works well with pattern matching (section 1.2.5 on page 16) but is at odds with encapsulation (section 2.2.2 on page 32). This is not an issue of object en- capsulation but has been recognized in the functional programming community as well [Wadler87]. A function that uses pattern matching for a datatype is exposed to its representation — as it accesses its constructors — and is subject to change whenever the datatype has to be changed. There has been a number of propos- als how to avoid this drawback, each trying to improve on the weaknesses of the former [Wadler87, Thompson89, Burton & Cameron94, Gostanza et al.96]. 4.2 Cohabitance 4.1.2 Redundancy 57 It is obvious that opposing concepts exclude their integration. All the same, it is of no use to integrate concepts that are redundant to each other. Concepts must complement each other otherwise a software solution will become an incoherent mix of styles which is hard to understand but does not justify its diversity with corresponding properties. 4.1.2.1 Parameterization Higher-order functions (see section 1.2.3 on page 12) and inheritance (see sec- tion 2.2.3 on page 33) can both be used for behavior parameterization [K ¨uhne95b]. The Template Method pattern uses subclassing to achieve behavior parameteriza- tion akin to higher-order functions. Unless the two mechanism do not yield soft- ware solutions with different properties and no further complement of one to the other can be found, one should be dismissed. 4.1.2.2 Dispatch Pattern matching (section 1.2.5 on page 16) and dynamic binding (section 2.2.5 on page 35) can both be used to decompose a function into partial definitions and to select the appropriate portion when executing the function. Both mechanisms di- vide a function according to the constructors it operates on. While pattern matching keeps the patterns at one place, dynamic binding distributes it to the constructors. It should be clear whether two code selection mechanisms are needed. 4.2 Cohabitance If we literally agreed to all the arguments made in the previous sections it would be time to abandon any paradigm integration at all. Luckily, many points made above that at first appear excluding are in fact complementing. In the following, I propose how to subsume and integrate functional concepts into those of the object-oriented paradigm. The last section goes beyond feasibility and presents synergistic effects between the two paradigms. 4.2.1 Subsumption Subsuming functional concepts into object-oriented ones means to find ways to express them without changing an object-oriented language. 4.2.1.1 Pure functions Even without motivation from functional languages it has been suggested to use side-effect free functions only [Meyer88]. The so-called command-query separation principle prescribes to use side-effects for commands only. For instance the code, 58 4 Conflict&Cohabitance a:=io.nextChar; b:=io.nextChar; should be written as a:=io.lastChar; io.nextChar; b:=io.lastChar; io.nextChar; because the latter version clearly differentiates between reading input (query) and advancing the input stream (command). Thus, it is possible to assign a charac- ter from the input stream to two variables without accidently advancing the input stream in between. Concerning the side-effects of functions one might make a difference between abstract state and concrete state of objects. The latter may change without affecting the former. For instance, a complex number may have two states of representation: A Polar coordinates state for fast multiplication and a Cartesian coordinates state for fast addition. A complex number object may autonomously switch between these concrete states without affecting the abstract state, i.e., its complex number value. Also, some side-effects that do not affect the behavior of a system can be allowed too. The protocoling of executed commands [Dosch95] is a typical example of harmless side-effects. As a result, the command-query separation principle reconciles state with re- duction semantics and also state with lazy evaluation. The reduction semantics of functional languages is embedded into an imperative environment by restrict- ing functions to query functions only. Especially, with the interpretation of objects being sub-states of a system [K ¨uhne96a] the configuration of side-effect free func- tions operating on system states is close to the design of John Backus’ AST1 sys- tem [Backus78] with its main-computations between system states. Furthermore, when functions do not cause state changes and also are not influ- enced by state changes (see section 4.2.2.2 on the facing page) they may be executed in any order. This opens up the possibility of lazy evaluation again. 4.2.1.2 Dispatch We have seen that pattern matching is responsible for many subtleties (section 1.3.2 on page 21) and problems (section 4.1.2.2 on the preceding page) without a real im- pact on software engineering properties (section 1.2.5 on page 16). We therefore de- cide to abandon pattern matching in favor of dynamic binding which is capable of achieving the same but in addition provides further opportunities (see section 2.2.5 on page 35). I admit it is unfortunate to loose the nice syntax of pattern matching and to be forced to work with several classes (representing constructors) in order to deal with one function. Notwithstanding, the question of how to present and manipulate a function seems to be more a matter of tools rather than language [K ¨uhne96b]. Al- though functions are distributed over objects they can be presented to the program- mer as a single definition with an appropriate tool (see the epilogue on page 261 for a further discussion). 1Applicative-State-Transition 4.2 Cohabitance 4.2.2 Integration 59 In contrast to subsumption, integration demands for adaptions of the host language in order to encompass functional concepts. 4.2.2.1 Evaluation We already regained functions for lazy evaluation (section 4.2.1.1 on page 57) but may also want to have lazy object state semantics too. Otherwise, internal object state might be computed without being requested at all. One approach to achieve lazy effects relies on a refined state monad concept [Launchbury93, Launchbury & Jones94]. Another opportunity is to use models from concurrent object-oriented languages and to propose lazy message mailboxes for objects. In the context of this dissertation we will be satisfied with lazy functions only, though. A short note is in order regarding the remark that dynamic binding does not co- habit with lazy evaluation (see section 4.1.1.3 on page 56). First, dynamic binding only requires to know the type the receiver which amounts to a partial evaluation only. Second, function application is strict in its first argument anyway. One might evaluate arguments first but eventually the function to be applied must be eval- uated to a function abstraction. Therefore, dynamic binding poses no additional requirements that would render lazy evaluation useless. 4.2.2.2 Closures Section 3.4 on page 49 demonstrated how to use objects as closures in order to im- plement functions with lexical scoping. Closures capture the values of variables at their declaration and/or application environment as opposed to the point of exe- cution. That is why, one can safely use closures in conjunction with lazy evaluation (see section 4.2.1.1 on page 57). Once applied to values closures do not depend on state changes anymore. Why do closures need to be integrated rather than subsumed? Most object- oriented languages do not allow free functions2 and require class definition over- head for closures which especially gets worse when currying should be supported. Moreover, few object-oriented languages (e.g., SMALLTALK and JAVA) provide mechanisms for anonymous closures (e.g., blocks [Goldberg & Robson83] and in- ner classes [Sun97] respectively). 4.2.3 Synergy Synergy occurs when two or more concepts complement each other in a way that results in properties that cannot be described by the sum of the separated concepts. 2C++ being an exception but then its functions are too weak to support closures. 60 4 Conflict&Cohabitance 4.2.3.1 Parameterization Section 4.1.2 on page 57 postulated to either surrender higher-order functions or inheritance unless a good reason not to do so can be found. This section argues that both concepts are necessary and even amplify each other. We will proceed with an example involving a cook who accepts recipes in order to prepare dishes. Written mathematically: Cook (cid:8) Recipe ) Dish. We will try several instantiations for the (cid:8)-operator, i.e., (cid:8) = object responsibility (cid:8) = single-, multiple-, repeated-inheritance (cid:8) = parameterization We can evaluate the resulting solutions by asking a set of questions: Category Question SIMPLICITY What is the number of classes and the nature of class rela- tions needed? FLEXIBILITY How flexible are we in adding new cooks and recipes? Is it possible to adapt cooks? SCALABILITY How does the model scale up when we add new cooks and recipes? SELECTABILITY What support do we have in selecting cooks and recipes? REUSABILITY Can we reuse cooks and recipes for other purposes? ENCAPSULATION Which degree of privacy is maintained in a cook and recipe combination? Table 4.1: Criteria to evaluate cook and recipe combination We will now try each of the above alternatives for the (cid:8) combinator. We assess the resulting solutions by referring to the above criteria by a SMALL CAPS typeset- ting. Object responsibility The first natural approach is to consider recipes as the cook’s knowledge and, thus, as- sign them to a cook as methods (see figure 4.2). This is the most SIMPLE model but it does not offer much FLEX- IBILITY: If the Cook does not know how to prepare our favorite dish, we are lost. There is no way to extend the available recipes except by changing cook itself. Sub- classing Cook for cooks with more recipes is akin to the alternative discussed in the next paragraph. Another Cook prepare1 : Dish1 prepare2 : Dish2 Figure 4.2: Cooking with object responsibility 4.2 Cohabitance 61 drawback is that we cannot REUSE the recipes for other purposes, e.g., nutrition analysis. Finally, we have to SELECT recipes by name (prepare1, prepare2, . . . ), i.e., we cannot exploit dynamic binding for recipe selection. Consequently, code that demands a Cook to prepare a dish is sensitive to the addition of new dishes. Cook Cook1 prepare : Dish recipe : Dish Single inheritance Creating a cook subclass for each new recipe to add recipes without allows changing existing cooks (see fig- prepare is a template ure 4.3; method using recipe as a hook method, hence this design fol- lows the design pattern Template Method [Gamma et al.94]). How- ever, the price to pay for this FLEX- IBILITY is a lot of traffic in the class name space for all these specialized cooks especially when we consider multiple cooks (e.g., French and Italian chefs de cuisine). Neverthe- less, the SELECTABILITY problem is coincidentally solved too. Now we can say prepare to Cook and depending on the actual subclass (Cook1 or Cook2) we get the corresponding dish. Code can rely on the abstract recipe method and is not invalidated by the addition of new recipes. Unfortunately, we still cannot REUSE recipes for other purposes. Figure 4.3: Cooking with single inheritance recipe : Dish2 recipe : Dish1 Cook2 Multiple inheritance The only way to make the recipes reusable is to make them en- tities (classes) of their own. Using inheritance this leads to multiple inheritance (see figure 4.4). Now we can REUSE recipes, SELECT recipes with dynamic binding, and are FLEXIBLE in adding new recipes. But the model gets more COMPLEX, because of the inheritance relations. SCAL- ABILITY suffers by the fact that each new recipe demands for an additional combinator class. A new cook multiplies the inheritance relationships. Recipe1 Recipe2 recipe : Dish1 recipe : Dish2 Cook prepare : Dish recipe : Dish Cook1 Cook2 Figure 4.4: Cooking with multiple inheritance 62 4 Conflict&Cohabitance Finally we experience a loss of ENCAPSULATION: Not only cook and recipes break the encapsulation of each other (as already the case with single inheritance) but the combinator classes break the encapsulation of both cook and recipes. A change to the implementation of Recipe2 may produce a conflict with the implementation of Cook (e.g., through the use of a common variable name). Cook Recipe1 Recipe2 recipe : Dish1 recipe : Dish2 prepare : Dish prepare : Dish Repeated inheritance The try involving inheri- final tance aims at saving combi- nator classes, hence, regain- ing SIMPLICITY and SCALA- BILITY. The idea is to fold all combinator classes from the solution above to a sin- gle combinator class (see fig- ure 4.5). This combina- tor class (AllPurposeCook) re- peatedly inherits Cook and distributes each inheritance path and recipe to a unique recipe name [Meyer92]. We gained a little SIMPLICITY but did not really gain on SCALABILITY, since for many recipes we get big combina- tor classes with a lot of re- naming needed inside. More- over, adding a Cook means adding a new big combinator. As a serious drawback we lost on SELECTABILITY. We cannot exploit late binding for choosing cooks or recipes anymore. Finally, EN- CAPSULATION problems represent the worst of all solutions. Figure 4.5: Cooking with repeated inheritance prepare1 : Dish1 prepare2 : Dish2 AllPurposeCook Parameterization Clearly, inheritance does not provide a satisfactory answer to the combination of cooks with recipes. Now let us take a fresh look at the prob- lem and regard cooks as a higher-order function parameterized with recipes. Now Cook “takes-a3” Recipe (see figure 4.6 on the next page). This is a client/server relationship, hence, ENCAPSULATION is provided. The Cook asks recipes to apply themselves and depending on the actual Recipe1 the corresponding dish is pre- pared. Ergo, SELECTABILITY is also enabled. All components are REUSABLE and the model SCALES up without problems. The model also is fairly SIMPLE especially since one can forget about the (abstract) inheritance relationship as a model user. It only needs to be considered when adding new recipes which must simply contain an inherit clause in their class definition. 3“uses” or accepts as a parameter. 4.2 Cohabitance 63 Cook Recipe prepare(recipe : Recipe) : Dish apply : Dish Recipe1 Recipe2 apply : Dish1 apply : Dish2 Figure 4.6: Cooking with parameterization Now is the above model the solution to all problems? Unfortunately not, as it is not as FLEXIBLE as the inheritance models. Why is this so? Remember, the criterion for FLEXIBILITY (see table 4.1 on page 60) also asked for the ADAPTABILITY of cooks: Let us assume we receive an exquisite recipe requiring a special procedure of evaporation. Our standard cook is not capable of that so we need an extended version. The inheritance solutions have no difficulties with that requirement, for they can add the extension in the subclass that combines cook and recipe. In a pure parameterization model a redesign would require to change the parameter of Cook to something like CookAdapter, provide a dummy adapter for old recipes, and develop a special adapter — containing the cooking extension — for the new recipe. Parameterization and Inheritance To reconcile FLEXIBILITY with parameteriza- tion one can simply combine inheritance and parameterization in their appropriate places (see figure 4.7). Cook prepare(recipe : Recipe) : Dish Recipe apply : Dish Cook* prepare(recipe : Recipe) : Dish evaporate : Something Recipe1 Recipe2 apply : Dish1 apply : Dish2 Figure 4.7: Cooking with synergy 64 4 Conflict&Cohabitance In this setup both parameterization and inheritance contribute to a solution that yields optimal criteria evaluation except for a little loss of ENCAPSULATION be- tween the cook classes. Yet, this appears inevitable considering the amount of FLEXIBILITY possible with regard to a cook adaption. Note that using a pure pa- rameterization model it is not an option to simply add a new cook to the system since that would not allow subtyping between cooks, that is, we cannot reuse code that assumes the existence of one cook type only. What are the lessons to be learned from this excursion? Parameterization is su- perior to inheritance to model behavior variability. Although, it is not true — as often claimed [Pree94] — that inheritance allows for static combinations only, it in- volves severe encapsulation problems between the entities to combine. Note that it is easy to apply the arguments for cooks and recipes to other domains. For instance, let cook be an iterator and recipes iteration actions. Indeed, even the repeated in- heritance solution has been suggested for combining iterators with several iteration actions [Meyer94b]. Parameterization achieves its complexity and encapsulation advantages through the principle of black-box reuse [Johnson & Foote88, Griss95], that is, to reuse components as is without further modification. Nonetheless, parameterization alone can reduce flexibility. The strength of in- heritance, i.e., to adapt a class in unforeseen ways, is also called “white-box reuse”. White-box reuse breaches encapsulation and requires internal knowledge of the reused component but is a powerful supplement to black-box reuse due to its flex- ibility. Of course, inheritance has many other useful purposes we did not mention here (see section 2.3.2.3 on page 42) that justifies its coexistence with parameterization. Parameterization in turn has more to offer than demonstrated here. This discus- sion is continued in chapter 7 on page 93 mentioning, among other issues, why restricted flexibility can also be of advantage. 4.2.3.2 Decomposition Data abstraction We already mentioned the fundamental dichotomy between a functional and an object-oriented decomposition (see section 4.1.1.2 on page 56). Today there is no doubt about the advantages of structuring a software system ac- cording to the business objects it handles. Enormous grow rates for object-oriented systems, languages and publications, give testimony to the historical win of the object-oriented paradigm since the mid eighties over structured design and analy- sis [Quibeldey-Cirkel94, Shan95]. Apparently, there is no need for functions oper- ating on data, i.e., data abstraction, since everything can be — with advantage — expressed with data annotated with functions, i.e., procedural abstraction. Unfortunately, it is not as easy like this. The decision for data abstraction of procedural abstraction is an inevitable trade-off. It just happens to be the case that choosing procedural abstraction more often results in better future maintainability. Why is this? Consider table 4.2 on the next page. The first row contains data constructors while the first column contains opera- tions. The intersection cells contain operation implementations for the correspond- 4.2 Cohabitance 65 Line Rectangle Circle move(x, y) move two points move four points move center scale(f) scale one point scale one point scale radius display draw line draw four lines draw circle Table 4.2: Decomposition matrix ing data constructor. If we slice table 4.2 into columns we obtain an object-oriented decomposition. Each constructor packages its operation implementations. If we slice table 4.2 into rows we obtain a functional decomposition. Each function pack- ages the implementations for all constructors. What happens when we add a new constructor (e.g., Rhombus) to table 4.2 repre- senting our our software system? This is the case where functional decomposition requires to modify all functions in order to account for the new constructor (see ta- ble 4.3). It does not matter whether we need to add a new function pattern or have to extent a type switch. The crucial point is that all functions must be changed and that they might be spread over the whole software system hiding in unexpected regions. This is the case where the object-oriented decomposition excels. All that needs to be done is to add another object to the system supporting all required operations. Adding constructor function Decomposition Functional Object-oriented open all functions local object addition local function addition open all objects Table 4.3: Sensitivity of decomposition types Now, what happens when we add a new function (e.g., rotate(a)) to table 4.2? The object-oriented decomposition requires to open all objects and add the opera- 66 4 Conflict&Cohabitance tion implementation (see table 4.3 on the preceding page). At least, no client code has to be modified. Still, we must perform changes at distributed locations and pos- sibly re-testing (! regression test) of objects becomes necessary. Furthermore, op- erations will accumulate at object interfaces which become bloated sooner or later. Is display an operation an object should support? Or should display be a function of an external view component whose exchange would allow for eased manage- ment of changing presentation style? The addition of a function is the case where functional decomposition excels. Only a local addition of a function covering all constructors is necessary. Two reasons speak for preferring the object-oriented decomposition. First, it is more likely to expect the addition of constructor. A software system can be re- garded as a competence provider. Quite often one wants to expand the compe- tence to more data (e.g., handle more graphical objects) rather than expanding the competences (adding yet another user interaction). Second, extending objects with operations (monotonously) does not affect clients. Changing data in a functional decomposition, on the contrary, involves changing client code at many different places. Nevertheless, there are opportunities where a functional decomposition pays off. For instance, you might want to experiment with language tools working on an abstract syntax representation. Then the language will not change, that is, the constructors are fixed, but often new operations will be defined on the abstract syntax tree. Here, the functional decomposition is better suited as it, in addition, keeps individual operations — which are the focus in this scenario — at a single place rather than distributing them over abstract syntax nodes. This discussion is continued in chapter 12 on page 201 that combines functional decomposition with local semantic functions. In summary, there are reasons for both object-oriented and functional decom- position. One accounts for data- and the other for operational extensions. It is my understanding of Ivar Jacobson’s control objects [Jacobson et al.94] that these ac- count for operational extensions. Jacobson also features entity objects which could be — using a dogmatic object-oriented view — extended to support any required functionality. However, a division of functionality into intrinsic object properties (e.g., a circle’s center and radius) and extrinsic properties (e.g., displaying a circle or calculating p by stochastically dropping a needle onto a circle) results in a system that features hot spots for extending both data and operations. Part III beginning at page 233 will discuss a third option to interpret table 4.2 on the preceding page, elegantly escaping the dichotomy. Structured programming While it appears worthwhile to retain functions — out- side of class definitions — there is further motivation caused by the need to struc- ture functions. It has been argued that functional decomposition techniques are still useful for the decomposition of methods [Henderson & Constantin91]. Fur- thermore, functional decomposition techniques should also be applied to extract methods from objects. Consider the simulation of planes. During take-off a lot of 4.2 Cohabitance 67 management is needed to control landing gear, throttle, vertical rudder, etc. This is best managed by a dedicated takeOff function that abstracts from individual ac- tions required. In case we also want to handle helicopters we may simply alter takeOff adding rotor adjustments etc. Other code remains unchanged since it relies on a take-off abstraction. With this view, takeOff is a business-transaction [Coplien92] that orchestrates multiple objects to achieve an operational effect. It is not a direct property or capability of an object. It is true that it is perfectly natural to create an abstraction FlyingObject that supports takeOff as a method. In this case object and function abstraction just happily coincide. But what about checkPlane? Is it a feature of planes or of service personnel? Is the presentation (view) of an object its responsibility or an external function? Shaft Piece collision(s : Shaft) interface dependency representation dependency Is it the responsibility of a Tetris piece or the shaft to de- tect collisions? In the latter ex- ample with either choice a rep- resentation dependency would be created (see upper half of Representation figure 4.8). changes to the class containing the collision method would af- fect the method’s implementa- tion and interface changes to the other class would require to adapt the former. Ergo, a nat- ural solution would be a free collision function belonging to neither Piece nor Shaft (see lower half of figure 4.8). In that case changes to piece or shaft representation do not affect the collision function and interface changes are not propagated be- yond the collision function. In either case a potential ripple ef- fect of changes through the sys- tem is prevented. Object-oriented purists might invent a Part object abstraction that would feature a collision method and let Piece, Shaft, or both inherit from it. Just in order to avoid a free function (Collision) the resulting design would posses the natural coincidence of object and function abstraction as in the takeOff case above and, furthermore, would have worse properties than that of figure 4.8. If Part is used for other function abstractions and even holds data then a restructuring of one function abstraction affects the others and may require the data hierarchy to be changed. This should not happen and would be the result of forcing function abstractions into a pure object-oriented view. Figure 4.8: Restructured dependencies interface dependency Collision Piece Shaft 68 4 Conflict&Cohabitance 4.3 Conclusion Although a fruitful integration of functional concepts into object-oriented program- ming appeared to be impossible first (section 4.1 on page 55) I demonstrated the most concepts to be complementary rather then excluding (section 4.2 on page 57). We retained stateful objects for real world modeling without impedance mis- match, efficiency, and for maintenence reasons. “Pure languages ease change by making manifest the data upon which each operation depends. But, sometimes, a seemingly small change may require a program in a pure language to be extensively restructured, when judicious use of an impure feature may obtain the same effect by altering a mere handful of – Philip Wadler lines. [Wadler92]” Nevertheless, the virtues of referential transparency are still available due to the command-query separation principle. We have seen that a restriction to a pure functional or object-oriented de- composition leads to unsatisfactory results in either case. Both object-oriented or functional decomposition can be appropriate depending whether the objects or the functions (business-transactions) are the more stable concept. After an initial repulsion of any function which is not a object method it is time to re- integrate free functions into object-oriented design again. A move towards this direction are so-called command objects, e.g., used for application callback func- tions [Meyer88, Gamma et al.94]. Still, these are not acknowledged as functions but as representations of actions to be undone or protocoled [Meyer88]. Regarding the separation between subsumption (section 4.2.1 on page 57) and integration (section 4.2.2 on page 59) it should be noted that the borderline be- tween is somewhat arbitrary. One may also claim that objects subsume functions without further integration needed or that side-effect free functions and value se- mantics need further integration. With the assessment that stateful programming subsumes functional programming and objects subsume values I meant the op- portunity to partly renounce side-effects by programmer discipline. Of course, it is acknowledged that language support for controlling side-effects is highly desirable (see section 14.5 on page 243). In conclusion, it appears quite reasonable and promising to integrate two seem- ingly contradicting paradigms. However, someone using such a dual-paradigm approach needs guidance when to choose which alternative. Figuratively speaking, when to choose the declarative or the procedural branch of figure 4.1 on page 56. It is the intent of the pattern system presented in part II starting at page 85 to provide a base of arguments for a decision. 69 5 Design Patterns Once is an event, twice is an incident, thrice it’s a pattern. – Jerry Weinberg T he notion of a software design pattern is not formally defined. More than two years after design patterns have been popularized in 1994 there are still argues about their correct definition and their meaning for soft- ware development [Gabriel96]. In order to define the role of design patterns in the context of this dissertation I provide an introductory definition starting with ex- amples from everyday life (section 5.1). After considering the origins of patterns (section 5.2 on page 74) I mention some of the promises to be expected from design patterns (section 5.3 on page 76). This chapter concludes with an explanation of the design pattern format (section 5.4 on page 76) and a description of the diagram notation (section 5.5 on page 79) both to be used in part II beginning at page 85. 5.1 Definition Patterns are ubiquitous. Most often they are not recognized and less often they are captured by a literate form, but still they exist. This is true of the everyday meaning of the word “pattern”, but also, and more importantly, it is also true for a special meaning first described by the architect Christopher Alexander: “Each pattern is a three-part rule, which expresses a relation between a certain context, a problem, and a solution [Alexander79].” – Christopher Alexander However, it is the nature of patterns to recur, so let us simplify the above to: A pattern is a recurring solution to a problem in a context. That is why patterns are ubiquitous. People, but also the mechanisms of nature, repeatedly apply the same solutions to problems that occur in a certain context. 5.1.1 A gentle introduction Let us assume the context of writing a piece of music for the mass market. We are confronted with conflicting forces: 70 5 DesignPatterns (cid:15) Easy listening. For quick reception and lasting pleasure we need a catching theme that is often repeated. (cid:15) Boring monotony. Simple repetition of the same stuff will turn people off and we will loose their interest. A solution to these forces that has been applied again and again by composers all over the world is to use two basic themes that are repeated in turn and throw in a special theme near the end of the song. If we name the themes A, B, and C, the successful composition pattern is depicted by figure 5.1. introd uce entertain consolidate w ake u p calm d o w n fade out A B A C A B Figure 5.1: A composer’s pattern Using this pattern has several consequences: (cid:15) Familiarity. The listener is smoothly introduced into our musical ideas and can learn the themes due to their repetition. After the exiting change with special theme “C” we calm him down again. (cid:15) Keep awake. At the point when the listener might think there is no new stuff to discover we surprise him with the more exiting “C” theme. (cid:15) Regularity. If we build all our songs according to this knitting pattern the lis- tener will recognize it in the long run and be bored in spite of the surprising variation of repetition. This description does not aim at completeness. It merely attempts to explain the pattern concept with everyday notions. If we travel a bit further down this road we can see how patterns may collaborate with each other. Let us imagine we composed a song according to the “ABACAB” pattern but aim at a very dramatic coda. We may use another item from a composer’s toolbox that can tentatively be called “One Note Up”. It is another pattern resolving a different set of forces: (cid:15) Keep attention. Listeners accustomed to the “ABACAB” pattern may loose their interest towards the end of the song. (cid:15) Do not confuse. Introducing yet another theme will overstrain the listener. 5.1 Definition 71 We resolve the forces by repeating the last theme again but this time with all notes increased by one note. As consequences we have: (cid:15) Cosy feeling. The listener recognizes the variation to the theme and is not re- quired to learn a new one. (cid:15) Boredom prevented. The added note creates a dramatic effect that keeps the listener tuned. Note, how the solution of the first pattern provides the context for the second pat- tern. This is how true pattern languages work. We can think of single patterns as elements of a grammar that, by its production rules, defines a language which tell us how to weave patterns together. In contrast, a set of patterns that provides use- ful collaborations but does not provide guidance how to choose patterns from a lower level when higher level decisions have been made is called a pattern system. Also, we have seen how the names of patterns like “ABACAB” and “One Note Up” establish a vocabulary to discuss the design of songs. As language can lever our level of thinking we may now design songs on a higher level. In fact, patterns work as chunks and instead of handling only three simple things at once we may deal with the same amount of high level chunks. Let us recapitulate by assuring that “pattern” is indeed a good name for the above described notion. Table 5.1 features everyday meanings on the left hand side and their interpretation in our context on the right hand side: Facet1 Meaning pattern recognition; in- ducing attention by re- peated occurrence. Patterns are not invented, they are discovered. form or model proposed for imitation. Resolving forces in a successful way is worthwhile to be copied. knitting pattern; form or style in literary or mu- sical composition. A pattern description suggests to arrange existing participants in a certain style rather than prescribing the participants. dress pattern; struction plan. a con- Pattern descriptions also tell you how to arrive at the solution described. configuration; like frost- or a wallpaper patterns. The participants in a pattern solution form a struc- ture by their relations. behavior pattern; com- plex of individual or group characteristics. Each pattern participant plays a characteristic role in the solution. Table 5.1: Facets of “pattern” 72 5 DesignPatterns With regard to patterns we may have several level of consciousness. 1. We are not aware of a recurring solution. 2. We know some “tricks” that jump to our help in some situations, but we can- not fully capture what the real magic is, neither can we determine the charac- ter of the applicable situations. 3. We exactly know when we can apply a recurring solution. 4. We know when to apply the solution and are aware of all consequences in- volved. 5. We are able to linearize our knowledge into a literate form such that others can learn from our experience. The last level of course refers to design pattern descriptions. Considerable in- sight is necessary to write a comprehensible pattern description and the result can be considered to be novel work, although the main content on the contrary must be well-known to work. Of course, it is possible to use a pattern description style (see, e.g., section 5.4 on page 76) to document original solutions, i.e., that did not yet recur. It has been suggested to call these “Proto-Patterns”. 5.1.2 Software Patterns Patterns are useful in the context of software development, since software engi- neering is not a science yet. There is no straight way of creating an optimal system and probably never will be. This is the case for patterns. They are useful when something creative has to be build and no hard and fast rules apply. Patterns are to be applied by humans not by (today’s) computers. “: : :software designers are in a similar position to architects and civil en- gineers, particularly those concerned with the design of large heterogeneous constructions, such as towns and industrial plants. It therefore seems natural that we should turn to these subjects for ideas about how to attack the design problem. As one single example of such a source of ideas I would like to mention Christopher Alexander: Notes on the Synthesis of Form [Alexander64].” – Peter Naur “Subsystems created by the composition of objects or interaction machines do not conform to any accepted notion of structure and are very hard to charac- terize, though they do determine subsystems that exhibit reusable regularities of interface behavior. The term pattern is used to denote reusable regularities of behavior exhibited by interactive subsystems created by the composition of – Peter Wegner interaction primitives. [Wegner95].” 1These are partly taken from a dictionary [Harkavy & et al.94]. 5.1 Definition 73 “A pattern is a piece of literature that describes a design problem and a general solution for the problem in a particular context.” – James O. Coplien As yet, no methodology (e.g., [Booch94]) offers a means to describe designs as ap- propriately as patterns can do2. “The problem specification and results for sorting can be formally spec- ified, while the problem class and effects of interactive patterns cannot gen- erally be formalized and require a sometimes complex qualitative descrip- tion. [Wegner95]” – Peter Wegner Patterns tie a design solution to its driving forces and its related consequences. Why is it important to capture the forces too? First, this helps to decide whether a pattern is applicable or not. escher-space-filling.ps Figure 5.2: Space filling pattern 2The upcoming UML Method integrates patterns, though [Rumbaugh et al.97]. 74 5 DesignPatterns Second, when you look, for instance, at a space filling pattern by the Dutch artist M.C. Escher the complexity of its derivation is completely hidden in the beauty of its solution. However, if you try a variation to the basic filling pattern you might be able to fill some space, only to find out it does not work in general. Similarly, it has been observed that month of work flow into the change of a system only to find out that there was a particular good reason for the old solution, which has to be restored then. In addition to presenting a concrete solution, rather than a set of first principles (like e.g., aim at low coupling and high cohesion), patterns also tie the solution to a concrete example. “If a prior experience is understood only in terms of the generalization or principle behind the case, we don’t have as many places to put the new case in memory. We can tell people abstract rules of thumb which we have derived from prior experiences, but it is very difficult for other people to learn from these. We have difficulty remembering such abstractions, but we can more easily remember a good story. Stories give life to past experience. Stories make the events in memory memorable to others and to ourselves. This is one of the – Roger C. Shank reasons why people like to tell stories [Shank90].” Design expertise is not a collection of abstract rules, because rules, though useful for evaluations, do not generate solutions. Design experts know many working solutions and know when to apply them: “: : :wisdom is often ascribed to those who can tell just the right story at the right moment and who often have a large number of stories to tell [Shank90].” – Roger C. Shank 5.2 History It is hard to determine who described the notion of a pattern first. Because of its fundamental and universal meaning for the human mind it should not come as a surprise to discover works as old as Liu Hsieh‘s (465–522) “The Literary Mind and the Carving of Dragons” (subtitle: A study of thought and pattern in Chinese literature). He discusses forty nine different patterns in Chinese literary classics in the style of a cookbook [Freddo96]. The first to popularize patterns, however, was Christopher Alexander. He cap- tured successful architectural solutions in pattern descriptions that are supposed to allow even non-architects to create their own individual architectural solutions (e.g., rooms, houses, gardens) [Alexander et al.77, Alexander79]. His patterns form a language, i.e., they provide a top-down guidance from the arrangements of cities down to areas, houses, and the shaping of rooms. Alexander’s work also caused the software community to pay attention to patterns. His first influence already took place at the 1968 NATO conference3 in Garmisch when Naur, referring to Alexan- der’s Ph.D. thesis [Alexander64], mentioned the connection between architecture 3Coincidentally, Friedrich L. Bauer coined the term “Software Engineering” at this conference. 5.2 History 75 and software construction [Naur69] (see section 5.1.2 on page 72). Alexander’s the- sis is even said to have been the basis for the structured design concepts of coupling and cohesion [Ballard95]. In retrospect, Robert Floyd mentioned pattern-like ideas in his 1978 Turing Award lecture, where he proposed to teach concepts or paradigms like “prompt- read-check-echo” and “generate-filter-accumulate”, rather than concrete program- ming languages [Floyd87]. But officially, it took the software community nineteen (19!) years to rediscover patterns when Ward Cunningham and KentBeck, inspired by Alexander’s work, in 1987 decided to let future users of a system design it by supplying them with a small pattern language for developing user interfaces with SMALLTALK. They reported the results at OOPSLA ’87 [Meyrowitz87]. Independently, Erich Gamma was dealing with ET++ [Weinand & Gamma94] and realized the importance of recurring design structures in his Ph.D. thesis about ET++ [Gamma91]. Ralph E. Johnson used patterns to document a framework in 1992 [Johnson92] and also James O. Coplien’s C++ idioms4 contributed to the pattern litera- ture [Coplien92]. Further publications on patterns followed [Gamma et al.93, Beck & Johnson94]. A landmark was established with the first design pattern cat- alog book by the GOF5 in 1994 [Gamma et al.94]. In August 1994, the first con- ference on patterns were held at the Allerton Park estate near Monticello, Illinois. Many followed since then and are sure to follow in the future. Curiously, the foundation of object-orientation was perceived by Alan Kay through a typical pattern discovery. First, in 1961 he noticed a particularly clever solution to store and retrieve data for the Burroughs 220 machine [Kay96]. The scheme provided a flexible file format by using three file sections: The first was an indirection table to procedures (residing in the second part) that knew how to read and write the data of arbitrary size and format in the third pard. This scheme corresponds to objects that “know” how to store and restore themselves. The next occurrence for Kay was the Program Reference Table of the Burroughs B5000. It similarly allowed a procedural access to modules. The third encounter in 1966 was caused by the Sketchpad6 system. It used embedded pointers to procedures to achieve an indirection in calls to procedures. Finally, Kay was asked to fix an ALGOL compiler, which has been doctored to compile SIMULA. When, during his compiler studies, he noticed the similarity between Sketchpad and SIMULA objects and discovered the independent nature and message sending activities of SIMULA instances the “pattern” object-orientation came alive in his mind. 4Idioms can be considered to be language-specific patterns that are used to circumvent limita- tions in — and/or to provide extra functionality beyond — an implementation language. 5Gang of Four: Erich Gamma, Richard Helm, Ralph E. Johnson, and John Vlissides; authors of the first design pattern catalog book [Gamma et al.94]. 6An early interactive computer graphics system. 76 5.3 Promise 5 DesignPatterns Design patterns recommend themselves by many good reasons (see table 5.2). Category Description Reusable Software Software systems built with patterns are easier to main- tain, since patterns, among other aspects, often capture the key solutions that make software extensible. Reusable Design Patterns can be used as building blocks for system de- signs [Beck & Johnson94]. There is no need to invent these micro-architectures again and again. Documentation Augmenting a design documentation with pattern names and assigning pattern roles to system components in- stantly communicates a chosen design decision to a pat- tern connoisseur7 [Johnson92]. Communication Pattern names establish a vocabulary that enables to dis- cuss about designs at a higher level. Teaching As recorded experience patterns can be used for teaching successful designs. Patterns are a way to hand down cul- ture from generation to generation [Coplien96b]. Language Design If a recurring solution can be recognized as a work-around due to an inappropriate implementation language, it should be taken in consideration for the design of new lan- guages [Baumgartner et al.96, Seiter et al.96]. Table 5.2: Promise of design patterns The last category in table 5.2 is not usually promoted as a benefit of patterns, yet plays an important role for this dissertation: Part III starting at page 233 discusses the impact on language design caused by the pattern system presented in part II beginning at page 85. 5.4 Form All design pattern descriptions of part II use a slightly modified GOF pattern tem- plate [Gamma et al.94]. Besides minor changes to style and the division of the mo- tivation into a problem and solution section I added categories to section “Related Patterns” in order to clarify the nature of each relation. The following template description has been adapted from the original GOF template description: 7A scientific proof of this statement has been made with a controlled experiment showing the recognition of patterns to result in faster and better changes to a program [Prechel et al.97]. 5.4 Form 77 escher-pattern-system.ps Figure 5.3: Software system of interwoven patterns. Pattern Name The pattern’s name conveys the essence of the pattern succinctly. A good name is vital, because it will be used in design discussions. Intent A short statement that answers the following questions: What does the design pat- tern do? What is its rationale and intent? What particular design issue or problem does it address? Also Known As Other well-known names for the pattern, if any. 5 DesignPatterns 78 Motivation A scenario that illustrates — Problem — a design problem — Solution — and how the class and object structures in the pattern solve the problem. The scenario will help you understand the more abstract description of the pat- tern that follows. Applicability What are the situations in which the design pattern can be applied? What are ex- amples of poor designs that the pattern can address? How can you recognize these situations? (cid:15) An applicability bullet. An applicable situation. Structure A diagram showing relationships between participating classes and/or objects. Participants The classes and/or objects participating in the design pattern and their responsi- bilities. (cid:15) Participant Name – Responsibility for what. Collaborations An interaction diagram and verbal description explaining how the participants col- laborate to carry out their responsibilities. (cid:15) Collaboration. 5.5 Diagramnotation Consequences 79 How does the pattern support its objectives? What are the trade-offs and results of using the pattern? What aspect of system structure does it let you vary indepen- dently? (cid:15) An consequence bullet. Description of consequence. Implementation What pitfalls, hints, or techniques should you be aware of when implementing the pattern? Are there language-specific issues? (cid:15) An implementation bullet. Description of implementation aspect. Sample Code Code fragments that illustrate how you might implement the pattern in EIFFEL. Known Uses Examples of the pattern found in real systems or languages. Related Patterns What design patterns are closely related to this one? Categorization Patterns with important similarities and differences. Collaboration Patterns that allow useful collaborations. Implementation Patterns likely to be used for implementation. 5.5 Diagram notation Following the GOF pattern template, all interaction-, class- and object diagrams use an extended OMT diagram notation [Rumbaugh91]. In the following figures shadowed ellipses are used to separate notational elements from their description. 80 5 DesignPatterns 5.5.1 Class diagram notation Class name AbstractClass method(argument : ArgType) Abstract Method inherits Type parameter ConcreteClass GenericClass method(argument : ArgType) method(argument : ArgType) : ResultType s e t a e r c s e t a g e r g g a Heir1 Heir3 Heir2 uses Figure 5.4: OMT Notation: Class diagram Aggregation, as usual, means that a class has an attribute of the pointed-to class type. The uses relation denotes the pointed-to class to be a server and thus includes aggregation and parameter passing. 5.5.2 Object diagram notation anObject references creates anObject anObject Figure 5.5: OMT Notation: Object diagram 5.5 Diagramnotation 81 5.5.3 Interaction diagram notation Object identifier anObject anObject anObject create method call e n i l e m T i returning a result Object active method(argument) Skipping time Figure 5.6: OMT Notation: Interaction diagram 82 5 DesignPatterns 83 Part II Pattern System 84 85 6 Catalog Design patterns are points in a multidimensional design space that denote configurations of abstract design rules that actually work. – me C hapter 3 on page 45 discussed the compatibility and utility of several functional concepts for object-oriented design. Now we select the con- cepts to be presented as design patterns. First, I briefly discuss the ten- sion between Patterns and Proto-Patterns. Second, I motivate the particular selec- tion of concepts made and use so-called pattlets to generalize on each resulting pattern’s solution or supporting idea. Table 6.3 on page 92 lists all patterns with page index and intent description. Finally, I motivate the use of EIFFEL for sam- ple code. Chapter 13 on page 221 elaborates on the possible interactions between patterns of the system. 6.1 Proto-Patterns Before I am going to present a system of patterns I should elucidate whether it should better be called a system of Proto-Patterns. Section 5.1.1 on page 69 intro- duced the term “Proto-Pattern” to denote patterns that lack the empirical evidence of successful application. After all, I aim at enriching object-oriented culture with concepts from a different paradigm. Indeed, for most of the presented patterns and their variants I cannot fulfill the rule of three, that is, name three occurrences in software systems. Does that make the patterns less useful? My answer is no! First, it is problematic to define the quality of something by its quantitative occur- rence. According to this measure, FORTRAN and COBOL would probably be the best programming languages. Neither does the widespread use of a design solu- tion imply its quality. Second, the patterns presented are proven designs though not in object-oriented systems. Nevertheless, they record successful designs from functional programming. Apparently, the rule of three is a good idea to motivate pattern authors to do their homework but should not be taken too literally. “And occasionally, we do not start from concrete observation at all, but build up the invariant by purely abstract reasoning. 86 6 Catalog For of course, the discovery of patterns is not always historical. Some of the examples I have given might make it seem as though the only way to find patterns is to get them from observation. This would imply that it was impossi- ble to find patterns which do not already exist in the world already: and would therefore imply a claustrophobic conservatism; since no patterns which do not already exist could ever be discovered. The real situation is quite different. A pattern is a discovery in the sense that it is a discovery of relationship between context, forces and relationships in space, which holds absolutely. This discov- ery can be made on a purely theoretical level [Alexander79].” – Christopher Alexander Anyway, the presented patterns capture the software engineering properties of functional concepts and techniques and introduce object-oriented designers to some remarkable solutions. 6.2 Pattern System The subsequent sections motivate each pattern (see table 6.3 on page 92) in the context of the discussion in chapter 4 on page 55. The actual patterns are presented in the following chapters using the template description of section 5.4 on page 76. Any collaboration between patterns within the presented system will be covered in chapter 13 on page 221. Subsequently, I use so-called pattlets to express the general idea, wisdom be- hind, or observations supporting a pattern. Pattlets recommend themselves to ex- press a practical advise without the overhead of a full blown pattern description. For instance: Pattlet I Describe a very general pattern extremely poignant and concise with a pattlet. } A pattlet just very briefly expresses a statement that has a very general applica- bility and would need specializations at least in the form of multiple examples or even dedicated patterns in order to be a comprehensible pattern description. 6.2.1 Function Object One of the most important concepts in functional programming are higher-order functions [Hughes87]. Coincidentally, parameterization was shown to cohabit with inheritance even synergistically (see section 4.2.3.1 on page 60): Pattlet II For mature domains use black-box reuse. } 6.2 PatternSystem 87 This statement refers to the fact that object composition, i.e., function parame- terization is superior to white-box reuse [Johnson & Foote88, Griss95] but the hot- spots of the domain have to be known. In other words, use white-box reuse until you found out the right spots to parameterize. Then, black-box reuse is more safe, dynamic, and comfortable. A Function Object [K ¨uhne94, K ¨uhne95b, K ¨uhne95a, K ¨uhne96c, K ¨uhne97] is foremost a method object (akin to the Strategy pattern [Gamma et al.94]) but — as a variant — also subsumes a message object (Command pattern [Gamma et al.94]). A method object “objectifies” [Zimmer94] a method, thus, lifting it to first class sta- tus. A message object reifies a message, hence, making it amenable to persistence, logging, undoing, and most importantly allows to postpone it. The latter property is ideal for implementing callbacks [Meyer88, Gamma et al.94]. Beyond this, Function Object adopts the functional technique of currying (see section 1.2.3 on page 12). Hence, it expands on Strategy by capturing real closures and extends Command applications with partial parameterization. 6.2.2 Lazy Object The second most important concept supporting modularity — next to higher-order functions — from functional programming is lazy evaluation [Hughes87]. It is worthwhile attempting to achieve the same decoupling between generators and consumers, to aim at a similar independence of irrelevant dependencies — Pattlet III The early bird gets the worm but the lazy lion gets the flesh. } — and to capture infinite data structures. Pattlet IV Capture infinity by creating a cycle. } Lazy Object handles both aspects of call-by-need semantics: 1. Latest possible evaluation and 2. at most once evaluation of expressions. It also leads to the particular useful notion of lazy streams, which can be used to support iteration and conversion of collections. 6.2.3 Value Object Section 4.2.1.1 on page 57 argued in favor of pure, i.e., side-effect free functions. A deliberate renunciation of state in programming was chosen to reduce software complexity due to state interactions. 88 Pattlet V 6 Catalog Expand your freedom by restricting yourself. } Beyond that, we might want to exclude certain procedures from changing other- wise mutable objects. The desirable properties of values from functional program- ming motivate the need for declaring immutable access paths to objects. Further- more, some objects should behave like values, that is, should have copy semantics. 6.2.4 Transfold Whereas in object-oriented programming iterators are very popular (see Iterator pattern [Gamma et al.94]) functional programmers use so-called mappers. Pattlet VI For safety and reuse: Don’t call us we call you. } Mappers (e.g., map and fold) accept a function and apply it to all elements of a collection. Transfold, therefore, is an application of the functional principle to use general purpose library functions which can be specialized by using higher-order functionality. Transfold is shown to be specializeable to a plethora of operations. Pattlet VII Provide the most powerful concept and specialize it to solve concrete problems. } Although, it is acknowledged that functional mappers are easier and safer to use than conventional iterators, it is commonly assumed that (cid:15) they are not applicable in languages without built-in support for closures and (cid:15) they are less flexible than iterators. Transfold invalidates both arguments by generalized folding with function objects. 6.2.5 Void Value Void Value [K ¨uhne96b] is inspired by pattern matching. Yet, instead of establishing a similar mechanism I observe that pattern matching is simply another form of case statements. One of the fundamentals of object-oriented design, however, is Pattlet VIII Replace case statements with dynamic binding. } 6.2 PatternSystem 89 Although this guideline can also be taken too far (see the Translator pattern on page 201 for a discussion when external dispatching is superior) it is at the heart of object-oriented design to avoid change sensitive client code. Surprisingly, object- oriented languages force their users to write case statements all the time, namely in the form of conditionals testing for object references to be uninitialized (Nil1) or initialized (non-Nil) [K ¨uhne96b]. This is caused by the fact that empty or non- initialized data is often represented as a Nil value. Dispatching on Nil does not work since it is not a type but an exceptional reference state. Luckily, another pattlet comes to our rescue: Pattlet IX Dispatch on values by making them types. } Void Value shows how to represent datatype constructors as subtypes in or- der to avoid client code checking for uninitialized data. Coincidentally, the values as types metaphor shows the object-oriented way of pattern matching. Instead of representing, e.g., the empty or non-empty state of a list as the list’s state one can define NilList and ConsList as subclasses of List and distribute any function’s defi- nition respectively. Curiously, for Void Value the functional concept was not the prototype, but pointed out a weak and inconsistent spot in the object-oriented framework. 6.2.6 Translator Translator [K ¨uhne98] uses external functions to avoid element interfaces from be- coming bloated and being subject to changes. It, accordingly, prefers a functional over an object-oriented decomposition (see section 4.2.3.2 on page 64). Actually, it employs the Generic Function Object pattern on page 109 to implement free multi- dispatching functions. Translator is inspired by denotational semantics descrip- tions [Schmidt86] and facilitates incremental evaluation. Pattlet X Incremental evaluation calls for homomorphic translations. } Homomorphic translations are well-known in functional programming (e.g., by the so-called fold operators [Bird86, Gibbons & Jones93]) and Translator relies on them for local function descriptions and incremental evaluation. Translator also uses an intermediate data structure which is in close correspon- dence to the explicit data structures used in functional programming to facilitate proofs and clarify design [King & Launchbury93]. John Backus’ FP language com- 1Nil has many different names such as Null, None, and Void. 90 6 Catalog pletely relied on such intermediate structures which were processed by the avail- able set of fixed functional forms [Backus78]. Summarizing, Translator joins the four aspects of (cid:15) homomorphic translations, (cid:15) separation of translation and semantics, (cid:15) potential incremental translations, and (cid:15) external polymorphism. and thus describes a general approach for homomorphic translations in an object- oriented language. In conclusion, I certainly do not claim to have presented all patterns existing in functional programming. Functional programming is full of interesting techniques that are worthwhile to be documented and are not presented here. Nevertheless, the patterns chosen for this thesis provide a complete coverage of concepts sup- ported by functional languages. See section 4 on page 55 for the discussion about concepts worthwhile to be covered and reasons for rejecting others. Most importantly, higher order functions and lazy evaluation are captured by Function Object and Lazy Object. The virtues of immutability are discussed by Value Object. With Transfold we leave the realm of language concepts and enter language usage. Using folds on collections stems from functional programming folklore rather than being a particular language concept2. Also, Void Value does not capture pattern matching in full but draws an important lesson from this func- tional language concept. Finally, Translator builds on Function Object and the idea of Transfold — to replace constructors with functions — to externalize function- ality for homomorphic interpretations. While less obvious then folding, recursive interpretations and explicit intermediate data structures are certainly reoccurring concepts in functional programming. Coincidentally, Transfold is an ideal example to showcase the virtues of higher-order functions and lazy evaluation contributing to a design solution that resolves surprisingly many issues at once. The presented patterns cover a spectrum from functional language concepts to typical uses of functional languages as depicted in table 6.1. Language concept Language usage Function Object Lazy Object Value Object Transfold Void Value Translator Functional programming Object-Orientation Table 6.1: Functional pattern spectrum 2Higher order functions certainly suggest to use internal iteration, though. (cid:224) (cid:224) (cid:224) (cid:224) (cid:224) (cid:224) 6.3 WhyEiffel? 6.3 Why Eiffel? 91 I choose EIFFEL as the language to illustrate pattern issues with sample code, since (cid:15) it is well-known, (cid:15) uses garbage collection, (cid:15) has a clean-syntax, (cid:15) and is purely object-oriented. A language like C++ would force the examples to include code for memory management, distracting from the prior goals. EIFFEL’s syntax is sufficiently easy to read in order to allow C++- or SMALLTALK programmers to benefit from the examples. BETA has a much less clear syntax and is also already influenced by functional ideas which makes it less clear to demonstrate the emulations needed for pure object-oriented languages. EIFFEL scores well above a number of other well-known general purposes lan- guages with 21 source statements per function point [Jones96] (see table 6.2). Language ADA 95 CLOS C++ EIFFEL HASKELL JAVA Level 6.50 15.00 6.00 15.00 8.50 6.00 SMALLTALK 15.00 Average source statements per function point 49 21 53 21 38 53 21 Table 6.2: Language levels JAVA would have been a very good candidate too. I expect a correction to the language level given in table 6.2. Unfortunately, until today JAVA lacks support for generic datatypes. Any generic treatment of datatypes means to create superfluous subclasses or spurious cast statements. Given this limitation, EIFFEL was the best choice to express pattern goals with the least distraction from language limitations. 92 6 Catalog Name (page) Intent Function Object (93) Encapsulate a function with an object. This is useful for parameterization of algorithms, partial parameterization of functions, delayed calculation, lifting methods to first- class citizens, and for separating functions from data. Lazy Object (115) Defer calculations to the latest possible point in time. This is useful for resolving dependencies, calculation complexity reduction, increased modularity and infinite data structures. Value Object (149) Use immutable objects with generator operations for protection against side-effects and aliasing. Transfold (163) Process the elements of one or more aggregate objects without exposing their representation and without writ- ing explicit loops. Void Value (191) Raise Nil to a first-class value. This allows to treat void and non-void data uniformly, is a way to provide default behavior, facilitates the definition of recursive methods, and enables to deal with error situations more gracefully. Translator (201) Add semantics to structures with heterogeneous ele- ments without changing the elements. Separate interpre- tations from each other and use local interpretations that allow for incremental reevaluation. Table 6.3: Functional pattern system 93 7 Function Object The secret of dealing successfully with a child is not to be its parent. – Mell Lazarus 7.1 Intent Encapsulate a function with an object. This is useful for parameterization of algo- rithms, partial parameterization of functions, delayed calculation, lifting methods to first-class citizens, and for separating functions from data. 7.2 Also Known As Lexical Closure [Breuel88], Functor [Coplien92], Agent [Hillegass93], Agent- Object [K ¨uhne94], Functionoid [Coleman et al.94], Functoid [L¨aufer95], Function- Object [Stepanov & Lee94, K ¨uhne95b, K ¨uhne97]. 7.3 Motivation Almost any software system makes use of behavior parameterization in one or the other way. Iterators are a good example. The iteration algorithm is constant whereas an iteration action or function varies. A special kind of iteration is the has (member test) function of a collection. Consider a collection of books. Member testing should cover testing for a particular book title, book author, book type, etc. 7.3.1 Problem We may implement the predicate in the collection’s elements. This works nicely if (cid:15) the element’s natural interface contains the predicate. (cid:15) the predicate implementation may vary with the element type, but we do not want to have a selection of several predicates for one element type. Composing traversal algorithm and predicate as above uses dynamic binding of the element’s predicate method. For instance, it is acceptable for a SortedList of 94 7 FunctionObject Comparable elements to defer the compare predicate to the elements [Meyer94b]. As it is the sole purpose of such a collection to transpose an element property (e.g., ordering relation) to the entire collection, it is convenient that the element type automatically provides a fixed predicate. The collection of books, however, should be sortable according to various criteria, like author, title, and date. In order to be able to change the sorting criterion at runtime, we do not want to encumber the traversal algorithm with a switch statement that would select the appropriate book compare method. One way to achieve this is to use an external iterator. Then the varying predicate (compare title, compare author, etc.) can be combined with the traversal algorithm by placing the predicate in an explicit loop that advances the external iterator one by one. As a result, the number of explicit loops corresponds to the number of member test predicates uses. However, there are good reasons to write such a loop only once (see “Write a Loop Once” [Martin94], the discussion in the Iterator pattern [Gamma et al.94] and chapter 10 on page 163). Consequently, we use an internal iterator. Given a predi- cate, it returns true if any of the books fits the predicate. Here is how the predicate is “given” to the internal iterator conventionally: The actual member test method is a Template Method [Gamma et al.94], which depends on an abstract predicate. The implementation for the abstract predicate, and thus the specific member test op- eration, is given in descendants [Madsen et al.93, Meyer94b, Martin94]. Selection of the member tests is done by selecting the appropriate descendant. So, traversal algorithm and functions in general are combined through dynamic binding of the abstract function method. Note that this forces us to place the member test method outside the collection of books (e.g., at iteration objects) since we do not want to create book collection subclasses but member test variations only. Further disad- vantages aligned with the above application of an object-oriented design, using inheritance and dynamic binding are: Static combination. All possible combinations of iteration schemes and functions are fixed at compile time. Neither is it possible to create a new function at runtime. Combinatorial explosion. Sometimes it is useful to select not just one, but a combi- nation of functions or tests and functions. With subclassing, it is not feasible to provide any independent combination, since it leads to an exponentially growing number of subclasses. Subclass proliferation. Each new function demands a new Iterator subclass. The class name space is cluttered by many concrete Iterator subclasses. We may use repeated inheritance to combine all functions in one subclass [Meyer92], but this makes things worse. First, it is non-local design to lump all functions in one class. Sec- ond, we have to apply heavy renaming for iteration schemes and functions in the subclass; any combination of iteration scheme and function must be given a dis- tinct name. Third, we lose the ability to use dynamic binding for the selection of a function. Since all functions belong to one class, we no longer can use concrete Iterator instances to select the actual combination of iteration and function. 7.3 Motivation 95 Awkward reuse. Reusing the functions for other iteration schemes or different pur- poses is practically impossible if they are defined in Iterator subclasses. The solu- tion is to extract the functions in classes of their own. But now multiple inheritance is necessary in order to inherit from Iterator and to inherit from a particular func- tion. At least multiple tests or functions can be “mixed-in”, but scope resolution is needed, and each function combination results in a combinator subclass. Note that some languages, such as SMALLTALK and JAVA do not even allow multiple inheritance. Poor encapsulation. Composing an iteration scheme and a function with inheritance joins the name spaces of both. In fact, the multiple inheritance solution causes iterator, function, and combinator class to share the same name-space. Implemen- tation changes to either of the classes can easily invalidate the other. An interface between super- and subclasses, as the private parts in C++ [Ellis & Stroustrup90], alleviates the problem considerably. As an evidence for the relevance of this prob- lem consider the programming of a dialog in JAVA. If you use an is valid flag to record the validity of the dialog fields you will not get a dialog display although the program compiles fine. It probably takes a while till you find out about a flag is valid in class Component (you happen to indirectly inherit from) which is used for recording whether a window has been displayed already [Maughan96]. Unrestricted flexibility. Creating a designated class for the combination of an iter- ation scheme and a function opens up the possibility of overriding the iteration scheme for particular actions. Explicitly counting the elements in a collection could be replaced by just returning the value of an attribute count. Unfortunately, this advantage for the designer of a library is a disadvantage for the user of a library. The user may rely on properties of the original iteration scheme. If the iteration function not only counts the elements, but in addition produces some side-effect, the side-effects will not be executed in the optimized version described above. Or, referring to the cook and recipe example of section 4.2.3.1 on page 60, an adapted cook may accompany its cooking with singing which is not always expected and desirable. Also, it is possible that the optimized version does not fully conform to the original version, not to speak of plain errors. An original robust iteration scheme [Coleman et al.94], that first evaluates all tests and then applies all func- tions, could be replaced by a standard scheme, that evaluates test and functions on each element separately. The two versions will behave differently when iteration functions side-effect neighboring elements. Pre- and postconditions [Meyer88] can help to enforce behavioral identity between iteration schemes, but problems like the above are hard to cover and checking con- tracts at runtime boils down to testing, as opposed to rigorous proofs. Identity changes. In order to change the iteration function a different iterator in- stance must be used. While one would seldom need to rely on an unchanging iterator instance, this property is inhibiting in other settings of parameterization. 96 7 FunctionObject For instance, it might be mandatory to keep the same instance of a cook, while being able to process different recipes. 7.3.2 Solution The best way to get rid of the above disadvantages is to objectify predicates with the Function Object pattern. Combining traversal algorithm and function with Func- tion Object works through literally “giving” an objectified function to an internal iterator. In our example, the member test method accepts a test predicate to be used during iteration: has(predicate : Function[Book, Boolean]) : Boolean is do from i:=1; until i>count or Result loop if predicate @ (books.item(i)) then Result:=true; end; i:=i+1; end; end Here, the collection of books simply consists of an array of books that is tra- versed with an integer loop. Note how EIFFEL allows using a nice function appli- cation syntax for passing the current book to the predicate. The operator “@” is defined as an infix operator in the common interface for all function objects: deferred class Function[In, Out] feature infix "@" (v : In) : Out is deferred end; end The @-operator is defined to take a generic argument type In and to return a generic result type Out. The member test method from above instantiates these to Book and Boolean respectively. A predicate to check for a bible instance is: class IsBible inherit Function[Book, Boolean] feature infix "@" (b : Book) : Boolean is do Result:=(b.title.is_equal("Bible")); end; end 7.3 Motivation 97 Now member testing looks like: containsBible:=library.has(isBible)); Checking for a book with a certain title can be achieved by passing a predicate that receives the book title through its constructor: !!checkTitle.make("Moby Dick"); library.has(checkTitle); The code for CheckTitle is straightforward: class CheckTitle inherit Function[Book, Boolean] creation make feature title : String; make(b : Book) is do title:=clone(b.title); end; infix "@" (b : Book) : Boolean is do Result:=(b.title.is_equal(title)); end; end Yet, there is another exciting way to achieve the same thing. Suppose the ex- istence of a predicate that compares the titles of two books, e.g., for sorting books with respect to titles. Although the member test method expects a predicate with one book parameter only we can make the two argument compare function fit by passing a book with the title to be looked for in advance: library.has(titleCompare @ mobyBook); Predicate titleCompare has two parameters. Passing mobyBook results in a predicate with one parameter that perfectly fits as an argument to the has method. Class titleCompare is easily implemented like this: class TitleCompare inherit Function[Book, Function[Book, Boolean]] feature infix "@" (b : Book) : CheckTitle is do !!Result.make(b); end; end 98 7 FunctionObject Thanks to the generic type parameters of function objects the compiler can check for correct function application and will reject wrong uses concerning number or type of arguments. Finally, we may combine multiple search criteria like title comparison and date checking by composing predicates with a composite function object: library.has(and @ pred1 @ pred2); Function object and takes a variable number of predicates as arguments, applies each of them to the book it receives, and returns true if all predicates hold. 7.4 Applicability (cid:15) Parameterization. Function objects are a good candidate whenever general be- havior can be adapted to special behavior: Dynamics. In addition to runtime selection of existing function objects, new function objects can also be created at runtime. A user may dynamically com- pose a multi-media function object from text-, graphic-, and sound-producing function objects. Orthogonality. Having more than one behavior parameter creates the problem of handling all possible combinations of the individual cases. Function objects can freely be mixed without interfering and without combinator classes. Reuse. Function objects can be used by any adaptable algorithm that knows their interface. Even if the algorithm was not designed to supply a function with mandatory arguments, it is often possible to supply them to the func- tion in advance. Consider an error reporter, parameterized by output format functions, only intended for generating text messages. We can upgrade the re- porter to create a graphical alert-box by passing a function object that already received information about box-size, colors, etc. Identity. When the behavior of an object should change while keeping its identity, function objects can be used as behavior parameters to the ob- ject. In contrast, encoding behavior in subclasses calls for something like SMALLTALK’s “become:” in order to achieve the same effect. (cid:15) Business transactions. Often the functions are the stable concepts of a system and represent good maintainance spots, in order to cope with changing func- tionality. Instead of being a well-defined operation on one single object, transactions are “an orchestration of objects working together toward a common goal” [Coplien92]. When transactions do not naturally fit into existing data ab- stractions, Function Object can lift them to first-class status while providing a uniform function application interface. Functions may even form a hierarchy, just like algorithms, for classification and reuse purposes [Schmitz92]. 7.4 Applicability 99 (cid:15) Monolithic algorithms. Just like Strategy [Gamma et al.94] Function Object can be used to define localized algorithms for data structures. When a data struc- ture is stable, but the operations on it often change, it is not a good idea to use the standard object-oriented method to distribute the operations over the object types involved. For instance, each change or addition of an algorithm on abstract syntax trees (such as typeCheck, compile) demands a change in all node-object types. In addition, it is not possible to exchange the operations at runtime. If we turn the algorithm into a Function Object, we must use a generic Func- tion Object (see section 7.10 on page 107) to dispatch on the node types, but in analogy to the Strategy pattern [Gamma et al.94], (cid:15) the algorithm logic is localized, (cid:15) a function’s state can accumulate results, and (cid:15) we can dynamically choose an algorithm. See section 7.12 on page 111 for a comparison of Command, State, Strategy, and Function Object. (cid:15) Small interfaces. When an object potentially supports many extrinsic opera- tions (e.g., CAD-objects may support different viewing methods, cost- and stability calculations, etc.), but its interface preferably should contain the ba- sic, intrinsic functions only (e.g., geometric data), then the functionality can be implemented in function objects that take the object as an argument. The MVC-framework is an example for separating views from data structures though presentation might be considered to be the model’s responsibility at first glance. (cid:15) Method simplification. If a large method, containing many lines of code, can- not be split into smaller, more simple methods, because the code heavily uses temporary variables for communication, then the method can be transformed into a function object. The main transformation is to replace the temporary variables with function object attributes. As a result, the method can be split up into more manageable sub-methods, without passing parameters between inter-method (but still intra-object) invocations, since communication still can take place via function object attributes. The main computation method sim- ply puts the pieces together, itself being as clear as documentation [Beck96]. (cid:15) Symmetric functions. Some functions do not need to privilege (break the en- capsulation of) one of their arguments, by making it the receiver of a method. For instance, checking whether a Tetris piece hits something in the shaft can equally be implemented by Piece or Shaft. We are even forced to introduce a free function if the source code of neither class is available. Likewise, basic types are often not extendible. Note that the free function replaces a mutual 100 7 FunctionObject dependency between two classes by two classes depending on the free func- tion. If the two classes belong to separate large clusters, we decoupled the clusters with regard to change propagation and recompilation. (cid:15) Delayed calculation. Function objects postpone the calculation of their result until it is actually needed. When a function object is passed as a parameter but the receiving method does not make use of it, it will not be evaluated. If the result is never needed, this pays off in run time efficiency. Function Ob- ject effectively supports lazy evaluation and thus can be used to implement infinite data structures and supports modularization by decoupling data gen- eration from data consumption [Hughes87]. Trying to achieve this with methods establishes a function object one way or the other. We may delay method invocation by passing the object that sup- ports the method. Then we use the object as a closure with the disadvantage of non-uniform method names. When we allow the client to call the method, although the client might not really need the result, the method must produce an object which stands for the calculation of the result. This is precisely the case of returning a Function Object. Do not use Function unless you have a concrete reason. There is a time and space penalty in creating and calling a function object, instead of just invoking a method. Also, there is an initial effort to write an extra function class. Functions with many parameters require just as many class definitions in order to exploit partial parameterization. Finally, sometimes unrestricted flexibility as mentioned in section 7.3.1 on page 93 is clearly desirable. Functions that vary with the imple- mentation of their argument should be members of this abstraction and not free function objects. Also, see the remarks concerning flexibility and efficiency in sec- tion 7.8 on page 102. 7.5 Structure See figure 7.1 on the next page. 7.6 Participants (cid:15) Function – declares an interface for function application. (cid:15) ConcreteFunction (e.g., isBible) – implements a function. – collects argument values until evaluation. 7.7 Collaborations 101 Client Invoker Function application(argument) ConcreteFunction constructor(initial args) application(argument) collected arguments Figure 7.1: Function Object structure diagram (cid:15) Client – creates or uses a ConcreteFunction. – possibly applies it to arguments. – calls Invoker with a ConcreteFunction. (cid:15) Invoker (e.g., Iterator) – applies ConcreteFunction to arguments – returns a final result to its Client. 7.7 Collaborations (cid:15) A client creates a function and possibly supplies arguments. (cid:15) An invoker takes the function as a parameter. (cid:15) The invoker applies the function to arguments. (cid:15) The client receives a result from the invoker. Client and invoker do not necessarily have to be different objects. A client may evaluate a function object itself if no further arguments are needed or it does not need an invoker to determine the point of evaluation. If an invoker does not supply further argument but uses a dummy argument only to evaluate the function object than we have an application of the Command pattern. 102 7 FunctionObject aClient aFunction anInvoker Creation make(initial args) Parameterization invokerMethod(aFunction) Application apply(arg) Figure 7.2: Function Object interaction diagram 7.8 Consequences (cid:15) Abstraction. Function objects abstract from function pointers and in particular from pointers to methods [Coplien92]. Instead of the C++ code: aFilter.*(aFilter.current)(t); we can write aFilter(t); (cid:15) Simplicity. The use of function objects does not introduce inheritance relation- ships and does not create spurious combinator classes. (cid:15) Explicitness. The code cook.prepare(fish) is easy to understand. When recipes are wired into Cook subclasses, cook.prepare depends on the Clever variable names (e.g., fishCook.prepare) actual Cook type. often are not an option, e.g., cook.prepare(fish), followed by cook.prepare(desert). Note that the client must know the function object. A variability on the im- plementation — rather than the behavior — of Cook is better treated with a Bridge [Gamma et al.94]. Clients of Cooks should not know about implemen- tation alternatives, unless they explicitly want to decide on time and space behavior. (cid:15) Compositionality. In analogy to Macro-Command [Gamma et al.94], function objects can be dynamically composed to form a sequence of functions by forwarding intermediate results to subsequent function objects. Composite function objects may also apply several component function objects in paral- lel. Composite function object variants differ in the way they produce a final output from the single results. 7.8 Consequences 103 (cid:15) Uniform invocation. Imposing a function object’s interface on related oper- ations allows uniformly invoking them. Instead of switching to different method names (like compTitle and compAuthor), we evaluate an abstract func- tion object and rely on dynamic binding [Meyer88, Gamma et al.94]. Conse- quently, we can add new operations, without changing the caller (e.g., event handler). Whenever specific evaluation names (e.g., evaluate, solve, find) are consid- ered important, then they can be provided as aliases. (cid:15) Encapsulation. As function objects establish client relationships only, they are protected from implementation changes to algorithms that use them. function objects can change without Likewise, invalidating the algorithms. Hence, Function Object allows black-box reuse [Johnson & Foote88] and helps to advance reuse by inheritance to reuse by composition [Johnson94]. the implementation of (cid:15) Security. A client of an adaptable algorithm can be sure to use a fixed algo- rithm semantics. It is impossible to be given an optimized version which does not fully comply to the original semantics (see 7.3.1 on page 93, Unrestricted flexibility). (cid:15) Flexibility. + A statement like iterator.do(f) is polymorphic in three ways: 1. Iterator may internally reference any data structure that conforms to a particular interface. 2. The actual instance of Iterator determines the iteration strategy (e.g., pre- or post-order traversal on trees). 3. The actual instance of f determines the iteration function. − It is not possible to automatically optimize algorithms for specific func- tions. Nevertheless, one more level of indirection can explicitly construct optimized combinations of functions and algorithms. − As discussed in section 4.2.3.1 on page 60 black-box composition does not allow adapting, e.g., cooks for new recipes. Often, it is nonetheless possible to use new recipes with unaltered cooks by partial application of recipes. If a new class of recipes needs the room temperature as an argument (which cooks do not supply) it is possible to curry the recipe with the room temperature in advance. Thus, the new recipe class looks just like the old ones to cooks (see how keyword parameters boost this option in section 7.10 on page 107). − When a function has been extracted from a data structure, it is no longer possible to simply redefine it in future derivations. One way to account for this is to make the extracted function a generic Function Object (see section 7.10 on page 107) that will discriminate between data structure variations. 104 7 FunctionObject (cid:15) Separation. The ability to carry data (e.g., alert box size) enables function ob- jects to operate on data from outside an (e.g., error-reporter) algorithm as well as (e.g., error text) data from inside the algorithm. The data from outside the algorithm can be local to the algorithm caller. There is no need to communi- cate via global data. Since function objects can combine local data spaces, they allow decoupling data spaces from each other while still supporting commu- nication. (cid:15) Reuse. Adaptable algorithms become more reusable because they do not need to know about additional parameters for functions. Moreover, function objects are multi-purpose: + Functions can easily be used for different iteration strategies. They are are not bound to a particular adaptable algorithm, e.g., comparing book- titles is useful for sorting and for membership testing in collections. + One function with n parameters actually represents n functions and one value. The first function has n parameters. The second, created by ap- plying the first to an argument, has n − 1 parameters, and so on, until the last function is applied to an argument and produces the result1. An example from physics shows the useful functions which can be cre- ated from the gravitational force function [Leavens94]: GravityLaw m1 r m2 = G m1 m2 r2 forceearth = GravityLaw massearth forcesurface = forceearth radiusearth forcemy = forcesurface massmy (cid:15) Iteration. Function Object suggests the use of internal, rather than external, it- eratorsiterator. Internal iterators avoid explicit state and re-occurring explicit control loops. Often external iterators are promoted to be more flexible. It is said to be practically impossible to compare two data structures with an in- ternal iterator [Gamma et al.94]. However, the Transfold pattern on page 163 simply extends an iterator to accept not just one, but n data structures. A transfold-method accesses the first, second, etc., elements of all data struc- tures simultaneously. Function Object allows making iteration a method of data structures since it does not demand for subclassing the data structure. This facilitates the use of iterators and allows redefining iteration algorithms for special data struc- tures. Moreover, the data structure (e.g., Dictionary) then does not need to export methods (e.g., first, next) in order to allow iterators to access its ele- ments. 1Which could again be a function. 7.9 Implementation (cid:15) Efficiency. 105 + A function object may calculate partial results from arguments and pass these to a result function. Hence, the partial result is computed only once, no matter how many times the resulting function object will be applied to different arguments in the future, e.g., forcesurface = forceearth costlyCalculation and then forcemy = forcesurface massmy forceyour = forcesurface massyour: − Passing client parameters to function objects can be more inefficient than In to, e.g., directly access internal attributes of an iterator superclass. principle this could be tackled by compiler optimizations [Sestoft88]. − Care should be taken not to unnecessarily keep references to unevalu- ated calculations, i.e., function objects. Otherwise, the occupied space cannot be reclaimed. − Finally, function objects access the public interface of their servers only. This is why Symmetric functions (see section 7.4 on page 98) enforce the encapsulation of their arguments. This represents positive decoupling, but can be more inefficient than unrestricted access. However, selective export (EIFFEL) or friends (C++), allow trading in efficiency for safety. − It is not possible to apply partial evaluation techniques to clo- sures [Davies & Pfenning96]. Hence, function objects typically cannot be evaluated before runtime. However, this is just a price to pay for flexibility. Dynamic binding introduces runtime overhead exactly for the same reason. 7.9 Implementation (cid:15) Creation. Quite naturally, function objects must be created before they can be used. A typical code fragment shows the overhead in notation to create the object: ... local times : Times[Integer]; do !!times; Result:=times @ 6 @ 7; end; 106 7 FunctionObject The fragment coincidentally also illustrates the danger in forgetting the cre- ation instruction, causing a runtime exception. Fortunately, EIFFEL enables the useful idiom of declaring a variable of expanded type, i.e., rendering the creation instruction unnecessary. With — ... local times : expanded Times[Integer]; do Result:=times @ 6 @ 7; end; — the declaration already takes care of providing times with a value. No sharing of function objects can occur since the first application of times cre- ates a new instance anyway (see 7.12.3 on page 113, Prototype). (cid:15) Call-by-value. Function objects must copy their arguments. Otherwise, their behavior will depend on side-effects on their arguments. In general, this would produce unpredictable results. In some cases, however, it may be de- sirable. The function object then plays the role of a future variable, which is passed to an invoker before all data needed to compute the result is avail- able. Long after the function object has been passed to the invoker, it can be supplied with the necessary data by producing side-effects on arguments. (cid:15) Initial arguments. Real closures (e.g., SMALLTALK blocks) implicitly bind vari- ables, which are not declared as parameters, in their creation environ- ment. This is not possible with function objects. One way out is to treat initial arguments and standard arguments uniformly through lambda lift- ing [Field & Harrison88]: Initial arguments are passed as arguments to the function object in its creation environment. Alternatively, it is possible to pass initial arguments through the function ob- ject constructor (see CheckTitle in section 7.3.2 on page 96). This saves the intermediate classes needed to implement the partial application to initial ar- guments. Of course, both variants do not exclude each other. Note, however, that explicit binding is equivalent to implicit binding. This follows from the reverse application of b -reduction. Yet, the implicit variable binding of real closures forces them to use the the same variables names as its creation environment. In contrast, a function object can be used in various environments without the need to make initial argument names match the environment. (cid:15) Delayed calculation. Commonly a function is evaluated after it has received its last argument. Yet, function object application and evaluation can be sepa- rated by corresponding methods for application and evaluation. As a result, the client, triggering evaluation, does not have to know about the last argu- ment. Also, the supplier of the last argument does not need to enforce the 7.10 Function Objectvariants 107 calculation of the result, which is crucial for lazy evaluation. In order to en- able automatic evaluation on full argument supply while still supporting the above separation, it is possible to evaluate on the last parameter and use a kind of dummy parameter (called unit in ML [Wikstr ¨om87]). So function objects that separate their evaluation from the supplement of the last parameter (e.g., Call-back functions) are defined with a dummy parame- ter, which is supplied by the evaluator. (cid:15) Interface width. As already mentioned in section 7.8 on page 102 function ob- jects may force their argument classes to provide access to otherwise non- public features. If a particular function object is closely coupled to a specific object type, then declaring the function object as the object’s friend, i.e., us- ing selective export, allows efficient access to internal data nevertheless. As a result the object can keep its public interface to other clients to a minimum. (cid:15) Partial parameterization. Two extremes to implement partial parameterization exist. The less verbose is to always keep the same instance of function ob- ject and assign incoming arguments to corresponding internal attributes. This will cause trouble when the same function object is used by several clients. As the application of a function object does not create a new instance, the clients will get confused at the shared state. Furthermore, static typing becomes im- possible. If each application of a function object produces a new instance of a different type, then static typing is enabled again and no unwanted sharing of function object state can occur. Unfortunately, this forces us to write at least n − 1 classes for a function object with n parameters. Note, that it is not necessary to provide application methods, that allow pass- ing multiple arguments at once. This would only produce even more imple- mentation overhead and can easily replaced by passing the arguments one by one. (cid:15) Covariance. In principle, EIFFEL allows using a non-generic function interface since the application method can be covariantly redefined. A general ancestor would have the type Any to Any and descendents could narrow input and out- put types. However, we recommend to use a generic interface as presented in order to avoid complications2 through the combination of polymorphism and covariant redefinition, i.e., catcalls [Meyer96]. 7.10 Function Object variants This section shortly touches upon important extensions to Function Object. (cid:15) Keyword Parameters. In addition to standard application, function objects may provide keyword parameters. Accordingly, parameters can be passed in any 2Until today no EIFFEL compiler implements system-level validity checking, which is necessary to catch type errors resulting from mixing argument covariance with polymorphism. 108 7 FunctionObject order. This makes sense, in order to create useful abstractions. If the definition of the gravitational force in the example of section 7.8 on page 102 had been GravityLaw m1 m2 r = G m1 m2 r2 ; (note the different order of parameters) we cannot define: forcesurface = GravityLaw massearth radiusearth:3 With keyword parameters, notwithstanding, we can write: force:=gravityLaw.m1(earthMass).r(earthRadius); Keyword parameters effectively extend currying to partial application. Be- yond the reusability aspect above, however, they also enhance the clarity of programs: Consider the initialization of a node with a label and an identifica- tion. The code node.make(a, b); does not tell us what parameter plays which role. The meaning is hidden in the position of the arguments. Surely, we should use more expressive variable names but these should reflect the meaning in their environment, e.g.: node.make(name, counter); In other words, we use a name and a counter to initialize a node but still are confused about the argument meanings. Only the version using keyword parameters — node.label(name).id(counter); — documents the meaning of arguments without compromising the docu- mentation of the environment variable meanings. In another case a node is possibly created using a number string and a random number which should be visible in the initialization statement. Keyword parameters respect that there is both an inner and an outer meaning for parameters and that both meanings are important to document. (cid:15) Default Parameters. A keyword parameter does not need to be mandatory. Recipes may be designed to work for four persons by default but allow a optional adaption. So, both cook.prepare(dinner); and 3Functional programmers know this problem and use the flip function to exchange the order of (howbeit only) two arguments. 7.10 Function Objectvariants 109 cook.prepare(dinner.people(2)); are valid. Default parameters in general have been described as the design pattern Convenience Methods [Hirschfeld96]. Unix shell commands are an example for the application of both default- and keyword parameters. (cid:15) Imperative result. It is possible to use the internal state of a function object to calculate one or multiple results (add accumulated results to ConcreteFunction in the structure diagram of section 7.5 on page 100). For instance, one func- tion object may count and simultaneously sum up the integers in a set during a single traversal. The set iteration client must request the results from the function object through an extended interface (add an arrow getResult from aClient to aFunction in the diagram of section 7.7 on page 101). Note that im- perative function objects may produce any result from a standard traversal algorithm. The latter does not need any adaption concerning type or whatso- ever. (cid:15) Procedure Object. If we allow function objects to have side effects on their arguments we arrive at the Command pattern extended with parameters and result value. Procedure Object makes methods amenable to persistent command logging, command histories for undoing, network distribution of commands, etc. Like Command, Procedure Object may feature an undo method, which uses information in the procedure object’s state to undo op- erations [Meyer88, Gamma et al.94]. Note how easy it is to compose a pro- cedure object, to be used as an iteration action, with a function object pred- icate that acts as a sentinel for the action. As a result, special iterations as “do if” [Meyer94b] can be replaced with a standard iteration. (cid:15) Multi-dispatch. Sometimes an operation depends on more than one argument type. For instance, adding two numbers works differently for various pairs of integers, reals, and complex numbers. Simulating multi-dispatch with standard single-dispatch [Ingalls86] results in many additional methods (like addInteger, addReal). The dispatching code is thus distributed over all in- volved classes. If, as in the above example, the operation must cope with a symmetric type relation (e.g., real+int & int+real), each class has to know all other argument types. A generic4 function object removes the dispatching code from the argument types and concentrates it in one place. It uses runtime type identification to select the correct code for a given combination of argument types. As such, it is not simply an overloaded Function Object, which would statically resolve the types. Note that nested type switches can be avoided with partial parameterization: Upon receipt of an argument, a generic function object uses one type switch 4Named after CLOS’ [DeMichiel & Gabriel87] generic functions. 110 7 FunctionObject statement to create a corresponding new generic function object that will han- dle the rest of the arguments. Unfortunately, the necessary switch statements on argument types are sensi- tive to the introduction of new types5. Yet, in the case of single-dispatch sim- ulation, new dispatching methods (e.g., addComplex) are necessary as well. The goals of the Visitor pattern [Gamma et al.94] can be achieved with a com- bination of generic Function Object and any iteration mechanism. A generic function object chooses the appropriate code for each combination of opera- tion and element type. Once the generic Function Object has done the dis- patch, the exact element type is known and access to the full interface is pos- sible. Between invocations, function objects can hold intermediate results, e.g., variable environments for a type-checking algorithm on abstract syntax nodes. A generic function object may even be realized as a type dispatcher, parame- terized with a set of function objects that actually perform an operation. This allows reuse of the dispatching part for various operations. Visiting data structures with Function Object is acyclic w.r.t. data dependen- cies [Martin97] and does not force the visited classes to know about visi- tors [Nordberg96]. Finally, a generic function object is not restricted to dispatch on types only. It may also take the value of arguments into consideration. Consequently, one may represent musical notes and quarter notes by the same class. The corresponding objects will differ in a value, e.g., of attribute duration. Nev- ertheless, it is still possible to use a generic function to dispatch on this note representation. 7.11 Known Uses Apart from the uncountable uses of Function Object in functional programming and SCHEME [Abelson & Sussman87], there are many truly object-oriented uses: SMALLTALK [Goldberg & Robson83] features blocks as true closures with implicit binding of free variables. SATHER provides bound routines [Omohundro94]. AL- GOL 68’s thunks for call-by-name parameter passing are also closures but bind free variables dynamically in the calling environment. JAVA 1.1 features “inner classes” which are essentially (anonymous) closures used, e.g., for callbacks [Sun97]. The Eiffel Booch Components uses Function Object for searching, sorting, transforming and filtering of containers [Hillegass93]. The Standard Template Library, which was adopted as part of the standard C++ library, uses Function Object to inline operations for arithmetic, logic, and comparison [Stepanov & Lee94]. 5A more flexible approach is to use dynamically extendible dictionaries that associate types with code. 7.12 RelatedPatterns 111 7.12 Related Patterns 7.12.1 Categorization Objectifier: A function object, like Objectifier, does not represent a concrete object from the real world [Zimmer94], though one can reasonably take business- transactions for real. Function Object is very similar to Objectifier, in that it objectifies behavior and takes parameters during initialization and call. Per contra, clients “have-an” objectifier, while clients “take-a” function object. The latter is a uses, not a has-a relationship. Command: A procedure object which does not take any arguments after cre- ation and produces side-effects only boils down to the Command pat- tern [Gamma et al.94]. One key aspect of Command is to decouple an invoker from a target object. Function objects typically do not delegate functionality. Rather than delegating behavior to server objects they implement it them- selves. So, function objects normally do not work with side-effects, but return their computation as an argument-application result. Nevertheless, function objects also can be used for client/server separation, i.e., as Call-back func- tions. In addition to Command, invokers are then able to pass additional information to function objects by supplying arguments. State, Strategy: Function Object, State, and Strategy [Gamma et al.94] are concerned with encapsulating behavior. A decision between them can be based on concerns such as: (cid:15) Who is responsible for changing the variable part of an algorithm? The State pattern manages the change of variability autonomously. Function objects are explicitly chosen by the client. Strategies are chosen by the client also, but independently of operation requests. Requesting an operation can rely on a Strategy choice made some time earlier. (cid:15) Is it feasible to impose the same interface on all variations? the available Strategies range from simple to complex, the If abstract Strategy must inter- face [Gamma et al.94]. Function Object avoids this by partial param- eterization. the maximum parameter support (cid:15) Does the combination of common and variable part constitute a useful concept? The State pattern conceptually represents a monolithic finite state ma- chine, so the combination of standard- and state-dependent behavior makes sense indeed. Strategies are a permanent part of general behav- ior and thus provide default behavior. Here, the combination acts as a built-in bookkeeping for the selection of the variable part. Function ob- jects, however, take part in the “takes-a” relation. A function object and 112 7 FunctionObject its receiver are only temporarily combined in order to accomplish a task. Function objects, nevertheless, can have a long lifetime, such as being memorized in attributes or representing unevaluated data. Visitor: Data structures need to know about Visitors because they have to provide an accept method [Gamma et al.94]. Sometimes this is undesirable because of the so-introduced mutual dependency between data structures and Vis- itors [Martin97]. When the data structure is not available as source code, it is even impossible to add the accept method. A combination of Iterator and generic Function Object (see also the Transfold pattern in chapter 10 on page 163) avoids these drawbacks, while providing the same functionality as Visitor: (cid:15) It frees the data structures from needing to provide the operations them- selves. (cid:15) It differentiates between types in the data structure. The generic function object chooses the appropriate code for each combination of operation and element type. (cid:15) It allows heterogeneous interfaces on the data elements. Once the generic function object has done the dispatch, the exact element type is known and access to the full interface is possible. (cid:15) It concentrates operations at one place and provides a local state for them. Between invocations, function objects can hold intermediate re- sults, e.g., variable environments for a type-checking algorithm on ab- stract syntax nodes. Of course, the type switches are sensitive to the addition of new structure objects. However, if the structure is unstable, Visitor is not recommended either [Gamma et al.94]. Lazy Object: An unevaluated, i.e., still parameter awaiting function object is a lazy object (see chapter 8 on page 115). It represents a suspended calculation and may, therefore, be used for delaying calculations. 7.12.2 Collaboration Iterator: Function objects allow the use of data from inside (elements) and outside the collection (previous arguments). There is no collaboration between Com- mand and Iterator, since Command does not take arguments. Adapter: Function Object extends the use of Parameterized adapters as described in the implementation section of the Adapter pattern [Gamma et al.94] from SMALLTALK to any object-oriented language. 7.12 RelatedPatterns 113 Chain of Responsibility: Pairs of test- (check responsibility) and action function ob- jects can be put into a Chain of Responsibility in order to separate responsibil- ity checks from the execution of tasks. Function Object allows replacing the inheritance relationship between Links and Handlers [Gamma et al.94] with object-composition. Observer: Instead of receiving a notification message from a subject an observer may register call-back procedure objects at the subject that will perform neces- sary updates. This scheme adds another variant to the push- and pull- models of the Observer pattern. Void Value: A void value (see chapter 11 on page 191) may define the default func- tion to be used for initializing a parameterized structure. Void Value, Value Object: A function object does not have to copy void value (see chapter 11 on page 191) or value object (see chapter 9 on page 149) arguments because they ensure immutability anyway. 7.12.3 Implementation Composite: Standard and composed function objects can be uniformly accessed with the Composite pattern [Gamma et al.94]. A composite function forwards arguments to its component functions. A tuple-Composite applies all func- tions in parallel to the same argument and thus produces multiple results. Several reduce functions (e.g., and of section 7.3.2 on page 96) may somehow fold all results into one output. A pipeline-Composite applies each function to the result of its predecessor and thus forms a calculation pipeline. Prototype: Often it is useful to distribute the accumulated state of a function object to different clients. For instance, a command for deleting text can capture the information whether to ask for confirmation or not. However, when placed on a history list for undoing, different commands must maintain different pointers to the deleted text. Consequently, Prototype can be used to clone pre-configured function objects which should not share their state any fur- ther. With this regard, any partially parameterized function object can be in- terpreted as a prototype for further applications (see example about the costly calculation in section 7.8 on page 102, Efficiency). Chain of Responsibility: Generic Function Object can employ a Chain of Responsibil- ity for argument type discrimination. Chain members check whether they can handle the actual argument type. This enables a highly dynamic exchange of the dispatch strategy. 114 7 FunctionObject 115 8 Lazy Object Ungeduld hat h¨aufig Schuld. – Wilhelm Busch 8.1 Intent Defer calculations to the latest possible point in time. This is useful for resolving dependencies, calculation complexity reduction, increased modularity and infinite data structures. 8.2 Also Known As Stream [Ritchie84, Abelson & Sussman87, Edwards95], Pipes and Filters Architec- ture [Meunier95b, Buschmann et al.96], Pipeline [Posnak et al.96, Shaw96]. 8.3 Motivation Typically, the specification of a solution to a problem inevitably also makes a state- ment about the order of solution steps to be applied1. In this context, it does not matter whether order is expressed as a sequence of events in time or as a chain of data dependencies. The crucial point is that we welcome to be able to order sub- solutions in order to decompose problems into easier sub-problems. But sometimes overstating the order of events yields complex systems. 8.3.1 Problem Listening to music at home usually amounts to the problem of converting a pit structure on a flat disc to an alternation in air pressure. In this example we concen- trate on the part to be accomplished by the compact disc player (see figure 8.1 on the next page). We may think of the surface-reading-unit (SRU) as some electronic circuit that controls the spinning speed of the disc, tracks the laser, and produces a digital bit 1Note, however, that especially the logical paradigm allows being very unspecific about order. 116 C D 8 LazyObject Surface Reading Unit Digital/ Analog Converter to Amplifier Figure 8.1: Schematic CD-player stream. These bits are actively fed into the digital-analog-converter (DAC). Let us assume the DAC has a small buffer which it permanently consumes and resorts to during reading problems. Hence, the SRU has to watch the buffer for underflow — causing speed up of reading — and for overflow — resulting in slowing down reading. In this scenario, both SRU and DAC modules are closely coupled. The SRU pushes bit per bit into the DAC but always requests the DAC for its buffer condition. Any alternative SRU to be used in the future must know about the buffer query interface of the DAC module. Vice versa, a future DAC replacement must accept bits through a protocol as specified by the current SRU. This coupling limits the freedom in designing future implementations. Furthermore, it will not easily be possible to insert bit processing modules inbe- tween SRU and DAC (see figure 8.2). Provided we still want to keep the bit buffer in the DAC module, the error correction unit (EC) must fake a buffer protocol for the surface reading unit and the DAC buffer condition must be fed back through all components. 8.3.2 Solution Instead of handshaking the bits through a “push and request” procedure call pro- tocol, we better recognize the data flow architecture of the schematic compact disc player. We should observe that a unit either produces (SRU), transforms (EC and DF), or consumes (DAC) a stream of bits (see figure 8.2). Surface Reading Unit Error Correction Digital Filter Digital/ Analog Converter Figure 8.2: Plug-in modules in a bitstream architecture With this view, it is most natural to let the DAC request further bits from its preceding unit whenever its (self managed) buffer runs out of data. Driven by the demand of the DAC each unit generates bits, possibly by pulling further data from its preceding unit. In this scenario the modules are much less coupled since buffer management is solely accomplished by the DAC itself. The interface between the 8.3 Motivation 117 modules consists of the stable notion of a bitstream. Ergo, alternate implementa- tions or future extensions (like a digital deemphasize unit) can be easily exchanged, shuffled, and inserted. Data sources and transformers are lazy in that they output data only if required by a data sink. Compare this to the model before, where the SRU pushed data to the DAC while managing the DAC’s buffer load. A lazy data source does not need to know about the existence and nature of a receiving unit at all. Moreover, the compact disc’s control panel now needs to communicate with the DAC only. Instead of directing a press of the stop button to the SRU it now informs the DAC. Control panel and DAC communicated before also (e.g., for digital volume control), therefore, the cohesion between control panel and DAC increased while the coupling of control panel to other units decreased. With the data driven approach, the architecture can now be divided into layers that depend on each other in one direction only. We were able to decouple a data source and a data sink since we abandoned the idea of explicitely handshaking each single piece of information and adopted the notion of a stream, representing all information that is still to come at once. John Hughes uses a similar example of separating an algorithm to compute approxima- tions to numerical solutions from the controlling issues like relative or absolute pre- cision reached [Hughes87]. By representing successing approximations as a stream between generator and controlling module he was also able to achieve a higher degree of modularization. Without lazy evaluation we are often forced to either sacrifice modularity or efficiency. Suppose, we want to feed a server object with an argument whose eval- uation requires considerable effort: server.update(costlyCalculation(a, b)); The server does not always need the argument to perform its update operation. With a lazy calculation everything is fine. With a standard calculation that is al- ways performed, however, we either accept unnecessary calculations or defer the calculation to the server: server.calculateAndUpdate(a, b); While the efficency issue is apparently solved since the server can decide to do the calculation or not, modularity was severely hurt. 1. The server now contains a calculation that is probably alien to the abstrac- tion it previously represented. 2. The server now depends on two argument types that it did not need to know before. It is amazing that the initial version above manages to combine both modularity (separation of concerns) and efficiency (avoiding unnecessary calculations). A lazy attitude is of special importance when a (possibly infinite) stream is gen- erated by referring to itself. A popular example are the so-called hamming num- bers. They are defined as the sorted list of all numbers with prime factors two, 118 8 LazyObject three, and five [Bird & Wadler88]. A functional program to generate all hamming numbers — as given by definition 8.1 — clearly shows that hamming numbers can be generated by depending on themselves. hamming = 1 : merge (map ((cid:3) 2) hamming) (8.1) (merge (map ((cid:3) 3) hamming) (map ((cid:3) 5) hamming)) merge (x : xs) (y : ys) = x : merge xs ys ; x = y merge (x : xs) (y : ys) = x : merge xs (y : ys); x < y merge (x : xs) (y : ys) = y : merge (x : xs ) ys); y < x Figure 8.3 shows the unfolding of function calls into a sequence of numbers. hamming = 1 : merge(map (* 2) , merge((map (* 3) , map (* 5) )) hamming = 21 : : merge(map (* 2) , merge((map (* 3) , map (* 5) )) hamming = 1 : 32 : : merge(map (* 2) , merge((map (* 3) , map (* 5) )) hamming = 1 : 2 : 43 : : merge(map (* 2) , merge((map (* 3) , map (* 5) )) hamming = 1 : 2 : 3 : 4 : 5 : merge(map (* 2) , merge((map (* 3) , map (* 5) )) hamming = 6:5:4:3:2:1 : merge(map (* 2) , merge((map (* 3) , map (* 5) )) Figure 8.3: Generating hamming numbers Each of the three prime factor multipliers (map invocations) maintains its own access point to the already generated list of numbers. Note that the recursive calls 8.4 Applicability 119 of map to the remaining hamming numbers must be suspended until a sufficient amount of numbers has been generated. If recursive calls referred to function calls again, rather than access already generated concrete numbers, nothing at all would be generated due to an infinite recursion on function calls. This observation also emphasizes that in order to make evaluation really lazy — as opposed to just non- strict — we also need to cache generated results. Any element should be calculated only once, no matter how often accessed. In an object-oriented implementation this memoization effect is achieved by maintaining caches and/or replacing suspension objects with value objects. While streams have extensively been used as an example for lazy objects, this should not create the impression that Lazy Object is about streams only. Simply put, streams are just lazy lists. All the same, we may have lazy trees or whatever type of lazy structure. A lazy tree could be the result of a depth first search traversal of a graph. Such a tree would traverse only as much of the graph as needed for the tree part actually accessed [King & Launchbury93]. A lazy object is not even required to represent a collection. Any operation that can reasonably be delayed is a candidate to be become a method of a lazy object. 8.4 Applicability (cid:15) Unknown demands. Often, the amount of certain data needed is not known before that data is actually accessed. We might, for instance, repeatedly ac- cess fibonacci numbers in order to achieve an evenly workload distribution for the parallel evaluation of polynomials. An upper bound is difficult to es- timate and we do not want to waste time and space for providing fibonacci numbers that will never be accessed. The solution is to generate them lazily, i.e., calculate them on demand. Note that although we sometimes might be in a position to provide enough space for some data to be calculated in advance, nevertheless infinite data (like fibonacci numbers) would force us to fix an artificial upper bound. (cid:15) Up-front declaration. The freedom of not needing to care about unnecessary evaluations allows the adoption of a declarative style of programming. It is possible to decompose a routine’s arguments or make recursive calls to parts of them without concern whether the results will be needed [Jeschke95]. This style is clearer if the routine needs to multiply access the results. Otherwise, the repeated decomposition code might even require a distributed cache man- agement, if it is not known which branch — of several — will cause the first evaluation and calculations should be made at most once. (cid:15) Dependency specification. Execution of lazy functions follows dynamic data dependencies. As a result, even statically circular definitions can be specified, without regard for execution order. For instance, a machine code generator needs to determine the destination address for a forward jump. At the time the forward jump is generated, the destination address is not known. A lazy 120 8 LazyObject function does not care because it will not evaluate until the destination ad- dress is actually inspected (e.g., by accessing the code after its generation). At this time, however, the forward jump can be completed since code generation either already proceeded beyond the destination address or it will be forced to do so by the inspection of the forward address. Similarly, one may use an implicit evaluation order for the generation of a wavefront in a table. All that is needed are the constant values for the table borders and the main equation, e.g., ai j = ai−1 j + ai−1 j−1 + ai j−1 [Hudak89]. (cid:15) Partial evaluation. When a composed operation, such as retrieving all inter- face information from a programming language library, is evaluated lazily, this may amount to a partial execution of the operation only. When a compiler — for a given source code — does not need to look at the whole library infor- mation, because only a part of the library was used, then it will explore only as much interface information as needed [Hudak & Sundaresh88]. Unneeded information will not be retrieved from disc nor extracted from archives. (cid:15) Module decoupling. When modules should exhibit loose coupling but, never- theless, an interleaved inter module execution is desirable, then use a stream to connect the modules. For instance, a parser can be connected to a scanner through a stream of tokens. Parser and scanner will alternate their execution, driven by the parser’s demands. (cid:15) Co-routines. Co-routines basically represent suspended behavior. The same effect can be achieved by using a stream generating all co-routine states in succession. As with co-routines, clients do not need to care about state man- agement and behavior scheduling which all happens behind the scenes. Do not apply Lazy Object, in case of (cid:15) Strict ordering. If one needs to rely on a fixed evaluation order (e.g., involving side-effects), a lazy evaluation strategy is inappropriate. Lazy evaluations are data driven and may vary their execution order from application to applica- tion. (cid:15) Limited memory. Suspensions of function calls may claim substantial amounts In the unlikely case (since object-oriented programming of heap memory. normally implies heap oriented memory use, anyway) you need an emphasis on a stack based memory allocation, you will not be able to draw from the benefits of Lazy Object. (cid:15) Time constraints. Lazy evaluation may cause unexpected and unevenly dis- tributed claims of computing resources. If you need to depend on a calcula- tion complexity that is easy to predict and fixed with regard to an algorithm — rather than depending on an algorithm’s usage — you should prefer eager evaluation. 8.5 Structure 8.5 Structure 121 Client LazyObject request : LazyObject access : T Suspension Value request : LazyObject access : T request : LazyObject access : T Figure 8.4: Structure diagram 8.6 Participants (cid:15) Client – issues a request to LazyObject (e.g., calling tail on a stream of hamming numbers). – accesses Suspension and later Value for a value (e.g., calling item on a stream of hamming numbers). (cid:15) LazyObject – provides an interface for requests and accesses. – creates and returns a Suspension on request. (cid:15) Suspension (e.g., representing a stream’s tail) – implements LazyObject’s interface. – represents a suspended calculation. – calculates a result when accessed. – creates a Value for further accesses. (cid:15) Value (e.g., a cached hamming number) – implements LazyObject’s interface. – caches the result calculated by Suspension. 122 8 LazyObject aClient aLazyObject aSuspension aValue Lazy Call request make Data Access access evaluate make Repeated Access access Figure 8.5: Interaction diagram 8.7 Collaborations (cid:15) A client issues a request to a lazy object. (cid:15) The lazy object creates a suspension and returns it to the client. (cid:15) Some time later the client accesses the suspension. (cid:15) The suspension calculates a result (possibly accessing the lazy object), returns it to the client, and creates a value for future accesses2. (cid:15) Future client accesses are made to the value that caches the calculation result. 8.8 Consequences (cid:15) Initialization. The best place for initialization values (e.g., return 1 as the ra- dius of a fresh circle) is the data abstraction they belong to [Auer94]. A getter-routine can calculate an initial value whenever it is called the first time. Hence, initial values will not be calculated unless they are actually needed. Also, setter-routines may provide space for received arguments, only when actually asked to do so. 2For instance, a stream element will convert its successor to a value when it is safe to do so. 8.8 Consequences 123 (cid:15) Data generation. Lazy generation of data structures does not require artificial upper bounds in order to restrict time and space investments. The calculate by need approach allows specifying all as the upper bound even in the case of infinite data structures. (cid:15) Data-flow architecture. Streams emphasize data flow rather than control flow. A system based on streams is determined by the types of modules and the in- terconnections (streams) between the modules [Manolescu97]. Delayed eval- uation and streams nicely match because delayed evaluation inherently pro- vides the intertwined processing of data, required for interconnected streams. Note that connected streams do not necessarily operate in a synchronized fashion. A pipeline with a Huffman encoding3 inbetween (e.g., to simulate a distorted transmission via a serial connection), will do much more process- ing at the two ends of the bitstream due to the distribution of one element into several bit elements representing its Huffman encoding. Lazy evalua- tion, however, will make this inner stream automatically run faster than the outermost ends. A chain of components, such as chain = double (cid:14) filter(< 81) (cid:14) square, can be as- sembled without worrying about the size of lists to be processed or specifying loops with exit conditions [Braine95]. One may also reason about such chains and, for instance, simplify the above to chain = double (cid:14) square (cid:14) filter(< 9)4, in order to save operations. (cid:15) Modularity. The ability to separate what is computed from how much of it is computed is a powerful aid to writing modular programs [Hughes87]. The control over data consumption can be separated from the issues involved with data generation. A lazy stream allows separating iteration loops (deciding on halting and possibly using other streams) and iterations (including element generation and iteration strategy). See pattern Transfold on page 163. Modules can be decoupled but not necessarily implying a loss of efficiency. On the contrary, through partial evaluation, efficiency can actually be gained. Lazily connected modules never cause each other to perform beyond the ab- solute minimum to produce the results of the driving module. (cid:15) Partial evaluation. With respect to streams it is worthwhile noting that one may evaluate the spine of stream without causing evaluation of the under- lying stream elements. For instance, the call sieve @ (nats.tail) does not evaluate the first natural, i.e., “1” since it directly proceeds beyond to the second member “2”. This can be useful when advancing the state is easy but the actual calculation of elements is costly and not all elements are required. (cid:15) Information hiding. A lazy object hides its delayed internals from clients. A client is never requested to treat lazy object specially, e.g., by passing a 3A variable-length code, discovered by David Huffman. 4Provided processed numbers are positive only. 124 8 LazyObject dummy parameter for evaluation5. Not every lazy computation can be ex- pressed like this, though. In certain cases it might be necessary to use lazy function objects, e.g., as elements of a stream. For instance, laziness may be achieved between a program and an operating system that interchange re- quests and responses (see figure 1.4 on page 28), only if a response is struc- tured. An atomic response, e.g., an integer, must be encapsulated in a function since it can not be calculated until a request has been processed. Streams also hide their element generation strategy from clients. They may 1. directly represent stream elements (e.g., a chain of StreamVals). 2. precompute and buffer groups of elements. 3. compute elements on demand. All these strategies can be mixed in stream combinations. This is pos- sible since through their access restriction — one may only access the head of a stream — two dual views on streams are possible without con- flict [Edwards95]. On the one hand, a stream appears as a channel for trans- mitting data. On the other hand, a stream can be regarded as the entirety of all elements to come. Summarizing, streams are an excellent example for interface reuse. (cid:15) Value semantics. Lazy results are actually lazy values. Repeated evaluation will not yield different (state dependent) results. Hence, Lazy Object nicely uses state (for memoizing results) in a referentially transparent manner. One may use this kind of clean state to model efficient non-single-threaded amor- tized data structures [Okasaki95c], without the need to resort to side-effects. (cid:15) Iteration. The close resemblance of a Stream interface (see section 8.10 on page 130) to that of an Iterator [Gamma et al.94] suggests to use streams as an intermediate language between collections and iterators. Then, streams would provide a linear access to both finite and infinite data collections. Clients, in general, do not have to care whether streams are finite or not with the exception that operations on infinite streams — such as na¨ıvely trying to detect “42” in a list of primes — may not terminate. (cid:15) Inter-collection language. Actually, streams are a generalization of collection iterators since they can be generated by far more alternatives. Moreover, streams can be used to generate collections. Consequently, streams are an intermediate language for the conversion of data structures into each other (see pattern Transfold on page 163). (cid:15) Persistence. Streamable collections could also easily made persistent without requiring additional mechanisms, such as Serializer [Riehle et al.97]. 5As it is the case with the explicit delay styles using unit and delay of ML and SCHEME. 8.8 Consequences 125 (cid:15) Stream configuration. The ease of exchanging stream components facilitates end user programming [Manolescu97]. Instead of recoding an application by a programmer, such a flexible system allows reconfiguration by the user. (cid:15) Parallel execution. + A pipeline of interconnected streams can be processed in parallel since it represents a pure dataflow architecture [Buschmann et al.96]. − Eager processing can execute operations in parallel to make their results available prior to their usage. Lazy evaluation is directed by data de- pendencies and, thus, normally does not allow for exploiting peaks of computing resources for future use. (cid:15) Workload distribution. The increased modularization achieved by streams can be exploited to distribute development efforts nicely into autonomous groups [Manolescu97]. (cid:15) Execution profile. Lazy operations can lead to a peak in computation perfor- mance. Insertions to a lazily sorted list nicely require constant time only. Yet, the first reading access will have to resolve all necessary sorting (which may or may not mean to sort the whole list). This behavior — akin to non- incrementally garbage collected applications — should be kept in mind if a more evenly distribution of work is aspired. Sometimes, however, it can be desirable to postpone operations. For instance, system start up time should be kept minimal while more elaborate system initialization will happen as required [Wampler94]. (cid:15) Time efficiency. + The computational complexity of a lazy algorithm is determined by its usage rather than its intrinsic properties. It is possible to use an O(n2) sorting algorithm (insertion-sort) to obtain the minimal element from a collection in O(n) time. Access to the first element of the list will not cause the costly sorting of the rest of the list. A collection which is traversed with a predicate and a lazy and operator will not be fully visited, if predicate application on one of the elements yields false. + Speed is not only to be gained by avoiding unnecessary calculations but also by avoiding to calculate values more than once. For instance, a fibonacci number stream, modeled as the addition of two fibonacci streams (fibs = 0 : 1 : streamadd fibs tl(fibs)), exhibits linear6 time com- plexity through the inherent caching of stream values. With this respect, 6As opposed to standard exponential time behavior. 126 8 LazyObject Lazy Object is in close relationship to dynamic programming7. For ex- ample, to calculate all squared natural numbers one can choose an incre- mental approach by deriving each new value from its predecessor: With pred = n2, it follows that (n+1)2 = pred +2n+1, avoiding the costly squar- ing operation for each element. Speed penalties introduced by closure creations [Allison92] can be more than compensated by this incremental computation option. + It is efficient to pass lazy objects (like streams) around since only refer- ences will be moved. There is no need to pass lazy objects with copy semantics since they are immutable anyway. + It is often possible to avoid repeated traversals of a structure if results are lazily constructed. In order to replace all values in a tree with the minimum value one typically needs to traverse the tree twice. Once, for determining the minimum value, and twice for replacing all values with the minimum. With a lazily computed minimum value, however, one can replace all tree values with a value yet to be computed [Bird84]. The same traversal which is used to replace the values is also used to build a hierarchical function structure of min functions over the whole tree. When the tree is accessed afterwards, the minimum function will be evaluated once and the obtained value will be used for each tree value accessed. − When streams are designed to be interchangeable and therefore are re- quired to share a common element format, this may impose conversion overhead. For instance, UNIX commands need to parse ASCII8 repre- sentations of numbers, calculate, and write out an ASCII representation again [Buschmann et al.96]. Object-oriented streams, however, open up the possibility to defer conversions to the elements themselves. Then, special stream element subtypes could avoid any conversions at all. − Access to lazy values is indirect. Typically, method dispatch redirects calls to either existing values or function calls. Alternatively, flags have to be checked whether cached values already exist. (cid:15) Space efficiency. + Streams can help to restrict the amount of memory needed. Consider a data source that is transformed into another structure that requires ex- ponential space. If this structure is to be processed by a function that extracts a linearly sized result, it is beneficial to model the structure gen- erating process as a stream. Then the extraction function is applied to the stream, resulting in the same linearly sized result, without causing a transient exponential space requirement. Thus, useful intermediate 7Also called memoing or tabulation [Abelson & Sussman87]. 8American Standard Code for Information Interchange. 8.9 Implementation 127 structures [King & Launchbury93] do not imply infeasible space over- head. + Since lazy objects are immutable they can be shared by multiple clients without requiring duplication in order to avoid interferences. − As can be seen in section 8.10 on page 130 (figures 8.12 on page 138, 8.13 on page 139, 8.14 on page 141 and 8.15 on page 142) the representation of a stream with objects can be storage intensive (see section 8.9 for allevia- tions). Also, the space required by stream suspensions might be overkill (see next bullet Closure overhead). Care should be taken not to unneces- sarily keep references to stream suspensions. This would prevent their early removal by garbage collection. Although object overhead encumbers garbage collection, clever mem- ory management can make lazy languages outperform eager and even imperative languages [Kozato & Otto93]. (cid:15) Closure overhead. Sometimes, more overhead through closure management is introduced, than is gained by computational savings. Accessing the second element of two lazily added lists [1; 2; 3; 4] + [5; 6; 7; 8], results in, (1 + 5) : 8 : ([3; 4] + [7; 8]), creating two suspensions for one standard addition and a de- ferred list addition respectively. Especially, when all elements of a structure will eventually be evaluated anyway, it can be worthwhile to avoid the cre- ation of space and time (through creation and management) consuming sus- pensions. (cid:15) Event handling. A pull driven stream model as presented here cannot handle prioritized or asynchronous events. It is, however, possible to either use push driven streams [Buschmann et al.96] or to apply an Out-of-band and In-band partition [Manolescu97]. The latter approach divides an interactive applica- tion into user interaction (events) and data (stream) processing parts. 8.9 Implementation On a first reading the reader might want to skip this section. After examination of the sample code in section 8.10 the following implementations details are easier to deal with. (cid:15) Stream functions. It is very interesting to note that the value, step, and eos in- terface of ContFunc (see section 8.10 on page 130) is justified by a categori- cal interpretation of streams as coinductive types [Jacobs & Rutten97]. Here, streams over a type A are modeled as a state of type B (state of a ContFunc object) and two functions: Function h : B ! A produces a new stream element (value) and function t : B ! B produces a new state (step). Then, an anamorphism on h and t (a stream generating function) is defined to be the unique function f : B ! Str A such that diagram 8.6 on the next page 128 8 LazyObject A (cid:2) B (cid:27) hh;ti idA (cid:2) f ? A (cid:2) Str A (cid:27) hhead; taili B f ? Str A Figure 8.6: Definition of an Anamorphism commutes. A stream generator f , hence, needs to build a list cell (eventually StreamValue) from calling an element generator h (value) and itself composed with a state transformer t (step): f = Cons (cid:14) hh; f (cid:14) ti [Pardo98b]. This view concerns infinite streams only. Introducing finite streams involves consideration of a function that determines the stream’s end (eos of ContFunc and last of Stream, respectively). For a further discussion the reader is referred to [Meijer et al.91]. (cid:15) Avoiding tail suspensions. Some stream generating functions may create a suc- cessing stream state without waiting for arguments to become available. For example, it is not necessary to suspend a tail call to a StreamFunc which refers to a NatFunc (see section 8.10 on page 130). One can save the creation of a StreamTail and its later removal by introducing an optimized stream function (StreamFuncOpt) that redefines the tail method of StreamSuspension and the forceTail method of StreamFunc: class StreamFuncOpt[G] inherit StreamFunc[G] redefine tail, forceTail end creation make feature tail : Stream[G] is do if not tailIsValue then if tailCache/=Void then if tailCache.isNotSuspended then tailCache:=tailCache.toValue; tailIsValue:=True; end; elseif not last then tailCache:=forcedTail; end; end; 8.9 Implementation 129 Result:=tailCache; end; feature {None} forceTail : Stream[G] is do !StreamFuncOpt[G]!Result.make(contFunc.step); end; end Instead of creating a stream tail suspension, tail directly creates a stream function successor by calling forceTail. A client can make advantage of this optimized scheme by declaring, e.g., nats : StreamFuncOpt[Integer]; Creation and usage of the optimized stream is exactly as before. Clients can be made unaware of the existence of two different StreamFunc by either going via an application syntax function like Pipe (see section 8.10 on the next page) or deferring creation of stream functions to continuation functions. (cid:15) Class versus object adapter. In order to improve clarity of presentation I used the object version of Adapter [Gamma et al.94] for class StreamFunc (see sec- tion 8.10 on the following page). What it essentially does is to translate a service from continuation functions (value, step, eos) to the requirements of StreamSuspension (forceItem, forceTail, last). Alternatively, one can use the class adaptor version for StreamFunc. There would be several stream function heirs to StreamSuspension that would also inherit from an individual continuation function respectively. On the one hand this would less cleanly separate stream functionality (e.g., forcing) from continuation functionality (e.g., argument processing). On the other hand, a considerable amount of object creation and storage would be saved. In fig- ure 8.15 on page 142 all pairs of StreamFunc and ContFunc objects would collapse into single objects. On may even think about dropping any adaption at all and make continua- tion functions direct heirs to StreamSuspension. Yet, this would burden im- plementors of new continuation functions to create objects of their respective functions in the step routine. Either class or object adaption scheme nicely takes care of this. (cid:15) Storage requirements. Also for reasons of presentation clarity I choose a direct modeling of streams with objects. In cases where the space requirements of the object model presented in section 8.10 on the following page are pro- hibitive, one can evade to more compact representations. Streams can, for 130 8 LazyObject instance, maintain a shared generic array of elements and use an internal cur- sor for their actual position. Access suspensions could then be much more ef- ficiently be represented as an internal counter that increases with tail accesses and has to be decreased — inducing function evaluation — before element access. 8.10 Sample Code We will now consider the implementation of lazy streams in detail. Again, note that a stream is just an example for Lazy Object and that there are many other applications for Lazy Object besides streams (see section 8.4 on page 119). The most important issue to establish upfront is the stream interface9 to clients: deferred class Stream[G] feature item : G is deferred end tail : Stream[G] is deferred end last : Boolean is deferred end feature {Stream} isNotSuspended : Boolean is deferred end toValue : StreamVal[G] is deferred end feature {StreamTail} forcedTail : Stream[G] is deferred end end Class Stream plays the role of LazyObject (see figure 8.4 on page 121) with the exception that it defers creation of sus- pensions to its heirs. For now, we do not care about the non- public features of Stream besides noting that toValue al- lows converting a stream element, which no longer needs to be suspended, into a value (see class Value in figure 8.4 on page 121). We start simple by trying to establish a stream of natu- ral numbers. In this case our Suspension (see figure 8.4 on page 121) will be a function (represented by class NatFunc) that yields the next natural number. Heading for a general scheme we introduce an indirection with StreamFunc that uses any ContFunc to generate a stream element (see fig- ure 8.7) and make NatFunc an heir to ContFunc (see figure 8.8 on the facing page). StreamFunc c o n t F u n c NatFunc arg = 1 Figure 8.7: Natural number stream 9For brevity I omitted an out feature that is useful to display streams to the user. 8.10 SampleCode 131 Client Stream item : G tail : Stream last : Boolean ContFunc value : G step : Current eos : Boolean NatFunc StreamSuspension StreamVal forceItem : G forceTail : Stream item : G tail : Stream StreamFunc StreamTail Figure 8.8: Using streams A client then creates a StreamFunc by passing a NatFunc for initialization. The client can now access stream elements via the Stream interface. Requesting an item from Stream is — in this case — handled by StreamFunc. StreamFunc inherits the implementation of item from StreamSuspension: deferred class StreamSuspension[G] inherit Stream[G] feature item : G is do if itemCacheFlag=False then itemCache:=forceItem; itemCacheFlag:=True; end; Result:=itemCache; end; 132 8 LazyObject tail : Stream[G] is do if not tailIsValue then if tailCache/=Void then if tailCache.isNotSuspended then tailCache:=tailCache.toValue; tailIsValue:=True; end; elseif not last then !StreamTail[G]!tailCache.make(Current); end; end; Result:=tailCache; end; last : Boolean is deferred end feature {Stream} isNotSuspended : Boolean; toValue : StreamVal[G] is do If valueCache=Void then !StreamVal[G]!valueCache.make(item, tail); end; Result:=valueCache; end; feature {StreamTail} forcedTail : Stream[G] is do if not isNotSuspended then tailCache:=forceTail; isNotSuspended:=True; end Result:=tailCache; end; 8.10 SampleCode 133 feature {None} itemCache itemCacheFlag : Boolean; : G; tailCache valueCache tailIsValue : Stream[G]; : StreamVal[G]; : Boolean; forceItem forceTail : G is deferred end : Stream[G] is deferred end end Method forceItem (called by item) is implemented by StreamFunc: class StreamFunc[G] inherit StreamSuspension[G] creation make feature make(f : like contFunc) is do contFunc:=f; end; feature {None} contFunc : ContFunc[G]; forceItem : G is do Result:=contFunc.value; end; forceTail : Stream[G] is do !StreamFunc[G]!Result.make(contFunc.step); end; last : Boolean is do Result:=contFunc.eos; end; end 134 8 LazyObject In our example the invocation of value on contFunc (see implementation of forceItem) will return the current natural number: class NatFunc inherit ContFunc[Integer] creation make feature arg : Integer; make (a : like arg) is do arg:=a; end; value : like arg is do Result:=arg; end; step : like Current is do !!Result.make(arg+1); end; end StreamFunc tail StreamTail suspension c o n t F u n c NatFunc arg = 1 Figure 8.9: Suspension of a stream tail Requesting the tail of a Stream usually results in the creation of a StreamTail (see figure 8.8 on page 131 and figure 8.9). An item call on a StreamTail object forces the generation of a tail from suspension and delegates the item request to the result of this operation. Again, the implementation of item in StreamSuspension will be called and — this time — will use forceItem from class StreamTail: 8.10 SampleCode 135 class StreamTail[G] inherit StreamSuspension[G] creation make feature make(s : like suspension) is do suspension:=s; end; feature {None} suspension : Stream[G]; forceItem : G is do Result:=suspension.forcedTail.item; end; forceTail : Stream[G] is do Result:=suspension.forcedTail.forcedTail end; last : Boolean is do Result:=suspension.forcedTail.last; end; end Delaying the computation of stream elements with StreamTail is crucial for self referring streams such as the ham- ming numbers stream. Not before the hamming number represented by the head of the suspension is accessed is it safe to access any numbers immedi- ately required by the tail of the suspen- sion (see section 8.3.2 on page 116). tail StreamFunc item = 1 StreamFunc item = 2 c o n t F u n c Yet, given a suitable stream genera- tion function and the optimization pre- sented in section 8.9 on page 127, a di- rect “stepping” of the current NatFunc object can be achieved. The optimized forceTail method of StreamFuncOpt directly calls the step method of its continuation Figure 8.10: Streamfunction unfolding NatFunc arg = 2 136 8 LazyObject function and creates a new StreamFuncOpt with the obtained result. Hence, the intermediate creation of a StreamTail object is avoided — in this case without loss of generality. Calling tail in the situation of figure 8.7 on page 130 creates the scenario in fig- ure 8.10 on the preceding page. In this diagram — as well as in the following object diagrams — creation lines are drawn were requests on objects (e.g., tail) induce the creation of objects. Presenting the actual circumstances would unnecessarily com- plicate the pictures. Further tail calls will finally replace StreamFuncs with StreamVals (see fig- ure 8.11). tail StreamFunc item = 1 tail StreamVal item = 2 tail StreamVal item = 3 StreamFunc item = 4 c o n t F u n c NatFunc arg = 4 Figure 8.11: Streamvalue insertion StreamVal objects, i.e., stream values are not suspended stream elements and allow discarding obsolete StreamFunc objects while representing their cached val- ues. Summarizing, we may observe that StreamTails will be replaced by StreamFunc- tions, which in turn will be replaced by StreamVals. With our just established stream of natural numbers it is not very difficult to generate a stream of prime numbers. We make use of the Sieve of Erathostenes (see definition 1.5 on page 15) and obtain primes by filtering naturals. This time, however, we do not explicitely create a StreamFunc object but obtain the stream of prime numbers by applying a Sieve function on naturals: : StreamFunc[Integer]; : Stream[Integer]; : Sieve; nats primes sieve ... !!sieve.make; primes:=sieve @ (nats.tail); Class Sieve encapsulates the stream function creation process and provides a nice Its purpose is to create a StreamFunc instance that refers to application syntax. a SieveFunc (see figures 8.12 on page 138 and 8.16 on page 147). The resulting 8.10 SampleCode 137 situation is exactly analogous to the NatFunc case. I deliberately did not use an intermediate application syntax function for natural numbers in order to make the presentation of the underlying mechanisms as clearly as possible. However, clients are released from dealing with StreamFunc objects and are given a more natural way to deal with streams when provided with such adapter classes like Sieve. For this purpose, class Pipe provides the generation of StreamFunc objects: class Pipe[G, H] inherit Function[Stream[G], Stream[H]]; creation init feature pipeFunc : PipeFunc[G, H]; init(f : like pipeFunc) is do pipeFunc:=f; end; infix "@" (s : Stream[G]) : Stream[H] is do !StreamFunc[H]!Result.make(pipeFunc @ s); end; end Class Sieve simply inherits Pipe and initializes pipeFunc to SieveFunc on creation (see also figure 8.16 on page 147). The client code from above (sieve @ (nats.tail)) produces the situation of figure 8.12 on the following page. Here, natural numbers are not using the op- timized StreamFunc in order to show that caching of tail values achieves that both the first natural number and the sieve function refer to the same stream suspension. Figure 8.13 on page 139 shows the same structure after several item and tail calls. It can clearly be seen that SieveFunc employs FilterFunc objects to filter the stream of naturals: class SieveFunc inherit PipeFunc[Integer] feature value : Integer is do Result:=stream.item; end; step : like Current is local filter : expanded Filter[Integer]; divides : expanded Divides; 138 8 LazyObject StreamFunc tail StreamTail suspension c o n t F u n c NatFunc arg = 1 StreamFunc c o n t F u n c SieveFunc stream Figure 8.12: Stream of primes notf : expanded NotF[Integer]; do Result:=Current @ (filter @ (notf @ (divides @ value)) @ stream.tail); end; end A FilterFunc will be generated by the application of Filter. Note how the three func- tion objects are declared to be of expanded type in order to safe explicit creation. SieveFunc makes use of the PipeFunc abstraction that specializes a continuation function to a function with one stream argument. Heirs to PipeFunc profit from a predefined eos function and are immediately subject to the nice application syntax provided by Pipe (see above). Providing application syntax and specializing the continuation function interface, PipeFunc is an heir to both Function and ContFunc: deferred class PipeFunc[G, H] inherit inherit Function[Stream[G], PipeFunc[G, H]]; ContFunc[H] redefine eos end feature 8.10 SampleCode 139 tail StreamFunc item = 2 tail StreamFunc item = 3 StreamFunc item = 5 c o n t F u n c SieveFunc stream StreamFunc item = 5 c o n t F u n c stream FilterFunc !divides = 3 StreamTail suspension tail StreamFunc item = 3 StreamFunc item = 5 t c o n F u n c StreamFunc item = 5 stream FilterFunc !divides = 2 t c o n F u n c NatFunc arg = 5 Figure 8.13: Unfolded primes stream : Stream[G]; init (s: like stream) is do stream:=s; end; 140 8 LazyObject infix "@" (s : like stream) : like Current is do Result:=clone(Current); Result.init(s); end; eos : Boolean is do Result:=(stream.last); end; end See also figure 8.16 on page 147 for all relations between Client, Pipe, ContFunc, and PipeFunc. Requesting yet another prime from the structure of figure 8.13 on the page be- fore results in figure 8.14 on the facing page. Starting from the bottom it can be observed that the current natural amounts to seven rather than five. The interme- diate six has been filtered out. Note that the StreamTail object between the factor three and factor two filter has vanished and a new one was introduced right after a newly inserted factor five filter. The same stream in a normalized state — resulting from some more item re- quests — is visible in figure 8.15 on page 142. Compared to figure 8.14 on the facing page, 1. a StreamFunc object has been converted to a StreamVal object, 2. a chain of StreamFunc objects has been reduced to one StreamFunc object, 3. and the natural number source has been advanced to number eleven. From the dynamics of stream implementation — which is well hidden from the clients of streams — back to the static structure of the encountered participants: Figure 8.16 on page 147 shows all classes involved in the presented prime numbers example. Here, Client demonstrates the two ways to interact with stream functions. On the one hand it creates both NatFunc and StreamFunc and on the other hand just uses Sieve. The diagram also depicts that Pipe and PipeFunc support function application syntax by inheriting from Function. It is interesting to follow the creation chain from Sieve via SieveFunc to Filter, which takes a predicate argument in order to create Filter1 that in turn creates a FilterFunc object and, finally on receipt of a stream argument, will create a Stream- Func object (via class Pipe). Note that the Stream subtree at the right part of the diagram exactly corresponds to the stream supporting classes of figure 8.8 on page 131. An abstracted version of figure 8.16 on page 147 with a client that deals with streams via intermediate stream functions only is presented in figure 8.17 on page 148. This diagram depicts the rich 8.10 SampleCode 141 tail StreamFunc item = 2 tail StreamValue item = 3 tail StreamFunc item = 5 StreamFunc item = 7 t c o n F u n c stream SieveFunc StreamFunc item = 7 c o n t F u n c stream FilterFunc !divides = 5 StreamTail suspension tail StreamFunc item = 5 StreamFunc item = 7 t c o n F u n c StreamFunc item = 7 stream FilterFunc !divides = 3 t c o n F u n c StreamFunc item = 7 stream FilterFunc !divides = 2 c o n t F u n c NatFunc arg = 7 Figure 8.14: Expanding the sieve 142 8 LazyObject tail StreamFunc item = 2 tail StreamValue item = 3 tail StreamValue item = 5 StreamFunc item = 7 StreamFunc item = 7 c o n t F u n c stream FilterFunc !divides = 5 StreamTail suspension t c o n F u n c stream SieveFunc StreamFunc item = 11 t c o n F u n c StreamFunc item = 11 stream FilterFunc !divides = 3 t c o n F u n c StreamFunc item = 11 stream FilterFunc !divides = 2 c o n t F u n c NatFunc arg = 11 Figure 8.15: Normalized sieve 8.11 KnownUses 143 design of stream functionality that is available and ready to be used. Only the two classes in the lower left corner of the diagram with the “Concrete” prefix need to be supplied in order to introduce a new stream function. A new ConcretePipeFunc has to provide implementations for the value, step, and (only if deviating from the standard behavior) eos methods. The new ConcretePipe simply initializes class Pipe’s pipeFunc attribute to the new ConcretePipeFunc. Often, it is not even necessary to introduce a new stream function but a standard function can be applied to a whole stream by using the stream function Map. Its implementation is as simple as this: class MapFunc[G, H] inherit PipeFunc[G, H] creation make feature mapFunc : Function[G, H]; make (f : like mapFunc) is do mapFunc:=f; end; value : H is do Result:=mapFunc @ (stream.item); end; step : like Current is do Result:=Current @ (stream.tail); end; end 8.11 Known Uses its flexibility to stream based The UNIX operating system owes much of The com- commands that can be interconnected via pipes [Ritchie84]. line separated ASCII characters allows the free compo- mon format of to sition of services. POSTSCRIPT10, and to finally view it, one can build a pipe of four commands: cat bitmap.pbm.gz | unzip | pbmtolps | ghostview -. to uncompress a bitmap, convert For instance, it SATHER [Omohundro94] supports a restricted form of coroutine called an Iters yield elements to be used in iterations. Their pri- “iter” [Murer et al.93b]. 10PostScript is a registered trademark of Adobe Systems Incorporated. 144 8 LazyObject mary difference to streams is the bounded lifetime, which ends with the iteration loop. ALGOL 60 [Backus et al.63, Landin65] features call-by-name parameter passing. This mechanism enables delayed evaluation, save the “evaluate only once” strategy of call-by-need. Programming languages with static array sizes (e.g., PASCAL [Rohlfing78]) force us to specify an array size that is often not know at the time of array creation. Programming languages that enable dynamic array sizes (e.g., EIFFEL) — in a way — allow for a lazy size specification for arrays. A force operation enlarges an array whenever necessary, so the initial size given does not restrict future array uses. Graph algorithms in general are a popular target for lazy evaluation. David King and John Launchbury present algorithms with linear-time complexity that are constructed from individual components, which communicate via lazily generated intermediate structures [King & Launchbury93]. A lazy depth-first search tree of a graph will not cause the whole graph to be visited, when only part of the tree is re- quested. Even possible side-effects, such as marking nodes as visited, will only be performed as necessary [Launchbury & Jones94, Launchbury & Jones95]. In func- tional programming it is common to express, e.g., tree traversal problems first by flattening the tree into a list, and then by processing the list. The intermediate list can then provide a channel of communication between standard components. Lazy evaluation successfully avoids the transient creation of a large intermediate list and of large intermediate structures in general [Launchbury93]. Lazy graph algorithms also promise to be suited for multiprocessor architec- tures. The absence of side-effects and the memory friendly behavior of data bind- ings, as opposed to data structures, contributes to their suitability for parallel pro- cessing [Kashiwagi & Wise91]. The Convex Application Visualization System (AVS) is a visualization packages that organizes various modules, such as file readers and data processing, to a net- work of interconnected components [Convex93]. The Lazy Propagator pattern [Feiler & Tichy97] uses updates on demand in a network of distributed computations. A network node is updated only when re- quested for one of its values. A single request thus may cause a wavefront of back- ward updates until the system is just enough updated to answer the initial request. Attribute grammars with lazy attributes can be specified without regard to the nature of dependencies (e.g., left-right, or even circular). Attributes will automati- cally be resolved in the order of their dependencies [Mogensen95]. For further examples the reader is referred to the literature given in section 8.2 on page 115. 8.12 RelatedPatterns 145 8.12 Related Patterns 8.12.1 Categorization Iterator: The interface of Iterator from the Iterator pattern is very similar to that of Stream. Both internal iterators and streams allow separating the iteration loop and collection navigation from the iteration body. Streams, however, do not provide a first method. As opposed to external iterators, streams are not stateful objects. To re-iterate a stream, one has to keep a copy of the stream’s start. Stream, Pipeline, Pipes and Filters: are architectural views on the stream concept which can be provided by lazy evaluation [Ritchie84, Edwards95, Meunier95b, Buschmann et al.96, Posnak et al.96, Shaw96]. Singleton: A Singleton is a Lazy Object since it is created at most once. If created at all, every accessor will be given the same instance, which exactly corresponds to call-by-need semantics. Co-Routine: A stream can be regarded as a co-routine. Resuming happens through tail calls and one can think of a series of subsequent continuation functions as co-routine incarnations distributed over stream element functions. The co- routining behavior of lazy objects is vital for their ability to avoid large in- termediate structures. Calculation suspension allow building a pipeline that processes elements one by one rather then causing whole transient structures to be constructed in between. 8.12.2 Collaboration Function Object: Function Object can be used for lazy evaluation as well. A func- tion object represents a suspended calculation until requested to evaluate its result. Streams are connected to function objects through class Map. It allows apply- ing standard function objects to streams. Ergo, it is easy to obtain a stream function, if a corresponding a single element function already exists. Translator: The result of a translation obtained using Translator can be a stream. Transfold: Transfold processes lazy streams, that function as a lingua franca between collections and iterators. Serializer: Serializer could be used to flatten data in order to transfer it via a stan- dard stream channel. Instead of using Serializer on may alternatively simply use a specialized object stream. 146 8 LazyObject Composite: A stream append operation (appending f and g yields h = f (cid:14) g) can be regarded as a stream composite [Gamma et al.94, Manolescu97]. One may also use a Component to encapsulate the splitting of one stream into multiple branches and their re-joining into one stream again. For instance, one may filter various information types from a multi-media stream, compress them individually, and re-compose them to a compressed multi-media stream. 8.12.3 Implementation Memoization: Being a technique rather than a pattern, memoization nevertheless is used by Lazy Object in order to perform calculations only once. Further requests are served by accessing a cache [Keller & Sleep86]. Value Object: A lazy object may use Value Object (see chapter 9 on page 149) to produce immutable cached values of calculated results, e.g., stream elements. Command, Function Object: StreamTail can be regarded as a function object with one argument (suspension) and three entry points with no further arguments. Its task is to separate the creation of an operation and its actual invocation in time, very similar to the Command pattern. Factory Method: If clients defer the creation of stream functions to continuation functions — that know whether to choose a StreamFunc or StreamFuncOpt — the corresponding method is a Factory Method [Gamma et al.94]. Void Value: The behavior of a stream’s end may be implemented with Void Value (see chapter 11 on page 191). Client Function Pipe ContFunc value : G step : Current eos : Boolean NatFunc Stream item : G tail : Stream last : Boolean Filter Filter1 Sieve PipeFunc eos : Boolean StreamSuspension StreamVal forceItem : G forceTail : Stream item : G tail : Stream FilterFunc SieveFunc StreamFunc StreamTail NotF Divides Figure 8.16: Sample structure 8 . 1 2 R e l a t e d P a t t e r n s 1 4 7 148 8 LazyObject Client Function Pipe PipeFunc ConcretePipe eos : Boolean ContFunc value : G step : Current eos : Boolean Stream item : G tail : Stream last : Boolean StreamSuspension StreamVal forceItem : G forceTail : Stream item : G tail : Stream ConcretePipeFunc StreamFunc StreamTail Figure 8.17: Stream structure diagram 149 9 Value Object Even in change, it stays the same. – Heraklit 9.1 Intent Use immutable objects with generator operations for protection against side-effects and aliasing. 9.2 Also Known As Immutable Object [Siegel95]. 9.3 Motivation Mutable objects are the bedrock of object-oriented design and programming. The ability to dynamically instantiate objects, change their value, retrieve their state, and share them among multiple users for communication allows the development of efficient software with a close relationship to real word models (see chapter 2 on page 29). Yet, another bedrock of object-orientation is encapsulation (see section 2.2.2 on page 32). Encapsulated objects provide us with the peace of mind that every change to an object is controlled by the object itself. Obviously, an inconsistent or unex- pected change of object state can be caused by the object itself only. Unfortunately, this is not quite true. 9.3.1 Problem Surprisingly many object-oriented languages do not provide complex numbers ei- ther as primitive types or as a library component. Luckily, we can extend the exist- ing library and provide a complex number class as if it has been part of the stan- dard1. The straightforward approach is to write a class with a set of operations, 1As always the devil lurks in the details. See Matthew Austern’s account on the difficulties of introducing a Complex class with EIFFEL. 150 9 ValueObject e.g., add, mult, etc., that modify their receiver. There are countless examples of this approach in object-oriented libraries, especially for container classes. The change to an internal state is done by the most competent instance — the object itself — and it is the most efficient way to implement updates. A partial implementation, therefore, could be: class Complex creation make feature re : Real; im : Real; make(r : Real; i : Real) is do re:=r; im:=i; end plus(other : Const_Complex) is do re:=re + other.re; im:=im + other.im; end end Where is the problem? Let us have a look of a typical code fragment using the new class: ... local a : Complex; b : Complex; do !!a.make(1, 2); b:=a; a.plus(b); io.putstring(b.out); end; Two variables of the new type Complex are declared and one is created with the value 1 + 2i. It is only natural to move complex numbers between variables which happens in b:=a. Then a is changed by adding the value of b, i.e., it is effectively doubled. As the following print statement reveals, the value of b is not 1 + 2i any- 9.3 Motivation 151 more, but reflects the change that happened to a! Alas, what happened?2 One the one hand nothing unusual took place: The value of a complex number variable was changed. Since another reference (b) to the value of a was created before, any change can be seen trough b as well. On the other hand, our intuition about dealing with numbers has been severely hurt. The keyword to resolve this disturbing confusion is reference. We imple- mented complex numbers with reference semantics, i.e., 1. variables are pointers to complex number objects, 2. operations change object state, and 3. assignment copies references only. As a result, aliasing with unwanted effects is the standard behav- ior. One way to con- trol such introduced side-effects is to disal- low the application of operations that mutate object state. Hence, it appears useful to in- troduce an immutable interface to complex numbers [Wilson95]. For instance, with NonMutatingAccessor ImmutableInterface make accessor_functions MutableInterface mutator_procedures Figure 9.1: Providing a “const” interface class Complex inherit Const_Complex creation make feature plus(other : Const_Complex) is do re:=re+other.re; im:=im+other.im; end; end any caller of plus can be sure that the passed argument will still be unchanged after the execution of plus. This scenario assumes a hierarchy as depicted in figure 9.1. An attempt to assign other to a variable of type Complex, i.e., an mutable interface, results in a type error. 2Anyone not feeling irritated about this behavior is probably spoiled by commonplace reference semantics in object-oriented languages. 152 9 ValueObject An immutable interface (i.e., the Constant Object pattern) is a powerful means to control side-effects and in ... local a : Complex; b : Const_Complex; do !!a.make(1, 2); b:=a; a.plus(b); io.putstring(b.out); end; one can be reasonably3 sure that the value of b cannot be altered by passing it to plus. As already stated it is also not possible to modify the value of b by obtaining a mutable interface to it, e.g., by a:=b. Unfortunately, the above code (using b:=a) will still compile and produce the unwanted effect. One might be tempted to reverse the hierarchy of figure 9.1 on the page before and make immutable interfaces inherit from mutable interfaces. Apparently, statement b:=a; would be illegal then. But, (cid:15) reversing the hierarchy of figure 9.1 on the preceding page is not sound in terms of subtyping. Class ImmutableInterface would have to hide operations from MutableInterface destroying substitutability. Therefore, (cid:15) it is no longer possible to pass mutable objects to servers with read-only in- terfaces. The const modifier property of immutable interfaces is lost. (cid:15) of course, it is still possible to create aliasing, e.g., with a:=b;. In conclusion, immutable interfaces do not provide a suitable solution to refer- ence semantics induced problems with complex numbers. After all, it should not be necessary to declare variables to provide read-only access in order to avoid the above aliasing effects. Moreover, we want to be able to modify complex numbers. There is no point in installing a safety mechanism that inhibits modification of val- ues to such an extent. 9.3.2 Solution Complex numbers are values, i.e., they are timeless abstractions and therefore un- changeable [MacLennan82]. They deserve to be implemented with value seman- tics. Instead of modifying the receiver of operation add, the receiver should be left 3Of course, a server may use a reverse assignment attempt to gain a mutable interface to an object passed through an immutable interface. Anyone doing so, is hopefully aware of the surprises possibly introduced. 9.3 Motivation 153 untouched and a new instance of a complex number should be returned. In anal- ogy, there are no operations that change integers. An expression like 41.inc does not make sense, since 41 is a unique abstraction, rather than an object with iden- tity [Khoshafian & Copeland86]. While it could be used to arrive at the abstraction 42 by adding 1 it is not possible to change it. A version of Complex with value semantics is: class Complex creation make feature ... infix "+", plus(other : Complex) : Complex is do !!Result.make(re + other.re, im + other.im); end; end Note that now it is now possible to use the more natural infix "+" notation for addition of complex numbers. It was added as a synonym for plus which we just retained for better comparison with the previous version of plus. The client code needs a bit of adaption to reflect the change — ... local a : Complex; b : Complex; do !!a.make(1, 2); b:=a; a:=a + b; io.putstring(b.out); end; — and will, finally, work as expected. When a:=a + b; is executed, a is reat- tached to a new instance of a complex number. Variable b continues to point to the old value of a. A change to a finally no longer affects the value of b. It is worthwhile noting that immutable interfaces remain useful for classes with reference semantics but are absolutely superfluous for classes with value seman- tics (e.g., method plus above does not need a Const Complex argument interface anymore). There is no way a server could change a complex number anyway. Any modification results in a new complex number, leaving any passed instance intact. In other words, values do not require immutable interfaces. 154 9 ValueObject 9.4 Applicability (cid:15) Modeling. When clients are interested in values only and never care about identity, use Value Object for the object in question. Types representing val- ues, i.e., abstractions with no identity for which creation, change, and shar- ing have no meaning, should be implemented with value semantics. Besides numbers, strings are a typical example. Sharing of string contents easily oc- curs with a straightforward implementation but is certainly unexpected4. (cid:15) Aliasing protection. When a type should be immune to aliasing effects, replace its mutator operations with generator operations. in- stance, a matrix multiplication using destructive updating of the receiver works fine until someone calls: This may destroy parts of the matrix that are still needed for input. With aMatrix:=aMatrix.mult(aMatrix); there is no such danger since target and source matrix are different. aMatrix.mult(aMatrix);. For (cid:15) Small objects. Use Value Object preferably for small data structures. The larger the structure, the greater the overhead through duplication of unchanged data when creating a new instance in response to an operation. Using Value Object for larger structures can still be reasonable to avoid aliasing and does not necessarily have to be inefficient (see 9.9 on page 158, Copy on demand), but is most appropriate for objects with a small memory footprint. (cid:15) Efficient value passing. Creating new instances when changes occur is appro- priate when the frequency of passing and assigning values is higher than their update frequency. If updating values is the predominant operation then consider to implement a copy-on-passing scheme (see 9.9 on page 158, When to copy values) or use a language’s built-in support for value types (e.g., expanded types in EIFFEL or non-pointer variables in C++). 9.5 Structure Client ValueObject make accessors generators Figure 9.2: Structure diagram 4One of the few things the JAVA library got right: String objects are immutable and sharing can be achieved with StringBuffer only. 9.6 Participants 9.6 Participants (cid:15) Client 155 – calls an accessor or generator operation of ValueObject. (cid:15) ValueObject – returns a value on access. – creates and returns a new object when asked for a modified version of itself. 9.7 Collaborations aClient aValue aValue* generator Figure 9.3: Interaction diagram (cid:15) A client performs an operation on a value object. (cid:15) The value object creates a new instance containing the information according to the requested operation. (cid:15) The value object returns the freshly created instance. 9.8 Consequences (cid:15) Simplicity. Code using values can be much simpler than code using objects, since there is no need to be cautious about side-effects and aliasing. All ad- vantages of dealing with immutable values apply (e.g., validation of correct- ness, understandability, transformability (see section 1.3.1 on page 17)). Value objects do not offer mutator operations, but use generator operations to pro- vide new instances. Note that object encapsulation is still preserved. Any changes to the object are still under the regime of the object itself. Plainly the target of the updated information is different to a conventional destructive update. 156 9 ValueObject (cid:15) Encapsulation. While direct aliasing (as presented in the example of section 9.3 on page 149) might be mastered with effort, indirect aliasing is a notoriously hard issue [Hogg91, Almeida97]. For instance, even expanded types in EIF- FEL use a shallow copy on assignment only. As a result, components are shared and are subject to change while the owning object is not in control. Value Object provides full state encapsulation even for groups of objects. (cid:15) Equality. Testing value objects for equality cannot be accomplished by using a built-in equality operator such as “=”5. The standard operator will compare pointer values, which can be different for otherwise equal values (but see also 9.9 on page 158, Sharing of values). The solution is to use a (usually already setup) equality method, like in if equal(aComplex1, aComplex2) then ... or using a custom infix operation: if aComplex1 #= aComplex2 then ... Often, as in EIFFEL, the setup equality method will perform as desired: It will compare all value components for equality. Nonetheless, it can be useful to override the equality method (is equal in EIFFEL) in the value class, in or- der to achieve equality checking of abstract state rather than concrete state. For instance, a custom equality method could appreciate the equality of two complex numbers, even if one of them is represented with cartesian coordi- nates while the other uses polar coordinates. (cid:15) Subtyping. Usually “is-a” or specialization inheritance is at odds with sub- typing. Although a set of integers “is-a” set of numbers it is not possible to pass it to a method expecting a set of numbers. This restriction is sound since the method could legally insert any number, e.g., a real number, violating the caller’s integer set constraint. However, if the method’s parameter is declared to be a value set of numbers then calling the method with a set of integer values is valid and safe. There is no way in which the called method could corrupt the passed set, since it is immutable. Other examples of “is-a” relationships that usually must be avoided but work in the context of value types are Ellipse being subclassed by Circle and Rect- angle subclassed by Square. In both cases some mutations, e.g., stretching in one direction only, would throw subclass objects into trouble6. 5Every JAVA programmer must discover that comparing strings with “=” does not work. 6Unless they can dynamically change their type, as with SMALLTALK’s become: method. 9.8 Consequences 157 Note that it is even save to pass a mutable square, i.e., one that offers destruc- tive updates, to a method with a square value object interface (see figure 9.4 on the following page). Inside the method the mutator operations are not ac- cessible and therefore subtyping is provided. Anyway, a generator method might return a result whose type is different from that of the receiver. For instance, stretching a Square yields a Rectangle. Sadly, while subtyping with values actually works with the latter examples, there is no way to pursue EIFFEL’s type system to believe that an integer in- stantiation of a generic class can be passed to a number instantiation. The type system rightfully assumes invalidness in the general case and cannot be hinted to the value semantics constraints that make it safe in this particular case (see section 14.5 on page 243). (cid:15) Heterogeneity. Reference types and value types cannot be used uniformly by clients. Besides the need for a different equality check7, clients must either use aComplex.add(aNumber); ... aComplex:=copy(aNumber); aComplex.negate; or -- keep aNumber intact aComplex:=aComplex + aNumber; ... aComplex:=aNumber.negate; -- keep aNumber intact This may appear unfortunate at first sight but it should be pointed out that value semantics — in this case for numbers — is not motivated historically but deeply roots in the fundamental difference between abstract values and changeable objects [Eckert & Kempe94]. Modeling of types and the corre- sponding syntax should reflect this difference. (cid:15) Efficiency. + As value objects are immutable they can be shared safely, i.e., imple- mented with pointer semantics. Parameter passing and assignment are, therefore, cheap operations. Safe sharing of objects may also vastly sim- plify concurrent and parallel designs [Siegel95]. + Value objects can share subcomponents as long as these do not change. This reduces the amount of memory needed and speeds up copying of unchanged data to new instance created by generator operations. − Creating a new object instead of modifying an existing one, increases memory demand and adds strain to the garbage collector. 7C++ with an overloadable “=” operator being an exception. 158 9 ValueObject − Generator operations on large value object structure are expensive. Val- ues with large components must recreate these even if only a tiny part of them changes. Changing a small part of a large value object is, thus, highly inefficient. A mixed scheme — mixing generator and mutator operations — is pos- sible (see figure 9.4), allowing mutator operations whenever safe. Unin- tentionally passing a value object to a method with a mutable argument interface, will be caught as an error by the type system. If, on the other hand, the method uses an immutable argument (ValueObject) interface, callers are protected against changes to the ar- gument, whereas code inside the method will be checked against unin- tended mutation attempts. Client ValueObject make accessors generators MutableObject mutators Figure 9.4: Optional mutators for Value Object 9.9 Implementation (cid:15) When to copy values. Value Object creates new values when values are gener- ated. Another choice would have been to create copies whenever the possi- bility of aliasing is created, i.e., whenever a new access thread is created by parameter passing or assignment. In figure 9.5 on the next page an arrow pointing downwards denotes the cre- ation of a new value by a generator operation. Indexes and shading indi- cate the generation of a value, i.e., the count of copy operations in its history. Note that the situation in figure 9.5 on the facing page is in favor of copy-on- passing rather than copy-on-change, but with less updates and more passing or assignments the score is reversed. Most programming languages implement their basic types with copy-on- passing. And in most languages we have no choice but implement Value Ob- 9.9 Implementation 159 ject with copy-on-change, because implementing copy-on-passing requires to modify the semantics of assignment and parameter passing. However, copy- on-change seems to be the better alternative in most cases, since assignment and passing should be more frequent than updates and it is more intuitive to expect a cost when operations are performed. 1v v2 v3 v4 v2 v3 1v v 1 v1 v 1 v2 v2 v1 v 1 v 1 v 1 v 1 v2 copy on change copy on passing copy on demand Figure 9.5: Copy strategies (cid:15) Value Object versus language support. Some languages offer support for the ad- dition of types with non-reference semantics. Typically, however, another motivation was the driving factor, for instance, the ability to specify ob- ject containment rather than object referencing [Madsen et al.93, Meyer92]. Consequently, value semantics is often, e.g., in EIFFEL, not sufficiently sup- ported [Kent & Howse96]: Expanded types, for instance, (cid:15) do not allow polymorphism. (cid:15) cannot be specified with deferred classes. (cid:15) make shallow copies on assignment only. (cid:15) Abstract state. Value objects must preserve equality only modulo their respec- tive equality operation. For instance, fraction numbers could decide to nor- malize their representation after addition but not after multiplication. Hence, while 15 6 + 15 6 = 15 3 15 6 (cid:3) 2 = 30 6 ; but comparing the two results would yield that they are equal. 160 9 ValueObject (cid:15) Deep copy. If a value object has subcomponents consisting of standard refer- ence type objects, it has to create a deep copy of them in case it produces an altered copy of itself. Otherwise unintended sharing of subcomponents is inevitable. (cid:15) Copy on demand. A (deep) copy of a value subcomponent is not necessary if that component does not change due to the requested operation. At least for components a value object can implement the copy-on-demand strategy of figure 9.5 on the page before. Only if a subcomponent changes, a copy has to be created before any changes are made. (cid:15) Sharing of values. Ordinarily, values are generated without regard whether an equal instance of the result already exists. If every result is first looked up in a repository of already existing values, then (cid:15) memory can be saved by avoiding multiple value instances with the same value. (cid:15) equality testing can be achieved by pointer comparison again. This is much more efficient and allows to use standard syntax for comparison. (cid:15) a repository has to be kept8 and looking up equal values consumes time. If values are more frequently compared than transformed than it pays off to use the above — so-called Singular Object [Gamma95] or Identity Ob- ject [Smith95] — strategy. 9.10 Known Uses It is needless to emphasize that functional programming languages treat their com- putational “objects” as values. Many object-oriented languages split their types into basic types (with value semantics) and reference types. Even in SMALLTALK, where everything is an object, numbers are correctly treated differently, i.e., they do not provide mutator but generator operations. Ian Poole et al. use a combination of Lazy Object and Value Object, i.e., im- mutable lazy values to represent complex computation networks [Poole et al.98]. The benefits of their Lazy/RT9 pattern (e.g., modular reasoning, parallel evaluation multiple level undo, exception recovery, etc.) is a combination of the consequences of both Lazy Object (see section 8 on page 115) and Value Object. 8In SMALLTALK such a collection can readily be obtained by sending allInstances to an ob- ject. 9Lazy and referentially transparent objects. 9.11 RelatedPatterns 161 9.11 Related Patterns 9.11.1 Categorization Constant Object: A value object interface to a mutable object fulfills the same pur- pose as an immutable interface (see section 9.3 on page 149). In addition to accessor functions, a value object interface, furthermore, provides generator operations, which can be used to create new values without altering the orig- inal. Identity Object, Singular Object: This pattern emerges when Value Object or Constant Object is implemented with unique value instances as described in section 9.9 on page 158. For instance, the datatype Symbol in SMALLTALK is implemented as an Identity Object. Unshareable Object: Another way to prevent aliasing it to allow single references to an object only [Minsky95, Minsky96]. While values can be motivated from a modeling point of view, it is unclear what the real world equivalent of an unshareable reference is. Unshareable references are not copied but moved, i.e., the source of an assignment statement will be nil afterwards. A useful analogy might be a physical object that can only be moved but not shared or a unique item that is allowed to occur only once, e.g., a token in a network. 9.11.2 Collaboration Function Object: If function objects accept arguments through a value object inter- face, they do not need to copy arguments in order to be safe from future changes. This, however, does not hold if there is also a mutating interface available (see figure 9.4 on page 158) through which arguments could be cor- rupted. Strategy, Function Object: The equality operation can be parameterized with a function ob- ject predicate. Depending on the application, several “views” on equality could be implemented (e.g., books which differ in publishers only could be considered equal for a certain purpose). Void Value: Default values (e.g., 0 + 0i for complex numbers) can be provided with Void Value. Transfold: Due to the absence of aliasing, it is less error prone to generate new val- ues from a collection of values than dealing with shared objects. A value aggregate can be iterated without concerns for changes occurring during iter- ation. 162 9 ValueObject Lazy Object: Pattern Lazy Object (see chapter 8 on page 115) may be used to lazily initialize value object components. Decorator: A decorator could be used to provide a value semantics interface to stan- dard reference types. A decorator must first copy the receiver to a new in- stance, then update that instance according to the operation, and finally re- turn the new instance. 9.11.3 Implementation Lazy Object: A value object may return a lazy result. Especially, if the calculation of a new value is costly and frequently only parts of the result are actually used then it is worthwhile to defer the calculation until the result is accessed. Transfold: A value object may implement equality by using Transfold to compare reference type collections in subcomponents. Translator: Extremely complex values may implement sophisticated operations — such as providing a certain view or an interpretation of a value — by translat- ing their subcomponents. 163 10 Transfold I can’t understand why a person will take a year or two to write a novel when he can easily buy one for a few dollars. – Fred Allen 10.1 Intent Process the elements of one or more aggregate objects without exposing their rep- resentation and without writing explicit loops. 10.2 Motivation Collections play a very important role in software design. They allow us regarding a bunch of objects as a single abstraction. Clients pass and store collections as sin- gle entities and defer the responsibility of reacting to a message to the collections. A single request to a collection may mean to forward the request to all collection members (e.g., most Composite services in the Composite pattern), find a member best suited to deal with the request (e.g., Chain of Responsibility pattern), extract one member with particular properties (e.g., a detect method), etc. Hence, with the exception of a few Dispenser types like Stack and Queue, that restrict access to one end, it is common to access collection members one by one until a condition is met or full coverage has been achieved. In other words, a frequent operation on collections is iteration. 10.2.1 Problem It is clearly not an option to let clients iterate over collections — or more generally, aggregates — using knowledge about an aggregate’s internals. In ... list l do : List[Integer] : Link[Integer]; from l:=list.first; until l=void 164 10 Transfold loop io.putint(l.item); l:=l.next; end; ... the client is committed to a linked list implementation of List. If the implementation of List changes to a balanced tree representation, e.g., to make searching a faster operation, each client iteration as above is subject to change. Even C++ code as commonplace and innocent looking as ... for (i=0; i>< >>: 8 1 6 ) 3 5 7 4 9 2 IP @ vecs:tail = 71 Figure 10.12: Inner product application The horizontal processing function is a fold declared as 10.9 SampleCode 183 fold : expanded Fold[Integer, Real]. The use of expanded is an idiom to save the otherwise necessary explicit attach- ment of an object to fold (see section 7.9 on page 105, Creation). Apart from the argument-collection classes, Fold is implemented with class Fold2[E, A] inherit Function[Stream[E], A]; creation make feature func : Function[E, Function[A,A]]; init : A; make(i : like init; f : like func) is do init:=i; func:=f; end; infix "@" (stream : Stream[E]) : A is local fold : expanded Fold[E, A]; do if stream=void then Result:=init; else Result:=func @ stream.item @ (fold @ func @ init @ stream.tail); end; end; end and, thus, shares a striking similarity to its functional counterpart (see defini- tion 10.9 on page 180). With the help of fold and map (which was established in chapter 8 8.10 on page 143), the class implementing the body of transfold is quite straightforward: class inherit Function[Stream[Stream[E]], A] TransFold3[E, I, A] StreamUtility[E] creation make feature 184 10 Transfold foldFunc : Function[I, Function[A, A]]; init mapFunc : Function[Stream[E], I]; : A; make(f : like foldFunc; i : like init; m : like mapFunc) is do foldFunc:=f; init:=i; mapFunc:=m; end; infix "@" (streams : Stream[Stream[E]]) : A is local map : expanded Map[Stream[E], I]; fold : expanded Fold[I, A]; do Result:=fold @ foldFunc @ init @ ((map @ mapFunc) @ transpose(streams)); end ... The application method directly corresponds to the functional transfold (see def- inition 10.3 on page 171). I intentionally left out the transpose method of class TransFold3, because it nicely demonstrates the difference between a high-level, functional, internal iteration style — transpose(streams : Stream[Stream[E]]) : like streams is ... do newRow:=mapToHeads @ head @ streams; tails:=mapToTails @ tail @ streams; if (fold @ oneEmpty @ False @ tails) then tails:=void; else tails:=transpose(tails); end; Result:=consS @ newRow @ (tails); end — and its “von Neuman14”, procedural, external iteration style: 14Referring to the critique of John Backus towards a “von Neumann [programming] Style” [Backus78]. 10.9 SampleCode 185 transpose(streams : Stream[Stream[E]]) : like streams is ... do from rows:=streams; !!rowArray.make(1, 7); until rows=void loop rowCount:=rowCount+1; rowArray.force(rows.item, rowCount); rows:=rows.tail; end; from until shortestEnds loop from row:=rowCount; newRow:=void; until row=0 loop newRow:=cons @ (rowArray @ row).item @ newRow; rowArray.put((rowArray @ row).tail, row); shortestEnds:=shortestEnds or ((rowArray @ row) = void); row:=row-1; end; Result:=consS @ newRow @ Result; end; end The latter version uses nested loops to construct new rows by repeatedly collecting heads and checking for the shortest row. The functional version (implementing def- inition 10.2 on page 169) replaces this nesting with normal mapping and a recursive call. The procedural version uses an array to memorize the respective positions of the partially consumed rows. The functional version avoids this by transforming the argument matrix into the desired residual matrix. It is noteworthy to observe that the procedural version features one loop count- ing downward, rather than upwards to get the element order right. No such detail must be respected in the functional version15. 15Admittedly, on other occasions one has to think about whether to add to the front or to the back of a stream. 186 10 Transfold Also, although both versions do not transpose matrices with an infinite number of rows (infinite rows are handled), the functional version is easier to extend to- wards this property. First, because it is simpler and easier to understand. Second, because the recursive call to transpose exactly defines the extension hot spot: The strict recursive call must be replaced by a lazy stream generation, using the framework of StreamFuncs, presented in chapter 8 on page 115. A final example, hinting at the expressiveness of transfold (see section 10.10 on the next page for a comparison with APL), shall be the implementation of matrix multiplication with transfold. To show the result of the multiplication of a magic square with another matrix, as depicted in figure 10.13, 2 6 6 4 8 1 6 3 5 7 4 9 2 3 2 7 7 5 ⊗ 6 6 4 1 1 0 1 0 1 0 1 1 3 7 7 5 = 2 6 6 4 9 8 3 7 7 5 14 7 10 12 13 6 11 Figure 10.13: Matrix multiplication with a magic square one simply calls showMatrix(matMult(vecs, rows)); using matMul(xss, yss : Stream[Stream[Integer]]) : like xss is local transMapInner : expanded TransMapInner[Integer]; map : expanded Map[Stream[Integer], Stream[Integer]]; do Result:=map @ (transMapInner @ yss) @ xss; end; We exploit the fact that the elements of the result matrix are defined by zi j = inner product xi yt j; i.e., the inner products of all rows of the left matrix x with all columns of the right matrix y determine the result matrix. To implement this, we process each row of the left matrix with a func- tion transMapInner, that applies the inner product operation to its argument row of the left matrix with each column in the right matrix, i.e., the body of transMapInner is: Result:=transFold.mapWith(applyInner @ xs) @ yss; 10.10 KnownUses 187 Each row (xs) of the left matrix is transfolded with the right matrix, i.e., combined with each column of the right matrix, and the inner product operation is applied to each such pair. The applyInner function simply constructs a two row matrix out of its two stream arguments and applies the inner product operation to it: ... ip:=transFoldInner @ plus @ number.zero @ Result:=ip @ (cons @ xs @ (cons @ ys @ void)); (fold @ times @ number.one); The use of number.zero16 achieves that the function is generic, since either 0, or 0.0 would create a type-mismatch with the wrong generic instantiation of matrix multiplication17. Summarizing, matrix implementation was implemented by mapping, and two transfolds, one realizing the inner product operation. 10.10 Known Uses Although C++ usually promotes external iteration, there is an example of an in- ternal iterator (foreach) interface in the Borland C++ libraries [Borland94]. Since it builds on passing function pointers, it must use an extra, unsafe void type for passing parameters, though. Folding is commonplace in functional programming [Bird & Wadler88], but also used in SCHEME [Abelson & Sussman87] and available in the SMALLTALK li- brary [LaLonde94]. It is not a frequently used operation in SMALLTALK [Smith95], which can be attributed to the peculiar name (inject: into:), but also to the unfamil- iarity of most SMALLTALK users with the nature of that operation. The SMALLTALK collection library even contains a with: do: method, allowing to iterate two struc- tures in parallel, which represents a special case of transfolding. SMALLTALK also uses streams to efficiently implement concatenation of collections. The caching ef- fect of streams avoids to repeatedly generate the prefix of sequenced concatenations like coll1 + coll2 + coll3 + coll4 + .... APL [Harms & Zabinski77] is well-known for its high-level operations on vec- tors, matrices, and structures of even higher dimension. Three of its four primitive extension operators18, reduction (f/A), scan (f\A), and innerProduct (Af.gB), can directly be expressed with transfold. The fourth, outerProduct (A(cid:14).fB), is express- ible with a combination of transfold and map. The matrix multiplication example from section 10.9 on page 182, builds an outer product of the matrices’ rows and columns, and then reduces it with an inner product. 16number is a variable declared to be of the generic matrix element type. 17Although an escape was possible, one would wish that simply 0 has had sufficed. 18Extend an operation to a collection. 188 10 Transfold 10.11 Related Patterns 10.11.1 Categorization Function Object: Transfold’s interface to clients is that of a Function Object. Iterator: Transfold realizes internal iteration, whereas the Iterator pattern uses ex- ternal iteration [Gamma et al.94]. There is also an internal variant of the Iter- ator pattern [Gamma et al.94], but it uses inheritance for iterator and action combination and exhibits the normal one-structure-iteration inflexibility. Bridge: The stream between structure and iterator is like a Bridge [Gamma et al.94], separating a consumption interface from the implementation of a structure (including the exploration implementation). Serializer: The use of an intermediate stream resulting from structure exploration is akin to the Serializer pattern [Riehle et al.97], whose purpose is to flatten structures for persistence. 10.11.2 Collaboration Lazy Object: Transfold is very well suited to transform one lazy structure into an- other. Value Object, Composite: Iterators are often applied to recursive (possibly value) structures like Composite. Furthermore, Composite [Gamma et al.94] may be implemented with Transfold to enumerate its children. Translator, Visitor: Structure consuming patterns may defer their structure exploration to Transfold. Chain of Responsibility: An event may be transfolded over a collection of handlers to find the most appropriate handler. Observer: An Observer [Gamma et al.94] may use Transfold to iterate over its dependents-collection. Keyword Parameter: Transfold may use the keyword parameter variant of Function Object (see section 7.10 on page 107) to ease supplement of its parameters. 10.11.3 Implementation Lazy Object: Transfold uses a lazy stream as well as lazy generation and consump- tion functions. 10.11 RelatedPatterns 189 Void Value: A void value may be used to define the base case of the exploration process. Function Object, Strategy: A structure may use a Strategy [Gamma et al.94] or a Function Object to allow variation of its exploration process. Function Object is used to provide closures for function parameter passing and is also employed to implement Transfold’s functionality. 190 10 Transfold 191 11 Void Value I’m not completely worthless! I can still serve as a bad example. – Mark Twain 11.1 Intent Raise Nil to a first-class value. This allows to treat void and non-void data uni- formly, is a way to provide default behavior, facilitates the definition of recursive methods, and enables to deal with error situations more gracefully. 11.2 Motivation Classes are often regarded as representing abstract data types (ADTs). Typically, the empty constructor of an ADT is translated to Nil. For instance, an empty Bina- ryTree is represented by Nil and a tree leaf is represented by setting both child at- tributes to Nil. It is tempting to identify empty constructors with the special pointer content Nil, since there are some reasons in favor of it: (cid:15) Operations are often partial due to empty, well accounted for by Nil. (cid:15) Nil does not waste any storage. (cid:15) Most languages initialize references to Nil, i.e., provide the empty case by default. It is, for instance, suggested to represent an unknown book author with a void reference [Meyer97]. 11.2.1 Problem However, such a design decision puts a great burden on the clients of the ADT. Before the client can apply operations on a tree, it must check whether it is void or not. Otherwise, invoking a method through a Nil reference would produce a runtime-exception or even -error. Typical (EIFFEL) code1 is depicted at the upper part of figure 11.1 on the next page. 1All code examples in this chapter have been extracted from a commercially available library; identifiers have been adapted for presentation, though. 192 11 VoidValue Result:=(tree /= Void) if tree /= Void then and then tree.has(v); io.putstring(tree.out); Result:=tree.has(v); io.putstring(tree.out); Figure 11.1: Code example: Robust behavior 11.2.2 Solution The code in the upper part of figure 11.1 can be replaced with the code at the lower part of figure 11.1, if we abandon Nil (Void) and replace it with an extra class (VoidTree) (see figure 11.2). Client Tree has(v : Any), out, ... VoidTree MutableTree has(v : Any) = false out ... has(v : Any) ... out ... Figure 11.2: Empty tree as void value Now we can define VoidTree to return false on a has message and to produce no output on out. In fact, we applied the general pattern to replace case analysis (testing for Nil) with dynamic binding (provision of an extra type constructor Void- Value). As a result, many such if statements as above can be removed from client code. An important special case of robust behavior is a Null iterator (also see Itera- tor [Gamma et al.94]). Instead of external iteration (upper part of figure 11.3 on the next page) one should use internal iteration, e.g., and let void sets do nothing on iteration (lower part of figure 11.3 on the facing page). Answering queries to void values with default values again allows replacing the upper part of figure 11.4 on the next page with its lower part. This obviously improves the treatment of trees from a client’s perspective, but there is more to gain. Let us count the number of nodes in a tree. 11.2 Motivation 193 if set /= Void then loop over all set elements setIterator.do_all Figure 11.3: Code example: Null iterator if tree /= Void then maxPlane:=tree.maxPlane else maxPlane:=DefaultMaxPlane end maxPlane:=tree.maxPlane Figure 11.4: Code example: Default Behavior The recursive calls in the upper part of figure 11.5 must be guarded with if state- ments when tree leafs are represented with Nil. If void tree values return 0 as their count, then the code becomes much more readable and better expresses the func- tion’s spirit (see lower part of figure 11.5). Result:=1; if left /= void then Result:=Result+left.count; end if right /= void then Result:=Result+right.count; end Result:=1+left.count+right.count; Figure 11.5: Code example: Base case definition The provision of base cases by void values is a special case of the general scheme to distribute cases. Complicated nesting, like depicted in the upper part of fig- ure 11.6 on the following page becomes much clearer code when distributed to VoidAny and AnyObject class definitions. Note that the use of obj.is void is different to obj = Void. The former tests for voidness as well, but allows the rep- resentations for void objects to be changed. This is useful for multiple void values as well as for standard objects which just happen to be in an empty or void state. 194 11 VoidValue equal(me: ANY; you: ANY): ... Result:=(me=Void and you=Void) or else ((me/=Void and you/=Void) and then me.is_equal(you)) Result:=you.is_void Result:=is_equal(you) (in class VoidAny) (in class AnyObject) Figure 11.6: Code example: Case distribution It is worth mentioning, that we may provide void trees, -nodes, and -leafs in- dependently with possibly differing behavior. See section 11.10 on page 199 for an analogous example when this is useful. Also note that while void values are a nat- ural concept for container classes their applicability extends to all types of classes. For instance, the subordinates method of a VoidEmployee can return an empty list. Its salary method may create a no-valid-employee exception, return 0, or a VoidSalary instance, depending on the desired semantics. All three possibilities may coexist in separate void values. Application parts can individually choose to inject the appropriate void value into server calculations. 11.3 Applicability (cid:15) Uniformity. Clients benefit from Void Value by uniformly treating void and Inquiring information and invoking behavior can be done non-void data. independently of the data’s void state, without the eventual need for creating initial instances first. (cid:15) Default behavior. Void values allow the specification of default behavior. In contrast to Nil, a void value may specify results and behavior for any of its interface methods. This is useful for providing reasonable behavior for unini- tialized data and void result values. (cid:15) Error handling. A void value can be used just like an exceptional value [Cunningham94] when most of the code should not deal with er- ror situations. An exceptional value may either behave properly indicating an error situation (e.g., by describing the error when by default displayed to the user) or can be caught with error checking code by the application’s top-level code. (cid:15) Termination. Use Void Value to relieve recursive definitions from testing the base case. A recursive call can then be made regardless of possibly void argu- ments. Accordingly, the definition for the base case can be given at the base values (i.e., void values) instead of one step before. Here, Void Value plays the role of a first-class terminator. 11.4 Structure 11.4 Structure 195 Client ObjectInterface is_void operation VoidValue* VoidValue MutableObject is_void = true operation is_void = true operation is_void = false operation Figure 11.7: Void Value structure diagram 11.5 Participants (cid:15) Client – accesses both MutableObject and VoidValue through ObjectInterface. (cid:15) ObjectInterface – provides the common interface for MutableObject and VoidValue. – may provide common abstract2 behavior to MutableObject and Void- Value. (cid:15) MutableObject – defines the standard object behavior. – introduces attributes for object state. – does not care for recursion base cases, default- and error behavior. (cid:15) VoidValue – replaces Nil. – defines base cases for recursion, default- and error behavior. 2Concrete methods do not rely on state (attributes), but on an abstract interface, only. 196 11 VoidValue 11.6 Collaborations (cid:15) Clients use the ObjectInterface to manipulate void, non-void, default, and er- ror objects. If MutableObject extends the interface of ObjectInterface, clients may maintain specialized references to MutableObject, provided they are guaranteed to receive objects of type MutableObject only. 11.7 Consequences (cid:15) Abstraction. Void Value abstracts from the implementation detail to represent void or uninitialized data with Nil. Clients, therefore, are relieved of the need to treat data with Nil differently from data which does not use Nil for its representation. (cid:15) Object-Orientation. Checking for Nil, i.e., case analysis, is replaced with dy- namic binding. This moves all case analysis from the program to a single distinction between normal object and void value. Object behavior must be defined at two places (object and void value). One the one hand, this allows combining several void values with a single object. On the other hand, chang- ing a single method might mean to change two class definitions (object and value class). (cid:15) Efficiency. It is more storage friendly to use void values, instead of full blown objects that simply have the state of being empty or uninitialized, but carry the overhead of unused attributes. Note, however, that void values are not as easily turned into objects again without support for “Implicit creation” (see sections 11.8 on the facing page & 14.7 on page 248). Depending on the implementation of dynamic binding a calling overhead may be incurred, compared to if statements. Note, however, that the tradi- tional solution at the upper part of figure 11.5 on page 193, unnecessarily checks aritydepth−1 arity−1 nodes for being void, in addition to the following count call. (cid:15) Immutability. If void values should not need more storage than Nil, they have to be immutable. Mutable objects without attributes just claim code space for their methods once, and possibly a little storage for housekeeping mecha- nisms such as RTTI (Run Time Type Identification) and object identification. (cid:15) Separation. Program code relying on Void Value describes the standard case only. In analogy to language support for exception handling, framework code or algorithm descriptions thus become easier to write and read (see code ex- amples). Handling of errors, denoted by void values, can be done in a few places at the application’s top level. Lower level code can be designed to smoothly pass error values around, until they are identified by the top level. 11.8 Implementation 197 (cid:15) Implicit initialization. In order to avoid any occurrences of Nil, one must tie the creation of objects to the declaration of references. See section 11.8 for suggestions how to automatically achieve initialization to void values. (cid:15) Initialization errors. Exceptions, caused by the application of operations on Nil, are dealt with more gracefully. Instead of triggering an exception or halt- ing the system a possibly inappropriate behavior is executed, but users are still able to continue the application. The downside of this is that a missing initialization might go unnoticed, only to be discovered as unexpected appli- cation behavior very much later in the execution. According to the experi- ences of the UFO project [Sargeant93], however, unnoticed missing initializa- tion has not been found to be a problem in practice. Additionally, it is almost as easy to pass Nil around undetected. Error messages simply become more meaningful with void values [Sargeant96b]. If the void value is one among several alternatives for a type, its identity allows tracing back its origin. Yet, care should be taken to never allow the misinterpretation of an erroneous value to be a valid result. Calculations must preserve the exceptional status of void values to prevent the masking of errors. 11.8 Implementation (cid:15) Interface extension. In analogy to the Composite pattern [Gamma et al.94], Mu- tableObject might provide more operations than ObjectInterface, in order to provide operations that make no sense to VoidValue. Each such extension, however, will minimize the applicability of VoidValue to play the role of an error value. Clients using the extended MutableObject interface, will not be able to transparently work on void values too. In this context, it is better to have the full interface at ObjectInterface and to define exception (e.g., error reporting) methods in VoidValue respectively. (cid:15) Storage requirements. Any behavior that goes to ObjectInterface should rely on abstract state only, i.e. “Implement behavior with abstract state” [Auer94]. Any attributes will incur a space penalty on VoidValue. (cid:15) Value initialization. If representation of a complex constant (requiring creation effort) is more important than preserving minimal space requirements, then a void value may calculate the constant information (possibly taking creation arguments) and store it in attributes. Accordingly, VoidValue is best imple- mented as a Singleton [Gamma et al.94] in order to allow sharing of the con- stant data. (cid:15) Multiple inheritance. In statically typed languages VoidValue needs to multiply inherit from its interface class and another void value class, if code reuse is desired between void value classes. One of the inheritance paths is used only 198 11 VoidValue for interface inheritance though, unless the interface classes implement com- mon behavior for VoidValue and MutableObject. In the latter case the language should properly support repeated inheritance of code (e.g., as in EIFFEL). (cid:15) Reference initialization. The value to be gained from Void Value partly de- pends on how thoroughly we can eliminate any Nil values from program execution. To this end, one may replace standard references with default- references, akin to smart references [Edelson92]. Client ObjectInterface MutableObject VoidValue DefaultReference value constructor make Figure 11.8: Automatic reference initialization A DefaultReference (see figure 11.8) is a value (e.g., non-reference type in C++, expanded type in EIFFEL, part-object in Beta, etc.). Consequently, it is initial- ized by declaration, i.e., cannot take the value Nil. After initialization it dele- gates any message to VoidValue by default. Hence, any usage of DefaultRefer- ence without prior initialization results in applications to VoidValue, instead of Nil. Ultimately, DefaultReference will be initialized (e.g., using a method called make) to reference a MutableObject. In the structure of figure 11.8 De- faultReference plays the role of an ObjectProxy. Implementation issues, such as using overloading of the member access operator (->) in C++, are dis- cussed in the implementation section of Proxy [Gamma et al.94]. Note that using a DefaultReference involves a delegation overhead for each access to either VoidValue or MutableObject. This might be a price too high to pay just for gaining automatic initialization of references. Of course, we can achieve automatic initialization of MutableObject by di- rectly declaring it to be a value, resulting in a much simpler structure and no delegation overhead. Yet, this is possible only if value semantics (e.g., no sharing, no efficient updates) is intended [MacLennan82]. (cid:15) Implicit creation. SMALLTALK allows changing the type of an object without affecting its identity. Ergo, a void value may change to an object and vice 11.9 KnownUses 199 versa. If a void value cannot deal with a message it may delegate it to its as- sociated object and then become:3 the object itself. Hence, there is no need for explicitly creating objects anymore, since void values can be made responsi- ble for this. In the absence of a become mechanism, implicit creation can still be achieved by “Reference Initialization” (see above). 11.9 Known Uses NoController, NullDragMode, NullInputManager, NullScope are classes in the class hierarchy of VISUALWORKS SMALLTALK that are used as void values [Smith95]. 11.10 Related Patterns 11.10.1 Categorization Composite: Both Composite and Void Value provide uniform access to components (value & object) of a structure (type). A VoidValue plays the role of a spe- cial immutable component leaf. As a difference, Void Value does not involve issues of child management. Value Object, Constant Object: A void value “is-a” value object, in that it does not offer mutator operations. As value objects, void values lend themselves to be implemented with Identity Object (see next bullet and section 9.11.1 on page 161). Singleton: A void value is a Singleton [Gamma et al.94], in that it can be shared by all objects of a type. As it is immutable, there is no need to have multiple instances. Flyweight: VoidValue can be regarded as a ConcreteFlyweight, as it holds intrinsic data and is shared among all clients that use it. State: When used as described in Implicit creation at section 11.8 on page 197, Void- Value and MutableObject play the role of ConcreteStates, representing the void and non-void states of data. 11.10.2 Collaboration Lazy Object: Pattern Lazy Object (see chapter 8 on page 115) may be used to lazily initialize void value components. Command: Void Command may stand for default or idle behavior. 3SMALLTALK’s method name to replace objects. 200 11 VoidValue Template Method: While subclass responsibility [Goldberg & Robson83] normally re- quires at least one concrete subclass, Void Value can be used as a default sub- class, allowing the execution of abstract methods. Of course, Void Value is just a special concrete subclass, so this collaboration is a lot more dramatic with language support for void values (see section 14.7 on page 248). Memento: A Void Memento can be used as a default and reset state. Factory Method, Abstract Factory: A void value can be used as a return value for not available prod- ucts, e.g., unsupported widget types with regard to a specific window factory. Iterator, Visitor, Chain of Responsibility: All these patterns are based on iteration and Void Value can help to define their termination behavior, analogous to a base case in recur- sion. State, Strategy: Void State may represent the initial or an error state. While used like Void State, Void Strategy would allow StrategyContext creation without a Strategy selection. 11.10.3 Implementation Proxy: DefaultReference (see figure 11.8 on page 198) works as a Proxy since it keeps a reference to an object whose interface it shares. Bridge: DefaultReference behaves like a bridge, in that it shields the clients from the two “implementations” VoidValue and MutableObject. 201 12 Translator The art of programming is the art of organizing complexity. – Edsger W. Dijkstra 12.1 Intent Add semantics to structures with heterogeneous elements without changing the elements. Separate interpretations from each other and use local interpretations that allow for incremental reevaluation. 12.2 Motivation Many operations on data structures can be viewed as homomorphisms, that is, as structure preserving mappings from one domain into another. For instance, com- pilers typically map the abstract syntax of the source language into a specific ma- chine code language1. Other kinds of abstract interpretations (e.g., pretty-printing and type-checking) should be expressed as homomorphisms between source and target domain as well. The reason for this recommendation can be explained by means of an equation that holds, if a homomorphic relationship between two struc- tures exists: f (op(a; b)) = op 0(f (a); f (b)) (12.1) An interpretation f on an operation op (from a source domain) with subcompo- nents a and b is defined as a new operation op0 (from a target domain) whose subcomponents are determined by again applying f to a and b [Wechler92]. An instance of this general equation for a compiler is, e.g.: compile(assign(lhs, rhs)) = store(compile(lhs), compile(rhs)) Note how in the above equations an interpretation is shifted down from operators down to operands. Also, the right hand side of the equations has a structure that allows us to account for incremental modifications to the source structure. In case of changing the left-hand-side (lhs) of assign, there is no need to rebuild the 1Historically, homomorphisms are closely connected to syntax-directed transla- tions [Aho et al.86] and correspond to compositional definitions [Nielson & Nielson93]. 202 12 Translator whole result term. One simply has to apply compile to the changed lhs and plug the result into the first operand of store. 12.2.1 Problem Consider a programming environment that represents programs as abstract syntax trees. It will need to perform various interpretations on the abstract syntax tree like type-checking, code generation, and pretty-printing. Figure 12.1 depicts two sample transformations. abstract Syntax Tree Pretty Print IfThen "B" Assign "IF" "THEN" "END" "B" ":=" "V" "W" Assembler Code "V" "W" TEST BEQ