Você está na página 1de 47

Automatic

Transformation

of Series Expressions

into Loops
RICHARD C, WATERS MIT Artificial Intelligence Laboratory

The that

benefits are

of programming easier
this reasons:

in

a functional of functions and


is

style than
used less

are well equivalent

known.

In

particular, expressed
as of it could expression of

algorithms of data as loops.


be, than for with and at

are expressed

as compositions to
kind (1)

operating
anywhere familiar poor

on sequences/vectors/streams algorithms
as often kind this this for near with support

elements
~Tnfortunately, least loops, (3) when In troducing any three (2)

understand
of most expression

modify
not are provide efficient second

programmers languages it 1s seldom the

most support

programming is provided,

kmd

expression;

programming a data type

language. called

and

third

problems

can

be

largely

solved

by

in-

series, extension)

a comprehensive that specifies they supporting which

set of procedures converts expressions of high in Pascal. Techniques] Language E.1 most series

operating series

on series, expressions at all times.

and into

a preprocessor efficient grammers rfemonst Categories tional) Lzsp; D.3.3 loops. stay rates

(or compiler within Lisp that

automatically are gllaranteed

A set of restrictions the limits macro package

can be optimized efficiency

If pro-

imposed,

A Common

series has been supported

in use for some time.

A prototype

series can be straightforwardly Descriptors D. 3.2 Languages]:

and

Subject

D. 1.1 [Programming Languages]: Language

Applicative Classificat stTuctur-es;


D

(Func34

Programming; [Programming

[Programming

ionsPascal,

Constructs-control nptr,mr,zatzo~

gramming

Languages]:

Processorspreprocessors,
Automatic

[Data

Structures]

[Pro.

lzsts; I 2.2 [Artificial General Additional Terms: Key

Intelligence]: Languages Phrases:

Programmingprogram

tran.sfownatton

Algorithms, Words and

Sequences,

Vectors,

Streams,

Series

1. SEQUENCE EXPRESSIONS
The mathematical

(or some initial

term sequence refers to a mapping from the non-negative integers subset of them) to values. Whether called sequences [5, 37], vec-

tors [24, 32, 33, 37], lists [37], streams [6, 20, 26, 31], sets [36], generators [19, 49], or flows [35], data structures providing complete (or partial) support for mathematical sequences are ubiquitous in programming. is as mutable aggregate storoperations for accessing and The most common use for sequence data structures age. Almost every programming language provides altering the elements of at least one such structure.
Authors 02139. Permission to copy without fee all or part commercial of this material is granted provided that the copies are not address: MIT Artlficial Intelligence Laboratory,

545 Technology

Square,

Cambridge

MA

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 repubhsh, requires a fee and/or specific permission. C 1991 ACM 0164-0925/91/0100-0052 $01.50
made or distributed for direct advantage,
ACM Transactions on Programming Languages and Systems, Vol. 13, No. 1, January 1991, Pages 52-98

the ACM

Automatic Transformation Sequences have another ported use that

of Series Expressions into Loops just as important

53

is potentially

and yet is sup-

by only a few languages:

Most algorithms

that can be expressed as loops can

also be expressed as functional expressions manipulating sequences. For example, consider the problem of computing the sum of the squares of the odd numbers in a file Data. program.
type

This

can be done using


Integer = file of

a loop
Integer;

as shown in the following

Pascal

[25]

FileOf

function FileSumLoop (var Data: FileOf Integer) var Sum: Integer; begin Reset (Data) ; sum := o; while not Eof (Data) do begin if Odd(Data-) then Sum := Sum+ Sqr~Data-); Get (Data) end; FileSumLoop := Sum end

: Integer;

Alternatively, computed using

the sum of the squares the sequence expression

of the odd shown

numbers This

in the file can be expression assumes

below.

that four subroutines have been previously defined: collect sum computes the sum of the elements of a sequence; MapFn computes a sequence from a sequence by applying the indicated function to each element a predicate;
FileOf (Sqr, Integer):

of the input; and ScanFile


Integer;

Choose If

selects the

elements of a sequence that the values in a file.


function begin FileSum end For those FileSum (var

satisfy
Data:

creates a sequence of

:= CollectSum(MapFn

ChooseIf(Odd,

ScanFile(Data))))

who the

are

not

accustomed FileSumLoop important in the loop then

to may

functional make

programming, preferable.

the

greater

fathe

miliarityof program that

program has two together

it appear First,

However, of computation Second, the pattern

FileSum

advantages. in FileSumLoop into

thepatterns apart.

are mixed

are pulled accumulating they

each of

of these initializing is distilled

subcomputations a variable into the action at ions. makes CollectSum.

isdistilled to o and

a subroutine.

For example, a result

repetitively apart,

by addition in isolaof the in isolasubations memory, many in the writfew


1991.

Because tion. The tion. into The subcomput This

subcomputations of the expression This FileSum makes makes easier FileSum oft he subcomput

are pulled as a whole more ations shorter reliability Fi.leSum

can be understood of the actions than File can be altered of the the Since the reusability correct they

is the composition self-evidently that The distillation ways.

SumLoop.

separation subroutines

also means and

to modify.

subcomputations of the subcomput from

enhances

computations. are being there times, it

It also enhances reused of error. to work are two instead

y in two

explicitly is practical is robust.

of regenerated since to that hard ensure

by the programmer each subroutine that the algorithm

is less chance

In addition, very

can be reused used from very

subroutine ing programs

Unfortunately, like

there

problems most

inhibit

most

programmers provide
13, No,

Fi leSum.
ACM TransactIons

First,

programming
Languages and

languages
Systems, Vol.

on Programming

1, ,January

54

Richard C. Waters procedures on their that operate onsequences as aggregates, rather than merely

predefined operating

individual

elements.

Second, even in languages

such as APL and

Common Lisp, where a wide range of sequence operations are available, sequence expressions are typically so inefficient (2 to 10 times slower than equivalent loops), that programmers are forced to use loops whenever efficiency matters. The primary source of inefficiency when evaluating sequence expressions is the physical creation of intermediate sequence structures. This requires a significant amount of space overhead (for storing elements) and time elements and paging). The key to solving the efficiency tion that it is often possible the crest ion of intermediate straightforward to transform A transformational overhead (for accessing problem is the realizainto a form where For example, it is

to transform sequence expressions sequence structures is eliminated. the expression to the efficient evaluation

in FlleSum into the loop in FileSumLoop. of sequence expressions APL the

approach

has been used in a number of contexts. For example, it is used by optimizing compilers [12, 22], Wadlers Listless Transformer [39, 40] which can improving

efficiency of programs written in a Lisp-like language, and Bellegardes transformation system [7, 8] which can improve the efficiency of programs written in the functional shown that
data base

programming
queries. it is not

language

FP [5]. In addition, approach

Goldberg

and Paige [18] have


the efficiency of

the transformational
possible There from

can be used to improve


transform ways and to every deal

Unfortunately, sion First, into

to completely are the two basic

sequence this

expresproblem. what can of

an efficient the

loop. issue

with

one can hide and

programmer programmer

simply the

transform defining

be transformed. transformed individual The vantage wit bout difficult they remain quite issue The tions tion identify every restrict Second, tions. Sections prehensive language. expression have completely quite simple into

Second,

one can

develop the

a set of restrictions about

what

can be

communicate expressions. which can about to This if any in

with

transformability

sequence hidden that for

approach, programmers to think programmers

is followed benefit from in

by

all

the

systems efficiency when they

above,

has

the

ad-

increased any efficiency a given because fails suffice to

in some However, want it to,

situations makes because will typically addition, the be it

having

efficiency think for about sure

sit uat ion.

no way

of knowing

whether

sequence

expression In

transformed. inefficient changes

is significant, part one. of them often

sequence to change really

expressions

be transformed. a favor

an algorithm

an untransformable to hide

a transformable y from

As a result,

it is not

of t ransformabilit most that important can a class system ions, the serve

programmers. of the the loops. research reported here is a set of restricto the 2, these always embodies other be used into type any called
1991.

contribution as a basis into for

communicative As discussed expressions in two explicit than sequence ways. expressions

approach in Section that First, implicitly can they

transformarestrictions While set of [.41].

of sequence The

expressions of optimizable

sequence are novel sequence used are not

be completely some

transformed.

rest rict ions optimizes

are explicit. of Wadler

that the

rest rict ions they how by

except than

in the work most can restrictions.

restrictions 36 and This show

in Section the support adding

2 are less strict

sets of restric. to add coma

In particular,

are less strict for

Wadlers approach

communicative a new

efficient is done

expressions data

programming

sequence
Vol.

series and

ACM TransactIons on Programmmg Languagesand

Systems,

13, No.

1, January

Automatic Transformation
preprocessor support key that the for restrictions restriction that series can transform the by

of Series Expressions into Loops


series restrictions the The rest of the expressions in two into ways. series loops. One

55
The of the so

optimizable optimizability selecting

utilizes cannot

is enforced

set of predefined

operations are explicitly with series warning

be violated. Non-optimizable If users complete fact

restrictions are flagged

checked messages sion

by the preprocessor. and left unoptimized. they

expressions take the time

to make that every

each series

expresis use

optimizable, This

can have

confidence that simple

expression that only

efficient. each

is facilitated can always the

by the

series

expressions

series

once

be optimized. data type support in Section can can with be used. and a broad for series suite with of associated Lisp. Lisp functions. This imexample [29, 46] series ex4

Section Currently, plement showing shows

3 presents the ation how most series

series

comprehensive expressions 5, along Readers familiar

is in Common an extended

[47, 48] is presented

4, along

A prototype into Pascal. Pascal to focus

implementation This example of how

that

series can

expressions

also

be added

implementation of Section

is presented pressions

in Section be used. the most

an extended

are encouraged language.

on whichever

or 5 discusses Section into loops.

6 presents It should computing

the algorithms be noted that

used to transform these algorithms

optimizable are not

series

expressions

capable

of optimizing that series of In

expressions series particular, computing Section comparison to expressing

series of series. when expressing involving they do not

However, looping nested have

it should algorithms loops

also be noted as series that related modify.

are not

necessary

expressions. as expressions

while series

algorithms of series,

can be expressed

to be expressed expressions with and

way. concepts. The

7 concludes includes loops

by comparing both other that in ways

series

implementations are easy

of sequences

and other

approaches

to understand

1.1 Getting
To fully able

Rid of Loops
the practical one must for loops. of series. essentially that case impact return The The of series to the program series expressions perspective File to the and in general of sequence and optimizexpressions based on the and is

appreciate

ones

in particular, variant into merely same form there a loop the

as a notational Pascal it is not the transformed

Sum is an example one in FileSumLoop. compute exactly the or notation the

implementation

expression

in it is optimizable

identical

As a result, same the result deof else, loops by them. the (At this many is is

FileSumLoop the any two

FileSum

using tailed

abstract without

algorithm; the paying series

programs in FileSum, in terms than

denote

same

computation.

Using

expression price

one gains of efficiency form.

advantages anything for

functional because The directly Any loop value

is no change of optimizable to the be expressed

in anything

other

the

expressions of loops that

as an alternate can be profitably series expression

related can

percentage

replaced by them value into

as an optimizable series operations a single the to familiar is at least the familiar that series can loop

converting together. of doing and the how change

subcomput worst, depends of these there An the

at ions used in it into entire loop many becomes fragments as there on how fragments

and composing operation. ) The be decomposed fragment, unfamiliar. 80%


and Systems,

correspond

computations. one familiar from the approximately


Languages

In general, because of the


Vol

advantageous is value informal

as long study
ACM

at the least, program1991

in separating

[42] revealed
Transactions

loops

on Programmmg

13, No

1, January

56
mers looping with some

Richard C. Waters
write are constructed (A somewhat solely similar by combining study just a few in dozen familiar

typically

fragments. the Lisp

is reported that at least benefit

[16].)

Experience contain series

implementation comput at ion.

of series Given this,

indicates the

95% of loops
of opt imizable

familiar

practical

expressions

can

be summarized

as follows:

Optimizable as structured
Structured capable there control constructs anything a few every They

series expressions control constructs


(If. that then. cannot for which

are to loops are to gotos.


while using do, etc. ) are got os. not

else , case, be expressed the

of expressing are probably in almost gotos. but

In addition, Nevbetter to be in a way do not they where one can can be of the is quite things step

algorithms situation, are better, they allow

use of gotos constructs allow to

is preferable. are much algorithms

ertheless, to use than expressed, that allow allow the argue At

structured not the and have that in because same modify. exactly cannot a much constructs there

control they

more

because easier series

algorithms the same

be expressed They

is much algorithms

to understand expressions

Optimizable

advantage. as loops. The only

to be expressed to be expressed

be expressed better breaks series way.

However, place while that most This hardest be a major

algorithms analogy that the with gotos better current

structured are never as loops time, than

control needed,

down some

is that algorithms loops loops.

are definitely contain

expressed

as opt imizable programs

expressions. and

most in

one or more occurs in these

interesting unfortunate, to understand possible, forward

computation since most loops

these

programs

are generally would readability not

acknowledged series any and cent ain

to be one of the expressions This maintainability. were would

in any program. programs

If opt imizable y, verifiability,

used whenever

loops.

in conciseness,

2. OPTIMIZABLE
As noted above, is the First, used

SEQUENCE
the creation primary

EXPRESSIONS
source may of inefficiency intermediate waste time Second, of time when sequences. computing even and a physical elements elements associated does not when data space. can are not with save be overcome computed sequences. constructing any when space. sequence by using until they evaluating There sequence structure sequence are two elements exthat of an

pressions to this. are not the lazy lazy are

of physical of the

aspects

a sub expression by the rest sequence wastes are used, a significant

expression. constructing amount unused that

all the elements

intermediate elements The problem evaluation evaluation all used, The (Each reuse of the much

containing

of computing [17] to ensure

sequence

sequence the waste time

are needed. intermediate wasted, elements. crest ed. a later All and

(This

also

makes

it easy

to support

unbounded

) However, physical Time is the is still of a sequence

does little sequences. lazy

to reduce In particular, wastes

in sit uat ions where and is required a physical recomputation. physical computing somewhere

the elements

evaluation coordination space element associated waste

because

overhead is used, would with the

to decide after

to compute

same of the waste

because require with

intermediate )

element

has to be stored

it is computed; intermediate sequence expression.


1991

otherwise, sequences elements can

constructing

of the

associated

unused

be eliminated
ACM TransactIons

by pipelining
on Programming

evaluation
and Systems,

of a sequence
Vol. 13, No

Languages

1, January

Automatic Transformation Definition


and only sub expression when

of Series Expressions into Loops


of a sequence hold for every expression sequence

57
if

1 (pipelined)
of E. any First,

The two

evaluation

E is pipelined
once. Second, and then

if the following

conditions each element it

S computed

by any

of S is computed wherever it needs

at most to

an element before primary by E may transferring the

is computed, other

is used

be used

discarded The called when it and

element

of S is computed. 1 is that, within while some of the of E. procedures is needed each creates in this 5). by of

implication buffer

of Definition elements

sequence

themselves,

no buffering

sequence that

elements use it. CosMax

between at a time (which

the subexpressions between like the

Rather, that

sequence Consider paper, The in first a file

is transmitted procedures the statement Data. each The value program using

one element

procedure of the

below

most of series

examples of the

is written

the second

Pascal body

implementation computes third

discussed Vals of

in Section values

in the

of CosMax computes

a sequence a sequence computes

numbers

statement The

scaled the

dividing the scaled

by its

cosine.

statement

maximum

values.

function CosMax (var Data: FileOfReal) : Real; var Vals, ScaledVals: series of Real; begin Vals : = ScanFile(Data) ; ScaledVals := MapFn(/, Vals, MapFn(Cos, Vals)) CosMax : = CollectMax(ScaledVals) end The and body of CosMax can be evaluated of which in four data are structure. not. The The numbers The data cosine in many ways,

some elined

of which method

are pipelined of evaluation in a physical and stored in a by

some

standard in the

non-pip file

proceeds interrnediate second its

steps.

are read

and

stored

of each number The result data

is computed of dividing structure. operates is read

physical

intermediate in a t bird

structure.

each number The maximum

cosine

is stored of the third the at

physical pipelined

intermediate method Each and time the

element

intermediate standard

structure

is determined. of evaluation a number scaled value on the the neither they input file, it an the are time

In contrast, numbers accumulator number, discarded (as opposed is as efficient (at input cosine. place must compile number (series its one

a time divided

as follows. by its cosine of the scaled

from to Since

is immediately

is used

update

keeping cosine, the schedule to at run as the time

track nor next time) the

maximum valued is read.

value is needed when

encountered. for later

processing,

before

number then

If the basic

for what

to evaluate the

can be determined evaluation loop. possible.

at compile

pipelined equivalent always

of a sequence Unfortunately, For example, rather than

expression pipelining consider that by its the each own ) In it the


1991,

evaluation time)

of an is not This sum

or at run

program

NormalizedMax is divided creates

below. by the

program of the indefinitely way

is the same input numbers, repeating pipelined

as CosMax except the value

a sequence

of its argument. uses of Vals proceed. and must

In NormalizedMax, contradictory pipelining be used particular, numerator

pipelined constraints requires

evaluation on the that both the each

is impossible, time

because evaluation

the two

an element of computing cannot


and

of Vals the

is computed, sum until


13, No.

immediatelyi.e., However,

as part division

as the

of a division.
ACM

be performed
Systems, Vol.

after

Transactions

on Programming

Languages

1, January

58

Richard C. Waters

sum after save when

has been all the the

computed elements

and the of Vals in

computation been

of the

sum As

cannot a result,

be completed one either or recompute

until has to them

have an

computed. data

elements

of Vals

intermediate

structure,

it is time

to do the

divisions.

function NormalizedMax (var Data: FileOfReal) : Real; var Vals, ScaledVals: series of Real; begin Vals := ScanFile(Data); ScaledVals := MapFn(/, Vals, Series(CollectSum(Vals))); NormalizedMax := CollectMax(ScaledVals) end Given that the existence pipelining system, further of programs can only it invaluable given advantages like NormalizedMax, it is clear that arrysystem expressions. explicit. failto is given the meet Ifin the a clear

supports the

do so for a restricted forthese feedback restrictions about and which The

class of sequence to be made expressions programmer are not, and they so that sufficient However, is of greater algorithms the pipelining. can

Whatever addition, restrictions, picture has the It

the programmers two of which opportunity be nice why expressions

are obtained. expressions and

are efficient inefficient

which

programmer specifying are a number benefit. can Second, succeed check the in

to change to have

can be pipelined.

would

a set of necessary stricter

restrictions there pragmatic that

exactly First, whether restrictions fixing The that strictions

which the

sequence

expressions must hold

can be pipelined. set of restrictions with practical perform that

of reasons

a somewhat

restrictions the restrictions must

be associated and can

actually

be sufficiently that violate

straightforward them. of the work presented without sequence into loops,

programmers

expressions primary are practical define time

contribution and a class

here

is a set of four strict. can that

restrictions These be pipelined in Section 6. re-

straightforward of optimizable them

being using

excessively the

expressions

at compile 2.1 In

by transforming

algorithms

Straight-Line
the interest

Expressions
of simplicity, optimizable not ,sub ject control flow sequence to any flow could in expressions are required cent rol must to be

straight-line While hibited, would much it

comput is likely simple

at ions, that the

conditional sequence probably flow

or looping expressions be allowed. of ways. can be done

flow.

looping algorithms using instead.

be prothis

conditional can be done Choose If

control

However, Conveniently, using

complicate of what like

in Section conditional

6 in a number control

sequence

operations

Restrictiorl
line comput aspect sequence

1 (straight-line)
at ions. of the simplicity the In the of each of the other

Optimizable

sequence

expressions

must

be straight-

One allows example, program The right the

engendered data-flow figure, procedure boxes. the Data

by the graph procedure

straight-line simple calls are corresponding

restriction data-flow to by dots Cross the represented

is that graphs. body by

it For

expressions 1 depicts outputs arrows of

to be represented

using

Figure and

of the boxes. left and

Normal izedMax.

inputs edges ports. the

are represented flow the


Vol.

on the hatched of

(respectively) Simple flow

is represented only
13, No

by arrows

between arrows are

indicate values.
Languages

flow

of sequences.

indicate
ACM

Because
and Systems.

expressions
1, January 1991

int crest

TransactIons

on Programming

Automatic Transformation

of Series Expressions into Loops

59

Fig.

1.

The

sequence

expression

in Normal

izedMax.

straight-line

ones,

there

is no need

to consider

control

flow.

2,2 Static Analyzability


As with expression what most at other compile optimization time, is being processes, unless performed. Optimizable expression and sequence expressions if and must only be if it can it is not possible at to pipeline compile a sequence time exactly

be determined

computation

Restriction statically
each sequence

2 (static
value defined

analyzability) A sequence is computed sequence guarantees

analyzable.

is statically by explicit bodies always expression cannot

analyzable calls

consumed whose it will

in the

expression analyzable. how that each every inside in data

on previously Static sequence sequence the or same in any

procedures that used.

are statically be it clear also must exactly implies

analyzability is being computed sequence other is bound be allowed. in another

computed expression. kind of data to block For

and

Unfortunately, a sequence Arbitrary pipelining.

in an optimizable Further,

sequence

be used solely of sequences certain how

be stored However, determine

in a sequence limited

structure. compile-time

storage

structures cases could cent ained demonstrated The used. static

inst ante, is being


APL

one can sometimes computed. The

a sequence has been

sequence for

practicality

y of this

by compilers analyzability it requires this by any definition process

[12], Hibol
allows sequence to the the for

[35], and Model


sequence procedure compiler

[33].
procedures before its to be first This calls.

restriction that that each calls

user-defined

However, that

be defined at the point

use and is required

be available

of use.

inline

compilation

of procedure

2.3 The Preorder Restriction


Suppose call must and order pick good element G. that For a procedure the comput First, call at ion it must T uses a sequence two the calls elements that be the case that computed to the sequence must this by another two elements procedure conditions are created in the same

of these Second, way

be pipelinable,

be satisfied. consumed they some order

one at a time. A good and

be consumed will always unbounded with

are created. fixed order in that

to ensure every of the a desire

be the

case is to one a

require order. natural

that Given order

procedure to support elements

process starting

every

sequence sequences, the first.

at a time to pick

is the

Restriction
expression the order elements starting

must

Every procedure called by an optimizable sequence 3 (preorder) A procedure is preorder if and only if it processes be preorder. of its sequence the first element.
on Programming Languages and Systems, Vol. 13, No. 1. January 1991.

of each with

inputs

and

outputs

one at a time

in ascending

ACM Transactions

60
It than

Richard C. Waters
to note In referring rather this to than not consider that the the restriction word above applies is used while The term to procedures, to refer the of the rather

is important functions. that for

paper,

procedure
function, function.

to a particular word way

algorithm reserved procedures, tion output For mation some

implements

a mathematical a mathematical functions, because

function
applies

is
to

preorder

it is a property relationship

a computaand the

is performed, example,

of the

mathematical the

between elements

the

input

computing in many procedure and

sum

of the ways,

in a sequence. are preorder reading

Sumand the

can be implemented of which are not. The

different

some operates

of which

CollectSum adding

in preorder, a running sum.

sequence one could other Any the how

elements choose

one at a time to read way. function can does the

each one into order,

However, or in some

elements

in reverse

or two

at a time,

non-preorder sequence

be implemented not than more limit In any what addition, other further buffering

as a preorder functions most implement of input at the

procedure. but

Therefore, rather only implepreorder other-

preorder they can

rest riction

can be used, of the time, at ion. elements end

be implemented. requires issue

a preorder However, than would section.

ment at ion processing wise 2.4

is no less efficient sometimes This

be required.

is discussed

of this

The On-Line Cycle Restriction


looking at the last the and restriction following off-line) or writes initial optimizable property. An input or output and port of a procedure in lock element is written, element If the step with then of each sequence If a port is sequence expressions must satisfy, to consider

Before

it is useful

Definition on-line
all the on-line the on-line ports if and other input output

2 (on-line
only on-line is read, element

if it reads ports then of each the and all

a sequence

operates The the initial output second

of the on-line

procedure element input the

as follows: is read, rest then of the

of each

of each on-line

second

is written, are on-line, the ports

so on for the

elements.

of a procedure is not

on-line,

procedure

as a whole

is on-line.

or procedure Definition applies term of one some these inputs line of the There has only processing.

it is off-line. standard as well 2 applies operation Mapping and some the result first definition as whole of the procedures. rather term

2 extends

on-line
Like than the >

[1, 23] so that


definition over

it

to individual preorder, or more of which values,

of the

Definition consider sequences. are on-line way, writ reading ing the

to procedures, of mapping can element first

functions. the elements ways, 7 to the different applying so on the until

For example,

the

a procedure

be implemented are not. of each output choose The input

in many procedure sequence, and

of which

MapFn operates

in an on-line

as the

element, to implement inputs

are exhausted. way by accelerating is a close every port output.

However, the

one could

mapping

in an offwriting

reading between

of one or more on-line is preorder is trivially processing mathematical


Systems, Vol. 13, No.

or delaying and preorder when

relationship on-line input is involved, while

processing and every on-line. is more

processing. that than more

In particular, one sequence

procedure or output on-line every

preorder However,

procedure than

one sequence

constrained can
1991.

preorder

In particular,

function
1, January

be implemented

ACM Transactions on Programming Languages and

Automatic Transformation
as a preorder on-line For satisfy become remain input procedure, there are many

of Series Expressions into Loops


functions that cannot

61
as

be implemented

procedures. example, consider P. problem of the step with It the operation of selecting for some writing of the gets takes returns first out two this to the elements of a sequence as an input output soon that on-line do not cannot as an

a predicate The elements in lock element

is impossible is that since the reading output that and in the

be implemented elements elements of the of the of the inputas

procedure.

of the of the elements

output, the the

is skipped, a related

of phase. sequences, the elements It However, the in a sequence of the is also note two this second that impossible of boolean sequence for this the output be on-line. in order can to be

Consider values that function is the Definition capture the and

function sequence, values

some

other to true

correspond

sequence.

to be implemented only problem. 2 specifies fact that There what

as an on-line is no it means like

procedure. in one

difficulty for the

having ports proposed

inputs paragraph

individual

to be on-line

functions that

implemented Returning CosMax and concept

in ways

are partly

on-line. in general, of this and the example programs the in

to a discussion NorrnalizedMax processing while

of pipelinability used in the key

beginning

section why

in particular, the computation

of on-line

is the the of the because

to understanding

CosMax can be pipelined Recall the and put. way that the the output requires

computation sequence pipelining

in NormalizedMax expression imposes The before of its implies

cannot. (see Figon re-

evaluation of ScanFile that

in NormalizedMax contradictory procedure the sum two

ure 2) cannot

be pipelined,

constraints CollectSum

must

be computed. be produced before element this first

quires

that Series

all the elements

of its input its input that the

can be returned its outinputs be

be available

it can start

producing sequence the must first

However,

MapFn requires available. of Series element For (and

simultaneously of the before the output the

pipelining therefore output more above

to work, the output

that

element

of CollectSum) is computed. this into

be available if

second

of the

of ScanFile than

Unfortunately,

output The

of ScanFile

contains

one element, down

is impossible. two parts. First, there with

essence

of the problem

can be broken Second, these

is a cycle each The because connecting other.

of interacting

constraints.

constraints

are inconsistent

expression it contains ScanFile,

in Figure

2 is associated data-flow Series,

with cycle. and

a cycle

of interacting the cycles, a non-directed flow that

constraints, data-flow cycle. then all of the are not example, though data it flow arcs

a non-directed CollectSum, does not contain are

In particular, data

MapFn form

If an expression constraints However, independent, the sequence will not while

any non-directed independent flow imply cycle that

on pipelining it does not

completely of a data

and

cannot

be inconsistent. For even that

the presence

implies they

the constraints

necessarily

are inconsistent. 3) is pipelinable to guarantee they flow that data

expression lead

in CosMax (shown data flow cycle.

in Figure It

contains cycles

a non-directed

is possible

to inconsistencies cycle) it passes on-line.


Transactions

by requiring non-directed A non-directed two ports

be on-line. cycle in an optimizable cycle procedure is on-line call, if the

Restriction
sequence and two only ports

4 (on-line
must

Every through

expression if, whenever are both

be on-line.

data-flow of the same

ACM

on Programmmg

Languages

and

Systems,

Vol.

13. No.

1, January

1991.

62

Richard C Waters

Fig.

2.

Theseqllence

expression

in

NormalizedMax

Fig.

3.

The

sequence

expression

in CosMax.

The output matter through in Figure the

non-directed that any the other

data-flow with output port of

cycle both ScanFile

in

Figure inputs is on-line,

3 is on-line, of MapFn because the /

because are the on-line. cycle

the It does

input does not

and not pass cycle

of MapFn Cos along

of ScanFile. because

In contrast, the output

non-directed

data-flow and

2 is off-line, of Series

both

of CollectSum

is off-line

because

input To

is off-line. why on-line 2 and If cycles guarantee pipelinability, of two the the branches processing cycle that port will while and whether processing relevant to the is each the the at it is useful (an upper to think branch along

understand cycles in

of the and the

Figures

3 as consisting is to work, If two other not

a lower two

branch). must each

pipelining be

of elements the will In does

branches synchronized will

synchronized. of the way the the does

on-line, branch other output this

lock-step be comif one not the not, will and

synchronization pletely branch (as in Figure be balanced. is on-line, processing cycle. ) The gram This with viding output PositiveMax program ChooseIf the ith

step and

guarantees an off-line matter the is not

therefore contains while case, it

branches is not,

balance. two

cent rast,

2) one branch be synchronized (In because at the either the other

branches port branch port.

of ScanFile

relationship ports

between

of ScanFile is off-line that

either

of the The pro-

of CollectSum below positive. element

because off-line

it is a non-sequence sequence 3) except in Posit element ports that iveMax

illustrates The of Vals

also block MapFn


CO.

pipelining.
is

is identical

to CosMax (see Figure scaled by the numbers ith positive

replaced by di-

are computed

of Vals.

function Posit iveMax (var Data: FileOfReal) : Real; var Vals ,ScaledVals : series of Real; function Positive (X: Real) : Boolean; begin Positive := X>O. O end; begin Vals := ScanFlle(Data); ScaleclVals := IlapFn(/, Vals, ChooseIf(Positive, Vals)); Posit iveMax := CollectMax (ScaledVals) end
ACM Transactions on Programming Lan~ages and Systems, Vol. 13, No. l,,January 1991.

Automatic Transformation

of Series Expressions into Loops

63

Fig. 4.

The qualitative

effect of the on-line

cycle restriction.

As phase

in the with that In this after

program synchronized, Vals the

NormalizedMax, because and the element in third second the

the

two

branches

of the

computation will In the not get

cannot out of is particular, second input,

be reliably suppose not. until the any

output

of Choose If element are

Positive while will

as soon first

as a non-positive elements output

is encountered. positive of Choose If

of Vals element

situation, the t bird delay pipelining delay. cycle branches and only

be available on the

of Vals iveMax

has been may

computed. small

Depending or that to be, and even

unbalanced unbalanced on-line both

Posit is only

be very

zero. will it

However, never be

compile-time The ble for

possible is stronger to

when

it is known it has

there because

restriction

than

is possiboth be

of a cycle yet still

contain because where restrictions

off-line they balanced above.

ports

therefore

desynchronized However, practice the

balance, situation by the

are identically

desynchronized. occurs output in

common handled of the

desynchronization If the non-sequence

is indirectly branch upper

in the lower output a whole could The the

cycle

in Figure then it (one

2 were

balanced left and

by a second to view one the on the

non-sequence expression right) as which on of

in the as two on-line

branch, optimized. restriction illustrated calls. all the

would

be possible on the

separate cycle form

expressions forces in

be individually

optimizable 4. the In At

sequence particular, level, in this of the the

expressions the there complex data on-line are flow

to take a number must

qualitative

Figure

restriction

forces

a two-level clusters can line. off-line; In be used;

structure however,

of connectivity. Within of the ports

bottom arbitrarily

of procedure contrast, however,

each cluster, and flow

interconnection be oncan be clusters

participating outputs

overall

inputs data

the top-level cycles. perspective, that

connecting

clusters

cannot

cent ain any

non-directed From compute on-line sequence are softened cycle

data-flow by the fact from restriction

a pragmatic sequences expressions.


ACM

the

limits

imposed

by the on-line on-line way.

cycle

restriction procedures the

several

of the most

commonly

used sequence

sequences

in a completely stringent

Nevertheless, on optimizable

is by far the most

of the restrictions

Transactions

on Programming

Languages

and

Systems,

Vol.

13, No.

1, <January

1991.

64 25
The

Richard C. Waters of Restrictions


discussed if and above only if and can be summarized as follows. A sequence

Summary
four

restrictions

expression (1) (2) (3) (4)

is optimizable

It is a straight-line It is statically procedure non-directed restriction than analogous at compile pairs that 4. every

computation; and

analyzable; called data-flow greatly necessary. time

Every Every first

by it is preorder; cycle

and

in it is on-line. the algorithms that time. can it will The in Section be weakened third will restriction The have fourth the form 6, but is undoubtfuture. A that in that is to be

The edly

simplifies It is hoped

stronger

in the

restriction applied isolated ensures Figure approach ing can

to the second as opposed procedure restrictions

one is required to run calls ensure

for any optimization

ensures shown

of sequence the

be pipelined. the following

restriction

optimizable

sequence

expression that

Together, can be used

divide-and-conquer cluster, in the lock top-level into The two pipelinstep, one data non-

to pipeline by simply

an optimizable consists every evaluating than

sequence of a single procedure then

expression. on-line call (since data of the

If an opt imizable be achieved at a time. element flow overlapping as a whole separately tion of the the The because simple. single

sequence If there the

expression is more that

one cluster, can

is cycle-free) can and

expression

as a whole are connected the of lazy

always

be divided flow. two

sub expressions be pipelined using

by a single evaluation evaluation The

expression

by pipelining form in a pipelined

sub expressions the when only evaluais very the is simplified

a simplified for determining

to interleave lazy evaluation to evaluate when and

subexpressions method source

fashion.

which needs needs

sub expression to be evaluated a new value

subexpression subexpression

when

destination

to read

computed

by it.

2.6 Obeying the Restrictions


In the ensuring composition The isfies In the the Lisp left It t ions. without which the only cycle
ACM

implement that other these into current

at ions there

discussed

in the that

following every

sections,

the

preorder (Note

restriction and the satexin that

is implicitly

enforced of two three

by ensuring is no way preorder restrictions the loop.

predefine )

procedure procedure. Whenever used satisfied, are fatal the static to take

is preorder

to define procedures

a non-preorder is preorder. checked. are do not not in Section they these

are explicitly algorithms When

an expression to transform a warning errors. the

rest rictions, an efficient Pascal

6 are

pression

is issued. However, are simply restricExperifor by has expressions

implementation, expressions to on-line follow cycle the

warnings satisfy and

implementation,

that

restrictions analyzability more complex. is to write

as is and However,

evaluatedicompiled the that

without restriction cycle

optimization. is significantly restriction virtues unlikely sequence stored and then of this to violate procedure

is straightforward

straight-line for programmers The

ence suggests warning that

the best about

approach

worrying

the on-line are reported. that

fix any expressions are enhanced of the then restrictions. the any

messages simple and

approach

fact

expressions sequence

are very if every outputs

In particular, one output

it can be shown cannot

in an expression

are not

in variables,

on-line

rest rict ion


Transactions

be violated.
Languages and Systems, Vol. 13, No. 1, January 1991

on Programmmg

Automabc Transformation

of Series ExpressIons into Loops

65

{)ScanFilet
CollectMax

MapFn B thScanFileo , CollectSum ) Series 4

/ ~I

Fig.

5.

Thesequence

expression

in NormalizedMaxA.

Violations ing to break into brought ScanFile into

of the on-line the offending compliance

cycle cycle. with

restriction For

can always the cycle restriction cycle

be fixed

by using

code the

copycan call be on

inst ante,

program

Normal izedMax

the on-line This

by duplicating and converts the

as in NormalizedMaxA. is opt imizable

breaks 5).

the

expression

one that

(see Figure

function NormalizedMaxA (var Data: FileOfReal) : Real; var ScaledVals: series of Real; Sum: Real; begin (Data)); sum := CollectSum(ScanFile ScaledVals := MapFn(/, ScanFile(Data), Series(Sum)); NormalizedMaxA := CollectMax(ScaledVals) end It would with It For be possible cycle leave the to automatically restriction. it up to the However, introduce this code copying to resolve conflicts

the on-line to

can leadto to figure

significant out how into to

inefficiencies. fix conflicts. more

is better example,

programmer can

procedure that

NormalizedMax the operations

be brought

compliance

efficiently, by the sum

by realizing commute

ofcomputing

the maximum

and dividing

as shown

in Normal izedMaxB. Real;

function NormalizedMaxB (var Data: FileOfReal): series of Real; var Vals: begin Vals := ScanFile(Data); NormalizedMaxB := CollectMax(Vals)/CollectSum(Vals) end This presented expressions expression accurate example here. are brings This up unimportant goal isto and make which given secondary it easy are that not. this what It goal would

underlying be better

the to

restrictions tell make which every need

forprogrammers

toreliably

efficient

efficient. information

However, in order

is not to do.

possible,

programmers

to decide

2,7 Other Approaches


The years. erations Apprentice in [43, 44]. four The optimizability basic

to Restrictions
restrictions was present The first are the in some result of research looping formal is presented
Languages and

going on the

back

thirteen as opappears

concept

of representing attempt

common tostatea

subcomputations work set of restrictions in [45].


Vol. 13, No.

on sequences [42, 34]. An

of the earliest

Programmers

intermediate
ACM Transactions

set of restrictions
on Programming

Systems.

1, January

1991.

66

Richard C. Waters

Optimizability optimizers. mizers explicit Wadlers iting. next In work,

restrictions However, rather these than is that differs imply

are

implicit

in

all

the

work implicit only

on

sequence in the way

expression the opti-

restrictions explicitly

are typically stated. [41]. presented in a different his restrictions situation where sequence one here here, The

being from

other

research ways.

featuring while limin the with can is

restrictions work restrictions Second, subsection, [41] Wadler output the

of Wadler the the work ones

Wadlers

in three Third, is different.

First, more

presented

they way.

are needlessly

he uses his restrictions motivation addresses with procedure they here. and effect behind the only

as discussed procedure His results

a sequence procedure. and

a single

is composed generalized each

a second to the has

be straightforwardly expressions, used The from more the in which than once. to

case of statically-analyzable, only output

straight-line no output complex value situations. different that each

However, presented one It output has the

are not

applicable straight-line the

to more implicit

limitation limitations have

statically-analyzable,

expressions

is no

However, no output

requirement more than once

procedure cantly cycles, cles. more Since

only

be used

is signifidata-flow cyeven

limiting. it is often

of outlawing by outlawing a sequence cycles, of view is used states this

off-line expression

non-directed very

however,

it goes way possible

beyond

this

all non-directed

data-flow efficiently

to evaluate data-flow the point that

t bough It

it cent ains non-directed (both that from Wadler

is an overly

severe and

restriction. to

is unreasonable that The rest rict ion

of readability twice that

efficiency) twice. must here,

require

ever y intermediate

value explicitly

be computed procedures stated each (Wadlers here.) situations

(i.e.,

be preexcept proceof com-

order listless)
that, dure the putat the t ure First, limits have ate they oping is more being These operate term Wadler ions

is basically in the using

equivalent next amount in the ing

to the

preorder

restriction that storage.

as discussed

subsection,

it also requires of internal from type the

sequence

a bounded by itself

definition where

preorder involving used data lists kind

is different data

one used to identify can

uses his restrictions a pre-exist here type. places This

background (lists)

be optimized.

In contrast, feaunduly vectors)

approach of a new since the evolved for that a new

Restrictions more that active unbounded can a suite well it for

14 in the approach

foreground is followed focusing Second, operations use, but suite part

as a primary for lists that are not three on lists (and

reasons.

cannot

represent

sequences,

of procedures of use and work makes this is the

be expressed. of associated their to Third, that were intended from using the lists intended efficient create

a style use. be from data

are approprias useful as that

These the type for here

could

perspective

of writing possible messages if they did not from

sequence a new

expressions. of operations of the

Develapproach

appropriate advocated are important and that type, the

purpose. error

an important report unopt efficiency. constantly restrictions and

imizable However, being when

expressions. they By they would for adding choose ways. list a to

if programmers counterproductive programmers being inhibited

are to achieve

be irritating expressions new follow data them,

reported

to be optimizable. vectors

programmers

can benefit

without

in standard

2.8 Avoiding
Wadlers expressions work

Unnecessary Storage
and the ly. work presented the here basic
Systems,

share

the

goal

of evaluating two

sequence is

efficient

However,

framework
Vol.

of the

approaches

ACM Transactions on Programming Languages and

13, No 1, January

1991,

Automatic Transformahon
different, has been Consider ~ is applied .T and Y(g(x) object because achieved. again the basic situation computed as efficient inefficient, value of ~(x). presented (i.e., 7 they use different

of Series Expressions into Loops


to assess whether efficient

evaluation

67

methods

of an expression by another as possible. because they

F(~(z)),

where

a procedure further that

to a sequence

procedure Traditional create

G. Suppose methods an intermediate

Q are individually ) are nevertheless representing work the

of evaluating sequence

Wadlers for you eliminating the way ~, but

and the work the creation procedures individually ~ can operate

here

use the same sequences. them compute Second,

basic

two-step place results that

approach limits This as 7 does on and for not forces

of intermediate and time 7oG

First, the they

they same provide ~oG than

procedures which 7 maybe and

by requiring ~ that into (z)

to be preorder).

to use preorder

less efficient.

algorithms

combining cause is how The is that almost will ~. the

at compile

a joint

procedure more test used efficient

creation

of an intermediate

e sequence. is really

A key question

in both

approaches ~(~(x))?

is one to assess whether work presented (z) If f here

uses pipelining then the

as its resources as .T and

of efficiency. to transmit with guarantees the

The values 3 that mere

basic from F0~ fact as 7

idea the to (z) that and

if >oG zero.

is pipelined, with and

computation

associated

~ to the computation

associated ~, this

is reduced

~ are as efficient efficient not ~ must g. than

be significantly is pipelined since than 7 F In fact,

more does and and

.T(~ (z)). that that 7 they

Unfortunately, and ~ are sometimes

70~(x)

imply

are as efficient forced

be preorder,

to be much

less efficient From is that required sequence order. input

an efficiency it sometimes by that other

standpoint, requires methods the

the fundamental input elements to For

problem

with in

preorder a way

processing that is not a

be buffered example, input

of computation. same elements very This

consider sequence,

computing but that in reverse

cent ains can one

as an efficiently with the

Reversal elements

be implemented at a time the is not

by a procedure last, need while not writing use

reads the

the of

beginning

output process preorder before it

elements elements, both its

start ing however, inputs

with it

first.

procedure Since

any

buffering must any

preorder. in order

a preorder with input the first

procedure element, inside

and

outputs

starting all the

implementation can begin writing

of reverse the

has to store elements. requires

elements

itself

output processing calls and

Typically, tion being

preorder implemented reversal case.

the buffering

of inputs

whenever of an input functions

the funcsequence. where

for the reordering sorting are the only

of the elements common

Conveniently, this is the

sequence

In the expression tal ion 7 efficient is merely 7. While Wadler He over requires and it violates must than this the uses that the above moved 7(G

F(Q(z)), (z)). from The does on (z)

if ~ implements less efficient simultaneous not violate than

reversal 7 that storage

then 30~

the preorder (x) by cannot elements definition as his input test and that
tol

implemenbe any more by inside ~

be so much

of the computed of the (z)

computed

an intermediate of what is trying the storage operate needed unbounded

sequence the used

G to a buffer

situation spirit a limit 7oG

letter by ~0~ the this

of pipelining, of efficiency. storage Since output.

to be achieved. using bounded internal overall storagei.e.,

storage

to represent in length,

sequences

are potentially

guarantees
Systems,

an intermediate
13, No. 1, January 1991

ACM TransactIons on Programming Languages and

68

Richard C. Waters

sequence shows amount guaranteed An beyond ruling a buffer storage The somewhat procedure very if .F for both 3

is not if

being an

created

during restriction is added amount

the

computation. requiring that

The

central

theorem use then

in [41]

that

additional storage virtue the

procedures restriction, restriction

a bounded 30!7 it by (z) is

of internal important eliminating out the

to

the

preorder

to use a bounded of the

of internal internal

storage. storage is that sequences goes also in of intermediate

bounded

physical

representation of the

simultaneous a sequence has problem the tile

storage procedure. advantage the The

elements

of an intermediate although the it

sequence internal

inside

Unfortunately, of closing bounded real this internal

bounded has

restriction biggest

loophole, storage

a number

of

disadvantages. is that point. restriction a preorder 7. This is really sequence only that F in beside 7 question is whether counterpart uses bounded then use bounded if In this likely unbounded situation, not,

is as efficient with the

as its non-preorder of whether and 3 does both if they F

correlates It is true certain that

weakly

question storage However, less efficient.

storage. storage storage there

uses unbounded less efficient. to cases, be T Y, rather additional much could because than way problem to

it is virtually

is much

it is still

possible

Further,

is necessary would this

be just while with detect

as efficient. it is very the

be no need expense applied. there is unto be prothere is that is

to outlaw inherent An

expensive internal

to compute, transformations restriction To start

engendered

by the optimization bounded it whether is being

being with,

storage obeyed. sequence

is no practical no useful bounded out Iawed. cedures as their Given ability storage confined cient and users way

to determine As than a result, this,

whether many

user-defined of the most storage would the fact that

procedures would kinds

require have sequence

storage. Worse are capable arguments. the

user-defined unbounded limits and that

sequence common

procedures higher-order certain

of using Draconian

if given

of procedures pipelininternal However, 3) is as effireversal

have not

to be set here it has nothing to include sequence that the

as well. to do with bounded

problems

it causes

per se, it was decided


restriction to functions of overall

it was better

in the definition efficiency, that have

of an optimizable the set of predefine preorder

expression. are (nearly) ions

in the interest

procedures

(see Section

implementations In particular, off on the feel right

as the best sorting from

non-preorder This whatever

implementation. gets things functions they

t he funct foot, without

are omitted. defining

preventing

are useful.

3. SERIES EXPRESSIONS
Vectors, series lists, data type and other such data structures and in the differ range that for in how closely full they model the The for the efficiency. a pre-

mat hematical mathematical As illustrated added to any processor Informally in length. remainder

concept concept in the

of a sequence of a sequence, next two language. like are

of associated combine can

operations. support and high by 6. can

embodies

a set of design sections, the

decisions range support High

a wide

of operations, series is obtained in Section that they they can

be straightforwardly using

programming speaking, series series section

efficiency except way

or compiler Formally, of this

extension are (See

one presented by the

like

vectors

be unbounded on. The as operations mathematical

defined [9] for

be operated of these of the


1991,

presents

an illustrative an in depth

selection discussion

mat he mat ical

functions.

ACM TransactIon. on Pro~ramming Lan@ages and Systems, Vol. 13, No 1, January

Automatic Transformation
properties two ming sections of many present of these functions

of Series Expressions into Loops


applied these to finite functions sequences.) in specific

The

69
next

when

procedures

implementing

program-

languages. lowercase letters (r, x) denote letters a literal : R denotes (F, series the arbitrary 7) denote containing series values, functions the formed by uppercase letters

In the following, (R, The S) denote notation up to series, z.

and calligraphic notation of the r

and predicates. z, y, and the appending

(a, y, . . . . z) denotes The front can series, series kinds the Other

elements

so on element

r to the functions from values

series

Re.g.,
into compute Some three

6: (7,8)
series

Series series

be divided scanners series.

= (6,7, 8). categories: collectors


from non-series

compute and data

non-

values,

transstructhe

ducers compute
There ture series from series are two cent aining elements. the elements

from

of collectors.

collectors for

create

an aggregate table

elements collectors

of a series, create for

inst ante, value sum.

a hash

containing by some

a summary their

computed The Lisp

formula of can

of a series,

instance,

implementation shown functions below)

supports to define

18 collectors. the rest. is a higher-order first argument is a binary series final

However, functiona z is used function into the value

one of these function initial

(collection, that value takes

be used

Collection ments. second elements left identity ion. collect The

as arguThe to be a of

as the F,

of an accumulator. to combine the z is chosen as the

argument

which

is used

successive value

of an input The example

accumulator. of the accumulator sum = z collection Z+Y,

(Typically,

of .?_.) The

is returned

computes fi, F,

the ())

of a series.

collection(z, collection(.z,

r : R)

(fi(.z, =

r), 6

Y,

R)

collection(o, There are also two kinds data create

A xv.

(1, 2, 3)) scanners

of scanners.

Some

create

a series of the elements in a hash the table.

in an aggregate Other powers However, to define The element is used The should scanners of

structure, a series The (the

for instance, based

a series

of the elements for series instance, supports

on some function initial

formula, at ion of

successive be used the 7, first which

a number. one of these the rest. argument series

Lisp

implement

15 scanners. can

higher-order is an second element

scanning, value z,

see below) which

first

of scanning created. each output The

becomes function previous where

of the

argument after the

is a stepping first, from the

to compute argument The

element. the series so on,

third end.

is a predicate created the first The .F, F)

P, which the

is used elements 7. the

to determine z, -T(z), If no value powers f($(z)

series

contains value example =

), and

up to but

not

including in length.

satisfying computes

satisfies

P, the series 100.

is unbounded

of 2 less than if T(z)

scanning(z, scanning(l, Transducers is no one higher-order are

()
{
z : scanning Az..zs1OO) than wide collectors for classes (~(.z), = 3, P)

otherwise

Az.

z+z,

(1,2,4,8,
or scanners. the rest.

16,32,64)
In particular, four there key

more that

complex serves support

transducer transducers

as a basis

Nevertheless, transduction

of common

operations.
1991.

ACM TransactIons on Programming Languages and Systems, Vol 13, No 1, January

70

Richard C. Waters

Collecting rather of the than input.

is the same just The a final

as collection value. The computes .F, ()) = =

except length a series () .F(z, r)

that of the

it returns output

a series of partial is the same as the

results, length

example (z, .T,

of partial

sums.

collecting collecting(z,

r : R)

: coIlecting(.F(z, (1,2)3)) = (1,3,6)

r),

.F,

R)

collecting(O, By fart F the The over he most commonly

~xy.z+y, used series producing same elements . . .) = ()

function

is mapping, of the

which results.

maps Each

a funct element

ion of The

some

number of the the

of series, output

a series as the

output length

is computed

by applying is the

Y to the

corresponding length series. of the

elements shortest

of the input.

inputs.

example

adds

corresponding . . . . (),

in two

mapping(.T, mapping(.F, rl : RI,

. . . . rn : Rn)
.z+y, by

= .F(rl,
(1,2,3), testing as soon

. . . . rn) : mapping
(4, 5,6,7)) = with

(~,

RI,

. . . . Rn)

mapping(~zy Truncating carding all the cuts off a series

(5,7,9) a predicate the and dis-

each

element

remaining The

elements

as an element before the

satisfying first

predicate element.

is encountered.

example

truncates ()) = ()

a series

negative

truncating(~, truncating(~, truncating(~z Choosing picks out the selects the

r : R)

() { r : truncating
(0,3,2, 7, that (~,

if P(r)

R)
=

otherwise (0,3,2} The example

.x<O,

1, l)) satisfy

elements elements ()) r : R) z<O, series

of a series oft = he input. ()

a predicate.

negative

choosing(~, choosing(T, choosing(~z. Mingling icate. will element The from combines comparison two two

r : choosing(~, { choosing(~,
(0,3,2, one not 7, under ordered 1, l)) the to l?)

l?)

if T(r) otherwise

= control

(7,

1) predthe the

into are

of a comparison that The the example predicate,

is performed elements the series S, F) (), T) element into = = S

as indicated from S in the

ensure

combination shows

be stableif of two

by the result.

comparison

R precedes
sorted

result.

combination

a sorted

mingling((), mingling(R, mingling(r : R,

R r : mingling(R, {
s : mingling(r Axy. x<y) above, s : S, P) : R, S, P) = (1,2,3,4, some other if 1P(s, otherwise 5,7) transducers are r)

s : S, P)

=
(2,4,5),

mingling((l,3,7), In addition to the higher-order

transducers

ACM Trmsact,cns on Programming Lmgua~es and Systems, Vol. 13, NrI. 1, January 1991

Automatic Transformation
import ant as well. Catenating catenating catenating(r catenating((6, Spreading integers of S. copies copies In the is a quasi-inverse a series the ith output, appends (), : R, S)

of Series ExpressIons into Loops


two = series S S) 10) a series containing end to end.

71

S) = r : catenating(R,
(9, 10)) = (6,7,8,9, takes out ith

7, 8),

of choosing. S, and

Spreading creates

of non-negative the elements them with by r below

R and

of values elements

a series

of S are spread the and

by interspersing

of z. If the of z. Taken the

element with

of R is r, then the above choosing = = () ()

element of choosing,

of S is preceded the example

together

example

illustrates

relationship spreading((), spreading(R,

between S, z) (), z)

spreading.

spreading(r

: R,

s : S, z) = _
r copies

: s : spreading(R,

S, z)

spreading((3, Sectioning begins the n input input, extracts with element the creates the indexed out put

1),

(7,

l),

O) =

(0,0,0, of its

7,0, series

1) input. the length The not index The subseries including O.) Th~ series example

a subseries indexed

of the first the

elements and element runs

element

by m >0 or fails input

continues

up to but the

by n z O. (The If n is omitted cent inues out of the until middle (), m, m,

in a series-has out

is optional. a section

to be less than

of the

of elements.

of a series. n) = () () O, nl) ml, = (2,2,3) because it can produce (possibly nl)

sectioning sectioning(R, sectioning(r sectioning(r sectioning((l, The more function than one chunking output. of widt manner

O) =

: R, O, n) : R, m,

= r : sectioning(R,

n) = sectioning(l?,
2, 5) the of ones

1,2,2,3,3,4,4), from effect

is different It has the

above,

breaking chunks (For

a series are displaced example,

R into
if R =

overlapping) to the right,

chunks in the

h w >0.

Successive window.

of a moving

w = 2, and d == 1 then the chunks output


second series output where of the length input the first the output second cent ains

are 1 5, 5 3, and 3 7.)


contains element the of each w outputs. [1 + first chunk, The

d > 0 elements (1,5,3, 7), Chunking produces w


of each Thus, the chunk, ith the chunk (and the

element etc.

is composed therefore length the of the

it h elements of each series d, ())

of the is

number where

of chunks

output)

( IRI w)ldj,

\Rl denotes

R.
= () d, sectioning(R, dl))

chunking(l, chunking(l, chunking(w,

d, r : R) d, R)

= r : chunking(l, = S1,

. . . . SW where d, sectioning(R,
Languages and

Sk = chunking(l,
ACM TransactIons on Programming

kl,
Systems, Vol.

lRl+kw))
13, No. 1, January 1991.

72

Richard C. Waters

chunking(2, mapping(~xy. By itself, chunking with (z+y)/2, may other appear (1,5,

1, (1, 5,3,7)) 3), (5,3, 7))

= =

(1, 5,3), (3,4,5) however, the last part that )

(5,3,7)

somewhat For

unusual, inst ante,

it

is quite of the

useful example

in combination shows average. channel The

transducers. could differ to the can languages

how the results (Programming the outputs list can preceding that

of chunking of chunking of functions profitably

be used as the basis in the inputs mechanisms of mapping.

for computing could

a moving be used for the to

be viewed

as a recommendation in conjunction (e.g., none APL) with support contain. elements vectors

kind data

of functions type. The it does value In are not efficiently ruled out the elements as on-line (The only mingling, 4,

be supported 7, some support languages almost

a sequence most

As discussed others list not of functions contain

in Section (e.g., Pascal)

of these with, the series just functions as

functions;

of them. To start or altering or lists,

is also interesting functions This only for

for what arbitrary fact

it does not series unlike can (The

accessing the

of series intended addition,

elements. the list

reflects includes

that, that way.

to be used

as mutable as any list these that

data functions other

storage. be implemented only that in common

as preorder by this criteria oft heir procedures, functions cat enating,

procedures input. ) The

are ones like reversal

and sorting functions useful

that

rearrange

the order

of

also favors are require more and

can be implemented expressions. are choosing,

because in the list spreading,

opt imizable )

off-line

implementations

sectioning,

chunking.

A COMMON
Eeries can be

LISP IMPLEMENTATION
added of the into series essentially data any programming and defining language by adding an supcan both has structure 3. The a macro and a set of procedures of series Such expressions that package

implementation porting of these been the series things

functions easy

in Section to do using

optimization 6). package. is generally series a data the a second series the runs and

be supported in regular In

by a preprocessor are use for the A Lisp a number

(see Section of years

It is in the nature available are

of Lisp,

a macro

(see [47, 48]). lazily

Series.
using part data The

implementation, has 30] a procedural capable are computed accessed data part

unoptimized part so far. using of the time series The the scheme and

implemented The one by

closures. part records

series [19, the

part.

procedural one. The that

is a generator elements

of computing

elements accessing then

elements

of a series

generator

enumerates part it returns argument The puted access is used The it is not implement
ACM

the elements additional element what two-level another specifying

in the

uses the generator takes

procedural is called, a procedure

to compute

elements. in the series. to do when generation

Each

accessing generator out that and

accessing ensures twice, with same Lisp, basic and

of elements. elements user are not comcan imeach of a series the

preceding until all

needed, the

no element For data Seque ation No effort the

is computed familiar The

elements. of series language implement efficient. because

those [20].

Figure

6 illustrates

plementation in the closure very

structures. of series

implementation straightforward; a more

approach however, efficient where

is effective expended

has been of series


and

on producing is on the
1,

at ion,

focus

expressions
Vol 13, No.

sit uat ions

Transactmns

on Programmmg

Languages

Systems,

January 1991

Automatic Transformation of Series Expressions into Loops


(setq first5 (let ((x O)) (list #(lambda (i-f ; Implementation (at-end) (< x 5) (setqx of (1,2,3,4, 5).

73

(+ x 1))

(funcall

at-end)))))) of a series.

; Returns agenerator fortheelements (defun generator (s) (let ((g (car s))) #(lambda (at-end) (when (null (cdr s)) (setf (cdr s) (block nil (liet (funcall g #(lambda () (return (if (not (eq (cdr s) T)) (car (setqs (ccir s))) (funcall at-end))))) ofchoosing(P, (defun choose-if (p s) ; Implementation (let ((gen (generator s))) (list #(lambda (end-action) (1OOP (let ((x (funcall gen end-action))) (if (funcall p x) (return)))))))) (defun (let ofcollection(O, collect-sum (s) ; Implementation ((gen (generator s)) (sum o)) (loop (let ((x (funcall gen #(lambda () (return (setqsnm (+snm x)))))) (choose-if
6. Illustration of

T)))))))

S).

~z~.z+y,

S).

sum)))))

(collect-sum
Fig.

ttoddp
the Lisp

first5))
implementation

9
of unoptimized series.

they In

can be optimized, situations where

eliminating optimization or list than

the

physical

representation it is usually

of series better to

altogether. represent a

is impossible, as a series.

sequence The

as a vector

protocol

for obtaining

an accessing

generator

for the elements ofthe below. series This

of a series implementation.

and

thencetheelements Users interest must manipulate

themselvesisnot series using in general The series for

anexportedpart the and procedures static

is important

inthe

of optimizability

analyzability

in particular. 3 are all supported syntax

Series procedures.
by Lisp #z(z y procedures

functions Figure7. and

describedin In addition, printing literal

Section the #macro series.

as shownin

character

z) is provided

reading

(catenate #Z(l 2) (choose-if #ockip (subseries (mingle #Z(l 5 9) #Z(2 6 (multiple-value-bind (XS ys) (chunk (map-fn T #(lambda (x y) (/ (+ x The cating, ments effect The ning. tial The higher-order and and collection produce given and procedures are multiple situation. always illustrate implementing so that (Series values. However, more the

#Z(8 -7 -6 l))) = #Z(l 2 -7 8) #i) 2 4) * #Z(5 6) 2 1 #Z(l 5 3 7)) y) 2)) xs ys)) ~ #Z(3 4 5) scanning, they oftuples multiple than procedure stepping using can collecting, accept could series scan-fn, procedure numbers state values
and

1)

mapping, series get the is usually supports and two oftheir Each
13, No.

trunargusame more scanseries par-

extended

multiple

be usedto values which is used and aseries values

in any

using Lisp the

convenient

almost below

efficient

tuples.)

examples

In the second sums). stepping While

example, scanning

atwo-valued seriesof is in progress,

are returned

(an unbounded procedure


ACM

natural

twointernal

are maintained. ofthese


1991.

must

accept

as many

as it returns.
Systems, Vol.

Transactions

on Programming

Languages

1, January

74

Richard C. Waters

Series collection(z,

Function .F_, R) (collect-fn

Lisp

Implementation

type type
type T P F

2 f
type

Y ?)
Z

RI
F

R,, ) Rl R,n)

scanning(zi
collecting(z,

~i
7.

7)
R)

(scsr-fn
(collecting-fn (map-fn (until-lf (choose-If

mapping(>,
trlmrating(~,

RI.
l?)

., ,, R,n)

R1 R.) R)

R.)

Rl

choosing(~, mingling(R,
catenating(R,

R) S, P)
S)

(mingle
(catenate

R S P) R S) n) R S z) R m tcI d R) for series functions,

spreading(R, sectioning(l?,
chunking(u,

S, z) m,
d,

(spread (subseries (chunk 7. Lisp support

n) R)

Fig.

values

is treated (scar-fn > (scan-fn #Z((a list

as a separate # (lambda b c d) (C d))

state ()

variable. (a b c d)) #)cddr #nuE

1)

(values #(lambda #(lambda (setq

Integer Integer) () (values 1 1)) (1 i sum) (+ i 1)) (values 3610 1 ...) (+ sum i)))

-#Z(1234.

..)and#Z(l

Three introduced, procedure. to ensure determined that values is the second scan-fn. As

other

features

ofscau-fn the type of typing

areworthy (ortypes) information

of note.

First,

anew returned

first

argument stepping

is

which Given that the

specifies the lack number

of the values in Lisp, bythe value

bythe

this

argument

is necessary canbe

ofargurnents Second, This the predicate

returned the initial argument is not

stepping is replaced

procedure by

at compile the initial needed. as supplying

time. values.

a procedure initial it and as adrepeator string, creates the to be first as well Omitting The

returns are same

inconvenient that

insinuations is made true of any ing-fn, scanners aseries

where optional. value. and

multiple

Third,

a predicate

extensions a convenience

are applied to the These

to collect-fn, anumberof serleswhich the

collect specific creates

map-+n

user,

are providedin indefinitely vector, which containing of object

ditiontoscan-fn. ing a given value, which

include: which

scan

enumerates the integers list

elei~~ents and with

in a list, scan-pllst

scan-range a series corresponding scanned.

enumerates The the first

in a range, along

of the

indicators values.

in a property

a second the

series type

argument defaults

of scan specifies to list. . . .)

If omitted,

type

(series test) * #Z(test test test, (scan (a b c)) ~ #Z(a b c) (scau vector #(a b c)) + #Z(a b c) #\U #\Z) llTu~tl) > #Z(#\T (scan string (scan-range :from 1 :upto 3) ?tZ(l 2 3) (Scan-pllst (a 1 b 2)) #Z(ab) and ttZ(l similarly, combines adds up elements the the anumber elements elements of specific of a series of and a series, collect-first collectors into a list, which

2) inclucling: returns last collect the which which of of a series

are provided vector, returns which

or string, the

collect-sum element

collect-length

number

in a series,

ACM Transactions on Programming Lan~a~es and Systems. Vol. 13, No. l, January 1991,

Automatic Transformation
(or nil object if the series is empty). The first the

of Series Expressions into Loops


argument of collect to list. specifies the

type

75
of

to be produced.

If omitted,

type

defaults

(collect #Z(a b c)) * (collect simple-vector (collect >string #Z(#\T (collect-sum #Z(l 3 2)) (collect-length #Z(fee (collect-first #Z(fee (collect-first #Z()) * Finally, (based inserting (based respond choose-if) only a number

(a b c) #Z(a b c)) > #(a b c) #\u #\z) ) 5- Tuz ~ 6 fi fo fum)) + 4 fi fo fum)) * fee nil transducers takes the first in a series front and elements argument, non-null of the non-null are and of its and provided shifts including: it over the second elements of this last previous by coron

of additional which value which elements the

on collecting-fn) the to indicated on choose-if) non-null which

one element element, (also that based

at the selects of its positions

discarding

choose

argument in aseries. series. !If.ill

positions

returns choose

If given

one argument,

returns

the

elements

(previous #Z(fee fi (choose #Z(Tnil T) #Z(l (choose #Z(nll 3 4nll)) (positions (rnap-fn#oclcip

fo fum) !! r!) > #z(!! r, l,fee,, 2 3)) +- #Z(l 3) =%- #Z(3 4) #Z(l 2 3 5 6 8))) * #Z(O 2 3) In cognizance provides The maps that .F. #Z(4 the 16)) * tlZ(2 4) three #macro ofthe ubiquitous that syntax

,,fo,,)

Convenient
ping, the Lisp to express a procedure (#Msqrt The over the form

support
series

formapping.
implementation kindsof mapping.

nature make

ofmapit easy

mechanisms character

particular Finto #Z(4 mapping

#M~converts

a transducer 16)) = (map-fn

T t#sqrt specify

can be usedto without as let.

mapping a literal

of a complex lambda

computation It has

one or more same basic

series

having For

to write

expression.

syntax

example,

(mapping (expt is the same

((x (abs as

(scan (2 X) 3)) *

2 3)))) #Z(8 8 27)

(map-fn

T #(lambda (scan (2 iterate

(x)

(expt
*

(abs
#Z(8

X)

3))

-2

3)))

8 27)

The returned.

form

is the

same

as mapping

except

that

the

value

nil

is always

(iterate ((x (if (plusp To a first mapc ar. the body lazy than

(scan (2 x) (prinl

-2 3)))) x))) * iterate for side

nil

; afterprinting differ than

23. inthe for same its result. iterate series wayasmapc However, and and where due where are

approximation, like evaluated nature a question used, they The ina

and mapping effect the rather

In particular, is being just is not until

mapc, iterate

is intended

to be used

in situations

to the is more the not

evaluation

of series,

difference If mapping

between is used because

mapping

of efllciency.

in a situation elements

output computed Nested

no computation are used.

ifi performed,

loops.

equivalent procedure

of a nested that is mapped

loop over

is expressed a series.

by This

simply is typically

using done

series

expression

ACM Transactions

on Pmgrammmg

Languages and Systems, Vol. 13, No. 1, January 1991.

76

Richard C. Waters

(defun

bset->list

(collect

(choose

(bset universe) (#Mlogbltp (scan-range (scan universe))))

:from

O)

(series

bset))

(defun

list->bset (items universe) (collect-fn integer # (lambda () O) #~logior (mapping ((item (scan items))) (ash 1 (bIt-position iternunlverse))))) bit-position (item (nconc Fig. 8. universe) universe Converting (#Meq (list between (series item) item)))))) hsts and bit sets. (scan urnverse))))

(defun

(or

(collect-first
(1(length

(positions

using

mapping.

In the

example,

a list

ofsums

is computed

based

on a list

of lists

of

numbers.
2 3) (45 6) (78)))) (let ((data ((1 (collect (mappmg ((number-list (scan data))) (collect-sum (scan number-list)))))

+ by the and

(6

15 15) of collect-sum can must be used and

User-defined
mappmg new tion below, series

series procedures.
the standard Lisp However, defined

As shown forms the defun

definitions defmacro

to define when declarausing

procedures.

series No

macro

package This

be informed the when

a series defmacro.

procedure

is being

with

defun. special

is done

by using is required

optimizable-series-function.

declaration

(defun

collect-sum

(numbers) () O) #+ ) numbers))

(declare (collect-fn

(optimizable-series-function) number #(lambda

(clefmacro mapping (var-value-pair-list &body body) (let* ((pairs (scan var-value-pair-llst)) (arg-llst (collect (#Mcar pairs))) (value-llst (collect (#Mcadr pares)))) (map-fn T #(lambda ,arg-list ,@ body) ,@ value-llst)))

Example.
in a realistic procedures packed of sets. Sets where sented used to over each into

The

following

example context.

shows The

what example

it is liketo consists aslists that

use series of two parts:

expressions a pair of bits

programming that convert

between and

sets represented algorithm be represented to as bitwise

and sets representedas integer representation

an integer, a small 1 bit Lisp logand this

agraph can

uses the

universe integer

very

efficiently in the set.

as binary Here,

integers sets reprecanbe bit sets.

in the integers provides bit

represents

an element

as binary manipulate

are referred anumberof sets. In their

litsets.
operations loglor on integers the ,which computes union of two and bit bit

Common sets while To perform and the


ACM

particular, intersection. between

computes in Figure conversion, elements. If

Theprocedures potential ith element set

8 convert amapping This

sets represented

aslists between

has to reestablished

positions

is a list

of elements.

a bit

set

in u is in the
on Programming

A universe is specified by a universe. b is associated with a universe u, then set represented by b if and only if the ith bit in b mapping integer
and Systems, Vol. 13, N0. l, January 1991

Transactions

Lan~ages

Automatic Transformation

of Series Expressions mto Loops

77

(defun collect-logior (bsets) (declare (optitnizable-series-function) (collect-fn integer #(lambda ()


(defun

) O) #loglor

bsets))

collect-logand (bsets) (declare (optirnizable-series-function) integer #(lambda () (collect-fn


Fig. 9. Operations

-1)

) #logand

bsets))
sets.

on series of bit

is 1. the

For set

example, {a,b,d}. bit. ) a bit set in the that

given (By and

the

universe Lisp

(a b c d e), convention, universe, of its their with the

the

integer Oth bit in

#bOIOll

represents is the converts by scanning alist set. (When of

Common its

the

an integer

rightmost Given the the the no bit

associated along

procedure It and

bset->list does this constructing the bit

set into

a set represented universe

as a list

elements. positions

elements elements :upto The

correspondto is supplied, list->bset

Is in the scan-range converts is the of the should bit

integer counts

representing up forever.) as alistof is to

argument

procedure set. Its

a set represented universe list, the that

its elements with

into the

a bit bit

second For which with integers

argument each bit element

be associated bit-position procedure

set created.

procedure 1. The

is called ash is used usedto into a bit the bit to

to determine create combine The position depending procedure the universe. Iftheitem onto the is done Figure The first the

position the correct list.

be set to 1. The individual and

an integer

setto to the

procedure elements auniverse operates universe. the

collectfnis together and in The position returns returns one first

corresponding takes the not item. the

set corresponding procedure

to the

bit-position to or

an item The

corresponding on whether

procedure is in the

of two line

ways of the

item

contains If the is not end of the by side effect 9showsthe procedure the

a series item in the universe so that

expression in the

that

determines the line

of the nil. adds of the

item

in

is not

universe, second

expression

universe, and it will the

the

of theprocedure The extension in the sets, recorded

the

item

returns oftwo

its position. collectors that

universe sets.

be permanently union of a series

universe. of bit the second

definition computes

operate of bit

onseries while

computes

intersection.

Live variable
sider the as blocks part block

analysis.

Asanillustration that

of the

way

bit

sets might code is being

be used,

con-

following.

Suppose

in a compiler, by

program possibly

represented The pieces top Each of to. these about that (i.e., the are field because
1991.

of straight-line 10 shows pieces blocks are the

code the

connected structure can branch

cyclic with the it.

control ablockof

flow.

of Figure Bhasseveral

data that

that associated that

represents

code.

ofinformation as a list

Twoofthese other

information A program fields. In addition the read that way written (computed are live by without

to B and point

blocks

B can branch
through

is represented to control are block the

of blocks

to each contains the the by below)

flow and

information, In the variables

each structure particular, that they before it are are is live


and

information variables block An

variables being by the

accessed.

records used written).

either

written procedure

or read

additional

determine-l~ve

discussed

records

variables

at the end of the block.


ACM Transactions

(A variable

if it has to be saved,
Systems, Vol. 13, No.

on Programming

Languages

1, January

78

Richard C. Waters

(defstruct (block predecessors successors written used live t emp )

(:conc-name nil)) ;Blocks that can branch to this one. ;Blocks this one can branch to. ;Variables written in the block. ;Variables read before written in the block ;Variables that must be available at exit. ;Temporary storage location.

(defun determine-live (prograwgraph) (let ((universe (list nil))) (convert-to-bsets program-graph (perform-relaxation program-graph) (convert-frori-bsets progra-graph program-graph) (clefstruct (tetnp-lxets used written live) (:conc-name

universe) universe))

lxet-))

(defun convert-to-bsets (program-graph universe) (iterate ((block (scan program-graph))) (setf (temp block) (make-temp-bsets :used (list->bset (used block) universe) :wrltten (list->bset (written block) universe) :llve O)))) (defun perform-relaxation (program-graph) (let ((to-do program-graph)) (loop (when (null to-do) (return (values))) (let* ((block (pop to-do)) (estimate (live-estimate block))) (when (not (= estimate (bset-live (temp block)))) (setf (bset-live (temp block)) estimate) (iterate ((prev (scan (predecessors block)))) (pushnew prev to-do))))))) (defun live-estimate (block) (collect-logior (mapping ((next (scan (successors block)))) (logior (bset-used (temp next)) (loganclcz (bset-live (ternp next)) (bset-written (temp next))))))) (defun convert-from-bsets (program-graph (iterate ((block (scan program-graph))) (setf (live block) (lxset->llst (bset-live (temp (setf (temp block) nil)))
Fig. lo.
Live variable

un,verse)

block))

universe))

analysis

it

can

potentially field, which

be

used with

by by the

a following procedures blocks. the

block. (such procedure determines

Finally,

there

is a temporary that which that are bit uses perform givena live in sets.

data The

is used of Figure perform

as determine-live) determine-l,ve, the the step variables procedure bit

computations remainder program each The

involved

10 shows this

represent To

ed as a list

of blocks, computation steps. data

block. procedure

efficiently, The first structure

operates

in three

(convert-to-bsets)

looksat

each block

and sets up an auxiliary

containing

set representations

ACM TransactIons on Programmm~ Languages and S~stems,Vol. 13, No. 1, January 1991

Automatic Transformation
for the written in the second variables, This auxiliary temp field step (perf the used

of Series Expressions into Loops


and integer an initial by the third guess form that there

79
are no 10 and

variables, The

live variables. is stored The is done

structure of the block.

is defined

in Figure an empty

O represents which are no live is obtained.

bit

set. This block

orm-relaxat initial the

ion) guess correct

determines that there

variables variables

are live. in any

by relaxation. improved step

The

is successively The block into third

until

answer

(convert-f and the

rom-bsets) bit

operates

in the reverse of the block. a block live

of the first variables

step.

Each

is inspected a list, which

set representation live field of the

is converted to determine

is stored of the loop

in the

On each cycle whether the to it the minus bit the sets.) successors

in perform-relaxation, To do this Each that variables procedure are plus of the inspected. the (The

is examined needs the

its live set has to be changed. of the block it writes. estimate variables variables A new it uses,

(see the procedure successor are supposed takes needed

live-est to have to be live difference

imat e), available after it, as of two

logandc2

total

set of variables

by the

successors

a group If are this live,

is computed new then estimate the

by using

collect-logior. from the In that new This there. current addition, estimate if the of what variables is changed, current block block requires onto stop

is different

estimate

is changed. sure the

estimate of the current each

perform-relaxation will their the be examined live list estimates unless comput Figure to

has to make see whether to be changed. it is already at ion stops.

all the estimate is done As soon

predecessors for by the

adding

predecessor of liveness

to-do the

as the

estimates

changing,

Summary.
sions are opt imizable. computations cient ly using

10 is a particularly to be used series would in three expressions otherwise on lists is expressed

good ways. are used

example First, all

of the the

way

series

expresare

intended

series

expressions

Second, that

in a number less clearly the main

of places as loops relaxation because the

to express or less effialgorithm data flow

be expressed Third, This

operations

or vectors. as a loop. being

in perform-relaxation in this This der algorithm the

is done, into

prevents fact that

it from

decomposed series but

two

or more

fragments. to renimproved

highlights iterative for

optimizable obsolete, majority vast

expressions rather

are not

intended

programs expressing

entirely the

to provide

a greatly

method 5.

of loops.

A PASCAL IMPLEMENTATION
can be added system to Pascal has been in in much Lisp rather error the ed that than is issued same way Pascal as they and are added this only [29, 46]. supports to Lisp. However, optimizis blocked. this still A construct demonstrates whenever Lisp

Series the able

prototype series

prototype

is written expressions.

A fatal than the

optimization implementation,

Although allows

less complete loops

approach

of the series

to be replaced Pascal series

by opt imizable preprocessor as shown


of

expressions. the declaration of series in anal-

Series. The
ogy with
type

supports below.

array

declarations = series series general same

var In line

Integers InputData: with have the

Integer;

of

Real; of Pascal, the it is required length that all the is not


13, No.

philosophy type.

elements of its
1991.

of a series

the

However,

of a series

part
1, January

ACM Transactions on Programmmg Languages and Systems, Vol

80

Richard C. Waters

Series Function collection scanning(z, collecting(z. mapping(~,


truncating(~, choosing(~,

Pascal CollectFn(z,

Implementation >, R)

(z, >, ~, Rl,


R)

R) T) .,. , I?n)

.F, R)

ScanFn(z, ~, CollectlngFn(z, MapFn(~, Rl,


TruncateIf(P, ChooseIf(T,

P) >,
R) R,) R) , Rn)

R) S, T) S) S, z) n, m) d, R) 11. Pascal

mingling(R, catenating(l?, spreading(l?, sectioning(R, chunking(w, Fig

Mingle Catenate Spread

(R, (R, (R,

S, S, (R, d,

7) S) z) n, R, m) S1, . . . Sm)

Subseries Chunk(u), support

for series functions

type.

This

is important

to length. The

facilitate

the

definition

of series

procedures

operating

on series

of arbitrary

Series procedures.
by Pascal the (e.g., not same procedures names linked the becomes to

series

functions in Figure (The Lisp Lisp

described procedures in that

in Section the with [29, 46] any

3 are

all

supported have set does the removed

as shown ScanFn). earlier

11. In general, examples procedure

Pascal show

procedures hyphens an Since obsolete Pascal values,

as the an

corresponding

scan-fn support

of names

implementation

of series. ) returns

concept

of a function into

multiple

out puts

of chunking ((l,2),

are turned ChooseIf(Odd, ,5,9), Xs,

arguments. (8,-7,6,-l))) ~ 2, := 4) (1,5,3) ~ (1,2,-7,1) (5,6) and


YS := (5,3,7)

Catenate Subseries Chunk(2,

(Mlngle((l 1, (1,5,3,7),

(2,6,8), Ys) ~ ~ (3,4,5) Integer): 2

<), Xs

MapFn(Average, Xs, Ys) function Average (x,y: begin Average := (x+y) div end; (Since the syntax the Pascal

Integer;

implementation 2 is used is very situations.) does 3for type the

does in the

not

provide

asyntaxforseties section. Note that

literals, while

from for

Section literal

examples

in this

a notation Iittle The their the Since, method The parameters they in

series

convenient not two extend reasons.

in small the Given without of multiple need the for usual allow

examples, higher-order the strong

it is of relatively procedures typing type values, over

importance Pascal specifications preprocessor Pascal needs does

in other implementation infection can not

in Pascal, arguments. some other on the

obtain support

information concept the follow

needing return Pascal the

to be employed in Figure

to avoid 11 do not of the allow

tuples. restrictions number in the any the not of arguments types oftheir generic and any being

procedures

of procedures. vary This For and instance,

Some they all

procedures considerable the series

receiveto

flexibility procedures to with 11 could

arguments. character. of series type

is important

because

are inherently number procedure

MapFn is naturally types

applicable

aslongas generic

the element nature,

are compatible in Figure

mapped. Due to their the procedures However,


and Systems,

be implemented they

asuser-defined
ACM Transactions

procedures
on Programming

in Pascal.
Lan~agss

asanextension
Vol 13, N0. l, January

to the language,
1991.

Automatic Transformation
do not violate the spirit of Pascal. the same most

of Series Expressions into Loops


the (e.g., are such predeclared Read and to [51], Pascal write) more it

procedures variable one

81

In particular, way. Several

are generic numbers of object. to implement

in exactly of arguments Using (at

allow than

and

of them language the

applicable as Ada series

type

a more least

flexible most of)

would

be possible

higher-order

functions

as user-defined

procedures. All tation as well. arguments. not lists. of the that specific are scanners, to collectors, Pascal in but are and transducers by and these the from the Lisp implemen-

applicable the strong with Pascal

supported scan lists,

Pascal

implementation do not apply need to sets type and

Given

typing has sets, the

Pascal, not style rather test,

Collect functions

Since In keeping

general argument test, z)

of Pascal, than

collect returning

takes

the

destination value.

vector/string/set Series (test)

as its * ~ 3) (T,

first

an aggregate

(test, u, ~ (1,2,3) z)) ~ fi,fo + 6 fi,fo )

. ..)

Scan(Tuz) ScanRange(l, Collect (X,

(T, > u, (fee,

Scan( [Men, Wed, Fri]

(Men, Wed, Fri)

{ places ,fum)) ,fum)) none ,fum), (1,2,3)) ~ ,) * (1,3) * *

Tuz 4 fum

in

X.

CollectSum((l CollectLength( CollectLast CollectLast((), Previous((fee,

,3,2)) (( fee,

none) fi,fo

(>

, ,~fee>,

>fi),

]fo~ )

Choose((true,false,true), Positions(MapFn(Odd,

(1,2,3,5,6,8))) To avoid constructs a related procedure series. the series making

=$=- (0,2,3) syntactic tothe that extensions, Lisp the Pascal mapping even implementaand more iterate. useful. mapped Sqr is autoset.

Implicit
tiondoes However, Whenever over the matically

mapping.
not it support supports

analogous concept is applied For example, of numbers

forms

is in

many

ways

anon-series elements mapped over

to aseries, in the created

it is automatically expression by scanning below, the

of the

CollectSum(Sqr(Scan( [2,41))) = CollectSum(MapFn(Sqr, The key virtue that on the of implicit have previous and to be

Scan([2,4]))) is that For can

* it

20 reduces in the the number of of helping

mapping defined. you

procedures average procedure

instance, the

example instead

a moving
a

page, explicitly Xs,

write it.

following

of defining

Average 1,

mapping Ys);

Chunk(2, The However, it, implicit type added conceptof

(1,5,3,7), implicit series

(Xe+Ys)

div

2 * separate

(3,4,5) from easily the be other dispensed that concepts with. support is being

mapping expressions.

is completely As with useful. such, APL and it

associated

with asshown

could the

by experience is extremely introduces implementation.)

other

languages ofreliable implicit

mapping Lisp

(Although

thelack

compile-time

information to the

a number

of complications,

mapping

User-defined
dures in Pascal

series procedures.
are simply procedures

As

shown that

in the

examples series

below, inputs

series or return

proceseries

either

have

ACM Transactions

on Programming

Languages and Systems, Vol. 13, No. 1, January 1991.

82
values.

.
As

Richard C. Waters
with series in general, all such definitions kind are handled directly Pascal by the does not

preprocessor. support the

There concept The

isnoneed of macros.

foranyspecial

of declaration.

Example. used

following As in the around The

example last a job section, queue

illustrates all data of the

how

series

expressions

can

best

be The

in Pascal. revolves system.

expressions that below. record

are opt imizable. might be used in

example operating to a chain of fields type type type type There procedures a job queue, the

abstraction are shown

an

basic

type

definitions

A JobQ is a pointer containing a number

of jobs.

A Job is a pointer a numerical priority.

to a descriptive

including

JobQ = JobQentry; JobQentry = record; Job = JobRecord; JobRecord = record are a number include a queue

JobInfo: Priority:

Job; Real; defined

Rest:

JobQ end; end;

of procedures anew near job

that a queue of this

operate (shown section).

on job below) To

queues. and

These removing onto frontof a

putting (discussed needs

onto end

from

the anew

add itto

a job the

one merely

to allocate

queue

entry

andattach

queue. procedure AddToJobQ var E: JobQ; begin new(E) ; E-.JobInfo := J; E-.Rest := Q;


Q,=E

(J:

Job;

var

Q: JobQ);

end In define as with out addition a number any that to ordinary of series data job convert have Q-.Rest, using operating procedures procedures structure, queues that that operate operate ofjobs on job on job to have and that queues, queues. procedures vice versa. enumerates below, scani% it In It is useful particular, and turns tails of also to

aggregate

it is useful to series ScanJobQtails .Rest, . ..)). series

ScanJobQ all the

CollectJobQ

to be usefulto (i.e., (Q,

a procedure Q-.Rest-

a queue

AS shown procedure

ScanJobQtails and two locally-

can be implemented defined procedures

the higher-order onjob queues.

function ScanJobQtails (Q: JobQ): series of JobQ; function JobQrest (Q: JobQ): JobQ; begin JobQrest := Q-.Rest end; function JobQnull (Q: JobQ): Boolean; begin JobQnull := Q=nil end; begin ScanJobQtails := ScanFn(Q, JobQrest, JobQnull) end Among below. over ing apointer the job other The things, ScanJobQtails ScanJobQtails(Q) the Joblnfo returned can be used .JobInfo field ofa to implement causes JobQentry the ScanJobQ operations asshown of followmapped

expression and queue

selecting pointers

to be implicitly

by ScanJobQtails. series -.JobInfo of Job;

function ScanJobQ (Q: JobQ): begin ScanJobQ := ScanJobQtaile(Q) end


ACM Transactions on Programming Lan~ages

and

Systems,

Vol.

13, No,

l, January

1991,

Automatic Transformation
The procedure using the to the is returned one job queue tails last RemoveFromJobq ScanJobQtails of the and as the next queue to last removes

of Series Expressions into Loops


a job from the end of a queue. with,

83

It can be

implemented enumerates pointers entry least the

as shown and uses

below.

To start and The

RemoveFromJobQ to last there obtain queue is at

CollectLast queue. (It next

previous job in the that entry with

entries

in the

result The entry

of RemoveFromJobQ. rest from pointer the The in the queue. storage

is assumed to last

in the

queue.) the itself last

is set to nil, to last the last entry, entry

in order is then

to remove variable freed.

(If there

is no next

is set to nil.)

associated

function RemoveFromJobQ (var Q: JobQ) : Job; var Qs: series of JobQ; NextToLast, Last: JobQ; begin Qs := ScanJobQtalls(Q); Last := CollectLast(Qs, nil); NextToLast := CollectLast(Previous(Qs, nil), := Last.JobInfo; RemoveFromJobQ if NextToLast=nll then Q := nil else NextToLast.Rest := nil; dispose(Last) end The while first the three statements in the body in the be noted

nil);

ofRemoveFromJobQ body that are non-series there aloop

form

a series

expression, From instance traverses consider an of the the the

remaining standpoint, the

statements it series should

expressions. is only that one only

eficiency queue As

since into

ScanJobQtails, once. a final

expression use This job the of

is converted optimizable

example

of the below. than

series ajob priority of the

expressions, queue is more the form the and

procedure last is no sion viation series returns cause (i.e., such that of the the darddeviations

SuperJob longest job,

procedure queue first average The

inspects whose four needed

returns twostan-

queued) larger nil

in the

than queue.

priority

jobsin

Ifthere expresand de-

is returned. the basic of selects jobs, the

statements to three calculate

a series mean form

computes the last selection

information jobs. jobs any. until if The that The

priorities that of these cannot

last have

statements large be

a second and bebeen

expression

the begin

sufficiently has mean to and

priorities twice, have

queue the

scanned deviation

after

computed. function SuperJob (Q: JobQ): Job; var Jobs, SuperJobs: series of Job; N: Integer; Mean, SecondMoment, Deviation, Limit: begin Jobs := ScanJobQ(Q); N := CollectLength(Jobs); Mean := CollectSum(Jobs.Priority)/N; SecondMoment := CollectSum(Sqr(Jobs.Priority))/N; Deviation := Sqrt(SecondMoment-sqr(Mean) Limit := Mean+2*Deviation; Jobs := ScanJobQ(Q); := Choose(Jobs .Priority>Limit, SuperJobs := CollectLast(SuperJobs, nil) SuperJob end
ACM Transactions on Programming

Real;

);

Jobs);

Languages and Systems, Vol. 13, No. 1, January 1991.

84
The

Richard C. Waters
above are a good with, the that all example of the way series expressions are intended This vast simple

programs

to be used. reflects majority programs. free form the

To start fact When that

of the in

programs of series

are straightforward expressions is to programs

in nature. convert into dirt the

primary are

goal fact

of programs

straightforward it is usually other than very

a program having

is straightforward, to use anything

easy to write simple series

it in a loopexpressions.

without

6. THE OPTIMIZATION A preprocessor


into loops can and a graph converts of the the Lisp original Pascal expressions collapses which place

ALGORITHMS extension
them into node

or compiler
converts into this

that transforms
stages: equivalent representing data

optimizable
which graphs, flow

series locates

expressions optimizable which

be implemented

in three

parsing

pipelining
and inserts the

a single node into expression.

an equivalent code and

loop,

unparsing
code in

appropriate

program

Below, Common tics

implementation works and the the parsing preprocessor are While

of series in the unparsing is applied located this

is used same

as a concrete except that

illustration. the

The

implementation

way,

characteris-

of Lisp

simplify When

stages. to a program, the static it begins types by of the parsing procey and

Parsing.
the dures program. called

Series by the

expressions program.

by inspecting done, and the any

is being

analyzability reported.

straight-line

computation like mapping Pascal can

rest rict ions where

are checked

violations type

In a language able, dures implicit

complete

compile-time by noting places

information non-series

is availproceuse

be supported Each such

where

are applied

to series.

application

is replaced

by an appropriate

of MapFn. The to each final action of the series parsing stage is to create located. to do. the variables a data each flow graph corresponding expressions becomes the is a a node way the

optimizable

expression this is easy

Since Each nodes

of these call

straight-line in the graph

computation, and the data

procedure is derived

flow and the

between way

from

procedure

calls

are nested The operation in the The only

are used. is illustrated computes the loop code the 5). the shown in Figure sum of the fact uses not 12. The squares that is quite more to lead it

Pipelining.
series expression of the odd

of the

pipelining SumSqrs

stage (which into loop

procedure readability

elements

of a vector)

is transformed of the generated problem (e. g., as long

in the procedure

SumSqrsPipelined. contains efficient. variables inefficiency is available. The conquer for While Once a single flow pipelining strategy doing this, a number The than

is reduced However, pipeliner this

by the the need sometimes

of internally significant necessary execution process discussed into the

variables.

code

is that Reeult

strictly

However, capable In the data flow

during

as a compiler steps. all the the

of simple first step, the connects obeys cluster the

optimizations the data divide flow on-line and graph ports. cycle into data series

operates clusters

in several where that

in Section checks the

2.5 is used

to partition expression in each

a series

expression

on-line

pipeliner is complete,

rest rict ion. partitioning procedure. the procedures the are combined based process, on the each The resulting procedures To support are then combined

between

subexpressions.

combination

ACM Transactions on Programmmg Languagesand Systems, Vol 13, No. 1, January 1991.

Automatic Transformation

of Series Expressions into Loops


Integer) : Integer;

85

function SumSqrs (V: array [1. .N1 of begin SumSqrs := CollectSum(Sqr(ChooseIf(Odd, end

Scan(V))))

[11 [41 [1] [11 [11 [21 [31 [41

function SumSqrsPipelined label 0,1; var Element12, Index15, begin ~ndex15 := O;

(V:

array

[1..NI Sum2:

of

Integer):

Integer;

Result5,

Integer;

sum2 := o;
:= l+Index15; Index15 if Index15>N then goto O; Element12 := V[Index15]; if not 0dd(Element12) then Result5 := Sqr(Element12); sum2 := Sum2+Result5; goto 1; := Sum2 o: SumSqrsPipelined end 1:

goto

1;

[11

-- Scan of a vector
inputsoutputsvarsprologbody-

---------------------------------

[A..LI of ElementType; Vector: array Element: Series of ElementType; Index: Integer; l-K; Index: Index := I+Index; if Index>L then goto O; Element := Vector[Index];

[2]

--

ChooseIf inputs-

----------------------------------------Boolean; function P(X: ElementType): Item: Series of ElementTbype; Item: Series of E1ementType;
2;

outputslabelsbody-

2:

NextIn(Item); if not P(Item)

then

goto

2;

[3]

--

Implicit inputsoutputsbody-

mapping

of

Sqr __________________________

Item: Series of ElementType; Result: Series of ElementType; Result := Sqr(Item);

[4]

--

coll~cts~ inputsoutputsprologbody-

-----------------------------------------Number: Series Sum: ElementType; of

ElementType;

sum := o;
Sum := Sum+Number; Transforming
Transactions

Fig. 12.
ACM

optimizable
on Programming

series expressions into loops.


Languages and Systems, Vol. 13, No. 1, January 1991.

86

Richard C Waters

procedure

is represented Input Output Auxiliary Labels Statements Statements Statements part series in each to data procedures the output

as a loop

fragment

with

one or more

of the following

parts:

inputs outputs-

variables. variables. variables used by the that that that used by the comput the at ion. computation starts. computation. before executed. after fragments These from. flow ports. flow between on-line ports (e.g, the Sum), of Sqr to the input the various and labels The of Collect the loop that fragments in the Two left terminates. represent are hand on-line the procedures to crealone

vars labelsPrologbodyepilogThe called ate the which gorithms bottom by the loop fragment

are executed are repetitively are executed 12 shows the

of Figure expression line flow

in Sumsqrs. The loop comes

combined margin ports combination

SumSqrsPipelmed. of the one corresponding touching

numbers to data

indicate and

different

are used:

between

corresponding When data flow two from

off-line

are connected of the implicit by simply In addition,

by data mapping

the procedures sponding

are combined together. there will

concatenating the variables

parts

of the correfragments between

fragments so that

in the data of the flow

are renamed the much of loop When input in the instance computed. algorithm the call fragment. evaluation procedures

be no possibility by output renaming variable of the

of conflicts. the input

is implemented same as the as an application [3].) procedures data the form two that rather process flow fragment Next In, fragments the body than

variable (The

destination above technique is

so that

it is the the same fusion two figure), of the The except on NextIn, (This [17].)

of the

source.

process

standard

compiler

optimization

are connected from the

by series output specifies the

data

flow

terminating series of the input procedure

on an off-line of Choose If contains should combination in place case of an be

(e. g., the

of Scan to the destination elements when

representing which are combined of the being

input

exact 1y as in the fragment with support in

on-line

source

is substituted the body for a simple

concatenated compiles

of the destination of lazy

essentially

Unparsing.
the into The into series outputs the a loop

The

result

of pipelining In the The below. of the in the

is a single unparsing combination surrounding series

loop

fragment this when process

that fragment eliminates the The loop

corresponds is converted the parts inputs. is substituted

to

expression

as a whole.

stage, code

as indicated are connected in place directly

up to the loop.

program appear label var begin

original

expression.

other

of the

fragment

O, 1, Iabek;
vars;

prolog ; 1:
body;

goto O: epilog; Once each

1;

series

expression Pascal

has been compiler.


and

replaced

by a loop,

the

resulting

code

can

be passed
ACM

to a standard
on Programming

Transactions

Languages

Systems,

Vol

13, No.

1, January

1991.

Automatic Transformation of Series ExpressIons into Loops


Side-effects.
only been However, The correctness under that the preserving assumption if unoptimized nature that series of the there transformations no side-effects

87
has in

above

verified

are

involved.

it is believed of Section presence computes for

are implemented

as illustrated preserving expression

the beginning even that in the only

4, then

the transformations as long output. transformed For code instance, motion lazy as they

are also correctness are applied to a series

of side-effects

a single
this is that

overall the

The ation of the the Path value

reason of the series

exactly the

mimics off-line requires

the port the

lazy

evalu-

untransformed code elements to the

expression. motion; however, place

combination the generation elements of

algorithm

involves

this

simply

moves

where

evaluation

series The

to be first seeks

computed. more formal at from compile the point of view in This of path a program analysis each can place [1 I]. lazy to determine and where in two time where

above analysis will

can be made used

be first lazy value,

it will ways.

be reused. If there

information

be used use for have

to optimize of a given that value.

evaluation then ordinary

is an identifiable instead the

of first

evaluation last

can be used use for

of lazy value

evaluation does not

If there beyond

is an identifiable that time. 2 guarantee series

a value,

to be stored The place last

restrictions where each

in Section element the the

that is first next

for each series, used and that, The

there for

is an identifiable each element, the above omit

of the

use precedes position long term above because results

computation computation

of the of the

element. at their that

transformations of first use and

merely their The atical, the net

elements realize

place

storage. one should makes will be. side-effects are still to figure problemout what func-

notwithstanding, lazy evaluation

it difficult Some that

for programmers situations mapping can will apply

of side-effects one can depend

be readily

understood.

For example, tion ~ first

on the fact of the

the mapped and

to the first order. series Thus,

element if ~

input, with

then itself

to the second, or the

so on, in strict outside or output), of the but

temporal cent aining does More 6.1 The years. Lets Lisp. The not

interacts X via

environment input

expression with

side-effects the should

(e.g., result

by doing

interact

anything

else in X,

is easy enough

to understand.

complex

uses of side-effects

be avoided.

Systems Based on Similar Algorithms


algorithms The first [43, 44]. described generally current The above available Lisp have evolved into their current form macro over package thirteen called Common

implementation

was a Lisp [4i] is available

implementation

in portable

same

basic

approach

to representing by Wile [49].

and combining However, same he does can

sequence not explicitly

procedures address intermediate

was the APL

independently question sequence compilers A quite ever, from rather series. series than can [12].

developed be eliminated.

of restrictions

and his approach Much the

does not guarantee be said

that

every

about

optimizing

similar

approach in the next In

is also particular,

used it

internally Loop

by the macro

Loop

macro English-like

[13].

Howsyntax on

as discussed

section,

the

is externally of procedures

very

different

expressions. representing

uses an idiosyncratic

computations

as compositions

operating

ACM Transactions on Pro~rammin~ Languages and Systems, Vol.

13, No.

1, January

1991.

88

Richard C. Waters

7, COMPARISONS
There with are related two primary concepts. vantage The From most with view this of the a few series points from which comparison the key to compare is with feature by the and are being series other of series vector expressions support for expressions operations operations continuation reuse of loop taken of series variety presents Each definition It also of of of

most

obvious perspective, operations of Common

sequence is that APL of Seque Another of the

expressions. they [20] support sequence (along to

supported Lisp is that toward [37], they ones) while

[32], the

operations

the more

stream

additional expressions

efficient. the

way

a logical

trend

in programming-language From this point [28] and of view, the Lisp

design series Loop

supporting extend The the key of being of this other

fragments. by iterators

expressions macro [13]. the

approach feature

in CLU in and depth this to

expressions fragments To detailed these lend

context the

is that

they above,

support the

reuse

a wider section

are easier

to understand comments series the the

and modify,

without

any less efficient.

remainder and This positive five

comparisons comparisons that a new how

between features computes series

expressions below. of the can

approaches. shows the of a vector.

example sum

example elements

of a procedure illustrates

procedure

be defined.

function SumPositive (V: arrav [1. .N1 of Integer) : Integer: function Positive (X: Integ~r) : Boolean; begin Positive : = X>O end; begin SumPositlve := CollectSum (Choose If(Posltive, Scan v))) end function CollectSum (S: series of Integer) begin CollectSum := CollectFn(O, +, S) end : Integer

7.1 Other Support


There sions, make sions.
APL.

for Sequence Expressions


languages bet ween languages each that provide that support it would languages for sequence and series expresto expres-

are many e.g., Three One [24, instead detailed

programming comparisons

[5, 6, 20, 26, 33, 35, 36, 37]. represent ative

So many, are discussed languages has

not

be practical

one of these

below. that takes where the a functional vector in Figure


corresponds

of the oldest 32]. 3 and A style the


extension

and must of writing The vector


of scalar

used
APL

approach are

is in

APL

evolved between

expressions 13.
to

used

of loops.

correspondence operators
operations

series

functions

discussed The
implicit APL

Section
of

APL

is summarized
to vectors

concept mapping.

the

As illustrated quence
rect ly

below, can

both

the

vector

summation represented

algorithm in APL.
an explicit

and
Since scan

user-defined
sequences operation. are

sedi-

procedures
represented ~ SUM= SUM+

be compactly
there

as vectors,

is no

need

for

[11 v ~ [11 7

SUMPOSITIVEAPL V COLLECTSUM ( (V>O) /V)

SUM+ COLLECTSUM SUM~+/NUMBERS

NUMBERS

ACM Transactions

on Programmmg

Languages and Systems, Vol. 13, No. 1, January 1991

Automatic Transformation

of Series ExpressIons into Loops

89

Series Function collection (z, .F, R)

APL OperatiOn F/R missing F\R RI (((P F Rz R) L 1)1) TR scanning extension take compression catenation based on expansion take and drop I m. ~ R

name

reduction

scanning(z, collecting(~,
mapping(.F, truncating(P. choosing%, mingling(R,

2, ~) .F, R)
RI, R) R) S, T) . . . . Rn)

of scalar

operations

(P R)/R missing R,S idiom (n-m) missing n)

catenating(R,
spreading sectioning chunking(w, simple missing idioms (R, (R,

S)
S. z) m, d, R)

index generation,
reversal, rotation,

membership,
grade up/down,

inner product,
modifying

etc.
elements

Fig. 13.

The correspondence

between series functions

and APL operations.

The

key differences unbounded given

between

APL and

series

expressions

are that

APL vectors

can-

not represent and users

sequences, any feedback

the set of APL operations about could what have is efficient been

is somewhat and what in In initially operation. be one of the As a result, a small initial set identity

different, is not.

are not all built-in support the

Although no direct does not

the

series support

operations for scanning,

supported

APL, addition,

there

is APL For

mingling, as well

or chunking. as it might

higher-order operator

operations appears operations

appear.

instance, at least scalar t ion

reduction APL,

to be a higher-order to be reduced cannot must be used. scheme for the

However, predefine the reducto of specific value

in standard

the operation just part

operationsuser-defined operator is actually (This has the

of a naming benefit

collectors. be implicit.)

collateral

of allowing

APL supports are not supported

four

operations by series

(reversal, expressions,

rot ation, because significant When language

grade they

up, cannot

and

grade

down)

that in the one

be implemented APL also allows

a preorder modification has to rely operations. For first more always Explicitly

fashion of the on other

without elements

introducing

inefficiency. using when

of a sequence. in the host

series

expressions, any

constructs

performing

of these

instance, collect obvious, requires APL the creating

to

sort

a series into it a list does

in or not

the

Lisp

implementation and then, the more of the sort the makes

of series, resulting nature

one

must

series however, that

vector structure make

structure. of sorting sorting

an intermediate

expensive expensive, sequence efficient however, more

sorting

because be created. code most efficient. are not

some

physical

representation capable

A few

compilers where

[12, 22] are expressions expression

of producing

in most are not.

of As

the sit uations a result, even least An that

series series

can be optimized; are typically much

optimizable the about ly) where array some

Further, given are any (at is can of


1991.

when implicit area the

compiler whether encouraged APL

supports optimization not

optimization, has occurred. about more in Figure to think structure

programmers Rather, issues. than array. a few such

feedback

programmers series APL

is fundamentally (not shown In

powerful is the 13) and

expressions has a number series

standard array

intermediate operators expressions.

in APL

of powerful opt imize

APL compilers to have


13, No.

contrast,

while

it is possible

ACM TransactIons on Programming Languagesand Systems, Vol.

1, January

90

Richard C, Waters

Series Function collectlon(z, $, R)


scanmng(z,
collecting mapping(.F, (z,

Sequence Operatmn (reduce


missmg missing
. . l?n) (map

R)

>,
F, RI,

P)
R)

type R

R1 P R) R S P) R type

R~)
T R))

truncating(~, choosing(P, R)

1?)

(subseq (merge missing


(subseq

O (posltiOn-lf

(remove-if-not type
S) (concatenate

mingling(l?,
catenating(R,

S, P)
S, z)
m, n)

S)

sprearling(R,
sectiOning(R,

R m

rL)

chunking(u~i simple missing Fig. 14 idioms

d, R)

missing elt, length, sort, count,


find, some, etc

reverse, between

modifying

elements t~perations.

Thecnrrespondence

series functions

andsequence

series, never is that special

there

are

no special but

series striking

operations difference

for

operating

on them, series

and

they and

are APL

optimized. a superficial expressions but between calling expressions while series use standard cryptic, subroutine notation APL uses a

Finally,

set of concise,

operators. While Lisp used are many instead (if not most ) Lisp programmers computuntil

Common
use loops ing recently, it

Lisp Sequence Operations.


a style and of writing vectors lists supported mapcar, was rectified Lisp, since the both but

extensively, Lisp

has evolved set of predefine When Common

where

expressions Unfortunately, sequence

intermediate

of loops.

an impoverished not much else.

operations designed [37],

supported defect

Lisp

was

this In tor.

by introducing term sequence

a relatively is used are basic 14.

comprehensive to refer to either

suite

of sequence or a vecbounded seseimplicit the Lisp

operations. Common However, Lisp The a list of these are not structures a complete the Figure in limited series Lisp to representing at ion functions does not and

sequences, quences. quence mapping. The

sequences correspondence

implement

of mathematical support

between

operations

is summarized

example the

below vector

shows

how the Common algorithm there

Lisp and

sequence

operations sequence scan

can be used procedure. procedure.

to express Since

summation sequence,

a user-defined for an explicit

a vector

is a Lisp

is no need

(clefun sum-positive-sequence (collect-sum (reIIIOVe-if

-not

(v) #pluSp

V)))

(defun collect-sum (nmnbers) (reduce #+ numbers) ) Except there operations. spreading, is specified a way that Current
ACM

for

the

fact why

that there

Lisp

sequences series direct

cannot could built-in

represent not impport value for

unbounded scanning,

sequences, by sequence collecting, (collection) in such op-

is no reason

all the

functions the identity

be supported

However, or chunking. in an odd when Lisp called

is no

In addition, way. with The zero

to use for reduce must the be implemented identity

procedure arguments optimize


and

argument it returns sequence


Vol.

value. As a result,

compilers
on Programming

do not
Languages

expressions.
1991

Transactions

Systems,

13, No. 1, January

Automatic Transformation
timizable mization, As in APL, elements Lisp An port does series expressions surprising are that toward much Lisp

of Series Expressions into Loops


efficient. In light of the about lack

91

more

of opti-

it is not there

provides

no feedback and

optimizability. of sequence however,

is no bias It

preorder to

functions have for

modification of sequences, them. they

is allowed. not provide

is also

common

sequences manipulating

any

special Lisp

operations sequence that

interesting a number the

aspect

of the

operations modify their takes in the

is that

typically For and

sup-

of keyword sequence a count

arguments

behaviors.

example,

consider and predicate.

operation of the

count -if,

which

a predicate sequence

a sequence, satisfy the

returns

number

of elements

that

(count-if The which occur. procedure be tested same Lisp can In

#plusp operation be used addition, that will to

(1

-2

3 4 -5)) takes

~ two

3 keyword of the : key can arguments input be in used : start which to and : end, is to access should is the by the necessary. The : key

count-if specify

a subsection argument the part

counting an that which

a keyword be used

specify

to fetch Finally,

of each

sequence

element exists,

by the

predicate. except by the

an operation

count-if-not negates the options by

as count-if

that example

it automatically below, can none

values

returned

predicate. As illustrated The : start and and of these with is strictly subseq. complex : end keywords be dispensed with using

keyword

count-if-not

can be dispensed

by specifying

predicates.

(count-if-not ~ (count-if

#plusp ((1) (-2) (3) (4) (-k)) :start O :end 3 :key #car) # (lambda (element) (not (plusp (subseq ((1) (-2) (3) (4) (-5)) various options described subseq an above instead

(car element)))) O 3)) ~ 1 for two and ) reasons. : end keythey arguand

Nevertheless, First, words increase ments easier pression eliminates ping desired makes test y). and they

the

are important of the : start

promote

efficiency. because that it

(Using creates

is inefficient, the instead to read. series below, the it probability

intermediate

sequence.

Second,

predefine This

operations makes

can be used uses of count-if

as procedure more concise

of lambda

expressions.

Using

expressions, the

neither

of these does

issues not lead

comes

up.

In the

Lisp since

series pipelining for

ex-

use of subseries creation to avoid is simply count - if of its the mapped itself The to

to inefficiency, Convenient lambda in question with by by using series

physical possible key Finally,

output need for over approach and

series. the

support (again

mapThe

an explicit series taken makes

expression.

and

without allows in

inefficiency of choose the

can be dispensed

a combination

collect-length. procedures

expressions more functional

individual

be simpler

programs

appearance.
(let

( (elements
(collect-length

(subseries

(SC= ((l)
(#llnot

(choose the name Using

(-2) (3) (4) (-5))) o 3))) (#Mplusp (#Mcar elements)))))) ~ are the central technique and in data type

1 of the in the the

Seque.
language beginning The

Under Seque

of streams, the same the supports series

sequences lazy both

[20].

basic

evaluation bounded

discussed Section

of Section

4, Seque between

unbounded

sequences. 3 and

correspondence

functions

discussed

ACM Transactions

on Programming

Languages and Systems, Vol. 13, No. 1, January 1991.

92

Richard C. Waters

Series Function
collection(z, scanning(zi collecting(z, mapping(>, >, >, F, R) P) 1?) Red(R,

Seque 7)

Operation

name reduction generators redllction

! Length(R)

Idioms
Red(R,

based
F)

on , Rn
! Z)

RI, R) R)

. . . . Rn)

[F(R1 R\\
[If [If

I z,
P(R

1 z)]
then ZI] ! 1

derived filtering

stream

truncating(p, choosing(P,

post-truncation

P(R

! ,)

then

R 1 ,]

mingling(f?,
catenating(l?, spreading(R, sect, ioning(R, chunking(w, simple missing Fig. 1.5. idioms

S, P)
S) S, z) m, d, R) n)

missing R->S missing R{ml, missing


Length, referencing, operations over streams, etc.

concatenation n2} sectiomng

modifying The correspondence between

elements series functions and Seque operations

stream of these mapping streams.

operations operations

provided

by Seque

is summarized of special

in Figure syntax. when

15. As in APL, In addition, they and streams are

many

are provided for many the

by means non-stream vector

implicit applied to se-

is supported below, vectors,

operations

As illustrated quence data procedures from type

both

summation ! equivalent

algorithm Since

user-defined

can be easily

represented

in Seque.

are a distinct

an operation (V) [: IV:

to Scan is required.

procedure return end

SumPositive CollectSmn(

lambda(e)

if

e>O then

e] )

procedure CollectSum L := Length(S) return if L=O then end Since all the lecting, because spreading, this unbounded series

(S) O else Red(S, +) !L

sequences

are supported, However, when of the higher-order for defining stream collection

it would there is only

be easy to completely support for indirectly value to supported return.

support mingling, by colsequence, There there is is an and in like by Seque to make

functions leads

in Seque.

is no direct is applied default scanning. functions, It

or chunking. there

In addition,

to awkwardness for the

collection correct function scanning on.

to an empty However, both

is no specification support

also no direct impressive the the language series Seque eliminating series programmers use of assigning on efficiency. does

array Icon not

of facilities [19], which

in Seque to note

Seque to

is based the

is interesting are preorder. of much stream

that

operations, the

all of Seques attempt are never of less

operations evaluation

optimize efficient

expressions As efficient. and

computation are encouraged to the elements

unnecessary and in terms

intermediate often of streams

streams. more of streams for the

a result,

expressions

to think

of streams

without

any regard

consequences

Summary.
principal the much
ACM

In comparison They in They Except

with

the languages a wider with feedback


Systems, Vol.

above, range the about


13, No.

series

expressions than compilers,

have any and they

three one of are is

advantages. more efficient.

support comparison give clear


and

of operations of APL what

languages.

best

is efficient
1991.

what

Transactions

on Programmmg

Languages

1, January

Automatic Transformation

of Series Expressions into Loops

93

not.

As

part

of this, as preorder of the

they

make procedures

the

use of procedures more awkward,

that

cannot the

be efficiently programmer to

implemented use the

by forcing

facilities

host

language.

7.2 Looping
The

Notations
virtues by the construct. of series discussion fact that complex More expressions in the in comparison of Section the allow use of only support equivalent forms which the with 1. looping However, the most several constructs this basic of the diskind feaand as a to

fundamental

are illustrated cussion of looping t ures

beginning

is colored of series (but

by the

it illustrates looping they

constructs

expressions. not

In particular,

of scanners rather makes Some than it easy

collectors stat ements Most express

transducers) in a loop. languages are based

to be expressed contain a for

as localized construct, a range

dispersed

programming loops this that

on enumerating special looping Lisp the

of integers.

languages

go beyond scanners. implement go further Some The

by providing based

forms elements wide

corresponding do list of a list.

to a few additional that makes it easy to A couple looping of languages fragments.

For example, a loop still

Common

provides

a form

on scanning a relatively

by supporting and most The

range support

of st andard for this

of the

oldest

comprehensive Lisp Clisp Loop facility way macro

is in Lisp. on the iterative into Lisp.

Lisp Loop macro.


in the supports syntax. range non-Lisp of the example summation InterLisp it

macro [38]) beyond has looping that

[13] (which introduces to for for been most always

is based two that and

statements First, keyword a wide of its because The vector

concepts uses an by

a looping Second, syntax, the it

construct goes Loop

analogous

Algol-like supporting Because However, wide use. the of

constructs controversial.

of looping

fragments

analogous

to scanners

collectors.

utility below

of its predefine shows a program (The ) The algorithm.

fragments, uses the does Loop not

it has gained macro support the

to implement definition is more are

Loop code

macro produced

new collector-like identical to the the

fragments. code two

by the Loop series

macro

or less

produced approaches

when

optimizable efficient.

expressions

pipelined.

As a result,

are equally

(clefun snm-positive-loop-rnacro (loop for item being each when (plusp item) sum item) ) Loop example). mapped in the like supports A over example). Loop the call the a keyword on the values However, does not definition (which that

(v) vector-element

of

when that Loop macro Loop

is similar can macro also does

to the contain not

transducer fragments

choosing body is not (this other

(see the that shown is

an arbitrary any

computed the

by the

scanner-like

support

transducerfragments, it

looping

fragments. support the definition fragments. of the are quite parts of new collector-like

Although does support

scanner-like shows with the these

However, keyword
cumbersome.

as illustrated vector-element
The user

by the used
has to

following define a list the

example a procedure

definition facilities

in sum-positive-loop-macro), deals Lisp of six parts the macro Loop analogous Common as part
ACM

parsing

of the Loop fragments on the


and

syntax decided grounds


Vol.

and that to adopt that


1:3, No

returns 6. of most

to the parts

of the loop However,


Languages

discussed

in Section

(Recently,

standardization Lisp.

committee

of Common

it was too
1, January 1991.

Transactions

on Programmmg

Systems,

94

Richard C. Waters
they decided not to include the scanner-defining (of)) capabilities. )

complex,

(define-loop-path (defun scm-vector

vector-element

scan-vector

(path-name variable data-type prep-phrases Inclusive? allowed-prepositions data) (declare (ignore path-name data-type incluslve? allowed-prepositions data)) (let ((vector (gensym)) (1 (gensym)) (end (gensym))) (((vector) (,i 0) (,end) (variable)) ((setq ,vector ,(cadar prep-phrases)) (setq ,end (- (length ,vector) l))) (> ,i ,end) (,variable (aref ,vector ,i)) nll (,1 (+ ,i 1))))) difficulty that with with canbe the cannot the in the Loop body macro and isthat thereis that there by the these in a more are no restrictionson to prevent fragments. will
the

A subtle computation from result, dowhat The the

the body As a

no attempt looping

interfering programmers they concept

computation depend to do. and Loop

specified on the fact

fragments [30],

necessarily essentially syntax and

are intended of Generators as the

Gatherers Macro, but

presented with

provides

same

capabilities defining forms.

functional

simpler

Iteratorsin
for the mentscalled are processed including (Alphard one

CLtT. Among
iterators bythe

Algol-like

languages,

some

ofthe

most

powerful scanner-like ofelements define

support fragthat ones. to

use of looping

fragments

is provided forloops

bycLu

[28].

In CLU, a series users can

can beusedin body ofthe for.

to generate anumber and that

CLuprovides a vector, a generator how called

ofpredefined is essentially can

scanners, new identical

corresponding

to scanning

[.50] supports More packages.

a construct recently, ) the example

a CLU iterator. using generic

[10] shows below that

generators how

be supported summation

in Ada algorithm This

As an illustration, can reexpressed is done by writing

shows the

the vector

in CLU.

It also shows

the definition

of a user-defined elements (INT)

scanner.

a coroutine = proc(V:

yields

scanned returns

one at a time.

SUM_POSITIVE_CLU SUM: INT :=


for

ARRAY IINT])

O ITEM: INT m SCAN_ VECTOR(V) do ITEM>O then SUM := SUM+ITEM if

end

end return
end SCAN_ I: END:

(SUM)

SUM_ POSITIVE_CLU VECTOR INT := = iter(V: ARRAY IINT]$LOW(V) IINT]) yields (INT)

ARRAY

INT := ARRAY IINT]$HIGH(V) while I<=END do yield(VII])

I := 1+1 end end SCAN_VECTOR

Taken the Loop

together, macro

CLUiterators except for three

and

the

for

statement besides

are essentially mapping and

the

sameas

things.

Nothing

scarmingis

ACM Transactions on Programming Lan~ages and S~stems,Vol 13, N0. l,,January 1991.

Automatic Transformation
supported. represent (In the example ed in non-local above,

of Series Expressions into Loops


of choosing loop. ) Each defining and summing for can only

95

the operations body method of the for

are both cent ain ines is

ways

in the CLUS the

one it erat or instead significantly A method in [15]. Going to easier for

of many. to use than

iterators

as corout form.

Loop

macros

scanner-defining

supporting this, in

multiple

iterators how

in a CLU for one could both

statement

is described (again present that

beyond only one than

[14] describes each loop). actual Loop

support papers there

collectors merely is no

restricted proposals everything

While

of these

rather

describing by the Lisp

implementations, macro could

doubt

supported

be straightforwardly

supported

in an Algol-like

language. The key difference the between the looping support and constructs looping and structure the ways. from how that means of the simply the only supporting one could they that the anysupport way the One is much that and can map iterative data collectors above are and highly nor the series correeffiidea

Summary.
expressions sponding cient, they to

is that do not loop

while support

looping concept

constructs scanners

fragments

(potentially

unbounded) the

of a sequence This

of t resting constructs, The thing The fragments has more to functional lack other fact

fragments

as procedures. limiting prevents

preserves in several

feel

of the

however,

it is significantly data simple without loop type

of a sequence than that a few the

the (It kind

constructs clear

transducers. having some are tied not

is not

transducers

in general can be used

of object the rather syntax than

can act upon.) constructs. using thing or for

fragments

procedures

is intimately In functional addition,

up with this new For

learn

a new than

language

of combination composition. fragment values

standard

composition.

language inst ante,

of combination

rest ricted with

be done some An avoid allowing preorder limiting restrictions they what

a scanner-like over thing involved the

is to use it in a call scanned. the looping of the be stored constructs restrictions in

on loop

computation interesting getting

to note with data largely

about

above in

is the 2.

way By

they not

a discussion structure ignoring to talk to

Section only off-line

a sequence fragments, the way without

a variable, implicitly

supporting ones), enforce and these

transducers them.

(particularly Unfortunately, 2. This

fragments having

can be combined, about

the constructs

the total

restrictions limits

embody can

are much

stronger

than

the ones in Section

unnecessarily

be expressed.

8. BENEFITS There
start support requiring most are with, for the three series principal perspectives can from which at that to view they series can expressions. To in any language, includes along

expressions without

be looked in such any

as embodying

relatively be included of the support and

complete

sequence language

expressions

a way

programming

removing syntax,

preexisting inefficiency. such

features This Lisp,

use of unusual operations

or causing

of the

provided ones.

by languages

as APL,

Seque

wit h a few An and Series to be

additional

alternate immediate expressions as efficient

perspective feedback that as they

focuses about the violate By

on the efficiency the means

fact

that

programmers series in Section messages, the

are

given they

clear write. are In

of the of error

expressions

do not look.

restrictions for computing

2 are guaranteed programmers they want.

encouraged

to think

of efficient

methods

results

ACM Transactions on Programming Languages and Systems,Vol. 13, No. 1, January 1991.

96

Richard C, Waters

particular, that

unlike

APL,

Lisp,

or

Seque,

programmers efficient. statement constructs to banish at least it loops

are

never

tempted

to

think

all sequence are to series

expressions loops

are equally

A final pressions optimizable Given has the In shown into the series that

perspective

is summarized as structured expressions, are much a step final As the

by the control

that are loops and

optimizable to gotos. most than that it from modify

series By

ex-

using

it is possible easier forward to analyze it should

programs. loops, has this gotos. been them that

expressions context of this

to understand is worthy and

potential

for being

as important of note

as banishing convert a tool

perspective,

[42, 34] that

it is possible

automatically to construct into of using series

expressions. converts allow

a result, loops

be possible ing programs benefits

automatically This when also much instruct rays alyzing might because The less clear. would

in pre-exist

expressions. expressions

programmers old has the in code. not the yet

to obtain

the full

series

maintaining the idea be helpful the ion same word

While sequential

been

explored, applied

optimizable Even of loops series schedule though for to opt imizable by the

series it series

expressions is oriented expressions on very

might toward is very large arof an-

cent ext software were In

of parallelism. pipelining for written the causes series situations in parallel not for most using

machines,

pipelining [27] and

as the

execution processors the

machines

execution

of systolic process

[2, 21]. the be

If programs programs

expressions, for in the Section

to determine addition, also

a good

pipelined

computation relevant,

simplified.

restrictions inefficiency expressions with of the might make

2 appear context.

buffering application The

of elements of optimizable emphasis

in a parallel to non-pipelined opportunities flow series it easier

parallelism for evaluatthem. where This

is is

in such

is on locating no data other make

ing subcomputations appropriate less, using parallelism optimizable exists.

completely but series

between operations. to detect

for mapping,

Neverthesuch

expressions this might

For example,

it easier

to vectorize

[4J programs.

ACKNOWLEDGMENTS The man, that the concept D. of optimizable Y. Tan, series and both research expressions A. Meyer, the concept at the has benefited C. Perdue, anonymous and the Artificial Support Naval and made from C. Rich, reviewers presentation. Intelligence was provided Research MCS-8117633, and in this the contained Laboratory by the the Computer paper of Defense National Techare those expressed the suggestions that of

a number significant This Advanced C-O643, Science and the nology of the

of people. Chapman,

In particular,

D. Wile, made

Y. Feldled to

P. Anagnostopoulos the

suggestions

improvements. improved describes Institute Projects (under The should paper

In addition, done

suggestions

significantly Massachusetts Research Foundation IBM,

of Technology. Agency grants views not and (under NOO014-85-K-0124, Sperry, and

contracts and

NOOO14-75IRI-86166.14),

NOOOl 4-80-( !-050.5, NYNEX, and of these

NOO014-88-K-0487),

MCS-7912179, conclusions

Siemens,

Microelectronics as representing

corporations. author

be interpreted

policies,

or implied,

organizations.

REFERENCES 1. AHO, A , HOPCRAFT,


Addison-Wesley,
ACM Transactions

J , AND ULLMAN, IvIA, 1974.


and

J.

The Design

and Analysis

of Computer

.41gorithms.

Reading,
on Programmmg

Languages

Systems,

Vol

13, No.

1, January

1991

Automatic Transformation
2. AIKEN, 1988). 3. 4. 5. 6. 7. ALLEN, mization ALLEN, A., AND NICOLAU, New York,

of Series Expressions into Loops


In Proceedings

97

A.
1988,

Optimal
308317.

100P parallelization.
Design and

of the SIGPL.4N (Atlanta, GA, .Jlme

88 Conference ACM,

on Programming

Language

Implementation

F., AND COCKE, of Compilers. R.,

J.

A catalogue
K

of optimizing
Prentice 9, 4 (Oct. Hall,

transformations.
New York. 1971.

In Design and Optito vector form. style

R. Rust in, Ed., Lang, and Syst.

AND KENNEDY,

Automatic

translation
1987),

of Fortran
491-542.

programs

ACAI

Tkans. Program.

BACKUS, J. Can programming be liberated from the Von Neuman style? A functional Cbmmun. ACM 21. 8 (Aug. 1978), 613641. and its algebra of programs. BARSTOW) Joint they 1984). D.

Automatic
F. New Rewriting York,

programming
IntelJigenre systems ACM 1984, 6373.

for streams.
(Aug. 1985).

In Proceedings
232237. that and reduce

of the 9th International the number of sequences (Aug.

Conference yield. ACM,

on Artificial

BELLEGARDE,

on FP expressions Symposium on Lisp

In Proceedings

Functional

Programming

8.

BELLEGARDE,

F. Convergent
Workshop introduction M. Broy, effect P.,

term rewriting

systems can be used for program


Objects. In Logic Springer Springer-Verlag, of Programming Verlag, New York, techniques. for lazy 1988. Institute Trans. Not. Softw.

transformation.
New York, 1985. of

In Proceedings
2441. 9. 10. 11 12. 13. 14. 15. BIRD) Discrete BISHOP, Softw. BLOSS, Symbolic BUDD, BURKE, Rep. T. G., R. J Eng. A., An Design. The

on Programs to the Ed., of data 1990), theory

as Data of lists.

and

Calcuh 5-42.

NATO 389-402.

ASI

series, on loop

1986, IEEE

abstraction J.

programming

Trans. Lisp and

16, 4 (April HUDAK,

AND YOUNG,
1988), Loop

Code

optimization New York,

evaluation.

Cornput. An APL

1, 2 (Sept. Compiler. D .July 1980.

147-164. iteration macro. with types. Massachusetts ACM of Technology Program. Lang. 1987),

Springer-Verlag,

AND MOON, R. Efficient Iteration Small-scale

LCS/TM-169, 11. 2 (Feb. J. J. 18-21.

CAMERON, and Syst. ECKART,

high-level 1989), and abstract

iteration data

accumulators. AChJ ACM

194211. SIGPLAN 22, 4 (April Eng. Not.

103110. 16.
17. 18. 19. 20. 21. EMERY, 1979), software D. components. should 44, Nov. SIGSOFT 4, 4 (Oct. of Indiana for COmEnglesequences. systolic Ed.,

FRIEDMAN, Computer GOLDBERG, pllter wood GRISWOLD, Cliffs,

D., AND WISE, Science

CONS Rep.

not evaluate 1975.

its arglunents. University Langnage.

University LabOratOry Prentice-Hall,

Department Rep.

A., AND PAIGE, R.


Research NJ, 1983. J. R.,

Stream

processing. Aug.

Rutgers 1983.

Systems

LCSR-TR-46, The Icon

AND GRISWOLD, . M

Programming

GRISWOLD, ., ANDO BAGY, R


Comput. array. Lang. 13, 1 ( Jan. GROSS, T,, AND SUSSMAN, A. New York, D.

Seque: 13-22. LIapping

A programming a single-assignment and

language

for manipulating onto

1988),

language

the Warp

In Functional L.,

Programming 1987,

Languages 347-363. and

Computer

Architecture. in APL. (Sept.

G. Kahn,

Springer-Verlag, 22. GUIBAS, York, 23. memory 1965, 24.

AND WYATT,

Compilation

delayed

evaluation Languages

In Proceedings 1978). ACM, by time Washington New and DC,

1978 ACh4 Conference 1978. J., LEWIS, HARTMANIS, 3135. Operators.

on the Principles P.,

of Programming

AND STEARNS,R.
IFIP

Classification 65. and

of computations Spartan Syst. Books, 1, 2 (Oct.

requirements.

In Proceedings

Congress Lang. and

IVERSON) K. 1985.

ACAI

Trans.

Program. User Nlanua]

1979), 161176
New York,

25. JENSEN, K., AND WIRTH, N.


26. 27

Pascal

Report. of parallel technique Language

Springer-Verlag, processes. for Design VLIW

KAHN, G.,
1977 IFIP

AND MACQUEEN, D. Coroutines and networks Congress. Nortll-Holland, Amsterdam, 1977. Software SIGPLAN pipehning: 1988). ACM. An effective York, scheduling 1988, 88 Conference New on Programming

In Proceedings
machines. In

LAM,

hl.

Proceedings (Atlanta,

and Implementation

GA,

June

318-328.
1991.

ACM Transactions

on Programming

Languages and Systems, Vol. 13, No, 1, January

98

Richard C, Waters

28. 29

LISROV.

B.,

et. al

CZU

Reference

ilIan,~aJ,

Springer-Verlag.

New

York,

1981.

ORWANT,J. Support of obviously synchronizable series expressions in Pascal. Massachllsetts Instit,,te of Technology Rep. AI/WP-312i Sept. 1988. 30. PERDUE,C , AND WATERS,R. Generators and gatherers. In Common Lisp: the language.
2nd 31. 3z, Ed. C, Steele Jr., Ed., Digital Press, Burlington, MA, evaluation, and Its 1990, part
Usage.

9.56-959 1. .iCM
Prentice-Hall,

PINGALI, K.,

AND ARVIND Efficient demand-driven Lang and Syst 7, 2 (April 1985), 311333. poL~v~A, R., .AND PAKIN, S, APL, The -Language
Cliffs, NJ. 1975. N., PNUELI, A.,

Trana

Program.
Englewood

33.

PRYWES,

AND SHASTRY, insoftware

IJse of a non-procedural clevelopmeIlt. ACMTrans

specification Program

language Lang and

andassociated ,SySt. I, 2 (Oct. 34. RICH,

program 1979),

generator 196217.

C., AND WATERS,R. The Programmers Apprentice. Addison-Wesley, Reading MA, 1!290. 35. RUTH, G , ALTER, S , AND MARTIN, W. A very h@ level lanwaw fOr business data Processing. 36. 37. 38 39. New York, G P. Massachusetts J JR W et. aI. 1986. Common Interlisp Lisp: The Language. Manual. program Digital Xerox Press, Maynard, MA, 1984. In ProceedArchitecture Reference languages, on Functional PARC, 1978. andhstoperators. and Computer Institlite Programming of Technology lt;ith Sets. Rep An LC!S/TR-254, Introduction 1981. To SETL Springer-Verlag, SCHWARTZ, STEELE, WADLER, ings .4CM

TFXTELMAN,

Applicative Conference

transformation,

Programmmg

Lang71ages

(Ott. 1981). WADLER, P compde-time. ACM, New P R R. R. R WADLER, ings WATERS, 1979), WATERS, Rep WATERS,

ACM, New Ycmk, 1981,2.5-32 Listlessn~ss R better than laziness; Lazy evaluation
In Proceedings York, 1984, 4552. is better than laziness loop loop II: Composing Springer-Verlag, programs. notation IEEE Listlessness cm Programs A method LetS. Oct. An ACM L$ymp. on Lisp and Functional

and garbage collection


Programmmg (Aug.

at

1984).

41 42. 43 44

listless New Trans

functions. Ymk, Softrv. Institute 198.5. Eng.

In Proceed5, 3 (Mav

workshop 237-247.

as Data

Objects.

for analyzing expressional loops. (Jan.

Massachusetts 1984 AC!M York, 1984,

of Technology of

AIhl-680a,

1982. In Proceedings 1984). ACM, New of synchronizable Conference 110 In Proceedings ACM SIGPLAN .4CM Not. on the Principles

Expressional Languages Efhclent 1987), Using

Programming 4.5. WATERS, SIGPL.4N 22, 7 (.July 46. lVATERS, ings 47. &3. 49. 50. 51 Computer WATERS, package. Press, WILE, 1983 WULF, Military ment R

interpretation

series expressions. Techmques. instead [Miami,

87 Symposium 7485.

on Interpreters synrhronizable York, 1988,

and Interpretive

obviously Press, New

series expressions Languages part Rep. Language, 338-346. I: A Imers AIM-1082, 2nd Sciences

of loops. FL, Ott.

In Proceed1988). IEEE

1988 Iuterna Society R.

tiomd

Conference

on Computer

Optimization

of series expressions: Institute Common 1990, of Technology Lisp: USC The

manual Dec

for the series macro 1989 Jr., Ed., Digital

Massachusetts

WATERSR
D.

Series. In
MA, Generator

Ed.

G. Steele Rep.

Burlington,

9239.55. Information Inst,tllte ISI/RR-83-116,

expressions.

TV., LONDON, Standard Printing

R , AND SHAW-) M IEEE Feb. Trans. 1983. Programmmg

An introduction 2, -i (Dee.

to the construction
1976), 253265. 181.5A-1983,

and verdication
C7.S. Govern-

of Alp hard programs.


Ada Office,

Soft w. Eng. Language.

ANSI/MIL-STD-

Received

December

1987: revised

November

1989 and

May

1990;

accepted

July

1990

ACM

TransactIons

on Programming

Languages

and

Systems,

Vol.

13, N.

1, January

1991

Você também pode gostar