Detecting Determinate Computations by Bottom-up Abstract Interpretation Roberto Giacobazzi , Laura Ricci Dipartimento di Informatica Universita di Pisa Corso Italia 40, 56125 Pisa fgiaco,
[email protected]
Abstract
One of the most interesting characteristics of logic programs is the ability of expressing nondeterminism in an elegant and concise way. On the other hand, implementation of nondeterminism poses serious problems both in the sequential and in the concurrent case. If determinate computations are detected through a static analysis, a noticeable reduction of the execution time may be obtained. This work describes a static analysis to detect determinate computations. The analysis does not require the knowledge of the activating modes of the predicate and it derives determinacies which may be inferred from the success set of the program.
1 Introduction One of the main features of logic programming is the ability to compute a set of output bindings for each variable of a query. While this characteristic supports an elegant and concise de nition of non-deterministic behaviours, it poses serious problems both in a sequential and in a concurrent implementation of the language. In several cases, the ability of logic programs to compute multiple bindings is not exploited: in this case a logic program has a determinate behaviour, i.e. it produces a single output value for a given query, or for a class of queries satisfying some properties. Recently, some analyses [6, 4, 11, 10, 12] have been proposed to statically detect when a computation is determinate. The knowledge of determinate computations, i.e. determinacies, supports optimization of the program execution time [5] end of bottom-up evaluators [10]. Even if the analysis to detect determinancies is related to the control features of the language implementation, we show that determinancies may be inferred by a bottom up analysis that does not require any knowledge of the type and activating modes of a predicate. The analysis derives those determinacies that may be inferred from the success set of the program. The de nition of the analysis requires the investigation of the relation between the input values of a predicate and those returned by the refutation of the To
appear in
European Conference on Programming ESOP'92
predicate itself. Some recent proposals [1, 6, 8, 12] characterize such a relation through the notion of dependence. This notion has been exploited, for instance, to describe the relation between the groundness of a set of arguments of a predicate upon invocation and the groundness of another set of arguments, after the refutation of the predicate. We re ne such notion through that of deterministic ground dependence. A ground dependence from a set of input arguments to a set of output ones is deterministic if, whenever the input arguments are ground, the refutation binds any output argument to a single ground term. The de nition of deterministic ground dependences supports a re ned notion of functionality, i.e. functionality is inferred with respect to subsets of arguments rather than to whole predicates. The analysis has been de ned and validated through a bottomup abstract interpretation technique. It integrates the theory of hypergraphs with the semantics of logic programs in order to obtain a powerful program analysis tool. The information useful to characterize deterministic computations is produced by applying an abstract interpretation which returns an abstract model of the program. This approximation is obtained by the nite computation of the least xpoint of an abstraction of the immediate consequence operator TP . In order to have such an abstraction, a suitable set of abstract domains and operators, supporting the de nition of TP , is given. Section 2 introduces some basic notions on hypergraph theory and semantics of logic programs, Section 3 formally de nes the abstract domains and abstract operators for the analysis and abstract xpoint semantics for deterministic dependences analysis, Section 4 shows some signi cant examples, while Section 5 deals with some concluding remark.
2 Preliminaries We assume the reader familiar with the notions of lattice theory, hypergraph theory [2], semantics of logic programs [7] and the basic concepts of logic programming [9]. In the following we will denote by S=fsg the set S where the element fsg has been removed. A hypergraph G is a pair (N ; E ) where N = fv1; :::; vng is a ( nite) set of nodes and E = fe1; :::; emg is a set of hyperarcs, where for each i = 1; :::; m, ei = hT; H i such that T N (the tail) and H 2 N (the head). In the following we denote by Tail(e) and Head(e) the tail and the head of any hyperarc e. The \tail" and \head" notions are extended to deal with sets of hyperarcs. A hyperarc e is satis ed by a set of nodes N 0 i Tail(e) N 0. A graph is a hypergraph (N ; E ) where 8e 2 E ; jTail(e)j = 1 (we denote by jT j the number of elements is T ). Given a graph , the nodes n and n0 are connected in i there exists a path E where = fe1; :::; ekg such that n = Tail(e1); n0 = Head(ek ) and 8j = 1; :::; k ? 1; Head(ej ) = Tail(ej+1). The connection between the nodes n and n0 (by means of a path ) in a graph will be denoted by: n 7! n0. The graph, denoted by G , associated with the hypergraph G = (N ; E ) is obtained by reducing each hyperarc: hfs1; :::; smg; hi 2 E , in a set of arcs fhs1; hi:::; hsm; hig. In the following we will denote by =G (n0; n) = f j n0 7! G ng the set of paths de ning a connection from n0 to n in the graph G associated with the hypergraph G . Given a hypergraph G = (N ; E ), let E 0 E . The sets of nodes N 0 and0 the node n are connected in G , by means of the hyperarcs in E 0, denoted by N 0 !EG n, i all the hyperarcs in E 0 are satis ed by N 0 [ Head(E 0), 8i 2 N 0 : =(N ;E 0)(i; n) 6= ; and
8e 2 E 0 ; 8i 2 Tail(e) : i 62 N 0; 9n 2 N 0 such that =(N ;E 0=feg)(n; i) 6= ;. We call E 0 a connection from N 0 to n in0 G . A connection E 0 from N 0 to n in a hypergraph G is minimal i for each e 2 E , N 0 6!GE =feg n. Example 2.1 Let us consider the following hypergraph and some connections with v5 ? fv4g !Gfd;c;e;ag v5, fv3g !fGc;e;ag v5, fv1; v2g !fGag v5, ... G = v1 v2 v3 v4 v5 6 66 6 while 8E 0 fa; b; c; d; eg: fv1 g 6!EG0 v5, fv5 g 6!EG0 v5. a
c
e
d
b
Notice that the de nition of connection requires that any node in the tail of an hyperarc which is not in the set of starting nodes N 0 can be reached from some starting node, without using the hyperarc itself. This condition avoids considering, in a connection, a hyperarc (e.g. a), such that each non-starting node (v2 ) in the tail can only be reached from some starting node (v1) by means of a path using the hyperarc itself (the hyperarc becomes auto-satis ed). As a consequence 6 9E 0 fa; b; c; d; eg : fv1g !EG0 v5.
The declarative semantics of logic programs in [7] is based on an extended Herbrand Universe containing also non-ground terms. It allows to declaratively characterize the ability of logic programs to compute answer substitutions (which are non-ground in general). Let us consider the set Cons of term constructors, and a denumerable set V ar of variables. The free Cons-algebra on V ar is denoted as TCons (V ar). A substitution # is a mapping from V ar into TCons (V ar), such that fx 2 V ar j #(x) 6= xg is nite. " denotes the empty substitution. #jG denotes the restriction of the substitution # to the variables occurring in G, which is extended as an identity, for any x 2 V ar(G) such that #(x) is unde ned. We denote by mgu a total function which maps a pair of syntactic objects (e.g. atoms, terms,...) to an idempotent most general uni er of the objects, if such exists; fail otherwise. The extended Herbrand Universe UP is de ned as TConsP (V ar)= , where t1 t2 i 9 #1; #2 j t1#1 = t2 ^ t2#2 = t1. Let Pred be a nite set of predicate symbols. An atom is an object of the form p(t1; :::; tn) where t1; :::; tn 2 TConsP (V ar) and p is an n-ary predicate symbol (i.e. p 2 Predn ). The set of atoms is denoted Atoms. We denote by V ar(a) the set of variables in any syntactic object a. The Base of interpretations BP is Atoms= where extends to atoms in the obvious way. An interpretation I is any subset of BP . Standard results on model-theoretic and xpoint semantics apply to the extended domains as well as in the ground case [7]. In particular, the xpoint semantics for a logic program P (denoted as F (P )) is de ned by means of an immediate consequences operator TP [7]. It derives possibly non-ground atoms by means of a bottom-up inference rule which is based on uni cation, as in the top-down SLD resolution. We assume the reader familiar with the basic notions of abstract interpretation as de ned in [3]. In the following an abstract interpretation supporting our analysis for de nite logic programs is developed according to a bottom-up technique [1] based on an abstraction of the declarative and xpoint semantics in [7].
3 Abstract Domains This section introduces an analysis for ground and ground-deterministic dependences. Informally, a ground dependence does exist between a set of arguments T of an atom
A and another argument h, i whenever the arguments in T are ground, the refutation of A produces a set of ground bindings for h. A ground dependence is determinate, i.e. deterministic ground dependence, i the refutation produces at most one binding for h. Consider the logic program append([]; X; X ) : ?: append([X jXs]; Y; [X jY s]) : ?append(Xs; Y; Y s): the refutation of the goal append([aj[]]; [bj[]];Z ) in the program produces only one binding for Z (fZ = [ajbj[]]g), while the goal append(X; Y; [ajbj[]]) produces multiple bindings for X and Y (fX = [aj[]]; Y = [bj[]]g, fX = [ajbj[]];Y = []g, etc.). In the rst case the groundness of the rst two arguments uniquely determines the groundness of the third one, while, in the second case, a non-deterministic dependence does exist between the third argument and the rst one and between the third and the second one. Let P be a de nite logic program, we denote by UP] = fg; ngg the abstract universe of terms where g represents the ground term set, and ng the possibly non-ground one. Two terms are ground-equivalent (ground ) i both are ground or non-ground terms. As a consequence, UP] can be considered as a set of abstract objects representing equivalence classes of terms, modulo ground equivalency. It is trivial to prove that UP] is a nite lattice since for each ground term there exists a non-ground term which is greater than it (i.e. g g ng). The elements in UP] will be denoted as t].
De nition 3.1 A dependence representation D is a triple (N ; V ; E ), where (N ; E ) is a hypergraph such that N is a nite set of symbols called the domain of the representation (Dom(D)), E is a set of labelled hyperarcs of the form hT; hil, where l 2 fd; ?; ?g, V N UP] such that jVj = jNj ^ 8(s; t]); (s0; t0]) 2 V ; s =6 s0. A dependence is a hyperarc in a dependence representation. An autodependence is a dependence hT; hil such that h 2 T . Dependence representations which are de ned over the same domain N are called homogeneous. The hypergraph notation is useful to represent dependence information in a concise way. Dependence representations are hypergraphs enhanced with a suitable set of labels (for nodes and hyperarcs). Since both the elements of V and the elements of E are de ned on the same set of nodes N , we will often omit the reference to the underlying set of nodes when this can be deduced by the context. Labels associated with nodes will correspond to the mode information (a mode analysis is considered in our framework), while the labels associated with the dependence notion represent the type of the corresponding dependence (d and ? mean deterministic and don't know respectively, while ? means unde ned). Hyperarc labels can be structured as a lattice, (fd; ?; ?g; d ), where d d? and ? d d. Let be a graph with labelled arcs and vh be a path from the node v to h in . We denote by (vh) the set of labels in the path vh. This notation naturally extends to sets of hyperarcs.
De nition 3.2 Given a dependence representation D = (N ; V ; E ), a hyperarc e 2 E is redundant in D i e =0 hT; hi? and h 2 T , or e = hT; hil and 9T 0 T; T 6= ;; 9E 0 E , e 62 E 0 such that T 0 !E(N ;E ) h and lubd ((E 0)) d l. A hyperarc is redundant in a dependence representation i it does not add any information to the representation itself (i.e. either it is an autodependence labelled by ?, or there exists a connection with the same target which is more concise (T 0 T ) and captures a stronger dependence relation). Notice that a connection is deterministic i all its labels are d. In the following we will use a suitable graphical notation for dependence representations.
Example 3.1 Let us consider the non-redundant dependence representation d
?
(v1; ng); (v2; ng); (v3; ng) . d
66 d
d
6
Note that the hyperarcs hfv2 ; v3g; v1id , hfv1 ; v3g; v2id and the autodependences hfv3g; v3id , hfv1; v2g; v2id, hfv1; v2; v3g; v1id , hfv1; v2; v3g; v2id, hfv1; v2; v3g; v3id , hfv1; v2g; v1id, hfv2; v3g; v2id , hfv2; v3g; v3id, hfv2g; v2i? are redundant if added to the previous dependence representation. As an example, hfv3g; v3id is redundant since v3 can be reached by the node itself without considering that autodependence. Of course, all the previous dependences are redundant even if they are labelled with ?.
3.1 Abstract Atoms
De nition 3.3 An abstract atom is a pair of the form (p; Dp) (usually denoted as p(Dp )) where p is an n-ary predicate symbol in Pred and Dp is a non-redundant dependence representation such that Dom(Dp ) = f1; :::; ng (the nodes represent the argument positions in the atom).
De nition 3.4 The abstract base, denoted as BP] , is the set of non-redundant abstract atoms p(Dp ), for each p 2 Pred. De nition 3.5 An -interpretation is any subset of the abstract base having at ]most one occurrence for each predicate symbol. We will denote such a set as ] (] 2BP ). To make the analysis more concise we will return non-redundant dependences and we will approximate success patterns with their least upper bound, allowing in each interpretation at most one abstract atom per predicate symbol [1, 8].
De nition 3.6 Let p(Dp ), p(Dp0 ) be two abstract atoms, where Dp = (f1::ng; Vp; Ep), 0] ] 0 0 0 0 ] Dp = (f1::ng; Vp; Ep), Vp = f(1; t1); :::; (n; tn)g and Vp = f(1; t1 ); :::; (n; t0n])g. p(Dp ) v p(Dp0 ) i 8i = 1; :::; n : t]i g t0i]
8hT 0; hil0 2 Ep0 either t0h] = ng, t]h = g and 8e 2 Ep : Head(e) =6 h or hT 0; hil0 is redundant in (f1::ng; Vp; Ep [ fhT 0; hil0 g). Proposition 3.1 v is a partial ordering. Let I1]; I2] 2 ], I1] I2] i 8p(Dp) 2 I1]; 9p(Dp0 ) 2 I2]: p(Dp ) v p(Dp0 ). Example 3.2 ? ?
? d
? d
f q(ng; ng; ng)g fq(ng; ng; ng)g, f p(ng; g; ng; ng) g f p(ng; ng; ng; ng) g, ? ? d
? ? ?
f p(ng; ng; ng; ng) g f p(ng; ng; ng; ng) g. d
d
Since each -interpretation includes at most one atom for each predicate symbol, it is easy to prove that is a partial ordering and whenever I1] I2], then I1] I2]. Moreover, since Pred is nite and for each n-ary predicate symbol p 2 Pred there always exists a nite set of abstract atoms in BP] de ned on p, it is easy to prove that (]; ) is a nite lattice [1].
3.2 Abstract Substitutions
To develop an abstract interpretation based upon a declarative semantics, we also have to reconsider the substitution notion within the abstract framework [1]. Given a nite set of variables V , we denote by SubstV the set of substitutions # de ned on V for each V V . Let 1; 2 2 2SubstV , 1 S 2 i (8# 2 1; 9#0 2 2 j # #0) ^ (8# 2 2; 9#0 2 1 j # #0 ) 1 2).
De nition 3.7 An abstract substitution #] is a non-redundant dependence representation such that Dom(#] ) = V (the nodes are the symbols of variables in V ).
Thus, an abstract substitution #] can be represented as a hypergraph of bindings of the form (X=t]) where X 2 V and t] 2 UP] . Given a nite set Vn of variable symbols fx1; :::; xng such that xi 6= xj for each i 6= j : Subst]Vn denotes the set of abstract substitutions de ned on the set of variables Vk , for each Vk Vn . We can deal with a nite set of variables because of the bottom-up approach. In fact, in this kind of analysis the meaningful substitutions are only those ]referring the nite amount of variables occurring in program clauses. Let ]Vn 2SubstVn such that 8] 2 Vn , ] contains at most one abstract substitution #] such that Dom(#]) = Vk , for each Vk Vn . Note that any element in ]V can be handled as an -interpretation. As a matter of fact, abstract substitutions can be considered as abstract atoms having a dierent predicate symbol of arity k for each Vk Vn . Thus, since the preorder v can be de ned between any homogeneous dependence representations, independently from the domain, an ordering relation can be de ned by extending the on the domain of sets of abstract substitutions. It is easy to prove that [1] given a nite set of variables Vn , (]Vn ; ) is a nite lattice.
3.3 Galois Connections
Let I 2 2BP and p 2 Pred. In the following we denote by I # p the set of atoms in I having the same predicate symbol p. Let I # p = fp(t1 ; :::; t1n); :::; p(tm ; :::; tmn)g. We de ne an abstraction map [ abs(I # p) where abs(I # p) = ; if I # p = ; and abs : 2BP ! ] such that abs(I ) =p2Pred abs(I # p) = p(Dp ) such that Dp = (f1::ng; Vp; Ep) otherwise; where 8j = 1; :::; m; V ar(tji ) = ;, Vp = f(i; t]i) j i = 1; :::; ng where t]i = gng i otherwise, Let T = fi1; :::; ikg; hT; hil 2 Ep i hT; hil is non-redundant in Dp and { 8j = 1; :::; m; V ar(tjh ) i2[T V ar(tji ), 8 r; f 2 f1; :::; mg; r 6= f ; mgu(tri ; :::; trik ; tfi ; :::; tfik ) = fail; { l = d? i otherwise. 1
1
(
(
1
1
d ? d ?
Example 3.3 Let I = fq(a; b); q(b; b); q(c;d)g, then abs(I ) = f q( g ; g )g. ? 6
In this case, the groundness of the rst argument of q uniquely determines the groundness of itself, moreover it uniquely determines the groundness of the second one. Since both the abstract arguments are ground, this information is relevant only because of the deterministic label (d) associated with the hyperarc. The (non-redundant) dependence from the second to the rst argument is irrelevant and can be discarded by the analysis.
Autodependences allow to detect the source of determinism in an atom. As a matter of fact, a deterministic autodependence represents a set of arguments containing at least a deterministic term (i.e. a single choice term). The framework is designed in order to handle general (possibly auto) dependences. However, in order to let the paper more readable, in the following we will often omit any (possibly non-redundant) auto dependence in abstract objects, thus showing only the inter-argument dependences, which are more relevant for the analysis point of view. ? ? ? d
Example 3.4 Let I = fp(X; X; d); p(a; b; c)g, abs(I ) = f p(ng; ng; g) g: ?
6
The non-redundant autodependences hf3g; 3id , hf1; 2g; 1id and hf1; 2g; 2id (which are not shown) show that the rst two arguments and the third one deterministically determines their own groundness (the sets of argument positions f1; 2g and f3g are the two sources of the determinism for the predicate p in I ). As a matter of fact, for any concrete atom p(t1; t2; t3) having either the rst two arguments or the third one ground, there exists at most one possiblity of unifying it with atoms in I . Let I = fp(f (X; Y ); Z; g(X; Y; Z )); p(a; b; c)g, the dependence representation associated with abs(I ) is shown in example 3.1, where v1 = 1, v2 = 2 and v3 = 3.
Analogously we can de ne a monotonic abstraction map abs : 2SubstV ! ]V . There are no concretization maps conc such that (abs; conc) is a Galois connection between
interpretations and -interpretations. Thus, in order to de ne a Galois connection between the concrete and the abstract domain of computation, we have to consider sets of interpretations as single objects. Let us consider the following partial ordering relations de ned on sets of interpretations and -interpretations respectively: 8D1; D2 2 2(2BP ); D1 D2 i (8I1 2 D1 9I2 2 D2 j I1 I2) ^ (8I2 2 D2 9I1 2 D1 j abs(I2) abs(I1) ) D1 D2), 8D1]]; D2] 2] 2] ;] D1] ] D]2] i ] (8I1 2 D1; 9I2 2 D2 j I1 I2) ^ (8I2] 2 D2] ; 9I1] 2 D1] j I2] I1] ) D1] D2] ). It is easy to prove that (2(2BP ); ) and (2] ; ) are complete lattices.
Proposition 3.2 The pair of functions (; ) such that : 2(2BP ) ! 2] where (D) = fabs(I ) j I 2 Dg, : 2] ! 2(2BP ) where (D]) = fI j 9I ] 2 D] ^ abs(I ) = I ]g, holds the conditions on the Galois insertion [3] between (2(2BP ); ) and (2] ; ).
3.4 Abstract Uni cation (-mgu)
n
The abstract most general uni er -mgu : BPn BP] ! Subst]V [ffailg, takes two n-tuples of concrete and abstract atoms respectively and returns an abstract substitution which is de ned on the set of variables of the concrete atoms. It spreads ground information on the variables of the concrete atoms and it generates a dependence between a set of variables T and a variable x i there exists a connection, in the abstract atom, between the nodes corresponding with the variables in T and the node corresponding with a term having x as variable. The resulting dependence is deterministic if such a connection contains only deterministic labels. Furthermore, it is enough to have a deterministic connection to return a deterministic dependence.
De nition 3.8 Let E 0 be a connection between a set of nodes N 0 and a node n 2 N in a dependence representation D = (N ; V ; E ). E 0 is relevant i lubd ((E 0)) 6= ?. The notion of minimality naturally extends to relevant connections (i.e. a minimal relevant connection is any connection E 0 such that 8e 2 E 0 , E 0 =feg is not a relevant connection). Let us consider the concrete and the abstract atoms, p(t1; :::; tn) and p(Dp ) respectively, where Dp = (f1::ng; Vp; Ep) and let R f1; :::; ng be the set of indexes such that 8i 2 R; V ar(ti) = ;. We de ne a dependence representation associated with the uni cation process: D?mgu = (N?mgu ; V?mgu; E?mgu ) where
N?mgu = f1; :::; ng [ V ar(p(t1; :::; tn)) [ f i j i 2 Rg, V?mgu = Vp [ f( i; g) j 9i 2 f1::ng : V ar(ti) = ;g [ f(x; ng) j 9x 2 V ar(t1::tn)g; E?mgu = Ep [ fhf ig; ii?; hfig; ii? j 9i 2 f1::ng : V ar(ti) = ;g [ fhfx1; :::; xkg; ii?; hfig; x1i? ; :::; hfig; xki? j 9i 2 f1::ng : V ar(ti) = fx1; :::; xkgg:
The abstract uni cation process works on this dependence representation and consists of the following two steps: 1. Ground information propagation It is an iterative process on the hypergraph G?mgu = (N?mgu; E?mgu ) which changes the labels in V?mgu. The process replaces any labelled node (v; ng) 2 V?mgu with (v; g) i there exists a hyperarc e 2 E?mgu such that Head(e) = v and 8vi 2 Tail(e) : (vi; g) 2 V?mgu [1]. The process halts when no more labels can be changed. 2. -substitution de nition This process considers the transformed hypergraph and returns an abstract substitution #] = (V ar(t1; :::; tn); V#; E#) such that V# = f(x; t]) 2 V?mgu j x 2 V ar(t1; :::; tn)g, 8xk 2 V ar(t1; :::; tn); hT; xk il 2 E# i hT; xk il is non-redundant in #] and if E is the set of minimal relevant connections from T to xk , then E 6= ; and l = glbd lubd ((E 0)) E 0 2 E . The condition lubd (()) 6= ? avoids the introduction of irrelevant hyperarcs, labelled by ?. As a matter of fact, we are only interested in those connections E 0 containing at least a hyperarc in Ep. Notice that D?mgu can be redundant.
n
o
(X; ng)
? 6 ?
, and E10 = fhfX g; 1i? ; hf1g; X i? ; hf1g; 1i? g, E20 = Example 3.5 Let D?mgu = fhfX g; 1i? ; hf1g; X i? g. 6 Ei0 Note that 8i = 1; 2 : fX g !D?mgu X , but since E20 contains only unde ned (?) hyperarcs, only E10 is \relevant" in the uni cation process. Example 3.6 The ground information propagation applied to the dependence representation associated with the abstract uni cation of p(f (X; Y ); a) and p(ng; ng) is (? labels (1; ng) ?
are omitted):
6
(X; ng) (Y; ng) ( 2; g) KA@ ? A@? A ?
(1; ng) ?
?
(X; g) (Y; g) ( 2; g)
6
A@ ? K A@? A ?
=)
?
(2; ng)
(1; g)
6
?
6 ?
(2; g)
6
It is easy to extend the previous abstract uni cation algorithm to any n-tuple (n 1) of atoms, as shown in the following example. Example 3.7 Let us consider the abstract uni cation of the concrete and abstract atoms ?
?
? ?
p(X; Y ); q(Y; Z; W ); r(Z; X ) and p(ng; ng) , q(ng; ng; ng) , r(ng; ng) . d
d
6
?
Since there exist shared variables between the concrete atoms p(X; Y ), q(Y; Z; W ), r(Z; X ), the uni cation hypergraph contains multiple connections between the corresponding variables and the abstract terms (the predicate symbol is subscripted in any corresponding argument position). The corresponding dependence representation is
(X; ng)
(Y; ng)
KA A
K A A
A AU
(Z; ng) A U A
d
6
(W; ng) 6
K A A A U A
?
(2r ; ng) (1p; ng) (2p; ng) (1q ; ng) (2q ; ng) (1r ; ng) (3q ; ng) 6
d ?
?
6
6
?
The resulting abstract substitution is: (Z=ng; Y=ng; X=ng; W=ng) . ?
6
Proposition 3.3 (-mgu monotonicity)
d
6
d
Let A be an atom. Given a0 pair of abstract atoms A], A0] such that fA]g fA0]g, -mgu(A; A]) -mgu(A; A ]).
3.5 Substitution Application (-apply)
In order to develop a deterministic ground dependence analysis we have to de ne the application of abstract substitutions (returned by the abstract uni cation process) to ]) ( B Subst ] P V concrete atoms, i.e. -apply : 2 ! . Given a set of pairs of concrete atoms (having the same predicate symbol) and abstract substitutions, it returns an abstract atom which collects the] deterministic ground information belonging to this set. Let S 2 2(BP SubstV ) such that S # p = f(p(t1 ; :::; t1n); #]1); :::; (p(tm ; :::; tmn); #]m)g, and let Nij = fx(j ) j x 2 V ar(tij )g be the set of variables in tij , indexed according to the corresponding argument position. For each pair (p(ti ; :::; tin); #]i) where #]i = (N#i ; V#i ; E#i ), we generate a dependence representation D?applyi = (N?applyi ; V?applyi ; E?applyi ) such that: 1
1
1
n N?applyi = N#i [ (j[=1 Nij );
V?applyi = V#i [ f(x(j ); ng) j 9j = 1::n : x(j ) 2 Nij g; E?applyi = E#i [ fhfx(j )g; xi?; hfxg; x(j )i?j9(x; t]) 2 V#i ; 9j = 1::n : x(j ) 2 Nij g (if 9(x; t]) 2 V#i ; 9j = 1::n : x(j ) 2 Nij , x(j ) is a linked variable). Example 3.8 The dependence representation associated with the pair ? d
? d
(p(f (X; Y ); Z; f (X; U )); (Z=ng; U=ng; Y=g ) ) is: d
6
(X (1); ng) (Y (1); ng) (Z (2); ng) (X (3); ng) (U (3); ng) F ree
6
6
?
(Z; ng)
(Y; g)
6 d
?
d
6
F ree
6 ?
(U; ng) 6 d
Let us consider a ground information propagation on G?applyi = (N?applyi ; E?applyi ), i = 1; :::; m, which is analogous to the -mgu case. We de ne
-apply(S ) =p2Pred [ -apply(S # p) S # p = ; , such that D = (f1::ng; V ; E ) and where -apply(S # p) = p;(D ) ifotherwise p p p p (
8(j; t]j ) 2 Vp : t]j =
8 > > >
> > :
ng otherwise; e = hfr1; :::; rkg; hil 2 Ep i e is non-redundant in Dp and for each i = 1; :::; m:
{ Free(Nih ) u=r[kr Free(Niu ), where Free(Nij ) is the set of variables x such that x(j ) is not linked with any node in the hypergraph G?applyi ; { for each linked node y(h) 2 Nih , there exists T u=r[kr Niu such that, if EiTy h is the set of minimal relevant connections from T to y(h) in G?applyi , then EiTy h =6 ;; d i 8z; f 2 f1; :::; mg; z = 6 f : mgu(tzr ; :::; tzrk ; tfr ; :::; tfrk ) = fail; rk 9y(h) 2 Nih ; 9T u=[r Niu : EiTy h =6 ; ^ { l= d = glbd lubd ((E 0)) E 0 2 EiTy h ) ; 1
( )
1
( )
8 > > > > > < > > > > > :
1
1
n
? otherwise:
( )
1
o
( )
Example 3.9 The following examples show the results of the application of the -apply
operator in some interesting cases: ?
?
S1 = f(p(X; Z ); (X=ng; Z=ng) ); (p(f (X ); K ); (X=ng; K=ng) )g; -apply(S1) = d
d
?
f p(ng; ng) g; ?
? d
? d
S2 = f(p(f (X; Y ); Z; f (X; U )); (Z=ng; U=ng; Y=g) ); (p(0; X; X ); ;)g, -apply(S2) = d
d
6
?d ?
f p(ng; ng; ng) g (see example 3.8). 6 6
?
?
Let I be a nite set of atoms, and S = I f;g. By -apply de nition we have -apply(S ) = abs(I ).
Proposition 3.4 (-apply monotonicity)
Given two sets of pairs of concrete atoms (having the same predicate symbol) and 0] 0] ] ] abstract substitutions f(A1; #1); :::; (Ak; #k )g and f(A1; #1 ); :::; (Ak; #k )g, such that 8i = 0] ] 1; :::; k : #i #i , it follows that
-apply(f(Ai; #]i) j i = 1; :::; kg) -apply(f(Ai; #0i]) j i = 1; :::; kg).
3.6 Abstract Interpretation
The approximation of the concrete xpoint semantics is given in terms of the xpoint of a nitely converging monotonic operator de ned in terms of -mgu and -apply.
De nition 3.9 Given a logic program P , let I ] be an interpretation. The abstract im] ] mediate consequence operator associated with the program P , TP : ! ], is de ned as follows
9 p(t) : ?B1; :::; Bn 2 P; 9B010]; :::; Bn0] 2 I ]; ). TP] (I ]) = -apply( (p(t); #]) #] = ? mgu(B1; :::; Bn; B1]; :::; Bn0]); ] # = 6 fail 8 > > < > > :
9 > > =
> > ;
The correctness of TP] (abs(TP (I )) TP] (abs(I ))) follows by the correctness of -mgu and -apply. Let lfp(f ) denote the least xpoint of a given function f . Since the lattice (]; ) is nite, there exists a nite positive number k such that F ](P ) = lfp(TP] ) = TP] " k. Let us consider the concrete and the abstract operators TP : 2(2BP ) ! 2(2BP ), such that TP (D) = fTP (I ) j I 2 Dg, TP] : 2] ! 2] such that TP] (D] ) = fTP] (I ]) j I ] 2 D]g. It is esay to prove that for each logic program P , fF (P )g and fF ](P )g are xpoints of TP and TP] respectively (TP " ! = fF (P )g and TP] " k = fF ](P )g). Thus, by TP] correctness: (TP " !) TP] " k (or equivalently [3] TP " ! (TP] " k)).
4 Applications In the following example [14] we show the overall analysis by describing the composition of the operators de ned in Section 3. Consider the predicate r de ned as follows r(g(X ); g(X )) : ?: r(f (X ); g(Y )) : ?r(Y; X ): Let us examine the behaviour of our analysis: ?
Tr] (;) = -apply(f(r(g(X ); g(X )); ;)g) = f r(ng; ng) g. 1
d
6
d
In the rst step, the abstract uni cation returns the pair (r(g(X ); g(X )); ;) only. A ground dependence is detected between the rst and the second argument and on the other way round by -apply. Since the argument of -apply is a set of pairs including only one element, the resulting dependences are labelled as deterministic. ?
?
Tr] (;) = -apply(f(r(g(X ); g(X )); ;); (r(f (X ); g(Y )); (X=ng; Y=ng) )g = f r(ng; ng) g. 2
d
6
d
d
6
?
In the second step, -apply considers two pairs: the one returned by the previous step
and the one produced by the abstract uni cation of the abstract atom returned by the previous step with the concrete atom in the body of the non-unit clause of r. Since the second arguments of the concrete atoms in the pairs unify, this step returns a single deterministic dependence from the rst argument to the second one. ?
?
Tr] (;) = -apply(f(r(g(X ); g(X )); ;); (r(f (X ); g(Y )); (X=ng; Y=ng) )g = f r(ng; ng) g. ?
3
6
d
?
6
?
The third step ( xpoint) detects that the dependence between the rst and the second argument is a non-deterministic one. As in the previous steps, the rst two arguments do not unify, but, since no deterministic dependence exists between the abstract terms associated with the variables X and Y , no deterministic dependence is returned. Let us consider the program sumlist([]; 0) : ?: sumlist([X jXs]; S ) : ?sumlist(Xs; Ss); plus(X; Ss; S ): select(X; [X jXs]; Xs) : ?: select(X; [Y jY s]; [Y jZs]) : ?select(X; Y s; Zs): check(X; Y ) : ?select(Y; X; R); sumlist(X; Y ): The predicate sumlist(X; Y ) is such that Y is the sum of the elements of the list X . The predicate plus is assumed to be a prede ned one. The dependences of plus relevant for the analysis are a deterministic one from the rst and the second argument to the third one and a non-deterministic one from the third argument to the rst and the second, respectively. The list X includes relative numbers. The predicate select(X; L1; L2) is such that L2 is the list L1 where exactly one occurrence of X has been removed. The predicate check(X; Y ) is such that Y is an element E of the list X and E is equal to the sum of the elements of X . The dependences detected by the analysis of sumlist and of select may be easily deduced from the declarative meaning of the predicates (only some dependences of sumlist and select are shown): ?
sumlist(ng; ng) d
6
?
? ?
select(ng; ng; ng) . 6?
?
6
The dependences of sumlist and select are propagated, through abstract uni cation, to the body of check. In the hypergraph built by abstract uni cation, the variable Y is the target of two paths. Both of them have the variable X as source node, but only one is labelled by d. In this case, a deterministic dependence between the variables X and Y is returned. Therefore, there exists a deterministic dependence between the rst and the second argument of check. As a matter of fact, since the sum of the elements in the list is uniquely determined, all the elements selected from the list are equivalent, hence the dependence may be considered as a deterministic one. The analysis infers deterministic dependences among arguments rather than determinacy of whole predicates. Let us consider the following modi ed version of predicate check: check(X; Y; R) : ?select(Y; X; R); sumlist(X; Y ):
In this case also a non-deterministic dependence is detected between the rst argument and the third one. As a matter of fact, if a list includes several elements equal to its sum, distinct lists are returned even by removing the same element. ? ? d ?
? d
check(ng; ng) , check(ng; ng; ng) . 6?
6?
?
6
The two former examples show that our analysis can return deterministic dependences not detected by previous analyses. In particular, the last example shows the importance of de ning deterministic dependences among arguments rather than for the whole predicate. Let us nally consider the classical example: append([]; X; X ) : ?: append([X jXs]; Y; [X jY s]) : ?append(Xs; Y; Y s): d
?d ?
The analysis detects the following dependences: append(ng; ng; ng) (see Example 3.8 and 3.9). 6 6 ?
?
5 Conclusions Our analysis of determinate computations extends previous proposals in several directions. The ground dependence notion, introduced in the database framework [11], has been extended in order to handle recursive clauses. Hence, the analysis returns signi cative results not only in the case of database programming, but also in other cases, such as programs manipulating recursive data structures. We recall that determinacy is inferred with respect to arguments rather than to whole predicates. This means that a ground instance of a set of arguments may uniquely determine another set of arguments of a predicate, while producing multiple bindings for other arguments. With respect to [14], our proposal can detect both ground and ground deterministic dependences in a single step, and requires no knowledge of the types of the predicates nor of the activating modes of a predicate. Our abstract interpretation can be easily integrated with further analyses, such as depth-k [13], type and general functional ones. By pairing our analysis with a term structure one (e.g. depth-k), dependences among subterms may be detected. Furthermore, it is possible to relax the ground dependences condition in order to obtain a more general analysis of functional dependences.
References [1] R. Barbuti, R. Giacobazzi, and G. Levi. A General Framework for Semantics-based Bottomup Abstract Interpretation of Logic Programs. Technical Report TR 12/91, Dipartimento di Informatica, Universita di Pisa, 1991. To appear in ACM Transactions on Programming Languages and Systems. [2] C. Berge. Graphs and Hypergraphs. North-Holland, 1973.
[3] P. Cousot and R. Cousot. Abstract Interpretation: A Uni ed Lattice Model for Static Analysis of Programs by Construction or Approximation of Fixpoints. In Proc. Fourth ACM Symp. Principles of Programming Languages, pages 238{252, 1977. [4] S. Debray and D.S. Warren. Functional Computations in Logic Programs. ACM Transactions on Programming Languages and Systems, 11-3:451{481, 1989. [5] B. Demoen, P. VanRoy, and Y.D. Willems. Improving the Execution Speed of Compiled Prolog with Modes, Clause Selection and Determinism. In H. Ehrig, R. Kowalski, G. Levi, and U. Montanari, editors, Proc. TAPSOFT 1987, volume 250 of Lecture Notes in Computer Science, pages 111{125. Springer-Verlag, Berlin, 1987. [6] P. Deransart and J. Maluszynski. Relating Logic Programs and Attribute Grammars. Journal of Logic Programming, 2:119{156, 1985. [7] M. Falaschi, G. Levi, M. Martelli, and C. Palamidessi. Declarative Modeling of the Operational Behavior of Logic Languages. Theoretical Computer Science, 69(3):289{318, 1989. [8] R. Giacobazzi and L. Ricci. Pipeline Optimizations in AND-Parallelism by Abstract Interpretation. In D. H. D. Warren and P. Szeredi, editors, Proc. Seventh Int'l Conf. on Logic Programming, pages 291{305. The MIT Press, Cambridge, Mass., 1990. [9] J. W. Lloyd. Foundations of Logic Programming. Springer-Verlag, Berlin, 1987. Second edition. [10] M.J. Maher and R. Ramakrishnan. Deja Vu in Fixpoints of Logic Programs. In E. Lusk and R. Overbeck, editors, Proc. North American Conf. on Logic Programming'89, pages 963{980. The MIT Press, Cambridge, Mass., 1989. [11] A.O. Mendelzon. Functional Dependencies in Logic Programs. In Proc. of the Eleventh International Conference on Very Large Data Bases, pages 324{330, 1985. [12] L. Ricci. Compilation of Logic Programs for Massively Parallel Systems. PhD thesis, Universita di Pisa, Feb. 1990. T.D. 3-90. [13] T. Sato and H. Tamaki. Enumeration of Success Patterns in Logic Programs. Theoretical Computer Science, 34:227{240, 1984. [14] J. Zobel and P. Dart. On Logic Programs, Functional Dependencies, and Types. Technical report, University of Melbourne, 1990.