The Ciao Modular, Standalone Compiler and Its Generic Program Processing Library

16

Transcript of The Ciao Modular, Standalone Compiler and Its Generic Program Processing Library

Electronic Notes in Theoretical Computer Science 30 No. 2 (2000)URL: http://www.elsevier.nl/locate/entcs/volume30.html 16 pagesRules and Strategies for ContextualSpecialization of Constraint Logic ProgramsFabio Fioravanti,IASI-CNR, Viale Manzoni 30, I-00185 Roma, ItalyAlberto Pettorossi,DISP, University of Roma Tor Vergata, I-00133 Roma, ItalyMaurizio ProiettiIASI-CNR, Viale Manzoni 30, I-00185 Roma, ItalyAbstractWe address the problem of specializing a constraint logic program w.r.t. a con-strained atom which speci�es the context of use of the program. We follow anapproach based on transformation rules and strategies. We introduce a novel trans-formation rule, called contextual constraint replacement, to be combined with vari-ants of the traditional unfolding and folding rules. We present a general PartialEvaluation Strategy for automating the application of these rules, and two addi-tional strategies: the Context Propagation Strategy which is instrumental for theapplication of our contextual constraint replacement rule, and the Invariant Promo-tion Strategy for taking advantage of invariance properties of the computation. Weshow through some examples the power of our method and we compare it with exist-ing methods for partial deduction of constraint logic programs based on extensionsof Lloyd and Shepherdson's approach.1 IntroductionThe goal of program specialization is the automatic adaptation of a program tothe context where it is used. Several program transformation techniques havebeen proposed in the literature for achieving program specialization. One ofthe most well known among these techniques is partial evaluation [9]. In thispaper we consider constraint logic programs [7] over a domain D (CLP(D), forshort). For this class of programs we assume that the context of use is givenby a constrained atom, that is, a conjunction c;A, where c is a constraint overD and A is an atom.c 2000 Published by Elsevier Science B. V.

Fioravanti, Pettorossi, and ProiettiWe address the problem of contextual specialization w.r.t. a constrainedatom which is de�ned as follows: Given a CLP(D) program P and a con-strained atom c;A, we want to derive a program Ps and an atom As suchthat, for every variable assignment � that assigns to each variable an elementof D, we have that:lm(P ;D) j=� c;A i� lm(Ps;D) j=� c;As (1: Contextual Specialization)where, for any program P and domain D, lm(P ;D) denotes the least D-modelof P [8].Thus, contextual specialization is more general than standard specializa-tion performed via partial evaluation [10,21]. Indeed, in the case of partialevaluation, given a CLP(D) program P and a constrained atom c;A, we derivea program Ppe and an atom Ape such that, for every variable assignment �,we have that:lm(P ;D) j=� c;A i� lm(Ppe ;D) j=� Ape (2: Partial Evaluation)Notice that every solution (Ppe; Ape) of the partial evaluation problem is alsoa solution of the contextual specialization problem, that is, it satis�es thecontextual specialization equivalence (1) when substituted for (Ps; As).To illustrate the di�erence between contextual specialization of CLP pro-grams and partial evaluation, let us consider the following CLP(R) programP over the domain R of real numbers:p(X) X�0; q(X) (Program P )p(X) X�0; r(X)p(X) X�3; u(X)where q, r, and u are predicates that do not depend on p.By contextual specialization of P w.r.t. the constrained atom X�3; p(X) wederive the following program Ps:1. ps(X) q(X) (Program Ps)2. ps(X) X=3; u(X)together with the atom ps(X). On the other hand, by partial evaluation of Pw.r.t. the same constrained atom X�3; p(X) we get the program Ppe:3. ppe(X) X�3; q(X) (Program Ppe)4. ppe(X) X=3; u(X)together with the atom ppe(X).Notice that in clause 1 of program Ps the constraintX�3 does not occur while2

Fioravanti, Pettorossi, and Proiettiit occurs in clause 3 of program Ppe. In fact, for every variable assignment�, lm(P;R) j=� X � 3; p(X) i� lm(Ps;R) j=� X � 3; ps(X). In contrast,for every � such that �(X) = 2 and lm(P;R) j=� q(X), it is not true that:lm(P;R) j=� X�3; p(X) i� lm(Ps;R) j=� ps(X). Thus, (Ps; ps(X)) does notsatisfy the partial evaluation equivalence (2) when substituted for (Ppe; Ape).The technique for specializing CLP programs which we present in thispaper, follows the approach based on transformation rules and strategies. Thisapproach has the advantage of separating the issue of proving the correctnessof the program specialization process from that of guaranteeing the e�ciencyof the specialized programs.The plan of the paper is as follows. In Section 2 we present a reconstructionof the partial evaluation of CLP programs by means of transformation rulesand strategies, similarly to what has been done in [17] for the case of partialevaluation of de�nite logic programs. To this end, we consider variants of theunfolding and folding rules for CLP programs presented in [1,4,13].In Section 3 we present an extra rule, called contextual constraint replace-ment, and an extra strategy, called context propagation strategy, which allowus to specialize a set of clauses de�ning a predicate, say p, with respect to aconstraint when this constraint is known to hold for every call of p. As wewill see, this contextual constraint replacement rule may indeed be applied toderive the specialized program Ps we have presented above.In Section 4 we introduce the so called invariant promotion strategy foravoiding redundant checks of constraint satis�ability, and reducing the numberof arguments of predicates. In Section 5 we compare our contextual special-ization technique to related work in the �eld of partial deduction [10,21] andspecialization of programs w.r.t. properties of their context of use [3,19].2 Rules and Strategies for Partial Evaluation of CLPIn this section we introduce a set of transformation rules and a strategy forpartial evaluation of CLP programs over a domain D. Our rules di�er fromthose studied in previous papers [1,4,13] in that they are tailored to partialevaluation.Throughout the paper we will use the following conventions. FV (') de-notes the set of the free variables of the formula '. We will write 8X1 : : :Xm also as 8X , where X denotes the set fX1; : : : ;Xmg. Analogously for 9, in-stead of 8. Similarly, we will write the atom p(t1; : : : ; tm) also as p(t) where tdenotes the sequence t1; : : : ; tm of terms. Constraints over the domain D aredenoted by the letters c; d; : : : (possibly with subscripts). Atoms are denotedby the letters A;B; : : : ;H; : : : (possibly with subscripts).3

Fioravanti, Pettorossi, and Proietti2.1 Transformation Rules for Partial EvaluationThe process of partially evaluating program P whereby deriving program Ppe,can be formalized as a sequence P0; : : : ; Pn of programs, called a transforma-tion sequence, where P0=P; Pn=Ppe and, for k = 0; : : : ; n� 1, program Pk+1is obtained from program Pk by applying one of the following transformationrules: (R1) constrained atomic de�nition, (R2) unfolding, (R3) constrainedatomic folding, (R4) clause removal, (R5) constraint replacement, and (R6)equality introduction and equality elimination.R1. Constrained Atomic De�nition. By constrained atomic de�nition(or de�nition, for short), we introduce the new clause� : newp(X1; : : : ;Xh) c; p(t1; : : : ; tm)which is said to be a de�nition, where: (i) newp is a predicate symbol notoccurring in P0; : : : ; Pk, (ii) X1; : : : ;Xh are distinct variables occurring inc; p(t1; : : : ; tm), and (iii) p is a predicate symbol occurring in P0. From programPk we derive the new program Pk+1 = Pk [ f�g.For i � 0, Defi denotes the set of de�nitions introduced during the transfor-mation sequence P0; : : : ; Pi.R2. Unfolding. Let : A c;A1; : : : ; Ai; : : : ; An be a renamed apartclause of Pk and let fBj dj ; Bj1; : : : ; Bjnj j j = 1; : : : ;mg be the set ofall clauses in Pk such that Ai and Bj have the same predicate symbol. Forj = 1; : : : ;m; let us consider the clause j : A c; Ai=Bj; dj; A1; : : : ; Ai�1; Bj1; : : : ; Bjnj ; Ai+1; : : : ; Anwhere Ai =Bj stands for the conjunction of the equalities between the cor-responding arguments. Then, by unfolding clause w.r.t. the atom Ai, wederive the new program Pk+1 = (Pk � f g) [ f j j j = 1; : : : ;mg.R3. Constrained Atomic Folding. Let : A c; d;A1; : : : ; Ai�1; p(t);Ai+1; : : : ; An where t is a sequence of terms, be a clause of Pk. Let � :newp(X ) d ; p(t), where X is a sequence of distinct variables, be a vari-ant of a clause in Defk. Suppose that each variable in FV (�)�X does notoccur in FV (A; c;A1; : : : ; Ai�1; Ai+1; : : : ; An). By folding clause w.r.t. theconstrained atom d; p(t) by using �, we derive the new clause' : A c;A1; : : : ; Ai�1;newp(X); Ai+1; : : : ; Anand we derive the new program Pk+1 = (Pk � f g) [ f'g.R4. Clause Removal. Let : A c; body be a clause of Pk. If D j= 8X :c,where X = FV (c), then we say that the constraint c is unsatis�able and wederive the new program Pk+1 = Pk � f g.R5. Constraint Replacement. Let : A c1; body be a renamedapart clause of Pk. Assume that D j= 8X (9Y c1 $ 9Z c2) for some con-straint c2, where Y = FV (c1)�FV (A; body), Z = FV (c2)�FV (A; body),4

Fioravanti, Pettorossi, and Proiettiand X = FV (c1; c2)� (Y [ Z). Then we derive the new program Pk+1 =(Pk � f g) [ fA c2; bodyg.R6. Equality Introduction and Equality Elimination. Let us considerthe clauses 1 : A body1 and 2 : B (X = t); body2, such that X doesnot occur in t, and (A body1) = (B body2)fX=tg.If 1 2 Pk, by equality introduction we derive 2 from 1, and we derivePk+1 = (Pk � f 1g) [ f 2g.If 2 2 Pk, by equality elimination we derive 1 from 2, and we derivePk+1 = (Pk � f 2g) [ f 1g.2.2 The Partial Evaluation StrategyWe now describe a general strategy for deriving e�cient CLP programs bypartial evaluation. Our strategy is a generalization of the strategies for thepartial evaluation of de�nite logic programs using unfold/fold transformationrules presented in [6,18,20]. In our Partial Evaluation Strategy the unfoldingsteps are performed according to a slightly modi�ed version of the determinateunfolding strategy proposed in [5].Partial Evaluation StrategyInput : A CLP(D) program P and a constrained atom c;A.Output : A CLP(D) program Ppe and an atom Ape of the form ppe(X1; : : : ;Xh).Initialization:Def := fppe(X1 ; : : : ;Xh) c;Ag where fX1; : : : ;Xhg = FV (c;A);NewDef := Def ;while NewDef 6= fg do(i) (unfolding)Unfold each clause in NewDef w.r.t. the leftmost atom in its body. Applyzero or more times the clause removal rule and the constraint replacementrule. Let � be the resulting set of clauses derived from NewDef .whilethere is a clause in � such that by applying the unfolding rule w.r.t. anatom A in the body of , we get at most one clause whose constraintis satis�abledounfold w.r.t. A and then apply zero or more times the clause removal,constraint replacement, equality introduction, and equality eliminationrules. Let �u be the resulting set of clauses derived from (�u is eitherempty or a singleton). � := (�� f g) [ �u.end(ii) (definition)Assume that every clause in � is of the form:H d0; d1; : : : ; dk; A1; : : : ; Ak 5

Fioravanti, Pettorossi, and Proietti(where for some i = 0; : : : ; k, the constraint di may be true).Apply the de�nition rule and introduce a set NewDef of new de�nitions,satisfying the following condition: for all i = 1; : : : ; k, there exists a clause� : K di; Ai in Def [NewDef such that we can fold using �. NewDefmay be empty.Def := Def [ NewDef .(iii) (full folding)For each constrained atom di; Ai occurring in the body of a clause in�, perform a folding step by using a clause in Def .endThe assumption at the beginning of Step (ii) can be realized by suitable ap-plications of the constraint replacement, equality introduction, and equalityelimination rules at the end of Step (i) of our Partial Evaluation Strategy.These applications are nondeterministic, and di�erent choices may lead to dif-ferent applications of the de�nition and folding rules in Steps (ii) and (iii),respectively. To illustrate this point, let us assume that during Step (i) wehave � = f g and Def = f�1; �2g, where : r(X) X�1; p(X); q(X)�1: newp(X) X�1; p(X)�2: newq0(X) X�0; q(X)Now we have, among others, the following three alternative ways of applyingour strategy:(A) (Step (i), continued) modify � by applying the constraint replacementrule to clause , thereby obtaining clause � :r(X) X�1;X�0; p(X); q(X)(Step (ii)) do nothing (NewDef is empty and thus the Partial EvaluationStrategy will terminate), and(Step (iii)) perform the full folding of � by using �1 and �2, therebyobtaining: r(X) newp(X); newq0(X)(B) (Step (i), continued) derive � as above,(Step (ii)) introduce the new de�nition �3: newq (X) q(X), so thatNewDef = f�3g, and(Step (iii)) perform the full folding of � by using �1 and �3, therebyobtaining: r(X) X�0; newp(X); newq (X) (when folding using clause�3 the constrained atom is: true; q(X))(C) (Step (i), continued) do nothing,(Step (ii)) introduce the new de�nition �3: newq (X) q(X), so thatNewDef = f�3g, and(Step (iii)) perform the full folding of by using �1 and �3, therebyobtaining: r(X) newp(X); newq (X).We will not discuss here how to choose among di�erent alternative ways of6

Fioravanti, Pettorossi, and Proiettiapplying our strategy in order to derive more e�cient residual programs.Theorem 1. (Correctness of the Partial Evaluation Strategy). LetP be a CLP(D) program, and c;A be a constrained atom. Let us consider atransformation sequence P; : : : ; Ppe constructed according to the Partial Eval-uation Strategy. Let us also consider the atom ppe(X1; : : : ;Xh) introducedduring the Initialization phase of the Partial Evaluation Strategy. Then, forevery variable assignment � we have that:lm(P;D) j=� c;A i� lm(Ppe;D) j=� ppe(X1; : : : ;Xh)and thus, lm(P;D) j=� c;A i� lm(Ppe;D) j=� c; ppe(X1; : : : ;Xh):Now we present an example of application of our Partial Evaluation Strat-egy. Let us consider the following CLP(R) program SumMatch:(Program SumMatch)summatch(P; S) L1=L2; R1=R2;length(P;L1); sum(P;R1);pre�x (G;S); length (G;L2); sum(G;R2)summatch(P; [CjS]) C�0; summatch(P; S)sum([ ]; A) A=0sum([AjB]; C) A�0; C�0; C=A+D; sum(B;D)length ([ ]; A) A=0length ([AjB]; C) A�0; C>0; C=D+1; length(B;D)pre�x ([ ]; A) pre�x ([AjB]; [CjD]) A=C; pre�x (B;D)Thus, summatch(P; S) holds i� there exists a substring G of S such that: (i)G and P have equal length, and (ii) the sum of the elements of G is equalto the sum of the elements of P . We want to partially evaluate the programSumMatch w.r.t. the constrained atom:A+B+C � 5; A�0; B�0; C�0; summatch([A;B;C]; S)In what follows we will write k(A;B;C) to denote the constraint A+B+C �5; A � 0; B � 0; C � 0. According to the Partial Evaluation Strategy weintroduce the following de�nition:summatchpe(A;B;C; S) k(A;B;C); summatch([A;B;C]; S):The program derived by the Partial Evaluation Strategy is SumMatchpe:(Program SumMatchpe)1. summatchpe(A;B;C; [D;E;F jS]) k(A;B;C);A+B+C = D+E+F;D�0; E�0; F �02. summatchpe(A;B;C; [DjS]) D�0; summatchpe(A;B;C; S)7

Fioravanti, Pettorossi, and Proietti3 Contextual Constraint ReplacementWe now present an extra transformation rule which allows us to derive verye�cient specialized programs by exploiting the knowledge of the contexts inwhich programs are used. As already mentioned, the notion of context isde�ned as a constrained atom. Below, in rule R7 and in Theorem 2, we gen-eralize this notion and consider a context to be de�ned as a set of constrainedatoms.R7. Contextual Constraint Replacement. Let be a renamed apartclause in P of the form p(t) c1; body, where t is a sequence of terms. LetC be a set of constrained atoms. Suppose that for some constraint c2 and forevery c; p(u) in C, D j= 8X ((c; u= t) ! (9Y c1 $ 9Z c2))where Y = FV (c1)�FV (t; body), Z = FV (c2)�FV (t; body), and X = FV (c; u;t; c1; c2)�(Y [ Z). Then, we derive program Q from program P by replacingclause by the clause p(t) c2; body. In this case we say that Q has beenderived from P by contextual constraint replacement w.r.t. C.This rule R7 is a generalization of rule R5.Theorem 2. (Correctness of Contextual Constraint Replacement).Let us assume that from a CLP(D) program P we derive a program Q bycontextual constraint replacement w.r.t. a set C of constrained atoms. As-sume also that C includes the set of all constrained atoms c;A such thatH c;A1; : : : ; An is a clause of P and A is Ai for some i = 1; : : : ; n.Then, for every constrained atom c;A in C and for every variable assignment� we have that: lm(P;D) j=� c;A i� lm(Q;D) j=� c;A:An application of rule R7 allows us to derive program Ps from programPpe (see Section 1) because, by the above Theorem 2, correctness is preservedfor C = f(X�3; p(X)); (X�3; q(X)); (X=3; u(X))g.Now we give another example of application of rule R7. By applying ruleR7 we will improve the program SumMatchpe derived by partial evaluation atthe end of the previous section. By the correctness of the Partial EvaluationStrategy we have that, for every variable assignment �,lm(SumMatch;D) j=� k(A;B;C); summatch([A;B;C]; S) i�lm(SumMatchpe;D) j=� k(A;B;C); summatchpe(A;B;C; S):At this point we would like to apply rule R7 for performing contextual spe-cialization of program SumMatchpe w.r.t. the constrained atom k(A;B;C);summatchpe(A;B;C; S), and in particular, in the body of clause 1 we would8

Fioravanti, Pettorossi, and Proiettilike to replace the constraint k(A;B;C) by true. Unfortunately, the applica-tion of rule R7 is not guaranteed to preserve correctness because the set C forSumMatchpe contains the constrained atom D � 0; summatchpe(A;B;C; S)and it is not the case that:R j= 8A;B;C;D (D�0 ! (k(A;B;C)$ true)):To avoid this di�culty, we apply the so called Context Propagation Strategywhich works by exploiting the information of the context where the programis used. In our case we proceed as follows.We introduce the following de�nition for the new predicate summatchc:summatchc(A;B;C; S) k(A;B;C); summatchpe(A;B;C; S)By unfolding it w.r.t. summatchpe(A;B;C; S) we get (here and in the sequelwe omit to indicate the applications of the equality introduction and equalityelimination rules):3. summatchc(A;B;C; [D;E;F jS]) k(A;B;C);A+B+C = D+E+F;D�0; E�0; F �04. summatchc(A;B;C; [DjS]) k(A;B;C); D�0;summatchpe(A;B;C; S)By applying rule R5 we replace clause 4 by:summatchc(A;B;C; [DjS]) k(A;B;C); k(A;B;C);D�0; summatchpe(A;B;C; S)This clause can be folded w.r.t. the underlined atoms, and we get:5. summatchc(A;B;C; [DjS]) k(A;B;C); D�0; summatchc(A;B;C; S)In the derived program consisting of clauses 3 and 5, we can apply twice ruleR7 with the objective of replacing k(A;B;C) by true. These applications ofrule R7 produce the following program SumMatchc:(Program SumMatchc)6. summatchc(A;B;C; [D;E;F jS]) A+B+C = D+E+F;D�0; E�0; F �07. summatchc(A;B;C; [DjS]) D�0; summatchc(A;B;C; S)The application of rule R7 preserves correctness because the set C includesfor the predicate summatchc the following two constrained atoms only:(i) k(A;B;C); summatchc(A;B;C; S), and(ii) k(A;B;C);D�0; summatchc(A;B;C; S),and for these two constrained atoms we have that:(i) R j= 8A;B;C;D (k(A;B;C) ! (k(A;B;C)$ true))(ii) R j= 8A;B;C;D ((k(A;B;C); D�0) ! (k(A;B;C)$ true)).9

Fioravanti, Pettorossi, and Proietti4 Avoiding Redundant Constraints and ArgumentsIn this section we introduce a strategy, called Invariant Promotion Strategy, foravoiding redundant checks of constraint satis�ability and reducing the numberof arguments of predicates. We will see this strategy in action by applyingit to the program SumMatchc made out of clauses 6 and 7, which has beenderived at the end of the previous section.The improvement realized by our strategy consists in avoiding the re-computation of the expression A+B +C depending on arguments whosevalue does not change during the evaluation of di�erent calls of the predi-cate summatchc(A;B;C; S). In this sense we say that A+B+C is an invariantfor summatchc(A;B;C; S).We start o� by introducing the following de�nition:8. summatch i(A;B;C; S) summatchc(A;B;C; S)By constraint replacement, de�nition, and folding we derive from clause 8 thefollowing two clauses (see Appendix B):9. summatch i(A;B;C; S) I = A+B+C; newp(A;B;C; S; I)10. newp(A;B;C; S; I) I = A+B+C; summatchc(A;B;C; S)where in the variable I we store the value of the invariant expression A+B+C,and in order to make this value available at every call of newp, I is includedamong the arguments of newp .We now derive a recursive de�nition for the predicate newp as follows. Byunfolding clause 10 w.r.t. summatchc(A;B;C; S), we get:11: newp(A;B;C; [D;E;F jS]; I) I = A+B+C; A+B+C = D+E+F;D�0; E�0; F �012. newp(A;B;C; [DjS]; I) I = A+B+C; D�0; summatchc(A;B;C; S)By rule R5 we replace clauses 11 and 12 by the following ones:13. newp(A;B;C; [D;E;F jS]; I) I = A+B+C; I = D+E+F;D�0; E�0; F �014. newp(A;B;C; [DjS]; I) I = A+B+C; I = A+B+C;D�0; summatchc(A;B;C; S)By folding clause 14 w.r.t. the underlined atoms we get:15. newp(A;B;C; [DjS]; I) I = A+B+C; D�0; newp(A;B;C; S; I)Let us now consider the derived program for summatch i which consists of theclauses 9, 13, and 15.Since in the derived program, for the predicate newp the set C contains thetwo following constrained atoms only: (i) I = A+B+C; newp(A;B;C; S; I),and (ii) I = A+B+C; D�0; newp(A;B;C; S; I), we may apply to clauses 13and 15 the contextual clause replacement rule, and we get:10

Fioravanti, Pettorossi, and Proietti16. newp(A;B;C; [D;E;F jS]; I) I = D+E+F; D�0; E�0; F �017. newp(A;B;C; [DjS]; I) D�0; newp(A;B;C; S; I)The program we have derived so far consists of the clauses 9, 16, and 17. ByTheorems 1 and 2 we have that:lm(SumMatch c;D) j= summatchc(A;B;C; S) i�lm(f9; 16; 17g;D) j= summatch i(A;B;C; S).Finally, we perform our last improvement by eliminating the �rst three argu-ments of the predicate newp(A;B;C; S; I) because they do not change betweenany two calls of newp. The �nal program SumMatch i is as follows:(Program SumMatchi)9.1 summatch i(A;B;C; S) I = A+B+C; newp1(S; I)16.1 newp1([D;E;F jS]; I) I = D+E+F; D�0; E�0; F �017.1 newp1([DjS]; I) D�0; newp1(S; I)The property that the �rst three arguments of newp(A;B;C; S; I) are indeedredundant, may be discovered by a suitable program analysis (such as theredundant argument �ltering technique presented in [11]). This property mayalso be proved by showing the equivalence:lm(f16;17;16:1;17:1g;D) j=8A;B;C; S; I (newp(A;B;C; S; I)$ newp1(S; I))which, in turn, may easily be shown by using the unfold/fold proof method[16].5 Related Work and ConclusionsWe have presented some transformation rules and strategies for the specializa-tion of constraint logic programs by taking into account their context of use.Our method extends related techniques for the partial evaluation of logic pro-grams because the context of use is speci�ed by means of a constraint whichmay be satis�ed by a possibly in�nite set of values, instead of a single value.Indeed, our specialization method belongs to a family of methods whichcan be used for the specialization of programs w.r.t. properties of their contextof use. Among these methods we would like to mention: (i) the methods forspecializing logic programs presented in [2,15], (ii) the method for multiplespecialization of logic programs presented in [19], (iii) the method for reducingthe workload of the constraint solving engine presented in [14], and (iv) themethod for parameterized partial evaluation of functional programs presentedin [3].In [2,15] the authors present very general approaches to the specializationof de�nite logic programs w.r.t. properties which, however, do not seem tobe amenable to full mechanization. In particular, most of the basic trans-11

Fioravanti, Pettorossi, and Proiettiformation rules introduced in [2] cannot be easily automated because theirapplicability conditions depend on undecidable properties. In this paper wehave considered the class of CLP programs and we have described the prop-erties of their context of use by means of constraints, instead of predicatesde�ned by logic programs. By doing so, we were able to consider more e�ec-tive transformation rules, whose applicability conditions are decidable, if weassume that the constraint theory is decidable, as it is often the case.A more practical approach is presented in [19], where a technique for theimplementation of multiple specialization is proposed. This technique pro-duces a polyvariant specialized program which contains di�erent, optimizedversions of the same predicate for di�erent uses.The aim of the work presented in [14] is to transform a standard CLPprogram into a non-monotonic CLP program, that is, a program in whichaddition of the constraints to the store is delayed until it becomes really nec-essary and constraints are removed from the store as soon as they becomeredundant. This approach requires a more advanced form of interaction withthe constraint solver as it should be able to deal with removal instructions.Indeed, most constraint solvers provide only primitives for testing the satis�-ability and the entailment of constraints.The two techniques described in [14,19] perform global analysis of theprograms based on abstract interpretation.The work in [3] is concerned with a strict �rst order functional program-ming language and the main tool for specifying a property of the contextw.r.t. which a program has to be specialized, is based on abstract interpreta-tion.Our transformation rules are intended for use within the program spe-cialization techniques and, in this respect, they are more speci�c than thetransformation rules for CLP programs considered in [1,4,13]. Moreover, wehave an extra rule, the contextual constraint replacement rule, which allowsus to replace a constraint c occurring in the body of a clause, by another con-straint which is equivalent to c in the given context. For instance, considerthe CLP(R) program P0:p(X) X�0; q(X) (Program P0)where q is a predicate that does not depend on p. By contextual constraintreplacement w.r.t. the constrained atom X�3; p(X), we may derive P1:p(X) q(X) (Program P1)because X � 0 is true in the context where X � 3 is true. Notice that P1cannot be obtained by the usual unfold/fold rules, because P0 and P1 mayhave di�erent least R-models.It should be noticed that the folding rule R3 could have been made more12

Fioravanti, Pettorossi, and Proiettipowerful by allowing non-atomic folding (i.e., more than one atom in the bodyof the de�nition �) and multiple folding (i.e., more than one de�nition clause),but we presented it in the weaker form for showing that this form is powerfulenough to achieve very high e�ciency improvements and it is easier to applyin an automatic way.Notice also that in our rules we assume that the comma operator is com-mutative. This assumption may indeed be too restrictive for the existingimplementations of CLP languages. We leave this issue for future studies.The general strategy we have proposed in Section 2 for partially evaluatingCLP programs, extends various techniques based on the unfolding and foldingrules for partially evaluating logic programs and constraint logic programssuch as those described in [6,18,20], and, indeed, our folding rule is morepowerful than the ones considered in those papers.In order to achieve various program improvements by exploiting the knowl-edge of the context in which predicates are called, we have illustrated twomore strategies: (i) the context propagation strategy, and (ii) the invariantpromotion strategy. These strategies make use of the contextual constraintreplacement rule.Finally, the papers [10,21] report on some correctness results for partial de-duction, that is, partial evaluation of logic programs based on Lloyd and Shep-herdson's approach [12]. Apart from some technical issues that make partialdeduction di�erent from program specialization based on unfolding/foldingrules, we would like to point out that the frameworks described in [10,21] donot allow for contextual constraint replacement. Indeed, from the constrainedatom X�3; p(X) and program P0 above we derive the resultant :p(X) X�3; q(X)where the constraint X�3 has to be solved at evaluation time, even if weknow that it holds at specialization time. In contrast to partial deduction, ourapproach is able to fully exploit the statically available information providedby the context of use of a program.Appendix A.The following table shows some execution times in milliseconds of the pro-grams derived in the previous sections.Programs LP = 3 LP = 10SumMatch 276000 ms 762000 msSumMatchpe 380 ms 720 msSumMatchc 170 ms 330 msSumMatch i 150 ms 260 ms13

Fioravanti, Pettorossi, and ProiettiLP indicates the length of the pattern to be found in a string of 1000 realnumbers. The experiments were performed on a Pentium II 300 MHz runningLinux and SICStus Prolog.Appendix B.Derivation of clauses 9 and 10 from clause 8.Since D j= 8A;B;C (9I (I = A+B+C)$ true), by constraint replacementfrom clause 8 we get:8.1 summatch i(A;B;C; S) I = A+B+C; I = A+B+C;summatchc(A;B;C; S)By de�nition we introduce the following clause:10. newp(A;B;C; S; I) I = A+B+C; summatchc(A;B;C; S)By folding clause 8.1 by using clause 10, we get:9. summatch i(A;B;C; S) I = A+B+C; newp(A;B;C; S; I).References[1] N. Bensaou and I. Guessarian. Transforming constraint logic programs.Theoretical Computer Science, (206):81{125, 1998.[2] A. Bossi, N. Cocco, and S. Dulli. A method for specializing logic programs.ACM Transactions on Programming Languages and Systems, 12(2):253{302,April 1990.[3] C. Consel and S.C. Khoo. Parameterized partial evaluation. ACM Transactionson Programming Languages and Systems, 15(3):463{493, 1993.[4] S. Etalle and M. Gabbrielli. Transformations of CLP modules. TheoreticalComputer Science, 166:101{146, 1996.[5] J. P. Gallagher. Tutorial on specialization of logic programs. In Proceedingsof ACM SIGPLAN Symposium on Partial Evaluation and Semantics BasedProgram Manipulation, PEPM '93, Copenhagen, Denmark, pages 88{98. ACMPress, 1993.[6] T. J. Hickey and D. A. Smith. Towards the partial evaluation of CLP languages.In Proceedings ACM Symposium on Partial Evaluation and Semantics BasedProgram Manipulation, PEPM '91, New Haven, CT, USA, SIGPLAN Notices,26, 9, pages 43{51. ACM Press, 1991.[7] J. Ja�ar and M. Maher. Constraint logic programming: A survey. Journal ofLogic Programming, 19/20:503{581, 1994.14

Fioravanti, Pettorossi, and Proietti[8] J. Ja�ar, M. Maher, K. Marriott, and P. Stuckey. The semantics of constraintlogic programming. Journal of Logic Programming, 37:1{46, 1998.[9] N. D. Jones, C. K. Gomard, and P. Sestoft. Partial Evaluation and AutomaticProgram Generation. Prentice Hall, 1993.[10] M. Leuschel and D. De Schreye. Constrained partial deduction. In B. FreitagF. Bry and D. Seipel, editors, Proceedings of the 12th Workshop LogischeProgrammierung (WLP'97), pages 116{126, Munich, Germany, September1997.[11] M. Leuschel and M. H. S�rensen. Redundant argument �ltering oflogic programs. In J. Gallagher, editor, Logic Program Synthesis andTransformation, Proceedings LoPSTr '96, Stockholm, Sweden, Lecture Notesin Computer Science 1207, pages 83{103. Springer-Verlag, 1996.[12] J. W. Lloyd and J. C. Shepherdson. Partial evaluation in logic programming.Journal of Logic Programming, 11:217{242, 1991.[13] M. J. Maher. A transformation system for deductive database modules withperfect model semantics. Theoretical Computer Science, 110:377{403, 1993.[14] Kim Marriott and Peter J. Stuckey. The 3 R's of Optimizing Constraint LogicPrograms: Re�nement, Removal and Reordering. In Proceedings 20th ACMSIGPLAN-SIGACT Symposium on Principles of Programming Languages(POPL '93), pages 334{344. ACM Press, 1993.[15] A. Pettorossi and M. Proietti. A theory of logic program specialization andgeneralization for dealing with input data properties. In O. Danvy, R. Gl�uck,and P. Thiemann, editors, Proceedings of the Dagstuhl Seminar on PartialEvaluation, Lecture Notes in Computer Science 1110, pages 386{408. Springer-Verlag, 1996.[16] A. Pettorossi and M. Proietti. Synthesis and transformation of logic programsusing unfold/fold proofs. Journal of Logic Programming, 41(2{3):197{230, 1999.[17] A. Pettorossi, M. Proietti, and S. Renault. Reducing nondeterminism whilespecializing logic programs. In Proc. 24-th ACM Symposium on Principles ofProgramming Languages, Paris, France, pages 414{427. ACM Press, 1997.[18] S. Prestwich. Online partial deduction of large programs. In ProceedingsACM Sigplan Symposium on Partial Evaluation and Semantics-Based ProgramManipulation, PEPM '93, Copenhagen, Denmark, pages 111{118. ACM Press,1993.[19] G. Puebla and M. Hermenegildo. Abstract Multiple Specialization and itsApplication to Program Parallelization. J. of Logic Programming. Special Issueon Synthesis, Transformation and Analysis of Logic Programs, 41(2&3):279{316, November 1999.[20] D. Sahlin. Mixtus: An automatic partial evaluator for full Prolog. NewGeneration Computing, 12:7{51, 1993.15

Fioravanti, Pettorossi, and Proietti[21] A. Wrzos-Kaminska. Partial evaluation in constraint logic programming. InZ.W. Ras and M. Michalewicz, editors, Proceedings of the 9th InternationalSymposium on Foundations of Intelligent Systems, Lecture Notes in ComputerScience 1079, pages 98{107, Zakopane, Poland, 1996. Springer-Verlag.

16