Strictness analysis of the untyped λ-calculus

Strictness analysis of the untyped λ-calculus

JnformatitinTrocessing Letters 28 (19810)121-125 North-Holland 4 July 1988 Prateek MlISHRA Department of Computer Science, 7?reState University of N...

810KB Sizes 3 Downloads 86 Views

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 rantypedlambda-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 benefits, 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 result has generated widespread interest within the functional programming community [2,7]. Considerable effort has been invested in extending 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. 020-0190/88/$3.50

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 concerned 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 euahuation [12], that approximates lazy evaluation. A definition of cstrictness’ 5 provided that appropriately generalizes that for first order. We show that this characterizes precisely the safe replacement of head evaluation by an ‘eager’ form

@ 1988, Elsevier Science Publishers B.V. (North-Holland)

121

Volume 28, Number 3

the analog Of call-byreplacement in first order. fn contrast to the first-order case, this is not trivial: For strict M, thlacai;er cwdu;a*Li~m of N in apphcation 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, reduction 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~~efunction used in the literature, it appears that the strictness analysis methods cited above require such a dialect of the lambda-calculus. Further discussion on these and other matters nay be found in [S].

of head evaluation, ~~~/~~-~y-v~~e

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 associates to the left. An occurrence of a v&ridble 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 variablesoccurringin 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

4 July 1988

INFORMATION PROCESSING LETTERS

for the substitution of the term N for free occurrences 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 normalform 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),

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 + IV9then 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~~~ in which case (Xz. ~~~~~

Table 1 Numer~s:(O,f,2,...,n,,..f Elooleans: ( jr, F ) succ if-then-else fix

~~.~~.f~~ hx.Ahy.x hx.Xy.y

hz.Xf.Ax.(z f )f x h p.Xx.X y.p x y

xf.f(CXx.f(xx))dhx,f(xx))

Volume 28, Number 3

INFORMATION PROCESSING LETTERS

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 contraction 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 evaluation is sometimes loosely identified with normalorder 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, t!J’[Xx.M] and &‘[MN], for any N.

then so does

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

4 July 1988

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 f’i.rst-or d er case, lvlycroft has used monotone Boolean formulas to express strictness properties. The strictness properties of the conditional 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-strictness 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 converges, and &‘[MN,N, . . . N, *i 8[ MN, . . . &‘[ N,]. . . Nk].

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 contractions. The proof is standard (see [l, Lemma 11.4.61). Part (2) states that a head reduction step cannot be replaced by a sequence of internal redex contractions alone. This result can be straightforwardly derived from the proof of [l, Lemma 11.4.51). 0

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 introduces 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. 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.6. Lemma. Suppose M and N are cioscd terms, d’[ M] converges and M Bi N; then 8[ N ] converges and d[M] ai E[N]. 123

rNF~~Tr~N

V&me 28,Number3 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

Mj*k

Nks

By part (2) of Lemma 2.2, JMicannot 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 . . Np 1 converges, Clearly, 4’[Nk] must converge since otherwise, by the ( k }strictness of M, ~~~N~N~ *. _NJ must diverge. l

NUW, kmJ$v~...

N*=y

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.

=* >hI[M].

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

4 July1988

PROCESSING LETTERS

MN,...b[N,I...N*

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

Proof. For all iVIN, . . . I’$, and Nk such t at &[I$] diverges, by Lemma 2J, a[ MN, N2.. .h J must diverge. Hence, M is (k }-strict. To see that M cannot be ( j}-strict for j + k, choose N,, N,,..., NP with Nj divergent and Nk = xy1 ..= ~;n+~.y,,~_+~.Then, &[MN,N, . . . NJ = h ym+laym+1 and hence doe& not diverge. EI 2.9. Lemma. SuppcareM is a conwergentclused termt M is ( k >-strictiff 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,] converge. As IMN,Nz*.mNp>>i (i4”[MfN,N~ms*$9 by Lemma 2.2 we can find & such that MNN 1 2*.* Nk*h fiBi

~[~[MJN~~~**.

Np].

B[MN~N, ._ No]

As ~~~~~]N~~~ t e. NJ is in head normal form, so is &k ai B I MN,... le[N,]... NP]. •I For the second case, let &[ MN, N, . . * N,] converge, As MN&, ..- Np>i tQM]N,N,... Np, by a 2.7. Examp!e. M= xx.xx, Iv= ~~X.X~~~~.~~“Y~~ direct app~cation of Lemma 2,6 we have that is a case where A/I is (I )-strict, and &[MN] and &[&[M]iVp&* N,] must converge. ci ~~~[~[Nlll are unequal, as B[N ] = fi = k.hy.yx, d[MN] = xy.yN, and Bf Ml?] = As examples, consider xx. xft P=fsix(Xf.Xx.x(fx))

and

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(~ ai124

~~~~e~fg~~.f(~~)~ wkh are (~]mst~ct and (2>-strict respectively.

Volume 28, Number 3

INFORMATION

PROCESSING LETTERS

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 simply 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 strictness 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. Xy. x), we could reasonably claim that it was also strict, jointly, in its second and third arguments. Though previous work has not made this distinction, 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.

4 July 1988

References 111 H.P. Barendregt, The Lambda Calculus: Its Syntax and Semantics (North-Holland, Amsterdam, 1981). PI G.L. Burn, C.L. Hankin and S. Abramsky, The Theory and Practice of Strictness Analysis for Higher-Or&r Functions, Res. Rept. 85/6, Dept. of Computing, Imperial

College, London, U.K., 1985. [31 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. VI J. Fairbairn, Design and Implementation of a Simple Typed Language Based on the Lamb&-Calculus, Cambridge University Tech. Rept. No. 75, May 1985. PI 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.

bl P. Hudak and J. Young, Higher-order strictness analysis in untyped lambda cakuhts, Principles of Programming Languages Conf XIII, FL (January 1986) 98-109. PI T. Johnsson, Efficient compilation of Iazy evaluation, SIGPLA N Proc, on Compiler Construction (1984) 101-112.

PI 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-expresPI sions, Workshop on Programs as Data Objects, Lecture Notes in Computer Science, Vol. 217 (Springer, Berlin, 1985) 136-155. WI 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. WI J.E. Stoy, The Scott -Strachey Approach to Programming Language Theory (MIT Press, 1977). WI 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. P31 S.C. Wray, A new strictness detection algorithm, Unpublished manuscript, Cambridge University, January 1985.

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

125