Functional Programming in CLEAN
Total Page:16
File Type:pdf, Size:1020Kb
Functional Programming in CLEAN DRAFT JULY 1999 Functional Programming in CLEAN Preface Functional languages enable programmers to concentrate on the problem one would like to solve without being forced to worry too much about all kinds of uninteresting implemen- tation details. A functional program can be regarded as an executable specification. Func- tional programming languages are therefore popular in educational and research environ- ments. Functional languages are very suited for teaching students the first principles of programming. In research environments they are used for rapid prototyping of complex systems. Recent developments in the implementation techniques and new insights in the underlying concepts such as input/output handling make that modern functional languages can nowadays also be used successfully for the development of real world applications. The purpose of this book is to teach practical programming skills using the state-of-the art pure functional language CONCURRENT CLEAN. CLEAN has many aspects in common with other modern functional languages like MIRANDA, HASKELL and ML. In addition CLEAN offers additional support for the development of stand-alone window based applications and it offers support for process communication and for the development of distributed applications. This book on functional programming using CLEAN is split into three parts. In the first part an introduction into functional programming is given. In six Chapters we treat the basic aspects of functional programming, functions, data structures, the type sys- tem and I/O handling. The idea is that you are able to write simple functions and applica- tions as soon as possible. The intention is not to treat all available features of CLEAN but only the most important ones that are most commonly used. A complete description of all available language constructs can be found in the CLEAN language manual. The main emphasis of this book lies in the second part in which several case studies are presented. Each case treats a tiny, but complete application in an illustrative problem do- main. The case studies include applications like a simple database, an object-oriented drawing program, a data compression utility, an interpreter for a functional language. Each case furthermore illustrates a certain aspect of functional programming. Some case applica- tions are reused in others to illustrate the reusability of code. In the third part of this book we discuss the different kinds of programming development techniques for functional programming and efficiency aspects are treated. So, a lot of material is presented in this book. However, one certainly does not have to work through all case studies. Depending on the programming experience already acquired and the time available one can use this book as a textbook for or one or two semester course on functional programming. The book can be used as an introductory textbook for people with little programming experience. It can also be used for people who already have programming experience in other programming paradigm (imperative, object-oriented or logical) and now want to learn how to develop applications in a pure functional language. We hope that you enjoy the book and that it will stimulate you to use a functional language for the development of your applications. Table of Contents Preface i Table of Contents iii Introduction to Functional Programming 1 1.1 Functional languages 1 1.2 Programming with functions 2 1.2.1 The `Start' expression 2 1.2.2 Defining new functions 3 1.2.3 Program evaluation with functions 4 1.3 Standard functions 5 1.3.1 Names of functions and operators 5 1.3.2 Predefined functions on numbers 5 1.3.3 Predefined functions on Booleans 6 1.3.4 Predefined functions on lists 6 1.3.5 Predefined functions on functions 7 1.4 Defining functions 7 1.4.1 Definition by combination 7 1.4.2 Definition by cases 8 1.4.3 Definition using patterns 9 1.4.4 Definition by induction or recursion 10 1.4.5 Local definitions, scope and lay-out 11 1.4.6 Comments 12 1.5 Types 12 1.5.1 Sorts of errors 13 1.5.2 Typing of expressions 14 1.5.3 Polymorphism 15 1.5.4 Functions with more than one argument 15 1.5.5 Overloading 16 1.5.6 Type annotations and attributes 17 1.5.7 Well-formed Types 17 1.6 Synonym definitions 19 1.6.1 Global constant functions (CAF’s) 19 1.6.2 Macro’s and type synonyms 19 1.7 Modules 20 1.8 Overview 22 1.9 Exercises 22 Functions and Numbers 23 2.1 Operators 23 2.1.1 Operators as functions and vice versa 23 2.1.2 Priorities 23 2.1.3 Association 24 2.1.4 Definition of operators 25 iv FUNCTIONAL PROGRAMMING IN CLEAN 2.2 Partial parameterization 26 2.2.1 Currying of functions 26 2.3 Functions as argument 27 2.3.1 Functions on lists 28 2.3.2 Iteration 28 2.3.3 The lambda notation 29 2.3.4 Function composition 30 2.4 Numerical functions 31 2.4.1 Calculations with integers 31 2.4.2 Calculations with reals 34 2.5 Exercises 36 Data Structures 37 3.1 Lists 37 3.1.1 Structure of a list 37 3.1.2 Functions on lists 40 3.1.3 Higher order functions on lists 44 3.1.4 Sorting lists 47 3.1.5 List comprehensions 48 3.2 Infinite lists 51 3.2.1 Enumerating all numbers 51 3.2.2 Lazy evaluation 52 3.2.3 Functions generating infinite lists 53 3.2.4 Displaying a number as a list of characters 53 3.2.5 The list of all prime numbers 54 3.3 Tuples 55 3.3.1 Tuples and lists 57 3.4 Records 58 3.4.1 Rational numbers 60 3.5 Arrays 61 3.5.1 Array comprehensions 62 3.5.2 Lazy, strict and unboxed arrays 63 3.5.3 Array updates 63 3.5.4 Array patterns 64 3.6 Algebraic datatypes 64 3.6.1 Tree definitions 66 3.6.2 Search trees 67 3.6.3 Sorting using search trees 69 3.6.4 Deleting from search trees 70 3.7 Abstract datatypes 70 3.8 Correctness of programs 71 3.8.1 Direct proofs 72 3.8.2 Proof by case distinction 73 3.8.3 Proof by induction 74 3.8.4 Program synthesis 76 3.9 Run-time errors 77 3.9.1 Non-termination 78 3.9.2 Partial functions 78 3.9.3 Cyclic dependencies 79 3.9.4 Insufficient memory 80 3.10 Exercises 82 FUNCTIONAL PROGRAMMING IN CLEAN v The Power of Types 83 4.1 Type Classes 83 4.1.2 A class for Rational Numbers 87 4.1.3 Internal overloading 88 4.1.4 Derived class members 88 4.1.5 Type constructor classes 89 4.2 Existential types 90 4.3 Uniqueness types 96 4.3.1 Graph Reduction 96 4.3.2 Destructive updating 98 4.3.4 Uniqueness information 99 4.3.5 Uniqueness typing 100 4.3.5 Nested scope style 102 4.3.6 Propagation of uniqueness 103 4.3.7 Uniqueness polymorphism 104 4.3.8 Attributed datatypes 105 4.3.9 Higher order uniqueness typing 107 4.3.10 Creating unique objects 108 4.4 Exercises 108 Interactive Programs 109 5.1 Changing files in the World 109 5.2 Environment Passing Techniques 112 5.2.1 Nested scope style 113 5.2.2 Monadic style 114 5.2.3 Tracing program execution 115 5.3 Handling Events 116 5.3.1 Events 116 5.4 Dialogs 118 5.4.1 A Hello World Dialog 119 5.4.2 A File Copy Dialog 120 5.4.3 Function Test Dialogs 122 5.4.4 An Input Dialog for a Menu Function 126 5.4.5 Generic Notices 127 5.5 Windows 129 5.5.1 Hello World in a Window 130 5.5.2 Peano Curves 131 5.5.3 A Window to show Text 135 5.6 Timers 139 5.7 A Line Drawing Program 140 5.8 Exercises 147 Efficiency of programs 149 6.1 Reasoning about efficiency 149 6.1.1 Upper bounds 150 6.1.2 Under bounds 151 6.1.3 Tight upper bounds 152 6.2 Counting reduction steps 152 6.2.1 Memorization 153 6.2.2 Determining the complexity for recursive functions 154 vi FUNCTIONAL PROGRAMMING IN CLEAN 6.2.3 Manipulation recursive data structures 156 6.2.4 Estimating the average complexity 157 6.2.5 Determining upper bounds and under bounds 160 6.3 Constant factors 160 6.3.1 Generating a pseudo random list 162 6.3.2 Measurements 163 6.3.3 Other ways to speed up programs 164 6.4 Exploiting Strictness 166 6.5 Unboxed values 167 6.6 The cost of Currying 169 6.6.1 Folding to the right or to the left 171 6.7 Exercises 172 Index 173 Part I Chapter 1 Introduction to Functional Programming 1.1 Functional languages 1.6 Synonym definitions 1.2 Programming with functions 1.7 Modules 1.3 Standard functions 1.8 Overview 1.4 Defining functions 1.9 Exercises 1.5 Types 1.1 Functional languages Many centuries before the advent of digital computers, functions have been used to de- scribe the relation between input and output of processes. Computer programs, too, are descriptions of the way a result can be computed, given some arguments. A natural way to write a computer program is therefore to define some functions and applying them to con- crete values.