Logic PDF
Logic PDF
Logic PDF
Daniel Richardson,
Department of Computer Science, University of Bath.
email : [email protected]
1 Formal Systems 5
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 Problem Solving . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Design principles of formal systems . . . . . . . . . . . . . . 8
1.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.5 Related ideas:Confluence and termination . . . . . . . . . . 9
1
2 CONTENTS
4 Predicate Logic 33
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4.2 Truth values and predicates . . . . . . . . . . . . . . . . . . 33
4.3 Variables and types . . . . . . . . . . . . . . . . . . . . . . . 34
4.4 Translation from informal to formal language . . . . . . . . 35
4.5 First order languages . . . . . . . . . . . . . . . . . . . . . . 35
4.6 Syntax, substitution . . . . . . . . . . . . . . . . . . . . . . 37
4.7 Free and bound variables . . . . . . . . . . . . . . . . . . . 37
4.8 Semantics for first order languages . . . . . . . . . . . . . . 38
4.9 Examples : LN and LR . . . . . . . . . . . . . . . . . . . . 41
4.10 Uses of these ideas in computing . . . . . . . . . . . . . . . 42
4.11 More Substitution . . . . . . . . . . . . . . . . . . . . . . . 43
4.12 Other kinds of language and other logics . . . . . . . . . . . 44
4.13 Logic Games . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.14 Normal Forms . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.14.1 CNF and DNF . . . . . . . . . . . . . . . . . . . . . 46
4.14.2 Prenex Normal form . . . . . . . . . . . . . . . . . . 48
4.14.3 Skolem form . . . . . . . . . . . . . . . . . . . . . . 49
4.14.4 Clausal form . . . . . . . . . . . . . . . . . . . . . . 51
4.15 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.16 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.17 Revision Problems. Clausal form . . . . . . . . . . . . . . . 55
4.18 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.19 Examples and Solutions . . . . . . . . . . . . . . . . . . . . 56
5 Semantic Tableaux 59
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.2 Semantic tableau rules . . . . . . . . . . . . . . . . . . . . . 63
5.3 Some advice about the use of the rules . . . . . . . . . . . . 66
5.4 How much use is this system? . . . . . . . . . . . . . . . . . 67
5.4.1 Gödel completeness theorem . . . . . . . . . . . . . 67
5.4.2 Unsolvability and intractability . . . . . . . . . . . . 70
5.5 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
5.6 Satisfaction Games . . . . . . . . . . . . . . . . . . . . . . . 72
5.7 Revision Problems . . . . . . . . . . . . . . . . . . . . . . . 72
5.8 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
5.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
12 References 139
Chapter 1
Formal Systems
1.1 Introduction
There is a long tradition, going back at least to the axiomatic geometry
of the ancient Greeks, of trying to model human thought processes by for-
mal systems. Computers, allowing global communications and requiring
development of standard languages, encourage standard formal representa-
tions of human thinking, reckoning, scheming, calculating and cogitation.
A great deal of work is currently being done in this area, which includes and
exceeds the bounds of the subject usually called artificial intelligence. This
book attempts to describe one of the most successful of the formalisms:
formal deductive systems.
5
6 CHAPTER 1. FORMAL SYSTEMS
Perhaps you will agree that there are some common features in this
list. It seems that in some ways we can take the mouse in the maze as
typical of problem solving agents. Problem solving seems to be a process of
exploration within some definite space of possibilities. Within this space of
possibilities, some moves are allowed and some are prohibited. What kind
of general representation can we make for this situation?
We will call a position in the space of possibilities a configuration. We
hope to be able to write down the configuration at each moment. The
configuration at a moment in a game of chess, for example, might be the
arrangement of pieces on the board, together with an indication of who
should move next. We think of each configuration as a point, or a posi-
tion in the space of possibilities, and the moves are transitions from one
configuration to another.
Assume that we represent each configuration by a data structure in some
set D of data structures, appropriate to the situation we are considering.
(Here, a data structure could be a number or a string of characters, or a
vector or a matrix, or a tree labelled with data structures, or a directed
graph labelled with data structures, for example.) To say that D is a set
of data structures does not commit us to very much. But we do want to
insist that the items in D are unambiguous and finite and can somehow
be written down. The items in D are syntactic; they are pieces of syntax,
arrangements of symbols.
We must also say how the moves within D are constrained. What
transitions are allowed, and what forbidden? We will suppose that we have
some collection of rules, which we will denote R, which determine which
transitions in D are allowed. If X and Y are in D, we will write
X ⇒R Y
to mean that the rules R allow a transition from X to Y in one step.
1.4 Summary
A formal system is a set of data structures together with a set of transfor-
mations which act on them.
Important examples are formal grammars for generating formal lan-
guages, formal proof systems in predicate logic, the lambda calculus which
formalises symbolic computation, and populations of autonomous agents.
Also all games.
α;β
where α and β are in Σ . Note that Σ∗ contains the empty word.
∗
11
12 CHAPTER 2. STRING REWRITING SYSTEMS
αn ; βn
A Σ rewriting system is meant to be a set of rules which defines a
computational process on strings of symbols from Σ. It turns out that any
computational process can be seen in this way.
A ⇒P B
if A is of the form W1 αi W2 , and B is of the form W1 βi W2 , where
αi ; βi is in P . (Note that W1 and/ or W2 can be empty.)
When the rewriting system P is understood, we will leave off the sub-
script P .
As with all formal systems, once we have got a clear notion of one step
derivation, we can iterate this to obtain derivations of any finite length.
Definition 2.3 A derivation in rewriting system P is a list of words A1 , A2 , ..., Ak
so that A1 ⇒ A2 ⇒ ... ⇒ Ak .
Definition 2.4 We will say that there is a derivation which goes from A
to B, and write this as A ⇒∗ B if there exists A1 , ..., An so that
A = A1 ⇒ A2 ⇒ ... ⇒ An ⇒ B
We allow derivations of length zero. So we will always have A ⇒∗ A.
Example 2.1 Suppose Σ = {a, b}, and P is
aba ; b
aaa ;
Let W be the word aaaababbbbabababaaaababa.
In order to apply P to W , we attempt to match left hand sides of the
rules of P with parts of W . There are 6 matches with aba and four matches
with aaa. So there are ten words B with W ⇒ B.
2.2. LANGUAGE GENERATION: GRAMMARS 13
Problem 2.1 Start with W , as given in the example above. Apply the
given rules in any order until you terminate. Why did you eventually ter-
minate? Do you get the same result at termination, no matter in which
order you apply the rules?
Example 2.2 Consider the language, L = {ab, aabb, aaabbb, ...}. The lan-
guage consists of the set of words which are formed by a string of a’s followed
by an equally long string of b’s. There are many different grammars which
could be used to generate this language. One would be:
X ; ab
X ; aXb.
The starting word is X. A typical derivation would be:
X ⇒ aXb ⇒ aaXbb ⇒ aaaXbbb ⇒ aaaabbbb
Note that the string on the end of the derivation is terminal and is in
the language L.
Try to prove that the language generated by this grammar is exactly L.
Note that there would be two parts to such a proof. We would want to show
that every terminal string generated by the grammar is in L; and we would
also want to show that every string in L could be generated by this grammar.
So even at this simple level there is a completeness and correctness problem.
2.
14 CHAPTER 2. STRING REWRITING SYSTEMS
Problem 2.6 Give a grammar for the language which consists of strings
of b’s separated by single a’s.
1. (xy)
2. (x(yz))
3. ((xy)z)
4. (λx.(xz))
2.3. PROBLEMS (WHICH MAY BE DISCUSSED IN TUTORIALS) 15
5. ((λx.x)(λy.y))
6. ((x(λy.(yy)))z)
2.4 Applications
Programming languages are formal languages. For example, the syntacticly
correct programs of C constitute a formal language. It is quite important to
have a compact and unambiguous description of a programming language,
and formal grammars give us exactly that.
Formal languages are also used as a language for specification of pro-
grams. This raises the possibility that part of the process of development,
from specification to working program, could be formalised and automated.
Formal languages are also used in the foundations of mathematics. Part
of the process of formalisation of a field of mathematics is the development
of an appropriate formal language.
Some people think also that natural human languages have some com-
mon formal core. The idea is that there is something like a formal language
which is common to humanity, and that natural languages are obtained
from this by a process of transformation.
2.6 Summary
In string rewriting systems, the data structures are strings of symbols, and
the transformations are string rewriting rules.
Given a string rewriting system P , we will say A ⇒P B if string B can
be obtained from string A by one application of the rules in P . We will say
A ⇒∗ B if strong B can be obtained from string A by a sequence (possibly
of length zero) of applications of the rules.
From an initial word I, a language is generated within a string rewriting
system P . The language is
{W : I ⇒P W, W/terminal}.
The string rewriting system together with the initial word is called a
grammar for the language.
Chapter 3
Term languages,
substitution and
unification
17
18CHAPTER 3. TERM LANGUAGES, SUBSTITUTION AND UNIFICATION
Example 3.1 The function names sin(X) and cos(X), together with the
variables generates a term language. One of the terms in this language is
sin(sin(cos(sin(cos(sin(Y ag)))))). This term is meant to stand for some
function of the variable Y ag. If we give sin and cos their usual interpreta-
tions, this term is determined as a real valued function of a real variable.
But sin and cos could be interpreted in other ways, and the term language is
just the set of terms, as syntactic things in themselves, without any special
assumptions about the interpretation of the function symbols.
Example 3.2 Suppose f (X, Y ), and s(X) are function symbols. The
term language generated by these function symbols consists of all terms
which can be written using any variables, and these two function symbols
and composition. So, for example f (s(s(W )), f (U, f (B, s(W )))) is a term
in this term language. Note that the terms are syntactic things, strings of
symbols. We have not yet decided what these terms mean, if anything. Note
also that we do have a strict notion of syntactic correctness for these terms,
even without being sure what they stand for. For example f (Z, W ( must be
3.3. INTERPRETATION OF A TERM LANGUAGE 19
Example 3.3 Suppose we take signature [+, ∗,0 , 0] with arities 2,2,1,0.
An example of a term is (X +(Y ∗X)00 ). Another term is 000000000000000000000000000000000 .
Example 3.5 Consider signature [(cons X Y ), (car X), (cdr X), nil] with
arities 2, 1, 1, 0. One of the terms in this language is
(cons (cdr X) (cons Y (cons Y Z))).
Note that no predefined meaning is attached to these function symbols.
In fact this term language is essentially the same as the term language
in the previous example, even though the function names and the format
conventions are dissimilar.
Problem 3.1 Write down three different derivations of the term above in
the grammar of its term language.
It should be clear that although there are different ways of deriving this
term, all the derivations are essentially the same. We could try to make
this clear by combining together several parallel steps:
< T >≡> f (< T >, < T >) ≡> f (g(< T >, f (< T >, < T >))) ≡>
f (g(X), f (Y, g(< T >))) ≡> f (g(X), f (Y, g(X))).
We may also display the derivation as a tree:
f
/\
/ \
3.4. PARSE TREES 21
g f
/ /\
/ / \
X Y g
\
X
These trees are called parse or expression trees. We can think of them
as growing downwards from the initial string.
+
/\
/ \
*3 +
/ /\
/ / \
22CHAPTER 3. TERM LANGUAGES, SUBSTITUTION AND UNIFICATION
2 3 *3
\
2
3.5 Substitution
Substitution of terms for variables inside other terms is a very natural
operation. If N and M are terms in a term language and x is a variable, we
will use the notation N [x := M ] to mean the result of substituting M for all
occurrences of x in N . We can also do several substitutions simultaneously.
N [x1 := M1 , ..., xn := Mn ]
means the result of simultaneously replacing all occurrences of x1 ,...,
xn by terms M1 ,..., Mn in term N .
Problem 3.2 Suppose A, B, C are terms. Which of the following are cor-
rect?
2. A[x := B, y := C] = A[y := C, x := B]
A new formal system has popped up here. The data structures are
terms. The transformations are the substitutions. It seems that everybody
who studies mathematics or computing already knows about substitutions.
Are there any hard mathematical problems in this area?
3.6 Valuations
Suppose A is a term language with signature (f1 , ..., fk ). Let D, f1 , ..., fk )
be an interpretation of A. A valuation from D is a function which gives
values in D to a subset of the variables of A. If X1 , ..., Xn are variables in
A, and a1 , ..., an are objects in D, we will use the notation
[X1 := a1 , ..., Xn := an ]
for the valuation which gives value a1 to variable X1 , ... and value an
to variable Xn .
The support of a valuation is the set of variables which are given values
by it.
Let τ be a term of A and set v be a valuation whose support includes
all the variables which occur in A.
We will use the notation
τ (v)
for the value of τ in D which results from setting the values according to
v, and using the given interpretation of the function symbols. If the support
of v does not include all the variables of τ , then τ (v) will be undefined. In
general, given an interpretation, terms denote partially defined functions of
valuations.
down names for all the objects in the domain. Of course, we can write
down names for some of the objects, for example
exp(1) − exp(3 + exp(2)), or exp(exp(π)).
Unfortunately, in the present state of mathematics, we do not know how
to decide whether or not any two given names of this kind denote the same
real number.
We expect that substitution and evaluation will interact in a nice way.
Problem 3.4 Let τ be a term and α a substitution, and v a valuation
whose support includes all the variables which occur in τ or in α. We can
first substitute, and then evaluate. This gives us
τ α(v)
Show that there exists a valuation w which, applied directly to τ , gives
the same result.
Definition 3.5 Let A be a term language and I = (D, f1 , ..., fk ) an inter-
pretation of A. If τ1 and τ2 are two terms of A, we will write
|=I τ1 = τ2
if
τ1 (v) = τ2 (v).
for all valuations v from I whose support includes all the variables of τ1
and τ2 .
|=I τ1 = τ2 will be read: Under interpretation I, terms τ1 and τ2 are
semantically equal. For example, under the usual interpretation of multi-
plication over the reals, X ∗ Y and Y ∗ X are semantically equal. Of course,
X ∗ Y and Y ∗ X are not syntactically equal. We can find an interpretation
in which X ∗ Y and Y ∗ X have different meanings.
A given interpretation induces a notion of semantic equality (in that
interpretation) on a term language.
Problem 3.5 Show that for any term language, A, there exists an inter-
pretation, I in which the notion of semantic equality in I is the same as
literal syntactic identity in A.
Of course we can decide syntactic equality in a term language. Two
terms are syntactically the same only when they are identical. But some-
times semantic equality is difficult to recognise. For example, in the term
language E mentioned earlier, interpreted in the reals in the natural way,
semantic equality is undecidable. That is, there can be no algorithm to
decide whether or not two terms of E, interpreted over the reals, have the
same meaning.
There are even difficulties, as mentioned earlier, in deciding semantic
equality of constants standing for real numbers.
3.7. PROPERTIES OF TERM LANGUAGES 25
Problem 3.6 Assume sqrt(x) means the square root of x, defined for non
negative reals.
What should be the value of x after the following?
Problem 3.7 How could a computer correctly solve the previous problem?
3.8 Unification
Definition 3.6 Suppose N and M are terms and σ is a substitution. We
will say that σ unifies M and N if N σ = M σ.
We are already starting to use the concept of most general unifier, but
we do not yet have a definition of it. We will have to return to this question
later. We do need to clarify it. You may be able to do this yourself at this
point if you think very hard about it. A unifier is a substitution which
unifies two terms. What is a most general unifier?
There is another problem here. Given two terms, how do we decide
whether or not they have a unifier, or a most general unifier? If there is
such, how can we find it? We will also return to this later.
For the moment, here is a useful image. Suppose we have two terms,
and we are looking for a unifier. A good way to begin is to superimpose one
parse tree over the other. If they contradict each other, there is no unifier.
If they do not contradict each other, we should be able to see the minimal
substitutions which will make the two trees the same.
2. W
3. f (W, f (g(U ), V ))
4. g(T )
3.9. THE UNIFICATION ALGORITHM 27
5. f (Y, Z)
The first two are unified with the substitution [W := f (g(X), f (Y, Z)).
The first and the third can also be unified. To see this, superimpose the parse
trees. The f matches at the top. So now we need to unify W and g(X)
to get the left hand subtree, and having done that we will need to unify the
right hand subtrees. You can see now why we need a most general unifier.
We wish to unify and also leave ourselves as much room to manoeuvre as
possible. (We still do not have a definition!) But you can see what to do,
namely [W := g(X)]. Fortunately there are no W occurrences in the right
hand subtree. We need now to unify f (Y, Z) and f (g(U ), V ). Once again,
the f at the top matches. Possibly you can finish this part of the example
yourself ?
Note that the attempt at unification fails if you get non matching func-
tion names at the top of the parse tree, as in items 4 and 5 above. There
is another way in which unification can fail. Consider terms 1 and 5. We
get [Y := g(X)]. We are then confronted with the alarming problem of try-
ing to unify Z and f (g(X), Z). Since terms are finite, we observe that no
term can ever be a proper subterm of itself; so there is no way that Z and
f (g(X), Z) can be the same.
Example 3.10 Suppose we wish to unify f (X, g(U, h(U, s), U )) and
f (g(U, V, s), g(W, Y, t))
We see that X must be the same as g(U, V, s), U ), and g(U, h(U, s), U )
must be the same as g(W, Y, t). Thus W and U must be the same, and Y
must be h(U, s), and U must be t. So we end up with
α = (X := g(t, V, s), Y := h(U, s), W := t, U := t)
to unify two individual terms, we will just put these two terms in two lists
of length one and apply the algorithm to this pair of lists.
The algorithm will be described recursively.
Basis. This is a collection of simple cases.
1. (Simple Case 1)
unif y((S1 , ..., Sn ), (T1 , ..., Tm )) = F AIL
if n 6= m,( since, of course, a substitution can not alter the number
of terms in a list).
2. (Simple Case 2)
Another easy case occurs when (S1 , ..., Sn ) and (T1 , ...Tm ) are already
equal. In this case no unifier is necessary. We let
unif y((S1 , ..., Sn ), (S1 , ..., Sn )) = [], the empty substitution.
3. (Simple Case 3)
Another simple case occurs when n = m = 1, and one of S1 or T1 is
a variable, say X.
We can assume unif y((S1 ), (T1 )) = unif y((T1 ), (S1 )), so swapping
of arguments is allowed.
Swap arguments if necessary to that S1 = X. We have already dealt
with the earlier simple cases, so we can assume that T1 is not X. We
check to see if X occurs inside T1 . If it does than no unification is
possible, since, for any substitution α, (S1 )α is strictly smaller than
(T1 )α . In this case,
unif y((S1 ), (T1 )) = F AIL.
The other possibility is that X does not occur in T1 . In this case
unif y((S1 ), (T1 )) = [X := T1 ].
Recursive Step .
There are several cases in the recursive step.
1. (Recursive Case 1)
First suppose that n = m and n and m are greater than one, and
none of the simple cases apply.
We are trying to unify
(S1 , ..., Sn )
and
3.9. THE UNIFICATION ALGORITHM 29
(T1 , ..., Tn )
We first find unif y((S1 ), (T1 )). If this fails, then unif y((S1 , ..., Sn ), (T1 , ..., Tn ))
also fails. Suppose, however that unification of S1 and T1 succeeds,
with most general unifier α. Thus (S1 )α = (T1 )α.
We apply α to the rest of the list and continue recursively. That is,
we find
2. (Recursive Case 2)
Finally, suppose n = m = 1, and none of the earlier cases apply. In
this case, we want to unify S1 and T1 . We can suppose that neither
is a variable, since this case was dealt with earlier, and we can also
suppose that they are not already equal. If one of them is a constant,
then return F AIL. Suppose that neither is a constant.
It must happen that both S1 and T1 are compound terms obtained
by application of a function symbol to argument lists.
S1 = f (A1 , ..., Aj )
T1 = g(B1 , ..., Bk )
for some function symbols f and g.
If f and g are not the same, then unification fails, and we return
F AIL. If f and g are the same, then we call the unification algorithm
recursively on the argument lists. That is,
unif y((S1 ), (T1 )) = unif y((A1 , ..., Aj ), (B1 , ..., Bk ))
2
It is claimed that:
Theorem
1) The unification algorithm, as given above, always eventually termi-
nates.
2) If the algorithm terminates with a unifier this unifier is a most general
unifier.
3) If the algorithm terminates and returns FAIL, then there is no unifier.
30CHAPTER 3. TERM LANGUAGES, SUBSTITUTION AND UNIFICATION
It is not obvious that any of these claims are true. The reader is invited
to try the algorithm on a few cases to see if it works. This may or may not
inspire confidence.
Exercise. Try to prove the first claim above, that the algorithm always
terminates. We can start to think about this by a dialogue between a
defender and a critic of the algorithm. Defender: the algorithm always
terminates. Critic: No it does not. Defender: OK, then give me a simplest
problem in which it does not terminate. Critic: Define simplest. Defender:
Minimum number of variables, and subject to that the minimum length.
Now, whatever the critic responds, the defender has a strategy to win the
argument. Consider each case in turn, using the assumed minimality of the
critic’s response....
The proofs of the other two parts can be done in the same way. For
example, for part 3) the critic would have to claim that there was a case
in which the algorithm did terminate and returned fail, but that there was
a unifier. The defender makes the critic give a minimal explicit case, and
then the defender demolishes the critic. Advice to the reader: you ought
to think about this proof. You ought to be able to prove the correctness of
the algorithm in specific problems.
Problem 3.9 Give a grammar for the smallest term language which con-
tains all the usual variables, and also 0 and 1, and also +, -, *, written in
the usual way, with arity 2. In which cases can you leave off some brackets
without ambiguity? Give the parse tree for ((X + W ) ∗ (Y − (Z ∗ (X + 1)))).
Suppose X, Y, Z, W have values 2,3,4,5 respectively; use this to attach a
value to every node of the parse tree.
3.11 Applications
Unification is an essential part of prolog, which will be discussed later.
3.12. SUMMARY 31
3.12 Summary
Term languages are the sets of expressions which can be built up from an
initial signature of function names, and a set of variables. Within program-
ming languages, term languages occur as sets of expressions. An interpreta-
tion of a term language is given by giving a domain, D, and by interpreting
each function name as a function defined over that domain. For the sake of
simplicity, we are, at this stage, assuming that the values returned by our
functions are also in the domain D. The domain may be a set, such as the
real numbers, which is important in applications, but for which we do not
have an adequate notation.
Each expression in a term language has a unique parse tree. Given an
interpretation, terms in a term language can be evaluated by working up
the associated parse trees.
If I is an interpretation and τ1 and τ2 are terms |=I τ1 = τ2 means that τ1
and τ2 have the same meaning (or are semantically equal) in interpretation
I.
We have defined substitution as an operation on terms.
We have defined a unifier of two terms to be a substitution which makes
them the same. A unifier α of two terms A and B is a most general unifier
if any unifier of A and B can be obtained as α followed by some other
substitution.
The unification algorithm, given two terms A and B, either produces a
most general unifier for A and B, or, in case no unifier exists, fails.
32CHAPTER 3. TERM LANGUAGES, SUBSTITUTION AND UNIFICATION
Chapter 4
Predicate Logic
4.1 Introduction
In English and other human natural languages complex sentences and prop-
erties are built up by combining simpler ones using certain logical operators.
There are surprisingly few of these operators. On the other hand, they are
quite ambiguous in natural speech.
In order to begin formalisation of reasoning, we consider, in this chapter,
formal versions of the logical operators.
33
34 CHAPTER 4. PREDICATE LOGIC
red(X), meaning that X is red, has arity 1; and between(X, Y, Z), meaning
that X is between Y and Z, has arity 3.
There are a number of formats for writing predicates. We will often put
the predicate name first and follow it by the arguments enclosed in brackets,
as in uncle(X, Y ). This is called prefix format. On the other hand, many
commonly occurring predicates use other formats. For example
X = Y , meaning that X and Y are the same
X < Y , meaning that X is less than Y .
Predicates of arity 1, such as even(X), are sometimes called properties.
Predicates can be combined together using logical operators. We will
consider the following list of operators:
In the following, we will assume that you know the truth tables for
∧, ∨, ¬, →, ↔, and that you have some working familiarity with the quan-
tifiers ∀, and ∃. If you are not happy with this, you should revise.
Note especially that the truth table for p → q is the same as the truth
table for ¬p ∨ q. This is somewhat counterintuitive. (In this case Occam’s
razor has been given priority over naturalness.) We do assume that the
truth value of a compound expression should be determined from the truth
values of its constituents, without knowing anything else about possible en-
tanglement of meaning. In other words we assume referential transparency
for the logical operators. Since some false propositions imply true ones,
we have to agree that (F → T ) evaluates as T ; and similarly (F → F )
evaluates at T . Continuing with this, we get p → q evaluates as true when
p is false, or when q is true.
these types languages. However, in order to try to keep the notation fairly
simple, we will in this chapter assume that all variables have the same type,
which will not be declared. Each variable X ranges over some fixed domain
D. The domains are the same for all the variables.
Example 4.1 Anyone who loves Jane is brave. There seem to be two pred-
icates at work here loves(X, Y ), which says that X loves Y , and brave(X),
which says that X is brave. We want to say that if X loves Jane, no matter
who or what X is, then X must be brave.
(∀X)(loves(X, jane) → brave(X))
Note that the variable X is not given a type. It just ranges over some
universe. All we know about jane is that jane is also in the same universe.
Suppose now that we are given a list of function symbols and a list
of predicate symbols. As before the function symbols will generate a term
language. We can write down predicates by applying the predicate symbols
to the terms in the term language. These are called atomic formulae, since
there is no way to break them down logically. The atomic formulae can
then be combined together, using the logical operators. The resulting set of
expressions are called formulae. A first order language is the set of formulae
generated from a given list of function symbols and list of predicate symbols.
In first order languages, all the variables have the same type. In the
interpretations of these languages there is only one universal domain, over
which all variables range. Later on we will see more complicated languages:
multisorted languages in which there are several different types of variable,
and higher order languages in which we have variables for sets or functions
as well as for objects.
The lists of function symbols and predicate symbols which specify a first
order language is called its signature.
The term language always includes the variables. We can therefore allow
the list of function symbols to be empty, since we still get infinitely many
terms. But we can not use an empty predicate symbol list, since in that
case we would not get any atomic formulae or formulae. Since equality
is used so often in mathematics and computing, we will assume that the
predicate symbol list always has = in it.
An example of a first order language is LZF , given earlier. In this case,
the term language is just the set of variables. The list of predicate symbols
is [=, ∈], both with arity 2.
In any first order language, the formulae are generated from the atomic
formulae in the same way, shown in the following grammar.
< S >; (< S > ∧ < S >) | (< S > ∨ < S >) | (¬ < S >) |
(< S >→< S >) | (< S >↔< S >) | (∀ < V ariable >) < S >| (∃ <
V ariable >< S >|< Atomicf ormula >
Example 4.3 LN , the first order language of arithmetic, has the signature
with function symbols [+, ∗,0 , 0], and predicate symbol [=]. Function symbols
+ and ∗ are written in the usual way, so that for example, ((X +Y )∗(X ∗Z))
is a term. The intended meaning of X 0 is X + 1, the successor of X. The
arity zero predicate symbol, 0, is intended to mean zero.
An example of an atomic formula would be
(000 ∗ X) = Y .
An example of a formula would be
(∃Y )(000 ∗ Y = X).
Under the usual interpretation, this means that X is even.
4.6. SYNTAX, SUBSTITUTION 37
Example 4.5 LG , the first order language of group theory, has term lan-
guage generated by the usual variables, 1 (meant to be the identity), X ◦ Y ,
meant to be the group operation, and X −1 meant to be the group inverse.
The only predicate symbol needed in LG is =, meant to be equality.
However, in an interpretation of LG , ◦ can be any operation : D2 →
D, and X −1 can be any unary operation. Even though we want to use
LG to talk about groups, we do not impose any of our ideas on the set of
allowable interpretations. The groups are the interpretations which satisfy
the axioms.
We do not even insist that = be interpreted as the usual equality. This
means that in this context when we give the group axioms we need to include
some statements which characterise equality, as follows.
1. X = X
2. X = Y → Y = X
3. (X = Y ∧ Y = Z) → X = Z
4. X = Y → X −1 = Y −1
5. (X = Y ∧ Z = W ) → X ◦ Z = Y ◦ W
6. X ◦ (Y ◦ Z) = (X ◦ Y ) ◦ Z
7. X ◦ 1 = X ∧ 1 ◦ X = X
8. X ◦ X −1 = 1 ∧ X −1 ◦ X = 1
4.8. SEMANTICS FOR FIRST ORDER LANGUAGES 39
Γ |= S
if S is true in every model of Γ. In this case we will also say that S is
a logical consequence of Γ.
4.9. EXAMPLES : LN AND LR 41
1. X 0 = Y 0 → X = Y
2. ¬(0 = X 0 )
3. ¬(X = 0) → (∃Y )(X = Y 0 )
4. X + 0 = X
5. (X + Y 0 ) = (X + Y )0
6. X ∗ 0 = 0
7. X ∗ Y 0 = X ∗ Y + X
1. X = X
2. X = Y → Y = X
42 CHAPTER 4. PREDICATE LOGIC
3. X = Y ∧ Y = Z → X = Z
4. X = Y → X 0 = Y 0
5. X = Y ∧ Z = W → (X + Z = Y + W ∧ X ∗ Z = Y ∗ W )
A(w) = F for some valuation w. But this can not happen if (∀X)A is
true in I.
Obviously our expectations are wrong here. It seems that some substi-
tutions followed by evaluation are not evaluations. Such substitutions are
a common source of error in mathematics and computing.
The problem is always caused by the fact that a variable in the substi-
tuted term is quantified inside the formula, and accidentally falls into the
scope of the quantifier after substitution.
In this course we will work on the simplest case of first order languages
and classical logic.
Next let the tutor change M and S and repeat the game.
So
Vn a formula
Wmi in CNF has the form
i=1 j=1 A ij , where each Aij is a literal.
That is, a formula in CNF all the ands on the outside, and all the
negations applying directly to atomic formulae.
On
Wn theVmother hand a formula in DNF has the form
i
i=1 j=1 A ij , where each AIj is a literal.
Theorem 4.1 Let S be any quantifier free formula. We can find a formula
in CNF which is logically equivalent to S. We can also find a formula in
DNF which is logically equivalent to S.
proof.
We will give an algorithm for this.
So return DN F (S) as
W
happens(Ri )
Vn write happens(Ri )
and
j=1 Bij
where Bij = Ai if Ri gives Aj value True, and Bij = (¬Ai ) if Ri gives
Aj value False.
2
The CNF algorithm does this the other way around.
Note that the method given above can not be called practical, since the
amount of work to be done increases with the number of rows of the truth
table and this increases exponentially with the number of atomic formulae
in the problem.
There are many other ways of finding CNF and DNF, which are not
unique. None of the other known methods are practical, i.e. have complex-
ity bounded by some polynomial in the size of the input.
Exercise: Write specifications for the DNF and the CNF algorithms.
What does it mean to say that the algorithms are correct with respect to the
specifications? What does it mean to say that the algorithms are complete
with respect to the specifications? Do the algorithms always terminate?
48 CHAPTER 4. PREDICATE LOGIC
3. Rename all bound variables so that no variable occurs both bound and
free in any subformula, and so that no two occurrences of the same
variable are in the scopes of different quantifiers.
It is fairly easy to see how to find a Skolem form for a formula in prenex
normal form. Suppose our formula is:
(∀X1 )(∀X2 )...(∀Xn )(∃Y )M (W1 , ..., Wk , X1 , ..., Xn , Y )
where M (W1 , ..., Wk , X1 , ..., Xn , Y ) is another formula in prenex normal
form, having free variables W1 , ..., Wk , X1 , ..., Xn , Y . Suppose we wish to
introduce a Skolem function to get rid of the (∃Y ).
The value Y which is asserted to exist by the original formula may
depend on W1 , ..., Wk , X1 , ..., Xn . We pick a new function symbol for our
Skolem function. Not trying to be very original here, suppose we choose
f . It will have arity k + n, where k is the number of free variables in the
original formula and n is the number of universal quantifiers to the left of
(∃Y ). Replacing this existential quantifier, we get:
(∀X1 )(∀X2 )...(∀Xn )M (W1 , ..., Wk , X1 , ..., Xn , f (W1 , ..., Wk , X1 , ..., Xn ))
Inside M there may remain more existential quantifiers. So this process
of inventing new function symbols and replacing existential quantifiers is
repeated until we get Skolem form.
Skolem form is not unique, since we can use any new function symbol
we choose. It is essential to use a function symbol which has not been used
previously.
The Skolem form of a formula logically implies the original formula.
However the converse is false. The original formula does not imply the
Skolem formula. The Skolem form is slightly stronger. So we have lost
logical equivalence. On the other hand, the difference is small.
Suppose we have an interpretation, I, in which the original formula is
true. The Skolem form can not even be interpreted in I as it stands, since
we have used new function symbols. But since the original formula is true,
4.14. NORMAL FORMS 51
we can extend I, defining the Skolem functions so that the Skolem form is
true.
This method only makes sense for sentences. That is, we have to start
with A which has no free variables.
We end up with a list of quantifier free clauses. Except for the fact
that we may have introduced new function symbols, the clausal form has
the same meaning as A. On the other hand, the clausal form is often a lot
easier to understand than the original.
The clausal form process may be regarded as a simplification procedure.
4.15 Problems
Problem 4.2 (6) Put the following statements into conjunctive normal
form, and also disjunctive normal form. Check that each statement has the
same truth table as its conjunctive normal form.
a) (¬a ∧ b) → ¬b)
b) (a ∧ ((¬p) ↔ ¬q))
Problem 4.4 (45) Translate the following statements into some first order
language. Note that in e) and h) you may need to use equality.
a) Not all toothless animals which have feathers can fly.
b) If some human being is in prison than all human beings are in prison.
c) It is not true that every short person who is not crazy likes some
other person who is either not short or crazy.
d) Gorillas are hairier than billiard balls.
e) Everyone is either loved or hated by some other person.
f ) There is a town in Spain in which there is a barber who shaves ev-
eryone in the town who does not shave themselves.
g) Everyone is happy except for Jane.
h) There is only one thing with wheels in the shed and that is a bicycle
with a flat tire.
i) For every X and Y there is a Z so that X ≤ Z and Y ≤ Z, and if
W is such that X ≤ W and Y ≤ W then Z ≤ W .
note: such a Z is called the least upper bound of X and Y . Can you see
how to establish that least upper bounds are unique?
Problem 4.5 (5) a) Repeat the first two parts of previous problem (i.e.
the statements about feathered animals and prisoners) without using any
existential quantifier.
b) (5) Repeat the first two parts of the previous problem without using
any universal quantifier.
4.16 Summary
In this chapter we have defined first order languages and given them a
semantics. We have defined the notions of logical consequence and logical
equivalence.
We have also developed a number of normal forms for formulae in first
order languages: conjunctive normal form, disjunctive normal form, prenex
normal form, Skolem form, and clausal form.
The most valuable and interesting of the normal forms is clausal form.
The clausal form of a sentence is a list of clauses, each clause being in the
form:
V W
Ai → Bi
where each Ai and Bi is an atomic formula. Thus clausal form uses no
quantifiers, and seems not to use negation. The crucial step in the reduction
of a sentence to clausal form is replacement of existential quantifiers by use
of Skolem functions.
The clausal form of a sentence is weakly equivalent to the original sen-
tence in this sense: Any interpretation which makes the clausal form true
also makes the original sentence true. Any interpretation which makes the
original sentence true can be extended, by definition of the Skolem func-
tions, to an interpretation which makes the clausal form true. In particular,
the original sentence can be satisfied if and only if the clausal form can be
satisfied.
4.17. REVISION PROBLEMS. CLAUSAL FORM 55
Problem 4.8 Every dog, except for Hugo, is frightened of some cat. But
Hugo is not frightened of any cat.
Problem 4.9 If you can get from A to B by rail and you can get from B
to C by rail, then you can get from A to C by rail, except on Wednesday.
Problem 4.11 Every bar stool has a person sitting on it, and every such
person has a mother, alive or dead.
Problem 4.13 If A(0) is true and if A(x) implies A(x + 1) for all x, then
A(x) is true for all x.
4.18 Solutions
1. m(X) ∧ is(X) →
2. d(X) → X = h ∨ c(γ(X))
d(X) → X = h ∨ f right(X, γ(X))
3. r(A, B) ∧ r(B, C) → wednesday ∨ r(A, C)
4. male(X) ∧ likes(j, X) → crazy(X)
5. barstool(B) → human(α(B))
barstool(B) → mother(µ(B), α(B))
barstool(B) → d(µ(B)) ∨ al(µ(B))
56 CHAPTER 4. PREDICATE LOGIC
6. → Tα = Sα
Tβ = Sβ → Tα◦δ(α,β) = Tβ
(a) If some of Al’s chickens have mange, then all of Al’s chickens
have mange.
(∃X)(a(X)∧ch(X)∧mg(X)) → (∀X)((a(X)∧ch(X)) → mg(X))
(b) Some people like other people who do not like anyone.
(∃X)(p(X)∧(∃Y )(p(Y )∧likes(X, Y )∧(∀Z)(p(Z) → ¬likes(Y, Z))))
(c) George must be eliminated unless Alice only saw one kangaroo.
I translate “A unless B” as “A or B”.
elim(g) ∨ ((∃X)(saw(a, X) ∧ kang(X)) ∧ (∀Y )(∀W )(kang(Y ) ∧
kang(W ) ∧ saw(a, Y ) ∧ saw(a, W )) → Y = W )
2. Put the statements of the previous section into clausal form. (30)
Prenex normal form is
(∀X)(∀Y )((a(X) ∧ ch(X) ∧ mg(X) ∧ a(Y ) ∧ ch(Y )) → mg(Y ))
a)
Clausal form is
(a(X) ∧ ch(X) ∧ mg(X) ∧ a(Y ) ∧ ch(Y )) → mg(Y )
b) Clausal form is
→ p(τ )
4.19. EXAMPLES AND SOLUTIONS 57
→ p(ρ)
→ likes(τ, ρ)
p(Z) ∧ likes(ρ, Z) →
c) Clausal form is
→ elim(g) ∨ saw(a, τ )
→ elim(g) ∨ kang(τ )
saw(a, X) ∧ saw(a, W ) ∧ kang(X) ∧ kang(W ) → elim(g) ∨ X = W
Semantic Tableaux
Assume that we have a set of axioms Γ, written in some first order language,
for some field of knowledge. Imagine that a lot of work and experience has
gone into Γ. We believe that Γ summarises every known truth in some area
which concerns us.
The nice feature of this situation is that Γ may be quite small. It is a
compact representation for all of its logical consequences. We believe that
the information we want is in Γ. But how do we get this information?
It becomes clear that we need to be able to answer the following ques-
tion.
Given Γ, and formula A, decide whether or not Γ |= A.
This is called the logical consequence problem, for the predicate calculus.
Of course this may be hard. As if it were not hard enough, another
problem immediately suggests itself.
Given formula A(X1 , ..., Xn ) with free variables X1 , ..., Xn , find,
if possible, values v1 , ..., vn so that Γ |= A(v1 , ..., vn ).
If we are to make some progress with these problems computationally,
we need some formalisation of the concept of proof.
There are a number of formal systems for deduction in predicate logic.
One technique is called forward chaining. A forward chaining system is
given by a certain number of rules of inference. Sometimes, for example,
such systems include the following rule, which is called modus ponens:
———————————-
A, (A → B)
————————-
B
———————————-
59
60 CHAPTER 5. SEMANTIC TABLEAUX
This says that if we have proved A and also (A → B), we may conclude
B.
In a forward chaining system, we start with the axioms Γ and apply the
rules of inference. We regard all the formulae in Γ as true, and every time
we apply a rule of inference we enlarge the set of known truths. We will
say Γ ` A if we can eventually derive A from Γ using the rules of inference.
If we think that A really is a logical consequence of Γ, we must try to get
from Γ to A using the rules of inference.
There are several severe difficulties here. Even if we know that Γ |= A,
it may be extremely difficult to find the right way to apply the rules of
inference to get from Γ to A. If we are not confident that A is a logical
consequence of Γ, our problem is even worse. A forward chaining system
will never establish that A is not a logical consequence of Γ.
Backward chaining systems are based on the method of proof by con-
tradiction. We begin by assuming that A is not a logical consequence of Γ
and proceed to attempt to construct a counterexample. (A counterexample
would be an interpretation in which Γ is true but A is false.) If our attempt
to construct a counterexample is eventually blocked by contradictions, we
conclude that there is no counterexample, and thus our original assumption
was incorrect, and Γ |= A. On the other hand, if some branch of the con-
struction never gets blocked, the construction, in the limit, should produce
a counterexample, and in this case A is not a logical consequence of Γ.
As mentioned above there are a large number of formalisations of proof
in predicate logic. The semantic tableaux method is one of these. This is
a backward chaining system.
5.1 Introduction
We should sort out the notation first. Γ |= A means that A is a logical
consequence of Γ. As explained previously, this means that if I is any
interpretation which makes Γ true, then I must make A true. So Γ |= A is
about semantics. It means that in reality A is true whenever Γ is true.
On the other hand, we will have a syntactic definition of proof.
Γ`S
if we can prove S from Γ in the semantic tableaux system.
Note that at this stage we are only dealing with sentences. So we will
need to replace our axioms by their universal closure.
5.1. INTRODUCTION 61
(4, from 3) q
|
(5, from 3) not p , contradicts (2)
(Note that in these examples “not p” is used instead of ¬p, purely for
the convenience of the typist. In later semantic tableaux, “(For all X)” and
“(Exists X)” will be used instead of (∀X) and (∃X) for the same reason.)
Here is another example, in which there is some branching.
(1) not((p->q)->q)
|
(2, from 1) (p ->q)
|
(3, from 1) not q
/ \
/ \
Please look at the tree below node 3). It is supposed to say that either p
is false or q is true.
We now have a contradiction on one branch. There is no contradiction
on the other branch. There is also nothing more we can do. So the con-
struction on the left hand branch has ended in a counterexample. Reading
down the left hand branch, we can see that p and q are both false. (Check
that this is a counterexample.)
In a tree, the node at the top is called the root. The node or nodes
immediately below a node are called its children. A node with no children
is called a leaf. The collection of leaves is called the frontier of the tree. A
path which starts at the root and goes all the way to the frontier will be
called a branch. If a node is labelled with formula A, we will say that A is
asserted at the node. If a node is labelled with ¬A, we will say that A is
denied at the node.
In a semantic tableau, we will say that a branch is closed if it contains
a contradiction. This means that some formula is both asserted and denied
on the branch. The tableau is closed if all branches are closed.
Suppose we wish to try to decide whether or not Γ |= A. Assume that
A is a sentence, and all the formulae in Γ are also sentences. The semantic
tableau method is the following. Form an initial semantic tableau with all
the formulae of Γ asserted, and A denied. Then apply the semantic tableau
rules, which will be described below. If a closed tableau is eventually ob-
tained, it follows that no counterexample is possible, and so A is a logical
consequence of Γ. The closed tableau is a proof of A from axioms Γ.
10. If (∀X)A(X) is asserted, and if t is any variable free term, assert A(t).
This may be done for any number of variable free terms t.
11. If (∀X)A(X) is denied invent a new Skolem constant c, and deny A(c)
.
13. If (∃X)A(X) is denied, and if t is any variable free term, deny A(t).
This may be done fro any number of variable free terms t.
Rules 10) and 13) are called substitution rules. They may be applied any
number of times. Some proofs need many substitutions. Note that only
variable free terms may be substituted. So, for example, in arithmetic,
(0000000 + 000 ) may be substituted, but (X + 00 ) may not be substituted. This
restriction ensures that all the formulae in a semantic tableau are sentences.
Variable free terms may involve arbitrarily many function symbols and
constants, and may be nested to any depth. Rule 10) says that if we assert
(∀X)A(X) and if t is any variable free term, then we may also assert A(t).
Rule 13) says that if we deny (∃X)A(X) and if t is any variable free term,
then we may also deny A(t).
In rules 11) and 12), a new Skolem constant is introduced. It is impor-
tant to realize that the constant which is introduced must not have been
previously used anywhere in the tree. The constant names an object which
either makes (∃X)A(X) true, or makes (∀X)A(X) false. If we correctly
assert (∃X)A(X), then there must exist an object which makes A(X) true;
we can call such an object by any name we choose, provided only that
this name has not been used previously. Similarly, if we correctly deny
5.2. SEMANTIC TABLEAU RULES 65
(∀X)A(X), it must follow that there is at least one object which makes
A(X) false; and we can invent a new name for such an object.
Note that a denied disjunction behaves like a conjunction of denials.
That is ¬(A∨B) is the same as ¬A∧¬B. Furthermore, a denied conjunction
behaves like a disjunction of denials.
The universal quantifier ∀ is, in a sense, just a big conjunction, and ∃ is,
in a sense, just a big disjunction. So a universal quantifier asserted behaves
like an existential quantifier denied.
Example 5.3 Suppose we want to test whether or not (∀X)(A(X) →
B(X)) logically implies ((∀X)A(X) → (∀X)B(X)). We start by asserting
(∀X)(A(X) → B(X)) and denying ((∀X)A(X) → (∀X)B(X))
/ \
Note that the Skolem constant c does not occur above node 5), where
it was introduced, using rule 13). All branches are closed. So there is no
counterexample. Therefore
(∀X)(A(X) → B(X)) |= (∀X)A(X) → (∀X)B(X). The above tree is a
proof of this in the semantic tableau system.
Definition 5.2 If T1 and T2 are semantic tableaux, we will say T1 ⇒ T2
if T2 can be obtained from T1 by one application of the rules.
66 CHAPTER 5. SEMANTIC TABLEAUX
On each branch, the rules in the first three categories only need to be
applied once. The substitution rules can be applied any number of times
with different terms on any branch. Since we do not want to have a lot
of branches, it seems a good idea to make all applications of rules in the
first two categories, before doing anything else. In general, it also seems
a good idea to do all possible category 3) operations, before doing any
substitutions. Of course there are cases in which this advice is not useful.
Whatever strategy we follow, we do not want indefinitely to defer taking
any allowed operation. Everything which can be done on any open branch
should eventually be done. Unless of course the branch gets closed for
some other reason. Since if there are function symbols in our language,
there may be infinitely many variable free terms, and thus infinitely many
possible substitutions, a perverse user of the semantic tableau system could
continue forever without getting a proof by insisting on doing one useless
substitution after another, even though closure might be obtained in some
other way.
Definition 5.4 We will say that the semantic tableau construction is done
systematically if any operation which is possible on any open branch (in-
cluding all substitutions) is eventually done, unless all branches extending
it get closed first.
5.4. HOW MUCH USE IS THIS SYSTEM? 67
Γ |= S
if and only if
Γ`S
for any list of sentences Γ and any S in any first order language.
5.5 Problems
Problem 5.2 Give a definition for:
term t is free for variable X in formula A(X).
This should be done in such a way that it makes sense to substitute t
for X in A(X).
Problem 5.3 Find a counterexample to the following, or give a proof. In
each case, complete the semantic tableau construction, and state how many
branches there are in the completed tree. How many are closed? Verify that
each branch which is not closed defines a counterexample.
a) (((p → ¬q) → p) → q)
b) ((q → ¬p) → ((p → (¬q))))
c) ((p → q) → ((¬p) → (¬q)))
Problem 5.4 If we know that either A is true or B is true and we know
that either B is true or C is false, and we know that A and C can’t both be
true, then it must happen that A is true. Either give a formal proof of this,
or a counterexample.
5.5. PROBLEMS 71
Problem 5.5 Show whether or not the following set of statements is con-
sistent.
A and B and C implies that D is false unless E is true. E and not B
implies C. A and B are equivalent. C is true.
———————-
Either show that the following statements are logically valid, or provide
a counterexample.
Problem 5.7 Esmerelda is a duck and all ducks like ponds so Esmerelda
likes ponds.
Problem 5.8 If all burglars are barbers and all barbers are bakers then all
burglars are bakers.
Problem 5.9 If all burglars are barbers and no bakers are not barbers then
some bakers are not burglars.
Problem 5.10 If all burglars are barbers and some barbers are bakers and
some baker is not a burglar then some barber is not a burglar.
72 CHAPTER 5. SEMANTIC TABLEAUX
Example 5.4 Γ = {((∀X)(∀Y )(r(X, Y ) → r(Y, X)), (∃X)¬r(X, X), (∀X)(∀Y )(∀Z)((r(X, Y )∧
r(Y, Z)) → r(X, Z))}.
1. ((∀X)a(X) → (∀X)b(X))
Problem 5.12 Construct parse trees for each of the three statements above.
Show the scope of each quantifier.
Problem 5.13 Use semantic tableaux to show that the first statement im-
plies the second.
5.8. SOLUTIONS 73
Problem 5.14 Use semantic tableaux to show that the first statement im-
plies the third.
Problem 5.15 Show that the second statement implies the first.
5.8 Solutions
74 CHAPTER 5. SEMANTIC TABLEAUX
1. To show
((∀X)a(X) → (∀X)b(X)) |= (∃X)(∀Y )(a(X) → b(Y ))
/ \
5.9 Summary
We have developed a complete and sound proof system for predicate logic.
This is based on proof by contradiction. Given a set of axioms Γ and a
sentence A, we attempt systematically to construct a counterexample, i.e.
an interpretation in which A is false although Γ is true.
The data structures in this semantic tableau system are trees labelled
with assertions or denials of formulae. The transformations are rules which
extend the trees.
Chapter 6
75
76CHAPTER 6. OTHER FORMAL DEDUCTIVE SYSTEMS FOR FIRST ORDER LOGIC
two clauses. Note that although the new clause may be longer, it has one
less atomic formula. We start with a pool of clauses in the original clausal
form, and we add to the pool by using resolution. If we eventually get to
the contradictory clause
→
which has empty left and right hand sides, this implies that the original
clausal form was contradictory, and thus not satisfiable.
We may also use substitution on clauses to obtain new clauses. In
order to apply resolution we need pairs of clauses in which some formula
C appears on the left of one and on the right of the other. The unification
algorithm can be used to find substitutions which create these matches.
Resolution and unification can be used together to make a complete and
correct deductive system for clausal forms. This is especially easy to apply
in the special case in which all the clauses have exactly one atomic formula
on the right hand side. This special case is the basis of the programming
language prolog, which will be discussed in detail later.
A sequent is like a clause except that quantifiers and negation are al-
lowed.
A sequent is obviously valid if the same formula appears on both left
and right hand sides.
We will use Γ and ∆ as variables for sets of formulae.
In order to make this system similar to the semantic tableaux system,
we will assume that all the formulae in sequents are sentences. That is, no
free variables are allowed.
The rules of inference will allow us to prove some sequents from others.
We have two rules for negation:
One of these rules is:
Γ ` ∆, A
————————–negation left
¬A, Γ ` ∆
The other rule for negation is:
A, Γ ` ∆
————————negation right
Γ ` ∆, ¬A
Please check that negation left and negation right are correct. Do you
get a strange feeling of familiarity when you look at these? What rules
should we have for implication?
A, Γ ` ∆, B
———————–implication right
Γ ` ∆, A → B
There is also an implication left rule, but it needs two sequents as
premises.
(Γ ` ∆, A), (B, Γ ` ∆)
——————————————————-implication left
A → B, Γ ` ∆
Please check again that these rules are correct.
Perhaps you can see the connection with semantic tableaux. Consider
a branch of a semantic tableau. Let ∆n be a subset of the formulae which
occur on the branch and which begin with negation. Let Γ be the other
formulae which occur on the branch. Obtain ∆ from ∆n by removing the
initial negation. Now write
Γ ` ∆.
78CHAPTER 6. OTHER FORMAL DEDUCTIVE SYSTEMS FOR FIRST ORDER LOGIC
This translates branches into sequents. The set Γ are the statements
asserted on the branch and the ∆ are the statements which are denied. A
branch is impossible to satisfy if and only if the corresponding sequent is
valid.
Notice that a branch is closed if and only if it contains a contradiction
if and only if it translates into an axiom in the sequent calculus.
You can now see: the rules for the sequent calculus are obtained just by
turning the semantic tableau rules upside down. A semantic tableau proof
is just a sequent proof turned upside down. As an exercise, you should
make sure that you can write down all the rules of inference for the sequent
calculus. Look at the quantifiers!
Please note that if you actually want to find a proof of a given sequent in
the sequent calculus, the best way to do this is usually to tackle the problem
backwards using semantic tableaux. Once you get a closed tableau, you turn
this upside down to get a sequent proof.
As an example, we could prove A ` (B → A) in the semantic tableau
system, and then obtain the following two step proof in the sequent calculus:
———–
A, B, ` (B → A), A Axiom
A ` (B → A) implication: right
————————–
We could also have used the simpler axiom:
A, B ` A
A Formalisation of
Mathematics: ZF set
theory
79
80CHAPTER 7. A FORMALISATION OF MATHEMATICS: ZF SET THEORY
6. Ordered pair axiom. For any A and B, there is an ordered pair (A, B),
with first element A and second element B. (A, B) = (C, D) if and
only if A = C and B = D.
12. Axiom of choice: If F is a set whose elements are disjoint non empty
sets, there exists a set choice(F ) which has exactly one element in
common with each element of F .
13. There is an empty set, denoted ∅, which has no elements. Also, there
exists the set of natural numbers N = {0, 1, 2, ...}.
81
The attentive reader may notice that some of the axioms, as given above
can be proved from others. For example, the existence of A ∩ B can be
proved from the axiom of comprehension, since A ∩ B = {X : X ∈ A ∧ X ∈
B}. And in fact the comprehension axiom itself can be proved from the
axiom of replacement. I have stated the axioms with these redundancies
since I thought they might help the reader to see what was happening. The
main sense of the axioms is that we can build up complex sets from simpler
ones using common operations.
The axioms of ZF set theory are written in our special language, LZF .
For example, the axiom which says that for any set A there exists a set,
singleton A, whose only element is A, would be written as follows:
(∀A)(∃B)(∀W )(W ∈ B ↔ W = A)
In order to write the axioms entirely in LZF , rather than in the mixture
of LZF and informal mathematical English used above, we need to define
functions, ordered pairs, and the natural numbers in terms of LZF . That
is to say, we need to express these ideas in terms of set membership and
equality.
The definition of function is especially important.
The rules of deduction are, of course, formal rules, which can be applied
mechanically. The theorems of ZF set theory are the set of formulae which
can be obtained by starting with the axioms and applying the rules of
inference.
The original specification for this formal system was quite amazing. It
was that the theorems of this system should contain no contradictions,
and should contain the translations into LZF of all the currently accepted
theorems of mathematics, together with all the translations into LZF of all
theorems of mathematics which may become accepted in the future. So ZF
set theory is an attempt to characterise the apparently ultimate infinite
object of mathematical reality.
The Occam’s razor principle was applied very seriously by the develop-
ers of ZF set theory. Since every statement in LZF is built up from set
membership and equality, and the logical operations ∧, ∨, ¬, →, ↔, ∀, ∃, all
mathematical ideas must also be constructed from these elements.
In order to get simplicity, some aspects of naturalness were sacrificed.
For example, in order to keep the basic elements minimal, the natural
numbers are built up from the empty set in a way which may seem arbitrary:
0 is ∅, 1 is {O}, 2 is {O, {O}}, and, in general, n + 1 is n ∪ {n}.
So in ZF set theory, The natural numbers is
{O, {O}, {O, {O}}, ...}
which does not seem friendly or necessary. We do lose something if we
adopt a nasty notation, such as this. A good notation encourages correct
and fluent thinking.
Some of these problems of notation can be overcome without much
difficulty, by extending LZF , adding names of important objects and oper-
ations. So we can define 0 to mean ∅, 1 to mean {O}, and so on. Similarly,
without serious difficulty, we can extend the language LZF to include fa-
miliar notations such as {X}, X ∪ Y, X ∩ Y, X ⊆ Y , etc. The point is that
these notations can be unambiguously defined in the original LZF . For
example, we can define: X ⊆ Y to mean (∀W )(W ∈ X → W ∈ Y ).
There is a more serious problem about the definition of functions in ZF
set theory.
The designers of this system restricted themselves to what could be
said with set membership, equality, and the logical operators. Once this
restriction had been accepted, they had to explain the notion of function
in these terms.
The problem with defining a function as a kind of set is that, intuitively,
functions are active and dynamic, and sets are static. However Occam’s
razor was given priority over intuition in this case. Consequently, a function
in ZF set theory is defined to be a set of ordered pairs, corresponding to
the graph of the intuitive function, as described above.
83
Gödel Incompleteness
theorems
85
86 CHAPTER 8. GÖDEL INCOMPLETENESS THEOREMS
constructed. This one piece of syntax with two different meanings is typical
Gödelian lateral thinking.
We can say that Y is the number of a provable formula by (∃X)P r(X, Y ).
We can now find a formula diag(X) which says that X is the Gödel
number of a formula A(Z) with one free variable and if n = g(A(Z) then
A(n) is not provable from the Peano postulates. Now let n = g(diag(X)).
We see that diag(n) is a sentence of LN . One of its meanings is that its
own self is not provable. diag(n) is either true or false in the standard
interpretation. If it is false, it is also provable from the Peano postulates.
But the Peano postulates are all true in the standard interpretation, and
deduction preserves truth. Therefore |=N diag(n). Also diag(n) is not
provable from the Peano postulates.
Remarks:
These results also apply to Zermelo Fraenkel set theory. In fact there is
no recursive axiomatisation which is complete for any extension of Peano
arithmetic.
It may be called common sense that human knowledge is limited. The
incompleteness theorems of Gödel prove this rigorously.
Chapter 9
Logic programming (
prolog)
What sort of computational use can we make of the semantic tableau idea?
87
88 CHAPTER 9. LOGIC PROGRAMMING ( PROLOG)
(1) q if p,r
|
(2) p if r
|
(3) r
|
(4) not q
/ \
We take the not q of the original tableau as a goal. We match this with
one of the conclusions of the clauses which are asserted, in this case with
the conclusion of clause (1). The premises of clause (1) then become sub
goals. So we have subgoals p and r. Proceeding recursively, we match p
with one of the conclusions of the clauses. In this case the match is with
the conclusion of clause (2). We generate another subgoal, namely r. This
matches with one of our original premises, so one branch gets closed. The
next branch gets closed in the same way.
It seems that in the case when our axioms are Headed Horn clauses, we
have a reasonable search strategy. That is, we seem to know what to do
next. The main idea of prolog is to try to exploit this.
From now on, we will suppose Γ is a list of headed Horn clauses which
are universally quantified, and A is an existentially quantified conjunction
of atomic formulae.
We are trying to decide Γ |= A.
9.1. HEADED HORN CLAUSES 89
|
(2) duck (esmerelda)
|
(3)pond(estero)
|
(4) pond(emeryville)
Our convention that constants begin with lower case and variables begin
with upper case is very useful here, since it allows us to distinguish constants
and variables.
1. Put Γ, the list of headed Horn clauses asserted and ¬A, a conjunction
of atomic formulae denied in the initial tree.
2. If A is the conjunction of n atomic formulae, split ¬A into n branches.
The i th branch has Γ asserted and has the i th conjunct of A denied.
3. Pick the leftmost open branch. Let’s say this has A1 denied.
90 CHAPTER 9. LOGIC PROGRAMMING ( PROLOG)
() A1
/ \
not B not(R1 , R2 , ... Ra)
Make the substitution α so that the left hand branch of this closes.
On the right hand branch we have a new denied conjunction of atomic
formulae
not (R1, R2, ... , Ra) α
7. Continue recursively with the new tree, after application of substitu-
tion α.
8. If we do not get closure of the new subtree below A1, backtrack, delete
this subtree, undo the substitution α and continue scanning Γ below
Ci.
9. If we do get closure, report the final substitution of terms for variables.
Prolog also has function and predicate symbols, just like first order
languages. We can use any string of characters starting with a lower case
letter as the name for a function or a predicate. As in any term language,
we can have complex terms built up out of simpler ones. So for example
f (g(X, hat), Y, Z, h(p, q))
is a possible prolog term. Prolog also has some built in function symbols.
Lists are especially important. In prolog, lists are written surrounded with
square brackets and separated by commas. So, for example,
[cat,dog,horse]
is a list of three items. Lists can also have other lists as components.
So
[hat,[big,yellow],got]
is a list whose first component is a constant, and whose second compo-
nent is a list. Lists are terms and lists can have any terms as components.
There is am empty list, written [ ].
Predicate names in prolog are strings of characters beginning with lower
case letters.
You should try to keep in mind the distinction between predicates, which
are semantic things, functions whose codomain is {T, F }, and predicate
names, which are defined by syntax.
Predicate expressions are usually written as predicate names followed
by a list of terms enclosed in brackets. For example
red(cup)
says that some particular object, called “cup” is red.
There is no systematic way to distinguish function names from predicate
names in prolog.
Prolog has a few built in predicates, whose names are already deter-
mined. An important built in predicate is equality, written
X = Y.
Inequality, written
X \= Y.
says that one person is the mother of another. We could decide to write
this as
qqqqqzsz(A,B).
mother(louise, mabel)
is a fact. To say that this is a fact does not, in this context, imply that
we think it is true. It may be true or it may be false. A fact, in this context,
is an expression which has the form
predicate-name(constant-term,..., constant-term).
likes(joe, alice)
p
9.3. PROGRAMS IN PROLOG 93
——————-
mortal(X) :- human(X).
featherless(socrates).
bipedal(socrates).
animal(socrates).
——————–
p :- q1,q2,..., qn.
94 CHAPTER 9. LOGIC PROGRAMMING ( PROLOG)
where
p, q1, q2,..., qn
are predicate expressions. The meaning of this rule is that p is true when-
ever all of q1,..., qn are true.
Statements in prolog are only of two types, either facts or rules. Prolog
statements are also called clauses.
Both rules and facts are headed Horn clauses.
In a rule, such as
mortal(X) :- human(X).
the left hand side is called the conclusion. A rule is supposed to state a
truth and also show a possible way to prove the conclusion. So, for example,
expresses the truth that any featherless bipedal animal is human, and
also tells us that if we have an X and want to show
human(X)
human(fred).
we need to check
On the other hand, occurrences of the same variable in different rules are
not linked at all. The program given above would have the same meaning
if the first line were changed to
mortal(Y):- human(Y).
Exercise: You can create a prolog program with any editor. Try this.
Once a program has been written, we may wish to ask questions about
its logical consequences. This process is called consultation and is done
with a prolog interpreter and/or compiler.
The reader should at this point discover how to get access to some
version of prolog.
At Bath University on the BUCS machines, prolog can be run by typing
pl.
Suppose you have written a prolog program. Give whatever local com-
mand is necessary to run prolog.
Once prolog is running, type
[file].
if file is the name of the program you have written. You can then ask
questions, and you should get logical consequences of the facts and rules
you have given. For example if you ask mortal(socrates). after reading in
the above example, it should, (after thinking for a while), say yes. If you
ask
96 CHAPTER 9. LOGIC PROGRAMMING ( PROLOG)
mortal(X).
it should eventually satisfy this with
X=socrates.
To get out of prolog type
halt.
—————–
criminal(boxcarjoe).
criminal(bigred).
criminal(eaglehat).
locate(bigred,ny,july3).
locate(bigred,sf,june4).
associate(bigred,boxcarjoe).
98 CHAPTER 9. LOGIC PROGRAMMING ( PROLOG)
locate(boxcarjoe,milan,june5).
etc.
——————–
We store this in some file. We then call prolog. We read in the file with
[’filename’].
criminal(bigred).
yes.
criminal(X).
X = boxcarjoe.
and we get the next crook, bigred. If we type ; again we get the next
one. When the list is exhausted, we get answer
9.6. BACKTRACKING 99
no.
We can also ask compound queries. Suppose, for example we are con-
cerned to discover a criminal associate of bigred who was in milan on some
date.
We ask
X=boxcarjoe, Date=june5.
9.6 Backtracking
When we were working with semantic tableaux, we made substitutions and
attempted to close branches of our trees. If a substitution did not result
in closure, we tried another substitution; we never deleted anything. But
prolog does delete the results of unsuccessful substitutions.
Suppose the current goal of prolog is query q(X1 , ..., Xn ). prolog scans
down its program trying to unify the query with one of its facts or rules.
Suppose the first match is with the head of a rule, with unifier σ, at line
k of the program. The unifier σ is applied to the tail of the rule, and
the conditions in this tail become new goals. Prolog continues recursively,
starting with the first subgoal in the tail. If this attempt ultimately fails,
prolog backtracks. This means that it discards the substitution σ and
resumes its attempt to match the goal q(X1 , ..., Xn ) just below the last
match, i.e. at line k + 1 in the program.
Consider, for example, the query
criminal(X), locate(X,ny,july3).
with the program given above. This will first try the substitution [X :=
boxcarjoe]; but this will not succeed, and backtracking will ensue.
100 CHAPTER 9. LOGIC PROGRAMMING ( PROLOG)
(1) p:- p.
|
(2) not p.
The goal is p. This is matched with the head of p:-p, and we obtain
a subgoal of p. Unfortunately, prolog now continues recursively, and so it
never terminates. It comes to no conclusion. Prolog is unable to recognise
that it is considering a goal which it has considered previously.
Obviously there are many ways in which prolog can tie itself into such
infinite loops, and it frequently does so. In predicate logic, we often consider
symmetric relations. For example,
married(X,Y):- married(Y,X).
To write such a rule in prolog would be to invite non termination.
To give another example, we could define fatherhood in terms of male-
ness and parenthood.
father(X,Y):- male(X), parent(X,Y).
Of course it is also true that fatherhood implies parenthood. We would
include this if we were writing axioms of fatherhood in predicate logic. But
in a prolog program, we would be unwise to have both
father(X,Y):- male(X), parent(X,Y).
9.8. HOW TO WRITE SIMPLE PROLOG 101
and
parent(X,Y):- father(X,Y).
since this would be likely to cause non termination.
Such difficulties are very annoying. There are ways of alleviating such
problems, but the alleviations are almost as irritating as the original prob-
lems. It seems to me that it is fair to say that prolog is a reasonable
first step toward implementing predicate logic, but that, at present, it is
extremely limited.
9.9 Examples
Imagine a small community where everyone is related to everyone else in
several different ways. We wish to express the relationships in a prolog
program.
Some of the predicates we might be concerned with here are: par-
ent(X,Y), father(X,Y), mother(X,Y), male(X), female(X), sister(X,Y), grand-
father(X,Y), aunt(X,Y), cousin(X,Y). Evidently there are many logical re-
lationships, for example:
———————–
parent(X,Y) :- mother(X,Y).
parent(X,Y) :- father(X,Y).
grandfather(X,Y) :- father(X,Z),
parent(Z,Y).
————————–
The problem is to decide which are the basic predicates, and then to
build up definitions of the more complex predicates in terms of the simpler
ones.
To begin with, suppose we took the above definition of parent in terms
of father and mother, and of grandfather in terms of father and parent.
Suppose we added some facts to the program given above.
———————-
father(jud,rubel).
father(rubel,jubel).
parent(jubel,marx).
parent(rubel,jed).
father(jubel,lu).
parent(marx,mabel).
parent(X,Y) :- mother(X,Y).
parent(X,Y) :- father(X,Y).
grandfather(X,Y) :- father(X,Z),
parent(Z,Y).
——————-
9.9. EXAMPLES 103
grandfather(rubel, lu).
yes.
grandfather(X,Y).
In this case, prolog will try to find values of X and Y which make this
true. It might reply
X=jud, Y=jubel.
X=rubel, Y=marx.
By giving more semicolons, you should get all the possibilities, one after
the other.
You can also have compound queries, such as
grandfather(X,Y), parent(Y,lu).
104 CHAPTER 9. LOGIC PROGRAMMING ( PROLOG)
cat(bags).
cat(felix).
bird(harold).
hunt(X,Y) : - cat(X), bird(Y).
————————–
hunt(felix, harold).
Prolog matches this goal with the left hand side of the first rule, using
unifying substitution α = (X, f elix), (Y, harold). It then has two subgoals
cat(felix), bird(harold).
hunt(felix,W).
yes, W=harold.
You can see what prolog is doing, step by step, by turning on the trace.
This is done as follows:
9.10. CIRCULAR DEFINITIONS AND RECURSION 105
trace, hunt(felix,harold).
It may happen that the same variable is used in two different statements
in a program. In order to prevent clashes in the substitutions, most versions
of prolog will begin by renaming the variables. So you will find that new,
perhaps peculiar variable names are used in trace.
The trace can be turned off by calling
notrace.
descendent(Z,X) :- parent(X,Z).
———————
q(a).
s(a).
p(X) :- q(X), r(X).
p(Y) :- s(Y).
9.11. THE CUT, AND NEGATION 107
——————
Suppose we ask
p(a).
Prolog will find a match with the first rule, using α = {(X, a)}. It then
has subgoals
q(a), r(a).
It satisfies q(a), but then fails to satisfy r(a). So it backtracks, and dis-
cards the substitution α; it gives up on the first rule and tries the second. It
matches, with substitution {(Y, a)}, and gets subgoal s(a), which it satisfies.
So it says “yes”.
Prolog’s search for ways to satisfy its goal, is like the exploration of a
maze. The process of backtracking is like the retracing of steps in a maze,
after a dead end has been found.
Backtracking may be inhibited by writing
!
The instruction ! is called a cut. An example of how this is used would
be:
p :- q1, !, q2
This statement would tell prolog that if it is trying to satisfy p and has
got as far as q1, it can’t backtrack in order to satisfy p. The only way to
satisfy p is then to satisfy q2.
Continuing with the maze analogy, the cut is like a one way door in the
maze of possibilities. Notice that this has absolutely no axiomatic analogy.
There is no such thing as a cut, or anything like a cut, in a set of axioms.
With the appearance of the cut, prolog blatantly diverges from its original
idea. Nevertheless, the cut is interesting and useful.
a :- b,c.
c:- d,!,e.
c:- f.
a:- d,f.
b.
d.
f.
108 CHAPTER 9. LOGIC PROGRAMMING ( PROLOG)
—————–
The goal
c
will fail, but the goal
a
will succeed.
————–
Suppose you are trying to decide whether or not
brother(a,b)
and you already know that a and b have a common father. You need now
to check that a is male and not the same as b. We know however that there
is no use looking for another common father, or in looking for a common
mother, since that will still leave us with the same subgoals. So we can add
cuts which speed up the computation without changing its results.
——————————–
9.11. THE CUT, AND NEGATION 109
————————–
p :- q, !, fail.
p.
————————
This means that if q can be satisfied, p must fail. On the other hand,
if q can not be satisfied, p is true. In this situation, p means that prolog
can’t prove q. We could think of p as
not `prolog q.
For example, prolog has a built in equality predicate, written
X == Y.
We can use this, together with the cut and fail combination to define
inequality, as follows.
————————
110 CHAPTER 9. LOGIC PROGRAMMING ( PROLOG)
notequal(X,Y) :- X == Y, !, fail.
notequal(X,Y).
———————–
Another way of thinking of the above is that we take inequality to be
true by default; it will be false only when we can prove equality.
To give another example, we could define femaleness as the negation of
maleness, or vica verse
————————
female(X).
———————–
Note that the whole effect is ruined if we change the order of the state-
ments in a prolog program of this kind. So the meaning of such a prolog
program depends on the order of the statements in it.
Another annoying feature of the cut is that it interferes with the capacity
of a prolog program to generate a list of substitutions which satisfy a given
condition. This is because the
;
depends on backtracking, which may be inhibited. For example, the
above program will not generate a list of females. It can only be used to
test whether or not a known individual is female.
To make the example even more annoying, consider
—————–
——————–
If this program is given query
9.12. FAMILY TREE 111
female(X).
The head of a list [t1 , t2 , ..., tn ] is the first component, t1 . The tail is
the rest of the list, [t2 , t3 , ..., tn ].
We use
[X | Y]
[X | Y]
112 CHAPTER 9. LOGIC PROGRAMMING ( PROLOG)
[a | [b,c]] = [a,b,c]
samelength([ ], [ ]).
———————-
First the predicate is defined in the simplest case, when both lists are
empty. Then if we are given two lists, neither of which is empty, we chop
off their heads, and compare the lengths of the tails.
Most versions of prolog have built in predicates
member(X,Y)
and
append(X,Y,Z).
The predicate member(X,Y) is true if and only if Y is a list and X is a
component of Y. If our prolog does not have member(X,Y) already defined,
we can define it as follows:
————————
member(X, [X | Z]).
———————-
The idea here is: X is a member of Y if X is the first component of Y.
Otherwise check to see if X is a member of the tail of Y.
The predicate append(X,Y,Z) is true if X, Y, and Z are all lists, and
the list Z is obtained by appending list Y to list X.
We could give a recursive definition. The idea here is first to think of the
simplest possible case: append(X,Y,Z) is true if X is empty and Y and Z
are the same. We then express a more complicated case in terms of simpler
cases. Suppose X is not empty, but the head of X is the same as the head
of Z. We chop off these two heads and continue recursively.
—————————-
append( [ ], Y, Y).
—————————
Exercise. Try to see what prolog actually does when given a question
such as:
append([cat],[dog,horse],[cat,dog,horse]).
9.14 Sorting
Prolog has built in predicates X = < Y and X < Y which have the usual
meaning as applied to numbers. We will say a list of numbers is monotone
non decreasing if every number in the list is less than or equal to the next
number, if any, in the list. We can define this as follows:
—————
mnd([X ]).
————————-
There is an algorithm called bubble sort, which takes a list, X, of num-
bers and rearranges it to get a monotone non decreasing list, Y . If the first
list, X, is already monotone non decreasing, then bubble sort does nothing
and Y is the same as X. On the other hand, if X is not already monotone
non decreasing, bubble sort finds a pair of numbers in X which is out of
114 CHAPTER 9. LOGIC PROGRAMMING ( PROLOG)
order, swaps them to get list Z and then applies bubble sort recursively to
Z.
c(i,n).
c(t,e).
c(f,g).
c(w,e).
c(w,l).
etc.
—————————–
A binary relation on a domain D is a set of ordered pairs of elements
from D. We may represent a binary relation by a predicate of arity 2.
An equivalence relation on a domain D is a binary relation r(X, Y ),
defined over D so that the statements
1) r is reflexive, i.e.:
r(X, X)
2) r is symmetric, i.e.:
r(X, Y ) → r(Y, X)
3) r is transitive, i.e.:
(r(X, Y ) ∧ r(Y, Z)) → r(X, Z)
are true over D. This means that these statements are true for all pos-
sible values of the variables X and Y in D.
There is a path from a node X to another node Y if and only if we can
prove r(X, Y ) from the axioms for an equivalence relation, and
(∀X)(∀Y )(c(X, Y ) → r(X, Y ))
and the above list of facts. Therefore we might try to solve the labyrinth
problem with the following program.
.
—————
facts as above
r(X,X).
116 CHAPTER 9. LOGIC PROGRAMMING ( PROLOG)
r(X,Y) :- r(Y,X).
r(X,Z) :- r(X,Y), r(Y,Z).
r(X,Y) :- c(X,Y).
————–
However this will certainly not work in prolog.
Prolog, given these axioms and a few facts, will just go into an infinite
loop. For example, trying to decide r(a,b), prolog might choose the second
of the two rules, and take as subgoal r(b,a); in trying to satisfy this, it
might again use the second rule, and find r(a,b) as a sub-sub goal, etc.
The fact that prolog falls on its face when given one of the simplest
axiom systems in mathematics shows that prolog does not fulfil its ideal
of directly representing logic. On the other hand, the fact that prolog gets
mixed up here can also be seen as a criticism of the usual way of doing
mathematics. The standard definition does not really give us an effective
definition of an equivalence relation.
Although the first attempt does not work, it seems that we have learned
something.
r(X,X).
r(X,Z) :- r(X,Y), r(Y,Z).
r(X,Y) :- c(X,Y).
r(X,Y) :- c(Y,X).
————–
r(X,Y) :- c(X,Y).
r(X,Y) :- c(Y,X).
r(X,X).
r(X,Z) :- r(X,Y), r(Y,Z).
————–
Remarks. This is an improvement. It sometimes works and sometimes
does not work.
Problem 9.4 Give an example in which the above program does not work.
————————-
—————–
nonmember(X,[ ]).
nonmember(X, [Y | Z]) :- X \=Y, nonmember(X,Z).
—————
————————–
Then to find a route, for example, from a to z, we ask:
path(a, z, Route, [ ]).
Prolog will give us the route if there is one, and will inform us if there
is none.
Problem 9.7 Do you believe the above claim? What would it mean to say
that the path finding program was correct? Try to state this carefully. Note
that if we ask
path(a,z,Route,A)
the program will fall on its face. How does it fall on its face? The area
to be avoided must be set initially to a constant. Once you have decided
how to state correctness, try either to prove or disprove it.
Would you be worried if your life depended on the correctness of a four
line program written by an expert?
9.16. LABYRINTH PROGRAM 119
Problem 9.9 If you think the program above, which you wrote, is correct,
try to say why it is correct. If you are not able to say why it is correct,
but you think it is correct, can you offer any justification to support your
assertion? Can you at least say what correctness would be in this case?
It has been known from the beginning of the history of science that
anecdotal evidence, in situations in which the variables are not controlled,
and in which the witnesses are not neutral, is not reliable at all.
—————————-
The number of proposition names can be increased just by adding to
the list in the last line. If we really need infinitely many proposition names,
we could include
sf(X) :- atom(X).
using the built in prolog predicate atom(X), which will return true for
any sequence of characters beginning with a lower case letter.
The above prolog program will not only recognise grammatical state-
ment forms in variables p, q, r, but it will also generate them. For example,
if we ask a question
sf(X).
prolog will find an X which is a statement form. Presumably it will say
yes. X=p.
9.18. KITCHEN TABLE PROPOSITIONAL THEOREM PROVER 121
If we say
;
we will get another grammatical statement form, presumably q, and if
we continue to request more, we will get r, [p, imp, p], etc.
So this program appears both to generate and also to recognise state-
ment forms.
Most context free languages can be dealt with in this way. (Some extra
difficulty may occur when the grammar allows erasure of some grammatical
symbols, i.e.
V := λ
or when the rewrite rules contain a loop. )
————————–
————————–
The six lines of this program are supposed to correspond to the semantic
tableau rules for negation and implication. You will need to add some more
rules for other logical operators. To try to prove (p → p) you would query:
contra([ ],[[p, imp ,p]]), and hope to get the answer yes. Since the semantic
122 CHAPTER 9. LOGIC PROGRAMMING ( PROLOG)
tableau system was sound and complete, this program is also a sound and
complete formal system for the propositional calculus.
As it stands this kitchen table theorem prover does a lot of unnecessary
backtracking. You may wish to add some cuts to speed things up. Another
idea is arranging the statements so that the branching is done as late as
possible. You may also want to use some write statements, to see what is
happening.
Note that although prolog is defined only using implication and conjunc-
tion in a very special form, we have obtained a complete theorem prover
for the propositional calculus inside prolog. In a sense this is a triumph.
We have encapsulated almost everything we know about the propositional
calculus in a few lines of code.
9.19 Problems
Problem 9.11 What is the relation between the Assertion and Denial lists
which occur in the theorem prover above, and the branches which occurred
in the semantic tableau algorithm?
contra([p],[[q,imp,p]]).
contra([],[[[p,imp,q],imp,[q,imp,r]]])
contra(A,D)
which fails. This means that there are only variables left in the live
part of the branch, and that the same variable does not occur on both sides.
Check that this gives a counterexample. What does prolog do next? Modify
the code, using cuts, so that as soon as prolog finds a counterexample, it
stops the computation and prints the counterexample.
To get something printed on the screen, you can use the built in
write(X)
which will write whatever term X is bound to. For example
write(A, ’are true’)
will print the current list of Assertions followed by the text “are true”.
9.19. PROBLEMS 123
Problem 9.14 Write a prolog program which will check whether or not an
expression represents a grammatically correct formula of LN , the language
of first order single sorted arithmetic. (Such a program is called a language
recogniser.) Then translate the following statements into LN , give them to
your language recogniser, and see if you were correct, at least grammatically.
If not, rewrite your translations, correcting the grammar. You can save
yourself some time by telling your language recogniser to report errors as it
finds them.
a) X is prime.
b) X can be written as the sum of three squares in just one way.
c) some linear combination of X and Y can not be written as the sum
of two squares unless Z is even.
d)X is congruent to Y modulo Z
e) There are infinitely many prime numbers.
f ) There are infinitely many prime numbers P such that P+2 is also
prime.
g)There are only finitely many prime numbers P so that P+2 is also
prime.
Problem 9.15 Implement the propositional theorem prover which was de-
scribed above, and apply it to formulae of LN . As a result you should be
able to prove all tautologies in LN , a tautology being a formula obtained by
substitution from a statement form tautology.
Problem 9.16 * Invent some axioms, which seem correct to you, and add
them to your theorem prover so that you can prove some statements which
are true in the normal sense of the word about the natural numbers, but
which are not tautologies. Try to make your theorem prover as good as you
can, in a couple of hours of work.
Try your theorem prover on f ) and g) in the above list.
What does “true in the normal sense” mean in the above? Is everything
which your theorem prover proves true in the normal sense? Do you think
there are any statements which are true in the normal sense but which are
not consequences of your theorem prover? Please think quite hard about this
and explain your opinion, using common sense arguments. We will return
to this later.
Problem 9.17 Give a recursive definition of finite tree and translate this
into prolog.
9.20.1 write(X)
If a variable X is instantiated to a term, then write(X) will succeed and
cause the term to be printed on the current output stream. So, for example,
the items in a list, L, would be printed one after another by
expose(L)
with the following definition
--------------
expose([]).
---------------
9.20.2 read(X)
The predicate
read(X)
will attempt to unify X with the next term that is input on the current
input stream. The term must be followed by a full stop and a non printing
character, such as a space or a return.
sum(X,0):- X = < 0.
sum(X,Y) :- Z is X-1, sum(Z,W),Y is W +X.
—————–
If we give this the query
sum(999,S).
we will get the answer S=999500. The program, given X, will find Y
so that sum(X,Y) is true. However it will not work in reverse. So the
program, given S=999500 will not find X so that sum(X,S). This is because
when the program gets to line 2, it will try to evaluate X-1 and fail since X
is uninstantiated.
Example 9.8 The following program finds the greatest common divisor K
of two integers I and J.
——————–
128 CHAPTER 9. LOGIC PROGRAMMING ( PROLOG)
gcd(I,0,I).
gcd(I,J,K) :- R is I mod J, gcd(J,R,K).
———————
As above this only works when I and J are instantiated.
natural(0).
natural(X):- X is Y+1, natural(Y).
—————–
Another difference between the Gödel language and prolog is that the
former has abandoned the cut, but uses a related construction called com-
mit. A virtue of the commit operation is that programs written with it
have a meaning which is not entirely destroyed by changing the order of
the statements in the program.
Gödel allows definitions with quantifiers. This implies that a full, com-
plete implementation of the language can not be specified.
Gödel also continues to follow the extreme form of the declarative ideal,
which is that the procedural part of a program should be automatically and
unobtrusively derived from the declarative part. My opinion is that this is
a mistake.
• Enter facts about predicates which are as simple as possible. Put the
facts at the top of the program.
[X | Y],
Terms in prolog include constants and variables and the set of terms is
closed under formation of lists and application of function symbols.
Two terms A and B are unified by a substitution α if Aα = Bα .
A substitution α is a most general unifier of two terms A and B if
• α unifies A and B.
• If β is any other unifier of A and B, there is a substitution δ so that
α◦δ =β
Up to this point we have been dealing with first order languages. In this
situation there is only one type of variable, and in the semantics there is
only one universal domain of objects. This is also the situation in prolog.
We have seen that it is possible to express almost anything in this frame-
work. However in computing languages, as in real life, we often wish to use
languages in which variables are given different types to show that they are
intended to range over different domains. For example, we might have a
type for the integers and another type for the reals, and various functions
which go from one domain to the other. In general, we will write X : A to
mean that variable X has type A. So for example X : integer means that
X is an integer. A multisorted language is a first order language in which
the variables and function and predicate symbols have all been typed. We
can also have a multisorted term language.
We may also wish to have variables for sets or functions, and to quantify
over these variables. For example we can obtain higher order arithmetic by
extending LN by adding variables for sets of natural numbers, and a new
predicate for set membership.
Bound variables can also be given types. So, for example,
(∀X : A)p(X)
means that p(X) is true for all X of type A. Similarly,
(∃X : A)p(X)
means that there exists and X of type A so that p(X). Many of the
131
132CHAPTER 10. MULTISORTED, HIGHER ORDER LANGUAGES, AND NON CLASSICA
ideas and methods we have considered for first order languages naturally
extend to these typed languages. For example, CNF, DNF, prenex normal
form, Skolem form, clausal form. The semantic tableau method also extends
in a natural way. However, since the types usually have a fixed intended
interpretation, the semantic tableau method is no longer complete.
So, for example, the factorial function has type nat → nat. This would
be written f act : nat → nat. The logical operator of conjunction has type
bool × bool → bool. Addition over the real numbers has type real × real →
real. Reversal of a list of natural numbers has type list nat → list nat.
• (A × B) × C and A × (B × C)?
does this imply that A is true? To see some of the developments and appli-
cations of this, the reader may refer to Logic in Computer Science by Huth
and Ryan.
www.cl.cam.ac.uk/Research/HVG/Isabelle
Semantics and
Specification for Programs
1. First order languages only have variables of one type. In a first order
135
136CHAPTER 11. SEMANTICS AND SPECIFICATION FOR PROGRAMS
gcd(x:integer, y:integer):integer;
r:=remainder(x,y);
while (r not = 0)
x:=y;
y:=r;
11.2. DENOTATIONAL SEMANTICS 137
r:=remainder(x,y);
return y;
References
See www.cs.bham.ac.uk/research/lcs/
Follow wwwtutor link for review questions about Huth and Ryan book,
also useful for this course.
See
www.afm.sbu.ac.uk
See
turing.wins.uva.nl/~johan/Phil.298.html
139
140 CHAPTER 12. REFERENCES
See
mas.colognet.org/objectives.html