Science of Computer Programming ELSEVIER
Science of Computer Programming 32 (1998) 145-176
Towards a logical semantics for pure Prolog’ Roberto Barbuti”, Nicoletta De Francesco b, Paolo Mancarella”? *, Antonella Santone b aDipartimento di Infbrmatica. Universitci di Pisa, I-56125 Piss. Italy bDipartimento di Ingeyneria dell’lnfom~azione, Universitri di Pisa. I-56125 Pisa, Ital)
Abstract The coincidence of the declarative and procedural interpretations of logic programs does not apply to Prolog programs, due to the depth-first left-to-right evaluation strategy of Prolog interpreters. We propose a semantics for Prolog programs based on a four-valued logic. The semantics
is based on a new concept of completion analogous to Clark’s and it enjoys the nice properties of the declarative semantics of logic programming: existence of the least Herbrand model, equivalence of the model-theoretic and operational semantics. @ 1998 Elsevier Science B.V. All rights resewed. Keywords:
Logic programming; Prolog; Semantics; Multi-valued logics
1. Introduction One of the most attractive features of the logic programming paradigm is the equivalence between its declarative and procedural reading. When looked at as a first-order theory, a collection of Horn clauses can be characterized by its least Herbrand model; when looked
at as a set of procedure
definitions,
a collection
of Horn clauses
can
be characterized by its success set, which coincides with the least Herbrand model. Unfortunately, it is well known that this equivalence is lost when moving from logic programming to Prolog programming, the reason being that Prolog interpreters use, for efficiency reasons, a depth-first left-to-right computation strategy. As a consequence, the declarative semantics of logic programming cannot be adopted as the abstract logical semantics for Prolog programs. For this reason, usually the semantics of Prolog is defined using non-logical frameworks [4-6,8-10,161. When dealing with computational issues, one has to abandon classical two-valued logic and has to move to multiple-valued logic. A first attempt is to adopt a three-valued logic where the third truth value (unde$ned) is introduced to model non-terminating * Corresponding author. E-mail:
[email protected]. ’ Work partially supported by the EEC Project KIT01 l-LPKRR. 0167~6423/98/$19.00 @ 1998 Elsevier Science B.V. All rights reserved. PII: SO167-6423(97)00033-6
146
computations
R. Barbuti et al. IScience
of Computer
Programming
32 (1998)
145-176
(see, e.g. [ 1,2, 14, 17,201). However, these three-valued
do not allow to model the computational In this paper we propose
behaviour
a logical semantics
based semantics
of Prolog.
for pure Prolog (without
extra-logical
features and negation) based on a four-valued logic. Roughly speaking, the fourth truth value is intended to model a computation in which a success is “followed by” a nontermination
as in the computation
of the goal c p with respect to the Prolog program
{P, PhPI. The semantics is based on the notion of sequential completion of a Prolog program, which differs from Clark’s completion in that the standard connectives A and V are interpreted as sequential conjunction and sequential disjunction. These connectives are suitably defined on our four-valued logic and their logical meaning reflect the computational behaviour of Prolog: A models the left-to-right computation rule of Prolog, while v models the search strategy, i.e. the sequential use of the clauses in a program. The semantics we propose for Prolog enjoys the nice properties of the declarative semantics of logic programming (existence of the least Herbrand model, equivalence of the model-theoretic and operational semantics). It is worth mentioning that our semantics is truly logical for propositional Prolog, whereas it looses part of its logical flavour when moving to the non-propositional case. This is due to the evaluation of existentially quantified goals which is based on a suitable ordering on ground instances of goals, which is obtained by exploiting the fixpoint approach of [9]. However, the truth value of an existentially quantified goal is still obtained logically as the sequential disjunction of its ground instances, according to the ordering just mentioned. For the sake of clarity, we first explore our approach
for propositional
Prolog, and
then we extend it to full, pure Prolog.
2. Preliminaries In this section, we will provide the basic notions
of multiple-valued
logic and logic
programming. 2.1. Multiple valued logics There are different ways to present multiple-valued logics (see, e.g. [ 13,23,21]): in this paper we basically follow the approach of [21] based on valuation systems. We consider a predicate language ?Z = (9, F, V, Co,Z?), where 9 is a set of predicate symbols, F is a set of function symbols, V is a set of variables, 0 is a set of operators (connectives), and L!Jis a set of quantifiers. With each function symbol f E 9, predicate symbol p E 9, and operator o in 0 is associated a unique natural number called its arity. We assume that the language contains at least one constant symbol (constant symbols are function symbols of arity 0). The ground term algebra over 9 is denoted by Tm(F). The non-ground term algebra over F and V is denoted by Tm(F,f).
R. Barbuti et al. IScience of Computer Programming 32 (1998)
The set of atoms constructed from predicate is denoted Am(Y, F, V) or Am for short.
symbols
A formula is (i) an atom, or (ii) o($l,.
145-176
147
in $27’and terms from Tm(P, Y”)
. .,c#+,), where each 4i is a formula
has arity n, or (iii) q x.4, where q is a quantifier
in 2, x is a variable
and o
in V and 4 is
a formula. Given a formula
4, the notion of free and bound occurrence
of a variable
is defined
as usual. We denote by b[x:= t] the formula obtained from C$ by replacing the free occurrences of the variable x by the term t. As usual, bound variables in 4 may be renamed in order to avoid conflict with the variables in t. A valuation system V for a predicate language 2? = (P,F, V, 0,s) is a tuple (Y, 9,92,C!2) where - Y is the set of truth values, with at least two elements; - 9 is the set of designated truth values, a non-empty proper subset of Y; - W is a set of functions. Each function r, E 9 corresponds to one operator in 0, and it is such that r0’. F&a --+ F ( w h ere n, is the arity of the operator 0). We say that r, interprets 0; - Y is a set of functions from g(r) to Y. Each function gq corresponds to one quantifier q and it maps possibly infinite subsets of .Y onto an element of 5. A basic assignment a relative to a valuation system (~,~,B,~) for a predicate language 2 = (P’, 9, -Y; 0,&L?)is a pair LX = (p, I), where I is a non-empty set of individuals (also called universe of discourse) and p is a mapping such that - p(t) E I for each t E Tm(P, V); - p(p) : I” + F for each predicate p E P with arity n. Each basic assignment a induces an interpretation (or valuation) v, of a sentence in the language, - &(P(h,...,
inductively
defined as follows:
t,)) = p(p)(p(t,),
. . . ,p(t,)),
where
p E9
with arity n, and tl,. . . , tn E
Tm( 9, V); - v,(o(41,..., prets 0; -
G4
x.
4)
&))=ro(ua(41),. = gq({ud+[x
:=
. . ,v,($,)),
where
0 E 0 with arity n, and r, inter-
tl>It E WW)).
An interpretation u, is a model for a sentence 4 iff v,(4) E 9. Given two formulae c$,# we say that 4’ is a logical consequence of 4, denoted by 4 t= I#+, iff u,(#) E 9, for all models v, of 4. As an example, consider the language 9 = (YC, 9$, K, UC’,, S?!,) of classical first-order logic, where CC& = {A, V, 7, _L} and 2 = {V, 3). In the corresponding valuation system, are the classical interpretations of the conwe have Y={Cf}, g={t>, _LfV,f7 nectives A,V and 7, while f~ is a constant function which returns the truth value f. Finally, the functions gv and g3 are defined as follows: f sv(X) =
t
iffEX, otherwise,
B(X)
=
t
if tEX,
f
otherwise.
It is worth noting that this definition of gv (resp. gg) corresponds to the usual interpretation of the universal (resp. existential) quantifier as a possibly infinite conjunction
148
R Barbuti et al. IScience of Computer Programming 32 (1998)
(resp. disjunction).
145-176
Later we will use an ordered set for X in the above for sequential
quantification. 2.2. Logic programming We assume that the reader is familiar with logic programming,
and so we recall only
some basic definitions. For the concepts which are not reported here, the reader can refer to [18,3]. Logic programming is based on a predicate language 9’1~= (9$, Fb, V& Log,91, ), where 61, = {A, t, false} and & = {Q, 3). A definite clause is a formula of the form A c B where A is an atom and B is a conjunction B, A . . . A B, of atoms. A is called the head of the clause, and B is called the body of the clause. All the variables occurring in a clause are implicitly universally quantified. Hence, A + B stands for Qxt . ..Vxk. AtB, where xl,... ,Xk are all the variables (possibly none) occurring in the clause. A logic program is a (finite) set of definite empty head, denoted by +-z. The declarative semantics of logic programs and Herbrand interpretations. The interpretation one, namely A is interpreted as conjunction, +value f. The ground term algebra Tm(F) is referred
clauses P. A goal is a clause with an is given by classical two-valued logic of the operators in Olp is the classical as consequence and false as the truth to as the Herbrand
Universe. A Her-
brand interpretation is a valuation v, corresponding to a basic assignment (p, Trn(9)), in which the domain of individuals is the Herbrand Universe. The standard semantics of a logic program P based on a first-order
language
JZ is given by its least Herbrand
model. On the other hand, the operational semantics of logic programs is given in terms of SLD-resolution and the SLD-refutation procedure. Given a logic program P and a goal G, an SLD-tree for P and G is a tree satisfying the following: (i) each node of the tree is a (possible empty) goal, and (ii) the root node is G, and (iii) let B, (m > 1) be a node in the tree and B, be the selected atom for this Bt,&,...,&,..., node via a computation rule. Then, for each clause A + B such that mgu(A, B,) = 29# fail, the node has child (Bl,Bl,. . .,B,. . ., B,,,)I~, where mgu(A,B) denotes the most general unifier of A and B, which is fail if A and B do not unify. A search rule is a strategy for searching SLD-trees. An SLD-refutation procedure is specified by a computation rule together with a search rule. Success branches in an SLD-tree are the ones ending in the empty goal, while failure branches are the ones ending in a non-empty node without children. The operational semantics of Prolog corresponds to a particular way of constructing and visiting SLD-trees, which can be formalized as follows. A Prolog-tree is an SLDtree such that _ the computation rule is the left-to-right one (i.e., s= 1); - the children of a non-leaf node are obtained (from left to right) by considering the clauses in the textual order they appear in the program.
R. Barbuti et al. IScience of’ Computer Programming 32 (1998)
Finally,
the operational
semantics
success nodes encountered
of Prolog corresponds
in the left-to-right
145-176
149
to the list of substitutions
at
depth-first traversal of the Prolog-tree
for
a given goal.
3. Related works The idea of giving the semantics to logic programs by means of a multiple-valued logic is not new. A third value, the undefined value u, was introduced to model infinite computations. Examples of semantic definitions based on Kleene’s three-valued logic are the ones in [14, 17,201. These logical semantics are defined for pure logic programming, i.e., they model an operational behaviour based on SLD-trees built by a fair computation rule and visited by a breadth-first strategy. Recall that a fiir computation rule is such that any (instance of) atom occurring in a goal is eventually selected. In [2], a semantics for Prolog in a logical style is presented. This logical semantics is proved correct with respect to an operational one, which essentially mimics the left-to-right depth-first visit of a Prolog tree. The left-to-right depth-first search rule is taken into account by using the completion of programs and by giving a sequential interpretation to the disjunction in the right part of each predicate-completed definition. Of course, in this case, the order of the arguments of the disjunction is essential. This order must respect exactly the order of clauses. Moreover, the left-to-right computation rule is modelled by the sequential interpretation of the conjunction. To define the semantics with respect to the completion of a program, [2] generalizes the standard notion of goal, in the style of [ 191. We refer to a similar notion of goal, which reflects the type of formulae which arise when Clark’s completion is taken into account. To simplify the notation, in the following we denote simply by x and t a sequence of variables and terms, respectively. Moreover, we denote simply by s = t a conjunction of equations si = tl A . . . A Sk= tk where s;, t, are terms. So, for instance, x = t may denote a conjunction Definition
1.
of equations
A goal is an element
of the form xi = tl A . . . AX, = t,,.
of the syntactic
category
Dis j-Goal
follows: : : = Ex-Goal V Dis j -Goal 1Ex-Goal Ex-Goal ::=Conj_Goal]h. s=tAConj_Goal Con j -Goal : :=AtomIAtomAConj_Goal Atom : :=fuhe 1true 1p(t) Dis j -Goal
In the rest of the paper we will refer to this definition As an example, consider the Prolog program
P(S)+ r(s’).
of goal.
defined as
R. Barbuti et al. IScience of‘ Computer Programming 32 (1998) 145-l 76
150
where t, t’,s, s’ are sequences 3y.(x = t A q(t’))
p(x) -
where y, z are the sequences The semantics
of terms. Its completion v 3z.(x = s A Y(d)),
of variables
of Prolog is obtained
that, when evaluating
is given by
the definition
occurring
by giving
in the two clauses, respectively. an order to the evaluation
of p(n), the part corresponding
of V, so
to the first clause is
examined first, and the second part is evaluated only if the truth result of the first one is f. The same evaluation order is given to A. The evaluation order of V models the sequential use of the clauses, while the one of A models the left-to-right computation rule. More formally, [2] gives the semantics in terms of the three truth values, {t, f, II} 2 with the following interpretation of V and A. The function mapping ground formulae onto truth values: v(C)
if v(B)=f, u(BAC)=
v(BVC)= v(B)
otherwise,
v is a valuation function
v(C)
if u(B)=t,
v(B)
otherwise.
Unfortunately, this interpretation of the connectives does not allow a complete logical specification of Prolog semantics. Consider the following propositional Prolog program (in completed form): p”qvloop.
loop ++ loop.
q tf
true.
r i-kfalse.
where the computation of the predicate loop is infinite. behaviour is modelled by assigning the truth value u to Consider the two goals p and r. It is easy to see function v and the predicate definitions, the first one
In the three-valued
logic this
loop.
that, by using the valuation has truth value t, while the
second one has value f. Consider now the goal p A r which is equivalent, by using the definition of p, to (qV loop) Ar. By using the values of q, loop and r and the valuation function u we obtain the result f, but this is not the result we get by a Prolog interpreter.
In fact, due to the backtracking,
its truth value should be u. If we expand the definition
the goal p A r would run indefinitely,
of p and we apply the distributivity
thus
of V on A we obtain,
from p A Y, the goal (q A r) V (loop A Y), which has the right value u. Intuitively, the problem comes from the fact that the valuation function v cannot model backtracking on different alternatives in a predicate definition. It works well only on goals in which the alternatives are “compiled”, by applying distributivity, in a disjunction. For this reason, [2] gives semantics to Prolog programs in two steps. In the first one a goal is transformed into an O-formula, that is a formula in which each disjunction is an immediate subformula of either a negation or other disjunctions. 2 Actually, one more value, n, is used to model floundering this paper we do not take negation into account.
of negation.
We do not consider
it, since in
R. Barbuti et al. IScience of Computer Programming 32 (1998)
Then, to these formulae, above example, Although the approach
the valuation
function
a step towards the definition
is not completely
it is not compositional
satisfactory.
on all the possible
151
is applied to get the truth value. In the
(q A r) v (loop A r) is an O-formula,
[2] represents
145-176
while (q V loop) A r is not. of a logical semantics
of Prolog,
It is not a truly logical semantics
because
goals. In the previous
the true
example,
value of pA r is not given simply by the and of the values of p and r. To get the correct value we have to apply a transformation, which has the sound of making some computation steps, to get a formula in the O-form. In the following section, we will present a four-valued logic which can be used to give a compositional truly logical semantics of Prolog. The fourth truth value, which is denoted by t,, models the computational behaviour solution, but whose computation is infinite.
of a goal which has at least one
A first intuition on the use of this fourth truth value can be found in [20], in which a value corresponding to t, was used to model a goal which results in t under some evaluation strategies and u in others. However, this is not suitable for the semantics of Prolog, because a solution for a goal is given only if the success branch, corresponding to this solution,
has no infinite branches
4. A logical semantics for propositional
on the left.
Prolog
Our aim is to give a compositional logical semantics to Prolog by using a fourvalued logic. For the sake of clarity, we first define the semantics of propositional Prolog. In the next section this semantics is extended to Ml Prolog.
4.1. Propositional
multiple-valued
logics
Throughout this section we refer to a propositional language. Propositional languages can be viewed as special cases of predicate languages, where predicate symbols have all arity 0, and neither variables, nor function symbols nor quantifiers are considered. Hence, a propositional language 3 is simply a pair (9,8), where 9 is a collection of predicate symbols and 8 is a set of connectives. The set of sentences of a propositional language 9’ is the smallest set containing 9 and closed under the operators 0”. The notion of valuation system for a propositional language is a simplified version of the one given in Section 2.1. Here, a valuation system V is simply a triple (Y, 9,9$) where - Y is the set of truth values, with at least two elements; - LB is the set of designated truth values, a non-empty proper subset of 5; - W is a set of functions. Each function r, f 93 corresponds to one operator in 0, and it is such that r0 : P’u + F ( w h ere n, is the arity of the operator 0). Finally, a basic assignment p relative to a valuation system (Y, 9,B) for a propositional language (9,U) is simply a mapping p : 9 + F. Each basic assignment induces
152
R. Barbuti
et al. IScience
of Computer
Programming
an interpretation (or valuation) up of a sentence
32 (1998)
in the language,
145-176
inductively
defined
by - vp( P) = P(P), for P E 9’. -
w h ere
v~(o(~I,...,~~))=Y,(~~(~I),...,~~(~~)),
n is the arity of o and r, inter-
prets 0. 4.2. Propositional logic programming A propositional
logic program is a collection
of definite clauses from a propositional
language _!??I~ = (9$,, Co,), where 91, is a set of propositional symbols and 0b = {A, c, false} (see Section 2.2). Our semantics is based on the notion of completion of a logic program which is similar to Clark’s completion [ll]. The latter was originally introduced by Clark in order to provide a declarative semantics to negation as finite failure. However, (variants of) Clark’s completion have been adopted to capture the logical meaning of Prolog, and this is the case also for our approach. Let us briefly recall the definition of Clark’s completion for propositional logic programs. Given a propositional language _!Z’bas before, let _Y~Omp = (9Jccomp, Ocomp) be the language where .!??cOmp = 9$ and O,,,p = {V, A, H, false, true}. Definition
Let P be a logic program
2.
C,, Zp+Bi
on the language
_5Zb and let
Odi
be the sequence of m clauses of P with head p, where each Bi is a (possibly empty) conjunction of atoms. The completed dejnition of p is the following formula in the language Ycomp: p-E,
v ..’ VE,,
where Ei coincides with Bi if Bi is a non-empty conjunction, and Ei is true otherwise. If an atom p E 9$, never occurs in the head of a clause in P, its completed definition is the formula p-false in the language 3’~0,,,p. The completion of a propositional Prolog program P, denoted by camp(P) is the collection of the completed definitions of the predicates For instance,
in p E YCcomp. the completion
of the logic program
p+qAr 4+ on a language P+-+qAr q H true r H false.
such that 9rp = {p, q,r}
is
R. Barbuti et al. IScience of Computer Programming 32 (1998)
145-176
153
4.3. A four-valued logic In this section we introduce
the valuation
we will use to provide propositional As mentioned
in the previous
system _y^4defining
the four-valued
logic
Prolog with a logical semantics.
section,
the idea is to extend the usual three-valued
logic by a fourth truth value, t,, which is intended to model an infinite Prolog tree which has at least a successful branch to the left of the first infinite one. Moreover, the connectives A and V are interpreted in a sequential manner, in order to reflect the operational behaviour of Prolog. Since the semantics of a Prolog program P is given in terms of (a variant of) its Clark’s completion, we directly define our valuation system with respect to the language
_Y~OMp = (9&,,p, Ocomp) introduced
Definition
system Y&= (&,9~,9&)
3.
The valuation
is defined as follows:
- ~~~={f,u,t,,t}; - 94 = {t}; _ The O-at-y functions jjblse and f&,, are defined as fibl.,r = f and f,,,fv are defined according to the following truth tables:
functions
f,+ is defined in the usual way:
and the function
Let us explain
ftrur = t, the
fvk Y)
fA(X,Y>
f-(x, v>=
in Section 4.2.
t
ifx=y,
f
otherwise.
intuitively
the above definitions.
computation corresponding to a conjunction a finite success, if it is the first argument
The interpretation
of A mimics the
of goals. Since t is intended to model of a sequential conjunction the result is
equivalent to the second argument. An argument equal to t, models a computation in which there is at least a success and then it is infinite; if it is the first argument of a sequential conjunction, the resulting computation is still infinite, but the existence of a success, in the whole computation, depends on the value of the second argument. Finally, a value f or u for the first argument is the result of the whole computation. On the other hand, the interpretation of V mimics the result of exploring different alternatives in a computation of a Prolog goal. The first argument equal to t models the fact that we have got a finite success in the computation of the first alternative; of course, if the second argument corresponds to an infinite computation the result must reflect it. If the first argument is t, we have at least a success and an infinite
154
R. Barbuti
et al. IScience
computation
independently
when the first alternative whole computation alternative
of Computer
Programming
from the behaviour
of the second
has a finite computation
is the one of the second
has an infinite
computation
the second one. Finally, the H connective
without
Obviously,
the result of the when the first
we cannot pass to examine way also in our four-valued
logic, that is it has t if and only if the two arguments Otherwise
alternative.
and, finally,
successes,
in the expected
145-I 76
successes,
alternative,
without
is defined
32 (1998)
have the same truth value.
its value is f.
4.4. Semantics of propositional Prolog The semantics
of a propositional
Prolog program P is given in terms of its sequen-
tial completion, denoted by s_comp(P), which is similar to Clark’s completion (see Definition 2). The only difference is that, when constructing the sequential completion of a program P, the textual order in which atoms occur in the body of a clause as well as the textual order in which clauses defining a predicate p occur in P determine exactly the syntactic form of the completed definition of p. Due to its similarity with the definition of Clark’s completion, we omit the definition of s_comp(P) and we illustrate by means of a simple example. Consider the following clauses defining a predicate p:
and assume that they appear in this order within a Prolog program P. Then, the completed definition of p in s_comp(P) is exactly the formula
On the other hand, two-valued
since Clark’s
or three-valued
completion
is usually
interpreted
in a classical
logic, the formula
p++sV(rAq) could equally be taken as the completed definition of p in Clark’s completion. It is important to notice that, for propositional Prolog, the notion of goal as in Definition 1 collapses down to the following definition. Definition 4. A goal is an element
of the syntactic
category
follows: Disj-Goal : : = Conj_GoalVDisj_Goal Conj-Goal : : = Atom 1Atom A Conj_Goal Atom :: = false 1true 1PropLetter Prop-Letter :: = plq1 . . .
1Conj-Goal
Dis j-Goal
defined
as
R. Barbuti et al. IScience of Computer Programming 32 (1998)
The first important
observation
is that in our four-valued
145-176
155
logic we can logically
model backtracking, as stated by the following formula, in a valuation system V= (Y-, 9,9),
proposition. Recall that a model of a is an interpretation up, where p is a
basic assignment,
a truth value in 9 (the truth value t
which assigns to the formula
in the case of our valuation
system ~74)).
Proposition 5. Given three goals, G, G’ and G”, in propositional Prolog, every interpretation is a model of the formula ((G
v
G’) A G")++((G
Stated otherwise,
the formulae
A G")
v
(G’ A G")).
(G V G’) A G” and (G A G”) V (G’ A G”) are equiv-
alent, i.e. they have the same truth value in every interpretation. Let us consider the sequential completion of the example of Section 3. pHqVloOp loop H loop q H true r *false Assigning the truth value u to loop, the value of p is now t, and, hence, the value of the goal p A r is u. It is important to remark that the classical properties of V and A are not preserved in our valuation system. For example, the formulae G A (G’ V G”) and (G A G’) V (G A G”) are not equivalent. 3 However, we are interested in maintaining the properties which model the evolution of Prolog computations. In this respect, notice that (G A G’) V (G A G”) does not model the evolution of the computation of the goal G A (G’ V G” ). Now, we can define the model-theoretic semantics of propositional Prolog as the least model of s_comp(P) with respect to a suitable ordering between basic assignments. This ordering is the pointwise ordering obtained from an ordering between the four truth values based on the following Hasse diagram: t /
‘\ 2 U
Following
[15], we will refer to this ordering
as the knowledge ordering, denoted
by 6,.
3 Consider the values t,,, f and t for the goals G, G’ and G”, respectively. truth value tU, while (GA G’) V (G A G”) has value u.
The goal G A (G’ V G”) has
R. Barbuti et al. IScience of’ Computer Programming 32 (1998)
156
145-176
Definition 6. Given a propositional language 2 = (S, O), let p, p’ : 9 --+ 94 be two basic assignments relative to the valuation system Va. We say that p is less than or equal than p’, denoted by p <, p’, iff for all atoms pi 9 we have p(p) 6, p’(p). It is clear that the set of basic assignments
relative to 922 is a complete
partial order
with respect to 6,. Proposition 7. Let 9 be a propositional language, Vd be the corresponding valuation system, and & be the set of basic assignments relative to Vh. Then the poset (&, <,) is a complete partial order. Proof. Straightforward. The next proposition
Cl states the existence
of the least model of s_comp(P).
Proposition 8. Given a propositional Prolog program s_comp(P) has a least element with respect to <,.
P, the set of all models of
The existence of the least model is based on the definition of a suitable bottomup operator Yp associated with any program P, which is the analogue of the Fitting operator @p for the three-valued case. Definition 9. Let P be a propositional Prolog program and 9 = (9, 0) be the language of its sequential completion. The operator Yjj mapping basic assignments in d to basic assignments in JZ! is defined as follows. For each predicate symbol pi 9 of
= v,(4),
where p * 4 is the sequential valuation
induced
completed
definition
of p in s_comp(P), and up is the
by p.
Lemma 10. Let P be a propositional Prolog program, 3 = (9,O) be the language of its sequentiai completion, and Fp be the operator associated with P. Then the following facts hold (i) & is continuous with respect to
R. Barbuti et al. IScience of Computer Programming 32 (1998)
Hence, up is a model of s_comp(P). Then
145-176
157
On the other hand, let up be a model of s_comp(P).
P(P) = Up(P)= Q(4) = so)(P)? hence, p is a fixpoint of 3~.
0
As a consequence, we have that the interpretation umin induced by the least fixpoint of the 9jj operator is the least model of s_comp(P). Finally, behaviour
we show that the least model of s_comp(P) reflects indeed the operational of Prolog.
Theorem 11. Let P be a propositional Prolog program, s_comp(P) and G a goal:
vmi” the least model of
v,in(G) success v,i”(G) success
= t ifs the Prolog tree of P and G is jinite, and it contains at least one branch = f ifs the Prolog tree of P and G is jinite, and it does not contain any branch
v,in(G) success U,in(G) branch
= t, lr the Prolog tree of P and G is infinite, and it contains at least a branch on the left of the first infinite branch = u ifs the Prolog tree of P and G is infinite, and it contains no success on the left of the first injinite branch.
Proof. See Appendix
A.
0
5. Pure Prolog In this section we extend the logical semantics to pure Prolog. The sequential completion of a Prolog program is obtained by extending the one of Section 4.4 to programs on a predicate
language
2 = (9’,9,
V, 0,s)
(which we consider
fixed from now on-
wards), where 6 is the set of operators {V, A, +-+, false, true} and 22= (3). The sequential completion of a Prolog program is again very similar to Clark’s completion. Each clause p(t1,t2,
. . ..h)
is transformed P(XI,X2
+
3
into
,...,Xk)ts3JJ.X,
=t1
Ax2=t2
A...AXk=tkAB,
where xi , . . . ,xk are new variables and y is the sequence of variables occurring in the original clause. Then the process proceeds as in the sequential completion of propositional programs. Recall that both the order in which atoms appear in clause bodies and the order of clauses is relevant when constructing the sequential completion. In addition
R. Barbuti et al. IScience
158
to the completed
definitions
of Computer
of predicates,
the axioms of Clark’s equality
To give a logical semantics
145-I 76
completion
of goal as given in Definition
to Prolog we have to give a meaning
of a formula
is equipped
with
1. to 3. It is worth
3x.G, cannot be simply given classically
of G[x : = t] under all possible
disjunction
the sequential
32 (1998)
theory [ 1 I] which are used to interpret the symbol = as
the syntactic identity. Recall that we refer to the definition noting that the meaning
Programming
assignments
for n. Actually,
by the
the disjunction
must be interpreted as a form of sequential disjunction, and the assignments have to be considered following a special order. To have an intuition of this let us give an example. Example 12. Consider the following F={a,b} and S=(p). p(b)
+
Its sequential
p(b).
p(a).
completion
p(x)++(x=b
Prolog program on a first-order language in which
is given by
A p(b))
V
(x=a
A true).
Obviously, the Prolog goal p(x) will loop, and this should be reflected by the meaning of the goal &p(x). The meaning of !kp(x) cannot be simply given by the disjunction p(x)[x := a] V p(x)[x := b], because this disjunction has truth value t in the least model of the program. Note that the program could be viewed as a propositional one, thus, in its least model the atom p(b) would have value u, while p(a) would have value t. On the other hand, the meaning of 3x.p(x) cannot be given either by the sequential disjunction p(x)[x := a] V p(x)[x := b], b ecause in this case its truth value would be t,. The value for the goal, corresponding to the operational behaviour of Prolog, is given by the sequential disjunction p(x)[x := b] V p(x)[x := a] in which the assignments for x are taken in the “right” order. This order is essentially given by the order of clauses in the Prolog program. In the following, we define the order in which the assignments for the variables must be taken to model the computational behaviour of Prolog. In a previous version of this work [8], we defined such an order by means of a relation + between ground substitutions, defined inductively on the structure of the program. However, the same notion can be given an easier and more intuitive characterization, by exploiting the notion of sequences used in [9] to give a bottom-up semantics for Prolog. 5.1. The bottom-up
semantics
of Bossi et al.
In [9], Bossi et al. present a fixpoint reconstruction of the semantics of Prolog which captures both the left-to-right selection rule and the depth-first search strategy
R. Barbuti et al. IScience
underlying
the Prolog evaluation
of Computer
mechanism.
Programming
32 (1998)
We summarize
145-176
159
the most relevant
defini-
tions and results below, and we refer to [9] for further details. Roughly speaking, the idea underlying the approach of [9] is to make use of an extended
notion
of interpretations,
more complex
syntactic
of sequences,
composed
atoms. A sequence
rather than Herbrand
objects than ordinary by either ordinary
in an interpretation
interpretations,
which include
ground atoms. An interpretation (possibly
non-ground)
is an abstraction
is a set
atoms or divergent
of the ordered
set of partial
answers obtained by a depth-first and left-to-right traversal of a partial Prolog tree for a query in its most general form (i.e. p(x) where x is a tuple of variables). A divergent atom in a sequence represents the fact that the corresponding branch of the partial Prolog tree can be further expanded. Given a set of symbols S let us denote by S’ the set of finite sequences of symbols in S. The concatenation of two sequences si and s2 is denoted by si :: ~2, whereas il stands for the empty sequence. Definition
13. Let &?= Am(.Y, 9, ‘V). The extended
base 6!8E is defined as
&={A^JAE&~}U@. The newly introduced set Y(Bi)
atoms of the form A^ are referred to as divergent
is a complete lattice under the usual inclusion
atoms. The
ordering, with top and bottom
elements defined, respectively, as Bi and 0. Sequences in 22; can be used to represent the frontiers of any finite cut of a Prolog tree, and such sets can be constructed bottom-up by a suitable operator. Recall that a finite cut at depth k of a tree is the tree obtained by cutting at depth k each branch longer than k. As mentioned above, the presence of a divergent atom in a sequence represents the fact that the sequence corresponds to the frontier of a cut of the Prolog tree containing nodes which can be further expanded, i.e. they are neither failure nor success nodes. As an example,
consider
the following
Prolog program:
P(Q). P(b)
+
P(b).
The sequence P(a)
1: P%)
represents any cut at level k 2 1 of the Prolog tree corresponding to the goal p(x). In fact, the leftmost branch of this tree is a success branch with answer substitution x = a, whereas the rightmost branch is an infinite one, which can be expanded indefinitely by using the second clause. In [9], an interpretation /.L is a set of sequences of elements in go. Given an interpretation ~1, a sequence SET and an atom A, we define Q(S), the projection of A on S, as the subsequence of S which consists of the elements which unify with A. If
160
R. Barbuti
A is in most general
et al. IScience
of Computer
form, say A = p(x),
z~(~)(S). In the next definition,
Programming
32 (1998)
145-l 76
then TC~(S) will be used as a shorthand
clauses are assumed to be standardized
they are used, in order to avoid variables
for
apart each time
clashes.
14. Let c be a clause. & : Bz -+ $9; is defined as follows:
Definition
- If c is the unit clause A, then ~A(S)=A. - If c=(A c B,D) and n~(S)=dl :: ... ::dk. +c(s)=~l
:: . . . :: tlk where c(i =
and Oi=mgu(B,B’)
Then
Zi
if di = @,
&A+D)o,(S)
if di =B’
for i~[l..k].
Definition 15. Let P be a program and cl,. . . , c,, the clauses of P. ~$p: ~$32+ 93; is defined as 4p(S) = &(S) :: . . . :: &,,(S). Definition 16. Let P be a program. are the predicate symbols in P.
Then P# = ~3,
17. The operator @p : P(@i)
Definition
@P(fi) = {MS)
-+ 9(Bi)
) :: . . . :: psn),
where P,, . . . P,,
is defined in terms of +p as follows:
I SEPL) u UT
The least fixpoint of the operator @p, 9’jj~~(P), consists of the ordered set of the sequences over BE which represent all the possible partial computations (w.r.t. the depth-first left-to-right derivation) originating from the most general atoms in the program. 18. yl,,(P)
Definition Consider the fixpoint. @(PO)= SO,...,
&
= @#,?I).
the interpretations
po,. . . , pk,. . . resulting
At the first step, ~0 = @+(a) = {P”}.
from the iterative computation Similarly,
at the second step, p1 =
{P”, $p(P”)}. In general, at step k, pk will consist of k + 1 sequences and these sequences can be ordered so as to ensure that, for any j E [ l..k],
~P(S~__I) = Sj. The fixpoint yb,(P) can then be viewed as the limit interpretation consisting of the (finite or infinite) ordered set of sequences S,-,,. . . , Sk,. . . Consider the following example taken from [9]. Given the Prolog program P, p(b). P(X) + 4x) P(C). 40)
of
+ P(U)
r(b)+ db)
p,
161
R. Barbuti et al. IScience of Computer Programming 32 (1998) 145-176
the least fixpoint lowing
y?DFL(P) is computed
at a finite iteration
and it consists
of the fol-
set of ordered sequences:
So=p~)::r~)::~~) Sr ==p(b)::pT)::p(c)::rF)::r+Pj S* = p(b) :: p&P) :: p$P) :: p(c) :: YG) S, = p(b) :: p&G) :: p(c) :: T$$. Notice that divergent
atoms can evolve to ordinary
atoms or they can disappear
later sequences, representing success or failure branches, Consider now the program
in
respectively.
P(O). P(G)>
+- P(X).
In this case the least fixpoint quences
requires
infinitely
many iterations
and it contains
se-
of the form
Sk = p(0) :: p@(O)) :: . . . :: p(sk_l(O)) Finally, P(W)
consider
:: p(?@)).
the program
+- P(X)
PW Also in this case the least fixpoint requires infinitely sequences Sk =
many iterations
and it contains
of the form
f&b)) ::p(sk-‘(o)) ::
’ ’ .
::
p(s(o)) :: p(0).
Among others, an important result which is a direct reported in [9] and which we will use is the following.
consequence
of the results
Proposition 19 ([9]). Let 3y.G be a closed goal. Then the following facts hold: (i) the Prolog tree of G is finite and it contains at least one success branch iff for some k, Sk is non-empty and does not contain divergent atoms (ii) the Prolog tree of G is jinite and it contains no success branch ifs for some k, .!$ = 2. (iii) the Prolog tree of G is injinite @for each k, Sk is not empty and does contain a divergent atom.
of Computer Programming 32 (1998) 145-I 76
R. Barbuti et al. IScience
162
5.2. A logical semantics for pure Prolog The problem junction
with an existentially
of all possible
corresponding
ground
quantified
instances
to the order of evaluation
sented by the sequences
in Y’,(P).
goal of the form 3x.G is that the dis-
G[x:=g]
must be taken in the right order
of Prolog. The idea is that this order is repre-
The projection
of a sequence Sk in yb,,(P)
over
a goal p(x) can be seen as representing the frontier of the cut at level k of the Prolog tree for p(x), which induces an order between ground instances. For instance, given the goal Ix.p(x), the order induced by the sequence p(a):: p(b):: p(c) imposes that [x := a] must precede [x := b] which itself must precede [x := c]. Hence, in the disjunction corresponding to the evaluation of Zlx.p(x) with respect to a basic assignment p, p(p(x)[x:= a]) must occur before p(p(x)[x:=b]) and the latter must occur before p(p(x)k= cl). Consider now the sequence p(a) :: pF) :: p(c), in which the divergent atom p(b) represents a potentially infinite path in the Prolog tree. This is reflected, in the evaluation of !lx.p(x), by taking the disjunction p(p(x)[x := a]) V u V . . . The actual truth value of an existential goal should be computed as a function of the truth value of the same goal with respect to any sequence in &L(P). Since a set 9 of truth values may not have a maximal value with respect to the B, ordering, the maxf(Y) function which in such cases ignores the f value.
we define
Definition 20. Let 3 be a set of truth values in {t,f, tu,u}. Then maxf(Y)
is defined
as follows: max,f(Y)
if 9 n {t, tU) # 0,
maxK(9\{f})
=
otherwise,
i max,(y) where max,
is the maximal
value with respect to the GK ordering.
Given a sequence and an assignment, we define the sequential disjunction of the sequence, which is based, according to the previous informal discussion, on the fact that the sequence dictates in which sequence should be evaluated.
Definition 21. sequential
Given
disjunction
a sequence corresponding
order the ground
instances
represented
by the
S = dl :: . . . :: d, in 63; and an assignment
p, the
to S in p, denoted by v(S) P
is defined by
if S = 2,
;(,,)v...“$(, P
P n
)
otherwise,
where Zp(d) =
if d is a divergent
U
max&(p(t)@)
where max, is the maximal
IY ground}
atom,
if d = p(t)&
value with respect to the truth ordering
f Gru Gt t, Gt t.
R. Barbuti et al. IScience of Computer Programming 32 (1998)
145-l 76
163
Notice that the above definition reflects the intuition that any divergent atom in a sequence “represents” the undefined truth value, whereas a non-divergent atom represents the logical disjunction of all possible truth values of its ground instances in the assignment
p.
In the next definition value to existentially
we exploit the notion of sequential quantified
disjunction
goals. Notice that in the definition
to assign a truth the projection
of
a sequence is defined over a possibly non-atomic conjunctive goal. The trivial extension of the projection operator n over a conjunctive goal G is still denoted by ?to and it is given in Appendix A (see Definition 32). Since &FL(P), in general, can be an infinite set of sequences,
we must consider the
sequential disjunction of the goal with respect to any such sequence maximal non-false value obtained in this way.
and then take the
Definition 22. Given a closed goal G, and an assignment p, the truth value of G with respect to the interpretation t+,, denoted by z+(G), is obtained as follows: - f if G is false, - t if G is true, - the truth value p(G) if G is (t = t’) or G is p(t), - the value of vp(GO) V v,(Gi ) if G is the goal Ga V Gi , - the value of vp(Ga) A u,JGi ) if G is the goal Gc A Gi, - maxf{vai
( CLi= zG’(&),
for each S; E&FL(P)},
if G is the goal 3x.G’.
P
Now we can define the notion
of model for pure Prolog.
Definition 23. Given a Prolog program P and an assignment induced by p is a model of s_comp(P) iff for every definition
p, the interpretation
v,,
is the same truth value of up(G[x :=t]),
for
P(X) t-t G in s_comp(P) we have that p(p(x)[x:=t]) each sequence t of terms in Tm(9).
According to this notion of model, the results for propositional Prolog can be extended to pure Prolog. In particular, we can define a new Yp operator. Definition 24. Let P be a Prolog program and Y = (9,9, V, 0, { 3)) be the language of its sequential completion. The operator Yp mapping basic assignments to basic assignments is defined as follows. For each predicate symbol p E 9’ WP)(P(t))
= $(4b
:=
tl),
where p(x) * C#Iis the sequential completed definition quence of terms in Tm(9) and up is the interpretation
of p in s_comp(P), t is a seinduced by p.
164
R. Barbuti et al. IScience of Computer Proyramminy 32 (1998)
145-I 76
25. Let P be a Prolog program.
Proposition
(i) An interpretation up is a model of s_comp(P) ifs p is a jixpoint of Fp. (ii) The Fp operator is continuous. Thus, we have that the interpretation
vmin induced
by the least fixpoint
of the J~P
operator is the least model of s_comp(P). Finally, as in the propositional case we show that the least model reflects indeed the operational behaviour of Prolog.
of s_comp(P)
Let P be a Prolog program, v,in the least model of s_comp(P) and G as in Dejinition 1, i.e. G = G’ if it does not
Theorem 26.
be a closed goal belonging to Ex-Goal contain variables, or G = 3x.G’.
v,in(G) = t iff the Prolog tree of P and G’ is finite, and it contains at least one success branch v,in(G) = f tjJ”the Prolog tree of P and G’ is jmite, and it does not contain any success branch v,in(G) = t, 13 the Prolog tree of P and G’ is injinite, and it contains at least a success branch on the left of the first injinite branch v,in(G) = u tff the Prolog tree of P and G’ is injinite, and it contains no success branch on the left of the first infinite branch. Proof.
See Appendix
A.
0
Let us now give two examples. Example 27. Consider p: p(s(x)) + P(X).
the Prolog program P(0).
Its sequential completion is given by p(x)+-+(3y.~=s(y)Ap(y))V(x=OAtrue). In the least model of the sequential completion all the atoms p(s”(O)), with n = 0,1,2 )...) have value t. Consider now the goal G = Zlx.p(x). The computation of Y~FL(P) requires infinitely of @,“(a). At step k, pk contains the k+ 1 sequences
many iterations for each j
Sj =
j = 0,
PT) { p(z))
:: p(sj-‘(0))
:: . . . :: p(0)
The truth value of the goal is then obtained maxf{u,
uVt ,...,
This corresponds well.
{SO,. . . , Sk}, where
j>O. as
uVtV...Vt}.
to the value II, which is the value of the initial
goal, Ix.p(x),
as
R Barbuti et al. IScience of Computer Programming 32 (I 998) 145-176
This models the operational
behaviour
165
of the Prolog goal p(x) with respect to the
given program, which has an infinite Prolog tree where all the success branches are inaccessible since an infinite number of nodes precedes them in the leftmost depth-first traversal. Example 28. Consider
the Prolog program
P(O). PW>) + P(X).
p:
Its sequential completion is given by p(x) H (x = 0 A true) V (3 y.x = s( y ) A p(y)). In the least model of the sequential completion all the atoms p(s”(O)), with n = 0,1,2 )...) have value t. Consider now the goal G = 3x.p(x). The computation of Y”FL(P) requires infinitely many iterations of G,“(s). At step k, pk contains the k sequences {SO,. . .,&}, where for each j j = 0,
PTI Sj
=
i
p(O)::...::p(s
j-'(O))::p(z))
The truth value of the goal is then obtained maxf{u,
tVu ,...,
j>O. as
tVtV...Vu}.
This corresponds to the value t,. This models the operational behaviour of the Prolog goal p(x) which has an infinite Prolog tree with infinitely many success branches on the left of an infinite one.
6. Conclusion We have shown how Prolog programming on a four-valued
can be given a logical semantics
logic. Besides the usual unde$ned
based
truth value, we have a fourth truth
value t, which models the computation of a goal which succeeds (at least once) and then loops. Future work will concentrate mainly on two issues. First of all, we plan to extend it to normal Prolog programs (i.e. Prolog programs with negation-as-failure), possibly adapting the approach of [2] where an extra truth value N is introduced to model j?oundering. Second, we plan to explore the possibility of further extending the approach to cope with other extra-logical features of Prolog.
Appendix A. Proofs The proofs of the main results are performed by relating the construction of the least model Umin with the sequences in &L(P). The least model U,in is the least fixpoint
166
R. Barbuti et al. IScience of’ Computer Programming 32 (1998)
FpU of the 5~ operator,
145-l 76
where
and I assigns u to every atom. We first give some useful definitions and technical lemmas and, for the sake of the reader, we give first the proofs for propositional Prolog and then the ones for pure Prolog.
Definition 29. Let S be a sequence in 93;, p(t),q(t’),r(s) be atoms, S’ = np(,)(S) = ar :: . . . a,, S” = 7q1’)(S) = b, :: . . . :: bk, where each ai (resp. bj) is either p(t)& (resp. 4(t’)‘)
or
PGQi
09.
d6Yj).
composer(,)(S’, s”) = Cl
:: . . . ::
c,,
where cr :: . . . :: c, is obtained from S’ and S” as follows: _ C; = &jZIj if Ui is J+j$i; - ci=b; :I... :: b:, where each b,! is I iff mgu(f9i,yj) =fad, bj =r(~)dj, Sj = mgu(Oi,yj) otherwise (as usual, mgu(8i,yj) denotes the most general solution of the union of the set of equations corresponding to the substitution 8i,yj).
Example 30. Assume S is a sequence such that S’ = z+)(S) a] and S”=n,(,,)(S)=r(x,y)[x:=a,y:=b]::r(x,y)[x:=b,y:=b].
compose,(,,(S’, S”) =
pT)[x:=c] ::p(x)[x
Lemma 31. Let c: p(t)+ql(tl),...,
= qB[x
:= c] :: q(x)[x := Then
:= a, y := b].
qk(tk) be a clause in P, and let S be a sequence.
For each i= 1,. . ., k let also ai be the sequence for qi(ti) given by zq,ct,)(S). Then
Proof. Obvious by definition
of compose and &(S).
0
As an example, consider the clause p(x) t q(x),r(x, y) and the sequence S of the previous example. The following definition is a slight extension of the projection operator rc over sequences in Y’FL(P) for arbitrary goals G.
R. Barbuti et al. IScience of Computer Programming 32 (1998)
Definition
32.
Let Sj be a sequence
nG(si
167
and G be a goal.
if G = p(t)
’ qQ)Vi 1 aZoO
in Y&L(P)
145-176
if G = GO V Gi
1:no,(&)
A
if G = 3x.t = t’ A G’ and t and t’ do not unify
nG/dSi)
if G = 3x.t = t’ A G’ and 0 = mgu(t, t’)
compose,,,G.,)(@l,
...compose,i_,(,_,)(ak-l,
>=
if G=ql(tl),...,qk(tk), predicate
mk)...)
where anso is a new
symbol, x are the free variables
in G,
and for each h = 1,. . . ,k, mh= TC~,,(~,,~($). We will make the following
abuse of notation.
Given a goal G = q(tl) A.
A q(tm)
and a sequence & in y?DFL(P) the sequence zo(&) ends up in a sequence of the form or an&(x), where x are d, ::... :: d,,, where each dj is an atom of the form anso the free variables of G (see Definition 32). In this case, it may not be clear what p(anso(x)y) stands for, y being a ground substitution for the variables x. However, we still use this notation with the proviso that dansG(xh)
Lemma 33.
= P(dtl
7)) A ’ ’ ’ A P(‘dtrnY)h
Let P be a logic program,
p(g)
be a ground atom and
CI :p(tl)tGI
cn :p(tn) +- G be the ordered set of clauses for p in P. Moreover, mj”=
A i %,0, (Sk >
for each j = 1,. . , n, let
if there is no mgu(g, tj), if ej = m&g,
where Sk is a sequence
tj 1,
in y)DFL(P). Then
n,(,)(Sk+l ) = a: :: ’ ’ :: p,“, where, for each j=l,...,n, . . . :: d,, where dI = pz)
/?;=A
if $=A,
if di is a divergent
Proof. The proof is straightforward
and b;=d{::
s.. ::dk
if $=d,::
atom, d: = p(g) otherwise.
by definition
of x and by definition
of &(sk).
13
The following proposition points out some useful properties of sequences in YDFL(P) which are obvious consequences of the definition of the operator @p in [9]. Proposition 34. Let P be a logic program, a goal G, the following facts hold:
and YDFL(P) = {So,Sl,.
. . ,&,. . .}. Given
R. Barb&
168
(i)
if
for some
et al. IScience of Computer Programming 32 (1998)
145-176
k, nc(Sk) = 2 then
for each k’ 2 k, xG(Sk ) = 2 (i.2) for each k’ -Ck, if zC(Sk’) # ,? then T&(&l) contains only divergent atoms. (ii) iffor some k, T@(Sk)=dl :: ... ::d, and each di is not a divergent atom, then (i.l)
(iii)
for each k’ak, xc(&) = xc(&) iffor some k, T@(Sk) contains a non divergent atom d, then any T’@(Sk’), with k’ 2 k contains the same atom d.
As we mentioned before, the proofs are based on relating the least fixpoint of the 4 operator to the sequences in ~‘DFL(P). The idea is that each Sk in 9&,(P) can be associated with an assignment pk, and that the limit assignment induced by these pk coincide with Jo-” P . Definition 35. Let Sk be a sequence Sk is defined as follows:
Pk(N))
in YD,vL(P). The assignment
f
if zp(f)(Sk) = I&,
t
if np(,j(Sk) # 1, and does not contain
n
if znp@)(Sk)= &je
=
( t,
pk associated
divergent
with
atoms,
:: S’,
if np(f)(Sk) = p(X)& :: . . . :: p(x)&
:: p&j6 :: s’.
We will see that, in the propositional case, each pk coincides with the qk, whereas in the non-propositional case the coincidence is obtained only at the limit assignment. A.1. Proofs for propositional Prolog In this section we will refer to a sequence
of the form po :: . . . :: pk such that each
pi is either p or ^p as a sequence for p. Obviously, rcP(S) is a sequence
Let S = go :: . . . :: q,, be a sequence
Definition 36. symbol. Then rename,(S) where pi=p
given an arbitrary
sequence
for q, and let p be a predicate
= po :: . . . :: pn,
if qi=q,
and pi=p
if qi=q.
Definition 37. Let S = po :: . . ::p,, be a sequence quence for q, and r be a predicate symbol. Then
for p, S’=qo::
... ::qk be a se-
compose,(S, S’) = dl :: . . . :: d, where d, =i: if pi = ij, and di = rename, The following
S,
for p.
lemma is the analogous,
if pi = p. for the propositional
case, of Lemma 33.
R. Barbuti
et al. IScience
of’ Computer
Programming
32 (1998)
169
145-176
Lemma 38. Let c : p + 41,. . . , qk be a clause for p, and let S be a sequence. each i= 1, . . . , k let also ai be the sequence for qr given by x+(S). Then
&-(S)=compose,(al, Proof. Straightforward Lemma 39.
...composeql_,(ak_l,
by definition
P
c(k)...).
0
... ::p,, he a sequence for
Let S=po::
quence for q, and r be a predicate (i) pCOi?lpOSe, ($S’) (ii)
of 4C.
(r) =8(P) renume,(S):: rmamr,qr)
symbol.
For
p, S’=qo::
... ::qk be a se-
Then
A pS’(9)1 = pS(p) ” pS’(q),
Proof. Straightforward by case analysis on the truth value of pco”posr~(xS’)(r) in 0 case (i) and on the truth value of pr~“Ume~(S)“r’n”m’~(S’)(r) in case (ii). Lemma 40. associated
Let P be a propositional
with the sequence Sk. Then, the assignments
Proof. We show by induction qk(p)
Prolog program,
and let pk be the assignment FPk and pk coincide for each k.
that for each k and for each predicate
symbol
p
= L’k(p).
Base step: Obvious. Inductive step: Let Cl
:
p +
qf A
. . . A 4;,
... crl 1P +qlA
‘.. Aq;: I?
be the ordered set of clauses defining For i= l,...,n, let
p in P. Moreover,
pi = compose,(af , . . compose,, ,,,_ ,(&Y Then, by definition np(&+,
for each qi., let c$ = rcq;(&).
4J-).
of @p and Lemma 38,
) = PI :: . ’
’
::
Pn.
By Lemma 39(i), we have that, for each i = 1,. . . ,n, pq p) = p”i (q{ ) A . . . A pai, (qi, ). Hence, by the inductive
hypothesis
(since p”:(q:) = pk(qj)) (i)
we have that
170
R. Barbuti
Let /?=/3t :: ... I:$.
et al. I Science of Computer
Programming
32 (1998)
145-l 76
We have that
Pk+l(P)
{definition =
of pj}
P”(P) {Lemma
39(ii)}
p”‘(p) v . . . v #j”(P) =
{statement (t )} (%k(4! ) A . . . A %kGd 1) v .‘. v (&kMw
... WkM,J
&k+‘(p).
•J
=
{definition
of Fp}
Let P be a propositional Prolog program.
Corollary 41.
Proof of Theorem 11. The theorem is a consequence
Then pw = $“.
of Proposition
19, Corollary
41.
A.2. Proofs for pure Prolog Lemma 42.
Proof.
For each k and ground atom p(g),
Straightforward
By the previous Definition
by definition
of pk and Proposition
0
34.
lemma we can define the limit assignment
pw
as follows:
43.
pYp(g))
= ki Pk(P(g))y w
where u denotes the least upper bound with respect to 6,. The following
lemma points out a useful property
of pw which will be used in the
sequel. Lemma 44. & = nG(&).
Let G be a goal of the form 3x.G’, & be a sequence in Y,Q,cL(P) and Then
PrOOf. ak = A + 7 & = f by Definition 0”’
2 1.
R. Barbuti et al. /Science
Assume sequence
of Computer Programming 32 (1998)
145-176
171
now ~CQ = f, and assume that ak # 2. By Definition 21, ak is a non-empty P”’ of the form d I :: . . I :: d, where each dj is not a divergent atom. Clearly,
for each j = 1,. . , n and for each ground
substitution
35, and by Lemma 42, pk+‘(d,y)=
Definition
y, we have that pk(d/y) = t, by
t. Hence, for each dj,
Zp,,>(dj)= t, which implies that Vxk = t, contradiction. P” Lemma 45. %(P”)
Let P be a logic program. Then
Proof. Let p(g) be an arbitrary p(x) ++ ($.x=t, be the definition =
q
AG,)V
ground atom and let ... V(Zly.x=t,~G,)
of p in s_comp(P). Then
%w?(P(g)) +((3y.g
{definition
of Yp,>
{definition
of V }
= tl A G, ) v . . . v (3y.g = t,, A G,))
V op’,j(3y.g = ti A Gi). if [Lnl
Let f = {jr , . . . , jh} be the ordered set of elements in [ 1, n] such that there exists 0, = mgu(g, tjr ). Obviously, for each j E [ 1, n] such that j 9 % we have that ~,,~.,(Ily.g = tj A Gj) = f and hence the above disjunction
reduces to
V U,c.(Zly.g = tj A Gj). .iE% The proof is done by case analysis
on the truth value of the last disjunction.
(i) if VjEY ~~~~~(3y.g= tj A Gj) = u there is nothing element with respect to d,.
to prove, since u is the bottom
(ii) Assume VjEB v,,~~(ZIy.g= tj A Gj) = f. We calculate V Vp,,>(3y.g=tjAGj)=f .E/ =
{definition
of V }
Vj’jE.Vp~,,(3y.g=tjAGj)=f
f where we have denoted by Zj the free variables
in GjtIj. For each j, let G; = Gj6j.
172
R. Barbud et al. IScience
of Computer
Programming
32 (1998)
145-176
We have Vp,,s(&j.GJ) =
{definition marf
{$7+X)1
Si
E
of up}
YmLCP))
ZZ
{previous
calculation}
f
For each j E f
we have
{definition
-
‘v’~.$$S~) 3t;.i7CQ(S/I) /I”’
E (u,f},
of maxf)
and
=f {Lemma 44)
3
Vi.~n~;(Sj) E {u,f}, P”] %j.XG~(S/,) = A.
Hence, for each j E f,
and
(iii) Assume
) = 1. Let then e = max(8’ 1
there exists t; such that Q(&,
j E f}. By Lemma 33, it is clear that n,(,)(S/+1)=/2, this and Lemma 42 we conclude that p”‘( p(g)) = f.
and hence p’+‘(p(g))=f.
By
V, E g upC,,(3y.g= tj A G,) = t. We calculate
V vp<,>(3y_g=tj /\Gj)=t iCf
{definition
ZE
b’j E $.u,~,,(~_JJ.cJ = tj A Gj) E
of V 1
{f, t} and
3jE~.v,,,,(3y.g=tjAGj)=t { 0, = mgu(g, tj), definition
E Vjj_$.~,,~.(Elz~.Gj8j)E
{f,t}
of up}
and
3j~$.v,<.(GjOj)=t.
For each j, let Gj = Gj9j. Moreover, for each j such that V,~~~(Zkj.G~) = f we can reason as in the previous case and conclude that there exists /’ such that zG;(&, ) = II.
R. Barbuti et al. IScience of Computer Programming 32 (1998)
For
145-176
173
each j such that u,c’,(%j.Gj) = t we have maxf (~n,:(S,)lSii”nir(P)j
=t
E
{definition Vi.vrc~;(S;) 0”’
of mazf }
E {u, t,, t}, and
-
definition Men,: P”’ Jej.nc:(S/,)
E {u, t,, t},
of t P
and
# 2 with no divergent
atoms.
Hence, for each j E $ there exists ej such that (a) either 71~ (St, ) = 1, (b) or xGr(S/:) # 1, and does not contain divergent atoms. MoreoGer, at least one such j satisfies (b). Let then 6’= max{/ 1j E 2). By Lemma 33, it is clear that a,(,,(Sf+1)#3, and does not contain divergent atoms. Hence p’+‘(p(g))= t. By this and Lemma 42 we can conclude that p”(p(g))= t. (iv) Finally, assume V u,C,J(3y.g = tj A Gj) = t,. Assume now that the thesis does .iEd not hold, i.e. (a) either p”( p(g)) = f @I or, @“Mg)) = u. In case (a), by definition of pw, there exists k > 1 such that (a.1) for each k>8, npcy,(&)=A and #(p(g))=f for each k such that 0
(a.2)
For j E 2
let, as in the previous
For each k k/ UP
n peg)
= PZ)
:: S’ and #(p(g))
cases, GJ = Gj8j, where 6, = mgu(g,
and for each j E 2,
= u. tj).
we have
= 3,
=+
{Lemma 33) nq(&_,)=i
=+
{definition
of t)
Cljnc;(&-,)=f 0”’ =+ = maxf {~n,:(4)lSltYoii(P)}
-
=f
Idefinition Of maxf’
{definition of up and definition of GJ} Vp’,,(3zj.g = tj A Gj) = f.
174
R. Barbuti
et al. IScience
of Computer
Programming
32 (1998)
145-I 76
Hence, we also have that V rp.J(3y.g = tj A Gj) E {u,f} iV V uP,,,(3y.g = tj A Gj) = t,. jGf In case (b), by definition of pw, for each k we have that n,(,,(&) = ~5):: #(p(g)) = u. For each k 2 1, we have
which is a contradiction
n,(,)(&)
with the assumption
S’ and
= PZ) 1:s’
*
{Lemma
33)
xc;,(&_,)=-&:s” =+
{definition
of v} P
gli,,ca4=u
By observing
that also V no;, (SO) = II, we have P”’
= -
{definition of vP and definition of Gj, } Vp,~~(3Zj.g=tj, AGj,)=U
*
{definition
of V }
V vp,,,(3y.g=tjAGj)=U Ed
which is a contradiction
with the assumption
VjEBvp”l(3y.g=tj/\Gj)=tu.
0
Lemma 46. For each k, pkGKS(pW). Proof. Analogous Corollary 47.
to the proof of Lemma 45.
0
p” GK S(p”)
Lemma 48. pw is a jixpoint of 6. Proof. Immediate
consequence
of Lemma 45 and Corollary
Since TPU is the least fixpoint of TP we have the obvious
47.
0
corollary.
Corollary 49. qw GKpw. In order to show that the converse some technical lemmas.
of the last statement
holds as well, we need first
R. Barburi et al. IScience of Computer Programming 32 (1998)
Lemma 50. Let p(g) s_comp(P). Then pk+‘(p(g))
145-l 76
175
be a ground atom and p(x) t+ G the definition of p in
= VnG[x:=&). P”
Proof. The proof is an easy case analysis Definition
35 and Lemma 33.
on the value
of #+‘(p(g)),
exploiting
0
Lemma 51. pk GK q”. Proof. By induction
on k.
Base step: Obvious. Inductive step: Let p(g) be a ground atom and let p(x) t-t G be the definition in s_comp(P). Then, by Lemma 50, #+‘(p(g)) the inductive
hypothesis,
is in the relation
of p
= v nG[x:=y](&), which, by exploiting P” ,K <
with
v ~ol~:=~l(&). .v
Indeed,
clearly
dK with max/{ v Xolx:=gI(Si) 1 Si E Y’DFL(P)}, which is g nothing but v,v(G[x := g]). This, by definition of 9~ and the fact that FpW is a fixpoint q of Yp allows us to conclude the proof.
this is itself in the relation
Corollary 52.
p” and &”
coincide.
Proof of Theorem 26. For ground atoms, the theorem is a consequence of Proposition 19 and Corollary 52. Let now G’ be a (possibly non-ground) conjunction of atoms and consider the closed goal G = 3.G’. It is clear that the truth value of G in YpW coincides value of the propositional symbol anSo in yp?“, where P’ is obtained 0 clause anSo t G’ to P. Hence the theorem.
with the truth by adding the
References [I] J.H. Andrews, The logical structure of sequential Prolog, in: S. Debray, M. Hermenegildo [2] [31
[41 r51 [61
(Eds.), Proc. 1990 North American Conf. on Logic Programming, MIT Press, Cambridge, MA, 1990, pp. 585-602. J.H. Andrews, A logical semantics for depth-first Prolog with ground negation, in: D. Miller (Ed.), Proc. 1993 Intemat. Symp. on Logic Programming, MIT Press, Cambridge, MA, 1993, pp. 220-234. K.R. Apt, Introduction to logic programming, in: J. van Leeuwen (Ed.), Handbook of Theoretical Computer Science, Vol. B: Formal Models and Semantics, Elsevier, Amsterdam and MIT Press, Cambridge, MA, 1990, pp. 495-574. B. Arbab, D.M. Berry, Operational and denotational semantics of Prolog, J. Logic Programming 4 (1987) 309-330. R. Barbuti, M. Codish, R. Giacobazzi, G. Levi, Modelling Prolog control, J. Logic Comput. 3 (1993) 579-603. R. Barbuti, M. Codish, R. Giacobazzi, M. Maher, Oracle semantics for Prolog, Inform. and Comput. 122 (1995) 178200.
176
R. Barbuti
et al. IScience
of Computer
Programming
32 (1998)
145-I 76
[7] R. Barbuti, P. Mancarella, A multiple valued logical semantics for Prolog, in: Proc. ESOP 96, April 1996. [8] E. Biirger, D. Rosenzweig, A mathematical definition of full Prolog, Science of Computer Programming 24 (3) (1995) 249-286. [9] A. Bossi, M. Bugliesi, M. Fabris, A new tixpoint semantics for Prolog, in: Proc. of the 10th Intemat. Conf. on Logic Programming, MIT Press, Cambridge, MA, 1993, pp. 374-389. [IO] A. de Bruin, E. de Vink, Continuation semantics for Prolog with cut, in: J. Diaz, F. Orejas (Eds.), Proc. CAAP 89, Lecture Notes in Computer Science, Vol. 351, Springer, Berlin, 1989, pp. 178-192. [l I] K.L. Clark, Negation as failure, in: Logic and Databases, Plenum Press, New York, 1978, pp. 293-322. [12] SK. Debray, P. Mishra, Denotational and operational semantics for Prolog, in: M. Wirsing (Ed.), Formal Description of Programming Concepts III, North-Holland, Amsterdam, 1987, pp. 245-269. [ 131 G. Epstein, The lattice theory of Post algebras, in: D.C. Rine (Ed.), Computer Science and Multiplevalued Logic, North-Holland, Amsterdam, 1984, pp. 23-40. Reprinted from Trans. Amer. Math. Sot. 95 (2) (1960) 300-317. [l4] M. Fitting, A Kripke-Kleene semantics for logic programs, J. Logic Programming 4 (1985) 295-312. [15] M. Fitting, Bilattices and the semantics of logic programming, J. Logic Programming 11 (1991) 91-l 16. [16] N.D. Jones, A. Mycroft, Stepwise development of operational and denotational semantics for Prolog, in: Sten-Ake Tamlund (Ed.), Proc. Second Intemat. Conf. on Logic Programming, 1984, pp. 281-288. [17] K. Kunen, Negation in logic programming, J. Logic Programming 4 (1987) 298-308. [18] J.W. Lloyd, Foundations of Logic Programming, 2nd Ed., Springer, Berlin, 1987. [19] D. Miller, G. Nadathur, F. Pfenning, A. Scedrow, Uniform proofs as a foundation for logic programming, Ann. Pure Appl. Logic 51 (1991) 125-157. [20] A. Mycroft, Logic programs and multi-valued logic, in: M. Fontet, K. Mehlhom (Eds.), Proc. STACS 84, Lecture Notes in Computer Science, Vol. 166, Springer, Berlin, 1984, pp. 274-286. [21] M. Rayan, M. Sadler, Valuation systems and consequence relations, in: S. Abramsky, D.M. Gabbay, T.S.E. Maibaum (Eds.), Handbook of Logic in Computer Science, Vol. I, Clarendon Press, Oxford, 1992, pp. l-78. [22] W.R. Smith, Minimization of multivalued functions, in: D.C. Rine (Ed.), Computer Science and Multiple-valued Logic, North-Holland, Amsterdam, 1984, pp. 227-267. [23] Special Section on Multiple-valued Logic, IEEE Trans. Comput. C-30 (1981) 617-706.