Conception, Evolution, and Application of Functional Programming Languages
Total Page:16
File Type:pdf, Size:1020Kb
Conception, Evolution, and Application of Functional Programming Languages PAUL HUDAK Yale University, Department of Computer Science, New Haven, Connecticut 06520 The foundations of functional programming languages are examined from both historical and technical perspectives. Their evolution is traced through several critical periods: early work on lambda calculus and combinatory calculus, Lisp, Iswim, FP, ML, and modern functional languages such as Miranda’ and Haskell. The fundamental premises on which the functional programming methodology stands are critically analyzed with respect to philosophical, theoretical, and pragmatic concerns. Particular attention is paid to the main features that characterize modern functional languages: higher-order functions, lazy evaluation, equations and pattern matching, strong static typing and type inference, and data abstraction. In addition, current research areas-such as parallelism, nondeterminism, input/output, and state-oriented computations-are examined with the goal of predicting the future development and application of functional languages. Categories and Subject Descriptors: D.l.l [Programming Techniques]: Applicative (Functional) Programming; D.3.2 [Programming Languages]: Language Classifications-applicative languages; data-flow languages; nonprocedural languages; very high-level languages; F.4.1 [Mathematical Logic and Formal Languages]: Mathematical Logic-lambda calculus and related systems; K.2 [History of Computing]: Software General Terms: Languages Additional Key Words and Phrases: Data abstraction, higher-order functions, lazy evaluation, referential transparency, types INTRODUCTION ent kinds of machines increased, the need The earliest programming languages were arose for a common language with which to developed with one simple goal in mind: to program all of them. provide a vehicle through which one could Thus from primitive assembly lan- control the behavior of computers. Not sur- guages (which were at least a step up from prisingly, the early languages reflected the raw machine code) there grew a plethora of structure of the underlying machines fairly high-level programming languages, begin- well. Although at first blush that goal ning with FORTRAN in the 1950s. The seems eminently reasonable, the viewpoint development of these languages grew so quickly changed for two very good reasons. rapidly that by the 1980s they were best First, it became obvious that what was easy characterized by grouping them into fami- for a machine to reason about was not lies that reflected a common computation necessarily easy for a human being to rea- model or programming style. Debates over son about. Second, as the number of differ- which language or family of languages is best will undoubtedly persist for as long as 1 Miranda is a trademark of Research Software Ltd. computers need programmers. Permission to copy without fee all or part of this material is granted provided that the copies are not made or distributed for direct commercial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission. 0 1989 ACM 0360-0300/89/0900-0359 $01.50 ACM Computing Surveys, Vol. 21, No. 3, September 1989 360 . Paul Hudak CONTENTS the significant interest current researchers have in functional languages and the im- pact they have had on both the theory and pragmatics of programming languages in INTRODUCTION general. Programming Language Spectrum Among the claims made by functional Referential Transparency and Equational language advocates are that programs can Reasoning Plan of Study be written quicker, are more concise, are 1. EVOLUTION OF FUNCTIONAL LANGUAGES higher level (resembling more closely tra- 1.1 Lambda Calculus ditional mathematical notation), are more 1.2 Lisp amenable to formal reasoning and analysis, 1.3 Iswim 1.4 APL and can be executed more easily on parallel 1.5 FP architectures. Of course, many of these fea- 1.6 ML tures touch on rather subjective issues, 1.7 SASL, KRC, and Miranda which is one reason why the debates can be 1.8 Dataflow Languages so lively. 1.9 Others 1.10 Haskell This paper gives the reader significant 2. DISTINGUISHING FEATURES OF MODERN insight into the very essence of functional FUNCTIONAL LANGUAGES languages and the programming method- 2.1 Higher Order Functions ology that they support. It starts with a 2.2 Nonstrict Semantics (Lazy Evaluating) 2.3 Data Abstraction discussion of the nature of functional lan- 2.4 Equations and Pattern Matching guages, followed by an historical sketch of 2.5 Formal Semantics their development, a summary of the dis- 3. ADVANCED FEATURES AND ACTIVE tinguishing characteristics of modern func- RESEARCH AREAS tional languages, and a discussion of 3.1 Overloading 3.2 Purely Functional Yet Universal I/O current research areas. Through this study 3.3 Arrays we will put into perspective both the power 3.4 Views and weaknesses of the functional program- 3.5 Parallel Functional Programming ming paradigm. 3.6 Caching and Memoization 3.7 Nondeterminism A Note to the Reader: This paper as- 3.8 Extensions to Polymorphic-Type Inference sumes a good understanding of the funda- 3.9 Combining Other Programming Language mental issues in programming language Paradigms design and use. To learn more about mod- 4. DISPELLING MYTHS ABOUT FUNCTIONAL ern functional programming techniques, PROGRAMMING 5. CONCLUSIONS including the important ideas behind rea- REFERENCES soning about functional programs, refer to Bird and Wadler [1988] or Field and Har- rison [1988]. To read about how to imple- ment functional languages, see Peyton Jones [ 19871 (additional references are The class of functional, or applicative, given in Sections 1.8 and 5). programming languages, in which compu- Finally, a comment on notation: Unless tation is carried out entirely through the otherwise stated, all examples will be writ- evaluation of expressions, is one such fam- ten in Haskell, a recently proposed func- ily of languages, and debates over its merits tional language standard [Hudak and have been quite lively in recent years. Are Wadler 19881. Explanations will be given functional languages toys? Or are they in square brackets [ ] as needed.’ tools? Are they artifacts of theoretical fan- tasy or of visionary pragmatism? Will they ‘Since the Haskell Report is relatively new, some ameliorate software woes or merely com- minor changes to the language may occur after this pound them? Whatever answers we might paper has appeared. An up-to-date copy of the Report have for these questions, we cannot ignore may be obtained from the author. ACM Computing Surveys, Vol. 21, NO. 3, September 1989 Functional Programming Languages 361 Programming Language Spectrum in the functional language Haskell by Imperative languages are characterized as fat x 1 having an implicit state that is modified where fat n a (i.e., side effected) by constructs (i.e., com- = if n>O then fat (n-l) (a*n) mands) in the source language. As a result, else a such languages generally have a notion of in which the formal parameters n and a are sequencing (of the commands) to permit examples of carrying the state around ex- precise and deterministic control over plicitly, and the recursive structure has the state. Most, including the most pop- been arranged so as to mimic as closely as ular, languages in existence today are possible the looping behavior of the pro- imperative. gram given earlier. Note that the condi- As an example, the assignment state- tional in this program is an expression ment is a (very common) command, since rather than command; that is, it denotes a its effect is to alter the underlying implicit value (conditional on the value of the pred- store so as to yield a different binding for icate) rather than a sequencer of com- a particular variable. The begin . end mands. Indeed the value of the program is construct is the prototypical sequencer of the desired factorial, rather than it being commands, as are the well-known goto found in an implicit store. statement (unconditional transfer of con- Functional (in general, declarative) pro- trol), conditional statement (qualified se- gramming is often described as expressing quencer), and while loop (an example of a what is being computed rather than how, structured command). With these simple although this is really a matter of degree. forms, we can, for example, compute the For example, the above program may say factorial of the number X: less about how factorial is computed than the imperative program given earlier, but n:= x; is perhaps not as abstract as a := 1; while n>O do fat x begin a := a*n; where fat n n := n-l = if n==O then 1 end; else n*fac (n-l) [== is the infix operator for equality], which appears very much like the mathe- After execution of this program, the value matical definition of factorial and is indeed of a in the implicit store will contain the a valid functional program. desired result. Since most languages have expressions, In contrast, declarative languages are it is tempting to take our definitions liter- characterized as having no implicit state, ally and describe functional languages via and thus the emphasis is placed entirely on derivation from conventional programming programming with expressions (or terms). languages: Simply drop the assignment In particular, functional languages are dec- statement and any other side-effecting larative languages whose underlying model primitives. This approach, of course, is very of computation is the function (in contrast misleading. The result of such a derivation to, for example, the relation that forms the is usually far less than satisfactory, since basis for logic programming languages). the purely functional subset of most im- perative languages is hopelessly weak In a declarative language state-oriented (although there are important exceptions, computations are accomplished by carrying such as Scheme [Rees and Clinger 19861).