Topics in AI – Two valued Logics
This lecture introduces the following • Predicate Logic • Horn Clause Logic • Prolog • Negation and the closed world world assumption • Reasoning under incomplete knowledge • Monotonicity • Proofs Example 1 How do we represent the assertions? “All the nice girls like a sailor, Mary is a nice girl, George is nice, Tom is a sailor” We i. Identify the domain of discourse ii. Identify the named objects iii. Identify the relations (or predicates) (between the objects) iv. Identify any functions The Domain of Discourse The domain of discourse must be sufficiently abstract to include the named individuals. In the above example a suitable choice for the domain of discourse is the collection of all People. The Named Objects These are mary, tom and george (who are now assumed to be people) Relations or Predicates (or Boolean valued functions) In this example identify Nice/1, Girl/1, Sailor/1, Sailor/1, Likes/2 Functions There are none (but see below) in this example
We may now represent the above scenario in terms of a language (with an inference system). A language used to express problems of this nature in AI is that of First Order Predicate Logic (FOPL). The language of FOPL consists of Non-Logical Symbols i) A set of constant symbols (to name particular individuals such as mary, tom etc. - these depend on the application) ii) A set of predicate symbols (to represent relations such as Nice, Likes etc. -these depend on the application) iii) A set of function symbols (to represent functions - these depend on the application) Logical Symbols iv) A set of variables (to refer to arbitrary individuals), we will use x,y,z,x1,x2,… v) The logical connectives -, . , u ,w , ¬ (to capture and, or, implies, iff (bi-implication) and not) vi) The Universal Quantifier, ∀ Quantifier, ∀ : : and the Existential Quantifier, ∃ Quantifier, ∃ : : (to capture “all”, “every”, “some”, “few”, “there exists” etc.) vii) Normally a special binary relation of equality (=) is considered (at least in mathematics) as part of the language.
Languages in FOPL differ only in their non-logical symbols (constant, predicate and function symbols); thus when describing a suitable FOPL to represent a given scenario, we need only specify such symbols. Returning to our example above, we will use th e constants and predicates identified above; the variables will be assumed to range over the set (collection) of all people – that is, they take (names of) people as values.
We now may express the assertions syntactically as i. OR ii OR iii
∀x.∀y.( Sailor(y) . Nice(x) . Girl(x) u Likes(x,y)). “Every nice girl loves every sailor” ∃y.( Sailor(y) . ∀x.(Nice(x) . Girl(x) u Likes(x,y)). “All the nice girls like a particular sailor ” ∀x.(Nice(x) . Girl(x) u ∃y.(Sailor(y) . Likes(x,y)). “Every the nice girl likes some sailor”
Nice(mary), Nice(george) Girl(mary)
“mary is nice”, “george is nice” “mary is a girl”
Notice that the formal representation makes clear how we wish to interpret the assertion “All the nice girls love a sailor”. Prolog Prolog is a programming language based upon a subset of FOPL called Horn Clause Logic (HCL). A Horn Clause is of the form
∀x1... ∀xn.(P1 . .... . Pk) u Q where P1, ...., Pk (k m 0) and Q are all positive literals each containing at most the variables x1,...,xn. (n m 0). Notice that each variable is universally quantified. ∀x1... ∀xn.(P1 . .... . Pk) u Q is logically equivalent to ∀x1... ∀xn.¬P1 -.... -¬Pk - Q, which if we assume all variables are universally quantified may be written as ¬P1 -.... -¬Pk - Q. Note, however, ¬P is NOT a Horn clause, nor for that matter is P - Q or ∃x.P. The Horn Clause ∀x1... ∀xn.(P1 . .... . Pk) u Q is written in PROLOG as q :- p1, ....,pk. Note that PROLOG is case sensitive - all variables are written in upper case, constants, functions and relations are written in lower case. All clauses terminate with a period (.). ∀x.∀y.( Sailor(y) . Nice(x) . Girl(x) u Likes(x,y)) is an example of a Horn Clause and thus may be written in Prolog as likes(X,Y) :- sailor(Y),nice(X),girl(X). Thus the Prolog program for the above scenario is /*
Prolog Program 1 for “All the nice girls like a sailor, Mary is a nice girl, George is nice, Tom is a sailor”
*/ nice(mary). nice(george). girl(mary). sailor(tom). likes(X,Y) :- sailor(Y),nice(X),girl(X). How do we represent the alternative interpretations for “all the nice girls like a sailor”? First and foremost we need to eliminate existential quantifiers. Consider
y Sailor(y)
x.(Nice(x)
Girl(x)
Likes(x,y)).
As we have seen this means that some particular sailor is liked by all nice girls, in order to eliminate the existential quantifier we simply introduce a new named sailor, say nelson1 who is liked by all nice girls giving Sailor(nelson) . ∀x.(Nice(x) . Girl(x) u Likes(x,nelson)). This is a conjunction of two clauses, namely Sailor(nelson) ∀x.(Nice(x) . Girl(x) u Likes(x,nelson)). These two sentences are now Horn Clauses and the corresponding Prolog program is /*
Prolog Program 2 for “All the nice girls like a sailor, Mary is a nice girl, George is nice, Tom is a sailor”
*/ nice(mary). nice(george). girl(mary). sailor(tom). sailor(nelson). likes(X,nelson) :- nice(X),girl(X). x.(Nice(x) Girl(x) y.(Sailor(y) Likes(x,y)). Consider Here we are required to eliminate the existential quantifier, however in this representation; the sailor “y” depends on the particular nice girl “x”. (Different nice girls may like different sailors). To represent 2 the observation that y “depends on” x, we may introduce a function symbol “f” and write y = f(x). Given this we may now translate the above sentence to ∀x.(Nice(x) . Girl(x) u (Sailor(f(x)) . Likes(x,f(x)))). 3 This is logically equivalent to the two Horn Clauses ∀x.(Nice(x) . Girl(x) u Sailor(f(x))). ∀x.(Nice(x) . Girl(x) u Likes(x,f(x)))).
/*
Prolog Program 3 for “All the nice girls like a sailor, Mary is a nice girl, George is nice, Tom is a sailor”
*/ nice(mary). nice(george). girl(mary). sailor(tom). sailor(f(X)) :- nice(X),girl(X). likes(X,f(X)) :- nice(X),girl(X). Some Semantic issues: There are three different Prolog programs, one for each different interpretation of the assertion “All the nice girls like a sailor”. In each case the named objects in the corresponding domain of discourse are different. Prolog Program 1 The named constants are mary, george, tom
Prolog Program 2 The named constants are mary, george, tom, nelson Prolog Program 3 The named constants are mary, george, tom, f(mary), f(george), f(tom), f(f(mary)), f(f(george)) etc. The introduction of the function symbol f has extended the domain of discourse to an infinite set. This is a consequence of the free semantics given to Horn Clause Logics.
1
nelson is known as a Skolem constant f is known as a Skolem function, its purpose is to select a sailor, y who is liked by the nice girl, x. 3 A u (C . D) is logically equivalent to (A u C) . (A u D) 2
Negation: Suppose we want to assert that mary is not a sailor. This may be achieved in Prolog (in FOPL it is represented as ¬sailor(mary)) by explicitly adding the clause
sailor(mary) :- !,fail. Ignoring the cut (!) for the time being, the above clause says that the goal clause sailor(mary) will result in failure (false or no) – that is sailor(mary) is false! However, as it stands, this clause is redundant for the following very important reason. Prolog employs the closed world assumption (cwa); this means that any assertion that is not a consequence of Prolog’s inference system is assumed to be false; in particular the goal clause sailor(mary) cannot be proved and hence is taken (by Prolog) to be false. However suppose we are now told that everyone except mary is a sailor, we may express this fact as sailor(mary) :- !,fail. sailor(_). For the goal clause ?-sailor(mary), both of the above clauses apply; the first clause returns No (or failure) whereas the second clause returns Yes or success. Prolog will search the program clauses in the order that they are declared; the purpose of the cut in the first clause prevents Prolog backtracking to search for the alternative clauses; the absence of the cut would enable us to deduce sailor(mary). Prolog includes as part of its syntax the predicate not/1. Suppose we wish to show that tom is not a girl. The goal clause for such a query is ?-not(girl(tom)). Prolog employs negation by failure (nbf) using the cwa to evaluate this goal. In particular, ?-not(girl(tom)) holds provided girl(tom) fails – this it does by virtue of there being no clauses to show that tom is a girl, hence the goal clause ?-not(girl(tom)) returns true. It is worth observing that neither Girl(tom) nor its negation ¬Girl(tom) are provable from the axioms within FOPL. FOPL neither adopts the closed world assumption nor negation by failure! Example 2 Consider the following assertions
All penguins are birds Polly is a bird Pingu is a penguin Birds typically fly Assuming the domain of discourse is Animals, we can represent the first three assertions in FOPL as ∀x.(Penguin(x) u Bird(x)) Bird(polly) Penguin(pingu) However the fourth assertion finds no simple expression in FOPL; the qualifier, “typically” cannot be expressed within this formalism. Why not? What is wrong with the following argument? Introduce an “Abnormal” predicate and write ∀x.(Bird(x) . ¬Abnormal(x) u Flies(x)) to represent the general case and describe the Abnormal predicate axiomatically as ∀x.(Penguin(x) u Abnormal(x)) This appears to capture our intention except that we cannot prove that “polly can fly”, this is contrary to the way that we reason under incomplete knowledge – in particular, we assume that polly is a normal bird i.e. can fly, unless we are told otherwise! However, a consequence of negation by failure (under the cwa) is that the above assertions may be easily expressed in Prolog as follows /*
*/ bird(X) :- penguin(X). bird(polly). penguin(pingu). flies(X) :- bird(X),not(abnormal(X)). abnormal(X) :- penguin(X). Now the goal clause “?-flies(polly).” gives the value true. The reason why this should arise is that the clause “abnormal(polly)” fails by the cwa, hence “not(abnormal(polly))” holds true by “negation by failure” and since “bird(polly)” holds then so does “flies(polly)”. Suppose now we subsequently discover that polly happens to be a penguin. The clause “penguin(polly)” is now added to the above set of assertions giving the program /* */ bird(X) :- penguin(X). bird(polly). penguin(pingu). penguin(polly). flies(X) :- bird(X),not(abnormal(X)). abnormal(X) :- penguin(X). The addition of this extra assertion will no longer enable us to deduce (quite correctly) “flies(polly)”. This form of reasoning is an example of non-monotonic logic; a logic is said to be monotonic if adding to the assumptions does not reduce the number of valid consequences. FOPL is monotonic, Prolog is NOT monotonic. Example 3 PROLOG's Deductive Apparatus PROLOG adopts a goal directed proof strategy. We will demonstrate Prolog's search strategy by the way of another example. Let us reconsider the following blocks world
e
b
described in Prolog by on(d,a). on(c,b). on(e,d). on(e,c). above(X,Y) :- on(X,Y). above(X,Y) :- on(X,Z),above(Z,Y).
/* /* /* /* /* /*
on.1 on.2 on.3 on.4 above.1 above.2
The following diagram is the Prolog search tree4 for the goal clause above(d,Y).
above(d,Y)
on(d,Y)
on(d,Z1),above(Z1,Y)
Y=a Success
Z1 = a above(a,Y)
on(a,Y) Fail
on(a,Z2),above(Z2,Y) Fail
We can relate such a search tree to a trace of the Prolog program given below. Welcome to SWI-Prolog (Version 3.2.8) Copyright (c) 1993-1998 University of Amsterdam. All rights reserved. For help, use ?- help(Topic). or ?- apropos(Word). d:/prolog/blocks.pl compiled, 0.00 sec, 932 bytes. ? Action (h for help) ? trace above(d,Y). Call: ( 7) above(d, _G160) ? creep Call: ( 8) on(d, _G160) ? creep Exit: ( 8) on(d, a) ? creep Exit: ( 7) above(d, a) ? creep Y=a; Redo: ( 7) above(d, _G160) ? creep Call: ( 8) on(d, _L146) ? creep Exit: ( 8) on(d, a) ? creep Call: ( 8) above(a, _G160) ? creep Call: ( 9) on(a, _G160) ? creep Fail: ( 9) on(a, _G160) ? creep Redo: ( 8) above(a, _G160) ? creep Call: ( 9) on(a, _L158) ? creep Fail: ( 9) on(a, _L158) ? creep Fail: ( 8) above(a, _G160) ? creep Fail: ( 7) above(d, _G160) ? creep No [debug] ?Additional Reading (reference only) PROLOG Programming for Artificial Intelligence Ivan Bratko Addison Wesley 4
It is also known as an “and/or” graph or hypergraph.
Prolog Practical
At the end of this practical you should be able to 1. Write and Execute a simple PROLOG program as given. 2. Create and execute a Prolog program from a natural language description. First of all, if you have not already done so, you will need to install Prolog on your system, to do this visit the site www.swi-prolog.org and select the down load option followed by stable releases, choose the appropriate implementation and follow your nose5. Run your Prolog interpreter in the normal way choosing the file/consult option to read your Prolog program. To Write a program in PROLOG Create a new folder (file and new) called "Prolog programs" Select this folder and create a new document called "blocks1.pro" Open this document and create the following program (be careful)
on(d,a). on(c,b). on(e,d). on(e,c). above(X,Y) :- on(X,Y). above(X,Y) :- on(X,Z),above(Z,Y). Save (this file) As blocks1.pro (this is the file that the Prolog interpreter reads) If all is well, run your Prolog interpreter against this program and try these goal clauses (remember to type ; for more solutions) ?- on(X,b). ?- above(c,Y). ?- above(X,table). Now it's your turn Exercise 1 Represent the following scenario in FOPL and Prolog. “buster is a terrier, benji is a spaniel, all spaniels are dogs and all terriers are dogs. All terriers chase all dogs” Run your Prolog code for the goal clauses ?-dog(X). ?-chases(X,benji). ?-chases(X,buster). Exercise 2 Represent the following scenario in FOPL and Prolog. “tom loves sue. jane loves anyone who loves tom. brenda and mary love tom.” Run your Prolog code for the goal clauses ?-loves(X,tom). ?-loves(tom,Y). ?-loves(jane,Y). Exercise 3. Express the following scenario in FOPL and Prolog
“Mickie and Jerry are mice. Tom and Felix is a cat”. Felix chases all mice whereas Tom chases everything”
5
I chose the extension to my Prolog programs to be “pro”
Using Prolog, find out a) who chases Jerry b) who is chased by Tom c) does felix chase tom Exercise 4 The following exercise is quite demanding. Enter the clauses in a piecemeal way
a) Create a program in PROLOG to express the following facts. You should use the predicates parentof(X,Y) male(X) female(X)
- read "X is a parentof Y" - read as "X is male" - read as "X is female"
WARNING: Prolog programming is not always a happy experience. Try to develop you programs using a piecemeal approach and write simple goal clauses to test each part of your program as you go along. Facts david and janet are the parents of john and emma. (4 clauses) ron and eva are the parents of david. (2 clauses) jack is a parent of ron. (1 clause) john, david, ron and jack are all males. (4 clauses) emma, janet and eva are female. (3 clauses)
By using the predicates parentof, male and female write Rules for the following predicates (do one at a time and test them as you progress) fatherof(X,Y) - read as "X is the father of Y" motherof(X,Y) - read as "X is the mother of Y" grandparent(X,Y) - read as "X is a grandparent of Y" ancestor(X,Y) - read as X is an ancestor of Y.
b) Replace my family tree with one of your own. Can you write a clause to determine who is your grandmother?
Peyton Palace The Prolog program given below6 is an attempt to represent the following scenario
Diana loves everyone apart from herself Charles loves Camilla but does not lo ve Diana James does not love anyone but himself No-one (apart from Diana and James) loves James Everyone (apart from James) loves their mother. Everyone is loved by someone. Charles and Philip do not love the same people Ann loves horses and highhat is a horse Question 1 By showing the output to the Prolog program (below), determine the outcome of the following goal clauses a) Who does James love b) Does Philip love Charles' mother Question 2 By constructing suitable and/or graphs (hypergraphs) explain why the goal clauses loves(X,diana). loves(lover(diana),Y). fail and yet the goal clause loves(lover(diana),diana). succeeds Question 3 Construct a suitable and/or graph for the goal clause loves(philip,ann). Explain the significance of the outcome of the above goal clause. Question 4 7 By executing the Prolog program for further suitable goal clauses, construct two different extensions for the above program
/* Peyton Palace */ /* diana loves everyone apart from herself */ loves(diana,diana) :- !,fail. loves(diana,_). /* charles loves camilla but does not love diana */ loves(charles,camilla). loves(charles,diana) :- !,fail. /* james does not love anyone but himself */ loves(james,james). loves(james,_) :- !,fail. /* no one but diana and james love james */ loves(_,james) :- !,fail. /* everyone (apart from james) loves their mother */ loves(X,mother(X)). /* everyone is loved by someone */ loves(lover(X),X). /* ann loves horses */ loves(ann,Y) :- horse(Y). /* charles and philip do not love the same persons */ loves(philip,Y) :- loves(charles,Y),!,fail. loves(charles,Y) :- loves(philip,Y),!,fail. horse(highhat).
6
Careful study of this program will show that the order of clauses is significant. An extension is a maximal set of sentences closed under logical inference.
7