صفحه 1:
صفحه 2:
Welcome to SWI-Prolog (Version 4.0.9)
Copyright (c) 1990-2000 University of
Amsterdam.
Copy policy: GPL-2 (see www.gnu.org)
For help, use ?- help(Topic). or ?-
apropos(Word).
You nta Prolog now.
صفحه 3:
?- append([abc,def],[ghi,Imn],L).
L = [abe, def, ghi, Imn]
Yes
?- append([abc,def],L, [abc,def,ghi,Imn]).
L = [ghi, Imn]
صفحه 4:
06
You can enter consult mode by typing in “[user]”
at the “?-”
3
9 AtCcm-vCOMey Mm ilteen br ysitiemy-\ anal omes ns
, type in what you want entered into your database.
ale(john).
male(mary).
ther(john,mary).
‘eat(massey).
1s_a_job(mary).
1e last fact is entered and at the “|:” prompt, enter a «
> consult mode. An example is shown on the next slide
صفحه 5:
صفحه 6:
Listing your predicates
= x
00000 irra
صفحه 7:
?- listing(likes/2). /* we could have said “listing(likes).” in
this case*/
likes(tom, jerry).
likes(mary, john).
likes(tom, mouse).
likes(tom, jerry).
likes(jerry, cheeze).
likes(Gjohn,book).
knows(tom,john).
صفحه 8:
Tracing your program
trace.
notrace.
Or simply n.
spy(likes/2).
nospy
صفحه 9:
?- trace.
Yes
[trace] ?- likes(X,Y).
Call: (7) likes(_G332, _G333) ?
| creep
| Exit: (7) likes(tom, jerry) ? creep
X= tom
Y = jerry;
Redo: (7) likes(_G332, _G333) ?
| creep
Exit: (7) likes(mary, john) ? creep
=X = mary
Y =john;
= Redo: (7) likes(_G332, G333) ?
creep
Exit: (7) likes(tom, mouse) ? creep
X= tom 5
Y = mouse ;
Redo: (7) likes(_G332, _G333) ?
creep
Exit: (7) likes(tom, jerry) ? creep
X= tom
صفحه 10:
x = jerry
Y = cheeze ;
Redo: (7) likes(_G332, -
_G333) ? creep '
Exit: (7) likes(mary, fruit) ?
creep
X = mary
LY = fruit ;
Redo: (7) likes(_G332,
1 _G333) ? creep
Exit: (7) likes(john, book) ?
creep
X = john
Y = book;
Fail: (7) likes(_G332, G333) ?
صفحه 11:
صفحه 12:
owns(john, book).
owns(victoria, book),
owns(george, book),
owns(john,book(prolog,clocksin_and_meéllish)y- 2 ی
owns(victoria, book(prolog,bratko)). a —
5 5 a — = يي يي
= > = = کل
owns(john,book(prolog,author(clocksin_and_mellish),year(1994
owns(victoria, book(prolog,author(bratko),year(2001), edit}
2 = = = 4
صفحه 13:
صفحه 14:
rithmetic Operators
2 +ع
i" ubtr eens
* multiplication NEN
i] fivision — RRs
صفحه 15:
صفحه 16:
?- concen(X,Y).
X=us
Y = 91.6667
Yes
?- concen(X,Y).
X=us
Y = 91.6667 ;
X = china
Y= 315.5;
X=nz
Y=16;
صفحه 17:
صفحه 18:
ot يم
Nee ی
ple:
two positive integers X and Y, their greatest common
yr “D” is found according to the following:
<= YthenD=X
< < Y then D = greatest common devisor of X and Y - X
< > Y then D = do the same as above with X and Y interché
0 is 5
ae a a ee
صفحه 19:
gced(X,X,X).
gcd(X,Y,D) :-X <Y,
Zis Y -X,
gcd(X, Z, D).
gcd(X,Y,D) :- Y<X,
gcd(Y, X, D).
gcd(X,Y,D) :- Y<X,
ZisX-Y,
2- gcd(20,15,D).
D=5
Yes
2 gcd(0,15,D).
ERROR: Out of local stack
صفحه 20:
?- edit(fact).
% d:/prolog/fact.pl compiled 0.00 sec, 64 bytes
Yes
?- fact(6,X).
X= 720
Yes
?- fact(3,X).
X=6
Yes
صفحه 21:
صفحه 22:
The list is the main data structure in Prolog. A list may
be empty:
11
or it may contain one or more terms (constants,
variables or structures).
[a,b,c,d,e]
[5,8,3,9,7]
[the, boy, run]
A list can be split into a head and a tail: [H|T].
grades(john, [70,87,90,58]).
?- grades(john, [H|T]).
H=70
T= [87,90,58
صفحه 23:
[©,0,6]
[[سحاصر حصصب] ,صاط ,لج ]
DY, wb]
fhe, [bev nn]
{s].b.2.<]
1
صفحه 24:
?- [a, b, c] = [Head |
Tail].
Head =a
Tail = [b, c]
?- [the, [boy,run]]=[Head|
Tail].
Head = the
Tail = [[boy, run]]
?- [a, b, c] = [X, Y | Tail].
صفحه 25:
Converting a structure to a list
?- likes(john,mary) =.. X.
X = [likes, john, mary]
Anything in double is equivalent to a list of
ASCII values:
?- "abc
X = [97, 98, 99]
صفحه 26:
Recursion and Lists
member(X,[Y|_]) :-X =Y.
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;
صفحه 27:
ther Examples
lange(you, i).
lange(are, [am, not]).
ange(french, australian).
lange(do, no).
۱21206), 6(۰ /* catchall */
ter([ 1, [ ]).
ter([H|T], [X|Y]) :- change(H, X), alter(T,Y).
?- alter([you,are,french],R).
R = [i, [am, not], australian]
Yes
?- alter([you,are,a,computer],R).
R= [i, [am, not], a, computer]
Yes
2
صفحه 28:
asserta/1.
assertz/1
assert/1
retract/1
retractall/1
Example:
assertz(fib(N,F)).
صفحه 29:
:-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)).
صفحه 30: