In this chapter we develop a more formal view of Logic Programming by means of a rigorous treatment of clausal logic and resolution theorem proving. Any such treatment has three parts: syntax, semantics, and proof theory.
Of these three, proof theory is most closely related to Logic Programming, because answering queries is in fact no different from proving theorems. In addition to proof theory, we need semantics for deciding whether the things we prove actually make sense. For instance, we need to be sure that the truth of the theorems is assured by the truth of the axioms. If our inference rules guarantee this, they are said to be sound.
Concepts like soundness and completeness are called meta-theoretical, since they are not expressed in the logic under discussion, but rather belong to a theory about that logic (‘meta’ means above). Their significance is not merely theoretical, but extends to logic programming languages like Prolog. For example, if a logic programming language is unsound, it will give wrong answers to some queries; if it is incomplete, it will give no answer to some other queries.
The structure of this chapter is as follows. We start with a very simple (propositional) logical language, and enrich this language in two steps to full clausal logic. For each of these three languages, we discuss syntax, semantics, proof theory, and meta-theory. We then discuss definite clause logic, which is the subset of clausal logic used in Prolog. Finally, we relate clausal logic to Predicate Logic, and show that they are essentially equal in expressive power.
Informally, a proposition is any statement which is either true or false, such as ‘2 + 2 = 4’ or ‘the moon is made of green cheese’. These are the building blocks of propositional logic, the weakest form of logic.
Propositions are abstractly denoted by atoms, which are single words starting with a lowercase character. For instance, married
is an atom denoting the proposition ‘he/she is married’; similarly, man
denotes the proposition ‘he is a man’. Using the special symbols ‘ :-
’ (if), ‘ ;
’ (or) and ‘ ,
’ (and), we can combine atoms to form clauses.
married;bachelor:-man,adult
is a clause, with intended meaning: ‘somebody is married or a bachelor if he is a man and an adult’
[2]
. The part to the left of the if-symbol ‘ :-
’ is called the head of the clause, and the right part is called the body of the clause. The head of a clause is always a disjunction (or) of atoms, and the body of a clause is always a conjunction (and).
Exercise 2.1.
Translate the following statements into clauses, using the atoms person
, sad
and happy
:
A program is a set of clauses, each of them terminated by a period. The clauses are to be read conjunctively; for example, the program
woman;man:-human.
human:-woman.
human:-man.
has the intended meaning ‘(if someone is human then she/he is a woman or a man) and (if someone is a woman then she is human) and (if someone is a man then he is human)’, or, in other words, ‘someone is human if and only if she/he is a woman or a man’.
The Herbrand base of a program P is the set of atoms occurring in P. For the above program, the Herbrand base is { woman
, man
, human
}. A Herbrand interpretation (or interpretation for short) for P is a mapping from the Herbrand base of P into the set of truth values { true, false }. For example, the mapping { woman
→ true, man
→ false, human
→ true } is a Herbrand interpretation for the above program. A Herbrand interpretation can be viewed as describing a possible state of affairs in the Universe of Discourse (in this case: ‘she is a woman, she is not a man, she is human’).
woman
, human
}.
Since a Herbrand interpretation assigns truth values to every atom in a clause, it also assigns a truth value to the clause as a whole. The rules for determining the truth value of a clause from the truth values of its atoms are not so complicated, if you keep in mind that the body of a clause is a conjunction of atoms, and the head is a disjunction. Consequently, the body of a clause is true if every atom in it is true, and the head of a clause is true if at least one atom in it is true.
The intended meaning of the clause is ‘ if body then head’, which is obviously true in the first case, and false in the second case.
What about the remaining two cases? They cover statements like ‘ if the moon is made of green cheese then 2 + 2 = 4’, in which there is no connection at all between body and head. One would like to say that such statements are neither true nor false. However, our semantics is not sophisticated enough to deal with this: it simply insists that clauses should be assigned a truth value in every possible interpretation. Therefore, we consider the clause to be true whenever its body is false.
married;bachelor:-man,adult
can also be read as ‘someone is married or a bachelor or not a man or not an adult’. Thus, a clause is a disjunction of atoms, which are negated if they occur in the body of the clause. Therefore, the atoms in the body of the clause are often called negative literals, while those in the head of the clause are called positive literals.
To summarise: a clause is assigned the truth value true in an interpretation, if and only if at least one of the following conditions is true: (a) at least one atom in the body of the clause is false in the interpretation (cases (3) and (4)), or (b) at least one atom in the head of the clause is true in the interpretation (cases (1) and (3)). If a clause is true in an interpretation, we say that the interpretation is a model for the clause.
woman
, human
}, { man
, human
}, and { woman
, man
, human
}. Since there are eight possible interpretations for a Herbrand base with three atoms, this means that the program contains enough information to rule out half of these.
Adding more clauses to the program means restricting its set of models. For instance, if we add the clause woman
(a clause with an empty body) to the program, we rule out the first and third model, which leaves us with the models { woman
, human
}, and { woman
, man
, human
}. Note that in both of these models, human
is true. We say that human
is a logical consequence of the set of clauses. In general, a clause C is a logical consequence of a program P if every model of the program is also a model of the clause; we write P ⊨ C.
Exercise 2.2.
Given the program
married;bachelor:-man,adult.
man.
:-bachelor.
determine which of the following clauses are logical consequences of this program:
1. married:-adult
;
2. married:-bachelor
;
3. bachelor:-man
;
4. bachelor:-bachelor
.
Of the two remaining models, obviously { woman
, human
} is the intended one; but the program does not yet contain enough information to distinguish it from the non-intended model { woman
, man
, human
}. We can add yet another clause, to make sure that the atom man
is mapped to false. For instance, we could add
:-man
(it is not a man) or
:-man,woman
(nobody is both a man and a woman).
So, instead of adding clauses until a single model remains, we want to add a rule to our semantics which tells us which of the several models is the intended one. The airline example shows us that, in general, we only want to accept something as true if we are really forced to, i.e. if it is true in every possible model. This means that we should take the intersection of every model of a program in order to construct the intended model. In the example, this is { woman
, human
}. Note that this model is minimal in the sense that no subset of it is also a model. Therefore, this semantics is called a minimal model semantics.
Unfortunately, this approach is only applicable to a restricted class of programs. Consider the following program:
woman;man:-human.
human.
This program has three models: { woman
, human
}, { man
, human
}, and { woman
, man
, human
}. The intersection of these models is { human
}, but this interpretation is not a model of the first clause! The program has in fact not one, but two minimal models, which is caused by the fact that the first clause has a disjunctive head. Such a clause is called indefinite, because it does not permit definite conclusions to be drawn.
On the other hand, if we would only allow definite clauses, i.e. clauses with a single positive literal, minimal models are guaranteed to be unique. We will deal with definite clauses in section 2.4, because Prolog is based on definite clause logic. In principle, this means that clauses like woman;man:-human
are not expressible in Prolog. However, such a clause can be transformed into a ‘pseudo-definite’ clause by moving one of the literals in the head to the body, extended with an extra negation.
woman:-human,not(man).
man:-human,not(woman).
In Prolog, we have to choose between these two clauses, which means that we have only an approximation of the original indefinite clause. Negation in Prolog is an important subject with many aspects. In Chapter 3, we will show how Prolog handles negation in the body of clauses. In Chapter 8, we will discuss particular applications of this kind of negation.
Recall that a clause C is a logical consequence of a program P (P ⊨ C) if every model of P is a model of C. Checking this condition is, in general, unfeasible. Therefore, we need a more efficient way of computing logical consequences, by means of inference rules. If C can be derived from P by means of a number of applications of such inference rules, we say that C can be proved from P. Such inference rules are purely syntactic, and do not refer to any underlying semantics.
The proof theory for clausal logic consists of a single inference rule called resolution. Resolution is a very powerful inference rule. Consider the following program:
married;bachelor:-man,adult.
has_wife:-man,married.
This simple program has no less than 26 models, each of which needs to be considered if we want to check whether a clause is a logical consequence of it.
Exercise 2.3.
Write down the six Herbrand interpretations that are not models of the program.
The following clause is a logical consequence of this program:
has_wife;bachelor:-man,adult
By means of resolution, it can be produced in a single step. This step represents the following line of reasoning: ‘if someone is a man and an adult, then he is a bachelor or married; but if he is married, he has a wife; therefore, if someone is a man and an adult, then he is a bachelor or he has a wife’.
married
, which occurs in the head of the first clause (a positive literal) and in the body of the second (a negative literal). The derived clause, which is called the resolvent, consists of all the literals of the two input clauses, except married
(the literal resolved upon). The negative literal man
, which occurs in both input clauses, appears only once in the derived clause. This process is depicted in fig. 2.1.
Figure 2.1.
A resolution step.
Resolution is most easily understood when applied to definite clauses. Consider the following program:
square:-rectangle,equal_sides.
rectangle:-parallelogram,right_angles.
Applying resolution yields the clause
square:-parallelogram,right_angles,equal_sides
That is, the atom rectangle
in the body of the first clause is replaced by the body of the second clause (which has rectangle
as its head). This process is also referred to as unfolding the second clause into the first one (fig. 2.2).
Figure 2.2.
Resolution with definite clauses.
A resolvent resulting from one resolution step can be used as input for the next. A proof or derivation of a clause C from a program P is a sequence of clauses such that each clause is either in the program, or the resolvent of two previous clauses, and the last clause is C. If there is a proof of C from P, we write P ⊢ C.
Exercise 2.4.
Give a derivation of friendly
from the following program:
happy;friendly:-teacher.
friendly:-teacher,happy.
teacher;wise.
teacher:-wise.
It is easy to show that propositional resolution is sound: you have to establish that every model for the two input clauses is a model for the resolvent. In our earlier example, every model of married;bachelor:-man,adult
and has_wife:-man,married
must be a model of has_wife;bachelor:-man,adult
.
married
) is either assigned the truth value true or false. In the first case, every model of has_wife:-man,married
is also a model of has_wife:-man
; in the second case, every model of married;bachelor:-man,adult
is also a model of bachelor:-man,adult
. In both cases, these models are models of a subclause of the resolvent, which means that they are also models of the resolvent itself.
In general, proving completeness is more complicated than proving soundness. Still worse, proving completeness of resolution is impossible, because resolution is not complete at all!
a:-a
. This clause is a so-called tautology: it is true under any interpretation. Therefore, any model of an arbitrary program P is a model for it, and thus P = a:-a
for any program P. If resolution were complete, it would be possible to derive the clause a:-a
from some program P in which the literal a
doesn’t even occur! It is clear that resolution is unable to do this.
However, this is not necessarily bad, because although tautologies follow from any set of clauses, they are not very interesting. Resolution makes it possible to guide the inference process, by implementing the question ‘is C a logical consequence of P?’ rather than ‘what are the logical consequences of P?’.
The idea is analogous to a proof technique in mathematics called ‘reduction to the absurd’. Suppose for the moment that C consists of a single positive literal a
; we want to know whether P ⊨ a
, i.e. whether every model of P is also a model of a
. It is easily checked that an interpretation is a model of a
if, and only if, it is not a model of :-a
.
a
if, and only if, there is no interpretation which is a model of both :-a
and P. In other words, a
is a logical consequence of P if, and only if, :-a
and P are mutually inconsistent (don’t have a common model). So, checking whether P ⊨ a
is equivalent to checking whether P ∪ { :-a
} is inconsistent.
Resolution provides a way to check this condition. Note that, since an inconsistent set of clauses doesn’t have a model, it trivially satisfies the condition that any model of it is a model of any other clause; therefore, an inconsistent set of clauses has every possible clause as its logical consequence. In particular, the absurd or empty clause, denoted by □ [3] , is a logical consequence of an inconsistent set of clauses. Conversely, if □ is a logical consequence of a set of clauses, we know it must be inconsistent.
a
is a logical consequence of P if, and only if, the empty clause can be deduced by resolution from P augmented with :-a
. This process is called proof by refutation, and resolution is called refutation complete.
This proof method can be generalised to the case where B is not a single atom. For instance, let us check by resolution that a:-a
is a tautology, i.e. a logical consequence of any set of clauses. Logically speaking, this clause is equivalent to ‘ a
or not a
’, the negation of which is ‘ not a
and a
’, which is represented by two separate clauses :-a
and a
. Since we can derive the empty clause from these two clauses in a single resolution step without using any other clauses, we have in fact proved that a:-a
is a logical consequence of an empty set of clauses, hence a tautology.
Exercise 2.5.
Prove by refutation that friendly:-has_friends
is a logical consequence of the following clauses:
happy:-has_friends.
friendly:-happy.
Finally, we mention that although resolution can always be used to prove inconsistency of a set of clauses it is not always fit to prove the opposite, i.e. consistency of a set of clauses. For instance, a
is not a logical consequence of a:-a
; yet, if we try to prove the inconsistency of :-a
and a:-a
(which should fail) we can go on applying resolution forever!
:-a
and a:-a
again yields :-a
. In this simple case it is easy to check for loops: just maintain a list of previously derived clauses, and do not proceed with clauses that have been derived previously.
However, as we will see, this is not possible in the general case of full clausal logic, which is semi-decidable with respect to the question ‘is B a logical consequence of A ’: there is an algorithm which derives, in finite time, a proof if one exists, but there is no algorithm which, for any A and B, halts and returns ‘no’ if no proof exists. The reason for this is that interpretations for full clausal logic are in general infinite. As a consequence, some Prolog programs may loop forever (just like some Pascal programs).
Propositional clausal logic is rather coarse-grained, because it takes propositions (i.e. anything that can be assigned a truth value) as its basic building blocks. For example, it is not possible to formulate the following argument in propositional logic:
Peter likes all his students
Maria is one of Peter’s students
Therefore, Peter likes Maria
In order to formalise this type of reasoning, we need to talk about individuals like Peter and Maria, sets of individuals like Peter’s students, and relations between individuals, such as ‘likes’. This refinement of propositional clausal logic leads us into relational clausal logic.
Individual names are called constants; we follow the Prolog convention of writing them as single words starting with a lowercase character (or as arbitrary strings enclosed in single quotes, like 'this is a constant'
). Arbitrary individuals are denoted by variables, which are single words starting with an uppercase character. Jointly, constants and variables are denoted as terms. A ground term is a term without variables
[4]
.
Relations between individuals are abstractly denoted by predicates (which follow the same notational conventions as constants). An atom is a predicate followed by a number of terms, enclosed in brackets and separated by commas, e.g. likes(peter,maria)
. The terms between brackets are called the arguments of the predicate, and the number of arguments is the predicate’s arity. The arity of a predicate is assumed to be fixed, and predicates with the same name but different arity are assumed to be different. A ground atom is an atom without variables.
All the remaining definitions pertaining to the syntax of propositional clausal logic, in particular those of literal, clause and program, stay the same. So, the following clauses are meant to represent the above statements:
likes(peter,S):-student_of(S,peter). student_of(maria,peter).
The intended meaning of these clauses are, respectively, ‘ if S is a student of Peter then Peter likes S ’, ‘Maria is a student of Peter’, and ‘Peter likes Maria’. Clearly, we want our logic to be such that the third clause follows logically from the first two, and we want to be able to prove this by resolution. Therefore, we must extend the semantics and proof theory in order to deal with variables.
The Herbrand universe of a program P is the set of ground terms (i.e. constants) occurring in it. For the above program, the Herbrand universe is { peter
, maria
}. The Herbrand universe is the set of all individuals we are talking about in our clauses. The Herbrand base of P is the set of ground atoms that can be constructed using the predicates in P and the ground terms in the Herbrand universe. This set represents all the things we can say about the individuals in the Herbrand universe.
The Herbrand base of the above program is
{ likes(peter,peter), likes(peter,maria),
likes(maria,peter), likes(maria,maria),
student_of(peter,peter), student_of(peter,maria),
student_of(maria,peter), student_of(maria,maria) }
As before, a Herbrand interpretation is the subset of the Herbrand base whose elements are assigned the truth value true. For instance,
{likes(peter,maria), student_of(maria,peter)}
is an interpretation of the above program.
Clearly, we want this interpretation to be a model of the program, but now we have to deal with the variables in the program. A substitution is a mapping from variables to terms. For example, { S
→ maria
} and { S
→ X
} are substitutions. A substitution can be applied to a clause, which means that all occurrences of a variable occurring on the lefthand side in a substitution are replaced by the term on the righthand side.
likes(peter,S):-student_of(S,peter)
then the above substitutions yield the clauses
likes(peter,maria):-student_of(maria,peter)
likes(peter,X):-student_of(X,peter)
Notice that the first clause is ground; it is said to be a ground instance of C, and the substitution { S
→ maria
} is called a grounding substitution. All the atoms in a ground clause occur in the Herbrand base, so reasoning with ground clauses is just like reasoning with propositional clauses.
M = {likes(peter,maria), student_of(maria,peter)}
is a model of the clause C above, we have to construct the set of the ground instances of C over the Herbrand universe { peter
, maria
}, which is
{ likes(peter,maria):-student_of(maria,peter),
likes(peter,peter):-student_of(peter,peter) }
and show that M is a model of every element of this set.
Exercise 2.6.
How many models does C have over the Herbrand universe { peter
, maria
}?
Because reasoning with ground clauses is just like reasoning with propositional clauses, a naive proof method in relational clausal logic would apply grounding substitutions to every clause in the program before applying resolution. Such a method is naive, because a program has many different grounding substitutions, most of which do not lead to a resolution proof. For instance, if the Herbrand universe contains four constants, then a clause with two distinct variables has 16 different grounding substitutions, and a program consisting of three such clauses has 4096 different grounding substitutions.
Instead of applying arbitrary grounding substitutions before trying to apply resolution, we will derive the required substitutions from the clauses themselves. Recall that in order to apply propositional resolution, the literal resolved upon should occur in both input clauses (positive in one clause and negative in the other). In relational clausal logic, atoms can contain variables. Therefore, we do not require that exactly the same atom occurs in both clauses; rather, we require that there is a pair of atoms which can be made equal by substituting terms for variables.
likes(peter,S):-student_of(S,peter).
student_of(maria,T):-follows(maria,C),teaches(T,C).
The second clause is intended to mean: ‘Maria is a student of any teacher who teaches a course she follows’. From these two clauses we should be able to prove that ‘Peter likes Maria if Maria follows a course taught by Peter’. This means that we want to resolve the two clauses on the student_of
literals.
The two atoms student_of(S,peter)
and student_of(maria,T)
can be made equal by replacing S
by maria
and T
by peter
, by means of the substitution { S
→ maria
, T
→ peter
}. This process is called unification, and the substitution is called a unifier. Applying this substitution yields the following two clauses:
likes(peter,maria):-student_of(maria,peter).
student_of(maria,peter):-follows(maria,C),
teaches(peter,C).
(Note that the second clause is not ground.)
likes(peter,maria):-follows(maria,C),teaches(peter,C).
Exercise 2.7.
Write a clause expressing that Peter teaches all the first-year courses, and apply resolution to this clause and the above resolvent.
Consider the following two-clause program P′:
likes(peter,S):-student_of(S,peter).
student_of(X,T):-follows(X,C),teaches(T,C).
which differs from the previous program P in that the constant maria
in the second clause has been replaced by a variable. Since this generalises the applicability of this clause from Maria to any of Peter’s students, it follows that any model for P′ over a Herbrand universe including maria
is also a model for P, and therefore P′ ⊧ P.
likes(peter,maria):-follows(maria,C),teaches(peter,C).
from P′ by means of the unifier { S
→ maria
, X
→ maria
, T
→ peter
}.
Unifiers are not necessarily grounding substitutions: the substitution { X
→ S
, T
→ peter
} also unifies the two student_of
literals, and the two clauses then resolve to
likes(peter,S):-follows(S,C),teaches(peter,C).
The first unifier replaces more variables by terms than strictly necessary, while the second contains only those substitutions that are needed to unify the two atoms in the input clauses. As a result, the first resolvent is a special case of the second resolvent, that can be obtained by means of the additional substitution { S
→ maria
}. Therefore, the second resolvent is said to be more general than the first
[5]
. Likewise, the second unifier is called a more general unifier than the first.
As it were, more general resolvents summarise a lot of less general ones. It therefore makes sense to derive only those resolvents that are as general as possible, when applying resolution to clauses with variables. This means that we are only interested in a most general unifier (mgu) of two literals. Such an mgu, if it exists, is always unique, apart from an arbitrary renaming of variables (e.g. we could decide to keep the variable X
, and replace S
by X
). If a unifier does not exist, we say that the two atoms are not unifiable. For instance, the atoms student_of(maria,peter)
and student_of(S,maria)
are not unifiable.
As we have seen before, the actual proof method in clausal logic is proof by refutation. If we succeed in deriving the empty clause, then we have demonstrated that the set of clauses is inconsistent under the substitutions that are needed for unification of literals.
likes(peter,S):-student_of(S,peter). student_of(S,T):-follows(S,C),teaches(T,C). teaches(peter,ai_techniques). follows(maria,ai_techniques).
If we want to find out if there is anyone whom Peter likes, we add to the program the negation of this statement, i.e. ‘Peter likes nobody’ or :-likes(peter,N)
; this clause is called a query or a goal. We then try to refute this query by finding an inconsistency by means of resolution. A refutation proof is given in fig. 2.3.
Figure 2.3.
A refutation proof which finds someone whom Peter likes.
In this figure, which is called a proof tree, two clauses on a row are input clauses for a resolution step, and they are connected by lines to their resolvent, which is then again an input clause for a resolution step, together with another program clause. The mgu’s are also shown. Since the empty clause is derived, the query is indeed refuted, but only under the substitution { N
→ maria
}, which constitutes the answer to the query.
In general, a query can have several answers. For instance, suppose that Peter does not only like his students, but also the people his students like (and the people those people like, and …):
likes(peter,S):-student_of(S,peter). likes(peter,Y):-likes(peter,X),likes(X,Y). likes(maria,paul). student_of(S,T):-follows(S,C),teaches(T,C). teaches(peter,ai_techniques). follows(maria,ai_techniques).
The query
?-likes(peter,N).
will now have two answers.
Exercise 2.8.
Draw the proof tree for the answer { N
→ paul
}.
As with propositional resolution, relational resolution is sound (i.e. it always produces logical consequences of the input clauses), refutation complete (i.e. it always detects an inconsistency in a set of clauses), but not complete (i.e. it does not always generate every logical consequence of the input clauses). An important characteristic of relational clausal logic is that the Herbrand universe (the set of individuals we can reason about) is always finite. Consequently, models are finite as well, and there are a finite number of different models for any program.
Relational logic extends propositional logic by means of the logical variable, which enables us to talk about arbitrary un-named individuals. However, consider the following statement:
Everybody loves somebody.
The only way to express this statement in relational clausal logic, is by explicitly listing every pair of persons such that the first loves the second, e.g.
loves(peter,peter).
loves(anna,paul).
loves(paul,anna).
First of all, this is not a precise translation of the above statement into logic, because it is too explicit (e.g. the fact that Peter loves himself does not follow from the original statement). Secondly, this translation works only for finite domains, while the original statement also allows infinite domains. Many interesting domains are infinite, such as the set of natural numbers.
loves(X,person_loved_by(X))
The fact loves(peter,person_loved_by(peter))
is a logical consequence of this clause. Since we know that everybody loves somebody, there must exist someone whom Peter loves.
person_loved_by(peter)
without explicitly stating whom it is that Peter loves. As we will see, this way of composing complex names from simple names also gives us the possibility to reflect the structure of the domain in our logical formulas.
Exercise 2.9.
Translate to clausal logic:
A term is either simple or complex. Constants and variables are simple terms. A complex term is a functor (which follows the same notational conventions as constants and predicates) followed by a number of terms, enclosed in brackets and separated by commas, e.g. eldest_child_of(anna,paul)
. The terms between brackets are called the arguments of the functor, and the number of arguments is the functor’s arity. Again, a ground term is a term without variables. All the other definitions (atom, clause, literal, program) are the same as for relational clausal logic.
Although there is no syntactic difference in full clausal logic between terms and atoms, their meaning and use is totally different, a fact which should be adequately reflected in the semantics. A term always denotes an individual from the domain, while an atom denotes a proposition about individuals, which can get a truth value. Consequently, we must change the definition of the Herbrand universe in order to accomodate for complex terms: given a program P, the Herbrand universe is the set of ground terms that can be constructed from the constants and functors in P (if P contains no constants, choose an arbitrary one).
plus(0,X,X). plus(s(X),Y,s(Z)):-plus(X,Y,Z).
then the Herbrand universe of P is { 0
, s(0)
, s(s(0))
, s(s(s(0)))
, …}. Thus, as soon as a program contains a functor, the Herbrand universe (the set of individuals we can reason about) is an infinite set.
Exercise 2.10.
Determine the Herbrand universe of the following program:
listlength([],0). listlength([_X|Y],s(L)):-listlength(Y,L).
(Hint: recall that []
is a constant, and that [X|Y]
is an alternative notation for the complex term .(X,Y)
with binary functor ‘ .
’!)
The Herbrand base of P remains the set of ground atoms that can be constructed using the predicates in P and the ground terms in the Herbrand universe. For the above program, the Herbrand base is
{ plus(0,0,0), plus(s(0),0,0), …,
plus(0,s(0),0), plus(s(0),s(0),0), …,
…,
plus(s(0),s(s(0)),s(s(s(0)))), … }
As before, a Herbrand interpretation is a subset of the Herbrand base, whose elements are assigned the truth value true. For instance,
{ plus(0,0,0), plus(s(0),0,s(0)), plus(0,s(0),s(0)) }
is an interpretation of the above program.
Is this interpretation also a model of the program? As in the propositional case, we define an interpretation to be a model of a program if it is a model of every ground instance of every clause in the program. But since the Herbrand universe is infinite, there are an infinite number of grounding substitutions, hence we must generate the ground clauses in a systematic way, e.g.
plus(0,0,0)
plus(s(0),0,s(0)):-plus(0,0,0)
plus(s(s(0)),0,s(s(0))):-plus(s(0),0,s(0))
plus(s(s(s(0))),0,s(s(s(0)))):-plus(s(s(0)),0,s(s(0)))
…
plus(0,s(0),s(0))
plus(s(0),s(0),s(s(0))):-plus(0,s(0),s(0))
plus(s(s(0)),s(0),s(s(s(0)))):-plus(s(0),s(0),s(s(0)))
…
plus(0,s(s(0)),s(s(0)))
plus(s(0),s(s(0)),s(s(s(0)))):-plus(0,s(s(0)),s(s(0)))
plus(s(s(0)),s(s(0)),s(s(s(s(0))))):-plus(s(0),s(s(0)),s(s(s(0))))
…
Now we can reason as follows: according to the first ground clause, plus(0,0,0)
must be in any model; but then the second ground clause requires that plus(s(0),0,s(0))
must be in any model, the third ground clause requires plus(s(s(0)),0,s(s(0)))
to be in any model, and so on.
plus(0,s(0),s(0))
plus(s(0),s(0),s(s(0)))
plus(s(s(0)),s(0),s(s(s(0))))
…
are in the model; the third group of ground clauses requires that
plus(0,s(s(0)),s(s(0)))
plus(s(0),s(s(0)),s(s(s(0))))
plus(s(s(0)),s(s(0)),s(s(s(s(0)))))
…
are in the model, and so forth.
In other words, every model of this program is necessarily infinite. Moreover, as you should have guessed by now, it contains every ground atom such that the number of s
’s in the third argument equals the number of s
’s in the first argument plus the number of s
’s in the second argument. The way we generated this infinite model is particularly interesting, because it is essentially what was called the naive proof method in the relational case: generate all possible ground instances of program clauses by applying every possible grounding substitution, and then apply (propositional) resolution as long as you can.
Although the introduction of only a single functor already results in an infinite Herbrand universe, models are not necessarily infinite. Consider the following program:
reachable(oxford,charing_cross,piccadilly). reachable(X,Y,route(Z,R)):- connected(X,Z,_L), reachable(Z,Y,R). connected(bond_street,oxford,central).
with intended meaning ‘Charing Cross is reachable from Oxford Circus via Piccadilly Circus’, ‘ if X is connected to Z by line L and Y is reachable from Z via R then Y is reachable from X via a route consisting of Z and R ’ and ‘Bond Street is connected to Oxford Circus by the Central line’.
{ connected(bond_street,oxford,central),
reachable(oxford,charing_cross,piccadilly),
reachable(bond_street,charing_cross,route(oxford,piccadilly)) }
Resolution for full clausal logic is very similar to resolution for relational clausal logic: we only have to modify the unification algorithm in order to deal with complex terms. For instance, consider the atoms
plus(s(0),X,s(X))
and
plus(s(Y),s(0),s(s(Y)))
Their mgu is { Y
→ 0
, X
→ s(0)
}, yielding the atom
plus(s(0),s(0),s(s(0)))
In order to find this mgu, we first of all have to make sure that the two atoms do not have any variables in common; if needed some of the variables should be renamed. Then, after making sure that both atoms contain the same predicate (with the same arity), we scan the atoms from left to right, searching for the first subterms at which the two atoms differ. In our example, these are 0
and Y
.
Y
→ 0
}), because it is going to be part of the unifier we are constructing. Then, proceed with the next subterms at which the two atoms differ. Unification is finished when no such subterms can be found (the two atoms are made equal).
Although the two atoms initially have no variables in common, this may change during the unification process. Therefore, it is important that, before a variable is replaced by a term, we check whether the variable already occurs in that term; this is called the occur check. If the variable does not occur in the term by which it is to be replaced, everything is in order and we can proceed; if it does, the unification should fail, because it would lead to circular substitutions and infinite terms.
loves(X,person_loved_by(X))
We want to know whether this implies that someone loves herself; thus, we add the query :-loves(Y,Y)
to this clause and try to apply resolution. To this end, we must unify the two atoms. The first subterms at which they differ are the first arguments, so we apply the partial substitution Y
→ X
to the two atoms, resulting in
loves(X,person_loved_by(X))
and
loves(X,X)
The next subterms at which these atoms differ are their second arguments, one of which is a variable. Suppose that we ignore the fact that this variable, X
, already occurs in the other term; we construct the substitution X
→ person_loved_by(X)
. Now, we have reached the end of the two atoms, so unification has succeeded, we have derived the empty clause, and the answer to the query is
X → person_loved_by(person_loved_by(person_loved_by(…)))
which is an infinite term.
Now we have two problems. The first is that we did not define any semantics for infinite terms, because there are no infinite terms in the Herbrand base. But even worse, the fact that there exists someone who loves herself is not a logical consequence of the above clause! That is, this clause has models in which nobody loves herself. So, unification without occur check would make resolution unsound.
Exercise 2.11.
If possible, unify the following pairs of terms:
plus(X,Y,s(Y))
and plus(s(V),W,s(s(V)))
;length([X|Y],s(0))
and length([V],V)
;larger(s(s(X)),X)
and larger(V,s(V))
.The disadvantage of the occur check is that it can be computationally very costly. Suppose that you need to unify X
with a list of thousand elements, then the complete list has to be searched in order to check whether X
occurs somewhere in it. Moreover, cases in which the occur check is needed often look somewhat exotic. Since the developers of Prolog were also taking the efficiency of the Prolog interpreter into consideration, they decided to omit the occur check from Prolog’s unification algorithm.
Most meta-theoretical results concerning full clausal logic have already been mentioned. Full clausal resolution is sound (as long as unification is performed with the occur check), refutation complete but not complete. Moreover, due to the possibility of infinite interpretations full clausal logic is only semi-decidable: that is, if A is a logical consequence of B, then there is an algorithm that will check this in finite time; however, if A is not a logical consequence of B, then there is no algorithm which is guaranteed to check this in finite time for arbitrary A and B.Consequently, there is no general way to prevent Prolog from looping if no (further) answers to a query can be found.
In the foregoing three sections, we introduced and discussed three variants of clausal logic, in order of increasing expressiveness. In this section, we will show how an additional restriction on each of these variants will significantly improve the efficiency of a computational reasoning system for clausal logic. This is the restriction to definite clauses, on which Prolog is based.
Consider the following program:
married(X);bachelor(X):-man(X),adult(X).
man(peter).
adult(peter).
:-married(maria).
:-bachelor(maria).
man(paul).
:-bachelor(paul).
There are many clauses that are logical consequences of this program. In particular, the following three clauses can be derived by resolution:
married(peter);bachelor(peter)
:-man(maria),adult(maria)
married(paul):-adult(paul)
Exercise 2.12.
Draw the proof tree for each of these derivations.
In each of these derivations, the first clause in the program is used in a different way. In the first one, only literals in the body are resolved away; one could say that the clause is used from right to left. In the second derivation the clause is used from left to right, and in the third one literals from both the head and the body are resolved away. The way in which a clause is used in a resolution proof cannot be fixed in advance, because it depends on the thing we want to prove (the query in refutation proofs).
On the other hand, this indeterminacy substantially increases the time it takes to find a refutation. Let us decide for the moment to use clauses only in one direction, say from right to left. That is, we can only resolve the negative literals away in a clause, as in the first derivation above, but not the positive literals. But now we have a problem: how are we going to decide whether Peter is married or a bachelor? We are stuck with a clause with two positive literals, representing a disjunctive or indefinite conclusion.
This problem can in turn be solved by requiring that clauses have exactly one positive literal, which leads us into definite clause logic. Consequently, a definite clause
$$ A :- B_1,\ldots,B_n $$
will always be used in the following way: $A$ is proved by proving each of $B_1$,…,$B_n$. This is called the procedural interpretation of definite clauses, and its simplicity makes the search for a refutation much more efficient than in the indefinite case. Moreover, it allows for an implementation which limits the amount of memory needed, as will be explained in more detail in Chapter 5.
But how do we express in definite clause logic that adult men are bachelors or married? Even if we read the corresponding indefinite clause from right to left only, it basically has two different procedural interpretations:
We should first choose one of these procedural interpretations, and then convert it into a ‘pseudo-definite’ clause. In case (1), this would be
married(X):-man(X),adult(X),not bachelor(X)
and case (2) becomes
bachelor(X):-man(X),adult(X),not married(X)
These clauses do not conform to the syntax of definite clause logic, because of the negation symbol not
. We will call them general clauses.
If we want to extend definite clause logic to cover general clauses, we should extend resolution in order to deal with negated literals in the body of a clause. In addition, we should extend the semantics. This topic will be addressed in section 8.2. Without going into too much detail here, we will demonstrate that preferring a certain procedural interpretation corresponds to preferring a certain minimal model.
married(X);bachelor(X):-man(X),adult(X)
Supposing that john
is the only individual in the Herbrand universe, and that man(john)
and adult(john)
are both true, then the models of this clause are
{man(john),adult(john),married(john)}
{man(john),adult(john),bachelor(john)}
{man(john),adult(john),married(john),bachelor(john)}
Note that the first two models are minimal, as is characteristic for indefinite clauses. If we want to make the clause definite, we should single out one of these two minimal models as the intended model. If we choose the first model, in which John is married but not a bachelor, we are actually preferring the general clause
married(X):-man(X),adult(X),not bachelor(X)
Likewise, the second model corresponds to the general clause
bachelor(X):-man(X),adult(X),not married(X)
Exercise 2.13.
Write a clause for the statement ‘somebody is innocent unless proven guilty’, and give its intended model (supposing that john
is the only individual in the Herbrand universe).
An alternative approach to general clauses is to treat not
as a special Prolog predicate, as will be discussed in the next chapter. This has the advantage that we need not extend the proof theory and semantics to incorporate general clauses. However, a disadvantage is that in this way not
can only be understood procedurally.
Clausal logic is a formalism especially suited for automated reasoning. However, the form of logic usually presented in courses on Symbolic Logic is (first-order) Predicate Logic. Predicate logic is more expressive in the sense that statements expressed in Predicate Logic often result in shorter formulas than would result if they were expressed in clausal logic.
Being syntactically quite different, clausal logic and Predicate Logic are semantically equivalent in the following sense: every set of clauses is, after minor modifications, a formula in Predicate Logic, and conversely, every formula in Predicate Logic can be rewritten to an ‘almost’ equivalent set of clauses.
We start with the propositional case. Any clause like
married;bachelor:-man,adult
can be rewritten by reversing head and body and replacing the ‘:-
’ sign by an implication ‘→’, replacing ‘,
’ by a conjunction ‘∧’, and replacing ‘;
’ by a disjunction ‘∨’, which yields
man ∧ adult → married ∨ bachelor
By using the logical laws A → B ≡ ¬ A ∨ B and ¬ (C ∧ D) ≡ ¬ C ∨¬ D, this can be rewritten into the logically equivalent formula
¬man ∨ ¬adult ∨ married ∨ bachelor
which, by the way, clearly demonstrates the origin of the terms negative literal and positive literal!
A set of clauses can be rewritten by rewriting each clause separately, and combining the results into a single conjunction, e.g.
married;bachelor:-man,adult.
has_wife:-man,married.
becomes
(¬man ∨ ¬adult ∨ married ∨ bachelor) ∧
(¬man ∨ ¬married ∨ has_wife)
Formulas like these, i.e. conjunctions of disjunctions of atoms and negated atoms, are said to be in conjunctive normal form (CNF).
The term ‘normal form’ here indicates that every formula of Predicate Logic can be rewritten into a unique equivalent formula in conjunctive normal form, and therefore to a unique equivalent set of clauses.
(married ∨ ¬child) → (adult ∧ (man ∨ woman))
can be rewritten into CNF as (replace A → B by ¬A ∨ B, push negations inside by means of De Morgan’s laws: ¬(C ∧ D) ≡ ¬C ∨ ¬D and ¬(C ∨ D) ≡ ¬C ∧ ¬D, and distribute ∧ over ∨ by means of (A ∧ B) ∨ C ≡ (A ∨ C) ∧ (B ∨ C)):
(¬married ∨ adult) ∧ (¬married ∨ man ∨ woman) ∧
(child ∨ adult) ∧ (child ∨ man ∨ woman)
and hence into clausal form as
adult:-married.
man;woman:-married.
child;adult.
child;man;woman.
For rewriting clauses from full clausal logic to Predicate Logic, we use the same rewrite rules as for propositional clauses. Additionally, we have to add universal quantifiers for every variable in the clause.
reachable(X,Y,route(Z,R)):-
connected(X,Z,L),
reachable(Z,Y,R).
becomes
∀
X
∀Y
∀Z
∀R
∀L:
¬connected(X,Z,L)
∨ ¬reachable(Z,Y,R)
∨reachable(X,Y,route(Z,R))
The reverse process of rewriting a formula of Predicate Logic into an equivalent set of clauses is somewhat complicated if existential quantifiers are involved (the exact procedure is given as a Prolog program in Appendix B.1). An existential quantifier allows us to reason about individuals without naming them.
∀X ∃Y: loves(X,Y)
Recall that we translated this same statement into clausal logic as
loves(X,person_loved_by(X))
These two formulas are not logically equivalent! That is, the Predicate Logic formula has models like { loves(paul,anna)
} which are not models of the clause.
loves(paul,person_loved_by(paul))
}. Thus, we have that the formula has a model if and only if the clause has a model (but not necessarily the same model).
So, existential quantifiers are replaced by functors. The arguments of the functor are given by the universal quantifiers in whose scope the existential quantifier occurs. In the above example, ∃ Y
occurs within the scope of ∀ X
, so we replace Y
everywhere in the formula by person_loved_by(X)
, where person_loved_by
should be a new functor, not occurring anywhere else in the clause (or in any other clause). This new functor is called a Skolem functor, and the whole process is called Skolemisation.
∃X ∀Y: loves(X,Y)
(‘somebody loves everybody’) is translated to the clause
loves(someone_who_loves_everybody,X)
Finally, we illustrate the whole process of converting from Predicate Logic to clausal logic by means of an example. Consider the sentence ‘Everyone has a mother, but not every woman has a child’. In Predicate Logic, this can be represented as
∀Y∃X: mother_of(X,Y) ∧ ¬∀Z∃W: woman(Z)→mother_of(Z,W)
First, we push the negation inside by means of the equivalences ¬∀X: F ≡ ∃X: ¬F and ¬∃Y: G ≡ ∀Y: ¬G, and the previously given propositional equivalences, giving
∀Y∃X: mother_of(X,Y) ∧ ∃Z∀W: woman(Z) ∧ ¬mother_of(Z,W)
The existential quantifiers are Skolemised: X
is replaced by mother(Y)
, because it is in the scope of the universal quantifier ∀Y
. Z
, however, is not in the scope of a universal quantifier; therefore it is replaced by a Skolem constant childless_woman
. The universal quantifiers can now be dropped:
mother_of(mother(Y),Y) ∧
woman(childless_woman) ∧ ¬mother_of(childless_woman,W)
This formula is already in CNF, so we obtain the following set of clauses:
mother_of(mother(Y),Y).
woman(childless_woman).
:-mother_of(childless_woman,W).
Exercise 2.14.
Translate to clausal logic:
X
∃Y: mouse(X)
→ tail_of(Y,X)
;X
∃Y: loves(X,Y)
∧ (
∀Z: loves(Y,Z))
;X
∀Y
∃Z: number(X)
∧ number(Y)
→ maximum(X,Y,Z)
.