صفحه 1:
Recursion and Lists
member(X,[Y|_] ) :- X = ۰
member(X, [ _ | Y]) :-
member(X, Y).
It would be easier to write this
as:
member(X,[X| _ ]).
member(X, [_ | Y]) :-
member(X, Y).
?- member(1, [3,4,5,8,1,9]).
Yes
?- member(X, [prolog, c, ada,
haskell]).
X= prolog;
وه
صفحه 2:
Other Examples
?- alter([you,are,a,computer],R).
R = [i, [am, not], a, computer)
Yes
?- alter([you,are,french],R).
R = [i, [am, not], australian]
Yes
ange(you, i).
ange(are, [am, not]).
ange(french, australian).
/* catchall */
صفحه 3:
asserta/1.
assertz/1
assert/1
retract/1
retractall/1
Example:
assertz(fib(N,F)).
صفحه 4:
:-dynamic fib
fib(1,1).
fib(2,1).
fib(N,F) :-
N= 2;
N1 is N-1, fib(N1,F1),
N2 is N-2, fib(N2,F2),
Fis Fl + F2,
asserta(fib(N,F)).
?- fib(8, F).
F=21
?- fib(6,F).
F=8
صفحه 5:
ae
£(5)
+
14 £(3) £(3)
+ +
0 0
13 ae ص iy fe
uo
(2) 0 1 1 1 1
2
be
1 1
صفحه 6:
asserta/1.
assertz/1
assert/1
retract/1
retractall/1
Example:
assertz(fib(N,F)).
صفحه 7:
?- animal(dog).
Yes
?- listing(animal).
:- dynamic
animal/1.
animal(dog).
animal(tiger).
animal(lion).
animal(monkey).
animal(A) :-
mamal(A),
:dynamic animal/1. %A
directive.
animal(tiger).
animal(lion).
animal(monkey).
animal(X):-mamal(X),
asserta(animal(X)).
mamal(cat).
mamal(dog).
صفحه 8:
Finding the length of a list
صفحه 9:
~ Exit: (10) 2 is 1+1 ? creep ۱
Exit: (9) leng([4, 5], 2)? creep }
~ Call: (9) _G485 is 2+1 ? creep ‘
~ Exit: (9) 3 is 2+1 ? creep
Exit: (8) leng([3, 4, 5], 3) ?
creep
۱
>< Call: (8) 06488 is 3+1 ? creep ۱
~ Exit: (8) 4 is 3+1 ? creep
Exit: (7) leng([2, 3, 4, 5], 4) ?
creep
>< Call: (7) G405 is 4+1 ? creep
~ Exit: (7) 5 is 4+1 ? creep
Exit: (6) leng([1, 2, 3, 4, 5],
5) ? creep
[trace] ?- leng([1,2,3,4,5],Ln).
Call: (6) leng([1, 2, 3, 4, 51,
_G405)?creep
Call: (7) leng([2, 3, 4, 5],
_G474) ? creep
Call: (8) leng([3, 4, 5], G474) ?
creep
Call: (9) leng([4, 5], G474) ?
creep
Call: (10) leng([5], G474) ?
creep
Call: (11) leng({], _G474) ? creep
Exit: (11) leng([], 0) ? creep
>< Call: (11) 06479 is 0+1 ? creep
صفحه 10:
.emoving an element from a list
remove(X, [ ], [ ]).
remove(X, [X|T], C):-
remove(X,T,C).
remove(X, [H|T], [H|T1]):- X \= H,
remove(X,T,T1).
?- remove(1,[4,9,8,7,1,9,7,5,11,R).
R= (4, 9, 8, 7, 9, 7, 5]
صفحه 11:
Insertion Sort
isort([], []).
isort([H|T],R):- isort(T,Q),
ins(H,Q,R).
ins(X, [], [x)).
ins(X, [H|T], [H|R]):- X>H,
ins(X,T,R).
ins(X, [H|T], [X,H|T])):- X=<H.
?- isort([4,1,9,5,8,3,2],S).
S =[1, 2, 3, 4, 5, 8, 9]
?- isort([a,b,c,d],S).
ERROR: Arithmetic: *c/0' is not
a function
صفحه 12:
1/0
صفحه 13:
SBA SP,
A full-stop must follow a term to be read
?- read(X).
|: massey.
X = massey
es
?- read(X).
px
X=_G185
es
?- read(X).
|: likes(Giohn,mary).
X = likes(john, mary)
es
?- read(X), name(X, List).
|: massey.
X = massey
List = [109, 97, 115, 115, 101, 121]
صفحه 14:
2- write(‘hello world’).
hello world
Yes
?- write(‘hello world'),nl,nl,tab(9),write(‘I
am here :)').
hello world
IT am here :)
Yes
2- write("abc").
[97, 98, 99]
Yes
2- write(‘abc').
abc
Yes
صفحه 15:
?- read(X),nl,write(‘Here is what was entered:
‘),write(X).
|: massey.
Here is what was entered: massey
X = massev
predicate for printing a list
printlst([ ]) :- put('.'), nl.
printlst([H|T]) :- - write(H), tab(1),
?- write([‘This’,is,a,list]).
[This, is, a, list]
?- printlst([‘This’,is, a,list]).
This is a list .
صفحه 16:
male(andrew).
male(john).
male(george).
male(greg).
male(adam).
female(mary).
female(jennifer).
female(eve).
parents(john,george,mary).
parents(greg,adam,eve).
parents(jennifer, adam,eve).
parents(andrew, adam,eve).
is brother _of(X,Y):-male(X),parents(X, Father, Mother),parents(Y,
Father,Mother), 26
=Y,write(X),tab(1),write(‘is'),tab(1), write(Y),write('s sister.').
is_sister_of(X,Y):-female(X),parents(X, Father, Mother),parents(Y, Father,
Mother). X\
?- is sister_of(X,Y). te(Y),write('s sister.').
jennifer is greg’s sister.
X = jennifer
Y = greg;
jennifer is andrew’s sister.
X = jennifer
صفحه 17:
?- likes(mary,X).
X= john
Yes
?- likes(mary,X), fail.
No
?- likes(mary,X),write(X),nl, fail.
john
fruit
book
No
?- likes(mary,X),write(X),nl,fail ;
true.
john
Fruit
صفحه 18:
2- likes(mary,X), write(‘Mary likes '), write(X),put(’."), nl, fail.
Mary likes john.
Mary likes fruit.
Mary likes book.
No
?- likes(mary,X),write(X),nl,fail;write(‘reached the OR part’).
john
fruit
book
reached the OR part
X=_G471
Yes
صفحه 19:
?- write('Enter a number X to be raised to the power of
5: '), read(N),
| P5 is N**5,write(N),write(' to the power of 5 is:
‘),write(P5).
Enter a number X to be raised to the power of 5:
۳
‘
۱
2 to the power of 5 is: 32 |
N=2
P5 = 32
?- repeat,write(‘Enter a number X to be raised to the
power of 5: '),
| read(N), P5 is N**5,write(N),write(' to the power of 5
is: (۶
صفحه 20:
صفحه 21:
Prolog
accepts input from an input stream
send output to an output stream
Streams can be files or I/O devices
Active streams
are the keyboard and the screen by
default (user)
We may only have
one stream open for input and
one open for output
صفحه 22:
tell(Filename) Opens the file named by Filename for
writing and makes it the current
output stream. It creates the file if it does
not exist.
telling(X) Retrieves the current input stream.
told Closes the file onened by the built-in
see(Filename) Opens the file named by Filename for
input and makes it the current input
straeam.
seeing(X) Retrieves the current input stream.
seen Closes the file opened by the built-in
صفحه 23:
?- tell('D:/test.txt').
Yes
?- telling(X).
X = '$stream'(1996248)
Yes
?- write(‘testing tell’).
Yes
?- told.
Yes
telling(Old),tell(‘D:/test.txt’),write(‘testing’),
told, tell(Old).
23
صفحه 24:
likes(tom,jerry).
likes(mary,john).
likes(tom,mouse).
likes(tom,jerry).
likes(jerry,cheeze).
likes(mary, fruit).
likes(john,book).
likes(mary,book).
likes(tom,john).
?- tell(‘Iksout.txt’),likes(mary,X), write(‘Mary likes ‘),write(X),
put(’.’),nl, fail; told.
X = _G662
Wes
tents of the file “Iks.tkt”
Mary likes john.
Mary likes fruit.
Mary likes book.
صفحه 25:
Eliminates choices
Always succeeds but stops backtracking
a:-b,c,!,d.
a:-e,f.
max(X,Y,Y) :- Y>X. max(X,Y,Y) -- Y>X, |.
max(X,Y,X).
?- max(1,2,X). و
227 x ae فك
X=1; No ,
No ?
7
25
صفحه 26:
suilt-in predicate cut, ۳
» Cut makes programs less readable.
» Using cut can make your programs mor
efficient.
» You have to be careful when using cut.
may alter
the way your program behaves. When
use cut in
a rule, you have to be certain of how y
rule will be
used. If you are not careful your progr
may behave
strangely.
26
صفحه 27:
Using member with cut we only get one
value for X, which is not what we
expect.
member1(X,[X|_]).
member1(X,[_|TI):-member1(X,1).
?- trace.
Yes
[trace] ?- member1(X,[1,2,3,4]).
Call: (6) member1(_G401, [1, 2, 3,
4]) ? creep
Exit: (6) member1(1, [1, 2, 3, 4]) ?
creep
Re iy
Fail
>
5
‘
|
6) member1(1, [1, 2, 3, 4]) ?
Unexpected
Member without cut
membernocut(X,[X|_]).
membernocut(X,[_|T]):-
membernocut(X,T).
?- membernocut(X,[1,2,3,4]).
صفحه 28:
membernocut(X,[X{_]).
membernocut(X,[_|T].
Using member without 0
predicate when we know that it will fail.
?- trace.
Yes
[trace] ?- membernocut(1,[1,2,3,4]), write(‘member
succeeded’), fail.
Exit: (7) membernocut(1, [1, 2, 3, 4]) ? creep
member succeeded
Redo: (7) membernocut(1, [1, 2, 3, 4]) ? creep
Call: (8) membernocut(1, [2, 3, 4]) ? creep
Call: (9) membernocut(1, [3, 4]) ? creep
Call: (10) membernocut(1, [4]) ? creep
Call: (11) membernocut(1, []) ? creep
Fail: (11) membernocut(1, []) ? creep
(10) membernocut(1, [4]) ? creep
: (9) membernocut(1, [3, 4]) ? creep
(8) membernocut(1, [2, 3, 4]) ? creep
Fail: (7) membernocut(1, [1, 2, 3, 4]) ? creep
Fail
صفحه 29:
29
Using member with cut:
member (X,[X|_])
member1(X,[_|T)):-
<- 200. member (X,T).
Yes
[trace] ?- member1(1,[1,2,3,4]),write(member
succeeded’), fail.
Call: (7) member1(1, [1, 2, 3, 4]) ? creep
Exit: (7) member1(1, [1, 2, 3, 4]) ? creep
member succeeded
Fail: (7) member1(1, [1, 2, 3, 4]) ? creep
No
2
صفحه 30:
g not instead of cut makes your program more cl€&
member1 (X,[X|_]):-!
member1 (X,[_|T]):-member1(X,T).
member(X, [X|T]).
member(X, [H|T]) :- not(X=H),
member(X, T).
30
صفحه 31:
male(andrew).
male(john).
male(george).
male(greg).
male(adam).
female(mary).
female(jennifer).
female(eve).
parents(john,george,mary).
parents(greg,adam,eve).
parents(jennifer, adam,eve).
parents(andrew, adam,eve).
is brother_of(X,Y):-male(X),parents(X, Father, Mother),parents(Y,
“ather, Mother),parents(Y, Father,
5
?- male(X),
X = andrew ;
No
31
Father, Mother), X\=
?- male(X).
صفحه 32:
male(andrew).
male(john).
male(george).
male(greg).
male(adam).
female(mary).
female(jennifer).
female(eve).
parents(john,george,mary).
parents(greg,adam,eve).
parents(jennifer, adam,eve).
parents(andrew, adam,eve).
is_brother_of(X,Y):-male(X),!,parents(X, Father, Mother), parents(y,
Father,Mother), X\=Y.
is_sister_of(X,Y):-female(X),!,parents(X, Father, Mother),parents(y,
Father, Mother),X\=Y.
صفحه 33:
apend(T1,L2,T2).
4], G409) ? creep
4], (1, 2, 3, 41)?
apend({],L,L).
apend([{H|T1],L2,[H|T2]):-apend(T1,L2,T2).
[trace] ?- apend((],[1,2,3,41,L).
Call: (6) apend({], [
Exit: (6) apend({], [1, 2,
creep
L=[1, 2,3, 4];
Fail: (6) apend({], [1, 2, 3, 4], G409) ? creep
No
apend([],L,L):-!.
apend([H|T1],L2,[H|T2]):-
[trace] ?- apend({],[1,2,3,4],L).
Call: (6) apend({], [1, 2, 3, 4], G409) ? creep
Exit: (6) apend([], [1, 2, 3, 4], [1, 2, 3, 4]) ? creep
T= [hy 253,41;
Fail: (6) apend({], [1, 2, 3, 4], [1, 2, 3, 41) ? creep
No
صفحه 34:
consult(Filename) :- see(Filename),
repeat,
read(X),
assert(X),
X=end_of_file,
!, seen.
?- tell(‘fam.pl’),
listing(male/1), told.
SoYAents of the file fam
male(andrew).
male(john).
male(george).
male(greg).
male(adam).
صفحه 35:
?-male(X). —?- female(X).
male(andrew). X= andrew; X= mary;
male(john). X= john; ennifer; 5
male(george). X= george; X= eve; SS
male(greg). X= greg; nee
male(adam). X= adam;
female(mary). ی
female(jennifer). ?- parents(X, adam, eve).
female(eve). X= greg;
parents(john,george,ma X= jennifer;
ry). X= andrew;
parents(greg,adam,eve). a
parents(jennifer, ?- findall(X, male(X), List).
adam,eve). List= [andrew, john, george, greg, ada
parents(andrew, ?- findall(X, female(X), List).
adam,eve). List= [mary, jennifer, eve]
?- findall(X, parents(X,adam,eve), List).
List= [greg, jennifer, andrew]
oy
صفحه 36:
male(andrew).
male(john).
male(george)
male(greg).
male(adam).
female(mary).
female(jennifer).
female(eve).
parents(john, george, mary).
parents(greg,adam,eve).
parents(jennifer, adam,eve).
parents(andrew, adam,eve).
?- findall(t(X,Y),parents(X,Y,Z),Li
X = _G353
G354
G358
List = [t(Gjohn, george), t(greg, adam), t(jennifer, adam),
t(andrew, adam)]
Yes
2» findall(t(X,Y),append(X,Y,
[a,b]),L).
X= 68
Y= 69
1 [t((1,[a,b]),t(Lal, [b)),t(La,b],
ves
صفحه 37:
?- call(a=a).
Yes
?- [user]. 9
3 2
|: likes(mary,joha). ?- call(likes(X,Y)). مه
I: سجن 2
% user compiled 0.02 sec, Y = john
152 bytes Yes
Yes 2- call(likes(X,mary)).
?- call(likes(mary,john)). No
Yes a
2- call(likes(mary,X)). ۳
2 > حطمز
Yes
37
صفحه 38:
not(likes (mary,john))
No
?- not(likes(mary,X)).
No
2
not(likes(mary,georg
e)).
Yes
7
صفحه 39:
rolog, programs and data are made of terms. A term may bi
4 constant (an atom or a number) 10, ‘abc’, likes, mary
1 variable ۹ ۹
١ compound term likes(mary,john), likes(?
بر تحص
are several built-in predicates that succeed or fail dependin
e of the term their argument is.
COR
atomic(X) Succeeds if X is bound to an atom, a
string ora number (integer or floating point).
atom(X) Succeeds if X is bound to an atom.
2
number(X) Succeeds if X is bound to a number
(integer or float)
float(X) Succeeds if X is bound to a floating
point number
صفحه 40:
2- atom(1).
No
2- atom(1.2).
No
?- atomic(1.2).
Yes
?- number(5).
Yes
?- number(1.2).
Yes
?- float(1).
No
?- float(1.1).
Yes
?- atom(a).
Yes
?- atom(X).
No
2- X=a,atom(X).
X=a
Yes
2- X=a,atomic(X).
X=a
Yes
?- atomic(a).
Yes
?- atomic(likes).
Yes
?- atom(likes).
Yes
صفحه 41:
9-1-2
[1,2,3,X,2],write(L),count(2,L,N).
[1, 2, 3, _G368, 2]
L= [1, 2,3, 2, 2)
X=2
N=3 % incorrect
Yes
2
?- count(1,[1,2,3,1],N).
N=2
Yes
?- count(1,[1,X,Y,Z],N).
26 < 1
Y=1
Z=1
N=4 % incorrect
Yes
2
صفحه 42:
Soluti
count(_,[],0).
count(Atom,[Head | Tail], N):- atomic(Head),
Atom=Head,
count(Atom,Tail,N1),
Nis N1 + 1;count(Atom,Tail,N).
las
?- count(1, ?L=
[1,2,3,11,N). (1,2,3,X,2],write(L),count(2,L,N)
N=2 :
[233 ec371 21
Yes
?- count(1,
([LXY,Z1N). L=[1, 2, 3, _G371, 2]
X = _G269 X =_G371
Y= 22 N=2
Yes
15