scieee AI-readable full text Open interactive document viewer

Normal higher-order termination

Jouannaud, Jean Pierre,Rubio Gimeno, Alberto

Abstract

We extend the termination proof methods based on reduction orderings to higher-order rewriting systems based on higher-order pattern matching. We accommodate, on the one hand, a weakly polymorphic, algebraic extension of Church's simply typed λ-calculus and, on the other hand, any use of eta, as a reduction, as an expansion, or as an equation. The user's rules may be of any type in this type system, either a base, functional, or weakly polymorphic type.

Full text

A Normal Higher-Order Termination Jean-Pierre Jouannaud, LIX, ´ Ecole Polytechnique, Palaiseau, France Albert Rubio, Technical University of Catalonia, Barcelona, Spain We extend the termination proof methods based on reduction orderings to higher-order rewriting systems based on higherorder pattern matching. We accommodate, on the one hand, a weakly polymorphic, algebraic extension of Church’s simply typed λ-calculus, and on the other hand, any use of eta, as a reduction, as an expansion or as an equation. The user’s rules may be of any type in this type system, either a base, functional, or weakly polymorphic type. Categories and Subject Descriptors: F.4.1 [Mathematical Logic]: Lambda calculus and related systems; F.4.2 [Grammars and Other Rewriting Systems] General Terms: Theory Additional Key Words and Phrases: Higher-order rewriting, higher-order orderings, higher-order patterns, typed lambda calculus ACM Reference Format: Jean-Pierre Jouannaud and Albert Rubio. 2015. Normal Higher-Order Termination ACM V, N, Article A (January YYYY), 46 pages. DOI:http://dx.doi.org/10.1145/0000000.0000000 1. INTRODUCTION Rewrite rules are used in logical systems to describe computations over lambdaterms used as a suitable abstract syntax for encoding functional objects like programs or specifications. This approach has been pioneered in this context by Nipkow [Mayr and Nipkow 1998] and is available in Isabelle [Nipkow and Paulson 1992]. Its main feature is the use of higher-order pattern matching for firing rules. A generalization of Nipkow’s setting allows using rewrite rules of higher type [Middeldorp et al. 2005; Jouannaud and Li 2012]. Besides, it is shown there that using the extensionality rule as an expansion [Nipkow 1991] or as a reduction [Jouannaud 2005] yields very similar confluence checks based on higher-order critical pairs. A first contribution of this paper is a general setting for addressing termination of all variants of higher-order rewriting ` a la Nipkow, called normal termination, thanks to the notion of a normal higher-order reduction ordering. While higherorder reduction orderings target termination of higher-order rewriting based on plain pattern matching, hence must include β-reductions, normal higher-order or- This work has been partly carried out in the INRIA-LIAMA project Formes and the UPC projects SweetLogics and DAMAS, supported by Spanish MEC/MICINN grants TIN2010-21062-C02-01 and TIN2013-45732-C4-3-P respectively. The first author is now Professor emeritus at Universit´ e Paris-Sud, although still working at ´ Ecole Polytechnique. This paper is based on a preliminary version published by the same authors in the Proceedings of the 17th International Conference on Term Rewriting and Applications. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies show this notice on the first page or initial screen of a display along with the full citation. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, to republish, to post on servers, to redistribute to lists, or to use any component of this work in other works requires prior specific permission and/or a fee. Permissions may be requested from Publications Dept., ACM, Inc., 2 Penn Plaza, Suite 701, New York, NY 10121-0701 USA, fax +1 (212) 869-0481, or [email protected]g. c YYYY ACM 0000-0000/YYYY/01-ARTA $15.00 DOI:http://dx.doi.org/10.1145/0000000.0000000 A:2 J.-P. Jouannaud and A. Rubio derings target termination of higher-order rewriting based on higher-order pattern matching, hence must be compatible with βη-equality. We show however that, in general, monotonicity, stability, compatibility and well-foundedness cannot be satisfied at the same time. This leads to the definition that higher-order rewrite orderings are normal when they include a subrelation enjoying some stability property for terms in βη-normal form. Checking the rewrite rules against such a subrelation then ensures normal termination. How to build normal higher-order reduction orderings from higher-order reduction orderings is our second contribution, that we illustrate with the case of the higher-order recursive path ordering HORPO [Jouannaud and Rubio 2007], yielding its normal version NHORPO. Some examples of use are presented. The method would also apply to other similar orders, such as CPO [Blanqui et al. 2008]. This method does not work well in the presence of abstractions in the righthand side of rules. Our third contribution is an interpretation-based framework that solves this problem, at an abstract level first, where the properties of such interpretations are specified which imply preservation of normal termination, and then at a concrete level, by defining a particular termination-preserving interpretation, neutralization, whose role is to remove abstractions that can never be applied. Combining neutralization with NHORPO allows us to solve complex examples from the literature. So far, these methods target the use of higher-order reduction orderings. In the recent years, other methods have popped up that allow us to show higher-order termination, like higher-order dependency pairs. The question arises whether these methods that aim at proving higher-order termination can also be turned into methods aiming at proving normal higher-order termination? Our fourth contribution is a transformation of a higher-order rewrite system Rinto a new system R0contain- ing β-reductions, such that Ris normal terminating if R0is terminating. Our second contribution then appears as a refinement of the latter when using a method based on a higher-order reduction ordering like HORPO. We describe our framework for simply typed terms in Section 2, and for higherorder rewriting in Section 3. NHORPO is given in Section 4 and illustrated by an example. Termination-preserving interpretations, including neutralization, are introduced and studied in Section 5. Section 6 covers weak polymorphism. Our implementation is described briefly in Section 7, several examples are carried out there. General transformations reducing normal higher-order termination to higherorder termination are introduced and illustrated in Section 8. The relevant literature is discussed in Section 9 where it is compared with the present work whose significance is discussed at this occasion. Brief concluding remarks are given in Section 10. Readers are assumed familiar with the basics of term rewriting [Dershowitz and Jouannaud 1990; Terese 2003] and typed lambda calculi [Barendregt 1993]. ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. Normal Higher-Order Termination A:3 2. HIGHER-ORDER ALGEBRAS This section recalls the notion of monomorphic higher-order algebras (HOAs). We discuss in Section 6 how our results can be lifted to polymorphic higher-order algebras. Both frameworks originate from [Jouannaud and Rubio 2007]. 2.1. Types and Signatures Given a set Sof sort symbols of a fixed arity, denoted by s:∗n⇒ ∗, the set TSof simple types is generated by the following grammar: TS:= s(Tn S)|(TS→ TS)for s:∗n⇒ ∗ ∈ S Types are functional when headed by the →symbol, and data types when headed by a sort symbol. As usual, →associates to the right. We will often make explicit the functional structure of an arbitrary type τby writing it in the canonical form σ1→. . . →σn→σ, with n≥0, where nis the arity of τand σis a data type called canonical output type of τ. We use σ, τ, ρ, θ for arbitrary types. We are given a set of function symbols denoted by the letters f, g, h, which are meant to be algebraic operators equipped with a fixed number nof arguments (called the arity) of respective types σ1∈ TS, . . . , σn∈ TS, and output type σ∈ TS. Let F=Uσ1,...,σn,σ Fσ1×...×σn⇒σbe the set of all function symbols. The membership of a given function symbol fto a set Fσ1×...×σn⇒σis called a type declaration and written f:σ1×. . . ×σn⇒σ. In case n= 0, the declaration is written f:σ. A type declaration is first-order if its constituent types are data types, and higherorder otherwise. Fis said to be first-order if all its type declarations are first-order, and higher-order otherwise. The pair S;Fis called the signature. We say that the signature is first-order, higher-order when Fsatisfies the corresponding property. 2.2. Raw terms The set T(F,X)of raw algebraic λ-terms is generated from the signature Fand a denumerable set Xof variables according to the following grammar rules: T:= X | (λX:TS.T)|@(T,T)| F(T,...,T). Raw terms of the forms λx :σ.u,@(u, v), and f(t1, . . . , un)are respectively called abstractions, applications, and pre-algebraic. As a matter of convenience, we may sometimes omit the type σin λx :σ.u and write @(u, v1, . . . , vn)for @(. . . @(u, v1), . . . , vn), assuming n≥1. Our syntax requires using explicit applications for variables, since they cannot take arguments. In the examples, we shall however allow ourselves writing X(v1, . . . , vn)for @(X, v1, . . . , vn)when Xis a higher-order variable. On the other hand, function symbols have arities, eliminating the need for an explicit application of a function symbol to its arguments. Currying a function symbol is possible by taking zero for its arity. ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. A:4 J.-P. Jouannaud and A. Rubio Raw terms are identified with finite labeled trees by considering λx :σ. , for each variable xand type σ, as a unary function symbol taking a raw term uas argument to construct the raw term λx :σ.u. We denote the set of free variables of the raw term tby Var(t), and its size (the number of symbols occurring in t) by |t|. A raw term is closed if it has no free variable. The notation swill be ambiguously used to denote a list, or a multiset, or a set of raw terms s1, . . . , sn. Positions are strings of positive integers. Λand ·denote respectively the empty string (root position) and the concatenation of strings. We use Pos(t)for the set of positions in t, and >for the prefix ordering on positions. Incomparable positions are called disjoint. The subterm of tat position pis denoted by t|p, and we write tt|pfor the subterm relationship. The result of replacing t|pat position pin t by uis denoted by t[u]p, and we denote it by t[u]p1...pnif the same replacement is made in several positions p1. . . pnat once. Accordingly, we use t[x:σ]pfor a raw term with a unique hole of type σat position p, called a context (and respectively t[x:σ]p1...pnwhen we have several holes). Both pand x:σmay be omitted. 2.3. Typing Definition 2.1.An environment Γis a finite set of pairs written as {x1: σ1, . . . , xn:σn}such that xi∈ X,σi∈ TS, and xi6=xjfor i6=j. Var(Γ) = {x1, . . . , xn}is the set of variables of Γ. We may write Γ(xi)for σi. Given two environments Γand Γ0, their composition is the environment Γ·Γ0= Γ0∪ {x:σ∈Γ|x6∈ Var(Γ0)}.Γ0is compatible with Γif Γ·Γ0= Γ ∪Γ0. We now collect all declarations into a single environment Σ; Γ, of which the signature Σ = S;Fis the fixed part. We assume that there is exactly one declaration for each symbol in an environment Σ; Γ. We may omit Σin our examples, using the word environment for Γalone. Typing rules restrict the set of raw terms by constraining them to follow a precise discipline. Our typing judgments are written as Γ`Σs:σ, and read “shas type σ in the environment Γ”. The typing judgments are displayed in Figure 1. Variables: x:σ∈Γ Γ` Σx:σ Abstraction: Γ· {x:σ} ` Σt:τ Γ` Σ(λx :σ.t) : σ→τ Application: Γ` Σs:τ→σΓ` Σt:τ Γ` Σ@(s, t) : σ Functions: f:σ1×. . . ×σn⇒σ∈ F Γ` Σt1:σ1. . . Γ` Σtn:σn Γ` Σf(t1, . . . , tn) : σ Fig. 1. Typing judgments in higher-order algebras Given an environment Σ; Γ, a raw term shas type σif the judgment Γ`Σs:σ is provable in our type system. Given an environment Σ; Γ, a raw term sis typable, ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. Normal Higher-Order Termination A:5 and said to be a term if there exists a type σsuch that Γ`Σs:σ. Classically, we consider the proof of a given judgment as a tree whose nodes are labeled by the judgments derived in the proof and the edges by the names of the rules used in the proof. We use as usual Pos(P)for the set of positions of the proof tree P. We can therefore speak of the rule used at a position p∈ Pos(P)whose conclusion labels the node at pwhile its premises, displayed above the conclusion, label its sons. We shall use without mentioning that a term has a unique type in a given environment. We further recall two important typing properties of HOAs used later: LEMMA 2.2 (SUBTERM).Let Pdenote the proof of the judgment Γ`Σs:σ and pbe a position in Pos(s). Then, there exists a unique position q∈ Pos(P) such that the subproof P|qis a proof of a judgment of the form Γs|p`Σs|p:τin which τis the type of s|pin Γs|p. LEMMA 2.3 (REPLACEMENT).Assume given an environment Σ; Γ, two terms sand v, two types σand τ, and a position p∈ Pos(s)such that Γ`Σs:σ, Γs|p`Σs|p:τ, and Γs|p`Σv:τ. Then, Γ`Σs[v]p:σ. 2.4. Substitutions Definition 2.4.Asubstitution γ={x17→ t1, . . . , xn7→ tn}is a finite set of pairs made of a variable in its domain Dom(γ) = {x1, . . . , xn}and a term in its range Ran(γ) = {t1, . . . , tn}, such that ∀i∈[1..n],ti6=xiand ∀i6=j∈[1..n], xi6=xj. We denote by γ\Vthe restriction of γto the variables in Var(γ)\V. We use the letter γfor substitutions and postfix notation for their application. Definition 2.5.A substitution γoperates as an endomorphism on sand yields the term sγ defined as: If s=x∈ X and x6∈ Var(γ)then sγ =x If s=x∈ X and x7→ t∈γthen sγ =t If s= @(u, v)then sγ = @(uγ, vγ) If s=f(u1, . . . , un)then sγ =f(u1γ,...,unγ) If s=λx :τ.u then sγ =λz :τ.uγx where γx=γ\{x}∪ {x7→ z}for some fresh variable z. A notion of typed substitution is used in [Jouannaud and Rubio 2007]. The present more convenient untyped notion is borrowed from [Blanqui et al. 2013]. As expected, substitutions preserve typability: LEMMA 2.6 (TYPE INVARIANCE).Let s, t be two terms and γbe a substitution such that t=sγ. Then, (i) if Γ`Σs:σand ∀x∈ Var(s),Θ`Σxγ : Γ(x)for some environments Γ,Θ and type σ, then Θ`Σsγ :σ. (ii) if Γ`Σt:τfor some environment Γand type τ, then there is some environment Θsuch that Θ`Σs:τand Γ`Σxγ : Θ(x)for all x∈ Var(s). ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. A:6 J.-P. Jouannaud and A. Rubio 2.5. Conversions The following three equations originate from the (pure) λ-calculus, and are called α-, β- and η-equality respectively: λx :α.u =αλy :α.u{x7→ y}if y6∈ BV ar(v)∪(Var(v)\ {x}) @(λx :σ.v, w) =βv{x7→ w} λx :σ.@(u, x) =ηuif x6∈ Var(u) (usually called extensionality) In the above equations, u, v and wstand for arbitrary terms to which substitutions {x7→ y}and {x7→ u}apply. We consider α-convertible terms as identical, and therefore omit α-conversions in the sequel. Both βand η-equalities can be oriented as rewrite rules. There are two possible choices for rewriting with η, either as a reduction (from left to right) or as an expansion (from right to left), in which case termination is ensured by restricting its use to positions other than the first argument of an application. Typed lambda-calculi have all termination and Church-Rosser properties one may need, as recalled below. We consistently use the following important notations: u−→βvfor one β-rewrite step, u−→∗ βvfor its reflexive, transitive closure, and =βfor its symmetric, reflexive, transitive closure, also called β-equality. Similarly u−→ηv,u−→∗ ηv, and =η are the corresponding notations for ηused as a reduction. We also use u↓β,u↓η and u↓βη for the β-normal form, η-normal form and βη-normal form of a typable term u. The notation u↓is exclusively used as a short form of u↓βη. We say that a term is β-normal (resp., η-normal, normal) if it is in normal form for β-reduction (resp., η-reduction, βη-reductions). We shall not need a notation for η-expansion, η-long forms or β-normal η-long forms. We shall use, possibly without mentioning, several related properties of the simply typed λ-calculus: (1) Subject reduction: Assume Γ`Σs:σand s−→βt. Then, Γ`Σt:σ. (2) η-preservation: Assume Γ`Σs:σand s←→ηt. Then, Γ`Σt:σ. (3) Postponement of η:(∀u, v, w)u=ηv−→βw,(∃v0)u−→∗ βv0=ηw. (4) Church-Rosser property (i) of β-reductions: s=βtimplies s↓β=t↓β (5) Church-Rosser property (ii) of η-reductions: s=ηtimplies s↓η=t↓η (6) Church-Rosser property (iii) of β-reductions modulo η:s=βη tiff s↓β=ηt↓β (7) Church-Rosser property (iv) of βη-reductions: s=βη tiff s↓=t↓ (8) Closedness of η-normal forms by instantiation: (tγ)↓η=t↓ηγ↓η, a consequence of (5), since variables bound in tcannot occur free in γ. 3. NORMAL HIGHER-ORDER REWRITING OF HIGHER TYPE Introduced in [Nipkow 1991], normal higher-order rewriting allows defining computations over λ-terms used as a suitable abstract syntax for encoding functional objects like programs or specifications. It differs from the more traditional definition of plain higher-order rewriting from [Jouannaud and Okada 1991]: ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. Normal Higher-Order Termination A:7 Definition 3.1.Arewrite rule is a pair of terms written l→r, such that l6∈ X, Var(r)⊆ Var(l)and (∀Θ, θ, γ) Θ `Σlγ :θiff Θ`Σrγ :θ. A higher-order term rewriting system is a set of rules containing both rules βand η. Given a higher-order term rewriting system R, an environment Γ, two higherorder terms sand t, and a type σsuch that Γ`Σs:σ, we say that srewrites to tat position pwith the rule l→rand the term substitution γ, written Γ`s−→p Rt, or s−→p Rtassuming Γ, if s|p=lγ and t=s[rγ]p. Nipkow’s formulation of normal higher-order rewriting, uses βand ηin two different ways, both as rules and equalities: given a term sto be rewritten with a set R of rules, sis first normalized, using η-long β-normal forms, before being searched for lefthand sides of rules in Rvia higher-order pattern matching [Nipkow 1991; Mayr and Nipkow 1998]. In [Middeldorp et al. 2005; Jouannaud and Li 2012], Nipkow and Mayr’s confluence result is generalized by allowing using ηas a reduction on the one hand, and rules of higher type on the other hand. We now define normal higher-order rewriting so as to capture the different ways in which a term can be βη-normalized before pattern-matching a subterm with a lefthand side of rule. This definition is not meant to be used for computing, as is the case of the previous just mentioned two. The fact that it has poor operational properties (including confluence) is therefore irrelevant. Its sole purpose is to study the termination properties of Nipkow’s normal higher-order rewriting and its variants: by providing a method for proving termination of this more general relation, we do provide a method for all variants of higher-order rewriting based on higherorder pattern matching, independently of a particular orientation of extensionality. From now on, by normal higher-order rewriting (possibly omitting normal), we mean the coming definition, while Nipkow’s specific definition is systematically referred to as Nipkow’s higher-order rewriting. Definition 3.2.Anormal rewrite rule is a pair of β-normal terms written l→r, such that l↓η6∈ X,Var(r)⊆ Var(l)and (∀Θ, θ, γ) Θ `Σlγ↓β:θiff Θ`Σrγ↓β:θ. Anormal term rewriting system is a set of normal rules. Given a normal term rewriting system R, an environment Γ, two β-normal terms sand t, and a type σ such that Γ`Σs:σ, we say that srewrites to tat position pwith the normal rule l→rand the β-normal term substitution γ, written Γ`s−→p Rβη t, or s−→p Rβη t assuming Γ, if s|p=βη lγ and t=ηs[rγ]p↓β. Note that our definition does not require rules to be typable, but does require that in case they are, then, their normalized instances have the same type. This property is weaker than usual, but suffices for our purpose and is indeed more convenient to work with. A key observation is: LEMMA 3.3 (TYPE PRESERVATION).Let sbe a β-normal term such that Γ`Σs:σand Γ`s−→p Rβη t. Then Γ`Σt:σ. ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. A:8 J.-P. Jouannaud and A. Rubio PROOF. By Lemma 2.2, Γs|p`Σs|p:τ. Let l→rbe the rule of Rmatching s|p, hence s|p=βη lγ. Since sis β-normal, so is s|p, hence, by Property (6) of untyped λ-calculus, (lγ)↓β=ηs|p. By η-preservation, Γs|p`Σ(lγ)↓β:τ. By Definition 3.2, Γs|p`Σ(rγ)↓β:τ. By Lemma 2.3, Γ`Σs[(rγ)↓β]p:τ. By confluence and subject reduction, Γ`Σs[rγ]p↓β:τ. We conclude by η-preservation. We often consider type preserving higher-order rewriting as a relation on terms instead of on judgments, therefore simplifying our notations. Example 3.4 (differentiation). We present here an encoding of symbolic derivation in which functions are represented by λ-terms of a functional type. We give two typical rules of higher type. The free variable Fstands for a function over the reals, while x, y stand for real values. Let S={real}, and F={sin,cos :real →real;diff : (real →real)→real →real +,×: (real →real)→(real →real)→real →real} diff(λx.sin(@(F,x))) →λx.cos(@(F,x)) ×diff(λx.@(F,x)) diff(λx.@(F,x)×λy.@(F,y)) →(diff(λx.@(F,x)) ×λy.@(F,y))+ (λx.@(F,x)×diff(λy.@(F,y))) This example makes sense when using normal higher-order rewriting, because using plain pattern matching instead would not allow for computing the derivative of all expressions: rewriting the expression diff(λx.sin(x)) does require higherorder pattern matching, since taking for Fthe identity substitution λy.y, we have diff(λx.sin(@(λy.y,x)) −→βdiff(λx.sin(x)). Note that we use η-expanded forms here. We shall give a mechanical termination proof of these two rules in Section 4. 3.1. Higher-Order Reduction Orderings We shall use well-founded relations for proving strong normalization properties [Jouannaud and Rubio 2007]. For our purpose, these relations may not be transitive, but their transitive closures will be well-founded orderings, justifying our abuse of terminology. We shall consider two kinds of higher-order reduction orderings, dubbed plain when they include β-reductions and normal when they are compatible with βη-equivalence. Definition 3.5.A binary relation on the set of judgments is –coherent iff for all terms s, t such that (Γ `Σs:σ)(Γ `Σt:σ), and for all environment Γ0such that Γ0`Σs:σand Γ0`Σt:σ, then (Γ0`Σs:σ)(Γ0`Σt:σ); –stable iff for all terms s, t such that (Γ `Σs:σ)(Γ `Σt:σ), and all substitutions γsuch that ∀x∈ Var(s),Θ`Σxγ : Γ(x)for some environment Θ, then (Θ `Σsγ :σ)(Θ `Σtγ :σ); ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. Normal Higher-Order Termination A:9 –monotonic iff for all environments Γ, contexts u[], variable x, type σand terms s, t s.t. Γ· {x:σ} `Σu[x:σ] : τand (Γ `Σs:σ)(Γ `Σt:σ), then (Γ `Σu[s] : τ)(Γ `Σu[t] : τ); –functional iff for all terms s, t, t0such that (Γ `Σs:σ)(Γ `Σt:σ)and (Γ `Σt:σ−→βt0:σ)then (Γ `Σs:σ)(Γ `Σt0:σ). Note that our definition of functionality here is slightly weaker than the one in [Jouannaud and Rubio 2007] which does not assume that st. Coherence is a natural property ensuring that a comparison does not depend upon the type of the variables in the compared terms. For example @(x, y)yshould not depend upon the type of x, y. HORPO and other similar orders are coherent. Definition 3.6.Ahigher-order reduction ordering is a well-founded ordering of the set of judgments satisfying coherence, stability and monotonicity. A higherorder ordering is plain if it also satisfies functionality. Plain higher-order orderings are an adequate tool for proving strong normalization of plain higher-order rewriting. One may wonder whether they should also include some form of extensionality, as in [Jouannaud and Rubio 2007]. This is indeed not possible if one wants to be compatible with both syntactic choices of having extensionality as either η-reduction (our choice in [Jouannaud and Rubio 2007]) or η-expansion (Di Cosmo-Kesner’s choice in [Cosmo and Kesner 1994], which includes a discussion of their respective advantages). 3.2. Normal Higher-Order Reduction Orderings We would need higher-order orderings satisfying the compatibility property: ∀s0, s, t, t0s.t. (Γ `Σs0:σ=βη s:σ),(Γ `Σt:σ=βη t0:σ)and (Γ `Σs:σ)(Γ `Σt:σ)then (Γ `Σs0:σ)(Γ `Σt0:σ) Unfortunately, no ordering can satisfy monotonicity, stability, compatibility and well-foundedness together: assume s:σt:σ(omitting judgments), where s:σ is in β-normal form. Given a variable X:σ→τ,@(X, s)is in β-normal form as well. By monotonicity, @(X, s) : τ@(X, t) : τ. Consider the substitution γ= {X7→ λy.a}where a:τis a constant. By stability, @(λy.a, s) : τ@(λy.a, t) : τ. By compatibility, a:τa:τ, contradicting well-foundedness. We therefore introduce a new kind of ordering for the normal case: Definition 3.7.A normal higher-order reduction ordering (normal ordering) is a pair h>, i of a plain higher-order reduction ordering and a relation >satisfying: (i) normal η-compatibility: for all β-normal typed terms s, s0, t s.t. s0=ηst there exists some β-normal term t0such that s0t0=ηt. (ii) normal stability: for all β-normal typed terms s, t and β-normal substitutions γ,s>timplies (sγ)↓β(tγ)↓β. ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. A:16 J.-P. Jouannaud and A. Rubio (7) s= @(u, v)is non-versatile and s0≥tfor some s0∈ {u, v}. Since sis non-versatile then (sγ)↓= @((uγ)↓,(vγ)↓), and we conclude as in Case 3. (8) s= @(u, v),t= @(u0, v0), and {u, v}>mul {u0, v0}. Since sis non-versatile, then (sγ)↓= @((uγ)↓,(vγ)↓)and we conclude as in Case 3. (9) s=λx :σ.u and u{x7→ z} ≥ twith zfresh. Since sis non-versatile, then (sγ)↓=λx.((uγ)↓). By, induction hypothesis (i) and property (ii), (u{x7→ z}γ)↓  wwith tγ −→∗ βη w, and, since zis fresh, (u{x7→ z}γ)↓= ((uγ)↓ ){x7→ z}. We conclude as in case 3. (10) s=λx :α.u,t=λx :β.v,α=βand u>v. Since sis non-versatile, then (sγ)↓=λx.((uγ)↓). By induction hypothesis (i), (uγ)↓  w0for some w0such that vγ −→∗ βη w0, and we conclude as in Case 3. (11) s=λx.u,tis not an abstraction and u{x7→ z} ≥ @(t, z),zfresh. Since sis non-versatile, then (sγ)↓=λx.((uγ)↓). By induction hypothesis (i) and property (ii), (u{x7→ z}γ)↓  w0with @(t, z)γ−→∗ βη w0. Since zis fresh, (u{x7→ z}γ)↓= (uγ)↓ {x7→ z}. Since tis not a variable nor an abstraction, w0= @(w, z)and tγ −→∗ βη w. Therefore, (uγ)↓ {x7→ z}  @(w, z), which implies by monotonicity and α-conversion λx.(uγ)↓  λx.@(w, x). Since HORPO includes η-reduction, we get (sγ)↓=λx.(uγ)↓  w. We assume here that this can be done with HORPO in one step, which could be achieved by adding the necessary case without modifying HORPO as an order. We can therefore use (>η,η)as our normal higher-order ordering for checking whether a given higher-order rewrite system Ris terminating. However, note that the lefthand side lof a rule l→r∈Rmust be in β-normal form in order to use NHORPO via η, since NHORPO needs lefthand sides of comparisons to be normal to have a chance of success, which will be the case by η-postponement. We strongly believe that the very same changes as done here work equally well for similarly defined higher-order reduction orderings such as CPO [Blanqui et al. 2008], with a similar proof as for HORPO, although details have not checked. Example 4.8 (differentiation continued). Symbols have multiset status, sin and cos are equal in the precedence, diff is bigger than all others. We consider only the first rule. Our goal is: diff(λx.sin(@(F,x))) > λx.cos(@(F,x)) ×diff(F), which, by Case 2, yields two subgoals: Subgoal 1: diff(λx.sin(@(F,x))) > λx.cos(@(F,x)) which succeeds by Case 1 since λx.sin(@(F,x)) = λx.cos(@(F,x)), sin and cos having the same precedence. Subgoal 2: diff(λx.sin(@(F,x))) >diff(F)which, by Case 3 yields Subgoal 3: λx.sin(@(F,x)) >F. We cannot apply Case 9 here as one might have expected, the type of Fis too large. We use instead Case 11, which yields: Subgoal 4: sin(@(F,x)) >@(F,x)which succeeds by Case 1. Since hη, >iis a normal higher-order reduction ordering, we conclude normal termination of (the first rule of) our example by using theorems 3.8 and 4.7 together. ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. Normal Higher-Order Termination A:17 Many examples cannot be treated by normal HORPO, because the only rule able to handle abstractions on the righthand side are the weak Rule 6 and the monotonicity Rule 10. Some of these examples would work with normal CPO, which was designed to remedy this very weakness of HORPO. This question is indeed at the heart of the next section, where a powerful technique is presented that makes it possible to solve complex goals, even when CPO itself would fail. 5. INTERPRETATIONS FOR NORMAL HIGHER-ORDER REWRITING The profusion of abstractions on both the lefthand and righthand sides of rules in normal higher-order rewrite rules is a challenge for termination proofs, even when using CPO instead of HORPO. This is the question addressed in this section, where we introduce neutralization, a particular higher-order interpretation aimed at eliminating abstractions which cannot originate reductions. Higher-order term interpretations that preserve termination of higher-order rewriting are studied first, making no assumption whatsoever on the method used for carrying out such a termination proof. On the other hand, we make an assumption on higher-order rules: their lefthand side is either of basic type or headed by an algebraic function symbol. Definition 5.1.A higher-order interpretation is a mapping from higher-order terms to higher-order terms which is the identity on free variables and preserves types in a given environment: (∀Γ, u, σ)such that Γ`Σu:σ, then Γ`ΣI(u) : σ. Higher-order interpretations are extended to terms, substitutions, rewrite rules, and rewrite systems in the natural way. The role of our assumption that types are preserved ensures that term manipulations in interpreted terms preserve typability. More general assumptions are of course possible, provided they achieve the same property and ensure that if two terms the same type σ, then their interpretations have the same type τfor some τ. Definition 5.2.A higher-order interpretation I:T(F,X)→ T (F0,X)is normal if it satisfies the following properties: –β-compatibility:s=βtimplies I(s) =βI(t)for every two typed terms sand t; –η-compatibility:s=ηtimplies I(s) =ηI(t)for every two typed terms sand t; – preservation of β-normal forms: I(t)is β-normal whenever tis ; – preservation of algebraic head: I(t)is F0-headed if tis F-headed ; –monotonicity:∀β-normal u,∀p∈ Pos(u), there exists a set {q1, . . . , qn}n>0of disjoint positions in I(u)such that, ∀β-normal t,I(u[t]p) =βη I(u)[I(t)]q1...qn; –stability: for all β-normal term tand substitution γ,I(tγ) =βη I(t)I(γ). LEMMA 5.3. Let tand ube higher-order terms. Then, for every set {p1. . . pm} of disjoint positions in Pos(t)there is a set {q1, . . . , qn}of disjoint positions in Pos(t↓β)such that t[u]p1...pm↓β= (t↓β[u↓β]q1,...,qn)↓β. Further, if uhas a basic type or is headed by an algebraic symbol, then t[u]p1...pm↓β=t↓β[u↓β]q1...qn. ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. A:18 J.-P. Jouannaud and A. Rubio PROOF. Follows from the Church-Rosser property of β-reductions, and the fact that u↓βhas a basic type or is headed by an algebraic symbol if this is true of u. LEMMA 5.4. Let ube a β-normal term, Ra terminating higher-order rewrite system whose lefthand sides are either headed by an algebraic symbol or of base type, pbe a non-empty sequence of disjoint positions of Pos(u), and l→ra rule in Rsuch that (∀p∈p)u|p=βη lγ. Then u−→+ Ru[rγ]p↓β. PROOF. The difficulty is that higher-order rewriting does not satisfy the socalled disjoint redex axiom because of the β-normalization which applies at each step, but all β-normalization steps can indeed be postponed and grouped together. Since lhence lγ is not an abstraction and u|pis β-normal and it is not an abstraction either, u=ηu[z]p{z7→ lγ ↓β}where zis a fresh variable, pis a set of disjoint positions in Pos(u[z]p)and u[z]pis β-normal. We prove that u=ηu[z]p{z7→ lγ ↓β} −→+ R(u[z]p{z7→ rγ})↓βby induction on ucompared in the well-founded order −→+ R. If |p|= 1, the result is clear. Otherwise, p=p, q. We choose to rewrite pfirst: u−→p Rv=η(u[z]p{z7→ rγ}[z]q{z7→ lγ↓β})↓β= (u[rγ]p[z]q{z7→ lγ ↓β})↓β. Because lγ↓βis not an abstraction and is β-normal, v=η(u[rγ]p[z]q)↓β{z7→ lγ↓β }, and zoccurs at a non-empty set qof disjoint positions of Pos(u[rγ]p[z]q↓β). By induction hypothesis, v−→+ Rv0=η((u[rγ]p[z]q)↓β{z7→ rγ})↓β. By confluence of β,v0=η((u[rγ]p[z]q)↓β{z7→ rγ})↓β=u[z]p,q{z7→ rγ}↓βand we are done. THEOREM 5.5. Let Rbe a normal higher-order rewrite system all whose lefthand sides are either headed by an algebraic symbol or have a basic type, and I be a normal higher-order interpretation. Then termination of I(R)implies termination of R. PROOF. Let sbe a β-normal term such that s−→Rt. By definition of higherorder rewriting, s=s↓β,s|p=βη lγ for some position p,β-normal substitution γand rule l→r∈R, and tis a β-normal term such that t=ηs[rγ]p↓β. By definition of an interpretation, I(s)and I(t)are β-normal. We now show that I(s)−→+ I(R)I(t), which implies termination of Rassuming termination of I(R). The assumption that interpretations preserve types ensures that all terms constructed in the coming proof are typable, thus environments can be dispensed with. Since s, hence s|pis β-normal, and s|p=βη lγ, by confluence of β-reductions modulo η,lγ −→∗ βlγ↓β=ηs|p. By assumption, lis headed by an algebraic symbol or is a term of basic type, hence so are lγ and therefore lγ↓β. It follows that s|p= λx.@(s|q, x)and x∩ Var(s|q) = ∅, and either s|qhas basic type or s|q=f(u). Now, s|q=ηlγ ↓β, hence s−→qt0=η(s[λx.@(rγ, x)]p)↓β. Further, s|qand lγ being βη-equivalent, Var(lγ) = Var(s|q), and therefore Var(lγ)∩x=∅. Since Var(r)⊆ Var(l),Var(rγ)⊆ Var(lγ), implying that Var(rγ)∩x=∅. It follows that t0=η(s[rγ]q)↓β=η(s[rγ]p)↓β=ηt. ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. Normal Higher-Order Termination A:19 We proceed with the calculation of I(s)first and then I(t), which are both βnormal since Ipreserves β-normal forms. By monotonicity of I, there are disjoint positions q1. . . qn(depending on sand qonly) such that I(s) =βη I(s)[I(s|q)]q1...qm. By preservation of β-normal forms I(s)and I(s|q)are β-normal. Further, since s|qhas basic type or is headed by an algebraic symbol, the same is true of I(s|q)by preservation of either types or algebraic symbols. Hence, by Lemma 5.3, since I(s)and I(s|q)are β-normal, we have I(s) =ηI(s)[I(s|q)]q1...qn. By η-compatibility and β-compatibility, I(s|q) =βη I(lγ). By stability, I(lγ) =βη I(l)I(γ). Hence I(s|q) =βη I(l)I(γ). Hence I(s)−→+ I(R)I(s)[I(r)I(γ)]q1...qn↓βby Lemma 5.4. We move to I(t). By successively η-compatibility and β-compatibility of I, we get I(t) =ηI(s[rγ]q↓β) =βI(s↓β[(rγ)↓β]q). Further, by monotonicity of I,I(s↓β [rγ ↓β]q) =βη I(s↓β)[I((rγ)↓β)]q1,...,qn. Finally, by stability and β-compatibility of I, it follows that I(t) =βη I(s↓β)[I(r)I(γ)]q1,...,qn. Since tis β-normal and I preserves normal forms, and βis Church-Rosser modulo η, we finally get I(t) =η I(s)[I(r)I(γ)]q1,...,qn↓β, which achieves the proof. 5.1. Neutralization and Normalization We present now a particular example of termination-preserving interpretation for normal rewriting, originally introduced in [Jouannaud and Rubio 2006]. The construction relies on a specific treatment, called neutralization, of those abstractions which should not be involved in a beta-redex, that is, which should not be or become after reductions the first argument of an application. To this end, a term built from the signature Fis transformed into a term built from an enlarged signature Fnew obtained from Fby adding (if necessary) a minimal constant type o, a coercion symbol mσ:σ→ofor every type σto the minimal type o, a function symbol ⊥n σ:τ1×. . .×τn→τfor every type σ=τ1→. . . →τn→τ, and a function symbol fnew for some of the function symbols in F. We omit the superscript nin ⊥n σ when it is clear from the context. The higher-order rules to be proved terminating are of course built from terms in T(F,X), not in T(Fnew,X). Definition 5.6.The neutralization of level i(i-neutralization for short) of a typable term t∈ T (Fnew,X)with respect to the list of (typable) terms hu1: θ1, . . . , un:θniin T(Fnew,X), is the term Ni(t, hu1, . . . , uni)defined as follows: (1) Ni(t:τ, hu1, . . . , uni) = mτ(t)if i < 0; (2) N0(t:τ, hu1, . . . , uni) = t; (3) Ni+1(t:τ, hu1, . . . , uni) = tif τis a data type; (4) Ni+1(t:σ→τ, hu1, . . . , uni) = Ni(@(t, ⊥n θ1→...→θn→σ(u1, . . . , un))). Terms of a functional type are neutralized by applying them to the ⊥-expression of the appropriate type, therefore reducing their functionality as long as the level ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. A:20 J.-P. Jouannaud and A. Rubio is non-zero. The role of N−1is specific to HORPO, we shall understand it in examples. The role of the sequence of terms hu1:θ1, . . . , un:θniis to become arguments of ⊥n σin case that symbol has a functional type. Note also that neutralization is the identity function if all levels are 0. Further, for each function symbol, we control which arguments of a functional type can be neutralized: Definition 5.7.To each symbol f:σ1×. . . ×σn→σ∈ F and each argument position j∈[1..n], we associate: – an integer nlj f≤ar(σj), called neutralization level of fat position j. We call neutralized (respectively, active, coerced) those positions jfor which nlj f>0 (respectively, nlj f= 0,nlj f<0), – a subset Aj f⊆[1..n]of argument positions of fused to filter out its list of arguments tby defining tj f=htk|k∈ Aj fi. We now neutralize terms recursively. To this end, for each symbol f:σ1× . . . ×σn→σsuch that σi=τ1→. . . →τk→τ, where τis a basic type, we add to the signature a new function symbol fnew :σ0 1×. . . ×σ0 n→σsuch that σ0 i=τq+1 →. . . →τk→τif nli f=q < k and σ0 i=τif nli f=k. Definition 5.8.The full neutralization of a term tis the term FN (t)s.t. FN (x) = xif x∈ X FN (λx.u) = λx.FN (u) FN (@(t1, t2)) = @(FN (t1),FN(t2)) FN (f(t1, . . . , tn)) = fnew(Nnl1 f(FN (t1),FN(t1 f)),...,Nnln f(FN (tn),FN(tn f))) where FN (hu1, . . . , uni) = hFN(u1),...,FN (un)i. Our definition makes sense since, in all cases, FN (t)is typable with the same type as t. Note also that the filtered list of arguments ti fis itself recursively neutralized before using it to neutralize tiin the last case. Example 5.9 (differentiation continued). We illustrate here the full neutralization of the lefthand and righthand sides of the rules of Example 3.4. To this end, we choose a neutralization level for each function symbol and argument position. The associated subsets of argument positions are chosen empty, hence ⊥0 real is a constant abbreviated as ⊥: L1 diff = 1 L1 sin = 0 L1 cos = 0 A1 diff ={} A1 sin ={} A1 cos ={} L1 ×= 1 L2 ×= 1 L1 += 1 L2 += 1 A1 ×={} A2 ×={} A1 +={} A2 +={} ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. Normal Higher-Order Termination A:21 We can now compute the full neutralization of both sides of the first rule: FN (diff (λx.sin(@(F,x)) ) ) =diffnew ( @( λx.sin(@(F,x)) ,⊥) ) FN (λx.cos(@(F,x)) ×diff (λx.@(F,x) ) ) = @( λx.cos(@(F,x)) ,⊥)×new @( diffnew ( @( λx.@(F,x),⊥) ) ,⊥) and normalizing both sides we get diffnew(sin(@(F,⊥))) →cos(@(F,⊥)) ×new @(diffnew(@(F,⊥)),⊥) and of the second rule: FN (diff (λx.@(F,x)×λy.@(F,y) ) ) =diffnew ( @( @( λx.@(F,x),⊥)×new @( λx.@(F,x),⊥),⊥) ) FN ((diff(λx.@(F,x)) ×λy.@(F,y))+(λx.@(F,x)×diff(λy.@(F,y)))) = @(@(diffnew(@(λx.@(F,x),⊥)),⊥)×new @(λy.@(F,y),⊥),⊥) +new @(@(λx.@(F,x),⊥)×new @(diffnew(@(λy.@(F,y),⊥)),⊥),⊥) and normalizing both sides we get diff new(@(@(F,⊥)×new @(F,⊥),⊥)) → @(@(diff new(@(F,⊥)),⊥)×new @(F,⊥),⊥) +new @(@(F,⊥)×new @(diff new(@(F,⊥)),⊥),⊥) An example with a non-empty set of argument positions is given at Example 7.2. 5.2. Properties of neutralization We investigate here the interactions between neutralization, monotonicity and instantiation. LEMMA 5.10. Let t:τand u1, . . . , unbe higher-order terms and γbe a substitution. Then Ni(tγ, hu1γ,...,unγi) = Ni(t, hu1, . . . , uni)γ. PROOF. We proceed by induction on i. There are three cases. (1) If i < 0. Then, we have tγ :τ, and hence Ni(tγ, hu1γ,...,unγi) = mτ(tγ) = mτ(t)γ=Ni(t, hu1, . . . , uni)γ. (2) If i= 0 or τis a data type. Then, we have tγ :τ, and hence Ni(tγ, hu1γ,...,unγi) = tγ =Ni(t, hu1, . . . , uni)γ. (3) Otherwise i > 0and τ=σ→ρ. Then, Ni(tγ, hu1γ,...,unγi) = Ni−1(@(tγ, ⊥θ(u1γ,...,unγ))) = Ni−1(@(t, ⊥θ(u1, . . . , un))γ). By induction hypothesis, this is equal to Ni−1(@(t, ⊥θ(u1, . . . , un)))γ=Ni(t, hu1, . . . , uni)γ. LEMMA 5.11. Let tbe a higher-order term and γbe a substitution. Then FN (tγ) = FN(t)FN (γ). PROOF. We proceed by induction on |t|. There are four cases: ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. A:22 J.-P. Jouannaud and A. Rubio (1) tis a variable x. Then FN(xγ) = xFN(γ) = FN (x)FN (γ). (2) t=λx.u. Then FN (tγ) = FN (λx.uγ) = λx.FN (uγ). By induction hypothesis, FN (uγ) = FN(u)FN (γ), and therefore λx.FN (uγ) = λx.FN(u)FN (γ) = (λx.FN (u))FN(γ), since xis not occurring in Ran(FN (γ)), and which is equal to FN(λx.u)FN(γ) = FN (t)FN(γ). (3) t= @(t1, t2). Then FN (tγ) = FN(@(t1, t2)γ) = FN (@(t1γ, t2γ)) = @(FN (t1γ),FN(t2γ)). By induction hypothesis, it is equal to @(FN (t1)FN(γ),FN (t2)FN(γ)) = @(FN(t1)FN (γ),FN(t2)FN(γ)) = @(FN (t1),FN(t2))FN (γ) = FN(@(t1, t2))FN(γ) = FN (t)FN(γ) (4) t=f(t1, . . . , tn)with f∈ F. Then FN(tγ) = FN(f(t1, . . . , tn)γ) = FN (f(t1γ,...,tnγ)) = fnew(Nnl1 f(FN (t1γ),FN(t1 fγ)),...,Nnln f(FN (tnγ),FN(tn fγ))). By induction hypothesis, this is equal to fnew(Nnl1 f(FN (t1)FN(γ),FN (t1 f)FN (γ)),..., Nnln f(FN (tn)FN(γ),FN (tn f)FN (γ))), and, by Lemma 5.10, to fnew(Nnl1 f(FN (t1),FN(t1 f))FN (γ),...,Nnln f(FN (tn),FN(tn f))FN (γ)) = fnew(Nnl1 f(FN (t1),FN(t1 f)),...,Nnln f(FN (tn),FN(tn f)))FN (γ) = FN (f(t1, . . . , tn))FN(γ) = FN (t)FN(γ) LEMMA 5.12. Let s,t,u1, . . . , umand wbe higher-order terms and let p be a position in tand q1. . . qnpositions in FN (t), such that FN(t[w]p) = FN (t)[FN(w)]q1...qn. Then we have that there are disjoint positions r1. . . rosuch that —Ni(FN (t[w]p),hFN(u1),...,FN (t[w]p),...,FN(um)i) = Ni(FN (t),hFN(u1),...,FN (t),...,FN(um)i)[FN (w)]r1...ro if t:τ1→. . . →τk→τand i≤k. —Ni(FN (t[w]p),hFN(u1),...,FN (um)i) = Ni(FN (t),hFN(u1),...,FN (um)i)[FN(w)]r1...ro if t:τ1→. . . →τk→τand i≤k. —Ni(FN (s),hFN(u1),...,FN (t[w]p),...,FN(um)i) = Ni(FN (s),hFN(u1),...,FN (t),...,FN(um)i)[FN (w)]r1...ro if s:τ1→. . . →τk→τand i≤k. PROOF. We only provide the proof of the first case, the other two are easily obtained from this one. Ni(FN (t[w]p),hFN(u1),...,FN (t[w]p),...,FN(um)i) = @(. . . @(FN (t[w]p),⊥τ1(FN(u1),...,FN (t[w]p),...,FN(um))), . . . ⊥τi(FN (u1),...,FN(t[w]p),...,FN (um)))) By assumption, we have that FN(t[w]p) = FN (t)[FN(w)]q1...qn, hence is equal to ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. Normal Higher-Order Termination A:23 @(. . . @(FN (t)[FN (w)]q1...qn),⊥τ1(FN(u1),...,FN (t)[FN(w)]q1...qn,...,FN(um))), . . . ⊥τi(FN(u1),...,FN (t)[FN (w)]q1...qn,...,FN(um)))) Now adding the corresponding prefixes p0 0, p0 1, . . . , p0 i, which are the disjoint positions of the i+ 1 occurrences of FN (t)we are considering, this is equal to @(. . . @(FN (t),⊥τ1(FN(u1),...,FN (t),...,FN(um))), . . . ⊥τi(FN (u1),...,FN(t),...,FN (um))))[FN(w)]r1...ro where r1. . . rois the set of disjoint positions p0 0·q1. . . p0 0·qn, p0 1·q1. . . p0 1·qn, . . . , p0 i·q1. . . p0 i·qn. LEMMA 5.13. Let t, w be higher-order terms. For every position pin t there are disjoint positions {q1, . . . , qn}n>0in FN(t)such that FN (t[w]p) = FN (t)[FN(w)]q1...qn. PROOF. We proceed by induction on |p|. If pis Λ, it trivially holds. Otherwise, there are three cases: (1) t=λx.u. Then p= 1 ·p0and FN(λx.u) = λx.FN(u). By induction hypothesis, there are disjoint positions {q0 1. . . q0 n}n>0such that FN (u[w]p0) = FN (u)[FN(w)]q0 1...q0 n, and therefore, taking qi= 1 ·q0 ifor all i∈ {1. . . n}, we have FN (λx.u[w]p) = λx.FN(u[w]p0) = λx.FN(u)[FN (w)]q0 1...q0 n= FN (λx.u)[FN (w)]q1...qn. (2) t= @(t1, t2). Let p= 1 ·p0(the case p= 2 ·p0is analogous). Then FN (@(t1, t2)) = @(FN(t1),FN (t2)). By induction hypothesis, there are disjoint positions {q0 1. . . q0 n}n>0such that FN (t1[w]p0) = FN(t1)[FN (w)]q0 1...q0 n, and therefore, taking qi= 1 ·q0 ifor all i∈ {1. . . n}, we have FN (@(t1, t2)[w]p) = FN(@(t1[w]p0, t2)) = @(FN (t1[w]p0),FN(t2)) = @(FN (t1)[FN(w)]q0 1...q0 n,FN (t2)) = @(FN(t1),FN (t2))[FN(w)]q1...qn= FN (@(t1, t2))[FN(w)]q1...qn. (3) t=f(t1, . . . , tm)with f∈ F. Let p=i·p0 for some i∈ {1. . . m}. Then FN (f(t1, . . . , tm)) = fnew(Nnl1 f(FN (t1),FN(t1 f)),...,Nnlm f(FN (tm),FN(tm f))). By induction hypothesis, there are disjoint positions {q0 1. . . q0 n}n>0such that FN (ti[w]p0) = FN(ti)[FN (w)]q0 1...q0 n0. Then by Lemma 5.12, we have that for every tjwith j∈ {1. . . m}there are disjoint positions qj 1. . . qj njsuch that — if i=jand i∈ Aj fand ijis the position of tiin tj fthen Nnlj f(FN (tj[w]p),FN(tj f[w]ij·p)) = Nnlj f(FN (tj),FN(tj f))[FN (w)]qj 1...qj nj — if i6=jand i∈ Aj fand ijis the position of tiin tj fthen Nnlj f(FN (tj),FN(tj f[w]ij·p)) = Nnlj f(FN (tj),FN(tj f))[FN (w)]qj 1...qj nj ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. A:24 J.-P. Jouannaud and A. Rubio — if i=jand i6∈ Aj fand ijis the position of tiin tj fthen Nnlj f(FN (tj),FN(tj f[w]ij·p)) = Nnlj f(FN (tj),FN(tj f))[FN (w)]qj 1...qj nj Therefore FN (f(t1, . . . , ti[w]p0, . . . , tm)) = FN (f(t1, . . . , ti, . . . , tm))[FN(w)]1·q1 1...1·q1 n1...m·qm 1...m·qm nm Now we prove η- and β-compatibility of FN . LEMMA 5.14. Let s, t be higher-order terms. If s=ηt, then FN(s)=ηFN(t). PROOF. First, we prove that if x /∈ Var(u)then FN(λx.@(u, x)) =η FN (λx.@(u, x)). By definition of FN, we have that FN(λx.@(u, x)) = λx.@(FN (u), x). Since FN preserves the set of free variables, we have that x /∈ Var(FN(u)), and hence λx.@(FN (u), x) =ηFN(u). Now, assume we have s[λx.@(u, x)]p=ηs[u]pfor some position p, then by Lemma 5.13, there are disjoint positions q1, . . . , qnin FN(s)such that we have that FN (s[λx.@(u, x)]p) = FN (t)[FN(λx.@(u, x))]q1...qnand FN(s[u]p) = FN (t)[FN(u)]q1...qn. Therefore, since, FN (t)[FN(λx.@(u, x))]q1...qn=η FN (t)[FN(u)]q1...qn, we conclude that FN (s[λx.@(u, x)]p) =ηFN (s[u]p). We conclude by induction on the number of η-equality steps applied in s=ηt. LEMMA 5.15. Let s, t be higher-order terms. If s=βt, then FN(s)=βFN(t). PROOF. First, we prove that FN(@(λx.u, v)) =βFN(u{x7→ v}). By definition of FN , we have that FN(@(λx.u, v)) = @(λx.FN (u),FN(v)), which is β-equivalent to FN(u){x7→ FN (v)}, and by Lemma 5.11, it is equal to FN (u{x7→ v}). We then conclude that FN (s) =βFN(t), proceeding as in the proof of the previous lemma, using Lemma 5.13 and induction on the number of β-equality steps afterward. The coming two lemmas follow easily: LEMMA 5.16. Let t:τbe a higher-order term. Then FN (t) : τ. LEMMA 5.17. Let tbe a higher-order term. Then FN(t)is headed by an algebraic symbol whenever tis. 5.3. Neutralization as an interpretation Definition 5.18.We define the interpretation Nas N(s) = FN(s)↓β. THEOREM 5.19. Nis a normal higher-order interpretation. PROOF. By definition and Lemma 5.16, Nis a higher-order interpretation. Since β-normalization preserves both η- and β-equivalence, by Lemma 5.15 we obtain β-compatibility and by Lemma 5.14 we obtain η-compatibility. Preservation of βnormalization trivially holds by definition. Lemma 5.17 implies preservation of ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. Normal Higher-Order Termination A:25 algebraic heads. Finally, Lemma 5.11 implies stability and Lemma 5.13 monotonicity. Therefore Nis a normal higher-order interpretation. Full normalization combined with neutralization can be used in conjunction with any normal higher-order reduction ordering. Of course, we then need to define the precedence on function symbols for the extended signature. In practice, we shall make ⊥σ-functions symbols small, as done now to prove our starting example. Example 5.20 (differentiation end). We consider again the differentiation example, using now neutralization. Let diffnew >F{×new,+new,cos}and diffnew ∈Mul. First rule: diffnew(sin(@(F,⊥))) >cos(@(F,⊥)) ×new @(diffnew(@(F,⊥)),⊥) Applying first case 2, we recursively obtain two subgoals: (i) diffnew(sin(@(F,⊥))) >cos(@(F,⊥)) (ii) diffnew(sin(@(F,⊥))) >@(diffnew(@(F,⊥)),⊥). (i): applying Case 2 yields diffnew(sin(@(F,⊥))) >@(F,⊥)shown by Case 1 twice. (ii): applying Case 5 generates two new subgoals (iii) diffnew(sin(@(F,⊥))) >diffnew(@(F,⊥)) ,which holds by case 3, then case 1. (iv) diffnew(sin(@(F,⊥))) >⊥,which holds by case 1 twice and then case 7. Second rule: diffnew(@(@(F,⊥)×new @(F,⊥),⊥)) >@(@(diffnew(@(F,⊥)),⊥)×new @(F,⊥),⊥) +new@(@(F,⊥)×new @(diffnew(@(F,⊥)),⊥),⊥) Case 2 generates two subgoals: (i) diffnew(@(@(F,⊥)×new @(F,⊥),⊥))>@(@(diffnew(@(F,⊥)),⊥)×new @(F,⊥),⊥) (ii) diffnew(@(@(F,⊥)×new @(F,⊥),⊥))>@(@(F,⊥)×new @(diffnew(@(F,⊥)),⊥),⊥) We only prove (i), as (ii) is shown exactly in the same way. By Case 5, (i) generates two new subgoals: (iii) diffnew(@(@(F,⊥)×new @(F,⊥),⊥)) >@(diffnew(@(F,⊥)),⊥)×new @(F,⊥) (iv) diffnew(@(@(F,⊥)×new @(F,⊥),⊥)) >⊥. The latter holds by case 1 and then case 7. By Case 2, (iii) yields two subgoals: (v) diffnew(@(@(F,⊥)×new @(F,⊥),⊥)) >@(diffnew(@(F,⊥)),⊥) (vi) diffnew(@(@(F,⊥)×new @(F,⊥),⊥)) >@(F,⊥), solved by applying successively Case 1, 7 and 1. By Case 5, (v) generates (vii) diffnew(@(@(F,⊥)×new @(F,⊥),⊥)) >diffnew(@(F,⊥)) (viii) diffnew(@(@(F,⊥)×new @(F,⊥),⊥)) >⊥solved by Case 1 first, then 7. Finally, (vii) is solved by Case 3, 7, and 1 successively. 6. POLYMORPHIC NORMAL REWRITING We investigate here the case of polymorphic rewriting defined by polymorphic rewrite rules. Polymorphism is introduced via type variables, quantifiers being omitted, hence is weak in the usual sense. We shall not mention further this “weakness” which is present throughout this section. Since types are first-order expres- ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. A:32 J.-P. Jouannaud and A. Rubio and, FN(∃− ρ,τ (T, λx:o y :ρ.∃− σ,τ (@(J, x, y), G)))↓β= ∃− new ρ,τ (mc(o,ρ)(T),∃− new σ,τ (mc(o,σ)(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))), @(G, ⊥c(o,σ)→o(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))), ⊥c(o,σ)→σ(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)))))) We prove s>tby case 4, yielding two subgoals: (i) ∃− new ρ,c(o,σ)(mc(o,ρ)(T),@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))) > mc(o,ρ)(T), and (ii) s > ∃− new σ,τ (mc(o,σ)(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))), @(G, ⊥c(o,σ)→o(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))), ⊥c(o,σ)→σ(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))))) For (i) we apply case 1 twice (note that mc(o,ρ)(T)has minimal type). For (ii) we apply case 4 again which leads us to two new subgoals: (iii) mc(o,σ)(∃− new ρ,c(o,σ)(mc(o,ρ)(T),@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)))) > mc(o,σ)(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))),and (iv) @(G, ⊥c(o,σ)→o(∃− new ρ,c(o,σ)(mc(o,ρ)(T),@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)))), ⊥c(o,σ)→σ(∃− new ρ,c(o,σ)(mc(o,ρ)(T),@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)))))) ≥@(G, ⊥c(o,σ)→o(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))), ⊥c(o,σ)→σ(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)))) For (iii) we apply case 3 first and then case 1, and for (iv) we apply case 8, which leads us to prove: ⊥c(o,σ)→o(∃− new ρ,c(o,σ)(mc(o,ρ)(T),@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)))) ≥ ⊥c(o,σ)→o(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))). which holds by case 3 and 1. Example 7.5.The previous encoding of natural deduction is now made polymorphic. As a result, it becomes finite, hence amenable to automation. Let S={o, c :∗ × ∗ → ∗}, and S∀={σ, τ, ρ}. Signature and rules follow. F={app : (σ→τ)×σ→τ;abs : (σ→τ)→(σ→τ); Π : σ×τ→c(σ, τ); Π0:c(σ, τ)→σ; Π1:c(σ, τ)→τ; ∃+:o×σ→c(o, σ); ∃−:c(o, σ)×(o→σ→τ)→τ} X={X:σ;Y:τ;Z:o;T:c(o, ρ), F :σ→τ;G:o→σ→τ, H:o→ρ→(σ→τ), I :o→ρ→c(σ, τ), J :o→ρ→c(o, σ)} app(abs(F), X)→F(X)∃−(∃+(Z, X), G)→G(Z, X) Π0(Π(X, Y )) →XΠ1(Π(X, Y )) →Y app(∃−(T, H), X)→ ∃−(T, λx :o y :ρ.app(H(x, y), X)) Π0(∃−(T, I)) → ∃−(T, λx :o y :ρ.Π0(I(x, y))) Π1(∃−(T, I)) → ∃−(T, λx :o y :ρ.Π1(I(x, y))) ∃−(∃−(T, J), G)→ ∃−(T, λx :o y :ρ.∃−(J(x, y), G)) Statuses: ∃− new ∈Lex and app,Π0,Π1∈Mul. Precedence: {app, Π0,Π1}>F∃− new. ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. Normal Higher-Order Termination A:33 Neutralization:L1 ∃− =−1,L2 ∃− =2 and A2 ∃− ={1}. The computations are now exactly the same as for the monomorphic encoding of natural deduction done in Example 7.4. 8. REDUCING NORMAL TO PLAIN HIGHER-ORDER TERMINATION In this section, we introduce a transformation that reduces the problem of normal higher-order termination to that of plain higher-order termination. Such a transformation is of course most useful since it allows one to prove normal termination by using already existing tools that can prove plain higher-order termination. We proceed in two steps. First, we reduce normal termination of Rto termination of the union of β-reduction and (S)modulo η, where Ssimulates Rin a way that we are going to describe next. Here rewriting modulo ηmeans rewriting on ηequivalence classes. Second, we show that termination of the union of β-reduction and (S)modulo ηcan itself be reduced to termination of S0∪βη, where S0is obtained from S, although in most cases S0will be Sitself. Moreover, we also show that, under some restrictions, termination of (S)modulo ηunion β-reduction can also be reduced to termination of S0∪β, where S0is an η-expansion of Sthat operates on η-long forms. Let us give a precise definition of rewriting modulo η: Definition 8.1.Given a set of higher-order rewrite rules R, we denote by (R)η the rewrite relation defined as s0−→(R)ηt0if s0=ηs−→Rt=ηt0. 8.1. Rigid simulation We know that normal termination differs from plain termination because of the existence of versatile terms (or subterms). Our transformation will therefore eliminate these versatile subterms when possible. Definition 8.2.A term lin β-normal form is a rigid pattern if (i) lis not a variable nor an abstraction; (ii) no subterm of lis a versatile application. A higherorder rewrite rule is rigid if its lefthand side is a rigid pattern. A higher-order rewrite system is rigid if so are its rewrite rules. Being a rigid pattern is different from being a higher-order pattern [Miller 1991], for which variables must be applied to different bound variables, resulting in versatile applications. This superficial difference will be understood later. LEMMA 8.3. If lis a rigid pattern and tan arbitrary β-normal term s.t. p∈ Pos(t), then t[lγ]pis β-normal for every β-normal substitution γ. PROOF. The absence of a β-redex in lγ follows from the absence of a versatile subterm in l. Since lis not a variable nor an abstraction, t[lγ]phas no β-redex. Term rewriting systems whose lefthand sides are not rigid patterns will be simulated by rigid systems: ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. A:34 J.-P. Jouannaud and A. Rubio Definition 8.4.A normal higher-order rewrite system Ris simulated by a rigid system RAiff, for any rule l→r∈R, there is a pair made of a rigid rule u→vin RAand an abstracting substitution γ, such that uγ =ηland vγ −→∗ RA∪βr0=ηr. Abstraction by fresh free variables is the main tool to obtain a rigid system from one which is not, therefore explaining our notation RA. Example 8.5.Consider the system R={f(λx.h(@(H, c(x)))) →g(H)}, in which H:α→βis the only free variable. Rcan be abstracted by RA={f(X)→g(X), h(Y)→Y, c(z)→z} The rule in Ris abstracted by the rule f(X)→g(X)added to RA with {X7→ λz.h(@(H, c(z)))}as abstracting substitution, requiring that λz.h(@(H, c(z))) −→∗ RAλz.@(H, z) =ηH. Considering this property as a new rule, it is abstracted in turn by the rule h(Y)→Yin RAwith {Y7→ @(H, c(z))} as abstracting substitution, requiring that @(H, c(z)) −→∗ RA@(H, z). This new requirement is satisfied with the versatile subterm free rule c(z)→zadded to RA. LEMMA 8.6. Let Rbe a rigid system and s, t terms s.t. sp −→ Rtfor some p∈ Pos(s). Then, s↓β−→∗ R∪βt↓β(with s↓β−→+ R∪βt↓βin case p=Λ). PROOF. Since sp −→ Rt, there is a rule u→vin Rand a substitution γ, such that s|p=uγ and t=s[vγ]p. Since uis a rigid pattern, then (uγ)↓β=u(γ↓β)by Lemma 8.3, and u(γ↓β)−→+ Rv(γ↓β)−→∗ βv(γ↓β)↓β= (vγ)↓β. If p=Λwe are done. Otherwise, uis not an abstraction since Ris a rigid system. Moreover, uγ is not an abstraction either since uis not a variable, hence s↓β= (s[uγ]p)↓β=s↓β[uγ↓β]Pwhere Pis the (possibly empty) set of positions of zin s[z]p↓β. Since (uγ)↓β−→+ R∪β(vγ)↓β, then s↓β[uγ↓β]P−→∗ R∪βs↓β[vγ↓β]P −→∗ β(s↓β[vγ↓β]P)↓β= (s[vγ])↓β. THEOREM 8.7. Let Rbe a normal higher-order rewrite system simulated by a rigid system S. Then −→Rβη is terminating if −→(S)η∪βis terminating. PROOF. Assume that S∪βis terminating modulo η. Let then be the higherorder reduction ordering −→+ (S)η∪βand >be defined as s>tiff there exist u→ v∈Sand γsuch that s=uγ and vγ −→∗ S∪βt. Note that this implies that >⊆+. By Theorem 3.8, we need to show that (, >)is a normal higher-order ordering, and for all l→r∈Rthen l0> r0for some l0=ηland r0=ηr. Since Ssimulates R, then for every rule l→r∈Rthere exists u→v∈Sand a substitution γs.t. l0=uγ =ηland vγ −→∗ S∪βr0for some r0=ηr, which implies l0> r0by definition of >. We now show that is normal η-compatible, which means that for all βnormal typed terms s, s0, t such that s0=ηs−→+ (S)η∪βtthere exists some β- ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. Normal Higher-Order Termination A:35 normal term t0=ηtsuch that s0−→+ (S)η∪βt0. Since sis β-normal, we have that s0=ηs−→(S)ηw−→∗ (S)η∪βtfor some term wand, by definition of −→(S)η, we have s0−→(S)ηw, which directly implies that s0−→+ (S)η∪βt, hence we take tas t0. We are left showing that it is normal stable when equipped with the relation . Assume that s>t. By assumption, there is l→r and γs.t. s=lγ −→ Λ Srγ −→∗ S∪βt. Given an arbitrary normal substitution θ,sθ −→ Λ Srγθ −→∗ S∪βtθ. By Lemma 8.6, sθ↓β−→+ S∪βrγθ↓βand rγθ↓β−→∗ S∪βtθ↓β, therefore sθ↓β−→+ S∪βrγθ↓β−→∗ S∪βtθ↓β, hence, by definition, since −→S∪βis included in −→(S)η∪β, we have sθ↓βtθ↓β. Example 8.8.Consider the rules of Example 7.4 (we could take the polymorphic version of Example 7.5 as well). By Theorem 5.19, we can apply first neutralization as an interpretation and then Theorem 8.7 to transform the resulting system to a new one whose plain termination implies that of normal rewriting with the original one. Using the same neutralization as in Example 7.4, and new variables Vτ:τfor all types τ, the rules resulting from abstraction are the following: appσ,τ (absσ,τ (F), X)→F(X)∃− new σ,τ (mc(o,σ)(∃+ σ(Z, X)), Vτ)→Vτ Π0 σ,τ (Πσ,τ (X, Y )) →XΠ1 σ,τ (Πσ,τ (X, Y )) →Y ⊥c(o,σ)→o(∃+ σ(Z, X)) →Z⊥c(o,σ)→σ(∃+ σ(Z, X)) →X appσ,τ (∃− new ρ,σ→τ(mc(o,ρ)(T), Vσ→τ), X)→ ∃− new ρ,τ (mc(o,ρ)(T), appσ,τ (Vσ→τ, X)) Π0 σ,τ (∃− new ρ,c(σ,τ)(mc(o,ρ)(T), Vc(σ,τ))) → ∃− new ρ,τ (mc(o,ρ)(T),Π0 σ,τ (Vc(σ,τ))) Π1 σ,τ (∃− new ρ,c(σ,τ)(mc(o,ρ)(T), Vc(σ,τ))) → ∃− new ρ,τ (mc(o,ρ)(T),Π1 σ,τ (Vc(σ,τ))) ∃− new ρ,c(o,σ)(mc(o,ρ)(T), Vc(o,σ))) →Vc(o,σ) ∃− new σ,τ (mc(o,σ)(∃− new ρ,c(o,σ)(mc(o,ρ)(T), Vc(o,σ))), Vτ)→ ∃− new ρ,τ (mc(o,ρ)(T),∃− new σ,τ (mc(o,σ)(Vc(o,σ)), Vτ)) This system can be easily proved terminating by most of the existing tools for proving termination of higher-order rewriting. Let us show in detail how the last rule of the original example is transformed into the last two given above. The original rule is ∃− σ,τ (∃− ρ,c(o,σ)(T, J), G)→ ∃− ρ,τ (T, λx :o y :ρ.∃− σ,τ (J(x, y), G)) and after applying the same neutralization as in Example 7.4 to both sides of the rule, we obtain ∃− new σ,τ (mc(o,σ)(∃− new ρ,c(o,σ)(mc(o,ρ)(T),@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)))), @(G, ⊥c(o,σ)→o(∃− new ρ,c(o,σ)(mc(o,ρ)(T),@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)))), ⊥c(o,σ)→σ(∃− new ρ,c(o,σ)(mc(o,ρ)(T),@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)))))) → ∃− new ρ,τ (mc(o,ρ)(T),∃− new σ,τ (mc(o,σ)(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))), @(G, ⊥c(o,σ)→o(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))), ⊥c(o,σ)→σ(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)))))) Then the rule is abstracted by ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. A:36 J.-P. Jouannaud and A. Rubio ∃− new σ,τ (mc(o,σ)(∃− new ρ,c(o,σ)(mc(o,ρ)(T), Vc(o,σ))), Vτ)→ ∃− new ρ,τ (mc(o,ρ)(T),∃− new σ,τ (mc(o,σ)(Vc(o,σ)), Vτ)) with the abstracting substitution γ    Vc(o,σ)7→ @(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)) Vτ7→ @(G, ⊥c(o,σ)→o(∃− new ρ,c(o,σ)(mc(o,ρ)(T),@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)))), ⊥c(o,σ)→σ(∃− new ρ,c(o,σ)(mc(o,ρ)(T),@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)))))    Following Definition 8.4, we need to add rules ensuring that the new righthand side instantiated by γ, namely ∃− new ρ,τ (mc(o,ρ)(T),∃− new σ,τ (mc(o,σ)(Vc(o,σ)), Vτ))γ, rewrites to a term η-equivalent to the original righthand side. No middle ηequivalent term is needed. Adding rules to the simulating system Sso that ∃− new ρ,τ (mc(o,ρ)(T),∃− new σ,τ (mc(o,σ)(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)), @(G, ⊥c(o,σ)→o(∃− new ρ,c(o,σ)(mc(o,ρ)(T),@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))) ), ⊥c(o,σ)→σ(∃− new ρ,c(o,σ)(mc(o,ρ)(T),@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))) ))))) rewrites to ∃− new ρ,τ (mc(o,ρ)(T),∃− new σ,τ (mc(o,σ)(@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))), @(G, ⊥c(o,σ)→o( @(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)) ), ⊥c(o,σ)→σ( @(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)) )))) is enough, which can be achieved with the rule ∃− new ρ,c(o,σ)(mc(o,ρ)(T),@(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))) → @(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T)) Since its lefthand side is not a rigid pattern, it must be abstracted again by the rule ∃− new ρ,c(o,σ)(mc(o,ρ)(T), Vc(o,σ))) →Vc(o,σ) with the abstracting substitution {Vc(o,σ)7→ @(J, ⊥c(o,ρ)→o(T),⊥c(o,ρ)→ρ(T))}, and we are done since the new righthand side with the substitution applied coincides with the original one. Hence framed rules are part of the simulating system. Our result allows one to show normal termination of Rby checking for higherorder termination of the rules of the rigid system Smodulo η. Computing a rigid system Ssimulating a given non-rigid system Ris not always possible, but succeeds often in practice. The way used in the previous examples is the following: abstract by a fresh free variable the smallest subterm of lcontaining an outermost versatile subterm, and whose free variables are included in that of l. This defines the abstracting substitution γ. Then, the righthand side is constructed so as to ensure the (stronger than needed) property vγ −→RAr0=ηrin Definition 8.4. Here is one more example using this transformation strategy: Example 8.9.Let H:α→β→ρ,x:αand y:βbe variables, and R={f(h1(g(x, y)),@(@(H, h2(g(x, y))), h3(g(x, y)))) →@(@(H, x), y)} ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. Normal Higher-Order Termination A:37 which can be abstracted by the three rules {f(h1(g(x, y)), Z1)→Z1, h2(g(x, y))) →x, h3(g(x, y)))) →y} Let us now consider a new system with a much bigger rule: f1(m1(f2(m2(T),@(J, h1(T), h2(T)))), @(G, h3(f2(m2(T),@(J, h1(T), h2(T)))), h4(f2(m2(T),@(J, h1(T), h2(T)))))) → f3(m3(T), f1(m1(@(J, h1(T), h2(T))), @(G, h3(@(J, h1(T), h2(T))), h4(@(J, h1(T), h2(T)))))) which contains the free variables J:α→β→ρand T:o. The abstraction includes the following additional three rigid rules: {f1(m1(f2(m2(T), XJ)), XG)→f3(m3(T), f1(m1(XJ), XG)), h3(f2(m2(T), XJ)) →h3(XJ), h4(f2(m2(T), XJ)) →h4(XJ)} 8.2. From termination modulo ηto termination union η-reduction We now turn to η-compatibility. Regarding termination, η-compatibility can easily be replaced by one-side η-compatibility, that is, either s0=ηstimplies s0t or st=ηt0implies st0. This fact is used to show that termination modulo ηcan be guaranteed by showing termination union η-reduction (after some minor transformation). Therefore, we can apply any technique that ensures termination union η-reduction, which is the case of orderings like HORPO or CPO, in order to show termination modulo ηof the original system. As shown below, since the result is proved by working on η-normalized terms, we only have to η-normalize both sides of the rules and add a few rules needed to catch η-reductions that may involve part of the rule and part of the context. For instance if we have a rule @(a, x)→bin our rewrite system, then λx.@(a, x)can be rewritten into λx.b, but its η-normalization acannot be rewritten by the η-normalization of @(a, x)→b which is the same rule. We therefore need to extend the set of rules by adding the η-normalization of the rules with all possible contexts which create η-redexes on top, and then η-normalize these rules. Apart from the extra η-normalization phase, these extensions are similar in spirit to Peterson and Stickel’s notion of associative commutative extension [Peterson and Stickel 1981], and its generalization by Jouannaud and Kirchner [Jouannaud and Kirchner 1986]. Definition 8.10.Let Rbe a higher-order term rewrite system. Let R↓η={l↓η→r↓η|l→r∈R} Rη=R↓η∪{ (λxi. . . xn.l)↓η→(λxi. . . xn.r)↓η|l→r∈R, i ∈[1..n], l= @(l0, x1, . . . , xn),(∀j6=k)xj6=xk,and (∀j)xj/∈ Var(l0)} ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. A:38 J.-P. Jouannaud and A. Rubio For example, if Rcontains a rule @(a, x, y)→bthen Rηcontains the rules {@(a, x, y)→b, @(a, x)→λy.b, a →λxy.b} THEOREM 8.11. Let Rbe a higher-order term rewrite system. Then −→(R)η∪β is terminating if −→Rη∪β∪ηis terminating. PROOF. We show that s0=ηs−→Rt=ηt0implies s0↓η−→+ Rη∪ηt0↓ηand s0=ηs−→βt=ηt0implies s0↓η−→∗ β∪ηt0↓η. By confluence of η,s0↓η=s↓η and t↓η=t0↓η. We therefore need to show that s−→Rtand s−→βtimply s↓η −→+ Rη∪ηt↓ηand s↓η−→∗ β∪ηt↓ηrespectively. — Suppose s=s[lσ]pand t=s[rσ]pfor some rule l→rin R, substitution σand position pin s. We proceed by induction on the size of s. — If p=Λthen it holds since, by Property 8 of λ-calculus, we have lσ↓η=l↓η σ↓ηand rσ↓η=r↓ησ↓ηand l↓η→r↓ηin Rη. — If s=λxi. . . xn.lσ and l= @(l0, x1, . . . , xn), where n > 0, all xjare different, xj/∈ Var(l0)and xj/∈ Dom(σ), there is a rule @(l0, x1, . . . , xi−1)↓η→ λxi. . . xn.r ↓ηin Rη. It follows that s↓η= @(l0, x1, . . . , xi−1)σ↓η= @(l0, x1, . . . , xi−1)↓ησ↓η→Rηλxi. . . xn.r↓ησ↓η=s[rσ]↓η. — If s=λx.@(s0[lσ]p0, x)with x /∈ Var(s0[lσ]p0)then s↓η=s0[lσ]p0↓η. Further, since rewriting cannot introduce new variables, t=λx.@(s0[rσ]p0, x)with x /∈ Var(s0[rσ]p0), hence t↓η=s0[rσ]p0↓η. Induction hypothesis concludes. — Otherwise, if s=λx.s0[lσ]p0then s↓η=λx.(s0[lσ]p0↓η), and t=λx.s0[rσ]p0 with t↓η=λx.(s0[rσ]p0↓η)↓η, by confluence of η-reduction. Now, by induction hypothesis, we have s0[lσ]p0↓η−→+ Rη∪ηs0[rσ]p0↓η, and hence s↓η= λx.(s0[lσ]p0↓η)−→+ Rη∪ηλx.(s0[rσ]p0↓η)−→∗ ηt↓η. — If s=f(s1. . . s0[lσ]p0. . . sn)then s↓η=f(s1↓η. . . s0[lσ]p0↓η. . . sn↓η), and t=f(s1. . . s0[rσ]p0. . . sn)with t↓η=f(s1↓η. . . s0[rσ]p0↓η. . . sn↓η). Now, by induction hypothesis, we have s0[lσ]p0↓η−→+ Rη∪ηs0[rσ]p0↓η, and hence s↓η= f(s1↓η. . . s0[lσ]p0↓η. . . sn↓η)−→+ Rη∪ηf(s1↓η. . . s0[rσ]p0↓η. . . sn↓η) = t↓η. — If s= @(s0[lσ]p0, s2)or s= @(s1, s0[lσ]p0)it holds as in the previous case. — Suppose s=s[@(λx.u, v)]pand t=s[u{x7→ v}]pfor some position pin s. There are two cases. — If u= @(u0, x)with x /∈ Var(u0)then, by confluence of η-reduction, s↓η= s[@(u0, v)]p↓η, and since t=s[@(u0, x){x7→ v}]p=s[@(u0, v)]p, we have that s↓η=t↓ηand thus trivially, s↓η−→∗ β∪ηt↓η. — Otherwise s↓η=s[z]p↓η{z7→ @(λx.u↓η, v ↓η)}, for a fresh variable z∈ Var(s[z]p↓η)by property of η. It follows that s↓η−→βs[z]p↓η{z7→ u↓η {x7→ v↓η}}. Therefore, by confluence of η-reductions, s↓η−→+ β∪ηt↓η. To conclude, we show that if −→Rη∪β∪ηis terminating then there cannot be an infinite sequence with −→(R)η∪β. Assume there is an infinite sequence ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. Normal Higher-Order Termination A:39 s0−→(R)η∪βs1−→(R)η∪β. . .. By the previous property, we get an infinite sequence s0↓η−→∗ Rη∪β∪ηs1↓η−→∗ Rη∪β∪η. . .. Since β-reduction is terminating there must be infinitely many Rη-steps, hence infinitely many (R)η-steps in the original sequence, contradicting our assumption. 8.3. From termination modulo ηto plain termination on η-long forms We reduce here termination modulo ηto plain termination again, but using this time η-expanded forms. The question was first considered in Kop’s PhD thesis [Kop 2012], Sections 2.2 and 2.3, where it is shown that termination of Rmodulo ηcan be ensured by proving termination of some η-expansion of R, provided the lefthand sides of rules in Rare higher-order patterns. Once Ris η-expanded, any termination technique for plain higher-order systems can be used. But since η-expansion relies on types, and type instantiation does not preserve η-long forms, these results do not accommodate polymorphism. Kop considers a restricted form of η-expansion where a subterm uis expanded inside its superterm s, denoted s[u],→ηs[λx.@(u, x)] if —x:σis a fresh variable; —u:σ→τis not (i) a free variable; (ii) an abstraction; (iii) the left argument of an application; ´ (iv) an application of the form @(y, t1, . . . , tn)where yis free in sand n > 0; (v) any of the ti’s in @(y, t1, . . . , tn)whenever yis free in s. The η-long form of s, denoted by s↑η, is the normal form of swith respect to restricted η-expansion. The η-long form of a rewrite system Ris the set R↑η={@(l, z1, . . . , zn)↑η→@(r, z1, . . . , zn)↑η:l→r∈R l:τ1→. . . τn→o, o is a base type and(∀i≤n)zi:τiis fresh} Kop proves the following result (2.15(7) in [Kop 2012]): LEMMA 8.12. Let Rbe a set of rules in η-long form, whose lefthand sides are patterns not headed by λ. Then s−→(R∪β)timplies s↑η−→+ R↑η∪βt↑η. We shall now reduce termination modulo ηto plain termination via the computation of η-long forms in Kop’s sense, hence allowing us to use Lemma 8.12. A nontrivial obstacle is that different η-equivalent terms may have different η-long forms. The reason originates in the syntactic restrictions in the definition of η-expansion that ensure termination, when the subterm of arrow type to be expanded is a free variable or the first argument of an application. For an example, @(λx.@(a, x), b) and @(a, b)are different η-equivalent η-long forms (in her sense). In order to avoid the problem with free variables we show termination by proving the absence of infinite sequences of closed terms, as already done in Theorem 3.8. ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. A:40 J.-P. Jouannaud and A. Rubio The second problem is more delicate, and that is where we need to introduce a new tool. Let β∩ηbe a new rewrite system encoding the intersection of βand η reductions via their two critical pairs: @(λx.@(u, x), v)→@(u, v)if x6∈ Var(u) λx.@(λy.u, x)→λy.u if x6∈ Var(λy.u) . Clearly, β∩η-reductions is included in β- and η-reductions. We now investigate the properties of this rewrite system. LEMMA 8.13 (DIAMOND).The rewrite system β∩ηis strongly confluent. PROOF. Using Huet’s characterization of strong confluence for linear systems [Huet 1980], we need to prove that the 4 critical pairs are strongly confluent. (1) First rule with itself. The superposition term is s= @(λx.@(λy.@(u, y), x), v), with x6∈ Var(λy.@(u, y)) and y6∈ Var(u). Then s−→ @(λx.@(u, x), v)and s−→ @(λy.@(u, y), v), both terms rewriting in one step to @(u, v). (2) Second rule with itself. The superposition term is s=λx.@(λy.@(λz.u, y), x), with x, y 6∈ Var(λz.u). Then s−→ λx.@(λz.u, x)and s−→ λy.@(λz.u, y), both terms rewriting in one step to λz.u. (3) Second rule inside the first. The superposition term is s= @(λx.@(λy.u, x), v), with x6∈ Var(λy.u). Then srewrites to @(λy.u, v)in both cases. (4) First rule inside the second. The superposition term is s= λx.@(λy.@(u, y), x), with x6∈ Var(λy.@(u, y)) and y6∈ Var(u). Then srewrites to λx.@(u, x)with the first rule and λy.@(u, y)with the second, both terms being equal up to α-renaming. Being confluent and terminating, the rewrite system β∩ηenjoys the unique normal form property. We denote by u↓β∩ηthe normal form of uwith respect to β∩η. LEMMA 8.14. Let u, v be two terms such that u−→Rv, where Ris a higherorder rewrite system whose lefthand sides are patterns. Then, u↓β∩η−→+ R∪βv↓β∩η. PROOF. The proof is by induction on ucompared with −→+ β∩η. Let up −→ Rv. If u=u↓β∩η, we are done. Otherwise, uq −→ β∩ηu0. We show by cases on p, q that u0−→Rv0and v−→β∩ηv0for some v0and conclude by induction hypothesis on u0 and confluence of β∩ηwhich ensures that u↓β∩η=u0↓β∩ηand v↓β∩η=v0↓β∩η. (1) If pand qare disjoint, both steps commute as needed. (2) p≥q. Since lefthand sides of rules in Rare rigid patterns, their lefthand side is not an abstraction nor a β-redex, hence no overlap with β∩ηis possible. Therefore, the R-redex is inside the substitution of the other redex, and since β∩ηis linear, both steps must commute and the result holds as above. ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY. Normal Higher-Order Termination A:41 (3) q > p, hence p=q·mfor some position m. This is the difficult case. Assume first that m∈ Pos(l)and l|mis an application, hence there is an overlap between the R-rule and the first rule in β∩η. Then l|mσ= @(λx.@(w1, x), w2). Since lefthand sides of rules in Rare β-normal, l|mcannot have an abstraction as first argument of the application, hence it can only be a variable. But this is impossible too by our assumption that lis a rigid pattern. Assume now that m∈ Pos(l)and l|mis an abstraction, implying an overlap between the R-rule and the second rule in β∩η. Then l|mσ=λx.@(λy.w, x) with x6∈ Var(λy.u). Higher-order substitutions do not capture variables, hence l|m=λx.@(w0, x), and w0must be an abstraction or a free variable. Impossible. We are left with the case where no overlap occurs, which holds as before. LEMMA 8.15. Given two terms u, v such that u−→βvthen u↓β∩η−→∗ βv↓β∩η. PROOF. Let up −→ βv, hence u|p= @(λx.s0, t0). The proof is by induction on u. If u=u↓β∩η, we are done. Otherwise, uq −→ β∩ηu0for some term u0and position q. We show that u0−→βv00 and v−→∗ β∩ηv00 and conclude by induction hypothesis on u0. (1) If the position qis disjoint from p, or at a strict prefix of pwithout overlapping p, or inside s0, or inside t0, then the same β∩η-rewrite takes place in v(possibly many times in the latter case), yielding the result. (2) If u|q= @(λy.@(λx.s0, y), t), with t0=y6∈ Var(λx.s0). Then u0and vare equal up to variable renaming. (3) If u|q=λy.@(λx.s0, y), that is, t0=y6∈ Var(λx.s0). Then u0and vare equal up to variable renaming again. LEMMA 8.16. Let u, v be β∩η-normal closed terms. If u=ηvthen u↑η =v↑η. PROOF. We first prove that if uis closed and normal with respect to β∩ηand u−→ηu0then u↑η=u0↑η. By assumption we have that there is a position pin usuch that u|p=λx.@(w, x) with x6∈ Var(w)and u0=u[w]p. Moreover, u0is also closed and normal with respect to β∩η. Now, we show that, u0=u[w],→ηu[λx.@(w, x)] = u, which implies that u↑η=u0↑η. Assume that the step u[w],→ηu[λx.@(w, x)] cannot be applied, then, since u0is closed there are only two possible reasons. — If wis an abstraction of the form λy.s then, we have that u|p=λx.@(λy.s, x), which contradicts the fact that uis normal with respect to β∩η. — If wis the left argument of an application then, with qthe position just above p, we have that uq= @(λx.@(w, x), w0)for some w0, which again contradicts the fact that uis normal with respect to β∩η. ACM Journal Name, Vol. V, No. N, Article A, Publication date: January YYYY.