• Tidak ada hasil yang ditemukan

Specimen Solutions to Practical Exercises

Dalam dokumen PDF Logic Programming with Prolog (Halaman 191-200)

Question 3

Here is the output produced by one Prolog system for the specified goals, with explanations inserted in italic.

?- write(hello).

helloyes

?- write(Hello).

_12544Hello = _

This obscure output is produced because Hello is not an atom (it begins with a capital letter). To print Hello World with a capital H it must be enclosed in quotes.

?- write('Hello!').

Hello!yes

It is not (usually) possible to suppress the final 'yes', which indicates that the goal has ben satisfied. However the output can be made more readable by using 'nl' to generate a newline after 'Hello!'.

?- write('Hello!'),nl.

Hello!

yes

?- 100=100.

yes

?- 100=1000/10.

no

Using = is not the right way to do arithmetic – see Chapter 4.

?- 100 is 1000/10.

yes

?- 1000 is 100*10.

yes

?- 2 is (5+7)/6.

yes

?- 74 is (5+7)*6.

no

Practical Exercise 2

Question 1

A suitable series of goals is as follows:

?- animal(mammal,A,_,_).

A = tiger ; A = hyena ; A = lion ; A = zebra

?- animal(mammal,A,carnivore,_).

A = tiger ; A = hyena ; A = lion ; no

?- animal(mammal,A,_,stripes).

A = tiger ; A = zebra

?- animal(reptile,A,_,mane).

no Question 2

A suitable additional rule would be

couple(X,Y):-person(X,male),person(Y,female).

Testing this gives the following output:

?- couple(X,Y).

X = bill , Y = carol ;

X = bill , Y = margaret ; X = bill , Y = jane ; X = george , Y = carol ; X = george , Y = margaret ; X = george , Y = jane ; X = alfred , Y = carol ; X = alfred , Y = margaret ; X = alfred , Y = jane ; no

Practical Exercise 3

Question 1

Suitable definitions are given below:

child_of(A,B):-parent(B,A).

grandfather_of(A,B):-father(A,C),parent(C,B).

grandmother_of(A,B):-mother(A,C),parent(C,B).

great_grandfather_of(A,B):-

father(A,C),grandfather_of(C,B).

great_grandfather_of(A,B):-

father(A,C),grandmother_of(C,B).

?- child_of(X,ann).

X = henry ; X = mary ; no

?- grandfather_of(A,caroline).

A = francis ; no

?- grandmother_of(B,caroline).

B = janice ; no

?- great_grandfather_of(C,caroline).

C = john ; no Question 2

The system begins by matching the goal with the first clause defining the ancestor/2 predicate, i.e. [A1].

?-ancestor(louise,Desc).

[A1] ancestor(louise,Y):-parent(louise,Y).

X is bound to louise. Variables Desc and Y are bound to each other.

The goal parent(louise,Y) is now matched with clause [P3], which is first rewritten to replace X and Y by X1 and Y1, i.e.

parent(X1,Y1):-mother(X1,Y1).

?-ancestor(louise,Desc).

[A1] ancestor(louise,Y):-parent(louise,Y).

[P3] parent(louise,Y1):-mother(louise,Y1).

X is bound to louise. Variables Desc,Y and Y1 are bound to each other. X1 is bound to louise.

The system now tries to satisfy the goal mother(louise,Y1). It matches it with clause [M9].

?-ancestor(louise,Desc).

[A1] ancestor(louise,Y):-parent(louise,Y).

[P3] parent(louise,Y1):-mother(louise,Y1).

[M9] mother(louise,caroline).

X is bound to louise. Variables Desc,Y and Y1 are bound to each other and to caroline.X1 is bound to louise.

This gives a solution to the user's goal, with Desc bound to caroline.

?- ancestor(louise,Desc).

Desc = caroline

If the user now forces the system to backtrack, the system will try to resatisfy the goal mother(louise,Y1) and fail. This will cause the rule [P3] to be rejected.

Attempts to resatisfy parent(louise,Y) in the body of [A1] will also fail, so clause [A1] will be rejected. This brings the system back to the original goal ancestor(louise,Desc).

The system tries to resatisfy it by matching it with the second clause for ancestor/2, i.e. [A2].

?-ancestor(louise,Desc).

[A2] ancestor(louise,Y):-parent(louise,Z),ancestor(Z,Y).

X is bound to louise. Variables Desc and Y are bound to each other. Variable Z is unbound.

The system now tries to satisfy the goal parent(louise,Z). It matches it with [P3], which is first rewritten as (say)

parent(X1,Y1):-mother(X1,Y1).

?-ancestor(louise,Desc).

[A2] ancestor(louise,Y):-parent(louise,Z),ancestor(Z,Y).

[P3] parent(louise,Y1):-mother(louise,Y1).

X is bound to louise. Variables Desc and Yare bound to each other. Variables Y1 and Z are bound to each other. Variable X1 is bound to louise.

It now tries to satisfy the goal mother(louise,Y1). It matches it with clause [M9].

?-ancestor(louise,Desc).

[A2] ancestor(louise,Y):-parent(louise,caroline),ancestor(caroline,Y).

[P3] parent(louise,caroline):-mother(louise,caroline).

[M9] mother(louise,caroline).

X is bound to louise. Variables Desc and Yare bound to each other. Variables Z any Y1 are bound to each other and to caroline. Variable X1 is bound to louise.

The next step is to satisfy the goal ancestor(caroline,Y). It is matched with [A1], which is first rewritten as (say)

ancestor(X2,Y2):-parent(X2,Y2).

?-ancestor(louise,Desc).

[A2] ancestor(louise,Y):-parent(louise,caroline),ancestor(caroline,Y).

[P3] parent(louise,caroline):-mother(louise,caroline).

[M9] mother(louise,caroline).

[A1] ancestor(X2,Y2):-parent(caroline,Y2).

X is bound to louise. Variables Desc and Yare bound to each other. Variables Z any Y1 are bound to each other and to caroline. Variable X1 is bound to louise.

Variable X2 is bound to caroline. Variables Y and Y2 are bound to each other.

Next the system tries to satisfy parent(caroline,Y2). It matches it with [P3], which is first rewritten as (say)

parent(X3,Y3):-mother(X3,Y3).

?-ancestor(louise,Desc).

[A2] ancestor(louise,Y):-parent(louise,caroline),ancestor(caroline,Y).

[P3] parent(louise,caroline):-mother(louise,caroline).

[M9] mother(louise,caroline).

[A1] ancestor(X2,Y2):-parent(caroline,Y2).

[P3] parent(caroline,Y3):-mother(caroline,Y3).

X is bound to louise. Variables Desc and Yare bound to each other. Variables Z any Y1 are bound to each other and to caroline. Variable X1 is bound to louise.

Variable X2 is bound to caroline. Variables Y,Y2 and Y3 are bound to each other.

X3 is bound to caroline.

Next the system tries to satisfy mother(caroline,Y3). It matches it with [M10].

?-ancestor(louise,Desc).

[A2] ancestor(louise,Y):-parent(louise,caroline),ancestor(caroline,Y).

[P3] parent(louise,caroline):-mother(louise,caroline).

[M9] mother(louise,caroline).

[A1] ancestor(X2,Y2):-parent(caroline,Y2).

[P3] parent(caroline,Y3):-mother(caroline,Y3).

[M10] mother(caroline,david).

X is bound to louise. Variables Desc and Yare bound to each other. Variables Z any Y1 are bound to each other and to caroline. Variable X1 is bound to louise.

Variable X2 is bound to caroline.X3 is bound to caroline. Variables Y,Y2 and Y3 are bound to each other and to david.

Now all the goals in the body of [A2] have succeeded, so the original goal ancestor(louise,Desc) succeeds with Desc bound to david.

Another solution with Desc bound to janet is available by backtracking.

?- ancestor(louise,Desc).

Desc = caroline ; Desc = david ; Desc = janet ; no

Dalam dokumen PDF Logic Programming with Prolog (Halaman 191-200)