Download On Style, Expressibility, and Efficiency in Functional Programming Languages and more Lecture notes Programming Languages in PDF only on Docsity!
THE
UCLA
COMPUTER SCIENCE
DEPART"TMENT
QUARTERLY
THE DEPARTMENT
AND ITS PEOPLE
FALL 1987/WINTER 1988
VOL. 16 NO. 1
SCHOOL OF ENGINEERING AND^ APPLIED^ SCIENCE
COMPUTER SCIENCE DEPARTMENT OFFICERS
Dr. Gerald Estrin, Chair Dr. Jack W. Carlyle, Vice Chair, Planning & Resources Dr. Sheila Greibach, Vice Chair, Graduate Affairs Dr. Lawrence McNamee, Vice Chair, Undergraduate Affairs Mrs. Arlene C. Weber, Management Services (^) Officer Room 3713 Boelter Hall
QUARTERLY STAFF
Dr. Sheila Greibach, Editor Ms. Marta Cervantes, Coordinator
Material in this publication may not be reproduced without written permission of the Computer Science Department.
Subscriptions to this publication are available at
$20.00/year (three issues) by contacting Ms. Brenda Ramsey,
UCLA Computer Science Department, 3713 Boelter Hall,
Los Angeles, CA 90024-1596.
Fall-Winfer CSD Quarterly
CONTENTS
COMPUTER SCIENCE AT UCLA AND COMPUTER SCIENCE IN
THE UNITED STATES
GeraldEstrin, Chair ...................................................................................................... I
THE UNIVERSITY CONTEXT .............................................................................................. 13
THE UCLA COMPUTER SCIENCE DEPARTMENT
The Faculty A rane nf;ntraract B
P
iographies .............................................................................................................................. 18 publications .............................................................................................................................. 33 Honors and awards .......................................................................................................... 40 The Students Biographies .............................................................................................................................. 41 Ph.D. Degrees Awarded In Calendar Year 1987 ........................................................... 57 Ph.D. Students In Advanced Stage ................................................................................. 58 M.S. Degrees Awarded In Calendar Year 1987 ............................................................. 62 M.S. Students In Advanced Stage ..................................................................................... 64 The Staff .......................................................................................................................................... 66 The Visitors ..................................................................................................................................... 67 The Supporters ............................................................................................................................... 68
DEPARTMENT ORGANIZATION
Committees .............................................................................................................................. 70 Administrative Assignments ........................................................................................... 71 Activities ................................................................................................................................... 73 Student Organizations ..................................................................................................... 76
ADMISSIONS DEADLINES, FACULTY POSITIONS
AND AFFIRM ATIVE ACTION ...................................................................................... 78
COMPUTER SCIENCE DEPARTMENT TECHNICAL REPORTS ............................... 80
TECHNICAL PAPERS
On Style, Expressability, and Efficiency in Functional Programming Languages GrabrielRobins ............................................................................................................... 106
On Style, Expressibility, and Efficiency in Functional Programming Languages
Gabriel Robins
Computer Science Department University of California, Los Angeles
Abstract
A functional style of programming was proposed by Backus as an alternative to conventional programming paradigms. Functional programming (^) is intended to alleviate what Backus dubbed as the "Von Neumann Bottleneck," a restrictive set of biases deeply ingrained in the thought processes of programmers. The functional programming paradigm is investigated here through the actual implementation of a series of common algorithms. One of the novel features of this paradigm is the lack of side-effects; it is shown how this property can be used to achieve considerable efficiency improvement in arbitrary implementations of functional languages, using the idea of value-caching. This scheme is then shown to reduce the execution of certain computations from (^) exponential-time to polynomial-time, a formidable speedup. It is also shown that the expressive power of functional languages can be greatly enhanced via the utilization of both macros and idioms, without changing the original semantics of the language.
- Introduction
A functional style of programming was proposed in 1978 by John Backus [Backus] as an alternative to conventional programming paradigms. Functional programming is intended to alleviate what Backus dubbed as the "Von Neumann Bottleneck," a restrictive set of biases which by now is deeply ingrained in the thought processes of programmers. Backus argues quite eloquently that because the processor and the memory of a conventional computer are connected via a narrow bus, processors end up spending most of their time ferrying useless data to and from the main memory.
The more serious problem, however, is that the classical "word-at-a-time" model of computer design has over the years managed to permeate up through the abstraction levels and become fused into most programming languages, and indeed into the very thought processes of the majority of computer scientists. According to Backus, this phenomenon is directly responsible for many horrendously complex programming languages with convoluted and even inconsistent semantics, where it is often impossible to prove formally even the most trivial correctness properties.
Backus gave (^) a syntax and semantics for a candidate functional programming language. Some of the novel features of his language are the lack of variable names, assignment statements, global state, and side-effects. Instead, several powerful combining operators can be used to compose functions, yielding clean and simple semantics, conciseness of expression, and (^) easily-derivable formal correctness properties. See [Vegdahl] for a more (^) detailed discussion of these issues. Considerable work regarding FP has also been performed at UCLA: [Alkalaji, [Alkalaj, Ercegovac, and Lang], [Arabe], [Meshkinpour], [Meshkinpour and Ercegovac], [Patel and Ercegovac], [Patel, Schlag, and Ercegovac], [Pungsomruk], [Sausville], [Schlag 86], [Schlag 84], [Worley].
In this (^) paper I investigate some of the claims made by proponents of functional programming, through the actual implementation of a series of common algorithms. Based on these examples, I then draw some conclusions with regards to the practical advantages and flaws of the functional programming paradigm. The language I use is Berkeley's functional programming language, FP [Baden]. All FP forms which appear in this paper were verified to be correct by actually typing them into the FP v. 4.2 (4/28/83) system. The rest of the paper assumes that the reader is familiar with Berkeley FP; for a brief refresher on the syntax and semantics of FP, the reader is referred to the appendix.
arguing that it is more the responsibility of the machines to catch up with functional paradigms of thinking and not the burden of functional programming to accommodate archaic architectures. The situation is dramatized in the following diagram:
Functional Programming
Hardware Support
2.2. List Reversal
Suppose we wanted to reverse a list. What first comes to mind is a recursive definition for the
reversal operation, namely that the reversal of a list is equal to the last element followed by the
reversal of the rest of the list. In FP this could be expressed as follows:
{REV1 (null -> id; apndl @ [lastREVl @ tir]))
For example:
REV1 @ iota: 13 ==>^ <13121110987654321>
In trying to determine whether any parallelism is inherent in list reversal, we try to apply the
highly-parallel "tree-insert" FP operator, as in the following:
{REV2 1 [2,1])
REV2 @ iota: 13 ==>^ <<<13^ <12^ 11>>^ <10^ <98>>>^ <<7^ <65>>^ <<43>^ <2^ 1>>>>
We observe that this simple function is almost what we want, except for the extra levels of list depth, so we revise our definition as follows:
{REV3 I (concat @ [(atom @ 2 -> [2] ; 2) , (atom @ 1 -> [1]; 1)] )}
REV3 @ iota: 13 ==> <13121110987654321>
Now we have obtained a highly parallel, yet simple and concisely-expressed function to reverse a
list. I consider this to be functional programming at its best.
t _kcf_*
2.3. Membership in a List
Suppose we needed a function that given an element and a list, would determine whether that
element appears in that list. Our first attempt is the following:
(MBMB! or @ &= @ distl)
MEMB : <13 <3 6 4 13 98 -45 13 73>> ==> T
The idea here is to distribute the search key over the entire list and "or" together the results of all
the comparisons of these pairs. Now suppose we know ahead of time that our list was sorted.
Using the classical binary-search technique, we could speed up the computation to "require" at
most O(log N) time by recursively searching half-lists, as follows:
{BS (null @ 3 -> = @ [1,1 @ 2];
(<= @ [1,1@rotr@2] -> BS @ [1,2]; BS @ [1,3])) @ apndl @ [l,split @ 2])
Note that although this "optimization" appears to reduce the evaluation time, this may not really
happen in reality. For example, a close examination of the code for BS would reveal that we have
assumed that the operation "split" operates in constant time. If indeed split is implemented in such
a way as to require time linear in the size of its argument, BS may in fact run slower than its
seemingly "less efficient" counterpart MEMB.
This example again illustrates our previous argument that FP programmers should not try to
"second-guess" the implementation, because they may loose. It is conceivable that in the future
intelligent compilers could algebraically manipulate such expressions and, for example, replace a
linear search with a binary search whenever it finds a monotonicity property to be preserved. So
again, I claim that such "optimizations" should be left to the compilers.
Of course one must draw the line somewhere. Given that the problem of whether two programs
are equivalent or not is undecidable, in the general case it is not possible to perform these
optimizations automatically. Yet even undecidability results do not mean that special classes of
instances could not be handled automatically, as well as efficiently. For example, it is probably
too much to hope that a compiler would recognize an O(N5)-time max-flow algorithm and replace
it with an equivalent but more efficient O(N3) time algorithm; on the other hand it is not far-
fetched to expect that an intelligent compiler could determine that a certain piece of code behaves
like a look-up table and consequently implement it as a hash-table rather than a list, yielding better
efficiency.
2.4. Fibonacci Numbers
The Fibonacci numbers are defined as the sequence of integers beginning with two l's with each
subsequent integer being the sum of the previous two. This recursive definition immediately
yields an FP program:
{F (<=@[id,%2] -> %1; +@[F @ D,F @ D @ D])) # returns the ith Fibonacci number
(D -@[id,%1]) # decrements by 1
{FIB &F @ iota) # returns the first n Fibonacci numbers
FIB : 18 :---> <1 12 3 5 8 13 2134 55 89 144 233 377 610 987 1597 2584>
Although this program is both elegant and concise, it is quite "inefficient", because any
conventional implementation of it would force the re-computation of the function on many of the
same arguments. We can alleviate this flaw as follows:
{FIB2 tlr@ tlr@ FF@ [iota, [%1,%l]]}
{FF (null @ 1 -> 2 ;FF@ [tl@l, apndr @ [2, +@ [last@2, last@tlr@2]]]))
Now successive Fibonacci numbers are appended to a growing list, where the last two elements
element. Each of these two sublists is similarly sorted in a recursive fashion,^ and^ the^ final^ result
then consists of the first list, followed by the pivot element, followed by the second list. The FP
implementation of quicksort follows:
{QS (<=@^ [length,%l]^ ->^ id;^ concat^ @ [QS^ @^ L,^ [1],^ QS^ @^ R]))
(L concat @ & (> -> tl; []) @ distl @ [1,tl]}
(R concat @ & (<=-> tl; []) @ distl @ [l,tl]}I
L: <7 3 9 -4 13 27> ==> <3 -4>
R: <7 3 9 -4 13 27> ==> <9 13 27>
QS : <76 13 -12 22 0 13 55 213 0 > ==> <-12 0 0^^13 13 22 55 76 213>
Again, we note the conciseness and elegance of the FP specification. Moreover, it is well-known
that quicksort has an O(NlogN) average-time behavior (although the normal worst-case behavior
is still quadratic). Later in this paper we describe how the FP definition of quicksort can be made
even more concise using macros.
2.6. Prime^ Numbers
A prime number is a positive integer with exactly two distinct divisors: itself and 1. We would
like to specify a functional program which given an integer N, returns a complete list of primes
between 2 and N, inclusively. Here is our first pass at this problem:
{PRIMES concat @ &(PRIME -> [id] ; [1) @ tl @ iota)
(PRIME land @ &(not @ = @ [%O,id]) @ &mod @ distl @ [id,tl@iota@.@ [id,%1]]}
PRIMES: 20 ==> <2 3 5 7 1113 17 19>
The idea here is to process all the integers between 2 and N, trying to determine which ones have
no factors other than themselves and 1. All integers satisfying this property are primes and are
therefore returned as a list. The function PRIME is a predicate which tests a single integer for
primality, while the function PRIMES maps PRIME onto an entire list of candidates for primes.
We note however, that to test for primality, we need only try to divide by factors no greater than
the square root^ of^ our^ candidate^ integer,^ since^ if^ our^ integer^ is divisible^ by a^ factor which^ is
greater than the square root, the result of the division^ is^ less^ than^ the square^ root^ and^ so we would
have tried that integer earlier. Furthermore, other than the integer 2, only odd integers need be
considered for primality, and only odd integers (up to the square-root of the candidate) need^ be
considered as factors, since an even integer can not divide an odd one. These "optimizations" are
reflected in a new version of our prime number generator:
(PRIMES apndl @ [%2, concat @ &(PRIME -> [id] ; []) @ ODDS])
(PRIME land @ &(not @ = @ [%0,id]) @ &mod @ distl @ [id,ODDS @ SQRT]]
(SQRT I @ (while (<@ [*@[id,id]@1, 2]) [+@[1,%1],2]) @ [%l,id])
{ODDS apndl@ [%2,&(+@ [*@ [id,%2],% 1])
@iota@-@ [+@ /@ [id,%2],mod@ [id,%2]],%l11]
PRIMES:50 ==> <23571113171923293137414347>
Note that while this^ scheme^ is^ "faster"^ than^ the^ previous^ one,^ the code^ is^ beginning^ to^ look^ rather
ugly. In fact, further speedup is possible since when checking for the primality of a candidate
integer, we need only to consider other primes (up to the square-root of the candidate) as possible
factors. The lesson here again is that the more "efficient" a functional program is, the messier it
tends to become.
In the case of prime numbers, for example, I think that only the first "optimization" (the one about
going only up to the square root) is of the type with which the functional programmers need
concern themselves, because it is "algorithmic" in nature, and relies on a non-trivial number-
theoretic fact; it so happens that it is also the only one which increases the "efficiency" of our
functional program by a non-constant factor.
We give another algorithm for prime generation, the sieve of Erastostene:.
(SIEVE concat @ &(MEMB -> [];[1]) @ distr @
[id,concat @ &&* @ &distl @ distr @ [idid]] @ tl @ iota)
(IEMMB !or@ &= @ distl)
SIEVE: 18 ==> <2 3 5 7 1113 17>
The idea here is to list all the integers and erase all factors of 2, all factors of 3, and so on.
Everything which remains after this process of elimination is by necessity a prime. Actually, in
this implementation we do the opposite: we form all possible products and then consider as prime
all the "missing" integers. Again we see an example of the trade-off between concise specification
and computational "efficiency".
3. FP vs. APL
There are some notable similarities between APL and FP is quite similar in spirit and syntax to
APL. Some functions and operators in FP are taken directly from APL; for example, the FP iota
function is equivalent to the APL iota function ("t"). Similarly, the FP operator "right-insert" ("! ")
is equivalent to the APL operator "reduce" ("I"). The difference with respect to these operators is
that in FP one can compose any number of operators together in a concise and semanticaly clean
manner, while in APL only a pre-defined limited set of types of compositions are allowed.
Both in FP and APL complex operations may be defined very concisely, often giving rise to "one-
line" programs. Yet unlike FP, the semantics of APL are very complex, being muddled by state,
local and global variables, parameter passing, operator overloading, goto statements, and the
existence of several hundreds of different functions and operators. I have seen one-line APL
programs which take the better part of an hour to understand; a possible moral here is that
"absolute power corrupts absolutely." While it is true that some pieces of FP code may also seem
rather dense, the situation in FP is still much better than in the notorious APL "one-liners".
4. FP vs. LISP
FP is also similar in many respects to LISP. Both languages rely heavily on lists as their primary
data structures, and both languages provide a rich set of list-manipulation primitives. On the other
hand, LISP functions are more defined than their FP counterparts; for example in FP tI : <> is
undefined, while in LISP (CDR NIL) is still NIL. Given that FP functions are bottom-preserving,
it is often the case that an FP function would return "?" on a boundary or pathological datum,
while the LISP equivalent would still return the "right" answer.
Here is an example of when it is preferable to have functions which are more defined on
"boundary conditions". Given an atom x and a list of atoms y, we would like to return the set of
all atoms in y which directly follow occurrences of x in y. The obvious way of specifying this in
FP elegantly is:
{F concat @ &(=@ [2,1@1] -> [2@1] ; []) @ distr @ [trans @ [tlr,tl] @ 2,11])
F:<3<m34 563 3x3>> ==> <4 3 x>
F: <y <>> ==>?
The problem here of course is that our function is not defined for the pathological case when the
second argument is the empty list, while it would be preferable to define the answer in that case to
be the empty list. We could modify our original function to check for that boundary condition as a
special case, but if the FP functions t] and tir were originally designed to return the empty list
when called upon the empty list, our function F would not have the problem mentioned here. For
example, if tl, tir, and trans were redefined thus:
{TL (= @ [ length, %0 ] -> 0 ; tl)}
expressed more than once; in particular, macros would allow the definition of an APPLY template, similar in spirit to the LISP apply function, something that is impossible to implement in "pure" (^) FP.
5.1. The Implementation of Macros
We emphasize that the usage of macros as defined here does not extend nor change the original semantics of FP in any way. In fact, an implementation of this idea might consist of a macro- expansion pre-processor which would take as input a specification in macro-enhanced FP (that is, an FP program with some macros) and output an equivalent "pure" FP program after properly doing the requisite macro expansion and substitution. The result would then be fed into the original FP processor.
For example, to implement a macro pre-processor for Berkeley FP, one could extend the original FP syntax to accommodate macros as described above, and construct a UNIX lex(l) and yacc(l) specification to lexically analyze and parse the new syntax. The associated production rules would perform the required macro expansion, producing as output an equivalent (^) FP program in the original "pure" syntax. The function "yyparse" can then be used as a "filter" to FP.
An easier alternative to using heavy-artillery such as yacc(l) and lex(l) consists of the following scheme: since many existing languages already have rather fancy macro-preprocessors, why not "fool" such a tool into doing our FP macro-processing work for us? For example, the standard UNIX C compiler has such a macro-preprocessor, and in fact it may be invoked without the actual C compilation. So an implementation of this scheme would consist of the following two steps:
1) Run the file containing "macro-enhanced" FP program through the C macro preprocessor and collect the resulting "pure" FP output in a file. This is accomplished by specifying the -E flag: cc -E macro-prog.fp > pure.fp
- Run the resulting file as usual through the normal FP system: fp < pure.fp
I have already tested this scheme with several examples, including the quicksort example (^) stated above, and it worked flawlessly. In fact, if one combines the above two steps into one UNIX command aliased to an appropriate name, the entire macro-expansion process would be transparent to the user. Of course, if the format of our proposed FP macros deviates at all from that of C, this trivial scheme would fail and we would be forced to revert back to our direct- translation scheme using lex(l) and yacc(l).
Macros could also be used to "modify" the syntax of FP by providing alternate ways of expressing FP constructs; such extensions to FP in no way alter the original semantics of FP but simply provide some "syntactic sugar" to ease the programming task.
- The Need for Idioms
I found that during FP programming, many constructs repeat themselves. For example, the function to decrement a counter, to sort, etc. Such common and repeatedly used functions can and should be provided for in the implementation of an FP-like language. Indeed such functions can be easily specified within FP itself, thereby not adding to the original set of "primitives", but instead be provided as a library of functions or macros.
I claim that it would be very convenient to define certain idioms within FP. Note that this convenience is not in conflict with the fact that such idioms may be easily specified using other FP forms. For example, the original definition of FP still contains the idiom "length", even though "length" may easily be defined as a recursive application of ti, or perhaps even more cleverly as:
{length I+ @ &%l)
Perlis and Rugaber have already devised such a set of (^) idioms for APL [Perlis and Rugaber]. I believe that a similar (^) set should be identified and defined for FP. In particular, I recommend the following list of possible idioms as a starting-point for the further development of this idea:
Removal of duplicates from a list - given a key x and a list y, return a new list z which is a copy of
y except that all elements equal to x have been removed.
Sorting a list given a specified partial-order predicate - given a list, return the list sorted according
to a specified (or default) comparison predicate. Since the said predicate is an argument to our
"sort" routine, an implementation using macros would probably be best here.
Generalized 'iota' - given A, B, and C, return a sequence of all multiples of C, beginning with A
and no larger than B.
Vector element uniqueness - given a sequence of objects, return "true" if there exists a repeated
element in this sequence, or "false" otherwise.
Is X a permutation of Y? - given two sequences x and y, determine whether x is a permutation of
y. This may be accomplished by sorting both into some "canonical order" and then comparing.
Minimum/maximum element - find the minimum (or maximum) element in a given sequence,
using a specified min (or max) function, or else use a default comparison function.
Membership in a vector/set - test whether a given element x is a member of vector/set y.
Union of vectors/sets - given two vectors/sets x and y, return a vector/set of elements which are
either in x or in y, without duplicates.
Intersection of vectors/sets - given two vectors/sets x and y, return a vector/set of elements which
are both in x and in y, without duplicates.
Subtraction of vectors/sets - given two vectors/sets x and y, return a vector/set of elements which
are in x but not in y, without duplicates.
Subset of a vector/set - given two vectors/sets x and y, determine whether each element of x is
also an element of y.
Transitive closure - given a sequence of pairs or objects, return the transitive-closure of this set,
interpreting the pairs as a specification of a binary relation.
Math functions - square root, exponentiation, factorials, binomial coefficients, successor,
predecessor, etc.
Flatten a list - return a list of all atoms in a given list, disregarding the original level (or nesting-
depth) of each.
7. Improved Efficiency Through Value-Cashing
We have observed that there exists a sharp trade-off in certain computations between
conciseness-of-expression and "computational efficiency." For example, in the case of generating
Fibonacci numbers, the elegant version of the algorithm appears as follows:
{F (<=@[id,%2] -> %1; +@[F @ D,F @ D @ D])) # returns the i0 Fibonacci number
(D - @[id,%l]) # decrements by 1
We also observed earlier that alternative ways of specifying the Fibonacci function yield more
complex and difficult to read specifications. So the question now is how efficiently can the current
specification be used to compute the id Fibonacci number on a conventional architecture, and
whether the situation can be improved. We settle the former part of the question first, and argue
that a conventional implementation would require exponential time to compute the current
Fibonacci numbers specification.
It is well-known that the Nh Fibonacci number is given by the expression:
@$ - value-caching is always performed. @^$ - value-caching is never performed. @ - architecture automatically decides when^ cashing should^ be performed.
Einstein discovers that time is actually money
Other instances where value-caching will yield considerable increase in execution efficiency are applications where computations have a geometric meaning in Euclidean D-space. For example, Conway's game of "life", where a cellular automata in the 2-dimensional plane is simulated and where the "next-generation" rule is a function of the local neighborhood surrounding the cell. A straight-forward recursive computation to determine the state of a given cell would run in time exponential in the magnitude of the generation to be computed, where the base corresponds to the size of the neighborhood around cells. On the other hand, the same algorithm running in a value- caching environment would run in polynomial time.
Generally (^) speaking, whenever a dynamic-programming algorithm of some kind is used, new values are dependent on certain old values (with respect to some set of indices), an therefore an ordinary functional solution would likely to give rise to an inefficient execution. But if value- caching is used in the implementation, the execution time of the exact same algorithm is likely to improve drastically.
Recently it has come to my attention that the idea of caching values in order to avoid recomputations is also discussed in [Keller and Sleep]. They discuss several ways of storing and using old values in order to improve execution efficiency, and also address the problem of "purging" or keeping (^) the cache size from (^) growing without bounds (^) during a computation. (^) They also give as example another application area where value-caching would greatly speed up the execution time, namely in (^) the finite-element method.
- Conclusion
Through a series of examples I have investigated programming style and practical considerations
in FP. I have demonstrated that it is often the case in FP that concise functions become verbose
and complicated when certain "efficiency optimizations" are attempted. I have also argued that
certain so-called "optimizations" may really not be optimizations at all, and should not be
undertaken by an FP programmer.
I have briefly discussed the similarities between FP and APL and LISP, and argued that certain FP
functions should be "more defined" and that there is a need for some additional FP "primitive"
functions or macros. I described a scheme where macros can be implemented in a clean way,
leaving the original semantics of FP intact. I have argued that FP would benefit from a library of
idioms, or commonly-used constructs upon which the programmer can draw.
It is possible via novel architectures and intelligent compilers to support efficient FP-like
languages, where many optimizations can take place automatically, leaving the programmer free
to see much of the high-level picture without drowning in the details. I introduced one such
possible optimization, value-caching, and showed that it can reduce the execution time of certain
computations by as much as an exponential amount.
9. Acknowledgments
I would like to thank Professor Milos Ercegovac for introducing me to FP, and for the numerous
illuminating discussions which inspired this paper. Warm thanks go to Professor Sheila Greibach
for encouraging me to polish up this paper and submit it to the quarterly; she made numerous
thoughtful comments, and also helped in unsplitting my infinitives.
10. Bibliography
Alkalaj, L., "A Uniprocessor Implementation of FP Functional Language", MS Thesis, UCLA
Computer Science Department TR CSD-860064, April 1986.
Alkalaj,L., Ercegovac, M., and Lang, T., "A Dynamic Memory Management Policy for FP", 1987
HawaiiInternationalConference on Systems Science.
Arabe, J., "Compiler Considerations and Run-Time Storage Management for a Functional
Programming System", Ph.D. Dissertation, UCLA Computer Science Department, CSD TR 86004,
August 1986.
Backus, J., "Can Programming Be Liberated from the Von Neumann Style? A Functional Style
and its Algebra of Programs", Communications of the ACM, Vol 21, No. 8, August, 1978, pp.
Baden, S., Berkeley FP User's Manual, Rev. 4.1, September 29, 1987.
Keller, R., Sleep, R., "Applicative Caching: Programmer Control of Object Sharing and Lifetime
in Distributed Implementations of Applicative Languages", Proceedings of the 1981 ACM
Conference on FunctionalProgrammingLanguages and Computer Architectures, October, 1981,
pp. 131-140.
Meshkinpour, F., "On Specification and Design of Digital Systems Using and Applicative
Hardware Description Language", M.S. Thesis, UCLA Computer Science Department,Technical
Report No. CSD-840046, November 1984.
Meshkinpour, F., and Ercegovac, M., "A Functional Language for Description and Design of
Digital Systems: Sequential Constructs", IEEE Proc. of the 22nd ACMIJEEE Design Automation
Conference, 1985, pp. 2 3 8^ - 2 4 4^.
Patel, D., and Ercegovac, M., "A High-Level Language for Hardware-Oriented Algorithm
Design," presented at the Symposium Electronics and Communicationsin the 80's, Indian Institute
of Technology, Bombay, February 1983.
Patel, D., Schlag, M., and Ercegovac, M., "vFP: An Environment for the Multi-Level
first: <<1 2> <3 4 5> <6 7 8 9>> ==> <1 2> tI : ==> tlr : ==>
Structural and list-manipulation primitives:
distl: x =--> if x=<y,<>> then <>
else if x=<y,<x 1 ,x 2 , -.. ,x, > > and 15n then < <y,x 1 >,< yx 2 >, • ,< y~xn > > else?
distr : x => if xf<<>,y> then <>
else if xf<<x 1 ,x 2 , ... ,x.>,y>andl_<nthen<<x 1 ,y>, <x 2 ,y>, ... ,<x,,y >> else?
apndl: x => if x=<y,<>> then
else ifx=<y,< xlx 2 , • • • ,X,>> and 1:5n then <y,xlX 2 , ,x>else?
apndr : x ==> if xf<<>,y> then
else if x<<x 1 ,x 2 , x,.,xn>,y> and ln then <x 1 ,x 2 , ... xn,y> else?
traits : x ==> if xf<<xlx 2 , ,Xn>,<YI,Y2, ,Yn>, .. ,<Z,Z 2 , ,Zn>> and 1!n
then <<xlyl ,zl>, • • • ,Z,>> else?
reverse : x ==> if x=<> then <> else if x=<x 1 ,x 2 , • ,xn> and 15n
then <xn,x,-, "-"-xI> else?
rotl : x => if x=<> then <> else if x= then else if x=<x 1 ,x 2 , ,Xn> and l<n
then <x 2 ,x 3 , * * ,x*,xI> else?
rotr : x =•> if x=<> then <> else if xf then else if x=<xl ,x 2 , "" ,x,> and 15n
then <x,,x 1 ,x 2 , • • ,Xn_2,Xn.l> else?
concat : x => if X=<<X 1 ,X 2 , *** *** ,Xn>,<y 1 ,Y 2 , • ,ym>, Y' • ,<zz 2 , ,z 1 > > and 1n,m, •
then <XIX 2 , '"* ,xnYl,Y 2 , * ,Ym",z*,z 2 , ",Zl> else?
pair : x ==> if x=<XlX 2 , •,Xn> and 1!5n is even then < ,X2>9 ,<X,- ,xn>>
else if x=<<xl,x 2 ,. ,xn> and n is odd then <<Xlx 2 >, -,<x> else?
split: x ==> if x= then <,<>> else if x=<xl ,x 2 , ' " ,xn> and 1•n
then <<x 1 ,x 2 , * * ,XfIoorn 2 )>,<Xftooin/2>+l, *,Xn>> else?
distl (distr) pairs from the left (the right) the first (second) argument with each element of the
second (first) argument, apndl (apndr) appends the first (second) element to the end of the list
given by the second (first) argument, trans treats the argument lists as the rows of a matrix and
transposes this matrix, reverse reverses a list, rotl (rotr) does a left (right) circular shift on a list,
concat concatenates two lists, pair pairs up the elements in a list, and split divides a list into
nearly equal halves. Examples follow:
distl: <u <r m 2>> ==> < <u 2>>
distr : < doo> ==> < >
apndr : < ho> ==> apndl: <r <2 d 2>> ==> <r 2 d 2> trans: <<1 2 3> <a b c>> ==> <<I a> <2 b> <3 c>>
reverse : ==>
rotl: < 10 20 30 40> ==> <20 30 40 10>
rotr : «1 2> <3 4> > ==> < <1 2> <3 4>>
concat : < > ==>
pair: ==> <«romeo juliet> >
split: hairl hair2 h3 h5> ==> hairil hair2 h3> >
Predicates:
atom : x ==> if x is an atom then T else if x*? then F else?
null: x ==> if x =<> then T else if x*? then F else?
length : x ==> if x=<> then 0 else if x = < x, X 2 , - ,xn > then n else?
atom tests whether the argument is an atom, null tests whether it is the empty list, and length
returns the length of a list. Examples follow:
atom : firefox ==> T null: ==> F
length : <tell how long > ==> 4
Arithmetic and logical operations:
+ : x ==> if x= and a and b are numbers then a+b else? (and similarly for -,, /,<,,* ,=,•) xor : x ==> if xf and a and b are boolean then xor(a,b)else? (and similarly for and, or, not, nand, and nor)
These operators compute the standard arithmetic and logical functions. Examples follow:
+: <2 3> ==> 5 -: <4 13> ==> -
<: <100 999> f=> T
=: ==> T
Functional forms, combinators, and conditional:
fl @ f2 : x ==> fl : (f2 : x)
[exprl,expr 2 , " ,exprn] : x ==> <exprj:x,expr 2 :x, • • ,exprn:x>
& expr : x ==> if x = <> then <> else if x=<xI ,x 2 , '.. ,xn>andl_<n
then <expr : xj, expr : x 2 , ",expr : x, > else?
expr: x ==> if x = then else if x=<xl A2 • ,x, > and 2<n
then expr: <xl, !expr:<x2, • • • ,x,> >else? \expr: x => if x= then else if x=<xl ,x 2 , ,x > and 2<n then expr : < expr: <xl , ' • ,XI >,xn > else? I expr: x =-> if x = then else if x=<x 1 ,x 2 , ,x > and 2<n
then expr : < I expr : <xl , • • ,Xfloorn/2) >, I expr : < Xflo,2) > > else?
(C -> A ; B) : x =-> if C : x - <> then A: x else if C : x = <> then B : x else?
@ denotes functional composition and is by far the most common FP operator. Square brackets ([
and ]) denote list construction. The functor & maps the given function onto each element (^) of the argument list. !, , and I invoke several kinds of parallel tree evaluation, while the construct (C -> A;B) is the FP conditional operator: if C evaluates to true, A is evaluated and returned, otherwise B is evaluated and returned. Examples follow:
*@tlr:<3 2 1> ==> 6
[+@[2,2],1,2] : <b 1> ==> <2 b 1> & atom: <yes <>> ==>
Iid: <1 2 3 4 5 6> ==> <<1 2> 3> <<4 5> 6>>
(atom -> %1 ; %-1): oxygen ==> 1