A Call-By-Need Lambda Calculus
Total Page:16
File Type:pdf, Size:1020Kb
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 parameter 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 functional programming 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 function 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 ecic 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 relation 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 kind 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 addition 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