Rules Cunjunction in Prolog

10
Rules and Conjunctions A man is happy if he is rich and famous might translate to:  happy(Person):-  man(Person),  rich(Person),  famous(Person). The `,' indicates the conjunction and is roughly equivalent to the of predicate calculus. Theref ore, read `,' as `and' . The whole of the above is one non!uni t" si ngle cl ause. #t has three subgoals in its body !!!these subgoals are `conjoined'. #n this single clause, the logical variable Person refers to the same object throughout. $y the way, we might have chosen any name for the logical variable other than Person. #t is common practice to name a logical variable in some way that reminds you of what %ind of entity is being handled. &e now describe this clause graphically. #n this case, we are going to represent conjunctions using an A( tree. )ere is an A( tree that represents the above. The way in which we discriminate between an *+ tree and an A( tree is the use of a horiontal bar to lin% the sub goals. &e need this distinction because we are going to represent the structure of a program using a co mbined A(-*+ tr ee. 1

Transcript of Rules Cunjunction in Prolog

Page 1: Rules Cunjunction in Prolog

8/19/2019 Rules Cunjunction in Prolog

http://slidepdf.com/reader/full/rules-cunjunction-in-prolog 1/10

Rules and Conjunctions

A man is happy if he is rich and famous

might translate to: 

happy(Person):-

 man(Person),

 rich(Person),

 famous(Person).

The `,' indicates the conjunction and is roughly equivalent to the of predicate calculus.

Therefore, read `,' as `and' . The whole of the above is one non!unit" single clause.

#t has three subgoals in its body !!!these subgoals are `conjoined'.

#n this single clause, the logical variable Person refers to the same object throughout.

$y the way, we might have chosen any name for the logical variable other than Person. #t iscommon practice to name a logical variable in some way that reminds you of what %ind of entity

is being handled.

&e now describe this clause graphically. #n this case, we are going to represent conjunctions

using an A( tree. )ere is an A( tree that represents the above.

The way in which we discriminate between an *+ tree and an A( tree is the use of a

horiontal bar to lin% the sub goals. &e need this distinction because we are going to represent

the structure of a program using a combined A(-*+ tree.

1

Page 2: Rules Cunjunction in Prolog

8/19/2019 Rules Cunjunction in Prolog

http://slidepdf.com/reader/full/rules-cunjunction-in-prolog 2/10

 

.   liable_for_fine(X):- owns_car(X,Y), untaxed(Y). 

&e assume that liable_for_fine/ holds when its argument is a person" liable for a fine,

that owns_car/! holds when the first argument possesses the object named in the secondargument and this object is a car", and that untaxed/ holds for all those objects that are

required by law to be ta/ed and are not0

1.   sa"e_#ouse(X,Y):- address(X,$), address(Y,$). 

The sa"e_#ouse/! relation holds between two arguments people" if the address/! relation holds between one of these arguments and a third object and between the other

and the same third object.

 ote that this ma%es sa"e_#ouse(fred,fred) true.

2.   siblin%s(X,Y):- "ot#er(X,&), "ot#er(Y,&), fat#er(X,P), fat#er(Y,P). 

The siblin%s/! relation holds between the two arguments when each is related via the"ot#er/! relation to a common object and via the fat#er/! relation to a different"

common object.

This is not correct if the intended meaning is to prevent one person being their own

sibling. &e would revise this by adding a subgoal such as not_sa"e(X,Y).

 ote that we could have designed a 'arents/ predicate relation" such that, for e/ample,

the second argument is the mother and the third is the father of the first argument. This

would result in siblin%s(X,Y):- 'arents(X,&,P), 'arents(Y,&,P).

Rules and isjunctions

 

3omeone is happy if they are healthy, wealthy or wise.

translates to:

happy(Person):-

2

Page 3: Rules Cunjunction in Prolog

8/19/2019 Rules Cunjunction in Prolog

http://slidepdf.com/reader/full/rules-cunjunction-in-prolog 3/10

  healthy(Person).

happy(Person):-

  wealthy(Person).

happy(Person):-

  wise(Person).

 ote how we have had to rewrite the original informal statement into something li%e:

3omeone is happy if they are healthy *+3omeone is happy if they are wealthy *+

3omeone is happy if they are wise

&e have also assumed that each clause is implicitly" universally quantified. i.e. the first one

above represents X.(#ealt#*(X) #a''*(X)).

The predicate name ``happy' is %nown as a functor . Any predicate name is a functor".

The functor #a''* has one argument .

&e describe a predicate with name ``predname'' with arity ``n'' as 'redna"e/n. #t has one

argument !!!we say its arity is .

The predicate #a''*/ is defined by three clauses.

.   britis#(X):- wels#(X).

britis#(X):- en%lis#(X).britis#(X):- scottis#(X).

britis#(X):- nort#ern_iris#(X). 

 ote that we have preserved the order of nationalities as described in the statement. Thishas no logical significance.

1.   eli%ible_social_securit*(X):- earnin%s(X,Y), less_t#an(Y,!+).

eli%ible_social_securit*(X):- oa'(X).

3

Page 4: Rules Cunjunction in Prolog

8/19/2019 Rules Cunjunction in Prolog

http://slidepdf.com/reader/full/rules-cunjunction-in-prolog 4/10

#n the first part of the disjunction, we have introduced an additional predicate less_t#an/!

which has the reading that the relation holds when the first argument is less than the

second.

Also, note that the original statement does not ma%e it clear whether or not someone

could qualify both as an old age pensioner oap" and as someone earning very little. Thiscould become an important issue.

2.   s'orts'erson(X):- 'la*s(X,football).

s'orts'erson(X):- 'la*s(X,ru%%er).

s'orts'erson(X):- 'la*s(X,#oce*). 

Conjunctions and isjunctions

&e are now ready for the whole thing: let us go bac% to the set of rules as found in section 1.1 

and some basic facts.

and consider the solution of the goal

happy(jean)

)ere is the standard A(-*+ tree representation of the search space again:

4

Page 5: Rules Cunjunction in Prolog

8/19/2019 Rules Cunjunction in Prolog

http://slidepdf.com/reader/full/rules-cunjunction-in-prolog 5/10

and the goal succeeds.

 ote that

. $oth the subgoal #ealt#*(jean) and wo"an(jean) have to succeed for the wholegoal to succeed.

1. &e then return to the top level.

 ow consider the top level goal of

happy(joan)

The resolution process generates the subgoals #ealt#*(joan) and wo"an(joan) from the firstclause for #a''*/. #n all, Prolo% tries three times to match #ealt#*(joan) as there are three

clauses for #ealt#*/. After failing #ealt#*(joan), however, Prolo% does not try to solve

wo"an(joan) !!!there is no point in doing so.

There is another way of trying to prove #a''*(joan) using the second clause of #a''*/. Theresolution process again generates subgoals !!! wealt#*(joan) and wo"an(joan)!!! and

wealt#*(joan) fails. A third attempt is made but this founders as wise(joan) fails. ow bac% totop level to report the complete failure to satisfy the goal.

 ow consider

happy(P)

as the top level goal.

5

Page 6: Rules Cunjunction in Prolog

8/19/2019 Rules Cunjunction in Prolog

http://slidepdf.com/reader/full/rules-cunjunction-in-prolog 6/10

4uch more complicated. 5irst, #ealt#*(P) succeeds binding  P to ji"  P/jim" but when the

conjunctive goal wo"an(ji") is attempted it fails. Prolo% now backtracks . #t reverses along

the path through the tree until it can find a place where there was an alternative solution.

*f course, Prolo% remembers to unbind any variables e/actly at the places in the tree where theywere bound.

#n the e/ample we are using we again try to resolve the goal #ealt#*(P) !!!succeeding with P 

 bound to jane. ow the conjunction can be satisfied as we have wo"an(jane). +eturn to top

level with P bound to jane to report success. &hat follows is what appears on the screen:

?- happy(P).

P=jane

yes

Prolo% offers the facility to redo a goal !!!whenever the top level goal has succeeded and there is

a variable binding. 6ust type ``7'' followed by +8T9+ !!!``7'' can be read as or . #f possible,Prolo% finds another solution. #f this is repeated until there are no more solutions then we get the

sequence of solutions:

#t is worth trying to verify this.

$asically, trying to follow the behaviour of Prolo% around the te/t of the program can be very

messy. 3eeing how Prolo% might e/ecute the search based on moving around the A(-*+ tree

is much more coherent but  it requires some effort before getting the benefit.

What You Should Be Able To Do

6

Page 7: Rules Cunjunction in Prolog

8/19/2019 Rules Cunjunction in Prolog

http://slidepdf.com/reader/full/rules-cunjunction-in-prolog 7/10

After finishing the e/ercises at the end of the chapter:

There is no perfect solution to the problem of representing %nowledge. ou may generate

representations that differ wildly from someone else's answers. To find out which answer is best

and in what conte/t will require some deeper thought.

7

Page 8: Rules Cunjunction in Prolog

8/19/2019 Rules Cunjunction in Prolog

http://slidepdf.com/reader/full/rules-cunjunction-in-prolog 8/10

 

.   studies(bill,ai!). 

&e have revised `A#1' to `ai1'. &e could have simply put quotes around as in

studies(bill,'!'). 

1.   'o'ulation(france,0).

where the reading is that the population of the first object in the relation 'o'ulation/! is

the second object e/pressed in millions of people.

 ote we have changed `5rance' to `france'.

2.   ric#_countr*(ital*). 

)ere, the statement has been e/pressed as a unary `relation' of something being a rich

country.

;.   #ei%#t(jane,tall). 

&e have covered a similar e/ample previously.

8

Page 9: Rules Cunjunction in Prolog

8/19/2019 Rules Cunjunction in Prolog

http://slidepdf.com/reader/full/rules-cunjunction-in-prolog 9/10

<.   'ri"e(!). 

&e have asserted that the attribute of primeness belongs to the number 1.

=.   britis#(X):- wels#(X). 

The statement has been turned into the equivalent `everybody who is welsh is british'.

This is an alternative to the statement subset(wels#,britis#). &e read this as meaning that

the subset/! relation holds between the set of welsh people and the set of british people.

As usual, we have lower!cased the words `&elsh' and `$ritish'.

>.   aut#or(#a"let,so"eone). 

This is a tric% question. ou cannot answer this one from the notes. &hy not? &ell, let

me give the meaning of the above: the aut#or/! relation holds between `hamlet' which

stands for the famous play called ``)amlet: @rince of (enmar%''" and the unique atom1so"eone2 which has been conjured from thin air.

The problem lies in e/pressing e/istential statements such as ``someone li%es ice!cream''

and so on. This is informally recast as there e/ists some person such that this person li%esice!cream. #n first order predicate logic, we would formalise this as x

lies(x,ice_crea"). This can be turned into lies(w#ats#isna"e,ice_crea") this is

%nown as Skolemisation). &ithout going into technicalities, we give a legitimate conte/t

when this `tric%' can be done !!!whenever we have no universal quantifiers  i.e. indicated by words such as all, everyone, etc" then we may introduce a unique atom we should be

able to guarantee its uniqueness" to stand for the `someone'.

.   "ortal(X):- #u"an(X). 

This is an e/ample of a universally quantified statement. #t is equivalent to x #u"an(x)

"ortal(x).

 ote that, in the Prolo% version, this `universal quantification' is implicit.

B.   'a*s_taxes(X):- 'erson(X), ric#(X). 

Again, the universal quantification is implicit in the Prolo% version.

)ere, we have a body with a conjunction of two goals. This could be avoided with'a*s_taxes(X):- ric#_'erson(X). &hich you prefer depends on the way other relevantinformation is to be used or, how it is provided.

C. taes(bill,u"brella):- rainin%. 

This is a version where it is true that `$ill' ta%es his umbrella whenever it is raining.

 ote that in many of these e/amples, there is no mention of how the truth of various

statements change with time.

9

Page 10: Rules Cunjunction in Prolog

8/19/2019 Rules Cunjunction in Prolog

http://slidepdf.com/reader/full/rules-cunjunction-in-prolog 10/10

. no_su''er(X):- nau%#t*(X). 

)ere, we might have tried to write su''er(X):- nau%#t*(X). This is, however, illegal

in Prolo% but not for syntactic reasons.

Another way of doing this might be eats_su''er(X,false):- nau%#t*(X). This allows for

a more uniform treatment of both those who are `naughty' and those who aren't.

1. e"'lo*s(firebri%ade,X):- "an(X), #ei%#t(X,Y), "ore_t#an(Y,3.0). 

Again, we have gone for the representation `most li%ely' to be useful.

&e could hide much of this as firebri%ade_e"'lo*s(X):- o4er_six_foot(X).

http://homepage!"#$!ed!a%!u&/pb'#a/p'ologboo&/boo&!html

1(