Download (8Mb)
Total Page:16
File Type:pdf, Size:1020Kb
University of Warwick institutional repository: http://go.warwick.ac.uk/wrap A Thesis Submitted for the Degree of PhD at the University of Warwick http://go.warwick.ac.uk/wrap/58566 This thesis is made available online and is protected by original copyright. Please scroll down to view the document itself. Please refer to the repository record for this item for information to help you to cite it. Our policy information is available from the repository home page. WA~IC]< Aspects of Functional Programming by Gary Meehan Thesis Submitted to the University of Warwick for the degree of Doctor of Philosophy Computer Science August 1999 Contents List of Tables viii List of Figures ix Acknowledgments xi Declarations xii Abstract xiii Chapter 1 Introduction 1 Chapter 2 Background 7 2.1 FUnctions, The A-calculus and Supercombinators 7 2.2 Strictness and Laziness. 8 2.3 Graph Reduction. 10 2.4 Types, Polymorphism and Overloading . 13 2.5 Abstract Machines . 17 Chapter 3 Fuzzy Functional Programming 20 3.1 FUzzy Logic . 21 3.2 FUzzy Subsets . 25 3.2.1 The Domain, Support and FUzziness of a FUzzy Subset. 28 III 3.2.2 Fuzzy Subset Operations . 30 3.2.3 Hedges and Fuzzy Numbers 32 3.2.4 Defuzzification . 36 3.2.5 An Example - Fuzzy Database Queries. 38 3.3 Fuzzy Systems .. 41 3.4 Further Examples. 47 3.4.1 Controlling a Shower. 47 3.4.2 Pricing Goods 55 3.5 Summary . 57 Chapter 4 Compiling Lazy Functional Programs to Java Byte-code 58 4.1 The Java Virtual Machine. 59 4.2 The Ginger Language ... 63 4.3 Representation of Graph Nodes 64 4.3.1 Updating 67 4.3.2 Evaluation 70 4.3.3 Printing. 73 4.4 Primitives . 75 4.4.1 Fuzzy Primitives 77 4.5 Compilation . 78 4.5.1 Compiling Supercombinators 82 4.5.2 The 'R Compilation Scheme . 83 4.5.3 The C Compilation Scheme 90 4.5.4 The £ Compilation Scheme 90 4.5.5 Tail Recursion 91 4.5.6 Initial Results. 91 4.6 Optimisations . 92 iv 4.6.1 Direct Function Invocations. 93 4.6.2 Single-instance Constants 94 4.7 Results and Other Work 96 4.8 Summary . 100 Chapter 5 The Aladin Abstract Machine 102 5.1 The Semantics of the AAM . 103 5.1.1 The Original Denotational Semantics. 104 5.1.2 The Denotational Semantics with Explicit Updates. 105 5.1.3 The Operational Semantics . 111 5.1.4 An Example of the Operational Semantics. 116 5.2 A Scripting Language for Aladin 120 5.2.1 Package Declarations . 122 5.2.2 Import Declarations 123 5.2.3 Strictness Signatures . 123 5.2.4 Definitions . 125 5.3 Representation and Evaluation of Aladin Programs. 128 5.3.1 Aladin Programs 130 5.3.2 Data Structures. 132 5.3.3 The Evaluation Mechanism 135 5.4 Primitives.............. 140 5.4.1 Java and Aladin Primitives 141 5.4.2 C Primitives .... 143 5.4.3 Primitives in Ginger 145 5.4.4 Overloading ... 149 5.4.5 Fuzzy Primitives 150 5.4.6 Importing Primitives . 154 v 5.5 Compilation . 155 5.5.1 Compiling Scripts .. 156 5.5.2 Compiling Constants and Strictness Signatures 158 5.5.3 Compiling Strictness Declarations 159 5.5.4 Compiling Imports . 160 5.5.5 Compiling and Optimising Local Blocks 160 5.5.6 Compiling Simple Programs . 164 5.5.7 Compiling the Target Code and Using the Resultant Classes 165 5.6 Summary . 167 Chapter 6 Partial Evaluation in Aladin 170 6.1 The Denotational Semantics . 171 6.2 The Operational Semantics 172 6.3 Implementation . 174 6.4 Partially Evaluating Programs 178 6.5 Primitives and Partial Evaluation. 181 6.5.1 Partial Data Structures ... 186 6.5.2 Partial Evaluation and C Primitives 192 6.5.3 Partial Evaluation and Ginger Primitives 194 6.6 Further Examples and Results 196 6.6.1 Matrix Multiplication 196 6.6.2 Gaussian Elimination 200 6.6.3 Exponentiation . 206 6.6.4 Polynomials . 208 6.6.5 Integration by Simpson's Rule 209 6.6.6 Fuzzy Systems 212 6.6.7 Results .... 215 vi 6.7 Summary . 220 Chapter 7 Conclusion and Further Work 221 7.1 Integrating Aladin and Ginger 223 7.2 Ginger and Type Systems 223 7.3 Parallel Aladin . 226 7.4 Other Aladin Enhancements . 228 Bibliography 230 vii List of Tables 4.1 Initial running times of programs produced by the Ginger compiler. 92 4.2 Running times (s) of programs produced by the Ginger compiler with and without direct function invocation.. .. 94 4.3 Running times (s) of programs produced by the Ginger compiler with and without single-instance constants. .. 97 4.4 Comparisons of running times of various lazy functional language implementations 98 4.5 Running times (s) of programs produced by the Ginger compiler with initial heap sizes of 1 and 4 megabytes ... 100 5.1 Aladin infix operators and their prefix form 126 5.2 Aladin 'dot-dot' lists and their prefix form. 128 6.1 Comparisons of running times of Aladin programs with and without partial evaluation. 216 6.2 Compile + Run times of Aladin programs with and without partial evaluation . 217 6.3 Comparisons of Running times of Ginger programs with and without partial evaluation . 219 Vlll List of Figures 2.1 The steps taken by an Abstract Machine. 18 3.1 Crisp definition of Profit. 26 3.2 Fuzzy definition of Profit. 26 3.3 Standard fuzzy subset distributions . 27 3.4 Operations on fuzzy subsets. .... 30 3.5 Very profitable and Somewhat profitable. 33 3.6 Fuzzy approximations to 20. 35 3.7 Defuzzifying a fuzzy subset 36 3.8 The fuzzy rule base for the height ~ shoe size expert system 42 3.9 Weighting, adding and defuzzifying the rules for a height of 1.75m 44 3.10 Fuzzy subsets of temperature, flow and tap change ......... 48 4.1 The EBNF of the Ginger language after lambda-lifting and depen- dency analysis. 64 5.1 An generic unwound application 108 5.2 Application of a function to too many arguments 109 5.3 Application of a function to too many arguments after evaluation of inner application . 109 5.4 The fp. aladin package 129 ix 5.5 A GU! for Aladin. .. 168 x Acknowledgments I would primarily like to thank my supervisor, Mike Joy, for his advice and help throughout the duration of my research, for getting me on the course in the first place, and not being too uptight about the size of my coffee bill. I'd also like to thank Steve Matthews for his encouragement, especially with regards to the fuzzy stuff (surely I'm allowed one colloquialism, Mike); Tom Axford, who along with Mike Joy, laid the groundwork for Aladin; and Ananda Amatya for various comments and papers over the years. In addition, I'd like to thank Mike and Steve for their comments and editorial advice on this thesis, and their corrections of my lousy typing. Finally, I'd like to say to Richard Gault: 'Beat you!' xi Declarations The work on fuzzy logic and functional programming, described in Chapter 3, is based largely on that detailed in [77] published in the Journal of Functional pro- gramming and its preceding technical report [75]. The published paper was written in collaboration with my supervisor, Dr Mike Joy, who acted in an advisory and editorial capacity; the technical report was authored by me alone. The Ginger compiler described in Chapter 4 has its origins in the technical report written by Dr Joy [53]. The compiler was written from scratch by me alone, apart from the parser for the language which is based on one developed by Dr Joy for a Ginger interpreter written in Java. The text of Chapter 4 is based on [78]published in Software - Practice and Experience, co-authored with Dr Joy who acted in an advisory and editorial capacity. An earlier version of the compiler is described in a technical report [74], written solely by me. My work on Aladin detailed in Chapters 5 and 6 is based on that done by Dr Joy and Dr Tom Axford of Birmingham University [10, 11]. I developed the updating denotational semantics for Aladin, with and without partial evaluation, from which an operational semantics for Aladin was obtained. My implementation of the Aladin without partial evaluation, based on this semantics, has previously been described in a technical report [76]. Nothing has yet been published on my work on partial evaluation and Aladin. xii Abstract This thesis explores the application of functional programming in new areas and its implementation using new technologies. We show how functional languages can be used to implement solutions to problems in fuzzy logic using a number of languages: Haskell, Ginger and Aladin. A compiler for the weakly-typed, lazy language Ginger is developed using Java byte-code as its target code. This is used as the inspiration for an implementation of Aladin, a simple functional language which has two novel features: its primitives are designed to be written in any language, and evaluation is controlled by declaring the strictness of all functions. Efficient denotational and operational semantics are given for this machine and an implementation is devel- oped using these semantics. We then show that by using the advantages of Aladin (simplicity and strictness control) we can employ partial evaluation to achieve con- siderable speed-ups in the running times of Aladin programs. xiii Chapter 1 Introduction Functional programming [15, 26, 40, 93] is, as its name suggests, programming with functions, by defining them and applying them to arguments.