PRG40
PRG40
John Hughes
July 1983
(published as a monograph September 19&4)
~
i
0
~ l?":"4'l? 0
0> Q' ~ .... 'A""
... = ... ~
..Q.i"lSQ. ~
"ClET ...
l}SC: 0
~e· ~"" S· e. go
S ~ - ~IQ ~
'00
~S ~.
.0 ~ '" il. f.
~
...Eo _. c ~~
... "8.
~
:z:
co 0
~IT'"
c:~
S-~ "
O'"
..
IT
II
o~ "=-.
e. G
.. CO
II Q. ..
"'" "t"'
!:oI:
... ~
o II .g.
pi
"'0 ;;
S .... ~
... ...
0
~
1>
AbAtr.CI
••• mples.
grltelul to Peler Henderson and Tony Hoare: aach 01 them has Influenced
my work proroundly. Chris Dollln IIrst Inleresled me In luncllonal language~.
end Devld WI,e and Jack Dennis between Ihem Inleresled me In reference
counting garbege collection. Geralnl Jone, listened patiently 10 many 01 my
IdlU belore they were even half-baked. and I have had many Inleresllng
Ind enloyeble discussIons with Simon Jones and David Turner. Meny thanks
to them ell. Finally. I am very grateful 10 my wile. Mary. end my parents
lor Ihelr conslent moral support. and the Science end Engineering Research
Council 01 Gre.t Brllaln for their IInenclal support.
THE DESIGN AND IMPLEMENTATION OF PROGRAMMING LANGUAOES
1. InlrOduCllOn
2 FunCllonal Languages 3
2 , InlroduCllon 3
22 Functlonal Fealuro5 e
2.3 Formal Spectllca!ion
•
3 ImplemenlalJonS '0
3.1 Inlroduction '0
3.2 The ~-calculus 10
3.3
3e
Represenlalion
3.
3 .•
CAF reduction
e Super-comblnators 23
el IntrOducllon
An ImperaUwe compiler
A funcllonal complier
..
.0
5.'
5.5 A logic compiler
"
55
5.6 Conclusion 59
6 Analysis 01 efficiency 61
6.1 Introduction 61
,. Ev.luelion order 75
,..
1. , InlroduCllon 75
Lazy evaluallon 17
1.3 The need lor parallelism 6.
1.' PAR tor more parallelism 66
1.5 S't'NCH lor less parallelism 66
16 Qulcksorl 91
11 Pipes 9.
1.6 Dls.avenleges 96
1.• Conclusion 96
8. Garbage COll8cUon 99
81 Introduction 99
8.2 Garbage collection sirategtes 100
8.3 CIrcularity 101
8.' 'he sHIUe program graph 103
References
CHAPTER 1
INTRODUCTION
The Ulle 01 this the!lIs. "The Design and Implemenlatlon 0' Programming
languages". was chosen before any of Ihe work rep0rled was envisaged
to saUsty University regulations. Nevertheless. /I Is an BpI. If Cheeky.
descrIption of rne conlents. II 15 Cheeky beceuse we arB actually concerned
only wllh functional programming languages. II 15 apt because our first advice
10 a language designer would be to make his language functional. We b&gln
In chapler 2 by JusUfylng lhls advice.
longer) and we bellevL! that Ihls experience has given us e gOOd gen&ral
underslandlng 0' the principles Involved. In Chapter 3 we expleln our
viewpOint and Introduce some terminOlogy lor use laler.
melhod. and much of this lhests 15 concerned with our own Improvement
on II using ·super-comblnalors". Our melhod Is explained Bnd analysed In
tne lollowlng three chapters. First. In chapter 4. we explain the principles
of our method and a number of enhancements 10 It. Here we are only
concerned with determining whel code a program should be complied InlO.
2
unreasonably large amounts of storage. and thai even qulle simple programs
can gradually clog the memory with useless garbage. We argue In chapter
7 Ih81 there 8,e deep-seated reasons 'or Ihls behaviOur. and Ih81 It can
Ihat there are compelling reasons tor usIng relerence counllng garbage
collection. bul hlther10 1hls has been awkward because r.r.rence counting
garbage collectors had dllflcully wllh circular structures. We propose an
alructure.
This completes an oyerylew 01 our thesl5. We wish 10 nole one other polnl.
The reader will find Ihat the words "theorem· and ·prool" occur yery rarely.
demonSlralion. We hope thai lhe reader will gain " better underslandlng
'rom our Informal elilplanalions and "prOOfs by elemple" Ihan he would haye
FUNCTIONAL LANGUAOES
10 examine this pOint In mont detatl. We shall try 10 answer the quesHon
in lis early days, and yel II was often dell ned as 'programmlng withoullhe
InclusIon 01 new slruclured programming construels such lIS Ihe while loop
and 1l-1hen-el:se In new languages like Pascal, One can omll gotos In
FORTRAN IV unlll one Is blue In the face withoul makIng programming any
easier.
•
In Ih. same way tile negaUve dellnllion falls IO'IIIly to cepture the splrll
118181 thai Ihls weakens our ClUe at all. In ract 11 slrengthens II. becausa
.ach or Ih.s. languag.s Is nlc. pr.cls.ly b.cause It Includes sam. Of the
'••Iur.s we ar. applaUding. None of Ih.m Includ.s all the Important f.alur.s
we ,"II d.scrlbe, howe.... r.
• nd lI.scrlba our ... I.w of how programs could b. d.....lop.d In the fUlur•.
We h..... said Ihat wa b.lla ...e functional languag.s m.lI. progremmlng •• sle,
DeCau.. th.y Includa naw, powerful featur.,. But what ,.aclly are lh.se
".Iur.,? In Ihls s.cllon w. Id.nllfy thO•• we f•• 1 .r. the rnosl Important.
5
Onlt reason why the functional programmer does less work 15 thai the
functional syslem does more work. The system essumes responslblllly In 8
number 01 areas. Ireelng the programmer Irom hevlng 10 Ihlnk aboul them
lit all. One Importsnt area Is srorege allocetlon. The functional programmer
ere no references to them lel1. This avoids the risk 01 bugs caused by
delellng objects too early. or excessive storage use caused by delellng them
100 late (e "dumb" garbage collector or bad vlrluel machine design can
stili cause oblc-cl, 10 be deleted 100 lale). The mosi Imporlsnt advantage.
however. Is thai the programmer does not need 10 think aboul It.
In the program. but In funcllonal languages the system Is free 10 use any
sullable order; Ihis gives rise 10 Ihe possJbllily 01 several different slrategles
tor delermlnlng order. InCluding lazy evaluation. This Is even more Imporlent
Oreal s/mplllications can result: lor example. problems lhat are usually solved
functions thai return a IIsl 01 aU solullons. II only the IIrSI solullon In the
tlsl Is used. Ihen only that solullon will be compuled: on the olher hand.
II Ihat solullon proves unsatisfactory and Ihe nexl solution Is used Inslead
lhen Ihe syslem will pick up where II lell olf IIhel Is. we can gel backlracilng
be solved most naturally by programs whose struclure does not rellecl the
outputs. and the system chooses an execution order In whIch the outpuls
eetlly for exemple. funcllonal operallng systems will nol need specie' ·plpes·
In order lor one program 10 read another', output
extend our language with whalever control structures we desire. We can use
For e:remple. the well known map funcllon 15 en approprlale conlrol structure
essence this Is lusl another way of writing certain condilionais. lis advantages
are thai equallona are usually mora readable than conCIIlIonals. en(l that
It allows quite complex conditions to be expressed very simply. ThIs Is
programs.
but WhiCh Ills e~slly Inlo theIr conCeptual framework. Is the provIsIon 01
ebstracUon function that stamps objects with a type. Its Inverse. and a type
testing lunctlon, lhls simple extension allows the programmer to make hIs
Inlenllon far clearer. and allows the system to catch lar more erron. lWtlh
the conceplual difforence between two objects with the same represenl~lIon
Dy stamping them with dillersni types. and so the system Is able to clleck
that the usage 0' these objects Is consislent with Ihe programmer's Intenl/on.
however. II the fact thlt the various Illnguage elements cen be combined
end Ir••dom Illows one 10 become conversant with the language qUiCkly.
Itt, ",ry compelling r•• son lor I.vourlng functlonsl programming Is thaI II
fils ""y wen with forme' program development We shell Illustrets Ihls tty
d.scrlblng how programs are developed formally 'It Oxford lind ellpl81nlng
Why IIllng luncUonal programming I.ngueges can simplify lhe process greauJ'.
Thll conltlta 0' a number 0' deflnlUons 01 types end runctlons on them.
con,lruCtively by
.qua:re: N ..... N
N _ N
"". square
-,
Having written the lor mal speclllcallon. the programmer lranslorms II InlO
Junellans and proving the new deflnltions equIvalent 10 the old ones. AI the
end of thls slage. the specilicallon not only dellnas wnat Is 10 be computed.
and Is nol proved Corree!. Indeed. the resulting program usually lalls to
mael 1he speClflcallon because It has tmpiemenl8Uon limits built Inlo II.
need 10 go any furlher. Slopping al this stage nOI only ellmlnales Ihe InlDrmal
CHAPTER 3
tWPLEMEHTAllONS
3.1. INTRODUCTION
In this chapter ..e discuss the main Implementallon techniques used lor
Into (he l.-calculus pius constants. we lake the l.-calculus as the canonical
luncllonal lenguage. Aller 8 descrIption of reduction, which 15 an 8)(ecutlon
l-reducer. •nd show how different anempts to cur. It can lead to the SEeD
machine end Turne", combinalor,.
B ,,- C I V I (B Bl I 'V.B
wllere C r.nges over • sel or const.nts .nd Y r.nges avel'" • ,el 01 V81'"18ble
11
functions such as Cons. ). Y.E is said 10 bind the variable V Inside the body.
EJ. red El. which means Ihlll wherever an 8llpresslon of Ihe form El
appears. It may be replaced by E J. Each basic 'unellan has lis own ruill.
lor ell8mple
+ m n red m+n
provldad thai m and n life natural numbers. (Here we adopl Ihe convention
that application is leU associative and drop unnecessary brackets), The elleel
whare the right hand aide stands for E with all free occurrences of II
replaced by Eo.
However. the p-rule Is onty applicable when Eo has no free variables. This
Is more restrIctive than Ihe p-rule I!lS usually stated. but we C1on·t care
program must have arguments with no free variables either. and so the
by lhl' Is thai we ne-.'ef need to Invoke the expensive a-rule (which renames
yallables to avollS Inadvertent blncllngl.
I, then said 10 be In normal form. and this norma' form Is the result of
the compulsUon.
In normll form. and by Ihe tact that reductions 0' the top-level of the
10 lake place, This gives "lazy evaluaUon", This approaCh can run
rules 01 Ihe form E 1 Irans E:z, whiCh when applied repeetedly will translate
• p"'og ...am Inl0 the equivalent lo-expfesslon. A complete set 0' translation
rules would be too long-winded 10 reproduce here. but we give " lew
examples:
"here IF Is dellned by
uSing Ihe basic funcllon Y which we have not seen belore. The reduction
rule lOr V Is
Y fred f (I f)
which uses Y I again on the right hand sIde. This means Ihat Y I has no
W. hope Ihal these Bll8mptes h....e convinced Ihe feader that translallon
1.3. REPRESENTATION
Ihls reason 51rlng redvc!lon 15 really only sult/lble 'or strict languages. which
Atrlng reduction machine. lor example. uses FFP ....hlch Is Indeed e strict
application much more ellicienl. Not only this. II also means thai there Is
only ever one copy 0' the argument The IIrSI lime II Is reduced It will be
replaced by Its normal lorm. and all polntars to lhe argument will now
aUlometlcally refer 10 lis normal form. ThIs means that no melter when l~e
ar~ument Is flrsl reduced. II will not be reduced more then once. Ora,h
y t red t (Y !)
On II graph reductlon machine •• can lake adventage of the 'acl Ihel III
copy of Ihe left hanes aide appears on the rlghl by cons.ructlng II circular
result
y t red i.J
n II clear Ihal Ihls ,eduellan rule Is equivalent 10 the 0(.0In8' one. but Is
destructive - lor It It 'Iller. then the funcUon WOuld be corrupted and could
apply a 'unCllon. we must copy lhe function bOdY. This Is the lunda mental
Inelllclency of graph-reduCtion: Ihe teChniques we shall discuss In the resl
01 Ihls chapter seek 10 ellmlnale or reduce II.
lalblc. E
17
When this funclton Is applied. E would have 10 be copied three time!. Mea
In 11. Since there are often many variable, In scope 8t • lime thle Is
prohIbitively expensive.
One 01 the anampls 10 8void Ihls Is the SEeD machIne ILandln64). The
fundamental Idea is 10 delay subsUtuUons until the very lasl minute. so Ihat
copyIng the expreSSion only once. To do this we need Iwo new Clala types:
C (p) red C
V (pI red p(V)
11 It clear from this descrlpllon Ihat lhe SEeD machine Is stili a graph
support for non-slrict languages. and for the same reason: the environments
once.
However. real SEeD maChines use Jour reglslers. Ihe steck. environment.
Uhls I, Ihe reason for lhe name). ThIs Optlmlsallon. coupled wllh the more
.11.
Ttl. sEeD machine does Ind8sd ....old doing any subslltullons. bul It replaces
lhem wllh enylronment lookups which elln proys very 8xIJenslye when there
.r. m.ny names In the environment. Turner found thet his SEeD
CTurner791. This hIlS led to other 811empts to reduce the cosl 01 subsl/1utlon.
E ::. C I (E E)
C : : - X I 1 .•. h.B
B ::. C I I I (B B)
expressions thai can be subslUuted Inlo are combinalor bodies. end the
resutl of !lubsUlullon Is nOI a combinator body. So we have eliminated the
We may \rBnslate the ),-calculus Inlo cals as follows: If the tree variables
Repeated appllcsllons 01 this rule will eliminate all the ),-expresslons In Ihe
program. and II Is clear 11"/81 Iha resuillng comblnators will hav& no free
II has long been known that ),-expresslons can be lranslafed Inlo cafs which
S - Kabc. a c (b c)
K - Kab. a
1 - Ka. a
20
lV.V tran. I
lV.VI trane K VI
lV.E! E2 trans S (lV.EI) (lV.E41)
Howe....'. applying these ',anslallon rules to .lin expression of eny size yields
an enormously large result. Turner observed lTurner191 thai using 8n
optlml.atlon rule
S (K a) (K b) trane K (a b)
dr••llceUy reduces the size of the result. He used this rule. together with
He round thai the use of his opumlS8uon rule had another Interesllng
lX. + 1 2
The addition Is performed on every call, since the function body Is COpl,,<l
during applicatIon.
). x. ... 1 2 tr ana K (+ 1 2)
and
X (+ 1 2) a red + 1 2 red 3
$0. aller the IIrst call the lunctlon Is slmpty (K 3). end II returns 3 wllhou,
performing any addl1lons at all.
22
constant a_pression, are only eVl!lluated once. II, effect 18 mor. far-reaching
tt1ln thhl. though. because It Ireals any e:olpre"lon Insloe l!l lunellan thai
-'olll!l nOI Involve the bOund variable In the ume way. It gU8ranl88S that
any !Iuch expression Is evaluated only once. and thl!lt II, value will be used
direCtly lherealter. Thus II subsumes such optlmlsallons as constant fOlding
WII call this properly fully lazy 8v8/uation. 11 I, IInalogou5 to IlIzy evalul!ltlon,
but I, more general ,Ince the latler slales only thai e..,ery argument of a.
CHAPTER 4
SUPER-COM8INATORS
4.1. INTRODUCTION
8 more complex complier and breaks the execution down Inlo very sm811
steps: an applies lion of S. fOr 8Jl8mple. does nOI achleY8 very much
compared to an appllcallon 01 some larger comblnators. Howeller. It brings
In this chapter we shall develOp II methOd that combines the besl fealurtlS
will accomplish much. The basic method will be IntrOduced In secllon .,2.
Subsequent sections present various Improvements that can bEl made. and
the Iinal section presents the results of an experimental comparison of' our
lV.E tl'an.
{ldl. .. InV.E[ll/El, ...• In/En) El ... En
tha comblnetor we ha"a produced cannot ha"e any tree YBrlables. because
.ny frea ,,8rleble 15 8 tree expression. and would theretore be enelosed
We have yet 10 show how ltlls method gives fUlly tliZY 8Yelullllon, We shall
-X(+12)
8XIlc::t1y the same code as prOduced by Turner's approach. When this funcllon
ere performed.
To lake a more realistic 8ll:ample. we consider the lunctlon Ihel selecls Ihe
81 II YP.e!lnlS.
IF (- n 1) (hd 0) (01 (- n 1) (tlo»)
This function can be parllelly paramelerlsed. liS In (el 2>. to give II 'unellan
In Ihls cllse would mil en lhet such II pllrlUllly parameterised function would
looking 1II1 Ihe Innermost ),-ell:pression first. we see thllt II hilS mllll:lmlli Iree
• (IF (- n 1) (81 (- n 1»
When we apply the same process 10 the other ).-expresslons too. we end
up with
el II Y .,
., IQ lCe1. IJ el
IJ '" Kel n. a (IF ( ... n 1») (el (- n 1»)
a a lea b 8. a (hd 8) (b (tl e»
Now. wllan /he partially pBramelerl:\oed function (01 2) 1& IIrst applied 10 a
el 2 red Y .,. 2
red.,e12
red IJ e1 2
red a (IF (- 2 1) ) (el (- 2 1)
All 01 these (eClucUons will be performed on the ItrSI call, trenslormlng (el
Noltee Ihal we did noc oother to abstract out constant free expressions In
expressions. but wllh the understanding lhal the lutl laziness property stili
One faclor Which might Influence a choice 01 parameter order Is Ihe eftslre
a appear. then II can choose the Olher oreJer, making ,SQlCab.aab which
Is equlvalenl to a Thus a correCI chotC& 01 parameter order can help make
more comblnators redundanl.
more Widely. ThIs will reduc& the total number 01 combinator appllCUlons
2.
are redundant
Anotller leclor Influencing choice 0' parameter order Is Ihe desire 10 make
~-e.pre55Ion combine InlO one larger one. then we have Improved the
elliciency 01 the enclosing combinator. This Is analogous to trying 10 move
whiCh hEls mfes (0 (hd 5» and (II s) This )..-eJilpresslon will be translaled
1010
The laller form Is more elficlen!. bolh because Ihe combinator has lewer
because the large ell:presslon (a <hd s) (II sl) need only be conslructed
once and can then be shared between all calls 01 lhe function.
We derive a general rula Irom this example. The combinator a Is derived
'rom a k-ellpruslon with mfes (hd s), (II 15) and (i- n n. We have se,"
lhat. when choosing a paramaler order lor a. those mles which are also
free e_presslons 01 the nelll enclosing k-ellpresslon should ap~ar before
those which are not. Suppose a has paramelers E 1 En, so that the call
of a will appear as
aEl •• _En
There should be some J such that. lor all I less than or equal 10 J. EI Is
a Iree allpresslon 01 the ned enclosIng k-axpresslon. and fOr all k grealer
than J. Ek Is not. This guarantees that (a E1 E~ Is a free expression
0' tha nellt enclosing k-ell'presslon.
Now. c~nslder the k-ellpresslon enclosing that. To maximise the size of tis
mfes In Ihe same manner, all the EI which are free In II should appear
before the Ek which are not, and so on and so forth, In general. the optimal
Ordering under this criterion can be established as follows. Every El Is a
because ellpress10ns wllh the same natl'i8 ),-ellpresslon can appear In any
order. This doesn't maner. because any ordering satisfying our condition
Is as optimal as any olher.
because they ara free In alt A-expressions. For the 58ke of uniformity 1hey
.re regarded 8S being native 10 some nollona' )..-ellpresslon enclosing the
whole program.
6 Is defined by
6 • kpq~8. a ...•...
No parameters are redundanl unt.u s Is. but is was the bound variable
coma al the end of Its parameter IIsl. It Ihere Is only one such parameter.
a £1 ••. Eft e
where s does nOI occur In Ella En. Therefore each Ej Is frae In 11. and
hence so Is (a El ... En). If n Is non-zero. Ihen 11 would actually be defined
by
11 • kpe. p e
31
~.kB.«a
Fn III k I 1 . . . l. In. G I n . . . I 1
The malllmal free 8Kprllsslons 0' the Innermost k-9xpresslon are 11 ... In-"
so. II we do not choose the opllmal order. _e could translate
Un. G In ... 11
Inlo
an In-I ... 11
, ... til
Here Cll Is of size om. 8nd so we will Ir8nslale the ),-e:lCpresslon of size
'oreed to define
glvlOQ
All the olh8r 1Il -..111 be rsOundenl. 80 we will fInally define f'"l"I_an. The code
will be 01 size Ofn). The tremendous Improvement In this example leUs
U!I to expect 8 slgnUlclinJ Improvement In practice. In chapter e we will show
When the method described above Is used In prac11ce .a find thaI. on the
whole, II performs well, but In some clrcumstanca5 performs WOrse lhan
'unction
33
t lb II Y a
II • I(f n. If « n 2) n
(+ (f (- n 1» (f (- n 2»)
f 1b 1 red a t ib 1
red IF « 1 2) 1
(+ (fib ( 1 1» (fib (- 1 2»)
reel tF t.rue 1
(0 (fib ( 1 1» (fib (- 12»)
red 1
end we see tlla! the large expression (+ (fib (- 1 1)) (lib (- 1 2))) Is
I, likely 10 oU1weigh lhe rest 01 the cost 01 computln~ <lib ll. Since Ihe
expreSSion. In the expresa10n (IF fa E:a EI). 11 Is certain that only one
of E:3 and E:II will actually be required. end so to construci both Is wasteful.
,.
El El Ell IS O.X. IF EJ Hh.E1J xl (().. X.El) x)). Since bOth 'uncllons ).x.El
and h.E3 Bre Independent of x. the only expressions that need 10 be
constructed on eech call arB the applications 01 these functions 10 x.
IF EJ £:1 £1
IF EJ «l.X.El)ll) «l.z..EJ)x)
In this case h.E1 and h.El will be free expressions of the enClosln(l
IP El (a ll) (b K)
wnere a and b are parameter nemes corresponding 10 E]: and EJ. Only
two cetls will be allocaled towards E:z and EJ when x Is bound. Aller an
alternalNe hes been chosen Ihe selected brench will be construcled and
e~.lueled. or course. II one or both Of El: and EJ doe, not In~ol~e x lhen
.lIocated per recursion for n greater than 1 haa only Increased from 13
to 104. This rapresents a greal Improvement In eHlclency.
program Into th" l. -calculus and prOduces comblnators trOth thaI. This Illds
to sO'"e Inefflclency In the treatment of declarations: for example .... :I '"
1 In 1+2 Is lass efficIent than 1+2. because the former Is translaled Into
graphs. For example. let x:::1+2 In cons l( Jl can be Interpreted as the graph
(cone tP
~<. 1 2)
c<con. 1 r
Since 11'8 ere already worlling wllh graph ,educllon, the Inlroducllon of
e.amples 11'8 began with actually represent the same graph. and so ar.
We now have comblnators with general graphs as bodle,. rather than trees.
y • ~l. letrec x - t x in ~
since (y n I, reduced 10
l
d
In order to perform super-comblnalor abstraction on 8 program con1alnlng
provIded x does not occur In E 1. This equallon holds because both sides
represent the same graph. and we are simply Choosing the mosl convenient
requires some care In Ihe case of tetrec deciarallons). The result will be
1n f lb
we del"lve
80th comblnalors .re simpler and more efficient Ihen Ihelr earlier
counterparts. Tile Improvement In efficiency Is perllcularly great In the elISe
We will not pur!5ue this parllcular extension any further. A5 we have remarked
gener.' cese.
Ten ,mall programs were wr!lIen and benchmerked, rangIng from Ackerman',
Junellon 10 a unillcailon algorithm. and the resull, are summarised In the
table below.
Program Size ACode AReductione ACelle ATime
SiZe Claimed
1 26 36 -13 39 o
2 36 -10 -48 46 o
3 49 o -42 10 -12
4 H -5 -47 32 o
5 75 9 -36 9 -3
6 93 -9 -42 -16 o
7 106 -9 -23 -19 -21
8 115 -13 -30 -8 -11
9 307 -7 -39 21 -17
10 317 -31 -60 -35 -45
be borne In mind thai all the exemple were very smell. and that the
.dyanleges 0' super-comblnators should become more pronounced 'or larger
we look down the co'umns. The anomalies In the ·cells claimed" column
are prObably due to the fact Ihal we did nol use Ihe teChnique of secHon
<4.<4. which affects particularly programs ~. <4 and 9. We are heartened lhat
CHAPTER 5
SUPER-COMBINATOR COMPILERS
5.1. INTRODUCTION
4.2 tnd ".3. The optlmlsaUon 01 $ecllon ..... Is aaslly added. and the
'lIrt11 need to work with the netlve ,l-axpresslons of parts 0' the program.
enclosing a particular program polnl lorm II sequence. and '0 we mey reler
01 zero, and we deal wlltt Identifiers using an environment which maps lhem
them).
application using the rules above. This Is the approach we will take.
scanned. lis lelllclIl level Is delermlned, and II Is converled Into the equlvalenl
42
combinator 'orm. When the scan termlnatss, the whole progr.m will helve
).-811'pre'llon. Fortunately this cen be done using only the level numbers.
would ba Iree In ell Ihe seme }. -expressions and n could not be mallimat
'ree In any. We can even Identify the }.-expresslon It will be mexlma' Iree
In; II ha. 10 be one In which It Is free. bUI In which the enclosing expression
body of a }. -expression and lis lexical le...el 15 less than the le...el of Ihe
0' the }.-expresslon It Is the body of In the Ilrsl case. and Ihe native
For the most part. this algorUhm Is lalrly Simple. However, It Is quIte triCky
10 replace mfes by argument names. as we gaily said In the last pllregr~ph.
tre. Into the combinator version there Is provision 'or storing 8 level number
name.
45
1Ivt&
The complier malnlslns ....... Imporlant global yarlables, whose declaraUons
.re:
holds the nestIng depth of the nee rest enclosing ).-ellpresslon. mfes Is used
to alore maxlmsl frea expressions .15 lhey are found. II Is 8n array with
one element per ).-ellpresslon enclosing (he current node. The element holds
8 sequence 01 all the mles 01 that ).-ellpresslon found so far. In 'acl. II
Is the addresses 01 the mIss which are held In the sequence; this permits
address n.cpt)
f1
ocl
eUt lelambda (n) then
el ,- el + 1; mC . . [elJ ,- 0; b-ts(c.l]:~<)i
compile (n.body, bind (n.bvar, cl, e»;
aortmfes (mfe. [ell);
..1
n :- SUPER (length (mces [elk n.body);
tor: 1 in 1. . length (mfee [ell) do
n :- APPLY (n, derl!Jt mfes (el] (1]);
n.levsl :- n.arg.level;
it n.tn.level < n.level then
augment (mfes (n.level], address n. tn)
t1:
dare! mles [ell [1] :- "-HG (1)
Noles: n Is 8 ver parameter 50 Ihal the complier cen overwrite It witt! the
which returns lin envIronment equal 10 the given one except thet the glv,,"
variable Is mappet' 10 'he given Inleger. and lookUp which returns the level
relld or updated vie lis a"Or8n. sorlmles sorlS II sequence 01 mfs addresses
The essential feature of thIs complier Is the way Ihal II stores mfe addresses
so that II can both letch the mle to construct the replacement applle8tlve
As In the lesl section. we shall begin by describing Ihe types we use. There
Ire several types In the functional complier. since we cennot use assignment
10 slore ell the Intormallon In the same nOde. We use • synt•• Ilk. Ihal
The recursive compile luncllon Itsel' mu,' h.8ve ,everal resul1s. since we
expecl II 10 return lhe level of en expre55lon. find Its mfes. and return a
ver.lon of the e-presslon wllh ell mles replaced by AR!3(n} nodes. We
Slnct we expect lhe complier 10 replace mfes Dy AAGCn} nodes. end since
Ihe n.mes to be used depend on lhe context In which the expression being
complied occurs. we must pass Ihe complier an argumen! givIng the names
10 use tor e.Ch mle found. We use the NAME type to pBSS the neme of
.n mle. and ttle names of .11 I,s suD-mfes logelher.
10 be used as a replacement.
compile: ENV-toNAME-LIST-.NODE-toEXPR
Since Ihe NAME-LIST gives the nemes 01 the mfes found. It will always have
APPLY(f.a) -+
LAMBDA (v , e) -. ...
esac
Compiling variables and conslanis Is easy: the relevant paris of compile afe
Appllcallons are harder 10 deal wllh since the complier must decide whether
either the function or the argument Is an mIa, 8nd If &0 replace 11 ~y lis
compUe Is also tricky. We shalt first show the necessary program and lhen
BlIplsln 11.
50
APPLY(f,a) -+
letrlltc EXPR(flev,fmfllts,fnew) ..
compile env fnames f
and EXPR(alev,amfee,anew) ..
compile env anames a
and result, fnamee, anames
flev-alev -+
(EXPR(flev,fmfes++amfes,
APPLY(fnew,anew»,
,
take(£fmfee)namee,
drop(~fee)names) ;
let HAME(num,eubnamee):names' - names
In
f lev<alev -+
(EXPR(alev,
EXPR(flev,fmfes,fnew)~amfee,
APPLY(ARG(num),anew»,
eubnamee, names');
e lev)alev -+
(EXPR (f lev,
EXPR( alev, ameee, anew) : fmL ee,
APPLY(fnew,ARC(num»,
namee'r eubnamee)
in result
51
append. : Is cons. f: Is length 01 l!l Ust. and lake and drop return the first
n elements and all bul the Ilrsl n elements of • list respecUvely.
Notice that compile computes !names end enames from the results of the
recursive cells. even though they are themselves parameters 10 lhose celts.
11 guarantees thai II the original call had 8 names pare meier Isomorphic
to US EXPA-UST resull. then Ihe recursive calls will have 100. This circular
easy lor funeUans wrillen In this way to 1.11 10 termlnale. In the case or
compl/e. one can argue that all level numben are obviously defined. end
so all mles are 100. Finally. this Implies Ihal all names and new ellpresslons
LAMBDA(v,e) -+
I_tree vlev. env' - bind v vnum env
and em!ee. enew
I.tree EXPR(elev' ,emtes' renew')
compile env' enames' e
and enamee'
vlev-elev' -+enames; l!lubnamee
and INAME(num,eUbnames)] - enames
In v!ev-elev' -+
em!ee', enew';
[EXPR(e!ev I. emfee' , ene",'}],
ARG(num)
and orderedmtes, permutation
let II
sort (~(7.EXPR(alevr7.1»
~ (7 .EXPR(blev, 7.7».
alev)blev)
(zip (1. .£emfee] emfee)
1n map (~( 1 • e) . e) II, map ( .. ( 1 , e) . 1) l!!I
and enamea
a.p O.(l,n).n)
Flnt, we add Ihe bound variable 10 Ihe envirOnment using bind. which we
assume also assigns and returns Ihe new level number. Then we compile
the body 01 the l,-expresslon IlInd take account 01 Ihe lacl Ihal Ihe whole
bOdy might be an mre (If vlev Is greater Ihan eI8\1'). Nollee thai the
declaration of num and subnames can only be executed when enames has
to be executed.
We go on 10 sort Ihe mles found Inio optimal order, and record Ihe
orIginal order laler. The question mark used 8S 8 variable name mBans Ihal
the variable Is nOI aClUally requIred. sort lakes a parUal order and a Itst
and SOrts Ihe IIsl Inlo Increasing order. zip lakes IwO lists and returns the
We can then use the subSidIary funcllon mkap. which lakes a comblnalOr
and a list 01 arguments lin reverse order) end conslructs the application
of the combine tor to the argumenls. It also needs to be passed the names
ergument number
Finally. we can construct Ihe super-comblnelor itself and sorl the names
(EXPR(alev,
amfes++fmfes,
APPLY(fnew, anew»,
drop (£amfes) rnames,
take (£amfes) rnames);
Clev<alev ~
(EXPR(alev,
EXPR(flev,fmfes,fnew):amfes,
APPLY(ARG(num) , anew»,
subnames,
rnames' )
1n (r,
NAME (nextarg. anames) : anamee' I
nextarg'H)
5.5. A 1OO1C COMPILER
compi1e(env ,node,expr,namee)
compile Is dellned by lour clauses. lhe Ilrsl lwo 01 which compile variables
and conSlsnls. They are:
We have assumed Ihal lOOkup finds lhe level and corresponding argument
number 01 a variable Irom the envlronmenl. and that Ihe underline Is an
anonymous variable.
comp11e(env,APPLY(f,a),expr,namee)
compile(env,f,fexpr,fnamee),
comp11e(env,a,lIexpr,lInameB),
apply(fexpr,aexpr,fnameB,anames,expr,names).
56
The luncllon and argument are compiled firs!. and then apply Is used to
combine the results. II the luncUon and argumenl have Ihe same level. Ihen
apply(EXPR(l,fmfes,fnew),EXPR(l,amfeB.anew),
(names,anames,
EXPR(l,mfeB,APPLY(fnew,anew» ,names)
append(fmfes,amfes.mfes),
append(fnamee,anames,names) .
Otherwise Ihe one with Ihe lowest level Is made Into an mle
apply(EXPR(fl,fmfes,fnew),EXPR(al,amfes,anew).
(names I an ames r
EXPR(al,EXPR(fl,fmfes,fnew).8mfes,
APPLY(ARG(n) ,anew»,
NAME(n,[names).anames) :- [Hal.
APPLY(fnew,ARG(n»)J.
NAME(n, anames). fnames) : - ! l>al.
),-ellpresslons are complied by the clause:
comp11e(env,LAHBDA(v,el,expr,namee)
b1nd(env,v,nextarg,env',lev),
compl1e(env',e,eexp',enarnee'),
lambody(lev,eexp',enames',
EXPR(elev,emfes,enew},enamee),
Bortmfes(emfee,enames,Bortedmfee,eortedname3),
mkap(EXPR(O,[],SUPER(nextarg,enew»,
aortedmfes,Bortednamee,
expr,namee,nextarg).
level number lev, 85 In the functional V8rslon. Then we compile the body.
and take account 01 (he lacl that II might lorm 8n mfe by IbeIF (In lambody).
aortmfes sorts the mres and names Into optimal order, and finally mkap
lambody Is dallned by
lambody(lev,EXPR(lev,mfes,new),namee,
EXPR(lev,mfea,new),namee).
lambody(lev,EXPR(l,mfes,new),namea,
EXPR(lev,(EXPR(l,mfee,new)],ARG(n)),
[NAME(n,namea))) ;- l<lev.
which Just makes the bOdy 01 the ),-expresslon Into an mle II It doesn't
con'aln the bound variable.
58
mkllp applies lhe combinator 10 Us arguments and assigns Ihelr names. Its
form Is
mkap(comb,mfee,namee,expr,enames,nextarg)
Ihe names 10 be essig ned 10 those argumenls. expr the resulting expression.
en ames the names at mIss of that expression. and nexlarg Ihe next Iree
argument number. II Is defined by the clauses:
altap ( f • r)• I ) • f • r 1• 1) •
akap(f,arg.mfee,NAME(nextarg',anames) .names,
e,enames,nextarg)
mkap(f,mfes,names,e',enames',nextarg'),
apply(e',arg,enames',anamee,e,enames),
nextarg 19 nextarg'+l.
We have made greal use of the Prolog variable In this program to dls1rlbute
(he computation 01 name structures and argument numbers to convenient
places. We feel that this program demonstrates the power of Prolog rather
well: such things as sorllng mles and names proved considerably simpler
(han In the luncUonal equIvalent. We have also used thla style 10 Include
an opUmlS8uon In lhe complier. which removes repeated mles from the
optlmlee(mfes,names,mfes' ,names')
whiCh lakes lin IJnopUmlsed mle Iisl In mfes .nd names. and computes the
optimised equlvalenl In mles' and names', II Is defined a&
mfe.mfee' ,name.namee')
not(member(mfe,mtee».
optimiee(mfe8,namea.mfee',namee').
optimise(mte.mfee,name.namea,mfee',namee')
element(mfea,i,mte), element(namea,i.name),
optimiae(mfea.namea,mtea'.names').
5.1. CONCLUSION
com pie. data-structure. ralher Ihan breaking It down Into simpler unlls. and
It depends crucially on slde-ellecls happenIng at the rlghl times. We contend
The funcUonll1 and logic programs lire very sImilar. However. the
This renders Ihe structure 0' the functional version more obscure. We
consider this strong evidence thai the need 10 specify dlrecllon of Information
110" can occasionally lead 10 badly structured programs. Excep1 for this point
we found the funcllonal style more expressive than Prolog. Both the functional
We would have liked 10 claim thai the experience of Impleme"tI"g the same
UnlOrlunalely Ihls example Is nol terribly conClusive. The Pascal hacker may
lunctlonal and logiC versIons. while writing the Imperative progrem served
only to contuse us. In this respect we fael that Ihe aumple has demonstrated
ANALYSIS OF EFFICIENCY
8.1. INTOOOUCTION
Inlo Turner's combinators. lind we lInd the Order Of 1he slle of code
produced. Burton has IIlrelidy shown that the comblnillor code need be no
thIs Ilirge. We will also show thllt Ihe super-combinator code Clln be no
Ilirger Ihan O<nlogn). bul thllt II mlly sometimes be thIs Ilirge, lind we will
Some smllll programs complied Inlo code so Ilirge Ihlll 11 could not posSibly
•. ~. TURNER'S CQMBINATOAS
tree. since Ihl!. 15 consistent wllh the gTeph reduction applications we have
TepresenlaUon. nOI counllng brackets. In this deflnlllon "'8 diller Irom Burton.
whO counts the lengths 01 IdenUlIers In his sIze measure. (However • •e heve
Now. since Turner usee a llxed sel 01 comblnators. 5 dlfferenl ones SBy.
where all the YI are dlrrerenl Identifiers. and each wi Is one 01 the VI. These
nn ( gN
nlogn ( NlogS
'.S. 8UPEA-COMBINATORS
Consider 8 single ).-expresslon h.E. and suppose It has mle5 E 1 tel En.
It will be translated Inlo
" we consider the syntax tree of the body of the original ~-8Xpresslon, we
••• thai It Ie broken up Inlo the mfes. and the non-mre part. The parts
IIluelrltt. thill, using dot1ed lines to dellmll the perts of the tree .
•y
IC:l11:1y ,,
=
,,
,,
/1
"'
• J( "z\:.J
I'
th' nemes of ItS .rguments. I(ll .. Ir«,E would therelore be represented lIS
«n,E. Ind so we count the rand argumsnl nllmes as one node. the same
I' Ax.
h appears thai the only nodes In the cOde which do nof correspond directly
'0 nodes In Ihe original !lource are thoae usad to apply the combinator
10 Its arguments. There Is one 01 lhese lor each mle. and !l0 we may
Since accounllng for mfes Is so cenlral to our problem II I!I worth studying
them further. We haya already pointed oul Ihat an mle Is an mle of Ihe
Observing thet. since E1 will be passed 'tom lis naUye l. -ellpresslon 10 lis
proper locallon. E II will be carried along Inside It and need nol be pnssd
uparalely.
Moreover. each mrs will only be an mle 01 ona ),-exprasslon. That Is. II
not an m'e of eny other ),-expresslon. allltouglt hI EI ... EI) may be.
55
So. now we know that we need only count mfes, each or which will only
Occur once. HOW8\1er. the slluatlon 15 com plica led by Ihe fact Ihat 8lCpressions
and (II Ell wilt I\sell be an mfe 01 the nlilive ),-expresslon of El. NOlonly
tnls, (lJ El EJ) me)' be an mle of the next enclosIng ),-express1on, EIther
of thes8 new mles may then cause enClosing ),-expte:nlons to have slill
In order 10 keep lrack of ttle genera led mfes we Introduce the notion 01
an accounflnQ tree. An Iccountlng free lor a program Is a Iree whose
verllces are ttle l. -expressions ot ttle program. and whose edges are
constraIned to connect a l.-expresslon 10 an enclosing one. By con'tentlon
we draw accounting trees growing upwards wl\tI the notional ouiermOs1
l.-t1apreulon II 'tie boltom. so. lor example. the diagram below shows two
possible accoul'l1lng Irees lor
r '\lid
'V
d
'a
In our
root
l-expr85stons below.
For any program. thefe 15 8 ',allesl" accounting Iree which we call the Initial
Immediately enclosing >.-expresslons. The Iree on the left above Is the Inillal
Slarllng Irom I!I program's lolllal accounting tree. we will use the mles
Iree so thai we can discover Irom II Which lurlher mfas will be genera led
recently Cll.covered mfa. original or generated. unless none 01 Its mfas have
been dleco.... red yet. In which cese It will be connected 10 lis Immediately
encloSing >.. -expression. The order in which original mfas are used 10
transform the tree will be conSlralned b'J' the rOllowlng rule: If Eland E:2
are mIn such that the neUve >.. -e;tpresslon 01 E.1 encloses (15 nearer the
root 01 the tree than) the naUve A-expression of E 1. then we wilt use E 1
belors we UI!I8 E:a. ThIs constraint means thai we wHl always dIscover the
expression
It .•• E E'
It ••• E
(a .. _ E)
~-expresslon cannot possibly have any mfes at all. II must termlnale Instead
\/
I
\ A
\/
\
~
.:.....: ...
- \/. . vvv
\/
i
70
So. If the path length from /II. 10 N Is n. then the presence of 8n original
mi. 01 /II. with native L -exprenlon N Implies the presence or n-3 generated
super-comblnators the Increase In Its size I, less than Ihe maximum lolal
tree leedlng towards tha rOOI and reconnecting every olhe r node on the
path direCtly 10 (he and near.llS1 lhe root. end where the cost of a flallen
unIon-lind problem end algorIthms lor Its solullon. II will lurn oul lhat
UNIONCA.B.C) which unites sels A and 8 Into a new set called C. and FINo(x)
wUh set nllmes 81 the rools. UNIONs are performed by making the rool 0'
'he tree represenUng one set point at the root of the free represenllng Ihe
other. FINO' are performed by following polnlers unlll one reaches a rool.
UNION I" therefore 01 constanl cost, and FINO 01 cost proportional 10 the
langth of Ihe peth lollowed. Two optimisations can be applied to Ihls
elgorllhm:
The Collap~/ng Rule: when a FINO Is performed. all nodes on the palh 10
1he root are made to poInt dlreclly lit Ihe root. speedIng up subsequent
FINOs.
Tha We/ghtlng Rule: a UNION operation alweys makes Ihe set containIng
lewer elements poInt at the set conlalnlng more. This will elso lend fo speed
up subsequent FINDs.
n turns out thel. lor the purposes of analysIng these algorithms. we elln
consider all UNION" to occur first. building a tree. followed by partial FINDs.
which start at an element and follow a path part of the way to the rool.
H should be clear thai the algorithm using only the COllapsing rule performs
In bolh cases.
Tarlan hes analysed these algorllhms ITarJan75] and hIs results are Ihal.
If m)n partial FINOs IIrll performed on a tree 01 n nodes (hen the lotal
t(a,n) - O(m.maz(1,10g(nJ/m}/log(2m/n»)
72
t(.,n) - O(m.a(m,n»
II the CO".pslng and weighting rules are both used, where a Is relaled 10
the lnyer,s of Ackerman's luncllon and grows so slowly (hal II Is bounded
by • ,men constsnl for ell practical purposes.
t(n,n) CI nlogn
using only the collapsing rUle. showing the former upper bound tight In the
cas. m=n.
e.e. CONCLUSION
O(m.~az(1.log(n)/m)/log(2m/n»)
O(_.max(l,loQ(n/a)/log 2a»
11 Is equ81 10 1 for a= -/(nI2). Therefore, lor a> -/(n/2l the bound Is linear
In m, and there'ore linear In the size of lhEt program. For 1«(I( v(n/2J. the
bound 15 less than the value at (1"'1. O(nlogn). In 'act. logln/(I)/Iog 211(k
when (I>(k.,vC2n»/2, 50 lor lhEtsEt values 0' a Ihe bound Is km. For 0(1
there are programs whose code Is this large; howevEtr. his IIXlimple Is highly
symme1rlc lind Is nOl II likely strUClure tor II relll progrllm. For programs
where lhe avEtrage number 01 mlEts per ~-lIxpresslon 15 large enough fhen
the code size Is linear In the program size, For progrllms "blllllnced' In
IInEtlir. bound applies Lastly. Ihese are worst-CllSe bounds. lind we have
been told that In praC11cEt Etven Ihe COllapsing rute 15 sufficient to maKe
luper-combinator code size will be nEtarly linear In the size 0' the program.
Imp lemen III lions rellecls a ·stow-down factor", Whereby Ihe slime expression
may Ilike a 't'arlable amount 01 Ume to execute. It might appellr thllt this
languages like Pascal, In IhEtse cases II appears lIS the time ror an
w. conjeclure that any Implementation method for the l, -cl!llculus (or any
other languaga .,.llh Algol-like scope rules) must have a slow-down ,aclor.
ot ......t
and thai In Ih. worsi case thl, 'aclor will be A OUogn).
I CHAPTER 1
EVALUATION ORDER
1.1. INTRODUCTION
let us first con sider how conirol over evaluation order Is used to ImJjrove
reads e file and prlnlS the number 01 capllal As In II. One possible evaluillon
order Is to read Ihe whole lIIe Into memory IIrsl. and then counl all lhe
(~
76
Alternlll\'8ly. we could read the Ille one ctulrllcl.r at 1!I time and update
Thl' laUe' program Is much less modular than 1he former: the two logically
Independent ope,etlons of reading the file and counting the As have had
10 be programmed loge the,. However. It Is much more efficient beceuse
tI needs only • constant amount of spece 10 run In. The former method
may requires en amounl of space which depends on the size of the file.
Thl. I. such • gross dillerence that we cannot aflord to Ignore II.
e~.luellon (or eny other slrategy already In use> the correct choice enyway,
.nd If nol. how can we besl cause the correCI order 10 be used? In secllon
7.'1 w. will eJlhlbll eJlamples WhiCh demonstral8 (hat lazy evaluaUon- Is
"',t.•
Now let us move on 10 a simple lellt processing funC110n tp. Ip allows
InlO
" Is defined by
tp IJ ab . [J
tp (-I-:X) ab • ab ++ tp x ab
tp (- (.: x) ab .. tp (after ")" xl (before .). x)
tp (c :1) ab - c: tp x ab
where belore and afler are 8$sumed to return the characters In the second
OW4rtU\tL
.rgument up to and beyond the first I II 1 II or the IIr" argument
respectively.
7.
\hus:
length 1 - length' 1 0
length' (] n - n
length' (a:x) n - length' x (n+1)
u,lng only constant space. Howellsr. thl, conslant space versIon depends
on Ihe second parame1er of length' being reduced belore length' Is applied
lise". This would nol be so In a completely lazy system: Inslead the sum
wculd be accumulated unevalua'ed, as the 811presslon (((0+1)+11+1), and
would only be reduced when returned as the re~nJlI of lenglh. So III .llllzy
lValuator "oull! make this version no more efflclenl than the lormer. We
will ,ho" Illter how we can force 8n otherwise IlIzy evaluator to execute
this function elllclenUy.
evaluallon In sectlons 7.4 and 7.5. In seclIons 7.8 and 7.7 we tackle some
Inlerestlng examples using Our prlmll!ves. and ehow thai they allow efficIent
In this section we will explain 8 lew examples which show thai simple lazy
the 'unction that compules the length 01 a list. The simplest dellnltlon we
length [J - 0
length (a:x) • 1 + length x
Ihls formulallon will Clearly require space proporllonal 10 'he lenglh 01 lis
argument since. for ellample. leng1h 11.2.31 Is reduced lIa lollows:
red 3
When the last element of the list Is scanned by length. then the enllre chilin
One would expect thai the only space requ1rttd by tp 10 process a file would
be the space required to slore the curren I abbreviation. Alas. this Is nOI
10. Consider the InpUl 11,1 ·la)bcdelgh1jklmnop.. • Ip 0' this IIsl will be
So. since the value of the abbreviation Is not reqUired unlll en exclamation
mark occurs In the Inpul. It will aClually be stored In the unevalualed lorm
(balora "). ·albe....). It will retaIn II pOinter 10 almost all the Input lIIe.
preventing II trom being reclaimed by the garbage collectOr. Thus Ip. like
lenglh. mey reqUire an .rbllr.ry amount 01 space to run In.
VAL ! x red t x
but we Insl" lhal II Is evaluated t1elore VAL Is applied. We will usually use
an 'nllx notation. writing I «".1 x) Ins lead ot VAL I ll. and we will leel free
(0 use the nOlation (yal E) In any conle~ where E Is e 'unCtion ergument,
whether Ihls 15 Implicit or ellpllcll. For ellample. we will hepplly write lei
x • .,al E In... 10 Oenote e strict dectarellon. and we will write la. yal b,
cl to denote that b Is evaluated before the luple Is constructed. While a
and c are left unevaluated until required.
a ; b II (lX. b) val a
.nd we can dellne olher functions which control evalueUon order, for eJlample
force -- an IdentIty lunctlon on lists that relurns Its result only after all
the elements 01 the list heve been BV8lueled.
torce 1 - ca.. 1 at
[} --+ IJ
(a:b) -+ a ; torce b
esac ; 1
All lheea .....Iuatlon conlrol methods ere In usa In the lIspkll 80ltwar8 tieIng
by III lazy evaluator and W8 showed how they can be made efflclenl using
VAL We regard this as III good solullon because It doas nol necessltale
chenglng program structure. II seems thal we can wrlle our programs wlthoul
consIdering executIon order 81 IIrst. and then 8nn0181e them with VAL and
maner. are nol qulle thai sImple. 85 we will seB In Ihe nex1 section.
In lhls section we will consIder III very simple paning problem. We wanl
• l~nctlon split which lekes II list 01 characters and relurns a pelr whos(
flr.1 componenl Is the IIrst Une 01 characters. and whose second component
{length a. length b)
contain pointers 10 lhe entire Input list. and so whichever Is evaluated IIrst.
the other will prevent garbage collection of any of the Input In the simple
program above this will lead to al least the firS! Une 01 Input beIng present
In memory In Its enUrely. SInce the IIrst line may be arbItrarily long. this
Is an Intolerable OVErrhead. Ot mIght ,eErm that unnecessary buffering 01 one
We might try to address this problem In the same way a. we solved the
problems In the last section. by rewrlung split In .. different way and using
VAL to control the execuUon order, However, this example does nOl yield
to thl, treatment. We will prove Informally thai no sErquentlel evaluetor can
eJ(ecule any verslan of ,pllt elllclently.
reduca E and olher expressions 'hal E demands until E has been cornpletely
reduced.
Now. we assume I"a' s Is a vErrslon of ,pili which will run In constent ,pace.
provided It Is used In the right context. We Irnaglne thet e Is applied to
the InUnlte Ust 01 characters from Ihe keyboard. and thaI characters are
typed relallvely slowly. We consider the second component of s's result Ihe'
pari of the Input IIsl atter Ihe flrst newline. This Is a pOinter Inlo Ihe Input
84
II., etler Ihe IIrsl newline characler, and so C8nnOI poSSibly be com puled
unlll an the IIrst line has been typed, Therefore. If the conleX1 demands
Iht.. value before II has consumed all the Itrst line. then II 'Will be 5uspended
until lhl Ilrst newline Is typed. and arbitrarily much Of lhe line may need
coniallt consumes all 01 the rtrst line belore demanding Ihe rest of Ihe InpUI
woule reqUire mOre than constant space. Lei I be Ihe Inpul list. and lei
I" be lhe part of Ihe Input list aller Ihe IIrsl n characters. Since is must
retu,n Its two fasulls to Ihe conlexl belore U"le context can consume any
characters al all. we know that s rellJrns belore K+l characters have been
typed. Therelore the expreSSIon E denoting the "rest of the Input- must be
pointer Inlo Ihe Inpul list Us unevaluated lorm must also coniain a pointer
Inio the Input list ThIs unevatuated form Is created before K+l characters
havi been typed, end '0 the pointer musl point before 'to. If this pointer
remains unchenged throughout the consumption 0' the 'Irst line Ihen It wllt
am..unt of space.
We must therelore assume Ihal lhe relerence from E 10 lie Is via some other
01 (he flrs( line. This reduction musl eliminate Ihe pointer to Ito. We have
alreedy ahown that the consumption 01 the first line cannot demand E. so
E - P E'
'or aome Junellon F. In order to sells1y our assump1l0n eboul K, E' must
be reduced aftel" al most 2K charaClers have been typed, and consequenlly
the result 01 reducing e' must contain a poInter Inlo the Input Itsf belore
I., Once agaIn. thIs pointer. II relalned throughout the consumption 01 Ihe
IIrsl line. would leed 10 an arbUrary amounl of Input being bullered In Sl{)re.
long. a chain of el least n/K expressions will be buill up. Since none of
lhe expressions ere equal. 8t leasl n/K cells will be used. and 8
no sequential evaluator can execute any versIon of SpIlt In con slant sp8ce.
whenever there Is more than one ·Con,umer· lor a value: If the sequenllal
nalure 01 the abstract machine lorces one consumer to run first then the
other will retain the value and delay garbage collecllon. (In the case 01
spHI. thare are two consumers. They are Us IwO results>. Even " II were
possible 10 combine Ihe IwO consumers Inlo one Ihls would be a bad solullon
since It would destroy Ihe modularlly of the program_ We have shown l.hal
machine.
86
Having done so the prOblem Is nol yet solved: we musl address lhe problem
lor Doing so. However. he has also shown that If 8 program cannot be
SCheduling seems the more desirable solullon In Ihe long run. we consider
lhal Lhese dllilculiles malle It Impracllcal al pre5enl. We prefer 10 look lor
By analogy wUh the VAL IlJncllOn which we IntrodlJced In sec lion 7.2. we
PAR t x :red t x
wllh Ihe addillonal property that • Slarl5 e.eclJling In parallel before the
reduction Is performed Since PAR d085 nol walt for • to finish II Is trUly
11\ ICienUty function (VAL 15 not because II Is slrlCl In .l, We will use en
Il'Ifi. not.lIon for PAR and detlne control funCllons lor use wIth II. IUSl 85
so that. as the conlelct consumes the flrSI line. the second component Is
al.o ell:ecut.lng and s*allo*s characters as they arrive. (par II, the Inflll:
version of PAR. Ind the notation In this enmple means lhat Cafter nl n
starts ell:ecullng Immediately the resull or split Is conSlructed).
par atter n I l )
....hlch 15 betler than the former In lhal. even \I the contell:l does not refer
10 the first line until long aller Il has consumed the rest of the Input the
BB
first Uti. will be compu1ed and will nOI relaln II pointer 10 the entire Input
10 make split run elllelsnlly when applied to the keyboard, but we haye so
It spill I, not epplled 10 the keyboard. bul to some computed 1151. then there
'.,ter than !he contelC1 Is able 10 consume the Ilrsl line, This may result
In much 01 the IIrsl line being buffered while the context cetches up. Wa
We recall at this llage that "lazy evaluation" sCheduling 0' graph reduction
operations through the graph. Only paris 01 the graph at which demand has
SYNCH e - [e, e]
However, the two caple, 01 e whIch Bre relurned arB actually dlllerenl' call
Ihem B.l and 81. No demand Is propagated from 81 or 81 10 e unlll both
we cen be sure thel 1 and 2 will not be added unlll both (ac10rlsl and
In the spill example. we need 10 ensure that two separate processes consume
• list at the sarna rate. We will define a funcllon SYNCHUST thet takes a
lis: Cind relurns two versions of It. In such a way thai both versions must
SYNCHL I ST 1 ..
let [sl, e2J - S~NCHLIST (tail 1) in
let [11, 12J - CAse 1 of
[J --> [[I, [I)
(x:I') --i' [x:eI, x:s2)
eSAc in
lira t!lvelusled 8nd It relurn, lis two results Immedlalely. Evenlually. demand
will arrive 8t one of them, say (w1; In. and so wl will be demanded. Since
Leier another parallsl prOcess will demand (w2; 12J. and so demand will
arrive 8t w2. Now the conditions lor SYNCH 10 propagate demand are
satisfied. and so SYNCH's argumenl I will be evaluated and returned 8s
the value 01 wl and w2. ThIs wi" allow 11 snd 12 10 begin 8.lCeCUllng. and
Now If we define
to be driven by Ihe consumer 01 Ihe firSI line. (Technical pOint: for lhe
when 11 Is reclaimed by the garbage collector Wlthoul Ihls the e.llample above
would deadlock el the end 01 Ihe IIrsl line because no more demands woul.d
IIrrlve al 11).
really can write Ihe prognm first without regard 10 e..,aluallon order. and
annoIale II alterwards. However. we musl leke InlO account the Intended use
of /I function when we annolate It and we may need dltlersnUy annotsted
"s"lons for use In dlflerenl conlexts. In the remainder of Ihls chapter we
ifill gl...e a lew more examples 01 the application of PAR and SYNCH.
7.e. QUICKSORT
aot:t l] - II
80:rt (a:xl - BO:rt (b ....... ; b<a) ++
(al ++
so:rt (b4-tl:; b)a)
using Turner's ZF nolaUon [Turner811 Ho.....; b(8) means Ihe list of elements
b of lI: whIch ere less than s). The ImperaU..,e Quicksort sorts 8 IIsl of n
elements In space OCn) and time 0(n2) In Ihe worst case. Olnlogo) on
a"'elage. We shall examine the complexlly of the functional ....,slon.
Fil,t of all we consider the lime laken 10 sort e list 01 n elements. Tin).
On average. Ihe IW'O recursi...e calls to sort will be on IIsls 01 lenglh nl2.
and ~o we ha... e
T(O) - a constant
T(n) - 2T(n/2) + the rBet of the coet
92
Since the Test of Ihe Cosi 01 a calt of sari Is proporllonal '0 the length
T(n) • 0(n10gn)
However. In the worst case Ihe Iisl will be spill InlO an empty list and 8
T(n) - 0(n 1 )
So Wf obeerve with salls faction thai the functlona' version of QuIcksort has
How. lei us consider the space complexUy. SCn). Clsarly SCn) ( Ten). We
Consider the amount 01 'pace In use Just aller computing the lirst element
of Ihe ,orled list. Since this element could origInally be anywhere In the
.rgument to sort II Is clear thai sort must force the complete eveluatlon
01 lis argument belore II can compute lhls element. So. considering the
_econd equellon lor ,art. we 'ee that by thl, stage the expression (b .........;
b<1I mu,t be completely evaluated. and '0 wIll not ,hare any cell, wIth
the orlglnel argumenl. On a lazy evaluator. the second recursive call 0' sort.
sari Ib.f--tl:; b~a) wUl nClt have been evaluated at aiL and so will contain a
approximately the space required lor x plus the space required 10 sort tb~.
5(0) • n + 5(n/2)
and :<;0
Sen) • O(n)
5(0) • n + S(n-l}
and so
5(n) ·O(n 2 )
as lis lIme compleldty, lar worse than the Imperative equivalent Even In
casas thai deviate slightly Irom lhe average. this means a worse space
behavIour,
Of course. Ihls Is JUSl the kind 01 prOblem Ihat our primitives were lnlended
10 solve. We shalt use them to lorce bolh recursive sorts 10 consume the
To find lhe apace complewlty 01 this funcllon, we observe that. since access
ThiS erample shows lhat our prlmlllves can be used not only 10 make cenaln
7.7. PIPES
The UNIX operating system provides a lacUlty whereby two (or more)
of 'll"rlling multi-pass compilers. lor exam pte. so lhal each pass receives
the OUlPUt of the prevIous one. and all passes can run In parallel and exploit
synchronised In that each prOducer may run ahead of Its consumer. bUl
which Just compotes the lwo programs. Running (connect progl prog2)
In this case Ihe programs are synchronised ellaclly. since prog2 will only
slanding idle while Ihere Is real work 10 be done. We can easily correcl
Ihls by defining
using the lunctlon pIpe which takes a number and a !lst and relurns a copy
01 the list. but starts Ihe evaluation 01 the N'4-I'th list elemenl as soon as
Now when (connect prog 1 prog21 is run. prog2 Is started Immedlalely and
Its IIrst N outputs are demanded Therealter. whenever progl consumes one
01 lis Inpuls. another output of prog2 starts to be compuled. prog' and prog2
The method we have used here Is actually more general than lhe UNIX pIpe.
It can be used In any conlexl where this Is desIrable. f~r example to starl
disc translers shortly berore the data on the disc Is needed. or Just (0
7.8. DtSADYANTAGES
were defined by
then 11 would never produce any results because demand would never arrive
at 12. On Ihe olher hanlf. rampant parallelism occurs II PAR Is used too
pipe n 1 - paIliBt 1
"'," II would try 10 compute all the outpul 01 Ih, prOducer process In
exists p - p. \ function p
exists p - exiets (p true) or exists (p falee)
and returning 8 boolean. and returns true II lis argument Is not Identically
a space efficient sequentlal search. but If they are e",aluated In parallel then
rampant parallelism will result which will clog the system completely.
In theIr use. This may slgnlly thai lhey should be replaced by a more
7.'. CONCLUSKJN
achieved. IT I:!I debalable which 15 the besl way 10 ensure correct sCheduling.
engender confidence thai they are sulflclenlly powerfUl lor most pr8cllcel
problema. Our prImitives have the ImpOrlant advantage Ihal they are
CHAPTER 8
GARBAGE COLLECTiON
8.1. WTROOUCTION
IOe'1'11581) we 81'0 believe that the besl way to provide 1118slor8, and
In secllon 8.2 we will discuss the general siralegies available and argue
thai feference counung Is the most promising lor our needs. In seellOl') 8.3
we will 81lamlne how circular structures, the bane of reference counllnl1
ga,bege collectors, arIse. In sectiOns 8.4 and 8,5 we will p'esenl our
eJltenslon 10 reference counting fo, managIng circular structures. flnall~, In
5ectlon 8.e we will examine the costs or our method, In section 8.7 we will
There 119 two main 51r81egles lor deleting objects no longer required. which
deletea by vis lUng all obJec1s accessible trom any machine regIster. and
lhen delellng the others. Relerence counllng algorithms siore a count of
the number 01 references 10 an object wHh the object and delele 'he oblect
when Ihis count becomes zero. Both categories cover III wide varlel"/ 01
mark-scan algorllhms do. Is very ellpenslve when there are a very large
number of objects. Our vary large virtual memory will conlaln a very large
can all share the same locations. These locallons wIll therefore form part
'hit It very large vlrlual memory Is vIable il Ihey are not deleted promplly
101
For Ihese reasons we CIa nol believe Ihal mark-scan garbage collection Is
viable In largo Ylrtual memory parallel appllcaUve systems. However.
are nOI deleted at aiL Since even (he Ubiquitous recursive 'unelian is usually
represented by a circular struclure. 11'1115 presents a very serious problem
8.3. CIRCULARITY
~n . if n - a then 1
else n * fact (n-1)
vhere fact
Olher thin recursive lunctlons (which are vUat since naralla" Is expressed
have oherved In IFriedman791 (hal reference counting will work for recursIve
thai ,~ey are almosl always unnecessary: but occasionally a program cannOI
p,-oblem here Is rhat, since luncllons In the language being Inlerpreted must
languagn. and lor Ihe lime being we assume thai the graphical comblnators
In"lanl. and looking for 8 way 10 describe Its circulatilles. We recall two
Definition: A greph Is "'tongly connected II, for any two nodes A and B.
there Is 8 path from A 10 B and vice versa. For example. the lell hand
graph below Is strongly connected. but the right hand graph 15 nol.
connected sUbgraph.
tI tollows from Ihese definitions Ihat any graph elln be decomposed Into
disJoint ,trongly connected componenls The !lgure below shOWS a sample
Cl8kled. then ell Ihe nodes In Ihe component can. II 101l0W5 thai It Is more
0' coalescing all nodes In Ihe same strongly connected component. More
precisely, the nodes of 0' are the strongly connected components 01 1],
The derived graph Is always acyclic. We can see thts because. If It _were
not then there would be two distinct strongly connected components A and
B with paths both hom A 10 B and tram B 10 A. Bul this means thaI. In
the original graph. (here must have been paths from a node In A to a nOd~
In B and vice versa. Because A and Bare bOlh strongly connected. lhls
106
Implies lh!lt there must have been 8 path Irom any node In A 10 any node
In B end vice vers!!; so A !Ind B would both be pari 01 the S8me strongly
dlslolnl.
Since the derived graph Is acyclic. II can be garbage collected safely using
• field 10 each node which points to 8 shared reference count. All the nodes
[D .
I!J
·······lIJ
CONS 1
107
We can now delete a whole strongly connected component when the shared
reference count becomes zero. We can also perform a lasl tast to discover
whether two nodes are In the same strongly connected componen!. by
we show how the machine can update the ,hared reference counts during
reduction.
Since our machine language Is appllcallye. all the reducllon rules obey e
very Imporlent property: all the nodes accessible from the root ot a reduction
after the reduction are either newly claImed. or were already accessIble
before the reduclion. Because of thIs. two dlsllnct strongly connected
components will always remain so. slnca no reducllon can make one
accessible tram the other If /1 was not already so. The only ways In whiCh
a strongly conneCled componenl can change are by growing. /e having newly
created nodes added 10 It. or by splitting InlO several smaller ones.
'esult are added 10 the node being reduced. This may cause slrongly
second stage. the old polnlers from the rool are deleted. This may cause
108
In be~een Ihe two stages there will be more than IwO pointers Irom the
When adc:Ung nodes and poInters 10 the program graph we must decIde which
,1 rang')' connected component the new nodes belong 10. Having decIded
this Irlo local and shared reference counts may be adjusted according I),.
We Il'iU51'818 how this may be done using the combInator S. whose elise I
Ie aMwn below.
s A
Arter Ihe first stage In Ihe application 01 S. Ihe graph will appear as shown
below. wllh foul'" pointers from the rool. T"e nodes marked • are new and
Since the starred nodes form a tree. t"ere Is only one node polntln·g at
each one: the node directly above It In the tree. II follows thai If any slarred
node Is In a non-trivIal component 1hen the node dlreclly above It 15 In
110
the S8rrte component. and. ultimately. so Is the root. Therelore. every slarred
component then al leBst one 01 the nodes II points 10 Is part or the same
slamd component wllh the same properly. II lallows Ihal all starred nodes
same component.
Con'lersely. any node on a path bSlween two nodes In the same component
Is also In that component. We may lherelore allocate new nodes 10
components as follows: when a redUCtion Is performed lhen any new nodes
on I palh from lhe root to an argument In lhe same strongly conneCled
Even lhe '( opera lor Is covered by this rule. It adds no new nodes 10 the
graph. but adds a circular poInter. This pointer cannot aller lhe strongly
connected component struClure or the graph: II only atlers the local reference
········GJ CD
=
x F
When a pointe,. Is deleted there are two casas to consider. If the pointer
connects nodes In two dUlerent strongly connected components then It Is
only necessary to update the local l!IInd shared reference counts 01 Ihe lergs!.
and delel8 the large I II necessary. All Ihe nodes at 8 strongly connected
component can be deleted at once because they BrB all accessible from
any node In Ihe component.
II the polnler connecls two nodes In Ihe same slrongly connecled componen!.
Ihen II Is possible that Ihe component may split Into many smaller ones.
For example. aelellng Ihe marked poinler In Ihe diagram below spillS Ihe
,"
, '
•••
(~''-'-'
I "
.......
I "
I
,I
I
.•'.'
I
•
We hive to find the strongly connected components of lhe component beIng
Spill. and mark them 8S new strongly connected components of the program
Ihe ~lrongly connected components or 8 graph In one scan over It. We can
apply this elgorllhm 10 the component being Split. and II will run quickly
the ~ame time we can compute the shared reference counts 01 the new
because each node mUSl have 8 shared reference count pointer slored In
It. Since, In general. I reference count requires about 85 many clls ItS 8
node already containIng 8 local reference count. e head pointer. and I lell
very little extra siore need be used 10 hold the shared reference counts
distinguished In some wey. then the shared relerence count can be stored
In Its shared reference count pOinter lIeld. and Ihe other nodes In lhe
component cen pain! 10 It. This requires only one bit per node. UsIng this
scheme non-circular par1s 01 the greph. whIch consist 01 s!rangly connectad
components 01 one node each. can be stored compactly and have their
reference counlS updated quiCkly. ThIs Is very Importanl because the bulk
01 the graph Is of lhis lorm.
Our method is also slighlty slower than ordinary relerence counllng. pE!rtly
because 01 the extra cos I 01 updaUng shared re'erence counls. but mainly
used several limes: lor example recursive functions are usually called many
114
lime!. Since 5lrono'y connected componenis need to be split only when Ihey
change shape. lhl:s will happen whUe they lire beIng buill. bul nol while
they are In use. Therefore. for example, calling II recursive funcllon should
Involta no splitting.
Our method timers Irom the other approaChes we discussed in section 8.3
1851 suong pol nler 10 III node I, dele led lhe garbage collector aHer, lhe
,Ialus of enough other pointers to ensure thai lhe whole graph I! spanned
by an acyclic graph Of strong pointers. and deletes parts 01 lhe graph that
The greal advantage of Brownbrldge', melhod over our own Is thel It reqUires
no sssumplJons about Iha kind 01 usa 10 which II " put whereas our method
can only be used wJlh graph rsductlon Implementations Of appllcaUve
they are both applicable. We are unable to compare them In any more derail
8,8, CONCLUSION
We expect that computer syslems In Ihe future will be hIghly parallel. and
will have large ....Irtual memories. Garbage collection will be e55enllal. but
such general circular slruclures are a ....lIal programming 1001. We have shown
CHAPTER g
RELATED WORK
•. 1, INTRODUCTION
to Ihe promise thai they can be used 10 exploit highly parallel arChitectures.
aec~on 9.2 .e will descrIbe 1""0 string reduction architectures. In sec lion
9.5 we wIll dlscu!n attempts 10 e.-ploil Turner'S SKI approach lalrly directly.
sup,r-comblnal0rs or very similar Ideas. Finally. sec lion 9.7 concerns 11sell
_Ith de'eflow.
consists 01 a processor and two stllcks. II lell slack and II right SlIlek Un
'acl U1e processor contains several olher ,lacks for holding expressions
temporarily). The mllchlne sl8rls wIth Iha Input expression In ellner Ihe left
or Ihe right slack and transIent II back and fOrlh between them performing
greal deal 01 lime on reductIons which lire nOI eClually necessary. This Is
bodies: If the argumenl Is used more Ihan once then any subsequent
extended to exploit seversl processors. and Ihls mlghl psr!lally Outweigh lhe
dlsadvantsges.
In a long linear sequence whiCh holds the symbOls or the FP progrsm bell)g
reduced. and Ihe Olher kind Is arranged ItS a binary tree connected to the
The machlna operates In cycles. during whIch 811 reducible expressions are
IOCllld, mIcrocode broadcast through the tree 10 the cells that need 11. emply
spac!t!l r8arranged 10 make room for expansion of the program. and finally.
ne~8r duplicated. The prIce. howoyer, Is Ih81 all funcllons ere strict. and
where It I, and moyo other arguments end results past II. This Is not 8
dal8 movement.
Turner has used greph reducllon of the saurce In his KRC Implemenl8110n
keller. Lindstrom and Palll use graph reducllon or source programs In their
,Ignlflcanl computlltlons. and the Iree serves only lor communication lind
120
execuUng processor. end when Ihe processor Ilnishes (or suspends) the
current process 11 IIIk8' the n8Kl one Irom lhe INVOKE-list and proceeds
with 11181 Inslead. Periodically the communlcaUon Iree oblalns the lenglh of
eflIC'Elntly. since they need only pa55 through a small number 01 node5.
This Is both /15 strength and Its weakness. 115 deslgner5 belle". thai executing
proorems will exhlb/l 5ufflclant locality of reference that almost all
communlcetlons will be local and therefore 'a51. However. since all
communlcetlon& tre"ellino a dlslance 01 hall Ihe machine or more must go
through the root Of Ihe Iree. lhen If only a 5mell percentage 01
cOJllmunlceUons lurn out to be trUly long-dl5lance the rool will become the
bolUeneck 0' the enllre system.
Itiroullh e packel switching ne1work. Thus alt proceno,s are the same
distance from all memorle5. and locality 01 reference 15 not an Issue. The
"I funcllon5 In Ihe user's prooram before execution begins. ALICE al50
SubsUlullng lor the arguments. so nesled ).-expresslons are nOI very ellicient.
ALICE contain!! some messy lealUres. lor example. uncurrled luncllons with
81 mosl three arguments are much more erUclenl lhan olher kInds. We
suspecl Ihat ellons 10 exploll such fealUres will lead 10 complex end unWieldy
sohware Nevertheless. the overall design accords very closely wllh our own
views. and we are conridenl Ihal all the resulls of our work would prove
The SEeD machine (described In secllon 3.4) has also been used as the
and then executes the machine code wllh an Inlerpreler. Inlerprelers have
been wrillen lor a wide varlely 01 machines <ranging from a ZBO 10 a VAX)
environment.
122
Then Implementations perform fairly w811, but have it lendency to run oul
'ree variable lists rather than environments. wl'1tch contain only Ihe pari 01
the anvlronmenl that is l:lClUally needed. However. many more Iree variable
SYllll!m.
Steele and Sussman also used the SECD machine as the basis for their
and that It doas nOl support lazy e'laluatlon. A '1ery simple compiler con'lerts
standard funcllons In SCHEME programs Into Indexes Into lhe microcode.
and '1arlable names Into Indexes Into the en'llronment. The SCHEME-79
of magnttude laster.
Friedman and Wise Is used which Is able 10 recover the permissible Circular
similar to ALICE.
does permit parallelism. so Ihe problem may be much less severe in this
coni ext. Parallelism is controlled In a verV dillereni way Irom Our own
suggestions. and II Is nol Clear whelher the programmer can control slDre
thai S Is defined by
s - ~abc. a c (b c)
lis Ihlrd .rgument M and directs II lowards both Eland E J. " elthor E 1
wl'1tre It Is required.
One of the allraCtions of Turner's comblnalors Is Ihal (here ara lew enough
of them to form Ihe machine code 01 a compuler. This has been exploued
- Indica ling thai an argumenT Is 10 be dlrecled to both. the lell. the rIght.
These dlreclor strings will be used In Burton and Sleep's ZAPP (lero
may steal a penellng proCes' tram one 01 Its neighbours. with Ihe reslrlcllon
115 origin. and therelore that Its result need only be transmllled one nelwork
step once It Is computed. Since eaCh processor sees 8n Infinite binary 1ree.
work can spread through the network very quickly. lAPP does not support
8 global address space: Instead all deta a process may need 15 slolen along
wllh Ihe process. Herein lies the weakness 01 the design. tor data can be'
10 lois 01 parallsllsm. but also lOis 01 space utilisation; on tha olher hand.
aectlons 3.5 and 4.5. The comblnators are then compiled Into machine cotje
for tile G-machlne, an abstract machine with a slack In addition 10 the graph
code Is translated Into VAX machine code and called from a graph reducllon
can Ufe" be reduced Immediately: tor eXllmple. the 'unction auec dellned
by
suec n • n + 1
10 optimise the code produced. makes Johnsson's lezy Ml run liS lest 85
more conven,lonal lengu_ges. Johnsson Is considering other Implemenillfions
version.
like Johns,on. he will make grellt efforts to "lIecule each combinator more
1.7. DATAFLOW
detaflow designs. The origInal Idea ot dalaflow was thai the program woula
and out of which answars would emerge. Since many paris of lhe graph
might be actlye at the same Ume the approllch often good prospacts lor
• switChing network like Ihe one used In ALICE and DSI. Its disadvantage
10. Thll 8110ws mUltiple concurrent activations 01 the same piece of progrem
graph. and so recursion Is possible. but Ihls scheme slill does nol support
higher-order funcllons n8lurally. Walson and Gurd have built 8 single
e velu,. rather than a function through whIch dala musl tlow. " Is eble
CONCLUSION
not believe Ulat Turner's comblnators suffered Irom one unlll we saw
lactor unlll _e saw Fischer's example. Only after being wrong twice did we
te-examlne other approaches and observe ,he same behaviour In them all
behaviour.
machInes must suppOrl parallelism. lind thet there is strong 8'Wldence Ihal
ImpManl property thai they can be IHlded 10 a workIng program: they need
Impractical due to the size 01 virtual memories thai wilt need 10 be garbage
10 Turner comblnalors',
Conlerence.
(Curry58] H. B. Curry, R. Feys. ·Comblnatory logic·, North-Holland Publishing
CompUler Architecture.
Vol. 8, No.
lFriedman80J O. P. Friedman. D. S. WIse. "An indetermlnale constrUClor lor
ProgrammIng languages.
a
IHenderson76J P. Henderson. J. H. MOrris, "A lazy evaluator , Proceedings
languages. Allanla.
ImplemenlaliOn R
• Prentice-Hall.
PAO-32.
PRG-28.
ProgfilmmlOg
10 combinators".
Archileciure
CarnegIe-Mellon Universlly
~
fWadsworU17lJ C. P. WadswOrth. "Semantics .nd pragmatics 0' Ihe
Tbil pros ram. whicb Usel Eratostbene:.' lieve, was adapted from one in
[Turnu79a~ It illustrates the use or Nose's indexing operator (!) to access
componcfltJ or I. pair. This program was also used in our experiments.
I olhu::any -) falK
dnik
Secondly, our compiler was very slow due to the use of a sept
Interpreler for GEDANI:EN abstract machine-code. This, coupled with the
.man unount of store available (about 25K words) limited· the
.ize of example we could try sever~ly. Our results may therefore be
distorted by "end-effeeu". We have interpreted them by looking for
trend. as program size increues.
A.4 A Lispkil compiler in Prolog
The Note compiler described in section A.3 was rar too large to
include as an appendix. So that the reader my run a super-combinator
compiler if he so wishes, we have included a version written in
ProloK;_ This compiler iI the model for section ,.,. It compiles Lispkit
Lisp rather than Nose. but is otherwise very similar.
COMPILE.PRO
/- Tbis rue contains the stcerln. prolram or the Lisplr:il compiler. Consult it. Ind then
In~olr:c tbe compiler by. for cn.mple, compilt{'frolo.lso','holo-sup').
-,
1- consul1(lilu). /_ standud li.t-prCKa.inJ (unctions _/
1- consult(lisplr:h).
1- consul1(.up).
1- con.ult(optimlse).
7- conlult(writecode).
7· conluh(cn'l').
colnpllt{From.To) :
w,itc('USP Compiler to Supcr·comblnatou'). n1,
wrlte(From)• •rite{' ---) '), wriu:(To). nl,
(docomplle(Prom,To). ~t'rlte('Compllltion .ucuedJ·). nl;
wrlle('Com pllalion reUed'). nil.
docomplle(From.To) :
IU(from). read(Prol). ItCn,
wrlte('Syntu. Analy.l. CODlpltt". nI. I.
preproc(Prol.X). lI.pklt(X.Lam).
wdte(·Con..crttd to Ilmbda ealclIhu'). nI. I.
sup(nlle.Lam .upr(O,[J.5I1P....J.m.
wrlte('Com pUed to Juper-c:om blnatou·). nl. I.
oplimlR(Sup.Opt),
wrhe('Optim Itation com plete·). nI. I.
ldl(To). wrilecode(Opt). nl. told. l
L1SPICIT.PRO
r. This file derma the predicate Iisptit(A.B), which c.onyerLJ I lispkit pralum
into lambda-ealtulus ready (or compilation.
'/
Iispklt(Name,quotc(NIDlc)) :- ttandlrd(Namc).
Ilspkil(lnt,quotc(constlnl(lnt))) :- irnclu(lnt).
hspkit(Namc,Namc) :- IlomCNlmc).
hiP Iti 1([quotc,El,q yale{canstanICE»).
1Isptil«(lam bd"',[],Bodyl,Body!) ;- lisptil(Body.Bodyl).
lisplr:it«(b,m bd.,Nam toNlm CI.8ody 1.11mbda( N I mc,Body I» :
lisp ki t([I_ m bd.,N 1m es,Bodyl.Body I).
lisp kll«(1 cl.Bod y leehJ.E) :
decom pose(Defs,Nlm eI. Val,),
Iispti 1(( II m bd.,N 1m es,.BodyJ.EI),
I PPIisl( Et.V IIs,E),
hsptit«(Ict rec"Body IDd11•• Pply (Body 2,Ipply(quote:( fi ll).Vab2»)) :
decom pose(OcfI,NI m d. Vals).
Iispkit(Body,Body!),
lam Iist( N I mes.Body I,Body 2),
mlkeIiSl(Vab.VahU,
lam Iist(Names,Valsl,Vals2).
li1pkit((F],F1) ;.- lispkil(F,FI).
lilpkil((F,Al,lpply(FI,AI)) :- Iispkil(F,FI), lispkil(A,AI).
l\t;Jkil«(F,AlRl,E) :- R\o:(l lispkil([(F,AJIR1,E).
appHst(E'(l,E).
appllst(E,V.vs.EI) ;- Iispkit(V.VI), appliSl(apply(E,VI),Vs,El).
lalll Ilst([],F,apply(quotc(k),F».
bmllst(A.B.F,apply(quotc(unpal;k)Jlmbda(A,A») ;- Ilmlist(B.F,FI).
m Ikclist([],quotc( ni I».
DIlkdi.t(A.B,apply(lpply(quotc(cons),AI.BI» ;-.
matelill(B,BI). Iilpkit(A,AI).
d""m pOK([]!U]).
dcmm po:ae((A.BlClJAIN1,(OrvlJ ~ dceomposc(C,N.V).
allndard(add).
Itlndard(sub).
ItIJdard(di'r-).
Ilildard(mul).
11I.ldard(rem ).
tl~rrd.rd(eq).
Italdard(lcss).
•taldard(.reater).
tlardard(cons).
IUlrdard(c.ar).
Itardard(tdr).
Itamard(lf).
ltanlard(wbicb).
preproc(A,A) :- atomlc{A).
preproc(A.B,ALBI) ;- preproc(A,AI), preproc(B,BI).
prcproc(A.C) :- A-..e. prcproc(B,C).
SUP.PRO
/_ TbiI Is a compiler to luper-combinaton written In Prolol. Input syntax:
atom quole(conn) IppIJ«n,ar1l la.bet.Od.e)
Output syntu:
., aupu(nul',body) .,,(n)
Jupapp(cxpr(FL.FM.FE,FS),cxpr(AL,AM.AE.AS).FNm,,ANms.
expr(L.M,. pp 1,(FE.AE),.pp Iy(FS,ASJJ,N fa,) :
(FL-o~L-O;Fl;"AL), m....(FL,AL.L).
Ippend(FM.AM,M), append(FNmJ,ANmt,Nmt).
lupapp(cxpr(FL,FM.FE,FS),cxpr(AL,AM.AE.AS),FNmt,ANmt.
apr(AL.txpr(FL,FM,FE,FS).AM..pp1l( ar ,(NN),AE),lpply(FS,AS)),
name(NN,FNm.).ANml) :
FL<AL. FL \-0.
I·Jp.pp(cxpr(FL.FM.FE,FS),upr(AL.AM.AE,AS),FNmt,ANIa'.
apr(FL,apr(AL,AM,AE,AS).FM..pply(FE.ul(NN)),lpply(FS,AS)),
namc(NN,ANm.).FNm.) :
FL> AL, A.L \-0.
laU(A.B.B) :- A<B.
laU(A,.B,A) :- A)-a
OplmCallUJ,lIJD.
opt.Ca(M.Ms,N.Ns.Jo(.MsI,N.Nsl} :. not(membet(N.Mt)). optmfc:s(Ms.Ns.Msl,Nsl}.
optIlllCa(M.Ms"N.Ns.Ns1.NII) :- demUll(Ms......). a1e::.ent(NI),N), optlafc:s(Ms.Ns,MaI,HsI).
..'mreo(lllUW).
10' Ull res«( apr(L .M,E-SlIMs],N,MsI,Nsl)
Iplilm res(L In pr(L,M..E.Sl[Msl,N,sm M,sm N,EqM,Eq N,Bi M,Bi N).
IOfIDlres(SmM'sn:lN,smMI,smNI). ~ortmrc.o(BiM.BiN.BiMl,.BiNI).
append(BIMJ.EqM.BiEqM). appeod(BiNI.EqN.BiEqN),
appcnd(BiEqM,5mMl.Msl). append(BiEqN,smNI.Nsl).
,
4
"
'5
Ackerman's funclion (non-curried)
faclorial
6 93 append
7 106 20 primes
& II' eratosthenes' sieve
9 307 unirication algorithm
10 317 e to 20 decimal places
Prl!&Wll ~ SKI ~ ~
1 26 22 30 -36
1 36 48 43 +10
3 49 70 70 0
,4 .$1 76
91
62
99
+5
-9
6
7
"
93
106
130
160
11&
14'
+9
+9
8
9
II'
307
176
479
153 +13
+7
44'
10 317 639 435 +31
T'ble IV. Number of reductions.
fro,ram ~ W Sl;;. ~
I 26 65 90 -39
! 36 851 1235 -46
I 49 3300 3626 -10
4 51 1446 1897 -32
1 75 887 967 -9
6 93 199 168 +16
1 106 7463 6108 +19
8 1/5 8337 7728 +8
9 307 2787 3363 -21
IG 317 27220' 177712 +35
[Landin66) p.J.Landin. The N~t 700 Prolnmmlnl Lanlul.et. Cornmunlcatioru of the "eM,
9(l). 157-16t. March 1966.
[Turner79b] DA.Turntr, Another Allorithm for Brackd Ab'trlcUon. Journal of Symbolic LOlk,
44(2). June 1979.
[PAL rd]