A Notation for Lambda Terms II: Re nements and ... - Semantic Scholar

1 downloads 0 Views 356KB Size Report
(The de nition that we present here adapts the one in NW90]. ... We outline in this section the `nameless' notation for lambda terms proposed by de Bruijn Bru72].
CS-1994-01

A Notation for Lambda Terms II: Re nements and Applications Gopalan Nadathur

Department of Computer Science Duke University Durham, North Carolina 27708{0129 January 1994

A Notation for Lambda Terms II: Re nements and Applications Gopalan Nadathur

Department of Computer Science Duke University, Durham, NC 27706 ([email protected])

Abstract Issues that are relevant to the representation of lambda terms in contexts where their intensions have to be manipulated are examined. The basis for such a representation is provided by the suspension notation for lambda terms that is described in a companion paper. This notation obviates -conversion in the comparison of terms by using the `nameless' scheme of de Bruijn and also permits a delaying of substitutions by including a class of terms that encode other terms together with substitutions to be performed on them. The suspension notation contains a mechanism for `merging' substitutions so that they can be e ected in a common structure traversal. The mechanism is cumbersome to implement in its full generality and a simpli cation to it is considered. In particular, the old merging operations are eliminated in favor of new ones that capture some of their functionality and that permit a simpli ed syntax for terms. The resulting notation is re ned by the addition of annotations to terms that serve to indicate whether or not they can be a ected by substitutions generated by external -contractions. These annotations permit substitutions to be performed trivially in certain situations, and can consequently lead to a conservation of space and time in a graph-based implementation of reduction. The use of the re ned notation in the comparison of terms is then examined. The notion of head normal forms is generalized to its context and shown to be useful in checking terms for equality and a graph-based procedure for nding head normal forms is presented. This procedure is accompanied by a complete proof of its correctness and the tools of analysis a orded by our notation are used in this process. The relevance of the considerations here to an ongoing implementation of the language Prolog is discussed.

1 Introduction We continue the investigations contained in [NW93] in this paper. The matter of general concern is that of a representation for lambda terms that is suitable in situations where these terms are used as data structures. Our interest in this issue arises primarily from implementation questions pertaining to Prolog, a logic programming language that employs the terms of a typed lambda calculus in a representational capacity [NM88]. However, there is a wider reason for interest in this issue: lambda terms provide a convenient means for representing objects whose structure incorporate This paper has been submitted for publication. Comments on its contents are welcome and may be sent to the author at the indicated address.



1

the notion of binding [Chu40, HL78, MN87, Pau87, PE88] and are, in fact, used for this purpose in a number of computer systems and programming languages that support the manipulation of formulas, programs, proofs and other similar objects [Bru80, CAB+ 86, CH88, GMW79, HHP93, MN87, Pau88, Pfe89]. The intended use of lambda terms places constraints on the kinds of representations that might be considered for them. The applications that are of interest to us here typically require operations that are based speci cally on the structures of lambda terms to be performed. A suitable representation must, therefore, make these structures readily available. At a more detailed level, it is often necessary to compare lambda terms ignoring di erences based on the names of bound variables. To cater to this need, the representation that is used must permit equality up to -convertibility to be determined easily. Finally, an operation of obvious importance in connection with lambda terms is -reduction; in the context of interest, this operation typically encodes the idea of making a substitution into some structure. A satisfactory representation must enable this operation to be performed eciently. In the preceding paper, we have described a notation for lambda terms, called the suspension notation, that provides a basis for meeting the requirements mentioned above. This notation is similar in spirit to those proposed in [ACCL90] and [Fie90] and that are based on the Categorical Combinators of Curien [Cur86]. It also shares features with data structures that have been used in implementing -reduction, most notably that in [AP81]. Speci cally, the suspension notation uses a scheme suggested by de Bruijn [Bru72] for eliminating variable names from lambda terms. Further, it incorporates a generalization of the notion of an environment that is used in implementations of functional programming languages as a mechanism for delaying substitutions. The ability to perform substitutions lazily, and, more importantly, the re ection of this ability into the notation, has several potential advantages. First, if substitutions can be delayed, then it may be possible to combine substitutions that arise from di erent -contractions. Such combinations reduce the traversals over the structures of terms and, thus, have eciency bene ts. Second, it may not be necessary in several instances to compute the e ects of substitutions in their entirety. The ability to carry out substitutions incrementally permits redundant work to be avoided in such cases. Finally, the explicit treatment of the substitution process in the notation makes it easy to describe a wide variety of reduction procedures as well as to verify the correctness of these procedures. We extend on the discussions in [NW93] here by describing a simpli cation and a subsequent re nement of the suspension notation. To facilitate the combination of (delayed) substitutions, the suspension notation includes a fairly general mechanism for `merging' environments. Implementing this mechanism in its full generality can be cumbersome. Towards coping with this problem, we rst simplify the suspension notation to a point where the explicit merging of environments is no longer possible. We then regain the ability to merge environments in circumstances of special interest by introducing new reduction rules that encode such mergings while remaining within the simpli ed notation. The re nement that we consider to the resulting notation consists of adding annotations to terms that indicate whether or not they can be a ected by substitutions generated by external -contractions. The rules for rewriting expressions are modi ed both to utilize and to preserve these annotations. At a qualitative level, the annotations are useful at least in that 2

they permit substitutions to be carried out trivially in certain situations. A ecting substitutions in this manner also has auxiliary bene ts: it can lead to a conservation of space and can also foster a greater sharing of work in a graph-based implementation of reduction that uses our notation. These bene ts can be signi cant in practice, e.g., in the implementation of Prolog [BR91]. This paper complements [NW93] in a second respect: it considers applications for the notation(s) developed. The suspension notation is intended to serve as a substrate upon which coarser-grained representations for lambda terms may be developed and shown to be correct. The use of this notation is, therefore, already manifest in the manner in which we justify our simpli cation of it. At a more concrete level, we consider the use of the notation developed here in the task of comparing lambda terms. When lambda terms are represented using the conventional notation, such a comparison can be based on the so-called head normal forms. The reduction of a lambda terms to a head normal form can be simulated using the new notation and some of the bene ts it provides in implementing this operation can be reaped in the comparison process. However, the value of laziness in substitution can be realized more fully if the comparison of lambda terms can be performed directly within the new notation. To facilitate this possibility, we `lift' the notion of a head normal form to the new notation and establish the relevant correspondence between this and the usual understanding of the notion. (The de nition that we present here adapts the one in [NW90].) We then describe a graph-based procedure for nding head normal forms for terms in the new notation. This procedure is presented in Standard ML and is similar to environmentbased interpreters like those in [HM76] and [Fie90]; it di ers from these mainly in that it nds head normal forms instead of weak head normal forms. It is also closely related to the simpli er presented in [AP81]. In connection with the latter, we observe some of the value of a notation such as ours: it facilitates a complete proof of correctness of our reduction procedure and also permits us to intermingle normalization operations with operations such as comparisons of terms. Some of these bene ts have also been noted in conjunction with the  -calculus and CCL in [ACCL90] and [Fie90]. The rest of this paper is structured as follows. The next two sections summarize notions pertaining to rewrite systems, the de Bruijn notation and the suspension notation that are relevant to this paper. This material is included to make the paper self-contained and may be skipped by an informed reader. In Section 4, we consider the elimination of the rules for merging environments from the suspension notation. We show here that these rules are redundant from the perspective of simulating -reduction. We observe that these rules are, nevertheless, useful in implementing this reduction operation and we present new rules that help recover some of their bene ts without altering the underlying relation of equality on terms. In Section 5 we present some auxiliary rules for simplifying expressions. The principal contribution of this section is a notion of closedness pertaining to terms and the justi cation of some reduction rules based on this notion. We incorporate the discussions of the previous two sections into the suspension notation in Section 6 and we observe properties of the resulting rewrite system such as its (weak) con uence and its correspondence to -reduction. In Section 7 we consider the use of our notation in comparing lambda terms. We generalize the notion of head normal forms to our notation here and show how it might be used in comparing lambda terms. We also present a procedure for head normalizing terms together with a 3

proof of its correctness. We conclude this paper in Section 8 with some remarks on the pertinence of our discussions here to the implementation of Prolog.

2 Preliminaries We outline in this section the `nameless' notation for lambda terms proposed by de Bruijn [Bru72] and describe notions pertaining to it that are used in subsequent discussions. The mechanism of rewrite systems is employed in presenting operations on terms in this and other notations and we therefore rst recapitulate the vocabulary that is relevant from this perspective.

2.1 Some terminology pertaining to rewrite systems The rewrite systems that we shall be concerned with in this paper are each speci ed by a set of rule schemata. A rule schema has the form l ! r where l and r are expression schemata referred to as the lefthand side and the righthand side of the rule schema, respectively. For example, the system we describe in Section 3 contains the schema [ (t1 t2 ); ol; nl; e] ! ([[t1 ; ol; nl; e] [ t2 ; ol; nl; e] ). In this schema, t1 , t2 , ol, nl and e represent metalanguage variables ranging over appropriately de ned categories of expressions. Particular rules may be obtained from this schema by choosing suitable instantiations for these variables. All our rule schemata satisfy the property that any syntactic variable appearing in the righthand side already appears in the lefthand side. Given a notion of subexpressions within the relevant expression language, a rule schema de nes a relation between expressions as follows: t1 is related to t2 by the rule schema if t2 is the result of replacing some subexpression s1 of t1 by s2 where s1 ! s2 is an instance of the schema. We refer to the relation so de ned as being generated by the rule schema. The relation generated by a collection of rule schemata is the union of the relations generated by each schema in the collection. Let > denote such a relation. We express the fact that t is related to s by virtue of > by writing t > s. We refer to occurrences in expressions of instances of the lefthand sides of the rule schemata generating > as >-redexes. The correspondence to a particular rule schema may also be expressed by referring to the subexpression as a redex occurrence of the schema. The re exive and transitive closure of > is denoted by > , a relation that is, once again, written in in x form. Intuitively, t > s signi es that t can be rewritten to s by a (possibly empty) sequence of applications of the relevant rule schemata. Accordingly, we refer to the relation > as a rewrite or reduction relation and we say that t >-reduces to s if t >s. Assume now that we have a xed but unspeci ed collection of expressions and that > is a rewrite relation on this collection. We refer to an expression t as a >-normal form just in case there is no expression s such that t >s. A >-normal form of an expression r is an expression t such that r >-reduces to t and t is a >-normal form. A collection of rewrite rule schemata is usually intended as a set of equality axioms in a given logical system. Using them to rewrite expressions is then useful if this helps in determining equality. The existence and uniqueness of normal forms 4

for every expression relative to the reduction relation generated by the rule schemata are matters of interest from this perspective: the equality of two expressions in the relevant sense can then be determined by reducing them to their normal forms and comparing these. Certain properties of reduction relations are pertinent to determining the existence and uniqueness of normal forms. The rewrite relation > is noetherian if and only if there is no in nite sequence of the form t1 > t2 >    > tn >    ; i.e., if and only if every sequence of rewritings relative to > terminates. If > is noetherian, a >-normal form must exist for every expression. The relation > is said to be con uent if, given any expressions t, s1 and s2 such that t >s1 and t > s2 , there must be some expression r such that s1 > r and s2 > r. The following proposition, whose proof is straightforward, explains the interest in this property:

Proposition 2.1 If > is a con uent reduction relation, then if a >-normal form exists for any

expression, it must be unique.

2.2 The de Bruijn notation for lambda terms We begin by identifying the class of de Bruijn terms. These terms are intended to correspond to lambda terms whose conventional syntax is given, for example, in [HS86]. De nition 2.2 The collection of de Bruijn terms, denoted by the syntactic category hDTermi, is given by the rule

hDTermi ::= hConsi j #hIndexi j (hDTermi hDTermi) j ( hDTermi) where hConsi is a category corresponding to a predetermined set of constant symbols and hIndexi is the category of positive numbers. A de Bruijn term of the form (i) #i is referred to as an index or a variable reference, (ii) ( t) is called an abstraction and is said to have t as its scope and (iii) (t t ) is referred to as an application. The subterm or subexpression relation on de Bruijn terms is given recursively as follows: Each term is a subterm of itself. If t is of the form ( t0), then each subterm of t0 is also a subterm of t. If t is of the form (t t ), then each subterm of t and of t is also a subterm of t. 1

2

1

2

1

2

The correspondence of de Bruijn terms to lambda terms in the conventional notation is explained as follows: Let the level of a subterm in a term be the number of abstractions in the term within which the subterm is embedded. Further, assume that there is a xed listing of the free variables with respect to which we can talk of the n-th free variable. Then, a variable reference #i occurring at level j in a term corresponds to a bound variable if j  i. Further, in this case, it represents a variable that is bound by the abstraction at level (j ? i) within which the variable reference occurs. In the case that i > j , the index #i represents a free variable, and, in fact, the (i ? j )-th free variable. Under the translation just outlined, lambda terms that are -convertible in the conventional notation correspond to the same de Bruijn term. As an example, the terms (x ((y (y x)) x)) and (z ((w (w z )) z )) both translate to the de Bruijn term ( (( (#1 #2)) #1)). Thus, the de Bruijn notation has the virtue of obviating -conversion in the comparison of terms. 5

The following generalized notion of substitution for de Bruijn terms is intrinsic to most operations on these terms. De nition 2.3 Let t be a de Bruijn term and let s1; s2; s3; : : : represent an in nite sequence of de Bruijn terms. Then the result of simultaneously substituting si for the i-th free variable in t for i  1 is denoted by S (t; s1; s2; s3; : : :) and is de ned recursively as follows: (1) S (c; s1; s2; s3; : : :) = c, for any constant c, (2) S (#i; s1; s2; s3; : : :) = si for any variable reference #i, (3) S ((t1 t2 ); s1; s2; s3; : : :) = (S (t1; s1; s2; s3; : : :) S (t2 ; s1; s2; s3; : : :)), and (4) S (( t); s1; s2; s3; : : :) = ( S (t; #1; s01; s02; s03 ; : : :)) where, for i  1, s0i = S (si ; #2; #3; #4; : : :). We shall use the expression S (t; s1; s2 ; s3; : : :) as a meta-notation for the term it denotes. The main complexity in the above de nition is in case (4). Towards understanding this case, we note that within a term of the form ( t), the rst free variable is, in fact, denoted by the index #2, the second by #3 and so on. This requires, in (4) above, that the indices for free variables in the terms s1 ; s2; s3; : : : being substituted into ( t) be `incremented' by 1 prior to substitution into t. Further, the index #1 must remain unchanged within t and it is the indices #2,#3,: : : that must be substituted for. De nition 2.4 The -contraction rule schema is the following (( t1) t2 ) ! S (t1 ; t2; #1; #2; : : :) where t1 and t2 are schema variables for de Bruijn terms. The relation (on de Bruijn terms) generated by this rule schema is denoted by > and is called -contraction. The corresponding reduction relation is also referred to as -reduction. There are certain commutativity properties of the substitution operation and -reduction that will be useful in later discussions. These are is stated in the following proposition that is evident from the discussions in [NW93].

Proposition 2.5 Let t ; t ; t ; : : : be de Bruijn terms. (a) If l ! r is a -contraction rule, then so is S (l; t ; t ; t ; : : :) ! S (r; t ; t ; t ; : : :). (b) If ti > t0i for i  0, then S (t ; t ; t ; t ; : : :) > S (t0 ; t0 ; t0 ; t0 ; : : :): 0

1

2

0

1

2

3

1

2

3

0

1

2

1

2

3

3

The following proposition, the Church-Rosser Theorem for -reduction, is proved (for de Bruijn terms) in [Bru72].

Proposition 2.6 The relation > is con uent. 6

We are interested in the comparison of de Bruijn terms in a situation where two terms are considered to be equal if one -contracts to the other. Propositions 2.1 and 2.6 provide the basis for a procedure for this purpose: we attempt to reduce the two terms to a > -normal form and, if we succeed in doing so, we compare the resulting terms for identity. The outlined procedure becomes a decision method in the case that > -normal forms exist for the given terms. Although this property does not hold for arbitrary de Bruijn terms, it might hold of subcollections that are of interest. It holds, for instance, of the class of (de Bruijn) terms restricted by a typing scheme based on simple types [And71, Chu40]. It is this restricted class of terms that underlies the Prolog language. In determining equality of terms, it is possible to interleave the reduction to > -normal form with the checking for identity. The notion of a head normal form is useful in this context. For technical reasons, we also identify a weaker form of this notion. De nition 2.7 A head normal form relative to > or a > -hnf is a de Bruijn term of the form ( : : : ( (: : : (h t1 ) : : : tn )) : : :) where h is either a constant or a variable reference. The number of abstractions at the front of such a term is referred to as its binder length, h is called its head, and the terms t1 ; : : :; tn are said to be its arguments; in particular instances, there may be no arguments and the binder length may be 0, i.e., the binder may be empty. A de Bruijn term is said to be a weak > -hnf if it is a > -hnf or if it is of the form ( t). We say t is a > -hnf of a de Bruijn term s if t is a > -hnf and, further, s > t. It is easily seen that two > -hnfs are equal in the sense being considered if and only if their binder lengths and heads are identical, they each have the same number of arguments and their arguments, taken pairwise, are equal. Further, a de Bruijn term has a > -normal form only if it has a > -hnf. Thus, in determining if two de Bruijn terms are equal, we may proceed by rst reducing them to > -hnf, checking the binder lengths, the heads and the number of arguments of the resulting terms for identity and then comparing the arguments of these terms if this is still relevant. A procedure for reducing de Bruijn terms to > -hnfs is, thus, of interest. There are certain kinds of reduction sequences that are guaranteed to produce such a form from a given term whenever one exists. The following de nition identi es a sequence of this kind. De nition 2.8 The head > -redex of a de Bruijn term that is not a > -hnf is identi ed as follows: If t is a > -redex, then it is its own head > -redex. Otherwise t must be of the form (t1 t2 ) or ( t1). In either case, the head > -redex of t is identical to that of t1 . The weak head > -redex of a de Bruijn term that is not a weak > -hnf is de ned similarly, except that the term in question cannot be an abstraction. The head > -reduction sequence of a de Bruijn term r is the nite or in nite sequence r = r0 ; r1; r2; : : :; rn; : : :, where, for i  0, there is a de Bruijn term succeeding ri if ri is not a > -hnf and, in this case, ri+1 is obtained from ri by rewriting the head > -redex using a -contraction rule. Such a sequence is obviously unique and terminates just in case there is an m  0 such that rm is a > -hnf. 7

The following proposition shows the importance of head > -reduction sequences. This proposition is proved for lambda terms in the conventional notation in, for instance, [Bar81] and this proof can be readily adapted to de Bruijn terms.

Proposition 2.9 A de Bruijn term t has a > -hnf if and only if the head > -reduction sequence of t terminates.

By virtue of Proposition 2.9, a nite collection of head > -reduction sequences can be used to obtain a > -normal form of a de Bruijn term whenever such a form exists.

3 The suspension notation Head > -reduction sequences permit -contractions to be performed lazily in determining the equality of terms. There are practical bene ts to permitting a laziness in the substitution operation inherent in -contraction as well. For example, consider the task of determining whether the two de Bruijn terms (( ( ( ((#3 #2) s)))) ( #1)) and (( ( ( ((#3 #1) t)))) ( #1)) are equal modulo the -contraction rule; s and t denote arbitrary de Bruijn terms here. It can be seen that they are not, simply by observing that these terms have as > -hnfs the terms ( ( (#2 s0 ))) and ( ( (#1 t0 ))) respectively, where s0 and t0 result from s and t by appropriate substitutions. This conclusion can, in fact, be reached without explicitly carrying out the potentially costly operation of substitution on the arguments. Along a di erent direction, laziness in substitution can be utilized to combine structure traversals needed in -reduction, leading to gains in eciency [AP81]. The suspension notation for lambda terms [NW93] re nes the de Bruijn notation so as to permit laziness in substitution to be realized. In essence, this notation enhances the category of de Bruijn terms by including one new class of terms: those that encode terms with a pending substitution. The idea used is similar in spirit to that underlying the notion of a closure. However, the suspension notation re ects this idea into the syntax of terms and also generalizes the notion of an environment to permit the propagation of substitutions inside abstractions as well as the rewriting of -redexes embedded in such contexts. We summarize the important aspects of this notation below, thereby providing a background to the discussion concerning its enhancement and use later in the paper.

3.1 The syntax of suspension expressions The collection of suspension expressions is identi ed in the de nition below. De nition 3.1 The categories of suspension terms, environments and environment terms, denoted by hSTermi, hEnv i and hETermi, are de ned by the following syntax rules:

hSTermi ::= hConsi j #hIndexi j (hSTermi hSTermi) j ( hSTermi) j [ hSTermi; hNati; hNati; hEnv i] 8

hEnvi ::= nil j hETermi :: hEnv i j ffhEnv i; hNati; hNati; hEnv igg hETermi ::= @hNati j (hSTermi; hNati) j hhhETermi; hNati; hNati; hEnviii. We assume that hConsi and hIndexi are as in De nition 2.2 and that hNati is the category of natural numbers. We refer to suspension terms, environments and environment terms collectively as suspension expressions. We observe that the class of suspension terms includes all the de Bruijn terms. Conversely, there is only one new form for terms in the suspension notation: [ hSTermi; hNati; hNati; hEnv i]. By an extension of terminology, we shall refer to suspension terms of the form #i, ( t) and (t1 t2 ) as indices or variable references, abstractions and applications, respectively. Terms of the new form are referred to as suspensions. An example of such a term is [ t1 ; 1; 0; (t2; 0) :: nil] , where t1 and t2 are de Bruijn terms. This term, which arises, for instance, from the ` -contraction' of the term (( t1) t2 ), represents the fact that t1 that was originally in the scope of 1 abstraction is now to be thought of as being in the scope of none and that t2 , originally in the scope of 0 abstractions, is to be substituted for the rst free variable in t1 . Viewed di erently, [ t1 ; 1; 0; (t2; 0) :: nil] encodes the term that results from substituting t2 for the rst free variable in t1 and decrementing the indices corresponding to the remaining free variables by 1. De nition 3.2 The immediate subexpression(s) of a suspension expression x are given as follows: (1) If x is a suspension term, then if (a) x is (t1 t2 ), these are t1 and t2 , (b) if x is ( t), this is t, and (c) if x is [ t; ol; nl; e] , these are t and e. (2) If x an environment, then (a) if x is et :: e, these are et and e, and (b) if x is ffe1 ; i; j; e2gg, these are e1 and e2 . (3) If x is an environment term, then (a) if x is (t; l), then this is t, and (b) if x is hhet; i; j; eii, then these are et and e. The subexpressions of a suspension expression are the expression itself and the subexpressions of its immediate subexpressions. We sometimes use the term subterm when the subexpression in question is a term. A proper subexpression of a suspension expression x is any subexpression distinct from x. Two suspension expressions are said to have the same top-level structure if they are both constants, variable references, abstractions, applications, or suspensions or if they are both of the forms nil, et :: e, ffe1; i; j; e2gg, @l, (t; l), or hhet; i; j; eii. If two suspension expressions that have the same top-level structure have any immediate subexpressions, then there is an obvious correspondence between these subexpressions. This correspondence will be utilized below. An environment appearing in a suspension determines bindings for a nite initial segment of the free variables. In its simplest form, such an environment `lists' the bindings. The more complex forms for environments allow for the `merging' of environments, thereby leading to the combination of substitutions. Part of the objective in this paper is to eliminate the complex forms 9

of environments while still permitting substitutions to be combined. It is, therefore, relevant to identify the simpler syntax for suspension expressions. De nition 3.3 A simple suspension expression is an expression that does not have subexpressions of the form hhet; j; k; eii or ffe1; j; k; e2gg. If the expression in question is a suspension term, an environment or an environment term, it may be referred to as a simple suspension term, a simple environment or a simple environment term, respectively. Note that a simple environment e is either nil or of the form et1 :: et2 :: : : : :: etn :: nil. In the latter case, for 1  i  n, we write e[i] to denote eti .

De nition 3.4 The length of an environment e, denoted by len(e), is given as follows: (a) if e is nil then len(e) = 0; (b) if e is et :: e0 then len(e) = len(e0 ) + 1; and (c) if e is ffe ; i; j; e gg then 1

len(e) = len(e1) + (len(e2) : i).

2

The symbol : used in the de nition above denotes the subtraction operation on natural numbers. To understand this de nition we note that, for a simple environment e, if len(e) = 0 then e must be nil and if len(e) = n > 0 then e must be of the form et1 :: et2 :: et3 :: : : : :: etn :: nil. Thus, this de nition is motivated by the view of a simple environment e as a list of length len(e). Simple environment terms are of the form (t; i) or @i. Expressions of the former kind represent a substitution of t for an appropriately determined free variable. Expressions of the latter kind represent a `dummy' binding, arising from the propagation of a substitution inside an abstraction. These expressions also encode the number of abstractions within whose scope the suspension term being substituted or the abstraction entered are to be thought to appear. This idea is made precise through the notion of an index that is de ned below for all environment terms and is extended, for technical reasons, to environments as well. De nition 3.5 The index of an environment term et, denoted by ind(et), and, for each natural number l, the l-th index of an environment e, denoted by indl (e), are de ned simultaneously by structural induction on expressions as follows: (i) If et is @m then ind(et) = m + 1. (ii) If et is (t0; m) then ind(et) = m.

(iii) If et is hhet0 ; j; k; eii, let m = (j : ind(et0)). Then ( indm(e) + (j : k) if len(e) > m ind(et) = ind (et0) otherwise. (iv) If e is nil then indl (e) = 0. (v) If e is et :: e0 then ind0(e) = ind(et) and indl+1 (e) = indl (e0). (vi) If e is ffe1 ; j; k; e2gg, let m = (j : indl (e1)) and l1 = len(e1). Then 10

8 > < indm (e2) + (j : k) if l < l1 and len(e2) > m indl(e) = > indl(e1) if l < l1 and len(e2)  m : ind if l  l1. (l?l1 +j ) (e2 ) The index of an environment, denoted by ind(e), is ind0 (e). We are eventually interested in only a subcollection of the suspension expressions that are referred to as the well formed ones. The wellformedness conditions that are presented below have the following properties: the class of suspension expressions that they deem to be well formed includes the de Bruijn terms and is closed under the rewrite relation that we shall describe on these expressions. De nition 3.6 A suspension expression is well formed if the following conditions hold of every subexpression s of the expression: (i) If s is of the form [ t; ol; nl; e] then len(e) = ol and ind(e)  nl. (ii) If s is of the form et :: e then ind(e)  ind(et). (iii) If s is of the form hhet; j; k; eii then len(e) = k and ind(et)  j . (iv) If s is of the form ffe1 ; j; k; e2gg then len(e2 ) = k and ind(e1)  j . Towards understanding De nition 3.6 at an intuitive level, consider a simple suspension term of the form [ t; ol; nl; e] . If this term is well formed, it must be the case that len(e) = ol, i.e., e must be of the form et1 :: et2 :: : : : :: etol :: nil. Further, for 1  i  ol, if eti is of the form @m then m < nl and if eti is of the form (t; m) then m  nl. Finally, it is easily seen that ind(etj )  ind(etk ) for j < k  n. The quali cation of wellformedness is henceforth implicitly assumed with respect to suspension expressions.

3.2 An ordering relation on suspension expressions In subsequent discussions we shall nd use for the well founded partial order on suspension expressions that is de ned in [NW93]. We therefore recall this relation here. Intrinsic to its de nition is the measure on suspension expressions presented below. We assume here that max represents the function on pairs of integers that picks the larger integer in the pair. De nition 3.7 The measures  on suspension expressions and  on suspension terms are de ned recursively in the table below:

11

exp

constant #i (t1 t2 ) ( t)

 (exp) 0 0

max( (t1);  (t2))  (t) [ t; ol; nl; e] (t) +  (e) nil 0 et :: e max( (et);  (e)) ffe1; nl; ol; e2gg (e1) + (e2) + 1 @l 0 (t; l) (t) hhet; nl; ol; eii (et) + (e) + 1

(exp)

1 1 max((t1 ); (t2)) + 1 (t) + 1 (t) +  (e) + 1 | | | | | |

The ordering relation of interest has the following character: It rst compares the `complexities' of two suspension expressions directly. If it cannot distinguish between them at this level, it compares, recursively, their subexpressions. De nition 3.8 Given two suspension expressions x1 and x2, we say x1 = x2 if either (x1) > (x2) or  (x1) =  (x2) and one of the following conditions hold: (1) x1 is #i and x2 is #j where i > j . (2) x1 is [ t1 ; ol1; nl1; e1] , x2 is [ t2 ; ol2; nl2; e2 ] and  (t1) >  (t2). (3) x1 is ffe1 ; nl; ol; e2gg, x2 is et :: e and x1 = e. (4) x1 and x2 have the same top-level structure, and each immediate subexpression of x1 is identical to the corresponding immediate subexpression of x2 except for one pair of immediate subexpressions x01 of x1 and x02 of x2 that are such that x01 = x02 . (5) x2 is an immediate subexpression of x1 . The relation  is the transitive closure of =. We write x1  x2 to signify that x1 = x2 or x1  x2 . The following proposition is proved in [NW93].

Proposition 3.9 The relation  is a well founded partial ordering relation on suspension expressions.

3.3 Reduction relations on suspension expressions

Three categories of rewrite rules on suspension expressions|the s -contraction rules, the reading rules and the merging rules|are presented through the schemata in Figures 1, 2 and 3, respectively. The s -contraction rules generate suspended substitutions corresponding to -contractions. The reading rules serve to propagate these substitutions and the merging rules permit the combination of 12

( s )

(( t1) t2 ) ! [ t1 ; 1; 0; (t2; 0) :: nil] Figure 1: The s -contraction rule schema

(r1)

[ c; ol; nl; e] ! c, provided c is a constant.

(r2)

[ #i; 0; nl; nil] ! #(i + nl).

(r3)

[ #1; ol; nl; @l :: e] ! #(nl ? l).

(r4)

[ #1; ol; nl; (t; l) :: e] ! [ t; 0; (nl ? l); nil] .

(r5)

[ #i; ol; nl; et :: e] ! [ #(i ? 1); (ol ? 1); nl; e] ; provided i > 1.

(r6)

[ (t1 t2 ); ol; nl; e] ! ([[t1 ; ol; nl; e] [ t2 ; ol; nl; e] ).

(r7)

[ ( t); ol; nl; e] ! ( [ t; (ol + 1); (nl + 1); @nl :: e] ). Figure 2: Rule schemata for reading suspensions

substitutions. The following tokens, used in these schemata perhaps with subscripts or superscripts, are to be interpreted as schema variables for the indicated syntactic categories: c for constants, t for suspension terms, et for environment terms, e for environments, i for positive numbers and ol, nl, l and m for natural numbers. The applicability of several of the rule schemata are dependent on `side' conditions that are presented together with them. In determining the relevant instance of the righthand side of some of the rule schemata, simple arithmetic operations may have to be performed on components of the expression matching the lefthand side. By a harmless abuse of notation, these operations are indicated by including them in the schema for the expression to be produced. De nition 3.10 The reduction relations generated by the rule schemata in Figure 1, 2 and 3 are denoted by > s , >r , >m respectively. The union of the relations >r and >m is denoted by >rm , the union of >r and > s by >r s and the union of >r , >m and > s by >rm s . The following proposition, proved in [NW93], establishes the legitimacy of the above de nitions.

Proposition 3.11 Let x be a well formed suspension expression and let y be such that x >r y, x >m y, x > s y, x >rm y, x >r s y or x > rm s y. Then y is a well formed suspension expression. 13

(m1)

[ [ t; ol1; nl1; e1] ; ol2; nl2; e2 ] ! [ t; ol0; nl0; ffe1 ; nl1; ol2; e2gg] , where ol0 = ol1 + (ol2 : nl1 ) and nl0 = nl2 + (nl1 : ol2 ).

(m2)

ffnil; nl; 0; nilgg ! nil.

(m3)

ffnil; nl; ol; et :: egg ! ffnil; (nl ? 1); (ol ? 1); egg, provided nl; ol  1.

(m4)

ffnil; 0; ol; egg ! e.

(m5)

ffet :: e ; nl; ol; e gg ! hhet; nl; ol; e ii :: ffe ; nl; ol; e gg.

(m6)

hhet; nl; 0; nilii ! et.

(m7)

hh@m; nl; ol; @l :: eii ! @(l + (nl : ol)), provided nl = m + 1.

(m8)

hh@m; nl; ol; (t; l) :: eii ! (t; (l + (nl : ol))), provided nl = m + 1.

(m9)

hh(t; nl); nl; ol; et :: eii ! ([[t; ol; l0; et :: e] ; m) where l0 = ind(et) and m = l0 + (nl : ol).

(m10)

hhet; nl; ol; et0 :: eii ! hhet; (nl ? 1); (ol ? 1); eii; provided nl = 6 ind(et).

1

2

2

1

2

Figure 3: Rule schemata for merging environments

14

We observe some auxiliary properties and introduce a few de nitions pertaining to the reduction relations on suspension expressions that will be useful in later sections. Proofs of the properties stated below appear in [NW93].

Proposition 3.12 The relation >rm on suspension expressions is noetherian and con uent. Fur-

ther, a suspension expression is in >rm -normal form if and only if one of the following holds: (a) it is a de Bruijn term; (b) is an environment term of the form @l or (t; l) where t is a de Bruijn term; or (c) it is an environment of the form nil or et :: e where et and e are, respectively, an environment term and an environment in >rm -normal form.

By virtue of Proposition 3.12, unique >rm -normal forms exist for every suspension expression. This justi es the following de nition: De nition 3.13 The >rm -normal form of a suspension expression t is denoted by jtj. Observe that if t is a suspension term, then jtj is a de Bruijn term. The following proposition clari es the sense in which a suspension term encapsulates a de Bruijn term with a `pending' substitution.

Proposition 3.14 Let t = [ t0; ol; nl; e] be a term and let e0 = jej. Then jtj = S (jt0j; s ; s ; s ; : : :) 1

where

2

3

8 if i > ol > < #(i ? ol + nl) si = > #(nl ? m) if i  ol and e0[i] = @m : j[ ti ; 0; nl ? m; nil] j if i  ol and e0[i] = (ti ; m).

The correspondence between the - and s -contraction rule schemata is an easy consequence of Propositions 3.12 and 3.14.

Proposition 3.15 Let l ! r be an instance of the s-contraction rule schema. Then jlj ! jrj is

an instance of the -contraction rule schema.

The above proposition immediately yields a correspondence between > and > s :

Proposition 3.16 Let t be a de Bruijn term and let t > s. Then there is a suspension term r such that t > s r and jrj = s. The reduction relation >rm s is intended to simulate -reduction and Proposition 3.16 re ects this only in part. To make this observation in its full generality, it is necessary to extend the notion of -reduction to suspension terms. De nition 3.17 The relation on suspension expressions generated by the -contraction rule schema is denoted by > 0 . The correctness of >rm s -reduction can then be stated as follows: 15

Proposition 3.18 (a) If x and y are suspension expressions such that x >rm s y , then jxj > 0 jy j.

(b) If x and y are suspension expressions in >rm -normal form such that x > 0 y then x >rm s y .

The reduction relation >rm s is also con uent. This fact can be established by using Proposition 3.18 and the con uence of > 0 . However, we do not use this property in this paper.

4 Eliminating the merging rules The suspension notation for lambda terms is a fairly general one and it permits -reduction to be realized through various interesting sequences of rewritings. However, the full generality of this notation may not be useful in practice and implementation considerations may, in fact, dictate a simpler notation. In this spirit, we consider the elimination of the merging rules and the restriction of our notation to only simple suspension expressions. We begin by noting that the merging rules can, in principle, be dispensed with: given a de Bruijn term t that has a > -normal form, this form can be obtained by using only the s -contraction and reading rules. This observation is a special case of the following lemma:

Lemma 4.1 Let x be a simple suspension expression and let y be such that x >rm s y. Then there

is a suspension expression z such that x >r s z and y >rm z .

Proof. We use two observations about the rules for rewriting suspension expressions. First, a

simple suspension expression that is not a >rm -normal form can always be rewritten by a reading rule. Second, rewriting a simple suspension expression by means of a reading rule or a s -contraction rule produces another simple expression. Now, since x >rm s y , it follows from Proposition 3.18, that jxj > 0 jy j. We claim that x >r jxj and that jxj >r s jy j. These facts yield the lemma: letting z = jy j, we see that x >r s z and y >rm z . The rst claim, that x >r jxj, follows immediately from the fact that x is a simple suspension expression. For the second claim, we note that if r is a >rm -normal form and r > 0 s, then there is some t such that r > s t and s = jtj; this can be seen by using Proposition 3.16 and an induction on the structure of r. Thus, using the observations about the rewrite rules, r >r s s. An induction on the length of the sequence by which jxj > -reduces to jy j now shows that jxj >r s jy j.

2

While the merging rules and the corresponding more complex forms for suspension expressions are strictly unnecessary, they have a particular purpose within our system: it is through these that substitutions embodied in di erent environments can be combined. Such a combination has an eciency bene t in that the di erent substitutions can be carried out in one walk over the structure of the term that is being substituted into. It is possible, however, to recognize some useful sequences of applications of merging rules and to devise new rules that encode these sequences and that also have the e ect of producing simple suspension expressions from simple suspension 16

expressions. Such rules can have the additional advantage of reducing a sequence of rewritings to a single rewriting step. We describe, in this section, two kinds of situations in which merging rules can be used to advantage and we codify the use of these rules in these contexts in new rules that preserve the underlying equality relation. The rst kind of situation is that in which merging rules are employed in combining environments generated by multiple uses of the s -contraction rule schema. As an illustration, we may consider the term (( (( ( ((#1 #2) #3))) t2 )) t3 ); in which t2 and t3 are arbitrary de Bruijn terms. Let us assume that this term is to be reduced to a >rm s -normal (or > -normal) form using the strategy of rewriting the outermost and leftmost redex at each stage in this process. Thus, the rst step would be to use the s -contraction rule schema to rewrite the given term to [ (( ( ((#1 #2) #3))) t2 ); 1; 0; (t3; 0) :: nil] : The substitution embodied in the environment of this term would then be propagated over the structure of the term that is being substituted into, eventually producing the term (( [ ( ((#1 #2) #3)); 2; 1; @0 :: (t3 ; 0) :: nil] ) [ t2 ; 1; 0; (t3; 0) :: nil] ): The s -contraction rule schema would now be used again to produce the term [ [ ( ((#1 #2) #3)); 2; 1; @0 :: (t3 ; 0) :: nil] ; 1; 0; ([[t2 ; 1; 0; (t3; 0) :: nil] ; 0) :: nil] : This term corresponds to the term ( ((#1 #2) #3)) embedded within two environments. The merging rule schema (m1) will now be employed to combine these environments into one, yielding the term [ ( ((#1 #2) #3)); 2; 0; ff@0 :: (t3 ; 0) :: nil; 1; 1; ([[t2 ; 1; 0; (t3; 0) :: nil] ; 0) :: nilgg] . The term displayed above is not a simple suspension term. However, it can be reduced to the simple suspension term [ ( ((#1 #2) #3)); 2; 0; ([[t2 ; 1; 0; (t3; 0) :: nil] ; 0) :: (t3; 0) :: nil] by repeatedly using the merging rule schemata. This simple suspension term would not be produced if the strategy of rewriting only the outermost and leftmost redex were followed at each stage. However, there is bene t to deviating from the mentioned strategy to produce this term since it simpli es the subsequent `lookup' of the environment. The sequence of rewriting steps above that start from the second use of the s -contraction rule schema can be collapsed into the use of one more `powerful' s -contraction rule. This rule schema is presented in the de nition below. Using this schema has the e ect of merging two environments in contexts of the kind described while producing simple suspension expressions from simple suspension expressions. De nition 4.2 Let the following symbols be schema variables for the corresponding syntactic categories: t1 and t2 for suspension terms, e for environments and ol and nl for natural numbers. Then the s0 -contraction rule schema is the following 17

(( [ t1 ; ol + 1; nl + 1; @nl :: e] ) t2 ) ! [ t1 ; ol + 1; nl; (t2; nl) :: e] with the proviso that the index of the environment instantiating e is less than or equal to the natural number instantiating nl. The proviso associated with the above rule schema cannot at the moment be veri ed in a straightforward manner. However, we shall soon restrict our attention to a class of suspension expressions every element of which satis es this condition. The proviso will then become unnecessary and the rule schema can be applied in a truly atomic fashion. The following lemma shows the correspondence between the s - and the s0 -contraction rule schemata. The proof of this lemma and others that follow exhibit the usefulness of the suspension notation in the analysis of coarser grained notations and rewrite rules.

Lemma 4.3 Let t ! s be an instance of the s0 -contraction rule schema. Then there are suspension terms r and u such that t ! r is a s -contraction rule and s >rm u and r >rm u. Proof. Since t ! s is an instance of the s0 -contraction rule schema, t is a suspension term of the

form (( [ t1 ; ol + 1; nl + 1; @nl :: e] ) t2 ), s is, correspondingly, of the form [ t1 ; ol + 1; nl; (t2; nl) :: e] and, further, e is such that ind(e)  nl. Now let r be the suspension term [ [ t1 ; ol + 1; nl + 1; @nl :: e] ; 1; 0; (t2; 0) :: nil] . We observe that t ! r is a s -contraction rule and, thus, one of the requirements of the lemma is satis ed. We now show that the remaining condition, that s and r >rm -reduce to a common expression, is also satis ed. We assume that the subexpression of s and r matching e is a simple environment. If this is not the case, then there are suspension terms s0 and r0 such that s >rm s0 and r >rm r0 that have the above forms and that also satisfy the mentioned condition. The argument we provide here can then be applied to s0 and r0 yielding, in an obvious fashion, the desired conclusion with respect to s and r. Now, under the assumptions made, r >rm [ t1; ol + 1; nl; ff@nl :: e; nl + 1; 1; (t2; 0) :: nilgg] >rm [ t1 ; ol + 1; nl; hh@nl; nl + 1; 1; (t2; 0) :: nilii :: ffe; nl + 1; 1; (t2; 0) :: nilgg] >rm [ t1 ; ol + 1; nl; (t2; nl) :: ffe; nl + 1; 1; (t2; 0) :: nilgg] : Now, using the facts that e is a simple environment and that ind(e)  nl, it can be seen that ffe; nl + 1; 1; (t2; 0) :: nilgg >rme; an explicit argument would proceed by induction on the length of e using rule schemata (m2), (m5), (m6) and (m10). From these observations it follows that r >rm s. Choosing u to be s, the required condition is seen to hold.

2

The following lemma, which can be veri ed by an induction on the structure of suspension expressions using Lemma 4.3, justi es the use of the s0 -contraction rule schema in reduction sequences. 18

Lemma 4.4 Let r result from the suspension expression s by a use of the s0 -contraction rule schema. Then there is a suspension expression u such that s >rm s u and r >rm u.

An alternative choice could have been made for the more powerful s -contraction rule schema. In particular, the rule schema ([[( t1); ol; nl; e] t2 ) ! [ t1 ; ol + 1; nl; (t2; nl) :: e] would produce an equivalent e ect. Such a rule schema would be in keeping with the behavior of reduction procedures that use environments [AP81, CCM87, HM76] and would also parallel the auxiliary rule described in conjunction with the  -calculus in [ACCL90]. The rule schema that we have chosen here is better suited to the use made of our notation in Section 7, where reduction procedures will be expected to return suspension terms that are constants, variable references, abstractions or applications at the top-level. We pause brie y to understand the interaction between the use of the merging rules and the sharing of work in reduction. Towards this end, let us examine an alternative reduction sequence for the suspension term considered earlier, namely (( (( ( ((#1 #2) #3))) t2 )) t3 ): We start as before by rewriting the outermost s -redex in this term to produce [ (( ( ((#1 #2) #3))) t2 ); 1; 0; (t3; 0) :: nil] : At this point, in contrast to the choice exhibited earlier, we proceed by rewriting the embedded > s -redex (( ( ((#1 #2) #3))) t2 ) to produce the term [ [ ( ((#1 #2) #3)); 1; 0; (t2; 0) :: nil] ; 1; 0; (t3; 0) :: nil] . Following this course could lead to a greater sharing of work in a graph-based implementation of -reduction based on our notation: the subterm (( ( ((#1 #2) #3))) t2 ) is rewritten before the propagation of the substitution `breaks' a possible sharing. Notice, however, that the full bene t of such a sharing of reduction is realized only if the substitutions encoded in the two suspensions are performed in separate walks over the structure of the term ( ((#1 #2) #3)). A further point to note is that opportunities for a di erent kind of sharing in reduction can be missed if the merging rules (and the derivative s0 -contraction rules) are not used. In the case being considered, t3 has eventually to be substituted into t2 . Not using the merging rules would result in this substitution being done separately in the di erent copies that might be made of t2 . In general, the trade-o s between di erent reduction strategies is more complex than it initially appears to be. We note that a notation such as ours can be used in a careful analysis of this issue. We now consider the second kind of situation in which the merging rules are useful. A situation of this sort arises essentially when a suspension has to be substituted in a particular context and the indices within the de Bruijn term it represents have to be adjusted as a result. We illustrate such situations by continuing with the example considered earlier in this section. At the point where we left o with this example, the given term had been reduced to [ ( ((#1 #2) #3)); 2; 0; ([[t2 ; 1; 0; (t3; 0) :: nil] ; 0) :: (t3; 0) :: nil] : 19

Using the reading rules repeatedly in a leftmost-outermost fashion, this term can be transformed into ( ((#1 [ [ t2 ; 1; 0; (t3; 0) :: nil] ; 0; 1; nil]) [ #3; 3; 1; @0 :: ([[t2 ; 1; 0; (t3; 0) :: nil] ; 0) :: (t3 ; 0) :: nil] )): The subterm [ [ t2 ; 1; 0; (t3; 0) :: nil] ; 0; 1; nil] of this term represents t2 embedded within two suspensions. The inner suspension represents the result of substituting t3 for the rst free variable within t2 and arises from the use of a s -contraction rule. The outer suspension represents the `bumping up' of the indices for the free variables in the resulting term by 1 and is necessitated by the substitution of the term in question into an abstraction. Now, the two di erent kinds of substitutions represented in the subterm being considered can, in fact, be performed in the same walk over the structure of t2 . Indeed, merging rules can be applied to [ [ t2 ; 1; 0; (t3; 0) :: nil] ; 0; 1; nil] to produce the term [ t2 ; 1; 1; (t3; 0) :: nil] that combines the two environments into one. We codify the above use of the merging rules in a new rule schema. De nition 4.5 The (bump) rule schema is the following [ [ t; ol; nl; e] ; 0; nl0; nil] ! [ t; ol; nl + nl0; e] , where t is a schema variable for a suspension term, e is a schema variable for an environment and ol, nl and nl0 are schema variables for natural numbers. The following lemma justi es the use of the above rule schema in reduction sequences. Lemma 4.6 Let r result from the suspension expression s by a use of the (bump) rule schema. Then there is an expression u such that s >rm u and r >rm u. Proof. We show the lemma assuming that s is an instance of the lefthand side of the rule schema being considered; this argument can be extended to the general case by a straightforward induction. Thus, s is a term of the form [ [ t; ol; nl; e] ; 0; nl0; nil] . By a reasoning similar to that employed in Lemma 4.3, we may assume that e is a simple environment. Now, using rule schema (m1), s >rm [ t; ol; nl + nl0 ; ffe; nl; 0; nilgg] . By a straightforward induction on len(e) and using rule schemata (m2), (m5) and (m6), it can be seen that ffe; nl; 0; nilgg >rm e. Letting u be r, the lemma follows.

2

The two new rule schemata presented here encode some commonly occurring uses of the merging rules in the reduction of expressions to >rm s -normal form. The do not, of course, capture the full generality of the merging rules. In particular, if the s -contraction rule schema is used to rewrite the subterms of a term in arbitrary order, then two environments that can be merged only by using the general rules may appear in juxtaposition. Such situations can occur, for instance, within an implementation that utilizes a sharing of subterms and, consequently, of reduction steps. The advantage of omitting the merging rules is a simpli cation of the notation. The new rules permit at least some of the functionality of these rules to be regained and may also lead to shorter rewrite sequences in some cases. 20

5 Some rules for simplifying suspensions It is useful to recognize situations in which the substitution embodied in a suspension does not a ect the term being substituted into. The suspension can, in such cases, be rewritten directly to its term component. We consider two kinds of such situations in this section. The rst of these corresponds to the case where the term being substituted into does not contain variable occurrences bound by abstractions external to it. The second corresponds to the case where the substitution represented by the suspension is a vacuous one. (Such substitutions may arise from attempts to renumber variable references.) We characterize these kinds of situations precisely below and also present the relevant simpli cation rules. The use of such rules has multiple bene ts and we highlight this aspect at the end of the section. In describing the rst kind of situation precisely, we need to characterize the independence of a term from abstractions in whose scope it might appear. This is the content of the notion of closedness for terms that is de ned below. De nition 5.1 Given natural numbers i and nl, the notions of being i-closed for a suspension term and (i; nl)-closed for an environment are de ned simultaneously by recursion on the structure of expressions as follows: A suspension term t is said to be i-closed if (1) t is a constant or of the form #k where k  i, or (2) t is of the form (t1 t2 ) where t1 and t2 are i-closed, or (3) t is of the form ( t1) where t1 is (i + 1)-closed, or (4) t is of the form [ t1 ; ol; nl; e] where t1 is (max(ol; i ? (nl ? ol)))-closed and e is (i; nl)-closed. An environment e is said to be (i; nl)-closed if (5) it is nil, or (6) it is of the form @j :: e0 where (nl ? j )  i and e0 is (i; nl)-closed, or (7) it is of the form (t; j ) :: e0 and t is (i : (nl ? j ))-closed and e0 is (i; nl)-closed. A suspension term is said to be closed if it is 0-closed. Given the notion that we wish to capture, our de nition of closedness is somewhat conservative: there are suspension terms that do not depend on outside abstractions but that are not closed in our sense. In particular, no suspension term that contains subexpressions of the form hhet; nl; ol; eii or ffe1; nl; ol; e2gg is closed according to our de nition. It is possible to extend our de nition to include such suspension terms. However, we do not do this here because we are eventually interested only in expressions that do not contain such subexpressions, i.e., in simple suspension terms.

Lemma 5.2 If t is an i-closed suspension term, then so is jtj. 21

Proof. We show the lemma by induction on t with respect to the well founded ordering relation . The argument proceeds by considering the cases for the structure of t. If t is a constant or a variable reference, then jtj = t and the lemma is obviously true. If t is of the form (t t ), we observe rst that t and t are i-closed and that jtj = (jt j jt j). Further, for i = 1; 2, t  ti . Thus, the inductive hypothesis can be used to conclude that jt j and jt j are i-closed and, therefore, jtj 1

1

2

1

1

2

2

2

must also be i-closed. A similar argument suces for the case when t is of the form ( t1). The only remaining case is that when t is a suspension. Let t be the expression [ t0 ; ol; nl; e]. Since jtj = j[ jt0 j; ol; nl; e] j, it suces to show that j[ jt0 j; ol; nl; e]j is i-closed. Towards establishing the latter, we observe rst that t  t0 and also that t0 is (max(ol; i ? (nl ? ol)))-closed since t is iclosed. Using the inductive hypothesis, it therefore follows that jt0j is (max(ol; i ? (nl ? ol)))-closed. Now we consider each possibility for the structure of jt0 j. jt0j is a constant: In this case j[ jt0j; ol; nl; e]j is itself a constant and, hence, is i-closed. jt0j is a variable reference: Let jt0j = #k. We distinguish two situations: that when k > ol and that when k  ol. If k > ol, then it is easily seen that j[ jt0 j; ol; nl; e]j = #(k ? (ol ? nl)). Further, since k  max(ol; i ? (nl ? ol)), it must be the case that k  (i ? (nl ? ol)). But then (k ? (ol ? nl))  i, i.e., j[ jt0 j; ol; nl; e] j must be i-closed in this case. To complete the argument, we consider the case when k  ol. Since t is i-closed, e must be of the form et1 :: : : : :: etol :: nil. Further, etk can only have one of two forms: @l or (t1 ; l). Suppose it is the former. It is then easily seen that j[ jt0j; ol; nl; e] j = #(nl ? l). The i-closedness of t, and the consequent (i; nl)-closedness of e, implies that (nl ? l)  i. Hence j[ jt0j; ol; nl; e] j is i-closed in this subcase. In the remaining subcase, [ jt0 j; ol; nl; e] >rm [ t1 ; 0; nl ? l; nil] : Since e is (i; nl)-closed, t1 is (i : (nl ? l))-closed and hence [ t1 ; 0; nl ? l; nil] is i-closed. We also note that t  [ jt0j; ol; nl; e]  [ t1; 0; nl ? l; e] : Thus, by the inductive hypothesis, j[ t1 ; 0; nl ? l; e] j is i-closed. But this term is identical to j[ jt0j; ol; nl; e]j and, hence, the latter must be i-closed. jt0j is an abstraction: Let jt0j be ( t1). Then [ jt0j; ol; nl; e] >rm ( [ t1 ; ol + 1; nl + 1; @nl :: e] ). Now, since t is i-closed, t0 is (max(ol; i ? (nl ? ol)))-closed. Noting that t  t0 and using the inductive hypothesis, it follows that jt0j is (max(ol; i ? (nl ? ol)))-closed. But then t1 must be (max(ol +1; (i +1) ? ((nl +1) ? (ol +1))))-closed. From the fact that t is i-closed, it also follows that e must be (i; nl)-closed and, hence, that @nl :: e is (i +1; nl +1)-closed. From these observations, it follows easily that [ t1 ; ol + 1; nl + 1; @nl :: e] is (i + 1)-closed. Hence ( [ t1 ; ol + 1; nl + 1; @nl :: e] ) is i-closed. Now t  [ jt0j; ol; nl; e]  ( [ t1 ; ol + 1; nl + 1; @nl :: e] ): By the inductive hypothesis, j( [ t1 ; ol + 1; nl + 1; @nl :: e] )j is i-closed. The desired conclusion follows from noting that this term is identical to j[ jt0j; ol; nl; e] j. jt0j is an application: This case can be dealt with by an argument similar to that provided for the case when jt0 j is an abstraction.

2

22

We can now present the simpli cation rules of the rst kind mentioned at the beginning of the section. De nition 5.3 The (cl) rule schema is the following [ t; ol; nl; e] ! t where t is a schema variable for closed suspension terms, e is a schema variable for environments and ol and nl are schema variables for natural numbers. There is a proviso on the use of the (cl) rule schema whose veri cation seems complicated. However, we describe an annotation system for identifying closed suspension terms in the next section and this allows the (cl) rule schema to be applied in an atomic fashion. The following lemma justi es the use of the (cl) rule schema in reductions.

Lemma 5.4 Let r result from the suspension expression s by an application of the (cl) rule schema. Then there is an expression u such that s >rm u and r >rm u.

Proof. It suces to show the lemma assuming that s is an instance of the lefthand side of the rule

schema in question. Further, if s is of the form [ t; ol; nl; e] , we may assume that t is a >rm -normal form. Observing that [ t; ol; nl; e] >rm [ jtj; ol; nl; e] , t >rm jtj and that, by Lemma 5.2, jtj is closed if t is, the lemma is easily extended to the general case. For the case being considered, we actually make a slightly stronger claim. Let i be a natural number. Assuming t is a >rm -normal form that is i-closed and letting @(nl + i ? 1) :: : : : :: @nl :: e stand for e in the case that i = 0, we claim that [ t; ol + i; nl + i; @(nl + i ? 1) :: : : : :: @nl :: e] >rm t. The claim is proved by induction on the structure of t. It is obviously true if t is a constant. Let t be of the form #j . Since t is i-closed, j  i. (Obviously, i > 0 in this case.) Thus [ t; ol + i; nl + i; @(nl + i ? 1) :: : : : :: @nl :: e] >rm #(nl + i ? (nl + i ? j )) = t.

In the case that t is an abstraction or an application, the claim is shown by a recourse to the inductive hypothesis. The argument is straightforward and we omit the details.

2

The following de nition presents the simpli cation rules of the second kind. De nition 5.5 The (nulle) rule schema is the following [ t; 0; 0; nil] ! t where t is a schema variable for a suspension term. We show now that the inclusion of this rule schema does not alter the underlying equality relation. 23

Lemma 5.6 Let s be a suspension expression and let r result from s by an application of the (nulle)

rule schema. Then there is a suspension expression u such that s >rm u and r >rm u.

Proof. Let i be a natural number and let @i :: @(i ? 1) :: : : : :: @0 :: nil denote nil if i is 0. We claim that if t is a >rm -normal form, then

[ t; i; i; @i :: @(i ? 1) :: : : : :: @0 :: nil] >rm t. The lemma is an easy consequence of this claim. The claim itself is proved by a straightforward induction on the structure of t.

2

Using the rule schemata (cl) and (nulle) whenever they are applicable obviously reduces the work involved in producing a >rm s -normal form. Somewhat less obvious consequences of using these schemata are the conservation of space and the possibility for a greater sharing of work in the context of a graph-based implementation of reduction. To appreciate these aspects, let us consider the reduction to >rm s -normal form of the term [ (( t1) t2 ); ol; nl; e] , assuming that (( t1) t2 ) is known to be a closed term. Using the (cl) rule schema, this term can be rewritten directly to (( t1) t2 ). This rewriting step preserves any sharing that might exist with respect to the subterm (( t1) t2 ) and, consequently, leaves unaltered the possibility of sharing the reduction work pertaining to this subterm. Additionally, no new structures are created as a result of the rewriting. In contrast, if the (cl) rule schema is not available, then the given term might be rewritten to ([[( t1); ol; nl; e] [ t2 ; ol; nl; e] ); this would be the case if, for instance, the strategy of rewriting the leftmost-outermost >rm s -redex were being used. Proceeding in this fashion destroys the sharing with regard to the outermost > s redex and thus precludes its reduction from providing a bene t in some other context. Furthermore, the propagation of the substitution would eventually result in the replication of the entire structure of the term (( t1) t2 ). The unnecessary use of space and time in computing such substitutions has been noted to be signi cant in practice [BR91], and the simpli cation rules presented in this section are of value from this perspective.

6 A re nement to the suspension notation We now describe a re nement to the suspension notation that incorporates within it the discussions of the last two sections. One aspect of this re nement is that it restricts attention to simple suspension expressions. Merging of environments is still possible in the context of the modi ed notation, but this must be realized through new rules akin to those presented in Section 4. The second change that is considered is that of including annotations in suspension expressions to indicate that certain expressions do not contain variables bound by abstractions in whose scope they appear. In particular, our annotation scheme categorizes abstractions, applications and suspensions as either closed expressions or expressions that cannot be presumed to be closed. These annotations 24

permit the (cl) rule schema to be utilized in an e ective manner. If these annotations are to be useful eventually, the information in them must be preserved in the course of reduction. We modify the rewrite rules for (simple) suspension expressions to achieve this e ect. We present the re nement to suspension expressions in the rst subsection below and we discuss its properties such as con uence in the following subsection.

6.1 Annotated suspension expressions and associated reduction relations Our re nement to suspension expressions is given by the following de nition. De nition 6.1 The categories of annotated suspension terms, annotated environments and annotated environment terms, denoted, respectively, by hATermi, hAEnv i and hAETermi, are de ned by the following syntax rules:

hATermi

::= hConsi j #hIndexi j (hATermi hATermi)o j (hATermi hATermi)c j (o hATermi) j (c hATermi) j [ hATermi; hNati; hNati; hAEnv i] o j [ hATermi; hNati; hNati; hAEnv i] c hAEnvi ::= nil j hAETermi :: hAEnv i hAETermi ::= @hNati j (hATermi; hNati)

We assume that hConsi, hIndexi and hNati correspond to the same categories of expressions as in De nition 3.1. Annotated suspension terms, annotated environments and annotated environment terms are referred to collectively as annotated suspension expressions. Annotated suspension expressions have a structure that is quite similar to that of simple suspension expressions; the main di erence is the presence of `annotations' on abstractions, applications and suspensions. We exploit the structural similarity to adapt much of the terminology and de nitions pertaining to suspension expressions to the new context. Thus we may speak of (annotated) abstractions, applications and suspensions. We also assume a notion of subexpressions for annotated suspension expressions that is similar to that for suspension expressions presented in De nition 3.2. The length of an annotated environment and the index of an annotated environment term and an annotated environment can also be similarly clari ed. The changed syntax of annotated environments and environment terms considerably simpli es the de nitions of these notions as indicated below. De nition 6.2 The index of an annotated environment term et, written ind(et) is (m + 1) if et is @m and m if et is (t; m). The index of an environment e, written ind(e), is 0 if e is nil and ind(et) if e is of the form et :: e0 . The length of an annotated environment e, written len(e), is 0 if e is nil and len(e0 ) + 1 if e is of the form et :: e0 . If len(e) = n, then obviously e is of the form

et1 :: et2 :: : : : :: etn :: nil. As before, for 0 < i  n, we write e[i] to denote the environment term eti . 25

In a manner analogous to suspension expressions, we will be interested only in a restricted class of annotated suspension expressions. The following de nition identi es this class. For the most part, this is an adaptation of the wellformedness criterion for suspension expressions to the new context. However, there is an exception. The simple form for annotated environments permits a slightly stronger requirement to be stipulated for them. The new condition e ectively ensures that the proviso on (a suitably adapted version of) the s0 -contraction rule schema will be satis ed by all relevant annotated terms. De nition 6.3 An annotated suspension expression is well formed if the following is true of all its subexpressions: If this is of the form @l :: e or (t; l) :: e, then ind(e)  l. If this is of the form [ t; ol; nl; e] o or [ t; ol; nl; e] c, then len(e) = ol and ind(e)  nl. The quali cation of wellformedness is, from now on, implicitly assumed in connection with annotated suspension expressions. The intended correspondence between annotated suspension expressions and suspension expressions is useful in establishing properties of the new notation. We therefore state this correspondence precisely. De nition 6.4 The suspension expression underlying an annotated suspension expression t is denoted by t and is given as follows: (1) If t is c, #i or nil, then t = t. (2) If t is (o t1 ) or (c t1 ), then t = ( t1). (3) If t is (t1 t2 )o or (t1 t2 )c , then t = (t1 t2 ). (4) If t is [ t1 ; ol; nl; e] o or [ t1 ; ol; nl; e] c, then t = [ t1 ; ol; nl; e] . (5) If t is @l :: e, then t = @l :: e. (6) If t is (t1 ; l) :: e, then t = (t1; l) :: e. The following lemma, which can be proved by an induction on the structure of annotated suspension expressions, shows that such expressions embody the mentioned simpli cation in syntax.

Lemma 6.5 If t is a well formed annotated suspension expression then t is a well formed simple

suspension expression.

Annotations on suspension expressions are intended to indicate whether or not they may be considered closed in the sense of Section 5. There is, therefore, a sense in which these annotations might be considered to be consistent. This is made precise by the following de nition. De nition 6.6 An annotated suspension term t is said to be c-annotated, or closed annotated, if it is of the form (c t1 ), (t1 t2 )c or [ t1 ; ol; nl; e] c. An annotated suspension expression t is said to be consistently annotated if the following conditions hold for each c-annotated subterm s of t: (a) s is 26

(a s )

((u t1 ) t2 )v ! [ t1 ; 1; 0; (t2; 0) :: nil] v

(a s0 )

((u [ t1 ; ol + 1; nl + 1; @nl :: e] o ) t2 )v ! [ t1 ; ol + 1; nl; (t2; nl) :: e] v

Figure 4: The s -contraction rule schemata for annotated suspension expressions a closed term, (b) if s = (s1 s2 )c , then, for i = 1 and i = 2, si either is a constant or is c-annotated, and (c) if s = [ s1 ; ol; nl; e] c, then s1 either is a constant or is c-annotated if ol = 0 and otherwise this property holds of every s0 such that e[i] = (s0 ; l) for some i between 1 and ol. Figures 4 and 5 present rule schemata for rewriting annotated suspension expressions. The interpretation of these schemata is similar to those for rewriting suspension expressions with the following di erences: the tokens t, et and e (used, perhaps, with subscripts or superscripts) are now schema variables for annotated suspension terms, annotated environment terms and annotated environments. Further, the tokens u and v that appear in these rules are schema variables for annotations and may be replaced by either o or c. The choice of rule schemata is, for the most part, honest to the discussions in the preceding two sections. The assumption that the annotations in an expression are consistent permits them to be used in implementing these ideas. As we shall see in Theorem 6.8, the rules manipulate annotations in a way that preserves their consistency. De nition 6.7 The reduction relations on annotated suspension expressions that are generated by the rule schemata in Figures 5 and 4 will be denoted by >ar and >a s respectively. The union of these two relations will be denoted by >ar s .

Theorem 6.8 Let t be a well formed annotated suspension expression that is consistently annotated. If t >ar s or t >a s s, then s is also well formed and consistently annotated.

Proof. Wellformedness. Instances of the lefthand sides of the rule schemata in Figures 4 and 5 can only be annotated suspension terms. From this and an inspection of De nition 6.3 it follows easily that s would be well formed if whenever an instance of the lefthand side of one of the relevant rule schemata is well formed, then the corresponding instance of the righthand side is also well formed. A straightforward inspection of the rule schemata veri es that this is the case. Consistency of annotations. Let l be the subterm of t that is replaced by one of the rule schemata in obtaining s and let r be the term that replaces l. By assumption, l is consistently annotated. We claim that (a) if l is i-closed (for any i), then r is i-closed, and (b) r is consistently annotated. If these conditions hold, s must be consistently annotated: the subexpressions of t that are not a ected by the replacement continue to be consistently annotated and from (a) and (b) it follows 27

(ar1)

[ c; ol; nl; e] u ! c, provided c is a constant.

(ar2)

[ #i; 0; nl; nil] u ! #(i + nl).

(ar3)

[ #1; ol; nl; @l :: e] u ! #(nl ? l).

(ar4)

[ #1; ol; nl; (t; l) :: e] u ! [ t; 0; nl ? l; nil] u.

(ar5)

[ #i; ol; nl; et :: e] u ! [ #(i ? 1); ol ? 1; nl; e] u; provided i > 1.

(ar6)

[ (t1 t2 )u ; ol; nl; e] v ! ([[t1 ; ol; nl; e] v [ t2 ; ol; nl; e] v)v .

(ar7)

[ (u t); ol; nl; e] v ! (v [ t; ol + 1; nl + 1; @nl :: e] o ).

(ar8)

[ (t1 t2 )c ; ol; nl; e] u ! (t1 t2 )c .

(ar9)

[ (c t); ol; nl; e] u ! (c t):

(ar10)

[ [ t; ol; nl; e] c; ol0; nl0; e0] u ! [ t; ol; nl; e] c:

(ar11)

[ [ t; ol; nl; e] o; 0; nl0; nil] o ! [ t; ol; nl + nl0; e] o:

(ar12)

[ t; 0; 0; nil] u ! t Figure 5: Rule schemata for reading annotated suspensions

28

easily that subexpressions that are a ected are changed only in ways that preserve the consistency of annotations. Claim (a) is established by considering in turn the various possibilities for the rule schema that is used. If this is (a s ), (ar12) or one of (ar1)-(ar6), the argument is routine. In the case that the rule schema is one of (ar8)-(ar10), we need the easily veri ed observation that if a term t is i-closed for some i, then it is j -closed for any j > i. The consistency of annotation of the lefthand side ensures that r is 0-closed in each of the relevant cases and hence it must be i-closed for any i. For the cases of (a s0 ) and (ar7), we need an additional easily con rmed fact: if e is an environment such that ind(e)  nl, then e is (i + 1; nl + 1)-closed if and only if e is (i; nl)-closed. The remainder of the argument in these cases is straightforward. The only rule schema left to be considered is (ar11). In this case, l is of the form [ [ t; ol; nl; e] o; 0; nl0; nil] o and r is, correspondingly, of the form [ t; ol; nl + nl0; e] o. Since l is i-closed and max(0; i ? nl0) = (i : nl0 ), it follows that t is max(ol; (i : nl0 ) ? (nl ? ol))-closed and that e is (i : nl0; nl)-closed. From the wellformedness of [ t; ol; nl; e] o (and the consequent wellformedness of [ t; ol; nl; e] ) it follows that ind(e)  nl. Now, it is easily seen that if e0 is a simple environment such that ind(e0)  n and e0 is (j : k; n)-closed, then e0 is (j; k + n)-closed. Thus, it must be the case that e is (i; nl + nl0 )-closed. We further note that ol  0, and, therefore, max(ol; (i : nl0 ) ? (nl ? ol)) = max(ol; i ? (nl + nl0 ? ol)). But then t is max(ol; i ? (nl + nl0 ? ol))-closed. It is now easily seen that [ t; ol; nl + nl0; e] , i.e., r, is i-closed. Claim (b) follows from a routine inspection of the rule schemata using claim (a); the only case that requires some consideration is that of rule schema (ar6), but even here the argument is simple.

2 We assume hereafter that all the annotated suspension expressions we deal with are consistently annotated in addition to being well formed.

6.2 Properties of the reduction relations Our interest is eventually only in suspension expressions and annotations have been included in these expressions largely to provide information that might be useful in reducing them to normal form. We may, in fact, conceive of annotations as being used in the following fashion: given a de Bruijn term that is to be reduced to a > -normal form or to a > -hnf, we introduce annotations into this term, use reduction on annotated suspension expressions to simulate > -reduction and eventually remove the annotations. This `procedure' is not intended to be taken literally; in particular, annotations need not be removed at the end of a reduction sequence to get the necessary information. However, it does convey the role of annotations, and we examine properties of the reduction relations on annotated suspension expressions that justify their presence in these 29

expressions from this perspective. To begin with, the eventual correspondence that is intended to de Bruijn terms motivates the following de nition. De nition 6.9 An annotated de Bruijn term is an annotated suspension term that does not have subexpressions of the form [ t; ol; nl; e] c or [ t; ol; nl; e] o. Let t be an annotated de Bruijn term. It is, then, easily seen that t is a de Bruijn term. In the framework being considered, the purpose of the reading rules for annotated suspension expressions is to transform arbitrary such expressions into (annotated) de Bruijn terms. The following lemma ensures that these rules achieve this end.

Theorem 6.10 The relation >ar is noetherian. Further, an annotated suspension expression is a

>ar -normal form if and only if it does not contain any subexpressions of the form [ t; ol; nl; e] c or [ t; ol; nl; e] o. In particular, if x is an annotated suspension term that is also a >ar -normal form, then x is an annotated de Bruijn term.

Proof. It is easily seen that if t >ar s then t  s. The noetherianity of >ar now follows from the fact that  is well founded. Any annotated suspension expression that has a well-formed subpart of the form [ t; ol; nl; e] c or [ t; ol; nl; e] o can be rewritten by one of the rules in Figure 5. Thus, such an expression cannot be in >ar -normal form, and the observation about the structure of these forms must therefore be true.

2

We would like to ascertain that the de Bruijn term corresponding to a >ar -normal form of a given annotated suspension expression is unique. This would be the case if >ar were con uent. However, this relation is not con uent in the strict sense. The reason for this is that the use of certain rules in opposition to others may result in a more `conservative' annotation. Thus, consider a term of the form [ (c #1); ol; nl; e] o. Depending on whether rule schema (ar7) or (ar9) is used in rewriting this term, the >ar -normal form that is produced with be either (o #1) or (c #1). The reading rules, nevertheless, induce a satisfactory reduction relation because, as we shall see presently, >ar -normal forms for a given annotated suspension expression di er only in their annotations.

Lemma 6.11 Let t and s be annotated suspension expressions. If t >ar s, then there is a suspension expression u such that t >rm u and s >rm u. If t >ar s s, then there is a suspension expression u such that t >rm s u and s >rm u. Proof. It is sucient to show the lemma assuming that t is an instance of the lefthand side of

one of the rule schemata in Figures 4 and 5 and that s is the corresponding righthand side. This is obvious in the case of all the rule schemata except (a s0 ) and (ar8)-(ar12). In the case of (a s0 ), the desired conclusion follows from Lemma 4.4; note that the wellformedness condition on annotated suspension expressions ensures that t satis es the proviso on the use of the ( s0 )-contraction rule schema. The arguments for (ar11) and (ar12) use Lemma 4.6 and Lemma 5.6, respectively, in an obvious way. Finally, for the cases of (ar8)-(ar10), we use Lemma 5.4, noting that the consistency 30

of annotations guarantee that t satis es the proviso on the use of the (cl) rule schema in each case.

2 We shall extend Lemma 6.11 to a sequence of rewriting steps based on the schemata in Figures 4 and 5. In doing so, we need the permutability property of reductions on suspension expressions that is contained in the following lemma. In the diagrams in this and other proofs below, the dashed arrows are to be interpreted as assertions of the existence of reductions given by the labels on them, depending on the reductions depicted by the solid arrows.

Lemma 6.12 Let t be a suspension expression, let t >rm s s and let t >rmr. Then there is an

expression u such that r >rm s u and s >rm u.

Proof. Let u = jsj. Then, the following diagram veri es the lemma: >rm s s t

>rm r >rm

t

j j

>rm

(1)

> 0

s

j j

(2)

>rm s Here, the dashed arrow from r to jtj is justi ed by Proposition 3.12, the dashed arrow from jtj to jsj in face (1) is justi ed by Proposition 3.18 and the remaining dashed arrow in face (2) is also justi ed by the same proposition.

2

Lemma 6.13 Let t be an annotated suspension expression. If t >ar s, then there is a suspension

expression r such that t >rm r and s >rm r. If t >ar s s, then there is a suspension expression r such that t >rm s r and s >rm r.

Proof. The arguments in both cases are similar and we consider explicitly only the situation

where t >ar s s. We note rst that if t >ar s s0 , then s0 is consistently annotated; this follows from Theorem 6.8. We use now an induction on the length of the reduction sequence. In particular, the requirements of the lemma in the case being considered are evident from the following diagram:

31

t

>ar s

s0

>ar s

(2) (1)

s0 >rm t

s >rm u >rm

(3)

r0

>rm s

>rm s

s

>rm s

r

The arrows labelled with in this diagram correspond to the use of De nition 6.4. Further, the rectangle labelled (1) in this diagram is completed by using the hypothesis, the rectangle labelled (2) is completed by invoking Lemma 6.11 and the rectangle labelled (3) is justi ed by Lemma 6.12.

2 We now show the uniqueness of >ar -normal forms up to annotations.

Theorem 6.14 Let t be an annotated suspension expression and let s and r be >ar -normal forms of t. Then s = r. Further, jtj = s. Proof. From Theorem 6.10, Proposition 3.12 and De nition 6.4 it follows that s is a >ar -normal form if and only if s is a >rm -normal form. Using this fact in conjunction with Lemma 6.13 it follows that t >rm s. By a similar argument t >rm r. But then, by Proposition 3.12, it must be the case that s = r. It is also obvious from these observations that s is a >rm -normal form of t.

2

Theorem 6.14 justi es the following de nition. De nition 6.15 Let t be an annotated suspension expression and let s be a >ar-normal form of t. Then jtja denotes s. We digress brie y to state a correspondence between the s -contraction rules for annotated suspension expressions and the -contraction rule for de Bruijn terms. This observation is analogous to Proposition 3.15 and will be useful in Section 7.

Lemma 6.16 Let l ! r be an instance of one of the s-contraction rule schemata for annotated suspension expressions. Then jlja ! jrja is an instance of the -contraction rule schema. 32

Proof. Using the wellformedness condition for annotated suspension expressions, it is easily seen that l ! r is an instance of either the s - or the s0 -contraction rule schema. Using Lemma 4.3 and Proposition 3.15 it then follows that jlj ! jrj is an instance of the -contraction rule schema. By Theorem 6.14, jlj = jlja and jrj = jrja.

2

We were concerned with the issue of con uence. Theorem 6.14 together with the existence of >ar -normal forms for all annotated suspension expressions easily yields a (weak) con uence result for >ar . In showing a similar property for >ar s , we need a `converse' to Lemma 6.13.

Lemma 6.17 Let t be an annotated suspension expression and let t >rm s s. Then there is an r

such that t >ar s r and s >rm r.

Proof. Given the structure of annotated suspension expressions, t must be a simple suspension

expression. Thus, by Lemma 4.1, there is some u such that t >r s u and s >rm u. Now, corresponding to each rule schema in Figure 1 and Figure 2, there is a rule schema for rewriting annotated suspension expressions that produces a similar e ect. Using this observation and an induction on the length of the reduction sequence, it follows that there is some r such that t >ar s r and r = u.

2

The use of >ar s -reductions in the fashion outlined at the beginning of this subsection is justi ed by the following theorem restricted to de Bruijn terms.

Theorem 6.18 (a) Let x and y be annotated suspension expressions such that x >ar s y . Then jxja > 0 jy ja .

(b) Let x and y be suspension expressions in >rm -normal form such that x > 0 y . If x0 is an annotated suspension expression such that x0 = x, then there is an annotated suspension expression z such that x0 >ar s z and z = y .

Proof. (a) By Lemma 6.13, there is a suspension expression w such that x >rm s w and y >rm w. But then it must be the case that x >rm s jy j. By Proposition 3.18, it must be the case that jxj > 0 jjyjj. Noting that jjyjj = jyj and using Theorem 6.14, it follows that jxja > 0 jyja. (b) Since x > 0 y , by Proposition 3.18, x >rm s y . But then, by Lemma 6.17, there must be a z such that x >ar s z and y >rm z . However, y is in >rm -normal form. Hence z = y .

2

We nally show the con uence property pertaining to >ar s .

Theorem 6.19 Let t be an annotated suspension expression and let s and s0 be such that t >ar s s

and t >ar s s0 . Then there are annotated suspension expressions r and r0 such that s >ar s r, s0 >ar s r0 and r = r0.

Proof. The lemma is evident from the diagram below. 33

>ar s

t

j j

>ar s

a

s

ta

s0

(2) j j

>ar s

a

a

(1) j j

>

>ar s

j j

>

sa

j j

>

(3)

js0 j

a

r

(5)

j j

>

a

u

(4)

a

j j

r0

The arrows labelled with j ja in this diagram correspond to the use of De nition 6.15. The dashed arrows in the various faces are justi ed as follows: those in (1) and (2) by using Theorem 6.18, the remaining ones in (3) by an obvious extension of Proposition 2.6 and those in (4) and (5) by using, again, Theorem 6.18.

2

Theorem 6.19 shows that there is some exibility in the order of rewriting in simulating reduction. It is well-known that not all reduction sequences will produce a normal form even when one exists and this situation is not altered in the context of our rewrite system. The particular reduction strategy to be used must be determined by a consideration of this issue as well as issues of eciency and ease of implementation. At a level of detail, our rewrite system presents some choices in the rule schema to be used even after the redex to be rewritten has been selected. These choices are between (a s ) and (a s0 ), between (ar6) and (ar8) and between (ar7) and (ar9). As already noted, using rule schemata (ar8) and (ar9) in opposition to (ar6) and (ar7) reduces the reduction work and preserves any existing sharing of terms and is therefore the preferred course. The choice between (a s) and (a s0 ) is somewhat more delicate, posing a dilemma between possible sharing in reduction and a sharing in substitution walks.

7 Using the notation in comparing lambda terms We now examine the usefulness of our notation in comparing lambda terms in a situation where two terms are considered to be equal if one can be obtained from the other by renaming bound 34

variables or by -contractions. We have already observed that the de Bruijn representation simpli es the comparison operation on these terms by eliminating names for variables, and this attribute obviously carries over to our notation. Our notation has the additional characteristic of permitting laziness in substitution and we show how this can be used to advantage in this section. In particular, we relativize the notion of head normal forms to the reduction relation >ar s and we show that such forms can be used directly in the comparison of lambda terms. We then describe a graphbased procedure for nding head normal forms for annotated suspension terms. This procedure is presented in Standard ML (SML) and is accompanied by a complete proof of correctness.

7.1 Head normal forms for annotated suspension terms We adapt the notion of head normal forms to annotated suspension expressions below. De nition 7.1 A head normal form relative to >ar s , or a >ar s -hnf, is an annotated suspension term of the form (u1 : : : (un (: : : (h t1 )un+1 : : : tm )un+m ) : : :) where, for 1  i  (n + m), ui is either o or c and h is either a constant or a variable reference. As before, we refer to t1 ; : : :; tm as the arguments of such an expression, to h as its head and to n as its binder length and we note that, in particular instances, a >ar s -hnf may have no arguments and its binder may be empty. An annotated suspension term is said to be a weak >ar s -hnf if it is a >ar s -hnf or it is of the form (c t) or (o t). A (weak) >ar s -hnf t is a (weak) >ar s -hnf of an annotated suspension term s if s >ar s t. The following theorem, an immediate consequence of the de nition of jtja for an annotated suspension term t, states a correspondence between head normal forms relative to > and >ar s .

Theorem 7.2 Let t be an annotated suspension term. If t is a >ar s -hnf with arguments t ; : : :; tm, head h and binder length n then jtja is a > -hnf with binder length n and in fact jtja = ( : : : ( (: : : (h jt ja) : : : jtmja)) : : :): 1

1

In analyzing the reduction of annotated suspension terms to >ar s -hnfs, we use a generalization of the notion of a head > -redex that takes into account the new collection of rewrite rules. De nition 7.3 An annotated suspension term t has (weak) head >ar s -redexes only if it is not a (weak) >ar s -hnf and, in this case, these are given as follows: (a) Let t be of the form (t1 t2 )o or (t1 t2 )c . If t is a >a s -redex then it is its sole (weak) head >ar s -redex. Otherwise the weak head >ar s -redexes of t1 are its (weak) head >ar s -redexes; notice that t1 cannot be a weak >ar s -hnf here. (b) Let t be of the form (c t1 ) or (o t1 ). Then the head >ar s -redexes of t are those of t1 . (This case does not arise if t is not a weak >ar s -hnf.) 35

(c) Let t be of the form [ t1 ; ol; nl; e] c or [ t1 ; ol; nl; e] o. If t is a >ar -redex, then it has itself as a (weak) head >ar s -redex. Further, every (weak) head >ar s -redex of t1 is a (weak) head >ar s -redex of t. We are interested in reducing annotated suspension terms to >ar s -hnfs. As before, we might think of using head reduction sequences for this purpose. We relativize this notion to the new collection of rewrite rules below and also incorporate into it a possibility for sharing in reduction. De nition 7.4 A (weak) head >ar s -reduction sequence of an annotated suspension term t is a ( nite or in nite) sequence t = r0; r1; r2; : : :; rn; : : :; where, for i  0, there is an annotated suspension term succeeding ri if ri is not a (weak) >ar s -hnf and, in this case, ri+1 is obtained from ri by simultaneously rewriting a (weak) head >ar s -redex and some number of other subterms that are identical in form to it using one of the rule schemata in Figures 4 and 5. Obviously, such a sequence terminates if there is an m  0 such that rm is a (weak) >ar s -hnf. A (weak) head >ar s -reduction from t to s is a nite initial segment of some (weak) head >ar s -reduction sequence of t that has s as its last element. Unlike in the case of > , we do not have the property of uniqueness of head >ar s -reduction sequences of annotated suspension terms. However, the redundancy turns out to be harmless: if an annotated suspension term has a >ar s -hnf, then any head >ar s -reduction sequence of it terminates. We show this through the following series of lemmas, culminating in Theorem 7.9.

Lemma 7.5 Let q be a de Bruijn term of the form (: : : ((( q1) q2 ) q3 ) : : : qm ) and let (( q1) q2 ) ! r be an instance of the -contraction rule schema. If q > p, then either p is of the form (: : : ((( p1) p2) p3 ) : : : pm ) where, for 1  i  m, qi > pi or (: : : (r q3 ) : : : qm ) > p.

Proof. By an induction on the length of the sequence by which q > p. The lemma is obvious when

the length is 0. If the length is i + 1, we consider the possibilities for the rst rewriting step. If this is a replacement of the subterm (( q1) q2 ), then the lemma is again obvious. Otherwise, the second term in the sequence has the form (: : : ((( q10 ) q20 ) q30 ) : : : qm0 ) where, for 1  i  m, qi > qi0 . By hypothesis, either p is of the form (: : : ((( p1) p2 ) p3) : : : pm ) where, for 1  i  m, qi0 > pi or (: : : (r0 q30 ) : : : qm0 ) > p for r0 = S (q10 ; q20 ; 1; 2; : : :). In the rst case the lemma follows from the transitivity of > and in the second case we use Proposition 2.5.

2

Lemma 7.6 Let r be a de Bruijn term that has a (weak) head > -redex, let s result from r by rewriting this redex using a -contraction rule and let t be such that s > t. Further, let e1 and e2 be simple environments such that je1 j > 0 je2j and the suspension [ r; ol; nl; e1] is well formed. Then j[ r; ol; nl; e1] j has a (weak) head > -redex and the term p that results from rewriting this redex by a -contraction rule is such that p > j[ t; ol; nl; e2] j. 36

Proof. From Propositions 3.14 and 2.5 and the facts that s > t and je j > 0 je j it can be seen that j[ s; ol; nl; e ] j > j[ t; ol; nl; e ] j. Thus, the lemma would follow if j[ r; ol; nl; e ] j has a (weak) head > -redex and rewriting this redex by a -contraction rule produces j[ s; ol; nl; e ] j. This is seen to 2

1

1

2

1

1

be the case by induction on the structure of r. If r is a > -redex, Proposition 2.5 yields the desired conclusion. If r is of the form (r1 r2 ) and is not a > -redex, then s is of the form (s1 r2) where s1 results from r1 by rewriting a weak head > -redex using a -contraction rule. Thus, j[ r1; ol; nl; e1] j has a weak head > -redex and rewriting this in the required manner yields j[ s1 ; ol; nl; e1] j. But then (j[ s1 ; ol; nl; e1] j j[ r2 ; ol; nl; e1] j) results from (j[ r1; ol; nl; e1] j j[ r2 ; ol; nl; e1] j) by rewriting a (weak) head > -redex. The lemma follows from noting that these two terms are identical to j[ s; ol; nl; e1] j and j[ r; ol; nl; e1] j respectively. A similar argument can be provided when r is an abstraction.

2

Lemma 7.7 Let q be an annotated suspension term that has a (weak) head >ar s -redex that is also

a >a s -redex and let p result from q by rewriting this redex and some other subterms of q identical to it in form using one of the rule schemata in Figure 4. Then jq ja has a (weak) head > -redex and the de Bruijn term p0 that results from rewriting this redex using a -contraction rule is such that p0 > jpja.

Proof. By induction on the structure of q. If q is itself the (weak) head >ar s -redex in question,

then the desired conclusion follows from Lemma 6.16. Otherwise we consider the cases for the structure of q . Suppose q is of the form (q1 q2 )u where u is o or c. Then p is of the form (p1 p2 )u where p1 is obtained from q1 by rewriting a weak head >ar s -redex of the required kind together with some of its `copies' and q2 >ar s p2. By hypothesis, jq1 ja has a weak head > -redex. Further, if p01 results from rewriting this redex using a -contraction rule, then p01 > jp1ja . Since jq ja = (jq1 ja jq2 ja), jq ja has a head > -redex and rewriting this yields (p01 jq2ja ). Using Theorem 6.18 together with the fact that q2 >ar s p2, jq2 ja > jp2ja . Observing that jpja = (jp1ja jp2 ja), it follows that p0 = (p01 jq2 ja) > jpja . An argument similar to that above suces when q is an abstraction. The only remaining case is that of a suspension. Let q be of the form [ r; ol; nl; e] u where u is o or c. Then p is of the form [ t; ol; nl; e0] u where t results from r by rewriting a (weak) head >ar s -redex that is also a >a s -redex together with some of its copies and e >ar s e0 . By Theorem 6.18, jeja > 0 je0 ja . By hypothesis, jrja has a (weak) head > -redex and rewriting it produces a (de Bruijn) term r0 such that r0 > jtja. By Lemma 7.6, it follows that j[ jrja ; ol; nl; jeja ] j has a (weak) head > -redex and rewriting it yields a term p0 such that p0 > j[ jtja ; ol; nl; je0ja ] j. Observing that jq ja = j[ jrja; ol; nl; jeja ] j and that jpja = j[ jtja; ol; nl; je0ja] j, the lemma is seen to hold in this, the last, case.

2

Lemma 7.8 Let t be an annotated suspension term such that the head > -reduction sequence of jtja terminates. Then any head >ar s -reduction sequence of t terminates. Proof. Let s be an annotated suspension term and let s0 be a de Bruijn term such that s0 > jsja. We then claim the following: if the head > -reduction sequence of s0 terminates, then any head >ar s -reduction sequence of s terminates. The lemma is a consequence of this claim. 37

We prove the claim by an induction on the length of the head > -reduction sequence of s0 . Suppose this length is 1. In this case, jsja must be a > -hnf. Thus, by Lemma 7.7, s cannot have a head >ar s -redex that is a >a s -redex. The noetherianity of >ar now yields the claim. Suppose the length of the sequence is i + 1. In this case, s0 has the form ( : : : ( (: : : ((( q10 ) q20 ) q30 ) : : : qm0 +2 )) : : :): Let r0 be the second term in the head > -reduction sequence of s. Then r0 has the structure ( : : : ( (: : : (q 0 q30 ) : : :qm0 +2 )) : : :) where q 0 = S (q10 ; q20 ; #1; #2; : : :). Since s0 > jsja , using Lemma 7.5, we know that either r0 > jsja or jsja has the form ( : : : ( (: : : ((( p01) p02 ) p03) : : : p0m+2 )) : : :):

where, for 1  i  (m + 2), qi0 > p0i . The inductive hypothesis yields the claim in the rst case. In the second case, let p0 = S (p01; p02; #1; #2; : : :) be a -contraction rule. Then, by Lemma 7.7 and the noetherianity of >ar , any head >ar s -reduction sequence of s must have a ( nite) initial segment s = s0 ; : : :; sl where sl is such that ( : : : ( (: : : (p0 p03 ) : : : p0m+2 )) : : :) > jsl ja . Using Proposition 2.5 in conjunction with the fact that q10 > p01 and q20 > p02 , we see that q 0 > p0. It thus follows that ( : : : ( (: : : (q 0 q30 ) : : :qm0 +2 )) : : :) > ( : : : ( (: : : (p0 p03) : : : p0m+2 )) : : :) and hence, by the transitivity of > , that r0 > jslja . We may conclude from the inductive hypothesis that any head >ar s -reduction sequence of sl must terminate. But then this property must hold also for s.

2

Theorem 7.9 An annotated suspension term t has a >ar s -hnf if and only if every head >ar s reduction sequence of t terminates.

Proof. The `if' part is obvious. For the `only if', suppose t has a >ar s -hnf. By Theorems 6.18 and 7.2, jtja has a > -hnf. By Proposition 2.9, the head > -reduction sequence of jtja terminates.

The theorem then follows from Lemma 7.8.

2

Using Theorem 6.18, Lemma 7.8 and Proposition 2.9, we see that an annotated suspension term

t has a >ar s -hnf if and only if jtja has a > -hnf. Suppose, now, that we wish to determine whether the de Bruijn terms t and s are equal. Assuming t0 and s0 are annotated suspension terms that are such that jt0 ja = t and js0 ja = s, we may proceed as follows: we reduce t0 and s0 to >ar s -hnfs, we then compare the binder lengths and the heads of the resulting terms for identity and nally, if this is still relevant, we compare the arguments for equality. The advantage of using this procedure is that substitutions are propagated over the structures of terms only when necessary. 38

7.2 A procedure for head normalization We now describe a procedure for nding head >ar s -normal forms of annotated suspension terms. We present this procedure in SML, assuming a familiarity on the part of the reader with this language. (A tutorial introduction to this language may be found, for instance, in [Har86].) We employ a graph-based representation for our terms in this procedure to realize some of the space and time advantages mentioned earlier in the paper. The SML type declarations pertaining to the concrete representation of terms are shown below. datatype rawterm = const of string | bv of int | ptr of (rawterm ref) | clam of (rawterm ref) | olam of (rawterm ref) | capp of (rawterm ref)  (rawterm ref) | oapp of (rawterm ref)  (rawterm ref) | csusp of (rawterm ref)  int  int  (envitem list) | osusp of (rawterm ref)  int  int  (envitem list) and envitem = dum of int | bndg of (rawterm ref)  int type term = (rawterm ref) The declaration of the type rawterm re ects, for the most part, the possible structures for annotated suspension terms and the actual representation of a term is a pointer to an expression of this type. An SML expression of type term can be visualized as a directed graph. We deal here only with expressions involving graphs of this kind that are acyclic, and we refer to such expressions themselves as being acyclic. We expect this acyclicity condition to be satis ed by all `input' expressions, and the programs that we present will be seen to preserve this property. The following de nition makes precise the correspondence between the `concrete' and `abstract' syntax for our terms; the acyclicity property is used in an essential way in the de nition. De nition 7.10 The function  from an acyclic SML expression x of type term or (envitem list) to, respectively, an annotated suspension term or an annotated environment is given recursively as follows: (1) If x is of the form ref(const(c)), then  (x) = c. (2) If x is of the form ref(bv(i)), then  (x) = #i. (3) If x is of the form ref(ptr(t)), then  (x) =  (t). (4) If x is of the form ref(clam(t)) or ref(olam(t)), then  (x) is, respectively, (c  (t)) or (o  (t)). (5) If x is of the form ref(capp(t1 ; t2)) or ref(oapp(t1 ; t2)), then  (x) is, respectively, ( (t1)  (t2 ))c or ( (t1 )  (t2 ))o. 39

(6) If x is of the form ref(csusp(t; ol; nl; e)) or ref(osusp(t; ol; nl; e)), then  (x) is, respectively, [  (t); ol; nl;  (e)]]c or [  (t); ol; nl;  (e)]]o . (7) If x is nil, then  (x) = nil. (8) If x is of the form dum(i) :: e, then  (x) = @i ::  (e). (9) If x is of the form bndg(t; i) :: e, then  (x) = ( (t); i) ::  (e). We con ate here the distinction between the SML representation of constants and natural numbers and the abstract syntax for these objects. The purpose of the constructor ptr in our representation of annotated suspension expressions bears some clari cation. We wish at certain points in our programs to identify two expressions in a way that makes the subsequent rewriting of one correspond also to the rewriting of the other. Such an identi cation is usually obtained by representing both expressions as pointers to a common location: the shared rewriting is then a ected by changing the contents of this location. This course cannot be adopted directly in SML since only references can be updated in this language. However, it is possible to simulate it by representing one term as a pointer to the other and the constructor ptr plays the obvious role in this context. There is a possibility for indirection in the representation explained above, and so it is useful to have functions for looking up the `actual' value of a term and for assigning one term to another. The functions deref and assign below achieve this purpose. fun deref(term as ref(ptr(t))) = deref(t) | deref(term) = term fun assign(t1,ref(ptr(t))) = assign(t1,t) | assign(t1,t2) = t1 := ptr(t2) Any invocation of these functions with acyclic expressions as arguments must obviously terminate. We note in passing that our programs are designed to never introduce more than one level of indirection and the recursion in these de nitions can therefore be replaced by conditionals. The following properties of these functions are also easily observed and will be utilized below: (a)  (deref(t)) =  (t), (b) deref(t) is not of the form ref(ptr(t)), and (c) if t =  (t2 ) before assign(t1; t2) is invoked, then  (t1 ) = t and  (t2 ) = t after it terminates. The head normalization procedure that we present functions as follows: It descends through abstractions and applications looking for a head >ar s -redex that is also a >a s -redex. If such a redex is found, then it is rewritten and the process repeats. If it is discovered that the term is a head >ar s -normal form, the procedure terminates. Finally, if a suspension is encountered, the task becomes one of rewriting this to a non-suspension term after which the process is repeated. The task of rewriting a suspension till a top-level structure that is a constant, a variable reference, an application or an abstraction is exposed is performed by the function lazy read that 40

fun nth(x :: l,1) = x | nth(x :: l,n) = nth(l,n - 1) fun lazy read(term as ref(csusp(t,ol,nl,env))) = lazy read aux(term,deref(t),ol,nl,env,true) | lazy read(term as ref(osusp(t,ol,nl,env))) = lazy read aux(term,deref(t),ol,nl,env,false) | lazy read( ) = () and lazy read aux(t,t1 as ref(const( )), , , , ) = t := !t1 | lazy read aux(t,t1 as ref(capp( , )), , , , ) = t := ptr(t1) | lazy read aux(t,t1 as ref(clam( )), , , , ) = t := !t1 | lazy read aux(t,t1 as ref(csusp( , , , )), , , , ) = (lazy read(t1) ; assign(t,t1)) | lazy read aux(t,ref(bv(i)),ol,nl,e,closed) = if i > ol then t := bv(i + nl - ol) else ((fn dum(j) => t := bv(nl - j) | bndg(t1,nl1) => if nl = nl1 then (lazy read(deref(t1)) ; assign(t,t1)) else (fn ref(osusp(ti,oli,nli,ei)) => (t := osusp(ti,oli,nli + (nl - nl1),ei) ; lazy read(t)) | =>

(if closed then t := csusp(t1,0,nl - nl1,nil) else t := osusp(t1,0,nl - nl1,nil) ; lazy read(t))) (deref(t1))) (nth(e,i))) | lazy read aux(t,ref(oapp(t1,t2)),ol,nl,env,closed) = if closed then t := capp(ref(csusp(t1,ol,nl,env)),ref(csusp(t2,ol,nl,env))) else t := oapp(ref(osusp(t1,ol,nl,env)),ref(osusp(t2,ol,nl,env))) | lazy read aux(t,ref(olam(t1)),ol,nl,env,closed) = if closed then t := clam(ref(osusp(t1,ol+1,nl+1,dum(nl) :: env))) else t := olam(ref(osusp(t1,ol+1,nl+1,dum(nl) :: env))) | lazy read aux(t,t1,ol,nl,env,closed) = (lazy read(t1) ; lazy read aux(t,deref(t1),ol,nl,env,closed)) Figure 6: Exposing a top-level non-suspension structure for an annotated suspension term

41

is shown in Figure 6. In dealing with a variable reference embedded in a suspension, this function combines several rewriting steps into one. This course is justi ed ultimately by the following observation:

Lemma 7.11 Let t be the annotated suspension term [ #i; ol; nl; e] u where u is either c or o. Then there is a (weak) head >ar s -reduction from

8 > < #(i + (nl ? ol)) if i > ol t to > #(nl ? m) if i  ol and e[i] = @m : [ t; 0; nl ? m; e] u if i  ol and e[i] = (t; m).

Proof. By an induction on i using the rule schemata (ar2)-(ar5).

2

The following lemma, whose proof is obvious, explains the purpose of the function nth that is used in lazy read:

Lemma 7.12 Let e be an SML expression of type (envitem list) and let i be a positive integer such that i  len( (e)). Then nth(e; i) terminates. Further, if the value returned by it is dum(j ), then  (e)[i] = @j and if it is bndg(t; j ), then  (e)[i] = ( (t); j ).

Suppose t is an SML expression of type term that is such that deref(t)= t. It is then obvious that  (t) is not a suspension at the termination of lazy read(t). The following lemma shows that lazy read(t) always terminates making, in the case of interest, a nontrivial contribution to the construction of a (weak) head >ar s -reduction sequence of the input term.

Lemma 7.13 Let t be an acyclic SML expression of type term. Then the function call lazy read(t) terminates, preserving the acyclicity property. Further, if  (t) = x prior to the invocation and  (t) = y after the termination, then there is a (weak) head >ar s -reduction from x to y. Finally, if x is an annotated suspension, then this (weak) head >ar s -reduction is of length greater than 1. Proof.

Let s0 and e0 be acyclic SML expressions of type term and (envitem list) with initial values such that s =  (s0 ) and e =  (e0 ) and let r0 be an SML expression of type term that does not occur in (i.e., is not pointed to from) either s0 or e0 . Also assume that s and e are such that [ s; ol; nl; e] o ([[s; ol; nl; e] c) is consistently annotated. We then claim the following: lazy read aux(r0; deref(s0); ol; nl; e0; false) (lazy read aux(r0; deref(s0); ol; nl; e0; true)) terminates, preserving the acyclicity property. Further, if r =  (r0) at termination, then there is a (weak) head >ar s -reduction of length greater than 1 from [ s; ol; nl; e] o ([[s; ol; nl; e] c) to r. The lemma is an immediate consequence of this claim. We prove the claim by induction on [ s; ol; nl; e] o ([[s; ol; nl; e] c) with respect to the well founded ordering relation . The preservation of acyclicity follows easily from examining the relevant function de nitions. For the rest, we consider the cases for the structure of s, making implicit use of the earlier mentioned properties of deref and assign. 42

In the case that s is a constant, an abstraction or an application, the claim is easily seen to be true: [ s; ol; nl; e] o ([[s; ol; nl; e] c) is its own (weak) head >ar s -redex and the value that r0 is set to is such that this expression rewrites to  (r0) by one of the reading rules for annotated suspension expressions. In the case that s is a variable reference, we use Lemmas 7.11 and 7.12. The argument is routine and we illustrate its form by considering the most complex situation: that when s is #i for i  ol and e[i] is of the form ([[si; oli; nli; ei] o; nli0 ). (This situation does not arise if s is such that [ s; ol; nl; e] c is consistently annotated.) Using Lemma 7.11 and rule schema (ar11) we see that there is a (weak) head >ar s -reduction of length at least 3 from [ s; ol; nl; e] o to [ si ; oli; (nli + (nl ? nli0 )); ei] o. From this it also follows that [ s; ol; nl; e] o  [ si ; oli; (nli + (nl ? nli0 )); ei] o . Suppose now that m0 is an acyclic SML expression that is such that  (m0 ) = [ si ; oli; (nli + (nl ? nli0 )); ei] o at the invocation of lazy read(m0). Using the inductive hypothesis, it is seen that this call must terminate and if m =  (m0) at this stage, then there is a (weak) head >ar s -reduction from [ si ; oli; (nli + (nl ? nli0 )); ei] o to m. It follows from this that lazy read aux(r0; deref(s0); ol; nl; e0; false) must also terminate and that r0 must be set at this point to a value that satis es the requirements of the claim. The only remaining case is that when s is a suspension. The argument here invokes the induction hypothesis twice but is otherwise similar to that provided in the case that s is a variable reference.

2 The SML function head norm that is presented in Figure 7 expects it rst argument to be of type term and has a twofold purpose: assuming that this argument is t, it is supposed to nd a >ar s -hnf of  (t) when the second argument is false and a weak >ar s -hnf of  (t) when the second argument is true. The structure of this function follows the informal description of it that was provided earlier. The only aspect that requires further clari cation is the treatment of an application with abstract syntax of the form (t1 t2 )c or (t1 t2 )o . The head >ar s -redex of this term is identical to that of t1 except in the case that t1 is an abstraction. Thus, the construction of a head >ar s -reduction sequence of the application in question involves looking for a weak >ar s -hnf of t1 . The following property of the function beta contract that is used in head norm is evident from its de nition.

Lemma 7.14 Let t0, t0 and t0 be SML expressions of type term and let t =  (t0 ) and t =  (t0 ). If t =  (t0 ) at the termination of beta contract(t0; t0 ; t0 ; true), then ((c t ) t )c ! t is an instance of 1

1

2

1

2

1

1

2

2

2

one of the rule schemata in Figure 4. If t =  (t0 ) at the termination of beta contract(t0; t01; t02 ;false), then ((o t1 ) t2 )c ! t and ((c t1 ) t2 )o ! t are instances of some rule schema in Figure 4.

We now prove the correctness of the head normalization procedure.

Theorem 7.15 Let t0 be an acyclic SML expression of type term and let t =  (t0). If t has a >ar s -hnf, then head norm(t0 ;false) terminates and preserves the acyclicity property. Further, when it terminates, t0 is set to an expression such that  (t0 ) is a >ar s -hnf of t.

43

fun beta contract(t,t1 as ref(osusp(t3,ol,nl,dum(nl1) :: e)),t2,true) = if nl = nl1 + 1 then t := csusp(t3,ol,nl1, bndg(t2,nl1) :: e) else t := csusp(t1,1,0,[bndg(t2,0)]) | beta contract(t,t1 as ref(osusp(t3,ol,nl,dum(nl1) :: e)),t2,false) = if nl = nl1 + 1 then t := osusp(t3,ol,nl1, bndg(t2,nl1) :: e) else t := osusp(t1,1,0,[bndg(t2,0)]) | beta contract(t,t1,t2,true) = t := csusp(t1,1,0,[bndg(t2,0)]) | beta contract(t,t1,t2,false) = t := osusp(t1,1,0,[bndg(t2,0)]) fun head norm(term as ref(capp(t1,t2)),whnf) = (head norm(t1,true) ; (fn ref(clam(t)) => (beta contract(term,t,t2,true) ; head norm(term,whnf)) | => ()) (deref(t1))) | head norm(term as ref(oapp(t1,t2)),whnf) = (head norm(t1,true) ; (fn ref(clam(t)) => (beta contract(term,t,t2,false) ; head norm(term,whnf)) | ref(olam(t)) => (beta contract(term,t,t2,false) ; head norm(term,whnf)) => ()) (deref(t1))) | | head norm(ref(clam(t)),false) = head norm(t,false) | head norm(ref(olam(t)),false) = head norm(t,false) | head norm(term as ref(csusp( , , , )),whnf) = (lazy read(term) ; head norm(term,whnf)) | head norm(term as ref(osusp( , , , )),whnf) = (lazy read(term) ; head norm(term,whnf)) | head norm(term as ref(ptr(t)),whnf) = (head norm(t,whnf) ; assign(term,t)) | head norm( , ) = () Figure 7: The head normalization procedure

44

Proof. Since t has a >ar s -hnf, by Theorem 7.9, every head >ar s -reduction sequence of t must

terminate. Now let s be an annotated suspension term and let s0 be an acyclic SML expression that is such that s =  (s0 ). We then claim the following: If every head >ar s -reduction sequence of s terminates, then head norm(s0; false) terminates, preserving acyclicity and setting s0 to a value such that  (s0 ) is a >ar s -hnf of s. If every weak head >ar s -reduction sequence of s terminates, then head norm(s; true) terminates, preserving acyclicity and setting s0 to a value such that  (s0 ) is a weak >ar s -hnf of s. The theorem is a consequence of this claim. The two parts of the claim are proved simultaneously by an induction rst on the length of the longest (weak) head >ar s -reduction sequence of s and then on the structure of s0 . We note that the latter induction is possible because of the acyclicity of s0 . The preservation of acyclicity follows easily from inspecting the de nition of the relevant function. For the rest, we consider the cases for the structure of s0 . The argument is straightforward in the case that s0 is a reference to an expression of the form bv(i), const(c) or ptr(s01). Suppose s0 is of the form clam(s01 ) or olam(s01 ). Then s is of the form (c s1 ) or (o s1 ) where s1 =  (()s01 ). Obviously s is its own weak >ar s -hnf. Further, if r1 is a >ar s -hnf of s1 , then (c r1) or (o r1) is a >ar s -hnf of s. Finally, the longest head >ar s -reduction sequence of s1 is at most as long as that of s and the structure of s01 is simpler than that of s0 . Hence, head norm(s01 ,false) terminates setting s01 to a value such that  (s01 ) is a >ar s -hnf of s1 . The claim follows from these observations and an inspection of the de nition of head norm. Suppose s0 is a reference to an expression of the form capp(s01 ; s02) or oapp(s01; s02 ). Then s is of the form (s11 s12 )u where u is either c or o and, for i = 1 and i = 2, s1i =  (s0i ). Now, if s11 ; : : :; sk1 ; : : : is a weak head >ar s -reduction sequence of s11 and, for i  1, si2+1 is obtained from si2 by rewriting some number of its subterms that are identical to the weak head >ar s -redex of si1 in the same manner as in si1 , then (s11 s12 )u ; (s12 s22 )u ; : : :; (sk1 sk2 )u ; : : : is an initial segment of a (weak) head >ar s -reduction sequence of s. Thus, any weak head >ar s reduction sequence of s11 is at most as long as the longest (weak) head >ar s -reduction sequence of s. Since the structure of s01 is simpler than that of s0 , it follows that head norm(s01,true) must terminate. Let r1 =  (s01 ) and r2 =  (s02 ) at termination. By the argument already outlined, there is a (weak) head >ar s -reduction from s to (r1 r2)u . Now, if r1 is not of the form (c x1) or (o x1 ), then (r1 r2 )u is already a (weak) >ar s -hnf. On the other hand, if it is of either of these forms, then (r1 r2)u has itself as a (weak) head >ar s -redex. Let (r1 r2)u ! r be an instance of one of the schemata in Figure 4. Then the longest head >ar s -reduction sequence of r is shorter than that of s by at least 1. Hence, if r0 is an acyclic SML expression of type term that is such that r =  (r0 ) at the invocation of head norm(r0,false) (head norm(r0,true)), then this invocation terminates. Further, if m =  (r0) at termination, then m is a (weak) >ar s -hnf of r. However, this term is also a (weak) >ar s -hnf of (r1 r2 )u and, thus, of s. The claim follows easily in the case being considered from these observations, Lemma 7.14 and an inspection of the de nition of head norm. Suppose s0 is a reference to an expression of the form csusp(s01 ; ol; nl; e0) or osusp(s01 ; ol; nl; e0). Then lazy read(s0 ) terminates. Let r =  (s0 ) at termination. By Lemma 7.13, there is a (weak) 45

head >ar s -reduction from s to r of length greater than 1. But then the longest (weak) head >ar s -reduction sequence of r is shorter than that of s. Thus, the invocation head norm(s0,false) (head norm(s0 ,true)) subsequent to that of lazy read must terminate. Further, when it does, s0 is set such that  (s0 ) is a (weak) >ar s -hnf of r. This is also a (weak) >ar s -hnf of s and thus the claim is true in this, the nal, case as well.

2

It is instructive to compare head norm when it is restricted to nding weak head normal forms to the procedure in [FT90]. The essential di erence between these two procedures can be reduced to the question of what is to be done when a term of the form [ t; ol; nl; e] v is encountered, assuming that t is not a suspension or a closed term. Rewriting the outermost redex rst, the usual choice of our procedure, has the e ect of destroying a possible sharing in the reduction of t. Reducing t rst, the course adopted by the procedure of [FT90], preserves this sharing but leads to a proliferation of structure traversals and might also preclude other kinds of sharing as noted in Section 4. On a balance, we believe that the approach embedded in our procedure might be the preferred one in practice. However, there is one kind of situation in which it is relevant to consider the alternative strategy. This is the case when the suspension encountered is of the form [ t; 0; nl; nil] v. A term of this kind is created when a substitution is made for an index and corresponds to the `bumping up' of the indices in t. Rewriting the outer redex rst so as to propagate the substitution over the structure of t in all such cases precludes any sharing. Our procedure eschews this course in reducing a term to weak head normal form, but follows it in many other cases. This behavior can be altered by invoking head norm from lazy read in order to reduce t rst in terms of the kind being considered. It is interesting to note that a similar behavior is obtained in [AP81] by using a representation for lambda terms that is di erent from the de Bruijn scheme and that avoids the `renaming' of free variables. As a nal comment, our de nition of (weak) head >ar s -reduction sequences encompasses the sequences produced by the alternative reduction strategies mentioned and thus permits a straightforward veri cation of procedures embodying them.

8 Conclusion We have considered embellishments in this paper to the notation for lambda terms described in [NW93]. These embellishments have consisted of a simpli cation of the earlier notation and the addition of annotations to indicate the independence of terms from enclosing abstractions. We have examined properties of the resulting notation that are relevant to its use in the comparison of lambda terms. Speci cally, we have shown the correspondence between this notation and the conventional notation for lambda terms. We have then lifted the notion of head normal forms to the context of our terms and have shown how this might be used in checking terms for equality. Using this approach permits the bene ts of laziness in substitution to be reaped in the comparison operation. Finally, we have described a procedure for head normalizing terms and have used the tools of analysis made available by our notation in providing a comprehensive proof of its correctness. This procedure has been presented recursively here, but it can be rendered in a straightforward fashion 46

to a stack-based form. It is, in fact, being used in this form in an implementation of Prolog [NJW93]. The comparison of lambda terms considered in this paper is intrinsic to most other operations on the intensions of such terms and the discussions here are therefore relevant in their contexts as well. An operation that is of particular interest to us is that of unifying (typed) lambda terms [Hue75]: this operation is central to Prolog, whose ecient implementation is a major reason for the investigations undertaken here. In its essence, the problem of unifying lambda terms is that of nding substitutions for existentially quanti ed variables that appear in these terms so as to make the terms equal by virtue of the rules of lambda conversion. In order to deal with this uni cation problem, it is necessary to extend the class of de Bruijn terms (and, consequently, also the class of suspension terms) with a new category of atomic symbols corresponding to existentially quanti ed variables. The standard uni cation procedure, that of Huet, is based on comparing terms in the resulting notation, and makes use of the notion of head normal forms. The discussions in this paper are, thus, extremely pertinent to implementing this procedure. In fact, certain aspects of our notation become especially relevant in this context. For example, existentially quanti ed variables and the terms that are substituted for such variables are closed in the sense described here, and our annotation scheme therefore has a potential for being genuinely useful. Similarly, there are bene ts to using de Bruijn's scheme beyond the one already mentioned. As an example, when the comparison of terms reduces to that of their arguments, it is technically necessary to carry around a context given by the binders of the terms. The use of indices permits this requirement to be realized implicitly. This feature is particularly useful when, as in the case of Prolog, the task of solving a uni cation problem may have to be suspended and later resumed. Our discussions in this paper have assumed a notion equality that is based only on - and -conversion. It is often desirable to extend this notion of equality by including also the rule of  -conversion. A consideration of this rule in a context where the terms are typed can be incorporated into the deliberations in this paper in a relatively straightforward fashion. In essence, the comparison of terms requires the  rule to be used in conjunction with head normal forms, and de Bruijn's scheme and suspensions permit a convenient implementation of this requirement. Some discussion of this aspect may be found in [NJW93].

Acknowledgements An early presentation of some of the ideas in this paper appears in [NW90] and we acknowledge the input provided by Debra Wilson at this stage of the work. Work on this paper has been supported by NSF grants CCR-89-05825 and CCR-92-08465.

References [ACCL90] Martn Abadi, Luca Cardelli, Pierre-Louis Curien, and Jean-Jacques Levy. Explicit substitutions. In Seventeenth Annual ACM Symposium on Principles of Programming Languages, pages 31{46. ACM Press, January 1990. 47

[And71]

Peter B. Andrews. Resolution in type theory. Journal of Symbolic Logic, 36:414{432, 1971.

[AP81]

L. Aiello and G. Prini. An ecient interpreter for the lambda-calculus. The Journal of Computer and System Sciences, 23:383{425, 1981.

[Bar81]

H. P. Barendregt. The Lambda Calculus: Its Syntax and Semantics. North Holland Publishing Co., 1981.

[BR91]

Pascal Brisset and Olivier Ridoux. Naive reverse can be linear. In Koichi Furukawa, editor, Eighth International Logic Programming Conference, pages 857{870, Paris, France, June 1991. MIT Press.

[Bru72]

N. de Bruijn. Lambda calculus notation with nameless dummies, a tool for automatic formula manipulation, with application to the Church-Rosser Theorem. Indag. Math., 34(5):381{392, 1972.

[Bru80]

N. de Bruijn. A survey of the project AUTOMATH. In J. P. Seldin and J. R. Hindley, editors, To H. B. Curry: Essays on Combinatory Logic, Lambda Calculus and Formalism, pages 579{606. Academic Press, 1980.

[CAB+ 86] R. L. Constable, S. F. Allen, H. M. Bromley, W. R. Cleaveland, J. F. Cremer, R. W. Harper, D. J. Howe, T. B. Knoblock, N. P. Mendler, P. Panangaden, J. T. Sasaki, and S. F. Smith. Implementing Mathematics with the Nuprl Proof Development System. Prentice-Hall, 1986. [CCM87] G. Cousineau, P-L. Curien, and M. Mauny. The categorical abstract machine. The Science of Programming, 8(2):173{202, 1987. [CH88]

Thierry Coquand and Gerard Huet. The calculus of constructions. Information and Computation, 76(2/3):95{120, February/March 1988.

[Chu40]

Alonzo Church. A formulation of the simple theory of types. Journal of Symbolic Logic, 5:56{68, 1940.

[Cur86]

P-L. Curien. Categorical Combinators, Sequential Algorithms and Functional Programming. Pitman, 1986.

[Fie90]

John Field. On laziness and optimality in lambda interpreters: Tools for speci cation and analysis. In Seventeenth Annual ACM Symposium on Principles of Programming Languages, pages 1{15. ACM Press, January 1990.

[FT90]

John Field and Tim Teitelbaum. Incremental reduction in the lambda calculus. In Proceedings of the 1990 ACM Conference on Lisp and Functional Programming, pages 307{322. ACM Press, 1990. 48

[GMW79] Michael J. Gordon, Arthur J. Milner, and Christopher P. Wadsworth. Edinburgh LCF: A Mechanised Logic of Computation, volume 78 of Lecture Notes in Computer Science. Springer-Verlag, 1979. [Har86]

Robert Harper. Introduction to Standard ML. Technical Report ECS-LFCS-86-14, Laboratory for Foundations of Computer Science, University of Edinburgh, November 1986. Revised by Nick Rothwell, January 1989, with exercises by Kevin Mitchell.

[HHP93] Robert Harper, Furio Honsell, and Gordon Plotkin. A framework for de ning logics. Journal of the ACM, 40(1):143{184, 1993. [HL78]

Gerard Huet and Bernard Lang. Proving and applying program transformations expressed with second-order patterns. Acta Informatica, 11:31{55, 1978.

[HM76]

Peter Henderson and James H. Morris. A lazy evaluator. In Third Annual ACM Symposium on Principles of Programming Languages, pages 95{103, 1976.

[HS86]

J. Roger Hindley and Jonathan P. Seldin. Introduction to Combinatory Logic and Lambda Calculus. Cambridge University Press, 1986.

[Hue75]

Gerard Huet. A uni cation algorithm for typed -calculus. Theoretical Computer Science, 1:27{57, 1975.

[MN87]

Dale Miller and Gopalan Nadathur. A logic programming approach to manipulating formulas and programs. In Seif Haridi, editor, IEEE Symposium on Logic Programming, pages 379{388, San Francisco, September 1987.

[NJW93] Gopalan Nadathur, Bharat Jayaraman, and Debra Sue Wilson. Implementation considerations for higher-order features in logic programming. Technical Report CS-1993-16, Department of Computer Science, Duke University, June 1993. [NM88]

Gopalan Nadathur and Dale Miller. An overview of Prolog. In Kenneth A. Bowen and Robert A. Kowalski, editors, Fifth International Logic Programming Conference, pages 810{827, Seattle, Washington, August 1988. MIT Press.

[NW90]

Gopalan Nadathur and Debra Sue Wilson. A representation of lambda terms suitable for operations on their intensions. In Proceedings of the 1990 ACM Conference on Lisp and Functional Programming, pages 341{348. ACM Press, 1990.

[NW93]

Gopalan Nadathur and Debra Sue Wilson. A notation for lambda terms I: A generalization of environments. Technical Report CS-1993-22, Department of Computer Science, Duke University, September 1993.

[Pau87]

Lawrence R. Paulson. The representation of logics in higher-order logic. Technical Report Number 113, University of Cambridge, Computer Laboratory, August 1987. 49

[Pau88]

L. Paulson. The foundations of a generic theorem prover. Technical Report Number 130, University of Cambridge, Computer Laboratory, March 1988.

[PE88]

Frank Pfenning and Conal Elliot. Higher-order abstract syntax. In Proceedings of the ACM-SIGPLAN Conference on Programming Language Design and Implementation, pages 199{208. ACM Press, June 1988.

[Pfe89]

Frank Pfenning. Elf: A language for logic de nition and veri ed metaprogramming. In Proceedings of the Fourth Annual Symposium on Logic in Computer Science, pages 313{322. IEEE Computer Society Press, June 1989.

50

Suggest Documents