A Transformational Approach to Generic Software Development Based on Higher-Order, Typed Functional Signatures
Total Page:16
File Type:pdf, Size:1020Kb
A Transformational Approach to Generic Software Development Based on Higher-Order, Typed Functional Signatures Vom Promotionsausschuss der Technischen Universität Hamburg-Harburg zur Erlangung des akademischen Grades Doktor der Naturwissenschaften (Dr. rer. nat.) genehmigte Dissertation von Daniel Lincke aus Saalfeld 2012 Betreuer: Prof. Dr. Sibylle Schupp, Institut für Softwaresysteme, Technische Universität Hamburg- Harburg Zweitgutachter: Prof. Dr. Friedrich Mayer-Lindenberg, Institut für Rechnertechnologie, Technische Universität Hamburg-Harburg Drittgutachter: Prof. Dr. Carlo Jaeger, Global Climate Forum Berlin Datum der mündlichen Prüfung: 27. Juni 2012 URN: urn:nbn:de:gbv:830-tubdok-11682 Contents 1 Introduction1 1.1 Related Work...................................3 1.2 Motivating Example...............................4 1.3 Preliminaries and Notation............................6 1.3.1 Haskell..................................6 1.3.2 Concepts.................................7 1.3.3 C++ ....................................8 1.3.4 Backus-Naur Form (BNF)........................ 10 1.4 Contributions and Outline of the Thesis..................... 10 2 Functional, Higher-Order Types 12 2.1 An Overview of Type Systems.......................... 13 2.2 Untyped Lambda-Calculus............................ 15 2.3 Simply Typed Lambda-Calculus......................... 16 2.4 Polymorphic Lambda-Calculus (System F).................... 18 2.5 Lambda-Calculus with Type Constructors.................... 18 2.6 Kinds....................................... 20 2.7 Higher-Order Polymorphic Lambda-Calculus (System Fw )........... 21 2.8 Higher-Order Functional Types.......................... 23 2.9 Higher-Order Types and Object-Oriented Languages.............. 24 2.10 Types and Specification.............................. 25 2.11 Related Topics and Conclusion.......................... 26 3 Generic Software 28 3.1 Generic Programming............................... 28 3.2 Genericity..................................... 29 3.2.1 Genericity by value............................ 30 3.2.2 Genericity by type............................ 30 3.2.3 Genericity by function.......................... 30 3.2.4 Genericity by concepts (Genericity by structure)............ 31 3.2.5 Genericity by property.......................... 33 3.2.6 Genericity by stage............................ 33 3.2.7 Genericity by shape............................ 34 3.3 Polymorphism................................... 35 3.3.1 Universal polymorphism......................... 35 ii CONTENTS iii 3.3.2 Ad-hoc polymorphism.......................... 36 3.3.3 Concept-controlled polymorphism.................... 37 3.3.4 Genericity and polymorphism...................... 37 3.4 Related Topics and Conclusion.......................... 38 4 Program Transformation 39 4.1 Overview..................................... 40 4.2 Defunctionalisation................................ 42 4.3 Our Transformational Approach......................... 43 4.4 Related Topics and Conclusion.......................... 44 5 Kernel Languages 46 5.1 The Functional Type Kernel Language (FTKL).................. 46 5.1.1 Syntax.................................. 47 5.1.2 Kind rules................................. 49 5.1.3 Kind inference and kind checking.................... 50 5.1.4 Kind structure............................... 56 5.2 The Concept Type Kernel Language (CTKL)................... 57 5.2.1 Syntax.................................. 57 5.2.2 Kind rules................................. 58 5.2.3 Kind structure............................... 61 6 Transformation 63 6.1 Overview..................................... 63 6.2 Uncurrying of Type Constructor Applications.................. 66 6.3 Defunctionalisation of the Codomain of a Function Signature.......... 68 6.4 Defunctionalisation of the Domain of a Function Signature........... 71 6.5 Encoding Higher Kinded Type Variables..................... 74 6.6 Optimising Context................................ 75 6.7 The Complete Transformation and its Properties................. 77 7 Backends 79 7.1 Language Features................................ 81 7.1.1 ConceptC++ ................................ 81 7.1.2 Scala................................... 83 7.1.3 Haskell.................................. 85 7.1.4 Other languages............................. 86 7.2 ConceptC++ Backend............................... 89 7.3 Scala Backend................................... 91 7.4 Generic Haskell Backend............................. 94 7.5 Conclusion.................................... 96 8 Implementation 97 8.1 Architecture.................................... 97 8.2 Compiler..................................... 98 8.3 Performance.................................... 99 8.4 Test systems.................................... 104 CONTENTS iv 9 Applications 107 9.1 Higher-order Functions.............................. 108 9.2 Recursive Data Types as Fixed Points...................... 110 9.3 A Domain Specific Language for Vulnerability Modelling........... 111 9.3.1 Vulnerability formalisation and functional prototype.......... 112 9.3.2 Transformation and ConceptC++ implementation............ 115 10 Discussion, Outlook, and Conclusion 118 Bibliography 120 A Algorithms 129 A.1 Algorithm: find_fct................................ 129 A.2 Algorithm: replace_type............................. 130 B Test Output for ConceptC++ 131 C Transformation Output for Origami++ 137 C.1 Map........................................ 137 C.2 Fold........................................ 138 C.3 Unfold....................................... 139 C.4 Hylo........................................ 140 Acknowledgements 141 List of Figures 1.1 A very simple intermediate model of vulnerability, in Haskell and in FTKL ...5 1.2 The CTKL function signature of the vulnerability function...........5 1.3 The C++ function object generated from the CTKL function signature......6 2.1 The syntax of the untyped lambda-calculus................... 15 2.2 The syntax of the simply typed lambda-calculus................. 17 2.3 The typing rules of the simply typed lambda-calculus.............. 18 2.4 The syntax of the polymorphic lambda-calculus................. 19 2.5 The typing rules of the polymorphic lambda-calculus.............. 19 2.6 The relation between values, types, and kinds (after [Pierce, 2002])....... 21 2.7 The syntax of the lambda-calculus with type constructors............ 22 2.8 The kinding rules of the lambda-calculus with type constructors........ 22 2.9 The kinding rules of the lambda-calculus with type constructors........ 23 2.10 The lambda cube (from [Barendregt, 1991])................... 23 3.1 Kinds of polymorphism (from [Cardelli and Wegner, 1985]).......... 35 4.1 Transformation T and language bindings.................... 44 5.1 The syntax of FTKL ................................ 47 5.2 Derivation tree of the fold function signature from the Origami library in FTKL 48 5.3 Kind rules in FTKL ................................ 50 5.4 Kind checking for the signature of the fold function.............. 51 5.5 Computation of the local kinds for the fold function signature.......... 55 5.6 The fold signature in FTKL and its kind structure................ 57 5.7 The syntax of CTKL ................................ 58 5.8 Derivation of the fold function signature from the Origami library in CTKL .. 59 5.9 Kind rules in CTKL ................................ 60 5.10 The fold signature in CTKL and its kind structure................ 61 6.1 Specification of transformation T ........................ 77 7.1 A user-defined type constructor mapping..................... 80 7.2 Generic inversion in ConceptC++ ......................... 82 7.3 Generic inversion in Scala—with subtyping................... 84 7.4 Generic inversion in Scala—with implicits.................... 85 7.5 Generic inversion in Haskell........................... 86 v LIST OF FIGURES vi 7.6 Generic inversion in Java............................. 87 7.7 Generic inversion in D.............................. 88 7.8 Function concepts in C++ ............................. 89 7.9 TypeConstructor concepts in C++ ......................... 89 7.10 Function concepts in Scala............................ 92 7.11 Type constructor concepts in Scala........................ 92 7.12 Function concepts in Haskell........................... 95 7.13 Type constructor concepts in Haskell....................... 96 8.1 The architecture of our transformation system.................. 98 8.2 The compiler and its stages............................ 99 8.3 Function concept and function datatypes in C++ ................. 101 8.4 The test cases for the compiler.......................... 105 9.1 The Haskell function signatures we transform for the FCPPC library...... 108 9.2 The C++ implementation of function composition................ 109 9.3 Function signatures of the recursion operators of the Origami library...... 111 9.4 Function signatures of the list algebra implemented in Origami++ ........ 111 9.5 The function signatures of the vulnerability model prototype.......... 114 9.6 The vulnerability computation in ConceptC++ .................. 116 C.1 The generated function signatures of the map function.............. 137 C.2 The generated function signatures of the fold function.............. 138 C.3 The generated function signatures of the unfold function............ 139 C.4 The generated function signatures of the hylo function............. 140 List of Tables 1.1 BNF syntax used................................