Abstracting Numerical Values in CLP(H,N) - Semantic Scholar

5 downloads 0 Views 231KB Size Report
m1g \. fY 2. 1 ;:::;Y 2 m2g;fX1;:::;Xng = fX1. 1 ;:::;X1 n1g fX2. 1;:::;X2 n2gg. The upper bound retains narrowing rules which are implied by P1 and P2: we retain the ...
Abstracting Numerical Values in CLP(H,N) Gerda Janssens1 , Maurice Bruynooghe1 , Vincent Englebert2 Department of Computer Science, K.U. Leuven Celestijnenlaan 200A, B-3001 Heverlee, Belgium Institut d'Informatique, Facultes Universitaires Notre Dame de la Paix rue GrandGagnage 21, B-5000 Namur, Belgium 1

2

Abstract. The paper de nes approximations for the numerical leaves of variables in CLP(H,N) constraint systems. The abstractions are based on intervals which are computed by narrowing rules. The novelty of this approach lays in the fact that intervals are used as abstraction and that narrowing rules do not only correspond to numerical constraints but also to uni cation constraints. In the rst abstraction the impact of the narrowing rules is limited. A prototype implementation has been developed and the obtained results are suciently precise to recognise (future) redundant constraints. The abstraction can be extended (1) by incorporating the narrowing rules more globally (uni cation constraints are abstracted by a Prop-like abstraction, numeric constraints are kept in concrete form) and (2) by deriving ordinal relationships between variables.

1 Introduction Constraint Logic Programming o ers a clean and simple paradigm for solving a large range of practical problems. Central is the notion of a global constraint solver which solves constraints which are monotonically added to a global constraint store. This allows for the formulation of simple and concise programs which can be queried in many di erent ways. However, this exibility has a price. General constraint solving is very expensive. To compete in performance with programs written (for a speci c class of queries) in a more restrictive language, program specialisation based on global analysis is almost indispensable. Abstract interpretation [8] provides a formal basis for this. How abstract interpretation for logic programs extends into abstract interpretation for constraint logic programs is by now well understood [22, 13, 3]. Key steps in creating an application are the selection of an appropriate abstract domain, and the abstraction of the operations of adding a constraint to the global store (conjunction), of projecting the global store on a subset of the variables, and of computing the upper bound of two global stores. Of particular concern are inequality constraints. The general form is a1  X1 + : : : + an  Xn  b. In case all Xi are known, they reduce to a simple test; however if some of the Xi are unknown, then they have to be added to the global constraint store where they are processed by the expensive simplex algorithm. The expensive processing can be avoided in some cases. The simplest case is

when the constraint is entailed by the global store. Then it need not be added: it is redundant. Testing this at run time is still expensive, however, if detected at compile time by global analysis, then the constraint can simply be dropped from the program. Another relevant case was recognised in [17], they coined the term future redundancy if the constraint to be added is entailed by the conjunction of the current store and of the constraints to be added in the next cycle of the constraint engine. In this case it suces to test whether the constraint is consistent with the current store; there is no need to add the constraint to the store. In [23], Marriott and Stuckey discuss strategies of Re nement, Removal and Reordening of constraints which can also contribute to reducing the cost of processing the constraints. In some cases, local information is sucient, i.e. X > 0 is future redundant, if in the next cycle, the global store will be extended with either X = X ? 1; X = 0 or with X = X ? 1; X > 0. Also [1] performs a local analysis to detect future redundancy. Constraint propagation techniques are used to detect entailed constraints. In other cases, knowledge of the state of the global store is required to recognise (future) redundancy. Here abstract interpretation comes in as a general framework for safely approximating the state of the global store. Marriot and Stuckey [23] use two non trivial domains, Lsign, where the coecients of equations and inequations are abstracted by their sign, and CHull, the convex hull abstraction already used by Cousot and Halbwachs [9]. They do neither report about the implementation nor about the analysis times. In the current paper, we focus on CLP(H,N) programs, in which constraint systems contain uni cation constraints over the Herbrand domain (H) and numerical constraints over an in nite domain of numbers (N). We explore the adequacy of simpler abstractions and investigate whether the obtained approximations are sucient for recognising (future) redundancy of simple inequality constraints of the form X  c; X > c; X  Y; X > Y . These types of constraints are quite frequent, and are the constraints which hurt when competing with a specialised program written for example in Prolog. Indeed, these are the kind of constraints compiled away by the programmer when writing his specialised code. Central to our approach is the use of intervals to approximate constraints on variables. The use of intervals in constraint solving is well known, e.g. [20], their use in abstract interpretation is not widespread. Also we use intervals in the abstraction of non numerical variables. Most of the previous work on analysis of CLP considers only constraints over variables in the numerical domain. An exception is the freeness analysis of [10], but this is not so much concerned with inequality constraints. When associating intervals with variables over the Herbrand domain, the interval constrains the numerical values which can occur in the leaves of the Herbrand term. This can sometimes be very useful. For example, suppose that a list L is constructed with elements from the numbers 0; 1; : : :; 9. Then we can derive an interval constraint L 2 [0; 9]. When selecting a number X from the list, one can infer that X  0 or X  9 are recognised as redundant. The paper is organised as follows. In Sect. 2 we present the basic approach using intervals where uni cation and numerical constraints are used for local 0

0

0

0

narrowing. This approach has been implemented and the obtained results are shown to be useful for recognising (future) redundancy. In Sect. 3, the abstraction is extended with concrete numerical constraints and a Prop-like [21, 7] abstraction for uni cation constraints to use the interval narrowing rules more globally. Section 4 introduces, in addition to the intervals which contain quantitative information about numerical leaves of variables, qualitative information in the form of ordinal relationships between variables. Conclusions and further work are in Sect. 5.

2 Intervals 2.1 Preliminaries In the following we assume familiarity with the standard de nitions and notation for constraint logic programs [5, 6, 15] and abstract interpretation [8]. A framework for abstract interpretation such as [2] needs to be lled in with an abstract domain and with the operations of conjunction { adding a constraint to a constraint system {, projection { hiding all but the projected variables {, upper bound { computing an abstract store which safely approximates two given stores {, a criterion for limiting the depth of recursion { optional if the abstract domain is nite{ and a widening operator { optional if the abstract domain has nite height. For simplicity of exposition, we assume that we know which variables in the CLP(H,N) program are numerical, i.e. all their possible values are numbers. A safe approximation can be obtained by a simple analysis at the level of program variables. More precise information about data structures containing numerical values can be obtained by type inference systems [4, 16, 12]. We assume a denumerable set of variables Var { VarN denotes the subset of numerical variables { and a set of function symbols  = N [ A (N and A overlap). Function symbols are classi ed as numerical or alphabetical , i.e. in N or A respectively. The numerical function symbols will typically include numerical constants (such as real, integer or rational numbers | depending on the speci c domain) and numerical functors (such as +; ?;  and =). The alphabetical function symbols include the constants (including numbers) and non-numerical function symbols. The corresponding term algebras are denoted respectively T(N ; VarN ) and T(A ; Var). A numerical constraint is an equation or an  inequality between T(N ; VarN ) terms3 . A uni cation constraint is an equation between T(A ; Var) terms. A constraint system is a nite conjunction of numerical and uni cation constraints, written as a set. Note that while an equation of the form X = f(2  Y + 1; T; 3) is neither a numerical nor a uni cation constraint, it can be normalised into the system fX = f(A; T; B); A ? 2  Y = 1; B = 3g consisting of a uni cation and two numerical constraints. Moreover, based on the typing information every constraint can be classi ed either as a numerical or as a uni cation constraint: 3

For simplicity of exposition, we do not consider > constraints.

X = 3 is clearly numerical, while X = Y is numerical if X and Y belong to VarN .

2.2 Intervals as Abstraction For our application, we want to derive that simple constraints of the form X  l and X  u are entailed by a constraint system. Therefore, we must know that X 2 VarN and that its possible (numerical) values satisfy such a constraint. The interval abstraction derives lower and upper bounds for the numerical leaves of a variable X, nl(X). If X is numerical, nl(X) corresponds to its possible (numerical) values. Otherwise, nl(X) corresponds to the numerical values which can occur in the leaves of the Herbrand terms possibly bound to X. Abstractions are de ned for concrete constraint systems C with C 2 ConC . The abstract constraint systems are denoted IC with IC 2 Con . An abstract element IC over a set variables D is either ? or a tuple (I; NR) with I the set of intervals fX 2 [l; u]jX 2 D; l and u are numbers including ?1 and 1 g and NR a set of narrowing rules. The empty interval is denoted ?. Note that X 2 ? does not indicate failure if X 62 VarN . However, if X 2 VarN and its interval is ?, then failure occurs and the canonical form of IC is ?. The function I(X)IC returns the interval associated with X in IC. The functions l(X)IC and u(X)IC return respectively the lower bound and the upper bound of the associated interval in IC. If the interval is ?, the functions l and u return ?. We will associate narrowing rules with the uni cation constraints and the numerical constraints. Interval narrowing uses a narrowing rule to compute an interval for a variable, then the interval of the variable is updated with the intersection of this new interval and the original interval [20]. There also techniques are described to obtain stable intervals with a minimum of computational e ort. The function interval narrowing(I; NR) computes for I the corresponding set of intervals which are stable under the narrowing rules NR. From now on we assume that in the abstract elements (I; NR) the set I is stable under NR. To each concrete constraint corresponds a set of narrowing rules. A narrowing rule for X has the form I(X) := [exprl ; expru] where the expressions exprl and expru de ne respectively the lower and the upper bound. For the uni cations we consider three base cases (for any syntactic object O, var(O) denotes the variables in O, number(O) the numbers and constant(O) the non numerical constants): X = cte with cte a non numerical constant I(X) := ? X = f(t1 ; : : :; tn) with n  1, fY1; : : :; Ym g = [ni=1 var(ti ) and fn1; : : :; nk g = [ni=1number(ti ) I(X) := if k = 0 and (m = 0 or l(Y1 ) = : : : = l(Ym ) = ?) then ? else [min(l(Y1 ); : : :; l(Ym ); n1; : : :; nk )4 ; max(u(Y1 ); : : :; u(Ym ); n1 ; : : :; nk )] I(Yi ) := [l(X); u(X)] for 1  i  m I

X=Y I(X) := [l(Y ); u(Y )] I(Y ) := [l(X); u(X)] Linear equalities associate a narrowing rule with each of their variables: Pni=1 ci  Xi = c0 P I(Xj ) := [c0=cj ?P ni=1;i=j ci =cj  ( if ci =cj < 0 then l(Xi ) else u(Xi )); c0=cj ? ni=1;i=j ci =cj  ( if ci =cj < 0 then u(Xi ) else l(Xi ))] 6

6

Also  inequalities associate a narrowing rule with each of their variables: Pni=1 ci  Xi  c0 I(Xj ) := if cj > 0 then P[l; 1] else [?1; u] where l = c0 =cj ? Pni=1;i=j ci =cj  ( if ci > 0 then u(Xi ) else l(Xi )) and u = c0 =cj ? ni=1;i=j ci=cj  ( if ci < 0 then l(Xi ) else u(Xi )) 6

6

De nition1 Abstracting a constraint system. Let C be a constraint system, let var(C) be the set of variables in C. Then (fC g) = (interval narrowing(I; NR); NR) with I = fX 2 [?1; 1]jX 2 var(C)g and NR a set of narrowing rules for C as de ned above 5. I

Example 1.

Let C = fX = Y +1; Y  0g. Then, IC = (fX 2 [1; 1]; Y 2 [0; 1]g; NR) where NR = fI(X) := [l(Y )+1; u(Y )+1]; I(Y ) := [l(X) ? 1; u(X) ? 1]; I(Y ) := [0; 1]g. The intervals of IC are computed by interval narrowing(fX 2 [?1; 1]; Y 2 [?1; 1]g; NR). The narrowing rules in the abstraction correspond to a concrete constraint system. When de ning the abstract operations, it is not trivial to take them into account, because entailed constraints become relevant. Section 3 suggests a solution for this problem. For the version of the abstract operations developed in this section we will weaken the abstraction based on the observation that (I; ;) is a safe approximation of (I; NR) with NR a set of narrowing rules. The abstract operations are de ned for the most general case that is needed during abstract interpretation. They can easily be completed to deal with the abstract constraint system ?.

De nition2 Abstract projection. Given an abstract constraint system IC, the abstract projection of IC on V  Var is de ned as proj (IC; V ) = (fX 2 I(X)IC jX 2 V g; ;). I

4 5

The functions min and max ignore arguments which are ?. To improve precision it is desirable to put the constraint system C in solved form. One of the reasons is that determined variables can be detected and nonlinear constraints might become linear. In this paper we do not associate narrowing rules to nonlinear constraints, the extension is feasible [20].

Notice that the narrowing rules are dropped.

De nition3 Abstract conjunction.

The abstract conjunction of IC1 = (I1 ; NR1); IC2 = (I2 ; NR2) 2 Con over the variables D is de ned as conj (IC1 ; IC2) = (interval narrowing(I; NR1 [ NR2); NR1 [ NR2) and I = fX 2 I(X)IC1 \ I(X)IC2 jX 2 Dg. I

I

De nition4 Order relation. Let IC1 = (I1 ; NR1); IC2 = (I2 ; NR2) 2 Con over the variables D. Then IC1  IC2 i 8X 2 D holds that I(X)IC1  I(X)IC2 and NR2  NR1. Proposition5 Intervals as safe approximation for numerical leaves. Let IC 2 Con and C 2 ConC be abstract and concrete constraint systems over the variables D such that (C)  IC . Then 8X 2 D holds that if I(X)IC = ? then nl(X) = ; else 8m 2 nl(X) holds that l(X)IC  m  u(X)IC . De nition6 Upper bound. Let IC1 = (I1 ; NR1); IC2 = (I2 ; NR2) 2 Con over the variables D. Then ub (IC1 ; IC2) = (fX 2 I(X)IC1 [ I(X)IC2 jX 2 Dg; NR1 \ NR2). I

I

I

I

I

I

I

Due to the presence of intervals, one can have in nite sequences IC0 ; IC1; : : : and IC0 ; IC1; : : : such that IC1 = ub (IC0 ; IC0 ), IC2 = ub (IC1 ; IC1); : : :; ICn = ub (ICn1 ; ICn1 ). Therefore, a widening operator W [8] is needed which computes the new interval in a di erent way: 0

0

0

I

0

I

0

I

De nition7 Widening for intervals.

Let [l1 ; u1] be an interval from ICi and [l2 ; u2] the corresponding interval from ICi . Then the successor of ICi , W(ICi ; ICi ), has [ if l1  l2 then l1 else if l2 > 0 then 0 else ? 1; if u1  u2 then u1 else if u2 < 0 then 0 else + 1] as corresponding interval. With our in nite abstract domain, we can have an in nite sequence of call patterns for recursive predicates. A simple solution is to impose an arbitrary depth on the level of recursion in the analysis [2]. A better approach could be along the following lines. Let P be a call called with abstract constraint IC. Let Hi ci1 ^ : : : ^ ci ; Bi: be the ith de ning clause and ICi = IC ^ (P = Hi). Let P be the recursive call called with abstract constraint IC . Let ICi = IC ^ (P = Hi ). If, for some i, there is a signi cant di erence, then, this new level is fully analysed, if not, the analysis of P starts over with call pattern W(IC; IC ). A signi cant di erence is de ned as follows: { ICi = ? and ICi is not or vice versa. { ICi has an interval constraint X 2 ? and ICi has an interval constraint X 2 [l; u] (or vice versa). { ICi has an interval constraint X 2 [c; c] and ICi has an interval constraint X 2 [l; u] with l 6= u (or vice versa). { ICi j= ci and ICi 6j= ci with 1  j  ki (or vice versa). Given that the abstract constraints are over a nite number of variables, there are only a nite number of signi cantly di erent cases and the recursion depth is nite. 0

0

0

ki

0

0

0

0

0

0

0

j

0

j

0

2.3 Implementation and Results The interval abstraction has been coupled to the top-down abstract interpretation system GAIA (Generic Abstract Interpretation Algorithm) [11, 19], which can be seen as an implementation of the framework [2] and which is implemented in C. We currently have a prototype implementation along the lines of the previous section. Analyses of typical CLP(R) programs such as option, critical, dnf, laplace, : : :take at most 1.2 seconds on a SUN SPARC2. To our knowledge this is the rst paper that gives an indication that abstractions of numerical values can be obtained with a reasonable eciency. Although the eciency of the analysis is obtained by keeping the operations simple, the results are valuable. This is illustrated for three representative examples. Example 2 SEND + MORE = MONEY. This example shows that the analysis can derive redundant constraints which can be used to obtain a re ned program whose execution is faster as the search space is reduced. The program is the CLP(R) version containing the predicate interval/3 which generates a list of numbers. solve(S, E, N, D, M, O, R, Y) :constraints(S, E, N, D, M, O, R, Y), % interval/3 generates the list of numbers [0,1,2,3,4,5,6,7,8,9] interval(0,9,Choice), gen_diff_digits([S,E,N,D,M,O,R,Y],Choice). constraints(S, E, N, D, M, O, R, Y) :S >= 1, M >= 1, M = C1, C2 + S + M = O + C1 *10, C3 + E + O = N + C2 *10, C4 + N + R = E + C3 *10, D + E = Y + C4 *10, bit(C1), bit(C2), bit(C3), bit(C4). bit(0). bit(1). interval(N,N,[N]). interval(L,H,[L|LL]):- L < H, interval(L+1,H,LL). gen_diff_digits([], _). gen_diff_digits([H | T], L) :- select(H, L, L2), gen_diff_digits(T, L2). select(H, [H | T], T). select(H, [H2 | T], [H2 | T2]) :- select(H, T, T2).

The analysis of constraints/8 derives that S 2 [1; 1]; M 2 [1; 1], C1 2 [1; 1], C2 2 [0; 1], C3 2 [0; 1] and C4 2 [0; 1]. The variable Choice is a list of numbers whose elements lay between 0 and 9: the analysis of interval(0; 9; Choice) derives that Choice 2 [0; 9]. L 2 [0; 9] in the call to select(H; L; L2) in gen di digits/2, and the analysis obtains that H 2 [0; 9] and L2 2 [0; 9]. Finally, it derives the interval [1; 9] for the variable S, [1; 1] for M and [0; 9] for E, N, D, O, R and Y in solve/8. This information combined with the type information { S, E, N,

D, M, O, R and Y are numerical { allows us to re ne [23] the program: we add redundant constraints in the beginning of constraints/8. constraints(S, E, N, D, M, O, R, Y) :S >= 1, E >= 0, N >= 0, D >= 0, O >= 0, R >= 0, Y >= 0, S = 0, B >= 0, R >= 0, T >= 2, T1 = T - 1, P1 = P*(1 + I/1200) - R, mortgage(P1,T1,I,R,B).

From the analysis results we can see that from the recursive call on, one can safely assume that the tests I >= 0; B >= 0 and R >= 0 are redundant. The program can be specialised by replacing the recursive call by a call to a version without the redundant tests. The execution times of both programs can di er signi cantly. For the four queries in [23] the following Original/Specialised factors are obtained:

query ?- B >= 0, mortgage(100000, 360, 12, 1025, B). ?- mortgage(P, 360, 12, 1025, 12625.9). ?- R > 0, B >= 0, mortgage(P, 360, 12, R, B). ?- B >= 0, B

Suggest Documents