Algebraic Computational Models of OR-Parallel Execution of Prolog Roberto Barbuti
Dip. di Informatica Universita di Pisa I-56125 Pisa, Italy
[email protected]
Nicoletta De Francesco
Dip. di Ingegneria dell'Informazione Universita di Pisa I-56125 Pisa, Italy
[email protected]
Antonella Santone
Dip. di Ingegneria dell'Informazione Universita di Pisa I-56125 Pisa, Italy
[email protected]
Abstract A speci cation of the OR-parallel execution of Prolog programs, using CHOCS (Calculus of Higher Order Communicating Systems) [24], is presented in the paper. A translation is de ned from Prolog programs and goals to CHOCS processes: the execution of the CHOCS process corresponding to a goal mimics the OR-parallel execution of the original Prolog goal. In the translation, clauses and predicate de nitions of a Prolog program correspond to processes. To model OR-parallelism, the processes 1 , corresponding to clauses (having the same head predicate ) start their execution concurrently, but, in 1 order to respect the depth- rst search rule, each is guarded by the termination of the executions of processes 's, . The computational model is proved correct with respect to the semantics of Prolog, as given in [4, 5]. Our model, because of its algebraic speci cation, can be easily used to prove properties of the parallel execution of Prolog programs. Moreover, the model exploits the maximum degree of parallelism, by giving the Prolog solutions in parallel, without any order among them. However, this model, being close to the Prolog semantics de nition, contains sources of ineciency which make it unpractical as a guide for the implementation. To overcome these problems, a new computational model is de ned. This model is obtained by modi cations of the basic one and thus its correctness can be easily proved. Finally, we show how to obtain models of dierent real implementations of OR-parallel Prolog by slight modi cation of the new model. The relations among all these models, in terms of parallelism degree, are studied by using of the concepts of bisimulation and simulation, developed for concurrent calculi. P
C ; ; Cn
; ; P
n
p
P
P
j
i
j < i
1 Introduction Parallel computers have started to be commercially available, and they seem to be the main future source of computation power. As a consequence many algorithms, written in sequential 1
languages, should be translated into parallel ones in order to take advantage from the new technology. Logic programming [2, 17], because of its implicit parallelism, which is disregarded in sequential implementations, can be eciently implemented on parallel machines. Thus, software systems can be transparently transferred onto the new architectures, without programming efforts. In logic programs there are two sources of parallelism: AND-parallelism, corresponding to the parallel execution of dierent literals in the body of the same clause, and OR-parallelism, corresponding to the parallel execution of dierent clauses applicable to the same literal. While AND-parallelism, in general, requires a non-trivial synchronization on the values computed in parallel (they must be consistent, otherwise the computation fails), OR-parallelism requires no synchronization among the computed values 1 . An interesting discussion about the exploitation of AND and OR-parallelism can be found in [22]. When considering Prolog, a real logic language based on logic programming, things are slightly dierent. Prolog has a sequential selection strategy for clauses thus, when implementing standard Prolog, OR-parallelism requires some form of control. In particular, in sequential implementations of standard Prolog, if the computation starting from a goal G using a clause Ci is in nite, there is no possibility to apply to G other subsequent clauses Cj (j > i). In this case, in a OR-parallel implementation, computations from G with clauses Cj are started, but they make only a speculative work (not necessary work), and their computed solutions (if any) are useless. Thus a \scheduler" must be added to discard these solutions. Many works were devoted to the parallel implementation of Prolog [15]. The correctness of such implementations should be proved with respect to a formal semantics of the language. Such a semantics, when embodying the structure of the language implementation, is called computational model. Of course, the correctness of the implementation is more easily proved with respect to a computational model than to a more abstract semantics, and, usually, the model itself is used as a guide to the implementation. Moreover, if the model is abstract enough, and de ned in a suitable formalism, it can be used for reasoning about programs and their execution properties. OR-parallel computational models have been widely studied both for logic programming and for standard Prolog. Computational models of OR-parallel execution are given in [1, 14, 18, 26]. They can be used either to specify the execution of logic programming or Prolog, depending how the solutions coming from parallel processes are scheduled. However, all these models are informal and often very concrete and close to the architectures used to implement them, and, in general they cannot be used to reason about programs and executions. An abstract model, based on CCS (Calculus of Communicating Systems) [20], for the process based execution of logic programs (not Prolog programs) is given in [8]. However, this model describes how to compute a single solution to a goal and not all solutions in parallel. For Prolog, many works were devoted to the de nition of the semantics in a formal, abstract way [3, 9, 13, 10, 12]. Nevertheless, such semantic de nitions embody the essence of sequential execution, thus being unpractical both to reason on parallel execution and to be the basis for a parallel computational model. The models in [23, 16] are given by using CCS and -calculus [21], respectively, but, although the use of calculi for modelling concurrent systems, these two 1 A discussion about the problems related to the handling of binding environments in OR-parallel implementations is out of the scope of this paper.
2
semantics still specify a sequential Prolog control. A semantics de nition which abstracts from the execution model is the one de ned in [4, 5]. This semantics assigns a meaning to a program in term of a set of solutions abstracting from the order they are computed. This abstractness makes it unpractical as a reference semantics of Prolog implementations, but, on the other hand, it can be used as the reference semantics for the de nition of either a sequential or a parallel computational model. The aim of this paper is to provide computational models of OR-parallel execution of pure Prolog (without cut, negation and system predicates), which are abstract enough to reason on programs and their execution. Process algebras (like as CCS) are formalisms well suited to reason about parallel computations: many properties have been de ned on them and a wide set of veri cation methodologies exist, based on their semantics [11, 19]. Thus, in order to use these models to reason about OR-parallel Prolog executions, we give a process interpretation of ORparallel Prolog execution using CHOCS (Calculus of Higher Order Communicating Systems) [24], an extension of CCS, allowing processes to be sent and received as messages. The use of CHOCS, instead of CCS, is motivated by its expressiveness. Process passing is a powerful mechanism allowing to easily describe the assignment of \tasks" to processes, and it contributes to the clearness and conciseness of the resulting computational model. We translate a Prolog program into a CHOCS process in such a way that the semantics of Prolog, given by the multiset of the solutions, as in [4, 5, 6], is preserved. In Section 3 a rst model, denoted as basic model, is de ned which directly corresponds to the de nition, by means of CHOCS processes, of the Prolog semantics in [4, 5]. This model, being close to the Prolog semantics de nition, contains sources of ineciency when used as a guide for the implementation. However, its de nition is motivated by its clearness and by the easiness in proving its correctness. The correctness proof is given in Section 4. To overcome the sources of ineciency, other computational models are de ned in Section 5. These models are obtained by slight modi cations of the basic one and thus their correctness can be easily proved. All computational models are abstract enough to represent most of the real OR-parallel implementations. A preliminary version of the results presented in this paper can be found in [7].
2 Preliminaries In this Section we provide some de nitions and concepts about Prolog and CHOCS. For a more complete background, the reader can refer to [2, 4, 5, 17, 20, 24]. Given two sets S and T , a transition relation ! over S labelled by T is a subset of S T S : t1 tn 0 we denote (s; t; s0) 2 ! simply by s !t s0 . If s ! ! s , we call (t1 tn ) a path by ! from s 0 0 to s , and we call s a t1 tn -derivative (or simply derivative, when t1 tn are clear from the context) by ! of s (if n = 0, we have that is a path from s to s). We say that s 2 S is path nite in ! if there is a nite number of paths from s by !.
2.1 Prolog
In this Section we summarize the notions about the semantics of logic programs [2, 17] and of Prolog (as in [4, 5]). We use the standard notation fa1 ; a2; : : :; an g to denote the set with elements appearing among a1 ; a2; : : :; an, while fj a1 ; a2; : : :; an jg to denote the multiset with 3
elements a1 ; a2; : : :; an. The union between multisets is denoted by ]. Let and be denumerable sets of function symbols, and variables respectively, we use c; f; : : : and x; y; z; : : : to range over them. T () and T (; ) denote the corresponding term algebras, respectively without (ground) and with variable (non-ground). In the following we use s; t; : : : to denote terms. Likewise, if denotes a denumerable set of predicate symbols, A(; ; ) denotes the set of atoms of the form p(t1 ; : : :; tn ) where p 2 and ti 2 T (; ) (1 i n). In the following we will refer to A(; ; ) as Atom, and to its elements as atoms. The set of nite sequences of atoms is denoted Atom . The sequence of atoms with rst element a followed by the sequence B is denoted a : B; the concatenation of two sequences B and B 0 is denoted B :: B 0 . The empty sequence is denoted by and sometimes omitted. The set of variables that occur in a syntactic object t is denoted vars(t). Tuples of variables and terms are denoted x; y; : : : and t; s; : : : respectively. A substitution is a mapping from to T (; ) which acts as the identity almost everywhere, i.e. such that its domain dom(#) = fx 2 j #(x) 6= xg is nite. The empty substitution is denoted ". The application of a substitution # to a syntactic object S is denoted by S# and it is de ned as the object obtained by replacing each variable x in S by #(x). The composition # of # and is de ned as function composition. A substitution #0 is more general than # (# #0), i there exists such that # = #0 . The set of substitutions is denoted Subst. Given an enumeration g of the set of variables , a nite set V such that ]V = k and two atoms a; a0, a ren =V a0 de nes a as the atom a0 in which the occurrences of the k variables in V are substituted by the rst k variables, according to the enumeration g, which do not belong to V [ vars(a0). In the following we refer to a xed enumeration. Moreover, we extend this notation for any syntactic object. Given two terms t and s, we say they are uni able i there exists # (a uni er) such that (t#) (u#), where denotes syntactic identity. Uni cation extends to apply to any syntactic object in the obvious way. We x a function mgu which maps a pair of syntactic objects into a most general uni er of the objects, if such exists, into fail otherwise. A uni er # is more general than another one if there exists a substitution such that # = . A goal is a sequence of atoms and we de ne Goal = Atom , we will use G; G0; : : : to range over Goal. When a goal consists of only one atom it is said to be atomic. A clause is an object of the form h B where h is an atom, called the head, and B is a goal, called the body. A clause with empty body is called unit clause or fact. The set of clauses constructed from elements of A(; ; ) is denoted C (; ; ) or Clause for short. A logic program P is a ( nite) set of clauses. When P is viewed as a Prolog program, we associate an ordering on the clauses of the program, which re ects the textual ordering of the clauses. Thus, we can de ne a Prolog program as an ordered sequence of clauses hC1; C2; : : :Cn i, where h and i are used to indicate sequences. The operational semantics of a logic program can be de ned by the following concepts.
De nition 2.1 (resolve) resolve : Goal Clause ! Goal Subst is de ned by: 0 00 00 resolve(G; h G0) = h(G :: G )#; #i if G = a : G and mgu(h; a) = #,
fail
where vars(G) \ vars(h
if mgu(h; a) = fail.
G0 ) = ; 4
We use resolve to de ne the basic step in the execution of logic programs: SLD reduction. We assume a left-to-right computation rule, thus SLD reduction applies the resolution inference rule to the rst atom in the current goal, as de ned by the following transition relation.
De nition 2.2 (SLD transition relation )
Let P be a logic program. The transition relation associated to P by the SLD resolution is the relation ???! Goal (Subst Clause) Goal de ned as follows: sld P
#;C G???! G0 , hG0; #i 2 resolve(G; C r); with C r ren =V C; C 2 P sld P
where V = vars(G).
When resolving a goal in logic programming, each clause can be used independently from the behaviour of computations starting with other clauses. Moreover, the clauses composing a program are considered as a set, without any order. The de nition of the operational semantics of Prolog programs is slightly more complicated. Clauses in a Prolog program are ordered, and, if the computation starting with a goal G and a clause Ci is in nite, no subsequent clauses Cj , j > i, can be used for G. Thus, following [5], to de ne the semantics of Prolog we need the concept of nite derivation.
De nition 2.3 (Prolog transition relation) Let P = hC1; C2; : : :; Cni be a Prolog program. The transition relation ???! Goal (Subst Clause) Goal associated to P is de ned as prolog P
follows:
#;C
i a : B ???! =V Ci; resolve(a : B; Cir ) 6= fail; G , Cir ren prolog P hG; #i = resolve(a : B;[Cir ); and 8hG0; #0i 2 resolve(a : B; Cjr)
Cjr ren = Cj ;1j
: not free in E nor F otherwise a!(E 0[F ! X ]):E [F ! X ] (E [F ! X ]) + (E 0[F ! X ]) (E [F ! X ])j(E 0[F ! X ]) (E [F ! X ])nL ((E [F ! X ])[f ] F if X = Y Y otherwise A 7
In rule Act2 the process a!E 0:E sends the process E 0 on port a and then becomes the process E . The Sum rules state that E1 and E2 are alternative choices for the behaviour of E1 + E2. The Com rules show how processes in a parallel composition can behave autonomously (the rst two rules), or interactively (the last two rules). When the third and forth rules are used, we say that a handshake occurs. A handshake occurs only if two processes can simultaneously execute the complementary actions of sending and receiving a message. Handshake corresponds to an internal communication: the two processes exchange information between them and the action is . Restriction prevents the actions on ports in L to be done. Relabelling renames portnames by means of a relabelling function f : A ! A. In the following, we use the notation ( a if x = b a=b, where a; b 2 A, to de ne the relabelling function f (x) = x otherwise When the sending or receiving actions, a?X:E and a!E:E 0 respectively, are used only for synchronizing processes (the sent or received process is not used) we will denote them simply by a?:E and a!:E 0. The Con rule says that A behaves as E if A is de ned by A def = E . Note that there is no rule for the empty process nil, which thus cannot perform any move. In the paper we assume that the executions of the processes are fair, i.e. each action which can be done is not inde nitely delayed. We shall use the following notation given in [24], de ning the portnames that a process uses during its executions.
De nition 2.7 (dynamicsort) dynamicsort: E ! A ?F 0 00 !F 0 00 dynamicsort(E ) = fa 2 Aj9F; F 0; F 00 : E ! F a! F or E ! F a! F g F and ! is the re exive and transitive closure of !. where E !F 9 2 Act : E !
We now de ne the notion of process bisimulation and simulation. De nition 2.8 (=)0 transition relation) a ?E 0 00 ?E 00 E =)E E ! a! !E 0 0 E E 00 E ! a! !E 00 E =a!) !E E0 E! E0 E =)
? ? 00 where E ! ! E means 9E 0:E !? E 0 & E 0 !? E 00, ! is the re exive and transitive closure of ! and E; E 0; E 00 2 E .
De nition 2.9 (Weak bisimulation) A weak bisimulation R is a binary relation on E such that E RF implies: 0 (i) E =)E 0 implies F =)F 0 for some F 0,0 with R^ 0 and E 0RF 0 0 (ii) F =)F 0 implies E =)E 0 for some E 0, 0 with R^ 0 and E 0RF 0 where 2 (Actnf g) [ fg, and R^ = f(; 0)j( = a?E 00 & 0 = a?F 00 & E 00RF 00 ) _ ( = a!E 00 & 0 = a!F 00 & E 00RF 00) _ ( = 0 = )g: 8
De nition 2.10 (Process equivalence)
Two processes E and F are said to be weak bisimulation equivalent i there exists a weak bisimulation R containing (E; F ). In this case we write E F .
Note that, if E E 0, then E jF E 0jF ,E nL E 0nL and E [f ] E 0[f ].
De nition 2.11 (Weak simulation)
A weak simulation S is a binary relation on E such that E S F implies:
F =)F 0 implies E =)0 E 0 for some F 0,0 with S^0 and E 0S F 0 where 2 (Actnf g) [ fg, and S^ = f(; 0)j( = a?E 00 & 0 = a?F 00 & E 00S F 00 ) _ ( = a!E 00 & 0 = a!F 00 & E 00S F 00) _ ( = 0 = )g:
De nition 2.12 (Process simulation)
A process E is said to weakly simulate a process F i there exists a weak simulation S containing (E; F ). In this case we write E / F .
Note that, if E simulates F , then E can mimic all the possible executions of F , while E can have more execution paths than F . In our model we use an extension of CHOCS, having the ability of exchanging Prolog terms through the communication channels. This ability does not represent a real extension of the language, since Prolog terms can be de ned as CHOCS processes. However, the de nition of Prolog terms in CHOCS is outside the scope of the paper, which is devoted to model and to prove properties of OR-parallel executions of Prolog programs. An example of representation of Prolog terms by means of processes can be found in [16], in which -calculus is used2 . As a consequence of this assumption, we consider the operations on terms as prede ned; in particular, uni cation and substitution application are given as primitive operations. Finally, we use a slightly dierent version of the Con rule: E0 r! E Con A ! E 0 (A def = E)
where E r is the expression E in which all the occurring free term variables (i.e. those belonging to ) are replaced by fresh ones. This does not modify the CHOCS semantics and is useful to easily model logic programming and Prolog.
3 The basic OR-parallel computational model 3.1 An intuitive description
In this Section we introduce the basic model for OR-parallel execution of Prolog, starting from an example. 2
The encoding in -calculus can be mechanically translated in CHOCS, as shown in [25].
9
Let us consider the following Prolog clauses de ning a predicate p. p(t1) q(tq ): p(t2) r(tr ); s(ts): p(t3) u(tu ): We assume the de nitions of predicates q , r, s, and u, given elsewhere. The OR-parallel execution of a goal p(t4) can be described as follows. Each clause for p can be interpreted as a process which, when started, uni es the goal terms, t4 , with the clause head and proceeds with the execution of the clause body. It is important to note that, when executing the body of the second clause, r(tr ) must be executed before s(ts ), and the computational model has to preserve this sequentiality. By denoting by P i the process corresponding to the ith clause for p, we can describe the ORparallel execution of p(t4) by the following CHOCS expression
P 1(t4) j P 2 (t4) j P 3 (t4 )
(1)
where the notation P i (t4 ), de ned in the next Section, denotes the execution of the process P i with terms t4 as initial data. Informally, this expression says that, in order to compute the solutions for a goal containing the predicate p, we can run in parallel all the clauses de ning p. Unfortunately, the parallel execution expressed by (1) does not correspond to the operational semantics of Prolog. It could happen that the execution of P 2 (t4 ) is in nite, while the execution of P 3 (t4 ) gives a solution, say #. In this case, the parallel system (1) would give the solution #, while a standard Prolog interpreter would not. In a Prolog implementation, the termination of both processes P 1 (t4 ) and P 2 (t4 ) should act as a guard for the process P 3 (t4 ), according to the semantics de nition of Section 2.1. Informally, P 3 (t4 ) cannot give any solution if either P 1 (t4) or P 2(t4 ) does not terminate. We express the control for P 3 (t4) by the CHOCS expression t
P 1 (t4) k P 2(t4) ; P 3 (t4)
(2)
where \;" denotes sequential composition, the superscript letter, t, means that we are interested only in the termination of P 1 (t4) and P 2 (t4 ) and not in their solutions, and k means parallel composition of two processes with synchronization on termination (the three operators will be formally de ned in the next Section). By (2), we make explicit that the execution of P 3 (t4) is guarded by the terminations of P 1 (t4) and P 2 (t4 ), which can be checked in parallel. Analogously, P 2 (t4 ) must be guarded by the termination of P 1 (t4 ), while P 1 (t4 ) is not guarded. Thus, the whole OR-parallel execution of the goal can be described by the expression
j
P 1 (t4)
P 1(t4 )t; P 2 (t4)
j
t P 2 (t4) ;
k
P 1 (t4 )
P 3 (t
4)
(3)
A point to remark concerns the time needed for the OR-parallel execution. It is clear that a certain amount of sequentiality is retained because of the guards. However, due to the parallel execution of the guards, the time needed for computing the solutions of an atomic goal 10
with predicate p is less than the sum of the times needed to execute the processes corresponding to the clauses for p. For example, the time needed for computing the solutions of the goal p(t4 ) is
maxfT (P 1(t4 )); T (P 1(t4 )) + T (P 2 (t4)); maxfT (P 1(t4)); T (P 2(t4))g + T (P 3 (t4 ))g where T (P (t)) is the time consumed by the process P (t) to produce all its solutions. Note that, in a sequential execution the time would be
T (P 1(t4)) + T (P 2 (t4 )) + T (P 3(t4 )). The model we have so sketched corresponds to a directed translation in a parallel environment of the Prolog semantics, as given in Section 2.1. This allows an easy proof of the equivalence between the computational model and the semantics. Moreover, due to its simplicity and understability, the model can be used as a reference model to prove the correctness of OR implementations of Prolog. However, it cannot be used as a practical guide for a real implementation, due to the redundant computation it de nes. In fact, an implementation directly based on expression (3) would run the processes P 1 (t4 ) and P 2 (t4) three and two times, respectively. We would expect that a real implementation runs each process only once. Thus, in real implementations, all the processes should be executed in parallel, and the task of giving the solutions of P 3 (t4 ) only if P 1 (t4 ) and P 2(t4 ) terminate should be performed by a \scheduler". When using a scheduler, guards are not longer necessary and each process can start its execution without waiting for the termination of guards: this allows to decrease the time needed for computing all the solutions to maxfT (P 1 (t4 )); T (P 2(t4 )); T (P 3(t4 ))g (if the time consumed from the scheduler is irrelevant). In Section 5 some other models are de ned, taking into account the above considerations. In particular, we rst de ne a second model, with no redundant computation and prove it equivalent to the previous one. Afterwords, we introduce a third and fourth model, which dier from the previous one in the ordering among the computed solutions, but precisely model some existing OR parallel Prolog implementations.
3.2 The model
In the de nition of processes, we use dierent port names, where each port name is evocative of the role of processes or terms sent or received through it. We provide explanations and comments while concepts and de nitions are introduced. The rst port name we introduce is , and we use it only to synchronize processes on termination, without exchange of messages. All the processes we de ne have the well ? formedness property, that is, they send an empty message on the port name if and only if they terminate. A typical use of can be found in the de nition of the operators k and \;". The two operators (de ned as in [20, 24]) correspond to the sequentialization and parallel execution of two wellformed processes. The processes resulting from their applications are well-formed too. In the following we assume the \;" operator to have a higher precedence than k.
11
E kF = (E [1=]jF [2=]j (1 ?:2 ?:DONE + 2 ?:1 ?:DONE ))nf1; 2g E ; F = (E [1=]j1?:F )nf1 g DONE def = !:nil The constant DONE corresponds to a process whose task is to terminate without further moves. The \;" operator will be suitably used to express the sequentialization between guards and guarded processes: guards must terminate before the guarded processes can start their execution. A further source of sequentiality is present in Prolog: the sequential execution of the atoms in clause bodies. While the executions of guards and guarded processes do not share values, the execution of subsequent atoms in a clause body proceeds by passing the values computed by the previous ones to the following ones, and then by executing them. Value passing is obtained by substitution application. To express this kind of sequentiality, the \;" composition is not sucient. We need a new operator able both to sequentialize executions and to apply substitutions. The new operator is denoted by \" , and its application P Q can be informally read as: execute P , and, for each computed substitution, apply it to the \continuation" Q, and then execute Q. As it will be clear in the following, this concept of continuation is particularly useful in describing OR-parallel execution. Thus, from now on, we will see P Q not simply as \Q after P ", but as \P takes the continuation Q, executes itself, and then passes the control to the continuation Q". The use of the symbol \" is then motivated by the fact that P Q is not a simple sequentialization, but a kind of composition of executions. Each process P , in our model, must be able to manage (receive on a port name and use) a continuation. The port name for passing continuations is cont. According to the de nition of \", when we use P Q, P starts with an input communication on cont and is executed in parallel with a process sending Q on cont.
P Q = (P kcont!Q:DONE )nfcontg In the following we consider the \" operator as associating to the right: then P Q R is equivalent to P (Q R). Moreover we assume that \" has an higher precedence than \;". Let us now consider a single clause in the de nition of a predicate p (say the ith one), and let us de ne the corresponding process in the CHOCS model. Let the clause be p(t) q1(t1); q2(t2); : : :; qm (tm): where t is a sequence of k terms and each ti , 1 i m, is a sequence of ki terms. The corresponding process P i , when executed, takes a sequence of k terms, and uni es them with t. Then, it takes a continuation process, executes the processes corresponding to 12
the clause body, and then continues with the execution of the continuation. The mechanism of continuations captures appropriately the essence of distributing tasks among parallel \workers". To have an intuition, consider the goal p(t) : G and the n clauses, in the Prolog program, hC1; C2; : : :; Cni, having p as head predicate. The execution proceeds by running in parallel the processes corresponding to the n clauses and by passing to each of them the continuation G, that is, the part of the goal which still have to be executed. In the de nition of process P i , the terms to unify are received on the terms port name and the continuation on the cont port name:
P i def = terms?y:cont?X:(UNIFY (y; t) Q1 (t1) Q2 (t2) Qm (tm ) X ) P (t) = (terms!t:DONE kP )nftermsg where y is a sequence of k term variables not occurring in t; t1 ; : : :; tm; Q1; : : :; Qm are the processes corresponding to the predicates q1 ; q2 ; : : :; qm and UNIFY is the uni cation process de ned in the following. The application P (t) of a process constant P to a sequence of terms t is de ned as the process P , executed in parallel with a process which send t on the terms port name; whenever we use P (t), P begins with an input communication on terms. The de nition of the uni cation process UNIFY is the following (its applying to two terms is de ned in a similar way to P (t)). def
UNIFY =
(
terms?(t1 ; t2):cont?X:apply(X; #) terms?(t1 ; t2):cont?X:DONE
if # = mgu(t1; t2 ) if mgu(t1; t2) = fail
We have that UNIFY (t1 ; t2) = cont?X:DONE if there is not a most general uni er between t1 and t2 ; otherwise, it is de ned as the process taking a continuation and simulating the Prolog application of the most general uni er # to it; apply (Q; #) is a function which, given the process Q and the substitution #, return the process obtained from Q by replacing each free variable x occurring in a term by #(x). We remark that in this paper we are not interested in de ning the
uni cation process; for a de nition of uni cation in process algebra, the reader can refer to [16], where the process is de ned by using -calculus. Now, given the processes P 1 ; P 2; : : :; P n , corresponding to the n clauses for p, we can de ne the whole process corresponding to p as follows: 13
def
8 > > > > > > > > > > > > >
> > > > > > > > > > > > :
terms?y:cont?X:DONE
if n = 0 (no clauses for p)
terms?y:cont?X: (P 1 (y) X k (P 1 (y) X )t; P 2(y ) X k
k(P 1(y) X k kP n?1 (y) X )t; P n (y) X ) if n > 0
This process de nition is the kernel of the OR-parallel computational model. When called, the process P starts the execution of all the processes P i , 1 i n, corresponding to the clauses for p, in parallel, but each P i is guarded by the termination of the parallel execution of the P j 's, 1 j < i. Finally, a goal q1 (t1 ); q2(t2); : : :; qs(ts ) is translated into a sequence of process calls in the following way.
Q1(t1 ) Q2(t2 ) Qs(ts ) SUCC (x) SUCC def = terms?t:succ!t:DONE where
x is the sequence of variables in q1 (t1); q2(t2); : : :; qs (ts ), that is x = vars(t1 ; t2; : : :; ts). The process SUCC , when executed, sends on the succ port name a computed solution, that is
the terms, in a computed substitution, corresponding to the variables occurring in the initial goal. Note that there is a dierent de nition of SUCC for each goal. It is important to remark that SUCC is executed at the end of an execution path if and only if none of the previous processes becomes DONE (which would terminate the execution along that path). Intuitively, SUCC is reached if and only if all the uni cations along the path have been successful. The succ port name corresponds to a communication channel with the user and all the solutions are sent through it. Note that, due to the re-de nition of the rule Con of the operational semantics of CHOCS, each time a process constant is used, all the free term variables are renamed by fresh ones. The last remark concerns the t operator. Because only the termination of processes in its scope is important, it must be able to hide all the success messages coming from them. This is guaranteed by the following de nitions.
14
B t = (B[succt=succ]; !:DONEkDEL)nfsucct; g DEL def = succt?x:DEL + ?:DONE
Example 3.1
Consider the following Prolog program: p(0) p(x) q(x); b p(x) p(x) q(1) q(2) q(2)
b
The corresponding CHOCS processes are: P def = terms?y:cont?X:(P 1 (y ) X k(P 1(y ) X )t; P 2(y ) X k (P 1 (y ) X kP 2 (y ) X )t; P 3(y ) X ) P 1 def = terms?y:cont?X:(UNIFY (y; 0) X ) def P 2 = terms?y:cont?X:(UNIFY (y; x) Q(x) B X ) P 3 def = terms?y:cont?X:(UNIFY (y; x) P (x) X ) def Q = terms?y:cont?X:(Q1 (y) X k(Q1(y) X )t; Q2(y) X ) Q1 def = terms?y:cont?X:(UNIFY (y; 1) X ) def Q2 = terms?y:cont?X:(UNIFY (y; 2) Q(2) X ) B def = cont?X:(B 1 X ) B1 def = cont?X:X
4 Correctness of the translation In this section we outline the proof of the correctness of the translation described in the previous section. The complete proofs of some theorems are shown in the Appendix. In the following, given a goal G and a program P , we denote by GaP the corresponding CHOCS process; for the empty goal, , we de ne aP = SUCC (x) for each program P , where, in this case, SUCC def = terms?t:succ!:DONE . When P is clear from the context, we denote GaP as Ga. We denote as GoalPa (brie y Goala) the set of processes obtained as translation of goals. In Section 4.1 we show some useful results enlighting the behaviour of a goal translation, while in Section 4.2 we prove that the moves produced by GaP coincide with the solutions of the Prolog goal G. Note that the correctness proof, besides showing that a Prolog program and its CHOCS translation have the same solutions, proves that the computational behaviour is the same, that is the execution of the Prolog program is nite if and only if the execution of its CHOCS translation is nite. 15
4.1 Some useful lemmas
Let us rst show that, for each goal G, Ga has the following properties: (i) it terminates if and only if its last action is and (ii) either it is path nite or it is in nite on all paths. We call well-formed the processes having this property.
De nition 4.1 (Well-formed process)
A process E is well-formed i
? E 00 and (ii) E 0 ! nil , 1. for every derivative E 0 of E , (i) E 00 does not exists such that E 0 ! = !.
2. either E is path nite or for each derivative E 0 of E there exists a process E 00 such that E 00 for some . E0 !
The following lemmas show some useful results for well-formed processes.
Lemma 4.1
1. nil is well-formed.
2. If E is well-formed and E =)E 0 then E 0 is well-formed. 3. If E is well-formed and E F then F is well-formed.
4. If E and F are well-formed then E kF , E ; F and E t are well-formed.
5. DONE is well-formed. Proof. From the well-formedness de nition.
Lemma 4.2
1. If E is well-formed then E ; DONE DONE ; E E 2. If E is well-formed then DONE kE E . 3. (cont?X:DONE ) E DONE
4. If E is well-formed and not path nite then E ; F E Proof. We give the proof of point 1: E ; DONE E . The proofs of DONE ; E E and the ones of points 2, 3 and 4 are similar. We show that R is a weak bisimulation, where
R = f((E ; DONE); E )j where E is well-formedg [ f(E 0; E 00)j where E 0 E 00 nilg Consider the three possible moves of E : E =)! E 0. By point (1) of well-formedness de nition we have E 0 = nil. Thus (nil[ = ]jDONE )nf g and by de nition of DONE and \;" E ; DONE =) 1 1 !! (nil[ = ]jnil)nf g nil 1 1 16
E =)E 0; 6= . E 0 is well-formed by lemma 4.1.1. Since = 6 , E ; DONE =)E 0; DONE and obviously (E 0; DONE; E 0) 2 R. E 0. As in the previous case, E ; DONE =)E 0; DONE . E =) Similarly, we can prove the vice-versa: the derivatives of E ; DONE have corresponding derivatives of E in R, up to . Thus the proof is complete. 2 The following lemma states that the process obtained by the translation of a goal is wellformed.
Lemma 4.3
For each goal G, Ga is well-formed. Proof. By induction on the length of the nite derivations of Ga; see the Appendix.
We denote as looping process a process E such that E LOOP , where
LOOP def = :LOOP is a process performing an in nite number of moves. The following lemma holds.
Lemma 4.4 1. LOOP is well-formed. 2. LOOP E LOOP: 3. If E is well-formed and not path nite then E kLOOP E .
Proof. By de ning suitable bisimulations. The following lemma states some important properties of well-formed guard processes.
Lemma 4.5
If dynamicsort(E ) fsucc; g and E is well-formed then 1. If E is path nite then E t DONE: 2. If E is not path nite then E t LOOP:
Proof. By de ning suitable bisimulations. The following lemma directly follows from the above one and gives a rule for eliminating guards from a process expression. In fact, if a guard is path nite, it can be ignored and the guarded process can be executed, while, if a guard is not path nite, the guarded process cannot do any move. Thus guards accomplish correctly their task.
17
Lemma 4.6
Consider the process expression:
E = E1k(E1)t; E2k(E1kE2)t; E3k k(E1kE2k kEn?1 )t; En where, for each i; 1 i n, dynamicsort(Ei) fsucc; g. Let k = minfi j Ei is not path finiteg or k = n if, for each i; 1 i n; Ei is path nite. Then
E E1k kEk
Proof. (k = 1). First note that (E1)t is well-formed by lemmas 4.3 and 4.1.4. Moreover, since dynamicsort(E1) fsucc; g we have (E1)t LOOP by lemma 4.5.2. Thus E1k kEi, 1 i n ? 1, is not path nite, since E1 is not, and then (E1k kEi )t is not path nite as well. Hence, (E1k kEi )t LOOP by lemma 4.5.2. As a consequence, by lemma 4.2.4, (E1k kEi)t ; Ei+1 LOOP . Thus E E1kLOOP kLOOP and, by lemma 4.4.3, we have E E1 . (k > 1). Similar to the proof of the rst case, but considering that, if E1; ; Ek?1 are path nite, then for each i; 1 i k ?1, (E1k kEi)t is path nite and thus (E1k kEi)t DONE by lemma 4.5.1 and by lemma 4.2.1 (E1k kEi)t; Ei+1 Ei+1 .
4.2 Equivalence with Prolog
We de ne an inductive chain of multisets, based on the semantics of Prolog as given in section 2.1.
De nition 4.2 (prologPi (G))
Let P be a Prolog program and G a goal. For each i 0, prologPi (G) : Goal ! Goal Subst is the multiset de ned by: prologP0 (G) = fj (h G; i jg ) 00 ;Cj # prologPi+1(G) = hG00; #i hG0; #0i 2 prologPi (G); G0???! G00 and # = #0#00 prolog P
Each multiset prologPi (G) contains the goals and related substitutions which are derived in exactly i steps by a goal G. It is easy to see that the relation between SPprolog(G) as de ned in U prolog section 2.1 and the chain prologPi (G) is: SP (G) = fj #jh; #i 2 i prologPi (G) jg. Let us now de ne the analog of resolve for CHOCS processes.
De nition 4.3 (resolvea) resolvea : Goala Clause ! (Goala Subst) [ fDONE g is de ned by: resolvea(P (t) G1 Gn ; C ) = 8 > > > > > > < > > > > > > :
happly(R1 Rn G1 Gn ; #); #i if C is the ith clause of p, P i def = terms?y:cont?X: (UNIFY (y; ti ) R1 Rn X ) and mgu(t; ti ) = #,
DONE
if C is not a clause for p or mgu(t; ti ) = fail.
18
The following lemma states the relation between resolve and resolvea; the proof is immediate from de nitions.
Lemma 4.7
resolve(G0; Ci) = hG#; #i , resolvea(G0a; Ci) = happly(Ga; #); #i and resolve(G0; Ci) = fail , resolvea(G0a; Ci) = DONE We now de ne an inductive chain of multisets, which is the counterpart of the chain prologPi (G).
De nition 4.4 (chocsiP (G)) Let P be a Prolog program hC1; C2; : : :; Cni and G a goal. For each i 0 chocsiP (G) : Goala ! Goala Subst is the multiset de ned by: chocs0P (G) = fj hGa; i jg hG0a; #0i 2 chocsiP (G); hG00a; #00i = resolvea(G0a; Ci); i +1 chocsP (G) = hG00a; #i # = #0 #00 and 8j 2 f1 : : :i ? 1g if hE; 'i = resolvea(G0a; Cj ) then E is path finite in ! 8 > < > :
9 > = > ;
Each multiset chocsiP (G) contains all processes obtained by applying resolvea exactly i times starting from Ga . Analogously to the de nition of SPprolog (G), we de ne the solutions SPchocs (G) of the CHOCS processes corresponding to the translation of a Prolog program and a goal: SPchocs (G) is the multiset containing all nal substitutions included in any chocsiP (G) for each i.
De nition 4.5 (CHOCS Prolog solutions)
The multiset SPchocs (G) of CHOCS Prolog solutions of a Prolog program P with a goal G is de ned by: SPchocs (G) = fj #jhSUCC (x); #i 2 Ui chocsiP (G) jg.
Now, we are ready to prove the equivalence theorem. The proof is based on the following lemma, which ensures that the transition system associated with Ga is path nite if and only if the execution of G is nite with the Prolog interpreter.
Lemma 4.8
A goal G is path nite in ???! i Ga is path nite in !. sld Proof. By induction on the niteP derivations: see the Appendix.
The following theorem states that the Prolog solutions coincide with the CHOCS Prolog solutions.
Theorem 4.9 (Equivalence theorem) For each Prolog program P and goal G, SPchocs (G) = SPprolog (G). Proof. The proof can be made by showing, by induction on the length of the chains chocsiP (G)
and prologPi (G) that, for each i, prologPi (G) contains, up to the translation, the same elements of chocsiP (G), i.e, for each i, F (prologPi (G)) = chocsiP (G), where F (hG; #i) = hGa ; #i. 19
Up to now we have proved that the Prolog solutions coincide with SPchocs (G). To complete the equivalence proof, it remains to show that Ga behaves correctly, i.e. that it produces all and only the substitutions in SPchocs (G). The following theorem shows this fact and moreover that Ga produces all solutions in parallel. This means that each path of the transition system corresponding to Ga , if we ignore the actions, is an interleaving of all nal substitutions, if any. Recall that we consider fair executions only.
Theorem 4.10
Let P be a Prolog program and G a goal. Then Ga kisucc!(#i(vars(G))):DONE ; or Ga (kisucc!(#i(vars(G))):DONE kLOOP ), for all #i 2 SPchocs (G). Proof. The proof of the theorem (see the Appendix) is made by showing, by induction on the length of the chain chocsiP (G) that, for each i, Ga is weakly equivalent to the process consisting of the parallel composition of both (i) the processes sending on succ the solutions belonging to chocsjP (G), j < i, and (ii) the goals belonging to chocsiP (G). As a consequence of theorem 4.9 and 4.10, we have that the translation in CHOCS of a Prolog program P and goal G produces exactly all the solutions obtained with a Prolog goal G. Moreover, our model has the highest degree of parallelism, since it allows whatever interleaving of the solutions, and this fact makes it useful as a reference model for OR-parallelism. The following corollary holds:
Corollary 4.11
Let P and Q two Prolog programs. If, for each goal G, GaP GaQ, then P and Q are equivalent. Proof. The proof follows by theorems 4.10 and 4.9. The converse of the corollary does not hold in general: consider two programs P and Q with have the same solutions but such that P does not terminate while Q does. The corresponding CHOCS translations, even if they produce the same multiset of solutions in parallel, are not weakly equivalent since the translation preserves non-termination, i.e. a goal G terminates with P if and only if GaP terminates.
Example 4.1
Recall example 3.1 in the Section 3.2. Consider now the goal p(z ). The standard Prolog interpreter gives the solutions z ! 0 and z ! 1 and then it runs inde nitely. The CHOCS translation of the goal is P (z ) SUCC (z ). First we show that Q(2) E LOOP , where E is a generic well-formed process such that dynamicsort(E ) fsucc; g. Q(2) E ! cont?X:((Q1(2) X k(Q1(2) X )t; Q2(2) X ) E ) ! (Q1(2) E )k(Q1(2) E )t; (Q2(2) E ) definition of Q1 DONE k(DONE)t; (Q2(2) E ) lemma 4:6 DONE k(Q2(2) E ) definition of Q2 DONE k(Q(2) E ) lemma 4:2:2 Q(2) E Thus Q(2) E LOOP:
20
Let us now consider Q(z ) B SUCC (z ):
Q(z) B SUCC (z) (Q1(z ) B SUCC (z ))k(Q1(z ) B SUCC (z ))t; (Q2(z ) B SUCC (z )) succ!1:DONE k(succ!1:DONE)t; (Q2(z) B SUCC (z)) succ!1:DONE k(Q2(z) B SUCC (z)) succ!1:DONE kQ(2) B SUCC (z) succ!1:DONE kLOOP
The behaviour of P (z ) SUCC (z ) is then:
P (z) SUCC (z) P 1 (z) SUCC (z)k(P 1(z) SUCC (z))t; P 2(z) SUCC (z)k (P 1 (z ) SUCC (z )kP 2(z ) SUCC (z ))t; P 3(z ) SUCC (z )) succ!0:DONE k(succ!0:DONE)t; Q(z) B SUCC (z)k (succ!0:DONE kQ(z ) B SUCC (z ))t; (P (z ) SUCC (z )) succ!0:DONE k(succ!0:DONE)t; (succ!1:DONEkLOOP )k (succ!0:DONE ksucc!1:DONE kLOOP )t; (P (z ) SUCC (z )) succ!0:DONE ksucc!1:DONEkLOOP
definition of Q
definition of Q1 lemma 4:6 definition of Q2 from above
definition of P definition of P 1; P 2 and P 3 from above lemma 4:6
5 Computational models for real implementations In this Section we present other computational models which can be used as a guide for real implementations. In these models, processes corresponding to clauses are not duplicated in guard processes. The rst model, described in section 5.1, expresses, as the basic one of Section 3, the maximal degree of parallelism: all the solutions are given without ordering among them. Thus it is equivalent to the basic model. In Section 5.2, we show how, by modifying the scheduler processes, we can introduce dierent orderings among solutions, still leaving parallel the executions of processes computing these solutions: the rst model sequentializes the sets of solutions coming from dierent processes (corresponding to dierent clauses), but leaving each set unordered, while the last one gives the solutions in the same order of a Prolog sequential interpreter.
5.1 A more ecient model
The de nition of the new computational model is intuitively given as follows. The translation of a goal is the same as in the basic model. Given a predicate p, de ned by n clauses, we obtain the corresponding processes P 1 ; : : :; P n as in the basic model. The new model diers from that one in the way P 1 ; : : :; P n are used in the de nition of the process constant P . When P starts its execution, all the processes P 1 ; : : :; P n are executed in parallel, without waiting for the termination of guards. The computed solutions are not sent through the succ port name but they are collected by other processes: the buer processes. A buer process, BUFFi , enqueues 21
the solutions computed by P i and sends them, on the succ port name, only when P 1 ; : : :; P i?1 are terminated. In essence, the buer processes implement a scheduling policy. This view is more exible of the one adopted in the previous model: the computational model can be changed, to cope with dierent orderings among solutions, by changing only the scheduling strategy, that is the de nition and the use of buer processes. Examples of this will be given in the next section. In the following de nition, after the termination of each process P i , a signal is sent through the i port name. This signal is used as a synchronization one for the buer processes. Moreover, a signal is sent, on the i port name allowing the process BUFFi to terminate its execution. The de nition of the process P is now the following. 8 > > > > > > > > > > > > > > > > > > > >
. n?1 (P (y) X )[fn?1]; n?1!: n?1 !:DONE k > > > > > > > (P n (y) X )[fn]; n!:DONE k > > > > > > BUFF1 k 1?:(BUFF2 k 2?:(BUFF3 k k n?1 ?:(BUFFn ) )))) > > > > > > n1 : nfsucci ; j ; i ; 1 i n ; 1 j < ng where fi = succi =succ A process BUFFi (1 < i n) is allowed to start its execution only when all the signals
1; 2; : : : i?1 are sent. Of course, the process BUFF1 is allowed to start its execution without waiting for any signal. A process BUFFi is de ned as follows. = succi?x:(succ!x:DONE kBUFFi ) + i ?:DONE BUFFi def According to this de nition, it collects all the solutions through the succi port name and sends them through succ. Each time a solution is taken from succi , it can be sent on succ, and, at the same time the process starts again its execution in parallel. Because of this duplication of the BUFFi process, and because of parallel execution, the order in which the solutions are sent through the succ port name is independent from the order in which they are received through the succi port name. Moreover, due to this parallelism, all the solutions for the whole process P are obtainable in any order. In fact, it can happen that the process BUFFj sends a solution through the succ port name before BUFFi (i < j ) does; this because when the process P j ?1 is terminated, the succ! actions of BUFFi and BUFFj proceed in parallel. 22
Finally, when the signal i is sent, the process BUFFi terminates. Let us now intuitively describe the execution of processes in this model. A process P i proceeds along all its possible paths in parallel. Each path goes on until it tries to send a solution on the succi port name: if the corresponding buer process is not active, the path becomes stuck. Thus, a process performs moves, along its possible paths, until only the communications through the succi port name have to be done. When the corresponding buer process (BUFFi ) starts its execution, the communications are performed and the solutions collected. This computational model can be used both as the reference semantics and as an implementation guide. Each process is not duplicated in guards, and its execution can start without waiting for the termination of guard processes. This allows a higher degree of parallelism and contributes to decrease the execution time. As an example consider a predicate p de ned by three clauses with corresponding CHOCS processes denoted by P 1 , P 2 and P 3 . The CHOCS process for p, P , is given by the following de nition.
P def = (terms?y:cont?X: ((P 1(y ) X )[f1]; 1!: 1!:DONE k (P 2 (y) X )[f2]; 2!: 2!:DONE k (P 3 (y) X )[f3]; 3!:DONE k BUFF1 k 1?:(BUFF2 k 2?:(BUFF3 )))) nfsucci; j ; i 1 i 3 1 j < 3g Where fi = succi =succ. Consider now the goal p(t4 ) and the corresponding process P (t4 ). The three processes, P 1 (t4 ); P 2 (t4) and P 3 (t4 ) are executed in parallel. The solutions of the process P 1 (t4) are sent on the succ1 port name and captured by the process BUFF1 , which, asynchronously, can send them, in any order through the succ port name. The computation of P 2 (t4) proceeds along each of all possible paths until they try to communicate on the succ2 port name. If P 1 (t4 ) is terminated, then BUFF2 is active, and the solutions are collected, otherwise the path trying to perform the communication is suspended. Notice that the suspension of a path depends only from an attempt to communicate through the succ2 port name, and this communication is the last action the computation can do, on that path, before termination. Thus, if all the computation paths of P 2 (t4) are suspended, because the computation of P 1 (t4 ) is not terminated, when P 1 (t4 ) does terminate, all the solutions for P 2 (t4) are already computed, and the time to complete the execution is only given by the time consumed in the communications with BUFF2 . By considering this time as marginal, we can approximate the execution of P (t4 ) by maxfT (P 1(t4)); T (P 2(t4)); T (P 3(t4))g. The following theorem states the equivalence between this computational model and the basic one presented in Section 3.2. Given a program P and a goal G, by GbP (shortly Gb ) we denote its translation in CHOCS processes according to the last de nition.
Theorem 5.1
Let P be a Prolog program and G a goal; then Ga Gb . Proof. By de ning suitable bisimulations: see the Appendix.
23
5.2 Introducing an ordering among the solutions
The exibility of the previous computational model can be shown by presenting how to obtain dierent scheduling policies (and, correspondingly, a dierent ordering in presenting the solutions) by simple changes in the use and de nition of buer processes. As we discussed in the introduction, the presented models exploit the maximal degree of parallelism in the OR-parallel implementation of Prolog. In the previous model, the degree of parallelism can be argued from the way the buer processes are scheduled: all buer processes, after their activation, can proceed in parallel. This means that the solutions can be given in any order. In many OR-parallel implementations, the standard operational semantics of Prolog is retained. This means that the solutions are given in the same order standard Prolog would. In our model we can obtain the same eect by altering the use of buer processes. We can force the sequentiality among their execution instead of executing them in parallel. The following de nition gives this new computational model. 8 > > > > > > > > > > > > > > > > > > > >
n?1 (P (y) X )[fn?1]; n?1 !:DONE k > > > > > > > (P n (y) X )[fn]; n!:DONE k > > > > > > (BUFF 1; BUFF2 ; BUFF3 ; BUFFn ) ) > > > > > > n1 : nfsucci ; i 1 i ng where fi = succi =succ In this de nition the execution of BUFFi processes is sequential. BUFFi+1 can start its execution only when BUFFi is terminated. Thus, leaving the de nition of buers unmodi ed, we force the solutions of a process P i to be sent, through succ, before the ones of P i+1 . However, note that we do not force any order among the solution given by the same buer, because the de nition of buer processes is the same as before: that is, internally to a buer, the succ! actions are executed in parallel. The following theorem states that that the solutions, in this model, are exactly the same of the previous one, the only dierence being in the freedom degree in the order they are given. Let us denote by GcP (shortly Gc ) the translation of a goal G accordingly to the introduced computational model.
Theorem 5.2
Let P be a Prolog program and G a goal; then Gb / Gc; and the multiset of solutions produced by Gc on the succ port name coincides with SPchocs (G).
24
Proof. Similar to the proof of theorem 5.1. Finally, we can furtherly modify the model to obtain the full sequentiality of standard Prolog. To do this we change the internal de nition of the buer processes to force sequentiality among the actions of sending solutions through the succ port name. The new buer de nition is the following
BUFFi def = succi?x:succ!x:DONE ; BUFFi + i ?:DONE By denoting with Gd the translation of a goal G accordingly to this new de nition we state the following theorem.
Theorem 5.3
Let P be a Prolog program and G a goal; then
Gc / Gd; and the multiset of solutions produced by Gd on the succ port name coincides with SPchocs (G). Proof. Similar to the proof of theorem 5.1. We remark that the converse of the rst point of theorems 5.2 and 5.3 is not true in general; an example of goal G such that Gc does not simulate Gb and Gd does not simulate Gc is shown in the following sub-section. Thus we have de ned a hierarchy of models, each one simulating the next one, from more parallel to more sequential in giving the solutions. Let us, once again, remark that the increasing in sequentiality applies only to the way the solutions are given. The OR-parallel execution is not aected. We can conclude by remarking that, by slight changes in the de nition of the part dealing with scheduling policy, we can model a variety of parallel Prolog implementations. Thus the computational model can be used as a guide for all of them.
5.3 An example
Let us consider the following Prolog program: p(3) p(x) q(x) q(1) q(2) and the goal G = p(x). The standard Prolog interpreter gives the solutions x ! 3, x ! 1 and x ! 2, and then terminates. Let us observe the behaviour of the goal G in all the CHOCS models given in the previous 25
sections. We start with the rst model, given in Section 3.2. By theorems 4.9 and 4.10, we have that:
Ga succ!3:DONE ksucc!1:DONEksucc!2:DONE Also in the model of Section 5.1, we have, using the theorem 5.1, that:
Gb succ!3:DONE ksucc!1:DONEksucc!2:DONE: In the third model, the rst one given in Section 5.2, the CHOCS translation of the Prolog program, seen above, is: P def = terms?y:cont?X:((P 1 (y ) X )[f1]; 1!:DONE k(P 2(y ) X )[f2]; 2!:DONE k BUFF1 ; BUFF2 )nf 1; 2; succ1; succ2g def 1 P = terms?y:cont?X:(UNIFY (y; 3) X ) P 2 def = terms?y:cont?X:(UNIFY (y; x) Q(x) X ) Q def = terms?y:cont?X:((Q1(y ) X )[f1]; 1!:DONE k(Q2(y ) X )[f2]; 2!:DONE k BUFF1 ; BUFF2 )nf 1; 2; succ1; succ2g def 1 Q = terms?y:cont?X:(UNIFY (y; 1) X ) Q2 def = terms?y:cont?X:(UNIFY (y; 2) X ) We have Gc = P (x) SUCC (x): The call Q(x) SUCC (x) is rst examined.
Q(x) SUCC (x)
fde nition of Qg ((Q1(x) SUCC (x))[f1]; 1!:DONE k(Q2(x) SUCC (x))[f2]; 2!:DONE k BUFF1 ; BUFF2 )nf 1; 2; succ1; succ2g fby de nition of Q1 and Q2g ((succ!1:DONE )[f1]; 1!:DONE k(succ!2:DONE )[f2]; 2!:DONE k BUFF1 ; BUFF2 )nf 1; 2; succ1; succ2g fde nition of BUFF1 g (DONE ; 1!:DONE k(succ2!2:DONE ); 2!:DONE k (succ!1:DONE kBUFF1); BUFF2 )nf 1; 2; succ1; succ2g flemma 4.2.1 and de nition of BUFF1 g (DONE k(succ2!2:DONE ); 2!:DONE k (succ!1:DONE kDONE ); BUFF2)nf 1; 2; succ1; succ2g + succ!1 26
(DONE k(succ2!2:DONE ); 2!:DONE k(DONE kDONE ); BUFF2)nf 1; 2; succ1; succ2g flemmas 4.2.1 and 4.2.2 and de nition of BUFF2 g (DONE kDONE ; 2!:DONE ksucc!2:DONE kBUFF2)nf 1; 2; succ1; succ2g
flemma 4.2.1 and de nition of BUFF2 g (DONE kDONE ksucc!2:DONE kDONE )nf 1; 2; succ1; succ2g flemma 4.2.2g (succ!2:DONE )nf 1; 2; succ1; succ2g (succ!2:DONE ) + succ!2
DONE Then Q(x) SUCC (x) succ!1:succ!2:DONE . The behaviour of Gc is the following. Gc
fde nition of P g ((P 1(x) SUCC (x))[f1]; 1!:DONE k(P 2(x) SUCC (x))[f2]; 2!:DONE k BUFF1 ; BUFF2 )nf 1; 2; succ1; succ2g fby de nition of P 1 and P 2g ((succ!3:DONE )[f1]; 1!:DONE k(Q(x) SUCC (x))[f2]; 2!:DONE k BUFF1 ; BUFF2 )nf 1; 2; succ1; succ2g ffrom aboveg ((succ!3:DONE )[f1]; 1!:DONE k(succ!1:succ!2:DONE )[f2]; 2!:DONE k BUFF1 ; BUFF2 )nf 1; 2; succ1; succ2g + succ!3 ((succ!1:succ!2:DONE )[f2]; 2!:DONE kBUFF2)nf 1; 2; succ1; succ2g fby de nition of BUFF2 g succ!1:DONE ksucc!2:DONE Thus Gc succ!3:(succ!1:DONE ksucc!2:DONE ).
Before showing the behaviour of Gd , we note that, also in this model, Q(x) SUCC (x) succ!1:succ!2:DONE.
Gd 27
fde nition of P g ((P 1(x) SUCC (x))[f1]; 1!:DONE k(P 2(x) SUCC (x))[f2]; 2!:DONE k BUFF1 ; BUFF2 )nf 1; 2; succ1; succ2g fby de nition of P 1 and P 2g ((succ!3:DONE )[f1]; 1!:DONE k(Q(x) SUCC (x))[f2]; 2!:DONE k BUFF1 ; BUFF2 )nf 1; 2; succ1; succ2g ffrom aboveg ((succ!3:DONE )[f1]; 1!:DONE k(succ!1:succ!2:DONE )[f2]; 2!:DONE k BUFF1 ; BUFF2)nf 1; 2; succ1; succ2g + succ!3 (DONE k(succ2!1:succ2!2:DONE ); 2!:DONE kBUFF2)nf 1; 2; succ1; succ2g + succ!1 (DONE k(succ2!2:DONE ); 2!:DONE kBUFF2)nf 1; 2; succ1; succ2g + succ!2
DONE Thus Gd succ!3:succ!1:succ!2:DONE , because succ!3; succ!1; succ!2 is the only sequence of moves that Gd can do.
6 Conclusions The idea of the paper is to use a concurrent calculus to specify a parallel computational model for Prolog. The idea of using a concurrent calculus for specifying the Prolog semantics is not new; it was introduced in [23], where CCS is used, and developed in [16], where the model is de ned using -calculus. What is new in our paper is the use of the calculus for specifying a parallel computational model. In fact all the known models of Prolog, given by a process algebra, describe the sequential execution of the language. In our model we have used CHOCS: in fact the use of a higher order process calculus makes the modelling of continuations in OR-Parallel Prolog programs easier. There are many advantages in using a concurrent calculus for modelling OR-parallel Prolog executions. The resulting system can be studied by using all the tools and the techniques developed for the calculus itself. As an example of this, we use the notions of bisimulation and simulation (and the proof techniques associated with them), to discuss the relations among dierent parallel computational models. The use of a concurrent calculus makes evident the exploitable parallelism. In the rst two models we presented, the Prolog solutions are given without a speci c order, with the maximal degree of OR-parallelism. The computational models corresponding to many real implementations (in which the order of standard Prolog solutions is preserved) can be obtained by slight restrictions of the parallelism degree, as we did in the last two models. 28
Let us now consider the cut operator of Prolog. The modelling or implementation of cut in an OR-parallel execution framework is a conceptually dicult task [15]. Processes corresponding to dierent clauses are started in parallel, but, if a clause contains a cut and the corresponding process reaches it during the execution, the execution of all processes generated by subsequent clauses should be stopped. Actually, this behaviour is not easy to implement, because the processes to be stopped are dynamically generated as the execution proceeds. Recall that each process corresponding to a goal is replaced by the parallel composition of the processes corresponding to the applicable clauses. A solution cannot simply be given by disregarding all the answers returned by subsequent clauses: this would maintain the semantics of Prolog as multiset of solutions, but would not respect its computational behaviour. In fact, to maintain this behaviour, a subsequent clause with an in nite computation should be stopped. This can be accomplished by complicating the execution model by adding to each process (corresponding to the clauses following a cut) an \enabling" signal. Such signal should be \disabled" if the cut is reached. Let us remark again that the resulting complication of the computational model would re ect the diculty of handling Prolog cut in an OR-parallel setting. Finally, as a future work, we would like to apply the ideas of this paper to other parallel computational models of logic programming, such as, for example, OLDT-resolution.
References [1] H. Alshawi, and D.B. Moran The Delphi Model and some Preliminary Experiments. In Proc. of the Fifth Int. Conference on Logic Programming, pages 1578{1589. MIT Press, 1988. [2] 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. [3] B. Arbab and D.M. Berry. Operational and Denotational Semantics of Prolog. Journal of Logic Programming, 4:309{330, 1987. [4] R. Barbuti, M. Codish, R. Giacobazzi, and G. Levi. Modelling Prolog control. In Proceedings of the Nineteenth Annual ACM Symposium on Principles of Programming Languages, pages 95{104, January 1992. [5] R. Barbuti, M. Codish, R. Giacobazzi, and G. Levi. Modelling Prolog Control. Journal of Logic and Computation, 3:579{603, 1993. [6] R. Barbuti, M. Codish, R. Giacobazzi, and M. Maher. Oracle Semantics for Prolog. to appear in Information and Computation. [7] R. Barbuti, N. De Francesco and A. Santone. Modelling OR-Parallel Execution of Prolog using CHOCS. In Proc. of the Twelfth Int. Conference on Logic Programming. MIT Press, 1995. [8] L. Beckman, R. Gustavsson, and A. Waern. An Algebraic Model of Parallel Execution of Logic Programs. In Proc. Logic in Computer Science, pages 50{57. 1986. 29
[9] E. Borger, and D. Rosenzweig. A Mathematical De nition of Full Prolog. Science of Computer Programming, 1994, (to appear). [10] A. Bossi, M. Bugliesi, and M. Fabris. A New Fixpoint Semantics for Prolog. In Proc. of the Tenth Int. Conference on Logic Programming, pages 374{389. MIT Press, 1993. [11] R. Cleaveland, J. Parrow, and B. Steen. The Concurrency Workbench. In Proc. of Workshop on Automatic Veri cation Methods for Finite State Systems, volume 407 of Lecture Notes in Computer Science. Springer-Verlag, Berlin, 1990. [12] S. K. Debray and P. Mishra. Denotational and Operational Semantics for Prolog. In M. Wirsing, editor, Formal Description of Programming Concepts III, pages 245{269. North-Holland, Amsterdam, 1987. [13] A. de Bruin and E. de Vink. Continuation semantics for Prolog with cut. In J. Diaz and F. Orejas, editors, Proc. CAAP 89, volume 351 of Lecture Notes in Computer Science, 178{192. Springer-Verlag, Berlin, 1989. [14] S.A. Delgado-Rannaudo. OR-Parallel Logic Computational Models. In [15]. [15] Implementations of Distributed Prolog. P. Kacsuk and M.J. Wise eds., Wiley, 1992. [16] B.Z. Li. A -calculus Speci cation of Prolog. In D. Sannella, editor, Proc. ESOP 94, volume 788 of Lecture Notes in Computer Science, 379{393. Springer-Verlag, Berlin, 1994. [17] J. W. Lloyd. Foundations of Logic Programming. Springer-Verlag, Berlin, 1987. Second edition. [18] E. Lusk et al. The Aurora Or-parallel Prolog System. New Generation Computing, 7:243{ 271, 1990. [19] E. Madelaine and D. Vergamini. Auto: a Veri cation Tool for Distributed Systems. In Proc. of FORTE 89 Conference, North-Holland, 1989. [20] R. Milner. Communication and Concurrency. Prentice-Hall, 1989. [21] R. Milner, J. Parrow and D. Walker. A Calculus of Mobile Processes. Information and Computation, 100:1{77, 1992. [22] R. Ramakrishnan. Parallelism in Logic Programs. In Proceedings of the Seventeenth Annual ACM Symposium on Principles of Programming Languages, pages 246{260, January 1990. [23] B.J. Ross, and A. Smaill An Algebraic Semantics of Prolog Program Termination. In Proc. of the Eighth Int. Conference on Logic Programming, pages 316{330. MIT Press, 1991. [24] B. Thomsen. A Calculus of Higher Order Communicating Systems. In Proceedings of the Sixteenth Annual ACMSymposium on Principles of Programming Languages, pages 143{ 154, 1989. [25] B. Thomsen. Plain CHOCS. A Second Generation Calculus for Higher Order Processes. Acta Informatica, 30:1{59, 1993. 30
[26] D.H.D. Warren The SRI Model for Or-parallel Execution of Prolog - Abstract Design and Implementation Issues. In Proc. 1987 Symp. on Logic Programming, pages 92{102. IEEE Press, 1987.
31
7 Appendix
7.1 Proof of lemma 4.3 Lemma 4.3
For each goal G, Ga is well-formed. Proof. Point 1. Point 1 (i) of the de nition of well-formedness is obvious because there is no occurrence of ? in the process de nition obtained by the translation. Point 1 (ii) ( is straightforward, since each occurrence of ! in the processes de nition obtained by the translation is within the expression !:nil. The proof of point 1 (ii) ) is made by induction on the length of the nite derivations of Ga. Let G = q1(t1); : : :; qn (tn ) n 1 then: Ga = Q1(t1) Qn(tn ) SUCC (x) n 1 Base step: We have the shortest derivations when n = 1 and either there are no clauses for q1 or there is only one unit clause for q1 . We have:
Q1(t1 ) SUCC (x) and the two possible de nitions of Q1 are : 1. Q1 def = terms?y:cont?X:DONE 2. Q1 def = terms?y:cont?X:(Q11(y) X ) Q11 def = terms?y:cont?X:(UNIFY (y; t) X ) Consider case 1.
Q1 (t1 ) SUCC (x)
fde nition of Q1 and Q1(t1)g (terms?y:cont?X:DONE kterms!t1:DONE )nftermsg SUCC (x) # ((cont?X:DONE )[t1 ! y ]kDONE )nftermsg SUCC (x) fde nition of g (((cont?X:DONE )[t1 ! y ]kDONE )nftermsgkcont!SUCC (x):DONE )nfcontg fde nition of CHOCS substitutiong ((DONE kDONE )nftermsgkDONE )nfcontg flemma 4.2.2 and de nition of DONEg DONE 32
We have shown that Q1 (t1) SUCC (x) DONE and by lemmas 4.1.3 and 4.1.5 we can say that Q1 (t1 ) SUCC (x) is well-formed. Consider case 2. Q1(t1) SUCC (x) where x = vars(t1 ) fde nition of Q1 and Q1(t1)g (terms?y:cont?X:(Q11(y ) X )kterms!t1:DONE )nftermsg SUCC (x)
# ((cont?X:(Q11(y ) X ))[t1 ! y ]kDONE )nftermsg SUCC (x) fde nition of g (((cont?X:(Q11(y) X ))[t1 ! y ]kDONE )nftermsgkcont!SUCC (x):DONE )nfcontg fde nition of CHOCS substitutiong (((Q11(t1 ) SUCC (x))kDONE ) nftermsgkDONE ) nfcontg {z
|
}
P
|
{z
}
Q
fterms 62 dynamicsort(P ); cont 62 dynamicsort(Q) and P nfg P if 62 dynamicsort(P )g (Q11(t1 ) SUCC (x))kDONE kDONE flemma 4.2.2g (Q11(t1 ) SUCC (x))kDONE fde nition of Q11 and Q11(t1)g ((terms?y:cont?X:(UNIFY (y; t) X )kterms!t1:DONE )nftermsg SUCC (x))kDONE (((cont?X:(UNIFY (y; t) X ))[t1 ! y ]kDONE )nftermsg SUCC (x))kDONE fde nition of g (((cont?X:(UNIFY (y; t)X ))[t1 ! y ]kDONE )nftermsgkcont!SUCC (x):DONE )nfcontgkDONE fde nition of CHOCS substitutiong (((UNIFY (t1 ; t) SUCC (x))kDONE ) nftermsgkDONE ) nfcontgkDONE |
|
{z
P
}
{z
}
Q
fterms 62 dynamicsort(P ); cont 62 dynamicsort(Q)g (UNIFY (t1 ; t) SUCC (x))kDONE kDONE kDONE flemma 4.2.2g (UNIFY (t1; t) SUCC (x))kDONE
By de nition of UNIFY there are two cases:
33
i) cont?X:DONE mgu(t; t1) = fail ii) cont?X:apply (X; #) mgu(t; t1) = # Case i): ((cont?X:DONE ) SUCC (x))kDONE flemma 4.2.3g
DONE kDONE DONE We have that Q1 (t1 ) SUCC (x) DONE and by lemmas 4.1.3, 4.1.5 we have proved the thesis. Case ii)
((cont?X:apply (X; #)) SUCC (x))kDONE fde nition of g (cont?X:apply (X; #)kcont!SUCC (x):DONE )nfcontgkDONE fde nition CHOCS substitutiong (|apply (SUCC{z (x); #)kDONE}) nfcontgkDONE P
fcont 62 dynamicsort(P )g apply(SUCC (x); #)kDONEkDONE flemma 4.2.2g
apply(SUCC (x); #) Q1 (t1) SUCC (x) apply(SUCC (x); #) and by lemma 4.1.3 (clearly SUCC (x) is well-formed)
we have proved the thesis. Inductive step: Let us assume that the thesis holds for goals with derivations shorter than m. Consider a goal Q1(t1 ) Qn (tn ) SUCC (x) with derivations with length m. There are two possible de nitions of Q1 : 1. Q1 def = terms?y:cont?X:DONE 2. Q1 def = terms?y:cont?X: (Q11 (y) X k(Q11(y ) X )t; Q21(y ) X k k ) Z (Q11 (y) X k kQn1 ?1 (y ) X )t; Qn1 (y) X ) Consider case 1. Similar to the proof done before. Consider case 2. Q1 (t1 ) Qn (tn) SUCC (x) fde nition of Q1 and Q1(t1)g (terms?y:cont?X:Z kterms!t1 :DONE )nftermsg Q2 (t2) Qn (tn ) SUCC (x)
# 34
((cont?X:Z )[t1 ! y ]kDONE )nftermsg Q2 (t2 ) Qn (tn ) SUCC (x) fde nition of g (((cont?X:Z )[t1 ! y ]kDONE )nftermsgkcont!(Q2(t2 ) Qn (tn )SUCC (x)):DONE )nfcontg fde nition of Z and CHOCS substitutiong (((Q11(t1 ) Qn (tn ) SUCC (x)k(Q11(t1 ) Qn (tn ) SUCC (x))t; Q21(t1 ) Qn(tn ) SUCC (x)k k (Q11 (t1) Qn (tn ) SUCC (x)k kQn1 ?1 (t1 ) Qn (tn ) SUCC (x))t; Qn1 (t1) Qn(tn ) SUCC (x))kDONE )nftermsgkDONE)nfcontg fcont 62 dynamicsort(P1 P2) and terms 62 dynamicsort(P (t))g ((Q11(t1) Qn (tn ) SUCC (x)k(Q11(t1 ) Qn (tn ) SUCC (x))t ; Q21(t1 ) Qn(tn ) SUCC (x)k k (Q11 (t1) Qn (tn ) SUCC (x)k kQn1 ?1 (t1 ) Qn (tn ) SUCC (x))t; Qn1 (t1) Qn(tn ) SUCC (x))kDONE By de nition of Qi1 (t1 ), i 2 f1; : : :; ng, Qi1(t1 ) Qn (tn ) SUCC (x) is equivalent to a process of the form: P1 (t1 ) Pn (tn ) SUCC (x) and its derivations are of length < m, so by inductive hypothesis and lemmas 4.1.3 and 4.1.4 we have proved the thesis. Point 2. We can equivalently prove that the existence of a path which is not the pre x of any longer path implies that all paths are nite. Thus the proof can be done by induction on the length of the nite derivations.
7.2 Proof of lemma 4.8 Lemma 4.8
G is path nite in ???! i Ga is path nite in ! . sld
P Proof: ()) By induction on the length m of the nite derivations by ???! . sld Base step: We have the shortest derivation when the goal is G = p(t) and there are either
no clauses or only unit clauses for p. The rst case is straightforward. For the second case we have: C1 : p(t1 ) .. .
Cn : p(tn )
where n 0. By de nition of transition relation associated to P by SLD resolution we have: #1 ;C1 sld P
if mgu(t; t1 ) = #1 then G???! 35
.. .
#n ;Cn sld P
if mgu(t; tn ) = #n then G???! The corresponding CHOCS translation is the following:
P def =
terms?y:cont?X:(P 1 (y) X k(P 1(y) X )t; P 2(y) X k k (P 1 (y) X k kP n (y) X )t; P n (y ) X ) def P 1 = terms?y:cont?X:(UNIFY (y; t1) X )
P n def = def Ga =
.. . terms?y:cont?X:(UNIFY (y; tn) X ) P (t) SUCC (z) where z = vars(t)
Thus:
P (t) SUCC (z) P 1 (t) SUCC (z)k(P 1(t) SUCC (z))t; P 2(t) SUCC (z)k k (P 1 (t) SUCC (z)k kP n?1 (t) SUCC (z))t; P n (t) SUCC (z) E1k(E1)t; E2k k(E1k kEn?1)t; En
where, by de nition of UNIFY , if mgu(t; ti ) = #i , then Ei = apply (SUCC (z); #i), otherwise (mgu(t; ti ) = fail) Ei = DONE . It is easy to prove that if E; F are path nite then E kF , E t and E ; F are path nite too. We can also prove that if E F and F is path nite then E is path nite. So by path niteness of SUCC (z) and DONE we have proved the thesis. with length Inductive step: Let us assume that the theorem holds for derivations by ???! sld < m and let us consider a generic goal G = a : B, where a = p(t), with derivations of length m. Let hC1; : : :; Cni be the clauses for p:
C1 : p(t1)
Then
Cn : p(tn )
q11 ; : : :; q1m1 .. .
qn1 ; : : :; qnmn
#1 ;C1 sld P
if #1 = mgu(t; t1) then G???! G1 = (q11 ; : : :; q1m1 :: B )#1 .. .
#n ;Cn
if #n = mgu(t; tn ) then G???! Gn = (qn1 ; : : :; qnmn :: B )#n sld P
36
We have that G1; : : :Gn are path nite with derivations length < m. Let us consider the CHOCS translation:
Ga def = P (t) B a P1 def = terms?y:cont?X (P11(y ) X k(P11(y ) X )t; P12 (y) X k k(P11(y) X k kP1n?1 (y) X )t; P1n(y) X ) = terms?y:cont?X:(UNIFY (y; t1) Q11 Q1m1 X ) P11 def .. .
P1n def = terms?y:cont?X:(UNIFY (y; tn ) Qn1 Qnmn X ) We have
E with jj > 0 Ga =)
where
E = UNIFY (y; t1) Q11 Q1m1 B a k (UNIFY (y; t1) Q11 Q1m1 B a )t ; UNIFY (y; t2) Q21 Q2m2 B a k k (UNIFY (y; t1) Q11 Q1m1 B a k k UNIFY (y; tn?1 ) Qn?11 Qn?1mn?1 B a )t; UNIFY (y; tn) Qn1 Qnmn B a If mgu(t; ti) = #i , then by de nition of UNIFY UNIFY (y; ti ) Qi1 Qimi B a
else (mgu(t; ti ) = fail)
apply(Qi1 Qimi B a ; #i) = Gai UNIFY (y; ti ) Qi1 Qimi B a DONE
Since, by inductive hypothesis the derivations of the Gi 's are shorter than m, then the Gai 's are path nite; moreover DONE is path nite; thus E , which is the parallel composition of them, is path nite by !. (() By induction on the length of the nite derivations by !.
7.3 Proof of theorem 4.10
Before proving the theorem we introduce some notation and two lemmas. Given a multiset of processes A = fj E1; E2; : : :; Es jg, we denote by kA the parallel compositions of the processes in A, kA = E1kE2k : : : kEs (the order is not relevant because k is commutative)
37
Lemma 7.1
Let P be a Prolog program composed by the sequence of clauses hC1; C2; : : :; Cni, and G be a goal. We have that Ga kG0akDONE , for all hG0a; #i 2 chocs1P (G) Proof If G is empty, the proof is obvious. Elsewhere, let G = p(t) : B with hCp1 ; Cp2 ; : : :; Cpm i being the sequence of clauses for p in the order they occur in P . If m = 0 then there are no clauses applicable to G, then A = ; and Ga DONE . If m > 0 we have, by de nition of the translation:
Ga = P (t) B a E = E1k(E1)t; E2k : : : k(E1k : : : kEm?1 )t; Em where Ei = P i (t) B a By de nition of UNIFY and resolvea, we have that, i j m if resolvea(Ga; Cpj ) = hGj ; #j i j Ej = G DONE if resolvea(Ga; Cpj ) = DONE By applying lemma 4.6 to E , we have that E E1k : : : kEk , where k = minfijEi is not path finiteg, or k = m if all the Ei's are path nite. By de nition of chocs1P (G) we have fj G0ajhG0a; #i 2 chocs1P (G) jg = fj Ei jEi 6= DONE jg. The proof follows from lemma 4.2.2, ensuring that, for each process E , E kDONE E .
Lemma 7.2 If hsucc!t:DONE; #i 2 chocskP (G), for some k, then t = #(vars(G)) Theorem 4.10 Let P be a Prolog program and G a goal. Then Ga kisucc!(#i(vars(G))):DONE ; or Ga (kisucc!(#i(vars(G))):DONE kLOOP ), for all #i 2 SPchocs (G). Proof. For each i let us de ne the following multisets of processes
X i = fj succ!t:DONE jhsucc!t:DONE; #i 2 chocsiP (G) jg Y i = fj G0ajhG0a; #i 2 chocsiP (G) and G0a 62 X i jg
Note that X i contains all processes which send a solution and then terminate, while Y i contains all processes which can be furtherly derived. It easy to see that X i ] Y i = fj G0ajhG0a; #i 2 chocsiP (G) jg First we prove that, for each i 1, kY i kX i+1kY i+1 . If Y i = ; then X i+1 = Y i+1 = ;. To prove the other cases we have to notice that the following equivalence holds: if Y i = U i +1 a a a fj G1 ; G2; : : :; Gs jg then chocsP (G) = j chocs1P (Gj ), where Gj is the Prolog goal corresponding to the process Gaj . Thus, if Y i contains only one element, the proof directly follows from lemma 7.1. Let Y i = fj Ga1 ; Ga2; : : :; Gan jg ; n > 1 and let, for all r; 1 r n; Gar kAr , where Ar is de ned as Ar = fj G00ajhG00a; #00i 2 chocs1P (Gar ) jg. We have kY i kA1 : : : kAn , which is equivalent 38
(recall lemma 4.2.2) to kX i+1kY i+1 . We have that thus, for each i,
Ga k(X 0 ] Y 0 )
Ga X 0kX 1k : : : kX ikY i+1 The proof is completed by lemma 7.2, which ensures that X i is the multiset of processes sending on succ the solutions belonging to chocsiP (G).
7.4 Proof of theorem 5.1
In the following we denote as Ga ; P a ; P ia and as Gb ; P b ; P ib the translation, in CHOCS, of a goal G, a predicate p and the ith clause for p, using the models given in Sections 3.2 and 5.1 respectively. Before proving the theorem we state the following lemmas:
Lemma 7.3
Consider the process expression:
E = (Gb1[f1]; 1!: 1!:DONE k kGbn?1 [fn?1 ]; n?1!: n?1!:DONE k Gbn [fn ]; n!:DONE kBUFF1k
1?:(BUFF2 k k n?1 ?:(BUFFn ) ))nfsucci; j ; i 1 i n 1 j < ng where Gb1 ; : : :; Gbn are obtained by goal translations. Let k = minfi j Gbi is not path finiteg or k = n if, for each 1 i n; Gbi is path nite. Then:
E (Gb1[f1]; 1!:DONE kBUFF1k kGbk [fk ]; k !:DONE kBUFFk )nfsucci; i 1 i kg
Proof. By de ning a suitable bisimulation and by considering that it is possible that the process
BUFFj sends a solution through the succ port name before BUFFi (i < j ) does; this may occur because, when all the processes Gbi (i < j ) are terminated, BUFFi and BUFFj , whose task is that of enqueuing the solutions of Gbi and Gbj , can proceed in parallel.
Lemma 7.4
Ga is path nite , Gb is path nite
Proof. The prove is similar to the one of lemma 4.8. Lemma 7.5 Ga1 k kGak (Gb1[f1 ]; 1!:DONE kBUFF1k kGbk [fk ]; k !:DONE kBUFFk )nfsucci; i 1 i kg where for each j 2 f1; : : :; k ? 1g Gaj; Gbj are path nite. Proof. We show that R is weak bisimulation, where R = f((Ga1k kGakkR); (Gb1[f1 ]; 1!:DONE kBUFF1k kGbk [fk ]; k !:DONE kBUFFk kR)nfsucci; i 1 i kg)j where : ? Ga1 ; : : :; Gak?1; Gb1; : : :; Gbk?1 are path nite; 39
? R is any well-formed process with dynamicsort(R) f; succgg We use the following shorthands: E = Ga1 k kGak kR; F = (Gb1[f1]; 1!:DONE kBUFF1k kGbk [fk ]; k !:DONE kBUFFk kR)nfsucci; i 1 i kg: Consider the possible cases of moves of E . Note that dynamicsort(E ) f; succg, dynamicsort(F ) f; succg and that Ga1; : : :; Gak; R do not communicate each other. E 0. E =) Let us consider only the general case in which all the processes Ga1 ; : : :; Gak ; R perform silent actions. The other ones are particular cases of that. R0 . We assume that R=) Let Gai = Pi a (ti ) CONTia for each i 2 f1; : : :; kg and CONTia is a goal translation, then E = P1a(t1 ) CONT1ak kPka(tk ) CONTkakR By de nition of Pia (ti ) and using lemma 4.6 (where nk = minfijPki (Tk ) is not path finiteg or nk equalsa the number of clauses with head predicate pk ) we have: a E P11 (t1) CONT1ak kP1n1 a (t1 ) CONT 1 k k a Pk1?aa 1 (tk?1 ) CONTka?1k kPakn?k?1 1 (tk?1 ) CONTa ka?1k nk ?1 (t ) CONT akP nk (t ) CONT akR a Pk1 (tk ) CONT k k k k kPk k k k a j a By de nition of Pi = terms?y:cont?X:(UNIFY (y; tij ) Cij X ), where Ciaj = Qaij1 Qaijn ijn 0, we have E 0 = E1a1 k kEkank kR0 where if mgu(ti; tij ) = #ij then Eiaj = apply(Ciaj CONTia; #ij ) else if (mgu(ti ; tij ) = fail)
Eiaj = DONE
Thus E1a1 ; : : :; Ekank ?1 are path nite. Let us now consider F . We have Gbi = Pib (ti ) CONTib because the goal translation has the same structure in both models. F = (P1b (t1) CONT1b[f1]; 1!:DONE kBUFF1k k Pkb (tk ) CONTkb[fk ]; k !:DONE kBUFFkkR)nfsucci; i 1 i kg As we have seen before, but using lemma 7.3 instead of lemma 4.6, we have: F (P11 b (t1) CONT1b [f11 ]; 11 !:DONE kBUFF11 k k P1n1 b (t1 ) CONT1b[f1n1 ]; 1n1 !:DONE kBUFF1n1 k k Pk1b (tk ) CONTkb [fk1 ]; k1 !:DONE kBUFFk1 k k Pknk b (tk ) CONTkb[fknk ]; knk !:DONE kBUFFknk kR)nfsucci; i 11 i knk g where P11 b (t1 ) CONT1b, : : :, Pknk ?1 b (tk ) CONTkb are path nite. Recalling that the translation of a clause is equal in both models and de ned as Pij b = terms?y:cont?X:(UNIFY (y; tij ) Cij b X ), we have: F 0 = (E1b1 [f11 ]; 11 !:DONE kBUFF11 k k Ekbnk [fknk ]; knk !:DONE kBUFFknk kR0)nfsucci; i 11 i knk g 40
where if mgu(ti; tij ) = #ij then
Eibj = apply(Cibj CONTib; #ij ) else if (mgu(ti ; tij ) = fail)
Eibj = DONE
F 0 and (E 0; F 0 ) 2 R. Thus, F =)
E succ =)!tE 0: =)!tE 0 there are two possible cases. If E succ E ak kE a; 9j j 2 f1; : : :; ng : E a SUCC (t) succ!t:DONE ; 1: E =) 1 n j succ !t 0 2: R =) R : We prove only Case 1: because Case 2: is straightforward. Case 1: E 0 = E1ak kEja?1 kDONE kEja+1k kEna. F 00 , where F =) F 00 = (E1b[f1]; 1!:DONE kBUFF1k kEnb [fn]; n!:DONE k BUFFn kR)nfsucci; i 1 i ng =)!tDONE , Because Eja SUCC (t) i Ejb SUCC (t) then Ejb[fj ]; j !:DONE kBUFFj succ F 0 , where F =) F 0 = (E1b[f1 ]; 1!:DONE kBUFF1k kDONE k kEnb [fn ]; n!:DONE k BUFFn kR)nfsucci; i 1 i ng Clearly, (E 0; F 0) 2 S . E 0. The proof can be done in a similar way. E =)
This proves condition (i) in the de nition of weak bisimulation; condition (ii) follows by a symmetric argument, therefore we omit the proof but it is interesting to consider the move of F because is a little bit dierent from the previous case. F0 F =) In this case it is possible, among the others, a silent communication corresponding to the exchange of a solution between a process and its buer. More precisely: there is a F 00 where: derivation such that F =) 00 b F = (F1 [f1 ]; 1!:DONE kBUFF1k kFkb [fk ]; k!:DONE k BUFFk kR)nfsucci; i 1 i kg and 9j such that Fjb SUCC (t) succ!t:DONE succ!t:DONE kDONE kDONE succ!t:DONE So Fjb [fj ]; j !:DONE kBUFFj =) F 0 = (F1b[f1 ]; 1!:DONE kBUFF1k ksucc!t:DONE k k Fkb [fk ]; k !:DONE kBUFFk kR)nfsucci; i 1 i kg E 0, As seen before and considering that Fjb SUCC (t) i Fja SUCC (t) we have E =) where E 0 = F1a k ksucc!t:DONE k kFka and clearly (E 0; F 0) 2 S 41
Theorem 5.1
Let P be a Prolog program and G a goal; then Ga Gb . Proof. Let Ga = P1a (t1) Pna (tn) SUCC (x). Then if P1a has n clauses, by de nition of P1a (t1 ) we have:
Ga P11aa(t1) Pna(tn ) SUCC (x)k (P11a (t1 ) Pna (tn ) SUCC (x))t; P12a (t1 ) a Pna (tn ) SUCC (x)k k (P11a (t1 ) Pna (tn ) SUCC (x)k kP1n?1 (t1 ) Pna (tn ) SUCC (x))t ; P1n (t1 ) Pna (tn ) SUCC (x) Let P1ka (t1 ) Pna (tn ) SUCC (x) be the rst process not path nite (k = n if there are only path nite processes). Using lemma 4.6 we have:
Ga P11a (t1) Pna (tn ) SUCC (x)k kP1ka (t1) Pna(tn) SUCC (x) Since P1ia (t1 ) = cont?X:(UNIFY (ti ; t1 ) Cia X ), where Cia = Qai1 Qain in 0, we have
Ga Ga1k kGak where if #i = mgu(ti; t1) then Gai = apply (Cia P2a (t2) Pna (tn ) SUCC (x); #i) else (mgu(ti; t1 ) = fail) Gai = DONE . Let us consider Gb = P1b (t1 ) Pnb (tn ) SUCC (x). By de nition of P1b (t1 ) we have:
Gb ((P11b (t1) Pnb (tn ) SUCC (x))[f1]; 1!: 1!:DONE k (P1nb (t1 ) Pnb (tn ) SUCC (x))[fn ]; n!: n !:DONE kBUFF1 k 1?:(BUFF2 k k n?1?:(BUFFn ) ))nfsucci; j ; i 1 i n 1 j < ng P1kb (t1) Pnb (tn) SUCC (x) is the rst process not path nite (or k = n) (lemma 7.4) and
by lemma 7.3 we have:
Gb ((P11b (t1) Pnb (tn) SUCC (x))[f1]; 1!:DONE kBUFF1k k (P1kb (t1) Pnb (tn ) SUCC (x))[fk ]; k !:DONE kBUFFk )nfsucci; i 1 i kg P1ib (t1 ) = cont?X:(UNIFY (ti ; t1 ) Cib X ), as we have seen before Gb (Gb1[f1]; 1!:DONE kBUFF1k kGbk [fk ]; k !:DONE kBUFFk )nfsucci; i 1 i kg Using lemma 7.5 we have proved the thesis.
42