Adding Algebraic Rewriting to the Untyped Lambda Calculus Daniel J. Dougherty Department of Mathematics Wesleyan University Middletown, CT 06457 USA
[email protected] Abstract
We investigate the system obtained by adding an algebraic rewriting system R to an untyped lambda calculus in which terms are formed using the function symbols from R as constants. On certain classes of terms, called here \stable", we prove that the resulting calculus is con uent if R is con uent, and terminating if R is terminating. The termination result has the corresponding theorems for several typed calculi as corollaries. The proof of the con uence result suggests a general method for proving con uence of typed reduction plus rewriting; we sketch the application to the polymorphic lambda calculus.
1 Introduction Term rewriting systems and the untyped lambda calculus are universal models of computation. Algebraic reduction is a natural technique for computing with standard functions such as successor and addition and with operations de ned by equations over an abstract data type, while the lambda calculus has proven to be a powerful model of several aspects of modern programming languages (e.g., programmer-de ned functions and their parameter passing mechanisms). It would seem pro table to combine the two modes, allowing each to do what it does best. For instance, as pointed out in [Bre88], algebraic rules such as rewriting x ? x to 0 could be treated as code optimizations in a functional language. From the point of view of the logic of programming, the equations from which rewriting rules are de ned should allow the use of rst order properties of the data to be involved in the higher order reasoning about programs. The following example (from [BM88]) shows that the combination of algebra and untyped lambda calculus is problematic. Suppose we have a system which allows any term x ? x to be rewritten to 0, and a term succ(x) ? x to be rewritten to 1, and further suppose that terms have xed points, so that there is a term X with X evaluating to succ(X ). Then X ? X evaluates to 0 and to 1. The insight in Val Breazu-Tannen's [Bre88] is that restriction to various type disciplines should allow lambda terms to inherit nice properties from the algebraic system. (See also [BM88], [Bre87]). In [Bre88], it was shown that if a con uent algebraic system is added to the simply typed lambda calculus, the resulting system combining and algebraic reductions is con uent. The question of preservation of termination was left open. Jean Gallier 1
Algebraic Rewriting and Lambda Calculus
2
and Breazu-Tannen (independently of the present work) have shown that the polymorphic lambda calculus remains con uent when enriched by a con uent algebraic system ([BG89]). The same methods are used there to show that termination is inherited. In this paper, we present some general results about the interaction between -reduction and term rewriting. The proofs are purely syntactic, and they do not rely on any speci c typing dicipline. Let be a set of function symbols with speci ed arities, x a set Vars = fvi ji 2 ! g of variables, and let () be the set of lambda terms over Vars [ . Each algebraic term f (A1 ; ; An) over corresponds, via currying, to a ()-term (fA1 An ), and so a R R over (). Write ?! system R of rewriting rules over induces a rewriting relation ?! R . for ?! [ ?! We will restrict attention to -strongly normalizing terms in an attempt to avoid the diculties arising from the existence of xed points. But it is easy to see that preservation of termination in a combined system requires some other restriction on the lambda terms considered. For example, if the only algebraic rule is \(fxx) ! x", which is clearly terminating, then the normal form f (x:xx)(x:xx)(x:xx) is R-in nite. A similar observation applies to con uence preservation. Furthermore, if the signature does not specify arities for the function symbols, then even in the absence of -reduction anomolies can occur when algebraic terms are curried. For example, the system R with rules f (x) ! g(x; x) and g(x) ! f (x; x) is trivially terminating in the algebraic setting, while (gx) ! (fxx) ! (gxxx) ! is an in nite R-reduction on ()-terms. (I am indebted to the referee for the latter observation and example.) We will not want to insist that function-arity is respected in the strictest sense, since we certainly wish to allow function symbols to occur (say, as arguments to higher-order procedures) without being instantiated by their arguments. But when the rewrite systen R is thought of as rewriting terms of base type, no function symbol should be presented with more arguments than its arity prescribes. Somewhat surprisingly, in the presence of -strong normalization this very elementary form of type-checking, which we may call \arity-checking", will suce to ensure inheritance of con uence or termination, without a commitment to a speci c type discipline.
De nition 1.1 A set S of terms is R-stable if R 1. S is closed under taking subterms and under ?! , 2. each term in S is strongly normalizing, and contains no subterm of the form (fA1 An ) where n is greater than the arity of f . Examples of R-stable sets include the sets of () terms which receive a type in the simply typed lambda calculus, polymorphic lambda calculi ([Gir71], [Gir72], [Rey74]), certain systems of dependent types ([Mac86]), and the Calculus of Constructions ([CH88]). When an R and an R-stable set are available from the context we may abuse notation and speak of \stable terms". Our main results are: R If R is con uent then ?! is con uent on R-stable terms.
Algebraic Rewriting and Lambda Calculus
3
R If R is terminating then ?! is terminating on R-stable terms.
The constructions can be brie y described as follows. To show preservation of con uence, we follow Breazu-Tannen [Bre88] in projecting R-reduction to R-reduction on -normal forms, but simplify and generalize his technique by passing to a bottom-up/parallel version of R-reduction which almost commutes, in a technical sense, with -reduction. This relation is similar to the relation used by Tait and Martin-Lof in their proof of -con uence for untyped lambda calculus. To analyze termination, we show that whenever M is not a -normal form then any R-reduction out of M can be projected along a properly chosen -reduction in such a way that if the rst reduction is in nite then so is the projection. We will be able to conclude that if M allows an in nite R-reduction then the -normal form of M will allow an in nite R-reduction { a contradiction if R is assumed to be a terminating rewrite system. A consequence of the approach we have adopted is that in order to derive the corresponding theorems about typed systems, the statements of the theorems above are not sucient (this is a familiar phenomenon; consider the con uence of the simply typed lambda calculus, which will not follow from the fact that the untyped lambda calculus is con uent, but which submits to exactly the same proof). By making essentially trivial modi cations to the proofs of the theorems given here, the reader may derive termination- and con uence-preservation results for simply typed and polymorphically typed lambda calculi. We omit a detailed treatment, but we do outline the the modi cations needed to address the Girard-Reynolds system 8. In [Klo80], Klop considers the addition of new rewriting rules to untyped lambda calculus, with restrictions on the form of the new rules (for example, that variables may not occur twice on the left side of a rule). We treat arbitrary algebraic rules. Toyama, in [Toy87], shows that the direct sum of con uent term rewriting systems is con uent, but the purely algebraic setting is very dierent from the present framework. The termination of a combination of terminating algebraic rewrite systems is a very delicate issue { Toyama presents several counterexamples in [Toy87]. Termination is known to persist in a combined system under various, somewhat restrictive, hypotheses ([Mid89], [Rus87], [TKB89]). We note in passing that the addition of constants and rules expressing surjective pairing on untyped terms can be cast as rewrite rules as above, but these are explicitly higher-order constructs and do not conform to our notion of \algebraic". It is well-known ([Klo80]) that the addition of these rules to the untyped calculus disturbs con uence, and that for simply typeable terms, con uence and termination are each preserved ([Pot81]). However, the techniques of the present paper do not shed any light on this situation, since arity-checking fails for simply typed terms with pairing. It may be that there is a generalization of the notion of stability which allows the present techniques to be adapted to such non-algebraic reduction. For basic de nitions and notation not explained below, see [Bar84] for the lambda calculus, and [HO80] for term rewriting.
Algebraic Rewriting and Lambda Calculus
Notation
4
Following a convention of algebraic rewriting, we identify each term with a partial function on sequences (here, f0; 1g-sequences). Thus, a variable or constant is identi ed with the function mapping the empty sequence to that variable or constant; (A0 A1) maps the empty sequence to a symbol for application, and maps the sequence iu to Ai (u); i 2 f0; 1g; and (xA) maps the empty sequence to a symbol for \x" and maps 0u to A(u). Sequences in the domain of a term A are called occurrences in A. If a and b are sequences with a an initial segment of b, we say that a precedes b and that b extends a; a and b are incomparable if neither extends the other. Suppose u is an occurrence in A. Then A=u is the term de ned by A=u(v ) = A(uv ); if B is of the form A=u, then B is a subterm of A (written B v A). The term A[u B ] is de ned by: A[u B ](w) = A(w) for occurrences w of A not extending u and A[u B ](uv ) = B (v ) for occurrences v of B . Any term is in one of the forms: x1 xn:hM1 Mm ; with n; m 0 and h 2 Vars [ , or x1 xn:(xM0)M1 Mm; with n 0 and m 1. A term is either a variable or a term (fA1 Am ), f 2 ; Ai 2 (); (0 i m). An algebraic term is either a variable or a term (fT1 Tm), f 2 ; m = the arity of f ; Ti algebraic for (0 i m). We will reserve S and T to stand for algebraic terms. Substitution into an algebraic term T is particularly simple since there is no variable binding in T . Since we will often have occassion to manipulate substitution instances of algebraic terms T , we adopt the following notational convention: If T is an algebraic term whose free variables are among the set fv1; : : :; vk g, and Q~ fQ1 ; : : :; Qk g is any multiset of () terms, then the result of simultaneously substituting each Qi for vi in T is denoted T Q~ . An algebraic rewrite system R is a set of pairs hS; T i of algebraic terms in which S is not a variable and Vars(T ) Vars(S ). Let P be any binary relation on terms. A P -reduction step from M to N is given by a pair hA; B i 2 P , an occurrence u of M , and a substitution such that M=u (A) and N M [u (B)]. We say that has source term A, and redex u. Any term (A) as above P holds from M to N if there is a P -reduction is called a P -redex term. The relation ?! P N ). from M to N (we write : M ?! P , we denote its re exive closure by ?! P , its re exive transitive closure by Given ?! P P P M ?! P ?! ?!, and its associated equivalence relation by !!. If we write : M0 ?! 1 then is a sequence of reduction steps hij0 i < ni for some n ! . P Y there is an A relation P is con uent, or Church-Rosser, if whenever X P?? M ?! ?! P N P?? Y . P is terminating, or strongly normalizing, if there are no N such that X ?! ?! P in nite ?! reductions. If P and Q are relations, we often write PQ for P [ Q.
Algebraic Rewriting and Lambda Calculus
5
2 Descendants and Projections The proofs of our preservation theorems proceed by isolating certain subterms of a term and analyzing reductions into steps which take place inside the given terms and other steps which are blind to the internal structure of those terms. In this section we develop some machinery enabling us to track the progress of subterms during a reduction. We use the notions of descendant of an occurrence with respect to an algebraic reduction (essentially as in [HL79]). R N have redex u and source term S . De nition 2.1 Let : M ?!
For an occurrence d of M , the set d= of descendants of d with respect to is the set of occurrences in N de ned as follows. If d does not extend u then d= = fdg. If d is uw, w a non-variable address of S , then d= = ;. Otherwise, writing d as uac, where S=a vi , d= is fua0cjT=a0 = vi g. S If D is a set of occurrences in M then D= is fd=jd 2 Dg. If = n 0 is a several step reduction then D= is ( (D=0)= =n ). When the descendant of a certain occurrence of subterm X is under consideration, we will often simply say \descendant of X". For example, suppose hfx; gxxi is a rule, and R h(g (ky )(ky )) N . Then ky has two descendants, consider the reduction M h(f (ky )) ?! the two occurrences of ky in N , and f (ky ) has one descendant, viz., g ((ky )(ky )) in N . We will pay particular attention to maximal non- subterms, the occurrences of which form the -boundary of a term.
De nition 2.2 The -boundary of M , O(M ), is the set of occurrences de ned as follows
(by induction on terms). If M is a variable then O(M ) is empty. If M is of the form fM1 Mm with f 2 , then d is in O(M ) i 9i 9d1 9d2; d = d1d2; M=d1 Mi; d2 2 O (Mi): Otherwise O (M ) contains the empty occurrence only (corresponding to the term M itself).
Lemma 2.3 If T is algebraic then d is in O (T Q~ ) i 9i 9d1 9d2; d = d1d2; T Q~ =d1 Qi; d2 2 O (Qi ): Proof. An easy induction on T . 2 R N , and let d be an occurrence in M . Lemma 2.4 Let : M ?! R N=e. 1. For each e 2 d=, M=d ?! R N=d. 2. If d precedes the redex of then M=d ?! 3. If M is stable and D is O (M ) then D= is O (N ).
Algebraic Rewriting and Lambda Calculus
6
Proof. The rst two assertions follow easily from the de nition of descendant. For part 3, use induction on M . If M is of the form x1 xn :(xM0)M1 Mm ; (n 0, m 1), then N is obtained by a rewrite inside one of the Mi, and the induction hypothesis
applies (since the same clause in the de nition of -boundary applies for N ). The same argument applies if M is x1 xn :hM1 Mm ; unless n = 0 and the redex of is not contained in some Mi . But in this case stability (arity-checking in particular) implies that M hM1 Mm is in fact S Q~ and N is T Q~ , for some rule hS; T i and some Q~ . The result then follows from Lemma 2.3 and the fact that no variables occur in T which are not in S .
2
To isolate the steps of a reduction which are independent of some particular subterms, we consider the term obtained by replacing those subterms by variables. We must do this with some care in order to preserve the rewriting relation.
De nition 2.5 An R-projection (or just projection, if R is available from the context) is
any function from terms to variables such that if M and N have a common R-reduct then they are assigned the same variable. Given a set D of pairwise incomparable occurrences in a term M , and a projection whose range is disjoint from the variables of M , write M for the term obtained from M by replacing M=d by (M=d) for each d 2 D, and say that M is a projection of M at D. R N , is a projection at D, and D= is pairwise incomparable, then is If : M ?! the projection at D= given by (N=e) (M=d); for each d 2 D and e 2 d=. We need Lemma 2.4.1 in order to justify the de nition of above. In order to ensure that D= is pairwise incomparable, it will suce (by Lemma 2.4.3) to choose D to be the boundary of M (it is clear that a -boundary is a pairwise incomparable set of occurrences). To go further and have the projection of a reduction induce a reduction on the projections, we must be careful to project on a suciently full set of occurrences, in the following sense:
De nition 2.6 If M=u is of the form T Q~ and D is a set of occurrences, then D is (T; u) full if no d 2 D is uw with w a non-variable occurrence in T , and for every d 2 D which is uac with T=a a variable, D contains each ua0 c for which T=a0 is the same variable. R N , have redex u and source term S , let D be pairwise incomLemma 2.7 Let : M ?! parable and (S; u) full, and suppose that M is a projection of M at D. Then R N : 1. M ?! R N . 2. If no d 2 D precedes u, then M ?! Proof. Let M=u be S Q~ for some Q~ . If no d 2 D precedes u then u is an occurrence in M . But since D is (S; u) full, M =u is of the form S Q~ 0 and N =u is of the form T Q~ 0 , proving
(2). On the other hand, if there is a d0 2 D preceding u, then D= = D and M N . So R N holds in any case. 2 M ?! Finally, in order to project an R reduction of several steps on D we must guarantee that its descendants will be full for the next step. This motivates the next result.
Algebraic Rewriting and Lambda Calculus
7
Lemma 2.8 If D is O(M ) then for any u such that M=u is of the form T Q~ , D is (T; u)-
full.
Proof. Let d 2 D be uac and let d0 = ua0c, with T=a and T=a0 the same variable; it suces
to check that there can be no non- occurrence above d0 . Indeed, if v 0 were one, then we could nd one above d as follows. Either v 0 would be above u, hence itself above d, or below a0 , in which case there would be a v below a and above d with M=v 0 M=v . This contradicts d 2 D. 2 Thus -boundaries are always sets of non- occurrences which are suciently full, and in the stable case their descendants inherit this property. These facts will enable us to iterate applications of Lemma 2.7 when we start with a projection of a -boundary. It will be important to isolate -redex subterms of a term which are contained in no other -redexes, and whose descendants are similarly maximal. Leftmost redexes have these properties under -reduction alone, but algebraic reduction can spoil leftmost-ness. So we need a generalization: De nition 2.9 An occurrence d is an outermost -redex occurrence of M if either M x1 xn:(xM0)M1 Mm; and d is the indicated occurrence of (xM0)M1, or M x1 xn:hM1 Mm with h 2 Vars [ , and 9i 9d1 9d2; d = d1d2; M=d1 Mi; d2 outermost in Mi: Of course, outermost redexes need not be in the -boundary of M .
Lemma 2.10 1. If T is algebraic then d is an outermost -redex in T Q~ i 9i 9d1 9d2; d = d1d2; T Q~ =d1 Qi; d2 outermost in O (Qi): 2. When M is stable, each R-descendant of an outermost -redex is an outermost redex. 3. When M is a stable -normal form each R-reduct of M is a -normal form.
Proof.
1. An easy induction on T . R N . If M x x :(xM )M M then N 2. By induction on M ; suppose M ?! 1 n 0 1 m has the same shape and the result is clear. When M x1 xn :hM1 Mm ; proceed using induction and part 1, (in the same manner as in the proof of Lemma 2.4.3).
2
R N . The only case 3. By induction on M . We may write M x1 xn :hM1 Mm ?! which does not submit immediately to the induction hypothesis is the one in which M hM1 Mm is S Q~ and N T Q~ . But part 1 and the fact that there are no variables in T which are not in S imply that there can be no (outermost) -redexes in N .
Algebraic Rewriting and Lambda Calculus
8
3 Termination In this section it will be shown that if a terminating algebraic rewriting system is added to the () calculus of -reduction, the resulting system is terminating on stable terms. We assume that all terms under consideration are stable. The rst step is to record some well-known results on -reduction which parallel some of the results of the previous section. The notions of residual of a -redex and of a development of a speci ed set of redexes are standard, and it turns out that we can con ne our attention to developing sets of incomparable -redexes. In the interest of maintaining a uniform terminology we will use \descendant" to refer to the image of an occurrence under either type of reduction. Hence: Notation 3.1 If : M ?! ?! , and D is a set of -redex occurrences in M , then the set D=, of descendants of D with respect to is the set of occurrences of residuals of the terms at D. Let D be a set of pairwise incomparable -redexes in M . Then '(D ; M ) is the term obtained from M by contracting those redexes. We say that '(D ; M ) is a development
of M .
Lemma 3.2 Let : M ?! N , and let d be an outermost -redex occurrence in M . 1. If d is the redex of then d= = ;, otherwise d= = fdg. 2. M=d ?! N=d. 3. If d strictly precedes the redex of , then M=d ?! N=d.
4. Each descendant of an outermost -redex is an outermost -redex.
Proof. The rst three parts are clear. The proof of part 4 is an induction on terms. Suppose M is x1 xn :(xM0)M1 Mm . If the redex of is (xM0)M1 itself there are
no descendants; otherwise the descendant of (xM0)M1 is in the same position in N and hence is outermost. When M is x1 xn :hM1 Mm ; apply induction. 2 The construction in the proof of termination for R involves choosing an outermost redex from the initial term of a reduction and developing it and all of its descendants. The next two results show that under the right conditions, such a development preserves ?! ?! R . The rst is a special case of the strong theorem (FD!) on nite developments. and ?! ?! In a redex term (xP )Q, call Q the argument term. Note that if D is a redex subterm of Q in (xP )Q, and if furthermore x 62 FV (P ), then a contraction of D is rendered moot by a subsequent contraction of (xP )Q. This possibility plays a role in the next two lemmas.
Lemma 3.3 Let : M ?! N . If D is a set of outermost redexes in M , and E is D=,
then
1. '(D; M ) ?! ?! '(E ; N ), and
Algebraic Rewriting and Lambda Calculus
9
2. if the redex of is neither an element of D nor an occurrence in an argument term of a redex from D then at least one reduction is done in '(D; M ) ?! ?! '(E ; N ).
Proof. 1. This is Lemma 11.1.7.(ii) of [Bar84]. 2. Assuming the redex u of is as described, u has at least one descendant relative to any reduction from M to '(D; M ). With this observation, the proof is, verbatim, the proof of Lemma 11.3.3 of [Bar84].
2
Now, if D is a set of outermost -redexes and is an algebraic reduction, then we know that the descendants are also outermost (hence incomparable) redexes, so it makes sense to develop them. This leads to an algebraic companion to Lemma 3.3, describing the interaction between algebraic reductions and developments. R N . If D is a set of outermost redexes in M Lemma 3.4 Let M be stable and : M ?! and E is D=, then R 1. '(D; M ) ?! ?! '(E ; N ), and 2. if the redex of is not an occurrence in an argument term of a redex from D then at R least one reduction is done in '(D; M ) ?! ?! '(E ; N ). Proof. Let have redex u and source term S . 1. We have two cases, de ned by the position of u with respect to D (of course u 2= D). If no d in D precedes u, expand D to the smallest (S; u) full set D+ containing D. Then D+ is still a set of outermost redexes (cf. Lemma 2.10.1) and D+ = is E . R N . Therefore Now if is any projection on D+ , Lemma 2.7 implies that M ?! by performing reductions in M before the R reduction we obtain '(D; M ) ?! ?! R '(E ; N ). '(D+; M ) ?! If the redex u extends some d0 2 D, then no element of D extends u, since they are all incomparable with d0 . Therefore D is trivially (S; u) full, each element of D is its own descendant, and we have two subcases as follows. Write M=d0 (xA)B , and M=u S Q~ . when S Q~ v A, M M [d0 (xA[u S Q~ ])B]; N M [d0 (xA[u T Q~ ])B]; '(D; M ) M 0[d0 (A[u S Q~ ])[x := B]]; '(E ; N ) M 0[d0 (A[u T Q~ ])[x := B]]; R '(E ; N ) by substitutivity of R. and '(D; M ) ?!
Algebraic Rewriting and Lambda Calculus
When S Q~ v B,
M M [d0 N M [d0
10 (xA)(B [u (xA)(B [u
S Q~ ])]; T Q~ ])];
'(D; M ) M 0 [d0
A[x := B[u
S Q~ ]]];
'(E ; N ) M 0 [d0
A[x := B[u
T Q~ ]]];
R '(E ; N ) by repeating the R-reduction for every free occurrence and '(D; M ) ?! ?! of x in A. 2. The second assertion can be seen by examining the cases in part 1 { the only case where collapsing might occur is in the last case, when x is not free in A.
2
We are now in a position to see that R-reduction is terminating on stable terms. It is convenient to treat pure R-reduction rst.
Theorem 3.5 If R is terminating on algebraic terms, then R is terminating on R-stable () terms.
Proof. For the sake of contradiction, let M be a stable term of minimal size among those
which are R-in nite. By hypothesis, M cannot be algebraic. Suppose M were not a term. Then M would be one of xP1 Pn , or (x:P1)P2 Pn , (n > 0), each R-reduct would be of the same shape, so that some Pi would be R-in nite, contradicting the minimality of M . So let D be O (M ) and let be a projection on D which replaces all subterms by the same variable. Since M is a term, each subterm represented in D is smaller than M , and since M is not algebraic, M is smaller than M . R M ?! R be an in nite R reduction, set D D, D Now let : M0 ?! 1 0 n+1 Dn =n, set 0 , n+1 n n and construct the sequence of terms Mnn . By Lemma 2.7.1, R M n+1 for each n. Mnn ?! n+1 Since M is smaller than M , the sequence above is nite as a reduction sequence, so R M n+1 fails for all n k. that for some k, Mnn ?! n+1 R M For n k, Lemma 2.7.2 applied to the reduction n : Mn ?! n+1 yields a dn 2 Dn preceding the redex of n . It follows that for n k, Dn =n = Dk . Furthermore, there must be a particular d 2 Dk such that for in nitely many n, d precedes the redex of n . Thus R in nite, contradicting the minimality of M . 2 M=d must be ?!
Theorem 3.6 If R is terminating on algebraic terms, then R is terminating on R-stable () terms.
Algebraic Rewriting and Lambda Calculus
11
Proof. The proof is by induction on the maximum number of steps which can occur in a R R reduction of a stable term M . For the sake of contradiction, let : M M0 ?! M1 ?! be an in nite reduction. When M is a -normal form, Lemma 2.10.3 implies that each n is an R-reduction, so is nite by Theorem 3.5. So let d0 be the leftmost -redex in M0 , M=d0 (xP0)Q0. This is certainly outermost. Since stability is inherited by subterms, the induction hypothesis applies to Q0, so Q0 is R terminating. Set D0 = fd0g; Dn+1 = Dn =n . Each Dn is a set of outermost redexes by Lemmas 2.4.3 and 3.2.4, hence is pairwise incomparable. Lemmas 3.3.1 and 3.4.1 imply that R '(Dn; Mn) ?! ?! '(Dn+1 ; Mn+1) for each n, but by induction, '(fd0g; M ) is R-terminating, R so this is nite as a ?! reduction. By Lemmas 3.3.2 and 3.4.2, from some point on each n -redex term is either equal to some -redex term from Dn , or is a subterm of the argument part of such a term. A reduction n of the rst type results in Dn+1 being smaller than Dn , while one of the second type yields Dn+1 the same size as Dn , so eventually every reduction is of the second type. That is, there is a k such that for n k each n has its redex term inside the Q of some term (xP )Q occurring in Dn . Just as in the previous theorem, for n k, Dn =n = Dk , and there is a particular d 2 Dk such that for in nitely many n, d precedes the redex of n . R Now, Mk =d is of the form (xPk )Qk , (xP0)Q0 ?! ?! (xPk )Qk , and no step in this re R duction occurs at the root of a term, so in fact Q0 ?! ?! Qk . The previous paragraph showed that Qk is R-in nite, so we have a contradiction of the fact that Q0 is R terminating.
2
It follows that adding terminating algebraic rules to the simply or polymorphically typed lambda calculus results in a terminating system, since an in nite reduction in the calculus would induce an in nite reduction in the R system de ned by erasing the types from terms. A dierent proof of this fact, obtained independently, is found in [BG89] . Indeed, any strongly normalizing typed lambda calculus which admits a notion of type erasure so that reductions induce reductions on the untyped erasures, and in which there can be no in nite R reductions which are invisible to the erasures, (for example, within the types themselves), will remain strongly normalizing if a terminating set of rules is added.
4 Con uence This section shows that when con uent algebraic rewriting is combined with reduction, con uence is inherited by stable terms. We again restrict attention to stable terms. As pointed out in [Bre88], we cannot expect con uence in the presence of reduction: R a, then x:fx has the two R normal forms x:a and f . if fx ?! We rst verify that a con uent algebraic system R remains con uent when extended to the expanded set of terms (). The global strategy in the proof of Theorem 4.1 (projecting R-reductions to R-reductions on -normal forms) was used in [Bre88] in the simply typed setting; we avoid the use of types in the argument.
Algebraic Rewriting and Lambda Calculus
12
Theorem 4.1 If R is con uent on algebraic terms, then R is con uent on R-stable () terms.
R Y then there Proof. We show by induction on stable terms M that if X R?? M ?! ?! R N R?? Y . If M is algebraic, con uence holds by hypothesis. If exists N such that X ?! ?! M is itself not a term, then M can be written as one of xP1 Pn or (x:P1)P2 Pn,
(n > 0), and X and Y must have the same shape, so we can build N using the induction hypothesis on the Pi . So suppose M is a non-algebraic term, let D be O (M ), and let R? be the relation R Ag. Since M is a term, each M=d for d 2 D is R restricted to fAj9d 2 D; M=d ?! ?! smaller than M . By the induction hypothesis, R con uence holds out of M=d when d 2 D, and it follows that R con uence holds out of every term in the domain of R? . So R? is a con uent relation. Let be de ned over D so that terms M=d and M=e are replaced by the same variable ? if and only if M=d R! ! M=e. This is an R-projection. Since M is not algebraic, M is smaller than M . By iterating Lemma 2.7 we can project the two reductions on D and its descendants, R X 0 and M ?! R Y 0 . By the induction hypothesis applied to M there obtaining M ?! ?! ?! R R exists N 0 with X 0 ?! ?! N 0 ?? Y 0. 0 The terms N , X 0, and Y 0 are obtained from M , X , and Y respectively by replacing subterms by new variables. We build our desired N by nding appropriate terms to substitute for these variables in N 0. Consider one of the new variables z and let A1; : : :Al be the subterms of X replaced by z to give X 0, and B1 ; : : :Bm the subterms of Y replaced by z to give Y 0. Each Ai is a reduct of some M=d with d 2 D, and the same holds for each Bj . By the con uence of R? , we can produce a term C(z) which is a common R-reduct of each Ai and Bj . When this has been done for each z , take N to be N 0 with each z replaced by C(z) . R X 0[~z := C ~ (z) ] by rewriting the various Ai to C(z) for each z and Now we have X ?! ?! R R N 0[~z := C ~ (z) ] and Ai as above. Similarly, Y ?! ?! Y 0[~z := C~ (z)]: Finally, X 0[~z := C~ (z)] ?! ?! R N 0[~z := C ~ (z) ] by substitutivity of R. Thus N 0[~z := C~ (z) ] is the desired N . Y 0 [~z := C~ (z) ] ?! ?!
2
To lift this result to full R reduction, we attempt to project reductions to reductions on normal forms (the latter reductions will be purely algebraic if the original term is stable). Now, R reductions will not commute directly with reductions, but the relation R de ned below almost commutes with ?! ?! ?! . The technique is inspired by a proof of the 1 con uence of ?! due to Tait and Martin-Lof. R is de ned inductively as follows: De nition 4.2 The relation ?! ?! 1 R M. 1. M ?! ?! 1 R M 0 and N ?! R N 0 then MN ?! R M 0N 0 . 2. If M ?! ?! ?! ?! 1 1 1 R M 0 then x:M ?! R x:M 0. 3. If M ?! ?! ?! 1 1
Algebraic Rewriting and Lambda Calculus
13
R T Q~ . R Q , then S P~ ?! ?! 4. If hS; T i 2 R, and for 1 i n; Pi ?! ?! 1 1 i R is sometimes known as the walk relation. The relation ?! ?! 1 R ?! R ?! R . Lemma 4.3 ?! ?! ?! 1
Proof. The rst is clear from (1) and (4) of De nition 4.2, the second is an easy induction R . over ?! ?! 1 R B implies M [x := A] ?! R M [x := B ]. Lemma 4.4 A ?! ?! ?! 1 1 Proof. An easy induction on M . R B and M ?! R N imply M [x := A] ?! R N [x := B ]. Lemma 4.5 A ?! ?! ?! ?! 1 1 1 R N . When M N , use the previous Proof. By induction on the derivation of M ?! ?! 1 R N follows from parts 2 or 3 of De nition 4.2, are easy. lemma. The cases when M ?! ?! 1 R T Q~ N , use the facts that M [x := A] is S P [x:=A] and N [x := B ] is When M S P~ ?! ?! 1 R Q [x := A] by the induction hypothesis. 2 T Q[x:=B] , and that Pi [x := A] ?! ?! 1 i R is that we can project and develop a single step ?! R The important feature of ?! ?! ?! 1 1 R reduction, as follows. reduction to a single step ?! ?! 1 R N , then there Proposition 4.6 Let M be any () term. If : M ?! X and M ?! ?! 1 R Z and N ?! are X 0 and Z such that X ?! ?! X 0 ?! ?! ?! Z . 1 R N. Proof. By induction on the derivation of M ?! ?! 1 1. M N : trivial. R (N N ) N , with each M ?! R 2. M (M1 M2 ) ?! ?! 1 1 2 i ?!1 Ni: There are two subcases. (a) If the redex term of is a subterm of , (say), M1 , then X (X1X2) with R Z and M1 ?! X1. By induction, there exists X10 and Z1 with X1 ?! ?! X10 ?! ?! 1 1 N1 ?! ?! Z1 . We can then take Z (Z1 N2): (b) If the redex term of is M itself, then M (xP )M2; X P [x := M2 ], with R N and M ?! R R ?! R xP ?! ?! ?! 1 1 2 ?!1 N2. Since ?! 1 ?!, N1 is of the form xQ with R Q. Then we can take Z to be Q[x := N ] and invoke Lemma 4.5. P ?! ?! 1 2 R xN N : similar to part 2a above. 3. M xM1 ?! ?! 1 1 R T Q~ N , with each P ?! R 4. M S P~ ?! ?! 1 i ?!1 Qi : If (xA)B is the redex term in then there is an i such that (xA)B v Pi . For this i, we have a -reduction out of Pi and R Q , so by induction there are P 0 and Q0 , with P ?! ?! Q0i , and Pi ?! ?! i ?! Pi0 , Qi ?! 1 i i i R ?!1 Q0i. Let P~ 0 denote the sequence of terms obtained from P~ by replacing Pi by Pi0 ?!
Algebraic Rewriting and Lambda Calculus
14
Pi0 , and take X 0 to be S P~0 . Let Q~ 0 denote the sequence of terms obtained from Q~ by P~ 0 replacing Qi by Q0i , and take Z 0 to be T Q~ 0 . Then X S P~ ?! ?! S X 0 by suitably R Z since each element of P 0 reduces reducing all occurrences of Pi to Pi0 , and X 0 ?! ?! 1 R 0 via ?! ?!1 to the corresponding element of Q .
2
Preservation of con uence now follows.
Theorem 4.7 If R is con uent on algebraic terms, then R is con uent over R-stable () terms.
Proof. Write nf (A) for the -normal form of a term A. We rst show, by induction R N then nf (M ) ?! R nf (N ). If M is a along ?! , that when M is stable and M ?! ?! ?! 1 1 -normal form then by Lemma 2.10 .3 so is N . Otherwise, let M ?! X be any reduction,
de ne X 0 and Z as in Proposition 4.6, and apply the induction hypothesis to the instance R Z. X 0 ?! ?! 1 R N imply that nf (M ) ?! R nf (N ). It follows that M stable and M ?! ?! R Now, to show con uence, suppose M is R stable, with A R ?? M ?! ?! B. Then R R R nf (A) ?? nf (M ) ?! ?! nf (B). Con uence of R on () yields P such that nf (A) ?! ?! R P R?? nf (B), so that A ?! ?! P R ?? B as desired. 2 The proofs above suggest an approach to proving inheritence of con uence in ( -strongly normalizing) typed R systems such as those using a polymorphic type discipline, (or a system of dependent types). The preservation of R-con uence on the set () should hold just as in Theorem 4.1 above. In these systems, reductions explicitly involving types are + de ned, so in such a system let ?! stand for term -reduction together with type reduction. The set of terms which type-check will be stable in the sense obtained by replacing by R is expanded so that ?! R + in De nition 1.1. When part 2 of the de nition of ?! ?! ?! 1 1 is compatible with all of the term-forming operations, it suces to prove a version of Proposition 4.6 in which there is replaced by +. 0 For example, in the Girard-Reynolds system 8 of polymorphic types, let ?! be reduction on types.0 When algebraic rewriting is added, stability of type-checking terms (with respect to ?! ) is clear, and the proof of algebraic con uence on type checking terms R is expanded so that A ?! R B imis exactly as in Theorem 4.1. When the de niton of ?! ?! ?! 1 1 R R plies (tA) ?! ?!1 (tB) and (A ?! ?!1 B ), for type variables t and types , Proposition 4.6 0 holds when the relation ?! there is replaced by ?! [?!. Preservation of con uence then follows just as in Theorem 4.7. Acknowledgements. I am indebted to Val Breazu-Tannen and Jean Gallier for illuminating discussions about the topics of this work, and to Patricia Johann for careful readings and suggestions for improvement. The referees helped to clarify the notion of stability and provided a substantial number of corrections.
Algebraic Rewriting and Lambda Calculus
15
References [Bar84] H. P. Barendregt. The Lambda Calculus: Its Syntax and Semantics. North-Holland, Amsterdam, 1981, revised 1984. [Bre87] V. Breazu-Tannen. Conservative extensions of type theories. dissertation, Massachusetts Institute of Technology, 1987. [Bre88] V. Breazu-Tannen. Combining algebra and higher-order types, in Proceedings of the Third Annual Symposium on Logic in Computer Science, pp. 82- 90, 1988. [BG89] V. Breazu-Tannen and J. Gallier. Polymorphic rewriting conserves algebraic strong normalization and con uence, in Proceedings, 16th ICALP, Springer- Verlag, 1989. [BM88] V. Breazu-Tannen and A. R. Meyer. Computable values can be classical, in Proceedings of the Second Annual Symposium on Logic in Computer Science, pp. 238245, 1988. [CH88] T. Coquand and G. Huet. The Calculus of Constructions, Information and Control, v.76, no.2/3, pp. 95-120, 1988. [Der87] N. Dershowitz. Termination of rewriting, J. Symbolic Computation 3, pp. 69-116, 1987. [Gir71] J-Y. Girard. Une extension de l'interpretation de Godel a l'analyse, et son application a l'elimination des coupures dans l'analyse et la theorie des types, in Proc. Second Scandinavian Logic Symposium, ed. J.E. Fenstad, North-Holland, Amsterdam, 1971. [Gir72] J-Y. Girard. Interpretation functionelle et elimination des coupures de l'arithmetique d'ordre superieur, These D'Etat, Universite Paris VII, 1972. [Hue80] G. Huet. Con uent reductions: abstract properties and applications to term rewriting systems, JACM 27, pp. 797-821, 1980. [HL79] G. Huet, J.J. Levy. Call by need computations in non-ambiguous linear term rewriting systems, Rapport Laboria 359, INRIA, 979. [HO80] G. Huet, D. Oppen. Equations and rewrite rules: a survey, in Formal Languages: Perspectives and Open Problems, ed. R. Book, Academic Press, New York, 1980. [Klo80] J. W. Klop. Combinatory Reduction Systems, Mathematical Center Tracts 127, Amsterdam, 1980. [Mac86] D. B. MacQueen. Using dependent types to express modular structure, in Conference Record of the Thirteenth Annual ACM Symposium on Principles of Programming Languages, pp. 277-286, 1986. [Mid89] A. Middeldorp. Modular aspects of properties of term rewriting systems related to normal forms, in Proc. Third International Conference on Rewriting Techniques and Applications, Springer-Verlag LNCS 355, pp. 263-277, 1989.
Algebraic Rewriting and Lambda Calculus
16
[Pot81] G. Pottinger. The Church-Rosser theorem for the typed calculus with surjective pairing, Notre Dame Journal of Formal Logic, v. 22, no. 3, pp. 264-268, 1981. [Rey74] J. C. Reynolds. Towards a theory of type structure, in Proc. Colloque sur la Programmation, Springer-Verlag LNCS 19, pp. 408-425, 1974. [Rus87] M. Rusinowitch. On termination of the direct sum of term rewriting systems, Information Processing Letters 26 pp.65-70, 1987. [Toy87] Y. Toyama. On the Church-Rosser property for the direct sum of term rewriting systems, Journal of the ACM, v.34, no.1, pp.128- 143, 1987. [TKB89] Y. Toyama, J. W. Klop and H. Barendregt. Termination for the direct sum of leftlinear term rewriting systems, in Proc. Third International Conference on Rewriting Techniques and Applications, Springer-Verlag LNCS 355, pp. 477-491, 1989.