You are on page 1of 6

Student Name

Student Roll #

Program

Section

RULES IN PROLOG
Lab-04

___________________________________________________________________________
17
Artificial Intelligence-Lab [COSC-3212]
OBJECTIVES:

 PROLOGS proof procedure


 Using trace in PROLOG debugger

1. PROLOG’s PROOF PROCEDURE


Figure1: Program 2

In responding to queries, the Prolog interpreter uses a backtracking search. To see how this
works, let's consider Rule 1 & 2 from program 2 above which are:
parent(X,Y) :- father(X, Y). /* Rule 1 */
parent(X,Y) :- mother(X, Y). /* Rule 2 */

And let's trace how PROLOG would process the query. Suppose the facts and rules of this
database are arranged in the order in which they were input.

?- parent(karen,susan).
parent(karen,susan) /* Prolog starts here and searches for a matching fact
or rule. */

parent(X,Y) /* Prolog unifies the query with the rule #1 using


{karen/X, susan/Y}, giving parent(karen,susan) :-
father(karen,susan) */

father(karen,susan) /* Prolog replaces LHS with RHS and searches. */

/* This fails to match father(jack,susan),


father(jack,ray), father(david, liza), father(david,
john), father(john, peter) and father(john, mary)so
this FAILS. */

___________________________________________________________________________
18
Artificial Intelligence-Lab [COSC-3212]
/* Prolog BACKTRACKS to the other rule #2 and unifies
with {karen/X, susan/Y}, so it matches
parent(Karen,susan) :- mother(karen,susan) */

mother(karen,susan) /* Prolog replaces LHS with RHS and searches. */

true. /* Prolog finds a match with a literal and so succeeds.

2. USING “trace”

trace is a powerful tool for letting you observe your code operating in action. trace allows
you to see:

 What clauses are called


 The values passed to clauses
 Where the engine succeeds and fails, triggering backtracking
 Where backtracking goes to
 What is returned from a clause

To use trace, simply issue the query trace before performing whatever queries you'd like to
trace. For example:

trace, foo(X,Y).

Here's a trace of query parent(karen, susan) on Program 2 using


PROLOG’s trace predicate:

3. LAB TASKS
3.1. LAB TASK 1

On Program 2 perform the following trace command and write the results.

trace, parent (X,Y).

___________________________________________________________________________
19
Artificial Intelligence-Lab [COSC-3212]
3.2. LAB TASK 2

Load the family.pl file (provided with the lab) in the PROLOG debugger and perform
following:

Statement Rule

Add a male() rule that


includes all fathers as males.

Add a female() rule that


includes all mothers as
females.

son_of(X,Y)

daughter_of(X,Y)

sibling_of(X,Y)

___________________________________________________________________________
20
Artificial Intelligence-Lab [COSC-3212]
brother_of(X,Y)

sister_of(X,Y)

3.3. LAB TASK 3

Considering exercise problem 4 above, given the addition of the sibling_of rule, and
assuming the order for the facts and rules as in problem 3 above, show the PROLOG trace for
the query sibling_of(paul,mary).

3.4. LAB TASK 4


Given the database below, study the queries underneath it. Indicate whether you think a
particular query will succeed or fail by answering true or false.

likes(john,mary).
likes(john,trains).
likes(peter,fast_cars).

likes(Person1,Person2):-
hobby(Person1,Hobby), hobby(Person2,Hobby).

hobby(john,trainspotting).
hobby(tim,sailing).

___________________________________________________________________________
21
Artificial Intelligence-Lab [COSC-3212]
hobby(helen,trainspotting).
hobby(simon,sailing).

?- likes(john,trains).

?- likes(helen,john).

?- likes(tim,helen).

?- likes(john,helen).

3.5. LAB TASK 5


Considering knowledge base provided below, write a “teaches” rule where a person
teaches a subject if he/she is instructor of a subject and student is enrolled in the same
subject.

Instructor (chan, math273).


Instructor (humaira, cosc3212).
Instructor (kevin, ee222).

enroll(zeeshan, ee222).
enroll(farah, cosc3212).
enroll(usman, math273).
enroll(husnain, cosc3212).

RULE:

OUTCOME: After this practical students would be able to effectively write and comprehend
rules in PROLOG and understand PROLOG’s proof procedure.

___________________________________________________________________________
22
Artificial Intelligence-Lab [COSC-3212]

You might also like