Вы находитесь на странице: 1из 39

-

-
2003
. .
. .


, -
. -
.
, -
.
-
, -
.
-
--
-
.

, 2003

25.03.03. 6084 1/16. . .


. . .1,65. . -. . 1,75. 300 .

-



190000, -, . . , 67

2


:
1) , . . -
;
2) , . . -
, .
(, , ) -
, .
( ).
( ), , -
,
, .
. ()
. -
, .

. -
-
, -
, .
, . -
,
,
(, , , , -
).
,
:
;
,
, . .;
;
1
-
;
,
.
1973 ., -
, -
,
. . -
.
1981 .
, -
.

. , CProlog,
Quintus Prolog, Silogic Knowledge Workbench, urbo Prolog .
-
Turbo Prolog
Borland. 1988 . Turbo Prolog 2.0,
. Borland 1990 .,
PDC ,
PDC Prolog. Visual Prolog,
.
1997 . PDC Visual Prolog 5.0,
1999 . 5.1.
Internet
Visual Prolog 5.1 Personal Edition,
Windows 3.1/95/98, NT, OS/2, SCO UNIX Linux.
20 : http://www.visual-prolog.com/
vip/vipinfo /freeware version.htm, http://www.pdc.dk/vip/vipinfo/freeware
version.htm. Personal Edition
, , -
. , Personal Edition -
-
. -
, ,
Web-.
Turbo Prolog.
.
2
1.
.
. -
, , -
{, }.
F(x), F(x, y), F(x, y, z) . ..
F(x),
M, , x F,
.
F(x, y) , x
y F.
F(x1 , x2 , x3 ,..., xN ) -
x1, x2, ..., xN -
: x1 , x2, xN F.

, -
.
:
()
(X,Y)
(X,Y,Z)
, , -
. -
, .
-
, .. -, -
:
().
n-
n1. , -
3
, -
.
, ,

.

(), .


(1 2 3 ... n ) .
, . .
. A B , A B
A B .
, -
, -
[ ()].

(X A) ( A Y) (X Y).
, -
, A . , A A .
, -
R () . -
, R, -
. ,

(1 2 3 ... n ) B ,
, .
, -
.

((PQ)(PR)(QS))(RS).
-

. :

4
1. ( )
P Q;
P R;
Q S.
2.

(R S) = R S ;
R;
S.
3.
R (R P ) (P Q) S ( Q S)

P , -
Q. Q .
,
Q Q = FALSE.
, ((PQ)(PR)(QS))(RS), -
.
-
. -
. , -
,
, -
.

2.
:
(domains);
,
(predicates);
(goal), ;

;
5
, (clauses).
() ,
.
,
, .
1
DOMAINS
gorod, strana = symbol
PREDICATES
situ(gorod,strana)
CLAUSES
situ (london, england).
situ (petersburg, russia).
situ (kiev, ukraine).
situ (pekin, asia).
situ (warszawa, poland).
situ (berlin, europe).
situ (X, europe):- situ (X, russia).
situ (X, europe):- situ (X, poland).

situ(kiev, ukraine)
, . -
, -
. -
, -
. :
DOMAINS
gorod, strana = symbol
PREDICATES
situ(gorod,strana)
:
PREDICATES
situ(symbol, symbol)
, -
.
clauses 1 .
, .
6
() -
. :- , -
. ,
, .

.
, , ,
, , -
() .
X ( ,
, ) , -
.
,
situ (X, europe):- situ (X, poland)
, -
. -
. ,
,

situ (X, europe):- situ (X, france)
- -
.
.
, -
.
:
2
DOMAINS
personal_library=book(title,author,publication)
publication= publication(publisher,year)

collector,title,author,publisher=symbol
year=integer
PREDICATES
collection(collector, personal_library)

7
CLAUSES
collection(,book( , ,
publication(,1990))).


. ,
. :
gigant(X) :- rost(X,Y),Y>200.
star_or_mlad(X) :- X>70; X<10.
, ..
, . , -
nl , write -
. -
, ,
-
.
= (-
) not. :
student(X) :- X=; X=.
xor_student(X) :- not(X=), not(X=).
planeta(X) :- not(X=).
not(X = Y) X<>Y.
, -
, . -
true fail. true , fail
. -
.
- ,
, ,
. ,
/* */. , %
, :
/*
*/
%

8
-
, -
.
,
.
:
1) .
2) .
3) .
:
(x), , x ,
(x,y), x y,
(x,y), , x y.
, , :
1) .
2) , .
3) .
:
?.
3
DOMAINS
person = symbol
PREDICATES
otec(person,person)
man(person)
brat(person,person)
CLAUSES
man(X):-otec(X,_).
brat(X,Y):-otec(Z,Y),otec(Z,X),man(X),X<>Y.
otec(ivan,igor). otec(ivan,sidor). otec(sidor,lisa).
X<>Y. -
- ,
.

goal: brat(igor,X)

X = sidor
9
.
,
,
-
.

3.
-
,
. 1.
1

>,<,=,>=,<=,<>
+, -, *, /
X mod Y X Y
X div Y X Y
abs(X) X
sqrt(X) X
sin(X), cos(X), tan(X), arctan(X)
exp(X) X
log(X) (ln) X
ln(X) X

-
. :
4
PREDICATES
add(integer,integer)
fadd(real,real)
maximum(real,real,real)
CLAUSES
add(X,Y):-Z=X+Y,write(Sum= ,Z),nl.
fadd(X,Y):-Z=X+Y,write(FSum= ,Z),nl.
maximum(X,X,X).
maximum(X,Y,X):- X>Y.
10
maximum(X,Y,Y):- X<Y.
-
. , , R ,
X Y, 3, ,
maximum, :
maximum(X,Y,Z), R= 3*Z.

:
gipotenuza(X,Y,Z):- Z = sqrt(X*X + Y*Y).

4. -
-
, ( and)
. -
. (goal).
, , ---
. : Yes No.
Yes , , -
.
, 1 ,

goal: {},
, , ,

goal: situ(petersburg, europe).


:
Yes,
. . ( ).
-
. -
. , -
. No
.
1, :
goal: situ(X, europe),
11
:
X = london
X = petersburg
X = kiev
X = warszawa
- -
. :
5
DOMAINS
nazvanie,stolica = symbol
naselenie = integer
territoria = real
PREDICATES
strana(nazvanie,naselenie_mln,territoria,stolica)
CLAUSES
strana(kitai,1200,9597000,pekin).
strana(belgia,10,30000,brussel) .
strana(peru,20,1285000,lima) .

goal: strana(X, _,Y, _), territoria > 1000000
,
X = kitai, Y = 9597000
X = peru, Y = 1285000
_
, .
,
, -
, .

goal. :
6
PREDICATES
hello
GOAL
hello.
12
CLAUSES
hello:-write(hello).
write -
. , ,
.
.
,
, goal
, .
-
. -
:
readln(X) /* */
readchar(X) /* */
readint(X) /* */
readreal(X) /* */
Turbo Prolog -
, , , . ., -
. -
HELP- .

5.

fail
(backtracking),
-
. -
, ,
.

, -
. goal , -
.
fail.
fail . -
,
.
13
7
PREDICATES
gorod(symbol)
show
GOAL
write( :),nl,show.
CLAUSES
gorod(). gorod().
gorod(). gorod().
show :- gorod(X), write(X),nl,fail.
. ,
fail.
cut
cut !. -
,
. , :
R : A,B, !, C
, -
,
. 8 9 cut.
8
DOMAINS
person=symbol
PREDICATES
deti(person)
show
make_cut(person)
GOAL
write( :),nl,show.
CLAUSES
deti().deti().deti().
deti().deti().deti().
show :- deti(X),write(X),nl,make_cut(X),!.
make_cut(X) :- X=.
8 .

14
9
PREDICATES
buy_car(symbol,symbol)
car(symbol,symbol,integer)
color(symbol,symbol)
CLAUSES
buy_car(Model,Color) :-
car(Model,Color,Price),color(Color,),!,Price<25000.

Car(,,12000).
Car(,,26000).
Car(,,24000).
Car(,,20000).
Car(,,20000).
Color(,).
Color(,).
, -
,
.

6.
, -
. -
(, ),
, ,
, , .
.
, :
doroje(dollar, rubl).
doroje(evro, rubl).
doroje(rubl, iena).
doroje(funt, euro).
:
? doroje(evro, rubl).
Yes
15
,
. ,
? doroje(evro, iena).
, .
:
? doroje(funt, rubl).
,
, :
doroje1(X, Y):- doroje(X, Y). /* */
doroje1(X, Y):- doroje(X, Z), doroje(Z, Y). /* */
, , , X>Z, Z>Y,
, X>Y.
. -
, :
doroje2(X, Y):- doroje(X, M), doroje(M, K), doroje(K, Z), doroje(Z, Y).
. -
, :
doroje1(X, Y):- doroje(X, Y).
doroje1(X, Y):- doroje(X, Z), doroje1(Z, Y).
-
.
, -
.
,
:
1. , ,
.
2. , -
,
.
.
.
, . :

16
write_string :- write(*****),nl, write_string.
.
1
7.
10
DOMAINS
number=integer
PREDICATES
write_number(number)
GOAL
write( :), nl, write_number(1).
CLAUSES
write_number(10).
write_number(N) :- N<10,write(N),nl,N1:=N+1, write_number(N).
clauses write_number.
,
.
11
.
11
PREDICATES
summa(integer,integer)
CLAUSES
summa(X,Y):-X<10,Y=X,!.
summa(X,Y):-X1=X div 10,summa(X1,Y1),Z=X mod 10,Y=Y1+Z.
! -
.
, -
.
, , -
1883 . .
, N -
, , ,
(. 1). , :
-
, -
17
1 2 3

. 1

.
.
:
<a,b> -
a b,
(m,a,b) m a b.
(1,a,b) <a,b> -
.
:
(m,a,b) (m-1,a,c) <a,b> (m-1,c,b)
. . m- a b :
1) m-1 a c ( ).
2) m a b.
3) m-1 c b ( ).

.
, m = 3, . . . :
(3,1,3)(2,1,2)<1,3>(2,2,3)

(3,1,3) (2,1,2) <1,3> (2,2,3)
(1,1,3)<1,2>(1,3,2) <1,3> (1,2,1)<2,3>(1,1,3)
:
<1,3><1,2><3,2><1,3><2,1><2,3><1,3>
-
.
(, ,
),
64 . -
, .
18
2n-1 . , -
1 , 40
2000000 , .
12 .
12
DOMAINS
loc =right;middle;left
PREDICATES
hanoi(integer)
move(integer,loc,loc,loc)
inform(loc,loc)
GOAL
hanoi(5).
CLAUSES
hanoi(N):- move(N,left,middle,right).
move(1,A,_,C):- inform(A,C),!.
move(N,A,B,C):- N1=N-1, move(N1,A,C,B), inform(A,C),
move(N1,B,A,C).
inform(Loc1, Loc2):- nl,write( , Loc1, , Loc2).

7.
-
. .
,
, :
[1,2,3]
[1.1,1.2,3.6]
[,,]
, .. ,
, .

.
(. 2).
, ,
, .
.
19
[1,2,3,4]

1 [2,3,4]

2 [3,4]

3 [4]

4 []

. 2

: num([1,2,3]).
.
13
DOMAINS
dog_list= symbol* /* * */
PREDICATES
dogs(dog_list).
CLAUSES
dogs([,,,]).
:
goal: dogs()
, :
goal: dogs(_,_,_,)
: = .
-
:
[Head|Tail]
-
. , -
13 ,
:
print_list([ ]).
print_list([X|Y]) :- write(X),nl, print_list([Y]).
20
goal -
:
show :- dogs(X), print_list(X).
,
:
find_it( X,[X| _ ]).
find_it( X,[ _ |Y]):- find_it( X,[Y]).

. ,
.
14
DOMAINS
dog_list=symbol*
PREDICATES
dogs(dog_list).
find_it(symbol,dog_list).
GOAL
find_it(,[, ]),write().
CLAUSES
find_it(X,[X,_]).
find_it(X,[_,Y]):-find_it(X,[Y]).
, -
.
-
, ,
.. (-
), .
Yes, -
.

.
15
DOMAINS
spisok=integer*
PREDICATES
summa_sp(spisok,integer)

21
CLAUSES
summa_sp([],0).
summa_sp([H|T],S):-summa_sp(T,S1),S= H +S1.
.
L1 L2 , .
L3 , L1 L2.
append([ ],L,L).
append( [N|L1], L2, [N|L3] ):- append(L1, L2, L3).

, .
:
append([1,2,3], [4,5],_).
,
.
, ,
.
,
, (
)
append([ ],[4,5],_).
,
:
append([ ],[4,5], [4,5]).
, , , -
( ) -
, ,
1 3:
append([1,2,3], [4,5],[1,2,3,4,5]).
, -
, , .

22
16
DOMAINS
LOC = east ; west /* */
STATE = state(LOC,LOC,LOC,LOC) /* */
PATH = STATE* /* */

PREDICATES
go(STATE,STATE) /* */
path(STATE,STATE,PATH,PATH) /* */
move(STATE,STATE) /* */
opposite(LOC,LOC) /* */
unsafe(STATE) /* */
member(STATE,PATH) /* ? */
write_path(PATH) /* */
write_move(STATE,STATE) /* */
GOAL
go(state(east,east,east,east),state(west,west,west,west)),
write(solved press any key to continue),
readchar(_),
exit.
CLAUSES
go(S,G):-
path(S,G,[S],L),
nl,write(A solution is:),nl,
write_path(L),
fail.
go(_,_).
path(S,G,L,L1):- move(S,S1), not( unsafe(S1) ), not( member(S1,L) ),
path( S1,G,[S1|L],L1),!.
path(G,G,T,T):- !. /* , L L1*/
move(state(X,X,G,C),state(Y,Y,G,C)):-opposite(X,Y). /* */
move(state(X,W,X,C),state(Y,W,Y,C)):-opposite(X,Y). /* */
move(state(X,W,G,X),state(Y,W,G,Y)):-opposite(X,Y). /* */
move(state(X,W,G,C),state(Y,W,G,C)):-opposite(X,Y). /* */

opposite(east,west).
opposite(west,east):-!. /* */

23
unsafe( state(F,X,X,_) ):- opposite(F,X) /* */
unsafe( state(F,_,X,X) ):- opposite(F,X). /* */

member(X,[X|_]).
member(X,[_|L]):-member(X,L) /* , */

write_path( [H1,H2|T] ) :- !,
readchar(_), write_move(H1,H2), write_path([H2|T]).
write_path( [ ] ).

write_move( state(X,W,G,C), state(Y,W,G,C) ) :-!,


write( ,X, ,Y),nl.
write_move( state(X,X,G,C), state(Y,Y,G,C) ) :-!,
write( ,X, ,Y),nl.
write_move( state(X,W,X,C), state(Y,W,Y,C) ) :-!,
write( ,X, ,Y),nl.
write_move( state(X,W,G,X), state(Y,W,G,Y) ) :-!,
write( ,X, ,Y),nl.

8.
-
.
-
, -
- .
, -
.
.
:
,
. ,
, ?

, . 2 (
):

24
2
1- 2- 3-


, ,
, (. 3).
3
1-e 2-e 3-e
+
+
+

-
.
17
PREDICATES
name(symbol)
mesto(symbol)
prizer(symbol,symbol)
solution(symbol,symbol,symbol,symbol,symbol,symbol)
CLAUSES
name(alex). name(pier). name(nike).
mesto(odin). mesto(dva). mesto(tri).
prizer(X,Y):-name(X),mesto(Y),X=pier,not(Y=dva),not(Y=tri);
name(X),mesto(Y),X=nike,not(Y=tri);
name(X),mesto(Y),not(X=pier),not(X=nike).
solution(X1,Y1,X2,Y2,X3,Y3):-name(X1),name(X2),name(X3),
mesto(Y1),mesto(Y2),mesto(Y3),prizer(X1,Y1),
prizer(X2,Y2), prizer(X3,Y3),Y1<>Y2,Y2<>Y3,Y1<>Y3,
X1<>X2,X2<>X3,X1<>X3,!.
,
. -
. -
( 3 , 1968):
25
.
, , , .
: , , ,
.
,
, .
, .
, .
.
.
.
, .
?
-
:
18
DOMAINS
name=simbol
PREDICATES
student(name) \* * \
gorod(name) \* *\
velo(name,name) \* *\
fact(name,name) \* *\
fact1(name,name) \* *\
rodom(name,name) \* *\
rodom_penza(name) \* *\
CLAUSES
\* 1 *\ student(X):- X=serg; X=boris; X=vict; X=grig; X=leo.
\* 2 *\ gorod(Y):- Y=penza; Y=lvov; Y=moskva; Y=xarkov; Y=riga.
\* 3 *\ fact(serg,piga).
fact(boris,penza).
fact(vict,moskva).
fact(grig,xarkov).
\* 4 *\ velo(X,Y):- student(X),gorod(Y), fact(X,Y), ! ;
student(X),gorod(Y), not( fact(X, _ )),not(fact( _, Y)).
\* 5 *\ fact1(boris,riga). fact1(vict,lvov).

26
\* 6 *\ rodom_penza(X) :- student(X), Z=penza,not(fact1(X,_)),
gorod(U),not(U=Z),velo(X,U),rodom(leo,U).
\* 7.1 *\ rodom(X,Z) :- student(X),gorod(Z),fact1(X,Z), ! ;
\*7.2 *\ student(X),not(X=leo),Z=penza,rodom_penza(X), !;
\* 7.3 *\ student(X),gorod(Z),not(fact1(_,Z)),X=leo,not(Z=penza),
student(K),not(fact1(K,_)),velo(K,Z);
\*7.4 *\ student(X),not(X=leo),gorod(Z),not(Z=penza),not(fact1(_,Z)),
not(fact1(X,_)), gorod(Y),not(Y=Z),velo(X,Y),
not(rodom(leo,Z)),not(rodom(leo,Y)).
.
-
student gorod. ,
.
, , -
, .
4 , -
. -
, ; .
, X Y,
. ,
. !
,
. .
5 -
.
6 ,
. ,
, , ,
.
7 -
. -,
.
, . -
. ,
, , ,
, , . ,
.

27

. (
):
, -
, :
1. , ,
, ,
.
2. , .
3. , .
4. , ,
, , ,
.
5. .
?
:
19
DOMAINS
s=symbol
PREDICATES
st_A(s) st_D(s) st_B(s) st_V(s) st_G(s)
ogr1(s,s,s,s,s) ogr2(s,s,s,s,s)
spisok(s,s,s,s,s)
norm1(s,s,s,s,s) norm2(s,s,s,s,s)
norm3(s,s,s,s,s) norm4(s,s,s,s,s)
CLAUSES
st_A(A):-A=andre; A=net.
st_D(D):-D=dmitri; D=net.
st_B(B):-B=boris; B=net.
st_V(V):-V=victor; V=net.
st_G(G):-G=grig; G=net.
ogr1(andre,_,_,net,_). ogr1(net,_,_,victor,_).
ogr2(_,dmitri,_,_,net). ogr2(_,net,_,_,_).

norm1(andre,dmitri,net,_,_).
norm2(andre,net,boris,net,_).

28
norm3(_,dmitri,net,net,_).
norm4(_,net,net,victor,grig).

spisok(A,D,B,V,G):-st_A(A),st_D(D),st_B(B),st_V(V),st_G(G),
norm1(A,D,B,V,G),ogr1(A,D,B,V,G),ogr2(A,D,B,V,G);
st_A(A),st_D(D),st_B(B),st_V(V),st_G(G),
norm2(A,D,B,V,G),ogr1(A,D,B,V,G),ogr2(A,D,B,V,G);
st_A(A),st_D(D),st_B(B),st_V(V),st_G(G),
norm3(A,D,B,V,G),ogr1(A,D,B,V,G),ogr2(A,D,B,V,G);
st_A(A),st_D(D),st_B(B),st_V(V),st_G(G),
norm4(A,D,B,V,G),ogr1(A,D,B,V,G),ogr2(A,D,B,V,G).
st_A(A),st_D(D),st_B(B),st_V(V),st_G(G),
not(norm1(A,D,B,V,G)),not(norm2(A,D,B,V,G)),
not(norm3(A,D,B,V,G)),not(norm4(A,D,B,V,G)),
ogr1(A,D,B,V,G),ogr2(A,D,B,V,G).
, -
, .
, -
, -
.

.

9.
, clauses, , -
(). -
.
database.
-
, -,
.
.
20
DOMAINS
name = symbol
rost, ves = integer
29
DATABASE
dplayer(name, rost, ves)
PREDICATES
player(name, rost, ves)
CLAUSES
player(, 180, 87)
player(, 187, 93)
player(, 177, 80)
,
.
:
assert_database:-player(N, R, V), assertz(dplayer(N, R, V)),fail.
assertz,
, -
.
(retract), (consult),
(save) (findall).
, , -
.
, :
GOAL: player(N, R, V),R>180
,
.4.
4

()

()

, , - -
.
- ?

. - -
30
. -
- -
( ).
,
.
21
DATABASE
xpositive(symbol, symbol)
xnegative(symbol, symbol)
PREDICATES
expertiza
vopros(symbol, symbol)
fish_is(symbol)
positive(symbol, symbol)
negative(symbol, symbol)
remember(symbol, symbol, symbol)
clear_facts
GOAL
expertiza.
CLAUSES
expertiza:-fish_is(X),!,nl,write( ,X, ),nl, clear_facts.
expertiza:-nl, write( !),clear_facts.
vopros(X, Y):-write( ,X, ,Y, ? (/)),readln(R),
remember(X,Y,R).
positive(X,Y):- xpositive(X,Y),!.
positive(X,Y):-not(negative(X,Y)),!,vopros(X,Y).
negative(X,Y):-xnegative(X,Y),!.
remember(X,Y,):-assertz(xpositive(X,Y)).
remember(X,Y,):-assertz(xnegative(X,Y)),fail.
clear_facts:-retract(xpositive(_,_)),fail.
clear_facts:-retract(xnegative(_,_)),fail.
fish_is():- positive( , >40 ).
fish_is():- positive( , <40 ),
positive( , ).
fish_is():- positive( , <20 ),
positive( , ).
fish_is():- positive( , <20 ),
31
positive( , ),
positive( , ).
fish_is():- positive( , <20 ),
positive( , ),
positive( , ).
, 21 -
.
.

1. ,
. ,
, , , , . . -
.
2. , -
, , ..
3. . ,
. -
, .
4. .
5. .
6. .
7. .
8. ,
, [1, 2, 3,
4, 5, 6, 7] [1, 3, 5, 7].
9. , . -
,
. , . , -
?
10. , .
, , .
11. , , ,
5 : , , , -
. 4 :
1) , .
2) , .
32
3) , .
4) , .
?
12. .
, , .

.
.
.
.
?
13. , , , -
, ,
. .
.
.
.
,
.
,
.
?
14. : , ,
.
.
. ,
.
-
?
15. , ,
, , .
.
:
1.

2.

3.
33

4.

5.


?

1. -
?
2. ?
3. ?
?
4. ?
5. ?
6. domains, predicates, goal clauses -
-?
7. ?
8. -
, , ?
9. ?
10. --
?
11. ?
12. fail?
13. cut (!)?
14. ?
15. ?
16. ?
17.
?
18. -
, ?
19. - ?
20. ?
21.
?
34

,
90- , 80-. -
-
, .
.

-

. --
, -
, -
.
,
, , -
, -
.
, .

. -
-
, -
.
, , -
-
. , -
.

35

1. . : 90- . .:
, 1985. 174 .
2. ., .
: . . .: , 1990. 235 .
3. ., . -: . . .:
, 1993. 608 .
4. . - : . . .: ,
1991. 95 .
5. .-. . .: , 1991.
568 .
6. . . .: , 1980. 520 .
7. . . .: , 1978. 558 .
8. . . .:
, 1985. 376 .
9. . . .: , 1990. 280 .
10. : -
: . . / . , .
, . . .: , 1990. 432 .
11. : . . / . . -
, . , . . .: , 1987. 441 .
12. . : . .
.: , 1989. 388 .
13. ., .
: . . .:
, 1990. 320 .
14. . . :
. .: , 1987. 288 .
15. ., . : -
: . . .: , 1987. 191 .
16. . ., . . -
: . / . ., 1997. 108 .

36

............................................................................................ 1
1. ................................................ 3
2. ................................................. 5
3. ............................................... 10
4. - ......................................................... 11
5. ........................................... 13
fail ........................................................... 13
cut ........................................................... 14
6. .............................................. 15
7. ...................................................................... 19
8. ................................................................. 24
9. ................................................. 29
.............................................. 32
.............................................................. 34
............................................................................................... 35
................................................................... 36

37

Оценить