0% found this document useful (0 votes)
3 views19 pages

Section 3 Ai303

The document discusses knowledge representation in Prolog, focusing on the use of categories and the effect of the cut operator in controlling backtracking. It provides examples illustrating how different conditions affect the possible values of variables and demonstrates arithmetic operations in Prolog. The examples highlight the importance of the cut operator in limiting the search for solutions.

Uploaded by

manchestermilf1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views19 pages

Section 3 Ai303

The document discusses knowledge representation in Prolog, focusing on the use of categories and the effect of the cut operator in controlling backtracking. It provides examples illustrating how different conditions affect the possible values of variables and demonstrates arithmetic operations in Prolog. The examples highlight the importance of the cut operator in limiting the search for solutions.

Uploaded by

manchestermilf1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

AI 303:

Knowledge
Representation
TA.Menna sherif
Example_2 category(10,Y).
• What are the possible value(s) of Y ?

category(X,11) :- X < 12.


category(X,22) :- X < 15.
category(X,33) :- X < 18.
Example_2 (Cont …)

?- category(10,Y).
Y = 11 ;
Y = 22 ;
Y = 33 .

Why? The program will continue search all the possible solutions
Cut off “stops backtracking”

case 1  cond:- ______ , ________ , !.

case 2  cond:- ______ , ! , ________ .

case 3  cond:- ! , ______ , ________ .


Example_1
• What are the possible value(s) of Y ?

category(X,44) :- X < 12, !.


category(X,55) :- X < 15, !.
category(X,66) :- X < 18, !.
Example_1 (Cont …)

?- category(10,Y).
Y = 44 .

Why? This is the effect of the cut


Example_2
• What are the possible value(s) of Y?

category(X,C) :- X < 12, !, C = pg.


category(X,C) :- X < 15, !, C = 12.
category(X,C) :- X < 18, !, C = 15.
Example_2 (Cont …)

?- category(10,12).
false .

?- category(10,pg).
true .
Example_3
• What are the possible value(s) of X ?
/* program P clause # */

p(a). /* #1 */

p(X) :- q(X), r(X). /* #2 */

p(X) :- u(X). /* #3 */

q(X) :- s(X). /* #4 */

r(a). /* #5 */

r(b). /* #6 */

s(a). /* #7 */

s(b). /* #8 */

s(c). /* #9 */

u(d). /* #10 */
Example_3: (Cont …)
• What happens for the following goal? P(X)
/* program P clause # */

p(a). /* #1 */

p(X) :- q(X), r(X). /* #2 */

p(X) :- u(X). /* #3 */

q(X) :- s(X). /* #4 */

r(a). /* #5 */

r(b). /* #6 */

s(a). /* #7 */

s(b). /* #8 */

s(c). /* #9 */

u(d). /* #10 */

Why? The program will continue search all the possible solutions
Example_3 (Cont …)

?- p(X) .
X=a;
X=a;
X=b;
X=d.

Why? The program will continue search all the possible solutions
Example_3 (Cont …)

?- p(X) , ! .
X=a.

Why? This is the effect of the cut


Example_3 (Cont …)

?- r(X) , ! , s(Y).
X = a , Y = a;
X = a , Y = b;
X=a ,Y=c.

Why? This is the effect of the cut


Example_3 (Cont …)

?- !,r(X) , s(Y).
X = a , Y = a;
X = a , Y = b;
X=a ,Y=c.

Why? This is the effect of the cut


Example_3 (Cont …)

?- r(X) , s(Y) , ! .
X=a,Y=a.

Why? This is the effect of the cut


Arithmetic in Prolog

?- X is 2 + 3 * 4 - 5.
X=9.
?- X = 2 + 2.
X=2+2.
?- X is 2 + 2.
X=4.
?- 2 + 2 is 2 + 2.
false .
?- 5 =:= 2 + 3.
true .
Arithmetic in Prolog

?- X is 6 + 2.
X=8.
?- X is 6 * 2.
X = 12.
?- X is mod(7, 2).
X=1.
?- ‘AI’ = ‘Ai’.
false .
?- 6 + 4 =:= 6 * 3 - 8.
true .

You might also like