5
JnformatitinTrocessing Letters 28 (19810) 121-125 North-Holland 4 July 1988 Prateek MlISHRA Department of Computer Science, 7?reState University of N~KJ York at Stony Brook, Stony Brook, NY 11?94-4400, II&& Communicated by G.R. An&_ A Receivexl24 August 1987 Revised 29 February 1988 We provide a precise characterization of the strictness properties of the rantyped lambda-calculus. Using the notion of head evaluation to model lazy evaluatioq we give a definition of strictness appropriate for the lambda-calculus. We establish that strictness is a necessary and sufficient condition for the ‘eager’ evaluation of function arguments. We describe an algorithm for computing strictness properties of convergent terms. We show that the natural classifkatioz~ of if -then- e&e as strict in its first and, jointly, strict in its second and third arguments is possible only in an applied lambda-calculus that either includes constants or has some form of t;lpe structure. Keyworu!r: Head ncrmal form, untyped h-calculus, strictness analysis I.. Introduction Strictness analysis deals with the question: given divergent term M, when is NM divergent? Answering this question can yield practical be- nefits, as observed by Mycroft [IO], who showed that, for first-order functional programs, the k th argument of a function Fcould be safely evaluated using call-by-value in place of call-by-name iff F was strict in its k th argttment. As call-by-value is cheaper to implement on most architectures, and may provide sites for parallel evaluation, this re- sult has generated widespread interest within the functional programming community [2,7]. Considerable effort has been invested in ex- tending Mycroft’s results to the strictness analysis of languages based on some dialect of the lambda-calculus [2,3,5,6,9,13]. However, this effort has almost exclusively been concerned with the development of algorithms for strictness analysis. Therefore, a number of fundamental, and truly * This research xas partly supported by the National Science Foundation under Grant No. CCR-8706973. basic, issues remain. The definition of strictness given for the first-order case [lo] has been widely used for the lambda-calculus. 1s this definition sensible for tLti lambda-calculus? This has not yet been shown. Some research appears to be con- cerned with the untyped lambda-calculus (e.g., [6,8]), while other work involves the typed lambda-calculus [2]. Is there a real distinction to be made? If so, which is the one appropriate to our needs? Without answers for these questions it is difficult to compare these different results. 5 deed, it is even difficult to formulate the strictness analysis problem without entering into details of particular algorithms. In this work we provide answers to the above questions. We devdop a precise characterization of the strictness analysis problem for the untyped lambda-calculus without constants. We identify a notion of program evaluation, called head euahua- tion [12], that approximates lazy evaluation. A definition of cstrictness’ 5 provided that ap- propriately generalizes that for first order. We show that this characterizes precisely the safe re- placement of head evaluation by an ‘eager’ form 020-0190/88/$3.50 @ 1988, Elsevier Science Publishers B.V. (North-Holland) 121

Strictness analysis of the untyped λ-calculus

Embed Size (px)

Citation preview

Jnformatitin Trocessing Letters 28 (19810) 121-125 North-Holland

4 July 1988

Prateek MlISHRA

Department of Computer Science, 7?re State University of N~KJ York at Stony Brook, Stony Brook, NY 11?94-4400, II&&

Communicated by G.R. An&_ A Receivexl24 August 1987 Revised 29 February 1988

We provide a precise characterization of the strictness properties of the rantyped lambda-calculus. Using the notion of head evaluation to model lazy evaluatioq we give a definition of strictness appropriate for the lambda-calculus. We establish that strictness is a necessary and sufficient condition for the ‘eager’ evaluation of function arguments. We describe an algorithm for computing strictness properties of convergent terms. We show that the natural classifkatioz~ of if -then- e&e as strict in its first and, jointly, strict in its second and third arguments is possible only in an applied lambda-calculus that either includes constants or has some form of t;lpe structure.

Keyworu!r: Head ncrmal form, untyped h-calculus, strictness analysis

I.. Introduction

Strictness analysis deals with the question: given divergent term M, when is NM divergent? Answering this question can yield practical be- nefits, as observed by Mycroft [IO], who showed that, for first-order functional programs, the k th argument of a function Fcould be safely evaluated using call-by-value in place of call-by-name iff F was strict in its k th argttment. As call-by-value is cheaper to implement on most architectures, and may provide sites for parallel evaluation, this re- sult has generated widespread interest within the functional programming community [2,7].

Considerable effort has been invested in ex- tending Mycroft’s results to the strictness analysis of languages based on some dialect of the lambda-calculus [2,3,5,6,9,13]. However, this effort has almost exclusively been concerned with the development of algorithms for strictness analysis. Therefore, a number of fundamental, and truly

* This research xas partly supported by the National Science Foundation under Grant No. CCR-8706973.

basic, issues remain. The definition of strictness given for the first-order case [lo] has been widely used for the lambda-calculus. 1s this definition sensible for tLti lambda-calculus? This has not yet been shown. Some research appears to be con- cerned with the untyped lambda-calculus (e.g., [6,8]), while other work involves the typed lambda-calculus [2]. Is there a real distinction to be made? If so, which is the one appropriate to our needs? Without answers for these questions it is difficult to compare these different results. 5 deed, it is even difficult to formulate the strictness analysis problem without entering into details of particular algorithms.

In this work we provide answers to the above questions. We devdop a precise characterization of the strictness analysis problem for the untyped lambda-calculus without constants. We identify a notion of program evaluation, called head euahua- tion [12], that approximates lazy evaluation. A definition of cstrictness’ 5 provided that ap- propriately generalizes that for first order. We show that this characterizes precisely the safe re- placement of head evaluation by an ‘eager’ form

020-0190/88/$3.50 @ 1988, Elsevier Science Publishers B.V. (North-Holland) 121

Volume 28, Number 3 INFORMATION PROCESSING LETTERS 4 July 1988

of head evaluation, the analog Of call-by- ~~~/~~-~y-v~~e replacement in first order. fn contrast to the first-order case, this is not trivial: For strict M, thla cai;er cwdu;a*Li~m of N in apphca- tion MN may yield a result that is somewhat more EXIUC~ thiln would be the ease for lazy M (I%=- pte 2.7 at the end of Section 2.1). The strictness properties of terms in the untyped calculus turn out to be elementary: Either a term is divergent or it is strict precisely in sohlfj; and only one, kth lament position. For convergent terms, reduc- tion to head normal form provides an algorithm for determining the value of k.

Given the above framework and results, it is easily shcwn that the strictness analysis problem of interest is that of the untyped lambda-calculus augmented with scme add~~cn~ structure. We show that the natural classification of the &then-else function as strict in its first and, jointly9 strict in its second and third arguments either assumes some form of type structure, as in Lazy ML 171, or requires the presence of constants and associated “delta’ reduction rules, as in Ponder f4]. As this is precisely the classification of the j~-~~e~-e~~e function used in the literature, it ap- pears that the strictness analysis methods cited above require such a dialect of the lambda-calcu- lus. Further discussion on these and other matters nay be found in [S].

Let V= (x, y, z,._ } be a mountable set of uM&e~. The language A of well-formed terms M is defined by the following grammar:

We use = fcr syntactic identity of terms and adopt the usual convention that application asso- ciates to the left. An occurrence of a v&rid ble x in a tern RI is free if it does not occur Ahin a subterm of M of the form hx.N, ar d is ~~~~~ otherwise. By .!V(M) we mean the set of free variables occurring in term M. A term M is closed if all occurrences of variables in it are bound; such terms are also called programs. We write M[N/xf

122

for the substitution of the term N for free occur- rences of x in M, with the usual renaming of bound variables of M to prevent capture cf free variables of IV.

A redex is a term of the form ~~~.~~~~~. The ~~~~~~~~~~ of a redex (Xx. ~~~~~ is the term ~~~~~/x]~ A term M ~rn~~ed~ate~ reduces to a term IV, written N > IV, if N can be obtained from M by replacing an occurrence of a redex as a subterm of M by its ~ontra~tum. Let a denote the reflexive, transitive &sure of > w If M s IV, then we say that M reduces to IV.

A term N is in head normal form if it has the form

If M is not in head normal form, it must have the form

M=Xx,... x~.(~~_M~)M~=._M~ (n, m>O),

in which case (Xz. ~~~~~ is called the head redex of M. Any redex of M which is not its head redex is called an ~~ter~a~ redex of 84. We write M >i IV if M immediately reduces to N by the contraction of an intem~ redex in M, and M >h N if M immediately reduces to N by contraction of the head redex in M. The contraction of internal redexes can never result in the creation, or the removal, of a head redex: if N + IV9 then M is in head nom& form iff IV is in head noes form.

Following standard practice [11], our language can represent the usual constants by closed terms in head normal form. Some representations are shown in Table 1.

An e~a~~tur is a partial recursive fun~~cn &’ : A --* A such that &C =*> . Given a term M wp say that lie[.M] diverges, and M is diuergen:, if a[ M] is undefined; otherwise, we say that &EM] converges~ and M is ~~~ve~ge~~~

Table 1

Numer~s:(O,f,2,...,n,,..f ~~.~~.f~~ Elooleans : ( jr, F ) hx.Ahy.x hx.Xy.y succ hz.Xf.Ax.(z f )f x if-then-else h p.Xx.X y.p x y fix xf.f(CXx.f(xx))dhx,f(xx))

Volume 28, Number 3 INFORMATION PROCESSING LETTERS 4 July 1988

The operational semantics of A is defined in terms of head evaluation, a theoretically pleasant notion that closely approximates lmy evaluation. 1

Head evaluation is defined as the repeated con- traction of the head redex. If evaluation succeeds, the evaluator yields a term in head normal form as the final result. Head evaluation models lazy evaluation successfully as it evaluates only those redexes- head redexes- that are ‘obviously’ needed to complete a computation. Lazy evalua- tion is sometimes loosely identified with normal- order evaluation; the essential difference between the two lies in that normal-order evaluation may require the contraction of non-head redexes as well. In what follows, we assume I to be a head evaluator and hence by divergent term mean a term that does not possess head normal form.

2.1. Lemma. I” d’[ Ad] diverges, then so does t!J’[Xx.M] and &‘[MN], for any N.

Proof. The proof is a standard procedure (see [l, Chap. 8.31). q

2.2. Lemma. (1) M >> N 3 3fi, such that M B,, fi >>i N.

(2) Further, if M >> N requires one or more head contractions, then

3iQ, A such that M >h d >>h A >>i N.

Proof. Part (1) states that a sequence of reductions can always be factored into a sequence of head redex contractions followed by internal redex con- tractions. The proof is standard (see [l, Lemma 11.4.61).

Part (2) states that a head reduction step can- not be replaced by a sequence of internal redex contractions alone. This result can be straightfor- wardly derived from the proof of [l, Lemma 11.4.51). 0

In practice, lazy evaluation differs from head evaluation in that redexes occurring within lambda-expressions are left unsimplified and contracted only when ‘needed’-after the outer lambda binding has been removed by application to an argument.

2.1. Strict terms are eagerly evaluable

We write S for any finite subset of the natural numbers.

2.3. Definition. Closed term M is S-strict if, whenever k >, max(S) and terms Ni, N2,. . . , Nk are such that Vi E S, &‘[NJ diverges, then QMN,N,... NJ diverges as well.

Observe that divergent terms are &strict, and that if M is S-strict, then M is S-strict whenever S E S. In th e i.rs -or f’ t d er case, lvlycroft has used monotone Boolean formulas to express strictness properties. The strictness properties of the condi- tional if (x1, x2, x3) would thus be written as xi A (x2 V x3). In our notation, we would say that it was (1)~strict and (2, 3)-s&t. Thus, S-strict- ness corresponds to Boolean formulas containing disjunctions only; however, conjunction can be modelled by listing S-strictness properties of a term.

2.4. Definition. Let p be a natural number. Closed term M is p-eagerly evaluable if, whenever given k >,p and terms N,, N2,. . . , Nk such that &‘[MN,hr,... NJ converges, then &‘[ N,] also con- verges, and &‘[MN,N, . . . N, *i 8[ MN, . . . &‘[ N,]. . . Nk].

If a term M is p-eagerly evaluable, we can unconditionally replace Np in MN, N2 . . . Nk by 8[ N,] and get results that are at least as good as those given by the original term. ‘At least as good means that the modified evaluation order intro- duces no additional risk of nontermination, and the results obtained will be at least as reduced as in the unmodified case.

25 Theorem. A term is ( k )-strict iff it is k-eagerly w&able.

We first give a lemma Aatiq head reducts of terms that differ only by the contraction of some internal redexes.

2.6. Lemma. Suppose M and N are cioscd terms, d’[ M] converges and M Bi N; then 8[ N ] con-

verges and d[M] ai E[N].

123

V&me 28, Number 3 rNF~~Tr~N PROCESSING LETTERS 4 July 1988

hf. TO show that &‘[hr] converges, we suppose the converse. Then, there must exist an infinite sequence of head reductions beginning with N,

N=Nb+JV*>h l *’ >hNk>h l **y

where each N# is not in head n~rmd form. AS &[M] converges, we also have the following finite sequence of head reductions beginning with 44:

MzSM*;4@2*>‘~ l =* >hI[M].

For any Nk, k > 0, by part (I) of Lemma 2.2 we can find some I$ where

MB, Mj*k Nks

By part (2) of Lemma 2.2, JMi cannot be M itself as at least one head reduction step is needed in going from M to Nk. Repeating this step we must ultimately find NP such that b[M J > i Np. AS &‘[M] is in head normal form, so must be NP and we are done. a

Proof of Theorem 2.5. Clearly, if M is k-eagerly evaluable, then it is (k)-strict.

Conversely, suppose M is (k}-strict. Let 4 be such that 8[ MNINz . l . Np 1 converges, Clearly, 4’[Nk] must converge since otherwise, by the ( k }- strictness of M, ~~~N~N~ *. _ NJ must diverge. NUW,

kmJ$v~... N*=y MN,...b[N,I...N*

and, by a direct application of Lemma 2.6, the required relations~p holds:

B[MN~N, ._ No]

ai B MN,... le[N,]... NP]. I •I

2.7. Examp!e. M= xx.xx, Iv= ~~X.X~~~~.~~“Y~~ is a case where A/I is (I )-strict, and &[MN] and ~~~[~[Nlll are unequal, as B[N ] = fi = k.hy.yx, d[MN] = xy.yN, and Bf Ml?] = xx. xft

A closed term M is &strict iff it is divergent; determining whether an arbitrary term is divergent is not even semi-decrdable [123. Thus, n(~ ai-

124

gorithm complete with respect to ~ef~tion 2.3 may be obtained. We are not, however, much interested in the strictness properties of divergent terms. Such terms do nut play a useful role in practical programs; frum a practical paint of view it is enough that our algorithm be complete for convergent terms.

Proof. For all iVIN, . . . I’$, and Nk such t diverges, by Lemma 2J, a[ MN, N2.. . h

at &[I$]

diverge. Hence, M is (k }-strict. J must

To see that M cannot be ( j}-strict for j + k, choose N,, N,,..., = xy1

NP with Nj divergent and Nk ..= ~;n+~.y,,~_+~. Then, &[MN,N, . . . NJ =

h ym + la ym + 1 and hence doe& not diverge. EI

2.9. Lemma. Suppcare M is a conwergent clused term t M is ( k >-strict iff isp[ M f is ( k >-strict.

Pm& We show, fur all Ni and p 2 k, that I[MN,N, .=. NJ converges iff &‘f&[ M]N, IV2 . _.

A$] converges, For the first case, iet &‘[&[M] NI N2.. . N,] con-

verge. As

IMN,Nz*.m Np>>i (i4”[MfN,N~ms* $9

by Lemma 2.2 we can find & such that

MNN 1 2*.* Nk*h fiBi ~[~[MJN~~~**. Np].

As ~~~~~]N~~~ t e.

so is &k NJ is in head normal form,

For the second case, let &[ MN, N, . . * N,] con- verge, As MN&, direct

..- Np>i tQM]N,N,... Np, by a app~cation of Lemma 2,6 we have that

&[&[M]iVp&* N,] must converge. ci

As examples, consider

P=fsix(Xf.Xx.x(fx))

and

~~~~e~fg~~.f(~~)~

wkh are (~]mst~ct and (2>-strict respectively.

Volume 28, Number 3 INFORMATION PROCESSING LETTERS 4 July 1988

Term Q is already in head normal form and E[ P] = hx.x( Px).

Lemma 2.9 has two important consequences. F&c ;t suggests an algorithm for computing the strictness properties of convergent terms. We sim- ply evaluate the term to its head normal form, which must exist. An examination of the term structure reveals the precisl- value of k for which the term is (k ]-strict. Second, Lemma 2.9 states that strictness properties of terms in the untyped lambda-calculus are essentially trivial: a term can only be ( k }-strict for some fixed k.

It should be evident that the discussion above does not capture many standard notions of strict- ness that have been discussed in the literature. For example, consider the usual representation of the if-then-else function:

Xp.Xx.A y.pxy.

By Definition 2.3, this term is only (l}-strict and, in the untyped case, clearly must be so. Tu see this, consider the case where p is bound to the term Az~.XZ&+~, which ‘throws away’ both x and y. However, if we were to restrict p to be bound only to Boolean values (AxA y. y, Ax. X y. x), we could reasonably claim that it was also strict, jointly, in its second and third arguments. Though previous work has not made this distinc- tion, it is now clear that to model the standard classification of if-then-else, as strict in its first argument and, jointly, strict in its second and third arguments, requires either the consideration of a typed dialect of the lambda-calculus cr the presence of constants with ‘delta’ reduction rules.

Acknowledgment

I would like to thank Tsung-Min Kuo, Esther Shilcrat and Eugene Stark for their many helpful comments on this work.

References

111

PI

[31

VI

PI

bl

PI

PI

PI

WI

WI

WI

P31

H.P. Barendregt, The Lambda Calculus: Its Syntax and Semantics (North-Holland, Amsterdam, 1981). G.L. Burn, C.L. Hankin and S. Abramsky, The Theory and Practice of Strictness Analysis for Higher-Or&r Func- tions, Res. Rept. 85/6, Dept. of Computing, Imperial College, London, U.K., 1985. C. Clack and S.L. Peyton-Jones, Strictness analysis--A practical approach, Syq. on Functional Programming Languages and Computer Architecture, Lecture Notes in Computer Science, VoI. 201 (Springer, Berlin, 1985) 35-49. J. Fairbairn, Design and Implementation of a Simple Typed Language Based on the Lamb&-Calculus, Cambridge Uni- versity Tech. Rept. No. 75, May 1985. C.L. Hankin, G.L. Burn and S.L. Peyton-Jones, A safe approach to parallel combinator reduction, European Symp. on Programming (1985) 59-62. P. Hudak and J. Young, Higher-order strictness analysis in untyped lambda cakuhts, Principles of Programming Languages Conf XIII, FL (January 1986) 98-109. T. Johnsson, Efficient compilation of Iazy evaluation, SIGPLA N Proc, on Compiler Construction (1984) 101-112. T.-M. Kuo and P. Mishra, On strictness and its analysis, Principles of Programming Languages Confi XIV (January 1987) 144-155. D. Maurer, Strictness computation using special A-expres- sions, Workshop on Programs as Data Objects, Lecture Notes in Computer Science, Vol. 217 (Springer, Berlin, 1985) 136-155. A. Mycroft, The theory and practice of transforming call-by-need into call-by-value, Lecture Notes in Computer Science, Vol. 83 (Springer, Berlin, 1980) 269-281. J.E. Stoy, The Scott -Strachey Approach to Programming Language Theory (MIT Press, 1977). C.P. Wadsworth, The relation between computational and denotational properties for Scott’s D,-models of the lambda-calculus, SIAM J. Comput. 5 (3) (1976) 488-521. S.C. Wray, A new strictness detection algorithm, Unpub- lished manuscript, Cambridge University, January 1985.

125