This paper was converted on www.awesomepapers.org from LaTeX by an anonymous user.
Want to know more? Visit the Converter page.

\Copyright

Frédéric Blanqui and Guillaume Genestier

Termination of λΠ\lambda\Pi modulo rewriting using the size-change principle (work in progress)

Frédéric Blanqui LSV, ENS Paris-Saclay, CNRS, Université Paris-Saclay Inria Guillaume Genestier LSV, ENS Paris-Saclay, CNRS, Université Paris-Saclay Inria MINES ParisTech, PSL University
Abstract

The Size-Change Termination principle was first introduced to study the termination of first-order functional programs. In this work, we show that it can also be used to study the termination of higher-order rewriting in a system of dependent types extending LF.

keywords:
Termination, Higher-Order Rewriting, Dependent Types, Lambda-Calculus.

1 Introduction

The Size-Change Termination principle (SCT) was first introduced by Lee, Jones and Ben Amram [8] to study the termination of first-order functional programs. It proved to be very effective, and a few extensions to typed λ\lambda-calculi and higher-order rewriting were proposed.

In his PhD thesis [13], Wahlstedt proposes one for proving the termination, in some presentation of Martin-Löf’s type theory, of an higher-order rewrite system \mathcal{R} together with the β\beta-reduction of λ\lambda-calculus. He proceeds in two steps. First, he defines an order, the instantiated call relation, and proves that =β{\longrightarrow}={\longrightarrow_{\mathcal{R}}\cup\longrightarrow_{\beta}} terminates on well-typed terms whenever this order is well-founded. Then, he uses SCT to eventually show the latter.

However, Wahlstedt’s work has some limitations. First, it only considers weak normalization, that is, the mere existence of a normal form. Second, it makes a strong distinction between “constructor” symbols, on which pattern matching is possible, and “defined” symbols, which are allowed to be defined by rewrite rules. Hence, it cannot handle all the systems that one can define in the λΠ\lambda\Pi-calculus modulo rewriting, the type system implemented in Dedukti [2].

Other works on higher-order rewriting do not have those restrictions, like [3] in which strong normalization (absence of infinite reductions) is proved in the calculus of constructions by requiring each right-hand side of rule to belong to the Computability Closure (CC\operatorname{CC}) of its corresponding left-hand side.

In this paper, we present a combination and extension of both approaches.

2 The λΠ\lambda\Pi-calculus modulo rewriting

We consider the λΠ\lambda\Pi-calculus modulo rewriting [2]. This is an extension of Automath, Martin-Löf’s type theory or LF, where functions and types can be defined by rewrite rules, and where types are identified modulo those rules and the β\beta-reduction of λ\lambda-calculus.

Assuming a signature made of a set 𝒞T\mathcal{C}_{T} of type-level constants, a set T\mathcal{F}_{T} of type-level definable function symbols, and a set o\mathcal{F}_{o} of object-level function symbols, terms are inductively defined into three categories as follows:

kind-level terms KK ::= Type(x:U)K\operatorname{Type}\mid(x:U)\rightarrow K
type-level terms T,UT,U ::= λx:U.T(x:U)TUtDF\lambda x:U.\,T\mid(x:U)\rightarrow T\mid U\,t\mid D\mid F where D𝒞TD\in\mathcal{C}_{T} and FTF\in\mathcal{F}_{T}
object-level terms t,ut,u ::= xλx:U.ttufx\mid\lambda x:U.\,t\mid t\,u\mid f where fof\in\mathcal{F}_{o}

By t¯\bar{t}, we denote a sequence of terms t1tnt_{1}\dots t_{n} of length |t¯|=n|\bar{t}|=n.

Next, we assume given a function τ\tau associating a kind to every symbol of 𝒞T\mathcal{C}_{T} and T\mathcal{F}_{T}, and a type to every symbol of o\mathcal{F}_{o}. If τ(f)=(x1:T1)(xn:Tn)U\tau(f)=(x_{1}:T_{1})\rightarrow\dots\rightarrow(x_{n}:T_{n})\rightarrow U with UU not an arrow, then ff is said of arity ar(f)=n\operatorname{ar}(f)=n.

An object-level function symbol ff of type (x1:T1)(xn:Tn)Du1uar(D)(x_{1}:T_{1})\rightarrow\dots\rightarrow(x_{n}:T_{n})\rightarrow D\,u_{1}\dots u_{\operatorname{ar}(D)} with D𝒞TD\in\mathcal{C}_{T} and every TiT_{i} of the form Ev1var(E)E\,v_{1}\dots v_{\operatorname{ar}(E)} with E𝒞TE\in\mathcal{C}_{T} is called a constructor. Let 𝒞o\mathcal{C}_{o} be the set of constructors.

Terms built from variables and constructor application only are called patterns:
p::=xcp1par(c) where c𝒞op::=x\mid c\,p_{1}\dots p_{\operatorname{ar}(c)}\text{ where }c\in\mathcal{C}_{o}.

Next, we assume given a set \mathcal{R} of rewrite rules of the form fp1par(f)rf\,p_{1}\dots p_{\operatorname{ar}(f)}\longrightarrow r, where ff is in o\mathcal{F}_{o} or T\mathcal{F}_{T}, the pip_{i}’s are patterns and rr is β\beta-normal. Then, let =β{\longrightarrow}={\longrightarrow_{\beta}\cup\longrightarrow_{\mathcal{R}}} where \longrightarrow_{\mathcal{R}} is the smallest rewrite relation containing \mathcal{R}.

Note that rewriting at type level is allowed. For instance, we can define a function taking a natural number nn and returning NatNatNat\operatorname{Nat}\rightarrow\operatorname{Nat}\rightarrow\dots\rightarrow\operatorname{Nat} with as many arrows as nn. In Dedukti syntax, this gives:

def F : Nat -> Type.
F 0 --> Nat.
nn
F (S n) --> Nat -> (F n).

Well-typed terms are defined as in LF, except that types are identified not only modulo β\beta-equivalence but modulo \mathcal{R}-equivalence also, by adding the following type conversion rule:

Γt:A\Gamma\vdash t:A   ΓA:s\Gamma\vdash A:s   ΓB:s\Gamma\vdash B:s  if AB and s{Type,Kind}\quad\text{if }A\longleftrightarrow^{*}B\text{ and }s\in\{\operatorname{Type},\operatorname{Kind}\}       Γt:B\Gamma\vdash t:B

Convertibility of AA and BB, ABA\longleftrightarrow^{*}B, is undecidable in general. However, it is decidable if \longrightarrow is confluent and terminating. So, a type-checker for the λΠ\lambda\Pi-calculus modulo \longleftrightarrow^{*}, like Dedukti, needs a criterion to decide termination of \longrightarrow. This is the reason of this work.

To this end, we assume that \longrightarrow is confluent and preserves typing.

There exist tools to check confluence, even for higher-order rewrite systems, like CSIˆho or ACPH. The difficulty in presence of type-level rewrite rules, is that we cannot assume termination to show confluence since we need confluence to prove termination. Still, there is a simple criterion in this case: orthogonality [12].

Checking that \longrightarrow preserves typing is undecidable too (for β\longrightarrow_{\beta} alone already), and often relies on confluence except when type-level rewrite rules are restricted in some way [3]. Saillard designed and implemented an heuristic in Dedukti [10].

Finally, note that constructors can themselves be defined by rewrite rules. This allows us to define, for instance, the type of integers with two constructors for the predecessor and successor, together with the rules stating that they are inverse of each other.

3 The Size-Change Termination principle

Introduced for first-order functional programming languages by Lee, Jones and Ben Amram [8], the SCT is a simple but powerful criterion to check termination. We recall hereafter the matrix-based presentation of SCT by Lepigre and Raffalli [9].

Definition 3.1 (Size-Change Termination principle).

The (strict) constructor subterm relation \lhd is the smallest transitive relation such that tict1tnt_{i}\lhd c\,t_{1}\dots t_{n} when c𝒞oc\in\mathcal{C}_{o}.

We define the formal call relation by fp¯f\,\bar{p} >callgt¯>_{call}g\,\bar{t} if there is a rewrite rule fp¯rf\,\bar{p}\longrightarrow r\in\mathcal{R} such that gTog\in\mathcal{F}_{T}\cup\mathcal{F}_{o} and gt¯g\,\bar{t} is a subterm of rr with |t¯|=ar(g)|\bar{t}|=\operatorname{ar}(g).

From this relation, we construct a call graph whose nodes are labeled with the defined symbols. For every call fp¯>callgt¯f\,\bar{p}>_{call}g\,\bar{t}, an edge labeled with the call matrix (ai,j)iar(f),jar(g)(a_{i,j})_{i\leqslant\operatorname{ar}(f),j\leqslant\operatorname{ar}(g)} links the nodes ff and gg, where ai,j=1a_{i,j}=-1 if tjpit_{j}\lhd p_{i}, ai,j=0a_{i,j}=0 if tj=pit_{j}=p_{i}, and ai,j=a_{i,j}=\infty otherwise.

A set of rewrite rules \mathcal{R} satisfies the size-change termination principle if the transitive closure of the call graph (using the max-plus semi-ring to multiply the matrices) is such that all arrows linking a node with itself are labeled with a matrix having at least one 1-1 on the diagonal.

The formal call relation is also called the dependency pair relation [1].

4 Wahlstedt’s extension of SCT to Martin-Löf’s Type Theory

The proof of weak normalization in Wahlstedt’s thesis uses an extension to rewriting of Girard’s notion of reducibility candidate [7], called computability predicate here. This technique requires to define an interpretation of every type TT as a set of normalizing terms T\llbracket{T}\rrbracket called the set of computable terms of type TT. Once this interpretation is defined, one shows that every well-typed term t:Tt:T is computable, that is, belongs to the interpretation of its type: tTt\in\llbracket{T}\rrbracket, ending the normalization proof. To do so, Wahlstedt proceeds in two steps. First, he shows that every well-typed term is computable whenever all symbols are computable. Then, he introduces the following relation which, roughly speaking, corresponds to the notion of minimal chain in the DP framework [1]:

Definition 4.1 (Instantiated call relation).

Let ft¯~gv¯f\,\bar{t}\leavevmode\nobreak\ \widetilde{\succ}\leavevmode\nobreak\ g\,\bar{v} if there exist p¯\bar{p}, u¯\bar{u} and a substitution γ\gamma such that t¯\bar{t} is normalizing, t¯p¯γ\bar{t}\longrightarrow^{*}\bar{p}\gamma, fp¯>callgu¯f\,\bar{p}>_{call}g\,\bar{u} and u¯γ=v¯\bar{u}\gamma=\bar{v}.

and proves that all symbols are computable if ~\leavevmode\nobreak\ \widetilde{\succ}\leavevmode\nobreak\ is well-founded:

Lemma 4.2 ([13, Lemma 3.6.6, p. 82]).

If ~\leavevmode\nobreak\ \widetilde{\succ}\leavevmode\nobreak\ is well-founded, then all symbols are computable.

Finally, to prove that ~\leavevmode\nobreak\ \widetilde{\succ}\leavevmode\nobreak\ is well-founded, he uses SCT:

Lemma 4.3 ([13, Theorem 4.2.1, p. 91]).

~\leavevmode\nobreak\ \widetilde{\succ}\leavevmode\nobreak\ is well-founded whenever the set of rewrite rules satisfies SCT\operatorname{SCT}.

Indeed, if ~\leavevmode\nobreak\ \widetilde{\succ}\leavevmode\nobreak\ were not well-founded, there would be an infinite sequence f1t¯1~f2t¯2~f_{1}\,\bar{t}_{1}\leavevmode\nobreak\ \widetilde{\succ}\leavevmode\nobreak\ f_{2}\,\bar{t}_{2}\leavevmode\nobreak\ \widetilde{\succ}\leavevmode\nobreak\ \dots, leading to an infinite path in the call graph which would visit infinitely often at least one node, say ff. But the matrices labelling the looping edges in the transitive closure all contain at least one 1-1 on the diagonal, meaning that there is an argument of ff which strictly decreases in the constructor subterm order at each cycle. This would contradict the well-foundedness of the constructor subterm order.

However, Wahlstedt only considers weak normalization of orthogonal systems, in which constructors are not definable. There exist techniques which do not suffer those restrictions, like the Computability Closure.

5 Computability Closure

The Computability Closure (CC\operatorname{CC}) is also based on an extension of Girard’s computability predicates [4], but for strong normalization. The gist of CC\operatorname{CC} is, for every left-hand side of a rule fl¯f\,\bar{l}, to inductively define a set CC(fl¯)\operatorname{CC}_{\sqsupset}(f\,\bar{l}) of terms that are computable whenever the lil_{i}’s so are. Function applications are handled through the following rule:

fl¯gu¯f\,\bar{l}\sqsupset g\,\bar{u}   u¯CC(fl¯)\bar{u}\in\operatorname{CC}_{\sqsupset}(f\,\bar{l})      gu¯CC(fl¯)g\,\bar{u}\in\operatorname{CC}_{\sqsupset}(f\,\bar{l})

where =(,()stat)lex{\sqsupset}={(\succ_{\mathcal{F}},(\rhd\cup\longrightarrow)_{\mathrm{stat}})_{\mathrm{lex}}} is a well-founded order on terms ft¯f\,\bar{t} such that t¯\bar{t} are computable, with \succ_{\mathcal{F}} a precedence on function symbols and stat\mathrm{stat} either the multiset or the lexicographic order extension, depending on ff.

Then, to get strong normalization, it suffices to check that, for every rule fl¯rf\,\bar{l}\longrightarrow r, we have rCC(fl¯)r\in\operatorname{CC}_{\sqsupset}(f\,\bar{l}). This is justified by Lemma 6.38 [3, p.85] stating that all symbols are computable whenever the rules satisfy CC\operatorname{CC}, which looks like Lemma 4.2. It is proved by induction on {\sqsupset}. By definition, ft¯f\,\bar{t} is computable if, for every uu such that ft¯uf\,\bar{t}\longrightarrow u, uu is computable. There are two cases. If u=ft¯u=f\,\bar{t}^{\prime} and t¯t¯\bar{t}\longrightarrow\bar{t}^{\prime}, then we conclude by the induction hypothesis. Otherwise, u=rγu=r\,\gamma where rr is the right-hand side of a rule whose left-hand side is of the form fl¯f\,\bar{l}. This case is handled by induction on the proof that rCC(fl¯)r\in\operatorname{CC}_{\sqsupset}(f\,\bar{l}).

So, except for the order, the structures of the proofs are very similar in both works. This is an induction on the order, a case distinction and, in the case of a recursive call, another induction on a refinement of the typing relation, restricted to β\beta-normal terms in Wahlstedt’s work and to the Computability Closure membership in the other one.

6 Applying ideas of Computability Closure in Wahlstedt’s criterion

We have seen that each method has its own weaknesses: Wahlstedt’s SCT deals with weak normalization only and does not allow pattern-matching on defined symbols, while CC\operatorname{CC} enforces mutually defined functions to perform a strict decrease in each call.

We can subsume both approaches by combining them and replacing in the definition of CC\operatorname{CC} the order \sqsupset by the formal call relation:

fl¯>callgu¯f\,\bar{l}>_{call}g\,\bar{u}   u¯CC>call(fl¯)\bar{u}\in\operatorname{CC}_{>_{call}}(f\,\bar{l})      gu¯CC>call(fl¯)g\,\bar{u}\in\operatorname{CC}_{>_{call}}(f\,\bar{l})

We must note here that, even if >call>_{call} is defined from the constructor subterm order, this new definition of CC\operatorname{CC} does not enforce an argument to be strictly smaller at each recursive call, but only smaller or equal, with the additional constraint that any looping sequence of recursive calls contains a step with a strict decrease, which is enforced by SCT.

Proposition 6.1.

Let \mathcal{R} be a rewrite system such that =β{\longrightarrow}={\longrightarrow_{\beta}\cup\longrightarrow_{\mathcal{R}}} is confluent and preserves typing. If \mathcal{R} satisfies CC>call\operatorname{CC}_{>_{call}} and SCT\operatorname{SCT}, then \longrightarrow terminates on every term typable in the λΠ\lambda\Pi-calculus modulo \longleftrightarrow^{*}.

Note that CC>call\operatorname{CC}_{>_{call}} essentially reduces to checking that the right-hand sides of rules are well-typed which is a condition that is generally satisfied.

The main difficulty is to define an interpretation for types and type symbols that can be defined by rewrite rules. It requires to use induction-recursion [6]. Note that the well-foundedness of the call relation ~\leavevmode\nobreak\ \widetilde{\succ}\leavevmode\nobreak\ is used not only to prove reducibility of defined symbols, but also to ensure that the interpretation of types is well-defined.

If we consider the example of integers mentioned earlier and define the function erasing every constructor using an auxiliary function, we get a system rejected both by Wahlstedt’s criterion since 𝚂\mathtt{S} and 𝙿\mathtt{P} are defined, and by the CCCC_{\sqsupset} criterion since there is no strict decrease in the first rule. On the other hand, it is accepted by our combined criterion.

Int : Type. 0 : Int.
def S : Int -> Int. def P : Int -> Int.
xx
S (P x) --> x.
xx
P (S x) --> x.
xx
returnZero x --> aux x.
aux 0 --> 0.
xx
aux (S x) --> returnZero x.
xx
aux (P x) --> returnZero x.

7 Conclusion

We have shown that Wahlstedt’s thesis [13] and the first author’s work [3] have strong similarities. Based on this observation, we developed a combination of both techniques that strictly subsumes both approaches.

This criterion has been implemented in the type-checker Dedukti [2] and gives promising results, even if automatically proving termination of expressive logic encodings remains a challenge. The code is available at https://github.com/Deducteam/Dedukti/tree/sizechange.

Many opportunities exist to enrich our new criterion. For instance, the use of an order leaner than the strict constructor subterm for SCT, like the one defined by Coquand [5] for handling data types with constructors taking functions as arguments. This question is studied in the first-order case by Thiemann and Giesl [11].

Finally, it is important to note the modularity of Wahlstedt’s approach. Termination is obtained by proving 1) that all terms terminate whenever the instantiated call relation is well-founded, and 2) that the instantiated call relation is indeed well-founded. Wahlstedt and we use SCT to prove 2) but it should be noted that other techniques could be used as well. This opens the possibility of applying to type systems like the ones implemented in Dedukti, Coq or Agda, techniques and tools developed for proving the termination of DP problems.

Acknowledgments. The authors thank Olivier Hermant for his comments, as well as the anonymous referees.

References