next up previous
Next: Implementation Up: foetus - Termination Checker Previous: Termination Checker Overall Outline


Formal Description  

Call Matrix

Be R = {` < ',` = ',`?'} set of the relations ``less than'', ``equal to'' and ``relation unknown''. In the context of `` f (x,y) calls g(a,b) '' a ` < ' y means ``we know that (call) argument a is less than (input) parameter y '', a ` = ' y means `` a is (at least) equal to y (if not less than)'' and a `?' y means ``we do not know the relation between a and y ''.

With the two operations + and $\cdot$ defined as in table 1 R forms a commutative rig[*] with 0-element `?' and 1 -element ` = ' . The operation + can be understood as ``combining parallel information about a relation'', e.g. if we have a `?' y and a ` < ' y we have a (`?' + ` < ') y and that simplifies to a ` < ' y . The operation $\cdot$ however is ``serial combination'', e.g. a ` < ' y and y ` = ' z can be combined into a (` < ' $\cdot$ ` = ') z , simplified: a ` < ' z . `?' is neutral regarding + because it gives you no new information, whereas ` < ' is dominant because it is the strongest information. Regarding $\cdot$ the relation ` = ' is neutral and `?' is dominant because it ``destroys'' all information. Check the table to see which relation overrides which.

Table 1: Operations on R
+ ` < ' ` = ' `?'
` < ' ` < ' ` < ' ` < '
` = ' ` < ' ` = ' ` = '
`?' ` < ' ` = ' `?'
$\cdot$ ` < ' ` = ' `?'
` < ' ` < ' ` < ' `?'
` = ' ` < ' ` = ' `?'
`?' `?' `?' `?'

Now we can define multiplication on matrices over R as usual:

$\displaystyle\cdot$ : R n x m x R m x l $\displaystyle\rightarrow$ R n x l

$\displaystyle\left((a_{ij}), (b_{ij})\right)\mapsto$(cij) = $\displaystyle\left(
 \sum_{k=1}^m a_{ik} b_{kj} \right)$

Why is this a reasonable definition? Assume you have three sets of variables {x1,...,xn} , {y1,...,ym} and {z1,...,zl} , a matrix A = (aij) $\epsilon$ R n x m reflecting the relations between the xi s and the yi s (i.d. aij = $\rho$$\iff$xi $\rho$ yj ) and a matrix B $\epsilon$ R m x l reflecting the relations between the yi s and the zi s. Then the matrix product C = AB reflects the relations between the xi s and the zi s. Because

cij = ai1 $\displaystyle\cdot$ b1j + ai2 $\displaystyle\cdot$ b2j + ... + aim $\displaystyle\cdot$ bmj,

we have e.g. xi ` < ' zj if we know it by intermediate variable y1 ( ai1 $\cdot$ b1j = ` < ' ) or by intermediate variable y2 or ... (to be continued).


A call matrix is a matrix over R with no more than one element different from `?' per row.

CM(n,m) : = {(aij) $\displaystyle\epsilon$ R n x m : $\displaystyle\forall$i$\displaystyle\forall$j$\displaystyle\forall$k $\displaystyle\not=$j(aij = `?'$\displaystyle\lor$aik = `?')}


The reason we define call matrices this way is these are the only ones foetus produces by function call extraction (see section 4.1). Because foetus recognizes only the three described cases of dependecies, a call argument can only depend of one function parameter. But multiple dependecies are imaginable, like in
f(x,y) = if (x=0) then 0 else let a=min(x,y)-1 in f(a,x)
Here the second call argument a is less than both x and y. The next proposition assures that all matrices foetus will have do deal with are call matrices.


Matrix multiplication on matrices induces a multiplication on call matrices

$\displaystyle\cdot$ : CM(n,m) x CM(m,l ) $\displaystyle\rightarrow$ CM (n,l )

This operation is well defined.


Be A = (aij) $\epsilon$ CM (n,m) , B = (bij) $\epsilon$ CM (m,l ) , AB = C = (cij) $\epsilon$ R n x l and k(i) the index of the element of the i th row of A that is different to `?' (or 1, if no such element exists). The we have with the rules in rig R

cij = $\displaystyle\sum_{k=1}^{m}$aikbkj = ai,k(i)bk(i),j

Now consider the i th row of C :

ci = (cij)1 $\scriptstyle\leq$ j $\scriptstyle\leq$ l = (ai,k(i)bk(i),j)1 $\scriptstyle\leq$ j $\scriptstyle\leq$ l

Because at most one bk(i),j is unequal to `?' , at most one element of ci is unequal to `?' . Therefore C $\epsilon$ CM (n,l ) .

Call Graph

For each i $\epsilon$ N we assume a set F (i) = {f (i),g (i),h (i),...} of identifiers for functions of arity i , $
\mathcal {F}
$ = $\biguplus_{i \,\epsilon\,\mathbf{N}}^{}$F (i).


We form the set of calls as follows

\mathcal {C}
$ = {(f (n),g (m),A) : f (n) $\displaystyle\epsilon$ F (n),g (m) $\displaystyle\epsilon$ F (m),A $\displaystyle\epsilon$ CM(m,n)}

On calls we define the partial operation combination of calls

o : $\displaystyle
\mathcal {C}
$ x $\displaystyle
\mathcal {C}
$ $\displaystyle\rightarrow$ $\displaystyle
\mathcal {C}

$\displaystyle\left( (g^{(m)}, h^{(l)}, B), (f^{(n)}, g^{(m)}, A) \right)\mapsto$(f (n),h (l),BA)

Meaning: If g calls h with call matrix B and f calls g with call matrix A , then f indirectly calls h with call matrix BA . o cannot be applied to calls that have no ``common function'' like g , therefore it is partial. o can be expanded to sets of calls

o : $\displaystyle
\mathcal {P}
\mathcal {C}
$) x $\displaystyle
\mathcal {P}
\mathcal {C}
$) $\displaystyle\rightarrow$ $\displaystyle
\mathcal {P}
\mathcal {C}

(C,C') $\displaystyle\mapsto$ {c o $\scriptstyle
\mathcal {C}
$c' : c $\displaystyle\epsilon$ C,c' $\displaystyle\epsilon$ C',(c,c') $\displaystyle\epsilon$ Dom( o $\scriptstyle
\mathcal {C}

Here we combine each call in C with each call in $\hat{C}$ to which o $\scriptstyle
\mathcal {C}
$ is applicable and form a set of the combined calls. o $\scriptstyle
\mathcal {P}
\mathcal {C}
$) is a total function.


A call graph is a graph (V,E) with vertices V = $
$F and edges E $\subset_{\mathrm{finit}}^{}$ $
\mathcal {C}
$ . A call graph is complete if

E o E $\displaystyle\subseteq$ E


The completion of a call graph (V,E) is a call graph (V,E') such that

(V,E') is complete, 
E $\subseteq$ E' and 
for all E'' satisfying (1) and (2) we have E' $\subseteq$ E'' .


The completion of a call graph (V,E) is the call graph (V,E') such that

c $\displaystyle\epsilon$ E'$\displaystyle\iff$$\displaystyle\exists$n > 0,c1,...,cn $\displaystyle\epsilon$ E : c1 o ... o cn = c


Be c $\epsilon$ E' o E' . Then there are d,e $\epsilon$ E' with c = d o e . Because (V,E') is complete, we have
d = d1 o ... o dn $\textstyle\qquad$ d1,...,dn $\displaystyle\epsilon$ E   
e = e1 o ... o em $\textstyle\qquad$ e1,...,em $\displaystyle\epsilon$ E   
Thus c = d1 o ... o dn o e1 o ... o em $\epsilon$ E' .
E $\subseteq$ E' is trivial with n = 1 .
Be (V,E'') complete and E $\subseteq$ E'' . This gives us E o E $\subseteq$ E'' from which we gain by induction

$\displaystyle\underbrace{E \circ \dots \circ E}_{n\mathrm{-times}}^{}$ = : E n $\displaystyle\subseteq$ E''    for   all   n.

Now be c $\epsilon$ E' . That implies c = c1 o ... o cn ( ci $\epsilon$ E ) for a suitable n . Hence c $\epsilon$ E n $\subseteq$ E'' . q $\cdot$ e $\cdot$ d

Proposition. (Completion algorithm)

Be (V,E) a call graph, (V,E') its completion and (En)n $\scriptstyle\epsilon$ N a sequence of sets of calls defined as follows:
E0 = E   
En + 1 = En $\displaystyle\cup$ (En o E)   
Then there is a n $\epsilon$ N so that

E' = En = En + 1 = En + 2 = ...

(Obviously the En grow monotonously.)


First we show by induction that En $\subseteq$ E' for all n $\epsilon$ N : It is obvious that E0 $\subseteq$ E' . Now be En $\subseteq$ E' and c $\epsilon$ En + 1 $\setminus$ En . Then c $\epsilon$ En o E , therefore c = d1 o ... o dn o e , d1,...,dn,e $\epsilon$ E . It follows c $\epsilon$ E' , En + 1 $\subseteq$ E' .

Second: Because we have a finit set of starting edges E and therefore a finit set of reachable vertices and also a finit set of possible edges between two vertices (limited by the number of different call matrices of fixed dimensions) the Ei s cannot grow endlessly. Thus an n $\epsilon$ N exists with En = En + 1 .

Third: We show that E' $\subseteq$ En for that particular n . Be c $\epsilon$ E' . Then there exists an m such that c = d1 o ... o dm , therefore c $\epsilon$ Em . Now if m $\leq$ n then Em $\subseteq$ En , otherwise m > n and hence Em = En , in both cases c $\epsilon$ En . q $\cdot$ e $\cdot$ d

Lexical Order


Be (V,E) a complete call graph and f (i) a function of arity i . We call

Ef(i) : = {$\displaystyle\Delta$(C) : (f (i),f (i),C) $\displaystyle\epsilon$ E} $\displaystyle\subset$ R i

the recursion behaviour of function f (i). ( $\Delta$ takes the diagonal of square matrices).

Each row of this set represents one possible recursive call of f (i) and how the orders of all parameters are altered in this call. The diagonals of the call matrices are taken because we want to know only how a parameter relates to its old value in the last call to f (i). Ef(i) of course is a finite subset of R i.

In the following we identify lexical orders on parameters with permutations $\pi$ $\epsilon$ Sn of the arguments. Often not all of the parameters are relevant for termination; these are not listed in the lexical order and can appear in the permutation in any sequence.

In example 3.11 (fib') only the argument 0 has to be considered to prove termination, the order of argument 1 and 2 are irrelevant and therefore both permutations

$\displaystyle\pi_{1}^{}$ = $\displaystyle\left(\begin{array}
{ccc}0&1&2\  0&1&2\end{array}\right)$


$\displaystyle\pi_{2}^{}$ = $\displaystyle\left(\begin{array}
{ccc}0&1&2\  0&2&1\end{array}\right)$

are valid continuations of the lexical order ``0''.


In the following we abbreviate the notation of permutations to $\pi_{1}^{}$ = [012] and $\pi_{2}^{}$ = [021] .

Definition. (1)

Be B the recursion behaviour of function f (n). We call the permutation $\pi$ $\epsilon$ Sn a termination order for f (n) if

$\displaystyle\forall$r $\displaystyle\epsilon$ B$\displaystyle\exists$1 $\displaystyle\leq$ k $\displaystyle\leq$ n : r$\scriptstyle\pi$(k) = ` < '$\displaystyle\land$($\displaystyle\forall$1 $\displaystyle\leq$ i $\displaystyle\leq$ k : r$\scriptstyle\pi$(i) = ` = ')

This definition is a very wide one. In most cases you will look for more special termination orders:

Definition. (2, inductive)

Be B the recursion behaviour of a given function. We call the permutation $\pi$ $\epsilon$ Sn a termination order on B if |B| = 0 or
     $\displaystyle\exists$r $\displaystyle\epsilon$ B : r$\scriptstyle\pi$(0) = ` < '   
     $\displaystyle\land$ $\displaystyle\not\exists$r $\displaystyle\epsilon$ B : r$\scriptstyle\pi$(0) = `?'   
     $\displaystyle\land$ $\displaystyle\pi$' $\displaystyle\epsilon$ Sn - 1  termination   order   on   B' : = {r'$\scriptstyle\pi$(0) : r$\scriptstyle\pi$(0) $\displaystyle\not=$` < '} $\displaystyle\subset$ R n - 1   
whereas $\pi$'i = [ - 1ki + - 1] $\epsilon$ Sn - 1 given $\pi$ = [ - 1] $\epsilon$ Sn and r'i = (k0,...,ki - 1,ki + 1,...,kn - 1) $\epsilon$ R n - 1 given r = (k0,...,kn - 1) $\epsilon$ R n.

The algorithm implemented in foetus searches termination orders like in definition (2); it is a one-to-one transfer of this definition. Every termination order matching definition (2) also matches definition (1) and it can easily be shown that if there is a termination order of type (1) there also exists on of type (2).

Example 2986

Be E = {(` = ',` < ',`?'),(` = ',` = ',` < '),(` = ',` < ',` = ')} the given recursion behaviour. Then $\pi_{1}^{}$ = [012] is a type (1) termination order on E and $\pi_{2}^{}$ = [120] is of both types.

next up previous
Next: Implementation Up: foetus - Termination Checker Previous: Termination Checker Overall Outline
Andreas Abel, 7/16/1998