Foreword Logic studies the way we draw conclusions and express ourselves, and deals with how to formalise it. In Logic, ...
20 downloads
556 Views
3MB Size
Report
This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!
Report copyright / DMCA form
Foreword Logic studies the way we draw conclusions and express ourselves, and deals with how to formalise it. In Logic, propositions are elementary statement and conclusion units; they are analysed both as to the form, namely their syntax, and the interpretation, namely their semantics. The relation between the syntax and the semantics is also examined. The first steps in Logic are credited to the Ionian and Elean philosophers and to the sophists. It is however Aristotle who is considered as the founder of Logic as a science.
Somehow, interest in Logic declined with the Roman prevalence
in the Mediterranean Sea. And in the Middle Ages, as most of the work of the ancient philosophers
except for Plato and Aristotle
was already lost or had
disappeared, Aristotle's syllogistics became the priviledge of only a few monks. Logic regained its interest as non-Euclidian geometries were discovered and as the need for a theoretical foundation of analysis became evident.
As soon as
1879, Frege presented the first formal language for Mathematics and Logic. But it was the paradoxes of set theory, and the many conversations and disputes among mathematicians of that period on that subject, that gave Logic its final impulsion. In 1895 and 1897, Cantor published the first and the second part of his study on cardinal and ordinal numbers. In this study, which constitutes the first foundation of set theory, each collection of objects is regarded as a separate entity called a set. Buralli-Forti discovered a paradox in Cantor's theory as soon as 1897. And in 1899, Cantor himself published a paradox concerning his theory on cardinal numbers. Russell published in 1902 a simpler form of that same paradox, known in the history of mathematics as Russell's paradox: In Cantor's theory, each set is defined by the characteristic property of its elements. Let A be the set of all sets X defined by property X r X,
A = {XIXr But then: AEA
if and only if A C A
which is obviously an antinomy. vii
viii
Foreword The paradoxes of set theory generated a great deal of doubt and concern among
mathematicians of that period about the well-founding of mathematics. They also made clear that only rigorous formalising of mathematical concepts and method could lead to "sound" theories without antinomies. And so, under Hilbert's influence, Remark 1.4.1 (2), the axiomatic method's development began and Logic started taking its actual form. With the use and the development of computers in the beginning of the 1950's, it soon became clear that computers could be used not only for arithmetical computation but also for symbolic computation. Hence, the first arithmetical computation programs, and the first programs created to answer elementary questions and prove simple theorems, were written simultaneously. The basic steps towards a general method based on Logic, which would allow statement procedures as well as manipulation of symbols, were first accomplished in 1965 by Robinson with the use of the resolution method, and later by Kowalski and Colmerauer who made use of Logic directly as a Logic Programming language. This book unfolds Logic's basic elements; it analyses the relation of and the transition from Logic to Logic Programming. The first chapter describes Propositional Logic (PL). It examines, in the first place, the propositional language, as well as the syntax and the semantics of PL formulae.
Three proof methods are presented afterwards: the axiomatic proof
method, mainly for historical reasons, the Beth tableaux method, with proofs using mathematical analysis of propositions, and the resolution method. The last method uses proofs in a similar but much more effective formalism that can easily be materialised in a program. These proof methods are validated by corresponding soundness and completeness theorems. The second chapter examines Predicate Logic (PrL). In Predicate Logic, structures of propositions are more extensively analysed, for the predicate language is much richer than the propositional language. The predicate language contains indeed quantifiers and constants, as well as predicates and functions. PrL semantics are hence much more complex. Herbrand interpretations are of great importance to these semantics, because they make satisfiability tests for PrL propositions possible by using PL methods. The axiomatic proof method, the Beth tableaux method as well as the resolution method, which by means of the unification algorithm constitutes the basis of Logic Programming, are examined afterwards. Corresponding soundness and completeness theorems confirm the validity of these proof methods.
Foreword
ix
The third chapter deals with Logic Programming as well as with the programming language PROLOG, and it also analyses PROLOG's derivation mechanism. Logic supplies Logic Programming and PROLOG with both a very rich language and the theoretical foundation which guarantees correct results. Each chapter includes solved as well as unsolved exercises provided to help the reader assimilate the corresponding topics. The solved exercises demonstrate how to work methodically, whereas the unsolved exercises aim to stimulate the reader's personal initiative. The contents of this book are self-contained, in the sense that only elementary knowlegde of Analysis is required to study them. This book can therefore be used by students in every academic year, as a simple reading, or in the context of a course. It can also be used by those who utilize Logic Programming without having any particular theoretical background knowledge of Logic, and by those simply interested in Logic and its applications in Logic Programming. All topics developed in this book are constantly analysed within the perspective of a transition from Logic to Logic Programming. Therefore, certain proofs, mainly those of completeness of the axiomatic method proofs, are omitted. References to the international bibliography direct to more complete developments of concepts and proofs that are only indirectly related to the perspective of the book, and therefore not extensively examined. The material in this book is based on the book published by the first author in Greek in 1992 and which, in turn, drew on the authors' seminars on the Principles of Logic Programming, as well as on teaching at the universities of Cornell and Rochester in the United States, and at the University of Patras, in Greece. At this point, the contributions of Aneta Sinachopoulos-Svarna in sections 1.1, 1.8, 2.3, 2.11, 3.2, 3.3, 3.6, 3.8, 3.9 and in the material's general configuration and presentation, and of George Potamias in units 3.4, 3.5 and 3.7 must be stressed. Costas Kontogiannis, Spiros Papadakis and Petros Petropoulos, postgraduate students of the university of Patras, also contributed to the material's preparation. All programs included in the text and in the exercices have been run by George Potamias in TURBO-PROLOG, v.2.0. George Metakides Anil Nerode
I Propositional Logic &q oOx ~az[. Man is the measure of all things; of the things that are, that they are; of the things that are not, that they are not. Protagoras
1.1 Introduction Until 1850, mathematical proofs were ba~sed on experience and intuition and were generally accepted.
Leibniz was the first to outline, as early as 1666, tile
need of a formal language, called "universal characteristic", which would be created and used in mathematical formulations and methodology. However his ideas were ahead of their time. The initial development of a formalism for mathematics is credited to Boole, two hundred years later, with his studies on "The Mathematical Analysis of Logic" published in 1847, and "The Laws of Thought" published in 1854. De Morgan also contributed to the efforts for tile creation of a formalism with publications of his own in 1847 and 1864, as well as Peirce in 1867 and Frege in 1879, 1893, and 1903. The publication of Russell and Whitehead's threevolumed "Principia Mathematica" (Mathematical Principles) marked the acme of these efforts, for it presented a logical system within which all mathematics was formalized and all corresponding theorems were derived by means of logical axioms and rules. Logic flourished as an independent
science after 1920, with Lukasicwicz
(1878-1956), Lewis (1883-1964), Ghdel (1906-1978), Warski (1901-1983), Church (1903-1995) and Kleene (1909-1994) as its main representatives.
2
Propositional Logic
Starting in the nineteen fifties, computer technology dealt with how to utilise a computer for symbolic computation. A solution to that problem was provided by Functional Programming, created by McCarthy among others, and used mainly in the United States. Another solution was provided by Logic Programming, created mainly in Europe by Colmerauer and Kowalski among others. Logic Programming gave Logic a new impulse: it solved many problems concerning forms of logical propositions which should allow the computer to execute syllogisms and judgement procedures. Logic Programming also brought answers to questions concerning the nature and the mechanisms of logical procedures executed by the computer. We thus need to examine, in the first place, how mathematical and logical propositions are formalised. Logical connectives are the basic elements of such a formalisation, as we can see in the following examples.
(1)
Conjunction is formalized by A. Let us assume that we know two properties of a certain x: A:
x>3
B:
x 3) ~ (x > O) Let us assume t h a t x > 3. Then x > 0. Which rule allows us to conclude "x > 0" when the a s s u m p t i o n "x > 3" is valid? And does this rule have a general validity; in other words, if M --4 N is valid and M is valid, is proposition N always valid? Here is another example. We know t h a t in order to go to the movies we need m o n e y for our ticket. movies -~ money for the ticket
(1)
Let us assume t h a t we do not have enough money for the ticket: -~ (money for the ticket)
(2)
The Language of Propositional Logic
5
Are we able to go to the movies? W h a t is the impact of negation? W h a t form will (1) take with the negation? (movies) --+ ~ (money for the ticket) ? Or
~ (movies --+ money for the ticket) ?
Or
-~ (money for the ticket) --+ ~ (movies) ?
And what comes as a valid conclusion when (1) and (2) are valid? We therefore need to examine the syntax of propositions, and the rules that determine when and how we can deduce valid conclusions from premises from available data.
Propositional Logic, as well as Predicate Logic, which is more
complex, deals with these topics. In the Propositional Logic (PL) chapter, we will examine PL propositions with respect to both their syntax and semantics. We will also study methods which deduce conclusions from premises and we will determine the adequate forms of PL propositions for knowledge representation, decision procedures and automatic
theorem proving in Logic Programming.
1.2 The Language of Propositional Logic Alphabet, Syntax and Semantics The language of Propositional Logic (PL) is a formal language. By means of this language, we formalize that specific part of everyday speech which is necessary to the formulation of logical and mathematical concepts. Having formalized propositions with the use of that language, we next examine them according to their structure and validity. For every formal language, we have: (a) an a l p h a b e t containing all the symbols of the language (b) a s y n t a x by means of which the symbols are utilised and the propositions are formed (c) s e m a n t i c s by means of which the language interprets and allocates meanings to the alphabet symbols.
6
Propositional Logic The alphabet of the language of Propositional Logic consists of: (i) Propositional symbols" A, A1, A2, . . . ,
B, B1, Be, ...
(ii) Logical connectives" V, A , - - , - - + , ++ (iii) Commas and parentheses: " , "
and " ( "
," ) "
Logical connectives intuitively correspond to conjunctions used in everyday speech. We thus have the following correspondence: V ,
disjunction 9
or
A ,
conjunction"
and
--+,
implication 9
if... then...
,
equivalence "
. . . if and only i f . . .
negation
not
-7,
9
Any sequence of symbols belonging to the alphabet of a language is called an expression. For instance, A V V B , A V B , ++ A are expressions of the Propositional Logic's language. Certain w e l l - f o r m e d expressions are considered by the syntax a.s propositions. The following definition looks into the concept of propositions and, furthermore, describes the syntax laws of the
PL
language.
D e f i n i t i o n 1.2.1: Inductive definition of propositions: (i) Tile propositional symbols are propositions, called a t o m i c p r o p o s i t i o n s or a t o m s . (ii) If a, r are propositions, then the expressions (a A r ) , (a V r ) , (a --+ r ) , (a ++ r ) , (--a) are also propositions, called c o m p o u n d p r o p o s i t i o n s . (iii) Tile expressions constructed according to (i) and (ii) are the only expressions of the language which are propositions,
m 1.2.1
The expressions VA V B and ~ A are thus not propositions since they were not constructed according to ( i ) a n d (ii), whereas (A V B) and ((~A)V (B ~ (~C)) are indeed propositions. The above definition uses the method of induction to define compound propositions; (i) is the first s t e p and
(ii)
is tile i n d u c t i v e s t e p . The induction is on
the logical length, the structure of the propositions. By the "logical length" of a proposition A, we mean a natural number denoting the number, the kind and
The Language of Propositional Logic
7
the order of appearance of the logical connectives used in the construction of A, starting from the atomic propositions which constitute A. We do know t h a t the p r i n c i p l e of m a t h e m a t i c a l
i n d u c t i o n holds for tile natural numbers:
D e f i n i t i o n 1.2.2: If
P(a) denotes a property of a natural number a, and if we have:
(1)
P(0),
and
(2)
for any natural number n, if
P(n) holds true then we can prove that
P(n + 1) holds true, then we can conclude that for all the natural numbers n,
P(n) holds,
m 1.2.2
The well-ordering of the natural numbers (which means t h a t every non-empty subset of the natural numbers has a least element), together with the principle of induction, leads to a second form of induction, called c o m p l e t e or c o u r s e o f values induction. D e f i n i t i o n 1.2.3: Complete Induction: If
P(a) is a property of a natural number a, and if we have
(1)
P(0),
and
(2)
For all tile natural numbers m and n, such t h a t
rn < n, we can derive
P(n) from P(m), then for all the natural numbers
n, P(n) holds true.
m 1.2.3
We used complete induction to give the inductive definition of propositions above (Definition 1.2.1), and will use it again to give the following definition. D e f i n i t i o n 1.2.4: General scheme of induction for propositions: Let P be any propositional property. proposition a has the property
P.
We shall write
P(a) to mean t h a t
If we prove that-
(a)
P(A) holds true for any a t o m A of the language
(b)
if
al,a2 are propositions and P((71) , P((72), then P((al A a 2 ) ) ,
P((o" 1 V 02)) , P((or 1 --+ 0"2)) , P((o" 1 ~ 0"2)) , P((-'nO'l) ) and
then we can conclude
P(a) for any proposition a of the language,
P((--'a2))
m 1.2.4
8
Propositional Logic
E x a m p l e 1.2.5: (i)
The property P: "the number of left parentheses is equal to the number of right parentheses" holds true for all propositions of PL. Indeed, according to the general scheme of induction, we have:
(a)
In the propositional symbols, the number of left and right parentheses is equal to 0.
(b)
If in the propositions or1 and a2, the number of left parentheses is equal to the number of right parentheses
let n and m be these
numbers respectively for al and if2 .... then in the propositions
(ffl A O'2) , (O"1 V if2), (O'1 ---+ 0r2), (O'1 ~ if2), (--70"1)and (~a2) the number of left parentheses is equal to the number of right parentheses, which is n + m + 1. (ii)
Tile expression E : (~(A A B) ~ C) is a proposition. The proof is based upon Definition 1.2.1.
(iii)
step 1 :
A A B is a proposition by Definition 1.2.1 (ii)
step 2 :
-~(A A B) is a proposition by Definition 1.2.1 (ii)
step 3 :
C is a proposition by Definition 1.2.1 (i)
step 4 :
(--(A A B) -+ C) is a proposition by Definition 1.2.1 (ii)
The expressions below are not propositions: --
:
the symbol '-~' is not an atom
A --+ A :
the symbol 'A' is not a proposition
(A A B :
there is a deficient use of par#ntheses Ig 1.2.5
E x a m p l e 1.2.6: The proposition F of our everyday speech is given: F :
"If it doesn't rain, I will go for a walk"
To formalize tile above proposition in the context of PL, we can use auxiliary propositional symbols: A :
"It's raining"
B :
"I'm going for a walk"
The Language of Propositional Logic
9
Then F becomes ((--A) -+ B), which is a proposition. If there is no risk of confusion whatsoever, the parentheses can be omitted: F:
-~A --+ B
II 1.2.6
Each atom which occurs in a well-formed formula D is considered a subforinula of D. For example, A and B are subformulae of F in Example 1.2.6. Moreover, each compound well-formed part of D is also a subformula of D. For example, if D: then -~A , C V ~ A
(A A ~ B ) --+ [(C V-~A) --+ B]
, A A - ~ B as well as ( C V - ~ A ) --+ B are subforrnulae of D.
Moreover, D itself, i.e., (A A-~B) --+ [(C v-~A) --+ B], is considered a subformula of D. The set subform(a) of all subformulae of the well-formed formula cr is given by the following inductive definition: D e f i n i t i o n 1.2.7:
(1) if a is an a t o m A, then subforrn(a) = {A} (2) if a has the form -~T, then subform(a) = subform(7)t2 {a} (3) if a has the form T o ~, where o C {V, A , ~ , e+}, then subform(a) Remark
9
= s u b f o r m ( T ) U s u b f o r m ( ~ ) t 2 {a}.
1.2.7
1.2.8: To avoid confusion by using connectives in formulae without
parentheses, we consider ~ to bind more strongly than all the other connectives, V and A to bind more strongly than ~
and -+, and ~
to bind more strongly
than ~ . Thus -~A ~ B V C ,
AAB
~C,
A ~ B ~C
read respectively (~A)--+ (B V C ) , Remark
(A A B) --+ C ,
A -+ (B ~ C)
I
1.2.8
1.2.9: For historical reasons we will consider the symbol "+--" to be a
logical connective, "B +-- A" meaning exactly the same as A -+ B.
m 1.2.9
10
Propositional Logic
Semantic Concepts in Propositional Logic
1.3
Valuations and truth valuations Propositions, according to Definition 1.2.1, are general and abstract syntactic objects. We wish to interpret these abstract objects by means of semantics. This means t h a t we are interested in determining the conditions under which a proposition is true or false.
We therefore create a structure, the domain of which is
{t, f } , with t, f denoting respectively tile t r u t h values " t r u t h " , and "falsehood", and we try to assign one of these values to each proposition. Tile method used in the allocation of the t r u t h values, as well as the necessary definitions, constitute the
PL
semantics.
D e f i n i t i o n 1.3.1: A v a l u a t i o n is any function"
F-Q:
~,{t,/}
where Q is the set of tile atoms of the language.
E
1.3.1
A valuation thus a~ssigns t r u t h values to the a t o m s of tile language. We now provide the set {t, f } of t r u t h values with internal algebraic operations, so as to transform it to an algebraic structure. which will be defined, i.e., ~ , v1 , IA , ~ logical connectives -~ , A , V , ~
and ~ .
Tile internal operators of t, f
and ~
, will correspond to the
The similarity of tile corresponding
symbols is not fortuitous, it outlines the correspondence. Consider, for instance, the proposition A V B. By assigning certain t r u t h values to A and B, wc interpret A V B using the interpretations of A and B. The operationbwhich does this is U, ms we will see more clearly in the next definition. D e f i n i t i o n 1.3.2: The internal operations
, I I , v1, -,'+ and ~
defined by the following tables.
U
t
f
n
t
f
f
t
t
t
t
t
f
t
f
t
f
f
f
f
of {t, f} are
Semantic Concepts in Propositional Logic
t
f
t
f
t
t
~
t
f
t
t
f
f
f
t
11
. _
In the tables defining R , U , ~
and ~
, the first column defines the first
c o m p o n e n t and the first row defines the second c o m p o n e n t of the corresponding operation,
m 1.3.2
The structure
({t, f } , ~ , U, R) with the operations ~ ,
u,
[q being defined by
these tables, is a two-valued Boolean Algebra. Boolean Algebras are of great i m p o r t a n c e to PL semantics [RaSi70, Curr63, Rasi74, Raut79] ms well as to the general studies of the theoretical foundation of c o m p u t e r science.
D e f i n i t i o n 1.3.3: Let S be the set of the propositions of our language. By t r u t h v a l u a t i o n or B o o l e a n v a l u a t i o n we m e a n any function:
V :S~-+ {t,f} such that, for every a, T C S: (a)
if a is an a t o m then V(a) e {t, f }
(b)
V(~)
(c)
v ( ~ v ~) = v ( ~ ) u v(~)
(d)
V(a A T) = V(a) n V(T)
= ~ V(~)
(e) V(~ ~ T) (f)
=
v(~).,~ v(~-)
V(a ++ T) = V(a) ~
V(T)
m 1.3.3
As we can see in the last definition, the values of the t r u t h valuations of the a t o m s of some proposition are interconnected with the algebraic operations ,,~ , LJ , V1 , ~
and ~-~, and provide the value of the t r u t h valuation of the
proposition under consideration [Smul68].
12
Propositional Logic A valuation assigns a truth value, t or f , to the atoms of the language.
A
truth valuation is the extension of a valuation to the set of the propositions of the language. As proved in the following theorem, each truth valuation extends uniquely to a valuation on the set of the language's propositions. T h e o r e m 1.3.4:
f or each valuation F, there is one and only one truth valuation
V such that V extends F. P r o o f : We will use induction on the length of the propositions. Let F be a valuation and Q the set of the atoms. We define inductively a truth valuation V in the following way: (a)
V(A) = F(A)
for every A C Q
Let a, ~ be two propositions. If V(a) and V(~) are already defined by (a), we impose: (b)
V(--a) =
,,~ V(a)
(c)
V(o- v ~)
(d)
v ( ~ A ~) = v(~) n v ( ~ )
(e)
v ( ~ ~ ~) = v ( ~ ) - ~ v ( ~ )
(f)
V ( a ~ ~) = V(a) ~
=
v(o-)
u v(~)
V(~)
V is obviously a truth valuation extending F. What is now left to prove is that if the t r u t h valuations V1 and V2 are extensions of F, then VI(~) = V2(~) for any proposition ~. The property P used for the induction is: P(~) (a)
:
VI(~) =
V2(~)
For every propositional symbol A, we have VI(A) = V2(A), since V1, V2 are extensions of F.
(b)
Let us suppose that V l ( o ) = V2(a) and V I ( ~ ) = Vz(~), then v,(-~o-)
=
,--, v, (o-) =
,.-, V~(o-)
=
v~(-~o-)
v~ (o- s ~) =
v~ (o-) n v~ ( f )
=
V2(o-)n v 2 ( f )
=
v2(o- A ,,,:,)
v~ (o- v ,,,:,) =
v, (o-) u v, (,,,:,) =
v2(o-)u v2(~)
=
v2(o- v ~).
Semantic Concepts in Propositional Logic
13
Treating similarly the other conditions of Definition 1.3.3, we prove t h a t I/1 and V2 have the same value for every proposition; they therefore coincide.
II 1.3.4
The next useful corollary is a direct consequence of T h e o r e m 1.3.4: C o r o l l a r y 1.3.5:
Let a be a proposition containing only the atoms A 1 , . . . , Ak.
If two truth valuations take the same values in the set { A 1 , . . . , Ak}, i.e., VI(A1) = V2(A1),
...
, VI(Ak)=
V2(Ak),
then V1 (a) = V2(a). Example
II 1.3.5
1.3.6: Calculation of a t r u t h valuation from a valuation:
Let S be the set of atomic propositions S = {A1, A2}, and F be a valuation such that: F(A1) = t
F(A2)= f By Theorem 1.3.4, the t r u t h valuation VF extending F which we are seeking is uniquely defined. Let us impose:
VF(AI) := F(A~) VF(A2) := F(A2) where by " := " is meant "equal by definition". We are now able to calculate the values of the t r u t h valuation VF for any set of propositions which contains only the a t o m s A1 and A2:
VF(A1A A2) = VF(A1) • VF(A2) = t rl f
= f
VF(A1V A2) = VF(AI) kl VF(A2) = tU f
= t
VF((A1V A2) ~ A2) = VF(A1V A2) "* VF(A2) = t ~ f
= f,
etc.. [] 1.3.6
We can classify the P L propositions according to the t r u t h values they are getting.
14
Propositional Logic
D e f i n i t i o n 1.3.7: A proposition a of
PL
is l o g i c a l l y t r u e , or a t a u t o l o g y , if f o r
every truth valuation V, V ( a ) = t holds. This is denoted by ~ a. We shall write
~= a to mean that a is not a tautology, i.e., that there exists a truth valuation V such that V(a) = / . A proposition a is s a t i s f i a b l e or v e r i f i a b l e if there exists a truth valuation V such that V ( a ) = t. A proposition a such that, f o r every truth valuation V, V ( a ) = f , is called l o g i c a l l y false, or n o t verifiable, or is said to be a c o n t r a d i c t i o n , Remark
m 1.3.7
1.3.8: Let V be a truth valuation, and Sv
-
{a 6 PL propositions I Y ( a ) - t}
be a set consisting of the propositions of our language satisfiable by V.
If for
every truth valuation V, the proposition ~ belongs to the corresponding set S v , then ~ is a tautology. Every S v set, with V being a truth valuation, constitutes a possible world [Fitt69, Chel80] of the set of
PL
propositions. Every possible world
is A r i s t o t e l i a n , meaning that for every proposition a, only one from a, -~a (:an be true in a certain possible world. ( Y ( a ) - t or V ( a ) - / ,
thus Y ( ~ a ) - t according
to Aristotle's principle of the E x c l u d e d M i d d l e , Remark 1.4.4.) Possible worlds are the basic concept of K r i p k e s e m a n t i c s . Kripke semantics is used in the studies of Modal Logic. Modal Logic contains special symbols besides the logical connectives
called modal operators, such as
II
$
~
S non-verifiable.
Proof:
Let C 1 , . . . , Ck be a proof from S by resolution. T h e n with the inductive
use of the above lemma, we prove t h a t any t r u t h valuation verifying S also verifies C1. If the conclusion is the e m p t y clause, then Ck coincides with [3. Since the e m p t y clause [3 is non-verifiable, S is non-verifiable,
m 1.13.3
To prove the completeness of resolution, we will use the following auxiliary
lemma. Lemma
1.13.4:
Let S be a non-verifiable set of clauses in which only the
literals A1, A2, . . . , Ak occur.
Let S k-1 be the finite set of clauses which are
provable by resolution, and in which the only atomic propositions occurring are A1, A2, . . . , A k - 1 .
Then S k-1 is non-verifiable.
66
Propositional Logic
Proofi
Let us suppose that S k-1 is verifiable. Then there is a t r u t h valuation V
on { A 1 , . . . , Ak-1} which verifies S k-1.
Let 1/1 and 1/2 be the two extensions of
V on {A1,... ,Ak} such that: VI(Ak)-
t
and
V2(Ak)- f
Since S is non-verifiable, there is a clause C1 c S which is not verifiable by V1. However, then -~Ak E C1, since otherwise there are two possibilities: (a) Ak ~ C1.
But then, 1/1 does not contain Ak, C1 c S k-1 and 1/1 verifies C1, contradiction.
(b) Ak C C1.
Then V1 verifies S k- 1 and V1 (Ak) = t.
1/1 therefore
verifies C1, contradiction. We can also conclude, in a similar way, that there is a clause (72 C S, which is non-verifiable by V2, such that Ak C (72. Let us consider D = (61 - {--Ak}) t2 (62 - {Ak}). D is the resolvent of C1, C2 and D E S k-1 . Hence, V verifies D. In other words, one of the following hohts:
(a')
V ve.rifies C1 - {-~Ak}. But then 1/1 verifies C1, contradiction.
(b')
V verifies C 2 - {Ak}. But then V2 verifies C2, contradiction.
Then S k-1 is non-verifiable. Theorem
1.13.5:
I1 1.13.4
Completeness of Resolution:
If S is a non-verifiable set of clauses, then the, e m p t y clause is provable by resolution from S.
Formally: S non-verifiable
Proofi
~
E] C R* (S).
By Definition 1.9.3, S contains a finite number of literals. Let A 1 , . . . , Ak
be these literals. By applying the previous le~nma once, we conclude that S k-1 is non-verifiable. Applying the same lemma k times we conclude that S is nonverifiable, since no atomic propositions occur in its clauses and S O c_ R* (S). Hence, [3 C S O c_ R*(S).
Remark
I1 1.13.5
1.13.6: If a is a proposition provable by resolution from tile set S of
propositions, then a is provable from S (Definition 1.8.5). We then have by the
~...
~
b~
9
~--.
.
m
~"
,:.n
~
~
~
t:::u
>
~
~-~
.
9
--
~-~
~
~'~
,-'.
~.
9
~.
9
~
0
"-d
~
~~
o
~
>
~
~
~~
~
9
~
~ ~ < ~
9
r../;'.
o ~
o
~
~
~
r162
,-..
~
i.-,,,, 9
~,
~
Oa
0~.
~
~
o
~
;~
$
$
>
$
$
~
~
c~
>
>
z'~ "~ ~
>
$
J ~
$
~
oo
9
c--e-
~.
=~
~
,
~o], where ~o] is derived from ~ol by substituting none, one, or more than one occurrence of a with a'. By the completeness theorem, we know that (C ++ D) -+ (-~C ++ ~D) is a tautology derivable in our axiomatic system.
(ii)
qa is the proposition ~x o qo2, where o C {A , V , -+ , +~}. ~1 e+ qo~ and I- ~2 e+ ~ ,
Then
and by the tautology C -+ (D -+ (C A D)),
completeness, and the theorem of substitution of equivalences we obtain !
!
The result follows by the tautology (C1 ++ Da) A ((5'2 ++ D2) -+ (C1 0 C2 ++ Dx 0 De)
1.14.23 Assuming that k (A A B) ~ - ( A -+ -~B) and -~(A v B) +-~ ( - A -+ B), derive: (a)
k ( - B -+-~A) ~ (A -+ B)
(b)
k (-,A --+ B) e+ (~B --+ A) and k (A --+ B) ++ (B --+ - A )
(c)
t- (A A B)--+ A
(d)
k (A A - ~ A ) - ~ B
(e)
k A V -~A
Exercises
83
Solution: (a) and (b). We use Axiom (3), tile Law of Negation, and the Theorem of substitution of equivalences, to prove -4 and then +--. (c) ~--~A -4 (B -4 ~A)
(1)
by Axiom (1).
-~A -+ (A -4 -~B)
(2)
by (1), (b), and substitution of equivalences.
(A -4 -~B) -4 A
(3)
by (2), (b), and substitution of equivalences.
But according to tile assumptions, this is what we are seeking, namely
( A A B ) -4 A. We usually need to proceed analytically in order to determine tile steps of the derivation: we first try to determine (3), then determine (2) from (3), and continue until a proposition known to be derivable, such as (1), is found. 1.14.24 Using resolution prove that tile following sets are not verifiable:
(a) S = {-~AvBvD, -~BvDvA, --,DvC, -~DvA, AvB, Bv-~C, -~Av-~B} (b)
S = {-~AvB, ~ B v C , ~ C v A , A v C , ~Av-~C}
(c) S - - { A v B v C ,
A v B v - ~ C , A v - ~ B , -~Av-~C, -~AvC}
Solution:
(c) Consider
(1)
AVBVC
(2)
AVBV-~C
(3)
A v-~B
(4)
-~AV-~C
(5)
--1AVe
(6)
AVB
by (1) and (2) with resolution.
(7)
A
by (3) and (6) with resolution.
(8)
--,A
by ( 4 ) a n d (5).
(9)
1--1
by ( 7 ) a n d (8).
Hence S is not verifiable.
~ > j
$
J
~
j
>
>
~
oo
9
~.,,~ 9
9
9
~ ~" o~
~
,
9
l--I
p__,.,
~
r
~ .
~" ,
~ ~ $
J J ~
~
I~
~
;~
o ~
j
..
$
~,
c~
J.
J
.,
$
$
> ~
~
o ~ ~ >
~
~ o " , the "greater than" relation of tile natural numbers, and where c~ corresponds to 1. Based on this interpretation, the meaning of S t)econms:
S
.
"There exists a natural number x such that, for every natural number y, x > y holds."
S states that there exists a maximal element in A, and this is obviously not true in A', since A' does not have a maximal element. In other words, we observe that we can a t t r i b u t e different interpretations for the same language, thus assigning different t r u t h values,
m 2.5.1
116
Predicate Logic
I n t e r p r e t a t i o n s a n d Truth" F o r m a l D e s c r i p t i o n We will continue with the formal description of the interpretation and tile truth of a sentence within the D e f i n i t i o n 2.5.2:
PrL
context, [ChLe73, Dela87, Hami78, Meta85, RaSi70].
Let
/: -- {R0, R1, ... ,f0, fl, . . . , c 0 , be a
PrL
c1,...}
language, where Ri are predicate symbols, fi are symbols of functions
and ci are constant symbols, i - 0, 1, . . . . .A -
(A, c(R0), r
An i n t e r p r e t a t i o n of s
c(f0), ~ ( f l ) , . . . ,
c(c0), e ( C l ) , . . . )
consists of: (i)
a set A :/= ~, the universe of the interpretation.
(ii)
an n-ary relation e(Ri) C_ A n for every predicate symbol Ri of arity n.
(iii)
a function r
(iv)
an element e(ci) E A for every constant symbol ci.
" A m.
> A for every function f/.
e(Ri), e(fi) and e(ci) are the i n t e r p r e t a t i o n s of Ri, fi and ci respectively in A. I
2.5.2
E x a m p l e 2.5.3: Let
/2be tile
PrL
{=,_
>
..
"
b~
|
|
~ ~
J
g~
~
o
~,-~
~
.
9
'~
~
C~
0
D,-,.
C}~
~,,~0
~
~
~ II
'~
~176
~176
II ,-.,,-,
r.~
~
•
II
%
~
~
II
.~
c.~
""
160
Predicate Logic
We must note at this point that in many applications, in the pursuit of higher efficiency, the PROLOG inference mechanism based on the unification algorithm ignores Occur Check. In other words, it substitutes the first term of a given DS for the first variable x. This can surely lead to erroneous conclusions; it is therefore for the programmer to create the suitable flow and security mechanisms in the program in order to avoid such errors. We are now in position to describe the PrL resolution method in simple terms.
Resolution in PrL The PrL resolution method is actually a combination of PrL unification and PL resolution.
Thus, just as in PL (Definition 1.9.17), if S is a set of PrL clauses,
then a p r o o f b y r e s o l u t i o n f r o m S, is a finite sequence of clauses C 1 , . . . , C,,, such that for every Ci (1 _< i < n), (1 < j, k < i), where
we have Ci c S
Ci c R({Cj, Ck})
and
R({Cj, Ck}) is the resolvent of Cj and Ck.
Note here that since the variables of all the clauses are considered bound by universal quantifiers, we can rename these variables to avoid confusions in each unification. This renaming procedure is called a n o r m a l i z a t i o n
of v a r i a b l e s .
Let us take a look at an example. Example
2.9.11: Consider the following clauses: C1
=
C2
-
{-.P(x,
z), P(x, z)} v), P ( v ,
We wish to conclude:
Ca
{-~P(x, y), -~P(z, y), P(x, z)}
The respective notations for C1, C2 and C3 in the context of PL are: (V x) (V y) (V z)
[P(x, y) A P(y, z) --+ P(x, z)]
for C1
(V x) (V y) (V z)
[P(u, v) ~ P(v, u)]
for (72
(V x) (V y) (V z)
IF(x, y) A P(z, y) -+ P(x, z)]
for C3
Method I. We work directly within the context of PrL"
II
o
II
~..
)
0
N
"
N
s
~
~
~
~
3>
~ >
IT1
m
-~ ~
~
m
m
o~
r~
~e
1" ~e
1" ~e
t.m.~
1"
~e m
v
1"
m
~e
q~
1"
~e
~4
rm~
1"
~4
~e
m
~4
|
|
o,-.i
0
9
o,..~
,mq
9
0 o,.., o,..~
E4
9
9 9
~
"~
9
.
~
~
LW
$
~
J
~
II
$
~
~
>
~
J
i~
9
c
s,
O~
~
~ $
o
-IT-
~
~
~
~
~
--
~
,1.
.~
~-
bl
9 o~
,..,.
2~
..,
II
..,
II
~
J
II
II
II
II
II
N
b~
b~
.J_..
9
~...~
--
li
cn
flr)
ii
C
9
o
~
~
e-,-
~
,----,
>
w
J
$
r
>
$
>
>
II
J
>
LI_I
w
.
r
r
~
r
o
9
o
o CP
o
.,-~
o
.,.-~
.,..~
o
o
r
.,....4
r
r
9
c4
~
150 Not_Alive(x4, t3) +-- Dead(x4, t3) Dead(x5, t4) e-- Not_Alive(x5, t4) Dead(x6, t6) +-- Died(x6, t5), t6 > t5 +-- Not_Alive(x, 1987)
where for uniformity with tile PROLOG formalism which will h)llow, we write tile indices beside the variables and terms; for example, Xl becomes x l. The first thirteen Horn clauses represent tile information which we give the program. P14 is the query we make: Who is not alive in 19877 The corresponding set-theoretic forln of the sentences p l , . . . , P14 is: (1)
(2) (3) (4)
(5) (6) (7) (S) (9) (10) (11) (12) (13) (14)
{Person(Marcus) } {Person(Aurelius) } {Mortal(xl), -~Person(xl)} {Born_Pompei(Aurelius) } {Born_Pompei(Marcus) } {Born(Aurelius, 45)} {Born(Marcus, 40) } {Died(x2, 79), -~Born_Pompei(x2)} {Erupted(volcano, 79)} {Dead(x3, t2), --,Mortal(x3), ~Born(x3, tl), --,(t2 - tl > 150)} {Not__Alive(x4, t3), ~Dead(x4, t3)} {Dead(x5, t4), -~Not_Alive(x5, t4)} {Dead(x6, t6), -~Died(x5, t5), -~(t6 > t5)} {~Not__Alive(x, 1987)}
206
Logic Programming: The PROLOG Paradigm
The corresponding representation in PROLOG is:
(6) (7) (8) (9)
per s on (mar cus ). person(aurelius). mortal(Xl) :- person(Xl). born_Pompei (aurelius). born_Pompei (marcus). born(aurelius, 45). born(marcus, 40). died(X2, 79):- born_Pompei(X2). erupted(volcano, 79).
(10) (11)
dead(X3, T 2 ) : - m o r t a l ( X 3 ) , born(X3, T 1 ) , gt(T2,150). not_Alive(X4, T 3 ) : - dead(X4, T3).
(12)
dead(X5,T4):- not_Alive(X5, T4).
(13) (14)
dead(X6, T6) :- d i e d ( X 5 , T5), gt(T6, T5). ? not_Alive(X, 1987).
( 1) (2)
(3) (4) (5)
where g t ( X , Y) means X > Y. We use the PROLOG notation, writing constants with lower case letters while variables are written in upper case letters. The period symbol ". " is used in most PROLOG versions to denote the end of a Horn clause input. PROLOG, similarly to Predicate Logic, tries to prove the goal. In its answer, if the goal is valid, PROLOG will instantiate; that is, it will give all possible values of the variables which validate the goal. Let us examine how this happens and how it relates to substitution, unification and resolution.
Step 1 : Our goal is: Not_Alive(x, 1987) PROLOG looks through the data for a Horn clause such that its head can be unified with the goal. In fact, the substitution 01 - { x 4 / x , t3/1987} unifies the goal with the head of the formula (11). PROLOG immediately tries to verify the body of the formula (11), by setting as a new goal, one after the other, all the assumptions of this formula. The resolution of (14) and (11) gives: (15)
{-~Dead(x, 1987)}
from (11)and (14).
PROLOG and Logic Programming
207
Step 2 :
Our goal is now: Dead(x, 1987) PROLOG, just like resolution does, now tries to unify the goal "Dead(x, 1987)" with a head of some clause from the data. This is achieved through the substitution 02 = { x 3 / x , t2/1987} in formula (10).
Step 3 :
The new goal for
PROLOG
Mortal(x),
is the triplet of assumptions from (10), i.e.:
Born(x, tl)
and
g t ( 1 9 8 7 - tl, 150)
which it tries to satisfy in tile order that they appear in rule (10). The resolution method respectively, would give us rule (16): (16)
{--Mortal(x), --Born(x, t l ) , ."gt(1987 - tl, 150)}
Step 4 :
PROLOG unifies "Mortal(x)" with the head of sentence (3) by using the substitution 03 = { x l / x } . Likewise, resolution would use the same substitution to unify "Mortal(x)" with "Mortal(xl)" of (3).
Step 5 :
Our new goal is now: Person(z) which in turn is unified through the substitution 04 = {x/Marcus} with the fact described by formula (1). Using the resolution method we would have: (17)
{-~Person(x),-~Born(x, t l ) , . " g t ( 1 9 8 7 - t l , 150)} from ( 3 ) a n d (16).
(18)
{."Born(Marcus, t l ) , . " g t ( 1 9 8 7 - t l , 150)}
from ( 1 ) a n d (17).
208
Logic Programming: The PROLOG Paradigm
Step 6 :
PROLOG continues its proof procedure and tries to satisfy the next goal from the triplet of goals presented in step (3), which now becomes: Born(Marcus, tl) The unification succeeds through the substitution 05 = {tl/40} with the head of rule (7). The resolution, correspondingly, would conclude the formula (19)
{-,gt(1987- 40,150)}
from (7) and (18).
Step 7:
The last goal to be satisfied from the triplet of goals from step (3) is gt(1987 - 40,150) which is satisfied by P R O L O G since 1 9 8 7 - 40 > 150.
Step 8 :
The goal of step (2), "Dead(x, 1987)", has been satisfied with the instantiation x = Marcus. The goal of the first step, "Not_Alive(x, 1987)", is therefore satisfied with the same instantiation. In this step, resolution deduces clause
(2o)
D
Step 9 . . .
:
PROLOG
does not stop here.
from (19).
It goes on, trying to unify the most recently
selected goal in a different manner, in order to find all possible solutions. Once the last goal has been satisfied in every possible way and all the valid solutions have been produced, the procedure is repeated for the immediately preceding
PROLOG and Logic Programming
209
goal. When this one is satisfied in some different fashion, tile program goes on by satisfying again the initial "last" goal using the substitutions which were made during the one before last goal, and continues along these lines: the final goal " g t ( 1 9 8 7 - 40,150)", cannot be unified in any other way, therefore PROLOG tries to unify, using a different substitution this time, the immediately preceding goal "Born(Marcus, 40)". With this method it eventually unifies "Mortal(x)" by means of the substitution 0 = {x/Aurelius} and with fact (2). It produces, just like in step (6), but for x = Aurelius this time, the new solution: Not_alive(Aurelius, 1987) This slick mechanism of P R O L O G which finds all the possible solutions is called b a c k t r a c k i n g and will be analyzed in detail in its respective section.
3.1.2 Logic and Programming The difference that distinguishes Logic Programming and the P R O L O G language from traditional programming and languages like F O R T R A N , BASIC, COBOL, and PASCAL, etc., lies in the fundamental principles of logic programming; both in the
design and tile implementation of a logic program. A program consists of two building elements, Logic and C o n t r o l [Kowa79, Lloy87]. By the term "Logic" we denote all those principal notions that determine W H A T a program is doing, whereas by the term
"Control" we mean all those
syntactic notions that determine HOW it is doing it (for example, the algorithm which solves a problem). If we wished to describe this using a single equation, we would write: PROGRAM
=
LOGIC + CONTROL
A traditional program written in BASIC or in some other traditional programming language consists of c o m m a n d s
which describe the actions which have to be
executed step by step by the computer in order for the program to produce the desired result. For example, the command in a BASIC program 10
LET
X = X + 5
increases by 5 the content of the memory address which corresponds to the variable X.
210
Logic Programming: Tile PROLOG Paradigm
Programming languages like BASIC are characterized by i m p e r a t i v e commands which describe the s t e p b y s t e p behaviour of the program, so that, after a finite sequence of those commands, the correct and expected result is produced. The structure of these commands during the design of the program comprises the element of CONTROL. Likewise, the element of LOGIC in the above command is the expression "X + 5", which is not a command by itself, but only a small d e s c r i p t i v e program. This program directly describes the arithmetic value which has to be computed and indirectly only how this value will be computed. Therefore, BASIC is an i m p e r a t i v e language which possesses a d e s c r i p t i v e element.
On the contrary, a PROLOG program could contain the sentence: n i c e ( X ) :- l o v e s ( X , p e o p l e ) which is simply a logical definition of the relationship between predicates "nice" and "loves". The design, therefore, of a PROLOG program is based upon the correct selection of predicates which define the relations between objects, such that the connection between the input data and the information that we expect as output is fully defined. In general, a program in a traditional programming language expresses a function from the input to the output of the program, whereas a program in a Logic Programming language expresses a relation between the data [Watt90].
As re-
lations are more general than functions (functions arc necessarily relations, but relations are not in general functions), Logic Programming has greater capabilities than traditional programming.
The selection of the predicates, and the relations which are expressed by these predicates, constitute the logic element in PROLOG. The control is found not only in the order in which we arrange the clauses, but also in a number of its structural control elements, like the cut "!" (section 4.3), which can be used as syntactic objects of PROLOG. PROLOG is therefore a d e s c r i p t i v e language which contains an i m p e r a t i v e structural element as a control element.
PROLOG and Logic Programming
211
Let us take as an example a program which reads two numbers and prints the greater of them. In order to make the difference between traditional programming and Logic Programming more concise, we will first give the program in BASIC and then exactly the same program in PROLOG: Program
in BASIC
10
INPUT "NUMBERI",X
20
INPUT "NUMBER2",Y
30
IF X > Y THEN 60
40
PRINT Y
50
GOT0 70
60
PRINT X
70
END
Program
in PROLOG program :- write("NUMBERl"), r e a l ( X ) , n l , write("NUMBER2"), r e a l ( Y ) , n l , greater(X, r, Z), write(Z). g r e a t e r ( X , X, X ) . g r e a t e r ( X , Y, Y ) : - X < Y. g r e a t e r ( X , Y, X ) : - Y < X . ? program.
" r e a l " , subsection 3.5.4, is a special predicate of PROLOG which checks whether a number is real. Using "nl", every number is written in a different output line. The program in BASIC is just a sequence of commands. These commands, which are executed in the order that the program indicates, constitute the control, i.e., the design and the flow of the program. The logic element in the BASIC program is found in the relation " Y " from the same example, and still others express the queries which have to be answered giving the solution to the problem. Thus in Logic Programming, and specifically in PROLOG, after applying the rules, the solution to the problem will either be an answer to the queries of the form yes or no, or a set o f v a l u e s which will form the desired solution. The commands therefore in Logic Programming are not of an exclusively functional nature, as in classical programming; but are predicates, elements of the Predicate Logic language, which are either true or false according to the interpretation of their terms. The truth or falsehood of certain predicates according to the interpretations of the variables provides the precisely corresponding answer of the form "yes" or "no" to the questions of the program.
PROLOG and Logic Programming
213
The answers to the queries in tile program arc given ONLY in relation to the information which we have given the program. Therefore, if we ask the program in the example of subsection 3.1.1 if 4 is a perfect square, that is, the square of an integer, tile program, not having been given tile predicate which characterizes the perfect square, will answer "no". This means that the goal: ? perfect_square(4). fails, Remark 1.9.9.
We have to pay attention here.
The failure of PROLOG
to verify tile goal does not mean that the goal is really incorrect, it only means that using the facts of the program and the inference mechanism that we have, we cannot conclude that this specific goal is true. This is the so called C l o s e d World Assumption
(see also subsection 3.6.1), [Watt90]:
A predicate Q of Predicate Logic is assumed false by the program if tile program cannot prove that Q is true. Therefore, every PROLOG program appears as a complete description of tile universe, and tile universe is fully characterized by the data of the program. Whatever is important in the universe is described by the data, and the relevant goals succeed; whereas whatever is not specified in the program is not known, and tile relevant goals fail.
3.1.~ Historical Evolution After Herbrand's algorithm (1930), using which we carl find an interpretation which contradicts a formula ~ of Predicate Logic if ~o is not logically true, Gilmore (1960) was the first to try to implement Herbrand's method on a computer [ChLe73].
Gilmore's implementation was based on the fact that a clause
is logically correct, Definition 2.5.19, if and only if its negation is contradicted in some interpretation of the language. Consequently, in his program, a procedure exists which forms PrL sentences which arc periodically checked for inconsistency, Definition 2.5.16. However, the program of Gilmore was not capable of analyzing very complex PrL formulae. After the publication of Robinson's [Robi65] unification algorithm, Loveland (1970) was the first to use linear resolution with a selection function. This is tile resolution during which we pick a clause, we resolve it with another, we resolve the resolvent (Definition 1.9.11) with a third clause and we continue always resolving the last resolvent until we find an empty programming clause [ChLe73, Lloy87].
214
Logic Programming: The PROLOG Para(ligm
The first steps (1972) in Logic Programming are attributed to Kowalski and Colmerauer [StSh86]. First Kowalski expressed the procedural interpretation of the Horn clause logic and showed that a rule of tile form: A:- B1, B2,
... , B n
can be both read and executed by a recursive programming language. At the same time Colmerauer and his research team developed in FORTRAN a programming language to be used as a theorem prover, which implemented tile procedural interpretation of Kowalski. The founding stone of PROLOG (pROgrammer en LOGique) was set! The first PROLOG interpreter, i.e., tile program which translates a text understandable by people (source file) to a language understandable by tile nlachine (machine code), was written by Roussel in Marseilles in 1972 using ALGOL, and was based on the theoretical works of Colmerauer. However the question of whether Logic, and consequently Mathematics, can be used as a programming language, must be answered negatively [Watt90]: A problem expressed in a programming language, has to be solvable by a computer, whereas in Logic and Mathematics there are problems which do not have algorithmic solutions, and hence the computer is not able to solve them.
One example of this is the decision problem in Predicate Logic, section 2.11. Hence Logic Programming, which means the languages which are based on Logic, is always restricted to a part of logic formulae; for instance PROLOG only deals with Horn clauses, i.e., a subset of the PrL sentences. Many researchers |lave recently focused in tile relations between several programming languages; such as Func'tional Programming, i.e., programming based oil A-calculus [Thay88], Logic Programming, and Object-Oriented Programming, i.e., programming based on the theory of types, according to which every object is represented by a set of properties, values and procedures [Thay88]. A lot of effort has been put into finding suitable transformations and equivalences that enable the transition from one programming language to others, as well as the use of different formalisms in one program at the same time. Most of the Logic Programming languages, which in fact are logic theorem proving systems using tile resolution method, are the first steps towards optimal Logic Programming.
Program Structure
215
For optimal programming, however, we have to solve the problem of Control completely, t h a t is: (i)
Have available more satisfactory and more slick Control procedures in every language of Logic P r o g r a m m i n g
(ii)
Be able to transfer Control during the design and execution of a program exclusively to the computer.
The solution of tile Control Problem will enable filture programmers and users to limit their interaction with tile computer to thc complctc and concise definition of the problem. The program in turn will take over the solution of the problem and the total control of tile program flow.
3.2 Program Structure 3.2.1
The Program Elements
A PROLOG program consists of d a t a and queries to which the program has to give an answer. The data and queries are Horn clauses, Definition 2.4.4. The data have one of tile following forms:
A ( c l , . . . , ck) +--
(*)
or as we symbolically write in PROLOG
A ( c l , . .. , c k ) . where A is a predicate and c l , . . . , ck are constants of
PrL.
The pcriod symbol "
signifies the point where a given formula stops.
A ( a l , . . . ,at:) e- B l ( b l , . . . b t ) ,
...
, Bj(dl,...
,dm)
(**)
or as we symbolically write in PROLOG
A(a,,...
,at:) :- B l ( b l , . . . ,bt) ,
...
, B j ( d l , . . . ,dm) .
where A, B 1 , . . . , Bj are predicates and a l , . . . , ak, b l , . . . , b e , . . . , d l , . . . ,dm are terms (Definition 2.2.1) of Predicate Logic.
216
Logic Programming: The PROLOG Paradigm
Data of the form (.) are the f a c t s of the prograin (Definition 2.4.6). Data of the form (**) are called the r u l e s of the program. Tim rules express the relations between the predicates occurring in the facts. The rules, which are implicative formulae of Predicate Logic on which the program applies unification and resolution, form the p r o c e d u r a l called the h e a d , and
p a r t of the program.
B~(b~,...,b~), . . . , B j ( d ~ , . . . , d m )
A ( a l , . . . ,ak)
is
is called the tail or
b o d y of the rule. The facts and the rules forn~ the d a t a b a s e of the program [Brat90]. The q u e r i e s are goals (Definition 2.4.5), i.e., Horn clauses of the form: B,
(b,,
. . . , be),
...
,
Bj(dl,
. . . , din)
or symbolically in PROLOG:
? Bl(bl,...,be),
...
, Bj(dl,...dm).
where B 1 , . . . , B j are predicates and a l , . . . a k , b l , . . . ,be,... , d l , . . . ,d,,~ terms (Definition 2.2.1) of Predicate Logic. Tile period ". " following each fact, rule or query denotes the end of the corresponding Horn clause. Let us study the PROLOG program of Example 2.4.9 with the query set in E x a m p h ~. 2.10.12, "What can Peter steal'?":
thief(peter). likes (mary, food). likes (mary, wine). likes (peter, money). likes(peter, X):- likes(X, wine). can_steal(X,g): thief(X), likes (X, Y). ? can_steal(peter, Y). The facts are the first four Horn clauses, the next two clauses are rules, and the last clause is the goal (the query) of the progrmn. PROLOG will respond with Y = money Y = mary which means that Peter ('.an steal money and Mary, something which we already knew from Example 2.10.12.
Program Structure
217
Hence a PROLOG program is a collection of facts, rules arid queries, withollt any special directions for the flow and control of the program. This structure exhibits the great slickness of PROLOG: extensions and improvements can be achieved by adding or deleting facts and rules using the c o m m a n d s " a s s e r t " and " r e t r a c t " which we will study in subsection 3.5.1. The facts in a PROLOG program can be interpreted as d e c l a r a t i o n s regarding the universe of the program, hence they are allegations which describe a specific world. The rules [Xant90] can be interpreted riot only as declarations, but also as p r o c e d u r e s of the program. Classical p r o g r a m m i n g languages essentially allow a procedural interpretation, whereas PROLOG is probably the only programming language [Xant90, Brat90, C1Mc84], which allows both a p r o c e d u r a l
and
a declarative interpretation.
3.2.2
The Facts
A s we saw in subsection 3 . 2 . 1 , a fact is a relation, i.e., a predicate, between
a number of objects, which are terms of a P r L language.
Thus, a fact is a P r L
sentence (Definition 2.2.17). The facts in P R O L O G express directly corresponding facts of the spoken language: Spoken language
PROLOG
the canary is a bird John is a man Peter likes Mary Peter can steal Mary Mary's parents are John and Kate
bird(canary). man(john). likes (peter ,mary). can_steal (peter ,mary). parents (mary, john,kate).
The names of predicates and constants always start with a lower case letter. The words in predicates which are expressed with more than one word, are connected with an underscore, " _ ".
Periods denote the end of each fact.
The position of the terms in the predicates is ordered. The fact"
likes(mary, peter). signifies that" "Mary likes Peter" and it is different from the fact:
likes(peter, mary).
218
Logic Programming: The PROLOG Paradigm
The selection of predicates and names which will be used is carried out by tile programmer. His interpretation of facts is therefore important. For example, the fact " f a t h e r ( j o h n , c h r i s ) "
can be interpreted either as "John is the father of
Chris" or as "Chris is the father of John". options, as long as we do not pick both!
We can pick either one of the two We should not expect correct results
from a program which contains the facts: father(john, chris). father(george, nick). when the first fact is interpreted as: "the father of John is Chris" yet the second fact as: "the father of Nick is George"
3.2.3
The Rules
A rule in P R O L O G is an implicative PrL formula (subsection 3.2.1). The rules express information and relations more general and more complex than the information expressed by the facts. For example, f a t h e r ( c h r i s , george). son(nick, john). f a t h e r ( X , Y)"- son(Y, X). The first two clauses are facts.
They express the relationship between specific
people, Chris--George and Nick - J o h n respectively. The third clause expresses a general inferential rule which has to do with the predicates of the data: if Y is the son of X, then X is the father of Y. The variables in both the rules and the queries are written in capital letters. The period denotes the end of each rule. The head is separated from the tail with the symbol " ' - " (neck symbol). If the tail of the rule consists of more than one predicate, then these predicates are separated from each other by a comma " , ".
Program Structure
219
Let us look at an example:
likes(john, ice_cream). likes (john, mary). likes(john, backgammon). food(ice_cream). eats(X,Y)" food(Y), likes(X,]/). Here tile tail of tile rule is composed of two predicates, "food(Y)"
and
" l i k e s ( X , ]/)"
This rule coniectures t h a t for all X and Y (Definition 2.4.1), if Y is food and X likes Y, then X eats Y (Definition 2.4.1 (ii)). In PROLOG we can write two or more rules which share the same head as a new rule, with head the c o m m o n head and using the symbol " ; ".
For example, the
rules:
likes(X, Y ) " - likes(Y, X ) . likes(X, Y)"- good(Y). can be written as one rule:
likes(X, Y ) : - likes(]/,X); good(]/). and tile meaning of it is "X likes Y, if Y likes X, or if Y is good". PROLOG emlmerates tile facts and tile rules of every database. For example, in the previous database, " l i k e s ( j o h n , i c e _ c r e a m ) " database and " e a t s ( X , Y )
:
is tile first Horn clause of the
food(Y) ; l i k e s (X , Y) " is the fifth. As we will see
in subsection 3.4.1, the order in which the facts and the rules arc entered in the database is very i m p o r t a n t for the derivation of conclusions.
3.2.~ The Queries The q u e r i e s or g o a l s in PROLOG, are questions related to the conditions and the predicates of Predicate Logic which a p p e a r in the d a t a of the program. For example, for the d a t a of subsection 3.2.2 we can ask:
(I0)
? likes(gas, X). ? like (X,Y), ood(Y).
220
Logic Programming: The PROLOG Paradigm
and PROLOG will respond to tile first query with X -
ice_cream
X
- mary
X
- backgammon
and to tile second question with X = john Y = ice_cream The queries begin with a
~ ? "
, question mark, and end with a period. A query,
for example ? l i k e s ( g a s , X). can be answered in a lot of different ways. A c o m p l e x q u e r y , like ? l i k e s (X, Y) , f o o d ( Y ) . has more than one subgoal, which are separated from one another with a comma. In the complex queries, the same variables always refer to the same terms. For example, we asked ? l i k e s (X, Y) , food(Y). hence we asked the program to find X and Y, such t h a t X likes Y and Y is a food. The query
kes(X,Y), food(X). would fail, t h a t is PROLOG would not succeed in finding a suitable value for X, and would therefore answer "no". Let us for example examine the description of a party oil the facing page. Queries like (1), (2) and (3), are d a t a v e r i f i c a t i o n q u e r i e s , whereas queries similar to queries (4) through (7), are d a t a m a n a g e m e n t (6) and (7) are c o m p l e x or c o n j u n c t i v e q u e r i e s .
q u e r i e s . Queries (3),
o
~-,
II
II
I~.
-
-~
II
~
~
-
II
-~
~I~.
II
~
II
0~
-
~0.
II
-~
I~ I~
II
~
o
o
~0
~
""
~'
~ l~'
0
0
~ I~ .
~
..
~
co ~
~
~0
,-,
-
9
(1)
~
co ~
~ ~I-~
~
~
~
~ , ~
c~ (1)
~
o
o
9
~
~
0
Ol
"
~.
N 0
~t-
~~ . . . : I"
~
~
~
o
~ ~
~,~
g
9r~
~
~-..~.
I~ ~~
~.
9~
~
I-~. ~
(1)
~ i~. ~
~ ~
o.
~
~
o~
o
9
~ ~ ~
o~
o
~
I~ (I)
o~
~.
~
~
I=1 9~
~
o~
~.
.
I=1 9
~
o~
~.
~
I~ ~9
g
o~
~.
~
I~-' I~
~
~o
~.
~
~.
9
9 I~
~
~
~.
oq oq
~
~
~
,..
o ~
g;
~
~
I~ ~.
.
"~'~.
~
~.
~
~.
~
~.
I:,~-' '~:: ~ . .~
~
~
~.
I::1 ~
~
on
~.
"D
,'-5
C~
0-q
222
Logic Prograrnming: Tile PROLOG Paradigm
In queries (2), (3) and (7) the PROLOG program failed (Remark 1.9.9 (2)): For query (2) there is no information concerning Jack and Mary in the data of the program. For query (3) the first subgoal succeeds, because Jane drinks wine, however, there is no information regarding the type of music the Jane listens to. Hence the second subgoal and consequently (why?) goal (3) fails. Query (7) fails because in the data for tile party there are no values for X and Y which satisfy all three subgoals at the same time.
3.3 Syntax of Data 3.3.1
The Objects of P R O L O G
As we have already said, the language which we use in PROLOG is a subset of the language in Predicate Logic, enriched with symbols which satisfy certain needs. Hence, the terms in P R O L O G are v a r i a b l e s , c o n s t a n t s , and f u n c t i o n s on variables and constants (Definition 2.2.1). The constants can be a t o m s , n u m b e r s , or s e q u e n c e s of s p e c i a l c h a r a c t e r s .
The fundamental objects of the PROLOG
language are the predicates and the lists. We will proceed by examining the terms and the basic objects of PROLOG.
3.3.2 The Alphabet of P R O L O G The PROLOG a l p h a b e t consists of: (i)
Upper and lower case letters of the English alphabet
ABCDEFGHIJKLMNOPQRSTUVWXYZ ab cde f g hij k lmn opqrst (ii) (iii)
The digits Symbols like
uv
w xyz
0 1 2 3 4 5 6 7 8 9 ! ~ $ % ^ &
9
(
) _
=
-
+
or
can be used as symbols of implication, -+ .
>
Syntax of Data
3.3.5
225
The Predicates
The predicates, Definition 2.2.1, express relations and properties of terms. For example, the predicate g r e a t e r ( + ( X , 2), : (Y, 3)) signifies that the term +(X, 2), or equivalently X + 2, is greater than tile term :(Y, 3) or Y : 3 ,
Definition 2.2.1. The symbols " + " and " : " correspond to the
addition and division symbols respectively, X and Y are variables, and 2 and 3 are constants. Predicates in PROLOG start with a lower case letter. In PROLOG, we have the ability to construct c o m p o u n d
predicates,
or
s t r u c t u r e s , from predicates which already exist in the program. For example, let us take the facts wtlich describe a family: father(nick). mother(mary).
Child( m e).
(,)
child(tim). In this example, " f a t h e r " , "mother" and " c h i l d " are the predicates, and"nick", "mary", "anne" and "tim" are the constants. The facts (.) can be expressed using only one predicate:
family(father(nick), mother(mary), children(anne, tim)).
(**)
In the expression (**), "family" is tile predicate, but " f a t h e r " , "mother" and " c h i l d r e n " are no longer predicates, but functions [Thay88]. This means that " f a t h e r ( n i c k ) " , "mother(mary)", and " c h i l d r e n ( a n n e , t i m ) " in (**), as specific values of functions, are considered as constants. Therefore, " f a t h e r ( n i c k ) " in the facts (.) is a different syntactic object from " f a t h e r ( n i c k ) " in (**). In tile same fashion, the fact l i k e s ( g e o r g e , ice_cream). where " l i k e s " is the predicate and "george" and "ice_cream" are constants, can also be expressed using the predicates
i c e_cre am (i ike s (ge o rge))
(1)
226
Logic Programming: The PROLOG Paradigm
or
(2)
george( i ikes ( i c e_c re am))
where in (1) "ice_cream", " l i k e s " and "george" are predicate, function and constant respectively, whereas in (2) "george", " l i k e s "
and "ice_cream" are
predicate, function and constant respectively. PROLOG identifies if a given expression is a constant, a function or a predicate,
according to the position of the expression in a fact, a rule or a query.
This
identification method will be seen better in tile next subsection, where we will represent predicates as trees. The great advantage of the use of compound predicates can also be seen in the family example of (,) and (**). C o m p o u n d predicates offer an abbreviated expression and slick manipulation of tile data. For example, if we are interested in the parents of the family, the corresponding queries for program (,) will be: ? father(X). ? mother(]/). whereas by using tile complex predicate in (**) we would have only one query: '.~ family(father(X),mother(Y),_
).
where tile anonymous variable (subsection 3.3.3) indicates that we are, not interested in the values of the function " c h i l d r e n " .
3.3.6 Tree Structure of Predicates Using a first, simplified approach of syntactic: analysis, tile sentences in a natural language are composed of a subject, a verb phrase and the object or tile predicate. The subject and the object are name expressions which consist of an article, an adjective and a noun, whereas verb phrases consist of a verb and an adverb. Hence tile phrase: the blond child slowly closed tile door
(1)
carl be analyzed syntactically by the use of a tree [Thay88, Xant90], whose final
Syntax of Data
227
nodes (Definition 2.8.9) are tile exact words of phrase (1):
sentence
subject
verb phrase
object
article
adjective
noun
adverb
verb
article
the
blond
child
slowly
closed
the
adjective
noun
door
Tile same technique of syntax analysis is used for tile predicates: each predicate corresponds to a tree.
Tile origin of tile tree, Definition 2.8.9, is the predicate
under consideration. Tile internal nodes are the functions which are included in the predicate, and tile final nodes are tile variables and the constants which appear in the functions and the predicate. Therefore data (,) of the previous section correspond to four trees: father
mother
child
child
nick
mary
anne
tim
whereas tile complex predicate (**) corresponds to the tree: family
fat her
mot her
nick
mary
children
anne
The expressions " l i k e s ( g e o r g e , i c e _ c r e a m ) ",
tim
" i c e _ c r e a m ( l i k e s ( g e o r g e ) ) ",
228
Logic Programming: The PROLOG Paradigm
and " g e o r g e ( l i k e s
(ice_cream)) "
/
have corresponding structures: ice_cream
george
likes
likes
george
ice_crealn
likes
george
ice_cream
By using the tree structure of predicates, PROLOG controls the success of the unification, as we will see in section 3.4. After the unification of two predicates, the corresponding trees must be the same, node by node and edge by edge.
3.3.7 The Lists Lists are simple catalogues of data enclosed in " [ " and " ] ", for example: [wine, soap, j ohn, X, house]. These lists, which constitute a fimdamental building block of LISP, [WiBe89], have been embodied in P R O L O G . Using lists we can collect a lot of information in one PROLOG object. For example, tile facts: likes(george, football). l i k e s ( g e o r g e , food). l i k e s ( g e o r g e , wine). can be given using a single list as the fact:
likes(george, [football, food, wine]). This means that the sequence of terms, f o o t b a l l ,
food, wine, is represented by
tile object:
[football, food, wine] In general, a list is composed of a sequence of terms. By using lists we achieve space efficiency and greater slickness when managing complex data.
Syntax of Data
229
Lists are generally defined recursively: (i)
The e m p t y
list, i.e., a list which does not contain any elements, is repre-
sented by "[ ]". (ii)
A non-empty
list is composed of two terms:
(1)
The first t e r m is called the h e a d of the list.
(2)
The second term consists of the rest of the list, and is called the t a i l or b o d y of the list
The head of a list can be an a r b i t r a r y P R O L O G term, a constant, a variable or a fllnction. T h e tail of a list has to be a list. For example, the head of the list:
[football, food, wine] is the constant, f o o t b a l l ;
and its tail is tile list:
[food,wine] Due to the recursive definition of functions in Predicate Logic, Definition 2.2.1, lists are not fllnctions.
Moreover, list elements are always selected based on a
relation or property they possess. For example, the list:
[meat,bread] m a y denote objects which have to be bought, in which case it is expressed by tile predicate:
shopping(meat, bread) Therefore a list can be considered as a predicate by the use of a p r e d i c a t o r ,
". ",
i.e., a special symbol denoting the existence of some predicate at tile corresponding place. For example, by writing . (salad, vinegar) we denote t h a t we Inade up the list:
[salad,vinegar]
230
Logic Programming: The PROLOG Paradigm
without specifying either tile qualities of the terms " s a l a d " and " v i n e g a r " , or tile relationship between them. Hence ". ( s a l a d , v i n e g a r ) " a generalized
predicate,
is considered to be
a predicate whose form we do not know precisely.
Therefore, we cannot query PROLOG: ? [salad, v i n e g a r ] .
[X,Y].
or
?
or
? IX, v i n e g a r ] .
because we do not know the predicate which corresponds to " . " .
Furthermore,
within the same program, more than one list can exist, all of them being expressed by the special symbol " . ". In general, a list can be written as a predicate: 9 (Head, T a i l )
The tail is also a list with a head and tail. If we also use " . " as a f u n c t o r , i.e., a symbol denoting the existence of some function at the corresponding place, then tile general form of a list is: 9 ( H e a d l , . ( H e a d 2 , . ( H e a d 3 , . . . . (Head,[ where
[ ] denotes
])...)))
tile e m p t y list.
For example the list [a, b, c] has the general form:
9 (a,. (b,.
[ 1)))
A list containing only one clement has the form:
[element] or
. (element, [
])
For the typical construction of a list we use trees. The tree structure of a list forms tile i n t e r n a l representation of lists with respect to the PROLOG interpreter.
o ~ C/) (1)
~
greater
greater or equal
=
tl, it is expressed by the set $2, where $1 ~= $2. In order to find the consequences of $1 and $2, in other words, in order to answer queries concerning $1 or
$2, we
assume that the Closed World Assumption holds
in both $1 and $2. This means that at time instant tl, the universe is closed and is expressed by the set of clauses $1; and at time instant t2, the universe is also closed and is expressed by the set $2. More information about Nonmonotonic Logics can be found in, for example, [Turn84] and [Besn89].
3.7 Expert Systems 3.7.1 Artificial Intelligence The field of A r t i f i c i a l I n t e l l i g e n c e , AI for short, deals with the developnmnt and formalization of intelligent methods and solution procedures for problems. By the term "intelligent", we mean methods which allow reasoning and judgement in a way comparable to the human thinking process. Research in AI aims at the construction of symbolic representations of the world, and covers areas such as robotics, natural language understanding, and expert systems.
276
Logic Programming: The PROLOG Paradigm
3.7.2 Expert Systems and Knowledge Management E x p e r t s y s t e m s arc computer programs capable of solving complex problems which require extensive knowledge and experience ill order to be solved [BuSh84]. The greatest difference between the traditional programs and expert systems, is that traditional programs manage data whereas expert systems manage knowledge. The differences between the two different kinds of programming can be presented in detail in the following table:
Data Management
Knowledge Management
Data Representation and Use
Knowledge Representation and Use
Algorithmic Procedures
Search Procedures
Repeating Procedures
Conclusive Procedures
Expert systems are specialized systems, meaning that they refer to subfields of specialized application fields; such as medical diagnosis systems, mechanical failure diagnosis systems, systems for the synthesis of complex chemical compounds, etc.. The means by which expert systems achieve their goals are based on sets of facts and sets of h e u r i s t i c s , i.e., rules for knowledge management. These facts and rules are developed by specialized scientists who work in the field to which the expert system has applications. We can say that this collection of facts and heuristics constitutes the knowledge of the system in this specific field, and hence expert systems are often called K n o w l e d g e B a s e d S y s t e m s . An expert system consists of a d a t a b a s e , an i n f e r e n c e m e c h a n i s m , and the
interface for the communication between the system and the uscr. The d a t a b a s e is a simple set of elements and states, which constitutes the description of the universe of the given field. Knowledge representation [Watt90] in systems based on rules is achieved by using rules of the form: IF (condition_l) a n d . . .
(condition_k)
THEN ( action ) Tile i n f e r e n c e m e c h a n i s m consists of a set of general purpose rules, which is used to guide and control the inference process. This mechanism implements
Expert Systems
277
general models, which characterize and formalize processing and resolution procedures of problems; such as the depth-first search, the backtracking and other general purpose inference procedures described in previol~s sections. In cases where the inference mechanism can become autonoinous with respect to the specialized knowledge which a given expert system contains, and work independently, it can be used for the development of new expert systems. Such systems with general inference power are called shells. The i n t e r f a c e is responsible for communication between the user and the system. In most cases, specialized subsystems which analyze and synthesize natural language (parsers) are used, in order to get a natural and friendly communication between the user and the system. The acquisition and formalization of knowledge needed to be embodied in a given expert system, is one of the most fundamental and, at the same time, difficult stages during the development of the system. Indeed, it is called a "knowledge acquisition bottleneck".
The difficulty lies in the provision and simultaneous
formalization of specific rules which express the procedures which a specialist in problem solution in the field follows. Therefore, the work of the k n o w l e d g e e n g i n e e r , in other words the person whose duty is to find and express these procedures, is the basic element in the successflfl development and use of an expert systein. In recent years, due to the importance of knowledge acquisition mechanisms, a u t o m a t i c k n o w l e d g e a c q u i s i t i o n s y s t e m s have been developed. The methods used are in many cases very successflll, and present great theoretic interest [O1Ru87, Quin86]. The t o o l s used in the development of expert systems are divided into two main categories [WeKu84]: the h i g h level s y m b o l p r o c e s s i n g l a n g u a g e s , and the g e n e r a l p u r p o s e e x p e r t s y s t e m s or shells. The most widespread high level symbol processing languages are LISP [WiBe89] and P R O L O G . The shells are used to facilitate and accelerate the development of special expert systems. With the use of special user friendly interfaces and the structures which they embody, they facilitate both the input and the revision, if needed, of an expert system's facts and rules.
Furthermore, they embody an autonomous inference
procedure capable of transacting and deriving conclusions from different databases. This offers the ability to develop expert system prototypes rapidly.
278
Logic Programming: The PROLOG Paradigm
3.7.3 An Expert-System for Kidney Diseases The K i d n e y E x p e r t S y s t e m , KES, is a system which diagnoses kidney diseases, and was developed with the cooperation of the Logic and Logic Programming Group of the department of Mathematics in the University of Patras, and a team of specialized doctors from the University Hospital of Patras. In the presentation which follows, the clauses and procedures which implement KES have been limited only to those clauses and procedures which describe the structure and operation of the system, and at the same time display the use of PROLOG in the development of expert systems. The rules which constitute the special purpose Knowledge Base of KES are the formalization of the manner in which the doctor makes a diagnosis based on the symptoms of the patient: the fact that the patient is relatively old, that his urine presents a qualitative change as well as his blood, and that the kidneys appear swollen. If all these conditions are met, then the patient probably suffers from hydronephrosis. Therefore, the rules of KES are of the following general form:
and/or and/or
the data of the patient satisfy some given requirements reported(X) observed<symptom_l > ....................
and/or and/or
observed<symptom_k > found< lab_result_l >
and/or
found( lab_result_n ) the diagnosis is (disease) with probability P
IF
and
THEN
These rules were formed under the guidance of specialized doctors. Therefore, for hydronephrosis, the corresponding rule is: d i a g n o s e ( h y d r o n e f r o s i s , 0.7):- a g e ( H ) ,
S > 11,
reported(change_in_quality_of_urine), observed(bloodurine), observed( twos i de d_k i daney_e xp an s i on ).
where 0.7 is the probability that the given patient has hydronephrosis. The theoretic probability measure which is appended to each diagnosis is an a priori assessment of probability. Therefore, the possibility of diagnosing tile same disease through different paths and with different probability statements is offered.
Expert
The basic predicates included in
KES
Systems
279
are:
d a t a : Accepts four arguments: sex, age, first name, and last name of the patient. o b s e r v e d : Accepts one argument: the name of tile symptom observed by the exanliner.
found : Accepts one argument: the name of the result of a possible laboratory test. r e p o r t e d : Accepts one argument: one of pain, c h a n g e _ i n _ q u a l i t y _ o f _ u r i n e , change_in_quantity_of_urine, change_in_frequency_of_urination.
The
predicate r e p o r t e d is use(t for the grouping of symptoms. Therefore a rule of the above general format which does not contain in its body the predicate r e p o r t e d , with any argument, will not be activated, resulting in the acceleration of tile inference procedure. d i a g n o s e : Accepts two arguments: tile name of the (tiagnosis and tile corresponding probability that the patient has the disease stated by d i a g n o s e . After the description of tile t)a.sic predicates and tile st)ecial rule of
KES, we
carl
nmve on to the presentation of the t)rocedures an(t the corresponding programming clauses which inlplement tile inference inechalfisin of the system. At tile begiilning, we have to define the input procedure of tile patient's data, in other words, predicate data: data(S, A, L, F ) : write(' 'Patient's sex(m/f)' '), read(S), nl, write( ccPatient's age''), read(A), nl, write(' 'Patient's last name''), read(L), nl, write( ccPatient's first name''), read(F), nl assertz(sex(S)) , assertz(age(A)), as s e rt z (surname(L)), assertz(name (F)). Next the input procedure for the symptoms reported by tile patient has to be defined, in other words, predicate r e p o r t e d : reported(X):- write(' 'Reported from patient'' , X , read(ANSWER), nl, A N S W E R ---- r Cy ' '
~sertz(reported(X)).
"(y/n)?"),
280
Logic Programming: The PROLOG Paradigm
Now, tile input procedures for the symptoIns observed during the examination, as well as the possible laboratory results which the doctor might have available, have to be defined: observed(S) :. write(' 'Observed during the examination' ',S," (y/n)?' '), read(ANSWER) , nl, ANSWER ~ c c y, ,
~ssert-(observea(S)). found(R):-
write(''Laboratory read(ANSWER), nl, ANS%VER ~ c c y, ,
result'',R,''observed
(y/n)?''),
~ssert-(observed(8)). In tile above definitions, the existence of assertz basic implementation
results in tile insertion of tile
of the head of the corresponding
rule, when
it is satisfied.
Therefore, tile interaction between the system and the user during the inference procedure is recorded and stored for any fllrther processing. Finally we define the general diagnose predicate: proceed_in_diagnose :-- !, data(S, A, L, F ) , di agnose ( D, P ) , a s s e r t z (diagnose (D, P)). If tile above corporates
predicate
there will be a dialogue diagnosis
is set as a query
heuristics, then PROLOG
which
Let us assume
between
if we
assume
that the database
on to derive conclusions.
tile user and
can be derived, depending for exalnple
and
will move
PROLOG
which
in-
In reality,
will result in tile
on tile user's responses.
that tile database
contains
the following
diagnose
rules: diagnose(acute_pyelonephritis,
0.7) :reported(pain), observed(high_fever_with_shiver), observed(bloodurine).
diagnose(acute_pyelonephritis,
0.8) :age(A),
A
1.80.
t a l l ( X ) :- height(X, f, Y), Y - > 1.75. normal(X) :- height(X,m, Y), Y < 1.70, Y < 1.80. normal(X) :- height(X,f,Y), Y > 1.60, Y < 1.75. s h o r t ( X ) : - height(X,m,Y), Y = < 1.70. short(X) :- height(X, f, Z ) ,
Y = < 1.60.
Z > W.
290
Logic Programming: The PROLOG Paradigm
3.10.5 Consider tile following tables of materials and their suppliers for a warehouse containing spare parts of cars: SUPPLIER
ARTICLE
SUP.CODE
PROFESSION
CITY
001
John
Manufacturer
ATHENS
002
Nick
Importer
PATRAS
010
John
Entrepreneur
SAL/ICA
110
Nick
Importer
PIRAEUS
ART.CODE
PRODUCT
MODEL
WEIGHT
003
Oil
30
ATHENS
004
Tyres
157/75
PATRAS
005
Lamps
RAAI
SAL/ICA
013
Oil
60
PIRAEUS
SUPPLIES
(a)
NAME
SUP.CODE
ART.CODE
QUANTITY
001
005
150
002
003
200
010
OO4
030
110
013
250
Represent the d a t a in the above tables in a PROLOG database containing 2-ary predicates only.
(b)
Give the adequate Horn clauses for the queries:
(1) (2)
W h a t are the names of the oil suppliers? Which city are tyre suppliers in, and which city are the lamp suppliers in?
(3) (4)
W h a t does John supply? Which oil suppliers supply less than 400 tons and are established in Patras?
(5)
Who are the lamp suppliers, and who are the tyre suppliers?
Exercises
291
Solution:
(a)
We introduce tile predicates: name(X, Y) profession(X, Y) city(X, Y) product (X, Y) type(X, Y) weight (X, Y) supp_art icle (X, Y) quantity(X, Y)
/ 9 / 9 / 9 / 9 /, /. / 9 / 9
X X X X X X X X
supplier supplier supplier product product product supplier supplier
code, code, code, code, code, code, code, code,
We thus form the following database: name(001, john). name (002, nick). name(010, john). name(110, nick). profess i on(001, manuf act urer). profession(002, importer). profession(010, entrepreneur). profession(110, importer). city(001, athens). city(002, patras). city(010, salonica). city( 110, piraeus). product (003, oil). product (004, tyres).
product (005, lamps). product (013,oil). type(003, ' ' 3 0 ' ' ). type(004, ' ' 157/75' ' ). type(005, ' 'RAAI' ' ). type(013, ' ' 6 0 ' ' ) .
weight (003,300). weight (004, 2000). weight (005, I0). weight (013,500). supp_article (001,005). supp_art ie ie (002,003). supp_art icle (010,004). supp_article (II0,013).
Y Y Y Y Y Y Y Y
supplier name 9 / supplier profession 9 / supplier city 9 / product name 9 / product type 9 / product weight 9 / article code 9 / product quantity 9 /
292
Logic Programming: The PROLOGParadigm
(b)
(1)
supp_oil (NameSupplier):product (CodProduct, oil),
supp_article (CodSupplier, CodProduct), name (CodSupplier, NameSupplier).
(2)
city_supp_tyres (CitySupplier):product (CodProduct, tyres), supp_article(CodSupplier, CodProduct),
city(CodSupplier, CitySupplier). city_supp_lamps (Cit ySupplier):product (CodProduct, lamps), supp_article(CodSupplier, CodProduct), city(CodSupplier, CitySupplier). (3)
supplies(NameSupplier, NameProduct) :name (CodSupplier, NameSupplier), supp_article(CodSupplier, CodProduct),
product(CodProduct, NameProduct). ? supplies(john, NameProduct).
(4)
supp_oil_le s s_400_patras (NameSupplier):product (CodProduct, 0il), weight (CodProduct, WeightProduct), WeightProduct < 400, supp_article(CodSupplier, CodProduct), city(CodSupplier, patras), name (CodSupplier, patras).
(5)
suppl ie s_lamps (NameSuppl ier):product (CodProduct, lamps), supp_article (CodSuppliers, CodProduct), name(CodProduct, NameSupplier). supplies_tyres(NameSupplier) :- product(CodProduct,tyres), supp_article(CodSupplier, CodProduct), name(CodProduct, NameSupplier).
Exercises
293
3.10.6 Analyse the PROLOG functioning of the following program, and give the corresponding state tree structure. A(a,b).
/,
B(X) :- C(X, r ) .
/ 9 2 9/
1 ,/
C(b,a) :-- A(a,b).
/,
C(X, Y):- A(X, Z). ? B(X).
/ 9 4 9/
3 9/
Solution: The goal B(X) is unified with the head of rule / 9 2 9
The new subgoal is
C(X, Y). C(X, Y) is unified with the head o f / . 3 9 for X / b , Y/a. However, A(a,b) is unified with fact / . 1 9 hence the program succeeds for X/b, Y/a, and PROLOG answers-
X--b Y-b PROLOG now frees variables X and Y from their values, and by backtracking it tries to satisfy the subgoal C(X, Y) differently. C(X, Y) is unified with the head of rule / 9 4 9 The new subgoal is A(X, Y), which is unified with fact / 9 1 9 / for
X/a, X/b. After having freed the variables and backtracking once again, is no longer able to satisfy the goal or any subgoal, and answers:
X-a Y=b The state space tree is" ? B(X). 2
X/b,Y / ~ ? A(a, b). yes
? c(x, Y).
? A(x, Y). x/~, Y/b ? A(a, b). yes
PROLOG
294
Logic Programming: Tile PROLOG Paradigm
3.10.7 The police have found an unfortunate woman by the name of Suzannc murdered, with her head smashed by a blunt instrument. The main roles in this sad story are played by Alan, 35, a butcher as well as a thief, and John, 25, a soccer player who is sentimentally attached to both Suzanne and Barbara. B a r b a r a is a 22 year old hairdresser who is married to Bert, a 50 year old lame joiner.
During tile
investigation, a revolver was found in John's house. For the police, jealousy and a planned robbery are possible motives. Help them find the murderer. Solution:
/ 9 assumptions and claims formulae from the police investigation 9 / person(john, 25, m, f o o t b a l l _ p l a y e r ) .
/ 9 m : male
*/
person(allan, 35, m, butcher). person(barbara, 22, f, h a i r d r e s s e r ) . person(bert, 50, m, carpenter). person(allan, 35, m, pickpocket). had_affair(barbara, john). had_affair(barbara, bert).
had_affair(susan, john). killed_with(susan, club). motive(money). mot i ve (3ealousy). owns(bert, wooden_leg). owns(john, pistol).
/ 9 "common logic" assumptions 9 / operat es_ident ically (wooden_leg, club). operates_identically(bar, club). operate s_ident i cally (pair_of_scissors, knife). operates_identically(football_boot, club). owns_probably(X, football_boot) :- person(X ..... owns_probably(X, pair_of_scissors) : person(X .... owns_probably(X, Object) :- owns(X, Object).
football_player). ).
Exercises
295
/ 9 assumptions about the murderer's motives- two categories of suspects:, / / 9 s u s p e c t s by their capability of c o m m i t t i n g tile m u r d e r , and / 9 s u s p e c t s by their motives
9/
9/
suspect_by_capability(X) :- killed_with(susan, Weapon), operates_identically(Object, Weapon), owns_probably(X, Object). suspect_by_motive(X):
motive(jealousy), person(X, _
, m , __ ),
had_affair(susan, X).
suspect_by_motive(X) :- motive(jealousy), person(X,_
),
,f,_
had_affair(X, Man) , had_affair(susan, Man). suspect_by_raotive(X) :- motive(money) ~ person( X . . . . .
/ 9 non-probabilistic " c o m m o n logic" a s s u m p t i o n
pi ckpo cke t ).
9/
mostly_suspected(X) :- suspect_by_capability(X), suspect_by _mot i re( X ). By its definition, the predicate m o s t l y _ s u s p e c t e d has no probabilistic interpretation, and depends only on the two predicates s u s p e c t _ b y _ c a p a b i l i t y and suspect_by_motive. Justify by a full track down the answers to the following PROLOG queries: ? suspect_by_capability (X). X = bert
X =john ? s u s p e ct_by_mot i v e ( X ) .
X = john X = barbara
X = allan ? mostly_suspected(X). X =john
296
Logic Programming: The PROLOG Paradigm
3.10.8 Reading and evaluating kilometric distances on a m a p often proves to be a very complex procedure in the preparation of an excursion. Hence a PROLOG program would be very useful.
Solution: As a sample program, consider: / 9 data from a map 9 / road(kalamata, t r i p o l i , 90).
road(tripoli, argos, 60). road(argos, korinthos, 49). road(korinthos, athens, 83). / 9 rules for the calculation of kilometric distances 9 / route(Wownl, Town2, Distance) :- road(Townl, Wo~m2,Distance). route(Wownl, Town2, Distance) :- road(Townl, X, Dist 1), route(X, Town2, Dist2), Distance -- Dist 1 + Dist2. W h a t is the distance between K a l a m a t a and Korinthos? ? route(kalamata, korinthos, X).
]
X = 199
3.10.9 Give the recursive definition of the predicate f a c t o r i a l ( X , based on: (a)
1! -
1
(b)
n! =
(n-1)!,n
Construct the state space tree of the program for the query:
? factorial(3, X).
Y),
Y being X ! ,
Exercises
297
Solution: Here (a) gives the b o u n d condition and (b) gives the recursive step. T h e definition we are seeking is thus: f a c t o r i a l ( I , 1) :- !.
/.
1 ./
/.
2 ./
f a c t o r i a l ( N , Factor) :- N1 = N - 1, f a c t o r i a l ( N 1, F a c t o r l ) , F a c t o r = N 9 F a c t o r l. T h e cut in f o r m u l a / ,
1 9
has been used in order to prevent P R O L O G from
b a c k t r a c k i n g d u r i n g the calculation of 1!.
T h e s t a t e space for the query: ? f a c t o r i a l ( 3 , X). is depicted by the following tree:
? factorial (3, X ) . 2 N/3, Factor/X
? N 1 = 2 , factorial(2, F a c t o r l ) , F a c t o r = 3 9 Factor1. 2 N/2, Factor/Factor1
? N 1 _.l.~...f.actorial(1, F a c t o r l ) ,
........... cut in 1
Factor = 2 9 F a c t o r l .
Factorl = 1
Factor 1 = 2 , 1 = 2
Factor - 3 , 2 yes
= 6 X --6
298
Logic Programming: The PROLOG Paradigm
3.10.10 Define recursively the predicate exp(x, y, z), m e a n i n g
"x y =
z",
which defines
the exponential function based on: x~ =
1
Xy
X.
--
X y- 1
3.10.11 Assume we wish to define a predicate declaring the n u m b e r of parents of a person x. According to the Bible, we have to declare t h a t A d a m and Eve had no parents. We thus write the p r o g r a m P I : /.
program P1 9 /
number_parents(adam, O) :- !.
/ 9 1 9/
number_parents(eve, 0 ) : - !.
/ 9 2 9/
number_parents(X, 2).
/ 9 3 9/
where we use the cut to avoid backtrackings in queries of the form: ? number_parents(adam, X). and .7 number_parents(adam, 0).
(a)
W h a t will PROLOG answer to the query:
(,)
? number_parents(eve, 2). and why?
(b)
W h a t will PROLOG answer to (.) if, instead of the p r o g r a m P1, we use the following p r o g r a m P2: /,
program P2
9/
number_parents(adam, N) :- !, N = 0. number_parents(eve, N) :- !, N = 0. number_parents(X, 2).
/, /, /,
1 ,/ 2 ,/ 3 ,/
Exercises
299
and what is the answer to
? number_parents(X, Y). given by the p r o g r a m P 2 ? 3.10.12 Define with a PROLOG p r o g r a m the absolute value of an integer.
3.10.13
Write a PROLOG p r o g r a m checking whether an element (a person or a list) is part of a list L, and examining all the elements of L.
Solution: member(H, [ H : _ ]). member(I, [ _ : T ] ) : - member(I, T).
3.10.14 Check whether the list H is m e m b e r of the list L, and find the first element of L.
Apply this to the list L - [ a , b, c].
Solution: member(H: [H _ ]):- !.
/,
1 ,/
member (I, [ _ : T]) :- member (I, T).
/*
2 */
The only solution found by PROLOG for the query:
? member(X, [a, b, c]). is X = a. The existence of the cut, !, in clause / 9 1 9 / makes the finding of other possible solutions impossible.
3.10.15 Write a program collecting the first n elements of a list.
300
Logic Programming: The PROLOG Paradigm
3.10.16 Write a program defining the subtraction of two lists. Solution:
subtract(L,[
],L) :- !.
s u b t r a c t ( [H : T], L, U):- member(H, L),
!, subtract(T, L, U).
s u b t r a c t ( [H : T], L, U]) :- !, subtract(T, L, U). subtract( . . . .
[ ]).
sambar(S,[S:
_ ]).
member(I, [ _ : T]):- member(I, T).
3.10.17 Define by means of a program, the relations member,
subset
and
intersection
of set theory. Solution:
I,
~ ,I member(H,[H : _ ]) member(I, [ _ : T]):- member(I, T).
/,
c ,/ subset([H : T], I) :- member(H, I ) , subset([
/,
N
subset(T, I).
],I).
*/
intersection([
],X,[
]).
i n t e r s e c t i o n ( [ X : T],I, [X: Z]):- member(X,I),
!,
i n t e r s e c t i o n ( T , I, Z).
3.10.18 Assume we are given a list of eight animals, V = [al, a2, r l , bl, b2, hl, ol, o2], which are classified as follows:
Exercises
301
F i s h 9 a l , a2
Animals
Reptiles "rl
N on- a q u a t ic
Herbivorous 9 hi
M a m m als
B i r d s 9 bl, b2
O m n i v o r o u s " 01, 02
Write a PROLOG database describing the above classification, and a PROLOG program replying to the queries(a)
W h a t is the classification of tile animal X?
(b)
Which animals have classification Y?
Solution:
aquatic(aquatic, [al, a2]). reptile(reptile, [rl]). bird(bird, [bl,b2]). mammal(V, X): herbivorous(V~ X) ; omnivorous(V~ X). herbivorous (herbivorous, [hi]). omnivorous(omnivorous, [ol,02]). classification(A, C) :- animal(C, L), member(A, L). animal(V~ X):- aquatic(V~X); terrestrial(V~X). terrestrial(terrestrial(V),X):- reptile(V~X); bird(V~X); mammal(V~X). In answer to the classification queries: (a)
? classification(X,
(b)
?
animal(X,
C).
Y).
3.10.19 Write a PROLOG program displaying a list of names in alphabetical order. The names in the list must be separated by a full stop, and the word " s t o p " must appear after the last name.
302
Logic Programming: The PROLOG Paradigm
Solution: The alphabetic ordering of the names can be achieved by means of a tree, every node of which is the predicate node(L, W, R), interpreted as "L is the next left node of the present node, R is its next right node, and the next middle ,lode is the name just read by the program". Each name will be compared to the origin, and recursively to the next left and right nodes of the corresponding node. This procedure will end when tile next node is a variable, or when the name is already in the tree. For example, for tile names john. mary. james, edward, stop.
we will have the following tree:
node
node
node
X
edward
james
john
node
U
Z
mary
Y
The program we are seeking is: order(X) :- reading(X), name(W, W l ) ,
c l a s s i f y ( W 1 , X , Y), order(Y).
order(X):- nl, write(" ordered words:''), nl, write_tree(X), nl, nl, write(''Done.''), nl. reading(W):-read(W),
W----stop,
!, fail.
classify(W, node(L, W, R),node(L, W, R)):- !.
classify(W, node(L, W1, R),node(U, W1, R)):lower(W, W1), !, classify(W, L,U). classify(W, node(L, W1,R),node(L, W1,U)):- !, classify(W,R, U).
lower([ ], _ ):- !, lower(Y,T). lower([W: Y] ,[Z, T] ) :- X < Z. write_tree(X):- raY(X), !. write_tree(node(L, W, R)) :- !, write_tree(L), name(W, Wl), write(W, Wl), write('C."),
write_tree(R).
V
Exercises
303
To run the program, the following instruction must be given: 9-
order(X).
and then the list of names to be ordered.
3.10.20
Write a PROLOG program forming a tree with a given list of numbers and printing that list as a tree with its elements ordered. 3.10.21 Write a program representing the diagram: e
T a
T d
> b
~f
T g
T > c
and finding the next node of c and b.
Solution: The arrangement of tile nodes is declared by tile predicate a r c ( x , y ) , whi('.h is interpreted as "an edge starting at x and ending at Y"- Tile p r o g r a m we are seeking is: arc(a, b). arc(c, b). arc(d, a). arc(d, c). arc(b, e).
~rr
f).
arc(f,g). arc(b, g).
'.~ ar~(c, z). ? arc(g, y).
0
,.~
~'~
0
,'~
~.~
~
..,
~
~'
~-
o.
.~"
~
~
~
N
.~
N
~
.,,
N
0
_.
~.,.
_
0
e-.,-
~
~"
~
~
=
~
~
~
~
9.
~ i"
~-~
~
0 0
0 0
~.
~
i~.
~
~
~.
W
i~.
0 0
o
~
~
m ~l
c~
m
c~
o
""
..
m
. o
o
o
~n cr~
0~
~.,
0 O~
c-l,-
L'O
O0 9
~"
~
0
"~
o
o
c~
oo o
Exercises
305
Solution" The given points are described by the predicate: p o i n t ( x , y)
where x and y are the coordinates of the points. The equation of the interior of the circle is" (x-c)
2+(y-d)
2