Genericity, Extensibility and Type-Safety in the Visitor Pattern
Total Page:16
File Type:pdf, Size:1020Kb
Genericity, extensibility and type-safety in the V pattern Bruno Cesar´ dos Santos Oliveira Wolfson College Oxford University Computing Laboratory Submitted for the degree of Doctor of Philosophy Abstract A software component is, in a general sense, a piece of software that can be safely reused and flexibly adapted by some other piece of software. The safety can be ensured by a type system that guarantees the right usage of the component; the flexibility stems from the fact that components are parametrizable over different aspects affecting their behaviours. Component-oriented program- ming (COP), a programming style where software would be built out of several independent com- ponents, has for a long time eluded the software industry. Several reasons have been raised over time, but one that is consistently pointed out is the inadequacy of existing programming languages for the development of software components. Generic Programming (GP) usually manifests itself as a kind of parametrization. By abstracting from the differences of what would otherwise be separate but otherwise similar specific programs, one can develop a single unified generic program. Instantiating the parameter in various ways retrieves the various specific programs (and ideally some new ones too). Instances of GP include the generics (parametrization by types) mechanism as found in recent versions Java and C# and Datatype-Generic Programming (DGP) (parametrization by shape). Both mechanisms allow novel ways to parametrize programs that can largely increase the flexibility of programs. Software components and GP, and in particular DGP, are clearly related: GP and DGP provide novel ways to parametrize software, while software components benefit from parametrization in order to be flexible. However, DGP and COP have mostly been studied in isolation, with the former being a research topic among some functional programming communities and the latter being mostly studied within the object-oriented communities. In this thesis we will argue for the importance of the parametrization mechanisms provided by GP, and in particular DGP, in COP. We will defend that many design patterns can be captured as software components when using such kinds of parametrization. As evidence for this we will, using DGP techniques, develop a component library for the V pattern that is generic (i.e. can be used on several concrete visitors); extensible (i.e. concrete visitors may be extended); and i Abstract ii type-safe (i.e. its usage is statically type checked). A second aspect of this thesis concerns the adaptation of functional DGP techniques to object-oriented languages. We argue that parametriza- tion by datatypes should be replaced by parametrization by visitors, since visitors can be viewed as encodings of datatypes and, through those encodings, the functional techniques naturally translate into an OO setting. ii Acknowledgements I would like to thank Jeremy Gibbons, my supervisor, for two different reasons. Firstly, for being an excellent supervisor, for giving me a very interesting project to work on, for helping me dur- ing my first stages of research (when I felt unconfident about my research and abilities), and for always being willing to help me with technical (and bureaucratic) problems. Secondly, for giving me the opportunity to study in Britain and in the beautiful and inspiring city of Oxford, which has greatly helped me on the development of both my personal and professional skills. I cannot forget Jos´eNuno Oliveira for introducing me to the beauty of functional programming with his always interesting lectures and for motivating me into coming to Oxford. Ralf Hinze provided great in- spiration for my work and taught me a lot about generic programming. He and Andres L¨oh were excellent hosts during my one month visit to Bonn, which proved very fruitful in terms of research. My two examiners, Martin Odersky and Ralf Hinze, provided very useful feedback that helped improving the presentation of this thesis substantially. Mike Spivey took me as his student during Jeremy’s sabbatical and provided important feedback during my confirmation of status. Richard Bird and Oege de Moor were very helpful in making me focus on a research direction during my transfer examination. The Friday meetings of the Problem Solving Club provided a relaxed envi- ronment to discuss research problems and to get feedback about my own research. The meetings of the Datatype-Generic Programming project gave me the opportunity to present progress on my research and to get feedback regularly, and also provided a constant flow of new ideas. The attic crowd (which included, among others, Daniel Goodman, Christopher Aycock, Rui Zhang, Jolie de Miranda, Edward Smith, and Zoltan Miklos) provided an entertaining working environment. Wolfson College and the many friends I made there did an excellent job in my social integration at Oxford. My girlfriend Warnchudee Chalitaporn, who I met at Wolfson, gave me a lot of support and was very patient during my DPhil. Finally, my family, and especially my mother, have always motivated me and gave me the conditions to pursue higher education. iii Table of Contents 1 Introduction 1 1.1 SoftwareComponentsandReuse. ..... 2 1.2 Component-OrientedProgramming . ...... 3 1.3 DesignPatterns:ASignofWeakness? . ....... 4 1.4 Datatype-GenericProgramming . ...... 5 1.5 AcasestudyontheV Pattern.......................... 6 1.5.1 TheV:AValuableAbstraction . 7 1.5.2 TheScalaOption............................... 8 1.6 OverviewoftheThesis ............................. ... 10 1.7 RelatedWork ..................................... 11 1.7.1 Encodings of Datatypes and the V Pattern............... 11 1.7.2 DesignPatternsandComponents . ... 12 1.7.3 DesignPatternsandFunctionalProgramming . ........ 14 1.7.4 FunctionalDGP ............................... 16 1.7.5 TheExpressionProblem . 19 2 Preliminaries 22 2.1 TheScalaProgrammingLanguage . ..... 22 2.1.1 ExpressionsandDefinitions . ... 23 2.1.2 ClassesandObjects. .. .. .. .. .. .. .. .. 25 2.1.3 TraitsandMixins............................... 27 2.1.4 GenericTypesandMethods . 28 2.1.5 AbstractTypes ................................ 29 2.1.6 ImplicitParameters. .. 32 2.1.7 Higher-kindedTypes . 33 2.2 ScalaasaDGPlanguage ............................. .. 34 2.2.1 EncodingType-ConstructorPolymorphism . ....... 34 2.2.2 ALittleDGPLibrary............................. 35 2.3 Vs and Cs ............................... 36 2.3.1 TheC Pattern ............................ 36 iv TABLE OF CONTENTS v 2.3.2 TheV Pattern.............................. 38 2.4 FunctionalNotation.............................. .... 42 3 Visitors as Encodings of Datatypes 44 3.1 Introduction.................................... .. 44 3.2 Internal or External Vs:ADesignChoice .. .. .. .. .. 47 3.3 InternalVisitorsandtheChurchEncoding . .......... 48 3.3.1 EncodingDataTypesintheLambdaCalculus . ...... 48 3.3.2 TheChurchEncodinginScala . .. 49 3.4 ExternalVisitorsandtheParigotEncoding . ........... 50 3.4.1 LimitationsofChurchEncodings . .... 50 3.4.2 ParigotEncodingsintheLambdaCalculus . ...... 51 3.4.3 TheParigotEncodinginScala . ... 51 3.5 Generic Visitors: Two Dimensions of Parametrization . .............. 52 3.5.1 Abstractingovertheshape . ... 52 3.5.2 Abstractingoverthedecompositionstrategy . .......... 54 3.6 TheV PatternasaLibrary ........................... 56 3.6.1 DefiningtheLibrary ............................. 56 3.6.2 UsingtheLibrary............................... 59 3.7 Syntactic Sugar for VsinScala......................... 61 3.7.1 ExtendingtheLibrary . 62 3.7.2 UsingtheExtendedLibrary . .. 63 3.7.3 ComparisonwithFunctionalProgramming . ...... 65 3.8 ExpressivenessoftheVisitorLibrary . .......... 66 3.8.1 ParametricDatatypes. .. 66 3.8.2 MutuallyRecursiveDatatypes . .... 67 3.8.3 ExistentiallyQuantifiedDatatypes . ....... 68 3.8.4 ParamorphicVisitors . .. 70 3.9 Discussion...................................... 72 4 Visitor-Generic Programming 74 4.1 Introduction.................................... .. 74 4.2 EncodingSumsandProductsinScala . ...... 76 4.3 Generic Programming with Vs......................... 78 4.3.1 GenericsfortheMassesinScala . .... 78 4.3.2 RepresentationsofVisitors . ..... 81 4.3.3 RepresentationsofScala’sCaseClasses . ........ 83 4.3.4 DefiningGenericFunctions . .. 84 4.3.5 ReuseviaInheritance. .. 85 4.3.6 LocalRedefinition .............................. 86 v TABLE OF CONTENTS vi 4.4 GMandIndexedVs............................... 87 4.4.1 IndexedVisitors ............................... 88 4.4.2 AVisitorLibraryforIndexedVisitors . ....... 89 4.4.3 GMasanInstanceoftheVisitorLibrary. ...... 90 4.5 AVisitorforaFamilyBasedonSumsofProducts . ......... 93 4.5.1 AV BasedonSumsofProducts . 93 4.5.2 CreatingNewDatatypes . 95 4.5.3 FunctorialRepresentations . ..... 97 4.5.4 Separating Recursion from Generic Programming . ......... 98 4.6 Example: Generic Serialization and Deserialization . ...............100 4.6.1 Serialization ................................. 100 4.6.2 Deserialization ............................... 102 4.7 Discussion...................................... 103 5 Extensible Visitors and Generic Functions 106 5.1 Introduction.................................... 106 5.2 GenericFunctionsandTheExpressionProblem . .......... 109 5.2.1 TheExtensibilityProblemofVisitors . ....... 110 5.3 ExtensibilityinInternalVisitors . .......... 111 5.3.1 SimpleExtensibleVisitors . 111 5.3.2 ExtendingGenericFunctionswithExtraCases . ........ 112 5.3.3 ExtensibleRepresentations. .. 114