Section 3 Ai303
Section 3 Ai303
Knowledge
Representation
TA.Menna sherif
Example_2 category(10,Y).
• What are the possible value(s) of Y ?
?- category(10,Y).
Y = 11 ;
Y = 22 ;
Y = 33 .
Why? The program will continue search all the possible solutions
Cut off “stops backtracking”
?- category(10,Y).
Y = 44 .
?- 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) :- 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) :- 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.
?- r(X) , ! , s(Y).
X = a , Y = a;
X = a , Y = b;
X=a ,Y=c.
?- !,r(X) , s(Y).
X = a , Y = a;
X = a , Y = b;
X=a ,Y=c.
?- r(X) , s(Y) , ! .
X=a,Y=a.
?- 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 .