Compositionality in SLD-derivations and their abstractions Marco Comini Giorgio Levi and Maria Chiara Meo Dipartimento di Informatica Universita di Pisa Corso Italia 40 56125 Pisa Italy fcomini levi meog@di.unipi.it Abstract We dene a semantic framework to reason about compositional properties of SLD-derivations and their abstractions (observables). The framework allows us to address problems such as the relation between the (top-down) operational semantics and the (bottom-up) denotational semantics the existence of a denotation for a set of denite clauses and their properties (compositionality w.r.t. various syntactic operators correctness and minimality). This leads us to a exible classication of the observables where we can reason about properties such as OR-compositionality and existence of abstract transition system. Using abstract interpretation techniques to model abstraction allows us to state very simple conditions on the observables which guarantee the validity of several general theorems. Keywords: SLD-derivations abstract semantics compositionality. 1 Introduction Our goal is the denition of a semantic framework which allows us to reason about properties of SLD-derivations and their abstractions (observables) in the case of denite logic programs. The issues we want to be able to address in the framework are the following the relation between the (top-down) operational semantics and the (bottom-- up) denotational semantics the existence of a denotation for a set of denite clauses the properties of the denotation (compositionality w.r.t. various syntactic operators correctness and minimality).
Similar problems have been studied in detail for various abstractions of SLD-derivations. Computed answers were considered in [6 7] call patterns and partial answers were considered in [11] and resultants were considered in [10] (see [2] for a survey of the approach). Moreover a framework where one can dene denotations modeling various observables (thus inheriting the basic constructions and results) was given in [9 8] by dening abstractions by means of equivalence relations. The same problem was tackled by [3] for CLP and by [12] by modeling abstractions according to abstract interpretation theory. This means that it is possible to accommodate in the framework observables which are useful for (approximate) program analysis. In this paper we build upon the idea in [3] of providing an algebraic characterization of the observables. We extend the approach by taking two basic semantics: a denotational semantics and a transition system which dene SLD-derivations. In addition our semantic properties are expressed as compositionality properties. This leads us to a more exible classication of the observables where we can reason about properties such as OR-compositionality and existence of abstract transition system. Using abstract interpretation techniques to model abstraction allows us to state very simple conditions on the observables which guarantee the validity of several general theorems. In Section 3 we dene the denotational semantics and the transition system for SLD-derivations in terms of four semantic operators directly related to the syntactic structure of the language. In Section 3.4 we prove several properties of SLD-derivations. In Section 4 we dene the observables as Galois insertions and characterize two classes of observables in terms of simple properties of the Galois insertion and of the basic semantic operators. The rst class (perfect observables of Section 4.4) has all the properties of SLD-derivations. In the other class the properties are preserved only for the denotational semantics (denotational observables of Section 4.5). The reconstruction of an existing semantics or the construction of a new semantics in the framework requires just a few very simple steps. i. The formalization of the property we want to model as a Galois insertion h; i between SLD-derivations and the property domain. ii. The verication of very simple algebraic properties relating h; i and the basic semantic semantic operators on SLD-derivations to assign the property to the right class of observables. iii. Depending on the class we automatically obtain the new denotational semantics transition system top-down and bottom-up denotations together with several interesting theorems (equivalence compositionality w.r.t. the various syntactic operators correctness and minimality of the denotations). Since it is based on standard operational and denotational semantic denitions the framework can be adapted to other programming languages.
2 Preliminaries In the following sections we assume familiarity with the standard notions of logic programming as introduced in [1] and [17]. We denote by G and B possibly empty sequences of atoms by t; x tuples of respectively terms and distinct variables. Moreover we denote by t both the tuple and the set of corresponding syntactic objects. B; B 0 denotes the concatenation of B and B 0. The set of all atoms is denoted by Atoms and the set of all goals is denoted by Goals. The empty goal is denoted by. We consider idempotent substitutions only. Subst is the set of all idempotent substitutions. Given a substitution # 2 Subst dom (#) = fx j #(x) 6= xg is the domain of # and range (#) = f y j x 6= #(x); y 2 var (#(x))g is the range of #. If # 2 Subst is a substitution and E is a syntactic expression #j E is the restriction of # to the variables var (E) of E. # n G # 1?! : : :?! G n : : : ; (n 0) denotes a (partial) SLD-derivation of goal G via c 1 c n the leftmost selection rule. The derivation uses the renamed apart clauses c 1 ; : : : ; c n and # = (# 1 # n ) j G is the computed answer substitution of G. Given a derivation d rst (d) and last (d) are respectively the rst and the last goal of d answer (d) is the computed answer substitution of d. We denote a zero-length derivation of G by G itself. Given two derivations d and d 0 such that last (d) = rst (d 0 ) = G and var (d) \ var (d 0 ) = var (G) d :: d 0 denotes the derivation obtained by concatenation of d and d 0 and clauses (d) is the set of clauses of d. is the equivalence modulo variance (extended also to derivations). 3 The basic framework In the following for the sake of simplicity we consider the PROLOG leftmost selection rule. All our results can be generalized to local selection rules. When we want to formalize program execution we must take into account in addition to the inference rules which specify how derivations are made the properties we observe in a computation (observables). An observable is any property which can be extracted from a goal computation i.e. observables are abstractions of SLDtrees. We assume the reader to be familiar with the notions of SLD-resolution and SLD-tree (see [17 1]). We represent here for notational convenience SLD-trees as sets of derivations. i. We dene a preorder on derivations: d d 0 i rst (d) = rst (d 0 ) and there exists a renaming such that d is a prex of d 0. ii. Let be the equivalence relation induced by. We denote by Deriv the set of all derivations modulo. In the following we denote the equivalence class of a derivation d by d itself and derivations are always intended as equivalence classes of derivations.
iii. S Deriv is well-formed i d 2 S implies 8d 0 2 Deriv d 0 d ) d 0 2 S. iv. A collection D is a set of pairs hg; Si such that (a) for any hg; Si 2 D S 6= ; is a well-formed set of derivations starting from the goal G (b) if hg 1 ; S 1 i and hg 2 ; S 2 i are distinct elements of D then G 1 6= G 2. Denition 3.1 D is the domain of all the collections ordered by v where D v D 0 if and only if 8hG; Si 2 D; 9hG; S 0 i 2 D 0 : S S 0. The partial order on D formalizes the evolution of the computation process. It is easy to prove that (D ; v) is a complete lattice. Proposition 3.2 Each collection where all the clauses used in derivations are in P is a representation of a family of partial SLD-trees of P. Moreover each maximal collection in P is a representation of the family of all SLD-trees of G in P for each goal G 2 Goals. Note that every clause c = p(t) :? B of P can be viewed as the \one step" collection tree (c) = f hp(x); f p(x); p(x) f?! x=t g B gi g where x is a tuple of new distinct c variables. In the following we often denote the derivation associated to the clause c by c itself. Moreover every goal G can be viewed as the collection f hg; f G gi g containing the \zero step" derivation G. The goal we want to achieve is to develop a denotation modeling partial SLDtrees. We follow the approach in [3 2] by dening a \syntactic" semantic domain (interpretation). Our modeling of partial SLD-trees is similar to the basic denotation dened in terms of clauses in [8 9]. Denition 3.3 A most general collection is a collection such that each initial goal is a most general atomic goal. An interpretation I is a most general collection modulo variance. We denote by I the set of interpretations. (I; v) is a complete lattice. Xj I represents the (greatest) most general sub-collection of any X 2 D =. Note that in interpretations the variance relation allows us to abstract w.r.t. the variables occurring in the initial goals of any collection. 3.1 Denotational semantics of SLD-derivations Our algebraic framework is dened by a set of semantic operators obtained from a denotational denition of derivations. We start with some notation. We denote the equivalence class (modulo variance) of a collection by itself. Moreover any subset I of I is implicitly considered also as an arbitrary collection obtained by
QUERY ::= GOAL in PROG GOAL ::= j ATOM ; GOAL PROG ::= ; j f CLAUSE g [ PROG CLAUSE ::= ATOM :? GOAL Figure 1: Syntax of logic programs choosing for each equivalence class 2 I an arbitrary representative of. All the operators that we will use on interpretations are independent from the choice of the representative. Therefore we can dene any operator on I in terms of its counterpart dened on D independently from the choice of the representative. All the denitions will be independent from the choice of the syntactic object. To simplify the notation we denote the corresponding operators on I and D by the same name. Several denotational semantics have been dened for logic programs (see for example [13 14 5]). The main dierences w.r.t. the above denitions are that we do not consider the PROLOG search rule and that our denotations model SLD-- derivations rather than just computed answers. We dene the denotational semantics inductively on the syntax of logic programs of Figure 1 (the syntactic structure of atoms is not dened). We express denotations in algebraic notation. This will be useful in Section 4 for reasoning about the abstract denotations. The choice of the semantic operators is induced by the syntactic operators due to the compositional nature of denitions in the denotational style. The informal meaning of the operators is the following. The operator \solves" an atomic goal A into an interpretation I. The operator computes the conjunction of two denotations. The operator on computes the denotations obtained by replacement. Finally the operator P computes the non-deterministic union of a class of denotations. Note that when the class is nite we use the inx notation +. Q : QUERY! D ; Q [G in P ] = G [G] lfp P [P ] G : GOAL! (I! D ); G [] I = f h; f gi g A : ATOM! (I! D ); P : PROG! (I! I); G [A; G] I = A [A] I G [G] I A [A] I = A I P [;] I = Idj I P [fcg [ P ] I = C [c] I + P [P ] I C : CLAUSE! (I! I); C [p(t) :? B] I = tree (p(t) :? B) on G [B] I : In order to dene the semantic operators we rst we need to dene an auxiliary operation on derivations. This operation formalizes the instantiation of a derivation. # 0 1 Let d = G 0 0?! : : : be a derivation and let be a substitution such that c 1 var (G 0 # 0) \ var (clauses (d)) = ;. Then d = 1 G 0?! : : : is the maximal c 1 derivation such that G 0 = G 0 0 and 8i > 0:
i. i = mgu (G i?1 ; G 0 i?1# 0 i) s.t. var (range ( i ))\var (G 0 i?1) var (G i?1 ) ii. # i = (# 0 i i )j (A;c i ) where A is the rst atom in G i?1 and iii. G i = G 0 i i : Let D; D 1 ; D 2 be collections in D and A be an atom. Id is the collection of zero-- length derivations for each goal f hg; f G gi j G 2 Goals g. The instantiation of D with A is A D = f ha; Si j S = f d j ha 0 ; Si is a renamed apart (from A) version of an element of D d 2 S and there exists such that A = A 0 g; S 6= ; g: The product of D 1 and D 2 is D 1 D 2 = f hg; Si j S = f d j G = (G 0 0; G 00 0) hg 0 0; S 0 i 2 D 1 hg 00 0; S 00 i 2 D 2 d 1 = G 0 # 1 0?! : : : 2 S 0 c 1 d 2 2 S 00 var (d 1 ) \ var (d 2 ) = var (G 0 0) \ var (G 00 0) and if exists k s.t. G 0 k = then d = d 0 :: d 2 (# 1 # k ) else d = d 0 where d 0 # = 1 G 0?! : : : is the derivation c 1 i. G 0 = (G 0 0; G 00 0) ii. 8i > 0 G i = G 0 i; G# i if there exists G s.t. G i?1 = G 0 i?1; G and G 0 6= g i?1 S 6= ; g. The replacement of D 2 in D 1 is D 1 on D 2 = f hg; Si j hg; S 1 i 2 D 1 S = S 1 [ f d :: d 0 j d 2 S 1 hlast (d); S 2 i is a renamed version of an element in D 2 d 0 2 S 2 and var (d) \ var (d 0 ) = var (rst (d 0 )) g. The sum of a class f D i g i2i is X f Di g i2i = f hg; Si j S = [f S i j hg; S i i 2 D i ; i 2 I g; S 6= ; g: In inx notation D 1 + D 2 = P f D 1 ; D 2 g. Note that D 1 v D 2 D 1 + D 2 = D 2 and that the lub operation on (D ; v) coincides with P. The operation P can be naturally extended to each domain of set of pairs where the second component is a set. In the following we call such an operation pair set union. Note that if D D 0 then A D = A D 0 because of the renaming apart property and the \collecting" nature of.
3.2 Operational semantics The operational semantics is described in terms of a transition system T = (D ; T P ). The rules of T are hg; Si 2 D; G # 1?! # n : : :?! (A; G 0 ) 2 S; c 2 P; c 1 c n D @ D on ((A tree (c)) f hg 0 ; f G 0 gi g) T P (D; D on ((A tree (c)) f G 0 g)) T P (D 1 ; D 0 1); T P (D 2 ; D 0 2) T P (D 1 + D 2 ; D 0 1 + D 0 2) The initial states of T are all the collections of SLD-derivations of zero-length while the nal states are all the collections of SLD-refutations. It can easily be shown that the transition system T denes the usual notion of SLD-derivation. Its specicity is due to the fact that we have dened it using the same semantic operators used in the denotational denition. Note that the rst transition rule can be expressed in a more general fashion like D 2 D ; c 2 P; A 2 Atoms; D @ D on ((A tree (c)) Id) T P (D; D on ((A tree (c)) Id)) thus obtaining an equivalent transition relation. Since we are interested in all the SLD-derivations of a query G in P we dene its behavior as B [G in P ] = X f D j T P(f hg; f G gi g; D) g where T is the reexive and transitive closure of P T P. As we will see in the following (Corollary 3.9) the operational and denotational semantics of each query are the same. We dene the equivalence of two programs P 1 P 2 as P 1 P 2 () 8G 2 Goals; B [G in P 1 ] = B [G in P 2 ]: 3.3 The program denotation We can note that the semantic function P [P ] is essentially the \bottom-up" T P operator in the case of derivations. Using standard techniques it can be proved that P [P ] is continuous hence we can dene the xpoint denotation of the program P as the interpretation F [P ] = lfp P [P ] = P [P ] "!. This can be viewed as a program denotation since it is the semantics of the program as a set of denite clauses (or a set of procedure denitions). We can also dene a top-down program denotation. This can be obtained by collecting the behaviors for all most general atomic goals i.e. the behaviors of
the procedures with no constraints on the inputs. The top-down SLD-derivations denotation of a program P is the interpretation O [P ] = X f B [p(x) in P ] = g p(x)2goals: In the following we will prove that F [P ] and O [P ] are equivalent. Now we give two denitions to relate program equivalences to denotations. Let S [P ] be a program denotation (F [P ] or O [P ]) and be a program equivalence. Then i. S is correct w.r.t. if S [P 1 ] = S [P 2 ] =) P 1 P 2 ii. S is minimal w.r.t. if P 1 P 2 =) S [P 1 ] = S [P 2 ]. 3.4 Semantic properties of SLD-derivations We show that the program denotation O [P ] has several interesting properties which can all be viewed as compositionality properties. The rst compositionality result is a theorem which shows that the semantic function B is compositional w.r.t. procedure calls (atomic goals) and composition (conjunction) inside goals (AND-- compositionality). Theorem 3.4 Let A be an atom G 1 G 2 B [A in P ] = A O [P ]; B [G 1 in P ] = B [G 1 in P ] B [G 2 in P ]: be goals and P be a program. Then Corollary 3.5 Let A be an atom G be a goal and P be a program. Then B [ in P ] = f h; f gi g and B [A; G in P ] = A O [P ] B [G in P ]: The above closure property of O w.r.t. B allows us to show that the denotation O is correct and minimal w.r.t.. Corollary 3.6 Let P 1 P 2 be two programs. Then P 1 P 2 () O [P 1 ] = O [P 2 ]: By using the replacement operator we can dene an operator ] which computes the OR{composition of two denotations. Namely D 1 ] D 2 = fd 1 + D 2 g where fd g is the least solution of the equation fd g = Idj I + P f fd g on ((A D) Id) g A2Atoms : The following theorem shows the next compositionality property. Namely O [P ] is compositional w.r.t. [ (OR-compositionality). Theorem 3.7 Let P 1 P 2 be programs. Then O [P 1 [ P 2 ] = O [P 1 ] ] O [P 2 ]: The top-down and the bottom-up denotations are indeed equivalent.
Theorem 3.8 Let P be a program. Then O [P ] = F [P ]: The following theorem asserts the equivalence between the denotational and the operational semantics. Corollary 3.9 Let G be a goal and P be a program. Then Q [G in P ] = B [G in P ]: 4 The abstraction framework The properties we found for O and F allow us to claim that we have a good denotation modeling SLD-derivations. Our goal however is to nd the same results for the denotations modeling more abstract observables. We want then to develop a theory according to which the semantic properties of SLD-derivations shown in subsection 3.1 are inherited by the denotations which model abstractions of the SLDderivations. We will model the abstractions by using the Abstract Interpretation theory [4]. 4.1 Galois Insertions and Abstract Interpretation Abstract Interpretation is a theory developed to reason about the abstraction relation between two dierent semantics. The theory requires the two semantics to be dened on domains which are complete lattices. (C; ) (concrete domain) is the domain of the concrete semantics while (A; ) (abstract domain) is the domain of the abstract semantics. The partial order relations reect an approximation relation. The two domains are related by a pair of functions (abstraction) and (concretization) which form a Galois Insertion. Denition 4.1 (Galois Insertion) Let (C; ) be the concrete domain and (A; ) be the abstract domain. A Galois insertion is a pair of maps : C! A and : A! C such that i. and are monotonic ii. 8x 2 C x ( )(x) and 8y 2 A ( )(y) = y. Given a Galois Insertion between the concrete and the abstract domain and a concrete semantics we want to dene an abstract semantics. The concrete semantics is the least xpoint of a semantic function F : C! C. The abstract semantic function ~F : A! A is correct if 8x 2 C F (x) ( ~F ((x))). The same property holds for the least xpoints of F and ~F. F is in turn dened as composition of \primitive" operators. Let f : C n! C be one such an operator and assume that ~f is its abstract counterpart. Then f ~ is locally correct w.r.t. f if 8x 1 ; : : : ; x n 2 C
f(x 1 ; : : : ; x n ) ( f((x ~ 1 ); : : : ; (x n ))). The local correctness of all the primitive operators implies the global correctness. Hence we can dene an abstract semantics by dening locally correct abstract primitive semantic functions. An abstract computation is then related to the concrete computation simply by replacing the concrete operators by the corresponding abstract operators. According to the theory for each operator f there exists an optimal (most precise) locally correct abstract operator ~f dened as f(y ~ 1 ; : : : ; y n ) = (f((y 1 ); : : : ; (y n ))). However the composition of optimal operators is not necessarily optimal. The abstract interpretation theory allows us to model the approximation process which is typically involved in program analysis. In this paper we are only concerned with dierent semantics and therefore we look for precise abstractions. The abstract operator f ~ is precise if 8x 1 ; : : : ; x n 2 C; (f(x 1 ; : : : ; x n )) = f((x ~ 1 ); : : : ; (x n )). Hence the optimal abstract operator f ~ is precise if and only if (f(x 1 ; : : : ; x n )) = (f(( )x 1 ; : : : ; ( )x n )). So the precision of the optimal abstract operators can be reformulated in terms of properties of and the corresponding concrete operator. The above denitions are naturally extended to \primitive" semantic operators from P(C) to C. 4.2 The observables An observable property domain is a set of properties of the derivation with an ordering relation which can be viewed as an approximation structure. An observation consists in looking at an SLD-derivation and then extracting some property (abstraction). Our concrete domain D is then the domain of all the collections. We have already shown that (D ; v) is a compete lattice. Therefore the observable is a function from D to a suitable property domain D which preserves the approximation structure. Such a function must be a Galois connection. Denition 4.2 Let (D; ) be a complete lattice. A function : D! D is an observable if there exists s.t. (; ) : (D ; v)! (D; ) is a Galois insertion and 8D; D 0 2 D D D 0 () ((D)) ((D 0 )). The last condition states that the observation does not depend on the choice of the variable names. Hence we can dene an abstract variance relation ~ as S ~ S 0 () (S) (S 0 ). Note that in the following if there exists a bijective Galois connection between two domains we identify them. Each observable induces an observational equivalence on programs. Namely P 1 P 2 i 8G 2 Goals (B [G in P 1 ]) = (B [G in P 2 ]) i.e. if P 1 and P 2 cannot be distinguished by looking at their abstract behaviors. Note that whenever we have two equivalent observables the induced equivalences on programs are the same but the converse does not hold. For example P 1 P 2 i P 1 P 2. Therefore the equivalence on programs induced by the SLD-derivations semantics is the same as the syntactical equivalence on programs (variance). However we can observe much more properties in SLD-derivations than in programs.
Example 4.3 If denotes computed answer substitutions we can take D = P(Goals P(Subst)) as abstract domain and dene (; ) : D! D as (D) = f hg; i j hg; Si 2 D = f # j G # 1?! c 1 : : : #?! n 2 S and c n # = (# 1 # n )j G g 6= ; g. (X) = f hg; Si j S = f d j rst (d) = G last (d) = ; hg; i 2 X answer (d) 2 g [ f d j rst (d) = G; last (d) 6= g; S 6= ; g: It is easy to see that P 1 P 2 i for any goal G G has the same computed answer substitutions in P 1 and in P 2. 4.3 From the observables to the abstract semantics Once we have an observable : D! D we want to systematically derive the abstract semantics. The idea is to dene the optimal abstract versions of the various semantic operators and then check under which conditions (on the observable) we obtain the optimal abstract semantics. This will allow us to identify some interesting classes of observables. We start by dening the optimal abstract counterparts of the basic operators dened on I. Hence 8S; S 1 ; S 2 ; S i 2 D ~ P f Si g i2i = ( P f (S i ) g i2i ) A~ S = (A (S)) S 1 ~ S 2 = ((S 1 ) (S 2 )) S 1 ~on S 2 = ((S 1 ) on (S 2 )). Once we have the optimal abstract operators we can dene the corresponding abstract semantics obtained from the denotational and operational semantics of SLDderivations by replacing the basic semantic operators by their optimal abstract versions. Q [G in P ] = G [G] lfp P [P ] G [] S = (f g) G [A; G] S = A [A] S ~ G [G] S A [A] S = A~ S P [;] S = (Idj I ) P [fcg [ P ] S = C [c] S ~+ P [P ] S C [p(t) :? B] S = (tree (p(t) :? B)) ~on G [B] S ; F [P ] = lfp P [P ] = P [P ] "!: S 2 D; c 2 P; A 2 Atoms; S < S ~on? (A~ (tree (c))) ~ (Id) T P;? S; S ~on? (A~ (tree (c))) ~ (Id)
T P; (S 1 ; S 0 1); T P; (S 2 ; S 0 2) T P; (S 1 ~+ S 2 ; S 0 1 ~+ S 0 2) B [G in P ] = ~ P f S j T P; ((f G g); S) g O [P ] = ~ P f B [p(x) in P ] g p(x)2goals: We are looking for conditions which guarantee that this denition does not lead to a loss of precision. 4.4 Perfect observables The rst class of observables we consider is the class of observables for which both the abstract denotational and the abstract operational semantics are as precise as possible. As a consequence we can equivalently compute the abstract property in a top-down and in a bottom-up way by mimicking the concrete computations. Denition 4.4 Let : D! D be an observable. Then is a perfect observable if i. ( P f D i g i2i ) = ( P f ( )D i g i2i ) ii. (A D) = (A ( )D) iii. (D 1 D 2 ) = (( )D 1 ( )D 2 ) iv. (D 1 on D 2 ) = (( )D 1 on ( )D 2 ). Example 4.5 We consider the computed resultants observable dened by = ~ (D) = f hg; Ri j R = f G# B j hg; Si 2 D # n G # 1?! c 1 : : :?! c n B 2 S; # = (# 1 # n ) j G g R 6= ; g: (X) = f hg; Si j S = wf (f d j rst (d) = G hg; Ri 2 X G# last (d) 2 R # = answer (d) g) R 6= ; g: where wf (S) is the (greatest) well-formed subset of any S Deriv. It is easy to show that and satisfy the conditions of Denition 4.4. Hence computed resultants are a perfect observable. The ~ ~on and ~ operations are A~ X = f ha; Ri j R = f (A B)# j hh; R 0 i is a renamed apart element of X H B 2 R 0 there exists s.t. A = H and # = mgu (A; H 0 ) g R 6= ; g; X 1 ~ X 2 = f hg; Ri j R = f ((G 0 1; G 0 2) B)# j G = (G 1 ) hg 1 ; R 1 i 2 X 1 hg 2 ; R 1 i 2 X 2 r 1 = G 0 B 1 1 2 R 1 r 2 = G 0 B 2 2 2 R 2 var (r 1 ) \ var (r 2 ) var (G 1 ) \ var (G 2 ) and if B 1 6= then # = mgu (G 0 1; G 1 ) and B = (B 1 ) else B = B 2 and # = mgu ((G 1 ); (G 0 1; G 0 2)) g R 6= ; g
X 1 ~on X 2 = f hg; Ri j hg; R 1 i 2 X 1 R = R 1 [ f (G 0 G 3 )# j r 1 = G 0 G 2 2 R 1 hg 2 ; R 2 i 2 X 2 r 2 = G 0 2 G 3 2 R 2 var (r 1 ) \ var (r 2 ) = var (G 2 ) # = mgu (G 2 ; G 0 2) g g. while the optimal ~P operation turns out to be pair set union. Another interesting observable which can be proved to be perfect is the proof trees observable whose corresponding derivation is the Heyting semantics in [16 15]. Note that there exist observables which are not perfect. For example the observable of example 4.3 is not a perfect observable since the axiom (4.4.iv) does not hold. The following theorem shows that the abstract denotational semantics and the bottom-up denotation are precise. Theorem 4.6 Let : D! D be a perfect observable c be a clause A be an atom G be a goal and P be a program. Then (A [A] I ) = A [A] (I ) (G [G] I ) = G [G] (I ) (C [c] I ) = C [c] (I ) (P [P ] I ) = P [P ] (I ) P [P ] is continuous on D (F [P ]) = F [P ] (Q [G in P ]) = Q [G in P ]: The following theorem shows that the abstract transition relation is also precise. Theorem 4.7 Let : D! D be a perfect observable and P be a program. Then 8D; D 0 2 D T P (D; D 0 ) () T P; ((D); (D 0 )): We show now that all the properties of SLD-derivations stated in Section 3.2 hold for the abstract top-down denotation for any perfect observable as well. Corollary 4.8 Let : D! D be a perfect observable A be an atom G G 1 G 2 be goals and P P 1 P 2 be programs. Then B [A in P ] = A~ O [P ]; B [G 1 in P ] = B [G 1 in P ] ~ B [G 2 in P ]; B [ in P ] = (f g); B [A; G in P ] = A~ O [P ] ~ B [G in P ]; P 1 P 2 () O [P 1 ] = O [P 2 ]: In order to express the abstract OR-compositionality we have to dene also the operator ~] S 1 ~]S 2 = fs 1 ~+ S 2 g where fs g is the least solution of the equation fs g = (Idj I ) ~+ ~ P f fs g ~on((a~ S) (Id)) g A2Atoms :
Theorem 4.9 Let : D! D be a perfect observable c be a clause and P P 1 P 2 be programs. Then O [P 1 [ P 2 ] = O [P 1 ] ~] O [P 1 ]; We can now show that the operational semantics and the top-down denotation are indeed precise. Corollary 4.10 Let : D! D be a perfect observable G be a goal and P be a program. Then (B [G in P ]) = B [G in P ] and (O [P ]) = O [P ]: Finally we can prove the equivalences between the denotational and the operational semantics on one side and between the top-down and bottom-up denotations on the other side. Corollary 4.11 Let : D! D be a perfect observable G be a goal and P be a program. Then O [P ] = F [P ] and Q [G in P ] = B [G in P ]: There are several examples of observables for which ~on is not precise (for example as already mentioned computed answers). We can still try to dene the semantics by choosing the optimal abstractions for an upper level semantic operation. In the denotational semantics the operator on is only used to dene the semantic function C. We obtain a new class of observables by taking the optimal abstraction ~ C. 4.5 Denotational observables To dene a denotational observable we relax the optimality condition of axiom (4.4.iv) and admit a ~on operator which is not precise. Hence every perfect observable is a denotational observable but the converse does not hold. Denition 4.12 Let : D! D be an observable. observable if i. ( P f D i g i2i ) = ( P f ( )D i g i2i ) ii. (A D) = (A ( )D) Then is a denotational iii. (D 1 D 2 ) = (( )D 1 ( )D 2 ) iv. (D 1 on D 2 ) = (D 1 on ( )D 2 ). The following theorem shows that under these conditions we can just replace C by the optimal abstraction ~ C of C to make the denition precise. ~C [c] = C [c] : Theorem 4.13 Let be a denotational observable. Then (C [c] I ) = ~ C [c] (I ) :
As a consequence the abstract denotational semantics and bottom-up denotation are precise. In particular F [P ] = (O [P ]). Hence F is correct and minimal w.r.t.. Example 4.14 We show now how to reconstruct the s-semantics [6 7] by means of the observable of example 4.3. We can prove that is indeed a denotational observable since the condition (4.12.iv) now holds. The abstract operation ~ P turns out to be pair set union while ~ and ~ are: A~ X = f ha; i j = f # j hh; 0 i is a renamed apart version of an element in X # 0 2 0 9 s.t. A = H and # = mgu (A; H# 0 )j A g 6= ; g X 1 ~ X 2 = f hg; i j = f # j G = (G 1 ) hg 1 ; 1 i 2 X 1 hg 2 ; 2 i 2 X 2 # 1 2 1 # 2 2 2 var (# 1 ) \ var (# 2 ) var (G 1 ) \ var (G 2 ) and # = mgu ((G 1 ); (G 1 # 1 # 2 ))j G g 6= ; g. The abstract semantic function is ~C [p(t) :? B] X = f hp(x); i j = f # j x are new variables ha i ; i i are renamed apart elements of X # i 2 i and # = (f x=t gmgu (B; (A 1 # 1 ; : : : ; A n # n )))j x g 6= ; g. The abstraction of the top-down denotation (O [P ]) = f hp(x); i j = f # j p(x) # 1?! c 1 : : :?! c n # = (# 1 # n ) j p(x) c i 2 P g 6= ; g = : is isomorphic to the top-down denition of the s-semantics O s [P ]. It is easy to see that it is just a matter of representation. In the s-semantics case the substitution is simply applied to the general atom while in our case is not. The same isomorphism holds between the abstract semantic function P [P ] X = f hp(x); i j = f # j 9 p(t) :? B 1 ; : : : ; B n 2 P x are new variables ha i ; i i are renamed apart elements of X # i 2 i # = (f x=t g# 0 )j x and # 0 = mgu ((B 1 ; : : : ; B n ); (A 1 # 1 ; : : : ; A n # n )) g 6= ; g. and the immediate consequences operator of the s-semantics. From Theorem 4.11 and 4.8 we can derive the usual properties of the s-semantics namely that F [P ] is correct and minimal w.r.t. computed answers and that the answers computed for any goal can be obtained from the answers computed for the most general atomic goals. The proof of the same results using classical methods [6] is much harder. # n
Example 4.15 The call patterns (with state) of a program P for a goal G are the atoms selected in any SLD-derivation of G in P via R. We dene the domain of the objects of the form hg; C; #i where the interpretation is \the execution of the goal G generates a procedure call C with state (computed answer substitution) #". Note that C can be. The abstraction which allows us to obtain the call patterns is (D) = f hg; Ri j R = f hc; #i j hg; Si 2 D G # 1?! c 1 : : : # n?! c n C; B 2 S # = (# 1 # n ) j G and B 6= ) C 6= g R 6= ; g: (X) = f hg; Si j S = wf (f d j rst (d) = G hg; Ri 2 X last (d) = (C; B) hc; answer (d)i 2 R and B 6= ) C 6= g) S 6= ; g: where wf (S) is the (greatest) well-formed subset of any S Deriv. The axioms (4.12.i ii iii iv) are satised while the axiom (4.4.iv) is not. Hence is a denotational observable. The operation ~ P turns out to be pair set union while ~ and ~ are: A~ X = f ha; Ri j R = f hc#; #j A i j hh; R 0 i is a renamed apart element of X hc; # 0 i 2 R 0 there exists s.t. A = H and # = mgu (A; H# 0 ) g R 6= ; g; X 1 ~ X 2 = f hg; Ri j R = f hc; #i j G = G 1 hg 1 ; R 1 i 2 X 1 hg 2 ; R 2 i 2 X 2 r 1 = hc 1 ; # 1 i 2 R 1 r 2 = hc 2 ; # 2 i 2 R 2 var (r 1 ) \ var (r 2 ) var (G 1 ) \ var (G 2 ) and if C 1 6= then hc; #i = hc 1 ; # 1 i else C = C 2 and # = mgu ((G 1 ); (G 1 # 1 # 2 ))j G g R 6= ; g: The abstract semantic function is ~C [p(t) :? B] X = f hp(x); Ri j R = f hc# 0 ; #i j B = B 0 ; B 00 x are new variables 9k s.t. hp i (x i ); R i i are renamed apart elements of X 8i < k h; # i i 2 R i hc; # k i 2 R k # = (f x=t g# 0 ) j x # 0 = mgu ((B); (p 1 (x 1 )# 1 ; : : : ; p k (x k )# k )) and B 00 6= ) C 6= g R 6= ; g:
5 Conclusions and Future Work The main feature of our construction is that we have now a powerful framework where we can easily reason about operational properties of denite logic programs. The overall construction has several similarities with the abstract interpretation frameworks which were one of the sources of inspiration. The main dierence has to do with the fact that we are concerned with abstractions which are not only correct but also precise. As a consequence we don't have to worry about the design of good abstract operators since we always automatically obtain the best operators. A second peculiarity of our framework is that we handle a denotational and operational semantics in an uniform way. This allows us to reason about the relation between the two denitions. We are currently working on an extension of the framework where we relax the requirement about precision by moving to the standard notion of correct approximation. This will allow us to accommodate abstract semantics useful for program analysis. Our extended algebraic framework will allow us to discuss various issues related to the denition of abstract domains useful for program analysis by providing the tools for formally comparing top-down and bottom-up analyses. We are currently P studying other classes of observables where the conditions on the operators and are not satised. This is typically the case P in observables used in program analysis. For example the abstract version of is often forced to loose precision in order to achieve termination. We are also considering the properties of those observables which are obtained by composition of observables of dierent classes. References [1] K. R. Apt. Introduction to Logic Programming. In J. van Leeuwen editor Handbook of Theoretical Computer Science volume B: Formal Models and Semantics pages 495{574. Elsevier Amsterdam and The MIT Press Cambridge 1990. [2] A. Bossi M. Gabbrielli G. Levi and M. Martelli. The s-semantics approach: Theory and applications. Journal of Logic Programming 19-20:149{197 1994. [3] M. Comini and G. Levi. An algebraic theory of observables. In M. Bruynooghe editor Proceedings of the 1994 Int'l Symposium on Logic Programming pages 172{186. The MIT Press Cambridge Mass. 1994. [4] P. Cousot and R. Cousot. Systematic Design of Program Analysis Frameworks. In Proc. Sixth ACM Symp. Principles of Programming Languages pages 269{282 1979. [5] S. K. Debray and P. Mishra. Denotational and Operational Semantics for Prolog. Journal of Logic Programming 5:61{91 1988.
[6] 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. [7] M. Falaschi G. Levi M. Martelli and C. Palamidessi. A Model-Theoretic Reconstruction of the Operational Semantics of Logic Programs. Information and Computation 102(1):86{113 1993. [8] M. Gabbrielli. The Semantics of Logic Programming as a Programming Language. PhD thesis Dipartimento di Informatica Universita di Pisa 1992. [9] M. Gabbrielli G. Levi and M. C. Meo. Observational Equivalences for Logic Programs. In K. Apt editor Proc. Joint Int'l Conf. and Symposium on Logic Programming pages 131{145. The MIT Press Cambridge Mass. 1992. Extended version to appear in Information and Computation. [10] M. Gabbrielli G. Levi and M. C. Meo. Resultants semantics for PROLOG. Journal of Logic and Computation 1995. [11] M. Gabbrielli and M. C. Meo. Fixpoint Semantics for Partial Computed Answer Substitutions and Call Patterns. In H. Kirchner and G. Levi editors Algebraic and Logic Programming Proceedings of the Third International Conference volume 632 of Lecture Notes in Computer Science pages 84{99. Springer-Verlag Berlin 1992. [12] R. Giacobazzi. On the Collecting Semantics of Logic Programs. In F. S. de Boer and M. Gabbrielli editors Verication and Analysis of Logic Languages Proc. of the Post-Conference ICLP Workshop pages 159{174 1994. [13] N. D. Jones and A. Mycroft. Stepwise Development of Operational and Denotational Semantics for PROLOG. In Sten-Ake Tarnlund editor Proc. Second Int'l Conf. on Logic Programming pages 281{288 1984. [14] N. D. Jones and H. Sndergaard. A Semantics-based Framework for the Abstract Interpretation of PROLOG. In S. Abramsky and C. Hankin editors Abstract Interpretation of Declarative Languages pages 123{142. Ellis Horwood Ltd 1987. [15] R. Kemp and G. Ringwood. Reynolds base clark models and heyting semantics of logic programs. submitted for publication. [16] R. Kemp and G. Ringwood. An Algebraic Framework for the Abstract Interpretation of Logic Programs. In S. K. Debray and M. Hermenegildo editors Proc. North American Conf. on Logic Programming'90 pages 506{520. The MIT Press Cambridge Mass. 1990. [17] J. W. Lloyd. Foundations of Logic Programming. Springer-Verlag Berlin 1987. Second edition.