Professional Documents
Culture Documents
Student Roll #
Program
Section
RULES IN PROLOG
Lab-04
___________________________________________________________________________
17
Artificial Intelligence-Lab [COSC-3212]
OBJECTIVES:
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. */
___________________________________________________________________________
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) */
2. USING “trace”
trace is a powerful tool for letting you observe your code operating in action. trace allows
you to see:
To use trace, simply issue the query trace before performing whatever queries you'd like to
trace. For example:
trace, foo(X,Y).
3. LAB TASKS
3.1. LAB TASK 1
On Program 2 perform the following trace command and write the results.
___________________________________________________________________________
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
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)
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).
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).
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]