Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://www.ssau.ru/files/resources/sotrudniki/soldatova_lezina.pdf
Äàòà èçìåíåíèÿ: Wed Jun 16 00:00:00 2010
Äàòà èíäåêñèðîâàíèÿ: Tue Oct 2 15:50:37 2012
Êîäèðîâêà: ISO8859-5

Ïîèñêîâûå ñëîâà: trees
Visual Prolog

: .. , ..

2010


004.89(075.8)

: - ? Ë .., ..., .. - ? Ë - , ..., ..

.. , .. Visual Prolog: : , 2010 -81 ., . ISBN - 978-593424-486-7

-

010400 ? Ë 230102 ? Ë, ? Ë, ? Ë, ? Ë ? Ë.



ISBN - 978-593424-486-7

? .. , .. ,2010

2


............................................................................................................. 5 1 ......................... 5 1.1 ......................................................................................... 5 1.2 . ............................... 7 1.2.1 ......................................................................................... 7 1.2.2 . ......................................................... 8 1.2.3 . .................................. 11 1.2.4 . .......................................... 13 1.2.5 ........................................ 15 2 . ........................ 16 2.1 ....................................................................................... 16 2.2 ............................................... 17 2.3 . ............................................... 21 2.4 . .............................................................................. 21 2.5 . . .................................................... 22 2.6 . .................................................................. 24 2.7 . ............................................................................ 25 2.8 . ................................................................... 26 2.9 ............................................................. 28 2.10 ............................................................................ 30 2.11 ........................................................ 32 2.11.1 ....................................................................... 32 2.11.2 ................................................................... 33 2.11.3 ................................................................... 34 2.11.4 ............... 35 2.11.5 ............................................................................... 35 2.11.6 ............................................................... 37 2.12 .......................................................... 38 2.12.1 ................................................................................. 38 2.12.2 ............................................................. 39 2.12.3 , ........................... 41 2.13 ................................................................ 42 2.14 ....................................................... 45 2.15 . ............................................................. 49 2.16 ......................................................... 51 2.17 ......................................................... 52 2.18 ................................................... 55 2.19 . ................................................................................ 57 2.20 " " ............................................................. 59 3 . ............................................................................................................... 62 3.1 .............................................................. 62 3.2 ......... 64 3


3.2.1 ....................................................................................... 3.2.2 ........................................................................................ 3.3 / . ..................................... 3.4 /- ................................ 3.5 ................................................. .............................................................................................................

64 67 71 74 76 80

4


. , , Visual Prolog- .. .. ? Visual PrologË - 2003 . , , , . , , . , Visual Prolog . Visual Prolog, . 1 1.1 , , . , , , . , . ( ) , , [1]. 1984 , () . , 5


. ( ) , , . : 1. : ( ); 2. (): . , . , , . 3. ( ): . , , -- P = P. : = P --P. - . . [1] , , , . . XIX . XIX .. ., , , . , . , , . , , . (), M = . T , 6


, . T . , , x T . P(T). S . T . , , . P(S), , X . A. A . , P(A), A. B . A, , B. . P(B), , , . , . , - . , . , . - . 1.2 . 1.2.1 XVII . XX , 1936 , , , . 7


, , , , ( , , ). 1930 . , . , . , , . . 1959 . , , . 1960 . , . 1965 , , , . . , -, , . 1.2.2 . , , . , , . : 1. , , . 2. , . 3. , .

8


. . . 1. , . 2. , , . 3. , . (K1x1)...(Knxn) (M), (Kixi), i = 1,...,n, (xi) (xi), M () . . 1. (K1x1)...(Knxn) (M), M . Kr (K1x1)...(Knxn), 1<=r<=n. 2. Kr - c, , M, xr M c Krxr . K1,...Ki - , M Kr, 1< i

. , , . 2: S S , S . , . 2. (x) (P(x) (y)(? (z)Q(x, y) (u)R(u, x, y))) . 1. : (x) (P(x) (y)(? ?(z)Q(x, y) (u)R(u, x, y))). 2. : (x) (P(x) (y)(? ?Q(x, y) (u)R(u, x, y))). 3. : (x) (P(x) (y)(Q(x, y) (u)R(u, x, y))). 4. : (x)(y)(u) (P(x) (Q(x, y) R(u, x, y))), . (x)(y)(u)(P(x) (Q(x, y) R(u, x, y))), . : (u) (x),(y) u f(x, y). , : (x)(y)(P(x) (Q(x, y) R(f(x, y), x, y))). . : {P(x), (Q(x, y) R(f(x, y), x, y))}. . 3. (x)(y)(z)(( ? P(x, y) Q(x, z)) R(x, y, z) (? Q(x, z) P(x, y))). : ((? P(x, y) R(x, y, z )) (Q(x, z) R(x, y, z)) (? Q(x, z) P(x, y))). : (y)(z) (x), y, z f(x), g(x). , : (x)(((? P(x, f(x)) R(x, f(x), g(x))) (Q(x, g(x)) R(x, f(x), g(x)) (?Q(x, g(x)) P(x, f(x)))). : 10


{?P(x, f(x)) R(x, f(x), g(x)), Q(x, g(x)) R(x, f(x), g(x)), ?Q(x, g(x)) P(x, f(x))}. 1. S - , F. F , S . 2. F1, F2,..., Fn G. G F1, F2,..., Fn , ((F1 F2 ... Fn)G) . 3. F1, F2,..., Fn G. G F1, F2,..., Fn , (F1 F2 ... Fn ?G) . . , , , : ?((F1 F2 ... Fn)G)= ?(?(F1 F2 ... Fn) G)= (F1 F2 ... Fn) ?G). 1 3 , G F , S ?G, S1 S2 ...Sn ?G. , S ?G , , G S. 1.2.3 . , , . , (). , : . , , . , . , , . 3: A , A ?A , { A, ?A } . , , . 4: : 11


C1 C2, L1 C1, L2 C2, L1 L2 C1 C2 , () C1 C2. 4: : C1: P R, C2: ?P Q. C1 P, ?P C2. , P ?P C1 C2 , R Q R Q. , C1 C2 C1 C2. . 4. C1 C2. C C1 C2 C1 C2. , , , . , . 5: S - . C S 1, C2,..., Ck , Ci S , Ci, Ck=C. S ( ) S. 5. S: 1. ?P Q, 2. ? Q, 3. P. 1 2 4. ?P. 4 3 5. . S , 4 S, , S . . 6: , . 6: 1 2 ...n ?N1 ?N2... ?Nm 28: , . 7: . 12


?N1 ?N2... ?Nm ?N1 ?N2... ?Nm =? (N1 N2 ...Nm) P (N1 N2 ...Nm) P N1, N2,...Nm , . , , . , , , . : , , ( ). 1.2.4 . , . . , , . 8. : C1: P(y) Q(y), C2: ?P(f(x)) R(x). C1, - C2. , f(a) y C1 a x C2, : C1': P(f(a)) Q(f(a)), C2': ?P(f(a)) R(a). P(f(a)) ?P(f(a)), C3': Q(f(a)) R(a). , f(x) y C1, C1'': P(f(x)) Q(f(x)). P(f(x)) C1'' ?P(f(x)) C2. , C3: Q(f(x)) R(x). , , . , C3 8 , , . 7: - {t1/v1,...,tn/vn}, vi - , ti - , vi, vi . 13


8: {E1,..., Ek} , E1=E2=... Ek. {E1,..., Ek} , . . : 1. , , . 2. , , , . 3. , . 4. , , , . 5. , . 9. : 1. Q(a, b, c) Q(a, d, l). . 2. Q(a, b, c) Q(x, y, z). . - Q(a, b, c). 9: {E1,..., Ek} , , = À , . 10: À , ( À ) [t]=[ [t]], t - , , [t] - , t . 11: {E1,..., Ek} () , E , , , . E. 10. : {P(x, f(y, z)), P(x, a), P(x, g(h(k(x))))}. , {f(x, y), a, g(h(k(x)))}. . 14


E - , D - , k - , k k- . 1. k=0, k=e ( ), Ek=E. 2. Ek Dk, : k - E. Dk. 3. vk tk Dk, vk , tk, 4. : E . 4. k+1=k { tk / vk}, Ek tk vk. 5. K=k+1. 2. 11. : E={P(f(a), g(x)), P(y, y)}. 1. E0=E, k=0, 0=e. 2. D0={f(a),y}, v0=y, t0=f(a). 1=={f(a)/y}, E1={P(f(a), g(x)), P(f(a), f(a))}. 3. 4. D1={g(x),f(a)}. 5. D1. , , E - . 1.2.5 . : x, x, x, . , . 12: ( ) C , C - C. 12. : C= P(x) P(f(y)) ?Q(x). 1 2 = {f(y)/x}. , C=P(f(y)) ?Q(f(y)) C. 13: C1 C2 - , . L1 L2 - C1 C2 . L1 ?L2 , (C1 - L1) (C2 - L2) C1 C2. 13. C1= P(x) Q(x) C2= ?P(a) R(x). x C1 C2, C2 C2= ?P(a) R(y). L1= P(x) L2=?P(a). L1 L2 ={a/x}. , Q(a) R(y) - C1 C2. 15


14. , R(a, z) S={?P(x, f(x)) R(x, f(x), g(x)), Q(x, g(x)) R(x, f(x), g(x)), ?Q(x, g(x)) P(x, f(x))}. C1=?P(x, f(x)) R(x, f(x), C2= Q(x, g(x)) R(x, f(x), C3=?Q(x, g(x)) P(x, f(x)). S C4=?R(a, z). C1 , C2 , C3 x, C2 y, C3 u. , : C1=?P(x, f(x)) R(x, f(x); C2= Q(y, g(y)) R(y, f(y); C3=?Q(u, g(u)) P(u, f(u)); C4=?R(a, z). C5 C1 C3 , x u, , u x, C5= R(x, f(x)) ?Q(x, g(x)). C6 C2 C5 , y x, , y x, C6= R(x, f(x)) R(x, f(x))= R(x, f(x)). C7 C2 C6 , x a, z f(a), C7= , . . 1. S , , 2. 2. S C1 C2, L1 C1 L2 C2. , , 3. 3. C1 C2 S. 1. 2 . 2.1 : . . , . . 16


( clauses) ( goal), , , , . ( ). : - . , S?P?Q?R S: - P, Q, R. : 1. NOD (x, x, x): -. NOD (x, y, z): - B (x, y), NOD (f (x, y), y, z). 2. 3. NOD (x, y, z): -B (y, x), NOD (x, f (y, x), z). NOD - z x y, B - ?Ë, f - . B f , : 1. NOD (x, x, x): -. 2. NOD (x, y, z): - x>y, NOD ((x- y), y, z). 3. NOD (x, y, z): -y>x, NOD ((x, y- x), z). , 4 6, : 4. : - NOD (4, 6, z). - , . 2.2 (PROgramming LOGic) . - , . . () , , [2]. , . , . 17


. , , , . , , . , . , , . - : Ç , Ç , Ç ( ), -, . . . . . , . , , . , (), . . , , . . : , - . , : Ç (, ), ; Ç (, ) - . ?_Ë , , 18


. , . . . , . - . . , , , , : ( , ). ( , ). , , , . . - , . () , : : (, ). , . , , : : ( X, ). : : Q1, Q2,...,Qn, , Q1, Q2,...,Qn - . , , , ( ). 15. : ( , ). (, ). ( , ). ( , ). ( , ). , , : : ( , X), (X, ). 19


, . , . , . , , () , . ( , ). ( , ). ( , ). , , X . . . , - , , , . - H: - P1, P2,..., Pn. ?: -Ë ?Ë, H , P1, P2,..., Pn . H ?P1? P2,...,?Pn. , . , - . 16. 15 , ?Ë: ( , ). (, ). ( , ). ( , ). ( , ). (X, Y): - (X, Z), (Z, Y). (X, Y): - (X, Z), (Z, Y). , , : : ( , ). - , , - . ?Ë. . , ?Ë : (x, y): - (x, y). (x, y): - (x, y). (x, y): - (x, z), (z, y). (x, y): - (x, z), (z, y). 20


, . , , , : ? Ë. - . 2.3 . , , , , . , , - , , . , . ? ; Ë. ?notË - P not(P). not(P) , () P. . , P, not(P), , . , P , . 2.4 . : , , , . . , ( ), . , , , , . . : . . : ? t YË. , , 21


. , , . . , . . 1. x y-, , . 2. x- , Y-, , Y x . 3. x y -, , () . ?=Ë, . ?=Ë . ?=Ë , . 17: X=Y, X Y - , , : X=5 Y=5, (); X=6 Y=5, (). X Y - , , ?=Ë . ?=Ë , . 2.5 . . () , , [1]: Q=Q1, Q2,..., Qn. . : , . 22


, . , . , , . , , , , . , . . , , . . : , . - . Q , ?Q. D - ( ). Q, , [D(?Q)] . : . 18: : 1. ( , ). 2. (, ). 3. ( , ). 4. ( , ). 5. ( , ). 6. (X, Y): - (X, Z), (Z, Y). 7. (X, Y): - (X, Z), (Z, Y). 8. (X, Y): - (X, Z), (Z, Y). 9. (X, Y): - (X, Z), (Z, Y). : Q1, Q2 = (X, Y), (, Y). Q1= (X,Y) : (,). 1={X=,Y=}. 1[Q2]= (, ). 1 , , : X= , Y= . 1[Q2]. , 23


, Q1, Q2 Q1 , . . 2={X=,Y=}. 1[Q2]= (, ). . : : (X, Y), (, Y). 2 : X=, Y= X=, Y=. , TestGoal Visual Prolog. : 1. -. 2. , -. 3. , , . 4. , . Visual Prolog ( project- ), , PDC Prolog, . 2.6 . , , . , . , : fail - ! - . fail ! , fail . , fail . , . , , , . 24


: 1. , - ? Ë. 2. - ? Ë. 19: ! fail. 16 ?-Ë, : 1._1(X,Y):-(X,Y), write(X," ",Y),nl. goal _1(X,Y). 3 : X=, Y= . X=, Y= . X=, Y= . 2._2:-(X,Y), write(X," ",Y),nl,fail. goal _2. 3 : X=, Y= . X=, Y= . X=, Y= . 3._3(X,Y):-(X,Y), write(X," ",Y),nl,!. goal _3(X,Y). 1 : X=, Y= . 2.7 . - . , . , ?Ë , . , : 1. . 2. . , , . 25


, case . 3. , . 4. go to. 5. . . 2.8 . , , : , , , . domains, constants, facts (database), predicates, clauses goal . : Ç domains , , , , ; Ç constants , , , ; Ç facts (database) , , ; Ç predicates , , , ; Ç clauses , , ; Ç goal . . , . Visual Prolog domains, facts, predicates, clauses , global. : 26




char integer byte word dword

-32768 32767 0 255 0 65535 0 +1E-307 +1E308 16 16 32 32 16 32

`a', 'b', `#', `B', `%' -63, 84, 2349



real short ushort long ulong unsigned

360, - 8324, 1.25E23, 5.15E-9



string symbol

?todayË, ?123Ë, ( 250) ?school_dayË 1. , , ; - . 2. , . DOS flower, school_day

?string and symbolË



ref file mail.txt, LAB.PRO

, domains. 20: domains number=integer name, person=symbol. symbol string - , . 27


Visual Prolog string symbol. , , string, - symbol: Symbol - , , , . String - , #0, . Visual Prolog , , . predicates. , . , ?goË ?repeatË. , predicates: 21: predicates mother (symbol, symbol) father (symbol, symbol). clauses, goal - . Visual Prolog goal . Test Goal. , , - . 2.9 , . , , . domains. , . , . . , . , : , . ( ). , 28


, , , . 22: , . personal_library, : personal_library= book (title, author, publisher, year), collection (collector, personal_library). book . , , 1990 , : domains collector, title, author, publisher = symbol year = integer personal_library = book (title, author, publisher, year) predicates collection (collector, personal_library) clauses collection (irina, book (?Using Turbo PrologË, ?Yin with SolomonË, ?Moscow, WorldË, 1993)). collection (petr, book (?The art of PrologË, ?Sterling with ShapiroË, ËMoscow, WorldË, 1990)). collection (anna, book (?Prolog: a relation language and its applicationsË, ?John MalpasË, ËMoscow, ScienceË, 1990)). goal collection (X, book( Y,_, _, 1990) Y . : collection (X, Z),Z=book( Y,_, _, 1990), Z book. . . . 23: , . domains person, title, author, artist, album, type = symbol thing = book (title, author); record (artist, album, type) predicates owns (person, thing) clauses owns (irina, book (?Using Turbo PrologË, ?Yin with SolomonË)). owns (petr, book (?The art of PrologË, ?Sterling with ShapiroË)). 29


owns (anna, book (?Prolog: a relation language and its applicationsË, ?John MalpasË)). owns (irina, record (?Elton JohnË, ?Ice FairË, ?popularË)). owns (petr, record (?Benny GoodmanË, ?The King of SwingË, ËjazzË)). owns (anna record (?MadonnaË, ?MadonnaË, ?popularËË)). goal owns (X, record(_, _, ?jazzË) Visual Prolog . , record (?Elton JohnË, ?Ice FairË, ?popularË) , : artist=art(family,name), family,name=symbol, : record (art("Elton","John"),?Ice FairË, ?popularË). 2.10 , . - . , , , , . : , , [4]. ([]), . , . . . , , . , : - ; - . , . - , , , ? Ë. . , , , , . ! (|): [Head | Tail]. 30


, - ! Head , Tail ( ). , , , X S [X|S]. , , . , , [a,b,c] :


a



b



c

[]

(*) . 24: , . domains list1=integer* list2=char* list3=string* list4=real* list5=symbol* personal_library = book (title, author, publisher, year) list6= personal_library* list7=list1* list8=list5* , personal_library, . 31


25: . [1, 2, 3, 4, 5] [6.9, 4.3] [cat, dog, horse] [`S', `K', `Y'] [?PIGË] [] 1 6.9 Cat `S' ?PIGË [2, 3, 4, 5] [4.3] [dog, horse] [`K', `Y'] []

Visual Prolog . - , . , , . 26: , , : domains llist=i(integer);c(char);s(string) list=llistl* 2.11 domains, , predicates, clauses goal. : , , , , , . 2.11.1 . . , , . . 27: . domains list=integer* predicates 32


member (integer, list) clauses member (Head, [Head |_ ]). member (Head, [_ | Tail ]):- member (Head, Tail). goal member (3, [1, 4, 3, 2]). , member. . , , . , , , . , , , , . , , . , , , , . find . , : member (Head, [Head |_ ]):- !. , . 2.11.2 , , . L1, - L2. L1 = [1, 2, 3], L2 = [4, 5]. append L2 L1 L3, L1 L2. : 1. L3 . 2. L1 L3, L3 [1, 2, 3]. 3. L2 L3, [1, 2, 3, 4, 5]. : 28: . 33


domains list=integer* predicates ppend (list, list, list) clauses append ( [], L2, L2). append ([H|T1], L2, [H|T3 ]):- append (T1, L2, T3). goal append ( [1, 2, 3], [4, 5], L3). append , append ([1, 2, 3], [4, 5], L3). : append (L1, [3, 4, 5], [1, 2, 3, 4, 5]) - L1=[1, 2], L2 = [3, 4, 5] L3 = [1, 2, 3, 4, 5]. append (L1, L2, [1, 2, 3, 4, 5]) L1 L2, L3 = [1, 2, 3, 4, 5]. 2.11.3 count_list1 count_list2. count_list1 , , byte , . count_list2 , , byte . 29. 29: . domains list=integer* predicates count_list1(list,byte,byte) count_list2(list,byte) clauses count_list1([],N,N). count_list1([_|T],N,M):- N1=N+1, count_list1(T,N1,M). count_list2([],0). count_list1([_|T],N):- count_list1(T,N1), N=N1+1. goal count_list1([0,-1,2,6,-9],0,N), count_list2([4,3,-8],K).

34


2.11.4 max_list min_list. max_list , , integer , - . min_list , , integer . 26. 30: . domains list=integer* predicates max_list(list, integer, integer) min_list(list, integer) clauses max_list ([],M,M). max_list ([H|T],N,M):- H>N, max_list(T,H,M). max_list ([H|T],N,M):- H<=N, max_list(T,N,M). min_list ([H|[]],H). min_list ([H|T],M):- min_list(T,M1), H>M1, M=H. min_list ([H|T],M):- min_list(T,M1), H<=M1, M=M1. goal L=[1,5,3,-6,8,-4],L=[H|T],max_list(T,H,Max), min_list([4,3,-8,6],Min). 2.11.5 . . : Ç , Ç , Ç . . , . , . . , , . 35


31: (). domains list=integer* predicates puz(list,list) perest(list,list) clauses puz(L1,L2):-perest(L1,L3),!,puz(L3,L2). puz(L1,L1). perest([X,Y|T],[Y,X|T]):-X>Y. perest([Z|T],[Z|T1]):-perest(T,T1). goal puz([1,3,4,5,2],L).] 32: . domains list=integer* predicates insert_sort (list, list) insert (integer, list, list) asc_order (integer, integer) clauses insert_sort ( [], []). insert_sort ([H1|T1], L2):- insert_sort (T1, T2), insert(H1, T2, L2). insert (X, [H1| T1], [H1| T2]) :- asc_order (X, H1), !, insert (X, T1, T2). insert (X, L1, [X| L1]). asc_order (X, Y):- X>Y. goal insert_sort ([4, 7, 3, 9], L). . , , insert_sort, H1 , . . , , insert, . H1 9, insert (9, [], [9]). , insert_sort. 3 asc_order, asc_order (3, 9):- 3>9. 36


, , , 3 9: insert (3, [9], [3, 9]). insert_sort, : insert_sort ([3, 9], [3, 9]). 7 asc_order asc_order (7, 3):- 7>3. , 3 insert , - [9]: insert (7, [9], _). asc_order (7, 9):- 7>9 , , insert, insert_sort. 7 3 9: insert (7, [3, 9], [3, 7, 9]). 4 asc_order asc_order (4, 3):4>3. , 3 insert , - [7, 9]: insert (4, [7, 9], _). asc_order (4, 7):- 4>7 , , insert, insert_sort. 4 3 7: insert (4, [3, 7, 9], [3, 4, 7, 9]). insert_sort [4, 7, 3, 9], [3, 4, 7, 9]). 2.11.6 . findall, . findall : Findall (Var_, Predicate_, List_), Var_ Predicate_, List_. 33: findall. domains d=integer predicates decimal (d) write_decimal clauses decimal (0) 37


decimal (1) decimal (2) decimal (3) decimal (4) decimal (5) decimal (6) decimal (7) decimal (8) decimal (9) write_decimal:- findall(C, decimal (C), L), write (L). goal write_decimal. 2.12 . , [3]. : Ç ; Ç ; Ç , ; Ç . . fail cut (!) , . , , , , . 2.12.1 , . , , . 34: . domains d=integer predicates decimal (d) write_decimal (d) clauses 38


decimal (0). decimal (1). decimal (2). decimal (3). decimal (4). decimal (5). decimal (6). decimal (7). decimal (8). decimal (9). write_decimal (C):- decimal (C), write (C), nl. goal write_decimal (C). 2.12.2 . fail, , . 35: . domains d=integer predicates decimal (d) write_decimal clauses decimal (0) decimal (1). decimal (2). decimal (3). decimal (4). decimal (5). decimal (6). decimal (7). decimal (8). decimal (9). write_decimal:- decimal (C), write (C), nl, fail. goal write_decimal. 10 , decimal (C). C , 0. , 39


decimal (C), , 0 . fail , , . 36: . domains d =integer predicates decimal (d) s (d, d) cikl clauses decimal (0). decimal (1). decimal (2). decimal (3). decimal (4). decimal (5). decimal (6). decimal (7). decimal (8). decimal (9). s( X, Z):- Z=X*X. cikl:-decimal (I), s(I , S), write (S), nl, fail. goal not(cikl). 37: 5 . domains d=integer predicates decimal (d) write_decimal. make_cut (d) clauses decimal (0). decimal (1). decimal (2). decimal (3). decimal (4). decimal (5). decimal (6). decimal (7). 40


decimal (8). decimal (9). write_decimal:- decimal (C), write (C), nl, make_cut (C). make_cut (C):-C=5. goal write_decimal. ! , . make_cut fail, , 5. 38: , 5. domains d=integer predicates decimal (d) write_decimal clauses decimal (0). decimal (5). decimal (2). decimal (3). decimal (4). decimal (5). decimal (6). decimal (5). decimal (8). decimal (9). write_decimal:- decimal (C), =5, write (C), nl, !. goal write_decimal. !, 5, . 5. 2.12.3 , , : repeat. repeat:- repeat. repeat , repeat . , , repeat. repeat - , ( repeat). repeat repeat, , repeat repeat. 41


repeat . . , repeat , . repeat , . 39: . 0. domains d=integer predicates repeat write_decimal do_echo check (d) clauses repeat. repeat:- repeat. write_decimal:-nl, write( ?, , Ë), nl, write(? , 0Ë), nl. do_echo:- repeat, readln (D), write(D), nl, check (D), !. check (0):- nl, write (?-OK!Ë). check(_):- fail. goal write_decimal, do_echo. do_echo - , , repeat readln, write, check. check . , 0, ?OK!Ë , check . 0, check fail . repeat. Repeat , repeat . 2.13 - , . , , . 42


, ( 48), . factorial , . , , . , -. 40: . predicates factorial (byte, word) clauses factorial (0, 1). factorial (1, 1):-!. factorial (N, R):- N1=N-1, factorial (N1, R1), R=N*R1. goal f (7, Result). . , . . 41: , . predicates f (byte, word) clauses f (1, 1). f (2, 1). f(N, F):- N1=N-1, f (N1, F1), N2=N1-1, f (N2,F2), F=F1+F2. goal f (10, Fib). : Ç ; Ç ; Ç , (, , ). - . , , , , , , . 43


, . , , , , . , . . Visual Prolog . , : Ç ; Ç . . 42: . count(100). count(N):-write(N),nl,N1=N+1,count(N1). goal nl, count(0). , . 43: . count1(100). count1(N):-write(N),N1=N+1,count1(N1),nl. goal nl, count1(0). - nl . 44: . count2(100). count2(N):-write(N),nl,N1=N+1,count2(N1). count2(N):-N<0, write("N - "). goal nl, count2(0). ( ), , . , . 45: . count3(100). count3(N):-write(N),nl,N1=N+1,check(N1),count3(N1). 44


check(Z):-Z>=0. check(Z):-Z<0. goal nl, count3(0). ( check). 44 45 , 42, . , ? , , . 44 45 , . 46: 41 . count4(100). count4(N):-N>0,!,write(N),N1=N+1,count4(N1). count4(N):- write("N - "). goal nl, count4(0). 47: 42 . count5(100). count5(N):-write(N),N1=N+1 check(N1),!, count5(N1). check(Z):-Z>=0. check(Z):-Z<0. goal nl, count5(0). 2.14 . . () . facts Visual Prolog () . , , . , . - . , , , . d 45


- . , facts : Ç ; Ç . facts , facts , facts - mydatabase. facts . facts predicates. , dbasedom. , , . TestGoal. , global facts. : Ç assert; Ç asserta; Ç assertz; Ç retract; Ç retractall; Ç save; Ç consult. assert, asserta, assertz, - , retract, retractall - . assert , asserta , assertz . retract , , retractall , . save , . , . consult , , . , domains. 48: , 4 . 46


facts dbin (byte, byte, byte, byte) predicates cifra (byte) bin (byte, byte, byte, byte) clauses cifra (0). cifra (1). bin (A, B, C, D):- cifra (A), cifra (B), cifra (C), cifra (D), assert (bin (A, B, C, D)). goal bin (A, B, C, D). 49: , . facts dcount (word) predicates modcount clauses dcount (0). modcount:- dcount (N), M=N+1, retract (dcount (N)),asserta (dcount (M)). goal modcount. 50: , . facts dsisters(symbol,symbol) dbrothers(symbol,symbol) predicates parents(symbol,symbol) pol(symbol,symbol) sisters(symbol,symbol) brothers(symbol,symbol) clauses parents (anna, olga). parents (petr, olga). parents (anna, irina). parents (petr, irina). parents (anna, ivan). parents (petr, ivan). pol(olga, w). pol(anna ,w). pol(petr, m). pol(irina, w). 47


pol(ivan, m). sisters (X, Y):-dsisters(X, Y). sisters (X, Y):- parents (Z, X), parents (Z,Y),pol(X,w), pol(Y,w),not(X=Y),not(dsisters(X,Y)), asserta(dsisters(X, Y)). brothers (X ,Y):-dbrothers(X, Y). brothers (X, Y):- parents (Z,X), parents l(Z,Y),pol(X,m), pol(Y,m),not(X=Y),not(dbrothers(X,Y)), asserta(dbrothers(X,Y)). goal sisters (X, Y), save ("mybase.txt"). 51: , , , : domains collector, title, author, publisher = symbol year = integer personal_library = book (title, author, publisher, year) predicates collection (collector, personal_library) q1(collector, title, year) q2(year) facts dq1(collector, title, year) count(year,byte) clauses collection (irina, book (?Using Turbo PrologË, ?Yin with SolomonË, ?Moscow, WorldË, 1993)). collection (irina, book (?The art of PrologË, ?Sterling with ShapiroË, ËMoscow, WorldË, 1990)). collection (petr, book (?The art of PrologË, ?Sterling with ShapiroË, ËMoscow, WorldË, 1990)). collection (petr, book (?Prolog: a relation language and its applicationsË, ?John MalpasË, ËMoscow, ScienceË, 1990)). collection (anna, book (?Prolog: a relation language and its applicationsË, ?John MalpasË, ËMoscow, ScienceË, 1990)). q1(C,T,Y):-dq1(C,T,Y), write(;*',C,' `,T,' `,Y),nl, fail. q1(C,T,Y):- not(dq1(C,T,_)),collection (C, book( T,_, _, 1990)), assert(dq1(C,T,Y)), write(C,' `,T,' `,Y),nl. count(2100,0). q2(Y):-collection (_, book(_, _, _, Y)), count(Yold,Nold), N=Nold+1, assert(count(Y,N)), write(Y,' `,N),nl, fail. goal %q1(C,T,1990). 48


q2(1990);count(Y,N). 2.15 . - . ?Ë - ASCII-. (\), ASCII- (N) , . \N (`\N'). ASCII- (?\N\N\NË). , , : Ç ; Ç , ; Ç . , : Ç str_len - ; Ç concat - ; Ç frontstr - ; Ç frontchar - ; Ç fronttoken - . str_len: str_len (Str_value, Srt_length), string, integer. 52: str_len (?TodayË, L)- L 5; str_len (?TodayË, 5) - ?TodayË 5. , , 5, . concat: concat (Str1, Str2, Str3), string. 53: concat (?TodayË, ?TomorrowË, S3)- S3 ?TodayTomorrowË; concat (S1, ?TomorrowË, ?TodayTomorrowË) - S1 ?TodayË; concat (?TodayË, S2, ?TodayTomorrowË) - S2 ?TomorrowË; 49


concat (?TodayË, ?TomorrowË, ?TodayTomorrowË)- ?TodayË ?TomorrowË ?TodayTomorrowË. frontstr: frontstr (Number, Str1, Str2, Str3), Number integer, string. Number , Str1 Str2, Str3. 54: frontstr (6,?Expert systemsË, S2, S3)- S2 ?ExpertË, S3 ,? systemsË. frontchar: frontchar (Str1, Char_, Str2), Char_ char, string. 55: frontchar (?Today Ë, C, S2)- C ?TË, S2 ,?odayË; frontchar (?Today Ë, `T', S2) - S2 ?odayË; frontchar (?TodayË, C, ?odayË) - C ?TË; frontchar (S1, ?TË, ?odayË) - S1 ?TodayË; frontchar (?TodayË, ?TË, ?odayË)- ?TË ?odayË ?TodayË. fronttoken: fronttoken (Str1, Lex, Str2), string. Lex Str1, Str2. - ( ). 56: fronttoken (?Expert systemsË, Lex, S2)- Lex ?ExpertË, S2 ,? systemsË. fronttoken (?$ExpertË, Lex, S2)- Lex ?$Ë, S2 ,?ExpertË. fronttoken (?Expert systemsË, Lex, ? systemsË)- Lex ?ExpertË; fronttoken (?Expert systemsË, ?ExpertË, S2)- S2 ? systemsË; fronttoken (S1, ?ExpertË, ? systemsË)- S1 ?Expert systemsË;

50


fronttoken (?Expert systemsË, ?ExpertË, ? systemsË)- ?Expert systemsË. 2.16 : upper_lower; str_char; str_int; str_real; char_int. . , . 57: upper_lower (?STARSË, S2). upper_lower (S1,?starsË). str_char (?TË, C). str_char (S, 'T'). str_int (?123Ë, N). str_int (S, 123). str_real (?12.3Ë, R). str_real (S, 12.3). char_int (`A', N). char_int (C, 61). , . , . 58: predicates conv_real_int (real, integer) conv_int_real (integer, real) conv_str_symb (string, symbol) clauses conv_real_int (R, N):- R=N. conv_int_real (N, R):- N=R. conv_str_symb (S, Sb):- S=Sb. goal conv_real_int (5432.765, N). (N= 5432). conv_int_real (1234, R). (R=1234). conv_str_symb (?Visual PrologË, Sb). (Sb=Visual Prolog). 51


59: frontchar. domains list=char* predicates convert (string, list) clauses convert (?Ë, []). convert (Str, [H\T]):- frontchar(Str, H, Str1), convert(Str1, T). 2.17 . . member . , , , . , tree (Top, Left, Right) tree (Left, Top, Right), Top - , Left Right . nil. : 60: domains treetype1=tree(symbol, treetype1, treetype1);nil treetype2=tree(treetype2, symbol, treetype2);nil 61: : a b d e c

: 1. : tree (b, tree (d, nil, nil), tree (e, nil, nil)). 2. : tree (c, nil, nil). 3. : tree (a, tree (b, tree (d, nil, nil), tree (e, nil, nil)), tree (c, nil, nil)). 52


62: . domains treetype = tree(symbol, treetype, treetype);nil() predicates in(symbol, treetype) clauses in(X, tree(X,_,_). in(X, tree(_,L,_):-in(X, L). in(X, tree(_,_,R):-in(X, R). goal in(d,tree(a, tree(b, tree(d, nil, nil), tree(e, nil, nil)), tree(c, nil, nil))). , . , . tree(X, Left, Right) : 1. Left X. 2. Right X. 3. . , , . . 63: . domains treetype = tree(byte, treetype, treetype);nil() predicates in(byte, treetype) clauses in(X, tree(X,_,_). in(X, tree(Root,L,R):-Root>X,in(X, L). in(X, tree(Root,L,R):-Root

predicates print_all_elements(treetype) clauses print_all_elements(nil). print_all_elements(tree(X, Y, Z)) :write(X), nl, print_all_elements(Y), print_all_elements(Z). goal print_all_elements(tree(a, tree(b, tree(d, nil, nil), tree(e, nil, nil)), tree(c, nil, nil))). 65: . domains treetype = tree(symbol, treetype, treetype);nil predicates isotree (treetype, treetype) clauses isotree (T, T). isotree (tree (X, L1, R1), tree (X, L2, R2)):- isotree (L1, L2), isotree (R1, R2). isotree (tree (X, L1, R1), tree (X, L2, R2)):- isotree (L1, R2), isotree (L2, R1). 66: . domains treetype = tree(symbol, treetype, treetype);nil predicates create_tree(symbol, tree) insert_left(tree, tree, tree) insert_rigth(tree, tree, tree) clauses create_tree(N, tree(N,nil,nil)). insert_left(X, tree(A,_,B), tree(A,X,B)). insert_rigth(X,tree(A,B,_), tree(A,B,X)). goal create_tree(a, T1)), insert_left(tree(b, nil, nil), tree(c, nil, nil), T2), insert_rigth(tree(d, nil, nil), tree(e, nil, nil), T3). : T1=tree(a, nil, nil), T2=tree(c, tree(b, nil, nil), nil), T3=tree(d, nil, tree(e, nil, nil)).

54


2.18 : 1. . 2. : . 3. : , - . , : Ç ; Ç , . 67: , :
a c b d e f g

, , . . domains top=symbol predicates edge (top, top) /* */ path( top,top) /* connected(symbol, symbol) .*/ clauses edge (a, b). edge (c, d). edge (a, c). edge (d, e). edge (b, d). edge (f, g). 55


path (X, X). path (X, Y):- edge (X, Z), path (Z, Y). 68: 67, . : . edge. domains edge= e(symbol, symbol) /* */ list1=symbol* list2=edge* graf = g(list1, list2) predicates path(graf, symbol, symbol) /* path(graf, symbol, symbol) .*/ clauses path (g([],[]),_,_). path (g([X|_],[e(X,Y)|_]),X,Y). path (g([X|T],[e(X,_)|T1]),X,Y):path (g([X|T],T1),X,Y). path (g([X|T],[e(_,_)|T1]),X,Y):path (g([X|T],T1),X,Y). path (g([X|T],[e(X,Z)|T1]),X,Y):path (g([X|T],T1),Z,Y). goal path (g([a, b, c, d],[e(a, b),e(b, c),e(a, d),e(c, b)]), a, c). 69: 67, . : , - . domains edge= e(symbol, symbol) /* */ list1=symbol* list2=edge* graf = g(list1, list2) predicates path(graf, symbol, symbol) /* path(graf, symbol, symbol) .*/ clauses path (g([],[]),_,_). path (g([X|_],[e(X,Y)|_]),X,Y). 56


%path %path path (g path (g path (g path (g path (g path (g goal path (g

(g([X|T],[e(X,_)|T1]),X,Y):(g([X|T],T1),X,Y). ([X|T],[e(X,Z)|T1]),X,Y):(T,T1),Z,Y). ([Z|T],T1),X,Y):-Z<>X, (T,T1),X,Y). ([X|T],[e(_,_)|T1]),X,Y):([X|T],T1),X,Y). ([a, b, c, d],[e(a, b),e(b, c),e(a, d),e(c, e)]), b, e).

2.19 . , , , . . , 65 . 70: , : A- B- P - ( - ).
b

c

d



domains top=symbol listtop=top* predicates edge (top, top) /* */ 57


path (top, top, listtop) /* path( top, top, listtop) , .*/ clauses edge (a, b). edge (b, c). edge (c, a). edge (b, d). edge (d, e). path (A, A, [A]). path (A, B, [A\P]):-edge(A, N), path(N, B, P). . , , . . : . path. , , . 71: , .
b

c

d



domains top=symbol listtop=top* predicates edge(top,top) path (top,top,listtop,listtop) path (top,top) member(top,listtop) reverse(listtop,listtop,listtop) clauses 58


edge(a,b). edge(b,c). edge(c,a). edge(b,d). edge(d,e). member(A,[A|_]):-!. member(A,[_|T]):-member(A,T). reverse([],T2,T2). reverse([H|T],T1,T2):-reverse(T,[H|T1],T2). path(A,B,P,[B|P]):-edge(A,B). path(A,B,P,P2):-edge(A,N),not(member(N,P)), P1=[N|P], path (N,B,P1,P2). path(A,B):-path(A,B,[A],P),reverse(P,[],Res),write(Res). goal path(a,e). 2.20 " " " " - , . , , , , . , , " ". , , , . " " . , , . member 27, . member (X, [1,2,3,4]) X=1, X=2, X=3, X=4. 72: . domains list=integer* predicates member (integer, list) 59


intersect(list,list) clauses member (Head, [Head |_ ]). member (Head, [_ | Tail ]):- member (Head, Tail). intersect (L1, L2):- member(X, L1), member(X, L2). goal intersect ([1, 4, 3, 2], [2, 5,6]). member intersect , member , . , , , X L1, , X L2. member append: member(X, L):- append(L1, [X|L2], L) , ? Ë. , , . append , X . N : N NxN , , . 8 , . . . N=2 N=3 ; N=4 . N=8 88 ( - 92) .
Q Q Q Q

60


Q Q Q Q

73 N . 1 N. , - , . [2, 4, 1, 3] , , [3, 1, 4, 2] . , . 73: N . domains list=integer* predicates range (integer, integer, list) /* , */ queens (list, list, list) /* N , - , , */ select (integer, list, list) /* */ attack (integer, list) /* attack, */ attack (integer, integer, list) /* , , M , M M */ fqueens (integer, list) clauses 61


range (M, N, [M|T]):- M
3.1 - , , (? Ë), . , N - , , - . , . 62


: ; , ; ? Ë; ? Ë; , ; ; ; . : ; , . , . ? Ë: , - , . : ; ; . . , , , - . , , . - , ? Ë - . , : . 73 N ( ) N X- , queens, range, attack.

63


3.2













3.2.1 N . ? Ë , . , , , ?Ë . - , . , . .

a b c

d

e

f

g

h

i

j

k

, a - , j f - . , . , , . after (X, Y, C), , X Y, C. after , 64


, . after , , . , . . . . , . : 1. ; 2. N 3 : Ç N-1 ; Ç ; Ç N-1 . 3 : Ç hanoi - , ; Ç move - ; Ç inform - . 74: . domains loc=right;middle;left % predicates hanoi(integert) % move(integer,loc,loc,loc) % , inform(loc,loc) % 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("Move a disk from ",Loc1," to ", Loc2). goal hanoi(3). 65


, . C A B A B C

. , . , . , , , . , . . . , , , . , . ?Ë . , [[c, a, b], [], []]. - , , . : [[a, b, c], [], []]; [[], [a, b, c], []]; [[], [], [a, b, c]]. 75: . domains list=symbol* % sit=list* % sits=sit* % predicates after(sit,sit) % , 66


solve (sit, sit, sits, sits) % member (list, sit) % member1(sit, sits) % writesp(sits) % clauses member(X, [X|_]):-!. member(X,[_|T]):-member(X,T). member1(X, [X|_]):-!. member1(X,[_|T]):-member1(X,T). after([St11,St12,St13],S):-St13=[H3|T3],S=[St11,[H3|St12],T3]. after([St11,St12,St13],S):-St13=[H3|T3],S=[[H3|St11],St12,T3]. after([St11,St12,St13],S):-St12=[H2|T2],S=[[H2|St11],T2,St13]. after([St11,St12,St13],S):-St12=[H2|T2],S=[St11,T2,[H2|St13]]. after([St11,St12,St13],S):-St11=[H1|T1],S=[T1,[H1|St12],St13]. after([St11,St12,St13],S):-St11=[H1|T1],S=[T1,St12,[H1|St13]]. solve(S,S1,Sp,[S1|Sp]):-after(S,S1), member([a,b,c],S1). solve(S,S2,Sp,Sp2):-after(S,S1), not(member([a,b,c],S1)), not(member1(S1,Sp)),solve(S1,S2,[S1|Sp],Sp2). writesp([]). writesp([H|T]):-writesp(T),write(H),nl. goal solve([[c,a,b],[],[]],S,[[c,a,b],[],[]],Sp),writesp(Sp). , . solve , . 3.2.2 , . , .

67


a b c

d

e

f

g

h

i

j

k

, . , -, . , , , -. - , . , - , . , , . , . , , . , - . , , ( , , ) , , . 68


?Ë , . -, - , , , . , . , , . , , . *, . , , . . , -. , f(n) , n ? nË. , f(n) . * ? Ë. ? Ë . , 1 8. , 33. , . - .

69


1 8 7

3 2 6 4 5 1 8 7 3 2 6 4 5

1 8 7

2 6

3 4 5

1 8 7 2 6

3 4 5

1 8 7

3 2 6

4 5

1 8 7 2 6

3 4 5

1 8 7

2 6

3 4 5

1 8 7

3 2 6

4 5

1 8 7

3 6

4 2 5

: 1. ; 2. ; 3. ; 4. . f(n) n . n : f(n)=g(n)+h(n), g(n) - n-, h(n) - n- . , h(n) , . g(n) n-, h(n) - n- ( , ). *, ( ) 70


? Ë ? Ë. : Ç 1; Ç 0, ; Ç 2, . - . : 1. h(n). 2. . 3. . 4. h(n). 5. , . , , . , , . f(1)=n+h(1)=1+4+3*(1+2)=14 f(2)=1+3+3*(1+2+2)=19. 1. 3 4 f(3)=2+2+3*(1+2+2)=19 f(4)=2+0=2. . f(1)=n+h(1)=1+2=3 f(2)=1+3=4 3.3 / . . , , , , . .

71


2 b 1 f 3 h 2 5

a

3 c 1 d 3 z

a,b,c,d,f,h,z - . . . , : f, - d. , , f, d. , : Ç a z, f; Ç a z, d. , , , : 1. , a z f, : Ç a f Ç f z. 2. , a z d, : Ç a d Ç d z. , , . / - , .

72



1 a- z | f a-f f-z| h a-d| c

a-z
a-z| d

2

d-z

a-f | b

a-f | c

f-h-z 4

a-c-d 4 3

a-b-f 3

a-c-f 8

. -. /- - , , - . - , , . , , . , , -, -, /- , -. , - - ; , - ,- - . , /- , . T : P - T; P -, T ( /-) ; P - -, ( /-) T. 73


/- , -, . , 7, 12, 7. . . , . 3.4 /- , , , /- . . , : Ç ; Ç . , , : . , , , : ; . , P - . G1, G2, G3 . Gi Pij. /- , , , - . . P, G1, G2, G3 . , P - . Gi - , , , Gi, Pij. , - -. - , . , , , . : , , , .

74


P



G1

G2

G3

....
P11 P12 P21 P22

....
P31 P32

?2 Ë. . , .
- - - -

.

...

75


, . 3.5 /- , , , , . . . : , ; , ; , . , , - , . , , . , , . , , , . , , , . - . , , . . , , , . :

76


a max b min d max t1 min 1 t2 4 4 t3 5 6 t4 6 t5 2 4 e f 2 t6 1 t7 1 1 t8 1 4 c 1 g

(Pk), Pk- . , , . , , . , (Pk) , :
max (pk)= min ( p ) k p k ) p k - max p k ) p k - min p k - ( (

76: trace domains pozic = symbol spoz = pozic* database xod (pozic, spoz) xod_min (pozic) xod_max (pozic) predicates minmax (pozic, pozic, integer) best (spoz, pozic, integer) oc_term(pozic, integer) vibor(pozic, integer, pozic, integer, pozic, integer) clauses 77


xod (a, [b,c]). xod (b, [d,e]). xod (c, [f,g]). xod (d, [t1,t2]). xod (e, [t3,t4]). xod (f, [t5,t6]). xod (g, [t7,t8]). xod_max (a). xod_max (d). xod_max (e). xod_max (f). xod_max (g). xod_min (b). xod_min (c). xod_min (t1). xod_min (t2). xod_min (t3). xod_min (t4). xod_min (t5). xod_min (t6). xod_min (t7). xod_min (t8). oc_term (a,4). oc_term (b,4). oc_term (c,1). oc_term (d,4). oc_term (e,6). oc_term (f,2). oc_term (g,1). oc_term (t1,1). oc_term (t2,4). oc_term (t3,5). oc_term (t4,6). oc_term (t5,2). oc_term (t6,1). oc_term (t7,1). oc_term (t8,1). minmax (Poz, BestPoz, Oc):xod (Poz, SpPoz),!, best(SpPoz, BestPoz, Oc); oc_term(Poz, Oc). best ([Poz], Poz, Oc):- minmax (Poz, _ , Oc), !. best ([Poz1| T], BestPoz, BestOc):minmax (Poz1, _ , Oc1), 78


best (T, Poz2, Oc2), vibor(Poz1,Oc1,Poz2,Oc2,BestPoz,BestOc). vibor(Poz0, Oc0, Poz1, Oc1, Poz0, Oc0):xod_min (Poz0), Oc0>Oc1,!; xod_max (Poz0), Oc0
79


1. .., . Visual Prolog - .: - , 2003. 2. . Prolog. .: , 2004. - 637 . 3. ., . : . . .: .1990. 4. . ., .. : . / . . , ...-: - . . . -, 2008- 52 .

80


. . , . .

VISUAL PROLOG : . . , . . 040910 10.08.98

: 16.06.2010 . Times New Roman. 100 .

. 60x84 . : 5 . . . 362

443001, . , , 3

? Ë 443001, . , , 3 .:242-37-07 443086, , , 34

81