Theory of Finite or Infinite Trees Revisited
We present in this paper a first-order axiomatization of an extended theory $T$ of finite or infinite trees, built on a signature containing an infinite set of function symbols and a relation $\fini(t)$ which enables to distinguish between finite or …
Authors: ** K. Djelloul, T. Dao **
Under c onsider ation for public ation in The ory and Pr actic e of L o gic Pr o gr amming 1 The ory of Finite or Infinite T r e es R evisite d KHALIL DJELLOUL F aculty of computer scienc e University of Ulm Germany THI-BICH-HANH D AO L ab or atoir e d’informatique fondamentale d’Orle ans Universite d’Orle ans F ranc e THOM FR ¨ UHWIR TH F aculty of computer scienc e University of Ulm Germany submitte d 15 Octob er 2006; r evise d 6 Mars 2007 ; ac c epte d 27 June 2007 T o app ear in Theory and Practice of Logic Programming (TPLP) Abstract W e presen t in this paper a first-order axiomatization of an extended theory T of finite or infinite trees, built on a signature containing an infinite set of function symbols and a relation finite ( t ) which enables to distinguish betw een finite or infinite trees. W e show that T has at least one mo del and prov e its completeness by giving not only a decision pro cedure, but a full first-order constraint solver which gives clear and explicit solutions for an y first-order constrain t satisfaction problem in T . The solv er is giv en in the form of 16 rewriting rules whic h transform any first-order constraint ϕ in to an equiv alent disjunction φ of simple form ulas such that φ is either the form ula true or the form ula false or a form ula having at least one free v ariable, b eing equiv alen t neither to true nor to false and where the solutions of the free v ariables are expressed in a clear and explicit wa y . The correctness of our rules implies the completeness of T . W e also describe an implementation of our algorithm in CHR (Constraint Handling Rules) and compare the p erformance with an implemen tation in C++ and that of a recent decision pro cedure for decomp osable theories. KEYWORDS : Logical first-order form ula, Theory of finite or infinite trees, Complete theory , Rewriting rules. 1 In tro duction The algebra of finite or infinite trees plays a fundamental role in computer science: it is a mo del for data structures, program sc hemes and program executions. As early as 1930, J. Herbrand (Herbrand 1930) ga ve an informal description of an algorithm for unifying finite terms, that is solving equations in finite trees. A. Robinson (Robinson 2 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth 1965) redisco vered a similar algorithm when he in troduced the resolution procedure for first-order logic in 1965. Some algorithms with b etter complexities ha ve b een prop osed after by M.S. Paterson and M.N.W egman (P aterson and W egman 1978) and A. Martelli and U. Montanari (Martelli and Montanari 1982). A go o d syn- thesis on this field can b e found in the pap er of J.P . Jouannaud and C. Kirchner (Jouannaud and Kirc hner 1991). Solving conjunctions of equations on infinite trees has b een studied by G. Huet (Huet 1976), b y A. Colmerauer (Colmerauer 1982) and by J. Jaffar (Jaffar 1984). Solving conjunctions of equations and disequations on finite or infinite trees has b een studied by H.J. Burc kert (Burkert 1988) and A. Colmerauer (Colmerauer 1984). An incremental algorithm for solving conjunc- tions of equations and disequations on rational trees has then b een proposed by V.Ramac handran and P . V an Hentenryc k (Ramachandran and V an Hentenryc k 1993) and a quasi-linear incremental algorithm for testing en tailment and disen tail- men t ov er rational trees has b een given by A. Podelski and P . V an Roy (Podelski and V an Roy 1994). On the other hand, K.L. Clark has prop osed a complete axiomatization of the equalit y theory , also called Clark equational theory CET, and ga ve in tuitions about a complete axiomatization of the theory of finite trees (Clark 1978). B. Cour- celle has studied the prop erties of infinite trees in the scop e of recursive program sc hemes (Courcelle 1983; Courcelle 1986) and A. Colmerauer has describ ed the execution of Prolog I I, I I I and IV programs in terms of solving equations and dis- equations in the algebra of finite or infinite trees (Colmerauer 1984; Colmerauer 1990; Benhamou et al. 1996). Concerning quantified constraints, solving universally quantified disequations on finite trees has been studied by D.A. Smith (Smith 1991) and there exist some deci- sion pro cedures whic h transform an y first-order formula in to a Bo olean com bination of quantified conjunctions of atomic formulas using elimination of quantifiers. In the case of finite trees we can refer to A. Malcev (Malcev 1971), K. Kunen (Kunen 1987) and H. Comon (Comon 1988; Comon 1991b; Comon and Lescanne 1989). F or infinite trees, w e can refer to the w ork of H. Comon (Comon 1988; Comon 1991a) and M. Maher (Maher 1988). M. Maher has axiomatized all the cases by complete first-order theories (Maher 1988). In particular, he has introduced the theory T of finite or infinite trees built on an infinite set F of function symbols and sho wed its completeness using a decision pro cedure which transforms any first-order formula ϕ into a Bo olean combination φ of quantified conjunctions of atomic form ulas. If ϕ do es not contain free v ariables then φ is either the form ula true or false . K. Djelloul has then presen ted in (Djelloul 2006a) the class of decomp osable theories and prov ed that the theory of finite or infinite trees is decomp osable. He has also giv en a decision pro cedure in the form of five rewriting rules which, for an y decomp osable theory , transforms any first-order form ula ϕ in to an equiv alen t conjunction φ of solved formulas easily transformable in to a Bo olean com bination of existentially quantified conjunctions of atomic form ulas. In particular, if ϕ has no free v ariables then φ is either the formula true or ¬ true . Unfortunately , all the preceding decision pro cedures are not able to solv e complex The ory and Pr actic e of L o gic Pr o gr amming 3 first-order constraint satisfaction problems in T . In fact, these algorithms are only basic decision pro cedures and not full first-order constraint solvers: they do not w arrant that the solutions of the free v ariables of a solved formula are expressed in a clear and explicit wa y and can even pro duce, starting from a formula ϕ which con tains free v ariables, an equiv alen t solved form ula φ having free v ariables but b eing alwa ys false or alwa ys true in T . The appropriate solved formula of ϕ in this case should b e the formula false or the formula true instead of φ . If we use for example the decision pro cedure of (Djelloul 2006a) to solve the following formula ϕ ¬ ( ∃ y x = f ( y ) ∧ ¬ ( ∃ z w x = f ( z ) ∧ w = f ( w ))) , then w e get the following solved 1 form ula φ ¬ ( ∃ y x = f ( y ) ∧ ¬ ( ∃ z x = f ( z ))) . The problem is that this formula contains free v ariables but is alwa ys true in the theory of finite or infinite trees. In fact, it is equiv alen t to ¬ ( ∃ y x = f ( y ) ∧ ¬ ( ∃ z x = f ( y ) ∧ x = f ( z ))) , i.e. to ¬ ( ∃ y x = f ( y ) ∧ ¬ ( x = f ( y ) ∧ ( ∃ z z = y ))) , th us to ¬ ( ∃ y x = f ( y ) ∧ ¬ ( x = f ( y ))) , whic h is finally equiv alent to true . As a consequence, the solv ed form ula of ϕ should b e true instead of φ . This is a go o d example which shows the limits of the decision pro cedures in solving first-order constrain ts having at least one free v ariable. Muc h more elab orated algorithms are then needed, sp ecially when w e w ant to induce solved formulas expressing solutions of complex first-order constraint satis- faction problems in the theory of finite or infinite. Of course, our goal in these kinds of problems is not only to know if there exist solutions or not, but to express these solutions in the form of a solv ed first-order formula φ which is either the formula true (i.e. the problem is alwa ys satisfiable) or the formula false (i.e. the problem is alw ays unsatisfiable) or a simple formula which is neither equiv alen t to true nor to false and where the solutions of the free v ariables are expressed in a clear and explicit wa y . Algorithms which are able to pro duce such a formula φ are called first-or der c onstr aint solvers . W e hav e then presen ted in (Djelloul and Dao 2006b), not only a decision pro ce- dure, but a full first-order constrain t solv er in the theory T of finite or infinite trees, in the form of 11 rewriting rules, whic h gives clear and explicit solutions for any first-order constrain t satisfaction problem in T . The in tuitions b ehind this algo- rithm come from the works of T. Dao in (Dao 2000) where many elegant properties of the theory of finite or infinite trees w ere given. As far as w e know, this is the first algorithm whic h is able to do a such work in T . This is an extended and detailed version with full pro ofs of our previous work on 1 φ is solved according to Definition 4.2.4 of (Djelloul 2006a) 4 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth the theory T of finite or infinite trees (Djelloul and Dao 2006b). Moreo ver, in this pap er we extend the signature of T by the relation finite ( t ) which forces the term t to b e a finite tree. Then we extend Maher’s axiomatization by tw o new axioms and show its completeness by giving an extended version of our previous first-order constrain t solver (Djelloul and Dao 2006b). W e also describ e a CHR (Constrain t Handling Rules) implementation of our rules and compare the p erformances with those obtained using a C++ implementation of our solver and the decision pro ce- dure for decomp osable theories (Djelloul 2006a). Overview of the p ap er This pap er is organized in five sections follow ed by a conclusion. This introduction is the first section. In section 2, we introduce the structure of finite or infinite trees and give formal definitions of trees, finites trees, infinite trees and rational trees. W e end this section by presen ting particular algebras which handle finite or infinite trees. In section 3, after a brief recall on first-order logic, w e presen t the five axioms of our extended theory 2 T of finite or infinite trees built on a signature containing not only an infinite set of function symbols, but also a relation finite ( t ) whic h enables to distinguish b etw een finite or infinite trees. W e then extend the algebras given at the end of section 2 by the relation finite ( t ) and show that these extended algebras are mo dels of T . In particular, w e show that the mo dels of sets of no des, of finite or infinite trees and of rational trees are mo dels of T . In section 4, we presen t structured formulas that we call working formulas and giv e some of their prop erties. These working formulas are extensions of those giv en in (Djelloul 2006a). W e also introduce the notion of reachable v ariables and show that there exist particular formulas which hav e only quantified reachable v ariables, do not accept elimination of quantifiers and cannot b e simplified any further. Such form ulas are called gener al solve d formulas . W e then present 16 rewriting rules whic h handle w orking formulas and transform an initial working formula into an equiv alen t conjunction of final working form ulas from which w e can extract easily an equiv alent conjunction of general solved form ulas. W e end this section by a full first-order constraint solver in T . This algorithm uses, among other things, our 16 rules and transforms an y first-order formula ϕ into a disjunction φ of simple form ulas such that φ is either the formula true or the formula false or a formula ha ving at least one free v ariable, b eing equiv alen t neither to true nor to false and where the solutions of the free v ariables are expressed in a clear and explicit w ay . The correctness of our algorithm implies the completeness of T . Finally , in section 5, we giv e a series of b enc hmarks. Our algorithm was imple- men ted in C++ and CHR (F ruehwirth 1998; F ruehwirth and Ab dennadher 2003; Sc hrijvers and F ruehwirth 2006). The C++ implementation is able to solve for- m ulas of a t wo pla yer game inv olving 80 nested alternated quantifiers. Even if the 2 W e hav e chosen to denote by T the Maher’s theory of finite or infinite trees and by T our extended theory of finite or infinite trees. The ory and Pr actic e of L o gic Pr o gr amming 5 C++ implementation is fastest, we found interesting to see ho w w e can translate our algorithm into CHR rules. Using this high-level approach, we will b e able to quic kly prototype optimizations and v ariations of our algorithm and hop e to par- allelize it. W e also compare the p erformances with those of C++ implementation of the decision pro cedure for decomp osable theories 3 (Djelloul 2006a). The axiomatization of T , the pro of that T has at least one mo del, the 16 rewriting rules, the pro of of the correctness of our rules, the first-order constraint solver in T , the completeness of T , the CHR implemen tation, the tw o pla yer game and the b enc hmarks are new contributions in this pap er. 2 The structure of finite or infinite trees 2.1 What is a tr e e? T rees are w ell known ob jects in the computer science w orld. Here are some of them: their nodes are labeled by the sym b ols a,b,f,s of respective arities 0,0,2,1. While the first tree is a finite tr e e , i.e. it has a finite set of no des, the tw o others are infinite tr e es , i.e. they hav e an infinite set of no des. Let us now num b er from 1 to n and from left to right the branches that connect eac h no de l to his n sons. W e get: Eac h no de c lab eled by l can now b e seen as a pair ( p, l ) where p is the p osition of the no de, i.e. the smallest series of p ositive integers that we meet if w e mov e from 3 In (Djelloul 2006a), we ha ve shown that the Maher’s theory T of finite or infinite trees is decomposable. W e can sho w easily using a similar pro of that our extended theory T is also decomposable. 6 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth the ro ot of the tree to the no de c . Thus, the preceding trees can b e represented b y the follo wing sets of no des: { ( ε, f ) , (1 , f ) , (2 , s ) , (11 , a ) , (12 , b ) , (21 , a ) } { ( ε, f ) , (1 , a ) , (2 , f ) , (21 , b ) , (22 , f ) , (221 , a ) , (222 , f ) , (2221 , b ) , ... } ( ε, f ) , (1 , a ) , (2 , f ) , (21 , s ) , (22 , f ) , (211 , a ) , (221 , s ) , (222 , f ) , (2211 , s ) , (2221 , s ) , (2222 , f ) , (22111 , a ) , (22211 , s ) , (222111 , s ) , (2221111 , a ) , ... Let us now formalize all the preceding statements. Let L b e a (p ossibly infinite) set. Its elements are called lab els . T o each lab el l ∈ L is link ed a non-negative integer called arity of l . An n -ary lab el is a lab el of arity n . A p osition is a w ord built on strictly p ositive integers (the empty word is denoted b y ε ). Let p b e a p osition and l a lab el. The pair ( p, l ) is called no de and its depth is the length 4 of p . An n -ary no de is a no de whose lab el is of arity n . A r o ot is a no de of depth 0. The r ow of an n -ary node, with n 6 = 0, is the last in teger of its p osition. W e say that c is the father of c 0 or c 0 is the son of c if c and c 0 are no des whose p ositions are resp ectively of the form i 1 ...i k and i 1 ...i k i k +1 , where the i j ’s are strictly p ositive in tegers and k a (p ossibly null 5 ) p ositive integer. Let us denote by N the set of the no des lab eled b y elements of L . Definition 2.1.1 A no de c of N is called arb or esc ent in a sub-set N 1 of N if N 1 6 = ∅ and either c 6∈ N 1 , or c ∈ N 1 and the t wo following conditions hold: • N 1 − { c } do es not contain any no de whose p osition is the same than those of c , • c is either a ro ot or the son of an n -ary no de of N 1 whic h has exactly n sons in N 1 of resp ectiv e rows 1 , ..., n . W e can now define formally a tr e e : Definition 2.1.2 A tr e e tr is a sub-set of N such that each element of N is arb orescent in tr . A finite tree is a tree whose set of no des is finite. An infinite tree is a tree whose set of no des is infinite. Let us no w define the notion of subtree: Definition 2.1.3 Let tr b e a tree. The subtree linked to a no de ( i 1 ...i k , l ) of tr is the set of the no des of the form ( i k +1 ...i k + n , l 0 ) with ( i 1 ...i k + n , l 0 ) ∈ tr and 6 n ≥ 0. W e call subtr e e of tr a subtree linked to one of the no des of tr . A subtree of tr of depth k is a subtree link ed to a no de of tr of depth k . F rom Definition 2.1.2, we deduce that each subtree of a tree tr is also a tree. 4 As usual, the length of the empty word ε is 0. 5 Of course, for k = 0 , i 1 ...i k is reduced to ε . 6 Of course, for n = 0 , ( i k +1 ...i k + n , l 0 ) is reduced to ( ε, l 0 ). The ory and Pr actic e of L o gic Pr o gr amming 7 Definition 2.1.4 A rational tree is a tree whose set of subtrees is a finite set. Note that an infinite tree can b e rational. In fact, ev en if its set of no des is infinite but n subtrees link ed to n differen t no des can b e similar. Let us see this in the follo wing example: Example 2.1.5 Let us consider the three trees presented in the b eginning of Section 2.1. Let us name them from left to righ by: tr 1 , tr 2 and tr 3 . The set of the subtrees of tr 1 is the follo wing finite set: { ( ε, a ) } , { ( ε, b ) } , { ( ε, s ) , (1 , a ) } , { ( ε, f ) , (1 , a ) , (2 , b ) } , { ( ε, f ) , (1 , f ) , (2 , s ) , (11 , a ) , (12 , b ) , (21 , a ) } i.e. The set of the subtrees of tr 2 is the follo wing finite set: { ( ε, a ) } , { ( ε, b ) } , { ( ε, f ) , (1 , a ) , (2 , f ) , (21 , b ) , (22 , f ) , (221 , a ) , ... } , { ( ε, f ) , (1 , b ) , (2 , f ) , (21 , a ) , (22 , f ) , (221 , b ) , ... } i.e. The set of the subtrees of tr 3 is the follo wing infinite set: { ( ε, a ) } , { ( ε, s ) , (1 , a ) } , { ( ε, s ) , (1 , s ) , (11 , a ) } , { ( ε, s ) , (1 , s ) , (11 , s ) , (111 , a ) } , ... { ( ε, f ) , (1 , a ) , (2 , f ) , (21 , s ) , (22 , f ) , ... } , { ( ε, f ) , (1 , s ) , (2 , f ) , (11 , a ) , (21 , s ) , (22 , f ) , ... } ... 8 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth i.e. Note that the tree tr 1 has a finite set of no des and a finite set of subtrees. Thus, it is a finite rational tree. The tree tr 2 has an infinite set of nodes but a finite set of subtrees. Th us, it is an infinite rational tree. The tree tr 3 has an infinite set of no des and an infinite set of subtrees. Thus, it is an infinite non-rational tree. Note also that a rational tree can alw ays be represen ted b y a finite dir e cte d gr aph . F or that, it is enough to merge all the nodes whose linked subtrees are similar. A non-rational tree cannot be represen ted by a finite directed graph. In this case, only an infinite dir e cte d gr aph representation will b e p ossible. F or example, the trees tr 1 , tr 2 and tr 3 can b e represen ted as follows: Of c ourse, tw o different directed graphs can represent the same tree. F or example the trees tr 2 and tr 3 can also b e represen ted as follows: The ory and Pr actic e of L o gic Pr o gr amming 9 2.2 Construction op er ations W e would like to pro vide the set T r of finite or infinite trees with a set of c onstruc- tion op er ations ; one for each lab el l of L . These op erations will b e schematized as follo ws: with n the arity of the label l . In order to formally define these construction op erations, we need first to define them in the set D of sets of no des 7 of N . Let i b e a strictly p ositive integer. If d = ( j 1 ...j k , l ) is a no de then we denote by i.d the no de ( ij 1 ...j k , l ). If a is a set of no des (i.e. a ∈ D ), then we denote by i.a the set of no des { i.d | d ∈ a } . Definition 2.2.1 In the set D , the construction op eration link ed to the n -ary label l is the application l D : ( a 1 , ..., a n ) 7→ { ( ε, l ) } ∪ 1 .a 1 ∪ ... ∪ n.a n with a 1 ...a n elemen ts of D . R emark 2.2.2 Let a b e an element of D . Let us denote by ν k ( a ) the set of no des of a of depth k . Man y remarks must b e stated concerning any elements a , a i and b of D : 1. a = b ↔ V ∞ k =1 ν k ( a ) = ν k ( b ). 2. ν 0 ( l D ( a 1 , ..., a n )) = { ( ε, l ) } . 3. F or all k ≥ 0, there exists a function ϕ k +1 whic h is indep endent from all the ν k +1 ( a i ), with i ∈ { 1 , ..., n } , such that ν k +1 ( l D ( a 1 , ..., a n )) = ϕ k +1 ( ν k ( a 1 ) , ..., ν k ( a n )). 4. The elements of ν 0 ( l D ( a 1 , ..., a n )) are arb orescen t in l D ( a 1 , ..., a n ). 5. F or all k ≥ 0, the elements of ν k +1 ( l D ( a 1 , ..., a n )) are arb orescent in l D ( a 1 , ..., a n ) if and only if, for each i ∈ { 1 , ..., n } , the elements of ν k ( a i ) are arb orescen t in a i . 6. If for all k ≥ 0 the elements of ν k ( l D ( a 1 , ..., a n )) are arborescent in l D ( a 1 , ..., a n ) then eac h element of N is arb orescent in l D ( a 1 , ..., a n ). Let no w F b e an infinite set of function symbols. Let us denote by: • N the set of the no des lab eled by F , • D the set of sets of no des of N , • T r the set of the elements of D which are trees, • Ra the set of the elements of T r whic h are rational, • F i the set of the elements of T r whic h are finite. 7 In other words, each element of D is a set of no des, i.e. a subset of N . 10 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth If f is an n -ary function sym b ol tak en from F then the op eration of construction f D asso ciated to f is an application of the form D n → D . Let tr 1 , ..., tr n b e elemen ts of T r . F rom the fourth and fifth p oint of Remark 2.2.2 we deduce that f D ( tr 1 , ..., tr n ) is also a tree, i.e. an element of T r . Th us, we can introduce the follo wing application: f T r : ( tr 1 , ..., tr n ) 7→ f D ( tr 1 , ..., tr n ) whic h is of type T r n → T r . On the other hand, the set of the subtrees of the tree f D ( tr 1 , ..., tr n ) is obtained b y the union of the sets of the subtrees of all the tr i plus the tree f D ( tr 1 , ..., tr n ). Th us, if all the tr i ’s are rational trees then the tree f D ( tr 1 , ..., tr n ) is rational. As a consequence, w e can introduce the following application: f Ra : ( tr 1 , ..., tr n ) 7→ f D ( tr 1 , ..., tr n ) whic h is of type R a n → Ra . Finally , if all the tr i ’s are finite trees, then the tree f D ( tr 1 , ..., tr n ) is finite. Thus, w e can introduce the following application: f F i : ( tr 1 , ..., tr n ) 7→ f D ( tr 1 , ..., tr n ) whic h is of type F i n → F i . The pairs < D, ( f D ) f ∈ F > , < T r, ( f T r ) f ∈ F > , < F i, ( f F i ) f ∈ F > and < Ra, ( f Ra ) f ∈ F > are kno wn as the algebr as of sets of no des, of finite or infinite trees, of finite trees and of rational trees. 3 The extended theory T of finite or infinite trees 3.1 F ormal pr eliminaries 3.1.1 F ormulas W e are given once and for all an infinite countable set V of variables and the set L of lo gic al sym b ols: = , true , false , ¬ , ∧ , ∨ , → , ↔ , ∀ , ∃ , ( , ) . W e are also giv en once and for all a signatur e S , i.e. a set of symbols partitioned in to tw o subsets: the set of function symbols and the set of r elation sym b ols. T o eac h elemen t s of S is linked a non-negative in teger called arity of s . An n -ary sym b ol is a symbol of arity n . A 0-ary function symbol is called c onstant . As usual, an expr ession is a word on L ∪ S ∪ V whic h is either a term , i.e. of one of the t wo forms: x, f ( t 1 , . . . , t n ) , (1) or a formula , i.e. of one of the elev en forms: s = t, r ( t 1 , . . . , t n ) , true , false , ¬ ϕ, ( ϕ ∧ ψ ) , ( ϕ ∨ ψ ) , ( ϕ → ψ ) , ( ϕ ↔ ψ ) , ( ∀ x ϕ ) , ( ∃ x ϕ ) . (2) In (1), x is taken from V , f is an n -ary function symbol taken from S and the t i ’s are shorter terms. In (2), s, t and the t i ’s are terms, r is an n -ary relation symbol tak en from S and ϕ and ψ are shorter formulas. The set of the expressions forms a first-or der language with e quality . The ory and Pr actic e of L o gic Pr o gr amming 11 The formulas of the first line of (2) are known as atomic , and flat if they are of one of the follo wing forms: true , false , x 0 = x 1 , x 0 = f ( x 1 , ..., x n ) , r ( x 1 , ..., x n ) , where all the x i ’s are (p ossibly non-distinct) v ariables taken from V , f is an n -ary function symbol taken from S and r is an n -ary relation symbol taken from S . An e quation is a formula of the form s = t with s and t terms. An o ccurrence of a v ariable x in a formula is b ound if it o ccurs in a sub-formula of the form ( ∀ x ϕ ) or ( ∃ x ϕ ). It is fr e e in the contrary case. The fr e e variables of a formula are those which hav e at least one free o ccurrence in this formula. A pr op osition or a sentenc e is a formula without free v ariables. If ϕ is a formula, then w e denote by v ar ( ϕ ) the set of the free v ariables of ϕ . The syntax of the formulas b eing constraining, we allo wed ourselves to use infix notations for the binary sym b ols and to add and remo ve brack ets when there are no am biguities. Moreov er, we do not distinguish tw o formulas which can b e made equal using the follo wing transformations of sub-formulas: ϕ ∧ ϕ = ⇒ ϕ, ϕ ∧ ψ = ⇒ ψ ∧ ϕ, ( ϕ ∧ ψ ) ∧ φ = ⇒ ϕ ∧ ( ψ ∧ φ ) , ϕ ∧ true = ⇒ ϕ, ϕ ∨ false = ⇒ ϕ. If I is the set { i 1 , ..., i n } , we call c onjunction of formulas and write V i ∈ I ϕ i , e ac h form ula of the form ϕ i 1 ∧ ϕ i 2 ∧ ... ∧ ϕ i n ∧ true . In particular, for I = ∅ , the conjunction V i ∈ I ϕ i is reduced to true . 3.1.2 Mo del A mo del is a tuple M = < M , ( f M ) f ∈ F , ( R M ) r ∈ R > , where: • M , the universe or domain of M , is a nonempty set disjoint from S , its elemen ts are called individuals of M ; • F and R are sets of n -ary functions and relations in the set M , subscripted b y the elements of S and such that: — for every n -ary function sym b ol f taken from S , f M is an n -ary op era- tion in M , i.e. an application from M n in M . In particular, when f is a constan t, f M b elongs to M ; — for every n -ary relation symbol r tak en from S , r M is an n -ary relation in M , i.e. a subset of M n . Let M = < M , F , R > b e a mo del. An M -expr ession ϕ is an expression built on the signature S ∪ M instead of S , by considering the elemen ts of M as 0-ary function symbols. If for each free v ariable x of ϕ we replace eac h free o ccurrence of x by a same elemen t m in M , we get an M -expression ϕ 0 called instantiation 8 or valuation of ϕ by individuals of M . 8 W e also say that the v ariable x is instantiated by m in ϕ 0 . 12 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth If ϕ is an M -form ula, we say that ϕ is true in M and we write M | = ϕ, (3) if for any instan tiation ϕ 0 of ϕ by individuals of M the set M has the prop erty expressed by ϕ 0 , when w e interpret the function and relation symbols of ϕ 0 b y the corresp onding functions and relations of M and when we give to the logical sym b ols their usual meaning. R emark 3.1.3 F or ev ery M -formula ϕ without free v ariables, one and only one of the following prop erties holds: M | = ϕ , M | = ¬ ϕ . Let us finish this sub-section by a conv enien t notation. Let ¯ x = x 1 ...x n b e a word on V and let ¯ i = i 1 ...i n b e a word on M or V of the same length as ¯ x . If ϕ ( ¯ x ) and φ are tw o M -form ulas, then w e denote b y ϕ ( ¯ i ), resp ectively φ ¯ x ← ¯ i , the M -formula obtained b y replacing in ϕ ( ¯ x ), resp ectiv ely in φ , each free o ccurrence of x j b y i j . 3.1.4 The ory A the ory is a (p ossibly infinite) set of prop ositions called axioms . W e sa y that the mo del M is a mo del of T , if for each element ϕ of T , M | = ϕ . If ϕ is a formula, w e write T | = ϕ, if for each model M of T , M | = ϕ . W e say that the form ulas ϕ and ψ are e quivalent in T if T | = ϕ ↔ ψ . Definition 3.1.5 A theory T is c omplete if for every prop osition ϕ , one and only one of the following prop erties holds: T | = ϕ , T | = ¬ ϕ . Let φ be a formula and ¯ x = x 1 ...x n b e a w ord on V such that v ar ( φ ) = ¯ x . F rom the preceding definition we deduce that a decision pro cedure is sufficient in the case where we wan t just to show the completeness of a theory T , as it was done in (Djelloul 2006a) for decomp osable theories. In fact, the completeness of T dep ends only on the truth v alues of the prop ositions in T . On the other hand, finding for eac h mo del M of T the instantiation s ¯ i of ¯ x such that M | = φ ¯ x ← ¯ i can b e obtained only using a first-order constrain t solver in T . This kind of problem is generally kno wn as first-or der c onstr aint satisfaction pr oblem . 3.1.6 V e ctorial quantifiers Let M b e a mo del and T a theory . Let ¯ x = x 1 . . . x n and ¯ y = y 1 . . . y n b e tw o words on V of the same length. Let φ , ϕ and ϕ ( ¯ x ) b e M -formulas. W e write ∃ ¯ x ϕ for ∃ x 1 ... ∃ x n ϕ , ∀ ¯ x ϕ for ∀ x 1 ... ∀ x n ϕ , ∃ ? ¯ x ϕ ( ¯ x ) for ∀ ¯ x ∀ ¯ y ϕ ( ¯ x ) ∧ ϕ ( ¯ y ) → V i ∈{ 1 ,...,n } x i = y i , ∃ ! ¯ x ϕ for ( ∃ ¯ x ϕ ) ∧ ( ∃ ? ¯ x ϕ ) . The ory and Pr actic e of L o gic Pr o gr amming 13 The word ¯ x , which can b e the empty word ε , is called ve ctor of variables . Note that the form ulas ∃ ? εϕ and ∃ ! εϕ are resp ectively equiv alen t to true and to ϕ in an y mo del M . Notation 3.1.7 Let Q b e a quantifier tak en from {∀ , ∃ , ∃ ! , ∃ ? } . Let ¯ x b e vector of v ariables taken from V . W e write: Q ¯ x ϕ ∧ φ f or Q ¯ x ( ϕ ∧ φ ) . Example 3.1.8 Let I = { 1 , ..., n } b e a finite set. Let ϕ and φ i with i ∈ I b e formulas. Let ¯ x and ¯ y i with i ∈ I b e vectors of v ariables. W e write: ∃ ¯ x ϕ ∧ ¬ φ 1 for ∃ ¯ x ( ϕ ∧ ¬ φ 1 ), ∀ ¯ x ϕ ∧ φ 1 for ∀ ¯ x ( ϕ ∧ φ 1 ), ∃ ! ¯ x ϕ ∧ V i ∈ I ( ∃ ¯ y i φ i ) for ∃ ! ¯ x ( ϕ ∧ ( ∃ ¯ y 1 φ 1 ) ∧ ... ∧ ( ∃ ¯ y n φ n ) ∧ true ) , ∃ ? ¯ x ϕ ∧ V i ∈ I ¬ ( ∃ ¯ y i φ i ) for ∃ ? ¯ x ( ϕ ∧ ( ¬ ( ∃ ¯ y 1 φ 1 )) ∧ ... ∧ ( ¬ ( ∃ ¯ y n φ n )) ∧ true ) . Notation 3.1.9 If ¯ x is a vector of v ariables then we denote by X the set of the v ariables of ¯ x . Let I b e a (p ossible empty) finite set. The tw o following prop erties hold for any theory T : Pr op erty 3.1.10 If T | = ∃ ? ¯ x ϕ then T | = ( ∃ ¯ x ϕ ∧ ^ i ∈ I ¬ φ i ) ↔ (( ∃ ¯ xϕ ) ∧ ^ i ∈ I ¬ ( ∃ ¯ x ϕ ∧ φ i )) . Pr op erty 3.1.11 If T | = ∃ ! ¯ x ϕ then T | = ( ∃ ¯ x ϕ ∧ ^ i ∈ I ¬ φ i ) ↔ ^ i ∈ I ¬ ( ∃ ¯ x ϕ ∧ φ i ) . F ull pro ofs of these tw o prop erties can b e found in detail in (Djelloul 2006a). 3.2 The axioms of T Let F b e a set of function sym b ols con taining infinitely many non-constan t function sym b ols and at least one constant. Let finite b e an 1-ary relation symbol. The theory T of finite or infinite trees built on the signature S = F ∪ { finite } has as axioms the infinite set of prop ositions of one of the five follo wing forms: ∀ ¯ x ∀ ¯ y ¬ ( f ( ¯ x ) = g ( ¯ y )) [1] ∀ ¯ x ∀ ¯ y f ( ¯ x ) = f ( ¯ y ) → V i x i = y i [2] ∀ ¯ x ∃ ! ¯ z V i z i = t i [ ¯ x ¯ z ] [3] ∀ ¯ x ∀ u ¬ ( u = t [ u, ¯ x ] ∧ finite ( u )) [4] ∀ ¯ x ∀ u ( u = f ( ¯ x ) ∧ finite ( u )) ↔ ( u = f ( ¯ x ) ∧ V i finite ( x i )) [5] 14 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth where f and g are distinct function sym b ols taken from F , ¯ x is a vector of (possibly non-distinct) v ariables x i , ¯ y is a vector of (p ossibly non-distinct) v ariables y i , ¯ z is a vector of distinct v ariables z i , t i [ ¯ x ¯ z ] is a term which b egins with an elemen t of F follo wed b y v ariables taken from ¯ x or ¯ z , and t [ u, ¯ x ] is a term containing at least one o ccurrence of an element of F and the v ariable u and p ossibly other v ariables taken from ¯ x . F or example, we hav e T | = ∀ x 1 x 2 ∀ u ¬ ( u = f 1 ( x 1 , f 2 ( u, x 2 )) ∧ finite ( u )) and T | = ∀ u ¬ ( u = f 1 ( f 2 ( u, f 0 ) , f 0 ) ∧ finite ( u )) where f 1 and f 2 are 2-ary function sym b ols and f 0 a constan t of F . The forms [1],..., [5] are also called schemas of axioms of the theory T . Prop osi- tion [1] called c onflict of symb ols shows that tw o distinct op erations pro duce tw o distinct individuals. Prop osition [2] called explosion sho ws that the same op eration on tw o distinct individuals pro duces tw o distinct individuals. Prop osition [3] called unique solution sho ws that a certain form a conjunction of equations has a unique set of solutions in T . In particular, the form ula ∃ z z = f ( z ) has a unique solution whic h is the infinite tree f ( f ( f ( ... ))). Proposition [4] means that a finite tree cannot b e a strict subtree of itself. W e emphasize strongly that t [ u, ¯ x ] should contain at least one o ccurrence of an element of F and the v ariable u . In Axiom [5], if ¯ x is the empt y vector and f is a constant then we get ∀ u u = f ∧ finite ( u ) ↔ u = f , which means that the prop ert y finite ( f ) is true for each constant f of F . This theory is an extension of the basic theory of finite or infinite trees given b y M. Maher in (Maher 1988) and built on a signature containing an infinite set of function symbols. Maher’s theory is comp osed of the three first axioms of T and its completeness was shown using a decision pro cedure which transforms each prop osition into a Bo olean com bination of existen tially quantified conjunctions of atomic formulas. Note also that b oth Maher’s theory and the theory T do not accept full elimination of quan tifiers, i.e. there exist some quantified formulas whose quan tifiers cannot b e eliminated. F or example, the formula ∃ x y = f ( x ) is neither true nor false in T . It accepts in each mo del of T a set of solutions and another set of non-solutions. As a consequence, we cannot simplify it any further. This non-full elimination of quan tifiers makes the completeness of T not evident. 3.3 The mo dels of T Let us extend the algebras given at the end of section 2.2 by the relation finite . More precisely , if u 1 , u 2 , u 3 and u 4 are resp ectively elements of D , T r , F i and R a then the op erations finite D ( u 1 ), finite T r ( u 2 ), finite F i ( u 3 ) and finite Ra ( u 4 ) are true resp ectiv ely in D , T r , F i and R a , if and only if u 1 , u 2 , u 3 and u 4 ha ve a finite set of no des. Let us no w denote by: • D = < D , ( f D ) f ∈ F , finite D > , the mo del of sets of no des, • T r = < T r, ( f T r ) f ∈ F , finite T r > , the mo del of finite or infinite trees, • R a = < Ra, ( f Ra ) f ∈ F , finite Ra > , the mo del of rational trees, • F i = < F i, ( f F i ) f ∈ F , finite F i > , the mo del of finite trees. W e hav e: The ory and Pr actic e of L o gic Pr o gr amming 15 The or em 3.3.1 The mo dels D , T r and R a are mo dels of the theory T . This theorem is one of the essen tial con tributions giv en in this pap er and sho ws that our theory T is in fact an axiomatization of the structures D , T r and Ra together with an infinite set of construction op erations and the 1-ary relation finite . It also sho ws that T has at least one mo del and thus T | = ¬ ( true ↔ false ). Pr o of, first p art: Let us show first that the mo del D of sets of no des is a model of T . In other words, we must sho w that the following prop erties hold: [1 D ] ( ∀ a 1 , ..., a m ∈ D )( ∀ b 1 , ..., b n ∈ D ) ¬ ( f D ( a 1 , ..., a m ) = g D ( b 1 , ..., b n )) [2 D ] ( ∀ a 1 , ..., a n ∈ D )( ∀ b 1 , ..., b n ∈ D ) ( f D ( a 1 , ..., a n ) = f D ( b 1 , ..., b n ) → V n i =1 a i = b i ) [3 D ] ( ∀ a 1 , ..., a m ∈ D )( ∃ ! b 1 , ..., b n ∈ D ) ( V n i =1 b i = t D i [ b 1 , ..., b n , a 1 , ..., a m ]) [4 D ] ( ∀ a 1 , ..., a m ∈ D )( ∀ u ∈ D ) ¬ ( u = t D [ u, a 1 , ..., a n ] ∧ finite D ( u )) [5 D ] ( ∀ a 1 , ..., a n ∈ D )( ∀ u ∈ D )( u = f D ( a 1 , ..., a n ) ∧ finite D ( u )) ↔ ( u = f D ( a 1 , ..., a n ) ∧ V n i =1 finite D ( a i )) where f and g are distinct function symbols taken from F , t D i [ b 1 , ..., b n , a 1 , ..., a m ] is a term which b egins with an element of F follo wed by v ariables taken from { a 1 , ..., a m , b 1 , ..., b n } , and t D [ u, a 1 , ..., a n ] is a term containing at least one o ccur- rence of an element of F and the v ariable u and p ossibly other v ariables taken from { a 1 , ..., a n } . According to Definition 2.2.1 and the definition of the relation finite D , the prop erties [1 D ], [2 D ], [4 D ] and [5 D ] hold. On the other hand, prop erty [3 D ] is m uch less obvious and deserves to b e prov ed. Let a 1 , ..., a m and b 1 , ..., b n b e elements of D . According to the first p oint of Remark 2.2.2, the D -form ula n ^ i =1 b i = t D i [ b 1 , ..., b n , a 1 , ..., a m ] , (4) is equiv alen t in D to ∞ ^ k =0 n ^ i =1 ν k ( b i ) = ν k ( t D i [ b 1 , ..., b n , a 1 , ..., a m ]) . (5) Let i ∈ { 1 , ..., n } . Let us denote by f i resp ectiv ely [ b 1 , ..., b n , a 1 , ..., a m ] i the function symbol resp ectively the set of the v ariables which o ccur in the term t D i [ b 1 , ..., b n , a 1 , ..., a m ]. According to the second and third point of Remark 2.2.2 w e hav e: • F or each i ∈ { 1 , ..., n } there exists one no de ϕ i 0 = ( ε, f i ), suc h that ν 0 ( t D i [ b 1 , ..., b n , a 1 , ..., a m ]) = { ϕ i 0 } . • F or each i ∈ { 1 , ..., n } and each k ≥ 0 there exists a function ϕ i k +1 , which is indep enden t from all the ν k +1 ( x ), with x ∈ [ b 1 , ..., b n , a 1 , ..., a m ] i , suc h that ν k +1 ( t D i [ b 1 , ..., b n , a 1 , ..., a m ]) = ϕ i k +1 ([ ν k ( b 1 ) , ..., ν k ( b n ) , ν k ( a 1 ) , ..., ν k ( a m )] i ) , where [ ν k ( b 1 ) , ..., ν k ( b n ) , ν k ( a 1 ) , ..., ν k ( a m )] i is a tuple of elements of the form ν k ( x ) for all x ∈ [ b 1 , ..., b n , a 1 , ..., a m ] i . 16 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth Th us, the D -formula (5) is equiv alent in D to ( n ^ i =1 ν 0 ( b i ) = { ϕ i 0 } ) ∧ ( ∞ ^ k =0 n ^ i =1 ν k +1 ( b i ) = ϕ i k +1 ([ ν k ( b 1 ) , ..., ν k ( b n ) , ν k ( a 1 ) , ..., ν k ( a m )] i )) , from whic h we deduce that: • (i) F or all i ∈ { 1 , ..., n } , ν 0 ( b i ) has a constan t v alue, which is equal to ( ε, f i ). • (ii) Eac h ν k +1 ( b i ) depends in the worst case on ν k ( b 1 ) , ..., ν k ( b n ) , ν k ( a 1 ) , ..., ν k ( a m ), i.e. on ν k ( b 1 ) , ..., ν k ( b n ) and a 1 , ..., a m . Th us, by recurrence 9 on k , we deduce that (iii) each ν k +1 ( b i ) with k ≥ 0 and i ∈ { 1 , ..., n } , dep ends only on a 1 , ..., a m . F rom (i) and (iii) we deduce that all the b i ’s depend only on a 1 , ..., a m and th us prop erty [3 D ] holds. In other w ords, for eac h instan tiation of a 1 , ..., a m b y elements of D we can deduce the v alues of ν k ( b i ) for all i ∈ { 1 , ..., n } and k ≥ 0. W e ha ve shown that the mo del D satisfies the five axioms of T and th us it is a mo del of T . Pr o of, se c ond p art: Let us now show that the mo del T r of finite or infinite trees is a mo del of T . F or that, it is enough to sho w the v alidity of the following prop erties [1 T r ] ( ∀ a 1 , ..., a m ∈ T r )( ∀ b 1 , ..., b n ∈ T r ) ¬ ( f T r ( a 1 , ..., a m ) = g T r ( b 1 , ..., b n )) [2 T r ] ( ∀ a 1 , ..., a n ∈ T r )( ∀ b 1 , ..., b n ∈ T r ) ( f T r ( a 1 , ..., a n ) = f T r ( b 1 , ..., b n ) → V n i =1 a i = b i ) [3 T r ] ( ∀ a 1 , ..., a m ∈ T r )( ∃ ! b 1 , ..., b n ∈ T r ) ( V n i =1 b i = t T r i [ b 1 , ..., b n , a 1 , ..., a m ]) [4 T r ] ( ∀ a 1 , ..., a m ∈ T r )( ∀ u ∈ T r ) ¬ ( u = t T r [ u, a 1 , ..., a n ] ∧ finite T r ( u )) [5 T r ] ( ∀ a 1 , ..., a n ∈ T r )( ∀ u ∈ T r )( u = f T r ( a 1 , ..., a n ) ∧ finite T r ( u )) ↔ ( u = f T r ( a 1 , ..., a n ) ∧ V n i =1 finite T r ( a i )) where f and g are distinct function symbols tak en from F , t T r i [ b 1 , ..., b n , a 1 , ..., a m ] is a term which b egins with an element of F follo wed by v ariables taken from { a 1 , ..., a m , b 1 , ..., b n } , and t T r [ u, a 1 , ..., a n ] is a term containing at least one o ccur- rence of an element of F and the v ariable u and p ossibly other v ariables taken from { a 1 , ..., a n } . Since T r is a subset of D , then according to the definition of f T r , f D , finite T r and finite D , the prop erties [1 D ], [2 D ], [4 D ] and [5 D ] imply [1 T r ], [2 T r ], [4 T r ] and [5 T r ]. On the other hand, to sho w prop ert y [3 T r ], it is enough to sho w the following implication: ( ∀ a 1 , ..., a m , b 1 , ...b n ∈ D )((( n ^ i =1 b i = t D i [ b 1 , ..., b n , a 1 , ..., a m ]) ∧ ( m ^ i =1 a i ∈ T r )) → ( n ^ i =1 b i ∈ T r )) (6) Let a , b , a 1 ,..., a m , b 1 ,..., b n b e elemen ts of D . Let us consider the follo wing notation: Ar b ( a, b ) ↔ each element of a is arb orescent in b . 9 If k = 0 then according to (ii) each ν 1 ( b i ) dep ends in the worst case on ν 0 ( b 1 ) , ..., ν 0 ( b n ) and a 1 ,..., a m . According to (i) all the ν 0 ( b 1 ) , ..., ν 0 ( b n ) hav e constant v alues and thus each ν 1 ( b i ) depends only on a 1 , ..., a m . Let us now assume that each ν k ( b i ) dep ends only on a 1 , ..., a m and let us show that this hypothesis is true for ν k +1 ( b i ). According to (ii), each ν k +1 ( b i ) depends in the worst case on ν k ( b 1 ) , ..., ν k ( b n ) and a 1 , ..., a m , which according to our hypothesis dep end only on a 1 , ..., a m . Thus, the recurrence is true for all k ≥ 0. The ory and Pr actic e of L o gic Pr o gr amming 17 According to Definition 2.1.2, the T r -formula ( n ^ i =1 b i = t D i [ b 1 , ..., b n , a 1 , ..., a m ]) ∧ ( m ^ i =1 a i ∈ T r ) , is equiv alen t in T r to ( n ^ i =1 b i = t D i [ b 1 , ..., b n , a 1 , ..., a m ]) ∧ ( m ^ i =1 Ar b ( N , a i )) , whic h is equiv alent to ( n ^ i =1 b i = t D i [ b 1 , ..., b n , a 1 , ..., a m ]) ∧ ( ∞ ^ k =0 m ^ i =1 Ar b ( ν k ( N ) , a i )) , (7) whic h for each j ≥ 0 is equiv alen t in T r to ( n ^ i =1 b i = t D i [ b 1 , ..., b n , a 1 , ..., a m ]) ∧ ( ∞ ^ k =0 m ^ i =1 Ar b ( ν k ( N ) , a i )) ∧ ( n ^ i =1 Ar b ( ν j ( b i ) , b i )) . (8) The equiv alence (7 ↔ 8) holds for j = 0 according to the fourth p oint of Remark 2.2.2, and if we assume that this equiv alence holds for an integer j with j ≥ 0 then according to the fifth p oint of Remark 2.2.2, we deduce that it holds also for j + 1. Th us, since the equiv alence (7 ↔ 8) holds for any j ≥ 0 then according to the sixth p oin t of Remark 2.2.2 and Definition 2.1.2 we deduce that (8) implies n ^ i =1 Ar b ( N , b i ) , whic h, according to Definition 2.1.2, implies n ^ i =1 b i ∈ T r . Th us, the implication (6) holds and T r is a mo del of T . Pr o of, thir d p art: Finally , let us show that the mo del R a is a mo del of T . F or that, it is enough to sho w the v alidity of the following prop erties: [1 Ra ] ( ∀ a 1 , ..., a m ∈ Ra )( ∀ b 1 , ..., b n ∈ Ra ) ¬ ( f Ra ( a 1 , ..., a m ) = g Ra ( b 1 , ..., b n )) [2 Ra ] ( ∀ a 1 , ..., a n ∈ Ra )( ∀ b 1 , ..., b n ∈ Ra ) ( f Ra ( a 1 , ..., a n ) = f Ra ( b 1 , ..., b n ) → V n i =1 a i = b i ) [3 Ra ] ( ∀ a 1 , ..., a m ∈ Ra )( ∃ ! b 1 , ..., b n ∈ Ra ) ( V n i =1 b i = t Ra i [ b 1 , ..., b n , a 1 , ..., a m ]) [4 Ra ] ( ∀ a 1 , ..., a m ∈ Ra )( ∀ u ∈ R a ) ¬ ( u = t Ra [ u, a 1 , ..., a n ] ∧ finite Ra ( u )) [5 Ra ] ( ∀ a 1 , ..., a n ∈ Ra )( ∀ u ∈ R a )( u = f Ra ( a 1 , ..., a n ) ∧ finite Ra ( u )) ↔ ( u = f Ra ( a 1 , ..., a n ) ∧ V n i =1 finite Ra ( a i )) where f and g are distinct function sym b ols taken from F , t Ra i [ b 1 , ..., b n , a 1 , ..., a m ] is a term which b egins with an element of F follo wed by v ariables taken from { a 1 , ..., a m , b 1 , ..., b n } , and t Ra [ u, a 1 , ..., a n ] is a term containing at least one o ccur- rence of an element of F and the v ariable u and p ossibly other v ariables taken from { a 1 , ..., a n } . Since R a is a subset of T r and according to the definitions of f T r , f Ra , finite T r and finite Ra then the prop erties [1 T r ], [2 T r ], [4 T r ] and [5 T r ] imply 18 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth [1 Ra ], [2 Ra ], [4 Ra ] and [5 Ra ]. On the other hand, in prop ert y [3 T r ], (in the preced- ing pro of ), a subtree of depth k of an y b i is either one of the trees b 1 ,... , b n or a subtree of one of the a j ’s with i ∈ { 1 , ..., n } and j ∈ { 1 , ..., m } . This is true for k = 0 and if we assume that it is true for k then we deduce that it is true for k + 1. Th us, if the a j ’s are rational then the b i ’s in [3 T r ] are also rational and thus we get [3 Ra ]. W e hav e shown that the mo dels D , T r and R a are mo dels of T . What ab out the mo del F i of finite trees? Since F contains at least one function symbol f which is not a constan t then according to Axiom [3] of T we hav e T | = ∃ ! x x = f ( x, ..., x ) . It is obvious that this prop erty cannot b e true in F i , i.e. there exists no x ∈ F i suc h that x = f F i ( x, ..., x ). Thus, the mo del F i of finite trees is not a mo del of T . Let us end this section by a prop erty concerning the cardinality of any mo del of T : Pr op erty 3.3.2 Let M = < M , ( f M ) f ∈ F , finite M > b e a mo del of T . The mo del M has an infinit y of individuals i suc h that M | = finite M ( i ). Pr o of Since the set F contains at least one function symbol f which is a constant then according to Axiom [5], with ¯ x = ε , w e hav e M | = finite M ( f M ) . (9) On the other hand, according to the definition of the signature of T , the set F con tains an infinit y of distinct function sym b ols whic h are not constants. Let f 1 one of these sym b ols. According to (9) and Axiom [5] we hav e M | = finite M ( f M 1 ( f M , ..., f M )) , th us the individual f M 1 ( f M , ..., f M ) is finite in M . Since the set F contains an infinit y of distinct function sym b ols f 1 , f 2 , f 3 , ... whic h are not constants then we can create by follo wing the same preceding steps an infinity of finite individuals f M 1 ( f M , ..., f M ) , f M 2 ( f M , ..., f M ) , f M 3 ( f M , ..., f M ) , ... which start by distinct func- tion symbols. According to Axiom [1], all these individuals are distinct. According to (9) and Axiom [5] all these individuals are finite in M . Cor ol lary 3.3.3 Eac h mo del of T has an infinite domain, i.e. an infinite set of individuals. 4 Solving first-order constraints in T 4.1 Discipline of the formulas in T Let us assume that the infinite set V is ordered by a strict linear dense order relation without endp oints denoted by . Starting from this section, we imp ose The ory and Pr actic e of L o gic Pr o gr amming 19 the following discipline to every formula ϕ in T : the quantified v ariables of ϕ are renamed so that: • (i) The quan tified v ariables of ϕ hav e distinct names and different from those of the free v ariables. • (ii) F or all v ariables x , y and all sub-formulas 10 ϕ i of ϕ , if y has a free o ccurrence in ϕ i and x has a b ound o ccurrence in ϕ i then x y . Example 4.1.1 Let x, y , z , v b e v ariables of V such that x y z v . Let ϕ b e the formula ∃ x x = f y ∧ ¬ ( ∃ z z = x ) ∧ ¬ ( ∃ z z = v ) . (10) The quantified v ariables of ϕ hav e no distinct names. Since the order is dense and without endpoints, there exists a v ariable w in V such that x y z v w , and th us ϕ is equiv alent in T to ∃ x x = f y ∧ ¬ ( ∃ z z = x ) ∧ ¬ ( ∃ w w = v ) . In the preceding formula, the v ariables z and w hav e b ound occurrences while the v ariables y and v hav e free o ccurrences. Since x y z v w then z and w must b e renamed. On the other hand, since the order is dense and without endp oints, there exist tw o v ariables u and d in V such that x u d y z v w . Th us, the preceding form ula is equiv alent in T to ∃ x x = f y ∧ ¬ ( ∃ u u = x ) ∧ ¬ ( ∃ d d = v ) . In the sub-formula ( ∃ u u = x ) the v ariable x has a free occurrence while the v ariable u has a b ound occurrence. Since x u then u m ust b e renamed. On the other hand, since the order is dense and without endp oin ts, there exists a v ariable n in V such that n x u d y z v w . Thus, the preceding formula is equiv alent in T to ∃ x x = f y ∧ ¬ ( ∃ n n = x ) ∧ ¬ ( ∃ d d = v ) . (11) This formula satisfies our conditions. Of course, the equiv alence b etw een (11) and (10) holds b ecause in each step we renamed only the quan tified v ariables. It is ob vious that we can alwa ys transform any formula ϕ into an equiv alent formula φ , whic h resp ects the discipline of the formulas in T , only by renaming the quantified v ariables of ϕ . It is enough for that to rename the quan tified v ariables by distinct names and differen t from those of the free v ariables and then c heck eac h sub-form ula and rename the quan tified v ariables if the condition (ii) do es not hold. W e emphasize strongly that all the formulas whic h will b e used starting from no w satisfy the discipline of the formulas in T . 10 By considering that each formula is also a sub-formula of itself. 20 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth 4.2 Basic formula In this sub-section we introduce particular conjunctions of atomic formulas that we call b asic formulas and sho w some of their prop erties. All of them will b e used to sho w the correctness of our rewriting rules given in section 4.6. Definition 4.2.1 Let v 1 , ..., v n , u 1 , ..., u m b e v ariables. A b asic formula is a formula of the form ( n ^ i =1 v i = t i ) ∧ ( m ^ i =1 finite ( u i )) (12) in which all the equations v i = t i are flat. Note that if n = m = 0 then (12) is reduced to true . The basic form ula (12) is called solve d if all the v ariables v 1 , ..., v n , u 1 , ..., u m are distinct and for each equation of the form x = y we hav e x y . If α is a basic formula then we denote by • Lhs ( α ) the set of the v ariables which o ccur in the left hand sides of the equations of α . • F I N I ( α ) the set of the v ariables whic h occur in a sub-formula of α of the form finite ( x ). Note that if α is a solved basic formula then for all v ariables x of α we hav e x ∈ Lhs ( α ) → x 6∈ F I N I ( α ). Example 4.2.2 The basic formula x = x ∧ finite ( y ) is not solved b ecause x 6 x . The basic formula x = f ( y ) ∧ z = f ( y ) ∧ finite ( x ) is also not solved b ecause x is a left hand side of an equation and o ccurs also in finite ( x ). The basic formulas true (empty conjunction) and x = f ( y ) ∧ z = f ( y ) ∧ finite ( y ) are solv ed. According to the axiom [3] of T we deduce the following prop erty: Pr op erty 4.2.3 Let α b e a solved basic formula containing only equations. Let ¯ x b e the vector of the v ariables of Lhs ( α ). W e hav e: T | = ∃ ! ¯ x α . Pr op erty 4.2.4 Let α and β b e tw o solv ed basic formulas con taining only equations. If Lhs ( α ) = Lhs ( β ) and T | = α → β then T | = α ↔ β . Pr o of Let α and β b e tw o solved basic formulas containing only equations suc h that Lhs ( α ) = Lhs ( β ) and T | = α → β . Let us show that we hav e also T | = β → α . Let ¯ x b e the vector of the v ariables of Lhs ( α ) and let ¯ y b e the vector of the v ariables whic h o ccur in α → β and do not o ccur in ¯ x . Since α and β are tw o solved basic form ulas such that Lhs ( α ) = Lhs ( β ) then (i) ¯ x is also the vector of the left hand sides of the equations of β . Moreo ver, the following equiv alences are true in T : The ory and Pr actic e of L o gic Pr o gr amming 21 α → β ↔ ∀ ¯ x ∀ ¯ y α → β ↔ ∀ ¯ y ∀ ¯ x ¬ α ∨ β ↔ ∀ ¯ y ( ¬ ( ∃ ¯ x α ∧ ¬ β )) ↔ ∀ ¯ y ( ¬ ( ¬ ( ∃ ¯ x α ∧ β ))) according to the prop erties 4.2.3 and 3.1.11 ↔ ∀ ¯ y ( ¬ ( ¬ ( ∃ ¯ x β ∧ α ))) ↔ ∀ ¯ y ( ¬ ( ∃ ¯ x β ∧ ¬ α )) according to: (i) and Prop erty 4.2.3 and using the other sense (righ t to left) of the equiv alence of Prop erty 3.1.11 ↔ ∀ ¯ y ∀ ¯ x ¬ β ∨ α ↔ ∀ ¯ y ∀ ¯ x β → α ↔ β → α Pr op erty 4.2.5 Let α b e a basic form ula containing only equations and β and δ tw o conjunctions of constrain ts of the form finite ( x ) such that α ∧ β and α ∧ δ are solved basic form ulas. W e hav e T | = ( α ∧ β ) ↔ ( α ∧ δ ) if and only if β and δ hav e exactly the same con traints. Pr o of If β and δ ha ve the same constraints then it is evident that we hav e T | = ( α ∧ β ) ↔ ( α ∧ δ ). Let us now show that if we hav e T | = ( α ∧ β ) ↔ ( α ∧ δ ) then β and δ ha ve the same constraints. Supp ose that we hav e (*) T | = ( α ∧ β ) ↔ ( α ∧ δ ) and let us show that if finite ( u ) o ccurs in β then it o ccurs also in δ and vice versa. If finite ( u ) o ccurs in β then T | = ( α ∧ β ) → finite ( u ), th us from (*) we hav e (i) T | = ( α ∧ δ ) → finite ( u ). Since α ∧ β is solved then u is not the left hand side of an equation of α . Thus, (ii) the conjunction α ∧ δ do es not con tain sub-formulas of the form u = t [ ¯ x ] ∧ V i finite ( x i ). Since α ∧ δ is solved then δ do es not contain form ulas of the form finite ( v ) where v is the left hand side of an equation of α . Th us, (iii) the conjunction α ∧ δ do es not contain also sub-form ulas of the form v = t [ ¯ x, u ] ∧ finite ( v ). F rom (i), (ii) and (iii), finite ( u ) should o ccur in δ . By the same reasoning (we replace β by δ and vice versa), we show that if finite ( u ) o ccurs in δ then it o ccurs in β . Let us no w introduce the notion of r e achable variable : Definition 4.2.6 Let α b e a basic formula and ¯ x a vector of v ariables. The reachable v ariables and equations of α from the v ariable x 0 are those which o ccur in a sub-formula of α of the form: x 0 = t 0 ( x 1 ) ∧ x 1 = t 1 ( x 2 ) ∧ ... ∧ x n − 1 = t n − 1 ( x n ) , where x i +1 o ccurs in the term t i ( x i +1 ). The reachable v ariables and equations of ∃ ¯ x α are those which are reachable in α from the free v ariables of ∃ ¯ x α . A sub- form ula of α of the form finite ( u ) is called reac hable in ∃ ¯ x α if u 6∈ ¯ x or u is a reac hable v ariable of ∃ ¯ x α . 22 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth Example 4.2.7 In the formula: ∃ uv w z = f ( u, v ) ∧ v = g ( v , u ) ∧ w = f ( u, v ) ∧ finite ( u ) ∧ finite ( x ) , the equations z = f ( u, v ) and v = g ( v , u ), the v ariables z , u and v and the formulas finite ( u ) and finite ( x ) are reachable. On the other hand the equation w = f ( u, v ) and the v ariable w are not reachable. R emark 4.2.8 Let α b e a solved basic formula. Let ¯ x b e a vector of v ariables. W e hav e: • If all the v ariables of ¯ x are reachable in ∃ ¯ x α then all the equations and relations of α are reachable in ∃ ¯ x α . • If v = t [ y ] is a reachable equation in ∃ ¯ x α , then α contains a sub-formula of the form k ^ j =1 v j = t j [ v j +1 ] (13) with k ≥ 1 and (i) v 1 6∈ X , (ii) for all j ∈ { 1 , ..., k } the v ariable v j +1 o ccurs in the term t j [ v j +1 ], (iii) v k is the v ariable v , (iv) v k +1 is the v ariable y and t k [ v k +1 ] is the term t [ y ]. According to the first p oint of Remark 4.2.8 and Definition 4.2.6 we hav e the follo wing prop erty: Pr op erty 4.2.9 Let α b e a solv ed basic formula. If the formula ∃ ¯ x α has no free v ariables and if all the v ariables of ¯ x are reachable in ∃ ¯ x α then ¯ x is the empty vector ε and α is the form ula true . According to the axioms [1] and [2] of T we hav e the following prop erty: Pr op erty 4.2.10 Let α b e a basic formula. If all the v ariables of ¯ x are reachable in ∃ ¯ x α then T | = ∃ ? ¯ x α. Pr op erty 4.2.11 Let ¯ x b e a vector of v ariables and α a solved basic formula. W e hav e: T | = ( ∃ ¯ x α ) ↔ ( ∃ ¯ x 0 α 0 ) , where: • ¯ x 0 is the v ector of the v ariable of ¯ x which are reachable in ∃ ¯ x α , • α 0 is the conjunction of the equations and the formulas of the form finite ( x ) whic h are reachable in ∃ ¯ x α . The ory and Pr actic e of L o gic Pr o gr amming 23 Pr o of Let us decomp ose ¯ x into three v ectors ¯ x 0 , ¯ x 00 and ¯ x 000 suc h that: • ¯ x 0 is the v ector of the v ariables of ¯ x which are reachable in ∃ ¯ x α . • ¯ x 00 is the v ector of the v ariables of ¯ x which are non-reachable in ∃ ¯ x α and do not o ccur in the left hand sides of the equations of α . • ¯ x 000 is the vector of the v ariables of ¯ x which are non-reachable in ∃ ¯ x α and o ccur in a left hand side of an equation of α . Let us no w decomp ose α into three formulas α 0 , α 00 and α 000 suc h that: • α 0 is the conjunction of the equations and the formulas of the form finite ( x ) which are reac hable in ∃ ¯ x α . • α 00 is the conjunction of the formulas of the form finite ( x ) which are non-reachable in ∃ ¯ x α . • α 000 is the conjunction of the equations whic h are non-reachable in ∃ ¯ x α . According to Definition 4.2.6, all the v ariables of ¯ x 00 and ¯ x 000 do not o ccur in α 0 (otherwise they will b e reachable) and since α is solved then ¯ x 000 is the vector of the left hand sides of the equations of α 000 and its v ariables do not o ccur in α 00 . Thus the form ula ∃ ¯ x α is equiv alen t in T to ( ∃ ¯ x 0 α 0 ∧ ( ∃ ¯ x 00 α 00 ∧ ( ∃ ¯ x 000 α 000 ))) . According to Prop erty 4.2.3 we hav e T | = ∃ ! ¯ x 000 α 000 . According to Corollary 3.3.3 w e hav e T | = ∃ ¯ x 00 α 00 . Thus, the preceding form ula is equiv alent in T to ( ∃ ¯ x 0 α 0 ). Example 4.2.12 The formula ∃ xy z w v = f ( x, x ) ∧ w = g ( y , z , x ) ∧ finite ( x ) ∧ finite ( y ) is equiv alen t in T to ∃ x v = f ( x, x ) ∧ finite ( x ) ∧ ( ∃ y z finite ( y ) ∧ ( ∃ w w = g ( y , z , x ))) , whic h, since T | = ∃ ! w w = g ( y , z , x ) and T | = ∃ y z finite ( y ), is equiv alen t in T to ∃ x v = f ( x, x ) ∧ finite ( x ) . Prop ert y 4.2.11 confirms the fact that the theory T do es not accept full elimi- nation of quantifiers and shows that we can eliminate only non-reachable quan- tified v ariables. On the other hand, reac hable v ariables cannot b e remo ved since their v alues dep end on the instantiations of the free v ariables. In fact, the formula ∃ x v = f ( x, x ) ∧ finite ( x ) is neither true nor false in T since for each mo del M of T there exist instan tiations of the free v ariable v which make it false in M and others which make it true in M , and thus the reachable quantified v ariable x can- not b e eliminated and the formula ∃ x v = f ( x, x ) ∧ finite ( x ) cannot b e simplified an ymore. On the other hand, the formula ∃ w w = g ( y , z , x ) is true in any mo del of T and for any instantiation of z . The quantified non-reachable v ariable w can then b e eliminated and the formula is replaced by true . As we will see in section 4.6, reac hability , has a crucial role while solving first-order constraints in T . It sho ws 24 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth whic h quantifications can b e eliminated and enables to simplify complex quantified basic form ulas. According to the axioms [1] and [2] and since the set F is infinite w e hav e the follo wing prop erty: Pr op erty 4.2.13 Let I = { 1 , ..., n } b e a finite (p ossibly empty) set and ¯ x and ¯ x 0 t wo disjoint v ectors of v ariables. Let ¯ y 1 ,..., ¯ y n b e vectors of v ariables and α 1 ,..., α n solv ed basic formulas suc h that for all i ∈ I all the v ariables of ¯ y i are reachable in ∃ ¯ y i α i . If eac h conjunc- tion α i con tains at least (1) one sub-form ula of the form finite ( x ) with x ∈ X , or (2) one equation whic h con tains at least one o ccurrence of a v ariable x ∈ X ∪ X 0 , then: T | = ∃ ¯ x ¯ x 0 ( ^ x ∈ X 0 finite ( x )) ∧ ( ^ i ∈ I ¬ ( ∃ ¯ y i α i )) . (14) Pr o of Let M = < M , ( f M ) f ∈ F , finite M > b e a mo del of T . T o show the v alidit y of (14) it is enough to sho w that: M | = ∃ ¯ x ¯ x 0 ( ^ x ∈ X 0 finite M ( x )) ∧ ( ^ i ∈ I ¬ ( ∃ ¯ y i α i )) . (15) Since the basic formulas α i are solved, they do not contain equations of the form x = x . Supp ose now that one of the α i con tains one equation of the form x = v with x ∈ X ∪ X 0 and v ∈ Y i . Since α i is solved then x v but according to the discipline of the form ulas in T we hav e v x 11 . Since the order is strict then x = v cannot b e a sub-formula of α i . Th us, according to the conditions of Prop ert y 4.2.13, each conjunction α i con tains at least (1) one sub-formula of the form finite ( x ) with x ∈ X , or (2) one equation of the one of the following forms: • (*) x = f ( v 1 , ...v n ) with x ∈ X ∪ X 0 , • (**) x = v with x and v t wo distinct v ariables suc h that x ∈ X ∪ X 0 and v 6∈ Y , • (***) v = t [ x ] where x is a v ariable of X ∪ X 0 whic h o ccurs in the term t [ x ]. According to the first p oin t of Remark 4.2.8 and since for all i ∈ { 1 , ..., n } the v ariables of ¯ y i are reachable in ∃ ¯ y i α i , then the equation v = t [ x ] is reac hable in ∃ ¯ y i α i and thus according to the second p oint of Remark 4.2.8 the conjunction α i con tains a sub-formula of the form ( V k j =1 v j = t j [ v j +1 ]) with v 1 6∈ Y i , for all j ∈ { 1 , ..., k } the v ariable v j +1 o ccurs in the term t j [ v j +1 ] and v k +1 is the v ariable x . But, since the case v 1 ∈ X ∪ X 0 is already treated in (*) and (**), then we can restrict ourself without lo osing generality to the case where v 1 6∈ Y i ∪ X ∪ X 0 , i.e. v 1 is free in (15). 11 In fact, the v ariable x has a free o ccurrence in ∃ ¯ y i α i and the variable v has a b ound o ccurrence in ∃ ¯ y i α i (because v is a quantified reachable v ariable in ∃ ¯ y i α i ) and thus according to the discipline of our formulas we hav e v x . The ory and Pr actic e of L o gic Pr o gr amming 25 Let ∃ ¯ x ¯ x 0 ( ^ x ∈ X 0 finite M ( x )) ∧ ( ^ i ∈ I ¬ ( ∃ ¯ y i α ∗ i )) (16) b e an any instan tiation of ∃ ¯ x ¯ x 0 ( V x ∈ X 0 finite M ( x )) ∧ ( V i ∈ I ¬ ( ∃ ¯ y i α i )) by individuals of M . Let us show that there exists an instantiation for the v ariables of X and X 0 whic h satisfies the preceding formula. F or that, let us c hose an instan tiation which resp ects the follo wing conditions: • (i) F or each x ∈ X 0 , the instan tiation x ∗ of x satisfies M | = finite M ( x ∗ ). • (ii) If a conjunction α ∗ i con tains a sub-formula of the form finite M ( x ) with x ∈ X then the instantiation x ∗ of x satisfies M | = x ∗ = f M ( x ∗ , ..., x ∗ ) with f an n -ary function symbol of strictly p ositive arity whic h do es not o ccur in any α i with i ∈ I . • (iii) If a conjunction α ∗ i con tains a sub-formula of the form x = f M ( v 1 , ...v n ) with x ∈ X ∪ X 0 , then the instantiation of x starts with a different function symbol than f . • (iv) If a conjunction α ∗ i con tains a sub-formula of the form x = v with x and v t wo distinct v ariables such that x ∈ X ∪ X 0 and v 6∈ Y , then the instantiation of x is differen t from those of v . • (v) If a conjunction α ∗ i con tains a sub-form ula of the form ( V k j =1 v j = t j [ v j +1 ]) with v 1 6∈ ( X ∪ X 0 ∪ Y ), for all j ∈ { 1 , ..., k } the v ariable v j +1 o ccurs in the term t j [ v j +1 ], and v k +1 ∈ X ∪ X 0 , then the instan tiation of v k +1 is different from v ∗ , where v ∗ is the instantiation of v k +1 obtained from those of v 1 in 12 (16) so that M | = V k j =1 v j = t j [ v j +1 ]. A such instan tiation of the v ariables of X and X 0 is alwa ys p ossible since : (1) there exists an infinit y of function sym b ols in F which are not constants (2) the set of the individuals i of M suc h that M | = finite M ( i ) is infinite (see Prop erty 3.3.2). As a consequence, according to axioms [1] and [4], this instantiation implies a conflict inside eac h sub-instantiated-form ula ∃ ¯ y i α ∗ i , with i ∈ { 1 , ..., n } and thus M | = ∃ ¯ x ¯ x 0 ( ^ i ∈ I ¬ ( ∃ ¯ y i α ∗ i )) . Since this instantiation satisfies the first condition (i) of the preceding list of con- ditions then (16) holds and th us (15) holds. W e emphasize strongly that this prop erty holds only if the formula (14) satisfies the discipline of the formulas in T . This prop erty is vital for solving first-order constrain t o ver finite or infinite trees. In fact, since the v ariables of each ¯ y i with i ∈ { 1 , ..., n } are reac hable in ∃ ¯ y i α i then we cannot eliminate or remov e the quan- tification ∃ ¯ y i form ∃ ¯ y i α i , and thus solving a constraint containing such formulas is not eviden t. Prop erty 4.2.13 enables us to surmount this problem by reducing to true particular formulas containing sub-form ulas which do es not accept full elimi- nation of quan tifiers. 12 Recall that v 1 6∈ ( X ∪ X 0 ∪ Y ) and thus v 1 is a free v ariable in (15). As a consequence, it is already instantiated in (16). 26 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth Example 4.2.14 Let x , y , z and v b e v ariables suc h that y x z w . Let us consider the follo wing form ula ϕ : ∃ x ¬ ( ∃ y z = f ( y ) ∧ y = g ( x )) ∧ ¬ ( ∃ ε x = w ) ∧ ¬ ( ∃ ε x = g ( x )) . (17) This form ula satisfies the discipline of the formulas in T . Let M = < M , ( f M ) f ∈ F , finite M > b e a mo del of T . Note that we cannot eliminate the quanti- fier ∃ y in the sub-form ula ∃ y z = f ( y ) ∧ y = g ( x ). In fact, this sub-form ula is neither true nor false in T b ecause there exist instan tiations of the free v ariable z in M whic h satisfy this sub-form ula in M and others which do not satisfy it. On the other hand, Prop erty 4.2.13, states that formula (17) is true in T for all instantiations of z even if the sub-form ula ∃ y z = f ( y ) ∧ y = g ( x ) is neither true nor false in T . Let us chec k this strange result. F or that, let us show that for each instantiation of the free v ariables z and w by tw o individuals z ∗ and w ∗ of M , there exists an instanti- ation x ∗ of x which mak es false the three M -formulas ( ∃ y z ∗ = f M ( y ) ∧ y = g ( x ∗ )), ( ∃ ε x ∗ = w ∗ ) and ( ∃ ε x ∗ = g ( x ∗ )). W e hav e: • In the formula ( ∃ y z = f ( y ) ∧ y = g ( x )), the v ariable x is reachable. Thus, its v alue is determined by the v alue of z (b ecause z = f ( g ( x ))). Tw o cases arise: — If z ∗ is of the form f ( g ( i )) with i ∈ M then it is enough to instan tiate x b y an individual x ∗ ∈ M which is differen t from 13 i , in order to make false ( ∃ y z ∗ = f M ( y ) ∧ y = g M ( x ∗ )) in M . — if z ∗ is not of the form f ( g ( i )) with i ∈ M then the M -formula ( ∃ y z ∗ = f M ( y ) ∧ y = g M ( x )) is false in M for all the instan tiations of x . • In the M -formula ( ∃ ε x = w ∗ ), it is enough to instantiate x by an elemen t x ∗ of M whic h is differen t from w ∗ in order to make false the M -formula ( ∃ ε x ∗ = w ∗ ). • In the M -formula ( ∃ ε x = g M ( x )), it is enough to instantiate x by an individ- ual which starts by a distinct function symbol than g in order to mak e false ( ∃ ε x = g M ( x )) in M . Since the set of the functions symbols which are not constants is infinite then there exists an infinity of instantiations of x which satisfy the three preceding conditions. Eac h of these instantiations x ∗ mak es false the three M -formulas ( ∃ y z ∗ = f M ( y ) ∧ y = g M ( x ∗ )), ( ∃ ε x ∗ = w ∗ ) and ( ∃ ε x ∗ = g M ( x ∗ )) and th us (17) holds. 4.3 Normalize d formula 13 F or example, we can take x ∗ = f M ( i ). The ory and Pr actic e of L o gic Pr o gr amming 27 Definition 4.3.1 A normalized form ula ϕ of depth d ≥ 1 is a formula of the form ¬ ( ∃ ¯ x α ∧ ^ i ∈ I ϕ i ) , (18) with I a finite (possibly empt y) set, α a basic formula and the ϕ 0 i s are normalized form ulas of depth d i with d = 1 + max { 0 , d 1 , ..., d n } . Example 4.3.2 Let f and g b e tw o 1-ary function symbols which b elong to F . The formula ¬ ∃ ε finite ( u ) ∧ ¬ ( ∃ x y = f ( x ) ∧ x = g ( y ) ∧ ¬ ( ∃ ε y = g ( x ) ∧ finite ( x ))) ∧ ¬ ( ∃ ε x = f ( z ) ∧ finite ( z )) is a normalized formula of depth equals to three. The formula ¬ ( ∃ ε true ) is a nor- malized formula of depth 1. The smallest v alue of a depth of a normalized formula is 1. Normalized form ulas of depth 0 are not defined and do not exist. W e will use now the abbreviation wnfv for “without new fr e e variables ”. A formula ϕ is equiv alent to a wnfv formula ψ in T means that T | = ϕ ↔ ψ and ψ do es not con tain other free v ariables than those of ϕ . Pr op erty 4.3.3 Ev ery formula ϕ is equiv alent in T to a wnfv normalized formula of depth d ≥ 1. Pr o of It is easy to transform any formula into a normalized form ula, it is enough for example to follo w the followings steps: 1. Introduce a supplement of equations and existentially quan tified v ariables to trans- form the conjunctions of atomic form ulas into conjunctions of flat formulas. 2. Replace each sub-form ula of the form false by ¬ true then express all the quantifiers and logical connectors using only the logical sym b ols ¬ , ∧ and ∃ . This can b e done using the follo wing transformations 14 of sub-form ulas: ( ϕ ∨ φ ) = ⇒ ¬ ( ¬ ϕ ∧ ¬ φ ) , ( ϕ → φ ) = ⇒ ¬ ( ϕ ∧ ¬ φ ) , ( ϕ ↔ φ ) = ⇒ ( ¬ ( ϕ ∧ ¬ φ ) ∧ ¬ ( φ ∧ ¬ ϕ )) , ( ∀ x ϕ ) = ⇒ ¬ ( ∃ x ¬ ϕ ) . 3. If the formula ϕ obtained do es not start with the logical symbol ¬ , then replace it b y ¬ ( ∃ ε true ∧ ¬ ϕ ). 4. Rename the quantified v ariables so that the obtained formula satisfies the imp osed discipline in T (see Section 4.1). 5. Lift the quantifier b efore the conjunction, i.e. ϕ ∧ ( ∃ ¯ x ψ ) or ( ∃ ¯ x ψ ) ∧ ϕ , b ecomes ∃ ¯ x ϕ ∧ ψ b ecause the free v ariables of ϕ are distinct from those of ¯ x . 14 These equiv alences are true in the empty theory and thus in any theory T . 28 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth 6. Group the quantified v ariables in to a vectorial quantifier, i.e. ∃ ¯ x ( ∃ ¯ y ϕ ) or ∃ ¯ x ∃ ¯ y ϕ b ecomes ∃ xy ϕ . 7. Insert empt y v ectors and formulas of the form true to get the normalized form using the follo wing transformations of sub-formulas: ¬ ( ^ i ∈ I ¬ ϕ i ) = ⇒ ¬ ( ∃ ε true ∧ ^ i ∈ I ¬ ϕ i ) , (19) ¬ ( α ∧ ^ i ∈ I ¬ ϕ i ) = ⇒ ¬ ( ∃ ε α ∧ ^ i ∈ I ¬ ϕ i ) , (20) ¬ ( ∃ ¯ x ^ j ∈ J ¬ ϕ j ) = ⇒ ¬ ( ∃ ¯ x true ∧ ^ j ∈ J ¬ ϕ j ) . (21) with α a conjunction of elementary equations, I a finite (p ossibly empty) set and J a finite non-empt y set. 8. Rename the quantified v ariables so that the obtained normalized form ula satisfies the discipline of the form ulas in T . If the starting formula do es not contain the logical symbol ↔ then this transfor- mation will b e linear, i.e. there exists a constan t k suc h that n 2 ≤ k n 1 , where n 1 is the size of the starting formula and n 2 the size of the normalized formula. W e show easily b y con tradiction that the final formula obtained after application of these steps is normalized. Example 4.3.4 Let x , v , w , u b e v ariables suc h that x v w u . Let f b e a 2-ary function sym b ol whic h b elongs to F . Let us apply the preceding steps to transform the follo wing formula into a normalized formula: ( f ( u, v ) = f ( w , u ) ∧ ( ∃ x u = x )) ∨ ( ∃ u ∀ w u = f ( v , w )) . Note that the form ula do es not start with ¬ and the v ariables u and w are free in f ( u, v ) = f ( w , u ) ∧ ( ∃ x u = x ) and b ound in ∃ u ∀ w u = f ( v , w ). Note also that this form ula do es not resp ect the discipline of the formulas in T . Step 1: Let us first transform the equations in to flat equations. The preceding form ula is equiv alent in T to ( ∃ u 1 u 1 = f ( u, v ) ∧ u 1 = f ( w , u ) ∧ ( ∃ x u = x )) ∨ ( ∃ u ∀ w u = f ( v , w )) , (22) where u 1 is a v ariable of V such that u 1 x v w u . Step 2: Let us now express the quantifier ∀ using ¬ , ∧ and ∃ . Thus, the form ula (22) is equiv alen t in T to ( ∃ u 1 u 1 = f ( u, v ) ∧ u 1 = f ( w , u ) ∧ ( ∃ x u = x )) ∨ ( ∃ u ¬ ( ∃ w ¬ ( u = f ( v , w )))) . Let us also express the logical symbol ∨ using ¬ , ∧ and ∃ . Thus, the preceding form ula is equiv alent in T to ¬ ( ¬ ( ∃ u 1 u 1 = f ( u, v ) ∧ u 1 = f ( w , u ) ∧ ( ∃ x u = x )) ∧ ¬ ( ∃ u ¬ ( ∃ w ¬ ( u = f ( v , w ))))) . (23) The ory and Pr actic e of L o gic Pr o gr amming 29 Step 3: As the form ula starts with ¬ , we mov e to Step 4. Step 4: The o ccurrences of the quantified v ariables u and w in ( ∃ u ¬ ( ∃ w ¬ ( u = f ( v , w )))) must b e renamed. Thus, the formula (23) is equiv alen t in T to ¬ ( ¬ ( ∃ u 1 u 1 = f ( u, v ) ∧ u 1 = f ( w , u ) ∧ ( ∃ x u = x )) ∧¬ ( ∃ u 2 ¬ ( ∃ w 1 ¬ ( u 2 = f ( v , w 1 ))))) , where u 2 and w 1 are v ariables of V such that w 1 u 2 u 1 x v w u. Step 5: By lifting the existen tial quantifier ∃ x , the preceding formula is equiv alent in T to ¬ ( ¬ ( ∃ u 1 ∃ x u 1 = f ( u, v ) ∧ u 1 = f ( w , u ) ∧ u = x ) ∧ ¬ ( ∃ u 2 ¬ ( ∃ w 1 ¬ ( u 2 = f ( v , w 1 ))))) . Step 6: Let us group the t wo quantified v ariables x and u 1 in to a vectorial quan tifier. Th us, the preceding formula is equiv alent in T to ¬ ( ¬ ( ∃ u 1 x u 1 = f ( u, v ) ∧ u 1 = f ( w , u ) ∧ u = x ) ∧ ¬ ( ∃ u 2 ¬ ( ∃ w 1 ¬ ( u 2 = f ( v , w 1 ))))) . Step 7: Let us introduce empty vectors of v ariables and formulas of the form true to get the normalized formula. According to the rule (19), the preceding formula is equiv alen t in T to ¬ ∃ ε true ∧ ¬ ( ∃ u 1 x u 1 = f ( u, v ) ∧ u 1 = f ( w , u ) ∧ u = x ) ∧ ¬ ( ∃ u 2 ¬ ( ∃ w 1 ¬ ( u 2 = f ( v , w 1 )))) , whic h using the rule (20) with I = ∅ is equiv alen t in T to ¬ ∃ ε true ∧ ¬ ( ∃ u 1 x u 1 = f ( u, v ) ∧ u 1 = f ( w , u ) ∧ u = x ) ∧ ¬ ( ∃ u 2 ¬ ( ∃ w 1 ¬ ( ∃ ε u 2 = f ( v , w 1 )))) , whic h using the rule (21) is equiv alent in T to ¬ ∃ ε true ∧ ¬ ( ∃ u 1 x u 1 = f ( u, v ) ∧ u 1 = f ( w , u ) ∧ u = x ) ∧ ¬ ( ∃ u 2 true ∧ ¬ ( ∃ w 1 true ∧ ¬ ( ∃ ε u 2 = f ( v , w 1 )))) . Step 8: This is a normalized formula of depth 4 which resp ects the discipline of the form ulas in T since w 1 u 2 u 1 x v w u . 4.4 Gener al solve d formula Definition 4.4.1 A gener al solve d formula is a normalized formula of the form ¬ ( ∃ ¯ x α ∧ n ^ i =1 ¬ ( ∃ ¯ y i β i )) , with n ≥ 0 and such that: 1. α and all the β i , with i ∈ { 1 , ..., n } , are solved basic formulas. 2. If α 0 is the conjunction of the equations of α then all the conjunctions α 0 ∧ β i , with i ∈ { 1 , ..., n } , are solved basic formulas. 3. All the v ariables of ¯ x are reachable in ∃ ¯ x α . 4. F or all i ∈ { 1 , ..., n } , all the v ariables of ¯ y i are reac hable in ∃ ¯ y i β i . 30 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth 5. If finite ( u ) is a sub-formula of α then for all i ∈ { 1 , ..., n } , the formula β i con tains either finite ( u ), or finite ( v ) where v is a reachable v ariable from u in α ∧ β i and do es not o ccur in a left hand side of an equation of α ∧ β i . 6. F or all i ∈ { 1 , ..., n } , the formula β i con tains at least one atomic formula whic h do es not o ccur in α . Example 4.4.2 Let w , v , u 1 , u 2 , u 3 b e v ariables such that w v u 1 u 2 u 3 . The following form ula is not a general solved formula ¬ ( ∃ ε finite ( w ) ∧ ¬ ( ∃ v w = v ∧ finite ( v ))) . (24) This formula satisfies all the conditions of Definition 4.4.1 but it do es not satisfy the discipline of the formulas in T . In fact, the v ariable v is b ound in ( ∃ v w = v ∧ finite ( v )) and the v ariable w is free in ( ∃ v w = v ∧ finite ( v )) and thus we should ha v e v w and not w v . Let u 4 b e a v ariable such that u 4 w v u 1 u 2 u 3 . The form ula (24) is equiv alent in T to ¬ ( ∃ ε finite ( w ) ∧ ¬ ( ∃ u 4 w = u 4 ∧ finite ( v ))) . This formula resp ects the discipline of the formulas of T but is not a general solved form ula since it do es not satisfy the first condition of Definition 4.4.1. In fact, w = u 4 ∧ finite ( v ) is not a solved basic form ula since we hav e u 4 w . The follo wing formula is a general solved formula ¬ ( ∃ v u 1 = f ( v ) ∧ v = u 2 ∧ finite ( u 2 ) ∧ ¬ ( ∃ w u 2 = f ( w ) ∧ finite ( w ) ∧ finite ( u 3 ))) . Pr op erty 4.4.3 Let ϕ b e a general solved form ula. If ϕ has no free v ariables then ϕ is the formula ¬ ( ∃ ε true ) else neither T | = ¬ ϕ nor T | = ϕ . Pr o of Let ϕ b e a general solv ed formula of the form ¬ ( ∃ ¯ x α ∧ ^ i ∈ I ¬ ( ∃ ¯ y i β i )) , (25) t wo cases arise: (1) If ϕ do es not contain free v ariables, then according to the first and third condition of Definition 4.4.1 and using Prop erty 4.2.9 we get ¯ x = ε and α = true . As a consequence, the form ula (25) is equiv alent in T to ¬ ( ∃ ε true ∧ ^ i ∈ I ¬ ( ∃ ¯ y i β i )) , (26) Since (26) has no free v ariables then each ∃ ¯ y i β i has no free v ariables. According to the first and fourth condition of Definition 4.4.1, and using Prop erty 4.2.9 we get: for all i ∈ I : ¯ y i = ε and β i = true . But according to the last condition of Definition 4.4.1 all the formulas β i should b e differen t from true (since we do not distinguish b et ween α and α ∧ true ). Thus, the set I must b e empty . As a consequence, ϕ is the form ula ¬ ( ∃ ε true ). The ory and Pr actic e of L o gic Pr o gr amming 31 (2) If ϕ contains free v ariables then it is enough to sho w that there exist tw o distinct instan tiations ϕ 0 and ϕ 00 of ϕ b y individuals of T r 15 suc h that T r | = ϕ 0 and T r | = ¬ ϕ 00 . Note first that if I 6 = ∅ then each ( ∃ ¯ y i β i ), with i ∈ I , should con tain at least one free v ariable. In fact, if ( ∃ ¯ y i β i ), with i ∈ I , do es not contain free v ariables then this form ula is of the form ( ∃ ε true ) according to the first and fourth p oint of Definition 4.4.1 and Prop erty 4.2.9, whic h con tradicts the last condition of Definition 4.4.1 (since we do not distinguish b etw een α and α ∧ true ). Thus eac h ( ∃ ¯ y i β i ), with i ∈ I , contains at least one free v ariable that can b e instan tiated. On the other hand: Case 1 : If ∃ ¯ x α contains free v ariables then we can easily find an instantiation of the free v ariables of ∃ ¯ x α which contradicts the constraints of α . In fact, let z b e a free v ariable. F our cases arise: • If z = w is a sub-formula of α then according to Definition 4.4.1 α is a solved basic formula and thus z w . As a consequence, w cannot b e a quan tified v ariable otherwise the formula ϕ do es not resp ect the discipline of the formul as in T . Thus is enough to instan tiate z and w b y tw o distinct v alues. • If z = f ( ¯ w ) is a sub-formula of α then it is enough to instantiate z by a tree which starts b y a function symbol which is different from f . • If w = z or w = t [ z ] is a sub-formula of α then according to Definition 4.4.1 all the v ariables of ¯ x are reachable in ∃ ¯ x α and thus according to the first p oint of Remark 4.2.8 the equations w = z and w = t [ z ] are reachable. According to the second p oint of Remark 4.2.8 the v alue of z is linked to another free v ariable v which o ccurs in a left hand side of an equation of α . This case is already treated in tw o preceding cases. • If finite ( z ) is a sub-formula of α then it is enough to instantiate z by an infinite tree. As a consequence, the instan tiated formula of ∃ ¯ x α will b e false in T r and th us T r | = ϕ 0 . On the other hand, b y following the same preceding steps and since: (i) the set F contains an infinity of function symbols which are not constants, (ii) T r con tains an infinity of individuals u of T r such that T r | = finite T r ( u ), (iii) ϕ is a general solv ed formula, then we show that there exists at least one instantiation which satisfies all the constrain ts of α and contradicts the constraints of each β i , with i ∈ I . In fact, (iv) in order to contradicts each constraint β i , it is enough to follow the preceding discussion (b y replacing α b y β i ) and use (i) and (ii). On the other hand, according to Definition 4.4.1 all the v ariables of ¯ x are reachable in ∃ ¯ x α , thus according to the first p oin t of remark 4.2.8 all the equations and relations of α are reac hable in ∃ ¯ x α . According to the second p oint of remark 4.2.8 the v alues of the free v ariables whic h o ccur in these formulas are mainly linked to those of free v ariables whic h occur in left hand side of equations of α . According to the tw o first conditions of Definition 15 Recall that T r is the mo del of finite or infinite trees. 32 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth 4.4.1, the v ariables of Lhs ( α ) are distinct and do not o ccur in F I N I ( α ), Lhs ( β i ) and F I N I ( β i ) for all i ∈ { 1 , ..., n } . As a consequence, from (iv) and using (i), (ii) and (iii) there exists at least one instantiation which satisfies ∃ ¯ x α and con tradicts eac h ∃ ¯ y i β i in T r , with i ∈ I and thus T r | = ¬ ϕ 00 . Note that if I = ∅ then we hav e also T r | = ¬ ϕ 00 and T r | = ϕ 0 using the preceding instan tiations. Case 2 : If ∃ ¯ x α do es not contain free v ariables then according to the first and third condition of Definition 4.4.1 and Prop erty 4.2.9 we hav e ¯ x = ε and α = true . Since ϕ con tains at least one free v ariable then I 6 = ∅ . Let k ∈ I . Since: (i) the set F contains an infinity of function symbols which are not constants, (ii) T r con tains an infinity of individuals u of T r such that T r | = finite T r ( u ), (iii) ϕ is a general solv ed formula, then w e can easily find an instan tiation of the free v ariables of ∃ ¯ y k β k whic h satisfies the constrain ts of β k (similar to the second part of Case 1 by replacing α by β k ). Suc h an instantiation mak es false the instantiated form ula ¬ ( ∃ ¯ y k β k ) in T r and th us T r | = ϕ 0 . On the other hand, according to (i), (ii) and (iii), we show that there exists at least one instantiation which contradicts the constraints of each β i , with i ∈ I (similar to the second part of Case 1 with α = true and ¯ x = ε ). As a consequence, this instan tiation satisfies all the ¬ ( ∃ ¯ y i β i ) in T r , with i ∈ I and thus T r | = ¬ ϕ 00 . F rom Case 1 and Case 2, w e ha ve T r | = ϕ 0 and T r | = ¬ ϕ 00 , and thus neither T | = ϕ nor T | = ¬ ϕ . Example 4.4.4 Let v 1 , v 2 , v , u and w b e v ariables such that v 1 v 2 v u w . Let ϕ b e the follo wing general solved formula ¬ ( ∃ v u = g ( v , w ) ∧ ¬ ( ∃ v 1 v = g ( v , v 1 ) ∧ v 1 = f ( v )) ∧ ¬ ( ∃ v 2 w = g ( w , v 2 ) ∧ v 2 = f ( w )) (27) Let us consider for example the mo del T r of finite or infinite trees. If we instan tiate the free v ariable u b y the finite tree 1 where 1 is a constant in F which is distinct from g then according to axiom [1] of conflict of sym b ols, the instantiated formula of (27) is true in T r . On the other hand, if u is instantiated by a tree of the form g ( v ∗ , w ∗ ) with v ∗ 6 = g ( v ∗ , f ( v ∗ )) (for example v ∗ = 1) and w ∗ 6 = g ( w ∗ , f ( w ∗ )) (for example w ∗ = 1) then the instan tiated formula of (27) is false in T r . As a consequence (27) is neither true nor false in the theory T . The reader should not think that the fact that we hav e neither T | = ¬ ϕ nor T | = ϕ means that ϕ is unsatisfiable in T . T his is of course false. In fact, since neither T | = ¬ ϕ nor T | = ϕ then ϕ has in each mo del M of T a set of solutions whic h mak e it true in M and another set of non-solutions which make it false in M . W e also remind the reader that all the prop erties giv en after Section 4.1 hold only for formulas that resp ect the discipline of the form ulas of T . A similar prop ert y has b een sho wn for the finite trees of J. Lassez (Lassez and Marriott 1987) and the rational trees of M. Maher (Maher and Stuck ey 1995). M. Maher in (Maher and Stuck ey 1995) has also shown that if the set F is finite and The ory and Pr actic e of L o gic Pr o gr amming 33 con tains at least one n -ary function sym b ol with n ≥ 2, then the problem of deciding if a formula containing equations and the logical symbols ∧ , ∨ , ¬ is equiv alen t to a disjunction of conjunctions of equations is a co-NP-complete problem, and the problem of deciding if an expression represents a nonempt y set of rational trees is NP-complete. Note also that in all our proofs w e hav e not used the famous indep endence of inequations (Colmerauer 1984; Lassez et al. 1986; Comon 1988; Lassez and McAlo on 1986) but only the condition that the signature of T is infinite and contains an infinity of function symbols which are not constants and at least one symbol which is a constant, which implies in this case the indep endence of the inequations. Pr op erty 4.4.5 Ev ery general solved formula of the form ¬ ( ∃ ¯ x α ∧ V n i =1 ¬ ( ∃ ¯ y i β i )) is equiv alent in T to the following Bo olean combination of existentially quantified basic formulas: ( ¬ ( ∃ ¯ x α )) ∨ n _ i =1 ( ∃ ¯ x ¯ y i α ∧ β i ) . Pr o of Let ¬ ( ∃ ¯ x α ∧ n ^ i =1 ¬ ( ∃ ¯ y i β i )) , (28) b e a general solv ed formula. According to the third p oint of Definition 4.4.1, all the v ariables of ¯ x are reac hable in ∃ ¯ x α . Thus, according to Prop erty 4.2.10, we hav e T | = ∃ ? ¯ x α . According to Prop erty 3.1.10, the formula (28) is equiv alent in T to ¬ (( ∃ ¯ x α ) ∧ n ^ i =1 ¬ ( ∃ ¯ x α ∧ ( ∃ ¯ y i β i ))) , i.e. to ( ¬ ( ∃ ¯ x α )) ∨ n _ i =1 ( ∃ ¯ x α ∧ ( ∃ ¯ y i β i )) , whic h, since the quantified v ariables hav e distinct names and different from those of the free v ariables, is equiv alent in T to ( ¬ ( ∃ ¯ x α )) ∨ n _ i =1 ( ∃ ¯ x ¯ y α ∧ β i ) , whic h is a Bo olean combination of existentially quantified basic formulas. Definition 4.4.6 Let ϕ b e a form ula of the form ∃ ¯ x α ∧ n ^ i =1 ¬ ( ∃ ¯ y i β i ) , (29) with ¯ x and ¯ y tw o vectors of v ariables, n ≥ 0 and α and the β i , with i ∈ { 1 , ..., n } , 34 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth basic formulas. W e say that ϕ is written in an explicit solve d form if and only if the form ula ¬ ϕ , i.e. ¬ ( ∃ ¯ x α ∧ n ^ i =1 ¬ ( ∃ ¯ y i β i )) , (30) is a general solv ed formula. This definition shows how to easily extract from a general solved formula, a simple form ula ϕ which has only one level of negation and where the solutions of the free v ariables are given in clear and explicit wa y , i.e. for eac h mo del M of T , it is easy to find all the p ossible instantiations of the free v ariables of ϕ which make it true in M . In fact, according to Definition 4.4.1, we w arrant among other things that the left hand sides of the equations of α are distinct and do not o ccur in those of eac h β i , the left hand sides of the equations of eac h β i are distinct and we cannot eliminate an y quantification since all the v ariables are reachable. Example 4.4.7 Let w , v , u 1 , u 2 , u 3 b e v ariables suc h that w v u 1 u 2 u 3 . Let ϕ b e the follo wing general solved formula ¬ ( ∃ v u 1 = f ( v ) ∧ v = u 2 ∧ finite ( u 2 ) ∧ ¬ ( ∃ w u 2 = f ( w ) ∧ finite ( w ) ∧ finite ( u 3 ))) . According to Definition 4.4.6, the following formula φ is written in an explicit solved form: ∃ v u 1 = f ( v ) ∧ v = u 2 ∧ finite ( u 2 ) ∧ ¬ ( ∃ w u 2 = f ( w ) ∧ finite ( w ) ∧ finite ( u 3 )) . (31) Let us c hose the mo del T r of finite or infinite trees and let us giv e all the possible in- stan tiations u ∗ 1 , u ∗ 2 , u ∗ 3 of the free v ariables u 1 , u 2 , u 3 so that the instantiated formula of φ is true in the mo del T r . F rom (31) it is clear that we hav e tw o p ossibilities: • Solution 1: — u ∗ 3 is an y infinite tree. — u ∗ 2 is an y finite tree. — u ∗ 1 is the tree f ( u ∗ 2 ). • Solution 2: — u ∗ 3 is an y finite tree. — u ∗ 2 is any finite tree which starts by a function symbol which is different from f . — u ∗ 1 is the tree f ( u ∗ 2 ). 4.5 Working formula Definition 4.5.1 A w orking formula is a normalized formula in whic h all the o ccurrences of ¬ are replaced by ¬ k with k ∈ { 0 , ..., 5 } and suc h that eac h o ccurrence of a sub-formula of the form p = ¬ k ( ∃ ¯ x α ∧ q ) , w ith k > 0 , (32) The ory and Pr actic e of L o gic Pr o gr amming 35 satisfies the k first conditions of the condition list b ello w. In (32) α is a basic form ula, q is a conjunction of w orking formulas of the form V n i =1 ¬ k i ( ∃ ¯ y i β i ∧ q i ) , with n ≥ 0, β i a basic form ula, q i a conjunction of w orking formulas, and in the b elo w condition list α 0 is the basic form ula of the immediate top-working formula 16 p 0 of p if it exists. 1. If p 0 exists then T | = α → α 0 and T | = α eq → α 0 eq where α eq and α 0 eq are the conjunctions of the equations of α respectively α 0 . Moreov er, the set of the v ariables of Lhs ( α 0 ) ∪ F I N I ( α 0 ) is included in those of Lhs ( α ) ∪ F I N I ( α ). 2. The left hand sides of the equations of α are distinct and for all equations of the form u = v w e hav e u v . 3. α is a basic solved formula. 4. If p 0 exists then the set of the equations of α 0 is included in those of α . 5. The v ariables of ¯ x , the equations of α and the constrain ts of the form finite ( x ) of α are reachable in ∃ ¯ x α . Moreov er, if n > 0 then for all i ∈ { 1 , ..., n } the conjunction β i con tains at least one atomic formula which do es not o ccur in α . The intuitions b ehind these working formulas come from an aim to hav e a full con trol on the execution of our rewriting rules by adding semantic informations on a syntactic form of formulas. W e emphasize strongly that ¬ k do es not mean that the normalized formula satisfies only the k th condition but all the conditions i with 1 ≤ i ≤ k . Example 4.5.2 Let w 1 , w 2 , w 3 , v 1 , u b e v ariables such that w 1 w 2 w 3 v 1 u . This is a w orking formula of depth 2: ¬ 2 ∃ v 1 u = f ( v 1 ) ∧ finite ( u ) ∧ ¬ 2 ( ∃ w 1 u = f ( w 1 ) ∧ w 1 = v 1 ∧ finite ( u )) ∧ ¬ 3 ( ∃ w 2 u = f ( v 1 ) ∧ w 2 = f ( v 1 ) ∧ finite ( v 1 )) ∧ ¬ 4 ( ∃ w 3 u = f ( v 1 ) ∧ v 1 = f ( w 3 ) ∧ finite ( w 3 )) Definition 4.5.3 An initial w orking form ula is a w orking form ula whic h b egins with ¬ 4 and such that k = 0 for all the other o ccurrences of ¬ k . A final working form ula is a working form ula of depth less or equal to 2 with k = 5 for all the o ccurrences of ¬ k . The relation b etw een the final working formulas and the general solved formulas is expressed in the follo wing prop erty: Pr op erty 4.5.4 Let p b e the follo wing final working form ula ¬ 5 ( ∃ ¯ x α ∧ V n i =1 ¬ 5 ( ∃ ¯ y i β i )) . The form ula ¬ ( ∃ ¯ x α ∧ V n i =1 ¬ ( ∃ ¯ y i β ∗ i )) , is a general solved formula equiv alen t to p in T where β ∗ i is the basic formula β i from which we hav e remov ed all the equations which o ccur also in α . 16 In other words, p 0 is of the form ¬ k 0 ( ∃ ¯ x 0 α 0 ∧ p ∗ ∧ p ) where p ∗ is a conjunction of w orking formulas and p is the formula (32). 36 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth Example 4.5.5 Let w 2 , v , u and u 1 b e v ariables such that w 2 v u u 1 . Let ϕ b e the follo wing final w orking formula ¬ 5 ∃ ε v = u ∧ finite ( u ) ∧ ¬ 5 ( ∃ ε v = u ∧ u = u 1 ∧ finite ( u 1 )) ∧ ¬ 5 ( ∃ w 2 v = u ∧ u = s ( w 2 ) ∧ finite ( w 2 )) . The form ula ¬ ∃ ε v = u ∧ finite ( u ) ∧ ¬ ( ∃ ε u = u 1 ∧ finite ( u 1 )) ∧ ¬ ( ∃ w 2 u = s ( w 2 ) ∧ finite ( w 2 )) . is a general solv ed formula equiv alent to ϕ in T . 4.6 R ewriting rules W e no w present the rewriting rules whic h transform an initial working formula of an y depth d into an equiv alen t conjunction of final working formulas. T o apply the rule p 1 = ⇒ p 2 to the w orking formula p means to replace in p a sub-formula p 1 b y the form ula p 2 , b y considering that the connector ∧ is associative and comm utative. In the following, the letters u , v and w represent v ariables, the letters ¯ x , ¯ y and ¯ z represen t vectors of v ariables, the letters a , b and c represent basic formulas, the letter q represents a conjunction of working formulas, the letter r represents a conjunction of flat equations, formulas of the form finite ( x ) and working formulas. All these letters can b e subscripted or hav e primes. (1) ¬ 1 ( ∃ ¯ x u = u ∧ r ) = ⇒ ¬ 1 ( ∃ ¯ x r ) (2) ¬ 1 ( ∃ ¯ x v = u ∧ r ) = ⇒ ¬ 1 ( ∃ ¯ x u = v ∧ r ) (3) ¬ 1 ( ∃ ¯ x u = v ∧ u = t ∧ r ) = ⇒ ¬ 1 ( ∃ ¯ x u = v ∧ v = t ∧ r ) (4) ¬ 1 ( ∃ ¯ x u = f v 1 ...v n ∧ u = g w 1 ...w m ∧ r ) = ⇒ true (5) ¬ 1 ( ∃ ¯ x u = f v 1 ...v n ∧ u = f w 1 ...w n ∧ r ) = ⇒ ¬ 1 ( ∃ ¯ x u = f v 1 ...v n ∧ V n i =1 v i = w i ∧ r ) (6) ¬ 1 ( ∃ ¯ x a ∧ q ) = ⇒ ¬ 2 ( ∃ ¯ x a ∧ q ) (7) ¬ 2 ( ∃ ¯ x finite ( u ) ∧ finite ( u ) ∧ r ) = ⇒ ¬ 2 ( ∃ ¯ x finite ( u ) ∧ r ) (8) ¬ 2 ( ∃ ¯ x u = v ∧ finite ( u ) ∧ r ) = ⇒ ¬ 2 ( ∃ ¯ x u = v ∧ finite ( v ) ∧ r ) (9) ¬ 2 ( ∃ ¯ x finite ( u ) ∧ a ∧ q ) = ⇒ true (10) ¬ 2 ( ∃ ¯ x u = f ( v 1 , ..., v n ) ∧ finite ( u ) ∧ r ) = ⇒ ¬ 2 ( ∃ ¯ x u = f ( v 1 , ..., v n ) ∧ V n i =1 finite ( v i ) ∧ r ) (11) ¬ 2 ( ∃ ¯ x a ∧ q ) = ⇒ ¬ 3 ( ∃ ¯ x a ∧ q ) (12) ¬ 4 ( ∃ ¯ x a ∧ q ∧ ¬ 0 ( ∃ ¯ y r )) = ⇒ ¬ 4 ( ∃ ¯ x a ∧ q ∧ ¬ 1 ( ∃ ¯ y a ∧ r )) (13) ¬ 4 ( ∃ ¯ x a ∧ a 0 ∧ q ∧ ¬ 3 ( ∃ ¯ y a 00 ∧ r )) = ⇒ ¬ 4 ( ∃ ¯ x a ∧ a 0 ∧ q ∧ ¬ 4 ( ∃ ¯ y a ∧ r )) (14) ¬ 4 ( ∃ ¯ x a ∧ q ∧ ¬ 5 ( ∃ ¯ y a )) = ⇒ true (15) ¬ 4 ( ∃ ¯ x a ∧ V n i =1 ¬ 5 ( ∃ ¯ y i b i )) = ⇒ ¬ 5 ( ∃ ¯ x 0 a 0 ∧ V i ∈ K ¬ 5 ( ∃ ¯ y 0 i b 0 i ) ∗ ) (16) ¬ 4 ∃ ¯ x a ∧ q ∧ ¬ 5 " ∃ ¯ y b ∧ V n i =1 ¬ 5 ( ∃ ¯ z i c i ) # = ⇒ " ¬ 4 ( ∃ ¯ x a ∧ q ∧ ¬ 5 ( ∃ ¯ y b )) ∧ V n i =1 ¬ 4 ( ∃ ¯ x ¯ y ¯ z i c i ∧ q 0 ) ∗ # The ory and Pr actic e of L o gic Pr o gr amming 37 with u v , f and g t wo distinct function sym b ols taken from F . In rule (3), t is a flat term, i.e. either a v ariable or a term of the form f ( x 1 , ..., x n ) with f an n -ary function symbol tak en from F . In rule (6), the equations of a hav e distinct left hand sides and for each equation of the form u = v we hav e u v . In rule (9), the v ariable u is reachable from u in a . In rule (10), the v ariable u is non-reachable from u in a . Moreov er, if f is a constant then n = 0. In rule (11), a is a solved basic form ula. In rule (13), a and a 00 are conjunctions of equations having the same left hand sides and a 0 is a conjunction of formulas of the form finite ( u ). In rule (15), n ≥ 0 and for all i ∈ { 1 , ..., n } the formula b i is differen t from the formula a . The pairs ( ¯ x 0 , a 0 ) and ( ¯ y 0 i , b 0 i ) are obtained by a decomp osition of ¯ x and a into ¯ x 0 ¯ x 00 ¯ x 000 and a 0 ∧ a 00 ∧ a 000 as follo ws: • a 0 is the conjunction of the equations and the formulas of the form finite ( x ) whic h are reachable in ∃ ¯ x a . • ¯ x 0 is the v ector the v ariables of ¯ x which are reachable in ∃ ¯ x a . • a 00 is the conjunction of the formulas of the form finite ( x ) which are non- reac hable in ∃ ¯ x a . • ¯ x 00 is the vector the v ariables of ¯ x which are non-reachable in ∃ ¯ x a and do not o ccur in the left hand sides of the equations of a . • a 000 is the conjunction of the equations whic h are non-reachable in ∃ ¯ x a . • ¯ x 000 is the vector the v ariables of ¯ x which are non-reachable in ∃ ¯ x a and o ccur in the left hand sides of the equations of a . • b ∗ i is the formula obtained by removing from b i the formulas of the form finite ( u ) which o ccur also in a 00 • ¯ y 0 i is the v ector of the v ariables of ¯ y i ¯ x 000 whic h are reachable in ∃ ¯ y i ¯ x 000 b ∗ i . • b 0 i is the conjunction of the equations and the formulas of the form finite ( x ) whic h are reachable in ∃ ¯ y i ¯ x 000 b ∗ i . • K ⊆ { 1 , ..., n } is the set of the indices i such that i ∈ K if and only if no v ariable of ¯ x 00 o ccurs in b 0 i . • The formula V i ∈ K ¬ 5 ( ∃ ¯ y 0 i b 0 i ) ∗ is the formula V i ∈ K ¬ 5 ( ∃ ¯ y 0 i b 0 i ) in which we ha ve renamed the quantified v ariables so that they satisfy the discipline of the form ulas in T . In rule (16), n > 0 and q 0 is the form ula q in whic h all the o ccurrences of ¬ k ha ve b een replaced by ¬ 0 . The formula V n i =1 ¬ 4 ( ∃ ¯ x ¯ y ¯ z i c i ∧ q 0 ) ∗ is the formula V n i =1 ¬ 4 ( ∃ ¯ x ¯ y ¯ z i c i ∧ q 0 ) in which we ha ve renamed the quantified v ariables so that they satisfy the discipline of the form ulas of T . The use of indices on the negations of the working formulas enables us to force the application of the rules to follow a clear strategy un til reaching a conjunction of final working formulas. In fact, the algorithm follo ws tw o main steps while solving an y first-order constraint in T : • (i) A top-down propagation of basic formulas follo wing the tree structure of the working formulas and using the rules (1),...,(13). In this step, basic form ulas are solved and copied in all sub-working formulas. Finiteness is also c heck and inconsistent basic formulas are remov ed b y the rules (4) and (9). 38 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth • (ii) A b ottom-up elimination of quantifiers and depth reducing of the w orking form ulas using the rules (14),...,(16). Inconsistent working form ulas are also remo ved in this step. More precisely , starting from an initial w orking formula ϕ of the form ¬ 4 ( ∃ ¯ x a ∧ V i ∈ I q i ), where all the q i are working formulas whose negations are of the form ¬ 0 , rule (12) propagates the atomic formulas of a into a sub-form ula q i , with i ∈ I , and changes the first negation of q i in to ¬ 1 . The rules (1),...,(5) can now b e applied un til the equations of a hav e distinct left hand sides and for each equation of the form u = v we hav e u v . Rule (6) is then applied and changes the first negation of q i in to ¬ 2 . The algorithm starts now a new phase which consists in solving the basic form ulas using the rules (7),...,(10). In particular finiteness is chec ked by rule (9). When a solv ed basic formula is obtained, rule (11) is applied and changes the negation into ¬ 3 . Note that if a working formula starts by ¬ 3 then its top w orking form ula starts by ¬ 4 . Rule (13) is then applied. It restores some equations and c hanges the first negation into ¬ 4 . Rule (12) can now b e applied again since all the nested negations are of the form ¬ 0 and so on. This is the first step of our algorithm. Once the sub-working formulas of depth 1 are of the form ¬ 4 ( ∃ ¯ y i b i ), the second step starts using rule (15) with n = 0 on all these sub-working-form ulas of depth 1 and transforms their negations in to ¬ 5 . Inconsistent working formulas of the form ¬ 4 ( ∃ ¯ x α ∧ ¬ 5 ( ∃ ¯ y α ) ∧ q ) are then remov ed by rule (14). When all the inconsistent w orking formulas hav e b een remov ed, rule (15) with n 6 = 0 can b e applied on the sub-working-form ulas of depth 2 of the form ¬ 4 ( ∃ ¯ x a ∧ V i ∈ I ¬ 5 ( ∃ ¯ y i b i )) and pro duces working formulas of the form ¬ 5 ( ∃ ¯ x a ∧ V i ∈ I ¬ 5 ( ∃ ¯ y i b i )). Rule (16) can no w b e applied on the working form ulas of depth d > 2 of the form ¬ 4 ( ∃ ¯ x a ∧ q ∧ ¬ 5 ( ∃ ¯ y b ∧ V n i =1 ¬ 5 ( ∃ ¯ z i c i ))). After eac h application of this rule, new w orking form ulas con taining negations of the form ¬ 0 are created which implies the execution of the rules of the first step of our algorithm, starting by rule (12) and so on. After several applications of our rules, we get a conjunction of working formulas whose depth is less or equal to 2. The rules are then applied again until all the negations of these w orking formulas are of the form ¬ 5 . It is a conjunction of final w orking formulas. Example 4.6.1 Let f and g b e tw o function symbols tak en from F of resp ective arities 2 , 1. Let w 1 , w 2 , v 1 , u 1 , u 2 , u 3 b e v ariables such that w 1 w 2 v 1 u 1 u 2 u 3 . Let us run our rules on the follo wing initial working formula ¬ 4 ∃ v 1 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ ¬ 0 ( ∃ w 1 v 1 = g ( w 1 )) ∧ ¬ 0 ( ∃ w 2 u 2 = g ( w 2 ) ∧ w 2 = g ( u 3 ) ∧ finite ( w 2 )) . (33) According to rule (12), the preceding form ula is equiv alent in T to ¬ 4 ∃ v 1 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ ¬ 1 ( ∃ w 1 v 1 = g ( w 1 ) ∧ v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 )) ∧ ¬ 0 ( ∃ w 2 u 2 = g ( w 2 ) ∧ w 2 = g ( u 3 ) ∧ finite ( w 2 )) . The application of rule (4) on the sub form ula ¬ 1 ( ∃ w 1 v 1 = g ( w 1 ) ∧ v 1 = f ( u 1 , u 2 ) ∧ The ory and Pr actic e of L o gic Pr o gr amming 39 u 2 = g ( u 1 ) ∧ finite ( w 2 )) simplifies this sub formula into the form ula true . Thus, the preceding form ula is equiv alent in T to ¬ 4 ∃ v 1 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ ¬ 0 ( ∃ w 2 u 2 = g ( w 2 ) ∧ w 2 = g ( u 3 ) ∧ finite ( w 2 )) , whic h according to rule (12) is equiv alent in T to ¬ 4 ∃ v 1 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ ¬ 0 ( ∃ w 2 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ u 2 = g ( w 2 ) ∧ w 2 = g ( u 3 ) ∧ finite ( w 2 )) . Rule (5) can no w b e applied. Thus, the preceding formula is equiv alent in T to ¬ 4 ∃ v 1 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ ¬ 1 ( ∃ w 2 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( w 2 ) ∧ w 2 = u 1 ∧ w 2 = g ( u 3 ) ∧ finite ( w 2 )) , whic h according to rule (3) is equiv alent in T to ¬ 4 ∃ v 1 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ ¬ 1 ( ∃ w 2 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( w 2 ) ∧ w 2 = u 1 ∧ u 1 = g ( u 3 ) ∧ finite ( w 2 )) . Since the conjunction of equations of the sub-formula whic h starts by ¬ 1 has distinct left hand sides and w 2 u 1 , then rule (6) can b e applied. Th us, the preceding form ula is equiv alent in T to ¬ 4 ∃ v 1 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ ¬ 2 ( ∃ w 2 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( w 2 ) ∧ w 2 = u 1 ∧ u 1 = g ( u 3 ) ∧ finite ( w 2 )) , whic h according to rule (8) is equiv alent in T to ¬ 4 ∃ v 1 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ ¬ 2 ( ∃ w 2 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( w 2 ) ∧ w 2 = u 1 ∧ u 1 = g ( u 3 ) ∧ finite ( u 1 )) , whic h according to rule (10) is equiv alent in T to ¬ 4 ∃ v 1 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ ¬ 2 ( ∃ w 2 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( w 2 ) ∧ w 2 = u 1 ∧ u 1 = g ( u 3 ) ∧ finite ( u 3 )) . Since the basic formulas are solved then rule (11) can b e applied. Thus, the pre- ceding form ula is equiv alent in T to ¬ 4 ∃ v 1 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ ¬ 3 ( ∃ w 2 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( w 2 ) ∧ w 2 = u 1 ∧ u 1 = g ( u 3 ) ∧ finite ( u 3 )) , whic h according to rule (13) is equiv alent in T to ¬ 4 ∃ v 1 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ ¬ 4 ( ∃ w 2 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ w 2 = u 1 ∧ u 1 = g ( u 3 ) ∧ finite ( u 3 )) . Rule (15) can now be applied with n = 0. Thus, the preceding form ula is equiv alent in T to ¬ 4 ∃ v 1 v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ ¬ 5 ( ∃ ε v 1 = f ( u 1 , u 2 ) ∧ u 2 = g ( u 1 ) ∧ u 1 = g ( u 3 ) ∧ finite ( u 3 )) . Once again rule (15) can b e applied, with n 6 = 0 and we get the following final 40 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth w orking formula ¬ 5 ∃ ε u 2 = g ( u 1 ) ∧ ¬ 5 ( ∃ ε u 2 = g ( u 1 ) ∧ u 1 = g ( u 3 ) ∧ finite ( u 3 )) , whic h according to Prop erty 4.5.4 is equiv alent in T to the following general solv ed form ula ¬ u 2 = g ( u 1 ) ∧ ¬ ( u 1 = g ( u 3 ) ∧ finite ( u 3 )) . W e hav e seen in the preceding example how the rules (1),...,(15) can b e applied. Let us no w see how rule (16) is applied. Example 4.6.2 Let s and 0 b e tw o function symbols taken from F of resp ective arities 1 , 0. Let w 1 , w 2 , u , v b e v ariables such that w 1 w 2 v u . Let us apply our rules on the follo wing working formula of depth 3: ¬ 4 ∃ ε true ∧ ¬ 5 ( ∃ ε u = s ( v )) ∧ ¬ 5 ( ∃ w 1 u = s ( w 1 ) ∧ w 1 = s ( v )) ∧ ¬ 5 ( ∃ ε v = u ∧ ¬ 5 ( ∃ ε v = u ∧ u = 0) ∧ ¬ 5 ( ∃ w 2 v = u ∧ u = s ( w 2 ))) . By considering that • ( ∃ ¯ x a ) = ( ∃ ε true ) • q = ¬ 5 ( ∃ ε u = s ( v )) ∧ ¬ 5 ( ∃ w 1 u = s ( w 1 ) ∧ w 1 = s ( v )) • ( ∃ ¯ y b ) = ( ∃ ε v = u ) • V n i =1 ¬ 5 ( ∃ ¯ z i c i ) = ¬ 5 ( ∃ ε v = u ∧ u = 0) ∧ ¬ 5 ( ∃ w 2 v = u ∧ u = s ( w 2 )) rule (16) can b e applied and pro duces the following form ula ¬ 4 ( ∃ ε true ∧ ¬ 5 ( ∃ ε u = s ( v )) ∧ ¬ 5 ( ∃ w 1 u = s ( w 1 ) ∧ w 1 = s ( v )) ∧ ¬ 5 ( ∃ ε v = u )) ∧ ¬ 4 ( ∃ ε v = u ∧ u = 0 ∧ ¬ 0 ( u = s ( v )) ∧ ¬ 0 ( ∃ w 11 u = s ( w 11 ) ∧ w 11 = s ( v ))) ∧ ¬ 4 ( ∃ w 2 v = u ∧ u = s ( w 2 ) ∧ ¬ 0 ( ∃ ε u = s ( v )) ∧ ¬ 0 ( ∃ w 12 u = s ( w 12 ) ∧ w 12 = s ( v ))) , where w 11 and w 12 are v ariables such that w 11 w 12 w 1 w 2 v u . Now, only the rules (1),...,(15) will b e applied until all the negations are of the form ¬ 5 . Rule (16) will not b e applied anymore since there exists no w orking form ulas of depth greater or equal to 3 and the rules (1),...,(15) never increase the depth of the w orking formulas. Pr op erty 4.6.3 Ev ery rep eated application of the preceding rewriting rules on an initial w orking form ula p is terminating and producing a wnfv conjunction of final w orking formulas equiv alen t to p in T . The ory and Pr actic e of L o gic Pr o gr amming 41 Pr o of Pr o of, first p art: The application of the rewriting rules terminates. Let us in tro duce the function α : q → n , where q is a conjunction of working formulas, n an in teger and suc h that • α ( true ) = 0, • α ( ¬ ( ∃ ¯ x a ∧ ϕ )) = 2 α ( ϕ ) , • α ( V i ∈ I ϕ i ) = P i ∈ I α ( ϕ i ) , with a a basic formula, ϕ a conjunction of working form ulas and the ϕ i ’s working form ulas. Note that if α ( p 2 ) < α ( p 1 ) then α ( p [ p 2 ]) < α ( p ) where p [ p 2 ] is the form ula obtained from p when w e replace the o ccurrence of the formula p 1 in p b y p 2 . This function has b een introduced in (V oroby o v 1996) and (Colmerauer and Dao 2003) to sho w the non-elementary complexit y of all algorithms solving propositions in the theory of finite or infinite trees. It has also the prop erty to decrease if the depth of the working formula decreases after application of distributions as it is done in our rule (16). Let us introduce also the function λ : ( u, a ) → n , where u is a v ariable, a a basic form ula, n an integer and such that λ ( u, a ) = 0 , if the conjunction of the equations of a has not distinct left hand sides or con tains a sub-form ula of the form x = y with y x, else 1 , if u do es not o ccur in a left hand side of an equation of a , or u is reac hable from u in a, else 1 + λ ( v , a ) , if the equation u = v is in a, else 2 + P n i =1 λ ( v i , a ) , if the equation u = f ( v 1 , ..., v n ) is in a. Since the v ariables which occur in our formulas are ordered by the order relation “ ”, we can num b er them by p ositive integers such that x y ↔ no ( x ) > no ( y ) , where no ( x ) is the num b er asso ciated to the v ariable x . Let us consider the 10- tuple ( n 1 , n 2 , n 3 , n 4 , n 5 , n 6 , n 7 , n 8 , n 9 , n 10 ) where the n i ’s are the following p ositive in tegers: • n 1 = α ( p ), • n 2 is the n umber of ¬ 0 , • n 3 is the n umber of ¬ 1 , • n 4 is the num b er of occurrences of function symbols in sub-form ulas of the form ¬ 1 ( ... ). F or example, if w e hav e ¬ 1 ( ∃ x x = f ( y ) ∧ y = f ( x ) ∧ x = g ( x, w ) ∧ y = f ( y )) then n 4 = 4. • n 5 is the sum of all the no ( x ) for each o ccurrence of a v ariable x in a basic formula of a sub-formula of the form ¬ 1 ( ... ). F or example, if w e ha v e ¬ 1 ( ∃ w x = f ( x, z ) ∧ y = x ∧ finite ( z ) ∧ ... ) then n 5 = no ( x ) + no ( x ) + no ( z ) + no ( y ) + no ( x ) + no ( z ) + ... . 42 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth • n 6 is the num b er of formulas of the form v = u with u v in sub-formulas of the form ¬ 1 ( ... ), • n 7 is the n umber of ¬ 2 , • n 8 is the sum of all the λ ( u, a ) for each o ccurrence of a sub-form ula finite ( u ) in a basic-formula a of a working formula of the form ¬ 2 ( ∃ ¯ x a ∧ q ). F or example, if w e hav e ¬ 2 ( ∃ z x = f ( x, z ) ∧ z = f ( y , y ) ∧ finite ( x ) ∧ finite ( x ) ∧ finite ( z )) then n 8 = λ ( x, a ) + λ ( x, a ) + λ ( z , a ) = 1 + 1 + (2 + 1 + 1) where a is the basic formula x = f ( x, z ) ∧ z = f ( y , y ) ∧ finite ( x ) ∧ finite ( x ) ∧ finite ( z ). • n 9 is the n umber of ¬ 3 • n 10 is the n umber of ¬ 4 . F or each rule, there exists a p ositive integer i such that the application of this rule decreases or do es not change the v alues of the n j ’s, with 1 ≤ j < i , and decreases the v alue of n i . These i are equal to: 1 for the rules (4), (9), (14) and (16), 2 for rule (12), 3 for rule (6), 4 for rule (5), 5 for the rules (1), (3), (7) and (8) , 6 for rule (2), 7 for rule (11), 8 for rule (10), 9 for rule (13), and 10 for rule (15). T o each sequence of formulas obtained by a finite application of the preceding rewriting rules, w e can asso ciate a series of 10-tuples ( n 1 , n 2 , n 3 , n 4 , n 5 , n 6 , n 7 , n 8 , n 9 , n 10 ) whic h is strictly decreasing in the le xicographic order. Since the n i ’s are p ositive in tegers, they cannot b e negative, thus, this series of 10-tuples is a finite series and the application of the rewriting rules terminates. Pr o of, se c ond p art: Let us now sho w that for each rule of the form p = ⇒ p 0 w e ha ve T | = p ↔ p 0 and the form ula p 0 remains a conjunction of w orking formula. Corr e ctness of the rules (1),...,(14) The rules (1),...(5) are correct according to the axioms [1] and [2] of T . Rules (6) and (11) are evident. The rules (7) and (8) are true in the empty theory and thus true in T . In rule (9), the v ariable u is reac hable from itself in a , i.e. the basic form ula a contains a sub-formula of the form u = t 1 ∧ u 2 = t 2 ∧ ... ∧ u n = t n (34) where u i o ccurs in the term t i − 1 for all i ∈ { 2 , ..., n } and u o ccurs in t n . According to Definition 4.5.1, since our working formula starts with ¬ 2 then all the equations of a ha ve distinct lef hand sides and for all equations of the form x = y we ha v e x y . Th us, there exists at least one equation in (34) whic h contains a function symbol whic h is not a constant, otherwise (34) is of the form u = u 2 ∧ u 2 = u 3 ∧ ... ∧ u n = u whic h implies u u 2 ... u , i.e. u u which is false since the order is strict. Thus, according to the fourth axiom of T we hav e T | = a → ¬ finite ( u ). As a consequence, rule (9) is correct in T . Rule (10) is correct according to the last axiom of T . Rule (13) is correct according to Prop erty 4.2.4 and Definition 4.5.1. The rules (12) and (14) are true in the empt y theory and thus true in T . Note that according to Prop erty 4.2.5, t wo solved basic formulas having the same equations are equiv alen t if and only if they hav e the same relations finite ( x ). This is why in Definition 4.5.1 of the working formulas (more precisely in condition 4) The ory and Pr actic e of L o gic Pr o gr amming 43 w e force only the equations to b e included in the sub-forworking formulas and use the elemen tary rule (14) to remov e inconsistent working formulas of depth 2. Corr e ctness of rule (15) ¬ 4 ( ∃ ¯ x a ∧ n ^ i =1 ¬ 5 ( ∃ ¯ y i b i )) = ⇒ ¬ 5 ( ∃ ¯ x 0 a 0 ∧ ^ i ∈ K ¬ 5 ( ∃ ¯ y 0 i b 0 i ) ∗ ) with n ≥ 0, and for all i ∈ { 1 , ..., n } the formula b i is different from the formula a . The pairs ( ¯ x 0 , a 0 ) and ( ¯ y 0 i , b 0 i ) are obtained by a decomp osition of ¯ x and a into ¯ x 0 ¯ x 00 ¯ x 000 and a 0 ∧ a 00 ∧ a 000 as follo ws: • a 0 is the conjunction of the equations and the form ulas of the form finite ( x ) which are reac hable in ∃ ¯ x a . • ¯ x 0 is the v ector the v ariables of ¯ x which are reachable in ∃ ¯ x a . • a 00 is the conjunction of the formulas of the form finite ( x ) which are non-reachable in ∃ ¯ x a . • ¯ x 00 is the vector the v ariables of ¯ x which are non-reachable in ∃ ¯ x a and do not o ccur in the left hand sides of the equations of a . • a 000 is the conjunction of the equations whic h are non-reachable in ∃ ¯ x a . • ¯ x 000 is the vector the v ariables of ¯ x which are non-reachable in ∃ ¯ x a and o ccur in the left hand sides of the equations of a . • b ∗ i is the form ula obtained by removing from b i the form ulas of the form finite ( u ) whic h o ccur also in a 00 • ¯ y 0 i is the v ector of the v ariables of ¯ y i ¯ x 000 whic h are reachable in ∃ ¯ y i ¯ x 000 b ∗ i . • b 0 i is the conjunction of the equations and the formulas of the form finite ( x ) which are reac hable in ∃ ¯ y i ¯ x 000 b ∗ i . • K ⊆ { 1 , ..., n } is the set of the indices i suc h that i ∈ K if and only if no v ariable of ¯ x 00 o ccurs in b 0 i . • The formula V i ∈ K ¬ 5 ( ∃ ¯ y 0 i b 0 i ) ∗ is the formula V i ∈ K ¬ 5 ( ∃ ¯ y 0 i b 0 i ) in which we hav e re- named the quantified v ariables so that they satisfy the discipline of the formulas in T . Let ¯ x 0 , ¯ x 00 , ¯ x 000 , ¯ y 0 and a 0 , a 00 , a 000 , b ∗ i , b 0 i b e the vector of v ariables and the basic form ulas defined ab o ve. According to Definition 4.2.6, (i) all the v ariables of ¯ x 00 and ¯ x 000 do not o ccur in a 0 , otherwise they are reac hable in ∃ ¯ x a . On the other hand, since the first negation in the left hand side of rule (15) is of the form ¬ 4 then according to Definition 4.5.1 (ii) a is a solved basic form ula and thus ¯ x 000 is the v ector of the left hand sides of the equations of a 000 and its v ariables do not o ccur in a 00 . Thus, according to (i) and (ii) the left hand side of rule (15) is equiv alen t in T to ¬ ( ∃ ¯ x 0 a 0 ∧ ( ∃ ¯ x 00 a 00 ∧ ( ∃ ¯ x 000 a 000 ∧ n ^ i =1 ¬ ( ∃ ¯ y i b i )))) . Since a is a solved basic form ula then a 000 is a solved basic formula which contains only equations and thus according to Prop erty 4.2.3 we hav e T | = ∃ ! ¯ x 000 a 000 . Thus, 44 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth according to Prop ert y 3.1.11 the preceding formula is equiv alent in T to ¬ ( ∃ ¯ x 0 a 0 ∧ ( ∃ ¯ x 00 a 00 ∧ n ^ i =1 ¬ ( ∃ ¯ x 000 a 000 ∧ ( ∃ ¯ y i b i )))) , whic h, according to the discipline of the formulas in T (the quantified v ariables ha ve distinct names and different from those of the free v ariables ), is equiv alen t in T to ¬ ( ∃ ¯ x 0 a 0 ∧ ( ∃ ¯ x 00 a 00 ∧ n ^ i =1 ¬ ( ∃ ¯ x 000 ¯ y i a 000 ∧ b i ))) . (35) Since all the nested negations in the left hand side of rule (15) are of the form ¬ 5 then according to Definition 4.5.1, for all i ∈ { 1 , ..., n } , the set of the equations of a is included in those of b i . As a consequence, the form ula (35) is equiv alent in T to ¬ ( ∃ ¯ x 0 a 0 ∧ ( ∃ ¯ x 00 a 00 ∧ n ^ i =1 ¬ ( ∃ ¯ x 000 ¯ y i b i ))) , i.e. to ¬ ( ∃ ¯ x 0 a 0 ∧ ( ∃ ¯ x 00 a 00 ∧ n ^ i =1 ¬ ( ∃ ¯ x 000 ¯ y i b ∗ i ))) . Since all the nested negations in the left hand side of rule (15) are of the form ¬ 5 , then according to Definition 4.5.1, for all i ∈ { 1 , ..., n } , b ∗ i is a solved basic formula. Th us, according to Prop erty 4.2.11, the preceding formula is equiv alent in T to ¬ ( ∃ ¯ x 0 a 0 ∧ ( ∃ ¯ x 00 a 00 ∧ n ^ i =1 ¬ ( ∃ ¯ y 0 i b 0 i ))) , whic h is equiv alent in T to ¬ ( ∃ ¯ x 0 a 0 ∧ ( ^ i ∈ K ¬ ( ∃ ¯ y 0 i b 0 i )) ∧ ( ∃ ¯ x 00 a 00 ∧ ^ i ∈{ 1 ,...,n }− K ¬ ( ∃ ¯ y 0 i b 0 i ))) , where K ⊆ { 1 , ..., n } is the set of the indices i suc h that i ∈ K if and only if no v ariable of ¯ x 00 o ccurs in b 0 i . Since all the nested negations in the left hand side of rule (15) are of the form ¬ 5 then according to Definition 4.5.1, for all i ∈ { 1 , ..., n } − K , the v ariables of ¯ y 0 i are reachable in ∃ ¯ y 0 i b 0 i and the form ula b 0 i is a solv ed basic form ula. Moreov er, since eac h b 0 i do es not con tain sub-form ulas of the form finite ( x ) whic h o ccur also in a 00 (see the construction of b ∗ i ), then the formula ∃ ¯ x 00 a 00 ∧ V i ∈{ 1 ,...,n }− K ¬ ( ∃ ¯ y 0 b 0 i ) satisfies the conditions of Prop erty 4.2.13. As a consequence, according to Prop ert y 4.2.13 the preceding formula is equiv alent in T to ¬ ( ∃ ¯ x 0 a 0 ∧ ^ i ∈ K ¬ ( ∃ ¯ y 0 i b 0 i )) , i.e. to ¬ ( ∃ ¯ x 0 a 0 ∧ ^ i ∈ K ¬ ( ∃ ¯ y 0 i b 0 i ) ∗ ) , where V i ∈ K ¬ 5 ( ∃ ¯ y 0 i b 0 i ) ∗ is the formula V i ∈ K ¬ 5 ( ∃ ¯ y 0 i b 0 i ) in which we hav e renamed the quantified v ariables so that they satisfy the discipline of the formulas in T . The ory and Pr actic e of L o gic Pr o gr amming 45 According to the conditions of application of rule (15) and the form of the negations in the left hand side of this rule, w e c heck easily that we can fix the negations of the preceding form ula as follows ¬ 5 ( ∃ ¯ x 0 a 0 ∧ ^ i ∈ K ¬ 5 ( ∃ ¯ y 0 i b 0 i ) ∗ ) . Th us, rule (15) is correct in T . Corr e ctness of rule (16) ¬ 4 ∃ ¯ x a ∧ q ∧ ¬ 5 " ∃ ¯ y b ∧ V n i =1 ¬ 5 ( ∃ ¯ z i c i ) # = ⇒ " ¬ 4 ( ∃ ¯ x a ∧ q ∧ ¬ 5 ( ∃ ¯ y b )) ∧ V n i =1 ¬ 4 ( ∃ ¯ x ¯ y ¯ z i c i ∧ q 0 ) ∗ # with n > 0, and q 0 is the form ula q in which all the o ccurrences of ¬ k ha ve b een re- placed b y ¬ 0 . The form ula V n i =1 ¬ 4 ( ∃ ¯ x ¯ y ¯ z i c i ∧ q 0 ) ∗ is the form ula V n i =1 ¬ 4 ( ∃ ¯ x ¯ y ¯ z i c i ∧ q 0 ) in which we hav e renamed the quantified v ariables so that they satisfy the dis- cipline of the form ulas of T . The left hand side of rule (16) is equiv alen t in T to ¬ ( ∃ ¯ x a ∧ q ∧ ¬ ( ∃ ¯ y b ∧ ¬ n _ i =1 ( ∃ ¯ z i c i ))) . Since the first negation of ¬ ( ∃ ¯ y b... in the left hand side of rule (16) is of the form ¬ 5 then according to Definition 4.5.1, all the v ariables of ¯ y are reachable in ∃ ¯ y b , and th us according to Prop ert y 4.2.10 we ha ve T | = ∃ ? ¯ y b . According to Prop ert y 3.1.10, the preceden t formula is equiv alent in T to ¬ ( ∃ ¯ x a ∧ q ∧ ¬ (( ∃ ¯ y b ) ∧ ¬ ( ∃ ¯ y b ∧ n _ i =1 ( ∃ ¯ z i c i )))) . By distributing the ∧ on the ∨ and the ∃ on the ∨ and since the quan tified v ariables ha ve distinct names and different from those of the free v ariables then the preceding form ula is equiv alent in T to ¬ ( ∃ ¯ x a ∧ q ∧ ¬ (( ∃ ¯ y b ) ∧ ¬ n _ i =1 ( ∃ ¯ z i ¯ y b ∧ c i ))) , i.e. to ¬ ( ∃ ¯ x a ∧ q ∧ (( ¬ ( ∃ ¯ y b )) ∨ n _ i =1 ( ∃ ¯ z i ¯ y b ∧ c i ))) , i.e. to ¬ ( ∃ ¯ x ( a ∧ q ∧ ¬ ( ∃ ¯ y b )) ∨ n _ i =1 ( a ∧ q ∧ ( ∃ ¯ z i ¯ y b ∧ c i ))) , whic h, according to the discipline of the formulas in T (the quantified v ariables ha ve distinct names and different from those of the free v ariables), is equiv alent in 46 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth T to ¬ ( ∃ ¯ x ( a ∧ q ∧ ¬ ( ∃ ¯ y b )) ∨ n _ i =1 ( ∃ ¯ z i ¯ y a ∧ q ∧ b ∧ c i )) , i.e. to ¬ (( ∃ ¯ x a ∧ q ∧ ¬ ( ∃ ¯ y b )) ∨ n _ i =1 ( ∃ ¯ x ¯ z i ¯ y a ∧ q ∧ b ∧ c i )) , i.e. to ¬ ( ∃ ¯ x a ∧ q ∧ ¬ ( ∃ ¯ y b )) ∧ n ^ i =1 ¬ ( ∃ ¯ x ¯ y ¯ z i a ∧ q ∧ b ∧ c i ) . Since we ha ve ¬ 5 ( ∃ ¯ y b... in the left hand side of rule (16) then according to Definition 4.5.1, we hav e (i) T | = b → a . But since w e hav e also ¬ 5 ( ∃ ¯ z i c i ) for all i ∈ { 1 , ..., n } , then according to Definition 4.5.1 we hav e (ii) T | = c i → b . F rom (i) and (ii) we ha ve T | = c i → ( a ∧ b ). Thus the preceding formula is equiv alent in T to ¬ ( ∃ ¯ x a ∧ q ∧ ¬ ( ∃ ¯ y b )) ∧ n ^ i =1 ¬ ( ∃ ¯ x ¯ y ¯ z i c i ∧ q ) , i.e. to ¬ ( ∃ ¯ x a ∧ q ∧ ¬ ( ∃ ¯ y b )) ∧ n ^ i =1 ¬ ( ∃ ¯ x ¯ y ¯ z i c i ∧ q ) ∗ , where V n i =1 ¬ 4 ( ∃ ¯ x ¯ y ¯ z i c i ∧ q ) ∗ is the formula V n i =1 ¬ 4 ( ∃ ¯ x ¯ y ¯ z i c i ∧ q ) in which w e hav e renamed the quantified v ariables so that they satisfy the discipline of the formulas of T . According to the conditions of application of rule (16) and the form of the negations in the left hand side of this rule, we c heck easily that we can fix the negations of the preceding form ula as follows ¬ 4 ( ∃ ¯ x a ∧ q ∧ ¬ 5 ( ∃ ¯ y b )) ∧ n ^ i =1 ¬ 4 ( ∃ ¯ x ¯ y ¯ z i c i ∧ q 0 ) ∗ , where q 0 is the formula q in which all the o ccurrences of ¬ k ha ve b een replaced by ¬ 0 . Th us rule (16) is correct in T . Pr o of, thir d p art: Every rep eated application un til termination of the rewriting rules on an initial working form ula pro duces a conjunction of final w orking formulas. Recall that we write V i ∈ I ϕ i , and call c onjunction each formula of the form ϕ i 1 ∧ ϕ i 2 ∧ ... ∧ ϕ i n ∧ true . In particular, for I = ∅ , the conjunction V i ∈ I ϕ i is reduced to true . Moreov er, w e do not distinguish tw o form ulas which can b e made equal using the follo wing transformations of sub-formulas: ϕ ∧ ϕ = ⇒ ϕ, ϕ ∧ ψ = ⇒ ψ ∧ ϕ, ( ϕ ∧ ψ ) ∧ φ = ⇒ ϕ ∧ ( ψ ∧ φ ) , ϕ ∧ true = ⇒ ϕ, ϕ ∨ false = ⇒ ϕ. Let us show first that every substitution of a sub-working form ula of a conjunction of working formulas by a conjunction of w orking formulas pro duces a conjunction of w orking formulas. Let V i ∈ I ϕ i b e a conjunction of w orking formulas. Let ϕ k with k ∈ I b e an element of this conjunction of depth d k . Tw o cases arise: The ory and Pr actic e of L o gic Pr o gr amming 47 1. W e replace ϕ k b y a conjunction of w orking formulas. Thus, let V j ∈ J k φ j b e a con- junction of w orking form ulas whic h is equiv alen t to ϕ k in T . The conjunction of w orking formulas V i ∈ I ϕ i is equiv alen t in T to ( ^ i ∈ I −{ k } ϕ i ) ∧ ( ^ j ∈ J k φ j ) whic h is clearly a conjunction of working formulas. 2. W e replace a strict sub-w orking form ula of ϕ k b y a conjunction of working formulas. Th us, let φ b e a sub-working formula of ϕ k of depth d φ < d k (th us φ is different from ϕ k ). Th us, ϕ k has a sub-w orking formula 17 of the form ¬ ( ∃ ¯ xα ∧ ( ^ l ∈ L ψ l ) ∧ φ ) , where L is a finite (p ossibly empty) set and all the ψ l are w orking formulas. Let V j ∈ J φ j b e a conjunction of working formulas which is equiv alen t to φ in T . Th us the preceding sub-w orking formula of ϕ k is equiv alen t in T to ¬ ( ∃ ¯ xα ∧ ( ^ l ∈ L ψ l ) ∧ ( ^ j ∈ J φ j )) , whic h is clearly a sub-working formula and thus ϕ k is equiv alen t to a w orking form ula and thus V i ∈ I ϕ i is equiv alen t to a conjunction of working formulas. F rom 1 and 2 we deduce that (i) every substitution of a sub-w orking formula of a conjunction of working formulas by a conjunction of w orking formulas pro duces a conjunction of w orking formulas. Since each rule transforms a w orking formula into a conjunction of working for- m ulas, then according to the sub-section “pr o of: first p art” and (i) we deduce that ev ery rep eated application of the rewriting rules on an initial working formula ter- minates and pro duces a conjunction of working formulas. Thus, since an initial w orking formula starts by ¬ 4 and all its other negations are of the form ¬ 0 then all long the application of our rules and by going down along the nested negations of any working formula ϕ obtained after any finite application of our rules, we can build man y series of negations which represen t the paths that w e should follo w from the top negation of ϕ to reach one of the sub-working formulas of ϕ of depth equal to one. Eac h of these series is of the one of the following forms: • a series of ¬ 4 follo wed by a p ossibly series of ¬ 0 , • a series of ¬ 4 follo wed by one ¬ 1 , follo wed by a p ossibly series of ¬ 0 , • a series of ¬ 4 follo wed by one ¬ 2 , follo wed by a p ossibly series of ¬ 0 , • a series of ¬ 4 follo wed by one ¬ 3 , follo wed by a p ossibly series of ¬ 0 , • a series of ¬ 4 follo wed by one or tw o ¬ 5 , • one or tw o ¬ 5 . 17 By considering that the set of the sub-formulas of an y formula ϕ contains also the whole form ula ϕ . 48 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth While all the negations of these series are not of the form ¬ 5 or their length is greater than 2 then one of the rules (1),...,(16) can still b e applied. As a consequence, when no rule can b e applied, we obtain a conjunctions of formulas of depth less or equal to 2 in whic h all the negations are of the form ¬ 5 . It is a conjunction of final w orking form ulas. Since all the rules do not introduce new free v ariables then Property 4.6.3 holds. 4.7 The Solving Algorithm Let p b e a form ula. Solving p in T pro ceeds as follows: (1) T ransform the formula ¬ p (the negation of p) into a wnfv normalized formula p 1 equiv alen t to ¬ p in T . (2) T ransform p 1 in to the following initial working formula p 2 p 2 = ¬ 4 ( ∃ ε true ∧ ¬ 0 ( ∃ ε true ∧ p 1 )) , where all the o ccurrences of ¬ in p 1 are replaced b y ¬ 0 . (3) Apply the preceding rewriting rules on p 2 as many time as p ossible. According to Prop erty 4.6.3 w e obtain at the end a wnfv conjunction p 3 of final working form ulas of the form n ^ i =1 ¬ 5 ( ∃ ¯ x i α i ∧ n i ^ j =1 ¬ 5 ( ∃ ¯ y ij β ij )) . According to Prop ert y 4.5.4, the formula p 3 is equiv alen t in T to the follo wing wnfv conjunction p 4 of general solv ed formulas n ^ i =1 ¬ ( ∃ ¯ x i α i ∧ n i ^ j =1 ¬ ( ∃ ¯ y ij β ∗ ij )) , where β ∗ ij is the form ula β ij from whic h we hav e remov ed all the equations which o ccur also in α i . Since p 4 is equiv alen t to ¬ p in T , then p is equiv alen t in T to ¬ n ^ i =1 ¬ ( ∃ ¯ x i α i ∧ n i ^ j =1 ¬ ( ∃ ¯ y ij β ∗ ij )) , whic h is equiv alent to the following disjunction p 5 n _ i =1 ( ∃ ¯ x i α i ∧ n i ^ j =1 ¬ ( ∃ ¯ y ij β ∗ ij )) . This is the final answer of our solver to the initial constrain t p . Note that the negations which w ere at the b eginning of each general solv ed form ula of p 4 ha ve b een remov ed and the top conjunction of p 4 has b een replaced b y a disjunction. As a consequence, the set of the solutions of the free v ariables of p 5 is nothing other than the union of the solutions of eac h form ula of the form ∃ ¯ x i α i ∧ V n i j =1 ¬ ( ∃ ¯ y ij β ∗ ij ). According to Definition 4.4.6, each of these formulas is written in an explicit solved form which enables us to easily extract the solutions of its free v ariables. On the other hand, t wo cases arise: The ory and Pr actic e of L o gic Pr o gr amming 49 • If p 4 do es not contain free v ariables then according to Prop erty 4.4.3 the for- m ula p 4 is of the form V n i =1 ¬ ( ∃ ε true ) and th us p 5 is of the form W n i =1 ∃ ε true . Tw o cases arise: if n = 0 then p 5 is the empty disjunction (i.e. the form ula false ). Else, if n 6 = 0 then since we do not distinguish b etw een ϕ ∧ ϕ and ϕ , p 5 is the form ula ∃ ε true . • If p 4 con tains at least one free v ariable then according to Prop ert y 4.4.3 neither T | = p 4 nor T | = ¬ p 4 and th us neither T | = ¬ p 5 nor T | = p 5 . Since T has at least one mo del and since p 5 is equiv alent to p in T and do es not con tain news free v ariables then we hav e the following theorem: The or em 4.7.1 Ev ery formula is equiv alen t in T either to true , or to false , or to a wnfv formula whic h has at least one free v ariable, which is equiv alen t neither to true nor to false , and where the solutions of the free v ariables are expressed in a clear and explicit w ay . The fact that T accepts at least one mo del is vital in this theorem. In fact, if T do es not hav e models then the form ula true can b e equiv alen t to false in T . In other w ords, a form ula can b e equiv alen t to true in T using a finite application of our rules and equiv alent to false using another different finite application of our rules. Theorem 3.3.1 preven ts these kinds of conflicts and shows that T has at least three mo dels D , T r and R a and th us T | = ¬ ( true ↔ false ). Cor ol lary 4.7.2 T is a complete theory . Note that using Theorem 4.7.1 and the prop erties 4.4.5 and 4.2.11, we get Maher’s decision pro cedure (Maher 1988) for the basic theory of finite or infinite trees. 5 Implementation of our algorithm W e ha ve implemented our algorithm in C++ and CHR (Constrain t Handling Rules) (F rueh wirth 1998; F ruehwirth and Ab dennadher 2003; Schrijv ers and F rue- h wirth 2006). The C++ implementation is a straightforw ard extension of those giv en in (Djelloul and Dao 2006b). It uses records and pointers and releases un- used p ointers after each rule application. The CHR implemen tation w as done us- ing Christian Holzbaur’s CHR library of Sicstus Prolog 3.11.0. It consists of 18 CHR constraints and 73 CHR rules – most of them are needed for the compli- cated rules (15) and (16) of our algorithm. Even if our C++ implementation has giv en b etter p erformances, we think that it is interesting to sho w ho w can w e translate our rules into CHR rules. W e will b e able to quickly prototype optimiza- tions and v ariations of our algorithm and to parallelize it. F or CHR, the imple- men tation of this complex solv er helps to understand what programming patterns and language features can b e useful. The CHR co de without comments and ex- amples, but pretty-prin ted, is ab out 250 lines, whic h is one seven th of the size of our C++ implementation. Indeed for co de size and degree of abstraction it 50 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth seems only possible and interesting to describ e the CHR implementation, and w e do so in the following. The reader can find our full CHR implemen tation at http://khalil.djelloul.free.fr/solver.txt and can experiment with it on- line using w eb chr at http://chr.informatik.uni- ulm.de/ ~ webchr/ . 5.1 Constr aint Hand ling R ules (CHR) Implementation CHR manipulates conjunctions of constraints that reside in a constraint store. Let H , C and B denote conjunctions of constraints. A simplification rule H ⇔ C B replaces instances of the CHR constraints H b y B provided the guard test C holds. A propagation rule H ⇒ C B instead just adds B to H without removing an ything. The h ybrid simpagation rules will come handy in the implementation: H 1 \ H 2 ⇔ C B remov es matched constraints H 2 but k eeps constraints H 1 . The constraints of the store comprise the state of an execution. Starting from an arbitrary initial store (called query), CHR rules are applied exhaustively un til a fixp oint is reached. T rivial non-termination of a propagation rule application is a voided by applying it at most once to the same constraints. Almost all CHR implementations execute queries from left to right and apply rules top-down in the textual order of the program (Duc k et al. 2004). A CHR constrain t in a query can be understo o d as a pro cedure that goes efficien tly through the rules of the program. When it matches a head constraint of a rule, it will lo ok for the other constraints of the head in the constrain t store and c heck the guard. On success, it will apply the rule. The rule application cannot b e undone. If the initial constrain t has not b een remov ed after trying all rules, it will b e put into the constraint store. Constrain ts from the store will b e reconsidered if newly added constrain ts constrain its v ariables. 5.1.1 CHR Constr aints The implementation consists of 18 constrain ts: t wo main constraints that enco de the tree data structure of the working formulas (nf/4) and the atomic form ulas (of/2), 9 auxiliary constraints that p erform reachabilit y analysis, v ariable renaming and cop ying of formulas, and 7 constraints that enco de execution con trol information, mainly for rules (15) and (16). In more detail, nf(ParentId,Id,K,ExVars) describ es a n egated quan tified basic f orm ula with the identifier of its parent node, its own identifier Id, the level K from ¬ k and the list of existentially quantified v ariables. Var=FlatTerm of Id denotes an equation b et ween a v ariable and a flat term (a v ariable or a function symbol applied to v ariables) that b elongs to the negated sub-formula with the identifier Id. finite(U) of Id denotes the relation finite ( U ). It is easy to represent an y working form ula ϕ using conjunctions of nf/4 and of/2 constraints. It is enough to create one nf/4 constraint for each quan tified basic form ula of ϕ and to use a conjunction of of/2 constraints to enumerate the atomic form ulas linked to each quantified basic formula. The ory and Pr actic e of L o gic Pr o gr amming 51 Example 5.1.2 Let ϕ b e the follo wing working formula ¬ 4 ∃ u u = 1 ∧ ¬ 0 ( ∃ ε u = s ( v )) ∧ ¬ 0 ( ∃ w 1 u = s ( w 1 ) ∧ w 1 = s ( v )) ∧ ¬ 5 ( ∃ ε v = s ( u ) ∧ u = 1 ∧ ¬ 5 ( ∃ ε v = s ( u ) ∧ u = 1 ∧ finite ( w 1 )) ∧ ¬ 5 ( ∃ w 3 v = s ( u ) ∧ u = 1 ∧ w 2 = s ( w 3 ) ∧ finite ( w 3 )) ) . ϕ can b e expressed using the following conjunction of constrain ts: nf ( Q , P1 , 4 , [ U ]) , U = 1 of P1 , nf ( P1 , P2 , 0 , [ ]) , U = S ( V ) of P2 , nf ( P1 , P3 , 0 , [ W1 ]) , U = S ( W1 ) of P3 , W1 = S ( V ) of P3 , nf ( P1 , P4 , 5 , [ ]) , V = S ( U ) of P4 , U = 1 of P4 nf ( P4 , P5 , 5 , [ ]) , V = S ( U ) of P5 , U = 1 of P5 , finite ( W1 ) of P5 nf ( P4 , P6 , 5 , [ W3 ]) , V = S ( U ) of P6 , U = 1 of P6 , W2 = S ( W3 ) of P6 , finite ( W3 ) of P6 5.1.3 CHR Rules The rules (1) to (14) hav e a rather direct translation into CHR rules. It seems hard to come up with a more concise implementation. % 1 Locally simplify equations (1) @ nf(Q,P,1,Xs) \ U=U of P <=> true. (2) @ nf(Q,P,1,Xs) \ V=U of P <=> gt(U,V) | U=V of P. (3) @ nf(Q,P,1,Xs), U=V of P \ U=G of P <=> gt(U,V) | V=G of P. (4) @ nf(Q,P,1,Xs), U=F of P, U=G of P <=> notsamefunctor(F,G) | true(P). (5) @ nf(Q,P,1,Xs), U=F of P \ U=G of P <=> samefunctor(F,G) | same_args(F,G,P). (6) @ nf(Q,P,1,Xs) <=> nf(Q,P,2,Xs). % 2 finiteness check (7) @ nf(P0,P,2,Xs), finite(U) of P \ finite(U) of P <=> true. (8) @ nf(P0,P,2,Xs), U=V of P \ finite(U) of P <=> var(V) | finite(V) of P. (9+10)@nf(P0,P,2,Xs),U=T of P \ finite(U) of P <=> nonvar(T) | reach_args(U,T,P), finite_args(U,T,P). (11) @ nf(Q,P,2,Xs) <=> nf(Q,P,3,Xs). % 4/0-4/1 copy down before solving (12) @ nf(Q,P,4,Xs), A of P, nf(P,P1,0,Ys) ==> A of P1. nf(Q,P,4,Xs) \ nf(P,P1,0,Ys) <=> nf(P,P1,1,Ys). % 4/3-4/4 replace down after solving (13) @ nf(Q,P,4,Xs),U=V of P, nf(P,P1,3,Ys)\ U=G of P1 <=> V\==G | U=V of P1. nf(Q,P,4,Xs) \ nf(P,P1,3,Ys) <=> nf(P,P1,4,Ys). % 4/5-true trivial satisfaction - each A of P1 also occurs as A of P (14) @ nf(Q,P,4,Xs), nf(P,P1,5,Ys) <=> \+(findconstraint(P1,(A of P1),_), \+findconstraint(P,(A of P),_)) | true(P). Note that rules (1) to (5) are similar to the classical CHR equation solver for flat 52 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth rational trees (F ruehwirth and Ab dennadher 2003; Meister and F rueh wirth 2006). By applying results of (Meister and F ruehwirth 2006), we can show that the worst- case time complexit y of these rules of the algorithm is quadratic in the size of the equations. In the rules (2) and (3), the predicate gt(U,V) c hecks if U V . Note that the constrain t true(P) used in rule (4) remov es all constraints asso ciated with P using an auxiliary rule not sho wn. In rule (9+10) reach args(U,T,P) c hecks reachabilit y of U from itself in P . If so, true(P) will b e executed and thus P will b e remov ed, implementing rule (9). Otherwise, the subsequent finite args(U,T,P) will propagate down the finite relation from U to its argumen ts, implementing rule (10). In the rules (12) and (13) w e handle equations one b y one (due to the c hosen gran ularity of the constraints), and th us we need auxiliary second CHR rules that p erform the up date of the level K afterw ards. F or rule (14) the implementation is easy when nested negation-as-absence (V an W eert et al. 2006) is used to v erify that there is no constrain t in the sub- form ula that is not in the main form ula. Negation-as-absence can b e directly enco ded in CHR, but then it requires tw o additional rules p er negation. In- stead, w e hav e c hosen to use in the guard of the rule the CHR library built- in findconstraint(Var,Pattern,Match) that returns on bac ktracking all con- strain ts Match that match Pattern and that are indexed on v ariable Var together with negation-as-failure pro vided by the Prolog built-in \ +. The translation of the complex rules (15) and (16) of the algorithm require 40 CHR rules, because sev eral non-trivial new expressions hav e to be computed. Simp- agation rules and auxiliary constrain ts collect the nested nf/4 constraints, compute the reachable v ariables and atomic form ulas, rename the quantified v ariables and pro duce up dated nf/4 and of/2 constraints. In order not to o verburden the reader with tec hnical details, we omit the description of those 40 rules. 5.2 Benchmarks: Two p artner game Let us consider the following tw o partner game: An ordered pair ( i, j ) is giv en, with i a non-negative (possibly null) in teger and j ∈ { 0 , 1 } . One after another, each pla yer changes the v alues of i and j according to the follo wing rules • If j = 0 then the actual play er should replace i by i − 1 in the pair ( i, j ). • If j = 1 and i is o dd then the actual play er can either replace i by i + 1 or replace j b y j − 1, in the pair ( i, j ). • If j = 1 and i is even then the actual pla yer can either replace i by i + 1 and j b y j − 1 in the pair ( i, j ) or replace only i by i + 1 in the pair let ( i, j ) The first play er who cannot keep i non negative has lost. This game can be repre- sen ted by the following directed infinite graph: The ory and Pr actic e of L o gic Pr o gr amming 53 It is clear that the play er which is at the p osition (0 , 0) and should play has lost. Supp ose that it is the turn of play er A to play . A p osition ( n, m ) is called k-winning if, no matter the wa y the other pla yer B pla ys, it is alw ays p ossible for A to win, after ha ving made at most k mov es. It is easy to show that w inning k ( x ) = ∃ y mov e ( x, y ) ∧ ¬ ( ∃ x mov e ( y , x ) ∧ ¬ ( ... ∃ y mov e ( x, y ) ∧ ¬ ( ∃ x mov e ( y , x ) ∧ ¬ ( f alse ) ... ) |{z} 2 k where mov e( x, y ) means: “starting from the p osition x we play one time and reach the p osition y ”. By moving down the negations, we get an embedding of 2k alter- nated quan tifiers. Supp ose that F contains the function sym b ols 0, 1, f , g , c of resp ective arities 0, 0, 1, 1, 2. W e co de the vertices ( i, j ) of the game graph by the trees c ( ¯ i, 0) and c ( ¯ i, 1) with ¯ i = ( f g ) i/ 2 (0) if i is even, and ¯ i = g ( i − 1) if i is o dd. 18 The relation mov e ( x, y ) is then defined as follo ws: mov e ( x, y ) def ↔ tr ansition ( x, y ) ∨ ( ¬ ( ∃ uv x = c ( u, v )) ∧ x = y ) with tr ansition ( x, y ) def ↔ ∃ u 1 v 1 u 2 v 2 x = c ( u 1 , v 1 ) ∧ y = c ( u 2 , v 2 ) ∧ ( v 1 = 0 ∧ v 2 = v 1 ∧ pr ed ( u 1 , u 2 )) ∨ ( v 1 = 1 ∧ ( ∃ w u 1 = g ( w ) ∧ ( u 2 = f ( u 1 ) ∧ v 2 = v 1 ) ∨ ( u 2 = u 1 ∧ v 2 = 0) ) ∨ ( ¬ ( ∃ w u 1 = g ( w )) ∧ u 2 = g ( u 1 ) ∧ ( v 2 = v 1 ∨ v 2 = 0)) ) ∨ ( ¬ ( v 1 = 0) ∧ ¬ ( v 1 = 1) ∧ u 2 = u 1 ∧ v 2 = v 1 ) pr ed ( u 1 , u 2 ) def ↔ ( ∃ j u 1 = f ( j ) ∧ ( ∃ k j = g ( k ) ∧ u 2 = j ) ∨ ( ¬ ( ∃ k j = g ( k )) ∧ u 2 = u 1 ) ) ∨ ( ∃ j u 1 = g ( j ) ∧ ( ∃ k j = g ( k ) ∧ u 2 = u 1 ) ∨ ( ¬ ( ∃ k j = g ( k )) ∧ u 2 = j ) ) ∨ ( ¬ ( ∃ j u 1 = f ( j )) ∧ ¬ ( ∃ j u 1 = g ( j )) ∧ ¬ ( u 1 = 0) ∧ u 2 = u 1 ) 18 ( f g ) 0 ( x ) = x and ( f g ) i +1 ( x ) = f ( g (( f g ) i ( x ))). 54 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth If we take as input of our solv er the formula w inning k ( x ) then we will get as output a disjunction of simple formulas where the solutions of the free v ariable x represen t all the k -winning p ositions. F or w inning 1 ( x ) our algorithm giv es the following formula: ∃ u 1 u 2 x = c ( u 1 , u 2 ) ∧ u 1 = g ( u 2 ) ∧ u 2 = 0 , whic h corresp onds to the solution x = c ( g (0) , 0). F or w inning 2 ( x ) our algorithm giv es the following disjunction of simple formulas ( ∃ u 1 u 2 x = c ( u 1 , u 2 ) ∧ u 1 = g ( u 2 ) ∧ u 2 = 0) ∨ ( ∃ u 3 u 4 u 5 u 6 x = c ( u 3 , u 6 ) ∧ u 3 = g ( u 4 ) ∧ u 4 = f ( u 5 ) ∧ u 5 = g ( u 6 ) ∧ u 6 = 0) , whic h corresp onds to the solution x = c ( g (0) , 0) ∨ x = c ( g ( f ( g (0))) , 0). Note that x is the only free v ariable in the t w o preceding disjunctions and its solutions represen t the p ositions whic h are k -winning. The times of execution (CPU time in milliseconds) of the form ulas w inning k ( x ) are giv en in the following table as w ell as a comparison with those obtained us- ing a decision pro cedure for decomp osable theories (Djelloul 2006a) (even though the later do es not pro duce comprehensible results, i.e. explicit solved forms). The b enc hmarks are p erformed on a 2.5Ghz Pen tium IV pro cessor, with 1024Mb of RAM. The sym b ol “-” b ellow means exhausting memory . k ( w inning k ( x )) 1 2 4 5 7 10 20 40 CHR (our 16 rules) 320 690 1750 2745 5390 − − − C++ (Djelloul 2006a) 28 50 115 150 245 430 2115 − C++ (our 16 rules) 25 40 90 115 175 315 1490 15910 This decision pro cedure takes from 10% to 40% more time, comparing with our C++ implementation to solv e the w inning k ( x ) formulas of our game and ov erflows the memory for k > 20, i.e. 40 nested alternated quan tifiers. Our C++ implemen- tation has b etter p erformance and is able to give all the w inning k strategies in a clear and explicit w ay until k = 40, i.e. 80 nested alternated quantifiers. The execution times of w inning k ( x ) using our CHR implementation are 12-30 times slow er than those obtained using our C++ implemen tation and the maximal depth of working formula that can b e solved is 14 ( k = 7). These results are in line with the exp erience that the o verhead of using declarative CHR without optimisa- tions induces an ov erhead of ab out an order of magnitude ov er implementations in pro cedural languages. As discussed in the conclusions, switc hing to a more recent optimizing CHR compiler ma y close the gap to a small constant factor. The algorithm giv en in (Djelloul 2006a) is a decision pro cedure in the form of fiv e rewriting rules which for every decomp osable theory T transforms a first-order form ula ϕ in to a conjunction φ of final formulas easily transformable in to a Bo olean The ory and Pr actic e of L o gic Pr o gr amming 55 com bination of existentially quantified conjunctions of atomic formulas. This deci- sion pro cedure do es not warran t that the solutions of the free v ariables are expressed in a clear and explicit w ay and can even pro duce formulas having free v ariables but b eing alw a ys true or false in T . In fact, for our tw o pla y er game, w e got conjunctions of final formulas where the solutions of the free v ariable x was incomprehensible, esp ecially from k = 5. W e also tried to use Remark 4.4.2 of (Djelloul 2006a) which gives a wa y to get a disjunction of the form _ i ∈ I ( ∃ ¯ x 0 i α 0 i ∧ ^ j ∈ J i ¬ ( ∃ ¯ y 0 ij β 0 ij )) (36) as output of the decision pro cedure. As the author of (Djelloul 2006a) wrote: ”it is mor e e asy to understand the solutions of the fr e e variables of this disjunction of solve d formulas than those of a c onjunction of solve d formulas” . That is of course true, but this do es not mean that the solutions of the free v ariables of this formula are expressed in a clear and explicit w ay . In fact, we got a disjunction of the form (36) where many v ariables which o ccurred in left hand sides of equations of α 0 i o ccurred also in left hand sides of equations of some β 0 ij . Moreov er, many formulas of the preceding disjunction contained o ccurrences of the free v ariable x but after a hard and complex manual chec king we found them equiv alent to false . As a consequence, the solutions of x was completely not evident to understand and we could not extract clear and understandable winning k ( x ) strategies for all k ≥ 5. In order to simplify the formula (36) we finally used our solving algorithm on it and hav e got a disjunction of simple formulas equiv alent to (36) in T in which: (1) all the formulas having free o ccurrences of x but b eing alwa ys false in T hav e b een remo ved, (2) the solutions of the free v ariable x w ere expressed in a clear and explicit w ay . W e now discuss why our solver is faster than the decision pro cedure of K. Djelloul. The latter uses man y times a particular distribution (rule (5) in (Djelloul 2006a)) whic h decreases the depth of the working formulas but increases exp onentially the n umber of conjunctions of the working formulas until o verflo wing the memory . Our solving algorithm uses a similar distribution (rule (16)) but only after a necessary propagation step which copies the basic formulas into the sub-w orking form ulas and c hecks if there exists no w orking formulas which contradict their top-w orking for- m ula. This step enables us to remov e the inconsistent working form ulas and to not lose time with solving a huge w orking formulas (i.e. of big depth) whic h contradicts their top-working formulas. It also preven ts us from making exponential distribu- tions b etw een h uge inconsistent w orking formulas whic h finally are all equiv alen t to false . Unfortunately , we cannot add this propagation step to the decision procedure of (Djelloul 2006a) since it uses many prop erties which hold only for the theory of finite or infinite trees and not for an y decomp osable theory T . The game in tro duced in this pap er was inspired from those given in (Djelloul 2006a) but is differen t. Solving a winning k ( x ) form ula in this game generates man y h uge working formulas whic h contradict their top-working form ulas. Our algorithm remo ves directly these huge w orking formulas after the first propagation step (rules 56 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth (1),...,(13)). The decision pro cedure cannot detect this inconsistency and is obliged to apply a costly rule (rule (5) in (Djelloul 2006a)) to decrease the size of these inconsisten t working formulas until finding basic inconsisten t formulas of the form ¬ ( a ∧ ¬ ( ∃ ε true )) or ¬ ( ∃ ε false ∧ ϕ ). At each application of this rule, the depth of the working formulas decreases but the num b er of conjunctions increase exp onen- tially until o verflo wing the memory . This explains why for this game the decision pro cedure ov erflo ws the memory for k > 20 while our solver can compute the w inning k ( x ) strategies un til k = 40. 5.3 Benchmarks: R andom normalize d formulas W e ha ve also tested our 16 rules on randomly generated normalized formulas such that in each sub-normalized formula of the form ¬ ( ∃ ¯ x α ∧ V n i =1 ϕ i ), with the ϕ i ’s normalized form ulas and n ≥ 0, we hav e: • n is a p ositive integer randomly chosen b etw een 0 and 4. • The num b er of the atomic form ulas in the basic form ula α is randomly c hosen b et ween 1 and 8. Moreov er, the atomic form ula true o ccurs at most once in α . • The vector of v ariables and the atomic form ulas of ∃ ¯ x α are randomly gen- erated starting from a set con taining 10 v ariables, the relation finite and 6 function sym b ols: f 0 , f 1 , f 2 , g 0 , g 1 , g 2 . Eac h function sym b ol f j or g j is of arit y j with 0 ≥ j ≥ 2. The b enchmarks w ere realized on a 2.5Ghz Pen tium IV pro cessor with 1024Mb of RAM as follows: F or each integer 1 ≥ d ≥ 42 we gen- erated 10 random normalized formulas 19 of depth d , w e solv ed them and computed the av erage execution time (CPU time in milliseconds). Once again, the p erformances (time and space) of our 16 rules are impressiv e comparing with those of the decision pro cedure for decomp osable theories. d 4 8 12 22 26 41 CHR (our 16 rules) 1526 4212 16104 − − − C++ (Djelloul 2006a) 108 375 1486 18973 − − C++ (our 16 rules) 88 202 504 3552 11664 2142824 Note that for d = 42, all the normalized form ulas could not b e solved and ov er- flo wed the memory . 19 W e of course renamed the quantified variables of each randomly generated normalized formula so that it resp ects the discipline of the formulas in T The ory and Pr actic e of L o gic Pr o gr amming 57 6 Discussion and conclusion W e gav e in this pap er a first-order axiomatization of an extended theory T of finite or infinite trees, built on a signature containing not only an infinite set of function sym b ols but also a relation finite ( t ) which enables to distinguish b etw een finite or infinite trees. W e sho w ed that T has at least one model and pro ved its completeness b y giving not only a decision procedure but a full first-order constraint solv er whic h transforms any first-order constraint ϕ into an equiv alent disjunction φ of simple form ulas such that φ is either the formula true , or the formula false , or a formula ha ving at least one free v ariable, b eing equiv alen t neither to true nor to false and where the solutions of the free v ariables are expressed in a clear and explicit w ay . This algorithm detects easily form ulas that hav e free v ariables but are alw a ys true or alw ays false in T and is able to solve any first-order constraint satisfaction problem in T . Its correctness implies the completeness of T . On the other hand S. V oroby ov (V oroby ov 1996) has shown that the problem of deciding if a proposition is true or not in the theory of finite or infinite trees is non-elementary , i.e. the complexity of all algorithms solving prop ositions is not b ounded b y a tow er of p ow ers of 2 0 s (top do wn ev aluation) with a fixed height. A. Colmerauer and T. Dao (Colmerauer and Dao 2003) hav e also given a pro of of non-elemen tary complexity of solving constrain ts in this theory . As a consequence, our algorithm do es not escap e this huge complexity and the function α ( ϕ ) used to sho w the termination of our rules illustrates this result. W e implemen ted our algorithm in C++ and CHR and compared b oth p erfor- mances with those obtained using a recent decision pro cedure for decomp osable theories (Djelloul 2006a). This decision pro cedure is not able to present the solu- tions of the free v ariables in a clear and explicit wa y and o verflo ws the memory while solving normalized formulas with depth d > 40. Our C++ implementation is faster than this decision pro cedure and can solve normalized formulas of depth d = 80. This is mainly due to the fact that our algorithm uses tw o steps: (1) a top-do wn propagation of constrain ts and (2) a b ottom-up elimination of quantifiers and depth reduction of the working form ulas. In particular, the first step enables to minimize the num b er of application of costly distributions and av oids to lose time with solving h uge formulas which contradict their top-formulas. F uture implementation w ork will fo cus on our CHR implemen tation, since from previous exp erience we are confident that we can get the p erformance o verhead do wn to a small constan t factor while gaining the possibility to prototype v ariations of our algorithm in a very high level language. Switc hing to a more recen t optimizing CHR compiler from K.U. Leuven w ould most likely impro ve p erformance. W e also think that w e can minimize the use of the debated negation-as-absence (V an W eert et al. 2006) b y in tro ducing reference coun ters for the tw o main constraints. This should also giv e us the p ossibility to obtain a parallel implementation that is deriv ed from the existing one with little mo dification, similar to what has b een done for parallelizing the union-find algorithm in CHR (F ruehwirth 2005). 58 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth Ac knowledgmen ts W e thank Alain Colmerauer for our very long discussions ab out the theory of finite or infinite trees and its mo dels. Many thanks also to the anon ymous referees for their careful reading and suggestions whic h help us to impro ve this pap er. Khalil Djelloul thanks the DFG research pro ject GLOB-CON for funding and supp orting his research. Thanks also to Marc Meister and Hariolf Betz for their kind review of this article. References Ab dennadher, S. 1997. Op erational Semantics and Confluence of Constraint Propaga- tion Rules. In Pro c of the third International Conference on Principles and Practice of Constrain t Programming. LNCS 1330. Baader, F. and Nipko w, T. 1998. T erm rewriting and all that. Cambridge univ ersity press. ISBN 0-521-45520-0. Benhamou, F., Colmerauer, A., Garetta, H., Pasero, R. and V an-caneghem, M. 1996. Le man uel de Prolog IV. PrologIA, Marseille, F rance. Burc kert, H. 1988. Solving disequations in equational theories. In Pro ceeding of the 9th Conference on Automated Deduction, LNCS 310, pp. 517–526, Springer-V erlag. Clark, K.L. 1978. Negation as failure. In Logic and Data bases. Ed Gallaire, H. and Minker, J. Plenum Pub. Colmerauer, A. 1982. Prolog and infinite trees. In K.L. Clark and S-A. T arnlund, editors, Logic Programming. Academic Press. pp. 231–251. Colmerauer, A., Kanoui, H. and V an-caneghem,M. 1983. Prolog, Theoretical Basis and Curren t Developmen ts. TSI (T echnology and Science of Informatics), 2(4):271–311. Colmerauer, A. 1984. Equations and inequations on finite and infinite trees. Pro ceeding of the International conference on the fifth generation of computer systems, pp. 85–99. Colmerauer, A. 1990. An introduction to Prolog I I I. Communication of the AC M, 33(7):68–90. Colmerauer, A. and Dao, T. 2003. Expressiv eness of full first-order form ulas in the algebra of finite or infinite trees, Constraints, 8(3): 283–302. Comon, H. 1988. Unification et disunification : Theorie et applications. PhD thesis, Institut National Polytec hnique de Grenoble. Comon, H. and Lescanne, P . 1989. Equational problems and disunification. Journal of Sym b olic Computation, 7: 371–425. Comon, H. 1991a. Disunification: a survey . In J.L. Lassez and G. Plotkin, editors, Com- putational Logic: Essays in Honor of Alan Robinson. MIT Press. Comon, H. 1991b. Resolution de con traintes dans des algebres de termes. Rapp ort d’Habilitation, Universite de Paris Sud. Courcelle, B. 1983. F undamental Prop erties of Infinite T rees, Theoretical Computer Sci- ence, 25(2):95–169. Courcelle, B. 1986. Equiv alences and T ransformations of Regular Systems applications to Program Schemes and Grammars, Theoretical Computer Science, 42: 100–122. Dao, T. 2000. Resolution de con traintes du premier ordre dans la theorie des arbres finis ou infinis. These d’informatique, Universite de la mediterranee, F rance. Djelloul, K. 2006a. Decomp osable Theories. Journal of Theory and practice of Logic Pro- gramming. (to app ear) Djelloul, K. and Dao, T. 2006b. Solving First-Order formulas in the Theory of Finite or Infinite T rees : Introduction to the Decomposable Theories. Proceeding of the 21st A CM Symp osium on Applied Computing (SAC’06). ACM press, pp. 7–14. The ory and Pr actic e of L o gic Pr o gr amming 59 Duc k, G., Stuck ey , P ., Banda, M. and Holzbaur, C. 2004. The Refined Op erational Se- man tics of Constraint Handling Rules. In Pro c of the 20th International Conference on Logic Programming. LNCS 3132, pp. 105-119. F ruehwirth, T. 1998. Theory and Practice of Constraint Handling Rules. Sp ecial Issue on Constrain t Logic Programming. Journal of Logic Programming. 37(1–3): 95-138. F ruehwirth, T. and Ab dennadher, S. 2003. Essen tials of Constraint Programming. Springer. F ruehwirth, T. 2005. Parallelizing Union-Find in Constraint Handling Rules Using Con- fluence. In pro c of the 21st International Conference of Logic Programming. LNCS, V ol 3668. pp: 113-127. Herbrand, J. 1930. Recherc hes sur la theorie de la demonstration. PhD thesis, Universite de Paris, F rance. Huet, G. 1976. Resolution d’equations dans les langages d’ordre 1, 2,. . . ω . These d’Etat, Univ ersite Paris 7. F rance. Jaffar, J. 1984. Efficient unification ov er infinite terms. New Generation Computing, 2(3): 207–219. John, E. and Ullman, D. 1979. Introduction to automata theory , languages and computa- tion. Addison-W esley publishing company . Jouannaud, J.P . and Kirchner, C. 1991. Solving Equations in Abstract Algebras: A Rule- Based Survey of Unification. Computational Logic - Essays in Honor of Alan Robinson, MIT press, pp: 257-321. Kunen, K. 1987. Negation in logic programming. Journal of Logic Programming, 4: 289– 308. Lassez, J., Maher, M. and Marriott, K. 1986. Unification revisited. In pro ceedings of the w orkshop on the foundations of deductiv e database and logic programming, pp. 587-625. Lassez, J. and Marriott, K. 1987. Explicit representation of terms defined by counter examples. Journal of automated reasonning. 3:301-317. Lassez, J. and McAlo on, K. 1989. Indep endence of negative constraints. In pro ceedings of TOPSOFT, LNCS 351, pp. 19-27. Lyndon, R.C. 1964. Notes on logic. V an Nostrand Mathematical studies. Maher, M. 1988. Complete axiomatization of the algebra of finite, rational and infinite trees. T echnical rep ort, IBM - T.J.W atson Research Cen ter. Maher, M. and Stuck ey , P . 1995. On inductive inference of cyclic structures. Annals of mathematics and artificial intelligence, 15(2):167-208. Malcev, A. 1971. Axiomatizable classes of locally free algebras of v arious t yp es. In B.W ells I II, editor, The Metamathematics of Algebraic Systems. Anatolii Iv anovic Malcev. Col- lected Papers: 1936-1967, volume 66, chapter 23, pp. 262–281. Martelli, A. and Mon tanari, U. 1982. An efficient unification algorithm. ACM T rans. on Languages and Systems, 4(2): 258–282. Meister, M. and F ruehwirth, T. 2006. Complexity of the CHR Rational T ree Equation Solv er. In Pro c of the third W orkshop on Constraint Handling Rules. P aterson, M. and W egman, N. 1978. Linear unification. Journal of Computer and Systems Science, 16:158–167. P o delski, A. and V an Roy , P . 1994. The b eauty and b east algorithm : quasi-linear incre- men tal tests of en tailment and disen tailment o ver trees. In proc of the 1994 In ternational Symp osium on Logic Programming. MIT press, pp. 359-374. Ramac handran, V. and V an Hen tenryc k, P . 1993. Incremen tal algorithms for form ula solv- ing and entailmen t ov er rational trees. Pro ceeding of the 13th Conference F oundations of Softw are T echnology and Theoretical Computer Science, LNCS volume 761, pp. 205– 217. 60 Khalil Djel loul, Thi-Bich-Hanh Dao and Thom F r¨ uhwirth Robinson, J.A. 1965. A machine-orien ted logic based on the resolution principle. JACM, 12(1):23–41. Rybina, T. and V oronko v, A. 2001. A decision pro cedure for term algebras with queues. A CM transaction on computational logic. 2(2): 155-181. Sc hrijvers, T., Demo en, B., Duck, G., Stuck ey , P . and F ruehwirth, T. 2006. Automatic implication chec king for CHR constrain ts. In Pro c of the 6th International W orkshop on Rule-Based Programming. ENTC, vol 147, pp. 93-111. Sc hrijvers, T. and F ruehwirth. CHR W ebsite, www.cs.kuleuven.ac.be/ ~ dtai/projects/ CHR/ Smith, A. 1991. Constraint op erations for CLP . In Logic Programming: Proceedings of the 8th International Conference. Paris. pp. 760–774. V an W eert, P ., Sney ers, J., Schrijv ers, T. and Demo en, B. 2006. Constrain t Handling Rules with Negations as Absence. In Pro c of the third W orkshop on Constraint Handling Rules. V oroby ov, S. 1996. An Improv ed Low er Bound for the Elementary Theories of T rees, Pro ceeding of the 13th International Conference on Automated Deduction (CADE’96). Springer Lecture Notes in Artificial Intelligence, vol 1104, pp. 275– 287.
Original Paper
Loading high-quality paper...
Comments & Academic Discussion
Loading comments...
Leave a Comment