<<

A CallByNeed Lamb da Calculus

Zena M Ariola Matthias Fel leisen

Computer Information Science Department Department of Computer Science

University of Oregon Rice University

Eugene Oregon Houston Texas

John Maraist and Martin Odersky Philip Wad ler

Institut fur Programmstrukturen Department of Computing Science

Universitat Karlsruhe UniversityofGlasgow

Karlsruhe Germany Glasgow Scotland

Abstract arguments value when it is rst evaluated More pre

cisely lazy languages only reduce an argument if the

The mismatchbetween the op erational semantics of the

value of the corresp onding formal is needed

lamb da calculus and the actual b ehavior of implemen

for the evaluation of the pro cedure b o dy Moreover af

tations is a ma jor obstacle for compiler writers They

ter reducing the argument the evaluator will remember

cannot explain the b ehavior of their evaluator in terms

the resulting value for future references to that formal

of source level syntax and they cannot easily com

parameter This technique of evaluating pro cedure pa

pare distinct implementations of dierent lazy strate

rameters is called cal lbyneed or lazy evaluation

gies In this pap er we derive an equational characteri

A simple observation justies callbyneed the re

zation of callbyneed and prove it correct with resp ect

sult of reducing an expression if any is indistinguish

to the original lamb da calculus The theory is a strictly

able from the expression itself in all p ossible contexts

smaller theory than the lamb da calculus Immediate

Some implementations attempt to exploit this observa

applications of the theory concern the correctness pro ofs

tion for sharing even more computations than just those

of a numb er of implementation strategies eg the call

of arguments Arvind et al provide an overview of

byneed continuation passing transformation and the re

such implementations particularly the socalled fully

alization of sharing via assignments

lazy or graphbased techniques

Unfortunately the mismatchbetween the op erational

semantics of the lamb da calculus and the actual b ehav

Intro duction

ior of the implementation is a ma jor obstacle for com

piler writers and users Sp ecically they cannot use the

Lazy languages implementthe

calculus to reason ab out sharing in the evaluation of a

callbyname lamb da calculus Their syntax provides

program

syntactic sugar for terms their evaluators map closed

Purushothaman and Seaman and Launch

terms to values The semantics of lazy calls

bury recently recognized this problem and devel

is the famous axiom every call is equivalenttothe

op ed twoslightly dierent natural semantics of the

b o dy of the function with the formal parameter replaced

callbyneed parameter passing mechanism Roughly

by the actual argument The axiom gives rise to an

sp eaking the semantics use storepassing to describ e

equational theory the calculus which implementors

callbyneed in terms of the semantics of assignment

can use to reason ab out the outcome of programs

statements Due to the lowlevel nature of this ap

Taken literallythe axiom suggests that a pro ce

proach these semantics p ermit neither a simple ex

dure must evaluate the argument of a sp eci call for

planation of language implementations nor sourcelevel

each o ccurrence of the corresp onding formal parameter

reasoning ab out program b ehavior Worse given slightly

Realistic implementations of lazy functional languages

dierent sp ecications based on natural or similar se

avoid such computational overhead by memoizing the

mantic frameworks it is dicult if not imp ossible to

compare the intentions with resp ect to sharing in the



evaluators

In this pap er we write callbyneed rather than lazy to

avoid a name clash with the work of Abramsky whichde

scrib es callbyname reduction to values

Ironically this problem immediately showed up in the dif

and transitive closure of interpreted as an asymmetric Anumb er of researchers havestud

ied reductions that preserve sharing in calculi with ex

plicit substitutions esp ecially in relation to optimal re

fxM N M x N j M M g

duction strategies Having dierent aims the resulting

calculi are considerably more complex than those pre

The compatible closure of is written as the

name

sented here Closest to our treatmentisYoshidas weak

reexive and transitive closure of as

name name

lamb da calculus whichintro duces explicit environ

is the symmetric closure of or the con

name

name

ments similar to let constructs Her calculus subsumes

gruence relation generated by We will omit the

name

several of our reduction rules as structural equivalences

tag when wemaydosounambiguously

name

even though due to a dierent notion of observation

In an implementation of the calculus closed expres

reduction in this calculus is not equivalent to reduction

sions play the role of programs An execution of a pro

to a value

gram aims at returning an observable value in realistic

In this pap er we pursue a dierent approachtothe

languages Observable values are basic constants like



sp ecication of callbyneed Our formulation is en

numb ers and b o oleans If a programs result is a pro ce

tirely syntactic and formulates the sharing in a call

dure or a lazy tree most implementations only indicate

byneed evaluator as an equational theory of the source

whether or not the program has terminated and p ossi

language The theory is a strict subtheory of the call

bly what of higherorder result it returned Since

byname calculus The key technical contribution of

the pure theory only contains abstractions an evalu

the pap er is a pro of of equivalence b etween the callby

ator only determines whether a program terminates

name and the callbyneed evaluator In we

Given this preliminary idea of how implementations

prove that the calculus relates to the evaluator in pre

work we can use the calculus to dene a partial evalua

cisely the same manner as Plotkins callbyname and

tion function eval from programs or closed terms

name

callbyvalue calculi relate to their resp ectiveevaluators

to the singleton consisting of the tag closure

The next two sections present the basic ideas of the

callbyname and callbyneed calculi The fourth sec

eval M closure i  M xN

name

tion presents basic syntactic results such as conuence

and standardization results The fth and sixth sec That is the evaluation of a program returns the tag

tions are devoted to the correctness pro of the sixth closure if and only if the theory can prove the pro

section also describ es an interesting alternativeformu gram is equal to a value It is a seminal result due to

lation of callbyneed The last three sections briey Plotkin that the evaluation function of a typical imple

discuss extensions of the language with constants and mentation is also determined by the standard reduc

recursive declarations the relation of our work with tion relation Put dierently a correct implemen

natural semanticsbased approaches and applications tation of the evaluator can simply reduce the standard

of the calculus or leftmostoutermost redex of a program until the pro

gram b ecomes a value

Evaluation contexts are a convenientway of for

The CallbyName calculus

mulating the evaluation relation A program M

standardreduces to N written as M N i

In this section we briey review the callbyname cal

name

M E xP Qand N E P x Q As usual

n n

culus we assume a basic familiarity of the reader with

M N means M standard reduces to N via the

this material

name



transitivereexive closure of M N

Figure details the callbyname calculus The set

name name

means M standard reduces to N in zero or one step

of lamb da terms called is generated over an innite

n

M N means M standard reduces to N in n

set of variables The expression M x N denotes the

name

capturefree substitution of N for each free o ccurrence steps As b efore we will omit the tag when no

name

confusion arises The following characterization of the

of x in M The reduction theory asso ciated with the

callbyname evaluator is a consequence of the conu

calculus is the result of taking the compatible reexive

ence prop erty and the standardization theorem of 

ferences b etween the formulations of PurushotomanSeaman and

Launchbury

In fact this approach unies the similar simultaneous and Prop osition For a program M

indep endentwork of two separate groups While many of our re

sults are indeed quite similar there are interesting and signicant

eval M closure i M xN

name

dierences in overall p ersp ectives in sp ecic denitions of calculi

and in pro of techniques We do not describ e these dierences

here and instead refer the interested reader to the full technical

This result is due to Plotkin

rep orts of Ariola and Felleisen and of Maraist Odersky and

Wadler for details

Syntactic Domains

Variables x y z

Values V W xM

Terms L M N x j V j MN

Evaluation contexts E j E M

n n

Axioms

xM N M x N

Figure The callbyname lamb da calculus

Syntactic Domains

Variables x y z

Values V W xM

Answers A V j let x M in A

Terms L M N x j V j MNj let x M in N

Axioms

let I xM N let x N in M

let V let x V in C x let x V in C V

letC let x L in M N let x L in MN

let A let y let x L in M in N let x L in let y M in N

Figure The calculus

let

From CallbyName to CallbyNeed representing the reference with a letb ound name eg

xxxII let x II

We augment the term syntax of the classical calculus

in xx

with a letconstruct The underlying idea is to repre

sent a reference to an actual parameter in an instanti

where I z z



ated function graph byaletb ound identier Hence

The intention of this sharing of graphs is to prevent

sharing in a function graph will corresp ond to nam

the duplication of work by reducing the same expression

ing in a term Figure details the calculus over

let

more than once However once a shared expression

these augmented terms As in the callbyname calcu

is a value in this core calculus values are simply

lus we extract the related reduction theory and will fre

abstractions there is no harm in the duplication In

quently make reference to its compatible closure

fact the duplication then b ecomes necessary reduction

need

the reexive transitive closure thereof and

requires an actual abstraction not just a p ointer to one

need

nally the congruence which it generates omit

need

This dereferencing is expressed bytheletV rule which

ting tags when the meaning is clear We will also refer

substitutes the value b ound to a name for an o ccurrence

to reduction theories of individual axioms in the same

of that name

manner eg M N and use for example the ab

Unfortunatelysuch a mo deling of the sharing of val

letI

breviation M N to mean that M reduces

ues in the source level syntax means that lo oking only

letfVCAg

for simple values is no longer adequate A value may it

to N by let V and by either letV let A or let C

self contain shared terms so the simple abstraction may

As Wadsworth p ointed out one waytoavoid dupli

b e buried within one or more let bindings Consider

cating reductions is by replacing the abstracted vari

the expression

ables in function b o dies with r eferences to arguments

rather than with the arguments themselves The

f f I fI z w z w II

let I axiom mo dels the creation of suchalet binding by

which letI equates with

The idea of keeping pairs of pro cedures and their arguments

in the syntax of the language is due to work on explicit substi

let f let z II

tutions on graph rewriting and on adding state to the

calculus Only the latter two though exploited

in w z w

the idea of mo deling the sharing relation of the program heap

in fIfI

inside of the source language

Further reduction of this term clearly requires that an

abstraction b e substituted for the rst f in fIfI Were

we to obtain this abstraction bynavely applying some this manipulation is generalized bythelet C axiom

relaxed form of letV to the term as a whole say The letA and letC rules may b e viewed as allow

ing the scop e of a b ound identier to b e expanded over

let x let y M in V in C x

broader expressions Toavoid copying unreduced ex

let x let y M in V

pressions such an expansion is required when the argu

in C let y M in V

ment of a function contains a binding the letA rule

and when a expression which is itself a binding is ap

wewould lose sharing

letC rule plied to some argument the

It is precisely these two rules letA and let C which

let f let z II in w z w

allow us to do without a separate store Rather than

in fIfI

creating references to new unique global addresses we

let f let z II in w z w

simply extend the scop e of identiers as needed

in let z II in w z w I fI

Example xx xy y

The redex II and the work involved in reducing it have

b een duplicated

xx xy y

The solution is to reasso ciate the bindings Rather

than accepting Eq reasso ciation will allow the cur

let x y y let x y y

letI letV

rent letV axiom to apply without loss of sharing

in xx in z z x

let x y y let x y y

let f let z II in w z w

letI letV

in let z x in let z w w

in fIfI

in z in z

let z II

let x y y

in let f w z w

letV

in let z w w

in fIfI

in v v

let z II

in let f w z w

In the callbyname calculus the terms we identied

in w z w I fI

as observable results or answers were simply abstrac

tions However in this formulation of callbyneed the

The rearrangement of Eq is captured bytheletA

notion of answer must reect the p ossibility that such

axiom In the example even though f do es o ccur twice

an abstraction can b e under bindings The appropriate



II will b e contracted only once

denition given in Figure means that answers are a

Equation p oints out a second situation where letV

syntactic representation of closures

is inadequate in the presence of values under bindings



In the expression

Basic Syntactic Prop erties

let z II in w z w I

Following Plotkins technique we dene the callby

wewould like to asso ciate the abstraction w z w with

need evaluator as a partial relation eval from pro

need

the argument I but the two are not directly adjacentas

grams or closed terms to the singleton consisting of

required by let I Again wemust rearrange the term

the tag closure based on equality in our callbyneed

exp ecting an equality

calculus

let z II in w z w I

eval M closure i  M A

need

let

let z II in w z w I

Furthermore from the following result a program eval

But note that weavoid duplication only of argumentevalua

uates to closure if and only if it reduces to an answer in

tions In the program

the callbyneed calculus

f f I fIw IIw

the redex II in the argument will b e reduced twice Put dier

Theorem is conuent

let

ently our calculus captures neither full laziness as describ ed by

Wadsworth nor the sharing required by optimal calculus

M

interpreters However as observed by Arvind

Kathail and Pingali full laziness can always b e obtained by

let let

R

extracting the maximal free expressions of a function at compile

R

 

time

M M

Although we rejected the lib eralized letV rule whichpro

duced this particular expression it is p erfectly reasonable to ex

let let

R

p ect similar terms to app ear In fact any leftnesting of two R

N applications LM N could pro duce suchaterm

Pro of The system consisting of just letI is triv callbyname calculus but without the convenience of

ially conuent then by marked and weighted redexes as a niteness of developments theorem for all redex typ es

in Barendregt the remaining reductions let V let C Lemma

and let A are b oth weakly ChurchRosser and strongly

normalizing and thus ChurchRosser as well Since

Completeness of the CallbyNeed calcu

b oth subsystems commute the theorem follows from

lus

the Lemma of Hindley and Rosen Prop osition

Replacing the callbyname interpreter with the callby

need interpreter requires an equivalence pro of for the

This result do es not imply that all reduction sequences

twoevaluators

lead to an answer in We therefore intro duce the

let

notion of standardreduction whichalways reaches an

eval eval

need name

answer if there is one Figure details our notion of

standard reduction An expression is only reduced when

In this section we will show the completeness direction

it app ears in the hole of an evaluation context The rst

that is each result obtained with the callbyname inter

two pro ductions for evaluation contexts are those of the

preter is also pro duced by the callbyneed interpreter

callbyname calculus Since arguments to pro cedures

Formallywehave for any program M

are evaluated only when needed the standard reduc

eval M closure eval M closure

tion system p ostp ones the evaluation of the argument name need

and instead pro ceeds with the evaluation of the pro ce

Weintro duce an ordering b etween terms of and

dure b o dy Once the formal parameter of the pro cedure

Intuitively M N if M can b e obtained byun

let

app ears in the hole of an evaluation context and only

winding N that is N contains more sharing than M

then the value of the argument is needed to complete

In other words the ordering relation expresses

the evaluation In this case the standard reduction sys

whether the terms have homomorphic graphs For ex

tem evaluates the argument b efore substituting it into

ample the tree of M xxxx can b e homomor

the b o dy of the pro cedure

phically emb edded into the graph of N let y xx

Given a program M M standardreduces to

let

in yy

N written as M N iM E K and N E L

need

where K and L are a standard redex and its contractum

ely M N means M and N are related

resp ectiv @ @

need

via the transitivereexive closure of As usual

ed

ne λ

e will omit the tags from the arrows when the meaning w x λx

λ

text Verifying that the standard

is clear from the con x

is indeed a function from programs to relation x

x

aluation Context

programs relies on the usual Unique Ev x

Lemma This lemma states that there is a unique

whichshows that M N Notation given a term

partitioning of a nonanswer into an evaluation context

M let D ag M b e the corresp onding dag and

and a redex which implies that there is precisely one let

given a term N let T reeN b e the corresp onding

way to make progress in the evaluation

tree

Lemma Given a program M either M is

let

Denition For M andN M N

let

an answer or there exists a unique evaluation context

i there exists an T reeM

E and a standardredex N such that M E N

D ag N

Pro of By structural induction on M

If M N theneach callbyname evaluation of M

Theorem Given a program M

let can b e simulated in the callbyneed calculus byevalu

ating N The term obtained in the callbyneed calculus

eval M closure i A M A

need

is not necessarily greater in terms of the ab ove order

Pro of The pro of relies on two subsidiary results that

ing than the one obtained following the callbyname

all answers are in normal form and that a se

evaluation This is b ecause a onestep callbyneed re

need

quence of nonstandard reductions followed bya se

duction may corresp ond to multiple callbyname re

quence of standard reductions can b e transformed into

ductions For example the evaluation

an equivalent sequence of rst standard reductions then

M xxxy y z z

nonstandard reductions The pro of is somewhat rem

iniscent of Barendregts standardization pro of for the

M z z y y z z

name

Syntactic Domains

Values V xM

Answers A V j let x M in A

Evaluation Contexts E j EM j let x M in E j let x E in E x

Standard Reduction rules

let I xM N let x N in M

s

let V let x V in E x let x V in E V

s

x M in AN let C let x M in AN let

s

let A let x let y M in A in E x let y M in let x A in E x

s

Figure Standard callbyneed reduction

corresp onds to the following callbyneed evaluation Example

i The term let z xx in z is drawn as

M xxxy y z z

z

let x y y z z in xx

need

let x let y z z in y in xx N

λx

need

Obviously M N However there exists an M such

 x

that M M and M N

 

name

The name z asso ciated with the ro ot p ointer is

M M z z z z N



drawn outside the shaded area

name

ii We can also have nested b oxes eg the term

Since M M the last p oint shows how to reconstruct

a callbyname evaluation in the callbyneed calculus

let z let y let w xx

Hence the completeness direction follows

in w

If M N and M is a redex N is not necessarily

in y

a let I redex Supp ose

s

in z

M xxxx

is drawn as

let z let w xx in w in zz

N z

then M N yet N do es not contain a let I redex The

s y

example p oints out that our graph mo del of terms

let w

must b e able to express the letstructure of a term in

ewant to use it for

addition to its sharing structure if w λx

a correctness pro of

We solve this problem by enriching dags with b oxes

x

and lab eled edges D ag M is the decorated dag as

so ciated with an expression M Abox can b e thoughtof

as a rened version of a no de the lab el asso ciated with

where the path to the no de must follow the lab el

an edge is just a sequence of letb ound variable names

zyw and hit three walls

The lab el can b e thought of as a direction to b e followed

In contrast in the term

in order to get to a particular no de Each let induces

one b ox and each edge to the shared term is deco

let z xx

rated with the variable name We pictorially represent

in let y z

a term let x N in M byabox divided in two parts

in let w y

the upp er part corresp onds to M the unshaded area of

in w

abox and the lower part contains N the shaded area

drawn as

of a b ox Let us illustrate the extended dag notation

and terminology with a numberofexamples

Lemma

M if M N then

w i Given

let

let fCAg s

y

D ag M D ag N

z

ii Given M if M N then N M

let

let V s λ

x The language and notation for decorated dags is use

ful in proving the following three key lemmas All could ulated in plain termbased language but at the

x b e form

cost of intro ducing more technical details

the path to the no de must follow the lab el wyz

Lemma Given M N ifM N and

let

and it p enetrates three walls but leaves two encas

 

M E xP R then there exists P R andE

n

ings

 

such that N E xP R

let fVCAg

s

In our running example D ag N is

Pro of Let z b e the ro ot in T reeM ofthe redex

 

b eing evaluated Let z and z b e the corresp onding

no des in D ag N and D ag N resp ectivelyWe know



hofz points to a no de while in @ that the left branc

z z 

D ag N the path from z to the no de maycontain

some obstacles Thus weshowthatby using let A

s

V and let C we can remove all the obstacles from

w let

s s

e reason by induction on the number n of

λx that path W



names asso ciated with the path in D ag N fromz to

the no de

x



n This means that the path from z to the no de

is free of names but it still may p enetrate inter

vening walls With m walls we need m let C

In this decorated dag the path from the application

s

steps to move the walls and exp ose the redex

ro ot no de to the no de has lab el zw and it p ene

trates the wall of one b ox To exp ose the redex means

n By the induction hyp othesis we can remove n

that the function p ointer of the ro ot no de must p oint

names Nowweneedtoshowhow to eliminate the

to the no de directly In terms of our graphical lan

last one There are two cases

guage wemust eliminate the names z wtheinternal

The name asso ciated with the no de is w

box and pull the no de out of the shaded area exactly

the task of the let A let V and let C rules Their

s s

s ::

dagbased representation in Table reveals that let V

s w alue out of the shaded area eliminating a name

pulls a v λz

on an edge let C movesawall and let A moves a wall

s s

that is in the shaded area The sequence let V let A

s s

M

let V suces to exp ose the redex in our example

s

Since w o ccurs in head p osition an applica

let z let w xx in w in zz

tion of let V exp oses the no de

s

let z let w xx in xx in zz

let V

s

The branch lab eled w points to m boxes that

let w xx in let z xx in zz

surround the no de eg

let A

s

let w xx in let z xx in xxz

w

let V s

:: hable dags illustrates these

Figure without unreac λz

steps

From Table it is clear that let C and let A do s

s M

not change the dag asso ciated with a term while let V

s

causes a duplication

Since w o ccurs in head p osition m applica

tions of let A followed by a single application

s

of let V exp ose the no de s

let V s

E[x] E[x] x ... x ... x V V

V

let C s

@ @ N N : : : ::: λz λz

L L

x ... x x ... x

M M

let A s

E[x] E[x]

x ... x x ... x : : : : λz λz

L L y ... y y ... y

M M

Table Standard callbyneed reduction rules in dagbased form

@ @ @ @ z z z z z z λx z w λ x x λ λx λx x x

x x x

Figure Exp osing the redex in let z let w xx in w in zz

  n  

Let N N N under the assumption n Let M M and M M By

name name

let fVCAg

s



   M the induction hyp othesis N

let

that N C xP R for C notanevaluation con

 

text that is the redex xP R is not needed and

n   

N M M and M N N

name

need

hence z not the ro ot of the leftmostoutermost redex in

M wehaveacontradiction

From the Strip Lemma M



 

M M and M M

 

name name

Lemma Given M N ifM N and

let



M xP then there exists an answer A such that

If M M then wehave



N A

let fVAg

 n 

s

M N N and M

name

need

Pro of The pro of is similar to but simpler than the



previous one In fact we need not move the walls sur

where M N Otherwise by the induction

 

rounding the lamb dano de that is no use of let C is

hyp othesis N M

s



let

required

    

N N M M and M N



 

name

need

Lemma Given a program M and N if

let

n 

M N and M M then M N

let

name

such that

Lemma Given a program M

 n 

M M N N and M N

name

need

n

A M xN M

name

need

Pictorial ly

Pro of Since M M from Lemma M

n

N such that

let



M M M

n

M M and xN xN

name

need

where xN M The result then follows from

Lemma

n

need

N N

With these lemmas we can prove the main result of this

Pro of By induction on the length n of the reduction

section namely that callbyneed can simulate a call

n

M M

byname evaluation

name

n Let M E xP R and let z b e the ro ot in

n

Theorem If M and eval M closure

name

T reeM of the redex in the hole of E From

n

then eval M closure

need

 

Lemma there exists P R and E

Pro of The assumption implies M xN

name

   

Hence the result follo ws from Lemma

N N and N E xP R

need

 

Let z be the root in D ag N ofthe let Iredex

s

A LetLess Formulation of CallbyNeed

From Lemma and the fact that M do es not



contain any sharing wehave M N Thus

In the calculus wehave treated the expression

let

let x M in N as a term distinct from xN M An

M M E P x R

n

name alternate treatment is also quite reasonable that the

former is merely syntactic sugar for the latter In other

and

words it is p ossible to completely eliminate lets from



 

the callbyneed calculus and still have a system with

N N E let x R in P

let I

s

the same desired prop erties By expanding letbindings

into applications we can derivethe calculus shown

If there exists a no de z in T reeM where z z



in Figure from There is of course no analogue of

such that z z where is the homomor let



the letI rule in since wemust no longer convert away

phism asso ciated with the ordering M N then

from plain applications Wecalltheevaluator for this

M N Let F b e the set of all suchnodes

 

to distinguish it from the evaluator language ev al

Let M M M by reducing all redexes in

need

name

for 

let

F and their residuals Wehave M N

While is p erhaps somewhat less intuitive than

its simpler syntax can make some of the basic let

Syntactic Domains

Variables x y z

Values V W xM

Answers A V j xAM

Terms L M N x j V j MN

Evaluation contexts E j EM j xM E j xE xE

Reduction Axioms

V xC x V xC V V

C xLMN xLN M

A xLy M N y xLM N

Figure Letless callbyneed

syntactic results easier to derive It also allows b et Each of the inclusions of Theorem is prop er eg

ter comparison with the callbyname calculus since no

xxy y y xx

additional syntactic constructs are intro duced

Clearly and are closely related More pre

let

where stands for a nonterminating computation is

cisely the following theorem states that reduction in

an instance of rule A but it is not an equality in the

can b e simulated in and that the converse is

let

callbyvalue calculus stands for a nonterminating

also true provided we identify terms that are equal up

computation On the other hand the following in

to letI intro duction

stance of is not an equalityin



Prop osition For al l M M

let

xx

M M N N

However one can show that the observational equiv

alence theories of and are identical and are in

letI letI letI letI

compatible with the observational equivalence theory

of

V

   

M N M N

let let

Theorem For al l programs M

Prop osition can b e used to derive the essential syn

eval M eval M

name

tactic prop erties of from those of in particular

need

let

the conuence result for follows from Theorem

Pro of Follows from Theorem and

by the prop osition

has close relations to b oth the callbyvalue cal

Theorem implies that any mo del of callbyname

culus and the callbyname calculus Its notion of

V

calculus is also a mo del of since it validates all

equality ie the least equivalence relation gen

equalities in Theorem implies that any ade

erated by the reduction relation ts b etween those

quate resp ectively fullyabstract mo del of is also

of the other two calculi making an extension of

V

adequate fullyabstract for since the observational

and an extension of

equivalence theories of b oth calculi are the same

Theorem

V

Corollary For al l terms M N

Pro of V can b e expressed by a sequence of

reductions as was shown at the b eginning of this section

M N i M N

name need

Therefore Each reduction rule is an

V

equalityin For instance in the case of V one has

Extensions

xC x V VxC x

Most lazy functional languages extend the pure calculus

VxC V

in several ways In this section we consider twosuch

xC V V

extensions for constructors and for recursion

The other rules have equally simple translations and so

For instance Abramsky and Ongs mo del of the lazy lambda

calculus is adequate for

wehave

Constructors and Primitive Op erators

hi M hi V

n Id

Figure extends with data constructors k of ar

hx M i x hx V i V

let

bitrary arity n and primitive op erators p of whichse

lectors are a sp ecial case There is one new form of

n

Abs

value k V V where the comp onents V through

n

hi x N hi x N

V must b e values otherwise sharing would b e lost

n

when copying the comp ound value For instance

hi L hi x N

 

inl is not a legal value since copying it would

hx M i x xN hi V

App

also copy the unevaluated term Instead one

hi LM hi V

writes

let x in inl x

Figure Op erational semantics

There are two new reduction rules Rule V is the

usual rewrite rule for primitive op erator application It

This extended callbyneed calculus corresp onds to

is dened in terms of a partial function also called

Ariola and Klops callbyname calculus with cycles

from op erators and values to terms This function

in the same way that our callbyneed calculus corre

can b e arbitraryaslongasitdoesnotlookinside

sp onds to the callbyname calculus The correctness

lamb da abstractions That is we p ostulate that for

pro of of the calculus with recursion can b e obtained by

all op erators p and contexts C there is a context D

showing its soundness and completeness with resp ect to

such that for all terms M p C xM D xM or

a calculus of innitary graphs

p C xM is undened Note that rule V makes

all primitive op erators unary and strict Op erators of

Relation to Natural Semantics

more than one argumentcanstillbesimulated by curry

ing Rule A allows let bindings of op erator arguments

This section presents an op erational semantics for call

to b e pulled out of applications of primitive op erators

byneed in the natural semantics style of Plotkin and

Alternatively one could phrase these constructs in

Kahn similar to one given by Launchbury We

terms of constructors and case statements in reduction

state a prop osition that relates the natural semantics

rules

to standard reduction

A heap abstracts the state of the store at a p oint

Recursion

in the computation It consists of a sequence of pairs

binding variables to terms

A deciency of our treatment of callbyneed is its treat

ment of recursive or cyclic values Traditionally one re

x M x M

n n

lies on the Y combinator for recursion In the absence of

data constructors this solution is ne However once

The order of the sequence of bindings is signicant

data constructors are included the sharing in the source

all free variables of a term must b e b ound to the left

language no longer reects the sharing in the evaluator

of it Furthermore all variables b ound by the heap

For example the term

must b e distinct Thus the heap ab oveiswellformed

if fvM fx x g for each i in the range

i i

M Y y cons y

i n and all the x are distinct Let range

i

over heaps If is the heap x M x M

evaluates to a term containing two distinct cons cells

n n

dene vars fx x g A conguration pairs a

even though an actual implementation would allo cate

n

heap with a term where the free variables of the term

only one cell representing M as a cyclic structure

are b ound by the heap Thus hi M is wellformed if

To cop e with recursion we extend the callbyneed

is wellformed and fvM vars The op eration of

calculus with a letrec construct where no ordering

evaluation takes congurations into congurations The

among the bindings is assumed This extended calculus

term of the nal conguration is always a value Thus

is given in Figure Unlike the calculus of Sec

let

evaluation judgments taketheformhi M hi V

tion wenowhave a restricted notion of substitution

The rules dening evaluation are given in Figure

In other words substitutions only o ccur when a variable

There are three rules for identiers abstractions and

app ears in the hole of an evaluation context Otherwise

applications

an unrestricted notion of substitution in the presence of

cycles would cause interesting nonconuence phenom

So this mo del of the heap is incompatible with the extension

for recursion given in Section see the end of this discussion ena

Syntactic Domains

Op erators p

n

Constructors k of arity n

n

Values V W x j xM j k V V n

n

Terms L M N V j MNj let x M in N j p

Additional Axioms

V pV p V f V dened

A p let x M in N let x M in pN

Figure Data constructors and primitive op erations

Syntactic Domains

Values V x j xM

Terms M N x j V j MN jhM j x N x N i

n n

Evaluation contexts E j EM jhE j D ijhE x j D x x x E Di

n n

D x x x E x x E x x E x

n  n n

Axioms

xM N hM j x N i

need

lif t hV j D i N hVN j D i

der ef hE x j x V Di hE V j x V Di

der ef hE x j D x x x V Di hE x j D x V x V Di

i n n n

assoc hhV j D ijD i hV j D D i

 

x hV j D iD i hE x j D x x x V D D i assoc hE x j D x x

n n n n i

Figure Recursion

may b e added by App The free variables of V Abstractions are trivial As abstractions are al

are b ound by so to ensure the heap stays well ready values the heap is left unchanged and the

formed the nal heap has the form x V abstraction is returned

As one would exp ect evaluation uses only well

Applications are straightforward Evaluate the

formed congurations and evaluation only extends the

function to yield a lamb da abstraction extend the

heap

heap so that the the b ound variable of the ab

straction is b ound to the argument then evaluate

Lemma Given an evaluation tree with root cong



the b o dy of the abstraction In this rule x is a

uration hi M hi V ifhi M is wel lformed then

new name not app earing in or N The renam

every conguration in the tree is wel lformed and fur

ing guarantees that eachidentier in the heap is

thermore vars vars

unique

Thanks to the care taken to preserve the ordering

Variables are more subtle The basic idea is

of heaps it is p ossible to draw a close corresp ondence

straightforward nd the term b ound to the vari

between evaluation and standard reductions If is the

able in the heap evaluate the term then up date

heap x M x M write let in N for the

n n

the heap to bind the variable to the resulting value

term let x M in let x M in N Every answer

n n

But some care is required to ensure that the heap

A can b e written let in V for some heap and value

remains wellformed The original heap is parti

V Then a simple induction on derivations yields the

tioned into x M Since the heap is well

following result

formed only is required to evaluate M Evalu

ation yields a new heap and value V The new

Prop osition hi M hi V i

heap will dier from the old heap in twoways

let in M let in V

y b e up dated byVar and bindings binding ma

need

lus can also mo del garbage collection Indeed garbage The semantics given here is similar to that presented

collection can b e easily expressed in our callbyneed by Launchbury An advantage of our semantics

calculus by adapting the garbage collection rule for ref over Launchburys is that the form of terms is stan

erence cells of Felleisen and Hieb dard and care is taken to preserve ordering in the heap

Launchbury uses a nonstandard syntax in order to

let x M in N N if x FV N

achieve a closer corresp ondence b etween terms and eval

uations in an application the argument to a term must

We exp ect that the work on garbage collection in func

be a variable and all b ound variables must b e uniquely

tional languages by Morrisett et al will apply to

named Here general application is supp orted directly

callbyneed languages Such a rigorous treatmentof

and all renaming o ccurs as part of the application rule

garbage collection would strengthen the calculus and

It is interesting to note that Launchbury presents an

its utility for reasoning ab out the implementations of

alternative formulation quite similar to ours buried in

lazy languages

one of his pro ofs

One advantageofLaunchburys semantics over ours

Conclusion

is that his cop es more neatly with recursion bythe

use of multiple recursive let bindings In particular

The calculus wehave presented here has several nice

our heap structure is incompatible with the extension

prop erties whic hmake it suitable as a reasoning to ol

for recursion of Section This extension would al

for lazy functional programs With op erations on the

ter b oth the ordering prop erty and the connection to

lamb daterms themselves or p erhaps a mildly sugared

standard reduction

version rather than on a separate store of bindings

and with a small set of straightforward rules we feel

Applications

that our approach is clearer and simpler than previous

approaches The unsugared calculus ts naturally b e

Callbyneed calculi haveanumberofpotential applica

tween the callbyname and callbyvalue versions of

tions Their primary purp ose is as a reasoning to ol for

the implementation of lazy languages Wesketch three

Acknowledgements The authors would liketo

ideas

thank H Barendregt J JW Klop and D N

Turner for numerous helpful discussions

Callbyneed and assignment

Callbyneed can b e implemented using assignments

References

Crank briey discusses a rewriting semantics of

callbyneed based on Felleisen and Hiebs calculus

M Abadi L Cardelli PL Curien and JJ Levy

with assignments Webelieve that a callbyneed

Explicit substitutions Journal of Functional Pro

calculus is the correct basis for proving this implementa

gramming

tion technique correct with a simple simulation theorem

S Abramsky The lazy lamb da calculus In

for the resp ective standard reductions

D Turner editor Declarative Programming

AddisonWesley

Callbyneed and cps conversion

Z M Ariola and Arvind Prop erties of a rst

Okasaki et al recently suggested a continuation

order functional language with sharing Theoretical

passing transformation for callbyneed languages In

Computer ScienceSeptember

principle this transformation should satisfy the same

Z M Ariola and M Felleisen The callbyneed theorems as the continuationpassing transformation for

lamb da calculus Technical Rep ort CISTR callbyname and callbyvalue calculi Plotkins

Department of computer science Universityof pro of techniques should immediately apply Since this

Oregon Octob er transformation app ears to b e used in several implemen

tations of lazy languages it is imp ortant to explore its

Z M Ariola and J W Klop Cyclic lamb da graph

prop erties with standard to ols

rewriting In Proc of the Eighth IEEE Symposium

on in Computer Science Paris

Garbage collection

Z M Ariola and J W Klop Equational term

Mo deling the sharing relationship of an evaluators

graph rewriting Acta Informatica

memory in the source syntax suggests that the calcu

Arvind V Kathail and K Pingali Sharing of L Maranget Optimal derivations in weak lamb da

computation in functional language implementa calculi and in orthogonal term rewriting systems

tions In Proc International Workshop on High In Proc ACM Conference on Principles of Pro

Level Computer Architecture gramming Languages Orlando FloridaJanuary

H P Barendregt The Its Syn

tax and Semantics NorthHolland Amsterdam I A Mason and C Talcott Reasoning ab out

programs with eects In Proc of Programming

Language Implementation and Logic Programming

E Crank Parameterpassing and the lambda cal

SpringerVerlag LNCS Berlin

culus Masters thesis Rice University

I A Mason and C L Talcott Equivalence in func

E Crank and M Felleisen Parameterpassing and

tional languages with eects Journal of Functional

the lamb da calculus In Proc ACM Conferenceon

Programming

Principles of Programming Languages

G Morrisett M Felleisen and R Harp er Mo d

M Felleisen and D PFriedman Control op era

eling memory management Technical rep ort De

tors the SECDmachine and the lamb dacalculus

partement of computer science Carnegie Mellon

In rd Working Conference on the Formal Descrip

University forthcoming

tion of Programming Concepts Ebb erup Den

COkasaki P Lee and T Tarditi Callbyneed mark August

and continuationpassing style In Lisp and Sym

M Felleisen and R Hieb The revised rep ort on the

bolic Computation

syntactic theories of sequential control and state

S L Peyton Jones A fullylazy lamb da lifter Theoretical Computer Science

in haskell SoftwarePractice and Experience

J Field On laziness and optimalityinlambda in

terpreters To ols for sp ecication and analysis In

G D Plotkin Callbyname callbyvalue and the Proc ACM Conference on Principles of Program

lamb da calculus Theoretical Computer Science ming Languages San Francisc o

D PFriedman and D S Wise Cons should

Purushothaman and J Seaman An adequate op er not evaluate its arguments In Proc International

ational semantics of sharing in lazy evaluation In ConferenceonAutomata Languages and Program

Proc th European Symposium on Programming ming

Springer Verlag LNCS

G Gonthier M Abadi and JJ Levy The ge

K H Rose Explicit cyclic substitutions In ometry of optimal lamb da reduction In Proc

rd International Workshop on Conditional Term ACM Conference on Principles of Programming

Rewriting Systems July Languages

J M Seaman AnOperational Semantics of Lazy V K Kathail Optimal Interpreters for Lambda

Evaluation for Analysis PhD thesis The Pennsyl calculus BasedFuntional Languages PhD thesis

vania State University Dept of Electrical Engineering and Computer Sci

ence MIT

C Wadsworth Semantics And Pragmatics Of The

LambdaCalculus PhD thesis UniversityofOx J Lamping An algorithm for optimal lambda

ford Septemb er calculus reduction In Proc ACM Conferenceon

Principles of Programming Languages San Fran

N Yoshida Optimal reduction in weakcalculus

cisco January

with shared environments In Proc ACM Confer

enceonFunctional Programming Languages and J Launchbury A natural semantics for lazy eval

Computer Architecture Copenhagen uation In Proc ACM Conference on Principles of

Programming Languages

J Maraist M Odersky and PWadler The call

byneed lamb da calculus unabridged Technical

Rep ort Universitat Karlsruhe Fakultat fur Informatik Octob er