Você está na página 1de 635

PROLOG

-
2004

32.973.26-018.2.75
87
681.3.07
""
. ..
.
"" :
info@williamspublishing.com, http://www.winiaraspublishing.com

, .
87

PROLOG, 3- . : . . . : "", 200-1. 640 . :


. . . .
ISBN 5-8459-0664-4 (.)

Prolog,
Prolog , , , , , , ,
.
, Prolog
.
. .
32.973.26-018.2.75


.
, , , Addison-Wesley UK.
Authorized translation from the English language edition published by Addison-Wesley
Publishers Limited, Copyright 2001 by Pearson Education Limited
All rights reserved. No part of this book may be reproduced or transmitted in any form or by
any means, electronic or mechanical, including photocopying, recording or by any information
storage retrieval system, without permission from the Publisher.
Russian language edition published by Williams Publishing House according to the Agreement
with R&I Enterprises International, Copyright 2004

ISBN 5-8459-0664-4 (.)


ISBN 0-201-40375-7 (.)

"", 2004
Pearson Education Limited, 2001

I. Prolog
1. Prolog
2. Prolog
3. , ,
4. :
5.
6.
7.
8,
9.
10.
II. Prolog
11.
12.
13. AND/OR
14.
15.
16.
17.
18.
19.
20.
21.

22.
23.
. Prolog
.


18
25
26
45
76
98
121
136
149
169
192
215
227
228
247
277
301
326
357
383
408
446
478
510
532
559
590
592
595
611
619

18

I. Prolog
1. Prolog
1.1.
1.2.
1.3.
1.4. Prolog
1.5.


2. Prolog
2.1.
2.1.1.
2.1.2.
2.1.3.
2.2.
2.3. Prolog
2.4.
2.5. :
2.6.
2.6.1.
2.6.2.

2.6.3.
2.7. Prolog

3, , ,
3.1.
3.2.
3.2.1.
3.2.2.
3.2.3.
3.2.4.
3.2.5.
3.2.6.
3.3.
3.4.

4. :
4.1.
4.2.
4.3.
4.4.
4.5.
4.5.1. 1

25
26
26
30
35
39
42
43
44
45
45
46
47
48
52
56
58
62
66
66
68
72
73
74
76
76
78
79
79
82
82
83
84
87
92
96
98
98
102
103
107
111
111

4.5.2. 2
4.5.3. 3
4.5.4.

5.
5.1.
5.1.1. 1
5.1.2. 2
5.2.
5.2.1.
5.2.2. ,

5.2.3.
5.2.4.
5.3.
5.4. ,


6.
6.1.
6.2. ,
6.2.1. read write
6.2.2.
6.2.3. ,
6.3.
6.4.
6.5.

, Prolog
7.
7.1.
7.1.1. var, nonvar, atom, integer, float, number, atomic,
compound
7.1.2. nonvar
7.2. ; =.., functor, arg name
7.3.
7.4.
7.5.
7.6. bagof, setof fmdall

8.
8.1.
8.2. Prolog
8.2.1.
8.2.2.
8.2.3.
8.3.
8.3.1.
8.3.2.
8.3.3.
8.4.
8.5.
8.5.1.

114
116
119
120
121
121
122
123
125
125
126
126
127
129
132
135
135
136
136
139
139
141
142
143
144
146
147
148
149
149
149
151
156
159
161
164
165
167
169
169
171
171
172
173
173
174
175
175
176
177
178
7

8.5.2.
8.5.3.

5.5.4.
8.5.5. arg
8.5.6.


9.
9.1.
9.2.
9.3.
9.4.
9.5.
9.5.1.
9.5.2.
9.5.3.


10.
10.1. -



10.2. AVL-

179
181
182
184
186
190
191
192
192
197
201
206
208
208
209
211
214
214
215
215
218
218
218
221
225
225

II. Prolog
11.
11.1.
11.2.
11.3.
11.4.


12.
12.1.
12.2.
" "
12.3.
12.4.
12.4.1. *
12.4.2. IDA*
12.4.3. RBFS


13. AND/OR
13.1. AND/OR
13.2. AND/OR
13.2.1. AND/OR
13.2.2.
13.2.3. AND/OR
13.3. AND/OR
8

227
228
228
232
238
242
245
246
247
247
256
261
265
265
266
269
275
275
277
277
281
281
282
283
284

13.4. AND/OR
13.4.1.
13.4.2.
13.4.3.


14.
14.1.
14.1.1.
14.1.2.
14.1.3. Prolog

14.2. CLP
CLP{R)
14.3. CLP
14.4.
14.5. CLP
CLP(FD)


15.
15.1.
15.2.
15.3. ,

15.3.1.
15.3.2.
15.3.3.
15.4.
15.5.
15.5.1.
15.5.2. ,
15.6.
15.6.1. ,
15.6.2.
15.6.3.
15.7.
15.7.1.
15.7.2.


16.
16.1.
16.2.
16.2.1.
16.2.2.
16.2.3. ,

16.2.4. ,

16.3.
16.3.1. explore
16.3.2. useranswer
16.3.3. useranswer
16.3.4. present

289
289
292
298
300
300
301
301
301
303
305
306
310
317
321
324
325
326
326
328
331
331
333
334
336
337
337
339
340
340
344
345
348
349
350
355
356
357
357
361
361
363
364
365
366
366
369
371
376
9

16.3.5.
16.3.6.
16.3.7.
16.4.


17.
17.1.
17.2.
17.3.
17.4.
17.5.
17.6.

17.7.

17.7.1.
17.7.2.


18.
18.1.
18.2.
18.2.1. ,
18.2.2.
18.2.3.
18.2.4.
18.3.

18.4.
18.4.1.
18.4.2.
18.5.
18.5.1.
18.5.2. ""
18.5.3.
18.6.
18.7.
18.7.1.
18.7.2. ,
18.7.3.


19.
19.1.
19.2. Prolog
19.2.1.
19.2.2.
19.2.3. MINIHYPER
19.2.4. , -
19.3. HYPER
19.3.1.
19.3.2.
19.3.3. HYPER
10

377
378
378
380
382
382
383
383
387
390
393
395
398
401
402
404
406
407
408
408
409
409
410
412
413
414
419
419
422
426
426
428
430
433
439
440
440
441
442
443
446
446
449
449
450
452
458
459
460
463
463

19.3.4. HYPER
odd(L) even(L)
path( StartNode, GoalNode, Path)
,


20.
20.1. ,

20.1.1.

20.1.2.






20.1.3.

20.2.



20.3.
20.4.
20.4.1.
20.4.2.
20.4.3.
20.5.


21.

21.1. Prolog
21.2.
21.2.1.
21.2.2.

21.2.3.
DCG
21.3.
21.3.1.

21.3.2. "" "every"


21.3.3.


22.
22.1. ,
22.2.
22.3. --:

470
470
471
472
474
476
477
478
478
478
479
480
180
480
480
481
482
486
486
486
486
493
496
497
498
502
507
508
510
510
516
516
518
520
521
521
525
527
531
531
532
532
534
537

11

22.4. , :

22.5.
22.5.1.
22.5.2.
22.5.3.
22.6. Advice
Language 0
22.6.1. ALO
22.6.2. " "


23. Me
23.1.
23.2. Prolog
23.2.1. Prolog
23.2.2.
23.2.3.
23.3.

23.4. -
23.5. ,
23.5.1. ,
23.5.2. Prolog, ,

23.5.3. ,
1
2
23.5.4. ,
23.5.5.

23.6. ,
,


. Prolog

assert retract

not " \ + "
name( Atom, CodeList)
consult reconsult

.

1
2
3
4
5
6
7
8
12

541
543
543
544
544
546
546
549
556
557
559
559
560
560
562
563
564
565
565
570
576
576
578
579
579
579
580
582
583
583
588
589
590
590
590
590
591
591
591
591
592
595
595
595
596
599
600
601
601
602

9
10
.1.1
1:'
13
14
15
17
18
19
20
21
23

603
604
605
606
606
606
606
607
607
608
608
610
610
611
619

13

,
, , ,
, ,
"".



, ,
, Prolog. Shrdlu (Terry Winograd). ,
, " " (, ) , , .
Microplanner,
, , Prolog . Microplanner, ,
Prolog . , "", " ", " ", "" "", ,
.

.
Lisp Lisp,
, .
Lisp
.
,
, . Prolog, .
,
, ,
, ,
, . , Fortran
. ( ) , ,
, 80- .
Fortran
,
. , Lisp , Lisp (, Common Lisp) :: , "HTI
, . Prolog, ,
, , .

15

, Prolog , ,
, .
, . , , . Prolog "" (grandfather), , ( f a t h e r )
(parent). g r a n d f a t h e r
:
grandfather! X, Z) :- father) Kr Y) , parent' Y, Z),
Prolog g r a n d f a t h e r
, , . , Prolog,
.
?- grandfather! X, patrick) .
X = james;
X = carl
Prolog ,
, f a t h e r p a r e n t . , . , ,
.
, Prolog, , .
, , , , , . ,
, .
,
,
, .
,
, , , . , , , .
,
Prolog, -, .
, Prolog,
, :
, , , .
.
, ,
. ,
, .
,
, , , .

Prolog ,
,
. , ,
,
.
, Prolog , ,
:
;
;

;
;
,
Prolog, .
, , .
,
,
,
, ,
.
, , , Prolog.

17


Prolog
Prolog , , , .
.
Prolog ,
( , ) . , Prolog
, , , . : X ,
Y, a Y , Z, X , Z.
Prolog
. Prolog . ,
, .
Prolog ,
.

Prolog
Prolog , 1970- ,
"programming in logic" ( ).
, ,
(Robert Kowalsld) ( ), (Maarten van Eniden) ( ) (Alain Colmerauer) ().
Prolog 1970- . . (David D.H. Warren) .

(Constraint Logic Programming CLP), Prolog. CLP

- . 1996
ISO Prolog,


Prolog
Prolog .
. Prolog . ,

.
,
Microplanner, ,
. Prolog
"
" , ,
"" , .
Prolog. ,
,
. , Prolog .
" " Prolog, , , ,
.

Prolog
Prolog ,
. , , , , , Prolog . ,
Prolog .
, a Prolog , , , . , , Prolog
. ,
, ,
Prolog,
-
.


I Prolog Prolog. , , ,
. II Prolog
, , , , , , ,
. ,
Prolog . . , , ,
Prolog , - ,

19

, , Prolog
Prolog.
; " ", , .


. ,
:
(Constraint Logic
Programming CLP);
;
.
:
( )
;
(IDA*, RBFS), , ;
, ;
, .
Prolog
, , Prolog (.
).


,
Prolog . Prolog , Prolog. ,
, . ; , (,
Pascal) ,
Prolog.

,
Prolog
, DKC-W;
ISO Prolog.
. Prolog
.
, Prolog1.

20


I. 2.4
Prolog , . 4 ,
( ) . 10, , .
II ,
. , , , , ,
( 11). . 1 .

. I.



Web- (www. books i t e s . n e t / b r a t k o ) .

21


Prolog (Donald
Michie). (Lawrence Byrd),
(Fernando Pereira) . . (David H. D. Warren), Prolog , .
(Andrew
McGettrick) . (Patrick H. Winston). ,
,
(Damjan Bojad?iev), (Rod Bristow),
(Peter Clark), (Frans Coencn), . (David , Dodeon),
(SaSo Dieroski), (Bogdan Filipic),
(Wan Fokkink), (Matja* Gams), . (Peter G.
Greenfield), (Marko Grobelnik), (Chris Hinde),
(Igor Kononenko), (MatevJ KovaCic),
(Eduardo Morales), (Igor Mo2etiC), . (Timothy .
Niblett), (DuSan Peterc), (UroS Pompe), (Robert RodoSek), (Agata Saje), (Claude Sammut),
(Cem Say), (Ashwin Srinivasan), (Dorian Sue),
(Peter Tancig1), (Tanja Urbancic),
(Mark Wallace), (William Wai ley), (Simon
Weilguny), (Blaz Zupan) (Darko ZupaniC),
, , .
, . (G. Oulsnam) (Iztok Tvrdy),
.
(Karen Mosman), (Julie Knight) (Karen Sutherland),
Pearson Education,
. (Simon Plunitree) (Debra Myson-Etherington) .
(Darko
SimerSek). , , ,
.
Pearson Education
Plenum Publishing Corporation ,
10 Human and Machine Problem Solving
(1989). . GUhooly (ed.).

2000

22


, , , .

Prolog
Prolog ,
. ,
, , , (Personal Edition), . Visual Prolog ( h t t p : //www. v i s u a l prolog.ccm http://www.pdc.dk). . Prolog
http;//dmoz.org/Computers/Programming/Languages/Prolog/Implementations/
SWI-Prolog, ,
.

Prolog
, ,
Prolog. " ? - " , ,
. , , ,
.
,
. ,
( ),
. " ", (consulting). a s s e r t (
a s s e r t a a s s e r t z ) , .
,
c o n s u l t .
.

. ""
u s e r , .

23

?!:
I:
I:
I:
%

[user] .
man(dima).
man(kolya).
man(petr).
end_of_file.
-jsec://3 compiled 2S.12 sec, 0 bytes
end_of_f i l e . ,
( , , <Ctrl+D> <Ctrl+Z>).

Prolog

, Prolog "" . ""
(-, , ..) . Prolog
. , Prolog
, Java, Delphi, C++ .
, , , .
Web. ( "Prolog" .)

24

Prolog

,.,
1. Prolog
2. Prolog
3. , ,
4. :
5.
.
7.
8.
9.
10,

26
45
76
98
121
136
149
169
192
215

Prolog
...
1.1.
1.2.
1.3.
1.4.
1.5.




Prolog

26
30
35
39
42

Prolog . ,
, , , Prolog. , Prolog
.

1.1.
Prolog , . ,
. . 1.1 :
. , ,
Prolog :
parent( torn, bob).
p a r e n t ; torn bob
. ,
, , tom, .
, . 1.1,
Prolog:
parent ( pain, bob) .
parent! tom, bob).
parent( torn, l i z ) .
parent[ bob, ann).
parent( bob, p a t ) .
parent( pat, jira).
,
p a r e n t . , p a r e n t ( tom, bob)
p a r e n t .
. .

. 1.1.

Prolog
p a r e n t , ,
? Prolog, :
?- parent! bob, pat).

, , , Prolog :
yes
:
7- parent( l i z , pat) .

Prolog :
no
,
. ""
?- patent! torn, ben).
.
, . , ?
7- parent( X, l i z ) .
Prolog "yes" "", X,
. :
X = t
, , Prolog :
?- parent( bob, X).
. Prolog
:
1. Prolog

27

X arm

{
), Prolog :
X - pat

, Prolog
"", .
,
? :
X if, , X Y.
Prolog :
?- parent ( , Y) .
Prolog .
, Prolog ( )
. :
X
Y
X
Y
X
Y


- bob;
torn
= bob;
= torn
= liz;

, <Enter>
.
, ,
, , ,
( ). g r a n d p a r e n t ,
(. 1.2).
1. ? ,
Y.
2. Y7 , X.

. 1.2. grandpa t,

parent

Prolog
:
?- p a r e n t ! Y,

jim),

p a r e n t ! X,

Y) .

:
- bob
Y = pat
28

I. Prolog

: X Y, :
parent ( Y, jim) parer.t ( X, )
,
:
parent! Yf parent( Y, jim)
, Prolog, :
?- parent! X, Y ) , parent( Y, jim).

.
, , :
?-

parent!

tomr

X),

parent;

X,

Y).

Prolog :
= bob
Y - arm;
X - bob
= pat

, , . .
1. (X)?
2. ( ) X ?
Prolog :
?-

parent!

X,

aim) ,

parent!

pat).

;
X = bob

.
Prolog , p a r e n t ,
- , .
Prolog , .
Prolog .
.
( ) , ( torn ), ( X Y). , , .
,
. ,
parent( X, ann), patent! X, pat)
:
X
X .
"" (goal) ,
Prolog , .
, , . 1. Prolog

29

, .
.
, Prolog , ( ).

1.1. , p a r e n t ,
(. . 1.1), , Prolog
?
) ?- p a r e n t { jim, X).
) ?- p a r e n t ( X, j i m ) .
) ?- p a r e n t ! pam, X), p a r e n t ( X, p a t ) .
r) ?- p a r e n t ! pam, X), p a r e n t ( X, Y), p a r e n t t Y, j i m ) .
1.2. Prolog
parent.
) ?
) ?
) ?

1.2.
.
, p a r e n t . , :
female( pam).
male[ torn].
male( b o b ] .
female { ) .
female( p a t ) .
female( ann} .
male( j i m ) .

male female.
( ). p a r e n t
. , ,
.
: . ,
, . :
{ pam, feminine).
sex{ torn, masculine).
sex [ bob, masculine).
o f f s p r i n g
(), p a r e n t . o f f s p r i n g
, p a r e n t ,
o f f s p r i n g , ,
, :
offspringf l i e , torn).

30

I, Prolog

o f f s p r i n g , ,
p a r e n t p a r e n t .
:
X v. Y,
Y ,
>; Y.
Prolog. Prolog, , :
offspring! Y, X) :- pacent( X, Yi .
:
X Y,
X Y,
Y .

Prolog,
offspring! Y, X) :- parent( X, YJ.
. .
,
patent ( torn, ) .
,
. , ,
, .
, :
( );
( ).
, , ,
, , , ,
offspring; Y, X)
:- patent( x, Y}.
parer.t [ X, Y] ,
of f s p r i n g ( , X).
Prolog . , :
?- offspring* l i z , torn).
,
o f f s p r i n g . ,
X Y; , l i z torn. l i z torn,
Y l i z , X torn.
( X Y), :
X < torn Y = li.z
,
:
offspring( l i z , torn) :- parent! ton, l i z ) .

parent( torn, liz)
Prolog ,
. :
offspring( l i z , tomi
1, Prolog

31

:
parent[ torn, l i z ]
, () , . ,
,
Prolog yes.
. mother
:

X
X
X

X Y,
Y,
Y
- .

Prolog :
mother! X, Y) :- parent; X, Y ) , female(X).

;
, .
, p a r e n t , o f f s p r i n g mother,
, . 1.3. . , .. . ( ) .
.

. , ,
. : , , ,
. . 1.3, g r a n d p a r e n t Prolog :
grandparent(X, Z) :- p a r e n t [ X , Y) , parent[ Y, Z).

. 1.3. , offspring,
grandparent e

mother u

.
Prolog .

. , ,
, , ,
.
. , . , grandparent :

32

I. Prolog

grandparent( X, Z) :p a r e n t ( X, Y),
p a r e n t ( Y, Z) .

s i s t e r (. 1.4) :
X Y
X Y,
1) X Y
2) X - .

, 1.4. sister
, . 1.4, Prolog
:
s i s t e r ( X, Y) :- parent ( Z, X), parentf Z, Y) , female(XI.
, "X Y
". : Z X, 2 Y. , : 21 X, Z2 Y Z1 Z2.
:
?- s i s t e r ( arm, p a t ) .
"yes", (. . 1.1).
, s i s t e r , ,
. ,
, :
?- s i s t e r ! X, pat) .
Prolog , .
X - arm;
X - pat
, ?! -, s i s t e r . ,
, Prolog .
, X Y , X Y. , Prolog
( ) , X Y ,
, , ,
.
, , X Y . , , , Prolog d i f f e r e n t
different( X, Y)
1. Prolog

33

, X Y .
s i s t e r :
s i s t e r ) X, Y) :parent[ Z, X),
parent ( Z, Y),
female! X),
different! X, Y) .
.
Prolog , .
Prolog : , .
,
.
,
,
, , .
Prolog .
, .
.
, .
. () .
.
.
, , " ". , .
,
hasachild! X) :- parent! X, Y) .
.
) Y,

X Y,
X .
) X,
X ,
Y, , X Y.

1.3. Prolog.
) , , ( happy
).
) X, X , , X
( h a s t w o c h i l d r e n ) .
1.4. g r a n d c h i l d p a r e n t . : crandparer.t (. . 1.8).
1.5. aunt ( X, Y) p a r e n t s i s t e r .
, aunt (), . 1.3.

34

I. Prolog

1.3.

p r e d e c e s s o r (). p a r e n t . .
() ,
. , X Z,
X Z,
. 1.5. , . 1.1,
.

. 1.5. predecessor: a) X
Z; ) Z
:
2,
X - Z,
X - Z.
Prolog :
predecessor( X, Z) :parent(x, Z).
, , ,
. , 1.6. , p r e d e c e s s o r
:
predecessor [ ., Z) :p a r e n t [ X , ) .
p r e d e c e s s o r ( X , Z) :p a r e n t ( X, Y),
p a r e n t [ Y, 2 ) .
p r e d e c e s s o r ; X, Z) :parent ( X , YD ,
p a r e n t ! Yl, 42),
p a r e n t ! Y2, 1) .
predecessor( X, Z) :p a r e n t t X, Yl),
1, Prolog

35

parent [ Yl, Y2),


parent! Y2, Y3),
parent( Y3, Z ) .

. 1.6. ,

, ,
. ,
.
p r e d e c e s s o r
. ,
. , p r e d e c e s s o r . . 1.7 :
,,
X - ,
Y,
1) X Y
2) Y - Z.
Prolog1, , .
predecessor( X, Z) :parent[X, Y ) ,
predecessor( Y, Z} .
, p r e d e c e s s o r ,
: , . , , .
predecessor( X, Z) :parent(X, Z) .
predecessor( x, Z) :parent! x, ) ,
predecessor! Y, Z) .
, p r e d e c e s s o r
.

36

I, Prolog

, , , , . .
;
, , . 1.7.
, Prolog
. ,
.
Prolog. Prolog - .

. 1.7. predecessor

, Prolog
, . , ?
? - p r e d e c e s s o r ( pam,
X = bob;
X = aim;
X = pat;
X = j ira

X).

Prolog, ,
p r e d e c e s s o r p a r e n t .
: Prolog ?
, Prolog, . ,
. 1.1.
, :
-, , -, , .
1.1. f a m i l y
parent(
parent[
parent[
parent[
parent(

, bob) .
torn,
torn,
bob.
bob,

bob) .
liz) .

arm) .
pat) .

1. Prolog

37

p a r e n t ( p a t , jira).
female( pam).
male( torn) .
male( b o b ) .
female[ l i z ) .
female [ arm) .
female[ p a t ) .
malej j i m ) .

% -
% -

o f f s p r i n g ! Y, X) :p a r e n t ! X, Y) ,

% Y X,
% X Y

mother[ X, Y) :p a r e n t ( X, Y),
female( X).

% X ,
% X Y
% X -

grandparent( , 2) :p a r e n t ) X, Y),
parent ( Y, Z).

% X Z,
% X Y
h, Y Z

s i s t e r f X, Y! :parent* . ),
p a r e n t ( Z, Y),
female( X),
d i f f e r e n t ! X, Y).

% X Y,
% X Y
* X -
% X Y

p r e d e c e s s o r ! X, Z) :p a r e n t ! X, Z) .

% p r l : . - Z

p r e d e c e s s o r ! Z) :p a r e n t ( Xr Y),
p r e d e c e s s o r ! Y, Z)

* 2: X - Z

, 1.1, :
p a r e n t , male, female, p r e d e c e s s o r .. , p r e d e c e s s o r . ,
p r e d e c e s s o r .
, .
.
1.1 , p r e d e c e s s o r ,
, p r l 2, .
.
Prolog.
, .
Prolog " / * " "*/". , Prolog
:
/ - */
, , " : t". , "%"
, .
\ -

1.6. p r e d e c e s s o r :
predecessor! X, Z) :parent! X F Z).

38

I. Prolog

predecessor [ X, 2) :parent[ Y, Z),


predecessor! X, ).
p r e d e c e s s o r ?
, . 1.7,
.

1.4.
Prolog

Prolog. Prolog . , Prolog
. " "?
, ,
, . , : ,
, . ,
Prolog ( ), . ,
. Prolog - , , "".
, Prolog : Prolog , , ; Prolog
, .. , .
Prolog . , .
.
- .
:
.
, ,
:
X, . - , .
Prolog,
.
f a l l i b l e ! X} : - man; X } . %
man( socrates).
% -
?- f a l l i b l e ( s c c r a t e s ) .
% ?
yes
%
, (. 1.1), .
?- predecessor! torn, p a t ) .
, p a r e n t ( ', p a t ) .
p r l , ,
p r e d e c e s s o r ! bob, p a t ) . , ,
, . , , :
1. Prolog

39

parent[ bob, pat) ==> predecessor! bob, pat)


: p a r e n t [ bob, pat)
p r e d e c e s s o r { bob, p a t ] , p r l . , , p a r e n t ( torn, bob).
predecessor;
bob,
p a t ) , ,
p r e d e c e s s o r ; torn, p a t ] , 2. ,
p r e d e c e s s o r ( torn, pat} .
:
parent;' bob, pat) ==> predecessor; bob, pat)
parent; torn, bob) predecessor! bob, pat) ==> predecessor) torn, pat)
, , , , .. , . .
, Prolog .
Prolog , , . , , ,
, , . :
?- predecessor; torn, p a t ) .
Prolog . , . ,
p r l 2. ,
p r e d e c e s s o r . , .
, p r l 2,
Prolog. ,
:
predecessor! X, Z) :- parent! X, Z).
p r e d e c e s s o r ; torn, p a t ) , :
X torn, Z p a t

p r e d e c e s s o r ; torn, p a t ) ;
parent! torn, pat)
,
, . 1.8. , p a r e n t ; torn, p a t ) , . Prolog
,

p r e d e c e s s o r ( torn, p a t ) , 2 :
predecessor! X, Z) :parent! X, Y ) ,
predecessor; Y, Z) .

, X Z :
X = torn, Z = pat
Y . p r e d e c e s s o r ; torn, p a t )
:
parent ( torn, Y) ,
predecessorf Y, pat)
40

I. Prolog

. 1.9,
, . 1.8.

. 1.8.
; , , , ,

. 1.9. ,
, . 1.8

Prolog
, . , . Y
bob. , ,
predecessor( bob, pat)
p r l . ,
() . Prolog . ,
p r l :
predecessor! X 1 , Z') :parent( X 1 , Z'),

p r e d e c e s s o r
[ bob, p a t ] , :
1 = bob,
z' = pat
:
parent( boh, pat)
, . , . 1.10.
(. . 1.10) .
, . () , . ,
( ) -, "yes". "yes",
. Prolog
, .
Prolog , . , , Prolog
.

1, Prolog

41

. 1.10. predecessor ( torn,


pat). ,

1.7. , Prolog
, 1.1.
. 1.8-1.10.
?
) ?- p a r e n t ( pam, b o b ) .
) ?- mother( pam, b o b ) .
) ?- g r a n d p a r e n t [ pam, a n n ) .
r) ?- g r a n d p a r e n t [ bob, j im) .

1.5.

, , .
, ,
Prolog, a
,

.
, .
,
. , , , , Prolog.

42

!. Prolog

Prolog
. Prolog
, .
, ( )
. ,

. , , , .
Prolog.
Prolog , Pascal. , ,
.
,
- ,
,
.
Prolog , .

Prolog
.
, : ,
.

.^- , , , .
,
.
, ,
. Prolog , .
Prolog , ,
,
, , , . Prolog ,
, .
. Prolog: .
. .
:
, , , ;
, ;
, ;
;
1. Prolog

43

, ;
;
;
, , , ;
, , , ;
;
, .


Prolog . ( DEC-10, Prolog DEC-10,
[9], [122]). ,
ISO Prolog, ISO/TEC 13211-1 [43]. Prolog .
, . .,
,
.

44

I. Prolog


Prolog

...
2.1.
2.2.
2.3. Prolog
2.4.
2.5. :
2.6.
2.7. Prolog

45
52
56
58
62
66
73


Prolog .
:
(, , );
;
;
( ) ;
;
;

.
1, .

2 . 1 .
. 2.1 Prolog. Prolog .
, Prolog
. 1
;
, . Prolog ( ) ,
.

2.1.1.
1 .
, ..
, :
, , ..., Z;
, , .... z;
0, 1, 2, .... 9;
, "+", "-", "*", "/", "<", ">", "=", " : " , " . " , "&",
.
1. , ("_"), :

nil
2 5
_25
_25
:-:_

alpha ^ procedure
miss_Jones
sarah_j ones

2. :
<>

R (jiopvo ; ,
; " : - " .
3. , . , , , , . , :

46

!. Prolog

'Tom'
'Sarah Jones'
, Prolog,
. , :
:

1313

-91

, , Prolog.
,
, :
3.14
-0.0035
100.2
Prolog
. , Prolog
, , , , , ,
.

Prolog, , . , ,
& .

- ,
. ,
10000 + 0 . 0 0 0 1 - 10000

0 0. 0001.

2.1.2.
, ,
. :

Result
Object2
Participant_list
ShoppingList

, .
, . , :
hasachildt X) :- parent; X, ).
: " X, X , X ". , h a s a c h i l d , ,
, . , .
, :
hasachildl X) :- parent! X, _ ) .
. , ,

2, Prolog

47

, , , :
somebody_has_child :- parent! _, _ ) .
:
somebody_has_child ;- parent! X, Y).
, , :
5omebody_has_child :- parent) X, X).
,
, Prolog . , , , :
?- p a r e n t ( X, _>,


. , , XI5 , . 15 .
:
( , ) .

2.1.3.
( ) , . , , . , : , , . , .
.
d a t e . , "1
2001 " {. 2.2):
date(

I,

may,

2001)

. 2.2. ; ) ;
) ,
Prolog


.
.

( )
, ,
:

d a t e ( Day, may,

2001}

, Day
- .
.
, Prolog
, .
48

I. Prolog

Prolog . ,
may

date! I, may, 2001)



(. . 2.2). , .
, , .
, (. 2.3). ;
, . ,
:
p o i n t , ;
, ;
t r i a n g l e , .

, 2.3.

, . 2.3, :
PI p o i n t ( 1 , 1 )
2 - p o i n t ( 2 , 3 !
S = s e g < P I , 2) = s e g ( p o i n t ( 1 , 1 1 , p o i n t ( 2 , 3 ) }
T - t r i a n g l e [ p o i n t ( 4 , 2 ) , p o i n t ( 6 , 4 } , p o i n t ("7,1)1

, 2.4,
, , , .
,
, ,
point3:
point3( X, Y, Zl

, p o i n t ,
- , , :
point( XI, YD pointt X, Y, Z)
, p o i n t , Prolog ,
2. Prolog

, . ,
.
1. , , .
2. , .. .

, 2.4, , . 2.3

, Prolog , . ,
Prolog . . 2.5 , :
( + ) * ( - 5)

. 2.5. , , ( + )
* ( - 5)

, , , "*", "+" " - " , :


* ( .+ [ , ) , - ( , 5) )

, , Prolog,
. , , -

50

I. Prolog

, .
Prolog
, " * " , " + " " - " . , , 3.

(. 2.6). . r l , -2, 4 ,
p a r z seq . Prolog.
seq< r l , r2)
par( r l , r2)
par( r l , p a r ( r 2 , r3) 1
par( El, s e q ( p a r ( r 2 , r 3 } , r 4 ) )

.. 2.6.
: ) rl z2; )
; )
; ) 2

2. Prolog

51


2.1. Prolog ?
(, , , )?
) Diana.
) diana.
) 'Diana'.
) _diana.
) 'Diana goes south',
) goes ( diana, south),

) 45.
a) 5( X, Y>.
) -( north, west).
) three( Black< Cats)).
2.2. , Prolog. , . 2.4. ,
(, , ).
, ,
.

2.2.
, .
. .
, , , .
1. .
2.
,
.
, d a t e [ D, , 2001) n d a t e t D l , may, Y1) .
, .
D D1.
may,
Y1 2001.
,
Prolog :
D - D1
= may
Y1 = 2 0 0 1

, d a t e ( D, , 2001) d a t e (Dl, Ml, 1444) ; d a t e [ X, Y, Z) p o i n t ( X, Y, Z),


,
, .
, , 52

I. Prolog

, , ,
.
. Prolog
" = " :
?- d a t e ; D, ,

2001) = date< Dl, may, Y l ) .

D = D1, = may, Yl = 2001, . , . .


D = 1
D1 = 1
= may
Yl = 2001
D - third
Dl = t h i r d
M = rociy
Yl - 001

, , D D1 , . , D D1,
.
Prolog . ,

( , ). :
?- date! D, , 2001) = date ( Dl, may, Yl) ,
date[

D,

M,

2001)

= date(

15,

M,

Y).

Prolog :
D =

D1

= may
Yl = 2001

,
.
D = 15
D1 = 1 5
- may
Yl = 2001
Y = 2001

,
.
, , S 1.
1. 3 , , .
2. S , ,
S . , ,
S.
3. S , , :

2. Prolog

53

) S ;
) .
.
,
, , . 2.7.
( ). , . ,

()

.
triangle = triangle,
p o i n t ( 1 , 1 ] = X,
= point(4,Y),
p o i n t (2,3) = p o i n t ( 2 , Z ) .

. 2.7,

triangle! point
(1,1),
A, point
(2,3)}
=
triangle { X, point (4, Y) , point (2, Z) )

, . :
= point [1,1)
= point(4, Y}
Z = 3

,
. (. , 2,4)
.
, Prolog . ,
. 2.8, , . , . ; -

54

I. Prolog

. , . ,
:
vertical; seg ( point(X,Y), point (X, YD ).
horizontal( segl point(X,Y), point(XI,Y)).

. 2.8. .

:
?- vertical( seg{ p o i n t t l , 1 ) , point(l,2) j ).

yes
?- v e r t i c a l ! seg; point [1,1), p o i n t ! 2 , Y ) ) ).
no
;- horizontal! seg( point(l,l), point(2,YJ ) ),
Y- 1

"yes",
. . Y 1 .
,
- ,
{2,3)?
?- vertical( seg(point(2,3),PJ).
P = point[2,Y}

, , (2,Y), ,
= 2. , Prolog
, , ( Prolog1) :
= point(2,_136)
. _136 , , _13 , . ,
. : -, ,
, , -, - ,
"" .
2, Prolog

55

,
, .
? - v e r t i c a l ! S), h o r i z o n t a l ! S ) .
s = segl p o i n t (X,Y), p o i n t (x,Y> )

Prolog
, , , .
. , X Y
, .

2.3. ? , ?
) p o i n t ( , ) = p o i n t ! 1, 2).
) p o i n t ( , ) = p o i n t ( X, Y, 2).
) plus{ 2, 2) = 4.
) +( 2, D) = +{ , 2).
) t r i a n g l e ( p o i n t ( - l , 0 ) , 2, ) = t r i a n g l e ; PI, p o i n t ( l , 0 ) ,
p o i n t (0,Y)).
. ?
2.4. , , ,
, = 5.
2.5. , r e c t a n g l e ! PI, P2,
, 4), ,
.
regular( R)
, R .

2.3. Prolog
1,
Prolog: .
Prolog. . :

1-

Q,

R.

, R . .
, Q R .
Q R .

.
, Q,
- ..
Q, - R.
56

!. Prolog

,
,
, , .
.
,
, . , . , . ,
. , :
hasachildl ) :- parent( X, Y}.
.
hasachild( ) :- p a r e n t ! A, 3 ) .
hasachildl XI) :- p a r e n t ( XI, 2) .

:
hasachildt peter) :- patent! peter, 2).
hasachildt barry! :- parent( barry, small(Caroline)).
-,
.
G (.. , ), :
1. , ,
2. I , ,
) I G
) I .
Prolog .
, Prolog , . , . .
,
. Prolog ,
.
. ,

:-

Q;

: ? , Q R.
, :

:-

Q.

;-

R.

, .

:-

Q,

R;

S,

:-

Q,

R);

S,

T,

D) .

::-

Q,
R.
5,'",

U.

2. Prolog

57


2.6. :
f( 1, one) .
f ( s(1) , two) .
f ( s ( s ( l ) ) , three) .
f ( s(s(s(X) ) ) ,
f ( X, N) .

N>

:-

Prolog ? ,
, .
) ?- f{ s ( l ) , ).
) ?- f ( s ( s i l ) ) , two) .
) ?- f I s<s ( s ( s l s l s U ) ) ) ) ) ) , C) .
r) ?- f ( D, t h r e e ) .
2.7. , , ;
) ,
6} ,
) .
relatives! X, ) :predecessor( X, Y ) .
relatives! X, Y) :predecessor ( , .) .
relatives( X, i) :predecessor! Z, X ) ,
predecessor; Z, Y ) .

% X Y

relatives 1 X, Y) :predecessor! X, Z ) ,
predecessor! Y, Z) .

% X Y

, .
2.8.
.
translate I Number, Word! :Number 1, Word one;
Nuiriber = 2, Word = two;
Number = 3, Word = three.

2.4.
, Prolog .
, .
, , , , .
Prolog . " " .
execute. . 2.9,
:
;
/ .
58

I. Prolog

. 2,9. .,

.
1. / "yea",
, "" . , "yes" , "" .
2. ; .
1, 1.4, "
Prolog", , execute. , , - .
,
2.1.
, .
:
Gl, G2,

. . . , Gm

execute .
, .
, () , "SCANNING".
SCANNING.
, , , G1. ,
.

:- 1,

. . . ,

..


, , G1, . . ., Gm . ,
' : - 1 '
1 .
G1 '; , S.
Gl, G2, ..., Grr. G1 1 ' , ,.,, E n ' ,
:
1 1 , . . . , , G2, . . . , Gm

2. Prolog

59

( , , = 0
, ;
,
, .)

, S,
:
1 1 ' , . . . , 1 ' , G21
Gm'
(,
) .
, . ,
SCANNING. , ( , ),
.

2.1. , Prolog:
e x e c u t e
".
big( bear).
big{ elephant).
small( c a t ) .

% 1
% 2
% 3

brown[ bear).
black[ c a t ) .
gray( elephant).

% 4
% S
% 6

dark) Z> :blackf Z ) .

% 1 -
%

dark( Z) :% S - -
%
brown(Z(.
*
? - dark (X), big (X). % ?

1) & : dark(X), big<X).
2) , ,
, dark(X). 7:
dark(Z) ;- black [ Z) .
7,
;
black ( X) , big{ X)
3) , black( X ) .
: black(oat). (

big( cat)
4] , big( cat),
. (3) .
= cat.
black( X) , big( X)
5.
. (2)
7, 8:
dark{ Z> :- brown( Z>.
brown[ X) ,

60

I. Prolog

brown ( X) , big( X) .
5) , brown( X } ,
brown( b e a r ) . ,

big( bear)
6) , b i g ( b e a r ) .
, .
,
v.v.% e
X = bear
2.2 , Pascal.
, e x e c u t e . ,
, . 5, , , , e x e c u t e .
execute SCANNING , . , Prolog .
: Prolog ( ),
.
, , . , Prolog ,
, ,
, .
, . e x e c u t e .
, Prolog
execute .
. Prolog
; , .

2.9. , 2.1, Prolog


:
?- b i g l .) , dark ( X) .
2.1,
,
:
?- dark( X), big( X).
Prolog , ?

2. Prolog

61

~
2.2. Prolog
procedure execute ( Program, GoalList, Success);
;
Program -
GoalList -
:
Success ; Success truet
GoalList true Program
:
Goal -
OtherGoals -
Satisfied -
MatchOK -
Instant -
, 1 , Bl, B1', ...,, 1 -
:
empty(L) - true, L -
head(L) - L
tail(L) - L
append(LI,L2) - L2 L1
match(1,2,MatchOK,Instant) - 1
12; MatchOK
true, a Instant

substitute(Instant,Goals) - Goals
Instant
if empty(GoalList) then Success := true
else
begin
Goal :- head[GoalList);
OtherGoais := tail(GoalList1;
Satisfied :- false,while not Satisfied and " " do
begin
, Program
:- 1, ..., .

1 :- 1', ..., '.
match[Goal.H1,MatchOK,instant);
if MatchOK then
begin
NewGoals := append([Bl',.. .,Bn'], OtherGoals);
NewGoals := substitute(Instant,NewGoals);
execute(Program,NewGcals, Satisfied)
end
end;
Success := Satisfied
end
end;

2.5. :
. Prolog,
, , .
,
. , .
62

I. Prolog

. , , . .
, .
, .
: , ,
( ) ,
. ?
. ", ", , .
. ,
.
1. .
2. .
3. .
4. .
. s t a t e (),
. . 2.10 , .

. 2.10. ,
. : , (
), ,

.
. -, ,
; , , :
S-L6.ZS { _, _,

, has)

-, , , . .
1. (grasp).
2. (climb).
3. (push).
4. (walk).
. , " " , ( ) -

2, Prolog

63

. Prolog
, move:
move( Etatel, Move, Stace2)
:
Statel
State2
Move
S t a t e l , Move
S t a t e 2 .
g r a s p , , :
move( state( middle, onbox, middleF hasnot),
i
grasp,
%
s t a t e l middle, onbox, middle, has] ).
i
, , , .
, Posl Pos2.
,
, . Prolog:
move{ s t a t e { P o s l , onfloor, Box, Has),
walk( Posl, Pos2),
s t a t e f Pos2, o n f l o o r . Box, H a s ) ) .

% Posl Pos2

, , :
" Posl
Pos2";
;
>:,
;
" " Has .
, ,
, .
. Prolog Prolog.
, push climb, .
:
S t a t e ?
:
canget( State)
S t a t e . canget
.
1, , , canget,
, ; -
. Prolog:
canget( state! _, _, _, has}).

2. . S t a t e l ,
Move S t a t e l
S t a t e 2 , , S t a t e 2
64

!. Prolog

( ).
. 2.11. Prolog, , .
canget( Statel) : move[ S t a t e l , Move, State2),
canget( State2).
, 2.3.
canget p r e d e c e s s o r , 1 ( . 1.7 2.11). Prolog .

. 2,11. canget
2.3.
\ move( Statel, Move, State2) - Move Statel
$ State2; :
% state{ MonkeyHorizontal, MonkeyVertical, BoxPosition, HasBanana)
move! state) middle, onbox, middle, hasnot),
%
grasp,
i
state) middle, onbox, middle, has) ).
%
move ( statel P, onfloor, P, H) ,
climb,
l
state[ P, onbox, P, H) ).
move [ state ( PI, onfloot, PI, H},
push! PI, P2),
% 1 2
state! P2, onfloor, P2, ) ).
move( state( PI, onfloor, B, Hi,
walk! PI, P2),
state( P2, onfloor, , ) ).

% PI 2

% canget) State) - State


canget) state! _, _, _, has) ). 4 canl -
canget) statel) :% 2 -
,
move I Statel, Move, State2),
%
canget[ Etate2).
%

, .
:
?- cangetf state) atdoor, onfloor, atwindow, hasnot)}.

Prolog "yes". ,
Prolog , ,
Prolog, . .

, . . . 2.12.

2. Prolog

65

. 2.12. .
, .
.

Prolog
.
. , ,
move. ( ) . .
, , , . , ,
.

2.6.
2.6.1.
:
=- -

66

I. Prolog

, " ,
". , . Prolog. :
?- .

, , , , ..
Prolog , .
, Prolog
.
. .
,
move, : g r a s p , c l i m b , push, walk
(, unclirab ).
, ,
.. Prolog, , , ,
.. . , ? , "walk" .
:
?- canget( s t a t e ( atdoor, onfloor, atwindow, hasnot)) .
. ( , )
, .
1) canget[ s t a t e I atdoor, onfloor, atwindow, hasnot) )
c a n g e t ( ' c a n 2 ' ) ,
.
2) move( s t a t e ( atdoor, onfloor, atwindow, hasnot), M1, S2 ') ,
canget( 52')
walk {atdoor., P2 ') :
3) canget( s t a t e ! P2', onfloor, atwindow, hasnot) )

an 2
4) move( s t a t e ( 2 ' , onfloor, atwindow, h a s n o t ) , M'', S2 " ),
canget( 5 2 ' ' )
. ,
, , walk (
climb, ). :
S2'' - s t a t e ( 92'', onfloor, atwindow, hasnot)

5) canget[ s t a t e ! 2 ' ', onflcor, atwindow, hasnot))
2, :
6) move[ s t a t e ( 2 ' ' , onfloor, atwindow, h a s n o t ) , M ' ' ' , S2 ' ' ' ) ,
canget( S 2 ' ' ' )


walk, :
7) canget{ s t a t e ( 2 ' ' ' , onfloor, atwindcw, hasnot))

2. Prolog

67

3), 5) 7). , ; ' , ' ' " ,


, . , , 3), . , , .2 walk. , . - ,
() ; Prolog
, .
, Prolog
, , , .
Prolog - . -
.
, Prolog , , , .
Prolog ,
, .
, - , , . , ,
, ,
.
, , ,
.
, , , .

2.6.2.

, 1,
, . , 1 , p r e d e c e s s o r :
predecessor) Parent, Child! :parent( Parent, Child).
predecessor{ Predecessor, Successor) :parent{ Predecessor, Child),
predecessor( Child, Successor).
. ,
,
. Prolog,
,
1. .
2. .
p r e d e c e s s o r ,
. , .
.
1. .
68

I. Prolog

2.
.
, p r e d l , pred2, pred3 pred4,
2.4.
2.4. p r e d e c e s s o r
% predecessor
%
predK X, Z) :parent{ X, Z) .
predK , Z) :patent! X, Y),
predK Y, Z) .
* :
pred2( X, Z) :parent! x, Y) ,
pred2( Y, Z) .
pred2( X, Z) :p a r e n t ( X , Z) .
* :
pred3< X, Z] :parent ( X , Z) .
pred3( X, Z) :pred3(X, Y) ,
parent ( Y, Z) .
% :
pred4(X, Z) :pred4(X, Y),
parent( Y, 2) .
pied4( X, Z) :parent(X, Z).
, , .
parent (. . 1.1 1). , ,
,
predecessor, .
?- predl( torn, pat; .
yes
?- prred2 ( torn, pat) .
yes
?- pted3( torn, p a t ) .
yes
?- pred4( torn, p a t ) .
Prolog . ,
Prolog , "More core
needed" ( ) "Stack overflow" ( ).
p r e d l ( 1
predecessor), , . 1.10
1. . 2.13 pred2, pred3
pred4. . 2.13, s , predO, . 2.13, , pred2 p r e d l , .
2. Prolog

69

70

I. Prolog

. 2.13,
predecessor
,

:
. p r e d e c e s s o r
.

2. Prolog

71

1. , ,
parent: predecessor-.
2. , -,
(, , ,
p a r e n t , , p r e d e c e s s o r ) .
p r e d e c e s s o r
p r e d l . ,
<3
, pred2 .
p r e d l ,
.
.
p r e d e c e s s o r , .
, p r e d l , prcd2 ; pred4
, pced3 , .
, pred3 .
?- pred3 ( l i z , jim) ,

. pred3
.

2.6.3.

, . , ,
, .

. ,
? ,
,
p r e d e c e s s o r ! -,

2}

: - p r e d e c e s s o r ! X,

2).

, .
, ,
,
,
.
, . Prolog
, , , :
, . ,
- ,
( ).
,
, ,
. ,
, ,
72

!. Prolog

,
, , , , .

2.7. Prolog
Prolog , . , Prolog . Prolog , . ,
, Prolog ,
. ,
Prolog ,
, , .
Prolog ()
, ( , )
(
, ,
). [32] Prolog,
. Prolog ,
,
[131]. Prolog , SLD.

,
([58]; [60]; [126]; [133]; . [51]).
, Prolog , [89].
Prolog ,
.
"", Prolog
,
(. . 2.10). , .

(occurs check), , . , .

2.10. , Prolog :
?- = f ( ).
?

,
, 2.2? ,
Prolog :
- f[f[f(f(f(f(f(f(f(f(f(f[f<f{...
2, Prolog

73


Prolog,
" Prolog". "", .
( 3-7) , ,
. .
Prolog , .
( ) , .
.
.
.
.

.
. Prolog
.

, .
,
Prolog , , , ,
, .
, .
Prolog . .
.
" Prolog" .
. ;
,
, , . .
, , , .
:
: , , , ;
;
74

I. Prolog

, ;
;
;
;
;
, ;
;
.

2. Prolog

75

, ,

...
3.1.
3.2.
3.3.
3.4.

76
78
87
92

,
, . ,
,
. Prolog,
2,
.

3 . 1 .
,
. ,
, arm, t e n n i s , torn, s k i i n g . Prolog :
[ arm, tennis, torn, skiing]
, , . 2, Prolog . .

Prolog : ,
. Prolog,
[ ].
.
1. , .
2. , .

[ ann, tennis, torn, skiing!
ann, :

[ tennis, torn, skiing]


, Prolog
(, ), .
:
. [ Head, Tall)
T a i l , , , ,
.
Prolog - ,
, .
:
. { arm,

, ( tennis,

. ( torn,

. ( skiir.g,

[]>!))

. 3.1. ,
. ,
:
I skiing]

. 3.1. [ ., tennis, torn, skiing)


:
[skiing]

.(skiing,

[])

, Prolog . , , ,
, . Prolog , , .
, . ,
, ,
.

. Prolog:
?- L i s t l = [a, b , c ] ,
L i s t 2 = . | a, . ( b, . ( c, [ ] ) ) )
Listl = [a,b,c)
List2 = [a,b,c]
?- Hobbiesi = . ( t e n n i s , .( music,

[])),

. , ,

77

Hobbies2 = [ skiing, food] r


L = [ ann, Hobbiesi, torn, Hobbies2].
Hobbiesl = [ tennis, music]
Hobbies2 - [ skiing, food]
L [ ann, [ tennis, music], torn, [ skiing, food] ;
,
; , .

. , , :
L = [,,]
:
T a i l [b,c] L = .{ a, T a i l )


, Prolog
: ,
, :
= [a I Tail]
,
,
" |" . :
[,,]

1,]

!,

[]

[,,

[]]

.
, , : . .
Prolog . Prolog , ,
:
[ Iteml, Item2y ...]

[ Head ] Tail]

I Iteml, Item2, ... | Others]

3.2.
,
, ,
; , , .
. , :
, , ;
, ; ;
.

,
78

I. Prolog

3.2.1.
,
:
member! X, L)
X , a L . member ( X, L) , X L. ,
member! , 1,,)]

,
member ( ,

[, [ , ] ] )

,
member (

[b,c],

[ a , [ b , ] ] )

.
.
X L, :
1) X L
2) X L.

;
, :
member! X, [X | Tail]) .
member) X, [Head | Tail]) :member! X, Tail) .

3.2.2.
:
conc( LI, L2, L3)
L1 L2 , a L3 . :
cone ( [a,b], t e d ] , [a,b,c,d])
,
cone (

(a,b),

[c,d],

[a,bra,c,d])

. cone
, , L1.
1. , ( L); Prolog:
concf[], L, L) .
2. cone ,
:
[X I

L1]

. 3.2 [X | L1]
L2. [X I L3], L3 L1 L2. Prolog :
[ (X | L 1 J ,
( L I , L 2 ,

L 2 , [X
L3).

L3])

:-

,
:
?- conct [a,b,c], [1,2,3], h) .
L - [a,b,,1,2,3]
?- conc([a,[b,c],d],
L = [a,
[ b , c ] , d , a,

[a,[],b],
[),b]

L).

. , ,

79

. 3.2.

cone ,
. , cone
, , .
?LI
L2
Ll
L2
Ll
L2
Ll
L2
no

c o n e ( L I , L2,
= []
= [a,b,c);
- [a]
- tb.cj;
- [a,b]
- [c];
- [a,b,c]
= [];

[a,b,c]).

[, , ] ,
cone .
, . , , , , , .
?- conc-( Before, [may | A f t e r ] ,
[jan,feb,mar, r , m a y , j u n , j u l , a u g , s e p , o c t , n o v , d e c ] ) .
Before [ j a n , feb, mar, apr]
After [jun, j u l , aug, sep, o c t , nov, dec] .

, , , , , :
7- ! _, [Monthl,may,Month2 ! _ ) ,
[jan, fsb,mar, apr,may, jun, jul, aug, sep, oct, nov, dec] } .
Monthl = apr
Month2 - jun

, , , L1 ,
z L1, , :
?- L1 - ta,b, , , , , z, z , d , e ] ,
cone[ L2, | , z , 2 I _ ] , L l ) .
Ll - [a, b , z, z, c, z, z, z, d, e]
L2 = [ a , b , 2, z , c ]


.
, :
member1( X, L) :c o n c ( L1, [X | L 2 ] , L } .

80

I. Prolog

: X L, L , X
. , member! , member.
,
. , :
memberl[ X, L) : [ _ , [X | _} , L) .

, mennber memberl.
, :
, X L:
1) , X L,
2) , X L.
, memberl , . , , memberl
. ; :
?-

meirJoerl (

[ , , ] ] .

. 3.3, , memberl member. ,


.

. 3.3, , member!
, ,

3, , ,

81


3.1. cone .
) L , L1. : I
L1 .
)
L L2.
3.2.
last( Item, List)

, Item L i s t . : ) cone; )
cone.

3.2.3.
, , . X
L , X,
[X|L]

- . , , :
a d d ( X, L, [X | L] ) .

3.2.4.
X L :
d e l ( X, L , L 1 )

L1 L X. d e l . .
1. X , .
2. X , .
del{ X, [X | T a i l ] , T a i l ) ,
del{ X, [Y | T a i l ] , IY | Taill])
deli X, Tail, T a i l l ) .

:-

member, d e l . , X, del ,
. , X, , :
?- del! a, [a,b,a, ], L ).
L = [ , , 1 ;
- ta,b,al ;
L = [,,] ;

del ,
, .
82

I. Prolog

, d e l
. ,
[ 1 , 2 , 3 ] , , ,
L, ,
[1,2,3], .
?- d e l a, L, [ 1 , 2 , 3 ] > .
L - [,1,2,3] ;
L - [1,,2,3] ;
L = [1,2,,3] ;
. = [1,2,3,];

X L i s t BiggerList :
insert( X, List, BiggerList) :del( X, BiggerList, List).
memberl
cone.
d e l . : X L i s t , , .
member2( X, List) :del( X , List, J .

3.2.5.
s u b l i s t .
L S, , L ,

sublist (

[c,d,e],

[a,b,c,d,e,f]l

,
subiisti

[,],

U,b,c,d,e,f])

. Prolog s u b l i s t
, memberl,
(. 3.4). , ,
.
S , :
1) L * , L1 L2,
2) L2 , L3,

L2

L1

member! X, L)

[X|L2]

L1

L3

sublistl S, L)

L2

. 3.4, member sublist


3. , ,

83

, cone.
Prolog :
sublist< s, L) :cone[ LI, L2, L),
cone( SF L3, L2).
, s u b l i s t . , ,
, , , , :
?- sublist{ S, [a,b,c] >.
s = [ ] ;

S = [a];
S = [a,b];
S = [a,b,c];
S =

Lb);

3.2.6.

. permutation . , , . permutation, :
?- permutation ( [ a , b f c ] , P).
= [,,];
- [,,];
- 1,,] , p e r m u t a t i o n
, .
1. , .
2. , [X | L] , . 3.5,
L, L1, X L1.

. 3.5. [X | L]

Prolog, .

84

I. Prolog

permutation! [ ] , [ j ) .
permutation( [X | Lj , P) :permutation( L, L I ) ,
insert* X, LI, P ) .

, (X) , , X . :
permutation2( [ ] ; [ ] !
p e r m u t a t i o n 2 ( L, [X j
d e l { X, L, LI) ,

P])

:-

permutation2{ LI, p) ,

. :
?- permutation! [ red, blue, green], P).
:
?=
=
=
=
=
=

red, blue, g r e e n ] ;
ted, green, " blue] ;
blue, red, g r e e n ] ;
blue, green , red];
gresn , rtid, blue);
green , blue, red],-

p e r m u t a t i o n :
?-

permutation {

L,

[a,b,c]).

, permutation,
L . , "", , ,
. , perm.utation2,
() , .
.

3.3.
evenlength( List) cddlength( List)

, , ,
, ,
( ). , ( , , , d] evenlengtri, [ , , ]
oddlength.
3.4.
reverse! List, ReversedList}
, r e v e r s e ( [ ., , , d ] , [ d, , , ] ) .
3.5. palindrome ( L i s t ) . ,
,
[, , , , .].
3.6.
3. , ,

85

shift( Listl, List?)


, L i s t 2 L i s t l " "
. ,
- s h i f t < [ 1 , 2 , 3 , 4 , 5 ) ,
s h i f t ( LI, L 2 ) .

L1) ,

;
L1 =
L2 -

[2,3,4,5,1]
[3,4,5,1,2]

3.7.
translate{ Listl, List2)
0 9 ,
, :
translate( [3,5,1,3], [three,five,one,three])
:
means( 0, zero).
ir.eans ( 1, one) .
means( 2, two).
3.8.
subset( Set, Subset)
Set Subset , .
,
,
, , :
?- s u b s e t ( [ a , b , c ] ,
S = [ a, b , ] ,S = [a,b];
S = [a,c];
S = [a];
S [b,c];
S = [b];

S] .

3.9.
dividelist( List, Listl, List2)
, L i s t
L i s t l L i s t 2 , L i s t l L i s t 2 ,
d i v i d e l i s t ([ , , , d, ] , [ , , ] , [ , d ] ) .
3.10. , 2,

canget{ State, Actions)


"yes" "", , , :
Actions - [ walk[door,window), push(window,middle), climb, grasp]
3.11.
flatten( List, FlatList)
L i s t , a F l a t L i s t L i s t , "" ,
{ ) L i s t , :
?- f l a t t e n !
[a,b, [c,d], [], [[[]] ],fj,
L =
[ a , b , c , d , e , f]

86

L) .

I. Prolog

3.3.
, .
2 * + *

+ * , 2, ,
. , + * ,
.
, . 3,6,
Prolog + * :
+ (

* |2,] ,

* {,} )

, , Prolog , Prolog :
2* + *

. 3.6.
2*a + b*c

,
,
Prolog,
, .
, Prolog
. + , Prolog , +{ , ) . Prolog
, + *, , *
, +. , + , *,
. , + *

+ {,

* (,) ;


*( +<,), )
, . , + *,
, + ,
*. + to* , + (*). ,
, , [ + *.

3, , ,

87

. , , has s u p p o r t s , ;
peter has information,
floor supports table.

:
has( peter, information).
Supports! floor, table).

, , (directive), .
, . has :
:-

600,

xfx,

has).

Prolog,
" h a s " , 500
""; .
"xfx" , , "f",
, "".
, -
. ,
( ). , ,
, , ,
"" - .
, , .
, . ,
1-1200.
, , . .
1. :
xfx xfy yfx
2. :
Ex

fy

3. :
xf

yf

, , "f" , "" "" .


""" , . , , , .
"" "" .
. ,
0; ,
. "" ,
, , "" , .
, . ,
- -
88

I. Prolog

( - ) - , - ( - ) .
"-" yfx.
. 3.7 , .

. 3.7. a - - ,
, " -"
500. "-"
yfx, , b -
"-"

not. not fy,


not not
, n e t fx,
, not not p,
, not.
, :
not( not )
Prolog ,
, ,
. , Prolog. , "" , , 3.1.
, Prolog, not.
, 3.1, , . .
. , , , , (de Morgan), :
-( ) <====> - v -
Prolog :
equivalence( not( and( , )), or ( not( ), n o t [ ) ) ) .
3.1.
:- ( 1200, xfx, [ : - , > ] ] .
:- ( 1200, fx, [ : - , ? - ] ) .
:- ( 1100, xfy, ' ; ')

3. , ,

89

::::::::::-

(
(
opt
opt
op(
op(
op(
opt
op{
op(

1 0 5 0 , x f y , ->) .
1 0 0 0 , x f y , ' , ') 900, fy, [ n e t , ' \ + ' l l .
700, xfx, [ =, \=-, , \ = = , = . . ] ) .
7 0 0 , x f x , [ i s , = : = , = \ = , <, =<, >, >-, @<, <?=<, @>, @>=)).
5 0 0 , y f x , [ -, - ] ) .
400, yfx, [ *, /, / / , rood]).
200, xfx, * * ] .
2 0 0 , x f y , ') .
2 0 0 , fy, -) .

,
,
, , . . :
::::-

(
(
(
(

0,
700,
600,
500,

xfx, < > ) .


xfy, v) .
xfy, > .
fy, -) .

:
-(ASB)

<> -A v -,

, . 3.8.

. 3.8. ~ ( & )

<===> -A v -
.

. ,
.
, - , .
; .
, ,
.
.
, .
, , 1-1200.

90

I. Prolog

. , .
, -, , -, . xfy , , ,
.

3.12. , :
:- ( 300, xfx, plays).
:- ( 200, xfy, and).

:
Terml - jimmy plays football and squash
Term2 = susan plays tennis and basketball and volleyball
Prolog? ?
3.13. ("was", "of", " t h e " ) ,
:
diana was the secretary of the department.
Prolog :
?- Who was the secretary of the department.
Who = diana

?- diana was What.


What - the secretary of the department
3.14. :
t(
t(
t(
t(

0+1,l+C).
x+O+l, x+l+0).
x+i+i, zj ;x+l, xi),

t (

X+l,

Z) .

, " + "
yfx ( ):
) ?- t ( 0 + l , A).
) ?- t ( o+1 + i , ; .
) ?- t( 1+0+1+1+1, ).
) ?- t ( D, 1+1+1+0).
3.15. , , :
member! Element, L i s t ) ,
conc( L i s t l , L i s t 2 , L i s t 3 ) ,
d e l ( Element, L i s t , NewList) ,

...

, :
Element in List,
concatenating Listl and List2 gives List3,
deleting Element from List gives NewList, . . .
" i n " , " c o n c a t e n a t i n g " , "and" ..,
. , .
3. , ,

91

3.4.

. .
+.
-. .
*. .
/. .
**. .
//, .
mod. , .
, ,
. . ,
:
?- X = 1

+ 2.

Prolog ""
X = 1 + 2

X = 3, . 1 + 2 Prolog, + , I 2 . ,
Prolog . i s . is , :
?- X i s 1 + 2 .

:
X = 3

, i s . .
Prolog
. , "/" . "/"
, " / / " ,
a "mod" .
?- X is 5/2,
Y is 5//2,
2 is 5 mod 2.

:
X = 2.5
Y = 2

= 1
is ,
, , . is ,

. {. 3.1) , 92

!. Prolog

. . ,
+ , -, *, / div yfx, ,
; ,
is S - 2 - 1
:
X is (5 - 2) - 1
, Prolog
, s i n ( X}, cos ( ;, a t a n ( X), log ( X) , exp ( X} ..
i s .
,
. ,
, 277 37
10000:
?- 277 * 37 > 10000.
yes
, ">", i s , .
, born,
. , 1980 1990 , :
?- born( , Year),
Year >- 1990,
Year =<

1990.

.
X > Y. X Y.
X < . X Y.
X >= Y. X .
X =< Y. X Y.
X - : - Y. X Y .
X =\- Y. X Y .
, "-" " = : - " ; , -
X = Y X =:= Y. X , X Y ,
X Y. . , X =: = Y ,
- . :
?-1+2-:-2+1.
yes
?- 1 + 2 = 2 + 1 .

? - 1 + = + 2.
= 2
: - 1


. , .
, X Y, , D, .

3. , ,

93

1. X Y , D X.
2. X < Y, D X Y - X.
3. Y < X,
, 2), X Y.
,
. , = 20 Y - 2,
D = 5
.
Prolog, , , :
gcd (

X,

Y,

D)


:
gcd{ X, X, X) .
gcd{ X, YF D) : X < Y,
Yl i s Y - X,
g c d ( X, Yl, D ) .
g c d ( X, Y, D) : Y < X,
gcd( 1, X, D) .

,
:
XI i s X - Y,
gcd [ XI, Y, D)

,
. ; , .
length; List, )
L i s t N . ,
, .
1. , 0.
2. , L i s t = [Head | T a i l ] ;
1 T a i l .
:
length ( [] , 0) .
length( [_ I Tail], H) :length{ Tail, N1),
N is 1 + N1.

length :
?N =

length!

[a,b,[,d],e],

).

, l e n g t h , , . ,
Ml , :
N i s I + HI

is , , .
94

,
length i s . .
lengthl[[1,0) .
l e n g t h l t [_ I T a i l ] ,

N}

:-

lengthl( Tail, N1),


N= 1 + HI.


?-

l e n g t h K | a , b , [c,d] ,e] ,

H) .

:
= l+(l+(l+(l+0))}.
,
, . , l e n g t h , l e n g t h l
, :
lengthl( [_ | T a i l ] , N) : - l +

HI,

lengthl( Tail, HI).

l e n g t h l , .
, :
l e n g t h K [_ | T a i l ] , 1 + )
l e n g t h l { T a i l , N).

:-

.
l e n g t h l
, .
?- l e n g t h l ( [ a , b , c ] , ), Length is N.
Length = 3

, , length
. ,
.

.
i s . ,
+ , - , * , /, div mod, .

.

, <, =< .. .

3.16.
man(

Y,

, , X Y.
3.17.
maxlistf List, Max)

, L i s t .
3.18.
sumlisti List, Sura)

3, , ,

95

, Sum L i s t .
3.19.
ordered! List)
, L i s t , o r d e r e d ( [ 1 , 5 , 6 , 6 , 9 , 1 2 ] } .
3.20.
subsumf Set, SumF SubSet)
, Set , SubSet ,
Subset Sum, :
7- subsumt [1,2,5,3,2], S, Sub).
Sub = [ 1 , 2 , 2 ] ;
Sub = [2,3] ;
Sub - [ 5 ] ;

3.21.
between( HI, N2, X)

Ml N2 X,
N1 =< X =< N2.
3.22. "if", "then", " e l s e " " : = " , :
if X > Y then Z := X e l s e Z := Y

, "if" . "if",
" i f - t h e n - e l s e "
:
if Vail > Val2 then Var := Val3 else Var := Val4
Vail, Val2, Val3 Val4 ( , ), a Var . "if" : Vail
, Val2, Var Val3, Val4. .
?- = 2, Y = 3,
Val2 is 2*X,
Val4 is 4*X,
if > Val2 then Z : = e l s e Z : = Val4,
if Z > 5 then : - 1 e l s e W : - 0.
X = 2
Y = 3

z =

W
1
Vaj.2 = 1

Val4 = S

. ,
, .
Prolog .
,
, : 96

I. Prolog

, , , , .
. .
,
, .
, - ;
,
.
.

is () : <, =< ..
:
, , ;
;
, ;
, ;
;
.

3. , ,

97

...
4.1.
4.2.
4.3.
4.4.
4.5.

98
102
103
107
111

, ,
, .
, , ,
. , Prolog. , ,
.

4 . 1 .


. , Prolog
.
Prolog
. ,
, .
. 4.1 , . : , .
, , .
, , , : , , , . "unemployed" ()
, , . -

, . 4.1, :
family[
p e r s o n ! torn, f o x , d a t e [7,may, I 9 6 0 ) , w o r k s ( b b c , 1 5 2 0 0 ) ) ,
person[ a n n , f o x r d a t e [ 9 , m a y , 1 9 6 1 ) , u n e m p l o y e d ) ,
[ person( p a t , fox, d a t e ( 5 , m a y , 1 9 8 3 ) , unemployed),
person! jim, fox, d a t e ( 5 , m a y , 1983), unemployed) ] ) .

. 4.1.

, , , , .
Prolog ,
. , ,
. Prolog

.
. 4.2. ,
:
family! . (_, armstrong,_,_) , _, _)
;
, . , :
family! _, _, !_,_,_] )
, ,
:
?- family! _, person( Name, Surname, _, _) ,

l_,_,_l_l )

, , .

.
, , ,
. . .

4. :

99

husbandj X) :- % X -
familyfX, _ , _ ) .
wiEe( X) :- % X -
family! _ , X , _ ) .
child! X) :- % X -
family! _, _, Children),
member( X, Children). X Children
exists( Person! :- % ,
husband( Person)
wife( Person)
child( Person).
d a t e o f b i r t h ( person{ _ , _ , Date, _ ) , D a t e ) .
s a l a r y [ person ( __,_,_, works [ _, S) ) , S ). %
s a l a r y 1 person I _r_/_> unemployed), 0 ) . %

, 4.2. : )
; ) , ; ) , . )
,
Name Surname
, ,
.
, , :
?- e x i s t s ( person I NameF Surname, _, _ ) ) .
, 2000 :
?- child(x),
dateofbirtM X, date( _, _, 2000)).
100

I. Prolog

:
1- wif( person[ Name, Surname, _, works! _, _) ) )

, 1973 :
7- exists( person( name, Surname, date[ _, _, Year), unemployed)),
Year < 1973.
, 1960 ,
8000:
1- e x i s t s ! Person),
dateofbirth[ Person, date( _, _, Year)J,
Year < I960,
salasy! Person, Salary),
Salary < 000.

, :
?-

fair.il (

person (

_,

Name,

_ , _ ! , _, [ _ , _ < _ ! _ ] ) -

, , , :
total! List of people. Sum of_their_salaries)
.
total) [] ,0) , %
totaK [Person | L i s t ] , Sum) :salary! Person, S), % S -
total( List, Rest), % Resr -
Sum is S + Rest.
:
?- family( Husband, Wife, Children),
total! [Husband, wife I Children], Income).
, l e n g t h , 3.4. , 2000,
:
7- family ( Husband, [life, Children) ,
total! [Husband, Wife I Children], Income),
length! [Husband, Wife I Children], N) , is N -
Incoroe/ < 2000.

4.1. :
) , ;
) ;
)
;
) ,
15 .
4.2. :
twins{ Childl, Child2)

4. :

101

4.2.
(,
) , , .
. , , ; . ,
, ,
.
Prolog.
.

.
, , . ,
, . 4.1. Prolog.
, .
FoxFamlly = family{ person) ton, fox, _i_) I_I_)
, , . 4,1. , . ( )
. : , , :
selector_relation( Object,

Component_selected)

.
husband! familyt Husband, _, _ ) , Husband).
wife! family! _, wife, _}, Wife).
children) family! _, _, ChildList), ChildList).
,
:
firstchild) Family, First) :children) Family, [First | _] ) .
secondchild! Family, Second} :children) Family, [_, Second I _]>.

, - , :
nthchildl , Family, Child) :children! Family, ChildList),
nth_member( K, ChildList, Child). % n-
. , , . 4.1.
firstname) person (, _,_,_) , } .
surname( petson(_,Surname,_,_), Surname].
born( person(_,_,Date,_), Date).
,
,
. 102

I. Prolog

. , . ,
,
. , , ,
.
, , Fersonl
Person2, . :
fiistname( P e r s o n l , torn), sarname( Personl, fox), % Fersonl -
firstname( Person2, j i m ) , sarname( Person2, f o x ) , % Person2 -
husoand( Family, P e r s o n l ) ,
secondchild{ Family, Petson2)
Personl, Person2 Family :
Personl = pecsoni torn, fox, _, _)
Person2 = person( jim, fox, _, _!
Family = family( person[torn,fox,_,_), _, [_, person!jim,fox) | _]>

. , .
,
.

4.3. n t h c h i l d , :
nth_member( , L i s t , X)
, X -
List.

4.3.

,
Prolog. , ,

, .
,
, . .
, .
, . 4.3.
Si, s2, S3 Si .
( si), ,
. ;
.
. ,
. , , . 4.3, si ,

4. :

103

si, ss. n u l l , .
. ,
, ,
" ", , - .

. 4.3.

, ,
. , ,
, ,
1. ;
2. ;
3. .
,
, . , ,
, .
(.. , ): , "" , , . , , . 4.3, ab aabaab,
abb abba. , , , .
Prolog
,
1. f i n a l , .
2. t r a n s ,
:
trans(Sl, X, S2)
, Si s; , X.
3.
silent(

SI,

SZ)

, s? S2.
104

I. Prolog

, . 4.3, :
final ( S3] .
trans [ SI, a, SI) ,
trans( SI, a, S2] .
trans( SI, b, SI) .
trans{ S2, b, S3) .
trans{ S3, b, S 4 ) .
s i l e n t { S2, S4>.
s i l e n t ) S3, S I ) .

Prolog. , aab [, , ] . , , , .
( ),
( ) .
a c c e p t s ,
, .
,
accepts( State, String)
, , S t a t e , , S t r i n g . a c c e p t s
, .
1. S t a t e , [ ] , S t a t e .
2. S t a t e ,
, S t a t e l ,
S t a t e l , . 4.4, .
3. S t a t e ,
S t a t e S t a t e l , () S t a t e l , . 4.4, .

. 4.4. : )
X; )

Prolog :
accepts! State, [ ]) :- %
final! State).

4. :

105

accepts! State, [X I Rest] ) :- %


trans! State, X, Statel),
accepts( Statel, Rest).
accepts! State, String) :- %
silent! State, Statel),
accepts! Statel, String).

, ,
aaab, :
?- a c c e p t s ! SI,
yes

[a,a,a,b)).

, Prolog
, , .
,
,
ab.
? - a c c e p t s ! S,
S = S1;
S = S3

[,]).

, , 3, si.
?XI
2

XI
2

accepts! SI, [X1,X2,X3]}.


=
=
- ;
=
-
- ;

,
, :
7- String [___] accepts! SI, String).
String = [a,a,b];
String - [b,a,b];
no

,
, , ,
7.
f i n a l , t r a n s s i l e n t .
, . 4.3, -
(, ). . 4.3
:
s i l e n t ! SI, S3)
. ,
, :
?- accepts! SI, [ a ) ) .

si, - .

4.4. , . 4.3, , ?
106

I. Prolog

4.5. a c c e p t s , , , .
, . a c c e p t s . : , .
accepts! State, String, MaxMoves)

4.4.
, , .
, ,
.
?
?
, ,
.
,
?
, . :
timetable) Placel, Place2, ListOfFlights)

ListOf F l i g h t s :
DepartureTime / ArrivalTime / FlightNumber / ListOfoays
"/"
, , .
ListOfDays , a l l d a y s
. t i m e t a b l e , , :
timetable! london, edinburgh,
( 9:40 / 1 0 : 5 0 / 4733 / a l l d a y s ,
19:40 / 2 0 : 5 0 / 4 3 3 3 / [mo, t u , w e , t h , f r , s u ]

]).

( ), " : " .
, .

:
route[ Placel, PI2, Day, Route)
Route ,
.
1. P l a c e l .
2. - 12.
3. , Day.
4. , Route, , t i m e t a b l e .
5. .
4. :

107

, :
From / / FlightNumber / Departure_time
,
.
1 ) f l i g h t ( P l a c e l , Place2, Day, FlightNum, DepTime, ArrTime)
, FlightNum,
P l a c e l 12
Day, .
2) deptime( Route, Time)
Route Time.
3 ) t r a n s f e r ! Timel, Time2}
Timel Time2 40 , .
, .
.
.

.
t r a n s i t i o n
timetable.

; .
, r o u t e a c c e p t s , ,
.
.
1. .
P l a c e l ?12, :
route( Placel, PI2, Day, [ Placel / PI2 / Fnum / Dep ]I :flight( Placel, Place2, Day, Fnum, Dep, An).
2. .
1 F2 , 1
, 2. ,
.
r o u t e t PI, P2, Day, [PI / / Fnuml / Depl | RestRoute)) :r o u t e ( , 2, Day, RestRoute),
f l i g h t f PI, P3, Day, Fnuml, Depl, Arrl),
deptime( RestRoute, Dep2),

transfer! Arrl, Dep2).


f 1 i ght, t r a n s f e r deptime
, ,
4.1. ,
.

108

I. Prolog

4.1. , ,


:- ( 50, xfy, : ) .
s route[ Placel, Place2, Day, Route):
I Route - , s Placel
12, Day
route! PI, 2, Day, [ PI / 2 / Fnum / Deptime ] )
flight( PI, P2, Day, Fnum, Deptime, _ ) .

:-

I
route( PI, P2, Day, [ (PI / P3 / Fnuml / Depl) I RestRoute] ) :route! , P2, Day, RestRoute),
flight! PI, P3, Day, Fnuml, Depl, Arrl),
deptime) RestRoute, Dep2) r
i
transfer! Arrl, Dep2).
%
flight) Placel, Place2, Day, Fnum, Deptirae, Arrtiine) :timetable{ Placel, Place2, Flightlist),
member! Deptime / Arrtiine / Fnum / Daylist , Flightlist),
flyday! Day, Daylist).
flyday( Day, Daylist) :member) Day, Daylist).
flyday) Day, alldays)

:-

member) Day, [mo,tu,we,th,fr,sa,su] ).


deptime) [ PI / P2 / Fnum / Dep I _ ) , Dep).
t r a n s f e r ) H o u r s l : M i n s l , Hours2:Mins2)
:60 * (Hours2 - Hoursl) + Mins2 - Minsl >- 40.
member) X, [X | L] ).
member! X, [Y \ L] )
member( X, L ) .

:-

%
timetable! edinburgh, london,
[ 3:40 / 10:50 / ba4733 / alldays,
13:40 / 14:50 / ba4773 / alldays,
19:40 / 20:50 / ba4833 / [mo,tu,we,th,fr,su] ] ).
timetable( london,
[ 9:40
11:40
1B:4O

edinburgh,
/ 10:50 / 4732 / alldays,
/ 12:50 / ba4752 / alldays,
/ 19:50 / ba4822 / [mo,tu,we,th,fr] ] ).

timetable) london, ljubljana,


[ 13:20 / 16:20 / jp212 / [mo,tu,we,fr,su],
16:30 / 19:30 / 473 / [mo,we,th,sa] ] ).
timetable( london, Zurich,
[ 9:10 / 11:45 / ba614 / alldays,
14:45 / 17:20 / srB05 / alldays ] ).
timetable( london, milan,
[ 8:30 / 11:20 / baElO / alldays,
11:00 / 13:50 / az459 / alldays ] ).

4. :

109

timetable! ljubljana, Zurich,


[ 11:30 / 12:40 / jp322 / [tu,th] ! ).
timetable) ljubljana, london,
[ 11:10 / 12:20 / jp211 / Jrao,tu,we,fr,su],
20:30 / 21:30 / ba4T2 / [mo,we,th,sa] ] >.
timetable( milan, london,
[ 9:10 / 10:00 / az45B / alldays,
12:20 / 13:10 / ba511 / alldays ] ).
timetable! milan, Zurich,
[ 9:25 / 10:15 / sr621 / alldays,
12:45 / 13:35 / sr623 / alldays ] ).
timetable! zurich, ljubljana,
[ 13:30 / 14:40 / jp323 / (tu,th) ] ).
timetable[ Zurich, london,
[ 9:00 / 5:40 / ba613 / [mo,tu,we,th,fr,sa],
16:10 / 16:55 / sr606 / [mo, tu,we, th, t r, su] ] ).
timetable( Zurich, milan,
[ 7:55 / 8:45 / sr620 / alldays ] ).
query3 (Cityl, City2,City3,FNl,FN2,FN3,FK4) :permutation! [milan,ljubljana, Zurich] , [Cityl,City2,City3]),
flight! london, Cityl, tu, FN1, Depl, Arrl},
flight( Cityl, City2, we, FN2, Dep2, Arr2},
flight! City2, City3, th, FN3, Dep3, Arr3),
flight! City3, london, ft, FN4, Dep4, Arr4) .
cone! [), L, L) ,
conc([X|LI],L2, [XJL3J} :conc(Ll,L2,L3) .
permutation[ [] , []),
permutation! L, [X | P ] |
del( X, L, LI),
permutation! LI, P) .

:-

del( X, [X|L], LJ .
d e l ! X, [ Y | L ] , [Y|L1])
d e l ! X, L, LI] .

:-

, , , . , .
,
.
,
?
1- f l i g h t ( l j u b l j a n a , london, Day, _, DeptHour:_, _ ) , DeptHout >= 16.

Day = mo;
Day - we;

?
?- route! ljubljana, edinburgh, th, R).
R - [ ljubljana / zurich / jp322 / 11:30, Zurich / london / sr806 /
110

I. Prolog

16:10,
london / edinburgh / ba4822 / 18:40]

, ,
.
,
?
.
p e r m u t a t i o n , 3. , , ,
:
?- permutation! [milan, ljubljana, Zurich], [Cityl, City2, City3]),
flight! london, Cityl, tu, FN1, _, _i,
f l i g h t ! Cityl, City2, we, FK2, _, _ ) ,
f l i g h t ! City2, City3, th, FN3, _, _ ) ,
f l i g h t ! City3, london, fr, FN4, _, _ ) ,
Cityl = milan
City2 = Zurich
City3 = ljubljana
FN1 = baSlO
FH2 - sr621
FN3 - jp323
FN4 = jp211

, , , , , , , :
?- r o u t e ( moscow, edinburgh, mof R).

,
. cone:
?- conc( R, _, [___#_]). route{ moscow, edinburgh, mo, R).
no

cene R 4,
.

4.5.
,
, .
:
solution[ Pos)
, Pos
, . ,
. ,
.

4.5.1. 1
, . ,
, . ,
. ,
4. :

111

(X Y), 1
8. :
X/Y

, , "/" ,
. . 4.5
.

. 4..
.


[1/4, 2/2, 3/7, 4/3. 5/6,
6/8, 7/5, 8/1]
,
[ Xl/YL, X2/Y2, X3/Y3

8/8]

, .
s o l u t i o n
XI, Y1, 2, Y2
8, Y8. , ,

, , ,
. , X,
, :
[ 1/U, 2/Y2, 3/Y3,

. . , B/YB]

8x8. . , , , .
.

. , ( ) 8,
, . s o l u t i o n
, .
1. . , ,
,
.
2. . :
[ X/Y I O t h e r s ] .

112

I. Prolog

2 X/Y, , Others. , .
1. , Others,
; , Others
.
2. X Y 1 8.
3. , X/Y,
Others.
, , s o l u t i o n . : Y 1 8, ..
[ 1 , 2 , 3 , 4 , 5 , , 7, ]. ,
X, , X . , X 1 8. , n o a t t a c k . ,
Prolog :
solution! [X/Y | Others]) :solution( Others),
memberf Y, [1,2,3,4,5,5,7,3] ),
noattackf X/Y, Others).
n o a t t a c k :
noattack; Q, Qliat)
, ,
.
1. Q l i s t , , , ,
, .
2. Q l i s t , [Ql | Q l i s t l ]
:
) , Q, ,
Q1;
) , Q,
Q l i s t l .
, , ,
, ,
: ,
.
, ,
:
;
( ) ; , , , X Y.
4.2. . , . :
?- template! S), solution! S).
4. :

113

:
S = [ 1/4, 2/2, 3/7, 4/3, 5/6, 6/8, 7/5, 8/1];
S = [ 1/5, 2/2, 3/4, 4/7, 5/3, 6/8, 7/6, 8/1];
S = [ 1/3, 2/5, 3/2, 4/8, 5/6, 6/4, 7/7, 8/13;

4.2, 1
% solution! BcardPosition} ,
%
BoardPosition -
solution [ tj ) .
solution) (X/Y | Others] ) :solution! Others),
member; Y, [1,2,3,4,5,6,7,8) >,
noattack{ X/Y, Others}.

noattack( _ , [ ] ) .

noattackt X/Y, [Xl/Yl | Others] )


Y \ Y1 F
Yl-Y -\- Xl-X,
Yl-Y =4- X-Xl,
noattack( X/Y, Others).

% X/Y,% Others -

: Y
l

member! Item, [Item | Rest] ).


member[ Iterc, [First | Rest] )
member( Item, Rest).

:-

%
template( [1/Y1,2/Y2,3/Y3,4/Y4,5/Y5,6/Y6,7/Y7,S/YS] ).

4.6, , 4.2, Y . ?
? , ,

4.5.2, 2
1
:
[ l/Yl, 2/Y2, 3/Y3, ..., 8/YS]

.
X , - . , Y , :
[

Yl,

Y2,

Y3,

...,

Y8)


. Y. 114

I. Prolog

1, 2, ..., 8. . :
[1,2,3,4,5, 6,1,8]

, S, ,
. :
solution ( S) : permutation (

[1,2,3,4,5,6,7,8],

S),

safe! S) .
p e r m u t a t i o n 3, safe.
.
1. S . , , , - .
2. S [Queen I O t h e r s ] .
, Others Queen - Others.
Prolog1 :
safe ([ ]) .
safe( [Queen | Others]] : safe[ Others),

noattack( Queen, Others) .


n o a t t a c k .
, Y,
X . n o a t t a c k , . 4,6. :
noattack( Queen, Others)
, Queen
Others, X Queen Others 1. X Queen Others. n o a t t a c k :
rioattackl Queen, Others, Xdist)
n o a t t a c k safe :
noattack( Queen, Others, 1)

. 4.6. noattack: ) X
Queen Others 1; ) X Queen
Others 3

4. :

115

n o a t t a c k , Others: Others
, , , , , ; Others , Queen Others (
Xdist Queen), , Others,
Xdist + 1. , 4.3.
4.3. 2
* solution( Queens),
%
Queens - Y
solution[ Queens) :permutation( [1,2,3,4,5,6,7,8], Queens),
safe( Queens).
permutation[ (], [] ).
permutation[ [Head | Tail], PermList) :permutation; Tail, PermTail),
dell Head, PermList, PermTail).
% Head Tail,
%
I del( Item, List, NewList) - NewList
%
Item List
del[ Item, [Item | List], List).
dell Item, [First | List], [First | Listl] )
del( Item, List, ListlJ .

:-

* safe( Queens),
%
Queens - Y
safe(11).
safe( [Queen | Others] ) :safe( Others),
noattack( Queen, Others, 1 ) .
noattack( _, [] , _) .
noattackl Y, [Yl I Ylist], Xdist)
Yl-Y -\- Xdist,
Y-Yl -\- Xdist,
Distl is Xdist + 1,
noattack( Y, Ylist, Distl).

:-

4.5.3. 3

. , ..
, , . , , , , , .
, , .

116

I. Prolog

. .
. .
. .
v. .
: , - v
( . 4.7).
:
= -
V = X +

. 4.7, ,
,
. ,
, , : 2, = 4, = 2 - 4 = -2. v = 2 + 4 = 6

.
DX Dy =
Du Dv =

[1,2,3,4,5,6,7,8]
[1,2,3,4,5,6,7,8]
[-7, - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

:
( X, Y, U, V) {X Dx, Dy ..),
. , X Y U
V . , , : , , ,
. , , 4.4.
4. :

117

Y. :
sol( Ylist, Dx, , DM, DV)
Y ( Y l i s t ) ,
,
Dx. Y U V
Dy, Du Dv. , s o l u t i o n ,
:
?- solution(S) .
s o l
, .
4.4. 3
% solution( Ylist),
%
Ylist - Y
solution! Tflist) :soil Ylist,
% Y
[1 , 2,3,4,5,6,1, 8],
* X
[1,2,3,4,5,6,7,8],
% Y
[-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7],

[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] ). %
SO1 ( 13 , [] , Dy, Du, Dv) .
soil [Y I Ylist], [X | Dxl), Dy, Du, Dv) :d e l ( Y, Dy, Dyl),
% Y
U is X-Y,
%
d e l ( U, Du, Dull,
%
V is X+Y,
&
d e l [ V, Dv, Dvl),
%
Y l i s t , Dxl, Dyl, Dul, D v l ) . %
d e l [ Item,

[Item [ L i s t ] , L i s t ) .

d e l Item, [ F i r s t I L i s t ] , [ F i r s t I L i s t l ] )
d e l ( Item, L i s t , L i s t l ) .

:-

s o l , N ( NXN).
Dx, Dy ..
.

gen( N1, N2, L i s t )
, HI N2, :
List =

[ N 1 , N1 + 1,

N1 + 2,

...,

2 - 1,

2 j

.
gen( , N, [ N ] ) .
g e n ( N1, N2, (N1 I L i s t ] } :N1 < N2, i s N1 + 1,
gen{ M, N2, L i s t ) .
, s o l u t i o n ,
, :
solution! N, S)
118

I. Prolog

N S , Y
N . s o l u t i o n :
s c l u t i o n ( N, S) igen( I, N, Dxy) , % Dxy - X Y
Nul i s 1 - N, Hu2 i s H - 1,
gen (Nub Nu2, Du! ,
Nv2 i s + K,
gen ; 2, Kv2, Dv},
sol{ 5, Dxy, Dxy, Du, Dv).

, 12 :
?- s o l u t i o n ( 12, S ) .
3 - [1,3,5,8,10,12,6,11,2,7,9,4]

4.5.4.
,
. ,
. , .
, ,
.
. ,
. , .
,
.
, . 2
,
. , 2, , ,

, . 3 , ,
, , .

4.7. , X/Y, X Y 1-8.


) iunp( Squarel, Square2),
. , Squarel
, a Square2
, , :
?- jump{ 1/1, 5) .
S = 3/2;
5 = 2/3;

4. ;

119

6} knightpatii ( P a t h ) , Path ,

.
) k n i g h t p a t h , , 2/1 (Y = 8), 5/4
.

, ,

Prolog.
Prolog.
Prolog

. ,
.
,
.
Prolog
.
, ,

Prolog.
,
, . .
.
.
, .

120

I. Prolog

...
5.1.
5.2.
5.3.
5.4.

121
125
129
132

, ,
,
, , . ,
Prolog
,
.

5.1.
Prolog , . ,
.
, .
. Prolog .
,
, , .
, . 5.1.
X Y .
1. X < 3, Y = 0.
2. 3 =< X X < , Y = 2.
3. =< X, Y = 4.
Prolog
f( ,

Y)

:
f( X, ) :- X < 3,
% 1
f( X, 2) :- 3 =< X, X < 6. I 2
f( X, 4} :- 6 =< X.
% 3

. 5.1.

, , , f ( X, Y) X , .
. ,
.

5.1.1. 1
, :
?-

i(

1,

) ,

< Y.

, ( 1, Y), Y 0. :
2 < 0

, . , , , ,
Prolog . . 5.2.

. 5.2. ,
, " ",
, , 2
3,

122

I. Prolog

, f,
,
. Prolog, , , , ,
, . , . 5.2, , 1 , " ". Prolog,
, .
. (!)
. , ,
f ( X

0)

:-

f ( X,

2)

: - 3 =< X,

( X,

4)

<

! .

<

6,

! .

: - 6 =< X.

! ,
. :
?- f; 1,

Y;,

2 < .

Prolog , . 5.2. 2 < 0. Prolog , , !, , 2 3,


.
, ,
, , .
, , .
, . ,
; , , .
.
, .

5.1.2. 2
, . , :
?- f; 7, Y) .
Y -

, .
, . ,
.
1. 7 < 3 ;
2 ( ).
2. 3 =< 7 , 7 < 6; 3
( ).
3. 6 =< 7 .
5.

123

. , X < 3
( 7 < 3 ). 3 =< X ( 3 =< 1 ). ,
, .
,
.
6 =< X 3.
.
X < 3, Y = ,
, : 6, Y
Y = 4.

.,

,
. :
f ( X,
f ( X,
f ( X,

0) : - X < 3 ,
2) : - X < 6,
4) .

! .
I.

, ,
.
, ?
:
f( , 0) :- < .
( X,
f ( X,

2) : - X < 6 .
4).

, ,
, , , ,
:
?- f
=
Y =
Y =

( 1,
0;
2;
4;

YI .

, , ,
, .
.
, , .
, ,
, , , , .
.
, ,
:
: - 1,

2,

. . . , Bin,

!,

. . . , .

, G, .
G .
,
1, .... .. () 1, -..,
.

124

I. Prolog

, G : G - .
:
: - , Q, R,
: - V.
: - , , D.
?- .

!,

5,

V.

, , , D, .. .
, . 5.3. ?, Q, R, , , Q, R . , ,
:- v.
. S, ,
. , ,
:
: - ,

D.

. 5.3.
.
, ,
, - . R S
" "

. , "" . ,
3, , D ,
, .

5.2.
5.2.1.
:
I X ,

Y,

= X, X Y, Y, X . :
( X,
( X,

,
Y,

X)
Y)

: - X >= Y.
: - X < Y.

5.

125

.
, .
, . , "".
X >- Y, - X,
= Y.
Prolog
:
( X,
( Xr

Y,
Y,

X) : - X >= Y,
Y) .

!.

,
. , max ( X, Y, )
.
.
?- ( 3, 1, 1) .
yes
max:
m a x [ X, Y, M a x ) : X >= Y, ! , Max - X
Max = Y.

5.2.2. ,

, X L,
:
raerabe ( X, L >

:
member( X, [X | L] }.
membert x, [Y ! L] ! - memberl X,

L) .

:
X, X, . member
, . ;
X,
. .
member(X,
m e m b e r ! X,

[X
[Y

|
|

L))
L])

: - !.
:- m e m b e r [

X,

L).

, :
?- m e m b e r <
X = ;

X,

[ , , ] ] .

5.2.3.
X L ,
X , X
L. X L, L 126

!. Prolog

, . add :
add! X, L, L1)

X , L , X,
L1 , .
:
X L, LI = L,
L1 L X.

X L , X
L1. :
add{ X, L, L) :- m e m b e r ! X,
a d d { X, L , [X | L j ) -

L),

!.

;
?- a d d ( a , [ b , c ] , L) .
L [,,]
;- adci< , [to,] , L) .
L = [ b , c ]
X=b
? - add{ a , [ b , c , X ] , L ) .
L = [b,c,a]
X=a

.,
add( X, LI, L2)
L2. , , add ( , [ ] , [ , ] ) .
, " ",
- ,
. ,
, add , , .
1~ { a, [a,b,c] , L> .
L = [,,] ;
L = [a,a,b,]

, . ,

5.2.4.
, ,
. - ( ), , . :
beat[ torn, j i m ) .
beat[ ann, torn).
beat[ p a t , j i m ) .


class( Player, Category)

. , .
5.

127

winner. , .
f i g h t e r . , ,
.
sportsman. - ,
.
, ,
, , .
:
X ,
Y, , X Y,
2, , Z X.
:
X ,
X Y
X .
"", Prolog, .
winner .
sportsman.
, winner
f i g h t e r " ". .
X - X - ,
X - ,
, X - ,
X - ,
, X - ,
X - .
Prolog.
:
class( X, fighter) :beat[ X, _ ) ,
beat 1 _, X), I .
class ( X, winner) :beat[ X, _ ) , ! .
class( X, sportsman) ; beat[ _, X ) .

,
sportsman . . ,
.
?- c l a s s ( torn. ).
f i g h t e r ; %

?- classi torn, sportsman),


yes %

c l a s s , . .

128

I. Prolog


5.1. :
(2) : (3) .

! .

Prolog .
) ?- () .
) ? - ( X], ( Y) .
) ?- ( X), !, ( Y).
5.2. : , .
class! Number, positive) :- Number > 0.
class( 0, zero) .
class( Number, negative) :- Numbe < 0.
.
5.3.
s p l i t ( Numbers, Positives, Negatives)
; ( )
, , .
s p l i t ( [3,-1,0,5, -2] , [3,0,5;, [-1,-2] )
: , .

5.3.
, Prolog '' , ". : X, X . Prolog
:
likes! , X} :- animal! X).
. ,
, .
X - , ' X' .
, X - , X.
" " Prolog
, f a i l ,
,
. Prolog f a i l :
l i k e s [ , X) : snake( X) , !, f a i l .
l i k e s ( , X) : animal( X).

: X ,
( ), f a i l .
:
l i k e s ! , X) :snake i X) , !, f a i l
animal( X!.

129


different! X, Y)

, X Y . , "" , :
X Y ;
X Y ;
X Y .
, X Y ,
.
Prolog .
X Y , ^ different( X, Y) ,
d i f f e r e n t ! X, Y) .
f a i l :
d i f f e r e n t ; X, X)
d i f f e r e n t ( X , Y) .

:-

!,

fail.

, :
d i f f e r e n t ; X, Y)
= Y, !, f a i l

:-

t r u e .
, not, ,
not{ Goal)

, Goal .
n e t , .
Goal , not{ Goal) ,
not( Goal! .
Prolog :
not! P) :, !, f a i l
tree .
, not Prolog,
. ,
::,
not(

snake(X)}


not snake{ X)

Prolog . not . , not Goal \+Goal. Prolog


: not, ( ), . , - , n e t
. .

130

I. Prolog

not . r.oz, :
likes! , X) :animal( X),
not snake ( X) .

different* x, Y)
not ! X = Y) .

:-

,
. .
, not ,

:
class( X, fighter! : beat< X, ],
b e a t { _ , .

class( X, winner) :beat! x, _i,


not beati _, X) ,
class( X, sportsman) :beat( _, X),
not beat ( X, _) .
not
1 (. 4.2). rio_attack . a t t a c k . , ,
5.1.
5,1,
s o l u t i o n ; {]} .
s o l u t i o n ; [X/Y I Others]) :solution! Others),
member! Y, [ 1 ( 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ) , % member
not s t a c k s ! X/Y, O t h e r s ) .
attacks! X/Y, Others) :member! Xl/Yl, Others),
( Yl - Y;
VI is Y + XI - X;
Yl is Y - XI + X ) .

5.4. , , Candidates RuledOut, , . ( member


not),
Candidates, RuledOut.
5.5.
set_difference[ Setl,

Set2,

EetDifference)

, :
set_diffe-ence |

[a,b,c,d] ,

[b,d,e,f] ,

5.

[, ] )

131

5.6.
unifiable[ Listl, Term, List2)

L i s t 2 L i s t l ,
Term, , :
?- unifiable( [X, b, t[Y)], t ( a ) , List)
List - [., t (Y) ]
, X , , t ( a )
. : not ( Terml = Term2);
Terml = Terrc.2 , not ( Terml =
Term2) !

5.4. ,

,
. . ; , .
1. . , Prolog, , .
2. , ,
:
, Q,
R.
, Prolog.
, ,
,
. ,
, . ,
17. . , -

,
:
: - -s, b.
: - .

: ,
b . :
<==> [ b) v

,
.
:
:- , !, .
:-

132

I. Prolog

:
<==> ( 6 v (- & )

:
:- .
:- , 1, .
:
<--> v (a & b)
, , . , .
, , ,

. , .
,
. "", .
.
, , ,
.
,
,
.
, f a i l .
, (not) .
, , , .
not " f a i l " ( ), , "
fail".
, not , .
, , not, , . , Prolog
:
?- not human[ ) .

, , "yes". ,
Prolog , . Prolog , , ,
. ,
not Prolog .
, , Prolog , not.
. , ,

. , - ( ), , , , .
Prolog , -

5,

133

, .
human( ) ,

, ,
!
, , not , , :
good_tandard(

jeanluis).

expensive[ j eanluis).

good_standard( francesco).
reasonable! Restaurant) ;- %
not expensive( Restaurant}.
% - !

?- good_standard( X), reasonable! X).
, , Prolog
X = francesco
, ,
?- reasonable ( X) , good_5tanclard ( xi .
Prolog

, .
, reasonable ( X)
, .
: not Goal , Goal
not Goal.
, .
, ,
.
Prolog
?- expensive! X).
: " X, expensive { X) ? , X?" X . Prolog , X = j e a n l u i s .
?- not expensive( X).

: " X, not
expensive (X! ?" ,
X = f r a n c e s c o . Prolog "",
. nor. e x p e n s i v e ! X)
:
not[ X, , expensive(X))
:
X expensive!!

134

I. Prolog

,
, not. , . . ,
, ,
Prolog, .

. , -, , -,
.
, Prolog
( ),
, ,
.
, :
, 1, 12

; not Goal
Goal.

, t r u e f a i l ; ,
.
, , , .
.
not . not .


onepa [160]. [S7] Prolog, , .

5.

135

...
6.1.
6.2.
6.3.
6.4.
6.5.

136
139
143
144
146

,
. . , ,
.

6 . 1 .
,
,
. . ,
.
:
, ,
;
;
,
.
,
, Prolog. ,
Prolog. Prolog . Prolog , . , , ,
,
..

,
, ,
.
, 6.1 , Prolog
. ,
, ,
,
. , ,
. , , , .
, , ( ) u s e r .
.

, 6,1. Prolog

Prolog ""
: , . , , . .
, Filename, :
see ! Filename)
( - ,
Filenanie) , Filename.
see , f i l e l , a
,
see( f i l e l ) ,
read_froui_file(
see( user),

Information),

, :
tell(

Filename)


f i l e 3 , .

6.

137

t e l l ( file3),
write_on_file(
t e l l { "user) ,

I71formati.cn),

seen
,
told

.
, , Prolog
. , . . , , .
.
, , , end_of_f i l e .
; .
.
,
.. , . ,
. , .
, .
Prolog , .
, .
.
, get, getO put.
, .
,
Prolog. -
, , . read, w r i t e . , , .
,
. , , . . ,
, .
, , .
,
.

138

I. Prolog

6.2. ,
6.2.1. read write
read
,
ieadl X)

, , X. X , X , , read( X) .
read , .
( ).
read( X; ,
end_of_file.
w r i t e ,
write! X!

X . X , Prolog . Prolog , w r i t e ""


, , .
, Prolog ,
. ,
tab[ )

N , ] ( ) .
.
, ,
:
cube{ N, ) : i s H * * N.

, . .
?-

cube(2,X).

X =

?- cube ( 5, Y) .
Y - 125
?- cube(12,Z) .
Z = 1728

, ,
cube . ,
stop:
cube : read! X),
process( xj .
process( stop! :- !.
process( N) : 6.

139

i s N * N * ,
w r i t e [ ),
cube.

, . : cube, X,
; X = s t o p , , X cube . :
?- cube.
2.

5.
125
12.
1728
stop.
yes

2, 5 12; . , ,
, , .
,
cube. :
cube : read[ stop), !.
cube :read( N ) ,
is N * N * N,
write ( C ) ,
cube.

, ,
, , 5.
reacH stop)
. read .
, , s t o p resd ( N),
.
cube . ,
, , , , . . cube
, , .
cube :write( 'Next item, please: ' ) ,
:ead( X ) ,
process ! .) .

processl stop) ;- !.
process[ N) : is N ' N,
write ( 'Cube of ' ) , write( N ) , write!' is ' ) ,
write( C ) , nl,
cube.

cube , , :
140

I. Prolog

?- cube.
(text item, please: 5.
Cube of 5 is 125
Next item, please; 12.
Cube of 12 is 1728
Next item, please: stop.
yes
Prolog, (, t t y f l u s h ) ,
.
,
.

6.2.2.
Prolog ,
, . ,
writelist( LI

L , L
:
writelist((]).
writelist[ [X | LJ) :write( X), nl,
w r i t e l i s t ( L).
,
. w r i t e l i s t 2 . .
?- writelist2(
a b
d e f
g h i

[a,b,c],

[d,e,f],

[g,h,i]

]).

:
writelist2([)).

writelist2( [L | LL]} :doline( L) , nl,


writelist2( LL).

doline([]).
doline( [X I L]> :write! X), tab( 1),
doline( L).
, , . , b a r s , , ,
. b a r s .
?- b a r s (

[3,4,6,5]

) .

b=r= :
bars! []J.
bars[(N| L]) :stars [ N ) , nl,
bars( L ) .

6.

141

stars I M) : > ,
write) * ) ,
HI is W - 1,
stars) N1).
stars( N] :N =< 0.

6.2.3. ,
F :
. . .,

see(

process file,

seef

user},

...

p r o c e s s f i l e , F ,
. p r o c e s s f i l e .
processfile :read! Term),
% , , Term -
process( Term).
process( end_of_iile) :- !. %
process{ Term) :t r e a t ( Term!,
%
processfile.
%
t r e a t ( Term) , .
. s h o w f i l e . .
showfilet N] :read! Term) ,
show! Term, .
show) end_of_file, _) :- ! .
show( Term, M) :w r i t e ( N) , t a b [ 2 ) , w r i t e ( Term) , n l ,
N1 i s N + 1,

showfile(Nl).

6.1. , f , .
findterraf Term)
f,
Term.
6.2. , f , .
findallterms< Term)
f,
Term. , Term
( , ).

142

!. Prolog

6.3.
:
putt CJ
ASCII ( 127) . ,
?-

pit (

65},

put(

66;,

putt

67)

:
ABC

65 ASCII "h", 66 "" 67 "".


getO( ]


ASCII . getO g e t , (, , ..).
get! )

( , )
. , , ASCII.
, ,
squeeze, : , , . , ,
s q u e e z e , , , .

The
robot t r i e d
to pour wine out
of the
bottle.
squeeze :
The robot t r i e d to pour wine out of the b o t t l e .
.squeeze , , .
, , . , : , .
, .
squeeze :getCM ),
putt ,
dorest ( ) .

dorest I 46) :- !.
dorest( 32) :- !,
gett ) ,
putt ) ,
dorest( ) .
dorest( Letter) :squeeze.

6.

% 46 - ASCII;
% 32 - ASCII
%

143


6.3. squeeze, . ,
, .

6.4.

, .
name,
ASCII.
name[ A, L)
, L ASCII
. ,
name( zx232, [122,120,50,51,50])
.
name.
1. , .
2. , .
,
, . , :
oirdecl, order 2, driver I, driver 2, t a x i a l , taxiluK

taxi( xj
, X :
taKi{ X)

:-

name( , x i i s t ) ,
name( t a x i , T l i s t ) ,
conc( T l i s t , _, X l i s t ) .

% ' t a x i ' X7

o r d e r d r i v e r .

.
getsenterce( Mordlist)

Wordlist
. ,
, :
Prolog,
. ,
Mary was pleased to see the robot f a i l .
g e t s e n t e n c e [Sentence) :
Sentence = [ '', was, pleased, to, see, the, robot, fail]
, .
6.1. g e t s e n t e n c e
Char,
get r e s t . get r e s t
.
144

I, Prolog

1. Cher . .
2. Char .
g e t s e n t e n c e .
3. Char . Word, Char, g e t s e n t e n c e
Wordlist. [Word | W o r d l i s t ] .
6.1.
I*

getsentence
. ,
getsentence( Wordlist)

Wordlist = [ 'Mary1, was, pleased, to, see, the, robot, fail]

Mary was pleased to see the robot f a i l .


V
getsentence( Wordlist)
:getO1 Char],
getrest( Char, Wordlist).
getrestl 46, [] )

:-

!.

% ; 46 - ASCII ' . '

getrest! 32, Wordlist)


;getsentence; Wordlist).

!,

* 32 - ASCII
%

getrest( Letter, (Word | Wordlist] ) :getletters< Letter, Letters, Nextchar), %


name! Word, L e t t e r s ! ,
getrest( Nextchar, Wordlist).
getletters( 46, [ ] , 46)

:-

!.

% ; 46 -

g e t l e t t e r s [ 32, [], 32)

:-

!.

* ; 32 -

g e t l e t t e r s [ Let, [Let | L e t t e r s ] , Nextchar)


getO ( Char),
g e t l e t t e r s ( Char, Letters, Nextchar).

:-

, , :
getletters{ Letter, Letters, Nextchar)

.
1. L e t t e r ( ) .
2. L e t t e r s ( L e t t e r ) .
3. Next char , . Nextchar .
, g e t s e n t e n c e .
. , ,
, Prolog.
6.

145

.
, , , .
21.

6.4.
s t a r t s [ Atom, Character)

, Atom C h a r a c t e r .
6.5. p l u r a l ,
, ,
:
7- plural( table, X)
X - tables
6,0.
search[ Keyword, Sentence!
, Keyword. Sentence
, .. ( g e t s e n t e n c e , ).

6.5.
Prolog , , , . ,
, Prolog.
, Prolog.
Prolog , ?, c o n s u l t (F) , , :
?- consult) }

, , , Prolog. ,
programs .
Prolog . . ,
, . Prolog
. , , , , ,
, .

146

I. Prolog

c o n s u l t
, , :
?- consult ( [ .3, prcgram4, queens)).
:
1- [ programs, prcgram4, queens].
, ,
Prolog (
). Prolog , . ,
5-10 . compile, , :
?- compile! ).

?- compile! [ program4, queens, ]).


, , Prolog. ,
.
,
Prolog.
Prolog .

( ,
) .
,
Prolog.
,
, . u s e r .

.
see ( F i l e ) . F i l e .
t e l l ( F i l e ) . F i l e .
seen. ,
t o l d . .
:
;
.

.
read ( Term}. .
,

147

w r i t e ( Term). Term.
put ( CharCode). ASCII,
getCH CharCode) . .
get ( CharCode) . .
, .
nl. .
tab ( ). N .
name ( Atom, CodeList) . CodeList ASCII
Atom.
Prolog , , , , ..

, Prolog
, , ISO
Prolog [43] ,
Prolong. ,
. , : see (Filename) , t e l l [Filename} , get(Code),
put (Code), name (Atom, CodeList), : set_input(Filename), set_outpu.t (Filename), get code(Code),
put_code(Code), atorc_codes(Atom, CodeList),

148

I. Prolog

...
7.1.
7.2. : =.., functor, arg name
7.3.
7.4.
7.5.
7.6. bagof, setof findall

149
156
159
161
164
165

,
Prolog. ,
. ; , , ,
.. Prolog; .

Prolog. , ,
Prolog,
.

7.1.
7.1.1. var, nonvar, atom, integer, float, number,
atomic, compound
: , , ..
,
. ,
, , .. , . ,
, X Y, :
Z is X + Y

, X Y
. , Y , .
number.
number (X) , X , . , X " " . , , X Y, ( )
X Y:
...,

numberj

.),

Yj ,

is

+ Y,

...

X, Y ,
. number
"" 2 is + V .
var, nonvar, atom, i n t e g e r ,
f l o a t , number, atomic, compound. .
var{ X). , X .
nonvar ( X). , X X
.
atom! X). , X .
i n t e g e r ( X). , X .
f l o a t [ X). , .
number ( X). , X ,
atomic ( X). , X .
compound ( X). , X ().
Prolog .
?- var tZ), Z = 2.
Z = 2
?- Z = 2, var( Z).
no
1- integer( Z), S = 2.
no
?- Z = 2, integer [ Z) , nonvar( Z ) ,

z - >

?- atom( 3.14) .
no
?- atomic; 3.14) .
yes
?- atom( --> }.
yes
?- atom! p(l)).
no
?- compound ( 2 + X)
yes

150

I. Prolog

atom . , , . :
c o u n t ! A, L, }

, L , N . count :
c o u n t [ _ , [] , 0) .
c o u n t [ , [ A I L ] , ) : - ! ,
c o u n t ( A, L, N 1 ) , % Ml
N i s N1 + 1.
c o u n t [ A , L 1 L ] , N) :c o u n t ( A, L, ] .

:
?- c o u n t ( ,
N = 3
?- count ( a,
- 3

[,,,],
[a,b,X,Y],

Nat.

?- count(
Mb - 3

[,,,Y],

).

?- L =
Na = 3
Nb = 1

[,

X,

],

] .

count(

a,

L,

Na),

count(

L,

).

=
-

X, Y , , a, Nb = 1,
.
, ,
. count ,
. .
count(_, 1] , 0] .
count ( , [ | L] , N1 : atom! ), = , !, % ?
count [ A, L, N1) ,
% -
R i s Mi 4 1
count( A, L, N).
%
, nonvar.

7.1.2.
nonvar
.
DONALD
+ GERALD
ROBERT

D, , N .. , . ,
7.

151

, ,
.
:
sum( HI, 2,
1, N2 N . sum{ N1,
N2, ) , , N1 +
2 = N.
,
N1, N2 .
. , 225 [ 2 , 2 , 5 ] . , . :
[D,O, , A, L, D]
+ [G,E, R,A,L,D]
=

[R,O,B,E,R,T]

Numberl

[D;:,D::,

. , , , D n ,

...

Number2
NumberJ

=
=

[DZi, ,
[D31, D 3 2 ,

. . ., D 2 i ,
. . ., D.4j,

. . .
. . .

]
]

D, , N ..,
. surc , Prolog , :
1- s u m !

[D,O,H,A,L,DJ,

[G, E, R, A, L, D] ,

[R, , , E, R, T)

).

stun , ,

. , , , ;
. , , .. ,
. , , N1, N2 ,
(. 7.1):

, ;
, ;
, ;
, .

. 7,1. ; i
: Djj = (Cl + DM + D?j) mod 10. = (Cl +
+ D2i) div 10

152

I, Prolog

sum
; , , suml. sural , :
suml{ , N2, N, C1, ,

Digitsl, Digits)

N1, N2 N , , sum, C1
( N1 N2) ( N1 N2).
.
?- suml [
= 8
=3
S = 7
U = 4
Digits =

[,] ,

[6,] ,

[U,S],

1,

1,

[1,3,4,7,8,9],

Digits; .

[1,9]

:
1 <- <- 1
5 3
6 3
4 7

. 7.1, 1 . 0, Hi, N2
sura.. , D i g i t s l
N1, N2 , a D i g i t s , .
, sum, , sum. suml :
sum( N1, N2, ) :suml ( Ml, N2, N, 0, 0, [0,1,2,3,4,5,6,7,8,9], _) .
suml.
, . , ,
, . ,
; " " .
suml , .
1. , :
suml( , [], ], , , 3igs, Digs).
2. - ()
, :
[Dl i N 1 ] ,

[D2 i N 2 ] ,

! D I ]

.
) , N1, N2 , suml;
(2)
, 3igs2.
) (Dl, D2 3) (2) , . 7.1,
^^ .1, t>\ & 1 ^- ^ - -^- - .
digitsum.
7.

153

Prolog , , :
s u m l ! [ D l I H I ] , [D2 | N 2 ] , [ D [ ] , C l , ,
suinU 1, N2, N, Cl, C2, D i g s l , D i g s 2 ) ,
d i g i t s u m ( Dl, D2, C2, D, C, D i g s 2 , D i g s ) .

Digsl,

Digs)

:-

Prolog digitsuT-n, , nonvar. Dl, D2 D .


, Digs2.
, Dl, D2 D
, , , . .. , ( ).
, .
d e l _ v a r i Item, L i s t , L i s t ) : nonvar( I t e m ) , !.
% Item
del_var( Item, [Item [ L i s t ] , L i s t ) .
%
del_var< Item, [A | L i s t ) , [A ! L i s t ! ] ) :d e l _ v a r ( Item, L i s t , L i s t l ) .
% Item


7.1, , Prolog ,
DOKALD, GERALD ROBERT, :
?- puzzleK HI, N2, ), sum( HI, N2, !.
7.1.

sum[Nl, H2, N) :$ ,
suinl ( HI, N2, N,
, 0,
* , ,
[0,1,2,3,4,5,6,7,6,9], _) . %
suml [ [], [], [], , , Digits, Digits).
suml[ [D1IN1], [D2|N2], [DIN?, Cl, C, Digsl, Digs)
suml( Ml, N2, H, Cl, C2, Digsl, Digs2),
digitsurot Dl, D2, C2, D, C, Digs2, Digs).

:-

digitsum( Dl, D2, Cl, D, C, Digsl, Digs) :del_var( Dl, Digsl, Digs2) ,
% Dl].
del_var( D2, Digs2, Digs3),
% D2
deliver( D, Dig3, Digs),
i D
S is Dl + D2 + Cl,
D is S mod 1,
%
is S // 10.
%
del_var( A, L, L) :nonvar(A}, !.

del_var[ A, [A|L], L ) .
del_var[ A, [B|L], [BjLl])
del_var( A, L, LI).

%
:%

154

I. Prolog

p u z z l e l [ [D,O,N,A,L,D] ,
[G,E,R,A,L,D] ,
[K,O,B,E;R,T] ) .
puzzle2(

[O,S,E,H,D],
[0,M,O,R,E] ,
(M,O,N r E,Y] ) .

, (, , D
5). , , Prolog
suml :
?- s u m l ( [ S , , N , A , L , 5] ,
[G,E,R,A,L,51,
[R,0,B,E,R,T],
, 0, [ 0 , 1 , 2 , 3 , 4, 6 , 7 , 8 , 9 ] , _) .

, .
, .

7.1. simplify
, ( ). ,

, .
.
?- s i m p l i f y ! 1 + 1 + , ) .
= +2
? - simplify! l + a + 4 + 2 + b + c ,
^ + + + 7
?- s i m p l i f y ) 3 + . + ., ! .
= 2+ + 3

).

7.2.
add_to_tail( Item,

List}

. , ,
, . L i s t . ,
. ,
, a, b .
List = [, , I Tail]

T a i l .
add_to_tail( d, List!

:
Tail = Id | HewTail] List = [a, b, c, d I HewTail]
, , , .

.

7,

155

7.2. :
=.., functor, arg name
: functor, arg "=. .". =. .,
"" (univ).
Term = . . L

, L , Term, . :
?- f ( , ) = . . L.
L = [f, a, b]
?- - . . [ r e c t a n g l e , 3, 5] .
= r e c t a n g l e ( 3, 5)
?- Z . . , X, f [X, Y)] .
Z = ( X, f <X,Y] )

( )
.
,
. , , , ..
, , , , .
square[ Side)
triangle; Eidel, Side2, Side3)
circle[ R)

.
:
enlarge! Fig, Factor, FiqlJ
Fig F i g l (
), Figl Fig, F a c t o r . ,
Fig ( ,
), Factor.
e n l a r g e .
enlarge! square(A), F, squarelAl}) :Al i s F*A.
e n l a r g e ( c i r c l e <R), F, c i r c l e ( R l l ) :Rl i s F*R1,
enlarge I r e c t a n g l e ( A , B ) , F, r e c t a n g l e [ A l , B l ) ]
Al i s F*A, Bl i s F*B.

:-

, ,
. ,
,
. ,
, , :
, , .
() , ,
.

156

I. Prolog

enlarge( Type(Par), F, Type(Psrl) ) :Parl is F*Par.


Prolog ,
, .
"=, .". enlarge {
) :
enlarge! Fig, , Figl) :Fig . . [Type | Parameters],
m u l t i p l y l i s t [ P a r a m e t e r s , T, P a r a m e t e r s l ) ,
F i g l = . . [Type I P a r a m e t e r s l ] .

m u l t i p l y l i s t ( [ ] , _ . , [] ) m u l t i p l y l i s t ( [X | L] , F, [XI | L I ] ) :XI is F*X, m u l t i p l y l i s t [ L, F, L I ) .

"=. ." , .
,
.
substitute ( Subterm, Terrr., Subterml, Terml)
: Subr.erm Term
Subterml, Terml, , .
?- substitute! sin(x), 2*sin(x)*f(sm(x)), t, F).
F = 2*t*f<t)
Subterm Term,
Terra, Subterm. ,
?- substitute( a + b, f[ a, A+B), v, F) .
:
F = f ( a, v)
= &,
=

- f ( a, v + v)
= . +
= +

s u b s t i t u t e
:
Subterm = Term, Terml = Subterml;
, Term 'atomic'
( ),
Terml = Term ( ),
Term.
Prolog, 7.2.
7.2.
% substitute I Subterir., Term, Subterml, Terml) :
%
Subterm Term
%
Subterml, Terml
I 1.
substitute{ Term, Term, Terml, Tecml)

:-

!.

% 2. , Term 'atomic'
substitute! _, Term, _, Term)

:-

7.

157

atomic(Term) , !.
% 3.
substitute( Sub, Term Subl, Terml) :Term =.. [F|Args],
s u b s t l i s t ( Sub, Args, Subl r Argsl),
Terml =.,
[Flftrgsl].

h
I

substlist< _, [] , _, []] .
s u b s t l i s t ( Sato, [TermlTerms], Subl, [Terml|Termsl])
Substitute( 5ub, Term, Subl, Terml),
Substlist( Sub, Terms, Subl, Termsl).

:-

, , " = . .". ,

, , . , , :
obtain( Functor;,
compute{ Arglist!,
Goal -., iFunctor I Arglist],
Goal
o b t a i n compute ,
. " = . . "
, Goal.
Prolog , ,
. , , .
, c a l l , . :
Goal =.. [Functor I Arglistj,
call( Goal)

. , ,
" = . .". ,
: functor arg. :
functor! Terra, F, N)
, F
F. ,
a r g ( , Term, A)

, N- Terra, , " , 1. :
1 - f u n c t o r ! t ( f ( X ) , X , t ) , Tun, A r i t y )
Fun = t
Arity = 3
?- a r g ( 2, f: X, t ( a ; , t ( b ) I , Y) .

158

I, Prolog

V = t(a)
1- functor[ D, date, 3),
arg( 1, D, 29) ,
arg( 2, D, June),
arg( 3, D, 1582).
D - d a t e ( 29, June, 1982)

f u n c t o r .
f u n c t o r ) D, d a t e , 3} ,
d a t e . ,
, Prolog, :
D = date( _5 _6, 7)
a r c .
^,
/ (. 6).
.
name ( , L)
, L ASCII .

7.3. ground ( Terra) , , Term .


7.4. s u b s t i t u t e , ,
'' " , .
, , , .
7- s u b s t i t u t e ! a+b, f (+) , , ) .
=
=
KewTerm = f( new!;
= a+b
= a+b
NewTerm = f( new + new)

.
7.5.
subsumes[ Terml, Tern2)
, ,
Terml , 2, , .
?- subsumes ( ., }.
yss
'?- s u b s u m e s ! g ( X ) , g [ t { Y M ) .
yes
?- subsumes* f(X,X), f(a,b) ).

7,3.

, .
,
7.

159

Prolog.
:
- i
, X Y . :
X is E

, X . , :
1 = : = 2

, 1 2. ,
, :
El = \= E2

: , . ,
"==":
1 2

, 1 2 ; , . , .
, :
Tl \== T2

.
?- f ( a, b) == f! a, b) .
yes
?- f ( a, b) == f ( a, X) .

no

?- f ( a, X) f { a, Y) .
no
?-

yes
?- t<
yes

\ "
X,

Y.
f[a,Yt>

==

t(

X,

ffa,Y}).


count{ Terra, L i s t , N)

7.1. , N
Term L i s t :
count (_, [], 0) .
count( Term, [Head | L ], H] :Term -= Head, !,
count( Term, L, HI) ,
N is (II t 1
count ( Terra, L, K) .

,
, X 2 < 5,

. ,
@<

160

I. Prolog

: X Y.
.
. ,
, X Y. .
?- paul @< peter.
yes
?- E(Z)e<f(3) .
yes

1 - g { 2 ) @ < f 13} .
no
7 - g ( 2 ) @>= f ( 3 ) .
yes
? - f ! a , g ( b ) , c ) @< f t a , h { a ) , a ) .
yes

@<, @=<, @>, @>=, .

7.4.
, ,
. Prolog ; (), ().
.

(
). a s s e r t ,
asserta, a s s e r t z r e t r a c t .
assert[ )

(assertion) , .. .

retract( )
: ,
.
Prolog:
?- c r i s i s .
no
?- assert( crisis) .
yes
?- c r i s i s ,
yes
?- retract( c r i s i s ) .
yes

?- c r i s i s .

, ,
"" .
a s s e r t r e t r a c t
. , , :

7.

161

nice :s-jr.shir.e, not gaining.


funn :sunshine, raining,
disgusting :raining, fog.
raining.
fog.
, ,
.
?- nice.

?- disgusting.
yes
?- retract [ fog) .
yes
?- disgusting.
no
?- assert! sunshine).
yes
?- funny.
yes
1- retract ( raining).
yes
?- nice.
yes
. Prolog , ,
a s s e r t / r e t r a c t , . dynamic ( P r e d i c a t e l n d i c a t o r ) . , a s s e r t , c o n s u l t , .
, r e t r a c t : r e t r a c t
. , ,
"", :
fast ( .) .
slow( torn).
slow( pat) .
:
?- a s s e r t (
[faster[X,Y) :fast (X), slow(Y) ) ) .
yes
?- f a s t e r ( A , B) .
A - ann
E = torn
?- r e t r a c t ( slow(x) ) .
X = torn;
X = pat;
no
?- faster( ann, _ ) .
no
, ( a s s e r t )
.

162

!. Prolog

,
.
asserta a s s e r t z .
asserta()
,
assertz()

. ,
a s s e r t a s s e r t z , Prolog.
,
:
? - a s s e r t ! p(b>), a s s e r t z l p(c)>, a s s e r t ! p ( d ) ) , a s s e r t a ( [ ) ) .
yes
? - P{X)
X - a;
X = b;
X < c ;
X *= d

c o n s u l t a s s e r t z . c o n s u l t a s s e r t " , : () .
a s s e r t a
. , , :
solve( Problem, solution)
, , , :
?- solve) probleml, Solution),
asserts( solve [ probleml. Solution)),
, (Solution) . "" , , , , .
, , , , . .
. , 9
: X V,
Z is X*Y, , . ,
.. maketable.
maketable : L = [0,1,2,3,4,5,6,7,8,9] ,

member( X, L ) ,
*
member! Y, L ) ,
%
Z is X*Y,
a s s e r t ( product(X,Y,Z)),
fail .

7.

163

,
?- maketable.
,
. , ,
8:
?- product(, , 8).

=
=
=
=

1
8;
2
4;


. , ,
a s s e r t r e t r a c t .
.
.
. ,
- , . . . ,
, , .

7.6. p r o d u c t .
) Prolog product
.
6} , , .
7.7.
copy_term( Tern, Copy)
Term, . a s s e r t a r e t r a c t . Prolog
, copy_terrti.

7.5.

, r e p e a t .
, .
, " ! " , . 5. once ( ), :
once[ )

164

:- ,

! .

]. Prolog

once [ ) . ,
once, .
f a i l , .
t r u e , .
not ( ?) ,
:
not()

: -

!,

fail;

true.

, not, 5.
c a l l ( ] , ,
.
r e p e a t , .
, , , . r e p e a t :
repeat.
repeat :- repeat.

r e p e a t ciosqcares, . s t o p ,
.
dosquares :repeat,
read; x ) ,
t - stop, !
Y is X*X, write(Y),
fail

7.6. bagof, setof findall


, , , .
.
, , . bagof, s e t o f f i n d a l l .

bagof ( X, , L)
L, X, , . , , X . , , :
age( peter, 7),
age( ann, ) .
age( pat, 8).
age ( torn, 5) .
5
:
7.

165

?- bagofl Child, age(


L i s t = [ a n n , torn)

Child,

5),

List).

, , :
?- bagcf{ Child, age f Child, Age!-, L i s t ) .
Age = 1
List = [ p e t e r ] ;
Age = 5
List = [ ann, torn];
Age = S
List = [ p a t ] ;
no
,
. , bagof " " ! TJXO Age;
, . :
?- b a g o f ( C h i l d , Age a g e ( C h i l d ,
L i s t = [ p e t e r , a n n , p a t , torn]

Age),

List).

" "
xfy.
bagof ( X, , L), bagof . X , L , .
s e t o ^ bagof.
s e t o f { X, , L)

I, X, .
L , , ,
. @<, , , :
?- setoft Child, ( Child, ) , ChildListJ,
setof( Age, Child ( Child, Age), AgeList).
ChildList - [ ann, pat, peter, tern;
AgeList = [ 5, 7, S]
, , , .
, , , , Age/Child :
?- setoft Age/Child, age( Child, Age}, List!
List = [ /ann, 5/torn, 7/peter, 8/pat;

, bago,
f i n d a l l .
findall< , , L)
, .
f i n d a l l bagof , X, () , X.
:
? - f i n d a l l I C h i l d , a g e [ C h i l d , Age),
L i s t = [ p- : ::, ann, p a t , torn]

List).

X, ?,
, f i n d a l l , , L - [ ].

166

I. Prolog

f i n d a l l ,
, . , ?, . ,
. ,
, . ,
. ==sert , . ,
, , , "bottom" ( , , , ).
f i n d a l l 7.3.
7.3. findall
EindalK , Goal, X l i s t ]
c a l l ( Goal),
a s s e r t z l queuelX) ),
fail;
a s s e r t z ( queue(bottom)
c o l l e c t I Xlist) .

:-

),

collect; L) :retract! quetie(X) ], !,


( X == bottom, !, L =

%
%
%

%
%
% ?

L = [X | Res^], collect( Rest) ),

7.8. bngof powerset ( Set,


S u b s e t s ) , ( ).
7.9. bagof
copy_term{ Term, Copy)

, Tern
.

Prolog ,
, Prolog. , Prolog.
Tun , .
var [ X). X () .
nonvar i X), .
atom! X]. X .
i n t e g e r [ X). X ,
f l o a t ( X). X .

7.

167

atomic ( X). X , .
compound [ X).X .
:
Term =.. [ Functor | ArgumentList]
functor( Term, Functor, Arity)
arg( M, Term, Argument)
name{ Atcm, CharacterCodes)
.
X = Y. X Y .
X == Y. X Y .
X \== Y. X Y .
X =:= Y. X Y .
X =\= Y. X Y .
X < Y. X Y (
-<, >, >=).
X @< Y. X Y ( @=<, ->, $>=}.
Prolog ,
.
a s s e r t ( Clause). Clause .
a s s e r t a ( Clause) . .
a s s e r t z { Clause). .
r e t r a c t ( Clause). , Clause.
, , .
bagof ( , L). L X, F.
setof ( X, , L). L X, .
inda.ll ( X, , L). bagof.
r e p e a t , .

168

I. Prolog

...
8.1.
8.2.
8.3.
8.4.
8.5.


Prolog

169
171
173
176
177

. : " Prolog?
Proiog? Prolog?
Prolog?"

8.1.

? ,
, ,
. .
. , .
, , .
. .
, , .
. .
.
, .
.
. , .
, .
.

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

. ,
. , , .
,
. ,
, ,
, .

.
, . . "
", " ".
, "" . , ,
; , . , , , . ,
, .
, , .

:
, ;
170

I. Prolog


, ;
, ;
, , , , ,
Prolog . ,

Prolog.

, , . , .

. ,
. ,
, , . . ,
,
: "
?"
,
, Prolog.
, Prolog1,
.

8.2, Prolog
Prolog ,
, .
2 ,
. , . , .
,
. , .
Prolog , .

8.2.1.
, , .
1. , , .
2. , (
) .
Prolog . :
,

8.

171

. ,
:
maplist( List, F, NewList!
L i s t , F ( ) NewList .
L i s t , .
1. : L i s t = [ ].
List = [], NewList = [], F
2. : L i s t = [X | T a i l ] .
, [X | T a i l ] ,
:
X F, NewX,
Tail, NewTa.il;
[NewX | NewTail].
Prolog :
m a p l i s t ( [ ] , _ , []} .
raaplistl [x I T a i l ] , F, [NewX I NewTail])
G =. . IF, X, NewX] ,
c a l i f G) ,
m a p l i s t [ T a i l , F, NewTail).

:-

,
. m a p l i s t :
square; X, Y) :Y i s *.
?- m a p l i s t ( [2, 6, 5 ] , square, S q u a r e s ) .
Squares - [ A, 36, 25]

,
Prolog, , . , . ( ),
, ( ).
( ), ,
( ). , .

8.2.2.
,
. .
. ,
, , .
.
, , . :
eightqueens( Pos)
, Pos . ,
N.
:
nqueens( Pos, N>
172

I. Prolog

, nqueens, .
1. : N = 0.
.
2. : N > 0.
N , :
, [ - 1) ;
,
.
:
eightqueens( Pos) :- nqueens( Pos, 8).

8.2.3.
, .
, . ,
.

^ :i:n,;'i, no , -

Prolog. , , .
1. Prolog , .
, , .
2. , Prolog,
.
3. Prolog Prolog, ,
, .
,
. ( .)

8.3.
, :
;
, , .
Prolog; , .

8.

173

8.3.1.
Prolog .
. ,
, .
, . ,
( ).
.
.
. , . ,
, ;
(, ,
, ); . Prolog .
,
. ,
.
.
, .
, ,
. 5, , .
, not
.
.
Condition, Goall, Goal2
Prolog :
Condition,
Goall
Goal2

!,

% Condition ?
* , Goall,
% Goal2

not , , , ,
not Prolog, ,
not ,
, - .
a s s e r t r e t r a c t
. ,
,
-.
, , ,
174

I. Prolog

.

; , , . ,
.
,
, :
merge ( L i s t l , List2, List3)
L i s t l L i s t 2 , L i s t 3 ,
:
meege(

[2,4,T],

[1,3,4,8],

[1,2,3,4,4,7,8]

merge,
.
merge; Listl, List2, List3) :Listl " [ ] , ! , List3 = List2; %
List2 - [ ] , ! , List3 = L i s t l ; %
Listl = [X I Restl],
List2 = [Y 1 Rest2),
[ X < Y, ! ,
Z = X, I Z - List3
merge( Restl, List2, Rest3);
Z = Y,
merge( L i s t l , Rest2, Rest3)),
List3 = [Z I Rest3] .
,
.
rae^ge|[|, List, List) :!.
%
merge! L i s t , [ ] , L i s t ) .
merge( [X j R e s t l ] , [Y | R e s t 2 ] , [X I Rest3]i :X < Y, !, merge( R e s t l , [Y [ R e s t 2 ] , R e s t 3 ) .
merge! L i s t l , [Y | R e s t 2 ] , [Y | Rest3]) :mergef L i s t l , R e s t 2 r Rest3).

8.3.2.
,
. , , . .
,
;
.
(
).

3.3.3.
, ,
. 8.

175

,
, , , .
,
.
, . , ,
, .
,
, . , .
, (,
), .
.
().
.
.
, .
; ;
, , . { ,
, .)
.
: . (
), .
PredicateNanWAcity
, merge [ L i s t I , L i s t 2 , List3) merge/3. /
" + " () " - " ().
, ~ierge{ + L i s t l , + L i s t 2 , - L i s t 3 ) ,
merge , .

8.4.
, , ().
( ), .
, .
Prolog : -, Prolog ,

Prolog; -, Prolog1
.
Prolog .

176

I. Prolog

. , , . , .
. .
. , ; .
. , .

.

, . ,
- , , .
: -, , ; -, , .
,
. .

trace
,
.
notrace
.
$( )
, . ,
, (
, ). spy "" .
r.ospyt )
"" .
. , , . , ,
, .

8.5.
,

. ,
.

Prolog, .
Prolog .
8.

177

, .
, .
, ,
, Prolog 1 , , Fortran,
0,1 , , . ,
, , 50 5 .
, Prolog . Prolog, ,
, , . ,
Prolog, , ,
. ,
Prolog , , , , ,
, , , , , , . , Prolog
.
,

. , Prolog , ,

.
, . .
. Prolog .

. ,
, , .


.

, , ,
.
, .
, , , , , . .

8.5.1.

(. 4.2). Y
178

!. Prolog


1 8. :
member; Y, [1,2,3,4, 5, 6,7.8])
member ,
Y = 1, = 2, Y = 3 .. ,
, ,
. , , , , , , . ,
, , :
member! Y, [1,5, 2, 6, 3, "7, 4,8] !
,
, 3-4 .
,
,
, .

8.5.2.
, ,
. , , .
,
:
ngb( Country, Neighbours)
Neighbours , Country.
, 30 , (
) :
{ albania, [greece, maceconia, Yugoslavia]).
ngb( andorra, [franee, Spain]>.
ngb( austria, [czech_republic, germany, Hungary, i t a l y , liechtenstein,
Slovakia, Slovenia, Switzerland]).
,
Country/Colour
.
,
. ,
Cl, C2, .. :
{ albania/Cl, ar.dc r / 2, austria/, . ..]
,
colours ( Country_colour_list)
, C o u n t r y _ c o l o u r _ i i s t
ngb. , , ,
. ,
, Prolog :

8.

179

colours( [ ] ) .
colours( [Country/Colour I Rest]) :Colours{ Rest},
member; Colour, [yellow, blue, red, green]),
noti member( Countryl /Colour, Rest), neighbour( Ccuntry, Countryl)).
neighbour[ Country, Countryl) :ngb( Country, Neighbours),
member( Countryl, Neighbours).
member ( x , L), ,
. . . ,
s e t c f , . :
country! ) ;- ngb[ , _ ) .
:
?- setof( Cntry/Colour, country( Cntry}, CountryColourList),
colours( CountryColOurList).
s e t o f / (CountryColOurList)
,
. , c o l o u r s
. , , - .
, Prolog c o l o u r s , .
/ ,
. ( ),
ngb. - , ..;
.
, ,
, , .
, .
, , .
, ; , ,
, ..
( ). ,
/ , . , ,
.
/ , ,
- .

, . rr.akelist.
( ) ,
(Closed).
, (Open),
180

I. Prolog

Closed. Open
Closed, Open .
jnakelist; List] :c o l l e c t ( [germanyl,
collect(

[},

Closed,

[],

List).

Closed).

%
% Closed

c o l l e c t ( [ X | Open), Closed, L i s t ) : member:! X, Closed! , !,


c o l l e c t { Open, 'Closed, L i s t ) .
ft
c o l l e c t ! [X | Open], Closed, L i s t ) :ngb( X, Ngbs),
conc( Ngbs, Open, Openl),
c o l l e c t ! Openl, [X | C l o s e d ] , L i s t ) .

% Closed?

% X
% Openl

, ,
.

8.5.3.

, , :
( [] , L, L) .
c o n c l [X | L I ] , L2, [X
c o n e ( L I , L2, L 3 ) ,

L3])

:-

,
. , :
?-

conc{

[a,b,c],

[d,e],

Li.

:
cone( [ a , b , c ] , [ d , e ] , L!
concf [ b , c ] , [ d , e ] , L 1 ) ,
cone ( [c] , [ d , e ] , L ' r ) ,
concl [ ] , [ d , e ] , L p p r ) ,
true,

L = [a | L ' ]
L ' - j b | L-1 ' ]
L " = [c I L 1 ' 1 ]
L 1 ' ' = [d,e]

, , , , .

? , ; , . ,
.
. ,
[a,b,d]

:
L1 = [ , , , d, ]
L2 - [d,e]

, L1-L2,
"" L1 L2. ,
, L2 L1. ,
.
, , [, , ] :
[,,]-[]

[ , , , d, e] - [d, e ]

181

L3

Puc. S.I. , ; L1 A1-Z1,


L2 A2-Z2, , , 1Z2, Z1 - 2
c o n c a t [ , , ] , , , I 1] - 1, [ d , e ] , [ d , e I 2] - 2.
?- concat( [,, I T1J - Tl, [d,e | 2] - 2, L!.
, c o n c a t , :
1 = [ d , e ] 2]
L = [ a , b , c , d , G | 12] - 2


, , .

8.5.4.

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

182

I, Prolog

. :
{ . . . ) :- . . . . ?
( . . . ) :- . . . . %
( . . . ) :-

. . . , !,
%
(...)
%
, ,
- . ,
. Prolog, ,
.
.
,
.
, :
sumlist( List, Sum)
, .
sumlist( [ ], 0).
sumlist( [ First I Rest], sum) :sumlist[ Rest, SumO),
Sum is X + SumO.

,
, ,
. ,
.
s u m l i s t Prolog ? , , is SumO. ,
.
sumlist( List, Sum) :sumlist[ List, 0, Sum). %
% sumlist[ List, PartialSum, TotalSum):
i TotalSum PartialSum + List
sumlist1 [], Sum, Sum). %
sumlist! [ First I Rest ], PartialSura, TotalSum) :NewPartialSum is PartialSum + First,
sumlistt Rest, NewPartialSum, TotalSum).
,
Prolog .
surr.list , ,
. s u m l i s t / 2 ,

sumlist/3.

,
FartialSum, .
(accumulator argument). -,
.

8.

183


.
reverse; List, ReversedList)

ReversedList , L i s t ,
. , :
reverse ( ;] , I ] ) ,
reverse( [X I Rest], Reversed) :reverse( Rest, RevRest),
conc< RevRest, [X], Reversed).

. ,
- conc( RevRest, [X], Reversed),
, RevRest.
:
, ' . , ,
. - ( ) " ".
,
:
reverse( List, Reversed) :reverse( List, [ ], Reversed!.
% reverse( List, PartReversed, Reversed):
%
Reversed List

PartReversed
reverse! [ ]/ Reversed, Reversed).
reverse* [X I Rest], PartReversed, TotalReversed) :reverse! Rest, [X | PartReversed], TotalReversed!.
%
i
( , ), .

8.5.5. arg

Prolog, . , , , , .
, 9 10, .
. , , , .
Prolog ,
functor. .
functort A, f, 100}
100 :

f < _ , _ , _ !

, , :
[60] = 1

184

!, Prolog

60- 1.
Prolog
:
( 60, ,

1)

60-
, :
- f ( _ , . . . , _ , 1 , _ , . . , , _ ) % 60- 1
, ,
- , N. :
= [60]


Prolog :
( 60, , X)
,
100 60-
. . , , :
[60] - [60] + 3


Prolog :
100 f u n c t o r ,
.
. ,
( ) ,
. , , , .
:
; ~ 1 ; : = 2 ; : = 3

Prolog
:
X - [1 | Restl]
% X = 1, Restl -
Restl = [2 I Rest2] % X = 2, Rest2 - :
Rest2 = [3 I Rest3J % X = 3

X = [1, 2, 3 | R e s t 3 ] . , X, ,
. , .
, ,
, ,
, , .
,
, 3
4 (. 4.4).
( X), { Y), ( U) ( 8.

185

V), .
, , . U V 4.4 , . . ,
15 , 15 :
Du = u(_,_,_,_,_,_,_,_,_,_,_,_,_,_,_!
, (X,Y) = ( 1 , 1 ! . 8- . , ,
8- Du
1 (.. X) :
arg( 8, Du, 1)
Du
Du - u(_,_,_,_,_,_,_ f l,_,_,_,_,_,_,_i

(X, Y) = ( 3 , 3 ) , S- , :
arg( 3, Du, 3)
i = 3
, 8- Du
1.
. 4.4.

8.5.6.


. Prolog
, .
-
N. :
1 , 1 , 2 , 3 , , S , 1 3 , . . .

, ,
.
ib{ N, F)
N-ro , F, N.
, = 1.
fib , , .
fib[
fib[
ilb(
N
Ml

1,1).
2, 1 ) .
, F) :> 2,
is N - 1,

% 1-
% 2-
% - &, N > 2
f i b ( Ml,

Fl! ,

132 i s N - 2, f i b ( N2, F2) ,


is Fl + F2.
% N-e -

.
, :
?- f i b (6,

186

F) .

!. Prolog

. 8.2 .
, , f (3), ,
.

2\

. 8,2. 6- fib

, . , a s s e r t a
() .
, f i b ,
, . fib2
f i b , :
fib2( 1 , 1 ) .
% 1-
fib2( 2, 1).
% 2-
fib2( N, F) :N > 2,
N1 i s N - 1,
fib2( Ml, F l ) ,
N2 i s - 2,
fib2( N2, F2),
F i s Fl + F2,
a s s e r t a ( fib2( N, F) ) .

% - , > 2

* Ne

fib2,
, ,
.
f ib2 { , Fi - 8.

187

. , 8.3 6-
fib2. . 8.2 ,
. ,
.

. S.S. 6- fih2,
; , fib (. . 8.2)

, , . , , .
, , . -
, " ",
. " ", , .
- . :
forwardiibi , N, Fl, F2, F)
F1 F2 (M-I)-e - , a F - .
forwardfib , 8. forwardfib , -

188

I, Prolog

( = N) . forwardfib , F,
, N. .
Eib3 ! N, F) :forwardfib[ 2, , 1, 1, F ) .
% 1
forwardfibl M, N, Fl, F 2 , F2) : >- N.
\ -
forwardfibl , N, Fl, F2, F) :!: < ::,
% N-e
HextM is M + 1,
NextF2 is Fl + F2,
forwardfibt NextM, H, F2, NextF2, F ) .

. 8.4. ; , , :
, f(M-l) f (M)
, forwardfib ,
. , F1 F2 .

8.1. , (subl, sub2 sub3}, , . subl "


" , sub2 s u b j " " . , .

. ?
?
subl( List, Sublist) :prefix] List, S u b l i s t ) .
subl( [ | T a i l ] , Sublist) :subl( Tail/ S u b l i s t ) .
% Sublist Tail
prefix( _ ,[]].
prefix! [X | Listl], [X I List2]) :prefix! Listl, List2).
sub2( List, Sublist] :cone( Listl, List2, List),
conet List3, Sublist, Listl).
sub3( List, Sublist) :cone( Listl, List2, List),
cone( Sublist, , List2).

8,

189

8.2.
add_at_end{ List, Item, HewList)

Item L i s t
KewLdst. .
8.3.
reverse( List, ReversedList)

.
8.4. c o l l e c t , 8.5.2, ,
.
8.5. ,
max( [XI, X) .
max( [X | R e s t ] , Max) :max( B e s t , M a x R e s t } ,
[ MaxRest >= X, !, Max = MaxRest
Max = X ) .

. : Max So Fa .
8.6. 3 (. 4.4}
, arg, , 8.5.5. , , .
8.7. ,
f u n c t o r arg, , , 8.5.5.

:
;
;
;
;
;
;
.
. , .
Prolog
.
. .
, ,
.

190

!. Prolog

. .

, , .
Prolog . .
.
:
;
;
( a s s e r t a ) ,
.
( ,
) . ;

;
, ;
;
functor .


[117] [132] Prolog.
(Sterling) [153]
Prolog .

8.

191


...
9.1.
9.2.
9.3.
9.4.
9.5.

192
197
201
206
208


, ,
. , : , .
Prolog1 , ,
, , ..
, Prolog.

9.1.
, . ,
:
gt{ , Y)
, Y, ,
"". , . , , :
g t ( X , Y) : - >.

, gt , , :
g t ( X,

Y)

:- @>.

, . ,
sort( List,

Sorted)

, L i s t , a Sorted
, -

gt. Prolog, . .
L i s t :
L i s t , X Y, ,
gt { X, ] , Y
List, L i s t l ; L i s t ! ;
L i s t , X Y, ,
gt ( X, Y) , L i s t .
, X Y, , , .
, .
(bubble sort).
Prolog, , b u b b l e s o r t .
bubblesori: ( L i s t , Sorted) ;swap! L i s t , L i s t l ) , !,
* L i s t ?
bubblesort( L i s t l , S o r t e d ) .
bubblesort( Sorted, S o r t e a ) .

swap( [X, Y I R e s t ] , [Y, X | Rest]) :gt( X, ).

swap* [Z I R e s t ] , [Z 1 R e s t l ] ) :%
swap( Rest, Restl) .
, .
, L = [X | ],
.
1. L.
2. X L , ,
.
.
Prolog
i n s e r t s o r t :
insertsort ( [),[]).
insertsort( [X | Tail], Sorted) :insertsort( Tail, SortedTail),
insert! X, SortedTail, Sorted).


i x

insert! x, [Y ! Sorted], [Y ; Sortedl]) :gt( X, Y), !,


insert{ X, Sorted, Sortedl).
insert! X, Sorted, [X | Sorted].).
.

193

b u b b l e s o r t i n s e r t s o r t , . ( i n 3 e r t s o r t ) . ,
i r . s c r z s o r t , \ q u i c k s o r t . ,
. 9.1.

- 91
quicksort

q u i c k s o r t .
L,
.
1. X L L ,
Small Big, :
L, X, Big, Small.
2. Small, SortedSmall.
3. Big, SortedBig.
4.
S o r t e d S m a l l [X | S o r t e d B i g ] .
, . q u i c k s o r t Prolog 194

I. Prolog

re 9.1. , X, L, L.
:
s p l i t ; X, L, Small, Big)
, , , .
, , log
, . , ,
, ,
;'. , , , q u i c k s o r t ,
.
, 9.1, , . , 8,
. , 9.1,
A-Z ;
SortedSmall A1-Z1
SortedBig A2-Z2
9.1. q u i c k s o r t
quicksort< L i s t , S o x t e d L i s t } :
l
L i s t quicksort
quicksort! [ ] , [] ) .
q u i c k s o r t ! [ X | T a i l ] , Sorted)
:s p l i t ( X, T a i l , Small, Big),
quicksort ( Small, SortedSmall),
quicksort { Big, 3 o r t e d 3 i g ) ,
conc( SortedSmall, [X ISortedBig], S o r t e d ) .
s p l i t ( X,

[ ] ,

[ ] ,

) .

Split; X, [Y|Tail], [|Small], Big)


g t ( x, Y ) ,
split;
s p l i t ; X,
split(

X,

:-

!,
Tail,

Small,

Big).

[Y|Tail), Small,
[Y|Big])
X, Tail,
Small, Big;.

:-

, SortedSmall [X | SortedBig] :
1 - \ [X | 2] - Z2

, , :
AI

Z2,

Z2

[X

2]

Z-Z. (. 9.1)
q u i c k s o r t , q u i c k s o r t 2 9.2. q u i c k s o r t ,

195

q u i c k s o r t 2 , .
:
quicksorts L, S! :quicksort2( L, S - I ]) ,
9.2. q u i c k s o r t
; s p l i t ( x, L i s t , S m a l l , Big)
9.1
% quicksort ( List, SojrtedList) :
%
List quicksort
quicksort( List, Sorted) :quicksort ( list, Sorted - [) ).
% quick5oi:t2 ( List, SortedDiffList) : List;
%

quicksort2{ J ! , Z - Z} .
quicksort2( [X I Tail), Al - Z2) :split( X, Tail, Small, Eig) ,
quicksoi:t2( Small, Al - iX j A2 ] ),
quicksort2{ eig, A2 - Z2).

9.1.
, , :
?- n e r g e f [ 2 , 5 , 6 , 6 , 8 ] ,
[1,3,5,9],
L = [ 1 , 2 , 3 , 5 , 5 , 6, 6 , 8 , ]

L) .

9.2. , 9.1
9.2, , , .
.

, 9.1, ,
9.2.
9.3. q u i c k s o r t ,
. , .
9.4. , q u i c k s o r t , ,
, ,
. , _, :
I, , L1 L2, ;
L1 L2, S1 S2;
S1 S2 L.
- .
- q u i c k s o r t .

196

I, Prolog

9.2.

. 1
,
. merrber ( X, L) , X
L, :
member! X,

[X | L] ) .

member! X, [Y | L]
m e n b e r ( X, L} .

:-

, X L, , X
.
.

, .
, .
, :
;
;
.
,
. . 9.2.
{, , , d}.
. . 9.2 ; ,
, .

Prolog. ,
, .
, . 9.2, :
{ , c(d) )

. 9.2.

9.

197

; ,
. , .

;

( ).
:
n i l ;
t, , , X, L R, t I L, X, R), . 9.3.
,
. 9.2, :
t t t( n i l , , n i l ) , a, t( t( n i l , d, n i l ) , c, nil) )

. 9.3,

, i n .
in( , >
, X . in
.
X , :
X ,
X ,
X .
Prolog :
in(X,t<_,,_))in(X, t ( L , _ , _ ) ) :i n ( X, L) .
i n ( X, t ( _ , _ , R ) ) :in ( X, R) .

,
i n i X,

nil)

X.
in. , . 9.2.
i n ! , )
:
X = ;

X = ;

X = ;

X = d

, in.
198

I. Prolog

in ( a, )


in. ,
in! d, T)
in, d. ,
in( e, TJ


in .
,
.

. . t ( Left,
X, Right) , ,
.
1. , Left, X,
2. , Right, X.
3. .
.
. 9.4.

. 9.4, ;
6
5>->

, .
X , X , ,
. , ,
. 9.4. 5, 6 5 , 6 > 5.
5, 6 . , S ..
.
X D, :
X D, X , ,
X , D, X D,

9.

199

X D;
D , .
in, 9.3. gt{ X, Y] : X Y. ,
, , X > Y.
9.3. X
% in( X, Tree}: X Tree
i n ( X, t ( _, X, _) ) .
i n ! X, t( Left, Root, Right) )
g t ( Root, X! ,
i n ( X, L e f t } .

:-

i n ( X, t( Left, Root, Right} )


gt( X, Root},
in( X, R i g h t ) .

:-

% , X
%
% X ,
%

in . , D, 5, 3, 8:
? - i n ( 5 , D) ,
D - t ( t ! Dl,

. ( 3 , D ) , i n ( a , D) .
3 , D2) , 5 , t ! D 3 , 8 , D4) ) .

Dl, D2, D3 D4 .
, D 3, 5 8.
(. 9.5).

. 9.5. ,
: ) D, i n ( 5, D), in f 3, D), i n ( 8, D); ) , in ( 3, D) , in (5, D) , in < 3, D)
, . , , . ?
, .
,
200

I, Prolog

. ,
,
, .
. .
() ,
. , log n.
, .
log
. , , , .
, .
, , .

, z .
. 10.

9.5. :
) b i n a r y t r e e l

Object).

) d i c t i o n a r y ! Object).
, Object
; .
9.6.
height , height)
, 0, 1.
9.7.
Iineari2e( Tree, List)
Tree .
9.8.
roaxelement ( D, Item)
, Item , D.
9.9.
in( Item, BinaryDictionary)
, Path, , Path
Item.

9.3.
,
. , S,
.
in ( X, S). X S.

9,

201

add ( S, X, S I ) . . S Si.
e e l ( S, X, S I ) . X S 31.
add.
, ,
. . 9.6 , . a d d l e a f ( D, X, Dl>.

, 9.6. ;
: add D1, 6, D2), ddd ( D2, 7, D3),
add< 03, 4, D4)
.
X t (
nil).

nil,

X,

X D, Dl = D ( ).
D X, X D; D
X, X .
9.4.
9.4.
' addleaf[ Tree, X, NewTree) :
%
X Tree
%
NewTree
addleaf( nil, X, t( nil, X, nil)}.
addleafl t( Left, X, Right), X, t( Left, X, Right)).

202

I. Prolog

addleafl t( Left, Boot, Right), X, t( Leftl, Root, Right))


gt[ Root, X),
addleaft Left, X, Leftl).

:-

addleafl t( Left, Root, Right), X, tl Left, Root, Rightl))


gt( X, Root) ,
addleaft Right, X r Rightl).

:-

. ,
.

, .
delleaf{ D1, X, D2) :addleaf ( D2, >:, DU .

, X ,
, . 9.7. X . Left Right. X
Left Right .
X
( ), .

. 9.7. X ,
, . X

(Left Right) , : ,
, ,
, 9.8. Right ( Y)
,
X. . ,
,
Left.

, 9.5. :
delmin( Tree, Y, Treel)
(.. ) Tree, a Treel Tree
Y.

9,

203

rue, if.a. , X
9,5.
% del( Tree, X, KewTree):
%

X Tree NewTree

del! t( nil, X, Right), X, Right).


del! t( Left, x, nil), X, Left).

del( t( Left, X, Right), X, t( Left, Y, Rightl))

:-

delmin( Right, Y, Right1).


del( t( Left, Root, Right), X, t( Leftl, Root, Right))
gtI Root, X ) ,
del! Left, x, Leftl).
del( t( Left, Root, Right), X, t( Left, Root, Rightl))
gt( X, Root),
del( Right, X, Rightl),

:-

:-

% delminl Tree, V, NewTree):


%
X Tree
*
NewTree
delitiint t( nil, , R), Y, R) .
delmint t( Left, Root, R i g h t ) ,
delmin( Left, Y, L e f t l ) .

Y,

t( L e f t l , Root, Right))

:-


. add
, ,
, . .
X D,
.
X D (, X ).
D , X D,
X D.
D.
:
a d d r o o t i D, X, D1)

X , D, a D1 , X. X, D D1

204

I. Prolog

. 9.9. . Li
L2 . 9.9 (, , R1 R2).
, .
L1 L2 .
L1 L2 L.
L1 X L2 X.

. 9.9. X
a d d r c o t . ,
X L , L1 L2. Prolog L1 L2 :
addrootl L, X, t( LI, X, L2
R1 R2:
a d d r o o t ! , X, 11 R1, X, R2] )

9.6 "" .
9.6.
i add( Tree, X, NewTree):
i
X Tree
$ NewTree
add[ Tree, X, ) :addxoot( Tree, X, NewTree).
add[ t ( L, Y, R) , Y,, t ( L I ,

gt(

Y,

add( L,

x),

X,

% X

r , R) )

LI].

add{ t ; L, Y, R), X,
g t ( X, Y) ,

t ( L,

Y,

Rl))

9.

% X

205

a d d ( , X, R l ) .
% a d d r o o t [ T r e e , X, K e w T r e e ) :

T r e e NewTree
addroot(

nil,

X,

t[ n i l ,

X,

nil)).

a d d r o o t ( tt L, Y, R) , X, t( L I , X, t( L2, Y,
g t ( Y, X),
a d d r o o t ( L, X, t( L I , X, L2>>.
a d d r o o t ( t( L, Y, R ) , X, t( t( L, Y,
g t ( X, Y),
a d d r o o t t R, X, t( R l , X, R2)} .

R l ) , X,

R) ) )

:-

R2))

:-

,
.
acid , . , ,
3, 5, 1, 6, 5, DD:
add(
add(

n i l , 3, D l ) , a d d ( Dl, 5, D 2 ) r
D 3 , 6 , D) , a d d ( DD, 5, D)

add(

D2,

1,

D3),

9.4.
Prolog, w r i t e .
write! T)
, . Prolog, ,
.
, .
.
, , , .
, , .
. 9.10.

show( T)
, . 9.10.
.
, .
1. , .
2. .
3. , .

206

I. Prolog

. .10. : )
; ) , show (
)

, ,
.

show2l , )

? . . show
show2 :
show! ) :- shov:2 ( , 0) .

, 2 ,
9,7. .
9.7.
% show( Tree):
show! Tree) :show2{ Tree, 0 ) .
show2( Tree, Indent): Tree Tndent
shov>2 ( nil, _! .

shou2( t( Left, X, R i g h t ) , indent)


Ind2 is Indent + 2,
showS( Right, I n a 2 ) ,
t a b ! I n d e n t ) , w r i t e ! X), n l ,
show2( Left, I n d 2 ) .

%
%
%
%

9.10. , , . ( )
, , .
9.

207

9.5.
9.5.1.
, , .
, . , . . .
, , . , 9.11.

. 9.11. : ) ; )
,

Prolog .
, , . , . 9.11,
, , :
connected! , ) .
connected! b, ) .
arc! s, t, 3).
ate( t, v, 1 ) .
arc! \L, t, 2) .
,
. :
. ,
. graph, .
, () ,
. 9.11, :
G1 = graph; [a,b,c,dj, [ ( , , e(b,d>, e(b,c), e ( c d ) ] )
d i g r a p h
( ). , . 9.11,
:
G2 = digraph! [ s , t , u , v l , I a ( s , t , 3 ) , a ( t , v , l ) , a ( t , u , 5 ) , a l u , t , 2 ) , a ( v , u , 2 ) ] l
, , , ,
.
, ,
.
208

I. Prolog

, . ,
, ;
S1 - [ -> [ b ] , b -> [ a , c , d ] , -> [ b , d ] , d -> [ b , c ) ]
G2 = [ -> E t / 3 ] , t -> [u/5, v / 1 ] , u -> [ t / 2 ] , v -> [u/21

, " - > " "/" .


,
. .
,
,
.
.

.

9.5.2.
, G , Z G.
:
pathf A, Z, G, }

Z G.
. - , . 9.11, , :
p a t h ( a, d, G,
p a t h ( a, d, G,

[a,b,d] }
[a,b,c,d] )

, . .
Z G,
:
1. = Z, = [];
2. PI Y Z,
Y, , 1.
,
,
( ?1).
:
pathl (A, P a t h l , G, Path)

. 9.12, .
.
G .
P a t h l G.
Path G, P a t h l
P a t h l .
p a t h p a t h l :
path( A,

Z,

G,

Path)

: - p a t h l ! /

9,

[Z],

G,

Path),

209

. 9.12. patbl, Path , Z; Path Pathl


, . 9.12, , p a t h l
. ,
P a t h l ( Y . 9.12)
Path ( ). ,
X, , :
1. Y X;
2. X Patr.l;
3. . p a t h l (, [X | P a t h l ] , G, P a t h ) .
9.8. member .

adjacent! X, Y, G)
, G X Y. . G (
) :
G = graph! Nodes, Edges)
:
adjacent[ , Y, graph( Nodes, Edges) ) :member( e(X,Y), Edges)
member( e CY,X!, Edges).
9.8. P a t h Z G r a p h

% path ( A, 7.r Graph, P a t h ) : Earn - Z Graph


path( , S, Graph, Path) :pathl1 A, [Z], Graph, P a t h ) .
p a t h l ! A, [A | P a t h l ] , _, [A I Pathl] ).
pathl ( A, [Y | P a t h l ] , Graph, Path) :adjacent ( X, Y, Graph),
net meanber{ X, Pathl)
% ,
p a t h K A, [X, Y P a t h l ] , Graph, P a t h ! .
; , . path, ;
hanultonian[ Graph, Path) :path! _, _, Graph, path),
covers( Path, Graph).
covers( Path, Graph) :not { node! N, Graph), not member! N, Path)).
node ( H, Graph) , N Graph.
210

I. Prolog

. . , , , ,
, 1, p a t h p a t h l
, , , , :
patht A, Z, , , )
p a t h l [ AF Pi, e l , G, p, c)
Cl 1.
adjacent . 9.9 , .
9,9. ; P a t h - C o s t z
G r a p h
* path( , 2, Graph, Path, C o s t ) :
%
Path - Cost or Z Graph
path( A, Z, Graph, Path, Cost) :p a t h l ( A, [Z], 0, Graph, Path, C o s t ) .
p a t h l [ A, [A } P a t h l ] , C o s t l , Graph, [A | Pathl], C o s t l ) .
p a t h l ( A, [Y I P a t h l ] , Costl, Graph, Path, Cost} :a d j a c e n t ! X, Y, CostXY, Graph),
not member( X, P a t h l ) ,
Cost2 i s Costl + CostXY,
p a t h l ( A, [X, Y | P a t h l ] , Ccst2, Graph, Path, c o s t ) .

. (nodel node2) Graph :
path( nodel, node2, Graph, MinPath, HinCost),
not ( path{ nodel, node2, Graph, , Cost), Cost < MinCost)
,
Graph :
path( _, _, Graph, MaxPath, MaxCost),
not ( psth{ _, _, Graph, _, Cost!, Cost > MaxCost)
, .
, - , .
. 11 12.

9.5.3.
, . , G = ( v, E} V
. G =
(V, ' ), Z ' - , , :
1. ;
2. .

9.

211

, . , . 9.11, , , :
Treel = [a-b, b-c, c-d]
2 = [a-b, b-d, d-c]
- [a-b, b-d, b-c]

X-Y X Y.
. , , , .
:
street G, )
G. , G .
: G , , , ,
, , .
. : ,
, .
, , 9.10.
:
spread! Treel, Tree, G]
9.10. : "" ;
,
%
% .
% , Graph = [-, -, b-d, c-d]
i stree{ Graph, Tree): Tree - Graph
stree{ Graph, Tree; :member! Edge, Graph),
spread! [Edge], Tree, Graph).
% spreadt Treel, Tree, Graph): Treel ""
i,
no Tree Graph
Spread{ Treel, Tree, Graph) :addedge( Treel, Tree2, Graph),
spread[ Tree2, Tree, Graph).
spread( Tree, Tree, Graph) :not addedge! Tree, _, Graph). % ,
% addedge( Tree, NewTree, Graph):
Graph Tree,
addedge( Tree, [A-B | Tree], Graph) :adjacent! A, B, Graph),
% Graph
r.ode{ A, Tree),
% Tree
not node! , Tree).
% - Tree
adjacent( Model, Node2, Graph)
member( Nodel-Node2, Graph)

:-

meniber{ Hode2-Nodel, Graph) .


212

I. Prolog

node! Mode, Graph]


:a d j a c e n t ! Mode, _, Graph).

% Node - Graph,
% Graph

. G
, T r e e l Tree G, ,
. Tree G,
G Treel ( ).
, " T r e e l Tree" ( spread).
,
, , . , ,
, 9.10.
.
1. -, :
G,
,
"" G, .. G .
2. , :
,
.
p a t h (. )
Prolog, 9.11. ,
- .
9.11. : "" ; node
a d j a c e n t 9.10
i

stree( Graph, Tree): Tree - Graph
stree ( Graph, Tree) :subset( Graph, Tree),
tree( Tree),
covers( Tree, Graph).
tree( Tree) :cormected( Tree),
not hasacycle( Tree).
% connected; Graph): Graph
connected) Graph) :not { node( A, Graph), node ( B, Graph), not path( ft, B, Graph, _) ).
hasacycle( Graph) :adjacent( Nodel, Hode2, Graph),
path[ Model, Node2, Graph, [Model, X, Y | _] ). % 1
% covers[ Tree, Graph): Graph Tree
covers) Tree, Graph) :not ( node( Bode, Graph), not node ( Node, Tree) i.

9.

213

% subset{ Listl, List2); List2 Listl


subset ( [], [] ).
subset( [X | Set], Subset)
subset) Set, Subset).

:-

subset! [, | Set], [X I Subset))


subset! Set, Subset).

%
:-

$ X

9.11. , . , . .
9.12. ,
9.10 9.11, . .

Prolog . , .
:
{ , ,
);
.
:
;
;
;
, ;
, ;
.
:
;
;
.


,
,
Prolog. ,
{, [2], [3], [37], [61] [73]). Prolog
(. 9.3) ( ) . van Emden (M. ).
214

!. Prolog

10

, ...
10.1. -
10.2. AVL-

215
221

. ,
.

,
. :
- (2-3) AVL-. (, ,
.)

10.1. -
, ( ) . leg n,
. , in, add
delete , .
,
log .
,
. , .
, . 10.1.
, . , log n, .
, , ,
, in, sad
delete log n. , , , .
.

in, add d e l e t e ,
log . - ( 2-3) , AVL-.
- .
, , , .
.
.
- - , , . (
. 10.2.) ,
.
, .
, .
X -
, .
, Ml 2. :

. 10.1. ;

. 10.2. - ; 10

216

I. Prolog

X < Ml, ;
, X < 2, ;
.
, , .
, X < , ,
. ,
,
X, .
, -
. ,
log , , .
- , . , ,
, . ,
, , , . . ,
. . 10.3.

. 10,3. - ;
, -

- ;
add23{ Tree, X, NewTree)
NewTree , X Tree.
, i n s . :
ins{ Tree, X, NewTree)
NewTree X Tree. Tree NewTree
. , ,
. i n s , , :
i n s ( T r e e , X, , Mb, HTb}

X Tree
, NTb, , Tree. Mb
NTb. ,
, . 10.4.
10.

217

. 10.4. ,
ins ( , 6, WTa, Mb, NTb)

- , , .
n i l .
1 (X) X.
2 (1, , 2) , 1 2; 2.
(1, 2, 12, , ) , Tl, T2
; 2 2, .
Tl, T2 - .
add23 i n s , , :
add23l Tree, X, NewTree) :ins ( Tree, X, NewTree).
, ir.s
, 71 72, :
a d d 2 3 [ T r e e , X, 2 ( T l , , 2))
i n s T r e e , X, T i , 2 ) .

:-

i n s ,
: , , :2
.
, . , i n s
, , ins, . . 10.5.
, , Prolog .

i n s ( 2 ( 1 , , 12),
g t ( , X ) ,
i n s ( T l , X, N T 1 ] .

X,

2 ( 1 , , 2 ) ) : % X


i n s ( n2( T l , , 2 ) , X, ( NTla, Mb, NTlb, , 2)
g t { , X],
i n s { T i , X, NTla, Mb, N T l b ] .

:-


i n s ( ri3 ( T l , M2, T 2 , M 3 , T 3 ] , X,
g t ( K2, X ) ,
i n s ( T l , X , N T l a , Mb, N T l b ) .

218

rs2 ( N T l a ,

Mb,

NTlb),

M2,

n2 ( T 2 ,

))

I. Prolog

, 10.5. ins: a) ins ( 2 (1, , 12),


X, .2 (NT1, , T2));6)ins( 2 {71, , 2) , X, (NTla, Mb, NTlb, , T2)l;
) ins ( (1, 2, 2, , } , X, r\2 (NTls, Mb, NTlb), 2, 2 ( 2, , )}

10.1 - , 10.2 - .
10.1. - ;

\ -
add23( Tree, X, Treel) :ins( Tree, X r Treel).
add23( Tree, X, n2( Tl, M2, T2)
ins( Tree, X F Tl, M2, T2).
del23( Tree, X, Treel) :add23( Treel, X, Tree).

% X Tree, Treel
%
:-

h X Tree, Treel

ins( nil, x, i(X)).


ins ( 1[A), X r 1(A), X, 1(X)) :
gt( x, A ) .
ins ( 1(A), X, 1(X), A, 1(A)) :
gt( A, X) .
ins( n2{ Tl, M , T2}, x, n2( 1, M, T2))
gt( M, X) ,
ins( Tl, X, HI1).

:-

ins( n2[ Tl, M, T2), X, n3( HTla, Mb, HTlb, M, T2))


gt{ , x ) ,

ins( Tl, X, NTla, Mb, NTlb).


ins [ n2 ( Tl, M, T2), X, n2 [ Tl, M, HT2) ) :gt{ x, M),
ins( T2, X, KT2).
ins( n2 [ Tl, M F T2), X, n.31 Tl, M, NT2a, Mb, NT2b))
gt( X, M),
10.

219

ins[ T2, X, NT2a, Mb, HT2b).


ins( n3( Tl, M2, T2, M3, T3), X, n3( HT1, M2, T2, , })
gt( 2, X ) ,
ins[ Tl, X, NT1).

:-

ins( n3( Tl, M2, 12, , ), X, n2{ HTla, Mb, NTlb), M2, n2{ T2, , )> :gt{ M2, x ) ,
ins[ Tl, X, NTla, Kb, HTlb).
ins( n3( Tl, M2, T2, M3, T3}, X, n3i Tl, M2, 2, , ))
gt( X, M2), gt( M3, X ) ,
ins( T2, X, NT2).

:-

ins 1 n3( Tl, M2, T2 r M3, T3), X, n2( Tl, M2, NT2a), Mb, n2( NT2b, , T3)> :gt( X, M2), gt ( M3 ; X ) ,
']ns( T2 r X, NT2a, Mb, NT2b).
ins[ n3( Tl, M2, T2, M3, T3), X, n3I Tl, M2, T2, , ))
gt( X, )

:-

ins( , x f ).
ins[ n3( Tl, M2, T2, M3, T3I, X, n2( Tl, M2, T2), M3, n2( , Mb, }>
gt( x, M3),
ins! T3, X, , Mb', ) .

:-

10.2. - () ,
. 10.2, ()
% -
ShOW(T) :shovHT, 0) ,
sh.ow{ nil,

15
15

J.

13
showd (ft) ,H) :tab[H), write(A), nl.

13

show( n2CTl,MvT2), H) THI is H+5,


show( T2, HI),
tab(H), write!i, nl,
tab[H), write(H), nl,
tab[K), write(--), nl,
show( Tl, H1J .
show( n3{ Tl, M2, T2, , ), )
HI is H+5,
show! T3, HI),
tab(H), write! ) , nl,
tab(H), write(M3f, nl,
show! T2, HI),
tab{H), write(M2), nl,
tab(H), write! ) , nl,
show! Tl, HI).

12
12
10
10
8
:-

8
1
7
5
5
4
4
3
3
I

220

i. Prolog

. , i n s ,
i n s . ,
, ing :
ins2( Tree, X, NewTrees)
NewTrees , 1 3, :
HewTrees = [ NewTree], ins{ Tree, XF NewTree)
HewTrees = [ NTa, Mb, NTb], i n s ( T r e e , X, HTa, Mb, NTb)

add.23 :
add23( T, X, 11) :i n s 2 ! , X, T r e e s ) ,
combine( Trees, T i l .

combine , 1, Trees.

10.1.
in( Item, Tree)
Item - Tree.
10.2. , 10.1, ( ins2 combine).

10.2. AVL- -

AVL- , .
1. 1.
2. AVL-.
. , AVL- log , ( ).
in, add d e l ,
.
AVL- ,
,
. , .

.
, - 1 , 0 +1.

, .
:
addavl( Tree, X, NewTree)
Tree, KewTree AVL-, , HewTree Tree X. AVL- :
t( Left, Pi, Right) /Height
10.

221

, Left Right , a Height . n i l / 0 . X


AVL-, .
Tree = t( L, A, R)/H

, X .
X :
addsvl( R, X, t l R1, , R2) /Hb)

. 10.6 ,
KewTree:
L,

A,

R1,

R2

. 10.6. AVL-: a) AVL


X, X > ; 6) AVL X R;
) ,

L, R, R1 R2? L R 1. . 10.6 , R1 R2. R ,


X, (R1 R2) h + 1.
, X , ,
, . NewTree ( T r e e l , Tree2
) , . , , NewTree, AVL-. NewTree ;
Tree!, , 2, ,
.
1. , 2, , .
2. T r e e l , , ,
2 .
3. , , ,
2 T r e e l .

222

I. Prolog

. 10.7 , NewTree
. 1 (2)
NewTree. , . 10.7,
Prolog :
combine( Treel, A, 2, , , ]

. 10.7. AVL-

, KewTree, AVL-,
, . , 1 :
conibine (
Tl/Hl, A, t ( 2 1 , 2 2 ) / 2, , /,
t( t [ T l / H l , A, T21) /, , t ( 2 2 , , / ) / ) / )
2 > HI, H2 > ,
is HI + 1,
is + l f
Hb is + 1.

:%
%
h
%

%
* V-.v.
-


addavl,
, 10.3.

10.

223

10.3. AVL-;
( c o m b i n e
. . 10.7)
% addavl{ Tree, X, NewTree): AVL-
%
Tree = t( Left, Root, Right)/KeightOfTree
%
- nil/0
addavl( nil/0, X, t(nil/0,X,nil/0)/1).
addavl ( t (L, Y, .)/, X, NewTree} :gtl Y, X ) ,
addavl( L, X, t(Li,Z,L2)/_) ,
combine( LI, Z, L2, Y, R, NewTree).
addavl ( t(L,Y,R)/Hy, X, NewTree) ;gt { X, Y) ,
sddavl( R, X, t(Rl,Z,R2)/_),
combine^ L, Y, Rl, Z, R2, NewTree).

% X
%
%
% MewTree

% E

4 combine( Treel, A, Tree2, , , NewTree):


%
Treel, Tree, AVL-
combine! Tl/Hl, A, t121,,22)/2, , /,
t[ t[Tl/Hl,,21)/, , t(T22, , /)/)/ ) :2 > HI, H2 > ,
& -
is HI + 1,
is + 1,
is + 1.
combinei Tl/Hl, A, T2/H2, , /,
t( Tl/Hl, A, t(2/2,,/)/)/ i :HI >= 2, HI > = -,
%
m a x l ( 2, , ! ,
m a x l ( HI, He, HaJ .
c o m b i n e [ T l / H l , A, T2/H2, , /,
t( t(Tl/Hl,ft,2/2)/, , /}/ )
: >- 2, 113 >= 1,
I
m a x l ( HI, H2, ) ,
maxl[ , , ) .
m a x l [ U, V, ) :U > V, , i s U + 1 ;
i s V + 1,

% 1 + , V V

. ,
, . (
), - 1 , 0 +1.
.

10.3.
avl(

Tree)

, AVL- Tree; , 1. , t { Left, Root, Right) n i l .


224

!. Prolog

10.4. AVL-,
5, 8, 9, 3, 1, 6, 7,
?

- AVL-, ,
.

: , . ,
log n, .


- , , [2] [3].
[3] Pascal. [172] Pascal AVL-. -
-.
, - AVL-, , , [61J . AVL-,
(.. , - 1 , 0 +1), , [159].

10.

225

II

Prolog

...

11.
12.
13. AND/OR
14.
15.
16.
17.
18.
19.
20.
21.

22,
23.

228
247
277
301
326
357
383
408
446
478
510
532
559

11

...
11.1.
11.2.
11.3.
11.4.

228
232
238
242


, .
, ,
. ,
. . , , , .

11.1.
, . 11.1. , , , . .
, .
(, ,
). , , .
.
: . , :
;
;
.
,
, ,
.

. 11.1.
,
.
1. .
2. , , .
,
. , , , 11.2. , .

( ) , .

, 11.2.
; , . 11.1
11.

229

. 11.3 . " " . , 1 8


3*3, .
, . :
, . , ,
, . 11.3.

. 11.3. " "

, .
" ",
, , ,
. ,
.
.
(, ); , .
( ) . , , -

230

II. Prolog

, , , .
. ,
.
, ,
.
.
, , .
, , , Prolog.

s i ., Y)

, X Y. Y
X. ,
:
( X, Y, C o s t }

,
.
. , , , s, , .
, , . ,
, , , , , , .
,
. 11.1. ,
, .

, . , , ,
.
. , , , ,
. .
(. . 11.1) :
[:c,a,b], [ J , t ] ]
,
, . :
[ [ , , ] ,
[],[]]
[[), [ , ] , []]

t[] ,,]]

: 2 1,
1 , S t a c k l Stack2,
S t a c k l Stack2.

11.

231

,
Prolog :
S{ Stacks, ( S t a c k l , [Topi [ Stack2] I OtherStacks] ) :%
% Topi Stack2
d e l l [Topi I S t a c k l ] , Stacks, S t a c k s l ) ,
%
d e l ( Stack2, S t a c k s l , OtherStacks).
%
d e l ( x, [x | L], L) .
del( X, [Y 1 LI, [Y I LI] j :d e l ( X, L, L I ) .
:
goal( Situation; :member( [a,b,cj, Situation!.

solve( Start, Solution)
S t a r t , a S o l u t i o n
S t a r t .

?- solve! [ [c,a,b], 1 1 , Solution).
S o l u t i o n
. ,
[ , b, ].

11.2.

. . ,
.

.
Sol , :
N , Sol = [N];
, - N1 , , Soil
N1 , Sol = [ N | S o l i ] .
Prolog :
solve[ N, [] ) :goal( !.
s o l v e ! , [ | s o i l ] ) :S< N, HI),
solve( Ml, S o i l ) .
, . , . ,

, .
232

II. Prolog

, ,
. . 11.4 , . Prolog :
7- solve( a, S o l ) .

. 11.4. :
a , f j . , ,
,
, : . , d, h, e, i, j. [a,brefj}.
[a,c,f]

Prolog. ,
Prolog .
.
(. 4), , .
,
solve, .
, .
- , .
, .
{
, ).
Y , :
s{ Queens, [Queen I Queens]) :member! Queen, [1,2,3,4,5,6,7,8]),
% Queen
*
noattack{ Queen, Queens).
goal ( [_i__r_i_(_i_._] ) -

11.

* 8

233

n o a t t a c k , Queen
Queens; ,
4.
?- solve[ (], Solution).

.
.
.
, ,
,
s o l v e .
, .
solve , . 11.4, : h d, (. 11.5). : , . , . 11.4, h , d.
h,
d. d, h ..,
d h .

. ,
, . :
depthfirst ! Path, Wocte, Solution)
. 11.6, Mode ,
, Path ( )
Node, S o l u t i o n Path, Node .
. Path .
1. , Node, ( ).
2. S o l u t i o n .

. 11.5. ,

d h : , , d, h, dr h, d, . . .

234

II. Prolog

. 11.6. depthfirstl
Path, Node, Solution)
11.1.
11,1. ,

% solve[ Hade, S o l u t i o n ) :
I
Solution (
i
) Node
solve{ Uode, Solution)
:d e p t h f i r s t ( [ ] , Mode, S o l u t i o n ) .
% d e p t h f i r s t { Path, Node, S o l u t i o n ) :
$
Solution [Node | Path]
$

d e p t h f i r s t ( Path, Node, [Node i Path] )
goal( Node).
d e p t h f i r s t l Path, Node, Sol}
:= ; Node, Nodel),
not member( Nodel, P a t h ) ,
d e p t h f i r s t ( [Node | P a t h ] , Nodel, S o l ) .

:-

,
. 11.5. , . ,
. , .
, .
, , .
,
Y ,
, .
() : . :
depthfirst2( Node, Solution, Maxdepth)

11.

235

, Maxdepth.
,
. 11.2.
11.2.
% d e p t h f i r s t 2 [ Node, Solution, Maxdepth):
%
Solution Node ,
% Maxdepth
d e p t h f i r s t 2 ( Node, [Node], _)
goal( Node) .

:-

d e p t h f i r s t 2 ( Node, [Node I Sol], Maxdepth)


Maxdepth > 0,

:-

s( Node, Model],
Maxl is Maxdepth - 1,
d e p t h f i r s t 2 ( Model, Sol, Maxll.
(. 11.2) , , . {.. ), .
,
. , , , .
, .
, 11.2, : d e p t h f i r a t 2 ,
1.
, ;
path( Model, Node2, Path)
Path Nodel N00162 . , . path :
p a t h ! SOde, Node, [Node]).
%
p a t h ; FirstNode, LastNode, [lastNode I Path]) :p a t h ! FirstNode, Or.eBurLast, P a t h ) ,
% , ,
i
s( OneButLast, LastNode),
%
not member( LastNode, P a t h ) .

, , . 11.4, .
?- path( a, Last, Path).
Last =
Path - [ ] ;
Last - b
Path = [b,a] ;
Last -
Path = [ c , a ] ;
Last = d
Path = [d,b,a] ;

236

II, Prolog

path, , . : ,
, , ,
. :
depth_first_iterative_deepening[ Node, solution] :path! Mode, GoalNode, Solution),
goal( GoalNode].
,
, , . , ,
- "" ,
. , , ,
.
, , , . , ,
( ),
. ;

.
. , ,
.

11.1. ( )
depttifirstl ( CandidatePath, Solution)
S o l u t i o n CandidatePath.
, , S o l u t i o n .
11.2. ,
, , 11.1 11.2.
11.3. dspth__f i r s t _ i t e r a t i v e _ d e e u e n i n g / 2 ,
, , .
, , , .
,
, . ,
i+1, i.
11.4. ,
<7, , np:f ,

. 11.1.
11.5.
show( Situation)

11.

237

S i t u a t i o n . , S i t u a t i o n , ,
, .
show(

[a],

[e,d],

[,]

])

, , ,

11.3.
, , . , , , , (. 11.7).

. 11.7. :
a , f j .
, ,
,
, : , , , d. . f. , fa, , f ], ,
,
[a,b,e,j)

. , ,
, . , .
,
.
. ,
breadthfirst( Faths, Solution)

,
Paths .
S o l u t i o n .

238

II. Prolog


: ,
. , , , .
, , :
[ [StartNode] ]

.
:
,
;

, ,

.
, . 11.7, , .
1. :
])
2. [ ] :
[

[,],

[,*]

( , .)
3. , [ , ] , :
[

[d,b,a],

te,b,a]

:
[

[,a],

i d , b , a ]

[e,b,a]

4. [, ) , :
[

[d,b,a],

[e,b,a],

[ f , c , a ] ,

[g,c,a]

[ d , b , a ]

[

[f,c,al,

[g,c,a] ,

[hrd,b,a],

[ i f e , b , a ] ,

[ j,e,b,a]

[ , , ] -

[ f , c , a ] , , f.
.
, , 11.3.

bagof. . , , , bagof b r e a d t h f i r s t . member cone, ,
.

11.

239

11.3.
% solvef S t a r t , Solution):
>
Solution (
%
) Start
solve( S t a r t , Solution)
:b r e a d t h f i r s t ( [ [Start] ], Solution).
* b r e a d t h f i r s t ( [ Pathl, Path2, . . . ] , Solution):
i
Solution
%

b r e a d t h f i r s t ( [ [Node | Path] I _ ] , [Node I Path])
goal! Mode).
b r e a d t h f i r s t ( [Path j Paths], Solution)
extend! Path, NewPaths),
cone( Paths, NewPaths, Pathsl),
b r e a d t h f i r s t ( Pathsl, Solution).

:-

:-

extend( [Node I Path], NewPaths) :bagoft [NewNode, Node [ Path],


( s( Node, NewNode), not member( FewNode, [Node I Path] ) ),
WewFaths),
i

% , bagof,
% , Node
e x t e n d ! Path, [] > .
. (. 8), , Paths Z, .
Paths -
, 11.3,
, 11.4.
.
11.4.
11.3;
. e x t e n d 11.3
& solve( Start, Solution):

Solution (
%
) Start
solve( Start, Solution) :breadthfirst( [ [Start] I Z] - Z, Solution).
breadthfirst[ ( (Node | Path] I _] - _, [Node | Path] )
goal( Node >.

:-

breadthfirst( [Path I Paths] ~ Z, Solution} ;extend! Path, NewPaths;,


cone[ NewPaths, Zl, Z ) ,
\ NewPaths
Paths \ Zl,
I -
breadthfirst( Paths - Zi, Solution).

240

II. Prolog


11.6. , , . , = 2 d = 3, ,
(
). K(b,d) , .
, N( b, dl
( b, d - 1).
11.7. , 11.3, , , ,
11.4. , , 11.4:
Paths \ 21
, , ; , . 11.7. :
,
.
11.8. ,
, ,
?
11.9. , , ,
( , )? :
s.
?
11.10. ; ..
. ,
. ( s)
, , , .
11.11. , f i n a l , fina2 fincO, ,
. .
findl( Mode, [Mode]) :coal( Node).
f i n d l ( Mode, [Node | Path])
s( Node, Nodel),
f i n d l ( Nodel, P a t h ) .

:-

find2< Node, Path) :cone ( Path, _, _) , % conc/3 .


f i n d l ( Node, P a t h ) .
find3( Mode, Path! :goal( Goal),
find3< Node, [Goal], P a t h ) .
f:_nd3 ( Node, [Mode | P a t h ] , [Node I P a t h ] ) .
find3( Node, [Node2 | ?athZ], Path) :s( Nodel, Node2),
find3( Node, [Nodel, Node2 [ Path2L Path}.
11,

241

11.12. :
% search; S t a r t , Pathl - Path2}: S
i
S o l u t i o n - P a t h l Path2
s e a r c h ( S, PI - P2) :s i m l l a r _ l e n g t h ( P I , P2), %
g o a l [ G) ,
path2 ( G, P2, N),
p a t h l [ S, PI, H] .
p a t h l ( H,

[H], N).

pflthl( F i r s t , [ F i r s t [ R e s t ] , Last)
s ( F i r s t , Second) ,
p a t h l ( Second, Rest, L a s t ) .
path2( , [N], N).

:-

path2( F i r s t , [ F i r s t I R e s t ] , Last)
s( Second, F i r s t ) ,
path2 ( Second, Rest, Last] .

:-

similar_length( L i s t l , List2} :- %
equal^length! List2, Listj,
%
{ L i s t l = L i s t ; L i s t l = [_ I L i s t ] ! .
equal_length[

[],[]).

e q u a l _ l e n g t h [ [XI | L I ) ,
e q u a l _ _ l e n g t h ( L I , L2 > .

[X2

L2])

:-

11.13.
.
11.14. , ,
,
. . , , . , 11.3 11.4, ,

11.4.
. , . ,
.
, , , 1 , , .
, , , , , . . 11.8.
, -
, . , ,
, ,
. ,
,
.

242

II. Prolog

- 11.8, : ) ,
; ) , , ,
. , - 11.3

, ,
. ,
( ).
. , , .
,
, . ,
.
- , . ( ),
. , 12.
, , .
, , , . , . ,
. , ,
, b . ,
d, d . d b d .
". () . ,
,
.
.

, ,
,
.
d.
11.

243

, ,
, :
1 + b + to2 + b

...

, d, 0 ( b n ) , Ofb^).
,
, O[b d ).
,
d . , dmaK, , d < d T l y . 0( ), O(dmaX).
, ,
, . ,
, ,
.
(d + 1) : , I, ..., d. 0 ! d ) . {d + 1)
, d ..
:
(d+l)*l

d*b

(d-l)'b

...

l*b

0{b d ).

. , , , / { - i ) . 2 , .

( ) (
)
.
( 11.10-11.12). , ( ), . ,
b , . ,
d, , ; , . ,
, , d/2.
, .
d ,
, , d/2. . 11.1 ,
.

244

II. Prolog

11.1. ,
. b - , d - , d* , d < d*

:V

b d x

"

1:

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

.
, , .
. .
.
, .
, , ,
.
, . :
.
, .
.

, .
, . .
.
11.

245


. . .
;
;
, , ;

;
;
;
;
;
.


, [133] [171]. [81] ,
. [78] .

246

II, Prolog

12

...
12.1.
12.2.
" "
12.3.
12.4.

247
256
261
265

,
, -
. .

. , .
,
. ,
, .

12.1.
. ,
. (..
, ),
:
.
, , ( , ' ) ' .
, , ,
f {}
" " .
, f -

.
f, *.
f (n) ,
, ,
. , , , ,
t. f ()
(. 12.1) :
f (n)=g(n) + h(n)
g (n) s n, a h () t.

, 12.1. f (n)
s t :
fin) = g(n) f h (n)

:

. s
( s ,
), ,
g (n), s . , h ( n i , , ""
t . h{nl , , , . h , h . ,
, .
, h ,
.
. , ; , . ; ..
,
, ;
f (
248

II. Prolog

f-).
, f- ,
- . . , : , , , ,
. , .
.
. 12.2. ,
,
z. X ,
d i s c ( X, t ) . :
f(X)

g(X)

h(X)

g(X}

d i s t (

X,

t)

, ,
: 1 , 2 . 1 , f-
, . , 1
, 2 , :
f(c)

g(c)

h(c)

10

f ()

g (e)

h(e)

f (e ] < f (), 2 , 1 . :
f(f)
f(c)
f(c)

= 7 + 4
= 10
<
f(f)

= 11

2 , 1
, d, , f (d) = 12 > ] 1. 2, , t.
, ,
- f- . , .
, .
.
1. 1( N, F/G; (); N
, G {>3) ( , N), F f (N) = G + h{N).
2. t ( , F/G, Subs) ;
N , Subs , G
g (N), "" f- ( f-
); Subs f- .
, , . 12.2.
, , :
s , . :
t( s, 7/0, [1 [, 7/2), 1 [,9/2)] >
12.

249

. 12.2. s t no
: ) , !
t; ) , , .
. ,
.
(,
, , ( , )
f- s 7, .. f- .
250

. Prolog

.
, f- 9. ,
, , f- 9. b . f () = 10,
. :
t( s, 9/0, [1(,9/2>, t(a,1.0/2, [t{b,10/4, [He,10/6!] > ] ) 1 )
, f- 10, s 9. , , . s ,
f- 9.
- ,

(.. , ). - f. . , (), , :
f <)

g{n)

h(n]

, ., Si, S2 .., :
f (T)

min

f (Si)

,
, 12.1.
.
expand, :
expand! ?, Tree, Bound, Treel, Solved, Solution)
(), , f-
Bound. expand.
. Tree.
Tree. () .
Bound. f- Tree.
Treel. 3, \:.; , f-
Treel Bound { ).
Solved. , "yes", "no"
"never".
S o l u t i o n . " T r e e l "
Bound ( ).
12.1.
$ bestfirst( Start, Solution):
%
Solution - S T O Start
bestfirst) Start, Solution) :expandf [], 1( Start, 0/0), 9S99, _, yes, Solution).
s , f- 9999
% expand! Pathr Tree, Bound, Treel, Solved, Solution):

12.

251

&
I
%

Path . Tree 1
Tree, Bound; , Solution
Solved = yes

1. - -,

expand! P, 1( N, _ ) , _, _, yes, [HIP])


goal(Ml.
%
4

:-

2. -; f- Bound.
- Bound

expand( , 1(,F/G), Bound, Treel, Solved, Sol) :F -< Bound,


( bagofl /, [ s(N,M,C), not member[M,P) ), Succ),
!,
%
succlist( G, Succ, Ts),
% Ts
bestf{ Ts, 1 } ,
% f-
expand! P, t(N,Fl/G,Ts), Bound, Treel, Solved, Sol)
Solved - never
) .
I

3. -; f- Bound.
;
continue

expand! P, t(N,F/G,[T|Tsj), Bound, Treel, Solved, Sol) :F =< Bound,


bestf ( Ts, BF! , mini Bound, BF, eoundl) ,
k Boundl - min (Bound, BF]
expand{ IN IP], r, Boundl, Tl, Solvedl, Sol),
continue( P, t(N,F/G, [Tl|Ts]), Bound, Treel, Solvedl, Solved, Sol) .
%
s

4. He - . ,

expand; _, t(_,_, []), _, _, never, _) :- !.


%

5 * f- Bound.

expand( , Tree, Bound, Tree, no, _)


ft Tree, F) , F > Bour.d.

:-

% continue! Path, Tree, Bound, NewTree, SubtreeSolved, TreeSolved, Solution)


continue{ _, _, _, _, yes, yes, Sol).
continue{ P, tIK,F/G,[Tl|Ts]), Bound, Treel, no, Solved, Sol)
insert( Tl, Ts, NTs),
bestf( HTs, Fl),
expand! P, t[N,Fl/G,NTS), Bound, Treel, Solved, Sol).
continue( P, t(N,F/G,l_|Ts]), Bound, Tceel, never, Solved, Sol)
bestf( Ts, Fl),
expand! P, t,Fl/G,Is), Bound, Treel, Solved, Sol).

:-

:-

I succiist( GO, [ Nodel/Costl, . . . ] , ! 1(BeEtMode,BestF/G), . . . ) ! :

F-
succiist{ _, [], []).
succlistt GO, [/ i NCs], Ts)
G is GO + C,

252

:-

II. Prolog

h( , ) ,
F i s G + H,
s u c c l i s t ( GO, HCs, T s l ) ,

% h(N)

i n s e r t ! 1{W,F/G], Tsl, Ts) .


i Ts
i f-
insert; T, Ts, [ | Ts] ) :f( , F), b e s t f { Ts, F l ) ,
F -< Fl, ! .
i n s e r t ; T, [Tl | Ts], [Tl | Tsl])
i n s e r t ( T, Ts, Tsl) .

:-

i f-
f( 1(_,F/_), F) .

% f-

f( t ( _ / F / _ , _ ) , F ) .
bestff [ | _ ] , F)

i f-
:-

f-

1 ( , F} .
bestf(

[j ,

9999).

min( X, Y, X)
X =< Y, ! .
mint X, Y,

% : f -

:-

Y) .

, Tree Bound ""


expand; , expand . expand , Solved, .
1. Solved = yes.
S o l u t i o n ,
Tree Bound.
T r e e l .
2. Solved = no.
Treel Tree, , f- Bound
(. 12,3).
S o l u t i o n .
3. Solved = never.
Treel S o l u t i o n .
, Tree () . , f-
Tree Bound, ,
(.. ) ,
, .
, expand, . , , , Tree , ..
Tree = t ( , F/G, [T | 7 s ) !

12.

253

, , , f-
, Ts. , , , .

f-. c o n t i n u e
; , . , , . , ,
Tree = 1 N, F/G!
- N N -. s u c c l i s t , -, g- f- (. 12.4),
,
Bound. , , ,
- Solved = ' n e v e r 1 .

1- > Bound

. 12.3. expand:
Tree , f- Bound,
Treel
.
5<

N,

- , N .
h ( N,

N .

254

II. Prolog


. ,
. , * (.
, ). * , . *.

: 12.4. g- , f- g-

, (.. ), ,
. , , , . ,
h* in) . , , : *,
h, , :
h(n)

<

h*{n}

. h*, h*
h *. , * . , , :
.) -
, .
. . =: ,
- . *,
h = 0, . ,
,
[, ' ) - 1 ( , ' ) .

. h, h* ( ),
h* ( ). ,
12,

255

h*,
h*, -, h*, , - .

12.1. s, goal h ,
. 12.2, . * .
12.2. :
" , * , _[) <
h* (n.) ". ?
12.3. , h\, h2 hj (<
h*), * .
, h,
hi, .
12.4. - .
,
. ,
, .
. (, ).
*.
s( S t a t e , NewState, Cost) h( S t a t e , 11) * (, ). , goal {Xg/Yg) , Xg Yg
. :
obstacle( Xmin/Ymin, Xmax/Ymax)
Xmin/Vmin , a Xmax/Ymax .

12.2.
" "
, 12.1, , , .
(" "),
, .
.
, , :
s( Node, Nodel, Cost)

256

II. Prolog

,
Mcde Nodel Cost.
g o a l ( Mode)

, Node .
h( Node, H)


Node .
: " " ( 11.1)
.
, " ",
12.2.
. . , X/Y. .
1. .
2. 1.
3. 2.
4
(. . 11.3), , :
goal! [2/2,1/3,2/3,3/3,3/2,3/1,2/1,1/1,1/2]).
12.2. " ",
,
12.1
/* " "
,

3
2
1

1 2

4
6

7
1

3
5

[2/2, 1/3, 2/3, 3/3, 3/2, 3/1, 2/1, 1/1, 1/2]

, - "" ,
. ,
; , ""

*/
% s [ node, SuccessorNode, Cost]
S( [Empty I Tiles], [Tile ] Tilesl], 1)
swap{ Empty, Tile, Tiles, Tilesl).

:- % 1
%
Empty Tile Tiles

swapi Empty, Tile, [Tile [ Ts], [Empty | Ts] ) :mandist{ Empty, Tile, 1).
% 1

12.

257

swap( Empty, Tile, [Tl I Ts], [Tl I Tsl] )


swapt Empty, Tile, Ts, Tsl).
mandist( X/Y, Xl/Yl, D)

:-

:-

% D -
i

dif ( X, XI, Dx),


dif < , , Dy) ,
D is Dx + Dy.
dif( , , D) :D is A-B, D >= 0 F !

* D IA-B|

D is -.

% h
% "" " "
hi [Empty I Tiles], H] :goal{ [Emptyl I GoalSquares] ),
totdist{ Tiles, GoalSquares, D ) ,
seq( Tiles, S ) ,
H is D + 3*S.

%
i

totdist{ [], [], 0) .


totdist( [Tile I Tiles], [Square ] Squares], D)
mandist 1 Tile, Square, 1),
totdist[ Tiles, Squares, D2) ,
D is Dl + D2.

:-

% seq( TilePositions, Score):


seq( [First I Other-Tiles], S) :seq( [First | OtherTiles ], First, 3 ) .
seq( [Tilel, Tile2 | Tiles], First, S)
score( Tilel, Tile2, SI),
seq( [Tile2 t Tiles], First, 2),
S i s SI + S2.

:-

seq( [Last], First, S} :score( Last, First, S ) .


score) 2/2, _, II

:-

!.

score! 1/3, 2/3, ) : - ! .


score!
score[
score[
score[
score(
secret
score!

% , , 1
% ,
% , 0

2/3, 3/3, 0) :- ! .
3/3,3/2,0) :- !.
3/2, 3/1, 0) :- !.
3/1, 2/1, 0) :- I.
2/1, 1/1, 0) :- !.
1/1,1/2,0) :- !.
1/2, 1/3, G) :- !.

score! _, _, 2 ) .

,
* , 2
goal [2/2,1/3,2/3,3/3,3/2,3/1,2/1,1/1,1/2] ). *

showsol( [] ).
showsolf [ | L] )
showsol{ L),

258

:-

II. Prolog

til, w r i t e ! '

'),

showpos( P ] .
%
showposi

[S0,Sl,S2,S3,S<S,S5,S6,S7,S81

:-

member[ Y, [3,2,1) >,


% Y
nl, member[ X, [1,2,3] ),
%
member! Tile-X/Y,
% X/Y
[ ' '-SO,1-S1,2-S2,3-S3,4-S4,5-S5, 6-S6,7-S7, 8-S8 ] ( ,
write( T i l e ) ,
fail

true.

%
Btartll

[2/2,1/3,3/2,2/3,3/3,3/1,2/1,1/1,1/2] ). % 4

s t a r t 2 ( [2/1,1/2,1/3,3/3,3/2,3/1,2/2,1/1,2/3] ).

* 5

Start3( [2/2,2/3,1/3,3/1,1/2,2/1,3/3,1/1,3/2] ).

% 19

% : ?- s t a r t l ( Pos), b e s t f i r s t [ Pos, S o l ) , showsol[ S o l ) .


:
n a n d i s t ( SI,

52,

D).

D S1 S2;
S1 S2 .
.
1. , 12.2, , , . 12.5.

, 12.5. " ": ) . 4 ;


) . 5 ; ) 18
h :
hi , )
Pos , ,
1. t o t d i s t . ,
,
. , ,
. 12.5, , t o t d i s L = 4.
2. seq. , , ,
, , . -

12.

259

seq
:
1;
, , ,

(, 1 2);
2,
.
, , . 12.5, ,
seq = 6.
, , = t o t d i s t + 3 * seq.
, . , ,
. 12.5, , , ,
, , .
, , - . , . 12.5, . ,
: ,
- .
, h ,
h < h* . , ,
. 12.5, , :
h = < + 3

6 - 2 2 , h* = 4

, "
", :
t o t d i s t < h*
: ,
,
,
. t o t d i s t .
,
, ,
t o t d i s " .

12.5. ,
12.1, , ,
. ,

r e t r a c t a s s e r t .
" "
, .

260

II. Prolog

12.3.

. ,
, t i , tz, ..-,
, Dj, D;, ... . .. ,
.
, , , , ,
- . ,

. . ,
.
. 12.6 , . , ,
. ,
, 12,6, 2 t j , t-i.

. 12.6. ,
, . , ts
20
, ti, t ; . : , 24, , 33.
(, [34], . 86),
Prentice Hall, Englewood Cliffs, New Jersey

12.

261

, , .
(
), , .
, .
. :
;
- , ;
, ,
, ;
;
, , ;
( ) ;
, ( ) , Fi
F;, F^ - W\,
. , , . , , . -
, - ,
.
, - ,
.
, .. . .
1. .
2. .
, .
3. ( ) , ,
.
:
[ T a s k l / Q l , Task2/D2,

... ]

, ; , :
Task/FinishingTime

m , .
,
. 262

II. Prolog


. ( , )
:
WaitingList * ActiveTasks * FinishingTime

, ,
:
precl Taskx, TaskY)
. , . . ,
.

,
. , .
1. .
2. () ,
.
,
Di, D;, ..,, Fi, F2
,
F i n a l l ,
, :
Finall =

v
( Z ^ + ZJF-,)/m

m . , :
Fin = (Fj)
( ,
)
:
Finall > Fin, .4 - Finall - Fin, =
,
,
12.3. , , . 12,6.
, 12.1. ,
, ,
, . 12.6.

12.

263

12.3. , ,
. , . 12.6,
() ,

/* , ,

,
:
[ WaitingTaskl/Dl, WaitingTask2/D2, ...] *
[ Taskl/Fl, Task2/F2, ...] * FinTime
;
, ,
, , F1 < F2, F2 -< F3 ...
Fintime

*/
I s( Mode, SuccesscrNode, Cost)
s{ Tasks! * [_/F | Activel] * Finl, Tasks2 * Active2 * Fin2, Cost) :del( Task/D, Tasksl, Tasks2),
%
not ( member( T/_, Tasks2), before( T, Task) },
%
% ,
not ( member! Tl/Fl, Activel), F < Fl, before[ Tl, Task! 1, %
%
Time is F + D, %
insert{ Task/Time, Activel, Active2, Finl, Fin2),
Cost is Fin2 - Finl.
s( Tasks * !_/F | Activel] * Fin, Tasks * Active2 * Fin, 0) :insertidle( F, Activel, Active2).
%
before! Tl, 12)

:-

%
% 11 2

prect Tl, T2) .


before( Tl, 12) :prec( T, T2),
before { Tl, T) .
insert; S/A, [T/B j L ] , [S/A, / I L], F, F)
A =< , !.
insert [ S/A, [T/B | L], [T/B | LI], Fl, F2)
insertr S/A, L, LI, Fl, F2).

:-

:-

insert; S/A, [], [s/A], _, A ) .


insertidle( A, [T/B | L], [idle/B, T/B I L) ) :- %
A < , !,
%
% ,
insertidle( A, [T/B I L ] , [/ 1 L1] )
insertidle( A, L, L1).
del ( , [ | L] , 1>) .

del( , [3 [ L] , [ | L1] )
del( A, L, L1).
goal( [] *

264

).

:-

:-

% ~
%

. Prolog

%
% ,
%
{ Tasks * Processors * Fin, H) -.totaltime[ Tasks, Tottime),
%
suinriumt Processors, Ftime, N),
% Ftime -
% , N -
Finall is ( Tottime + Ftime)/Nr
( Finall > Fin, !, H is Finall - Fin
H = 0
).
totaltime( [], 0) .
totaltimel [_/D | Tasks], T)
totaltime( Tasks, Tl) ,
T is Tl + D.

:-

sumnum( [], 0, 0}.


suranum( [_/T I Procs], FT, N)
summim( Procs, FT1 F HI) ,
N is HI + 1,
FT is FT1 + T.

:-

%
prec( tl, t4). prec{ tl, tb). prec( tZ, t4). prec( t2, tS).
prect t3, t5). prec( t3, t6). prec( t3, t7>.
l
startf |tl/4, t2/2, t3/2, t4/20, t5/20, t/ll, t7/ll] *
[idle/0, idle/0, idle/0] * 0 ) .
% : start( Problem), bestfirst( Problem, Sol).

, .
.
.

12.4.

12.4.1. *


,
: .

. , "" 12.

265

, ' ,
' .
,
* ( , 12,1) .
, ,
. ( ),
.
. *
. , ,
.
*, .
(. 11).
. , .

. IDA* (Iterative
Deepening A* * ),
RBFS (Recursive Best-First Search
).

12.4.2. IDA*
IDA* (Iterative Deepening A* * )
, .
. . IDA* (.. f- ). IDA*
, . IDA* .
Bound :- f(StartNode);
Repeat
, StartNode, ,
N , f() Bound;
if
then , " ",
else
NewBound f- ,
Bound:
KewBound = min {f [) I N, ,
f (N) > Bound}
Bound: - KewBound
u n t i l " "
* (. 12.2, 6), , f ( s ) = 6,
IDA* :
266

It, Prolog

Bound = f ( s ) = 6
, . f 6.
s, :
f () =7 > Bound
f [ e ) = 9 > Bound
NewBound min ( 7, 9} =7
, f < 1, s.
f-, , b .
NewBounc = min { f (bj , f <e] } " min( 8, ] = 8
Bound : 9 ( f ( e ) l , 10 ( f ( c ) ) ,
11 ( f { f l ) . .
Bound = 1 1
" " .

IDA*,
, , .
, IDA*, , , , . , .
f-.
,
. , f-, ,
.
f-. f-, ,
( ), ,
IDA*, .
IDA* . ,
f(N> g () + h(KJ . h
(.. h(N) < h* (N) , IDA* .
IDA* , {.. f-). , , , f = g + h. f ,
. f
, . , f , N'
: s ( , ' ) , () ( ' } .
, f f- ,
f- .
, f f-, , . , , f .
IDA* Prolog
12.4. Prolog. f-
neKt_bound( Bound)
a s s e r t r e t r a c t . . (
12.

267

r e t r a c t a s s e r t )
99999. , f-.
, N ,
f (N) < Bound. , , f (N) > Bound, f () Next Bound {
next_bound (NextBound)). f (N) < NextBound, nextjbound f ().

12.6. f,
IDA* .
12.7. IDA*, 12,4,
" "
/ t o t d i s t / , 12.2. h ( )
( t o t d i s t / ) . f/2 ( IDA*) , f {) = g{N) + {).
g(N) g-
(, = G r T i l e P o s i t i o n s ) . " ",
12,2.

[ 1 / 2 , 3 / 3 , 3 / 1 , 1 / 3 , 3 / 2 , 1 / 1 , 2 / 3 , 2/1,2/2].
, *
( 12.2) IDA*
{ ).
12.4. *
idastar( Start, Solution):
%
IDA*,- Start - ,
%
Solution -
Idastar( Start, Solution) :retract I next_bound(_J ), fail

%
%

assertaf next_bourid ( 0)),


idastarO < Start, Solution).
idastarO] Start, Sol) :retract! next_bound( Bound)),
asserta{ next_bound( 99999)),
f[ Start, F!,
df( [Start], F, Bound, Sol)
next_bound( NextBound;,
MextBound < S9999f
idastarO( Start, Sol).

i
%
I
%
%



f-
;

%
%

i df[ Path, F, Bound, Sol):


% Bound. Path -
% ( ),
I F f- , .. Path
df( [ I Hs], F, Bound, [N | Ns])
F < Bound,
goal! N ) .

268

:% -

II. Prolog

df( [N | Ns], F, Bound, Sol) :F < Bound,


% f- N
s( Ht N1) , not member( HI, Ns}, % N
f( N1, F1J ,
df( [HI, I NS], Fl, Bound, Sol).
df( _, F, Bound, _) :F > Bound,
update_next_bound[ F ) ,
fail.
update_riext_bound ( F)
next_boundI Bound},
Bound =< f, ]

% Bound
%
i

:-

retract! next_bound( Bound)), !,


asserta( next bound( F)}.

% He
%

12.4.3. RBFS
IDA* , . ,
, RBFS (Recursive
Best-First Search ).
RBFS *, 12.1 ( , , RBFS!). * RBFS , ,
. RBFS
( ), .
RBFS ( IDA*) . RBFS , , f- . f- f- ,
. ""
f , ( ),
f {N). N, (
).
F(N). f- ( , - N).
() .
F(N) = f (), N () .
F(N} = min{ F(Ni) | Ni - N) .
*, RBFS , f-.
F- (
F- , .. F- ,
). , N
(..
F-). N N f- Bound. ( F[N) > Bound) ,
12.

269

N, . F(N)
, .
F- , ,
.
. , N,
. F(N) > f () , , N
F{N) ,
. ,
Mi N f(Ni) \
. F{Ni) :

f[N;)

<F{N),

FfNJ

F[N),

F(Ni)

f[Ni)

:
F(Ni)=

raax{F(H)

, f <N;) }

, , f (N1) < f (N) , F- Ni N N;.


: N; ( ) ,
F(Ni) F ( N ) . , , F(N) .
RBFS (. . 12.2). . 12.7 ( ), .
( *) .
. . 12.7 , ,
F- .
( F ( a ) < F{e}).
Bound = 9 (.. , F < e ) ;
).
( ) , F{c) 10 > Bound.
( ) , , F ( c ) = 10 F ( a ) 10
( ). ( F [ e ) = 9 < 10 =
F { a ] ) Bound = 10 = F ( a ) .
f, F ( f ) = 11 > Bound ( ).
, F ( e ) 11 ( ). Bound = 1 1 .
, f () = 6 < F { a ) . F-
, F(b) 10.
F- , F ( c ) 10.
Bound = 11 , d,
F(d) = 12 ( ). t.
RBFS.
F- .
:
NewF(

N,

F(N),

Bound)

N , F- F (N).
Bound, N, F- N, NewF, . . ,
, . NewF
12.5.
270

II. Prolog

. 12.7, RBFS
, . 12.2;
F- (
)

12.5. F- RBFS
function NewF( , F(H), Bound)
begin
if F(ll] > Bound then NewF ;= F(N)
else if goal(N) then
else if then HewF := infinity ( )
else
begin
for Hi, N do
if f (N) < F(N} then F(Hi) :- max [ F(N> , f [Nil )
else EMBi) := f (Hi);
N-_ F-;
while F(Ni) Bound and (Hi) < infinity do
begin
Boundl := min( Bound, F- Hi
);

12.

271

F(Ni) := NewF( Nj. [ F[N,J, Boundl);


%, H 2 , ...
F(B:)
end
NewF : = F(Hi)
end
end

RBFS Prolog 12.6. *, 12.1.


rbfs( Path, SiblingNodes, Bound, NewBestFF, Solved, Solution)
RBFS. .
Path. , .
SiblingNodes. , ,
.. Path.
Bound. F-
SiblingNodes.
MewBestFF. F- Bound.
Solved. SiblingNodes
(Solved = yes, , Solved = , Bound, Solved = never,
, ).
S o l u t i o n . , Solved = yes, .
, , , f- F-, .
Node = ( State, G/F/FF)
G S t a t e , F , f ( S t a t e } , a FF (State). , F f-, a FF
F-. rbf s SiblingNodes
Bound NewBest.FF NewF,
12.5.
12.6. ,
( RBFS)
% ,
% , - RBFS.
% , f- 99999
bestfirst! Start, Solution): Solution - Start


bestfirstl Start, Solution) :rbfs( [;, ( (Start, 0/0/0) ], 999S9, _, yes, Solution).
I tbfE ( Path, SiblingNcdes, Bound, NewBestTF, Solved, Solution):
h Path - ,
% SiblingNodes - Path
i Bound - F- SibiingModes

272

li. Prolog e

%
%
%
*
1
%
%
%

NewBestFF - f-
Bound
Solved - yes, no never
Solution - , Solved = yes
: Node - ( State, G/F/FF)
G - State, F - f-
State, FF - f- State

rbfsl Path, [M Node, G/F/FF) | Modes], Bound, FF, no, _)


FF > Bound, !.

:-

rbfs{ Path, [ [ Node, G/F/FF) | _ ] , _, _, yes, [Node | Path]) :F = FF,


% ,
% ; F=FF
goal! Node).
rbfs( _, [ ] , _ , _ , never, _)

:- !.

4 ,
I !

rbfsf Path, ( ( Node, G/F/FF) | Ns], Bound, NewFF, Solved, Sol) :FF =< Bound,
i Bound -
findall; Child/Cost,
[ s[ Node, Child, Cost), not member [ Child, Path)),
Children),
inherit! F, FF, InheritedFF),
% FF
succlist( G, InheritedFF, Children, SuccNodes), %
bestfft Ns, NextBestFF),
% FF
%
mint Bound, NextBestFF, Bound2), !,
rbfsl [Node | Path], SuccNodes, Bound2, NewFF2, Solved2, Sol),
continue(Path, [(Mode,G/F/NewFF2>I Ns], Bound, NewFF, Solved2, Solved, Sol).
I continue) Path, Nodes, Sound, NewFF, ChildSolved, Solved, Solution)
continue! Path, [N | Ns], Bound, NewFF, never. Solved, Sol) :- !,
rbfsl Path, Ns, Bound, NewFF, Solved, Sol).
% N -
continue! _, _, _, _, yes, yes, Sol).
continue( Path, ( N | Ns], Bound, NewFF, no. Solved, Sol) :insert; N, Ns, NewNs), !,
%
%
rbfs{ Path, NewNs, Bound, NewFF, solved, Sol).
succlistt _, _, [] , [] ) .
succlistt GO, InheritedFF, [Node/C | NCs], Nodes) :G is GO +
hi Node, H) ,
F is G + H,
max ( F, InheritedFF, FF) ,
succlistt GO, InheritedFF, NCs, Nodes2J,
insert; ( Node, G/F/FF), Nodes2, Nodes).
inherit! F, FF, FF)
FF > F, !.

:-

I FF-
i , FF-
% F-

inherit; F, FF, 0) .
insert; ( N, G/F/FF), Nodes, I ( N, G/F/FF) I Nodes]) :bestff( Nodes, FF2),
FF =< FF2, !.

12.

273

i n s e r t ! N, [Nl I Ms), [HI


i n s e r t ( N, Ms, NslJ .
bestff(

( N, F/G/FF)

bestff(

[],

99999).

I Nsl))

1 Ms], F F ) .

:% - F F -

i - F F -
% " "

RBFS. -,
,
. , .
IDA*. -, * IDA*,
RBFS
;.

12.8. , . 12.2.
A*, IDA*
RBFS { )?
12.9. , . 12.8. ,
, 1 . , ( ) RBFS. F(b) F(cJ ?

. 12.8. ;

f- ; 1

12.10. F- RBFS
( ). , .
: RBFS ,
f- . , RBFS
F- 8.
12.11. A*, IDA* RBFS " ".
, , ( ).

274

II. Prolog


,

.
.
, , . *, .
*
, ,
.
.
, *, ,
.
* .
,
.
.
IDA* , , , . ,
IDA*, ,
f-,
, , f-, .
RBFS , , , IDA*.
IDA* RBFS . .
" " .
:
;

;
;
A*, IDA*, RBFS;
, ;
;
.


, ,
, * 12.

275

. * , , [116], [133], [171]. R [4.4]


, . [63J.
h h* *. [118]. [70] ,
. IDA* [78]. RBFS
[79]. [133] .
" "
, [44], [56] [102].
,
.
12.3 [34].
,
. ,
.
, .
( ) , , . , , [55].

276

N. Prolog

13


AND/OR

...
13.1. AND/OR
13.2. AND/OR
13.3. AND/OR
13.4. AND/OR

277
281
284
289

AND/OR , . , , , , .. AND/OR,
AND/OR, .

13.1. AND/OR
11 12 , .
. , AND/OR.
. , .
. ,
. 13.1. .
, , . : ,
, . ,
.
, . 13.1, . ,
,
f, . , , f,
. , .

. 13.1. a
z .
f .
AND/OR . 13.2

z, .
1. f.
2. z .
,
.
1. z f, :
1.1. f;
1.2. f .
2. z , :
2.1. ;
2.2. z.
: f . ,
{, 1.1 1.2
2.1 2.2). , ( )
.
AND/ (. 13.2). , , AND . , , . 13.2,
AND/OR. .
AND/OR? , , "".
" ",
.
. AND/OR
, , 278

II. Prolog

. .
AND OR, , - (. 13.3). ,
, AND, , OR. , AND, OR. , AND/OR
, OR. , AND,
AND, , OR, OR.

. 13,2. ,
13.1, AND/OR.
, , , ,
( )
0;

. 13.3. AND/OR: )
, us PI, P2 ...; )
Q, , Q1, Q2. ...

.
AND/OR? , AND. , .
, , .
, , .
OR, ( AND/OR) .
AND, (
AND/OR) .
13, AND/OR

279

. 13.4. , . . , ,
. , , . 13.4, .
)

61

. 13.4. : ) AND/OR, e
d, g h , ,
; ) ) ,
9 8.

. ,
, .
.
AND/OR .
AND/OR , .
, OR, OR. , OR, -.
, AND, AND. , AND,
-.

280

II. Prolog e

AND/OR
:
;

.
( "") ( "") .
, AND/OR.
AND/OR, OR.
AND/OR,
, , AND, , . :
AND, , ,
, .


, .

13.2. AND/OR
13.2.1. AND/OR

(. , 13.1) AND/OR,
, .
OR X-Z, ,
X Z.
AND :
X-Z v i a

X Z, ,
Y.
X-Z ( ), X z .
X-Z
X 7,.
() 0.
( ). , . 13.1, a-z. . 13.5
9. [ a , b , d , f , i , z),
, .

13. AND/OR

281

. 13.5.
(, . 13.1).
AND/OR

13.2.2.
(. 13.6) AXD/OR. , .
, 1, 2 3, , , ( .
). 1. ,
3.
.

. 13.6. .

.
282

II, Prolog

1. 3.
2. 3.
3. 3.
, . , 3, .
(
). ,
, , .

: 3 ( 3) ,
. , ,
, .
, ( , ),
, .
, , ,
:
" 3";
^ .
:
, . .
1. 1 3 .
2. 1 3 .
3. 3 3.
1 3, , , 2. 1 3 a, b .
1 3 , , .
1. 1 2 .
2. 1 3 .
3. 2 3 .
2 ( ). 2, b . 1 3, ( ). 1
.
1 2 , .
1. 1 3 .
2. 1 2 .
3. 3 2 .

13.2.3. AND/OR
, , , AND/OR.
, , ,
13. AND/OR

283

. (
,
: .) , , ,
. ,
: . ,
?. , Qi, Q2, ... (. 13.7).
, Qi
RH, R12, ... . AND/OR (. 13.7) ,
. .
, , ,
Qi, i, , Qi
. ,
Qi, Q;, ... . , OR.
i Q; , , , ,
. Qi , , Rj.1, Ri?, . . . . AND. ,
; ,
, . , , . , ,
. ,
: , ,
, .

. 13.7.
AND/OR;

13.3. AND/OR
, .
AND/OR.

284

II. Prolog

AND/OR
Prolog Prolog. , ,
Prolog , AND/OR. , AND/OR, . 13.4 ( ) :
:- . % - OR , b
:- .
b :- d, e. % b - AND , d
: - h.
:- 1, .
; :- h, i .
d, . h, I d, h
, , :
?- .

Prolog ,
. 13.4, "yes", , . 13.4, .
AND/OR
. .
"yes" "", . , ,
, .
,
.
AND/OR , ,
Prolog .
.
AND/OR.
AND/OR .
, "
>". , ,
OR, :

> or:[b,c].

, "
>" " : " , ,
:
:- [ 600, xfx,
:- [ 500, xfx,

>) .
:) .

AMD/OR, . 13.4,
:

> or:[b,c].
b
> and:[d,e].

> and: ! f, g] .
e
> or:[h].
t ---> o r : t h , i l .
goal( d ) . g o a l ( g) . goal[ h ) .

AND/OR
.

13. AND/OR

285

, N, .
1. , .
2. OR,
( ,
, ).
3. N AND,
( , ,
, ).
, , .
:
solve( Mode) :goal( Node).
solve( Bode) :Node
> or:Nodes,
% Node - OR
member! Nodel, Nodes), % Model Node
solve( Nodel).
solve( Node) :Node
> and:Nodes,
solveall( Nodes).
solveall([]).

$ Mode - AND
% Node

solveall( [Node 1 Nodes]) :solve( Node),


solveall( Nodes).
member .
.
;
,
AND/OR ( ).

, .
solve, :
solve! Node, SoliitionTree)
. .
1. Mode ,
Ncde.
2. Node OR, :
Node
> Subtree
Subtree Node.
3. Node AND,
Node
> and;Subtrees
S u b t r e e s Node.
, AND/OR (. . 13.4)
:

> b
> and:[d, e
> h]

286

II. Prolog

, s o l v e .
solve , ;
, s o l v e . 13.1.
show . , (. . 13.4) show :
-> b > d
>h
13.1 .
,

. , solve:
solve( Node, SolutionTree, MaxDepth)
, , Mode , , a S c l u t i o n T r e e , MaxDepth. , MaxDepth . , MaxDepth = 0, ;
, KaxDeoth > 0, Node
, MaxDepth - 1.
, 13.1.
, , s o l v e , ,
solve! Mode, Node
> Tree, MaxDepth! :MaKDepth > 0,
Node
> or:Nodes,
% Node - OR
raembec( Model, Nodes!,
% Model Node
Depthl is MaxDepth - 1,
%
solve[ Model, Tree, D e p t h l ) . % ,

, .
, ,
, . , 0, 1, 2 .. .
13.1, UND/OR.
. s o l v e ,
show . show ,

% AND/OR
% solve( Node, S o l u t i o n T r e e ) :
i
Hode AND/OR
:- ( 500, xfx, : ) .
:- { 600, xfx,
> ! .
solve( Mode, Kode)

:-

%
I Node

goal(Hode).
solvet Mode, Node
> Tree)
Mode
> or:Kodes,

:% Node - OR

13. AND/OR

287

member; Nodel, Modes),


solve( Nodel, Tree).

$ Nodel Node

solve( Mode, Node


> and:Trees)
Node
> and:Nodes,
solveall{ Nodes, Trees).

:% Node - AMD
% Node

i solveall{ (Nodel,Node2, . . . ] , [SolutionTreel,SolutionTcee2, ...])


sclvealK [], [) ) .
solvealK [Node I Modes] , [Tree ] Trees] ) :solve[ Node, Tree),
solveall( Modes, Trees).
%
show(Tree)

:-

show(Tree y 0), !.

% 0

% show( Tree, ) ;
show! Node
> Tree, H) :- !,
write(Node), write('
> '),
HI is H + 7,
show( Tree, HI).
show( and: IT], H) :- ],
% AMD
show IT,H) .
showf and:[T|Ts], H)
show{ T, ) ,
tab(H),
3how{ and;Ts, H ) .

:-

!,

% AND

show( Node, H) :write(Node), nl-

(. 11), ,
. , .

13.1. AND/OR , .
13.2. Prolog AND/OR , .
13.3. - , ,
AND/OR.
AND/OR, AND/OR.

288

II. Prolog

13.4. AND/OR
13.4.1.
, , AND/OR ,
- .
-
.
, .
, ,
, AND/OR.
, *.
*,
, 12.
,
AND/OR. AND/OR, . , AND/OR (. . 13.4)
:

> cr:[b/l,c/3].

> and: [ d / l , e / l ] .

> and: [ f / 2 , g / l ] .

> or:[h/S].
f
> or:[h/2,i/3].
g o a l [ d ) . goa] ( g ) . g o a l [ h) .


. ,
. . 13.4.
AND/OR
. .
, (
) AND /OR h.
. , .
, AND/OR ;
.

" " .
h , ? , , ,
( )?
N {) . () h(N]. ,
h ,
; , . , . 13.8,
N OR
:
() = rain Jccst [H,Ni)+H(Ui) J
13. AND/OR

289

c o s t {N,:) N N:.
:
, { " N"),
.

. 13.8. , . , AND /OR


N AND :
()

2^{cost [, N J + H t N i ! )
1

- .
, , . , N ,
N c o s t ( M, N); :
F(N)=cost(M,N}+H(H)
, N, a Hi, N 2 , ... , :
F(N) = cost( M, N} + minF(Hi), N - OR
F(N) - CO3t( M, N) + .ZrfF(Ki), N - AND
1

S , ,
() 0. , h
AND/OR 0 ,
F[5) ( , ).
OR.
, F- . . 13.4 AND/OR.
290

II. Prolog

, ,
. . 13.9 ,
. , h = 0 . ,
. 13.9, F-
(, ). . 13.9.

. 13.9. AND/OR
( h = 0) , . 13.4

13, AND/OR

291

( ) , . OR,
. F(b) = 1
< 3 = F ( c ) , . , . , .
1. F- b , , .
2. , .
, b F (), F{b) < 3 = F ( c ) . , d ( ), F- b 3.
, , to, . ,
d , ,
. F(b) = 9 > 3,
. , ,
h . . F ()
9, F(b) = . , ,
. ,
( h ) . ,
, .. , . 13.4, .

13.4.2.
, , ,
13.2. , , .
. 13.10, .
, , .
:
()
() .
:
, (
),
.
, ,
. :
leaf solvedleaf tree solvedtree

, .
.
F- .
AND/OR, .
292

II. Prolog

13, AND/OR

293


F-. . . , 13.2, :
ando( Mode, SolutionTree)
Mode . (
), , , . , h, . ,
h. ,
, 12 ( *). ,
COST(N) .
N AND/OR h(N) < COST(N),
. h , . ,
, h = 0 . ,
.
13,2. AND/OR
/*

AND/OR

. ,
,
,
:
tree( Node, F, , SubTrees)

leaf( Mode, F, )

solvedtree( Node, F, SubTrees)

solvedleaf( Hode, F)

- , Mode
F = + ,

-
Kode

SubTrees ;
{1) ;
(2) [ )
F-
:- ( 500, xfx, : ) .
:-

( 600, xfx,

> ) .

andor( Mode, SolutionTree) :expand! leaf ( Node, 0, 0 ) , 9999, SolutionTree, yes). * ,


% F- 9999
% expand! Tree, Bound, NewTree, Solved)

Tree Bound, NewTree,


%
" " Solved

294

II. Prolog

% 1. Bound
expand( Tree, Bound, Tree, no)
f[ Tree, F ) ,

F > Bound,

:-

!.

% Bound

% F =< Bound
% 2.
expand! leaf! Node, F, C ) , _, solvedleaf[ Node, F) , yes)
goal(Node), !.

:-

% . -
expand( leaf( Mode, F, C ) , Bound, NewTree, Solved) :{ expandnode[ Node, C, Treel), ! ,
expand! Treel, Bound, NewTree, Solved);
Solved = never, ! ) ,
% ,
% 4.
expand! tree[ Mode, F, , SubTrees), Bound, NewTree, Solved) :Boundl is Bound - C,
expandlist [ SubTrees, Boundl, NewSubs, Solvedl) ,
continuef Solvedl, Node, C, NewSubs, Bound, NewTree, Solved),
* expandlist! Trees, Bound, NewTrees, Solved)
%
Trees Bound,
%
NewTrees, " " Solved
expandlist! Trees, Bound, NewTrees, Solved) :selecttree{ Trees, Tree, OtherTrees, Bound, Boundl),
expand! Tree, Boundl, NewTree, Solvedl),
combine ! OtherTrees, NewTree, Solvedl, NewTrees, Solved).
* continue
%
continue! yes. Node, , SubTrees, _, solvedtree(Node,F,SubTrees}, yes) :backup[ SubTrees, K ) , F is t H, !.
continue! never, _, _, _, _, _, never)

:-

!.

continue; no. Node, C, SubTrees, Bound, NewTree, Solved) :backup! SubTrees, H), F is + H, !,
expand( tree( Node, F, C, SubTrees), Bound, MewTree, Solved).
% combine
combine! or:_, Tree, yes, Tree, yes) :- !.

% OR

combine! or:Trees, Tree, no, or:HewTrees, no) :insert! Tree, Trees, NewTrees}, !
% OR
combine; or:[] r _, never,

, never)

:- !.

combine( or:Trees, _, never, or:Trees, no)

% ,
% ,
:- !. % ,
%

combine( and:Trees, Tree, yes, and:[Tree I Trees], yes) :allsolved(Trees), !.


% AND
combine! and:__, _, never, _, never) :- !.

13. AND/OR

% AND
%

295

combine( and:Trees, Tree, YesNo, and;NewTrees, no)


insert( Tree, Trees, NewTrees), !.

:-

% AND

% expandnode ,
expandnode) Node, , tree ( Node, F, , Op:SubTreesl)
Node
> Op:Successors,
evaluate( Successors, SubTrees],
backup) Op:SubTrees, , F is + H.
evaluate) [], []).

:-

evaluate) [Node/ INodesCosts], Trees) :h[ Node, HI, F is + ,


evaluate( NodesCosts, Treesl),
insert! leaf) Node, F, C ) , Treesl, Trees).
% allsolved ,

allsolved) []) .
allsolved([Tree|Trees])
solved(Tree),
allsolved(Treesl.

:-

solved( solvedtree(_>_>_))solved ( solvedleaf !_*_) ) .


f ( Tree, F) :arg) 2, Tree, F ) , !.

% F- .
% F - Tree

% insert) Tree, Trees, NewTrees): Tree Trees,


%
NewTrees
insert( , [], [])

:-

!.

insert) , [TUTS], [,Tl |TS])


solved(Tl}, !.
insert) T, [TllTs], [TllTsl])
solved(T),
insert) T, Ts, Tsl), ! .

::-

insert) T, [Tl|Ts], [T,Tl|Ts]} :f( T, F) , f(Tl, Fl), F =< Fl, !.


insertl T, [Tl|Ts], [Tl|Tsl]J
insert[ T, T S , Tsl).

:-

% backup F-
% AND/OR
backup! or:[Treel_), F)
t ( Tree, F) , ! .
backup) and: [], 0)

:-

:- ! .

backup! and:[Treel|Trees], F)
f( Treel, Fl),
backup) and:Trees, F2) ,
F is Fl + F2 F
!.
backup) Tree, F)

296

% OR

:-

:-

. Prolog

f( Tree, F).
%
%
%
%

s e l e c t r e e ( Trees, BestTree, o t h e r T r e e s , Bound, Boundl);


OtberTrees - Trees AWD/OR , BestTree;
Bound - Trees,
Bouncll - BestTree

s e l e c t t r e e ( Op:[Tree], Tree, 0 p : [ ] , Bound, Bound! :- !.


* ,
I
s e l e c t t r e e ( O p : [ T r e e | T r e e s ] , Tree, Op:Trees, Bound, Boundl)
bacfcup( Op:Trees, F) ,
[ Op = or, !, min{ Bound, F, Boundl);
Op = and, Boundl is Bcund - F ) .
min( A, a, A)

:-

:- A < , ! .

min( , , } .
, 13.2, :
expand( Tree, Bound, Treel, Solved)
Tree Bound "" , a Treel Solved "". .
Tree. , .
Bound. F-,
Tree.
Solved. ,
.
Solved = yes. Tree
, T r e e l .
Solved no. Tree T r e e l
, F- T r e e l Bound
, F- Bound.
Solved = never. Tree .
, T r e e l , Tree Bound, ( Solved = never).

expandlist( Trees, Bound, Treesl, Solved)


expand. expand, Bound
, a Solved ,
("yes", "no" "never").
( AND OR), .
Trees - or:[Tl, T 2 , . . . ] Trees - and:[Ti, T2, . . . ]
e x p a n d l i s t Trees
( F-). Trees. Boundl, Boundl Bound, a
Trees. Trees
OR, Boundl Bound F- Trees (F-). Trees
AND, Boundl Bound F- 13, AND/OR

297

Trees. T r e e s l , Solved. , Solved = no, T r e e s l


Trees Trees, Boundl. Solved = yes, T r e e s l
Trees ( Bound). Solved = never, T r e e s l
.
c o n t i n u e , ,
, ,
e x p a n d l i s t . ,

"never" ( , , ).
,
combine( OtherTrees, NewTree, Solvedl, MewTrees, Solved)
, e x p a n d l i s t . NewTree , e x p a n d l i s t , OtherTrees ,
, a Solvedl " " NewTree. combine ,
Solvedl , AND OR. ,
combine; or:_, Tree, yes, Tree, yes).
, , OR,
Tree, Tree. combine.
,
show (. 13,1). .

13.4.3.
AND/OR
,
andor, 13.2. ,
:
s( Cityl, City2, D)
, C i t y l City2,
D. , ,
key( Cityl - City2, City3)
: C i t y l City2,
, City3 (City3 C i t y l City2). , , . 13.1,
f , .
key; -, ) . key ( a-z, } ,
. X 2, .
1. Yl, Y2
X Z,

Z Y1,
298

II. Prolog

Z Y2,

. *

2. X Z,
Y X, , Y Z.
, ,
, .
1. X-Z,
X Z.
2. X-Z v i a Y.
X Y.
"via" , " - " , , "
>". AND/OR
:
: - ( 560, xfx, v i a ) .
% -3,
$ X Z,- 0
X-Z
> or:ProblemList
:- bagof( { X-Z via Y)/0, key( X-Z, Y!, ProblemList}, !.
i X-Z,
X-Z
> or:ProblemList
:- bagof{ [ Y-ZJ/D, s( X, Y, 0 ) , ProblemList).
h "via" AMD
X-Z via Y > and: [ [ X-Y) /0, ( Y-Z) /0] .
goal( X-X),
i X X

h , ,
.

13.4. , AND/OR :

> or: [b/l,c/2l .


b ---> a n d : [ d / I , e / l ] .
- - - > and:[e/l,f/l].
d
> or: (h/G] .
e ---> or:[h/2].
f > OK:[h/4,i/2].
goal(h), goal(i).

. ,
*, , h- , -, , h i
0. h(c) h ( f ) ,
*.
13.5.
show?( SolutionTree)
, andor {. 13.2). , show (. 13.1), show2 show .

w r i t e ( Node) show
writenode[ Node, )
Node
, 13. AND/OR

299

Node . , .

AND/OR
. ,
.
.
, AND/OR : AND OR.
. .
AND/OR
.
, AND/OR,
.
.
- .
,

. .
Prolog ,
AND/OR.
:
AND/OR;
AND, OR;
AND, OR;

, ;
, ;
AND/OR, ;
AND/OR;
AND/OR;
AND/OR.


AND/OR
, .

[151]. AND/OR Prolog.
AND/OR AND/OR
[115], [116]. AND/OR , *.
- ( ) ; [118] .
300

II. Prolog

14

...
14.1.
14.2.
14.3.
14.4.
14.5.


CLP CLP(R)
CLP

CLP
CLP(FD)

301
306
310
317
321

,
, . , . . (Constraint Logic Programming CLP)
. CLP
, Prolog.
CLP
, .

14.1.

14.1.1.
, .
:

1) ;
2) , ;
3) , .
:
, ,
.

, .
, .
, , , , , ,
.
, , . , : ; ; ; ;
..
. , , , , d, 2, 3, 5 4 . : , b
d (. 14.1). ,
, , Td ,
Tf . , 0.

. 14.1.
, , , d
, .
: , , , Td, Tf.
: .
:
+ 2 . , 2 , ;
+ 2 5 ;
+ 3 < Td. d;
t 5 i . Tf;
Td + 4 < Tf. d Tf.
: Tf.
,
.
:
=
-

2 <, <
= 5
Tf - 9

, , 2 4.

302

II. Prolog

14.1.2.
,
. ,
. p[X,Y) X Y , (X,Y) (Y,X).
. .
.
. , , ,
( ).
X Y, Dx Dy.
, X Y p(X,Y).
(X,Y) , , X Dx Y Dy,
(X, Y), (X, Y) , Dx, Dy,
Dx. [X, Y) .
, X Y, 0 10 , :
Dx = 0 . . 1 0 ,

Dy = 0 , , 1 0

, p(X,Y) : X + 4 < Y. (X,Y) . , X = 7


Dy Y, p[7,Y). (X,Y)
, Dx Dx = 0 . . 6 .
, (Y,X) , Dy
: Dy = 4. .10. Dx
, , , - .
Dx . , , (Z,X), Z,
Dx 2.. , , ,'Z,X) , Dz. , , , , ,
. , ,
. , , .
1. ; ,
.
2. , .
, ,
, , ,
. ,
. 14.

303

.
, - . ,
. ,
.. ,
- ,
. , .
, .
.
,
. , 0 10. . 14.2
, . 14.1 . , " S t a r t " , 0. .10.
. 1 (.),
2. .10. (Td.Tb), Td
5. .10, .. S . ,
Tf = 9. , ,
, 2. . 4.
14.1.

0. .10

Start

1
2
3

(,)

(Td,Tf)
(Tb,Td)

Td

Tf

0. .10

0. .10

0. .10

0. .10

2. .10

(Td,Tb)

5. .10

(Tf,Td)

(Ta,Tb)

, . 1 0
5. .6

2 . .3
0. . 1

(,)

2. .

(TcTf)

2. . 5

. 14.2.

304

II. Prolog

,
.
,
. . , , . , , 23, ",
".

14.1. , . ?
14.2. , . 14.1, Tf 9.

14.1.3. Prolog

Prolog . Prolog
, . . Prolog.
, ,
,
. Prolog "" CLP , . ,
Prolog ,
. CLP, ,
, .
.
CurrConstr. .
. Prolog . Constr Constr CurrConstr ,
NewConstr. ,
,
MewConstr. : ) ,
NewConstr ,
; ) ,
NewCor.str ,
. , , X < 3 X < 2, , X < 2.
, . , .

14,

305

CLP ,
. CLP CLPfAO, X . ,
CLP(R) , ,
. CLP(Z),
, : CLP(Z) ,
CLP(Q) , CLP(B)
CLP(FD) .
.
, CLP(R) ,
.
, .
CLP(R), CLP(Q)
CLP(FD), CLP
SICStus Prolog {. " " ).

14.2. CLP
CLP(R)
:
?- 1 + = 5.
Prolog , Prolog "". , X , "+" , X = 4 . "=" is , CLP(R) .
( SICStus
Prolog) CLP(R) :
?- { 1 + X ~ 5 !. %
X = 4


,
,
.
Prolog , .
. Prolog, ,
. Cl, C2
:
<; Cl, C2, )
:
Exprl Operator Expr2

Exprl, 2 .
, CLP(R), , s i n ; X ) . Operator
, .
=. .
=\=. .
<, =<, >, >=. .
306

II. Prolog


, , ,
Prolog.
Prolog
i s , , :
convert( C e n t i g r a d e , Fahrenheit) : Centigrade is (Fahrenheit - 32)*5/9.


35 , ,
, is ,
, . , , ,
,
, .
CLP(R),
, ,
, .
c o n v e r t ( C e n t i g r a d e , Fahrenheit) : { Centigrade [Fahrenheit - 321*5/9 ).
?- c o n v e r t ( 35, F ) .
F = 95
?- c o n v e r t ( C, 95) .
- 35

CLP{R) ,
:
?- convert(, F).
t F = 32.0 + 1.8* }

, , : F ,
. , , CLP,
c o n v e r t .
, , .
.
? - ( 3* - 2*Y = 6 , 2*Y = X } .
X - 3.0
- 1.5
? - { 1 = < - 2 , Z =< 6 - , 2 + 1 = 2 ) .
i . o
{X > = 3 . 0 }
(X = < 5 . 0 }

CLP(R)
,
, .
CLP(R):
minimize! Expr)
maximize( Expr]
Expr , .
, , , .

14.

307

?X
?X
?X
Y
Z

{
{
=
{
=
=

X =< 5 } , m a x i m i z e ( X ) .
5.0
X =< 5, 2 =< X } , m i n i m i z e ( 2*X + 3 ) .
2.0
X > - 2 , Y > = 2 , Y - < X + l , 2*Y - < B-X, S - 2*X + 3 + Y b
4.0
2.0
14.0

?- { X =< b ) ,
no

maximize(Z).

minimize! X).

X , .
CLP(R) ( )
( ) :
sup( Expr, MaxVal)
inf( Expr, MinVal)

Expr , a MaxVal MinVal ,


~ , .
maximize/1 minimize/1,
, ,
? - { 2 =< X =< 5 f , i n f ( X, M i n ) , s u p ( X, ] .
= 5 . 0
Min - 2 . 0
(X > - 2.DJ
{X = < 5 . 0 }
? - (X >-2, Y > = 2 , Y - < X + l , 2*Y =< 8-X, 2 = 2*X + 3 * Y } ,
s u p ( Z, Max) , mi. {Z, M i n ) , m a i t i r a i z e ( Z) .
X = 4.0
Y = 2.0
Z = 14.0
- 1 4 . 0
Min = 1 0 . 0

, , d CLP(R) :
?- ( + 2< ,
+ 2 -< ,
+ 3 < Td,
+ 5 =< Tf,
Td + 4 = < Tf )i
minimize! Tf),
= 0.0
Tb = 2.0
Td = 5.0
Tf - 9.0
{ -< 4.0}
( >- 2.0}

%
%
%
%
%



d
Tf
Tf

,
Prolog.
fib{ N, F) - : F ( 0 ) - l , F{1)1, F{2}-2, F(3)3, F(4}=5 .. N > 1, F(N) = F(N-l) + F (N-2).
fib/2 Prolog.
fib( N, F) :N=0, F=l
H-i, F-i

308

II. Prolog

>1,
Ml i s H-l, f i b ( N l , F l ) ,
N2 is H-2, fib(N2,F2) ,
F i s Fl + F2.

, :
?- fib(6, FJ .
F=13
, :
?- f i b ( , 13) .

, N > 1 . , CLP(R),
, ,
f i b ( N, ) :{ N = , - 13
'( - 1 , F-1J
t > 1 , F = F 1 + F 2 , N1 - - 1 , N2 = N - 2 ) ,
fib( HI, F l ) ,
fib( N2, F2>.

. , F
N:
?- f i b ( N, 13) .
N=6

, :
?- fib{ H, 4}.

F1 F2, Fl + F2 = 4.
F1 F2 , 4. , 4,
4. , . , ,
,
f i b , . , N
F(N) N. N1, F1, N2 F2
: F1 >= N1, F2 >= N2.
f i b ,
f i b ( S, F) : { > 1, F = F1+F2, 1 = -1, N2 = -2,
Fl >= HI, F2 >- N2), %
fib( N1, Fl),
fib( N2, FZ).

,
:
? - > [ , <)) .

f i b
F F = 4:
4 - F - Fl + FZ =
F l ' + F2' + F2 =
F l " + F 2 ' ' + F 2 ' + F2

14.

309

.
, , .
CLP(Q),
CLP(R). , CLP(R) , Q , .. , .
. Q R ( ) ,
, .
.
?-

( X=2*Y,

Y=l-X }.

CLP(Q) : X = 2/3, Y = 1/3.


CLP(R) :
0.666666666,Y - 0.333333333.

14.3. , " ? - f i b ( N, 4} .", ,


, .

14.3. CLP
, , .
Ti, ..., -.
Di, .... Dn .
, , :
prec( Ti, Tj)

, Ti
, Tj.
., .
:
(
).
, ,
.
. ,
: , . CLP :
, S1, ..., Sn, ,
, ?1, ,,., .
. , ,
310

II. Prolog

. , ,
.
,
. ,
, , prec ( a, b ) . , Da,
b Sa Sb. , Sa Sb :
{ + Da -< s b }

, , ;
0, FinTime :
{ Si >- 0, Si + Di =< FinTime }


:
tasks(

[Taskl/Durationl, Task2/Duration2,

...])


.
prec( Taskl, Task2)
Taskl
Task.2.
resource( Task, [ Procl, Proc2, . . . J )
, Task
Procl, 2, ... . ,
, 12 ( 12.3),
. . 12 ,
, .
. . 14.1. , 14.1, .
, .
schedule[ Schedule, FinTime)
Schedule ,
t a s k s p r e c , a FinTime .
:
S c h e d u l e = [ T a s k l / S t a r t l / D u r a t i o n l , Tak2 / S t a r t 2 / D u r a t i o n 2 ,

...]

14.1.

I CLP
schedule! Schedule, FinTime) :tasks( TasksDurs),
precedence_constr{ TasksDurs, Schedule, FinTime), *
%
minimize! FinTime).
precedence_constr( [1, [], FinTime).
14.

311

precedence_constr( [T/D ] TDs], [T/Start/D I Rest], FinTime) :{ Start >- 0,


% 0
Start + D =< FinTime},
% FinTime
precedence_constr( TDs, Rest, FinTime),
prec_constr( T/Start/D, Rest).
prec_constt[ _, []).
prec_constr( T/S/D, [T1/S1/D1 | Rest])
( prec( T, Tl;, !, t S+D -< SI)
v
precl Tl, T ) , !, { Sl+Dl =< S)

:-

true ),
p r e c _ c o n s t r [ T/S/D, R e s t ) .
4 ,
tasks; [ tl/5, t2/7, t3/10, t4/2, t5/9]) .
%
prec[ t l , t 2 ) .

prec( tl,

t4),

prec ( t 2 , t 3 ) .

prec[ t4, t 5 ) .

schedule, , .
1. , .
2. .
,
, CLP(R)
.

precedence_constr( TasksDuraticns, Schedule, FinTime)



Schedule FinTime.

prec_constr[ Task/Start/Duration, RestofSchedule)

S t a r t Task
RestOfSchedule ,
.
, 14.1, . :
?- schedule; Schedule, FinTime).
FinTime = 22,
Schedule = [tl/0/5,t2/5/7;t3/12/10,t4/S4/2,tS/S5/9],
(SS =< 13)
(S4 >= 5}
(S4-S5 =< -2)

t4 t5
. S4 S5 t4 t5
. ( t l , t2 t3) ,
.

312

II. Prolog


. , , 12 (.
12.3, . 12.6), .
, ,
.
.
1. .
2. .
,
, 14.1. . . ,
Ti , Pi,
.
:
Schedule = [ T a s k l / F r o c l / S t a r t l / D u r l , Tcisk2/Proc2/Start2/Dur2,

...]

Procl , Taskl, S t a r t l Taskl Durl . , 14.2,


14.1.
schedule( BestSchedule, BestTime)
BestTime. ,
, :
precederice^consti: ( TasksDurations, Schedule, FinTimei
14.2. CLP(R)

% CLP
schedulef BestSchedule, EestTime)
:tasks{ TasksDurs),
precedence_constr< TasksDurs, Schedule, FinTime), % ,
i
initialise bound,
%
%
assign processors! Schedule, FinTime),

minimize( FinTime),
update_bound( Schedule, FinTime),
fail
,
bestsofar( BestEchedule, EestTime).

% precerfence_constr( TasksDurs, Schedule, FinTime):


%
,
I
, Schedule,

, .
Z
FinTime
4
,
precedence_constr( [], [], FinTime).
precedente_constr[ [T/D | TDs),
( Start >= 0,
Start + D =< FinTime },
precedence_constr( TDs, Rest,

[T/Proc/Start/D [ Rest], FinTime) :% 0


k FinTime
FinTime;,

14.

313

prec_constr( T/Froc/Start/D, Rest).


prec_constr( _, [}).
prec_constr[ T/P/S/D, [T1/P1/S1/D1 | Rest])
( prec( T, Tl), !, { S+D =< SI}

:-

( Tl, T ) , !, { Sl+Dl =< S}


true ),
prec_constr( T/P/S/D, Rest).
% assign_prccessors( Schedule, FinTime}:
%
Schedule
assign_processors( [] , FinTime).
assign_processoj:s { [/P/S/D | Rest], FinTime) :assign_processors( Rest, FinTime),
resource! T, Processors),
%
% Processors
member( P, Processors),
%
resource_coristr ( T/P/S/D, Rest},
%
bestsofar( _, BestTimeSoFar),
( FinTime < BestTimeSoFar }.
%
%
% resource^corsstr ( ScheduledTask, TaskList) :
,
%
ScheduledTask TaskList
resource_constr( _, []).
resource^constr( Task, [Taskl | Rest]) :no_conflict( Task, Taskl),
resouirce_constr ( Task, Rest).
no_conflict[ T/P/S/D, T1/P1/S1/D1]

:-

P \== PI, !

prec{ T, Tl), !

<t

prec{ Tl, T ) , 1

S+D =< sl

% ;

Sl+Dl =< S} .
initialise_bound : retract(bestsofar(_,_)), fail
assert! bestsofar{ duraiw_scheo:ule, 9999) ) . % ,
i 9999
update_bound( Schedule, FinTime!:
%

apdate_bound[ Schedule, FinTime) :retract( bestsofar( _ , _ ) ) , !,
asserts bestsofar( Schedule, FinTime]).
% , -
tasks! [tl/4,t2/2,t3/2, t4/20, t5/20, t6/ll, t7/ll]).

314

!1. Prolog

[ t l , f i ) .
prec{ t 2 , t 6 ) .

prec( t l , t 5 ) .
prec( t 3 , t 5 ) .

prect t2, t 4 ) .
p r e c ( t 3 , t&}.

prec ( t 2 , t 5 ) .
prec< t 3 , tl).

i r e s o u r c e ) Task, P r o c e s s o r s ) :
%
P r o c e s s o r s , Task

r e s o u r c e ) _,
.

[1,2,3]).

% ,
%

14.1.
.
.
, . , .
, , , .
, 14.2,
. , . ( ,
f a i l ) . . , (
b e s t s o f a r ) .
. , , .
, 14.2, . assign_jprocessors
.
, , , . . ,
- ,
.
, FinTime
, . :
( FinTime < BestTimeSoFar }
, .
, . , FinTime
< BestTimeSoFar , ;
.. ,
, . ,
14.

315

. , , ,
, . ,
.
, - .
, BestTimeSoFar, .
, , , . , , . , - ,
.
14.2
(. . 12.6),
. ,
, , .
?- schedule( Schedule, FinTime).
FinTime = 24
Schedule = I tl/3/0/4,12/2/0/2,13/1/0/2, t4/3/4/20,
t5/2/4/20, 16/1/2/11,17/1/13/11)

t l , , t2 2, 0 .. . , - . ,
, .
, , , 1 t7 t6 1 2. ,
r e s o u r c e . , , ,
.
, , , , ,
.

14.4. , 14.1. , , . ?
14.5. , 14.2, ( b e s t s o f a r ) ,
.
,
. , , , , , . , ,
316

II. Prolog


( ).

. ,
.

14.4.
CLP(R)
. ,
. , , , .
, ,
, .
. , .
.
CLP(R) ,
, ,
, ,
, .
.

. CLP
. , , ,
, CLP.

, , .
-.
, CLP(R)
, .
. 14.3 , , .
CLP(R) .
R , 1 2. ,
, V I (
). (V,I). :
resistor! (VI,II), (V2,l2), R ) :{ II = -12, V1-V2 = I1*R }.
, , 14.3. . , ,
, :
i [ Terminall, Terminal2, . . . ] )

14.

317

. 14.3.
.- ) ;
) ; ) ; %) ; )

14.3.
% , CLP(R)
% resistor! Tl, 2, R ) :
% R - ; Tl, T2 -
resistor! [VI,II), <V2,I2], R)
{ II - -12, V1-V2 - I1*R ) .

:-

% diode( Tl, 2) :
%
Tl, T2 - .
%
1 2
diode! (VI,II], (V2,I2) )
{ II + 12 - 0 ) ,
( II > 0, VI - V2

:-

II - , VI -< 42).
battery( (VI,II), (V2,I2), Voltage) :{ II + 12 - 0, Voltage = VI - V2 ).
% conn( [TlrT2, . . . ] ) :
% Tl, T2, ... .
I ,

318

!!. Prolog

conn( Terminals)
:conn( Terminals, 0>.
connf [ (V,I) ] , Sum)
(

Sum

:-

+ 1 = 0 } .

c o n n i [ ( V I , I I ) , < V 2 , I 2 ) I R e s t ] , Sum)
{ VI - V2, S u m l ^ Sum + I I ) ,
c o n n f [ (V2, 1 2 ) [ R e s t ] , S u m l ) .

,
.
.
. 14.4. , ,
CLP(R). . 14.4, . ,
, . ,
c i r c u i t _ a . 14.4, 21 . ""
. 2 0,
10 , (
c i r c u i t _ a ) .
, ,
21 6 , 1 .
?-circuit_a[
Rl = i . 0
R2 - 6 . 0

Rl,

R2,

[6,1)).

(. . 14.4, ).
, "" 35, 10 .
? - circuit_b<10, _ , _ , _ , _ , 51, 52).
51 = ( 7.3442553191494, 0.0425531914893617)
52 = ( 5.212765957446809, -0.0425531914 893617)

, R5 7,340
5,213 , 0,04255 .

14.6. , . 14.4. . , . 14.4, , R5.


517 .

14.

319

circuit a(R1,R2, T21} :T2 = (0,J,


battery! T1.T2, 10),
resistor! Til,T12.R1),
resistor! T21,T22,R2),
conn([T1,T11]),
conn{[T12,T21]),
conn([T2, T22J).

% 2
% 10

32
circuit [ U, 11, 21, 31, 41, 51, TS2):
2 = (0,_>,
battery! T1,T2,U),
resistor(T11,1-12,5),
resiBtor(T21,T22, 10),
resistor( T31, T32, 15),
reEistor(T41,T42,10),
resistor(T51,T52, 50),
([1,11,21]),
conn([T12,T31,T51}),
conn([T22,T41,T52]),
conn{[T2,T32,T42]).

% 2 0
%R1 = 5
%R2 = 10
%R3= 15
%R4=10
% R5 = 50

. 14.4.

320

II. Prolog

14.5. CLP
- CLP(FD)
CLP, ,
. CLP(FD) SICStus Prolog.
,
.

.
in/2, :
in Set
Set .
( I n t e g e r ! , Ir_tegcr2, , , . }, .
Terml, .Term2. Terml Term2.
S e t l \/ Set2. S e t l Set2.
S e t l A Set2. S e t l Set2.
\ S e t l . S e t l .
:
Expl Relation 2
Expl 2 , a R e l a t i o n .
#=. .
#\=. .
#<. .
#>. .
#=<, .
#>=. .
:
?X
X
Y
Z
?X

X
#<
in
in
in
X
in

i n 1 . . 5 , Y i n 0. ,
Y, Z #= X+Y+1.
I..3
2..4
3..7
i n 1 . . 5 , X i n S{4} .
( 1 . . 3 ) \/ {5}

iridomain (X)
X, , :
?- X in 1. . 3 ,
X = 1;
X = 2;
X = 3

i n d o m a i n (X) .

,
.
domain! L, Min, Max] . ,
L Min. .Max,

14,

321

a l l _ d i f f e t e n t { L). , L
.
l a b e l i n g ( Options, L) . L. Cp-cior.s
, ,
{"labelling"; ) L.
Options = [ ], ;
,
.
, . .
?- d o m a i n !
- 1 , -1;

[X, Y ] ,

1,

2),

-1, Y-2;
-2, Y-1;
= 2 , Y=2
? - L - [ , ] , d o m a i n ! L,
L- [ 1 , 2 ] , X - 1, Y = 2;
L = [ 2 , 1 ] , X = 2, Y = 1

labeling!

1,

2),

[],

[X,Y]J.

all_different[

L),

labeling!

[],

L).

.
:
DONALD
+ GERALD
ROBERT


,
. 14.4 CLP(FD) . .
?- solve! N1, N2, ).
N1 - [ 5 , 2 , 6 , 4 , 8 , 5 ]
N2 = [ 1 , 9 , 7 , 4 , S , 5 ]
N3 = [ " 7 , 2 , 3 , 5 , 7 , 0]

14.4, CLP(FD)
% DONALD+GERALD=ROBERT CLP(FD)
SOlvet [D, , , A, L,D] , [G, E,R,A,L,D] , [R, 0, B, E, R, ] )
:Vars = [ D , O , N , A , L , G , E , R , , ] , % ,
d o m a i n ! V a r s , 0, 9) ,
%
a l l _ d i f f e r e n t ( Vars),
% -
1000CO*D + 10000*0 + 1000*N + 100*A + 10*L + D +
100D00*G + 10000*E + 1000*R + 100*A + 10*L + D # =
100000*R + 10000*0 + 1000*B + 100*E + 10*R + T,
l a b e l i n g ! [], Vars).

14.5 CLP(FD)
.
, ( , ) : , .
CLP(FD).

322

II. Prolog

14,5. CLP(FD)
% CLP(FD)
s o l u t i o n ! Ys)
;Ys = [_,_,_i_i_t_i_i_}i
domain[ Ys, 1, ) ,
a l l d i f f e r e n t { Ys),
s a f e ! Ys),
labeling!

[ ] F Ys).

% Ys - Y
& 8
1..3
% - ,

,
%
I Ys

safe! [])
s a f e ! [Y | Ys])
:no_attack( Y, Ys, 1 ) ,
s a f e ! Ys) .

% 1 - Y
% Ys

I no_attack( Y, Ys, D):


l
, Y,
k
Ys;

D -
no_attack< Y, [ ] , _ ) .
n o j a t t a c M VI, [Y2 | Ys} , D)
D #\= Y1-Y2,
D #\= Y2-Y1,
Dl i s D+l,
n o _ a t t a c k ( Yl, Ys, Dl) .

:-

, CLP(FD) , . CLP(FD):
minimize( Goal, X) maximize! Goal, X)

Goal,
() X. , Goal indomain
, , .
?- in 1 . . 2 0 , V # = X * ( 2 0 - ) , m a x i m i z e ( i n d o m a i n ( X ) , V ) ,
X = 1 0 , V - 100

(. . 14.1) :
?.- StartTiraes = [, , , Td,Tf 1 , % Tf -
domain! S t a r t T i m e s , 0, 2 0 ) ,
#>= 0,
+ 2#=<,
%
+ 2#=<,
%
+ 3 #=< Td,
% b d
+ 5tt=<Tf,
% Tf
Td + 4 f=< Tf,
minimize( l a b e l i n g ! [ ] , StartTiraes], Tf) .
StartTimes = [ 0 , 2 , 2 , 5 , 9 ]

14.

323


14.7. , , 14.4,
.
:
labeling!

[ff],

Vars)

"ff" ( "first fail")


" ". , , . , ,
,
. , .
.
14.8. CLP(FD) N .
N N
"", , ,
. ( 14.5).

,
.

.

.
(Constraint Logic Programming CLP) , , .
CLP .

: CLP(R) ;
CLP(Q) ; CLP(Z) ; CLP(FD)
; CLP(B) .
CLP , , ,
.
CLP ,
, .
, .
.
CLP , .
CLP ,
, .

324

II. Prolog

:
;
;
;
;
(Constraint Logic Programming CLP);
CLP(R), CLP(Q), CLP(FD);
.


[94] CLP. [161]
CLP. [67] [91]
.
Constraints ( Kluwer Academic), Journal of Logic
Programming Artificial Intelligence Journal, , , , [35].
,
SICStus Prolog [150].

14,

325

15

...
15.1.
15.2.
15.3. ,

15.4.
15.5.
15-6.
15.7.

326
328
331
336
337
340
348

, .
, .
,
.
.

15.1.
, , . , , .
, . , . ,
. , ,
. ,

, -.
, (
),

. ,
.

, ,
. , , , . , ,
; , . ,
.
, ,
.
,
( ).
, , ,
.
, .
.
.
, . 16 , , .
,
.
(. 15.1).
1. .
2. .
3. .

. 15.1.

, , ,
, , , ,
. , .
,
,
, . 15.

327

, ,
.
, .
. , ,
, , ,
, . ,
, , .
,
, .
, ,
, , . , ,
.
. ,
( "if-then"),
( ), , ,
.

15.2.

,
. ,
( ),
.
, , ,
.
if () then () ;
if S then ;
if 1 2 then .
, .
. , .
.
.
( ).
.
. .
328

II. Prolog

.
. .
1. , : " ?"
2. , : " ?"
,
, .

. . "" , . , , . "" ,

( , ). ,
, , ;
if A then F
, < 15.1 -If).'. ,
. ,
: MYCIN , AL/X AL3 .
15.1. MYCIM [148].
0,7 ,
if
1. ,
2. ,
3. ,
- - then
( 0,7) ,
.

15.2. AL/X
[130]. N s - "" "". s
,
. N ,
,
if
V-01

then
V-01 [ = 0,005, S = 400]
if
V-01
, V-01
then
V-01 {-
) [ = 0.031, S = 2000]

15.

329

15.3. AL3 [14]


if
X. , ,
2. ,
Hi, R;
Hz, R.2 ,
3. , ;
then
1. , , "RL or R:"
,
2. , not(H)

,
,
. , . , , ,
. ,
,
. , , 15.2.
. .
( ). , ; ,
, .
. 15.2 . , . , , . , , , :
if hall_wet and bathroom_dry then problerr:_in_kitchen % ,
% , -
kltchen_dry ( )

. 15.2,

330

II, Prolog

, . 15.2,
AND/OR, 13. ,
AND/OR ,
.

15.3.
,

. .
.
.
Prolog. AND/OR (. 13). AND/OR
,
. ,
, , , ,
. ,
,
. Prolog,
,
AND/OR.

15.3.1.
(. . 15.2) .

(, l e a k _ i n _ k i t c h e n ),
.
, problem_in_kitchen (
) no_water_frora_outside ( ) .
, , . , ,
.
, ,
( l e a k _ i n _ k i t c h e n ) (hall_wet ..). Prolog
, ,
Prolog.
Prolog :
leak_in_bathrcom :h a l l wet,
kitchen_dry.
problem_in_kitchen. : -

%
%
%
%

15.

331

hall_wet,
%
bathroomdry.
%
no_water_from_ouitside r- %
window_closed
%
no r a i n .

leak_in_kitchen :%
problem_in_kitchen,
%
no_water_from_outside. %
Prolog :
hall__wet.
%
bathroom_dry.
%
windo*r_closed.
%
,
:
? - leak_in_kitchen.
%
yes

Prolog, , ,
.
1. , Prolog; , ,
, .
2. ; .
, Prolog. , "if", " t h e n " , "and" " o r " , , .
:- ( BOO, fx, if) .
: - op ( TOO, xfx, then) .
:- opi 300, xfy, or) .
:- op( 200, xfy, and).
, ,
. 15.2, :
if
hall_wet and kitchen_dry
then
lea k_in Jba th room.
if

hall_wet and bathroom_dry


then
probleni_in_kitchen.
if

window closed or no rain


then
~
no water from outside.

,
f a c t :
332

II. Prolog

fact[ hall_wet).
fact[ bathrcom_dry).
fact( window_closed).

, , .
:
is_true( )
f a c t , . 15.4. , . :
?- is_true ( leak_in_kitcheri) .
yes

15,4.
%
:- ( 800,
:- op{ 700,
;- op( 300,
: - op( 200,

fx, i f ) .
xfx, then) .
xfy, or) .
xfy, and) .

istrue( P)

:-

fact( P).

is_true( P) :if Condition then P,


is_true( Condition).
is_true( PI and P2)
is^true( PI),
i s t r u e l P2).
is_trueI PI or P2)
is_true( PI)
is_true( P2).

% /
%

::-

, , ,
,
. ,
. , , , .
16.

15.3.2.
(, ,
)
, (, ,
).
, "if" " t h e n " . , . , , , , ; , , ,
15.

333

, ; ,
.
Prolog
, ,
. 15.5 , , , ,
:
if Condition then Conclusion
(Condition)
AND/OR. , . , (
fact), (Conclusion),
, ( a s s e r t ) f a c t . , , :
?- forward.
Derived: problein_in_kitchen
Derived: no_water_frora^outside
Derived: leak_in_kitcheri
No more facts
15,5,
I Prolog
forward :new_derived_fact{ P),
i
!,
write I 'Derived: ' ) , write( P), nl,
assert ( fact( P)),
forward
i
write!

'No mote facts').

new_derived_fact( Concl)
if Cond then Concl,
not fact[ Concl),
compcsed_fact( Cond).
composed_fact( Cond)
fact! Cond).

:%
% -
I ?

composed_fact( Condi and Cond2)


composed fact [ Condi),
composed_fact[ Cond2}.
composed_fact( Condi or Cond2)
composed_fact[ Condi)

:%
:-

ccir;posed_f act [ Cond2 } .

15.3.3.
. 15.2, , . , :
- > . . . - >

334

II. Prolog

, , . (, ), , . ,
, , ,
. :
- , . . -
-*...-*
, - > . . . - > ,
- > . . . - > ,
, ,
. , .. ,
, .. ,
, . , , , .
, , .
(. 11). ,
. , , , . ,
, , ,
. , ,
, ;

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

, . ,
. ,

. , . 15.2, , , , . 15.3.

15.

335


15.1. ,
, , .

15.4.
.
,
"" ("How?") " " ("Why?").
.
, ,
. ,
. ,
,
"How?". .
:
1) , ,
;
2) , , .

,
. ,
"<=" .
,
.
1. , .
2.
if Cond then

:
<= ProofCond

ProofCond Cond.
3. , 1 2 ,
Proofl Proof2. ? is PI and P2,
Proofl Proof2. is ?1 or P2, Proofl Proof2.
Prolog i s_t rue
(. 15.4), . i s _ t r u e 15.6.
, , AND/OR.

AND/OR. ,
, ,
16.
, , . . 336

II. Prolog

, .
, "Why?",
, .

" " , , s 16.
15.6. ,
% is_true( , Proof): Proof -
:- ( 800, xfx, <=).
is true( , }
"fact [ P ) .

:-

is_true( , <= CondProof) :if Cond then P,


is_trua( Cond, CondProof).
is_true[ PI and P2, Proofl and Proof2]
is_true[ PI, Proofl),
is_true( P2, Proof2).
is_true( PI or P2, Proof)
is_true( pi, Proof)

:-

:-

is true( P2, Proof}.

15.5.
15.5.1.
,
, ; , "",
"", - . ,
( ), : " ".
. ( ), , . , , .
, "" "". , , , ,
, . ,
, 0 1 -5 +!>. , , . , .
( ) ,
, .
15.

337


. , .
(Proposition) 0 1, ( C e r t a i n t y F a c t o r ) .
:
Proposition: t a i n t Factor . , , ,
if Condition then Conclusion:Certainty.
. ,
, , 1 2, 1 2. ?l and P2, PI or P2?
. ,
PI 2 , (1) (2) .
:
(1 and 2) - min(c<Pl>, c(P2))
(1 or 2> = <|1}, <2))

if

PI

then

P2:C

:
(2) - (1] *
, .
, or , , 15.7
.
,
( ) :
given( Proposition, Certainty)
"" ( ) {. . 15.2), , .
if
hall_wet and bathroom_dry
then
problen)_in_kitchen : 0.9.
, , , , , , -, , ,
:
given( hall_wet, 1).
%
given! bathroom^dry, 1].
IB
given( kitchen_dry, 01.
%
given( no rain, 0.).
$ , ,
given( window_closed, 0). %
15.7.
%
% c e r t a i n t y ! Proposition, Certainty)
338

II. Prolog

certainty! PF Cert)
given! P, Cert) .

:-

certainty! Condi and Cond2, Cert)


certainty[ Condi, C e r t l ) ,
certainty; Cond2, Cert2),
min( Certl, Cert2, C e r t ) .
certainty! Condi or Cond2, Cert)
certainty! Condi, Ceptl),
certainty! Cond2, Cert2),
max( Certl, Cert2, C e r t ) .
certainty! P, Cert)
:if Cond then p : e l ,
c e r t a i n t y ! Cond, C2) ,
Cert is Cl * C2 .
:
?- certainty(leak_in_kitchen, ).
- 0.8
. , , 0,9.

no_water_from_outside ( ) 0,8. , l e a k _ i n _ k i t e h e n ( )
:
min ( 0 . 8 , 0 . 9 )

= 0 . 8

15.5.2. ,
, ,
.
, ,
, . ,
15.5.1, . , , 0,5, b . a or 0,5. ,
b 0,5.
a or b, .
,
.
. , , :
if a or then
, ; ,
, - .
, , ,
. ,
,
. , , -

15.

339

,
. ,
, , " ".
, ,
,
,
, , ;
.
, , ,
.
.
,
, .
, , ,
. , " ", , , .

, ,
.

15.2. ,
,
, :
(A or ) = max((),(})
,
? ,
, ?

15.6.
15.6.1. ,

,
, ,
. , , ,
, . .
340

II. Prolog

, "" ,

( ). ,
"" "". , , ( "burglary")
("alarm"). "alarm" ,
, " b u r g l a r y " , . . , , , ,
.
, . , "alarm" , alarm = t r u e .
,
( ). , .
, .
. , , , , .
; ,
"". "" "". , .
. , X Y ;
.
X Y. X Y.
X v Y. X Y.
-X. X.
() , X , p(X|Y) , X
, , Y.
, , : " , ,
?"
: " ,
, ?" , , . , ?
,
. , . 2" - 1
271 . ,
!

, , .
, , . - , ,
15.

341

. . ,
.
, , , . ,
( ) ,
.
,
. .
. 15.4
. ,
, . , . , ,
: ", . , ?"
.1

. 15.4. . ,
emu, . ,
.

, , . , , ( ). ,
,
.
, .
. , , .
: ,
,
. , , . , , .
, :
, (
), , .
,
.
,

.
342

II. Prolog

, Z X,
X Z.
, Yi, Yj, ... X
. , :
X , , . , X,
X X, Y;, Y; ..
X
X.
, , -, , -, .
,
, ,
. 15.4. , , .
, - . , , .. - . , { ), . .
X () :
( X | X)
( sensor)
: (burglary) ( l i g h t n i n g ) .
: ,
.. : b u r g l a r y l i g h t n i n g , b u r g l a r y --lightning ..

:
p(burglary) = 0.001
p(lightning) - 0.02
p(sensor | burglary lightning) - 0.9
pisensor 1 b-jrglary -lightning) = 0,9
ptsensor | -burglary lightning) =0.1
p [sensor 1 -burglary -lightning) = 0.001
pfalarm | sensor) = 0.95
p(alarm | -sensor) = 0.001
pfcall I sensor! 0.9
plcali | ~sensor) = 0.0

10 .
(.. , ), 31
(2 s - 1 = 31), , ,
2". 31
10. , , .

.
- , , ,
. , .

15.

343

, . , . , X Y, X Y . ,
,
, ,
, , .
.
, ,
[ b u r g l a r y | a l a r m ) .

15.6.2.

, . , X Y . :
= 1 - [ )

[-)

( Y)
[)

()

p(YIX)

Y)

p(Y)
~Y)

p(X|Y)
p(Y)

p(X|Y]

+ p(-Y)

p(X|~Y)

X Y , p(XIY) - () p(Y|X)
= p(Y! . , , Y ,
X . X Y
, :
( Y]

= {)

p(Y)

X Y , : (X Y) = 0, (X1 } = O n p ( Y l X ) = 0.
, X:, .... ~ ;
:
(;

...

p(Xi)

(;|])

p(X:,iXi

2 ) . . . { I :

...

Xn-i).

Xj ,
:
;; . . . | = p<XiJ p[x 2 ) ( ) . . . ()
, , .
, , (X Y),
. ,
,
.
( a l a r m [ b u r g l a r y ) . , ,
, p t b u r g l a r y j a l a r m ) .
:
(alarm | burglary)
p<burglary I alarm)- p(burglary)
p (alarm)
.
,
, :
( I )
(| )=(|) ( ^. ! )

344

II. Prolog

15.6.3.
, . , : " ,
?" .
( burglary S alarm) = ?
pf bueglary lightniftg) = ?
p{ burglary I alarm -lightning) = ?
p{ alarm -call I burglary) = ?
,
, .
1. :
( Xi 2 I Cor.dj = [ Xi

I CondJ * { 2 I Xi Cond)

2. :
pIXIYi , , . X . . . ) =1
3. :
(

Y]

...

...)

= 0

4. :
(

Cond)

- {|

Cond)

5. Corid ,
X, X :
Condo - Cond, - ,
X,
p(X|Corid 0 ) = p(XICond) * p.fY | Cond) / p{Y|Cond)
6. Cond ,
X, :
) X , (X | Cond) = (X),
, (X);
) X P a r e n t s ,
p o s s i b l e _ s t a t e s ,
p(XICond)=

{X|S} p[S|Cond)
2 _< I f u t D t j ]

: " ,
, ?"
(burglary I alarm) = ?
5:
ptburglary|alarmj = p(bucglary) * !alarm|burgiary) / (alarm)
6:
[alarmI burglary) = p(alarm I sensor> p(sensor!burglary)
+ p!alarm|-sensor) p(-sensor|burglary)
6:
pfsensorIburglary) = p(sensorjburglary lightning) p(burglary
lightningIburglary) + p(sensor|~burglary lightning)

15.

345

p(~burglary lightning)burglary) + p(sensor|burglary


-lightning) ptburglaiy -lightninglburglary)
+ ptsensorI-burglary -lightning) p(-burglary
-lightning I burglary)

1-4 , , :
( s e n s o r | b u r g l a r y ) - 0.9 * 0.02 + 0 + 0.9 * 0.8 + 0 = 0,
p ( a l a r m | b u r g l a r y j = 0 . 9 5 * 0 . + 0 . 0 0 1 * (1 - 0 . ) - 0 . 8 5 5 1

1, 4, 6, :
p(alarm)

= 0.00467929

, :
{burglary[alarm)

0.001

0.551

0.00467929

0.12741

, , 15.8. Xi . . .
[ X I , 2 , . . . ]. ~
n o t X Prolog. :

prob( Proposition, Cond, P)


P r o p o s i t i o n Cond. ,
.
p a r e n t ( ParentNode, Node). .
( X, P a r e n t s S t a t e , P}. , ; X , P a r e n t s S t a t e .
! X, ?!. , ; X , - .
15.9 , . 15.4. ,
15.8, 1.9,
. ,
, :
?- probl burglary,
F = 0.232137

[call],

P).

, ,
:
?- prob( burglary,
p = 0.00892857

[call, lightning], P).

15.8.
%
% .
%
parent! ParentNode, Node)
%
( Node, ParentStates, Prob)
Prob - Node
%
ParentStates, :
( alarm, [ burglary, not earthquake], 0.99)
%
p( Mode, Prob)
,
% prob( Event, Condition, P ) :

346

II. Prolog

i
%
I

Event Condition ;
Event - , ,

prob[ [X I Xs] , Cond, PI :- !,


probt X, Cond, Px) ,
prob[ Xs, [X | Cond], PRest),
P is Px * PRest.

probt [], _, 1)

:-

!.

prob( X, Cond, 1) :member( X, Cond), !.

% Cond

prob( X, Cond, 0) :member! not X, Condi, !.

% Cond , X -

probt not X, Cond, P)


prob( X, Cond, eO),
P is 1 - PO.

:-

!,

% , X
probt X, CortdO, P) :delete( Y, CondO, Cond),
predecessor! X, Y ) , !,
probt X, Cond, Px),
probt Y, [X | Cond], PyGiver.X) ,
prob( Y, Cond, E>y> ,
P is Px * PyGiveriX / Py.

$ Y - X

% , > 0

i ,
probl X, Cond, P)
| X, ] , !.

:% X - ;

X, Cond, ) :- ! ,
findalU (CONDi,Pi), p(X,COKDi,Pi), CPlist), % ,
*
sun>_probs[ CPlist, Cond, P) .
% suin_probs ( CondsProbs, Cond, WeicthedSum) :
%
CondsProbs - ,
%
WeightedSuTn - Conds
%
Cond
sum_probs( [] , _, 0 ) .
sum_probs( [ (CONDI,PI) I CondsProbs], COND, P)
probf CONDI, CCND, PCI),
sura_probs{ CondsProbs, COND, PRest),
P is PI PCI + PRest.
predecessor! X, not Y)

:- I,

:-

i Y,
%

predecessor! X r Y ) .
predecessor[ x, Y)
parent { X, Y ) .

:-

predecessor( X, Z) :parent( X, Y ) ,
predecessor( Y, Z ) .
member[ X, [X [

)).

15.

347

member[ X, [_ | L))
member(X, L ) .

:-

delete! X, tx | L] , L ) .
delete ! X, [Y | L], (Y | L2] )
delete( X r L, L2).

15.9. , . 15.4,
15.8
% "sensor"
parent!
parent!
parent(
parent[
p(
p(
p(
p{
p(
p(
p(
p[
p(
p(

burglary, sensor).
lightning, sensor).
sensor, alarm).
sensor, call).

%
$

b u r g l a r y , 0.001) .
l i g h t n i n g , 0.02} .
sensor, [ burglary, l i g h t n i n g ! , 0.9).
sensor, [ burglary, not l i g h t n i n g ] , 0 . 9 ) .
sensor, [ not burglary, lightning], 0.1).
s e n s o r , E n o t b u r g l a r y , n o t l i g h t n i n g ] , .001) .
alarm, [ s e n s o r ] , 0 . 9 5 ) .
alarm, [ not s e n s o r ] , 0.001).
call, [ sensor], 0.9).
call, [ not sensor], 0.0).

,
. , , .
?- prob( burglary, [call, not lightning), P).
P = 0. "373934
, . . , , 15.9,
, .
,

15.7.
, ; . , , , .
, , .
,
. , , - .
Prolog1.
,
.
348

II. Prolog

15.7.1.
. . , ,
, , . . 15.5. i s a "is " (). .
A b i r d is a kind of animal ( ).
Flying is t h e normal moving method of b i r d s ( ).
An a l b a t r o s s is a b i r d ( ).
Albert is an a l b a t r o s s ,
).

and so

is

Ross ( ,

animal
j.

isa
active at

daylight - *

bird

moving method

fly

^""^^ isa
^ *

colour
kiwi

albatross

/t\

colour/
/
r

Black_and_white

^ - brown

' \ ^ . movingmetriod

\iS3
\

"

\ \^

isa

Ross

^.
\
\ actrve_at
\

w a | k

night
Albert

Kim

, 15.5.
, ( " i s a " )
( ), ( ).
, , Prolog, , :
isa[ bird,

animal).

isa ( ross, albatross).


moving method( bird, fly).
moving method! kiwi, walk).

% -

i -
i -

% -

,
.

15.

349

. . , . 15.5, , " ",


" ". " " " ".
i s a . Prolog , :
moving_method( X, Method) :i s a [ X, SupetX),
% i s a
moving_method( SuperX, Method).
, . , : "
, ":
f a c t ( F a c t ) :- I - ; Fact = Rel( Argl, 2)
Fact, !. % -
f a c t ( F a c t ) :Fact = . . [ Rel, Argl, Arg2J,
i s a [ Axgl, SuperArg), % i s a
SuperFact = .. [ Rel, SuperArg, Arg2],
fact( SuperFact).

, , :
7- f a c t ) moving__method[ kim. Method) ) .
Method = walk
, , .
, :
?- fact( moving_method( albert, Method)) .
Method - fly
b i r d . , ,
i s a .

15.7.2.
.
""
, . ,
. , ,
, , .
. , . . ,
, - . . . , .
( ), (
), .
, ,
:

350

II. Prolog

FRAME: b i r d

a_kind_of: animal
moving_methcd: fly
active_at: daylight
. ,
(a_kir.d_of) ( ),
(moving_rr.ethod)
( a c t i v e _ a t ) .
.
FRAME; albatross
a_kind_of: bird
colour: black_and_white
s i z e : 115
FRAME: kiwi
a_knd__of: bird
moving_method: walk
a c t i v e _ a t : night
colour: brown
s i z e : 40
, . a l b a t r o s s
mcving_method a c t i v e _ a t .
, ,
:noving_method a c t i v e _ a t ,
. ,
, :
FRAME: Albert
instance_of: albatross
s i z e : 120
, a_kinci_of
i n s r a n c e _ o f . , .
, ,
Prolog , ,
.
.
:
Frame_name( Slot, Value]
.
, .
15.10 .
15.10.
h Prolog:
%
frame_name( Slot, Value)
I
Value - ,
% bird -
bird) a_kind_of, animal).
b i r d ! moving_itiethod, fly) .
b i r d ! active_at, d a y l i g h t ) .
% a l b a t r o s s ; - ,
$ : -^ 115
a l b a t r s s s ( a_kind_of, b i r d ) .
15.

351

albatross { colour, black_and__white) .


albatross{ size, 115).
% kiwi; - , ,
% ,
kiwi[ a_kind_of, bird).
kiwi [ mov-ing^method, walk),
kiwi( active_at, night).
kiwi( size, 40).
kiwi( colour, brown).
% albert -
albert( instance_of, albatross).
albert( size, 120).

% ross - -
ross( instance_of, albatross),
ross( size, 40).
% animal - rElative_size
% relative_size
animal ( relative__size, execute ( relative_size ( Object, Value) , Object,
Value) ).
, .
Prolog :
value( Frame, Slot, Value)

Value S l o t Frame. ( ),
;
, . ,
a_kind_of i n s t a n c e _ o f
. " ", .

Prolog :
value{ Frame, S l o t , Value) :Query = . . [ Frame, S l o t , Value],
c a l l { Query), !.
%
v a l u e ( Frame, S l o t , value) :p a r e n t f Frame, Parent Frame) , %
v a l u e ( ParentFrame, S l o t , Value).
p a r e n t ( Frame, ParentFra:ne) :( Query = . . { Frame, a_kir.d_of, P a r e n t Frame]
Query = . . [ Frame, i n s t a n c e _ o f , P a r e n t F r a m e ] ) ,
c a l l ( Query).

,
15.10, , :
?- value[ albert, active_at, AlbertTime).
AlbertTime = daylight
?- value[ kiwi, active_at, KiwiTirae).
KiwiTime = night

352

li. Prolog

,
. .
, , .
, 15.10, , : 104%, 35%.
( ) . ,
:
40/115 * 100% - 34.78%
, r e l a t i v e _ s i z e
. , r e l a t i v e _ s i z e animal
.
value :
?- value( ross, relative_size, R ) .
R = 34.78

. r o s s ,
r e l a t i v e ^ s i z e , i n s t a n c e of ( a l b a t r o s s ) , a_kind_of ( b i r d ) ,
a_kind_of, , , animal. .
s i z e ross a l b a t r o s s . value.
value, , . , ,
() .
,
Prolog :
relative_size( Object, RelativeSize) :value( Object, size, ObjSise},
value( Object, instance of, ObjClass),
value ( ObjClass, size, ClassSize),
RelativeSize is QbjSize/ClassSize * 100. %
r e l a t i v e _ s i z e animal . Object () R e l a t i v e S i z e
( ) ,
r e l a t i v e _ s i z e .
Prolog, , .
execute! relative_size( Object, RelSize), Object, RelSize}

r e l a t i v e _ s i z e animal :
animal( relative_size, execute! r e l a t i v e _ s i z e ! Obj, Val), Obj, Val)).
value
. , ,
, ,
. . value
. .
:
value! Frame, Slot, Value) :value! Frame, Frame, Slot, Value).

15.

353

()
value) frame, SuperFrame, S l o t , Value) ;Query =. . I SuperFrame, S l o t , Information],
c a l l ( Query!, % ,
process) Information, Frame, Value), !. %
% ,
%
value) Frame, SuperFrame, Slot, Value) :parent( SuperFrame, ParentSuperFrame),
value( Frame, ParentSuperFrame, S l o t , ValueJ.
% process) Information, Frame, Value)
p r o c e s s ! execute) Goal, Frame, Value) r Frame, Value) : ! ,
c a l l ( Goal) .
process[ Value, _, Value). % ,
,
, -
.
, , .
, , , .
.
, "" ( i n s t a n c e _ o f a_k:ind_of). , ,
. value , , ,
, .
.

15.3. :
?- value( ross, relative size. Value).
,
.
15.4. , . s i 2
:
si ( instar\ce__of, square) .
si[ side, 5).
r2[ instance_of, rectangle).
r2[ length, 6).
r2[ width, 4) .
square! a_kinct_of, rectangle! .
square( length, execute( value[Obj,side,L), Obj, L)).
square( width, execute! value(Obj,side,W), Obj, W)) ,
rectangle) area, execute! area(Obj,A), Obj, Al ) .
area( Obj, A) : value! Obj, length, L ) , value! Obj, width, W),
A is L*W.
, ,
:
? - v a l u e { r 2 , l e n g t h , ), v a l u e [ s i , l e n g t h , ), v a l u e ( s i , a r e a , ) .

354

II. Prolog


, , : ,
.
: . ,
,
.

,
, , ,

, .
, , :
.

, "" " ". .

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

, ,
Prolog , , , ,
,
.
:
;
, , ;
( "if-then"), ;
, ;
15.

355

, ;
, ;
, ;
;
;
.


[101] , . , ,
[66]. , [90], [126]
[133],
. , MYCIN,
, [148], [24]
, MYCIN. [119]. [10]
. [142] . [25] [68]
.
[103]. [171] ,
, .
, , [39]. [158] .
, , [14], [130] [148].

356

II, Prolog

16

...
16.1.
16.2.
16.3.
16.4.

357
361
366
380

, 15. , , 15,
, .
, ,
,
.

16.1.
,
, .
1.
.
2. , .
3. .
1 . , , 15.
. , .
16.1, , .
, ; ,
.

16.1. . [170].
" a s k a b l e " , .
" : :", i f , t h e n , and, or , 16.3
%

:- ( 100, xfx, ( has, gives, 'does not', eats, lays, isa]).


:- op( 100, stf, [ swims, flies] ) .

rule! ::
Animal
or
Animal
then
Animal

if
has hair

rule2 ;:
Animal
or
Animal
Animal
then
Animal

if
has feathers

gives milk
isa mammal.

flies and
lays eggs
isa bird.

rule3 :: if
Animal isa mammal and
( Animal eats meat
or
Animal has 'pointed teeth' and
Animal has claws and
Animal has 'forward pointing eyes' )
then
Animal isa carnivore.
rule4 :; if
Animal isa
Animal has
Animal has
then
Animal isa
rules :: if
Animal isa
Animal has
Animal has
then
Animal isa

carnivore and
'tawny color' and
'dark spots'
cheetah.
carnivore and
'tawny color' and
'black stripes'
tiger.

rule6 :: if
Animal isa bird and
Animal 'does not' fly
Animal swims
then
Animal isa penguin,

and

rule? :: if
Animal isa bird and
Animal'isa 'good flyer1
then
Animal isa albatross.

358

II. Prolog

fact :: X isa animal


:member! X, [cheetah, tiger, penguin, a l b a t r o s s ] ) .
askable! _ gives _, 'Animal' gives 'What1).
askable! _ flies, 'Animal' flies) .
askable( _ lays eggs,
a s k a b l e ; _ e a t s _,
askablef _ has _,

'Animal' lays eggs).

'Animal'

a s k a b l e i __ i s a

'What').

'Animal' has ' S o m e t h i n g ' ) .

a s k a b l e ! _ 'does n o t ' _ ,
askablef _ swims,

eats

'Animal'

'Animal'

'does n o t '

fly).

swims).
1

'good f l y e r ,

'Animal'

isa

'good f l y e r ' ) .

:
RuleKame :: if Condition then Conclusion.
Condition () , and or, a Conclusion () , . ,
not, , Prolog
(. 16.1) Prolog. and , or,
.
,
, . , .
, .
. 16.1. :
if
light I is on and
lightI is not working and
fusel is proved intact
then

% 1
% 1
% , 1 ,

l i g h t l is proved broken.
, 1
.
if

heater is working
I ,
then
fusel is proved i n t a c t .
% , 1
( ),
l i g h t l f u s e l , l i g h t l h e a t e r
. .
, Prolog, , . : , 16.

359

, .
:
broken_rule :: if Device is on and
not [ Device is working) and
Device is connected to Fuse and
Fuse is proved i n t a c t then
Device is proved broken.
, , 16.2.

. 16.1.
16.2. ,
. 16.1

%
% , ,
%
broken_rule ::
if
on(Device)

and

device( Device) and


(not working(Device)) and
connected( Device, Fuse) and
proved; intact[ Fuse))
then
proved! brokenf Device)).
%

fuse_ok_rule ::
if
connected( Device, Fusel and
working! Device)
then
proved! intact( Fuse)).

360

II. Prolog

%
*
%

) ,
, . :
!

fu$ed_rule ::
if
connected( Devieel, Fuse) and
on[ Devicel) and
[not working! Devicel)) and
samefuse[ Device2, Device 1) and
^on ( Device2) and
[not working! Device2))
then
proved[ failed( Fuse)).
same_fuse_rule ::
if
connected! Devicel, Fuse) and
connected( Device2, Fuse) and
different( Devicel, Device2)
then
samefuse! Devicel, Device2).
device_on_rule ::
if
working! Device)
then
on( Device).
fact ;: different! X, Y)
fact
fact
fact
fact
fact

::
::
::
::
::

device(
device(
device(
devicel
device(

fact
fact
fact
fact
fact

::
::
::
::
::

connected!
connected!
connected!
connected!
connected[

:-

not (X - Y) ,

heater).
lightl).
Iight2).
Iight3).
light4).
lightl,
Iight2,
heater,
Iight3,
lights,

fusel).
fusel).
fusel),
fuse2).
fuse2).

askable{ on[D), on('Device'))askable[ working(D), working('Device'I)-

16.2.

16.2.1.
15, , 16.1 16.2, Prolog
Prolog.
16.

361


.
1. ,
, , .
2. (
Prolog), - . , . , , . ,

.

.
, ,
( ).
Question, please:
peter isa tiger.
Is it true: peter has hair?
yes.
Is it true: peter eats meat?
no.
Is it true: peter has pointed teeth?
yes.
Is it true; peter has claws?
Khy. % ,
investigate, by rule3, peter isa carnivore
To investigate, by rule3, peter isa tiger
This was your question
Is it true: peter has claws?
yes.
Is it true: peter has forward pointing eyes?
yee,
Is it true: peter has tawny colour?
yes.
Is it true: peter has black stripes?
yes.
(peter isa tiger) is true
Would you like to see how?
yes.
peter isa tiger
was derived by rule5 from
peter isa carnivore
was derived by rule3 from
peter isa mammal
was derived by rulel from
peter has hair
was told
and
peter has pointed teeth
was told
and
peter has claws
was told
and
peter has forward pointing eyes
was told
and

362

II. Protog

peter
was
and
peter
was

has tawny colour


told
has black stripes
told

, ,
"" , , :
Is i t true: peter eats meat?

% , ?

, . .
1. .
2. , (
"Why?").
,
. , , .
, ,
, . , ,
: " ?" ,
, , , ,
, .
Prolog. , ,
, , .
Prolog Prolog, .
.

16.2.2.
( )
.
Answ 0, .
Q , Answ
, "Q is t r u e " ( Q ).

'if Condition then Q'
C o n d i t i o n
Answ Q.
Q , " a s k a b l e " (),
, Q,
Q Ql and Q2, Q1,

16.

363

Q1 , Answ, "Q
is f a l s e " , Q2
Q1 Q2 Answ.
Q Ql or Q2, Q1,
Q1 , Answ
Q1; Q2 ,
Q2 , Answ Q2.

not Q
.

16.2.3. ,

,
( "Why?"),
. ,
:
Is a true?
:
Why?
, .

, b Ra,
, Rb,
, z Ry,
z .
, .
( ),
. . , , , AND/OR (. 16,2). "Why?"

. ,
.

364

II. Prolog

. 16.2. - , ,
, ,
, .

16.2.4. ,

,
, ,
"How?". , , , , ..
, . , ,
.
15, . , , ,
:
peter isa carnivore
was derived by rule3 from
p e t e r i s a mammal

was derived by rulel from


peter has hair
was told
and

peter eats meat


was told

16.

365

16.3.
, . . 16.3
, . Goal ; Trace ,
Goal ; Answer Goal.

. 16.3. explore ( Goal,


Trace,
Answer), Answer
Gcal

.
e x p l o r e ; Goal, Trace, Answer). Answer Goal.
useranswer( Goal, Trace, Answer). Goal,
" a s k a b l e " (), ,
Goal, "Why?".
p r e s e n t [ Answer), "How?".
e x p e r t .
.

Prolog 16.3-16.6. ,
.

16.3.1. explore

explore( Goal, Trace, Answer)
Answer
Gosl , 16.2.2.
Goal ,
, , Goal , AND OR.
366

II, Prolog

.
Goal. ,
, AND/OR, , :
(X has feathers) or {X flies} and (X lays eggs)
Trace. ,
Goal
:
Go^l by Rule

, Goal Rule. , , " p e t e r i s a t i g e r "


( ), " p e t e r e a t s
meat" ( ). , , 16.1, .
t (peter isa carnivore) by rule3, (peter isa tiger) by rule5 ]
.
" p e t e r e a t s meat", r u l e 3 " p e t e r i s a c a r n i v o r e "
( ). , " p e t e r i s a c a r n i v o r e " , r u l e 5 " p e t e r i s a t i g e r " .
Answer. (.. AND/OR) Goal. Answer :
Conclusion was Found
Found Conclusion. , , .
1. :
( connected I heater, fusel) is true ) was 'found as a fact'
2. :
( peter eats meat) is false was told
3. :
( peter isa carnivore ) i3 true was ('derived by' rule3 from ( peter isa mammal)
is true was ('derived by' rulel from ( peter has hair ) is true was told ) and
( peter eats meat) is true was told )
e x p l o r e Prolog 16.3.
, 16.2.2,
, .
16.3.
%
t
%
%
%
%
i

explore! Goal, Trace, Answer)


Answer, Goal, Trace -
, explore
. Answer
,

:- ( 900, xfx, : : ) .

16.

367

::::::::-

op!
op(
op(
op(
op(
op(
op!
op(

800,
870,
880,
550,
540,
300,
600,
600,

xfx, was) .
fx, ifj .
xfx, then).
xfy, or) .
xfy, and) .
fx, 'derived by')xfx, from).
xfx, by) .

% , ( 700, xfx, is!


I ( 900, fx, not)
explore! Goal, Trace, Goal is true was 'found as a fact') :fact : Goal.
I ,
explore( Goal, Trace,
Goal is TruthValue was 'derived by' Rule from Answer! :Rule r: if Condition then Goal,
% Rule, Goal
explore[ Condition, [Goal by Rule | Trace], Answer),
truth! Answer, TruthValue).
explore) Goall and Goal2, Trace, Answer) :- !,
explore! Goall, Trace, Answerl],
continue{ Answerl, Goall and Goal2, Trace, Answer).
explore! Soall or Goal2, Trace, Answer] :exploreyes( Goall, Trace, Answer)
% , Goall
exploreyesi Goal2, Trace, Answer). % , Goal2
explore! Goall or Goal2, Trace, Answerl and Answer2) :- !,
not exploreyes[ Goall, Trace, _ ) ,
not exploreyesi Goal2, Trace, _ ) ,
%
explore! Goall, Trace, Answerl),
* Answerl
explore! Goal2, Trace, Answer2).
% Answer2 !
explore! Goal, Trace, Goal is Answer was toldl :useranswer! Goal, Trace, Answer).
l ,
exploreyes( Goal, Trace, Answer) :explore! Goal, Trace, Answer),
positive( ).
continue! Answerl, Goall and Goal2, Trace, Answer) :positive( Answerl),
explore! Goal2, Trace, Answer2),
{positive[ Answer2),
Answer = Answerl and Answer2
negative( Answer2),
Answer = Answer2
).
continue; Answerl, Goall and Goal2, _, Answerl) :negative( Answerl).
truth! Question is TruthValue was Found, TruthValue) :- !.
truth! Answerl and Answer2, TruthValue) :truth( Answerl, true),
truth! Answer2, true], !,
TruthValue - true
TruthValue = false.
positive! Answer) :truth! Answer, true).
negative{ Answer) :truth! Answer, false).
getreply! Reply) :read! Answer),
means! Answer, Reply),!

368

% ?

II. Prolog

nl, write{ 'Answer unknown, try again plee.se'), nl, %


getreplyl Reply).
%
means! yes F yes).
means{ , yes).
means( no, no).
means( n, n o ) .
means( why, why).
means[ w, why).

16.3.2. useranswer
useranswer " ". , , . useranswer, :
getreply[ Reply)

, "yes",
"" "why". g e t r e p l y , , ("" "") .
, g e t r e p l y .
, g e t r e p l y ,
read w r i t e , ,
. , g e t r e p l y
:
getreplyl yes)
"", ?
; "Answer unknown, t r y again p l e a s e " ( , ,
, ). g e t r e p l y
, , .
getreply[ Reply),
( Reply yes, interpretyes{ . . . !
Reply = no, interpretnol ...}

. )

uaeransweri Goal, Trace, Answer)


, Goal,
Answer. Trace
, "why".
useranswer , Goal ,
. " a s k a b l e " (). , ;
askable( Goal)
. Goal
"askable", ,
. , , , "why", Trace.
16.

369

Goal , Goal ( , ). :
useransue[ Goal, Trace, Answer) :askable [ Goal),
* ,
% Goal?
ask( Goal, Trace, Answer).
% ,
% Goal
ask( Goal, Trace, Answer) :introduce! Goal),
%
getreply t Reply),
%
processI Reply, Goal, Trace, Answer).
%
process( why, Goal, Trace, Answer) :% ,
%
showtrace[ Trace),
%
ask( Goal, Trace, Answer).
%
process! yes, Goal, Trace, Answer) :* , Goal
%
Answer - true,
askvars( Goal)
i ,
i

ask( Goal, Trace, Answer), %


process) no, Goal, Trace, false).
% , Goal
%
introduce I Goal} :nl, write! it true: ' ) ,
write 1 Goal), writet ? ) , nl.
askvars{ Goal)
Goal, :
askvars( Terra) :var( Term), !,
% ?
nl, write[ Term), write!' ' ) ,
read( Term),
i
askvars( Terra) :Term ".. [Functor I Args],
%
askarglist( Args).
4 ,
askarglist([1).
askarglist( [Term ] Terms]) :askvars( Term),
askarglist I Teryus) .
useranswer. , , "askable" eats,
.
askable' X eats Y).
Prolog ,
, .
?- useranswer( peter eats meat, [] F Answer).
Is it true: peter eats meat?
i
yea.
%
Answer true
, ,
:
?- useranswer) Who eats what, [ ), Answer).
Is it true: _17 eats _1B?
% Prolog
^
yes.
_17 = peter.
% ,
_1 = meat.

370

II. Prolog

Answer = true
Who = peter
What = meat %
Is it true: _17 eats _18?
yes.
_17 = susan.
_18 = bananas.
Answer = true
Who = susan
What = bananas;
Is it true: _I7 eats _18?
no.
Answer = false

16.3.3. useranswer
useranswer,
, , Prolog
. _17 .
, useranswer . useranswer
,
, .
,
, ,
, .
. ,

.
askable , ,
:
askable( X eats Y, 'z' eats 'Something').
, ,
:
?- useranswer( X eats Y, [ ] , Answer).
Is it true: Animal eats Something?
yes.
Animal = peter.
Something = meat.
Answer = true
X = peter
Y = meat

useranswer, 16.4,
format! Goal, ExternFormat, Question, VarsO, Variables)
Goal , a ExternFormat Goal,
:
askable( Goal, ExternFormat)
, Question Goal,
ExternFormat. V a r i a b l e s , Goal
( ExternFormat), VarsO, ,
.
16.

371

, yives 'What' to 'Whom',


Question, [], Variables!.
Question - 'Who' gives documents to 'whom1.
Variables = [X/'Who1, Y/'Whom 1 ] 16.4. : ,
"why"
%
%
h 'jseranswer( Goal, Trace, Answer)

%
%
%
^

,
Goal, .
Trace ,

useranswert Goal, Trace, Answer)


askable! Goal, _ ) ,
i
$
freshcopy( Goal, Copy;,
?
useranswer( Goal, Copy, Trace,

:,

Goal
Answer, 1 ) .

% He ,
useranswer( Goal, _, _, _, N) :N > 1,
* ?
instantiate( Goal),!,
% Goal
fail.
%
% , Goal
% *?
useranswer( Goal, Copy, _, Answer, _} :wastoldf Copy, Answer, _) ,
instance of( Copy, Goal), !.
i Answer Goal
% Goal,
% N
useranswer( Goal, _, _, true, M) :wastold; Goal, trueF M ) ,
% Goal?
useranswer( Goal, Copy, _, Answer, _) :end_answers{ Copy},
irjsrance_of { Copy, Goal}, !,
%
% Goal
fail.
% ()
useranswer{ Goal, _, Trace, Answer, } :askuser( Goal, Trace, Answer, N ) .
askuser( Goal, Trace, Answer, N) :askable( Goal, ExternFormat),
format( Goal, ExternFormat, Question, []( Variables), %
%
ask( Goal, Question, variables. Trace, Answer, N ) .
ask( Goal, Question, Variables, Trace, Answer, N) :nl,
(Variables = [] , !,
write! 'Is it true: 1 )
%
372

II. Prolog

write{ 'Any (more) solution to: )


%
),
write ( Question), write('? ' ),
getreply( Reply), !,
% yes/no/why
process! Reply, Goal, Question, Variables, Trace, Answer, N) .
process ( why, Goal, Question, Variables, Trace, Answer, N) :showtrace( Trace),
ask( Goal, Question, Variables, Trace, Answer, N) .
process) yes, Goal, _, Variables, Trace, true, N) :nextindex ( Next),
% wastold
Nextl is Next + 1,
(askvars( Variables),
assertz( wastold( Goal, true. Next))
%
freshcopyt Goal, Copy),
useranswer ( Goal, Copy, Trace, Answer, Nextl)
).

% Goal
% ?

process! no, Goal, _, _, _, false, N) :freshcopy( Goal, Copy),


wastold( Copy, true, _) , ! ,
% 'no' ,
assertz( end answers( Goal)),
%
fail
nextindex( Next),
% wastold
assertz { wastold ( Goal, false, Next)) . % 'no' ,
format( Var, Name, Name, Vars, [Var/Name | Vars]) :var( Var), ! .
format{ Atom, Name, Atom, Vars, Vars) :atomic( Atom), !,
atomic(Name).
format( Goal, Form, Question, VarsO, Vars) :Goal =.. [Functor | Argsl],
Form =.. [Functor | Forms],
formatall( Argsl, Forms, Args2, VarsO, Vars),
Question =.. [Functor | Args2].
formatall( [], [], [], Vars, Vars).
formatall( [X | XL], [F | FL], [Q | QL], VarsO, Vars) :formatall( XL, FL, QL, VarsO, Varsl),
format( X, F, Q, Varsl, Vars).
askvars( [ ] ) .
askvars( [Variable/Name | Variables]) :nl, write( Name), write!' = ' ) ,
read( Variable),
askvars( Variables) .
showtrace( [ ]) : nl, write( 'This was your question'), nl.
showtrace ( [Goal by Rule | Trace]) :nl, write( 'To investigate, by ' ) ,
write( Rule), write(', ' ) ,
write( Goal),
showtrace( Trace),
instantiate) Term) :numbervars( Term, 0, 0 ) .
% Term

16.

373

% instance_of( Tl, T2>: T2 - Tl; , 1


% , 2
instar.ce^of [ Terra, Terml) :freshcopyt Terml, Term2),
numbervarst Term2, 0, _ ) , !,
Term = Term2.
fresncopy( Terns, FreshTerm) :-

I Term Terral
$ Terml
%
% ,
* Terml - Term
i Term
s

a s s e r t a ( ( Term)),
r e t r a c t ( copy( FreshTerm)), !.
nextindex( Next) :% wastold
r e t r a c t ! lastindex[ Last)), !,
Next is Last + 1,
assert( lastindex[ Next)),
% l a s t i n d e x / 1 , wastold/3, end_answers/l
:- a s s e r t z { l a s t i n d e x ( 0 ) ) ,
a s s e r t z ( wastold ( dummy, f a l s e , 0 ) ) ,
a s s e r t z ( end_answers{ dummy)).
, , . , ,
. ,
, , :
assert( wastoldl gives documents to friends, true) ).
, , , , . . , (
, ), , .
(X has Y) and
% - Goall
(XI has VI) and

4 - Goal2

, , ( ) Goall.
-12,
Goall, ,
Goai2 ( , , Goal2). , Goal2,
- . GoaI2 . , .
, G o a l l ,
Goall.
, , . :
astold( Goal, TruthValue, Index)
Index , .
usernswer( Goal/ Trace, Answer)

374

II, Prolog

,
.
, useranswer, :
useranswer( Goal, Trace, Answer, N)
N .
Goal, N .
useranswer( Goal, Trace, Answer)
Goal.
, 1, :
useranswer( Goal, Trace, Answer) :useranswer( Goal, Trace, Answer, 1).

useranswer( Goal, Trace, Answer, N)

, Goal, , N .
, , Goal,
,
. ,
, :
end_answers( Goal)
, , :
wastoldt Goal, false, Index)
useranswer .
.
, . ,
, Goal, , , , Goal, .
:
wastold( Goal, false, _)
.
useranswer, 16.4. , ( Goal),
Goal,
Goal. .
:
i n s t a n t i a t e d ! Term)
, Term .
instance_of( Term, Terml)
Terml Term; , Term ,
, , Terml, , .
instance_of( X gives information to Y, gives information to Z)
:
numbervars( Term, N, M)
"" Term, ,
16.

375

"" N -1.
, , :
var/O, var/1, var/2, . . .

?- Term = f( X,

t( a,

Y,

X)

),

numbervars(

Term,

5,

M).

:
Term = f( var/5, t( a, var/6, var/5))
X = var/5
Y = var/6
M=7
numbervars Prolog
. , .
numbervars( Term, N, Nplusl) :v a r ( Term), ! ,
% ?
Term = var/N,
Nplusl is N + 1.
numbervars( Term, N, M) :Term =.. [Functor | Args], %
numberargs( Args, N, M ) .
%
numberargs( [ ], N, N) :- !.
numberargs( [X | L], N, M) :numbervars( X, N, N1),
numberargs( L, N1, M ) .

16.3.4. present

present! Answer)
16.5, . Answer , ,
, . p r e s e n t . , , "how", , .
16.2.1 16.2.4.
16.5. : ,

% ,
%
present; Answer) :nl, showconclusion( Answer),
nl, write( 'Would you like to see how? ' ) ,
getreply( Reply),
( Reply = yes, !,
show( Answer)
%

true
).
showconclusion( Answerl and Answer2) :- !,
showconclusion( Answerl), write( 'and ' ) ,
showconclusion( Answer2).
376

II. Prolog

showconclusion( Conclusion was Found) :write( Conclusion).


% show
show( Solution) :nl, show( Solution, 0 ) , !.

, 0

show( Answerl and Answer2, H) :- !,


show( Answerl, H ) ,
tab( H ) , write{ and), nl,
show( Answer2, H ) .

show( Answer was Found, H) :tab( H ) , writeans( Answer),


nl, tab( H ) ,
write( 'was ' ) ,
showl( Found, H ) .

%
%

showl( Derived from Answer, H) :- !,


write! Derived), write( 'from'),
nl, HI is H + 4,
show( Answer, HI).

showl( Found, ) :% Found 'told'


% ( ) 'found as fact' ( )
write( Found), nl.
writeans( Goal is true) :- !,
write( Goal).
writeans( Answer) :write( Answer).

% 'is true'
% ( )
% -

16.3.5.
Prolog ,
e x p e r t ( 16.6). e x p e r t (. 16.3-16.5), , :
?- expert.
Question, please: %
X isa animal and goliath isa X. %
Is it true: goliath has hair?

16.6. .
Prolog
expert
%
expert :getquestion( Question), %
( answeryes( Question) %
answerno( Question)
% ,
).
answeryes( Question) :% Question
ma^kstatus( negative),
%
explore! Question, [], Answer), %
16.

377

positive! Answer),
%
maxkstatus[ positive),
%
present! Answer), nl,
write( 'Mere solutions? ' ) ,
getreply( Reply),
%
Reply = .
% explore
answernoi Question) :- %
retract! no_positive_answer_yet), !,
% ?
explore] Question, [], Answer),
negative 1 Answer),
present( Answer), nl,
write{ 'More negative solutions? ' ) ,
getreply{ Reply),
Reply - no.
%
markstatus{ negative) :assert( no positive answer yet)*
markstatus{ positive; :retract{ no^positive answer yet}, !
true.
getquestion( Question) :nl, write{ 'Question, please'), nl,
read( Question).

16.3.6.
,
,
Prolog. , ,
, ,
,
, .
, ,
. , . ,
Prolog, .

16.3.7.
,
, , , e x p l o r e . :
explore[ not Goal, Trace, Answer) :- ! ,
explore ( Goal, Trace, Answer.!),
invert( Answerl, Answer).
%
invert ( Goal is true was Found, ( not Goal) is false was Found) .
invert( Goal is false was Found, ( not Goal) is true was Found}.
, Goal ,
. , , :
?- expert.
Question, please:
not ( X eats meat).
Any (more) solution to: Animal eats meat?

378

II. Prolog

yes .
Animal = t i g e r .
:
not [tiger cats meat) is false
% net (tiger eats meat)
. ,
:
not ( X eats meat) % ,
: " X,
X ?" , e x p l o r e { ), .
1. X, , X ?
2. , .
3. not ( t i g e r e a t s meat) .
, :
" , X ?"
,
. : explore
, X :
X: not (X eats meat)?
, , , :
X: X, not (X eats meat)?
,
.
.
.
not Goal, Goal, :
Goal , not Goal ;
Goal' Goal
Goal' , Goal,
not Goal ;
Goal' Goal
Goal 1 , Goal,
not Goal.
, .
. 16.2
broken_r-Lile:
broken_rule :: if
on( Device) and
device( Device) and
% Device
not working; Device) and
connected! Device, Fuse) and
proved! intact[ Fuse))
then
proved( broken( Device)) .

16.

379

device( Device)

""
not working; Device)
.

16.1. , .
r u l e l : : if bottle_empty then john_drunk.
r u l e 2 : : if john_drunk then battle_empty.

'i ,
'i ,

e x p l o r e .
e x p l o r e , . Trace. : , , , ,
.

16.4.

.
.
, .
,
..
e x p l o r e , , ""
( , ). ,
e x p l o r e ; , , Trace.

.
, ""
.
: " ", " ", .,.,
" ", "".
( "") ( " ") ,
.
.
, ,
, .
, , , .
Is it true: susan flies?
% ?
no.
Is it true: susan isa good flyer?

380

% ?

II. Prolog

,
! .
Any (more) solution to; Somebody flies?
yes.
Somebody = bird.
Is it true: albatross flies? %
, , . ,
,
.
(. 15).
.
, ,
. , , ,
, .
, ,
, "" . ,
.
: . , , . , , ,
. Trace
Answer, e x p l o r e . , , " "
, .

, .
,
.
, , "" .
"" , - .
, , , , ,
. , . , - , ,
" 3 ".
, .. , .


, , .

16.

381


, , .
.
(
"") ( " ").
.


[62].
, , [170].

382

II. Prolog

17

...
17.1.
17.2.
17.3.
17.4.
17.5.
17.6.








17.7.

383
387
390
393
395
398
401

, .
. , .

1 7 . 1 .
. 17.1.
(. 11).
, ,
.

, 17.1.
, , :
. .
( ) ()

,
.
; () . "" . , . , , .
, :
on( Block, Object)

clear( Object)
, Object ( ). , , ,
, ( ), , . Object
. , , 1, 2, 3 4 . (. . 17.1}
:
[ clear( 2), clear( 4), clear( b), clear( ), on [ a, I ) , on( b, 3), on[ c, a)]
, . , on ( , ) , .. on.
on, , .
. , .
1. ,
, .
2. , ; , ,
3. , .
:
can ( Action, Ccmd}

, Action , Cond.
:
adds( Action, AddRels)
deletes! Action, DelRels)
AddReis ,
Action. Action AddRels . ,
DelRels , Action. , Action.
:
move ( Blcci-:, From, To)
Block , From ,
. 17.1.

384

II. Prolog

17.1.
% move ( Block, From, To) **
% can( Action, Condition):
%
Action , Condition
can{ niovef Block, From, Tol, [ clear! Block), clear! To), on I Block, From!] ) :is_block( Block),
%
object! To),
% To -
\== Block,
$
object\ From),
$ From -
From \== ,

Block \== Frotn.
%
% adds! Action, Relationships):
%

Action Relationships

adds( move(X,From,To), [ on ; ), clear(From)]).


% deletes! Action, Relationships):
%

Action Relationships

deletes! move(X,From,To), ( on(X,From), clear(To)]).


object! X)
:place(X)
;
is_block( X) .
%

% X ,
% -
I
'i x -

is__block ( ) .
is_block( b ) .
is_block( ) .
place ( 1) .
place( 2) .
place( 3) .
place( 4) .
%
i
%
i
%
%

a b
=1234

statel(

clear(2),
on[c,a) ]

clear(4),
) .

clear(b),

clear[c),

on(a.l),

on(b,3),

, .
,
.

. 17.2 .
, .. . : , -

17.

385

, , , , . ,
, :
[ slot_closed< battery), slot_closed( film), in< battery),
ok( battery), in( film), film_at_start, film_unused]

17.2.
%
%
can{ open_case, [camera_in_case]).
adds ( open_case, |camera_outside__case] ) .
deletes( open_case, [camera_in_case]).
%

can( clase_case, [camera_outside_case, slot_closed( film),


slot_closed( battery)]).
adds [ close_case, [came>:a_iri_cae] ) .
deletesf close_case, (camera_outside_case]).
i

can I open_slot( X ) , [camera_outside_case, slot_closed( X)]).


adds( cpen^slct; X ) , [slot_open( X)]).
deletes! open_slot ( X ) , Lslot_closed( X}]).
%

cant close_slot( X) , [canera_outside_case, slot_open( X)]).


adds( close_slot( X ) , [slot_closed( X)]).
deletes( close_slot[ X ) , [slot_open( X)]).
%
cani rewind, [earnera_cutside_case, in( film), film_at_end]).
adds [ rewind, [filni_at_start] ) .
deletes( rewind, [film_at_end]!.
% ! remove I battery], [slot_open( battery), in! battery)]).
can( remove( film), [slot_open( film!, in[ film), film_at_Start]).
adda( remove( X}, (siot_empty( X)]).
deletes! eemovef x ) , [in( X)]>.
%

can! insert_new( X ) , [slot_open! XI, slot_empty( X)]).


adds! insert_newl battery), [in! battery), ok( battery)]).
adds [ insert_new( film) , [in( film) , f llrr._at_start, film_unused] ) .
deletes! ir.sett_new( X ) , [slot_empty( X] ] " .
%

can( take_picturesr [in| film), film at_start, film_unused,


in[ battery), ok< battery), slot_cTosed[ film), slot_closed{ battery)]),
adds ( take__pictures, [film_at_endj ) .
deletes! take_pictures, [film_at_start, film_un-jsed] ) .
% , ,
* (. ,
* ok! battery) .)
state1! [camera_in_case, slot_closed[ film), slot_closed( battery),
in[ film;, film_at_endr in[ ba-tery)]).

386

II. Prolog

,
. (. . 17.1) :
( ( , , on ( b , }]

, ,
, :
[ in( film), film_at_start, film_unused, in( battery),
ok{ battery) slot_closed( film), slot_closed1 battery)]

,
, " ", .

17.1. ,
17.1, , , . " " . , ;
, ( ).
17.2.
(. 2), "walk" (), "push"
(), "climb" () " g r a s p " ().

17.2.

(. . 17.1), ,
; on [ , ). ,
{.. ),
. , .
1. , on ( , ). adds ,
move! a, From, b)

From. , ,
,
.
2. move ( a, From, ) . , . :
( clear( a), c l e a r ! b), on( a. Prom)]
c l e a r ( b) on ( a, From)
( From = 1), c l e a r ( ) . c l e a r ( ) , .
3. adds, , c l e a r { ) . :
move[ BlocX, a, To)

:
[ clear( Block), clear( ), on; Block, a}]
17.

387

,
:
Block = = 2

move ( , , 2),
. :
, move ( , , 2);
,
.
:
[

c l e a r [

a ) ,

c l e a r {

) ,

c l e a r [

) ,

c l e a r {

A),

on {

1 ) ,

on (

3),

-,

2)]

move ( , 1, ), on { , ) .
:

move(

2),

move[

1,

)]

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

. 17.2. , .

.. 17.2.

S t a t e Goals,
; F i n a l S t a t e , .
Goals S t a t e , F i n a l S t a t e
S t a t e . .
Goals Goal, .
Action, Goal .
Action,
Condition Action, MidStatel.
4. Action MidStatel
Midstate2 ( MidState2 Goal ).
5. Goals MidState2,
F i n a l S t a t e .

=
1.
2.
3.

388

II. Prolog

Prolog,
17.3, :
plan( State, Goals, Flan r Fi rial State)
S t a t e F i n a l S t a t e , Goals
, a Plan , .
,
,
, .. - . . .
17,3.
%
%


plant State, Goals, Plan, FinalState)

plan[ State, Goals, [], State!


satisfied State, Goals).
%
%
%
%
%

;-

%
% E SLate Goals

,
cone, , Preplan,
Action, ,
,

plan{ State, Goals, Plan, FinalState) :cone I Preplan, [Action ] PostPlan], Plan),
select( State, Goals, Goal),
achieves; Action, Goal),
can( Action, Condition),
plant State, Condition, Preplan, MidStatel),

%
%
l

%
% Action
apply! MidStatel, Action, MidState2),
% Action
plan( Mid5tate2, Goals, PostPlan, FinalState).
%

% satisfied! State, Goals}: State Goals


satisfied! State, [] ) .
satisfied( State, (Goal | Goals!)
member( Goal, State) ,
satisfied( State, Goals) .
select( State, Goals, Goal}
member( Goal, Goals) ,
not member; Goal, State),

:-

:% Goal

% achieves( Action, Goal): Goal - Action


achieves( Action, Goal)
adds{ Action, Goals),
member! Goal, Goals).

:-

i apply! State, Action, NewState):


%
Action, State,
%
KewState
apply{ State, Action, NewState} :deletes( Action, DelList),
delete_alil State, DelList, Statel), !,
adds( Action, AddList),
cone! AddList, Statel, NewState).
%

delete_all{ LI, L2, Diff) :

17.

389

Diff - , L1 L2

delete_all(

[ ] , _,

[]) .

delete_all( [X | L1J, L2, Diff)


member! X, L2), !,
delete_all[ LI, L2, Diff).

:-

delete_all( {X ] LI], L2, [X I Diff])


delete_all[ LI, L2, Diff).

:-

, , . 17.1,
:
?- S t a r t
on( )
Plan - [
rinState
oni

b,

- [ clear( 2), c l e a r ! 4], clear( ), clear) ),


] , p l a n ( S t a r t , [ o n { , )], P l a n , F i n S t a t e ) .
move ( , a, 2 ) , move { a, 1, to} 1
- [ o n ( a , b ) , c l e a r f 1 ) , o n [ c , 2), c l e a r { a ) ,

( ,

1),

clear [ 4),

on ( ,

3),

clear{ c),

3)]

, , , . , F i x B a t t e r y ,
, FixCamera.
?- Start = [ camera_in_case, slot_closed( film;, slot_closed( battery),
in( film), film_at_end, in{ b a t t e r y ) ] ,
plan( Start, [ ok( b a t t e r y ! } , FixBattery, _) .
FixBattery = [ open^case, open_slot( battery), remove( battery),
insert_new( battery)j
?- Start = [ camera_in_case, slot_c.lo5ed( film), slot_closedt battejry} ,
in( film), film_at_end, in( b a t t e r y ) ] ,
can [ take^pictures, CavneraReady) , %
plant Start, CameraReady, FixCamera, FinStatei .
CamerdReady = [ in( film), film_at_start, film_unused, in( battery),
ok( battery), slot_cloEed( filmy, slot_closed( battery)]
FixCamera - [ open_case, rewind, open_5lot( film), remove( film),
insert_new( film), open_slot( battery), remove( b a t t e r y ) ,
insert_new( battery), close_slot( film), close_slot( battery)]
FinState = [ slot_closed( battery), slot_closed( film), in[ battery),
ok( battery), in( film), film_at_start, film_unused, camera_outside_case)
,
.
.
.

17.3. on { , 3) , . 17.1.

17.3.
,
. ,
. , 390

II. Prolog

. , ,
, . .
, . 17.1. , S t a r t . 17.1. :
plant Start, [ on( a, b), on ( b r ) ] , Plan, _)
, , .
Plan = [
move ! b,
move[ ,
move[ a,
move [ a,
move[ b ,
move[ a,

move( b ,
, 3 ) ,
a, 2) ,
1, b ) ,
b, 1) ,
3, c ] ,
1, b ) ]

3,

),

, , ,
!
. ,
. , , , .
movei b, 3, ), ( , )
move( , , 31, clear [ )

move[ , , 2), c l e a r [ )
move( , 1, )
move( , 1, ), on( , )
move( a, b F 1), c l e a r ( )
move( b, 3, )
move( b, 3, ) , - ( , ) ()
move( , 1, ), on( a r b) ()
,
, .
, on ( b, ) , ,
, on ( a, b ) .
on [ b, ) . , on { , }. , on. ( , on ( , ) .
:
plant

Start,

Clear;

2),

clear(

31],

Plan,

_)

.
move[ , 3, 2 ) , c l e a r f 3}
move[ , 2, 3 ) , c l e a r ( 2)
move( b, 3, 2), c l e a r ( 3)
move( b, 2, 3), c l e a r ( 2)

. , ,
b
2 3 .
,
. ,
17.

391

, . plan, :
plant State, Goals, ProtectedGoals, Plan, FinalState)
P r o t e c t e d G o a l s , "" Plan. ,
Plan
P r o t e c t e d G o a l s . . , 17.4, (. 17,3) .
2 3 , :
move) , 3, 2), clear( 3}
move! b, 2, 4), clear( 2)
clear{ 3)
17.4. ,
. s a t i s f i e d , s e l e c t , a c h i e v e s a p p l y 17.3
% ,

plan{ InitialState, Goals, Plan, FinalState} :plan( InitialState, Goals, [], Plan, FinalState).
%

plan( InitialState, Goals, ProtectedGoals, Plan, Finalstate I :


Goals FinalState,
''.
Plan
plan[ State, Goals, _, [1, State)
satisfied! State, Goals] .

:% Goals
% State

plant State, Goals, Protected, Plan, FinalState1 :concl Preplan, [Action I PostPlan], Plan),
\
select( State, Goals, Goal) ,
*
achieves! Action, Goal),
can( Action, Condition),
preserves! Action, Protected),
% He
plan! State, Condition, Protected, Preplan, Midstatel),
apply! Midstatel, Action, MidState2),
plant Midstate2, Goals, [Goal I Protected], PostPlan, FinalState).
% preserves( Action, Goals):
%
Action Goals
preserves! Action, Goals)
:deletes( Action, Relations),
not (member! Goal, Relations),
member! Goal, Goals] ),

% Action Goals

, , ,
,
move ( , 3, 4).
,
. . .

392

II. Prolog

17.4.

, 17,3 17.4, , . , , ,
. plan:
conc( Preplan,

[Action | PostPlan], Plan)

Plan cone
PrePlan :
Preplan = [];
PrePlan = [ _ ] ;
Preplan - [ _, _ ] ;
PrePlan = [ _, _, _ ] ;
Preplan. PrePlan Action. , ( ),
, P o s t P l a n
. , " "
, " " .

, .
" ". ,
.
,
,
, .
, , . , :
breadth_first_plan( State, Goals, Plan, FinalState) :candidate( Plan), %
plan( State, Goals, Plan, FinalState).
candidate[ [ ] ) .
candidate! [First I Rest]) :candidate( Rest).
,
, plan. :
conc( Plan, _, _)

. (. 17.3)
:
plan( state, Goals, Plan, FinState) ;concf Plan, _, _ ),
I B
%
( Preplan,

[Action | PostPlan],

17,

Plan),

393


, 17.4.
,
, . , S t a r t
, . 17.1,
plant

Start,

clear[

2),

clear(

3}],

Plan,

:
Plan = [ move( b, 3, 4) ]

. , . 17.1,
.
plan! Start, [ en[ , ), ( , ] ] , Plan, _)
:
move(,
move( ,

,
3,

2)
}

move( , , )
meve( , 1,

,
, . , , . , ,
, .
. -,
? -,
, move ( , 3, )?
. , move ( , 1, ),
on i a, b ) ,
move [ , 1, ), , c l e a r ( ) . ,
on ( , ) . , move [ , 3, ) .
. , , , , ""
.
, move [ , , 2) move ( , 3, ) ,
. , on ( , ) . move [ , 3, )
,
.
on ( , ), , , on [ b, ), on ( , } , .
{ , ) ,
T.ove ( , 3, ) xicve ( b, 3, ) .
, , , . , . . ,
, , . , , ( 394

II. Prolog

). ,
, .
.
, , .
( !), , ,

, . , ,
, , . ,
(. 11).

17.4. , , s e l e c t a c h i e v e s .
,
( , ), . , . S t a t e a c h i e v e s .

17.5.
, Goals,
S. , , S, SO, , SO, .
, Goals0
SO , Goals S, .
SO: GoalsO

> S: Goals

GoalsO , .
1, SO , GoalsO
.
2. G Goals :
G ;
G GoaisO,
.
GoalsO Goals
Goals ( ) . , ,
Goals G.
. 17.3,
17.

395

. 17.3.

GoalsG, : GaalsO - can < A) LJ Goals
add( ). , Goals
,
.
Goals
S t a r t S t a t e , .
S t a r t S t a t e Goals ,
.
G Goals ,
G. Goals , NewGoals,
NewGoals S t a r t S t a t e .
, , . , on [ a, bj c l e a r f b) .
:
impossible! Goal, Goals)
, Goal Goals, .. Goal Goals ,
. :
impossible( on[ X, X) , _ ) . %
impossible( on[ X, Y), Goals) :meraber{ c l e t ( Y) , Goals)
memberf on( X, YD, Goals), 1 \==
membert oz\{ XI, Y) , G o a l s ] , XI \== X.

%
%
%
I

impossible( c l e a r ! X>, Goals) ;. ( on [ _, X), Goals).


, ,
, 17.5. ,
.
p l a n :
cone! Preplan, [Action], PlanJ
396

II. Prolog

, . 17.1.
17.5. , ,

%
,
%
planf State, Goals, Plan)
plant State, Goals, []) :satisfied [ State, Goals).
1 Goals
% State
plan{ State, Goals, Plan) :cone( PrePlan, [Action], Plan),

$ ,
i
*

select! State, Goals, Goal),


achieves^ Action, Goal),
can! Action, Condition),
preserves[ Action, Goals) ,
regress! Goals, Action, RegressedGoals),

i
4
*
*
%


Action
Goals
Goals
Action

planf State, RegressedGoals, Preplan).


satisfied! State, Goals) :delete_all( Goals, State, []].
select! State, Goals, Goal)
member! Goal, Goals).
achieves! Action, Goal)
adds! Action, Goals],
member! Goal, Goals).

$ Goals,
% State

:-

% Goal Goals
%

:-

preserves! Action, Goals)

:-

i Action
% Goals

deletes! Action, Relations),


not (member) Goal, Relations),
member! Goal, Goals) ).
regress) Goals, Action, RegressedGoals)

:-

% Goals
% Action

adds) Action, NewRelations),


delete_all( Goals, NewRelations, RestGoals),
can) Action, Condition),
addnew1 Condition, RestGcals, RegressedGoals). %
I ,
% addnew! NewGoals, OldGoals, AllGoals):
%
OldGoals - NewGoals OldGoals;
I
NewGoals OldGoals
addnew! [], L, L) .
addnew! [Goal | _ ] , Goals, _) :impossible) Goal, Goals),
!,
fail.
addnew) [x | LI], L2, L3)
member) Y., LI), !,
addnew) LI, L2, L3) .

&

:-

addnewl [X I LI], L2, [X | L3])


addnewl LI, L2, L3) .

17.

% Goal Goals

i
:-

397

i delete_all( LI, L2, DiffJ:


%
Diff - , L1 L2
delete_all( [), _, []).
delete_all( [X | LI], L2, Diff)
member( X, L2), !,
delete_all[ LI, L2, Diff).

:-

delete_all( [X [ LI], L2, [X | Diff])


delete all( LI, L2, Diff].

:-

17.5. , , on ( , ) ,
. 17.1. , :
( move( , , 2), move( , 1, ) )

[ on ( , }], ( ) ?

17.6.


, , : ( ) .
, - ,
. ,
.
,
. , , , .
s e l e c t ( s t a t e , Goals, Goal),
. , ,
.
, , , " " (.. , ,
). , , ,
.
achieves ( Action, Goal),
, . ( 398

II. Prolog

,
.) ,
, , , ; ,
.
,
,
, ,
, , .
, . ,
.
(. 12), ; ,
.
1.
s( Nodel, Node2, C o s t ) .
2. , goal ( Node).
3. , h [ Node,
HeuristicEstimate).
4. .
.
, G c a l s l Goals2, , .
1. G o a l s l .
2. G o a l s l .
3. Goals2 G o a l s l , r e g r e s s , 17,5:
regress ( Goalsl, A, Goals2)
, , 1 .
:
s( Goalsl, Goals2, 1) :member( Goal, Goalsl),
%
achieves( Action, Goal),
h
can( Action, Condition;,
preserves! Action, Goalsl),
regress! Goalsl, Action, Goals2).
, , .
, .
,
. ,
, . , { )
17.

399

on ( a, b) , . 17.1,
:
[ [ c l e a r ! ) , c l e a t [ 2), on ( , a ) , c l e a r t ) , ( , 1 ) 1 , *
,
[ c l e a r ! a ) , c l e a r [ b) , on ( , 1 ; } ,
% ,
% move( , , 2)
[ on( , ) ] ]
% ,
% move! , 1, )
, . ,
, , , . , ,
.
. ,
.
:
Goals -> Action
, 17.6.
,
.
17.6.
. s a t i s f i e d , a c h i e v e s , p r e s e r v e s ,
r e g r e s s , addnew d e l e t e _ a l l 17.5
i
I
:- [ 300, , ->) .
s( Goals -> NextAction, NewGoals -> Action, 1)
member( Goal, Goals),
achieves( Action, Goal),
can[ Action, Condition),
preserves( Action, Goals),
regress I Goals, Action, NewGoals).
goal! Goals -> Action) :start! State),
satisfied! State, Goals).

:-

% 1

%
% Goals
I

hi Goals -> Action, H) :s t a r t ! State),


delete_all( Goals, State, Unsatisfied),
length; Unsatisfied, H).

%
%
%

(. 17.6) (. 12) .
adds, d e l e t e s can ( 17.1
). , i m p o s s i b l e , a
s t a r t , . , . 17.1, ;
s t a r t ! [ ( , 1), ( , 3), ( , a), clearf b ) , clear [ ) , clearf 2),
clear( 4)] ) .
400

II. Prolog

, , . 17.1, , , :
?- b e s t f i r s t t [ on( a, b), on( b, ] -> stop, Plan).
stop,
, , - . [ on ( , ) , on ( b, ) ]
, , . , , .
Plan = [
[ clear( 2), on i ,
move( , , 2),
[ c l e a r ! ) , ( ,
[ c l e a r ! a!, clear(
[ o n ! a, b) , o n ! b,

a ) , clear ( ) , { , 3) , clear ( ) , on ( , 1 ->


3 ) , c l e a r ! a ) , c l e a r ( b ) , o n [ , 1)1 -> move( b ,
), o n [ , 1 ) , ( , ) ] - > m o v e ! , 1, ) ,
c ) j -> s t o p ]

3,

),

,
, .

17.6. , 17.6. ? (
12.)
17.7.
, . , , , . , on. ( , )
, , , . , ,
,
, .
.
17.8. ,
17.6, , :
si Statel, State2, Cost)
Cost , ,
, . .

17.7.

, , , , , . - .
17.

401

. : . .

17.7.1.
, ,
, ,
. , ( adds,
d e l e t e s can). , 17.1 ., :
can( move( Block, From, ), [ clear( Block), clear{ To), on ( Block, From)]) :block! Block},
object( To),
, block ( Block), .
,
. , , . 17.1, c l e a r ( ) .
a c h i e v e s c l e a r ( ) :
move( Something, a, Somewhere)
:
can( move( Something, a, Somewhere), Condition)
, , Something
Somewhere. , , :
move( b, a, 1)
move( b, a, 2)
move( b, a, 3)
move( b, a, 4)
move( b, a, c)
move( c, a, 1)
move ( c, a, 2)

,
, . , :
( rr.ove ( Block, From, To), [ clear ( Block;, clear! ), on ( Block, From) 1)
, . 17.1, c l e a r ( ) .
s achieves :
move( Something, a, Somewhere)
:
[ clear{ Something)r clear! Somewhere), on( Something, a)]
, , , . , :
Something =
Somewhere = 2

402

II. Prolog

,
- , ,
. ,
, . , , 17.1,
, ,
.
, .
. ,
. ,
, . 17.1, :
move ( , , )
,
! , ,
. .
c a n [ move ( B l o c k , From, T o ) ,
[ c l e a r ! B l o c k ) , c l e a r ( T o } , on{ B l o c k , F r o m ) ,
d i f f e r e n t ; Block,
To),
d i f f e r e n t ! From,
To),
different;

Block,

From)]).

d i f f e r e n t ! X, Y) , X Y
. d i f f e r e n t ; X, Y) . -
, .
s a t i s f i e d , :
s a t i s f i e d ! State, [Goal [ Goals]) ;holds! Goal), % Goal State
s a t i s f i e d ! Goals).
, , d i f f e r e n t ; X, Y),
holds :
holds! different! X, Y))
.
X Y , d i f f e r e n t ( X, Y) .
X Y (X == Y),
,
, , i m p o s s i b l e .
(X Y , ) , . , , ,
X Y .
, , d i f f e r e n t ; X, Y),
, . p l a n , .
17.

403

, . , :
move(

1,

X)

c l e a r ( ) ? , X = ,
, d i f f e r e n t ( X, b ) . ,
: X ,
d i f f e r e n t ( X, Y).

17.7.2,
, , ,
.
, . 17.4,
, .
, :
Planl =
12 =

[
[

move(
move(

b,
,

,
d,

) , m o v e ( a , 1 , )]
f ) , move( d, 8, a ) ]

. 17.4, ,

, . , ,
: P l a n l , Flan2, 12, P l a n l ,
,
. ,
.
1 move( ), move( , d, f), move( d, 8, e), move[ a, 1, b) ]
, 24 ,
, , :
, .
, . , -
,
. ,
, . , , (
).
,
, . 17.1. , .

404

II. Prolog

on ( &, b) on ( b, ) , ,
:
Ml = move( a, X, b}
2 = move( b, Y, )

. ,
, .
. move ( , X, } c l e a r ( ] . , - :
= move ! U, , V)

Ml,
:
b e f o r e ' , 1)

, 2
, .
, . ,
[ Ml, M2, ], Ml, . , :
[ , M l , 2]
[ , 2,
( 2, , M l ]

, , , : = ,
V - 2, X = 1, Y = 3. ,
, .

, , , . . 17.5 ,
. , . , , , ,
. , ,
: ,
, , .
, .
, 17.1,
,
. , look; P o s i t i o n , Object) , P o s i t i o n (.. Object). ,
,
, . , { 17.

405

, , , , ).

.
, 17.5,
, ,
, 17.7.1.
, 17.7.2.

, 17.5. : ( ,
), on ( ,

), on ( , d)


, . ,
,
: (, )
(, ).
,
.
,
.
. : ,
.
,

, , , ,

,
.
. , ,
,
. .
, , ,
.
406

II. Prolog

,
.

, ,
:
, , ;
;
;
;
.




[113]. GPS
(General Problem Solver ); [46]. STRIPS [49], [50],
GPS. STRIPS
( adds,
d e l e t e s can), . ( ) [80]
[82], [143]. STRIPS
[116], , (Green)
(Kowalski). Prolog, , WARPLAN [164]. STRIPS, . WARPLAN
, [33], [163] , . , . ,
, . 17.1,
(Sussman) (., , [163]}.
[134] [157]. [29] . [166] .
,
.
[30],

. [6] .
, [126] [133]. Artificial Intelligence (., , [4]).
17.

407

18


J5 ...
18.1.
18.2.
18.3.

18.4.
18.5.
18.6.
18.7.

408
409
414
419
426
433
439

. ,
, , , .
,
.

18.1.
,
. , .
. ,
, ,
.
, . , ,

, .
, . .
. . , ,
, ,

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

, , , .
: ; ; ; ; ;
; .
. ,
.
. ,
, .
(Knowledge Discovery in Databases KDD),
.
,
. , , , , , ,
.
. .
, . , .

18.2.
18.2.1. ,

. , U ; , , . U .
U. . , X U , X .
, , ,
, ,
, .
18.

409

. U "" .
. U
, .
U, , ,
.
. U
, a Mult [ , , ) , ,
* - .
.
Mult= {fa,b,c) |a * - >
D.
,
D ,
, .

18.2.2.
, , . , ,
. ,
( ,
!). , :
( ). .
, ""
"", "" "+"
() "-" (). , , "".
. 18.1 , . ,
, " + " "-". , , = 3, = 1.
? , . 18.1,
"". , ,
, .
. ,
, . , , , , . ,
,
.
. ,
, .
?
.
W = 5 = 4,
, . W = 2
410

II. Prolog

= 2 , .
, , .
. , .
,
,
. , ,
, .

. 18.1. .
W () ().
"" ,
""
,
.
1:
2 < < 4 < 2 , "", ""
2:
> W, "", > 6 - , "",
""
3:
< 3 - (W - 3 ) , "", ""
. 18.2.
. ,
,
. 1 . 18.3.
, .
. , 1 = 3 = 2.5 , 2 3 . "" 1 , 2 3 ,
1. , 1, , 2 3. , 2 , , 3.

18,

411

. 183. ;
1 , ., 2 , 3 -

. 18.3. 1,
. , ,
. ,
W < 2,
,
( "-"). ,

18.2.3.
( ). , .
.
.
. , : , ( ); ;
; . , . ,

.
. , , , . -

412

II. Prolog

: 9 , 7 , .

. .
. ,

, .
, , , , . , .
, .
, .
, .
.
(,
Prolog).
,
. (Inductive Logic Programming ILP).
19.

18.2.4.
.
, . , L, ,
. , .
( Object, Class) , Class , Object.
L , . , ? , ,
S. S. S (.. , ), ,
, .
, ,
, S.
. 100%- . , ,
: "
, S?" , . ,
, ,
S, , S.
,
. 18.6.
. 18.

413

; ,
"", . ,
-. 18.7.

18.3.

, ,
[169]
.
, .
( ) , ARCHES.

. 18.4.

ARCHES (. 18.4). ,
, . , . 18.4, (.. ,
), ,
, .
1. ; , p o s t l
( 1), post2 ( 2) l i n t e l ().
2. p o s t l post2 , l i n t e l
, ( 1 4 . 18.4).
414

II. Prolog

3. p o s t l post2 (
2).
4. p o s t l post2 l i n t e l ( 3).
, Hi, 2 ..
. ,
.
.
Ei, 2, ..., ( Ei ) .
1. Ei Hi .
2. , Ei (i = 2,
3, . . .) .
2.1. Hi-i .; , D Hi-i Ei.
2.2. Hi-i D, ,
Ei ; Hi, .
, , . 2.1 2.2 . -.

, . 18.4.
.
ARCHES ,
. . 18.5.
, , .
, , , (. Hi . 18.5).
( 2 . 18.5)
. 2 Hi . ,
Hi .
D Hi 2. ,
, touch. , , 2 .
Hi, :

-
R, ,

R (
must_not_R).

18.

415


^~(

stable poty

rectangle

4
must_not_toucri

. 18.5. ;
;. ,
J/v,:

416

II. Prolog

Hi
2 (. . 18.5). ,
, must_not_touch ( ), , . , 2 , Hi.
, . 18.4, . 18.5. : s u p p o r t , , .

.
1. , .
2. , .
3. , .

. , , , 3.
, , support, support 2 must support (. . 18.5).
"",
, :

-
R, ,

R (
must_R).

,
, must_support. ,

must_support . ,
"-", .
, ,
.
, ^, . , 4 3, : 4 , 3 . i s a r e c t a n g l e _ o r _ t r i a n g l e
( ). ( )
. ,
, . 18.6, . , , s t a b l e poly ( ), , 4 (. . 18.5).
,
( ,
).
, . ,
.
18.

417

, . 18.7, ,
, , , , ?.

. 18.6.

. 18.7. ,
. Hj, !/ . 18.S,
, ,
, 18.6

,
.
. . , ,

.
,
, , .
418

II. Prolog


: ,
, ,
.

"-".
"" , ,
.
[12]
Prolog ,
, . ,
ARCHES. ,
.
( 18.4)
( 18.5).
,
.

18.4.
18.4.1.

, . , . . , ,
.
. .

. , .
. 18.8 ,
. : , , , , . ,
. , .
, . , , . , :
s i z e : small, l a r g e
shape: long, compact, o t h e r
h o l e s : none, I , 2, 3, many

18.

% : ,
% : , ,
% : , 1, 2, 3,

419

. 18.8. ,

,
. 18.1 , Prolog
:
example [ Class,

[ Attribucel = Vail,

A t t r i b u t e 2 <= V a i 2 ,

,,,]).

18,1.
( . 18.8)
attribute! size, [ small, large]).
attribute( shape, [ long, compact, other]).
attribute( holes, [ none, 1, 2, 3, many]).
example!
example[
example(
example!
example!
example!
example!
example!
example!
example!
example!
example!

nut, [ size = small, shape = compact, holes = 1 ] ) .


screw, [ size - small, shape = long, holes = none]),
key, [ size - small, shape = long, holes = 1]).
nut, [ size = small, shape - compact, holes = 1]).
key, [ size - large, shape = long, holes = 1 ] ) .
screw, [ size - small, shape = compact, holes = none]),
nut, [ size = small, shape - compact, holes - 1]).
pen, [ size = large, shape = long, holes = none]),
scissors, [ size = large, shape = long, holes = 2 ] ) .
pen, [ size = large, shape = long, holes = none]),
scissors, [ size = large, shape = other, holes - 2 ] ) .
key, size = small, shape = other, holes = 2 ) ) .

, , . 420

II. Prolog

,
.
n u t () key ().
nut <== [[ s i z e = small, h o l e s = 1]]
key <== [[ shape = long, h o l e s = 1 ] , [ shape = o t h e r , h o l e s = 2] ]

, .
,

.
,

:
Class

<==

Conjl,

Conj2,

...

Conjl, Conj2 ..
:
[ Attl = Vail, Att2 = Val2, ... ]

[ Conjl, C o n j 2 , . . . ] .
1. , , , Conjl, Conj2 ..
2. Conj,
, .
, ,
[ size = small, shape = long, holes = 1]

key,
. ,
Conj ; ,
.
, Conjl, Conj2 ..
; ,
.
Prolog :
match( Object, Description) : member( Conjunction, Description),
s a t i s f y ! Object, Conjunction).
s a t i s f y ! Object, Conjunction) : not (
member) Att = Val, Conjunction),
member) Att = ValX, Object),
ValX \== Val).

%
%
%

,
, , , .
, , Conjunction.

18.

421

18.4.2.
,
. , , . .
(batch learning) , (incremental learning).
,
,
. , , .
, , .
, ,
. :
, , , .

(covering algorithm),
18.2. , .
, . , .
. , , .
18,2. ; i n d u c e O n e R u l e (E) ,

RULELIST S
, , :
RULELIST := empty;
E:=S;
while E do
begin
RULE := InduceOneRule(E);
RULE RULELIST;
, RULE
end
(. 18.2)
. , ,
.
,
. , . ,
InduceOneRule , , ,
.
18.3.
learn) Examples, Class, Description)
422

II. Prolog

Description Class
Examples. , .
Class,
Examples, :
Examples Class,
Description = [] ( );
Description = [Conj I Conjs], Conj Conjs
.
1. Conj ,
Class
.
2. Examples , Conj,
Conjs .
18.3.
%
:- ( 300, xfx, <==).
% learn{ Class): ,
I
Class,
%
, Class
learnt Class)
bagof( example! ClassX, Obj), example( ClassX, Obj), Examples),

%
%
%
%

learnt Examples, Class, Description),


nl, write( Class), write(' <== ' ) , nl,
writelistt Description),
assert( Class <== Description).

% learnt Examples, Class, Description):


%
Description Class
%
. Examples
learnt Examples, Class, []) :not member! example! Class, _ ), Examples).
learn! Examples, Class, [Conj I Conjs]) :learn__conj ( Examples, Class, Conj),
remove( Examples, Conj, RestExamples),

% ,
%

%
%
%
%

learnt RestExamples, Class, Conjs).

,
Conj

% learn_conj( Examples, Class, Conj):


%
Conj - ,
%
Class -
%

learn_conj( Examples, Class, []) :not ( member( example! ClassX,
), Examples), % -
ClassX \== Class), ! .
%
learn_conj( Examples, Class, [Cond | Conds])
choose_cond( Examples, Class, Cond),
filter( Examples, [ Cond], Examplesl),
learn_conj( Examplesl, Class, Conds).

:%

choose cond( Examples, Class, AttVal) :findall( AV/Score, score! Examples, Class, AV, Score), AVs),
best! AVs, AttVal).
%

18.

423

best( [ AttVal/_], AttValJ.


best( [ AVD/SO, AV1/S1 | AVSlist], AttVal) :SI > SO, !,
% AVI , AVO
best{ [AV1/S1 ] AVSlist], AttVal)
best( [AVO/SO | AVSlist], AttVal).
% filter( Examples, Condition, Examplesl):

Examples! Examples,
%
Condition
filter( Examples, Cond, Examplesl) :findall( example[ Class, Obj) ,
( member( example{ Class, Obj) , Examples), satisfy( Obj, end)},
Examplesl).
% remove| Examples, Conj, Examplesl):
Examples , Conj,
%
Examplesl
remove ( [ ], _, [ ]) .
remove[ [example < Class, Obj) | Es], Conj, Esl) :satisfy! Obj, Conj), !,
% Conj
rem.ove ( Es, Conj, Esl) .
%
remove[ IE I Es], Conj, [E | Esl])
remove( Es, Conj, Esl),

:-

satisfy; Object, Conj) :not ( member; Att = Val, Canj),


menberi Att - Valx, Object),
ValX \ ~ Val) .
score( Examples, Class, AttVal, Score) :candidate( Examples, class, AttVal),
%
filter( Examples, [ AttVal], Examplesl),
% Examplesl
% Att = Val
length( Examples!, N1),
%
count_pos[ Examplesl, Class, HPosl), %
NPosl > 0,
%
% AttVal
Score is 2 + NPosl - Ml.
candidate; Examples, Class, Att = Val) :attribute! Att, Values),
member; Val, values),
suitable; Att - Val, Examples, Class).

%
%

suitable! AttVal, Examples, Class) :%


% AttVal
member{ example[ ClassX, ObjX), Examples),
ClassX \== Class,
,
not satisfy; ObjX, [ AttVal]), !.
% AttVal
% count_pos( Examples, Class, B ) :
%
- Class
count_pos( [], _, 0 ) .
count_joos { [example ( ClassX,_ ) ( Examples] , Class, M)
count_pos( Examples, Class, Ml),
( ClassX = Class, !, N is Ni + 1; = N1).

424

:-

II. Prolog

writelist t [)).
w r i t e l i s t [ [X I L]) :tab [ 2), -write ( X) , nl,
w r i t e l i s t ( L).
:
learn_conj( Examples, Class, Conjunction)
Conjunction ,
, :
Attribute = value
, ( ). , , Class.

. , , , , , . , . ,
,
, .
.
. , 18.3, ,
. , .
-
. , (greedy), " " (hill-climbing). , , .
, .

: , , .
.
. 18.9
. , ,
:
score[ Examples, Class, AttributeVslue, Score!
Score .
, 18.3,
, 18.1,
:
?- learn( nut), 1( key), learn( s c i s s o r s ) .
nut < = =
[ shape = compact, holes = 1]
key < = =

18.

425

[ shape - othsr, size - small]


( holes = 1, shape = lor.g]
scissors <==
[ holes = 2, size = large]

. 18.9.
; POS . NEG

. . ATTVAL, ,
,
.

ATTVAL

, l e a r n ,
, . . ,
, ,
classify! Object, Class) :Class <== Description,
% , Class,
%
member[ Conj, Description), %
satisfy( Object, Conj).
* Object Conj

18.5.
18.5.1.
, -,
, . .
. 18.10 , , 18.1 (.. , , 18.8). .
"nu.Il", , . . , .
426

II. Prolog

, . , , :
[ size small, shape = compact, holes 1]
nut { h o l e s = 1, shape = compact). ,
s i z e = small .

. IS.10. ,
, 18.1 (
. 18.8)

, , ,
. .
;
, . .
, , ,
.
. . ,
, .
. 18.11.
, .
- .
, . , . 18.11, ,
, , ,
. , , ,
.

18.

427

S,
;
S . ,
, ,
,

- " " , , v,..., v n ;
- S S., ..., S ;
- () .., ...S ;
, ,
, , v t
v^;
:

. 18.11.

, .
1. S ,
, " n u l l " .
2. ,
.
3. S , S , , ,
. , S S.
(class probability tree), .
( ,
, ).
4. .
.

18.5.2. ""
"" . ,
(impurity)
. (
).

. . .

.
. ,

428

II. Prolog

. , . . , 3, I, ,
:

V
I = -

LJ

() 1 ()

, {) , S . . , ,
1, 0. 1 = 0.
, I , .
S . 1 5
:
1(> = - j p{v) 2-i pfc I v) log; ( I v)
v , (v) v S,
( I v) , , v. (v) ( | v) S.

. ,
. ,
, S ,
, ,
. Ires
.
.
I (1,
, ,
I (A) = - 2 J p[v) og2 (p(v) }
I () .
:
GainRatio(A) =

Gain(A}
1 ( )

I - 1~()

, .
,
, . , (biiiarization of attributes).
. () , .
18.

429

, ,
, ..
,
.
.
().
, Gird, :
Gini = 2-1

p(i) p(j)

i H I . Gini :

G i n i [A)

Z_i

p(v)

L J

p(i

v)

p(j

\r)

v A, p ( i | v] i, ,
v.
, ,
, .
,

. .
, .

18.1. (. 18.1). , " s i z e " " h o l e s " , . , , ,


p(nuL) = 3/12 p t n u t I h o l e s = l ! = 3/5.
18.2. D 25% . S 75% , ,
. ,
, , D
( D) -D ( ). S .
S?

18.5.3.


:
induce_tiee( Attributes, Examples, Tree)

430

II. Prolog

Tree ,
Examples A t t r i b u t e s . , 18.1, , , :
induce_tree( Tree) :findalU example! Class, Obj), example( Class, Obj), Examples),
findalK Att, a t t r i b u t e ; Att, _ ) , Attributes),
induce_tree( Attributes, Examples, Tree).
.
1. Tree = n u l l , .
2. Tree = l e a f ( C l a s s ) , Class.
3. Tree = t r e e { A t t r i b u t e , [ Vail : SubTreel, Val2 : SubTree2, . . . ] ) ,
, A t t r i b u t e
, V a i l , Val2, ...
A t t r i b u t e , a SubTreel, SubTree2, ... .
:
% induce_tree( Attributes, Examples, Tree)
induce_;ree(_, [ ] , null) ;- 1.
induce_tree! _, [example( Class,_) I Examples], leaf( Class)) :not ( member[ example( Classx, _ ) , Examples),
%
Classx \- Class), !.
%
induce_tree( Attributes, Examples, t r e e ! Attribute, SubTrees)) :choose_attribute( Attributes, Examples, Attribute),
delete; Attribute, Attributes, RestAtts),
Attribute
attribute) Attribute, Values],
induce__tree.s ( Attribute, Values, RestAtts, Examples, SubTrees) .
induce t r e e s SubTrees
Examples Values
A t t r i b u t e :
% induce_trees( Att, Vals, RestAtts, Examples, SubTrees)
induce_trees( , [], _, _, [])
% ,
inducetrees( Att, [Vail | Vals], RestAtts, Exs, [Vail : Treel | Trees]) :attval_subset( Att = Vail, Exs, ExampIeSubset),
induce_tree( RestAtts, ExampIeSubset, Treel),
induce_trees[ Att, Vals, RestAtts, Exs, Trees).
a t t v a l _ s u b s e t ( A t t r i b u t e = Value, Examples, Subset) , Subset -- Examples,
A t t r i b u t e = Value :
attval_subset[ Attribute Value, Examples, ExampleSubset) :findall( example( Class, Obj},
( member; example( Class, Obj), Examples),
satisfy! Obj, [ AttributeValue])), ExampIeSubset).
s a t i s f y ! Object, D e s c r i p t i o n ) 18.3. c h o o s e _ a t t r i b u t e , .
.
setof.
, .
18.

431

choose_attribute( Atts, Examples, BestAtt) :setof( Impurity/Att,


( member[ Att, Atts), impuritylf Examples, Att, Impurity)),
[ Minlmpurity/BestAtt I _ ) ) .

impuritylt Examples, Attribute, Impurity)

;
Impurity
Examples A t t r i b u t e .

18.3. , i m p u r i t y ! .
, ,
Gini, . , 18.1, s i z e Gini

:
?- Examples = . . . % , 18.1
impurity]. ( Examples, size. Impurity).
Impurity - 0,647619
,
Impurity :
Impurity = Gini( size)
[small)*((nut I small)*p(screw | s m a l l ) + . . . ) + p ( l a r g e ) * ( . . . )
=
=

7/12
7/12

*
*

(3/7 * 2/7 +
. . . )
0 . 6 5 3 0 6 1 + 5 / 1 2 *

0.647619

+ 5/12
C.64

* ( . . . )

18.4. ,
, , ,
18.1. ,
c h o o s e _ a t t r i b u t e , setof,
.

show( DecisionTree!

, . ,
, . 18.10, :
holes
none
size
small = > screw
large => pen
1
shape
long ==> key
compact =-> nut
other ==> null
2
size
small > key
large > scissors
3 ==> null
many --> null

432

II. Prolog

18.6.

, , . . ,
. , , . , ,
,
. , ,
,
.
. , ,
. ,
.
:
-, , , -,
. , .
,
.
,
S. , S 100 , 99 1, 2. ,
, , , , 2 S
. , ,
1. , , ,
.
.
, ,
. , , ,
..
, , (forward pruning),
, (post-pruning).
,
. , , .
. 18.12.
. , . ?
18.

433

. 18.12. .

,
, .. ,
- . ,
, . ,
. ,
.
. , , .
.
( ) , , . , , .
, , . ,
. , , ? ,
, , . ,
(minimal error pruning).
, . . 18.13, , s, ;, ... , pi , = _. I, ,
. ,
s (..
., ...). ,
. , ...
.
, .
? , .. . , .
434

. Prolog

. 18,13.

1.
s, . s
s , ,
. s , , s, , . (static error) s :
(s) = pfclass * I )
2. s,
E(Ti), { 2 ), ...
Ti, , .... . :
: + 2 ( 2 ) +

(backed-up error).
, s
: ,
, .
,
,

() = m i n ( e ! s ) , i L /
1

P i

, , () =
(s) .
, e ( s ) , ,
. , ,
, s. ,
S, 5 N, . s. , , , / ( )
s. , s , , , ,
. , , s
. 1/1 * 100 = 100%, 0/1 = 0. s , 18.

435

. ,
s .
1/2 * 100 = 50%!
, , . , . ,
1, , , 0,5.
"" ,
0,5, 1, , .
,
, .
, , .
, -, . . m- ;
" N +
, m .
.
, , . . ,
.
, ;
. m-
. , ,
. m- , .
m- :
m
.
N
= ^ + * ^
m-;
, , N . , N = 0 = .
(N ), n/. . m (m > G)
m, .
m . ,
,
m (, m = 100) . ,
, , , (, . = 0. 2)
. , ,
. , 436

II. Prolog

. ,
.
, .
, .
, (
s), , .
( ) ,
. m-, . ,

1/k,

m =

,
5 . , ,
, . ,
.
. 18.14
.

[ 3 , 2 ] . , 1
2. , ,
:
e(b_left) = 1 - 7 = 1 - 5 + 2

0.429

b (b_right) =
0. 333. :
() = 0.375
:
BackedUpError(b) = 5/6 * 0.429 + 1/6 * 0.333 == 0.413
,
b
:
()

0.375

m-
. m-
, . .

, .
, ,
m-
.
m
.
18.

437

. 18.14. . , 1 2, . , , , .
, .
( )


.
. , .
5 (growing set) (pruning set). , "" . , , ,
.
,
-

438

II. Prolog

. . , , ,
. , , , , ,
, .

18.5. . , , ,
, . 30 25 . , ,
. ,
, ,
30%, , ,
, 50-60%. , ,
, m-.
?
18.6.
prune tree { Tree, PrunedTree)
Tree
, . Tree ,
. .
,
number of c l a s s e s ( ) .

18.7.

ARCHES. ARCHES
. ,
, ,
, , . ,
, ,
.

.
,
, .
. ,
" ";
, , , . , ,
. , , Prolog (. 18.3)
.

18.

439

,
Prolog .
.
.

18.7.1.
, , .
. ,
, .
, .
1. , , , S.
2. S. (, ,
.)
() , . , , -
.. , , . .
[100]. ,
,
.
.
.
, , .
1. (,
).
2. ( ).

18.7.2. ,
,
, , , . , , , , . ,
, .
: . , ,
, .
440

II. Prolog

- ,
. . ,
. , . , .
, .
, .
,
. ( , )
. , .
{k-fokl cross-validation).
.
: i-e ,
, i-e . ,
. , - 10.
,
.
, ,
.
(leave-one-out). , .

18.7.3.

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

.
18.

441

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

.
10- . , .
, m-. ,
, UCI
Repository for Machine Learning ( , . ; h t t p : / /
www.Ics,uci.edu/~mlearn/MLRepository.html).


, . .
.
:
, ;
;
;
.
, "" . ,
. ,
,
.
. .
.
. ,
.
:
,
, ;
442

II. Prolog

,
, .

.
.
, -.
,
, :
;
, ;
, -,
, -,
.
, ,
. 10- .
.
;
;
, ;
;
;
;
;
;
, ARCHES;
;
;
;
, ,
;
;
.



[105]. [97] , ,
.
;
Machine Learning (Boston: Kluwer) Artificial
Intelligence (Amsterdam: North-Holland),
ICML (International Conference on Machine Learning
18.

443

), ECML (European Conference on Machine


Learning ) COLT
(Computational Learning Theory ).
[75], [98]
[99]. [59] .
UCI
( , . ; h t t p : //www. i c s . u c i . eduZ-mlearn/
MLEepository. html).
,
( 18.3), ,
ARCHES [169]. ,
18.4, , AQ. AQ (Michalak!) (., ,
[96]).
CN2 [31].
; TDIDT (Do%vn Induction of Decision Trees ), [128]. TDIDT
. (Quinlan) ID3 Iterative Dichotomizer 3
[127],
ID3. (Breiman) [23], ,
.
[114] m- [27]. - [26] . [47] . ,

,
, . , , , -, [28] [12S].
[145].

, .
, , [137].
, .
[104] , .
,
[155]. D , ,
, ,
, .

444

II. Prolog

,
[76], , .
( [105]), . ,
. , . , , , .
(, [106]);
.
"" . , .
23 .

. ,
.
AM (Automatic Mathematician ) [88]. , AM
(, ),
, , , .. [149]
.

, (Inductive Logic Programming ILP), .
COLT
(Computational Learning Theory ) [71].
: " , ?
?"

18.

445

19

...
19.1.
19.2. Prolog
19.3. HYPER

446
449
459

(Inductive Logic Programming ILP)


,
. ILP , Prolog. , Prolog
. , ,
,
.
ILP , .. , . ILP .
ILP HYPER (HYPothesis
refiruER). ILP.

19.1.

. .
ILP .
,
Prolog.
, ( ) . ,
. ( ) , ,
{ ) ,
. , ,
.
Prolog. ,
p a r e n t (X, Y), male (X) female (X),

(. . 1.1 1).
has_daughter(X). , ,
. , .
has_daughter{torn;, has_daughter(bob)
:
has_da.ughter (pam) , has_daughter ( jim)
,
has_daughter (X) p a r e n t , male female ,

. ILF
has_daughter:
has_daughter[X) :parent! Y),
female( Y>.

.
., { )
. h a s _ d a u g h t e r X
X.
has_daughter , Y, X,
.
,
ILP. ,
, has_daughter, .
p a r e n t , male female (Background
Knowledge ), . ,
. , , , .
ILP, .
:
1. + .;
2. , , ,
. .
:
, , ,
1. + ;
2, _ .
, . ( ) ( , .. ).
, Prolog,
, Prolog. Prolog . , (X)
{)
(). :
?- () .

?- { .

%
%
%
%

19.

447

, ILP,
.
, ,
:
?- ( ) .
%
yes
I
?- (),
%

%
ILP Prolog .
,
, ,
. 1LP HYPER .
, ILP ,
, , ,
. ILP ,

. , .
.
ILP , ,
Prolog. .

, . ,

, . , ILP .
, ,
, .
, ,

. , ,
, . , ILP
.
ILP .

ILP .
, .
, , JLP.
, , .
ILP, HYPER (HYPothesis
refinlvR), Prolog .
,
MINIHYPEH. HYPER.

448

II. Prolog

19.2. Prolog

19.2.1.
,
.
, , 19.1.
. 1.1 (. 1) , . ,
, ,
, 19.1.
ex (Example) , , :
ex( has_daughter(torn)). %
19.1. h a s _ d a u g h t e r
% ,
%
backliteral( parent(X,Y),

[X,Y]).

% [X,Y]

backliteraK male(X), [X]).


backliteral( female(X), [X]).
prolog_predicate( parent(_,_)).

% parent(_,_)
% Prolog

prolog_predicate( male(_)).
prolog_predicate( female(_)).
parent( pam,
parent( torn,
parent( torn,
parent( bob,
parent( bob,
parent( pat,
parent( pat,

bob).
bob).
liz).
ann).
pat) .
jim).
eve).

female( pam).
male( torn).
male( bob).
female( liz).
female( ann).
female( pat).
male( jim).
female( eve).
%
ex( has_daughter(torn)).
ex( has_daughter(bob)).
ex( has_daughter(pat)).

%
nex( has_daughter(pam)).
nex( has_daughter(jim)).

start^hyp( [ [has_daughter(X)] / [X] ] ).

19.

449

nex (Example), , .
nex ( has_daughter (pair.) ) . %
b a c k l i t e r a l ( background literal )
, ILP
Prolog. ,
backliteral( parent(X,Y), ;X P Y;>.
, p a r e n t (X,Y), X Y .
b a c k l i t e r a l . , .
, Prolog. p r o l o g _ p r e d i c a t e , Prolog1. ,
prolog^predicate( parent(X,Y)).
, , p a r e n t (X,Y), Prolog , p a r e n t . , has d a u g h t e r (torn),
, Prolog, ,
ILP.

19.2.2.
, , 19.1.
, ( ), ( , , .. ). ,
.
, . Hi ;,
, 1 , Hi.
. . 19.1
, 19.1. , . Hi ? , ; Hi.

. .
. , . 19.1, ,
, .
.
1. , ( ).
2. .
, . 19.1, . , .
1. .
2. .
450

II. Prolog

. 19.1. , , 19,1.

:
hae_daughter(X) :- patently,Z) .

has_da-jghter ;xj ;- parent tx, Z) .
X=Y.
,
has_daughter(X).
:
has_daughter(X)

:- p a r e n t [Y,Z) .

. -, . ,
, .
(
).
.
-, ""
.
. , ,
,
.
, . ,
member! XI, LI) :- member! XI, L3) .
:
member! XI,

[2

L2]}

: - member[ XI,

L3).

19.

451

L1
[2 | L2]. MIN1HYPER, , , . , HYPER,
. 19.1 ,
. MINIHYPER
. HYPER
.

19.2.3. MINIHYPER
ILP, :
Hypothesis =

[ Clausel, Claused,

...

( , ) ,
.
Clause

[Head,

BodyLiterall,

BodyLiteral2,

. . . ] / [

Varl,

Var2,

...

,
pred(X,Y)
pred(X,z)

:- p a r e n t (X, Y) .
: - p a r e n t (x, Y),

pred(Y,z;.

:
[

[ p r e d ( X l , Y D , p a r e n t (XI, YD ]
pred[X2,Z2)j / [X2,Y2,Z2] ]

[XI, Y l ] ,

! p r e d ( X 2 , Z2J ,

p a r e n t 1X2, Y2) ,

, . ,
, .
, ,
Prolog , , .
:
prove[ Goal, Hypothesis, Answer)
Goal Hypothesis
Answer, , Goal
Hypothesis. ,
Goal Hypothesis ,
Prolog. , 23,
,
.
, :
[ [),

() ]

() :- (), . , prove
. ,
. Goal
, prove , . , Answer prove ,
452

II. Prolog

Answer = yes. Goal Hypothesis


.
Answer = no. , Goal Hypothesis, .
Answer = maybe. , D.
"Answer = maybe" , Goal .
1. Prolog ( )
.
2. Prolog , D.
3. Prolog - , D,
.
{ D), ,
.
prove 19.2. :
max_pEOof_length(

D)

D 6, .
( p r o l o g _ p r e d i c a t e )
Prolog . , ,
.
19.2. ,
%
* prove{ Goal, Hypo, Answ):
*
Answ = yes, Goal Hypo
%
, D
%
Answ = no, Goal
%
Answ = maybe, D
prove( Goal, Hypo, Answer) :max_proof_length{ D ) ,
prove( Goal, Hypo, D, RestD),
(RestD >= 0, Answer = yes
RestD < 0,

!, Answer - maybe

%
% , ,
%

).
prove! Goal, _, no).

% Goal
%

$ prove( Goal, Hyp, MaxD, RestD):


%
MaxD - , RestD - , "
%
" ;
%
, Hyp
prove [ G, H, D, D)
D < 0, !.

:%

19,

453

prove( [1, _, D, D)

:-

!.

prove{ [Gl | Gs], Hypo, DO, D]


prove( Gl, Hypo, DO, Dl},
provef Gs, Hypo, Dl, D ) .

:- !,

prove! G, _, D, D) :prolog_predicate( G ) ,
call( G ) ,

% Prolog?
%

prove( G, Hyp, DO, D) : DO =< 0, I, D is D0-1

% -

Dl is D0-1,
i
member( Clause/Vars, Hyp),
% Hyp
copy_term( Clause, [Head | Body] ), %
G - Head,
%
prove( Eody, Hyp, Dl, D).
% G Clause

"maybe", ,
"" ( ), .. , .
1. , "maybe"
, " ".
2. , "maybe"
, "
". , , .
"maybe" ,

. ""
, , .
MINIHYPER 19.3. , .
19,3. ILP - MINIHYPER
MINIHYPER
% induce( Hyp):

Hyp
%

induce( Hyp) :iter_deep( , 0 ) .

h ,
% 0

iter_deep( Hyp, MaxD) :write( 'MaxD = ' ) , write( MaxD), nl,


start_hyp( HypO),
complete! HypO),
%
depth_first( HypO, Hyp, MaxD)
%
NewMaxD is MaxD + 1,
iter_deepi Hyp, NewMaxD).
% depth_first( HypO, Hyp, MaxD] :

HypO
%
Hyp MaxD

454

II. Prolog

depth_first( Hyp, Hyp, _)


consistent( Hyp).

:-

depth_first( HypO, Hyp, MaxDO) :MaxDO > 0,


MaxDl is MaxDO - 1,
refine_hyp( HypO, Hypl),
complete( Hypl),
% Hypl
depth_first( Hypl, Hyp, MaxDl).
complete( Hyp)
:% Hyp
not (ex( E ) ,
%
once( prove( , Hyp, Answer)),
% Hyp
Answer \== yes).
% ,
consistent( Hyp)

:-

% ,
%
not (nex( E ) ,
%
once( prove( , Hyp, Answer)),
% Hyp
Answer \== no).
% ,

% refine_hyp( HypO, Hyp):


%
,
%
Hyp
refine_hyp( HypO, Hyp) :cone( Clausesl, [ClauseO/VarsO | Clauses2], HypO),
%
% ClauseO
cone( Clausesl, [Clause/Vars | Clauses2], Hyp),
%
refine ( ClauseO, VarsO, Clause, Vars). % Clause
% refine ( Clause, Args, NewClause, NewArgs):
%
, Clause
%
Args NewClause NewArgs
%
refine( Clause, Args, Clause, NewArgs) :cone( Argsl, [A | Args2], Args),
%
member( A, Args2),
%
cone( Argsl, Args2, NewArgs).
%
refine( Clause, Args, NewClause, NewArgs) :length( Clause, L ) ,
max_clause__length ( MaxL) ,
L < MaxL,
backliteral( Lit, Vars),
%
cone( Clause, [Lit], NewClause),
%
cone( Args, Vars, NewArgs).
%
% ,
max_proof_length( ) .

%
% , Prolog

max_clause_length( 3 ) .

refine ( Clause, Vars, NewClause, NewVars). Clause Vars


NewClause
NewVars.
Vars

19.

455

Clause. ,
( rtax_clause_length (MaxL)).
r e f i n e hyp{ Hyp, NewHyp}. Hyp
NcwEiyp,
Hyp
.
induce [ Hyp). Hyp , (
s t a r t _ h y p (StartHyp)).
i t e r _ d e e p ( Hyp, MaxD) . Hyp
, MaxD
, Hyp.
iter__d.eep (Hyp, 0) .
d e p t h ^ f i r s t ( , , MaxD). , MaxD, . MaxD, Hyp , MaxD .
c o m p l e t e ; Hyp). , Hyp ( Answer prove/3).
c o n s i s t e n t ( Hyp). , Hyp ( Answer prove/3).
M1NIHYPER
Prolog , 19.2 19.3,
member/2, cone/3, n o t / 1 , once/1 copy_term/2 ( 19.1). ,
max_proof_length max_clause_length .
, has__daughter (X) , 19.1, :
?- induce (H) .
MaxD = 0
MaxD = 1
MaxD = 2
MaxD = 3
MaxD = 4
H = [ [has_daughter(A),parent(A,B),female(B)]/[A,B]]

MaxD = Limit. 4 (. . 19.1).


, , 105, 25 . ,
, Prolog :
has_daughter() :- parent(,), female;).
.

456

II. Proiog


19.1. MINIHYPER, , h a s _ d a u g h t e r .
?
:
p r e d e c e s s o r () ,
19.1. ,
,
ILP.
:
ex( predecessor( pam, bob)),
ex( predecessor! pam, jim)).
ex( predecessor( torn, ann)).
ex( predecessor( torn, jim)).
ex( predecessor! torn, liz)).
nex( predecessor! liz, bob)),
nex( predecessor! pat, bob)),
nex ( predecessor! pam, liz)).
nex ( predecessor! liz, jim)).
nex( predecessor! liz, liz)).
"", (
), :
start_hyp( [ [predecessor(XI,Yl)] / [XI,Yl],
[ p r e d e c e s s o r ( X 2 , Y 2 ) ] / [X2,Y2] ] ) .
.
backliteral! parent(X,Y), [X,Y]).
backliteral( predecessor(X,Y), [X,Y]).
prolog_predicate( parent(X,Y)).

19.2. , .
MINIHYPER
, ,
. . ,
, . .
MINIHYPER HYPER
.
MINIHYPER p r e d e c e s s o r .
( "" , )
, p a r e n t (X,Y)
p r e d e c e s s o r (X, Y)
X. , , X , ..
:
b a c k l i t e r a l ! [atom(X), p a r e n t ( X , Y ) ] , [X,Y]).
b a c k l i t e r a l ! [atom(X), p r e d e c e s s o r ( X , Y ) ] , [X,Y]).
prolog_predicate( parent(X,Y)).
prolog_predicate( atom(X)).
, # atom(X) .

19.

457

, X ( atom(X)). ,
. , , ,
Prolog. induce (H) :
=

[[predecessor(,),[atom(),parent(,)],[atom(),predecessor(,)]]
/ [, , ] , [ p r e d e c e s s o r ( D , E ) , [ a t o m ( D ) . p a r e n t ( D , E J ] ] / [ D , E ] )

, p r e d e c e s s o r .
, MINIHYPER , .
.

19.3. MINIHYPER p r e d e c e s s o r .
?
"" atom[X) ,
. , [atora(X) , p a r e n t (X, Y) 3,
, , ( X
, - ).

, X . , . ,
.
atom[X) , : . , , , !
. , .

19.2.4. , -
,
ILP
" , " " , ". Hi
, ^, -, , , ,
:.
.
.
ILP ,
- . ,
.

458

II. Prolog

6 (). 6 = {Varl/Terml,
Var2/Term2, . . . } Varl, Var2 ..
Terml, 2 .. 9
,
9. 9 9,
, .

has_daughter(X)
{ X/tom,

:-

parent(X,Y),

female(Y).

Y/liz}

CG = h a s _ d a u g h t e r ( t o r n )

:- p a r e n t ( t o r n , l i z ) ,

female(liz).

-.
. - - , 9, ,
Ci9 . ,
parent(X,Y).

-
parent(X,liz).
9 = ( Y/liz) ,
has_daughter(X):-parent(X,Y).

-
has_daughter(X) :- parent(X,Y), female(Y).

9 = { }. - .
Ci - , Ci,
Ci , 2. , Ci
(, , ), , , . - . Ci , , Ci - .

19.4. , :
num(0).
, Ci
num(

s(X))

:- n u m ( X ) .

2 :
num( s ( s ( X ) ) ) :- num( X).
{C 0 ,Ci} , { 0 , 2 }? Ci - ?

19.3. HYPER

ILP, 19.3.
1. .

.
19.

459

2. :

.
3.
, .
4. .
.

19.3.1.
,
MINIHYPER, .
member (X,L)
( 19.4).
b a c k l i t e r a l ( m e m b e r (X, L ) ,

[Lilist],

[X:item]).

, meiaber(X,L) ( ), L , X
. .
backliteral{ Literal, InArgs, OutArgs)

InArgs OutArgs :
InArgs = [Inl:TypeIl, In2:TypeIZ, . . .
OutArgs - [Out 1 : T y p e 0 1 , O u t 2 : Type02,

]
...

Inl, In2, ... , a Typell, TypeI2, ... .


, OutAugs .
19.4.

i member(X,L)
backliteral( member(X,L), [L:list I, !X:item] ),


term list, [X|LJ, [ X:ltem, L:list]>.
ternU list, [), [1) .
prolog_p^edicate( fail).
I Prolog
start_cl.ause ( [ member [x,I,> ] / [ X:item, L:list] ).
%
ex I member! a, [a])) .
ex( member[ b, [a,b])).
ex ( member ( d, [a,b, c, d, e] )) .
nex( member( b, [a])).
nex ( member ( d, [a,b] ) ) .
nex ( member* f, [a,b, c,d, e] ) ) .

460

II. Prolog

. , , . ,
,
.
L
( "")
member (X,L) . ,
.
, .
,
MINIHYPBR. . X item () L l i s t ().
:
term! TypeF TeriT;, Vars)

,
Term; Vars ,
.. 19.4
term[ l i s t ,
termtlist,

[X|L],
[],[])

X:item,

L:list]|.

, l i s t [XIL], X item, a L l i s t ,
[] ( ).
, " : " .
19.4
start_clause( [ nexJaer(X,L) J / [ X:item, L : l i s t ] ) .
.
,
(). . max_clauses.
.
HYPER .
,
1. , XI = 2.
.
2. . , term/,
.
3. .
() ( ).
MINIHYPER, .-:. HYPER . , . 19.

461

re 19.5
member.
19.5.
. ,

member[XI,L1) .
member(X2,L2).
L \ LI = [X3|L3J
member[XI, [ X 3 | L 3 ] ) .
member(X2, L2) .
i- XI = X3
member(XI, [ X I | L 3 ] ) .
member[X2,L2).
i- L2 = [X4[L4]
member(XI, [XIj L 3 J ) .
member(X2, [ X 4 I L 4 ] ) .
i member 1X5, L.5) L5 - L4
member(XI,
[XI|L3]).
T.ember(X2, [ X 4 I L 4 ] ) : - member ( X 5 r L 4 ) .
I 2 = 5
meiLber ( X I , [XII L3] ) .
member[X2, [ X 4 | L 4 ] J :- member (X2,L4>.

HYPER ,
. ,
, , , .
,
.
, "" ( ). . ,
prove .
(Least Specific Specialization LSS). ;
,
;, , . LSS. L.SS ,
.
LSS
. ,
- , . , ,
, .
,
, .

462

II. Prolog

19.3.2.
.
, . ,
.
, conc( LI, L2, L3). (
, ). , . , , , .
HYPER
Cost (Hypothesis), (
), .
:
Cost [HI = wi * Size(HI + 2 * WegCover(H)
NegCover(H) ,
, " "
Answer prove. wi wj
. :
Size () = -, * _ () + * _ ()
HYPER, ,
: w-_ - 1, w; = 1, ki = 10, k; = 1.
.
Cost() - _() + 10 * _()
+ 10 * NegCover(H)

, .
, . , . , .
,
, , . , [18].

19.3.3. HYPER
19.6
HYPER. .
19.6. HYPER. p r o v e / 19,2
I HYPER (HYPothesis refinER)
&
:-

( 500, xfx,

:} .

% induce ( Hyp) :
19.

463

%
%

Hyp ^

induce( Hyp) :init_cour.tsr !,


start_hyps( Hyps),
best_search[ Hyps, _:).

%
I
%
%

% best_search[ CandidateHyps, FinalHypothesis)


best_search( [Hyp | Hypsl, Hyp) :show_counts,
%
Hyp = 0:,
% Cost = 0; ,
%
complete[) .
%
be3t_search( 50: I KypsO], ) :write('Refining hypo with cost ' ) , write( CO),
write(:), nl, show_hyp(HO), nl,
all_refinements{ HO, NewHS),
*
addhyps( HewHs, HypsO, Hyps), !,
addl ( refined),
%
best_search( Hyps, H) .
all_refinements( HO, Hyps) :findalK C:H,
1 refine_hyp(H0,H) ,
once 1( addl( generated!,
complete(H),
addl( complete),
eval[H,C)

i H -
*
%
%
%
\
% -

)) ).
Hyps).
% add^hypst Hypsl, Kyps2, Hyps):
%
Hypsl Hyps2
*
Hyps
add_hyps( Hypsl, Hyps2, Hyps) :mergesorti Hypsl, OrderedHypsl),
merge ( Hyps2, OrderedHypsl, HypsJ,
complete! Hyp) :not ( ex I P),
oncei prove) P,
Answ \-- ).

% Hyp
%
Hyp, Answ)),
% Hyp
% ,

% eval( Hypothesis, Cost):


%
Cost = Size + 10
i
* ___,

Size -
eval[ Hyp, Cost) :size( Hyp, S),
covers_neg< Hyp, N ) ,
( N - 0, !, Cost is 0;
Cost is S + 1O*N) .

%
\
I

% size { Hyp, Size) :


%
Size = kl _ + k2
%
* ___;
%
: kl=10, k2=l

464

II. Prolog

!< [ ] , 0 ) .

( [CsO/VsO 1 RestHyp], Size]


;ngth(CsO, LO),
sngthf vsO, MO) ,
iae( RestKyp, SizeRest),
ize is 10*LO + NO + SizeRest.

:-

overs_neg[ H, KJ:
U - , , ,
. , ,
, prcve/ 'yes' 'maybe'
r

ers_neg( Hyp, N) :% N
indall ( 1, ((), once(prove(,Hyp,Answ)), Answ \=- no), L ) ,
.ength( L, N) .
msatisfiable( Clause, Hyp):
Clause -
; , Clause *
Hyp
satisfiable( [Head I Body], Hyp) :once( prove( Body, Hyp, Answ)), Answ = no.
art_hyps ( Hyps) :~t
7Tiax_clauses ( M) ,
setoff C:K,
(start_hyp(K,M) , addl(generated) ,
complete(H) , addl(complete) , eval(H,C)} ,
Hyps).
start_hyp( Hyp, HaxClauses]:
Hyp - ,
MaxClauses
:art_hyp( [) , _) .
tart_hypl 1 I C s ) F )
> 0, Ml is M-l,
starc_clause( ) ,
statt_hyp t Cs, Ml).

:%

refine_hyp( HypO, Hyp):


, Hyp

efine_hyp{ HypO, Hypi :choose_clause( HypO, ClauseQ/VarsO, Clauses!, Clauses2), %
cone( Clauses 1, tClause/Vars [ Clauses21, Hyp),
%
refine( ClauseO, VarsQ, Clause, Vars),
4
%
non_redundant( Clause),
% Clause
not unsatisfiablet Clause, Hyp).
% Clause
%
choose_clause[ Hyp, ClauseF Clausesl, Clauses2) :- % ,
% Clause
cone { Clausesl, [Clause | Clauses2] , Hyp),
%
(),
|
prove( E, [Clause], yes),
% Clause
!
%
cone( Clausesl, [Clause | Clauses2], Hyp).

19.

i
%

465

refine( Clause, Args, NewClause, NewArgs):


%
, Clause
% *^ Args - NewClause NewArgs
%
refine{ Clause, Args, Clause, NewArgs) :cone( Argsl, [A | Args2], Argsl,
%
me:rJaer ( Ar Args2),
l
cone I Argsl, Args2, NewArgs).
%
refine{ Clause, ArgsO, Clause, Args}
del( Var:Type, ArgsO, Argsl),
*
i
term( Type, var, vars),
%
cone( Argsl, Vars, Args).
i

: Var:Type
ArgsO
var Type

%
rciine( Clause, Args, NewClause, KewArgs)
length( Clause, L ) ,
max_ciause_le:igth ( MaxL) ,
L < MaxL,
backliterall Lit, InArgs, RestArgs),
%
cone[ Clause, [Lit], NewClause],
h
connect_inputs( Args, InArgs},

%
cone[ Args, RestArgs, KewArgs).
i

:-

% redundant \ Clause) : , Clause


%

non_redundant( [_]).

non_redundant( [Litl I Lits]) :not literal_member( Litl, Lits),


non_redundant( Litsi.
literal_member( X, [XI I Xs])

:-

%
%

X == XI, !
literal_mensber [ X, Xs) .
% show_hyp( Hypothesis}:
%
, Hypothesis ,
%
, , ...
show_hyp( [])

:-

nl.

show_hyp( [C/Vars | Cs]) :- nl,


copy_term( C/Vais, Cl/Varsl),
name_vars ( V a r s l ,
s h o w _ c l a u s e [ CD ,
show_hyp[ C s ) , ! .

['A', ' E ' , ' C , ' D ' , ' E ' , ' F ' , ' G ' , ' H ' , ' I p , ' J 1 ,

s h o w _ c l a u s e ( (Head | Body])
:write [ Head),
( Body = [ ] ; w r i t e ! ' : - ' ) , n l
write_body< Body).
w r i t e _ b o d y ( []}
w r i t e { ' . ' 1, ! .

466

Kr, 'L1, ' ' , ' ' ] ) ,

),

:-

II. Prolog

write_body{ |G I Gs]) :- !,
tab( 2), write! G},
( Gs = [j, !, write ('.'), nl
writer, ' ) , nl,
writs_body( Gs)
).
name_vars{ [], _) .
name_vars( [Kame:Type | Xs] , [Name i Names]i
name_vars[ Xs, Names).

:-

I connect_inputs( Vars, Inputs):


%
, Inputs
%
Vars
cor.nect_inputs ( _, [! ) .
connect_inputs( S, [XI Xs])
member[ x, S ) ,
conr.ect_inputs ( S, Xs) .

:-

% merge! LI, L2, L3): , ,


*

merge( [], L, LJ

:- ! .

merge ( L, [],!)

:- ! .

merge! [X1|L1], [X2|L2], [X1IL3]) :XI @ = < X2, !, % XI " 1" 2 ( !


merge( LI, [X2|L2), L 3 ] .
merge) LI, [X2|L2], [X21L3]) :merge( LI, L2, L3).
% mergesort[ LI, L2): , L1, L2
mergesort ( [] , [] ) :- ! .
mergesortl [XI, [X]) :- !.
mergesort( L, S) :split! L, LI, L2),
mergesort( LI, El),
mergesort( L2, S2),
merge( SI, S2, Si.
% split( L, LI, L 2 ) : , L
%

split. ( [], [], []) .
split! [X] , [X], []>
split! [XI,2 | L ] , [XII LI], [X2|L21)
split! L, LI, L 2 ) .

:-

% ,
init_counts :retract( counter(_,_)!, fail

assert( counter( generated, 0)) , %


&

19.

467

a s s e r t ! counter( complete, 01),


assert( counter! refined, 0 ) ) .

%
%
%

addl( Counter)
:r e t r a c t ! counter ( Counter, N) ) , !, N1 is N+l,
a s s e r t ( c o u n t e r ! Counter, N11).
show_counts :c o u n t e r ! g e n e r a t e d , KG), c o u n t e r ! r e f i n e d , NR), c o u n t e r ! complete, NO,
n l , w r i t e ! 'Hypotheses g e n e r a t e d : ' ) , write(KG),
n l , w r i t e ( 'Hypotheses r e f i n e d :
' ) , write(NR),
ToBeRefined is NC - NR,
n l , w r i t e ! 'To b e r e f i n e d :
' ) , w r i t e ! ToBeRefined), n l .
%
max_proof_length[ 6 ) .
max_clauses[ 4 ) .
max_clause_length( 5 ) .

%
%
%
%

r e f i n e hyp ( HypO, Hyp). ,


Hyp,
.
ref i n e ( Clause, Vars, NewClause, NewVars). ,
Clause
Vars NewClause NewVars. Vars,
Vars
, Clause.
induce_hyp { Hyp). ,
Hyp .

best_search/2.
b e s t _ s e a r c h ( Hyps, Hyp). ,
Hyps, s t a r t _ h y p s / l , , Hyp.
. Cost: Hypothesis.
( ) .
prove t Goal, Hyp, Answer). , 19.2.
eval i Hyp, C o s t ) . . Cost
Hyp , . ,
Cost = 0.
s t a r t _ h y p s ( Hyps). , Hyps .
,
MaxClauses. MaxClauses

468

I!, Prolog

max_ciauses. s t a r t _ c l a u s e .
show hyp{ Hyp) . , Hyp
Prolog.
i n i t c o u n t s , show_counts, addl (Counter). , , .
: ( ),
( , ) ( ).
s t a r t c l a u s e ( C l a u s e ] . ( ),
:
start_Clause<

[ member(X,L)

X:item,

L:list]).

max proof_length(D), max_clauses(MaxClauses), max_clause_length(MaxLength).


, : ,
. ,
member/2 coric/ MaxClauses=2. , 19.6, :
max_proof_Iength(). max_clauses(4). max_clause_length(5).
19.6 , n o t / 1 , once/1, rnember/2, conc/3, d e l / 3 , l e n g t h / 2 , copy_jterm/2.
HYPER
member (X, L). HYPER, Prolog , 19.4.
:
?-

induce[),

show_hyp!H).

HYPER
(, ), , .
:
Hypotheses generated: 105
%
Hypotheses refined: 26 %
be refined: 15
% ,
member(, [ ] ] ) .
member(,[|]]
member(,).

:-

, .
, 103 , 26
, 15 . 105 - 26 - 15 = 51 , .
5 (. 19,5). , .
() HYPER, . HYPER
( 10%). ,
( , ) .

19,

469


19.5.
cone (LI, L2, L3) , 19.4, HYPER .

, . ,
HYPER.

19.3.4. HYPER
HYPER , MINIHYPER. HYPER , . ,
, HYPER. ,
( ,
, ).
HYPER.
odd(L) even(L)
HYPER (multi-predicate learning).
,
. ,
.
odd ( L i s t ) even [List]
{
). 19.7,
.
Hypotheses generated: 8 5
Hypotheses refined: 16
be refined: 29
even! [] ) .
even i[A,3|C]) : even(C).
odd![A[B]} :even (3).
19.7. ,

% ,
(odd) (even)
backliteral( even( L ) , [ L:list], []).
backliteral( odd( Li, [ L:list], []).
term( list, [X|L], [ X:item, L:list]).
term( list, [] , [] ) .
prolog_predicate( fail).

470

II. Prolog

E t a r t _ C l a u s e { [ odd( Lj ] / t L : l i s t ] )
s t a r t _ c l a u s e ( [ event L) ] / [ L : l i s t ]

ex( even( t) .
ex [
ex<
ex(
ex(
ex (

even( [ a , b j ) } .
oddt [a)M
oddt [ b , c , d ] ) ) .
oddt [a,b,c,d,e] ) } .
even[ [a,b, c, d] ) ) .

nex(
nex(
nex(
next
next

even( [a] }) .
even( [a,b, c] } ) .
odd{ [] ] ) .
odd! [ a , b ] ] ) .
odd( [ a , b , c , d ] ) ) .

. HYPER
, .
(, , ),
HYPER , ;
Hypotheses g e n e r a t e d : 115
Hypotheses r e f i n e d : 26
To be r e f i n e d : 32
even([]).
odd<[A|B]> : even(B).
even[[A|B]}
odd[3) .

:-

, ,

. 1. , l i s t
l i s t ( D ) , , :
term!

l i s t ( D ) ,

iX|L],

X:item,

L : l i s t ( l ) ] )

: - v a r ( D ) .

l i s t (";.), [X,YIL]. , , term


start_clause.
path( StartNode, GoalNode, Path)
19.8 p a t h
( link/2).
:
Hypotheses generated: 401
Hypotheses refined: 35
la be refined: 109
path(A,A,[A]).
path(A,C,[ft,BIE])
:link{A,E),
path{B,C, [D|E] ) .

19.

471

19.8. p a t h ,
% path[StartNode,GoalNode,Path),
%
%
]ink(a,b),
link(,) .
link[b,c) .
link(b,d) .
link(d,e> .
backliteral( link(X,Y>, [ X:item], [ Y:item] ) .
backliteral( path{X, Y,L), [ X:item], [ Y:itein, LrlistJ).
term( list, [X|L], [ X:item, L:list]}.
termt list, [] , [] ) .
prolog_predicate{ link(X,Y)),
start_clause( [ path(X,Y,L)] / [X:item, ;item,L:list] >.
'i
ex(
ex(
ex(
ex(
ex (
ex(
ex(
nex[
nex(
nex(
nex(
nex<
nex(
nex(
nex(

path(
path(
path)
path!
path(
path(
path(

a,
b,
e,
f,
a,
b,
a,

path(
path(
path(
path(
path (
path (
path!
path!

a,
a,
a,
e,
a,
a,
a,
a,

a,
b,
e,
f,
c,
e,
e,

[a]}).
[b]) ) .
[e] ) ) .
[f])).
[a,c] ) } .
[b,d,e] ) ) .
[a,b,d,e] ) ) .

a, [ J ) > .
a, [ b ] ) ) .
a, [ b , b ] ) ) .
d, [ e , d ] ) ) .
d, [a,b,c])} .
c, [a])) .
c, [ a , c , a , c ] n .
d, [a,d})S .


, p a t h (, , [|] ) . ,
. ,
35 , , . p a t h , , 12, , 10 1 ' !
.
,
.

,
19.9. ,
. ,
. 472

II. Prolog

. , , , . . ,
, . ,
. , 19.9, .
, s o r t ( L l , L 2 ) L1, ,
, L2 ,
s o r t . s o r t
L2, :
(

sort(

[ c , a , b ] ,

L),

L =

[a,b,c]

] ) .

19.9.
%
b a c k l i t e r a K sort ( L, S ) , [Lilist], [ S : l i s t ] ) .
b a c k l i t e r a K insert_sorted( X, LI, L 2 ) , [X:item, L l : l i s t ] ,

[L2:list]).

term( list, [X | L ] , [Xritem, L : l i s t ] ) .


term( list, [ ] , [ ] ) .
prolog_predicate( insert_sorted( X, LO, L ) ) .
prolog_predicate( X = Y ) .
start__clause( [sort (Ll, L2) ] / [Llrlist, L2:list] ).
ex( sort( [] , [] ) )
ex( sort ( [a], [a])) .
ex( [ sort ( [ c , a , b ] , L ) , L = [a,b,c] ]

).

% sort
% !

ex( sort( [ b , a , c ] , [ a , b , c ] ) ) .
ex( sort ( [ c , d , b , e , a ] , [a,b,c,d,e])) .
ex( sort( [ a , d , c , b ] , [ a , b , c , d ] ) ) .
nex(
nex(
nex(
nex (
nex(
nex(

sort(
sort(
sort(
sort (
sort(
sort(

[ ] , [a])).
[a,b], [a])).
[ a , c ] , [b,c])).
[b,a,d,c], [b,a,d,c])).
[a,c,b], [a,c,b])).
[ ] , [b,c,d])).

insert sorted( X, L, _)

:-

% "" :
%

v a r ( X ) , !, fail
var ( L ) , !, fail
L = [Y|_], var(Y),

!,

fail.

insert_sorted( X,

[],

[X])

:-

!.

insert_sorted( X,
X @< Y, !.

[Y | L ] , [X,Y | L])
:% X " " Y

insert_sorted( X, [Y | L] , [Y | Ll] )
insert_sorted( X, L, Ll) .

:-

19.

473

s o r t L, , s o r t ( !) L .
i n s e r t _ s c r t e d ( X , LI, L2),

(, X ). .
Hypotheses generated: 3708
Hypotheses refined: 24
To b e r e f i n e d : 4 4 8
s o r t ( [ ] , []) .
sort[[ft|Bj,D! ; sort (B,C) ,
insert_sorted{A,C,D).

,
,
18, , (. 19.2), (
[X, Y) Y ( a kind of) X;
. 18.6). ,
. , . arch ( a l , b l , c l ) , a l , c l , c l al
, al touch (). 2, 2
2 ( (2, 2, 2)), .2 2 2. 5, 5 5 , " ". 19.10.
(not),
touch/2 s u p p o r t / 2 . , , Prolog . . 18.4, . 19.2 .
.

. 192. . al, cl , 4, 4 4 . $2,


2 2

474

II. Prolog

Hypotheses generated: 368


Hypotheses refined: 10
To be refined: 151
arcb(A,BrC! :support[,),
not touch(A,B) r
support(,),
isa(C,3table_poly).

%
%
%
%
%

,



-

19.10.

backliterali isa[X,Y) , (X:object] , [] I :member( Y, [polygon,convex_poly,stabie_poly,unstable_poly,triangle,
rectangle, trapezium, unstable_triangle, hexagon]I. % Y -
%
backliterall support(X,Y), [X:object, Y:object], []).
backliteral! touch(X,Y), (X:object, Y:object], []).
backliteral ( not C-, [X:object,Y;object], [)) :G = toach(X,Y); G = support(X,Y).
prolog_predicate(
prolog_predicate(
prolog_predicate(
prolog_pcedicate(

isa(X,Y)).
support(x,Y)).
touch(X,Y)}.
not G ) .

ako( polygon, convexj>ply) .


%
%
ako( convex_poly, stablejpoly).
.
% OJTOKOE
.{ convex_poly, unsrablejpolyl .
v
% }
afco( stable poly, triangle),
%
%
ako( stable_poly, rectancle).

%
ako( stable_poly, trapezium).
%

ako( unstablejpoly, unstable_triangle). %
%
ako{ unsi:able_poly, hexagon).
%
I
=ko( rectangle, X) :member! X, [al,2,,4,5,1,2,,4,5,1,2,]). %
akoi triangle 4 ) .
ako( unstable_triangle, c5).
isa( "igurel, Figure2)
:ako( Figure2, Figurel).

%
% ,

% Figurel figure2

isa( FigO, Fig) :ako{ Figl, FigO),


isa( Figl, Fig).
support(al,cl).
support (a.3,c3) .
support(a4,c4).
support(a5,cS) .

support lbl,cl).
support (,) .
support(4,4 3.
support(b5,c5!.

touch (a3,b3) .

5tart_clause[ [ arch (X, Y, Z ) ] / \Y.: obj ect, Y: object, Z : object) > .

19.

475

ex( arch(al,bl,cl) ) .
( arch 14,, c4) J .
nex{ arch [a2,b2,c2) )
( arch[a3,b3,c3))
nex[ arch (a5, bS,c5) I
nex( arch(al,2,cl))
nex1 arch(a2,bl,cl))

(ILP) .
1LP ,
. , ILP
.
ILP : -,
, , -, , , -,

.

, .
( Prolog), .
()
().
:
-, , , , , , -,
.
- , .
HYPER, , Prolog ,
.
:
;
;
;
;
-;
.

476

II. Prolog



[110]. , ,
, [125], [136] [146]. HYPER, , ,
[18]. ILP [86]. [42] [112]
ILP. ILP Progol [111] z FOIL [129]. [21]
ILP.

19.

477

20

.,.
20.1. ,

20.2.
20.3.
20.4.
20.5.

478
482
486
493
502


. . , , , . ,
. ,
: "
, ...".
, ,
, . ,
.

20.1. ,

20.1.1.

, . 20.1. , , , . ?

. 20.1. ,
,

, -
.
, , , 1593 ,

, . , , . 62,53 159,3 .
.
, :
"
. ".
, , ,

.

,
, . , , , .

20.1.2.

. ,
. . . 20.1
. , . , , .
20.1.

L e v e l ( 3 . 2 s i = 2 . 6 cm

Level t t l ) = z e r o , . t o p

L e v e l ( 3 . 2 s) = 2.6 cm

L e v e l ( t l ) = pos

d / d t L e v e l ( 3 . 2 s)

L e v e l [ t l )

= 0 . 1 2 m/s

Amount = L e v e l * ( L e v e l + 5 . 7 )
20,

M"1 ( Amount, L e v e l )
479

T i m e

Amount

0.0
0.1

0.00
0.02

15.

62.53

Amount{start..end! = zero..top/inc



, 3,2
Level 2,6 , :
Level(3.2 s) - 2.6 cm
, Level t l
( zero) ( top), :
Level(tl) zero.,top
, 3,2 t l . , , , t l , Level .
,
62,53 z e r o . . t o p .
, Level
tl , :
Level(tl) -


Level :
d
^ L e v e l ( 3 . 2 s) = 0.12 m/s

, Level
tl .

: Amount = Level * (Level + 5.7).
;
Level >- 0, Amount Level, :
* [ Amount, Level). , Level Amount .

, Amount
0 159,3 ,
: " Amount s t a r t end zero f u l l ,
". :
Amount(start..end) = zero..full/inc
480

II. Prolog


. ,
. . , , Flow Level , .
:
+( L e v e l ,

Flow)

, , ,
- . , .

20.1.3.

, . , -
. , .
-, , .
, . ,
,
, . ,
, , ..
.
, ,
.
,
.
, . , ,
. , , , , .
, , , ,
. , , ,
,
, , .. , .
20.

481

-, . . , , , . .
,
: " - - ?" .
?
?
?
?
?
() .
, .
, .
, . ,
, ,
, .
:
; ,
. , , . ,
" ". , ,
. , , ,
.
, , , , .
.
. , 20.2
{ ,
). 20.3 ,
, . ,
, (Qualitative Differential Equation QBE), .

20.2.

, ,
(. 20,2). (.. ),
, . 482

II. Prolog

, ,
.
1, , : " , , ?"
, , .

. 20.2. , ,

,
2, : " , 2 , 3
, , 3 ?"
, 3, , .
2, 2 , 2 . 2 ,
2 . , 3 . 3. ,
2 , 3 ; , 3 .

"pos" (), " z e r o "
() "neq" (). , X , .
X > D, pos
X = ,
X < 0, neg


, . : -,
0, -, 0.
, .
X + V = Z
, :
qsurat X, , ZI
qsum . , , , :
asurnt pos, pos pos}.
20.

483


:
qsumi , neg, pos) .
qsumj pos, neg, zero).
qsum( pos, neg, neg).

().
- , , ,
.
.
, 20.1, .
. (
) :
switch( SwitchPositicn, voltage, Current)
bulb( BulbState, Lightness, voitage, Current)
SwitchPosition , Voltage , Current
, B u l b S t a t e , a Lightness .
20.1.

% : neg, zero, pos
%
%


switch( SwitchPosition, Voltage, Current)

switch* on, zero, ftnyCurrent}.


switch! off, Anyvoltage, zero).

% -
% -

% :
% bulb[ BulbState, Lightness, Voltage, Current)'
bulb(
bulb{
bulbf
bulb (
%
I

blown, dark, AnyVoltage, zero).


ok, light, pos, pos) .
ok, light, neg, neg).
ok, dark, zero, zero),

,
.

circuit I( SuitchPos, BulbState, Lightness) :switch; SwitchPos, SwVolt, Curr),


bulb( BulbState, Lightness, BulbVolt, Curr),
qsum( SwVolt, BulbVolt, pos)% pos

, ,

Circuit2[ Swl, Sw2, Sw3, Bl, 2, , LI, L2, L3)


switcht Swl, vswl, C D ,
bulb! Bl, LI, VB1, Cl) ,
switch) Sw2, VSw2, C2),
bulb( B2, L2, VB2, C2),
qsum( VSw2, VE2, V3\,
switch( 3w3, V3, CSw3),
bulb( E3, L3, V3, CB3),
qsum( VEwl, VB1, VI),
qsumt VI, V3, pos),
qsumf CSw3, , ),
qsumf C2, C3, Cl).

484

:-

II. Prolog

%
%
%

qsuml Ql, Q2, Q3):


03 = Ql + Q2,
[pos,zero,neg]

qsumt
qsumt
qsumt
qsumt
qsumt
qsumt
qsumt
qsumt
qsumt
qsumt
qsumt
qsumt
qsumt

pos,
pos,
pos,
pos,
pos,
zero,
zero,
zero,
neg,
neg,
neg,
neg,
neg,

pos, p o s ) .
zero, p o s ) .
neg, p o s ) .
neg, z e r o ) .
neg, n e g ) .
pos, p o s ) .
z e r o , zero) .
neg, n e g ) .
pos, p o s ) .
pos, z e r o ) ,
pos, n e g ) .
zeror neg).
neg, n e g ] .


Prolog. , (off)
(zero), , .
switch! off, AnyVoltage, ).
(blown) (dark), ,
, .
bulbt blown, dark, AnyVoltage, zero).
( i n t a c t ) , , , . ,
, . , , . , , :
Voltage - Resistance * Current
R e s i s t a n c e ,
Voltage C u r r e n t .
, . , , :
c i r c u i t l ( SwPos, Bulbstate, Lightness)
, SwPos, BulbState
Lightness, c i r c u i t l . , , , c i r c u i t l . , , .
1. .
2. .
3. ( ): + = .
, .
2 ( c i r c u i t 2 ) , , .
, , 20.1, .
20.

485


" "
( ),
( ). , ,
(on) , (ok)?
1LI
L2
L3

circuit2 (on,on,on,ok,ok,ok,Ll,L2,L3) .
= light
= dark
= dark


, ( ,
?). , 1 , 3
, 3 , ?
?Bl
B2

c i r c u i t 2 ( _,
- ok
= ok
= blown

_,

off,

Bl,

B2,

light,

_,

dark).


,
? , , 3, , ?
?- c i r c i j i t 2 ( SwPosl, SwPos2, SwPos3, ok, ok, ok, _, _, l i g h t ) .

SwPosl = on
EwPos2 = on

5wPos3
SwPosl
SwPos2
SwFos3

=
=
=
=

off;

on

off
off

20.1. :
qmultf ft, ,
= *, ,
pos, zero neg.
20.2. :
resistor( Voltage, Current)
diode( Voltage, Current)

.
Voltage Current .
, ,

20.3.


.
(Quali486

II. Prolog

tative Differential Equation QDE). QDE .


(. 20.3). .
: , (),
(), .

. 20.3.

, . ,
, : , .
.
. ,
, , , . , .
- .
" " ( ).
,
. ()
, . 20.4.

. 20.4, so

, . 20.4,
. .
:
Level - zero/inc
,
(zero) (top) .
20.

487


, zero top. ,
, ,
. , :
Level = zero..top/inc
,
, .
Level = zero..top/std
.
, . . .
Level. .
Amount. .
Inflow. ().
Outflow. ().
Netflow. (Netflow = Inflow Outflow).
, (landmark). ,
(minf), (zero) (inf). ,
Level,
(top), . , ,
minf Level. . (Level)
:
zero < top < inf
(Amount) :
zero < full < inf
.
,
.
, . Amount Level , . :
[ Amount, L e v e l )

M'(X,Y) , Y
X: X Y . M0(x,Y) , Y X, ,
Y(0) = 0. , * (0, 0) . "
( f u l l , t o p ) . , + (X, Y)
" ;Y,X) .
, ,
. (Amount, Level), , . ,
488

II. Prolog e

. :
Amount = f(Level}
, . 20.5. , . ,
, . , ,
. , , , ,
. - , .
(
, ).

. 20,5. - .

, Outflow Level. , .
, , . 20.2.
:

20,

489

[ Amount, Level)
( Level, Outflow)
( Outflow, Netflow, Inflow)
deciv[ Amount, Netflow}
Inflow = constant = inflow/std
20.2,

* <, Y )
- ( , Y)
sum(X,Y,Z)
minus(X,Y)

mult(X,Y,2)
denv{x,ir;

Y -
Y
Z = X+Y
Y = -X

z = X*Y
= dX/dt(Y- ]

, , .
.
,
. . 20.6 ,
.

. 20,6.
, . 20.6. , , , . , Amount=zero , Amount , Amount=zero/inc
, Amount
, . :
Amount = zero
M43, Amount Level
(. . 20.6), :
Level = zero
*,
:
Outflow = zero
Outflow + Netflow = Inflow
:
+ Netflow inflow
490

II. Prolog

Netflow = inflow.
d e r i v Amount Netflow,
, Netflow Amount.
Netflow = inflow > zero, ,
Amount , .
Amount = zero/inc
^, :
Level = zero/inc
Outflow = zero/inc
Outflow + Netflow = Inflow , .
zero/inc + Netflow = inflow/std
, Netflow
:
Hetflow inflow/dec
,

Amount - zero/inc
Level zero/inc
Outflow = zero/inc
Netflow = inflow/dec


. , ; , .
, , , , . ,
. , , .
,
. , " i n c " (),
" i n c " , " s t d " (),
"dec" (). , , ,
. " z e r o / i n c "
" z e r o / i n c " .
,
, , Level zero t o p ,
. , , Level :
Level <= zero..top/inc
. 20.6 . :
Level = zero..top/inc
Amount = zero..full/inc
Outflow = zero..inflow/inc
Netflow = zero..inflow/dec

20.

491

Level? .
1. Level = z e r o . . t o p / i n c .
2. Level = z e r o . . t o p / s t d .
3. Level = t o p / s t d .
4. Level = t o p / i n c .
Level ,
. , . . ,

( ).
, .
1. Level , . , . 20.6, ,
- .
:
Level = zero..top/std
Amount = zero..full/std
Outflow = inflow/std
Hetflow = zero/std
, ,
,
.
2. Level , .
, .
, 1.
3.
. , , . 20.6, .
. . ,
.
. ,
. 20.6,
.
,
. , , ,
. , ,
, .
, . 492

II, Prolog


, ,

. , .

20.4.

20.2 , QDE ,
. .
20.2. ,
.
member/2 / 3
%
:- ( 100,
: - ( 500,

xfx,
xfx,

..>.
:].

% landmarks! Domain, [ Landl, Land2, ... ]


%
Landl, Land2 .. - Domain;
*
,
%
%
%

correspond( Constraint):
Constraint

correspond( sum! Doml:zero, Dom2:zero, Dom3:zeroM.


correspond! sum( Doml:L, Don\2 : zero, Doml:L)) !~
qmag I Doml:L), L \ = = zero, not [L = _ _ ) % L -
i Doml
correspond! sum( Doml:zero, Dom2:L, Dom2:U)
qmag I Doro2:L), L \ = zero, not [L = _ . . _ ) .

:% L -
% Dom2

correspond; sum( VI, V2, V3)) :correspond! VI, V2, V3) . i


% correspond/,
% "
* " Prolog
Correspond! dumny, dummy, dummy).
% qmag! Domain:QualMagnitude)
[ Domain;Qm) :landmarks( Domain, Lands),
qmag ( Lands, QnO .
qmag( Lands, L) :member) L, Lands),
L \== minf, L \== inf.

20.

493

qmag( Lands, L1..L2) :concl _, [L1,L2 I _ ] , Lands).


%
%
\

*
l

relative_qmag{ Domainl:QM, Domain2:Landmark, Sign):


Sign - QM Landmark;
QH < Landmark, Sign - neg ..

relative_qraag( Domain:Ma..Mb, Domain:Land, Sign) :- !,


landmarks[ Domain, Lands),
( compare_lands( Ma, Land, Lands, neg). Sign neg, !
;
Sign - pos
).
rrlative_qmag( Domain:Ml, Domain:M2, Sign)
landmarks( Domain, Lands),
compare_lands( Ml, M2, Lands, Sign), !.

:-

% qdir( Qdir, Sign):


%
Qdir -
%
Sign
qdir[ dec, neg).
qdir( std, zero).
qdir( inc, pos).

% qsum( Ql, Q2, Q3] :
%
Q3 = Ql + Q2,
%
[pos,zero,neg]
qsum(
qsum;
qsumi
qsuml
qsuml
qsum(
qsum<
qsum(
qsumj
qsum{
qsum(
qsuml
qsum<

pos, pos, pos).


pos, zero, pos).
pos, neg, pos).
pos, neg, zero),
pos, neg, neg).
zero, pos, pos).
zero, zero, zero) .
zero, neg, neg).
neg, pos, pos).
neg, pos, zero).
neg, pos, neg).
neg, zero, neg).
neg, neg, neg).

% qdirsuml Dl, D2, D3):


%

qdirsumt Dl, D2, D3I :qdir[ Dl, Ql), qdiri D2, Q2), qdiri D3, Q3),
qsuraf Ql, Q2, Q 3 ) .
% sum( QV1, QV2, QV3):
i
QV1 - QV2 + QV3,
%
Domain:Qmag/Dir. ,
%

sum! D1:QM1/Dirl, D2;QM2/Dir2, D3:QM3/Dir3) :qdirsumf Dirl, Dir2, Dir3),
% : Dill + Dir2 = Dir3
qraag( Dl:QMl), qmag( D2:QM2), qmag( D3:QM3),
% QM1+QM2-QM3 :
not (

II. Prolog

correspond sum[ D1:V1, D2:V2, D3:V3)),


relative_qmag( D1;QM1, Dl:Vl, Signl),
relative_qmag( D2:QM2, D2:V2, Sign2},
relative_qmag( D3:QM3, D3:V3, Sign3},
not qsuml Signl, Sign2, Sign3) ).

% VI + V2 = V3

% inplus ( X , Y} :
*
Y - - X
raplus( Dl:QMl/Dir, D2:QM2/Dir) :%
qmag{ D1:QM1), qmagf D2:QM2),
$ QM1, QM2
$ D1 D2:
not ( correspond; D1:V1, D2:V2) r
relative_qmag( Dl:QMl, D1:V1, Signl),
relative_qmag( D2:QM2, D2:V2, Sign2),
Signl \== Sign2 ) .
% derivf V a n , Var2) :
%
varl Var2
deriv{ Doml:Qmagl/Dirl, Dom2:Qmag2/Dir2) :qriir( Dirl, Signl),
qmag( Dom2:Qmag2),
relative_qmag( Dom2:Qmag2, Dom2:zero, Sign2), % 3ign2 - Qmag2
Signl = Sign2.
I transitionf Domain:Qmagl/Dirl, Domain:Craag2/Dir2):
%
,
%
""
transition! Dom:Ll..L2/std, Dom:LI..L2/Dir2)
qdir( Dir2, AnySign).

:-

transition! Dom:Ll,.L2/inc, Don:Li..L2/inc).


transition! Dom:Ll..L2/inc, Dora:LI.,L2/std).
transition; Dom:Ll..L2/inc, Dom:L2/inc)
L2 \== inf.

:-

transition! Dom: LI. . L2/inc, Dom:Ij2/std}


L2 \== inf.

:-

transition! Dora:LI..L2/dec, Dora:LI..L2/dec;.


transition( Dem:LI,.L2/dec, Dom:LI..L2/std).
transition( Dom:LI.,L2/dec, Dom:LI/dec)
LI \== minf.

:-

transition; Dom:Ll..L2/dec, Dom:Ll/std)


LI \== minf,

:-

transition( Dem:Ll/std, Dom:Ll/std) :LI \ A..B.


% LI
transition; Dom:Ll/std, Dom:Ll..L2/inc)
qmag< Dom:LI..L2).

:-

transition( Dom;Ll/std, Dom:L0..LI/dec)


qmag( Dom:L0..LI}.

:-

transition; Dom:Ll/inc, Dom:Ll..L2/inc)


qpag( Dom:Ll..L2).

:-

20.

495

transition! Dom:Ll/dec, Dom:L0..Ll/dec)


qmag ( Dom;LQ-.LI}.

:-

t system_trans( Statel, State2):


%
; -
%

system_trans ( [] , [ ] ) .
system_trarts< [Vail I Valsl], [Val2 | Vals2]}
transition! Vail, Val2),
system_trans1 Valsl, Vals2).

:-

% legal_trans( Statel, State2):

,
legal^trans( Statel, State2) : system_ti:ans ! Statel, Scate2 ),
Statel \== State2, % ,
legalstate{ State2J.
% ,
* simulate! SystemStates, MaxLength):
% SystemStates
i , MaxLength
simulate{ [State], MaxLength)
( MaxLength = 1
not legaltrans{ State, _)

:%
%

1 , !

simulate( [Statel,State2 ( Rest], MaxLength)


MaxLength > 1, NewMaxL is MaxLength - 1,
legaltransl Statel, State2),
simulate) [State2 | Rest], NewMaxL).

:-

% simulate! InitialState, QualBehaviour, MaxLength)


simulate! InitialState, [InitialState I Rest], MaxLength) :Iegal3tate( InitialState),
%
simulate( [InitialState i Rest], MaxLength).
I compare_lands ( XI, X.2, List, Sign) :
I
XI 2 List, Sign = neg;
%
2 XI, Sign pos; Sign zero
coTTipare_lands [ XI, 2, [First I Rest) , Sign]
XI = X2, !, Sign - zero

:-

XI = First, !, Sign = neg


X2 = First, !, Sign = pos
cc;r.pare_lands ( XI, X2, Rest, Sign) .

20.4.1.

. , Outflow Netf low
, "flew", . , , :
landmarks( flow, [ minfF zero, inflow, i n f ] ) .
496

II. Prolog


Domain:QMag/Dir

QMag , ,
,
Lar.dl. . Land2, a Die ,
i n c , std, dec. Outflow.
flow:inflow/dec
flow:zero,.inflow/dec

. , Level, Amount, Outflow Net flow:


E level: /inc, amount: /inc, flow:zero/inc, flow: iriflow/dec ]

.

20.4.2.
, 20.2,
QDE d e r i v ( X, Y), sum( X, Y, 2), mplus ( X, Y) , (X, Y Z) . .
d e r i v ( X, ). Y X . X Y.
mplus ( X , Y). Y X. X Y Dx:QmagX/DirX
Dy:QmagY/DirY. , -,
: DirX = DirY. -, . X Y ( ). , l e v e l : z e r o . , top
t o p neg.
.
X Y,
sum( XF Y, Z).
X + Y = 2, (X, Y Z) Domain:Qmag/Dir,
, . -, . ,
inc + std - inc
, :
inc + std = std
-,
. ,
, X, "
Z. ,
sum.
20.

497

flow:zero + flow:inflow = flow:inflow


flow:zero..inflow + flow:zero..inflow flow:inflow
flow:zero.,inflow + flow:..inflow = flow:..inflow
:
flow:zero..inflow + flow:inflow = flow:inflow
. ( ).
.
:
sum!

flow:zero..inflow/inc,

flow:zero..inflow/dec,

flow:inflow/std)

-, : inc+dec = std.
-, ,
:
correspond I sural Di:zero, D2:Land, D2:Land)]
Dl D2 , a Land D2.
, :
correspond! sum! flcw:zero, flow:inflow, flow:inflow))
sum
?
sum
:
flow:zero..inflow 'pos' 1 flow:zero
flow:zero..inflow 'neg flow:inflow
flow:inflow '' flow:inflow
qsum [ pos, , z e r o ) . .
sum
. ,
su:n(X,Y,Z) (, , zC). X, Y
Z :
X = , Y = + , Z = zO + AZ

sum :
+ + + AY - zO *

.0 + = zO, , + AY = AZ. , AY
X, Y Z , zO.
qsum.

20.4.3.
, 20.2, ;
transition [ QualStatel, QualState2)

Q u a l S t a t e l QualState2 . Domain : Qmag/Dir. t r a n s i t i o n . 498

II. Prolog

. , , , . , i n c , (inc)
(std), {dec), s t d .
, t r a n s i t i o n , , , .
, , , .

,
,
.
,
.
, .
20.3 , , 20.2.
, , , ,
10.
?- i n i t i a l ( ), simulate( S, Behaviour, 10).
20.3.
i
landmarks) amount, [ , full, inf)) .
landmarks! level, [ zero, top, inf]).
landmarks[ flow, [ minf, zero, inflow, inf]).
correspond! amount:zero, level:zero).
correspond( amount:full, level:top).
legalstate( t Level, Amount, Outflow,
mplus[ Amount, Level),
mplus[ Level, Outflow),
Inflow flow:inflDw/std,
sum( Outflow, Netflow, Inflow),
detiv( Amount, Metflow),
not overflowing; Level).

Netflow])

:-

overflowing( level:top..inf/_).

%
% Netflow Inflow - Outflow
%

initial! I level: zero/inc,


amount; zero/inc,
flow: zero/inc,
flow: inflow/dec 3 ) .
Prolog ( ).
= [ level:zero/inc, amount;zero/inc, flow:zero/inc, flow:inficu/dec]
Behaviour = |
[level:zero/inc,amount:zero/inc, flow:zero/inc,flow:inflow/dec] ,
[level:zero..top/inc,amount:zero..full/inc,flow:zero..inflow/inc,
flow:zero..inflow/dec] ,
[level:zero..top/std,amount:zero..full/std,flow:inflow/stdF flow:zero/std]]

20.

499

, 20.2, . . 20.7 , 20.4 .


, . , ,
( ).
?- i n i t i a l ( S), simulate( S, Behaviour, 10].
Behaviour =
[ [volt:vO/dec,volt:zero/inc, . . . ],
[volt:zero..vO/dec,volt:zero..vO/inc,...],
[volt:zero..vO/std,volt:zero,.vO/std,...] ]

. 20.7,

20.4. , . 20.7
h
landmarks( volt, [minf, zero, vO, inf]).
landmarks( voltR, [minf, zero, vO, inf]).
landmarks( current, [minf, zero, inf]).

i
%

correspond! voltR:zero, current:zero).


legalstate[ [ UC1, 2, UR, CurrR]) :sumi , , uci],
mplus( UR, CurrR),
%
deriv( UC2, CurrR)r
Sum( CurrR, current:CurrCl, current:zero/std),
* CurrCl = - CurrR
deriv{ UCI, current:CurrCl).
% CurrCl - d/dt UCI
initial! [ volt:v0/dec, volt:zero/inc, voltR:vO/dec, current:zero..inf/dec]).
, , 1 , 2 ,
( ).

20.3. X .
() :
X{t) - a l * s i n ( k l * t ) , <t) - a2*sin(k2*t)
500

II, Prolog

a l , a2, kl 2 ;
0. tO = 0,
X(tO) = Y(tO) = z e r o / i n c .
)
.
) , X Y
Mj'iXrY). , .
20.4. X, Y Z :
sum(X, Y,Z)
, :
X, Y: minf, zero, inf
Z: minf, zero, landz, inf
tO x{tO) = z e r o / i n c . Y(tO) 2(tO)? X, Y Z ,
tO. . t l , ? X ( t l ) , Y ( t l )
Z ( t l ) , t l ?
20.5. (. 20.8)
Prolog, (. 20,2).
( , ). , (. . 20.8).

. 20.8,
,

20.6. ,
20.2, , : minus [X, )
( X = -Y), m_minus (X, Y} ( Y X),
mult(X,Y,Z) ( Z = X * Y).
20.7. :
landmarks( , [ minf, zero, xl, inf]) .
landmarks( v, [ minf, zero, vO, i n f ] ) .
legalstate< [ x, v]) :V = v:_/inc,
%
derivt X, V).
initial ( [ x: zero, .xl/inc, v:v0/inc] } .

20.

501

,
20.2, ( Prolog ):
?- i n i t i a l ! S), simulate! S, Behav, 31.
3ehav = [ [x : zero..xl/inc,v:vO/inc],
[ x:..xl/inc,v:vO.. inf/inc] ,
[x:xl/inc,v:vO..inf/inc] ] ;
Behav = ( [x zero.,Kl/inc,v:vO/inc],
[x:xl/inc,v:vO..inf/inc] ,
[x:xl..inf/inc,v:vO..inf/inc]

, , . . v:vO/inc,
(
, ). : x l / i n c , . . (
, X !
z e r o . . x l ) . , X
xl. , , ,
X xl,
X xl, X
, xl. , 20.2, ? : l e g a l _ t r a n s .

20.5.

, , QSIM ([84], [85]).
,
20.2, QSIM. ,
,
, , ,
QSIM . ,
,
.
. ,
,
. , QSIM
,
, .
, . .
502

II. Prolog

(top), , , ,
.
.
, . QSIM , , ,
, Level=top. ,
,
, . ,
, ( ) .
,
QSIM. ,

. ,
, .
. ,

, .
.

.
. ,
,
, .
, , , . QSIM
. , - . ; .
(spurious
behaviour).
,
(. 20.9). ,
. , ,
tO, (X = zerc) vC . X
, ,
, . , X - zero. , , vC. .
.
(. 20.2). :
. =

20.

503

X , , m , . 20.5.

. 20.9. ,
,

20.5. ,
% ,
landmarks! [ minf, zero, i n f ] ) .
landmarks( v, [ minf, zero, vO, i n f ] ) .
landmarks( a, [ minf, zero, i n f ] ) .

*
%
%

correspond; x : z e r o , a : z e r o ) .
l e g a l s t a t e t [ X, v, A])
derive ., V) ,
d e r i v [ v, A),
MinusA = :

:-

sum( A, MinusA, a:zero/std),


mplus( X, MinusA).
initial{

[ :zero/inc,

v:vO/std,

% MinusA = -A
%
a:zero/dec]).

:
?-

initial I

S),

simulate!

S,

Beh,

11).

Beh 8, .
[ x:minf..zero/inc, v:zero..vO/inc,a:zero..inf/dec]
.
:
[x:minf..zero/inc,v:vO/inc,a:zero..inf/dec]
[x:minf..zero/inc,v:vO..inf/inc,a:zero..inf/dec]
[x: zero/inc,v:vO..inf/std,:/dec)
vO ,
X . , X = zero,
vO. , , , ,
.
8 :
[: zero/inc,v:zero. .vO/std,a:zero/dec]
[x:zero..inffine, v:zero..vO/dec,a:minf..zero/dec]
[x:zero..inf/std,v:zero/dec,a:minf..zero/std]
X = zero vO. ,

504

II. Prolog

, , . 8
[ : zero/inc, v:vO/std,a:zero/dec]

.
:
, ?
, , ,
,
. , ,
. . ,
, .
,
! ,
, . , . QSIM , .
, , .
QSIM, ? [84],
QSIM
, .
QSIM , (.. ). . 20.10
, , ,
QSIM , . , - , .
"" QSIM . , , QSIM,
. ,
, - ,
. , .
, .
, , ,
. ,
, , . , ,
, .
20.

505

, ,
, :
1

- mv + - ' = c o n s t .
2
2
. , .
. , , X = 0 V2 = vO2. , V = vO,
X = 0, X = 0, V = vO V = -vO.
, {. 20.5),
.
l e g a l s t a t e l [ X, V, ]) :d e r i v f X, Vj,
d e r i v ( v. A),
MimisA = ;_,
sum( A, MinusA, a : z e r o / s t d > P
% MinusA = -A
mplus( X, KinusA),
%
energy[ X, VI.
%
e n e r g y ! X, V) :V = v:vO/_, !, . = x : z e r o / _
% V=vO, X
% zero
X = x : z e r o / _ , !, V = v : m i n f . . z e r o / _
true.

% V
% -vO

% X , zero,
% V -

. 20.10.
, QDE , DEI, DE2 DE3. a S1.
S2 S3 .
Ql, Q2 03
QDE; Q1
SI, Q2 52 S3, Q3 , -

506

II. Prolog


, "" , , ,
, " ".

.
( ), (
) , ,
(, ). ,
.

.
,
,
" ", .
.
, ,
pos, zero neg. , .
. (Qualitative Differential Equation QDE)
. QSIM , , . , QSIM, , , ,
, .

, , . ,
,
. ,
,

. .
:
;
;
;
;
;
20,

507

;
;
(Qualitative Differential Equation
QDE);
;
, ;
;
QSIM;
;
;
.


Artificial Intelligence [8], "Qualitative Reasoning about Physical Systems" ( ).
;
, [40], Qualitative Process Theory ( ) [53].
,
, (QDE), QDE. ,
.
Artificial Intelligence [41], . , 1990 [167]. [48].

Workshop on Qualitative Reasoning.
,
, QSIM [84] QDE.

QSIM,
. [92] [135]
QSIM. [85] , , QSIM. [138]-[140] . ,
20.7, (Cem Say) .
,
. ,
-
, [20], ,
, .

508

II. Prolog

[52] .
,
. QDE
[22], [36], [64], [83], [141] [162]. QDE
. [108] [109] ( [20]) , ,
.

20.

509

21

...
21.1. Prolog
21.2.
21.3.

510
516
521

Prolog , DCG (Definite Clause Grammar , DC-).


Prolog. ,
DCG, Prolog . DCG
, ()
, DC-, . , DCG
() , , : "Every woman chat
admires a nan t h a t p a i n t s l i k e s Monet" ( , , , ).

2 1 . 1 .
Prolog
. ,
- , , ,
, ,
, .
BNF (BackusNaur Form -), .
BNF. . BNF, .

<s>::=a<s>b

: s ab, , s
, , ,
, s " " .
, s . , b .
. BNF
:
<s>::=ab|a<s>b

, .
,
. , s.
. , .
, . :
$
s :
b

, :
& s b b

:
a a a b b b

, , ab, aabb ..
a t " , . = 1, 2, 3, ... . , , , .

.
. , .
, . ,
.
up ;
dewn .
, .
up
up up down up down

, .
"". , (move)
(step), , . :
21.

511

<move>
<move>
<Step>
<step>

:::;=
: : =
::=

<step>
<step> <raove>
iip
down

,
Prolog1,
, .
, . , ; , ,
. , ,
. ,
,
: , , . ,
.
, .

, .
,
. , Prolog .
Prolog
, DCG (Definite
Clause Grammar ). Prolog.
, DCG,
, . BNF
DCG .
, BNF DCG :
> [ ] , [] .
s > [ a ] , s , [] .
move - - > step,

move --> s t e p , move.


step --> tup].
step --> [down].

,
BNF DCG. " : : = " " - - > " ,
, s
Prolog. ,
, ,
Prolog.
Prolog, DCG, . , , ,
, {
8.) 512

II. Prolog

.
, , .
aabb [ a, a, b, b] [ ]
[ a, a, b, b, ] [ ]
[ , , , , 1, , 1] [ 1, 0, 1]

, , DCG , , ,
:
?- s( [ , , , ] , [ ] )
yes
?- s([a,a,b], []).
no
?- move( [ up, u p , down],
yes
?- move( [ up, up, l e f t ] ,
no
?- move( [ up, X, u p ] , [ ] )
X = up;
X = down;
no

% aabb

[]).
[]).
.

, Prolog DCG . Prolog


Prolog. , Prolog ,
.
. DCG, , :
move( List, Rest) :step( List, Rest),
move( Listl, Rest) :step( Listl, -List2),
move( L i s t 2 , R e s t ) .
step(
step(

[up | R e s t ] , R e s t ) .
[down | R e s t ] , R e s t ) .

? move. move :
move( List, Rest)

,
L i s t Rest . :
move(

[ up,

down,

up],

[]).

move( [ u p , down, u p , a, b, c ] ,

move( [ u p , down, u p ] ,

[ a, b, c] ) .

[ down, u p ] ) .

. 21.1 , :
move( Listl, Rest) :step( Listl, List2),
move( List2, Rest).

:
Listl Rest ,
Listl List2
List2 Rest - .

2 1 .

513

. 21.1.

,
: ( L i s t l , Rest)
, ( L i s t l , List2) ( L i s t 2 , Resc). 8, , , cone.

, DCG
Prolog. DCG
Prolog . ,
DCG :

-->

nl,

. i

nn.

n l , 2, ..., , :
n[ Listl, Rest) :nl ( Listl, List2} ,
n2 ( List2, List3} ,
nn( Listri, Rest) .

- n l , n2
nn ( DCG ), .
,
. DCG:
n>nl,

[ t 2 ] , ,

[ t4] .

nl , a t2 t4 .
:
n i L i s t l , Rest) : .1 ( L i s t l , [t2 | L i s t 3 ] ) ,
n3 ( L i s t 3 , [t4 | Rest] I .


.
DCG.
.
sentence > noun_phrase, verb_phrase.
verb_phrase --> verb, noun_pbrase.
phrase > determiner, noun.

514

II. Prolog

determiner -> [ a ] .
determiner- --> [ the] .
noun --> [ c a t ] ,
noun --> [ mouse].
verb --> [ s c a r e s ] ,
verb --> [ h a t e s ] .
, .
[ t h e , c a t , s c a r e s , a, mouse]
I
[ t h e , mouse, h a t e s , t h e , cat]
%
[ t h e , mouse, s c a r e s , t h e , mouse]
%

, [ t h e , mice,
h a t e , t h e , c a t s ] ( ) ;
noun --> [ c a t s ] .
noun --> [ mice].
verb --> [ scare].
verb > [ h a t e ] .
, ,
. , , , , , , , :
[ the, mouse, hate, the cat] %
, :
sentence --> noun_phrase, verb_phrase.
,
.
.
, .
(context dependency).
, . BNF, DCG
, DCG, BNF,
, . , (Number) :
noun_phrase ( Number)
verb phrase{ dumber)
, , , .
sentence < Number) --> noun_phrase( Number), verb_phrase( Number).
verb phrase( Number) --> verb( Number), noun_phrase( Numberl).
nou:n_phrase ( Number) --> d e t e r m i n e r ! Number) , noun { Number) .
noun( s i n g u l a r ) --> [ mouse). %
noun( p l u r a l ) --> [ m i c e ] . I
Prolog1 DCG
Prolog
,
. ,
sentence( Number) --> noun_phrase( Number), verb_phrase( Numbet),
:
sentence( Number, Listl, Rest) : ncun_phrase( Number, Listl, Llst2),
verb_phrase( Humber, List2, Rest).
21,

515

Prolog, , .
?- sentence( plural, [ the, mice, hate, the, c a t s ) , []}.

yes
?- sentence( plural, [
no
?- sentence[ plural, [
no
?- sentence( Kumber, [
Number - singular
?- sentence( singular,
What = cat;
What - mouse;
no

the, mice, hates, the, cats], [) ) .


the, mouse, hates, the, cat], []).
the, mouse, hates, the, cat] , []).
[ the, What, hates, the, cat] , []}.

23.1. Prolog DCG:


s > [] , s, [] .

21.2. Prolog
t r a n s l a t e ! DCGrule, PrologClause)
DCG
Prolog.
21.3. DCG , ,
:
move --> step, move.


move --> move, step.
, ,
, . Prolog . , . ?
:
?- move [ [up, l e f t] , [] ) .

21.2.
21.2.1.
.

[ the, cat, scares, the, mice]

,
. 21,2, ; , . [ tlie, mice] , nounjphrase ( ), [ s c a r e s ,
t h e , mice] ,
verb_phrase { ).
. 21.2,
.
516

II. Prolog

. SI.2, "the
( , )

cat

scares

the mice"

.

.
1. .
2. ;
, .
3. - ; ,
3>, q, .

s,
, q {. 21.3).

. 21.3. s
p. q

, , ,
.
DCG.
Prolog, , . ,
" t h e c a t " :
nounjphrase{ determiner! the) , noun( cat))
DCG

. , :
noim_phrase ( DetTree, NounTree)
21.

517

DetTree NounTree
(determiner) (noun). ,
, :
noun_phrase[ noun_phrase( DetTree, NounTree)) >
determiner( DetTree), noun; NounTree).
, .
, noun_phrase ( DetTree, HounTree), :
,
DetTree;
, NounTree.
.
. ,
sentence( Number, sentence( NP, VP!) >
noun_phrase{ Number, HP),
verbjphrase{ Number, VP).
verb_phrase( Number, verb_phrase( Verb, NP)) -->
verb ( Number, Verb) ,
noun_phrase( Numberl, NP).
noun_phrase( Number, noun_phrase( Det, Noun)) -->
determiner( Det),
noun| Humber, Noun),
determiner[ d e t e r m i n e r ! the)] > [ the] .
noun! s i n g u l a r , rsoun( cat) ) > [ cat] .
noun{ p l u r a l F ncurH c a t s ) ) > [ c a t s ] .
Prolog Prolog. ,
, :
s e n t e n c e ! Number, sentence{ NP, VP), L i s t , Rest) :noun_phrase( Number, NP, L i s t , RestO),
verb_phrase( Number, VP, RestO, R e s t ) .
Prolog , :
- sentence( Number, ParseTree, 1 t h e , mice, h a t e , t h e , c a t ] , [J) ,
Mumber = p l u r a l
ParseTree = s e n t e n c e ; noun_pnrase( determiner[ t h e ) , noun[ m i c e ) ) ,
verb_phrase{ verb( h a t e ) , noun_phrase[ d e t e r m i n e r ! t h e ) ,
noun( c a t ) ) ) )

21,2.2.

Prolog
, . , . .
1. .
2. .
518

II. Prolog

, , , , ; , , .
, .
. .
, , .
move( move( Step)) > step{ Step).
move( move( Step, Move)) > step( Step), move I Move).
step( step( up)) > [ up] .
step( step( down)) > [ down].
. 1 . "up up
down up" 1 + 1 - 1 + 1 = 2. ( d i s t a n c e ) , . 21.4. .
d i s t a n c e ( ' u p up down u p ' ) =
d i s t a n c e ( ' u p ' ) + d i s t a n c e t ' u p down up')

= 1 + 1 = 2

. 21.4.
, ( )
.
meaning{ ^, Value)
r.ieaning( move ( Step, Move), Dist) :meaning) Step, Dl),
meaning! Move, D2),
21.

519

Dist is Dl + D2 .
meaning( move{ Step), Dist) :meaning( Step, Dist).
meaning! step! up), 1 ) .
meaning! step( down), - 1 ) .

, ,
"up up down up" :
? move[ Tree, ( up, up, down, u p ] , [ ) ) , meaning[ Tree, D i s t ) ,
Dist = 2
Tree = move( step ( up), move( step( up), move( stept down), move) step t up)))))

21.4. meaning , .. ,
, , .
?- move( Tree, Move,

[ ] ) , meaning( Tree, 5 ) ,

21.2.3.
DCG
DCG
, . , DCG, .
Prolog . , . , , , Prolog, Prolog.
, , .

, . ; , move sLep,
, :
move[ Dist)
move,
Dist. .
move(
move(
stepi
step{

D)
D)
1)
-1)

-->
>
-->
>

s t e p ! D),
s t e p ( Dl), move( D2), {D is Dl + D2},
[ up].
[ down].


, .
, .
, D, :
, D1;
, D2,
D is Dl + D2.
520

II. Prolog

DCG , .
,
, , (collapsed). , , ,
. :
, .
. , gl 2. ,
gl, 1 , 2 2 . ,
(gl 2) ,
s t o p . .
stop
gl up up stop
gl up up g2 down up stop
gl gl g2 up up gl up down up g2 stop
(..
):
Dist = 2 * ( 1 + 1 1 + 1 * ( 1 - 1 + 1 ) = 5

:
:;( 0) --> [ stop] .
prog (
prog(
gear(
gear{

Dist)
Dist)
1) >
2) >

--> ( _), ( Dist).


> gear( Gl, move[ D) , prog( Distl), {Dist is G * D + Distl}.
[ gl].
[ g2).

21.3.

21.3.1.

,
.
,
, . () , .
, . ,
, .
21.

521

, ?

. ,
, ,
, ,
.

.
, . ;
, .
,
DCG. Prolog.
, , , . .
.
"John p a i n t s " ( ).
(
Prolog) :
paints( john)
, "" , p a i n t s .
"John l i k e s Annie" { ).
:
likes( john, annie)
"" , l i k e s
.
DCG. ,
.
, , .
sentence --> noun_phrase, verb_phrase.
%
*
noun_phra3e --> proper_noun.
%
verb_phrase --> intrans_verb.
% :
verb_phxase --> trans_verb, noun_phrase,
%
intrans_verb --> [ p a i n t s ] .
trans_verb --> [ l i k e s ] .
proper_noun --> [ john].
proper_noun --> [ annie).

. ( ), . , . -, .
john john:
proper_noun ( john) --> [ john].
-, , "". .
:
paints( X)
522

II, Prolog

X , ,
, . DCG
p a i n t s :
intrans_verb{ paints( X)) --> [ p a i n t s ] .
: , john p a i n t s { X), p a i n t s ( john) ?
, X p a i n t s john.
, . 21.5. ,
.
() , noun_phrase verb_phrase proper_noun i n t r a n s _ v e r b ,
,
noun_phrase{ HP) --> prcper_noun( HP).
verbjphrase{ VP) --> intrans_verb{ VP).

. 21.5. "John paints", , . , . ,


. .
s e n t e n c e ; S) > n o u n _ p h r a s e { HP), v e r b _ p h r a s e ( V?) ,

{compose( NP, VP, S ) } .

compose! NP, VP, 3)


john
p a i n t s [ X), , X p a i n t s { X), :
actor( VP, Actor)

21.

523

Actor VP. a c t o r :
actor!

paints(

X),

X).

a c t o r :
compose( NP, VP, VP) :a c t o r ( VP, NP).

% VP,
% ^ VP NP

, , . compose a c t o r , X p a i n t s [ X) "" , .
,
verb_phrase i n t r a n s _ v e r b , X , .
intrans_verb( Actor, paints{ Actor)) --> [ paints] .
verb_phrase[ Actor, VP) > intrans^verb( Actor, VP) .
Actor

:
sentence! VP) > noun_phrase( Actor), verb_phrase( Actor, VP).
"" (Actor)
.
, , , DCG. ,
.
"" , p a i n t s ( SorrieActor). , ( SomeActor). ( ""),
.
Prolog. , . , , " " ,
;
verb p h r a s e ( Actor, VPMeaning).
.
"" l i k e s ( Somebody, Something) (-
-), Sorr.ebody Something , . , :
trans_verb( Somebody, Something, likes( Somebody, Something)) > [ l i k e s ] .
, Something,
:
verb_phrase( Somebody, VP) -->
trar.s_verb( Somebody, Something, VP) , noun_phrase ( Something).

, DCG .
524

II. Prolog

, "" { ) "every" ( ""), . .

21.3.2. "" "every"


, , "",
,
. : "A man p a i n t s " (
). ,
p a i n t s ( man), : " ,
", " ". :
X, , X - X .
X, , , (
""). , , Prolog:
e x i s t s ! x, man( X) and p a i n t s ! X)}
X, , and.
:
:- ( 100, xfyr and>.
,
,
, "". ""
. , , "a man".
:
' X, , X - .
, "a man", "a man
p a i n t s " , - ( , , ). "a man"
:
e x i s t s ! X, man! X) and Assertion!
A s s e r t i o n X. , X,
, .. ,
"a man". A s s e r t i o n ,
, .

"". :
X, ,
X (, man ( X))
- , X
(, p a i n t s ( X)) .

Prolog :
exists! X, Property and Assertion)

, Property A s s e r t i o n , , .
21.

525

"", . DCG "" .


determiner[ X, Prop, Assn, exists ( X, Prop and Assn)1 --> [ a] .
""
. , "every", . "Every woman dances" ( ). .
X, X . X .
Prolog:
a l l I Xr woman( X) -> dances{ XI)
"=>" , .
, "every" ,
:
a l l ( X, Property ~> Assertion)
DCG, "every"
, ,
determiner! X, Prop, Assn, all{ X, Prop -> Assn)) --> [ every].
, ,

. , "A man p a i n t s " , :
exists!

X,

man[

X)

and p a i n t s [

X))

" " :
exists[ X, Prop and Assn)
,
. ,
. 21.6; ,
"" :
exists ( X, Prop and Assn)
Prop , a Assn . .
, , , . , , , . 21.6, DCG:
s e n t e n c e ! S) > noun_phrase( X, Assn, S), verb_phrase( X, Assn).
no-un_phrase [ X, Assn, S) --> d e t e r m i n e r ; X, Prop, Assn, S) , noun ( X, Prop) .
verb_phrase( X, Assn) --> i n t r a n s _ v e r b ( X, Assn).
i n t r a n s _ v e r b ( X, p a i n t s [ X)) --> [ p a i n t s ] .
determiner< X, Prop, Assn, e x i s t s ! X, Prop and Assn)) > [ a ! .
noun( X, man( X]) --> [ man].

Prolog
" p a i n t s " , .
?- sentence( S, [ a, man, paints), []).
= exists! X, man { X) and paints! X))

Prolog , _123, Prolog, X.


526

II. Prolog

. 21,6. "A man paints".


"".

,

, ,
, "John p a i n t s " .
, ,
"A man p a i n t s " , "John p a i n t s " .
. ,
.
pcoper_noun( John) --> [ John].
noun_phrase ( X, Assn., Assnl --> proper_noun j X).
, " ", .. Assn. ( ),
:
?- sentence( S, [ John, p a i n t s ] , [ ] ) .
S = paints i John)

21.5. , "John p a i n t s "


. , : ,
, , .

21.3.3.
(relative clause), , "Every man t h a t
p a i n t s admires Monet" ( , , ).
21.

527

"every man t h a t p a i n t s "


, " t h a t p a i n t s " .
, , :
noun_phrase --> determiner, noun, rel_clause.
rel_clause --> [ t h a t ] , verb_phrase.
rel_clause -->{].
I
. "Every man t h a t p a i n t s admires Monet" :
,
X - X ,
X .

Prolog:
a l l ( X, man( X) and p a i n t s ! X) => a d m i r e s ! X, monet)]

, "and" , "=>".
,
"every man t h a t p a i n t s " :
a l l ( X, man( X) and p a i n t s [ X} => Assn)


a l l ! X, Propl and 2 => Assnl
Propl , Prop2
, a Assn . DCG ,
, .
rel_clause( X, Propl, Propl and Prop2) > [ t h a t ] , verbjphrase( Xr Prop2).
noun_phrase( X, Assn, S) > determiner( X, Propl2, Assn, S],
noun[ X, Propl),
rel_clause( X, Propl, Propl2].
, , :
rel_clause(

X,

Propl,

Propl)

>

[j.

21.1 DCG , , " " "every", . , .


John paints.
Every man that paints admires Monet.
Annie admires every man that paints.
Every woman that admires a man that paints likes Monet.

21.1. DCG,

:- [ 100, xfy, and).
:- ( 150, Kfy, =>).
sentence( 3) -->
noun_phrase( X, P, S ) , verb_phrase( X, P ) ,
noun_phrase( X, P, S) -->
determiner! X, P12, P, S ) , noun( X, P1J, rel_clause( X, PI, P12).
noun_phrase [ X, P, pj
proper_noun( X ) .
verb_phrase( X, P)

528

>

>

II, Prolog

trans_verb[ X, Y, PI), noim_phrase ( Y, PI, P) .


verb_phrase( X, P) >
intrans__verb( x, P ) .
rel_clause[ X, PI, PI and P2)
[that), verb_phrase[ X, P2).
rel_clause[ X, PI, PI)

>

--> [],

determiner) x, PI, P, all[ X, PI => P))

--> [every].

determiner( X, PI, p, existst x, PI and pj} > [aj.


noun[ X, man(X) )

~>

[man] .

noun[ X, woman(X))

-->

[woman).

proper_rtoun[ John)

>

tjohnj .

proper_noun[ annie)

-->

[annie).

proper_noun( monet)

>

[monet].

trans_verb( X, Y, likes ( X, Y) )

-->

trans_verb{ X, Y, admires! X, Y))


intran5_verb( X, paintS{X>]

>

[ likes].

>

[admires],

[paints].

%
testl( M> :sentence! M, [john,paints],[]).
test2[ M) :sentence( M, [a, man, paints!, )
test3{ M) :sentence( M, [every,man,that,paints,admirea,manet] , [] ) .
test4( M) :sentence( M, [annie,admires,every,man,that,paints],[]),
test5( Mi

:-

sentence( M, [every, woman, that,admires,a,man, that,paints,likes,monet], []}.


,
:
?- sentence( Meaningl, [ every, man, that, paints, admires, monet], [ ] ) .

Meaningl = a l i i X, man( X) and p a i n t s [ X) => admires( X, monet)]

7- s e n t e n c e ! Meaning2, 1 a n n i e , admires, every, man, t h a t , p a i n t s ] ,


Meaning2 = a l l ! X, man( X) and p a i n t s ! X] => admires( a n n i e , X))

[]).

?- sentence ( Meaning3, [ every, -woman, t h a t , admires, a, man, t h a t , p a i n t s ,


l i k e s , monet] , [] ) .
Meaning3 a l l ( X, woman! X) and e x i s t s ( Yr { man! V) and p a i n t s ! IT J! and
admires( X, Y)) -> l i k e s ! X, monet))

, , .
, , , : "Does Annie
21.

529

admire anybody who admires Monet?" ( -,


). , . , ,
. , Prolog.
Prolog. ,
. , Prolog.
paints( John).
admires ( X, aionet) : man( x ) ,
paints[ X),
admires( annie, X) :man( X),
paints[ X) ,
"Does Annie admire anybody
who admires Monet?" Prolog:
?- admires( annie, X), admires( X, menet).
X - John

21.6. :
) Mary knows a l l i m p o r t a n t a r t i s t s ( ).
) Every t e a c h e r who t e a c h e s French and s t u d i e s music -understands
Chopin ( , ,
).
) A charming lady from F l o r i d a runs a beauty shop in Sydney ( ),
21.7. , 21.1, .
. , , tes~5, 21.1, :
?- a l l (X, woman. (X) and exists (Y, (man (Y) and paints (Y) ) and
admires(X,Y))
=> likes[X,monet)), sentence( M, S, [] I .
Prolog :
S [every,woman,that,admires,a,man,that, paints,likes,monet]
M = all (__022C, woman [_Q22O and exists (_02FC, (man [_02FC) and
paints(_02FC( )
and admires[_022C,_D2Fc; ) -> likes(_022C,monet))
.
, :
= all{monet,woman[monet) and exists[_Q364,(man(_0364) and paints(_0364))
and admires (monet, _0364)) => likes [monet, monetj )
S = [monet,likes, every,woman,that,admires r a,man,that,paints] %
% , ,

530

II. Prolog

, , . : ,
,
(, X a l l {X, . . . ) ) , (monet};
.
21.8. , 21.1, "if", "then", "and", "or", " n e i t h e r " ,
"nor" .. :
John paints and Annie sings.
*
If Annie sings then every teacher l i s t e n s .
,
%

, 21.1,
Prolog. ,
( ; .
6)
Prolog. , , .
,
.

, BNF,
DCG (Definite
Clause Grammar ). , DCG, Prolog,
, .
DCG
. .
DCG,
,


DCG,
, [121].
[120] ,

Prolog. [93]
DCG,
. [156] DCG
. 21.8 [77],
. , , [5], [38] [57].
21.

531

22


...
22.1. ,
22.2.
22.3. --:
22.4. , :

22.5.
22.6. Advice Language 0

532
534
537
541
543
546

,
, .
, , . , --. ,
, , , Advice Language. , Prolog
, ,

2 2 . 1 . ,

,
, . , .
. , . , , , . , .
.
, .
,
.
,
. :

. , , : . . , , . ,
,
.
AND/OR, 13. . 22,1
, AND/OR .
22.1, ,
ID/OR

AND/OR

OR

AND

,
, AND/OR.
, , , .
won( Fos)

:terminalwonf Pos}.
%
won( Pos) : not t e r m i r i a l l o s t ( Pos),
move( Pos, Posl},
%
not ( move I P o s l f Pos2), \
not won( P o s 2 ) ) .
%

, Posl

raove ( Pos, P o s l ) ,
, terTrdnalwon ( Pos) t e r m i n a l l o s t ( Pos),
, .
, , , .
, .
AND/OR,
. ,
.
, .
, . ,
.
.
,
, . - , , 22.

533

. . 22.1 .
101> . ,
, . 22.1, .
, , .

. 22.1, . ,
30 ,
40 . 2 ( 1
)

- ( .) , AND/OR,

22.2.

, , .
, ( ), . , , ,

. , ,
. . .
, , ( ).
,
" " " ". () , , , .
.

534

II. Prolog

. ,
. , , ,
.
, , MIN.
, , , .
MIN ,
. ,
( ) , . 22.2. , , ,
MIN. . . ,
, , . . 22,2
4, -.
MIN b-d .. .
, .
, .
, .

. 22.2. ( )
. , , , .. ,
( Prolog 22.1)
22.1. , 22.2, Prolog
% moves( P o s i t i o n , P o s i t i o n L i s t ) :
moves (
moves t
moves (
moves(
moves (
moves(

a,
b,
c,
d,
e,
f,

[b,c] > .
[d,e] ) .
[ f, g] ) ,
[i,j]) .
[};, 1] } .
[m,n]).

22.

535

moves( g, (o y p]).
% min_tc__move ( Pos) : KIN Pos
min__tQ_move ( b) .
rain_to_move( }.
h max^to_move( ; : MAX
max_to_move{ ) .
ma>:_to_jnove { d) .
max_to_move{ ) .
ma>:_to_move { f) .
max_t<3__move ( g) .
% staticval{ Pos, Value): Value - Pos
staticval[ i, 1 ) .
staticval( j, 4 ) .
staticval( k, 5 ) .
staticval(1, 6}.
staticval( m, 2 ) .
staticval( n, 1),
staticval( o, 1).
staticval{ p, 1),


. ,
, ., .
.
:
v(P)

:
V[P)

, Pi, ,.., , - .
, .
( = 0), V ()
, V ()

= max

= v (P).

ViPJ,

MIN, V(P) = min V(Pi),


i

Prolog1,
, 22.2.
:
minimax{ Pos, BestSucc F Val)

Val Pos, a BestSucc ,


Pos (, Val).
moves( , PosList)

; PosList Pos. , moves ,


( ), :
best) PosList, BestPos, BestVal;
536

II. Prolog

"" BestPos PosList.


BestVal BestPos 5. "" , , .
22.2.
% minimaxl Pos, BestSucc, Val):
%
Pos - , Val - ;
,
1
Pos, BestSucc
minimaxl Pos, BestSucc, Val)
moves( Pos, PosList), !,

:% , P O S ,
i PosList

best! PosList, BestSucc, Val)


staticval[ Pos f Val).

% Pos ,
%

best( [ Pos], Pos, Val) :minimax[ Pos, _, Val), !.


best( [Posl 1 PosList], BestPos, Bestval) :minima::! Posl, _, Vail),
best[ PosList, Pos2, Val2),
betteroff Posl, Vail, Pos2, Val2, BestPos, BestVal).
betterof( PosO, ValO, Posl, Vail, PosO, ValQ)
inin to move ( PosO) ,
ValO > Vail, !
max_to_move( PosO),
ValO < Vail, !.

:- % PosO , Posl
% PosO
% MIN
M A X
%

% PosO
h MIN

betterof( PosO, ValO, Posl, Vail, Posl, Vail).

%
Posl - , PosO

22.3. --:

, 22.2, , ,
. ,
.
. : , ; , ,
, ,
. , , . 22.2. , .
1. .
2. .
22.

537

3. .
4. d,
V(d) = 4.
5. , .
6. , 5. ( ) , , 5 , ,
( ) .
MIM, ,
, d, ,

5. b , , .
-- ( --), . . 22.3 --
, . 22.2, . 22.3, . ,
,
, , . 22.3,
- era- ( , 22.2) .

. 22.3. , . 22.2, . ,
, .
( ,
, , f; . 22.2). ,
,

, -- ,
" " ( ),
. ,
,
Alpha Beta. : Alpha , MAX, a Beta
, .
WIN,
538

It. Prolog

MIK, MIH. ( ) P.lpha Beta. , ,


Alpha-Beta, , , , . , Alpha
Beta. V( P, Alpha,
Beta) Alpha Beta , :
Vt P, A l p h a , B e t a ) < A l p h a , V(P> < A l p h a
V < P , A l p h a , B e t a ) = v ( p ) , A l p h a < V[P) < B e t a
V( P, A l p h a , B e t a ) > B e t a , V(P) > B e t a
, V(P)
, :
V(P, - , + } = V(P)

22,3 -- Prolog. :
alphabets [ Pos, Alpha, Beta, GoodPos, Val)
GoodPos , Val , , :
Val = V( Pos, Alpha, Beta)

22.3. --
% --
alpbabeta( Pos, Alpha, Beta, GoodPos, Val) :moves( Pos, PcsList), !,
boundedbeat( PosList, Alpha, Beta, GoodPos, Val);
Staticval( Pos, Val).
% Pcs
boundedbestf [Pos 1 PosList], Alpha, Beta, GoodPos, GoodVal) :alphabeta{ Pos, Alpha, Beta, _, Val),
goodenougM PosList, Alpha, Beta, Pos, Val, GoodPcs, Good val] .
goodenough( [], _, _, Pos, Val, Pos, Val)

:-

!.

goodenoughI _, Alpha, Beta, , Val, Pos, Val)


min to move( Pos), Val > Beta, !
~~ ~~
max_to__moue ( Pos), Val < Alpha, !,

:$ ,
%
% ,
%

goodenoughi PosList, Alpha, Beta, , Val, GoodPos, GoodVal) :neutrounds( Alpha, Beta, Pos, Val, NewAlpha, MewBeta),
%
boundedbest[ PosList, NewAlpha, MewBeta, Posl, Vail),
betterofl Pos, Val, Posl, Vail, GoodPos, GoodVal).
newbounds( Alpha, Beta, Pos, Val, Val, Beta)
min_to_move'{ Pos), Val > Alpha, !.
newbounds( Alpha, Beta, Pos, Val, Alpha, Val)
max to move( Pos), Val < Beta, !.
newbounds( Alpha, Beta, _, _, Alpha, Beta).

22.

:*
l
:I
%
I
%

539

betteroft Pos, Val, Posl, Vail, Pos, Val)


min_to_itLove! Pos), Val > Vail, !

:-

% Eos , Posl

max_to_move( Pos), Val < Vail, !.


betterof( _,

, Posl, Vail, Posl, Vail).

%
% Posl

baundedbest( PosList, Alpha, Beta, GbodPos, Val)


GoodPos PosList ,
Val GoodPos Alpha Beta.
-- ( !) --.
newbounds[ Alpha, Beta, Pos, Val, NewAlpha, NewBeta)

( NewAlpha, NewBeta), [ Alpha, B e t a ) . Alpha-Beta


. -
. :
" -- , 22.2?" - -
, .
.
, , - ,
. , --
.
, . , N , .
, ,
, - - %/N .
,
. , ,
, .
--
, . , ,
, .
--
( , ) . , ,
-- , . b ^ .
--
6 ,
30. ,

540

. Prolog

, --,
1 ( ) 6 .

(,
).
( )
, -.

22.4. ,
:
, --,
;
. : - ,
( , ),
;
( --), .
- . , , .
.
.
, , . , , .
"" ( , )
, . ,
,
( ) , . , . ,
, , ,
, ,
. ,
,
, . , . ,
, . , ( )
.
. 22.

541

. ,
--. , ,
.
.
--, , .
.
, . .
;
, .

, --
.
( ), .
, ,
. , , , , . , , , .
( "", ), , ,
, ,
, ,
. ( ),
.
.
, ,
, , , .
. { ) , .
,
, .
" " ,
. ,
, . ,

.
, , , .
.
.
.
, .
, . :
542

II. Prolog

, , 0. , . , , , ,
.
, ,
,
. , , , . ,
.
, , .
.

22.5.
22.5.1.
, , , , Advice Language. Advice Language ,
.
. Advice Language
, .
Advice Language ,
(piece-of-advice). , ( ) .
, , ,
.
; , . .
(better-goal) , ,
(holding-goal) , .
(us-move-constraints) , (,
).
(them-move-constraints) ,
,
(, ,
).
,
" ",
, - .
.

22,

543

.
.
.
.

22.5.2.
, , , , .
1. .
2. .
3. , .
. , , . , ,
. F , , :
;
;
,
;

, ;
(
) .

, .
, ; ,
,
, , ,
.
, .
, .

22.5.3.

Advice Language
. .
.
.
,
.
, , 544

II, Prolog

" ". , .
Advice
Language, .
Advice Language 0, AL0.
AL0,
Prolog.
AL0 .
. :
RuleName :: if Condition then AdviceList
Condition , , and, or, not, a AdviceList . " e d g e _ r u l e " ( ), " ".
edge_rule ::
if their_king_on_edge and our_king_close then [ mate_in_2F squeeze,
approach, keeproom, divide].
,
, ( ,
), : "mate_ir J _2",
"squeeze", "approach", "keeproon", "divide". : ; ,
"" , .. ,
Prolog.
Prolog
advice( AdviceName,
BetterGoal:
HoldingGoal:
Us_Move_Constraints :
Them_Move_Constraints).

, and,
or, not. ,
and then, and , a then
. , :
MCI t h e n MC2

, 1,
, 2.
, 2 "
", , :
advice ( mate_in_2, mate :
not rooklost:
(depth - 0) and legal then (depth - 2} and checkmove :
(depth - 1) and l e g a l ) .
mate (), not
r o o k l c s t ( ). ,
( ) , 2 ( ) . .
: 1.

22.

545


: ,
, ; .. :
Pos ; Pos ,
, Pos
.
,
, Pos.
,
, .
, . , . , , , .
. .

. AL0 ,
, , .

22.6.
Advice Language
ALO
.
1. AL0.
2. ALO.
3. , (
).
, .
AL0 .
.

22.6.1. ALO
, AL0, Prolog, 22.4. . AL0; ,
ALO, ,
.
AND/OR {. 13); AND/OR, ,
(. 15).

546

II. Prolog

22.4. Advice Language


Advice Language
%
i , ,
% Advice Language
:- opl 200, xfy, [:,::].
:- op<220,xfy, . .) .
:- op{ 185, ix, if) .
:- op[ 190, Hfx, then).
:- op( 1ES0, xfy, or) .
:- op( 160, xfy, and).
:- op ( 140, fx, notj .
playgame( Pos) :% Pos
playgame( Pos, nil).
%
playgame( , ForcingTree) : show! Pos),
( end_of_game( Pos),
% ?
writef 'End of game'), nl, !
playmove( Pos, ForcingTree, Posl, ForcingTreel), !,
playgame( Posl, ForcingTreel)
)

%
playmove( Pos, Move..FTreel, Posl, FTreel) :side( Pos, w)
% ,
%
legalmove.( Pos, Move, Posl),
showmove( Move).
I ,
playmove( Pos, FTree, Posl, FTreel) :side( Pos, b ) ,
write( 'Your move: ' ) ,
read( Move),
(legalmove( Pos, Move, Posl),
subtree; FTree, Move, FTreel), !
%
write[ 'Illegal move'), nl,
playmove( Pos, FTree, Posl, FTreel)
).
' ,
playmove( Pos, nil, Posl, FTreel) :side( Pos, w ) ,
resetdepth! P O S , P O S O I ,
% PosO - Pos
%
strategy! PosO, FTree), \,
%
playmove( PosO, FTree, Posl, FTreel).
%

, Move

subtree[ FTrees, Move, FTreel :member; ( Move . . FTree , FTtees } , ! .


subtree[_, _, nil).
strategy! Pos, ForcingTree) :% ? Pos
Rule ;: if Condition then AdviceList, %
% no
holds! Condition, Pos, _ ) , !,
% Pos
member! AdviceUame, AdviceList),
%

1
nl, write( 'Trying !, write! AdviceHame),
satisfiable{ AdviceName, Pos, ForcingTree), !. % AdviceHame
% Pos
satisfiable( AdviceKame, Pos, FTree) :advice! AdviceKame, Advice), %
sat! Advice, Pos, Pos, FTree). % sat
s ,

22.

547

sat( Advice, Pos, RootPos, FTcee) :holdinggoal( Advice, HG),


holds[ HG, Pos, RootPosI,
%
%
satl( Advice, Pos, RootPos, FTree).
Satl{ Advice, P O S , Root.Pos, nil) :bettergoal( Advice, BG),
holds[ BG, POS, RootPos),!.
i
satl( Advice, Pos, RootPos, Move..FTrees) :side[ Pos, w ) , !,
% ,
usmoveconstrI Advice, UMC),
move[ UMC, Pos, Move, Posl),
%
sat( Advice, Posl, RootPos, FTrees) .
Satl( Advice, Pos, RootPos, FTrees} :side | Pos, b ) , !,
i ,
themmoveconstr( Advice, TMC),
bagof( Move..Posl, move( TMC, Pos, Move, Posl!, MPlist),
satall( ftdvice, MPlist, RootPos, FTrees1.
%
% -
satall{ _, [] , _, [] ) .
satall { Advice, [Hove..Pos ! MPlist], RootPos, (Move..FT | MFTs] ) :sat( Advice, Pos, RootPos, FT),
satall( Advice, MPlist, RootPos, MFTs).
* : -
% , AND/OR/KOT
holds! Goall and Goal2, Pos, RootPos) :- !,
holds[ Goall, Pos, RcotPos),
holds ( Goal2, Pos, RootPos!.
holds{ Goall or Goal2, Pos, RootPos) :- !,
( holds( Goall, Pos, RootPcs)
holds( Goal2, Pos, Root)
).
holds[ not Goal, Pos, RootPos) ;- !,
not holds[ Goal, Pos, RootPos).
holds( Pred, Pos, RootPos) :( Cond =..[ Pred, Pos]
\ RootPos
i

Cond =..[ Pred, Pos, RootPos]),


call( Cond).
%
move! MCI and M.C2, Pos, Move, Posl) :- !,
nove( MCI, Pos, Move, Posl),
move 1 MC2, Pos, Move, Posl) .
move( MCI then MC2, Pos, Move, Posl) :- !,
( move! MCI, Pos, Move, Posl)
i
move( MC2, Fos, Move, Posl)
).
%
better-goal [ BG : _, BG! .
hoidinggoai( BG : HG : _, HG).
usmoveconstrf BG : HG : UMC : _, UMC).
theramoveconstrl BG : HG : UMC ; , TMC).
member( x, [X IL]).
member! X, [Y | L]} :member(X, L) ,

548

II. Prolog

, 22.4, ,
, .
playgame( Pos)
Pos ,
. Pos ,
, .
, , .
, , end_of_game (, ).
,

Move..[ Replyl..Ftreel, Reply2,,Ftree2, . . . ]


". ." , Move , Replyl, Reply2
.. , a F t r e e l , Ftree2 ..
, .

22.6.2. "
"
,
, , , . .
,
,
, .
1. .
2. ,
,
.
3. ,
.
4. 1, 2 3 ,
2 3 (.. ).
5. 1, 2, 3 4,
, , .
AL0
22.5.
AL0 (. 22.4). . 22.4 , , .

22.

549

. 22.4. , ,
22.5, ,
. keeproom ( ,
) squeeze ( ). , , jmo.ii .
squeeze

550

II. Prolog

22.5. ALO " ".



I " "
I Advice Language
edge_rule :: if their_king_edge and kings_close
then [ mate_in_2, squeeze, approach, keeproom,
divide_in_2, divide_in_3 ].
else_rule :: if true
then [ squeeze, approach, keeproom, divide_in_2, divide_in_3 ].
%
advice( mate_in_2,
mate :
not rooklost and their_king_edge :
(depth - 0) and legal then [depth - 2) and checkroove :
(depth - 1] and l e g a l ) .
advice[ squeeze,
newroomsmaller and not rookexposed and
rookdivides and not stalemate :
not Eooklost:
(depth - 0) and rookmove :
nomove).
advice) approach,
okapproachedcsquare and not rookexposed and not stalemate and
(rookdivides or lpattl and [roomgt2 or not our_king_edge)
not rooklost:
(depth - 0) and kir.gdiagfirst:
nomove),
advice[ keeproom,
themtomove and not rookexposed and rookdivides and okorndle and
(rootngt2 or not okedgej :
not rooklost:
(depth - 0) and kingdiagfirst:
nomove)
advice( dividejn 2,
themtomove and rookdivides and not rookexposed :
not rooklost:
(depth < 3) and legal:
(depth < 2) and l e g a l ) .
advice ( cfi vide in 3,
theratomove" and roofcdivides and not rookexposed :
not rooklost:
(depth < 5J and legal :
(depth < 4) and l e g a l ) .
, , . 22.2.
22.2. ,


mate

stalemate

rooklost

rookexposed

neKroomsmallet

, ,

rookdivides

22.

551

. 22.2

okapproacheficsquare
lpatt

it " ' (. 22.5); ,


L- (. . 22.5)

roomgt2

(room)


depth - N

, d e p t h = N

legal

checkmove

rookmove
nomove

kingdiagf irst

. 22.5. L : ) (, ,
,
);
, ; ) L-

( ),
( ). . , ( ) .
,
.
newroomsmaller, ,
(. . 22.4). ,
" " (show[ Pos)) 22.6.
22.6. " "
%

" "

i Side..Wx : Wy..RK : Ry..Bx : By..Depth

552

II. Prolog

%
%
%
%
*

Side - , ['w' - , '' - }


Wx, Wy - X Y
Rx, Ry - X Y
, By - X Y
Depth -

side ( Side. ._, Side) .


% Side - ,
wk( .,.. , WK) .
%
wr( __.WR.._, WR) .
t
(
, . , ) ,
%
depthf _.._.._.._..Depth, Depth).
%
resetdeptht S..W..R....D, S..W..R....). %
%

n( N, N1)

:-

%
%

( HI is + 1;
HI 15 - 1|,
in( N 1 ) .
in[N) :N > , N < 9.
diagngb( X : Y, XI : Yl) :( X, XI) , n( Y, Y D .
verngb( X : Y, X : Yl) :n( Y, Y1) .
borngb( X : Y, XI : Y) : { X, XI) .
ngb( S, SI) :diagngbl S, SI);
horngb( S, S I ) ;
verngbl S, SI).
end_of_game( Pos> :mate[ Pos).

% ,
% ,
% ,
i ,

%
% move! MoveConstr, Pos, Move, HewPos):
%

move( depth < Max, Pos, Hove, Posl) :depth! Pos, D) ,
D < Max, ! .
move ( depth D, Pos, Move, Posl) :depth{ Pos, D>, !.
move( kingdiagfirst, W..W..R..B..D, W-Wl, b..Wl..R..B..Dl) :Dl is D + 1,
ngb[ W, Wl), % ngb
not ngb{ Wl, S ) ,
%
Wl \== R.
%
roove( rookmove, W . . W . . R K : Ry..B..D, Rx : Ry-R, b..W..R....Dl) :Dl is D + 1,
coord! I ) ,
t 1
( R = Rx : I; R = I : R.y) ,
%
R \~~ Rx : Ry,
*
not inway( Rx : Ry, W, R ) .
i
move[ cbeckmove, Pos, R-Rx : Ry, Posl) :wr( Pos, R ) ,
bk[ Pos, Bx : By),
(Rx Bx; Ry = By),
%
move( rookreove, Pos, R-Rx : Ry, Posl).

22.

553

move( legal, w..P, M, PI) :( MC - kingdiagfirst; MC = rookmovej,


movef MC, w..P, M, PI}.
move! legal, b..W..R....D, B-Bl, w..W..K,,B1.,D1) :Dl is D + 1,
ngbl B, Bl),
not check( w.,W. .R. .Bl..Dl) .
legalmove[ Pos, Move, Posl) :move! legal, , Move, Posl}.
check(_.-W..Rx:Ry..Bx:By.._)
:ngb( W, Bx : By!;
( Rx - ; Ry = By],
Rx : Ry \== Bx : By,
not inwayl Rx : Ry, W, Bx : By).

inwayt S, SI, SI) :- !.


inwayt XI : , X2 : Y, X3 : Y) :ordered! X1 F X2, X3) , ! .
inwayl X : Yl, X : Y2, X : Y3) :orderedf Yl, Y2, Y3) .
ordered! N1, N2, N3) :N1 < N2, N2 < ;
N3 < N2, N2 < HI.
coord(1). coord(21 . coord(3). coord it).
coord(5). coord(6) . coord(7) . coord[8).
%
true[ Pos).
themtomovel b . . _ ) .

I ,
%

mate( Pos) : side[ Pos, b ) ,


check ( Pos),
not legalmove{ Pos, _, _ ) .
stalemate! Posl :side[ Pos, b ) ,
not check( Pos),
not legalmove[ Pos, _, _ ) .
newroomsmaller{ Pos, RootPos) :room( Pos, Room},
room( RootPos, RootRoom),
Room < RootRoom.
rookexposed [ Side..W..R..B.._ ) :distf W, R, Dl),
dist( B, R, D2),
[Side = w, !, Dl > D2 + 1
Side = b, !, Dl > D2).
okapproachedcsquaref Pos, RootPos} :okcsquaremdist( Pos, Dl),
okcsquaremdist( RootPos, D2) ,
Dl < D2.
okcsquaremdist1 Pos, Mdist) ;k
i WK
wk[ Pos, WK),
( Pos, CS),
i
manhdistf WK, CS, M d i s t ) .
r o o k d i v i d e s ( _.-WK : ..Rx : Ry..Bx : By.. ) :-

554

II. Prolog

orcteredt Vx, Rx, Bx) , !;


ordered( Wy, Ry, By).
lpatt( _. .W..R..B.._) :manhdist( W, B, 2) ,
roanhdist1 R, B, 3) .
Okorndle( _..W..R.._, _..W1..R1.._ )
dist( W, R, DJ,
dist[ wi, Rl, Dl),
D -< Dl.
roomgt2 ( Pos) : room( Pos, Room],
Roam > 2 .

% L-
:-

our_king_edge( _..X : Y.._) :( X - 1, !; X = 8, \; Y - 1, !; Y = 8) .


their_king_edge[_..W..R..X : Y.._) :[ X - 1, !; X = 8, ! ; Y - 1, ! ; Y = B ) .
kings_close( } :*
wk[ Pos, WK) , bk( Pos, BK) ,
dist[ WK, BK, D ) ,
D<4.
rooklost( _..W..B..B.._).
rooklost(b..N..R.,B._)
:ngb( , ) ,
not ngb( w, R ) .
dist ( X : Y, XI : 1 D) :absdiff(X, XI, Dx) ,
absdiff[ Y, Yl, Dy) ,
max( Dx, Dy, D) .
absdiff [ , , D) :A > B, !, D is A - B;
D is - A,
max[ , , ) :A > = , ! , M = A;
= .
manhdist( X : Y, XI : Yl, D> :absdifft X, XI, Dx),
absdifft Y, Yl, Dy] ,
D is Dx + Dy.
room! Pos, Room) :% ,
wr( POS, Rx : Ry),
bk( Pos, Bx : By),
( Bx < Rx, SideX is Rx - 1; Bx > Rx, SideX
( By < Ry, SideY is Ry - 1; By > Ry, SideY
Room is SideX SideY, !

%
%

%
i
*
%


is - Rx),
is 8 - Ry),

Room is 64.
%
cs( _..W..Rx : Ry..Bx : .._, : ) :t " "
[ Bx < Rx, !, is Rx - 1; is Rx + 1),
[ < Ry, !, is Ry - 1; is Ry + 1 ) .
%
show! Pos] :nl,
coord! Y ) , nl,
coord( X ) ,
writepiece( X : , ) ,
fail.
show( Pos) :side( Pos, S ) , depth( Pos, D) ,
nl, write( 'Side= ' ) , write( S ) ,
write! 'Depths ' ) , write( D ) , nl.
writepiece( Square, Pos) :wk( Pos, Square), !, write! ' ' ) ;

22.

555

wr ( Eos, Square), !, write( ' R ' ) ;


bk( Pos, Square) , !, write( ' B ' ) ;
write[ ' . ' )
showmove[ Move}
:
nl, write I Move), nl.

, ,
, . 22.4.
(. . 22.4), ( ,
, ). -
, "", "", " " .., 1, 2, 3 ..
( , w ), ( , R ). , " 7" :
, "" 7.
, . . 7
d5 7
5 7
WR

HR
WK
WK
WK
WR
WB

6
5


7
8
7
8

. ,
, ,
, , ,
" "? [13]
, , , 22.5, .
, ,
, ,
? ,
. , ( )
16 . , , , 50. ,
50 : , " ", 50 ; .

, "
", AL0 (
) .


AND/OR. AND/OR.

556

II. Prolog

, .
. .

-. -- ,
, -
.
--
: ,
, .
. ,
, .
Advice Language, .
:
--, Advice
Language "
'7.
:
, ;
;
, ;
, ;
--;
, ,
;
;
Advice Language;
, , , .


, --,
, , . [144].
, --,
,
, , . [74],
-- "neg-max" (
, ,
"neg-max" (neg) (max))
22,

557

. [118]. [69]
. [124] -. , : " ,
, ,
, ?" [118]
, .
, .
[11], [54] [95] , .
Advances in Computer Chess [1] ICCA.
,
Advice Language, (Michie)
[19], [14], [16], [17]. " ", , ,
[13].
, ( , ), [7], [123] [168], , , , , , -
, . , ,
,
. ,
,
Deep Blue ([65]}. ,
: . , ,
. , , ,
,
, . , , , , -
.

558

II. Prolog

23

, ...
23.1.
23.2.
23.3.
23.4.
23.5.
23.6.


Prolog

-
,
,
,

559
560
564
570
576
583

Prolog . ,
Prolog Prolog Prolog. ,
, .
,
: - , .

23.1.
Me ,
. . , . ,
Prolog Prolog, Prolog.
Prolog , . . , Prolog
: , .
, , .
. ,
,

. , . , , . Prolog ,
, , , .
;
(. 15 16). , , , , . ILP (. 19).
, ,
Prolog,
Prolog.
.
Prolog , ,
- , .

23.2. Prolog
23.2.1. Prolog
Prolog Prolog Prolog,
; ,
, . , Prolog; ,
.
, Prolog, .
prcve ( , ) :
prove( Coal)
, Prolog .
prove [ GoalJ :c a l l ( Goal] .
( c a l l ) Prolog, , Prolog. ,
,
- . ,
, " "
, ,
.
560

II. Prolog

, Prolog:
clause( Head, Body)
"" , . Head , , a Body . () Body
= t r u e . () . , Body . , :
3ody = ( FirstGoal, OtherGoalsJ
.
Prolog :
,( FirstGoal, OtherGoals}
OtherGoals ,
. c l a u s e { Head, Body)
Head . , ,
, member. member :
?- clause( member(X, L),
X =_14
L = \JLA | _ 1 5 ]
Body = t r u e ;
X = _14
L = [_15 I _ 1 6 ]
Body = member{ _14, _16)

Body).

23.1 Prolog, , , ,
. , "" ()
Prolog. ,
. , , .
Prolog. ,
Prolog
(. 23.2).
23.1, Prolog
% Prolog
prove( true).
prove( ( Goall, Goal2))
prove{ Goall),
prove( Goal2).

:-

prove( Goal) :clause( Goal, Body),


prove( Body).

23.

561


23.1. , 23.1, , , :
?- p r o v e [ p r o v e ! memberf X,

a,

b,

])))-

,
, c l a u s e .
, , ?
23.2, , 23.1,
Prolog. , prove [ Goal,
DepthLimit), , DepthLimit 0. .

23.2.2.
,
23.1, , .
prove {true} :- ! .
prove[ ( Goall, Goal2}) :- !,
provei Goall},
prove( Goal2}.
prove( Goal) :write! 'Call: ' ) , write ( Goal}, nl,
clause ( Goal, Body},
prove( Body),
w r i t e ( ' E x i t : ' ) , w r i t e ( Goal I , n l .

"" (true) ( G o a l l , Goai2j.


: ,
. , 23.2, . ,
, , . Prolog. .
?- trace ( ( member! -, I a, b]) , member! X, [ , ] ) ) ) .
C a l l : ir.ember( _00B5, [ a , b] )
Exit: member! a, [ a, b]i
Call: member( a, t b, c])
Call: member(a, [ c]}
Call: member[a, [ ] }
Fail: member [ a , [] )
Fail: member(a, [ c])
Fail: member[ a, [ b, c])
Redo: member( a, [ a, b])
Call: member! _00S5, [ b))
Exit: member[ b, [ b])
Exit: member( b, | a, b] ]
Call: member! b, [ b, ]]
Exit: member! b, [ b, c])

562

II. Prolog

23,2. Prolog Prolog


trace! Goal): Goal, Prolog,


trace( Goal) :trace! Goal, 0) .
trace( true, Depth)

:-

!. % ; Depth -

trace( ( Goall, Goal2), Depth)


trace! Goall, Depth;,
trace! Goal2, Depth).

:-

!,

trace] Goal, Depth; :display! 'Call: ', Goal, Depth),


clause! Goal, Body),

Depthl is Depth + 1,
trace( Body, Depthl),
display! 'Exit: ', Goal, Depth),
display_redo( Goal, Depth).

t r a c e ! C-oal, Depth) :%
display! ' F a i l : ', Goal, Depth),
fail.
display! Message, Goal, Depth) :tab( Depth), writet Message),
write( Goal), n l .
display_redo( Goal, Depth) :true
%
display! 'Redo: ', Goal, Depth),
% ,
% ,
fail.
%
%

.
1. , ( C a l l : Goal).
2. ( ).
3. ,
( E x i t : I n s t a r . t i a t e d G o a l ) ; , F a i l : Goal.
4. Redo: I n s t a n t i a t e d G o a l (
).
,
.

23.2.3.
, 23.1, . ,
. 15 16,

. Prolog,
.
23.

563

, 23.1. ,
, , .
1. t r u e t r u e .
2. [ G o a l l , Goal2)
( Proof I, Proof 2), .
3. Goal, , Body, Goal <= Proof, Proof Body.
,
23.1, :
:- ( 500, xfy, <==).
prove[ true, true).
prove{ ( Goall, Goal2), ( Proofl, Proof2)) :prove( Goall, Proofl),
prove ( Goal2, Proof2) .
prove( Goal, Goal <== Proofl :clause ( Goal, Body),

prove[ Body, Proof),


. 15 16 .

.

23.3.
,
.

(Explanation-Based
Generalization EBG), .
,
, , .
EBG
: , ,
.
,
, "" .

. , , ; , ,
. ,
( ), () . , "", .
EBG . Prolog .
,
.
564

II. Prolog

, , .
, Prolog.
, .

. ,
, ,
.
. , .
. ,
,

.

; ( ) , .
.
. ( ) " ", ..
. , EEG,
.
, .
,
, . . ,
" " . .
EBG. 23.3
EBG. ,
. . , :
% ( )
gives( John, John, chocolate)
23.3.
% Prolog
% :
:- dynamic gives/3, would_please/2, would_comfort/2, feels_sorry_for/2,
go/3, move/2, move_list/2.
$ ,

23.

565

gives( Personl, ?erson2, Gift) :likes( Personl, Person2),


would_please( Gift, Person21.
gives( Personl, Persan2, Gift; :feels_sorry_for( Personl, Person2),
would_coinfort [ Gift, Person2) .
wauld_please( Gift, Person)
neecis ( Person, Gift).

:1

would_comfort( Gift, Person)


likes[ Person, Gift).

:-

feels_sorry_for{ Personl, Person2)


likes ( Personl, Perso:i2) ,
sad{ Person2).
feels_sorry_for( Person, Person)
sad( Person;.

:-

:-

i
operational{ likes( _, _ ) ) .
operational( needs( _, _)) .
operati.cr.al ( sad [ _) ) .

likes( John, annie).
likes( annie, John).
likes! John, chocolate;,
needs( annie, tennis_racket).
sad{ John).
% , .
i

go( Level, GoalLevel, Moves)


Moves Level GoalLevel

go! Level, GoalLevel, Moves) :ncve_list( Moves, Distance!,


Distance -:- GoalLevel - Level.

move_list( [] , 0) .
move_list( [Movel | Moves], Distance + Distance 1)
move_list( Moves, Distance),
move( Movel, Distancel).

:-

move[ up, 1) .
move( down, -1) .
operational( A =:= B) .

:
gives ( John., John, chocolate) <==
( feels_sorry_for{ John, John) <== sad( John), % ,
% ,
would_comfort{ chocolate, John) <== likes( John, chocolate) %
$ ,

566

II. Prolog

John
c h o c o l a t e :
gives( Person, Person, Thing! <== % - ,
( feels_soi:ry_for( Person, Person) <-- sad( Person), I ,
% ,
would^comfort( Thing, Person! <== likes[ Person, Thing) %
% ,
)
23.3 sad () l i k e s () . gives
, ,
"", . :
gives( Person, Person, Thing) <-% - ,
( sad[ Person),
%
likes [ Person, Thing )
$
)
, Condition gives ( Person,
Person, Thing) :
Condition ( sad( Person), likes( Person, Thing))
,
.
asserta ( ( gives( Person, Person, Thing) :- Condition))
,
gives, :
gives( Person, Person, Thing) :sad( Person) ,
likes( Person, Thing).
gives { John, John, c h o c o l a t e )
( ) gives !
, :
gives; John, armie, tennis_racket). %

:
gives[ Personl, Person2, Thing) :4 ,
likest Personl, Person2),
% ? ,
needs( Person2, Thing].
i
, 23.3,
, EBG Prolog.
EBG :
,
" ".
, . .
, , 23.1, , . , EBG, ebg :
ebg( Goal, GenGoal, Condition)
Goal , , GenGoal . Condition
23.

567

GenGoal, . 23.4.
{. 23.3) ebg
:
?- ebg( gives! John, John, chocolate), gives! X, Y, Z) , Condition].
x=Y
Condition - ( sacl( X) , likes ( X, Z) )
23.4.
%

ebg{ Goal, GeneralizedGoal, SufficientCondition;


SufficientCondition,
, , GeneralizedGoal, Goal,
. GeneralizedGoal

%
%
'

ebg ( true, true, true)

:-

ebg( Goal, GenGoai, GenGoal)


operational( GenGoal},
call) Goal).

!.
:-

ebg( (Goall,Goal2), (Genl,Gen2), Cond} :- !,


ebg ( Goall, Gen]., Condi) ,
ebg( Goal2, Gen2, Cond2),
and{ Condi, Cond2, Cond).
% Cond = (Condi,CondZ)
ebg( Goal, GenGoal, Cond) :not operational( Goal),
clause( GenGoal, GenBody),
copy_term( [GenGoal,GenBody), (Goal,Body)), %
% [GenGoal,GenBody)
ebg( Body, GenBody, Cond).
% and( Condi, Cond, Cond)
I Cond Condi Cond2

(, )

and[ true, Cond, Cond)

:-

!.

% [true and Cond) <==> Cond

and[ Cond, true, Cond;

:-

!.

% (Cond and true) <==> Cond.

and( Condi, Cond2, [ Condi, Cond2i).


. , , , Moves,
, .
( 3, 6, Moves)
ebg :
?- Goal - go{ 3, 6, Moves),
GenGoal - go( Levell, LeveI2, GenMoves),
ebg( Goal, GenGoal, Condition),
asserta( ( GenGoal :- Condition)).
Goal = got 3, 6, [ up, up, up])
GenGoal - go( Levell , Level2, ( up, up, up])
Condition = (0+1 + 1 + 1=:= Level2 - Levell)
Moves = [ up, up, up]

568

II. Prolog

, , :
go( L e v e i l , Level2, [ up. Up, up] ) :0 + 1 + 1 + 1 =-:- Level2 - Level 1.

EBG
,
.
( 3, , Moves),
(up) (down).

(, ( 7, 10, Moves}) ,
- .
ebg, 23.4,
, 23.1.
copy_term.
copy_term{ Term, Copy)

Term . , , . ,
.
ebg .
clause! GenGoal, GenBody)
, . , .
:
copy_term( ( GenGoal, GenBody),

I Goal, Body))

, GenGoal Goal . GenGoai,


. , , ,
(),
(Goal). .
EBG.
, GenGoai,
Goal. .
,
. , KBG "" .
, "" .
.
: , , , -, , , ,
, ,
23.

569

. EBG , .

23.3. , ,
EBG, ,
, , ,
. ( "" ).
EBG , . , ,
, EBG, (
).

23.4. -

Prolog - .
- . - , . .
, . , . (. 15). , , ,
. . -

.
, Prolog, - , . ,
.
area ( ) , , ,
.
object[ Object, Methods)
Object - , (, , ),
Methods Prolog, . Prolog, .. Prolog ( , ). Prolog
,
, r e c t a n g l e [ Length,
Width). 4 3
570

II. Prolog

r e c t a n g l e t 4, 3 ) . , r e c t a n g le { Length, Width] , a r e a d e s c r i b e ,
:
object( rectangle! Length, Width),
[(area(A) :A is Length * Width),
(describe :write( 'Rectangle of size ' ) ,
write( Length * Width)> ] ) .

:
send( Object, Message)
4 3 , :
?- Reel = rectangle; 4, 3},
send! Reel, describe),
send! Reel, area( Area)).
Rectangle of size 4*3
Area = 12

send{ Object, Message) . Object. Prolog, Object. ,


Message. , , Head : - Body Head , . Message,
, Message, Prolog.
,
- " i s a " ()
. , " i s a " . , ,
. area;
. , square
, .
object! square( Side),
[ (describe :w r i t e ! 'Square with, s i d e ' ) ,
write( Side)) ] ) .
i s a ( square( S i d e ) , r e c t a n g l e [ Side,

Side)).

:
?- send! square( 5), area( Area)!.
- 25
area{ Area) :
area ( Area) square ( 5), . i s a
r e c t a n g l e ( 5, 5).
area, .
- , , , 23.5,
23,& - , .
23.

571

23.5. -
'i

4 send; Message, Object)


: :
i
Object ,
i
Message
send; Object, Message) :get_methods( Object, Methods),
process{ Message, Methods).
get_methods( Object, Methods!
object! Object, Methods!.

\ Object
%

:%

get_methods( Object, Methods) :isa( Object, SuperObject),


getjmethads{ SuperObject, Methods).

process! Message, [Message I _ ] ) .

process [ Message, [ (Message


call( Body),

:-

process( Message, [_ I Methods]}


process! Message, Methods).

Body) | _])

:-

:-

object( polygon( sides),


[ ( perimeter) P) :gum( Sides, P> ) ] ) .
object; reg_polygon[ Side, ) ,
[ ( perimeter! F) :.E is Side * N ) ,
( describe :- write( 'Regular polygon') ) ] ).
object( square! Side),
[ ( describe :write( 'Square with side ' ) ,
write [ Side) ) 3 )
object; rectangle! Length, Width),
[ ( area{ A) :A is Length * Width),
( describe :write{ 'Rectangle of size ' ) ,
write! Length * Width) ) ] ).
object! pentagon! Side),
[ ( describe

:- write( 'Pentagon') ) ] ).

isa( square{ Side) , rectangle ( Side, Side)) .


isa( square! Side! , reg_polygon [ .Side, 4) ) .
572

It. Prolog

isal rectangle! Length, W i d " h ) , p o l y g o n !

5LengthrWidthFLength,Width])).

j.sat pentagon [ S i d e ) , reg_polygon [ Side, 5 ) ) .


isa( reg_polygon( Side, M ) , polygon) L!)
raafcelist[ Side, N, L) .

:-

% makelist( item, , List;

i
List - , Item
makelistl _, 0,

[]) .

makelist( Item, N, [Item ! List])


N > 0, N1 is N - 1,
makeiist( item, H I , L i s t ) .

:-

& suml ListOfNumbers, Sum)

'i
Sum - ListofNumbers
sura ( [ ] , 0) ,

5( [Number | Numbers] , Sum)


sum[ Numbers, SumlJ,
Sum is Suml + Number,

:-

. , i s a
, , , , square (, 23.6). ,
.
, . , 23.5, , i s a . 23.5 ,
. ,
, " " .
- , ,
23.5, , . 23.1.
: , . , , . ,
, 1. , , ....
, , . ,
, .. , z. , , (
) . - , .
1, ,
, . - .
look ( , X, Y).
2. 1,
, - . , -
, xy_coord( X, Y) 1.
23.

573

. 23.1.

z
. , z 0, .
1, z_coord( Z1)
1 Z1 1, 2 . , 23.7, , ..
block ( BlockName), xy_coord z_coo!:d.
23.7. - ,
% : ,
object( camera,
[ look( a, 1 , 1 ) ,
look( d, 3, 2 ) ,
xy_coord( 2, 2 ) ,
z_coord( 20)] ) .

% -
% -
%

object) block) Block),


[ ( xy_coord( X, Y) :sendt camera, lookf Block, X, Y D ) ,
( xy_cootd( X, Y) :send( Block, under( Elockl)),
send[ Blockl, xy_coord( X, Y))) ,
( z_coord(0) :send [ Block, on! table)!),
( z_coord( Z)

send! Block, on( Blockl)),


send [

Blockl,

z_coord{

Zl]>,

is zi + i ) j ] .
obj ect( physical_obj ect( Name),
[ (coordt X, t, Z) :send! Name, xy_coord( X, Y[ ) ,
send( Name, z_coord( Z))}]).
object[a, | on|b|]].
object( b, [ under(a), on(c)]).
object( c, [ under(b), on(table)]).

574

II. Prolog

o b j e c t { d,
isa(
isa!
isat
isa!
isa(
isa(

[ on(table)]).

a, block{ a ) ] .
b, block! b)] .
c, block! c)) .
d, block( d)] .
block( Name), p h y s i c a l _ o b j e c t ( Name)).
camera, p h y s i c a l _ o b j e c t ( camera)).

, .
-
Prolog ( ) . ,
, - . ,
,
. ,
, . -
, . , () ,
.

23.4. , ,
23.5 23.6, , :
?- send( square( 6), perimeter( ]).
?
, 23.5, ?
23.5. 23.5 , ,
. - , ( , ) Prolog. , , , 23.6,
:
area! square( Side), Area) :Area is Side * Side,
area; rectangle! Length, Width), Area) :Area is Length * Width.
, send! Object, Message) ,
.

23,

575

23.5. ,
23.5.1. ,
, ,
.
, , . , , , , . , ,
.
, , .
, , . .
, , .
,
" ". ,
. , ,
, . .
1. .
2. , ,
.
, .
. . 23.2,

. 23.2. , . , ,
,

576

II. Prolog e

, . 23.2,
.
, . , .

, . , ,
.
, , .
,
.

. .
, , , . , .
. ,
, ,
.
, . , , :
, ,
, .
, , . , . 23.2, , . ,
,

, ,
. . (. . 23.2) ,
. , .
, , .
, , , .
1. . , .
2. . .
3. . , 2.
, 23.3.

23.

577

. 23.3. , .
, 1,3 4;
3

23.5.2. Prolog, ,

, , Prolog. Prolog ,
, .
, ,
Prolog .
, , , .
, Prolog.
, ,
.
" ( )" (
).
,
, . Prolog
.

578

II. Prolog

23.5.3. ,
, ,
,
, .
, : D
3. , , , .
D ,
.
, , ( )
:
> , - ,
- .
;
D ( ).
,
, .

1
. , X Y, X > Y.
. X X - Y.

2
. X.
. X .
, 1, 2, .
: 1 , 2.
, ,
.
, , , .
,
. . 23.4
, , , : 25, 10, 15, 30.
,
.

, ,
, .

23.

579

. 23.4. , .
. .
25. 10, 15 30; ,
, , . : 5, 5, 5, 5

23.5.4. ,

, :
ConditionPart

> ActionPart

C o n d i t i o n P a r t , a A c t i o n P a r t . :
[ Condi, Ccnd2, Cond3, ... ]

Condi, Cond2 .. Prolog. ,


. , :
[ Actionl, Action2, ... ]

Prolog.
. ,
.
, ,
. " s t o p " .
23.8 , ,
,
.
23.S. ,


::-

580

[ .)

( 800, xfx,
>> .
op( 300, fx, num) .

II Prolog

t num X, rum Y, X > Y 1


>
[ is X - Y, replace! num X, num NewX) ].
[ num X]

>

[ write( X ) , Stop ].

num
num
num
num

2 5.
10.
15.
30.

, ,

Prolog.
;
asserts! object)
retract! Object}

:
replace! Objectl, Object2)
retract! Qbjectl) , !,
assertz(Object2).

:-

,
r e t r a c t ( )
.
, , , 23.9. , . ,
,
, ( ). .

Prolog, , .
:
?- run.
23.9. ,
%
%
%

, .

assert/retract

:- op( 800, xfx,

>) .

% ran: , Condition
%
, 'stop'
run : Condition
> Action,
testl Condition),
execute! Action).

%
i ?

% test ( [ Condition!, condition2,


%

test( []).

23.

> Action,

...])

581

test( [First I Rest])


call( First),
test( Rest).

:-

% execute( [ Actionl, Action2, . . , ] ) :


execute! [ stop])
execute!
run.

>

:-

!.

:-

* ( )
%

execute; [First t Rest])


call{ First),
execute ( Rest) .

:-

replace , )
:retract! ) , !,
assert( ) .

%
%

23.5.5.
, , , . .
.
, .
. ,
.
,
, .
.
, . ,
, . ,
, . .
, ,
, - ,
s s s e r t z r e t r a c t . .
, ,
a s s e r t s r e t r a c t Prolog.
Prolog, run.
.
:
run( State)
Condition

582

:> Action,

[!. Prolog

t e s t ! Condition, State),
execute[ Action, State).
execute run
.

, , Prolog
(.. a s s e r t z r e t r a c t ) , , .
. ,
.

23.6.
,
,
, . ;
.
,
( , ). ,
Prolog
. : ;
, . ,
, ,
. ,
v ~

" " , . .
~. , " n o t " ().
S. , "and" ().
v. , "" ().
=>. , " i m p l i e s " ( ).
, " n o t "
, "and", " o r " " i m p l i e s " .
: ,
, , , . , , ,
(.. ). : , , 23,

583

, , , ( ).
, , .
. ,
, :
( => b) S ( => ) => ( => )
: " b ,
".
, , , .
, , , :
[pi v p j v . . . )

&

Iqi v qj v , . , )

( r i v v . . .)

...

, q
. ( , ), . (pi v p; v . . .)
. ( ). ,
, ,
. :
( => )

[ =>)

->

[ => )

:
- ( ( a =>b)

(=>)

->

( =>

, .
1. => - v .
2. --{ v ) ~ & -.
3. -{ & ) - v -,
4. ~(~) .
1 , :
- ( - [

[ =>

& ( => )

) v ( => )

2 4 :
{ -> ) ( ] 5 ~( -> !
1, :
(~ v b) t (-b v ) - ( - a v )
2, ,
:
(-a v ) & [~ v ) & & -
: [~ v b ) , (~b v ) , , --.
.
,
, ,
- . , :
v Y - v z
584

II. Prolog

, a Y Z . , , :
Y v 2

,
. , !Y v Z) ,
. , . "
" ( " n i l " ) , , . n i l ,
:
X

, , .
. 23.5 ,
.

. 23.5. (a => ) i ( => )


=> ( "> ) . .
,

23.10 , ; , . , .
, ,
.

".

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

1 ( ), ~ 2 ( )
: v
23.10,

% Prolog
:- dynamic clause/1, done/3.
23.

585

%
%
[ clause! X ) , clause! -X) ]

>

[ write['Contradiction found') stop].


%
[ clause! ) , in( P, ) , in( -P, C] ]
[ retract! C) ].

>

%
[ clause! ] , delete! P, C, C D , in ( P, CD ]

>

[ replace( clause! C ) , clause( Cl)) ] .


% ,
[ clause! P ) , clause! ) , delete! -, , Cl), net done( P, , ) ]

>

[ assert! clause! C D ) , assert! done [ P, , ) ) ].


% ,
[
[
%
[

clause! -P) , clause! ) , delete! , , C D , not done; -P, , P) ]


assert! clause! C D ) , assert! done ( -P, C, P) ) ] .
,
clause(Cl), delete I P, Cl, Cft),
clause[C2) r delete! -P, C2, CB), not done{ C1,C2,P) ] >
[ assert! clause[CA v CB)), assert! done[ Cl, C2, P)) ].

>

% :
[]
> [ write Not contradiction 1 ), stop].
* delete! P, E, 1)
%

delete( X, X v Y, Y) .
delete( X, Y v X, Y ) .
deletef X, Y
delete! X,
delete! x, Y
delete! X,
% ini P, E]

v Z, Y v Zl)
Z, Zl) .
v z, Yl v z)
Y, YD .

::-

P -

in! X, X).
in! X, Y) :delete( X, Y, _ ) .

, , :
[ clause! Cl), delete! P, Cl, CA),
clause! C2), delete! - P, C2, CB) ]
[ assertz( clause( CA v CB)) ).

586

>

If. Prolog


, . , 23,10, ,
, :
d o n e ( C l , 2,


.
, 23.10, , . , , . ,
a v b v -
,
. . ,
:
a v v a
a v b.
, . , , 23.11.
translate( Formula}
Cl, C2 .. , .
clause I CD .
clause! C2).
23.11.
( )
*
s:::-

( )
(
(
(
(

100, fy, ~ ) .
110, xfy, &),
120, xfy, v ) .
130, xfy, ->) .

%
%

% t r a n s l a t e ) Formula): -
%

% c l a u s e l )
translate! F I G ; : I,
translate( F),
translate( G ) .
translate( Formula) :transforml Formula, NewFormula),
!,
translate( NewForraula).
translate! Formula) :assert( clause( Formula)).

h
%

% ,
Formula
%
%

% ;
23.

587

* trans form[ Formulal, FormulaZ)


%
Formula2 Formulal,
k

transform* -1-), X ) .

transform! X => Y, -X v Y ) .

transform! - (X s YJ, ~X v -Y}.

transform< ~ (X v Y ) , -X - Y ) .

transform! X S Y v Z, [X v ZJ s IY v Z))

transform! X v Y S Z, [X v Y] fi (X v Z)).

transform! X v y, Xl V Y)
transform! X, X I ) .

:-

transform! X v Y, X v Yl)
transform! Y, Yl) .

:-

t r a n s f o r m ! - X, - XI)
t r a n s f o r m ! x, XI),

:%

, , run. ,
, . , , :
?- t r a n s l a t e ! -( (a=>b) S (b=>c)=>U=>O) >, run.
" C o n t r a d i c t i o n found" ( ),
, .

. . Prolog Prolog
Prolog.
Prolog,
,
.
. ,
. .
- ,
. , . .
, , , , " ". , ,
Prolog,
, ,
.

588

II. Prolog

,
,
.
:
, ;
;
- :

, , ;
;
, , , ;
, ;
, ;
, ;
, .


Prolog Prolog. Prolog, ,
[87], [147] [154].
. , , [106]. EBG
[72].
, Prolog . - (Kedar-Cabelli) (McCarty) EBG , .
- [152]. [107] Prolog++,
- Prolog.

[165]. , , " ".
[45] " ". , , .
,
[58], [60], [126] [133].

23.

589


Prolog
Prolog, ,
Prolog, Prolog,
ISO Prolog. , Prolog . ,
ISO.
Prolog, ,
, Prolog . .


ISO Prolog
.
. , a s s e r t (/) r e t r a c t ,
c l a u s e ( Head, Body) . , :
:- dynamic Predicatet-Iame ( PtedicateArity).
:
:- dynamic member! 2).
, a s s e r t l a / z ) , .

assert retract
ISO a s s e r t a a s s e r t z ,
a s s e r t . a s s e r t . a s s e r t , a s s e r t z .
r e t r a c t a l l ( Clause) a b o l i s h ( PredicateName/Arity)
, .


Prolog ,
, , Prolog
. Prolog -

( ) unknown ( _,
i : ; .

- n o t " \ + "

not Goal. Prolog ( )
(, ) :
\+Goal

, , , .
Prolog "not"
" \ + " ( )
not , ( ).

name( Atom, CodeList)


,
( atom__codes/2). Prolog , name ,
, .

consult reconsult
c o n s u l t r e c o n s u l t . , , ( ,
, ,
,
,
).

Prolog
, ,
.
, ,
.

. Prolog

591

. : .-."':.- . ~ : ".. "(= , .


Prolog . ,
( ) , , . .1 ,
Prolog. , ,
.-", ;: IVolog.
\+Goal.
Prolog , , Prolog
.
.1.
% f r e q u e n t . p l :
%
%
,
4
' \ + ' ,
\
\ + l i k e s ( , s n a k e s )
%

*
Prolog
: - Op( 900, fy, n o t ) .
not Goal :Goal, ! , f a i l
true.
once[ Goal}: , Goal
%
( )
%

%

once( Goal) :Goal, ! .
i member( X, List): , X
i
List
member ( , [ X I _] )
'^ X -
member(X, [ _ I Rest]) :member( X, Rest).
% X
% cone ( L2, L3): L3 L1 L2
( [] ,L,L) .

cone! [X I LI}, L2, [X I L3]) :concl LI, L2, L3) .


% del ( X, LD, L) : L LO X
%
. X
%
, X

L0
del( X, [X I Rest], Rest).
del! , [Y [ RestO], [Y \ Rest]) :dell X, RestO, Rest).

% X,
%
%

% subset{ Set, Subset): , ,


% Set Subset (
i )
%
. Subset Set ,
%
Subset
subset! [],[)>
subset( [First I Rest], [First | Sub]) :- % First
%
subset( Rest, Sub),
subset( [First | Rest], Sub) :subset! Best, Sub).

i First

i set_difference[ Setl, Set2, Set3); Set3


I
, Setl Set2
%
: Setl Set2
%
, a Set3 -
set_dif ference ( []._,[]).
set_difference! [X | SI], S2, S3) :member! X, S2) , !,
set_difference! SI, S2, S3).

% 32

set_difference( [X | SI], S2, [X | S3]) :- %


S2
set_differenceI SI, S2, S3).
t length! List, Length): , Length
%
List
%
. length/2
%
Prolog
i

I

i

length; L, ) :length( L, , ) .
length! [] , N, N) .
length! [_ | L], N0, ) :N1 is HG + 1,
length [ L, N1, N ) .
% max[ X, Y, ): - X Y
(, Y, ) :X>=Y, !, = X

593

Max = Y.
% min( X, Y, Min): Min - Y
rcinix, Y, Min) :X =< 4, !, Min - X
Min = Y.
% copy_term( Tl, T2); T2 Tl, ,
%

%

%

%

*
copy_term , 2 1
copy_term( Term, Copy) :asserta( term_to_copy < Term!),
retract( tetm_to_copy( Copy!), ! .

594

1
1.1,

X = pat

X = bob

)
1.2.

1.3

X = bob, Y = pat
?- parent! x, pat) .

?- parent ( liz, X) .

7- parent! Y, pat), pareyit ( X, Y)

a)
6}

happyix) :parent) X, Y ) .
hastwachildren [ X) :parent[ X, Y ) ,
sister( 2, Y ) .

1.4.

grandchild^ X, Z)
parent[ Y, X) ,
parent[2, Y ) .

1.5.

auntf X, Y) :parent[ Z, Y ) ,
sister ( X , Z) .

1.6.
1.7.

:-

, - .
)

2
2.1.

2.3.

)
)
)

.
= 1, = 2

D = 2, = 2

PI = point (-1, 0}
2 = point(1,0)
= point(,Y)
,
1 -1 , - .
seg( point (5, Y1) , point (5, Y2) J

2.4.

regular! rectangle! point(XI,Yl), point (X2, Yl>,


point [X2F Y3) , point (XI, Y3) J ) ,
% ,
% .

2.5.

2.6.

= two

)
)
2.7.

= .
D = s(s(l>);
D - s(s[s(s(sfl> > ) ) I
relatives; X, Y) :predecessor{ X, Y)
predecessor{ Y, X)
predecessor; Z, x) ,
predecessor( Z, Y}
predecessor! x, Z) ,
predecessor; Y, Z ) .

2.8.

translate! 1, one),
translate 1 2, two).
translate( 3, three!.

2.9.

, 2,1, Prolog .

2.10.

, 2.2,
. X
, X.

3
3.1.

a)

conc( L 1 , [_,_,_] , Li

) ( {_,_,_ I L2 ],[_,_,_] ,L)


3.2.

3.3.

596

a)

last! Item, List) :cone( _, [Item], List).

6)

last ( Item, [Item] ) ,


lastl Item, [First | Rest]) :last( Item, Rest).
evenlength ( [ ] ) .
Evenlength( [First I Rest]) :oddlength( Rest).

oddlength( [First 1 Rest]) :evenlength! Rest}.


3.4.

reverse ( [],()>.
reverse( [First | Rest], Reversed) :reverse! Rest, ReversedRest),
concl ReversedRest, [First], Reversed).

3.5.

% reverse
palindrome[ List) :reverse( List, List).
% ,
% reverse
palindrome 1( []).
palindrcmel( [_]).
palindrome 1( List) :conc( [First | Middle], [Firstl, List),
palindromel[ Middle!.

3.6.

shift; [First I Rest], Shifted! :concl Rest, [First], Shifted).

3.7.

translate ( [),!!).
translate! !Head | Tail], [Headl | Taill]) :Leans[ Head, Headl),
translate! Tail, Taill).

3.8.

% ,
% Subset , Set
subset! [],[])
% First Subset
subset! [First | Rest], [First | Sub]) :subset( Rest, Sub).
subset( [First | Rest;, Sub) :- % First
subset < Rest, Sub! .

3.9.

dividelist( j] , [] , [)) . %
%
dividelist! [X], [X], []). %
%
dividelist! [X, Y I List], [X | Listl], [Y | List2]) :dividelist[ List, Listl, List2).

3.10.

c=nget [ state (_,_,_,has) i !]). %


%
canget! State, [Action [ Actions]) :move( State, Action, NewState), %
canget{ NewState, Actions). %

3.11.

flatten! [Head | Tail], FlatList) :- %


%
flatten! Head, FlatHead),
flatten! Tail, Flatrail),
cone! FlatHead, FlatTail, FlatList).
flatten! [],[]).
flatten( X, [ X j ) .
4 .
%

3.12.

Terml = plays ( jimmy, and! football, squash) !


Term2 = plays! susan, and! tennis,
and[ basketball, volleyball)))

3.13.

3."4

:- op! 300, xfx, was) .


:- op{ 200, xfx, of),
:- opt 100, fx, the!.
a]

A - 1 + 0

597

6)

= 1 + I +

a)

c = l + i + l + l + O

r)

D - l + 1 + 0 + 1;
0 = 1 + 0 + 1 + 1 ;
0 = 3 + 1 + 1 + 1%
%

3.15.

:- ( 100, ,, in) .
:- ( 300, iy., concatenating).
:- opt 200, y.f-, gives) .
:- opt 100, xtx, and).
:- opt 300, fx, deleting).
:- opt 100, xfx, from).
%
Item in [Item | List].
Item in [First | Rest] :Item in Rest.
%
concatenating (] and List gives List.
Concatenating [X | LI] and L2 gives (X | L3] :concatenating LI and L2 gives L3.
%
deleting Item from (Item | Rest] gives Rest,
deleting Item from [First | Rest] gives [First | HewRest] :deleting Item from Rest gives Hewfiest.

3.16.

maxt X, Y, X! :X >= V.
max [ X, Y, Y] :X < Y.

3.17.

maxlist-; [ X ] , X) , *
%
maxlisti [X, Y | Rest], Max) :- %
%
maxlist) [Y | Rest], MaxRest),
max( X, MaxRest, Max). % Max -
% MaxRest

3.18.

sumlist { [] , 0] .
sumlist{ [First | R e s t ] , Sum) :suralistl Rest, S u m R e s t ) ,
S u m is First + SumRest.

3.19.

ordered{ [ X ] ) . 4
%
ordered; [X, Y | Rest]) :ordered! [Y I R e s t ] ) .

3.20.

subsuml [ ] , 0, [] ) .
subsuml [H | L i s t ] , Sum, [N | Sub]) :- %
% *
Suntl is Sum - H,
subsuml List, Suml, S u b ) .
subsum( [N | L i s t ] , Sum, Sub) :- % N
%
subsurrH List, Sum, Sub) .

3.21.

between! HI, 2, N1] :N1 =< N2.


between! HI, 2, X) :HI < N2,
MewHl is Ml + 1,
between{ KewNl, H2, X ) .

598

3.22.

:- opt 900, fx, If) .


;- op( 800, xfx, then).
:- op( 700, xfx, else).
: - op [ 600, xfx, :-) .
if Vail > Val2 then Var := Val3 else Anything :Vail > Val2,
Var - Val3.
if Vail > Val2 then Anything else Var :- Val4 :Vail - <Val2,
Vat = Val4.

4
4.1.

a)

?- f a m i l y ( p e r s o n (_, N a m e , _ , _ ) , _, [] ) .

?- child! person( Name, SecondName, _, works(__))).

?- family { person {_, Name, _, unemployed),


person(_,_,_,works [_,_) ) , _) .

r)

?- family ( Husband, Wife, Children),


dateafbirthi Husband, date(_,_, Yearl)),
dateofbirthf Wife, date(_,_,Year2)),
(Yearl - Year2 >- IS
Yeai:2 - Yearl >= 15
),
member( Child, Children).

4.2.

twinst Childl, Child2) :family! _, _r Children),


del ! Childl, Children, OtherChildren), %
* Childl
member( Child2, OtherChildren) ,
dateofbirthl Childl, Date),
dateofbirth( Chiid2, Date).

4.3.

nth_member( 1, !X I LI, X ) . i X -
% !X | L]
nth_raember( K, [Y | L ] , X) :- % X - n-
i is [V | L)
N1 is N - 1,
ntb_membe[ N1, L r X ) .

44

, , , .

4.5.

4.6.
4.7.

accepts (State, [ ], _) :final{ State) .


accepts; State, [X I Rest], MaxMoves) :MaxMoves > 0,
trans( State, X, State 1 ] ,
NewMax is MaxMoves - 1,
accepts] Statel, Rest, NewMax).
Accepts; State, String, MaxMoves} :MaxMoves > 0,
silent! State, Statel),
HewMax is KaxMoves - 1,
accepts[ Statel, String, NewMan).

a)

member

Y,

[1,2,3,4,5,5,7,8]).

jump! X/Y, XI/YD :- *


% X/Y X1/Y1
(
dxy( Dx, Dy) %

599


cbty( Dy, Dx) %
),
XI is X + Dx,
inboard! X I ) , % XI
Yl is Y + Dy,
ir.boarcM Yl) . i Yl
dxy( 2, 1 ) . 2 , 1
dxy( 2, - 1 ) , % 2 , 1
dxy( - 2 , 1 ) , % 2 , 1
dxy( - 2 , - 1 ) . % 2 , 1
inboard( Coord) :- %
%
< Coord,
Coord < 9.
6)

knightpath[ [Square]). % Square


knightpath( [S1,S2 ] Rest)) :jump( SI, S 2 ) ,
knigbtpath; [S2 I Rest]).

B)

?- knightpathf [2/1,R, 5/4,S,X/B] ) .

5
5.1,

a)

x = i,X = 2;

6)

X
Y
X
Y
X
4
X
Y

B)

X = 1
Y = 1;
X = 1
Y

5.2.

=
=
.=
=
=
=
=
=

1
1;
1
2;
2
1;
2
2;

, c l a s s .
class [ Number, positive) :Number > 0, !.
class( 0, zero) :- !.
class [ number, negative).

53.

split! [], [],[]>


split! [X I L ] , [X | LI], L2) :X > = 0, I ,
split( L, LI, L 2 ) .
split) [X I L ) , LI, [X i L2J) ;split! L, LI, L2] .

5.4.

member ( Item, Candidates] , not member! Item, RuledOut)

5.5,

set_difference! [], _, []).


set_difference{ [X | LI], L2, L) :member! X, 12) , !,
set_difference( LI, L2, L ) ,
set_difference{ [X | LI], L2, [X | L]) :set_difference( LI, L2, L ) . .

600

5.6.

unifiable( [], _, []} .


unifiablet [First I Rest], Term, List) :not! First = Term), !,
unitiable( Rest, , List).
unifiablel [First I Rest!, Term, [First I List]) :unifiable( Rest, Term, List).


6.1.

findterm( Term)

:- h ,
% f
read( Term), !, % f
% Term?
write( Term) %
%
findterm[ Term), % &
%

6.2,

findallterras ( Term) :reacU CurrentTerm), % , CurrentTerm


%
process( CurrentTerm, Term).
process! end_of_file, _) :- !.
process! CurrentTerm, Term) :( not ( CurrentTerm = Term), ! |
*
mrite( CurrentTerm], nl %
%
),
findallterms( Terra). %
%

5.4.

starts! Atom, Character) :name (. Character, [Code] ) ,


name( Atom, [Code I _ ] ) .

G.5.

plural ( Noun, Nouns) : name[ Noun, CodeList!,


namef s, CodeS),
cone( CodeList, CodeS, HewCodeList),
name( Nouns, NeuCodeList) ,

7
12.

add_to_tail( Item, List) :var 1 List), !, % List


%
List = [Item | Tail] .
add_to_tail( Item, [_ ] Tail]) :add_to_tail( Item, "Tail).
member{ X, List) :var{ List), \, % List
% ,
fail. % X
member; X, [X | Tail]).
member( X, [_ | Tail]) :member( X, Tail) .

7.5.

% subsumest Terml, Term2) :


%
Terml , Term2,
* subsumes! t[X,a,f;Y)}, t(A,a, f(g(B))))
% , Terml Term2

601

%
%
%
% literally( SubsumedTerm)
subsumes! Atoml, Atom2) :atomic( Atoml), ! r
Atoml -- Atom2 .
subsumes( Var, Term) :var( Var), !, %
Var = literally( Term!, t
% Var
subsumes! literally( Terml), Term2) :- !, %
% Term2
Terml == Term2.
subsumes) Terml, Term2) :- i Terml -
nonvar{ Term2),
Terml =.. (Fun | Argsl],
Term2 =.. (Fun | Args2],
subsumes_list( Argsl, Args2).
subs'Jir.es_list ( [] , [ j ) .
subsumes_list[ {Firstl | Restl], [First2 | Rest2]) :subsumes! Firstl, First2),
subsumes list( Restl, Rest2).
7.6.

a)
6)

7.7.

7.5.

1- retract! product! X, Y, Z) ) , fail.


7- retract! product! X, Y, 0} ) , fail.
copy_term( Terra, Copy) : assexta1 term_to_copyt Term)),
retract! term_to_copy( Copy)).
copy_term( Term, Copy) :bagof(

X,

X = Term,

[Copy]).

8
8.2.

a d d _ a t _ e n d < LI - [ I t e m I Z 2 J , I t e m , LI - Z 2 ) .

8.3.

reverse! A - Z, L - L) :- I
,
A == Z, !. % - Z
reverse! [X | L] - Z, RL - RZ} :- i
reverse! L - Z, RL - [X | RZ]).

8.6,

%
sol(Ylist) :functor! Du, u, 15), I
functor! Dv, v, 15), %
sclt Ylist,
[1,2,3,4,5,6,7,8], % X
[1,2,3,4,5,6,7,81, % *? Y
Du, Dv) .
sol! [],[], [),_,_) .
sol[ [Y | Ys], [X | XL], YLO, Du r Dv) :del! Y, YLO, Y D , % Y
U is X+Y-l,
arg ( U, Du, X ) , %
V is X-Y+,
arg! v, Dv, XI, %
SOl[ Ys, XL, YL, Du, Dv) .
del! X, | L ] , L) .
del! X, [Y I L0], [Y | L]) :del ! X, L0, L} .

602

9
9.4.

9.5.

% mergescrt( List, SortedLIst) :


%
-
mergesort f [] , [} ) .
mergesort( [X] , [X]).
mergesort! List, SortedList) :divide! List, Listl, List2), %
%
mergesort( Listl, Sortedl),
mergesort ( List2, Sorted2),
merge( Eortedl, Sorted2, SortedList). %
%
divide! [],[],[])
divide([Xj, [X], []).
divide( [X, Y | L ] , [X | LI], [Y | L2]) :- % X, Y
%
divide( L, LI, L2).
% merge! Listl, List2, List3): , 3.3.1
a]

binarytree ( nil) ,
binarytree{ t[ Left, Root, Right)) :binarytree( Left),
binarytree( Right).

9.6.

height! nil, 0) .
height! t( Left, Boot, Right), H) :height[ Left, LH),
height t Right, RH),
max( LH, RH, MH),
H is 1 + MH.
max( A, B, A) :A>=B, ! .
max { , , ) .

9.7.

linearize! nil, []) .


linearize! t( Left, Root, Right), Lis") :linearize! Left, Listl),
linearize! Right, List2),
cone! Listl, (Root | List2], List).

9.8.

9.9.

9.10.

maxelement) t!_, Root, nil) , Root) :- !. % Root - ,


%
maxelement( t!_,_,Right), Max) :- %
%
manelement( Right, Max).
in( Item, t(_. Item, ), [Item]),
in{ Item, t( Left, Root, _ ) , [Root | Path]) :gt t Root, Item),
in! Item, Left, Path).
in! Item, t[ _, Root, Right), [Root I Path]) :gt( Item, Root),
n{ Item, Right, Path) .
% ,
, -
3 ,
% *.
show( Tree) :dolevels! Tree, 0, more). % ,
i
dolevels! Tree, Level, alldone) :- !. % Level
%
dolevels! Tree, Level, mere) :- % ,
% Level

603

traverse( Tree, Level, 0, Continue), nl, %


%
% , Level
NextLevel is Level + 1,
dolevels( Tree, HextLevel, Continue).
%
traverse[ nil, _, _, _ ) .
traverse[ t( Left, X, Right), Level, Xdepth, Continue) :NextDepth is xdepth + 1,
traverse( Left, Level, NextDepth, Continue), %

{ Level = Xdepth, !, % Level?
write( X ) , Continue = more %
% ,
write(' ') %
),
traverse( Right, Level, NextDepth, Continue). %
%

10
10.1.

in( Item, 1( Item)).


in( Item, 2( Tl, M,
gt( , Item], !, i
%
in( Item, Tl)
%

%
T2)) - %
Item

in[ Item, 2). %


%
in( Item, n3[ Tl, M2, 12, , )! :- *
%
gt( MZ, Item), !, % Item
%
in( Item, Tl) %
gt{ , Item), !, %

in{ Item, T2) %
in{ Item, ).
10.3,

10.4.

604

avl(Tree) :avl( Tree, Height). Tree - AVL-


i Height
avl( nil, 0 ) . % - AVL-,
%
avl( t( Left, Root, Right), H) :avl ( Left, -4L) ,
avl( Right, HR) ,
( HL is HR; HL is HR + 1; HL is HR - 1 ) , %
%
maxl( HL, HR, ! .
maxl( , V, > :- % - V,
% 1
U > V, !, is D + 1
;
is V + 1.

5,

5.

11
11.1.

113

depthf

irstl ( [Node | PathJ, [Node | Path]) :goal( Node) .


depthfirstK [Node I Path], Solution) :s[ Node, Nodel),
not member] Nodel, Path),
depthfitstl1 [Nodel, Node | Path], Solution).
* ,
% ,
%
iterative_deepening( Start, Solution) :id_path[ Start, Mode, [], Solution),
goal[ Node).
i path[ First, Last, Path): Path
% First Last
path[ First, First, [First]).
path( First, Last, [First, Second I Rest]) :s( First, Second),
path[ Second, Last, [Second I Rest]).
%
% idj>ath( Fir3t, Last, Template, Path): Path
% First Last,
h
i Template.
%
id_path( First, Last, Template, Path) :Path - Template,
path! First, Last, Path)
copy_term( Template, P ) ,
path( First, _, P ) , !, %
% , Template
idjpathf First, Last, [_ | Template], Path). %
4 , Template

11.6.

15 ; 26 .
N ( b , 0) = 1
N( b , d) = K{ b ,

d -

1)

("+1 -

1)/( -

1)

d >

11.8.

solve! StartSet, Solution)

:- StartSet -
%
bagof( (Node), member[ Mode, StartSet), CandidatePaths),
breadthfirst[ CandidatePaths, Solution),

11.9.

, , . ,
.
% , origs( Model, Node2) -
i
$ s :
s{ Nodel, Node2) :origs( Node2, Nodel) .

11.10.

%
% StartNode-EndNode
% ,
s( Start - End, NewStart - NewEnd) :origs{ Start, MewStart), %
origs( NewEnd, End). *
% goal( Start - End)
goal! Start - Start) . h
goal( Start - End) :origs( Start, End). %

605

It. 11.

f i n d l ; f i n d 2 ( c o n e ( P a t h , _ , _ )
, f i n d l
); f i n d 3

11.12.

12
12.2.

; h < h* ,
.

12.3.

h(n] =

roax(hi(n),h 2 (n)

,h3(n) }

12.7,

\ " "
% IDA*
s( DepthrState, NewDepth:NewState> :s( State, NewState, _ ) ,
NewDepth is Depth + 1.
f( Depth:[Empty i Tiles], F) :goal( [EmptyO | TilesO]),
totdist I Tiles, TilesO, Dist),
F is Depth + Dist. %
*
\ IDA*
% 0:State, State -
h [, 12.2)
showpos( Depth:State) :showpos( State}.
[ 1 / 2 , 3 / 3 , 3 / 1 , 1 / 3 , 3 / 2 , 1 / 1 , 2 / 3 , 2 / 1 , 2 / 2 ) ,
* ( , ) , IDA* ( )
, 24.

12 9.

; , , , f, g, j, k, d, e, f, g, j, 1, k, m, j, 1.
F{b| F(c): F ( b ] = 2 T F < C ) = l , F ( c ) = 3 , F < b ) = 5.

13
13.4.

, 8, 10 11
: < h < c ) < 9, 0 h ( f ) < 5.

14
14.1,
14.6.

, ,
, R5, , 51. , , .

15
15 2
15.4.

606

, (|| = 1 [|)= 1. (0,5), () = () = 0,5 (|) = 0.


= 6, = 5, = 25.

17
17.6.

. , .

18
18.1,

: I = 2.225
IreE(size) 1,5422, Gain(size) = 0.7503
Info(size) - 0.979S, GainRatio(size) = 0.7503/0.9799
- 0.7657
Ires(holes) = 0.9675, Gain[holes> = 1.324
Info(holes) = 1.5546, GainRatio(holes) = 0.8517

18.2.

Gain = I - ires
I - - (p(D) log p{D) + p(~D) log p(~D)>
= - ( 0.25 log C.25 + 0.75 log 0.75) - 0.6113
Ires p(S), p(~S), p(DIS), p[D(-S)
p(S) = p(S[D> p<D) + p(SI~D) p(~D) - 0.75 * 0.25
+ 1/6 * 0.75 - 0.3125
:
p(D|S) - ptD) p(S|D) / p[S> - 0.25 * 0.75 / 0.3125 = 0.6
p(~D|S! = 0 . 4
plD|-S> = p(D)*p(~5|D) / p(~S) = 0.25 * 0.25 / (1-0.3125)
= 0.09090
Ires = p(S)*I(D|S> + p(~S}*I(D|~S) = 0.6056
I(D|S)
,
.
Gain = 0.2057
GainRatio - GainiS) / I(S) - 0.2057/0.860 == 0.2296

18.5.

% prunetree( Tree, PrunedTcee) : PrunedTree


i Tree,
%
%
% , :
% Tree = leaf( Mode, ClassFrequencyListi
% Tree = tree( Root, LeftSubtree, RightSubtree)
prunetree( Tree, PrunedTree) :prune( Tree, PrunedTree, Ecror, FrequencyList).
I prune( Tree PrunedTree, Error, FrequencyList) :
%
PeunedTree Tree,
I

%

%
, a FrequencyList -
%
Tree
prune ( leaf( Node, FreqList),
leafi Node, FreqList) , Error, FreqL.ist) :static_error( FreqList, Error) .
prune( tree! Root, Left, Right},
PrunedT, Error, FreqList) :prune( Left, Leftl, LeftEcror, LeftFreq),
prune! Right, Rightl, RightError, RightFreq),
sijmlists! LeftFreq, RightFreq, FreqList) , %
%
static_error( FreqList, StaticErr),
sum{ LeftFreq, N1) ,
suml RightFreq, K2),
BackedErr is ( N1 * LeftError + H2 * RightError)
/ ( Ml + H2) ,
decide! StaticErr, EackedErr, Root, FreqList, Leftl,
Rightl, Error, PrunedT).
% , ,

607

\ :
decide! StatErr, BackErr, Root, FreqL, _, _, StatErr,
leaf( Root, FreqLM :StatErr < BackErr,!. % :
%
% :
decide) _, BackErr, Root, _, Left, Right, BackErr,
treet Root, Left, Right)).
% static_error[ ClassFrequencyList, Error):
i
,
static_error( FreqList, Error) :max[ FreqList, Max), I
I FreqList
sum ( FreqList, All), $ FreqList
number_of_classes( NumClasses),
Error is ( All - Max + NumClasses - 1)
/ { All + NuraClassesl .
sum{[] ,0) .
suitH [Number | Numbers], Sum) :sural Numbers, Suml),
Sum is Suml + Number.
max< [X], X ) .
max( [X,Y | List], Max) :X > YF !, max( [X | List] , Max)
maxt [Y | List!, Max).
s u m l i s t s m , [],[)]sumlistst (XI | LI]. 1X2 ! L2], [X3 I L3]) :X3 is XI + X2,
suralistst LI, L2, L3).
%
treel( tree( a, i
tree( br leaf( e, [3,2]), leaf( f, [1,0])), %
%
treet , tree' d, leaf! g, [1,1}), leaf! h, 10,1])},
leaf! i, [1,0])))).
muaber_of_classea ( 2) .
% :
%
?- treel( Tree), prunetree) Tree, PrunedTree).

19
19.2.

19.3.

373179, -66518.

19.4.

{, Ci) , ( 0 , ;). Ci
- .

20
20.1.

qmult ( , , posi.
qrault( pos, zero, zero) .
qmult( pos, neg, neg).

20.2.

resistor! pos, p o s ) .
r e s i s t o r ! zero, z e r o ) ,
resistor( neg, n e g ) .
diode( zero, pos) .
diode( zero, z e r o ) .
diode( neg, z e r o ) .

20.3.
608

: X - z e r o / i n c , = /inc

: X
: X
X
x
)

=
=

zero..inf/inc,
zero..inf/std,
zero..inf/std,
zero..inf/inc,

Y
Y
Y
Y

=
=

zero..inf/inc
zero..inf/inc
zero..inf/std
zero..inf/std

) ,
, :
X = zero..inf/std, Y = zero..inf/std

20.4.

TtO: X ~ zero/inc, Y = zero/inc, Z zero/inc


T=tl: X = zero..inf/inc, Y = zero..inf/inc,
2 = zero..landz/inc
T=t2: X zero.-inf/inc, V = zero..inf/inc,
Z = landz/inc
X = zero..inf/std, Y = zero..inf/std,
S = zero..landz/std
X zero. . inf/stci, Y = zero.. inf/std,
Z = landz/std
.
,
20.2, ,
:
landmarks [ , [niinf, zero, inf ]).
landmarks! , [minf,zero,inf]).
landmarks[ z, [minf,zero,landz,inf]).
correspond! x:zero, y:zero).
iegalstatel fX,Y,Z]) :mplus(x, Y},
sum( X, Y, Z) .
initial[ [x:zero/inc, y:Y0, z :Z0]).
.
?- initial(S), simulate (S, Beh., 3) .

20.5.

I
* levAO levBO -
i , fABO - , fBAO
%
landmarks! level, [ zero, levBO, levAO, inf]).
landmarks[ leveldiff, [minf, zero, inf]).
landmarks( flow, [minf, 80, zero, fABO, inf]) .
correspond( leveldiff:zero, flow:zero).
correspond( flow:fABO, flow:fBAO, flow:zero).
legalstate( [ LevA, LevB, FlOwAB, FlowBAJ) :derivf LevA, FlowBA),
deriv( LevB, FlowAB),
stm( FlowAB, FlowEA, flow:zero/std) , % = -FlowAB
DiffAB = leveldiff:_,
sum{ LevB, DiffAB, LevA], % DiffAB = LevA - LevB
mplus( DiffAB, FlowABt.
initial( [ level:levA0/dec, level:levB0/inc,
flow:fABO/dec, flow:fBAO/inc]>.

20.7,

legal_trans[ Statel, State2) :system_trans( Statel, State2),


Statel \== State2, %
not (point_state{ Statel), % He ,
%
point_state( Stated)), ? Statel,
% St.5ito2
legalstate{ State2). %
point_state[ State) :member! _:Qmag/Dir, State),
not (Qmag = _ - _ ) , % Qmag - ,
Dir \== std. % Dir

609

21
21.1.
21,3,

( ( | List] , Rest) :5[ List, tb I Rest]).


.

23
23.1. :
prove! clause! Head, Body)) :clause ( Head, Body).
23.3.

610

, s q u a r e () p e r i m e t e r
. , s e n d


1.

2.
3.
4.
5.
6.
7.
8.

9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

Advances in Computer Chess Series: Clarke M.R.B. (ed.). Vols 1-2. Edinburgh
University Press; Clarke M.R.B. (ed.). Vol. 3. Pergamon Press; Beal D.F. (ed.).
Vol. 4. Pergamon Press; Beal D.F. (ed.). Vol. 5. North-Hoi land; Beal D.F. (ed.).
Vol. 6. Ellis Horwood.
Aho A.V., Hopcroft J.E., Ullman J.D. (1974). The Design and Analysis of
Computer Algorithms. Addison-Wesley.
Aho A.V., Hopcroft J.E., Ullman J.D. (1983). Data Structures and Algorithms.
Addison-Wesley.
AIJ volume 76 (1995). Artificial Intelligence, Vol. 76. Special issue on Planning
and Scheduling.
Alien J.F. (1995). Natural Language Understanding. Redwood City, CA:
Benjamin/Cu minings.
Allen J., Hendler J., Tate A. (eds) (1990). Readings in Planning. San Mateo, CA:
Morgan. Kauf mann.
Berliner H.J. (1977). A representation and some mechanisms for a problem solving
chess program. In: Clarke M.R.B. (ed.). Advances in Computer Ckess 1. Edinburgh
University Press.
Bobrow D.G. (ed.) (1984). Artificial Intelligence Journal, Vol. 24 (Special Volume
on Qualitative Reasoning1 about Physical Systems).
: Qualitative Reasoning about Physical Systems. Cambridge,
MA: MIT Press 1985.
Bowen D.L. (1981). DECsy&tcm-lO Prolog User's Manual. University of Edinburgh:
Department of Artificial Intelligence.
Brachman R.J., Levesque H.J. (eds) (1985). Readings in Knowledge Representation.
Los Altos, CA: Morgan Kauf mann.
Bramer M.A. (ed.) (1983). Computer Game Playing: Theory and Practice.
Chichester: Ellis Horwood and John Wiley.
Bratko I. (1990). Prolog Programming for Artificial Intelligence. Addison-Wesley,
Bratko I. (1978). Proving correctness of strategies in the AL1 assertional language.
Information Processing Letters, 7: 223-230.
Bratko I. (19S2). Knowledge-based problem solving in AL3. In: Hayes J., Michie
D., Pao J.H. (eds). Machine Intelligence 10. Ellis Horwood (
[11]).
Bratko I. (1932). Knowledge-based problem-solving in AL3. In: Hayes J.E., Michie
D., Pao Y.H. (eds). Machine Intelligence 10. Ellis Horwood.
Bratko I. (1984). Advice and planning in chess end-games. In: Amarel S., Elithorn
A., Banerji R. (eds). Artificial and Human Intelligence. North-Holland.
Bratko I. (1985). Symbolic derivation of chess patterns. In: Steels L., Campbell J.A.
(eds). Progress in Artificial Intelligence. Chichester: Ellis Horwood and John Wiley.
Bratko I. (1999). Refining complete hypotheses in ILP. In: Dzeroski S., Flach P.
(eds). Inductive Logic Programming. Proc. ILP-99. LNAI 1634, Springer.

19. Bratko L, Michie D. (1980). AH advice program for a complex chess programming
task. Computer Journal, 23: 353-359.
20. Bratko I., Mozetic, I., Lavrac, N. (1989). KARDIO: a Study in Deep and
Qualitative Knowledge for Expert Systems, Cambridge, MA: MIT Press.
21. Bratko I., Muggleton S., KaraliC A. (1998). Applications of inductive logic
programming. In: Michalski R.S., Bratko I,, Kubat M. (eds). Machine Learning
and Data Mining: Methods and Applications. Chichester: Wiley.
22. Bratko I., Muggleton S., Varsek A. (1991). Learning qualitative models of dynamic
systems. In: Brazdil P. (ed.). Proc. Inductive Logic Programming ILP-91, Viana do
Castelo, Portugal. . : Muggleton S. (ed.). Inductive Logic Programming.
London: Academic Press 1992.
23. Breiman L., Friedman J.H., Olshen R.A., Stone C.J. (19S4). Classification and
Regression Trees. Belmont, CA: Wadsworth Int. Group.
24. Buchanan B.C., Shortliffe E.H. (eds) (1984). Rule based Expert Systems: The
MYCIN Experiments of the Stanford Heuristic Programming Project. AddisonWesley.
25. Castillo E., Gutierrez J.M., Hadi A.S. (1996). Expert Sustems and Probabilistic
Network Models. Berlin: Springer-Ver lag.
26. Cestnik B. (1990). Estimating probabilities: a crucial task in machine learning.
Proc. ECAI 90, Stockholm.
27. Cestnik ., Bratko I. (1991). On estimating probabilities in tree pruning. Proc.
European Conf. on Machine Learning, Porto, Portugal. Berlin: Springer-Verlag.
28. Cestnik ., Kononenko I., Bratko I. (1987). ASISTANT 86: a knowledge elicitation
tool for sophisticated users. In: Bratko I., LavraC N. (eds). Progress in Machine
Learning. Wilmslow, England: Sigma Press;
Wiley.
29. Chapman D. (1987). Planning for conjunctive goals. Artificial Intelligence, 32:
333-377.
30. Clark P. (1985). Towards an Improved Domain Representation for Planning.
Edinburgh University: Department of Artificial Intelligence, MSc Thesis.
31. Clark P., Niblett T. (1989). The CN2 induction algorithm. Machine Learning, 3,
262-284.
32. Clocksin W.F., Mellish C.S. (1987). Programming in Prolog, second edition. Berlin:
Springer- Ver 1 ag.
33. Coelho H., Cotta J.C. (1988). Prolog by Example. Berlin: Springer- Ver lag.
34. Coffman E.G., Denning P.J. (1973). Operating Systems Theory. Prentice Hall.
35. Cohen J. (1990). Constraint logic programming languages. Communications of the
ACM, 33: 52-68.
36. Coiera E. (1989). Generating qualitative models from example behaviours. DCS
Report No. 8901, School of Computer Sc. and Eng., Univ. of New South Wales,
Sydney, Australia.
37. Cormen .., Leiserson C.E., Rivest R.L. (1990), Introduction io Algorithms
(second edition 2000). MIT Press.
38. Covington. M.A. (1994). Natural Language Processing for Prolog Pivgrammers.
Englewood Cliffs, NJ: Prentice Hall.
39. Davis E. (1990). Representations of Commonsense Knowledge. San Mateo, CA:
Morgan Kaufmann.
40. de Kleer J., Brown J.S. (1984). Qualitative physics based on confluences. Artificial
Intelligence Journal, 24: 7-83.
612

41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.

de Kleer J., Williams B.C. (fids) (1991). Artificial Intelligence Journal, Vol. 51
(Special Issue on Qualitative Reasoning about Physical Systems II).
De Raedt L, (ed.) (1996). Advances in Inductive Logic Programming. Amsterdam:
1OS Press.
Deransart P., Ed-Bdali A., Ceroni L. (1996). Prolog: The Standard. Berlin:
Sp ri n ge r- Verlag.
Doran J., Michie D. (1966). Experiments with the graph traverser program, Proc.
Royal Society of London, 294(A); 235-259.
Engelmore R., Morgan T. (eds) (1988). Blackboard Systems. Reading, MA:
Addison-Wesley.
Ernst G.W., Newell A. (1969). GPS; A Case Study in Generality and Problem
Solving. New York: Academic Press.
Esposito F., Malerba D., Semeraro G. (1997). A comparative analysis of methods
for pruning decision trees. IEEE Trans. Pattern Analysis and Machine
Intelligence, 19: 416-491.
Fallings ., Struss P. (eds) (1992). Recent Advances in Qualitative Physics.
Cambridge, MA: MIT Press.
Fikes R.E., Nilsson N.J. (1971). STRIPS: a new approach to the application of
theorem proving to problem solving. Artificial Intelligence, 2: 189-208.
Fikes R.E., Nilsstxn N.J. (1993). STHJPS. a introspective. Artefteial InMltes-nrs,
59: 227-232.
Flach P. (1994). Simply Logical: Intelligent Reasoning by Example. Chichester, UK:
Wiley.
Forbus K,, Falkenhainer B.C. (1992). Self-explanatory simulations: integrating
qualitative and quantitative knowledge. . [48].
Forbus K.D. (19S4). Qualitative process theory. Artificial Intelligence, 24: 85-168.
Frey P.W. (ed.) (1983). Chess Skill in Man and Machine (second edition). Berlin:
Sp ringer-Verlag.
Garey M.R., Johnson D.S. (1979). Computers and Intractability. W.H. Freeman.
Gaschnig J. (1979). Performance measurement and analysis of certain search
algorithms, Carnegie-Me lion University: Computer Science Department. Technical
Report CMU-CS-79-124 (PhD Thesis).
Gazdar G-, Mellish C. (1989). Natural Language Processing in Prolog, Harlow:
Addison-Wesley.
Genesereth M.R., Nilsson N.J. (1987). Logical Foundation of Artificial Intelligence.
Palo Alto, CA: Morgan Kaufmann.
Gillies D. (1996). Artificial Intelligence and Scientific Method. Oxford University
Press.
Ginsberg M. (1993). Essentials of Artificial Intelligence. San Francisco, CA:
Morgan Kaufmann,
Gonnet G.H., Baeza-Yates R. (1991). Handbook of Algorithms and Data Structures
in Pascal and (second edition). Addison-Wesley.
Hammond P. (1981). Micro-PROLOG for Expert Systems. In: Clark K,L., McCabe,
F.G. (eds). Micro PROLOG: Programming in Logic. Englewood Cliffs, NJ: Prentice
Hall.
Hart P.E., Nilsson N.J., Raphael B. (1968). A formal basis for the heuristic
determination of minimum cost paths. IEEE Transactions on Systems Sciences and
Cybernetics, SSC-4(2): 100-107.

613

64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.

614

Hau D.T., Coiera E.W. (1997). Learning qualitative models of dynamic systems.
Machine Learning Journal, 26: 177-211.
Hsu F.-H., Anantharaman T.S., Campbell M.S., Nowatzyk A. (1990). A
grandmaster chess machine. Scientific American, 263: 44-50.
Jackson P. (1999). Introduction to Expert Systems, third edition. Harlow: AddisonWesley.
Jaffar J., Maher M, (1994). Constraint logic programming: a survey. Journal of
Logic Programming, 19-20: 503-581.
Jensen F.V. (1996). An Introduction to Bayesian Networks, Berlin: SpringerVerlag.
Kaindl H. (1990). Tree searching algorithms. . [95].
Kanal L., Kumar V. (eds) (1988). Search in Artificial Intelligence, Springer-Verlag.
Kearns M.J., Vazirani U.V. (1994). An Introduction to Computational Learning
Theory. Cambridge, MA: MIT Press.
Kedar-Cabelli S.T., McCarty, L.T. (1987). Explanation-based generalization as
resolution theorem proving. In: Proc. 4th Int. Machine Learning Workshop, Irvine,
CA: Morgan Kauf mann.
Kingston J.H. (1998). Algorithms and Data Structures (second edition). AddisonWesley.
Knuth D.E., Moore R.W. (1975). An analysis of alpha-beta pruning. Artificial
Intelligence, 6: 293-326.
Kodratoff Y., Michalski R.S. (1990). Machine Learning: An Artificial Intelligence
Approach, Vol. III. Morgan Kaufmann.
Kolodner J.L. (1993). Case-Based Reasoning. San Francisco, CA: Morgan
Kaufmann.
Kononenko I., Lavrac1 N. (1988). Prolog through Examples: A Practical
Programming Guide. Wilmslow, UK: Sigma Press.
Korf R.E. (1985). Depth-first iterative deepening: an optimal admissible tree
search. Artificial Intelligence, 27: 97-109.
Korf R.E. (1993). Linear-space best-first search. Artificial Intelligence, 62: 41-78.
Kowalski R. (1979). Logic for Problem Solving. North-Holland.
Kowalski R. (1980). Logic, for Problem Solving. North-Ho Hand.
Kowalski R., Sergot M. (1986). A logic-based calculus of events. New Generation
Computing, 4: 67-95.
Kraan I.E., Richards B.L., Kuipers B.J, (1991). Automatic abduction of qualitative
models, Proc. 15th Int. Workshop on Qualitative Reasoning about Physical Systems.
Kuipers B.J. (1986). Qualitative simulation. Artificial Intelligence Journal, 29,
289-338 (. : [167]).
Kuipers B.J. (1994). Qualitative Reasoning: Modeling and Simulation with.
Incomplete Knowledge. Cambridge, MA: MIT Press.
LavraE N., Dzeroski S. (1994). Inductive Logic Programming: Techniques and
Applications. Chichester: Ellis Horwood.
Le T.V. (1993). Techniques of Prolog Programming. John Wiley & Sons.
Lenat D.B. (1982). AM: discovery in mathematics as heuristic search. In: Davis R.,
Lenat D.B. (eds). Knowledge-Based Systems in Artificial Intelligence. McGraw-Hill.
Lloyd J.W. (1991). Foundations of Logic Programming, second edition. Berlin:
Springer-Verlag.

90.

Luger G.F., Stubblefield W.A. (1998). Artificial Intelligence, third edition. Harlow:
Addison-Wesley.
91. Mackworth A.K. (1992). Constraint satisfaction. In: Shapiro S.C. (ed.) Encyclopedia
of Artificial Intelligence, second edition. New York: Wiley.
92. Makarovi A. (1991). Parsimony in Model-Based Reasoning. Enschede: Twenle
University, PhD Thesis, ISBN 90-9004255-5.
93. Markus C. (1986). Prolog Programming. Addison-Wesley.
94. Marriott K.> Stuckey P.J. (1998). Programming with Constraints: an Introduction.
Cambridge, MA: The MIT Press.
95. Marsland A.T., Schaeffer J. (eds) (1990). Computers. Chess and Cognition. Berlin:
Springer-Verlag.
96. Michalski R.S. (1983). A theory and methodology of inductive learning. In:
Michalski R.S., Carbonell J.G., Mitchell T.M. (eds). Machine Learning: An
Artificial Intelligence Approach. Tioga Publishing Company.
97. Michalski R.S., Bratko I., Kubat M. (eds) (1998). Machine Learning and Data
Mining: Methods and Applications. Wiley.
98. Michalski R.S., Carbonell J.G., Mitchell T.M. (eds) (1983). Machine Learning: An
Artificial Intelligence Approach. Palo Alto, CA: Tioga Publishing Company.
99. Michalski R.S., Carbonell J.G., Mitchell T.M. (eds) (1986). Machine Learning: An
Artificial Intelligence Approach, Volume II. Los Altos, CA: Morgan Kaufmann.
100. Michie D. (1986). The superarticulacy phenomenon in the context of software
manufacture. In: Proc. of the Royal Society, London. A405:189-212. . :
Michie D., Bratko L. Expert Systems: Automating Knowledge Acquisition. Harlow,
England: Addison-Wesley.
101. Michie D. (ed.) (1979). Expert Systems in the Microelectronic Age. Edinburgh
University Press.
102. Michie D., Ross R. (1970). Experiments with the adaptive graph traverser.
Machine Intelligence, 5: 301-308.
103. Minsky M. (1975). A framework for representing knowledge. In: Winston P. (ed.).
The Psychology of Computer Vision. McGraw-Hill.
104. Mitchell T.M. (1982). Generalization as search. Artificial Intelligence, 18: 203-226.
105. Mitchell T.M. (1997). Machine Learning. McGraw-Hill.
106. Mitchell T.M., Keller R.M., Kedar-Cabelli S.T. (1986). Explanation-based
generalisation: a unifying view. Machine Learning, 1: 47-80.
107. Moss C. (1994). Prolog++: The Power of Object-Oriented and Logic Programming.
Harlow: Addison-Wesley.
108. Mozeti I. (1987a). Learning of qualitative models. In: Bratko I., Lavrac N. (eds).
Progress in Machine Learning. Wilmslow, UK: Sigma Press.
109. MozetiC I. (1987b). The role of abstractions in learning qualitative models. Proc.
Fourth Int. Workshop on Machine Learning, Irvine, CA: Morgan Kaufmann.
110. Muggleton S. (1991). Inductive logic programming. New Generation Computing, 8:
295-318.
111. Muggleton S. (1995). Inverse entailment and Progol. New Generation Computing,
13: 245-286.
112. Muggleton S. (ed.) (1992). Inductive Logic Programming. London: Academic Press.
113. Newell A., Shaw J.C., Simon H.A. (1960). Report on a general problem-solving
program for a computer. Information Processing: Proc. Int. Conf. on Information
Processing. Paris: UNESCO.

615

114. NibLett ., Bratko I. (1986). Learning decision rules in noisy domains. In: Bramer
M.A. (ed.). Research and Development in Expert Systems III. Cambridge
University Press.
115. Nilsson N.J. (1971). Problem-Solving Methods in Artificial Intelligence. McGrawHill.
116. Nilsson N.J. (1980). Principles of Artificial Intelligence. Palo Alto, CA: Tioga; also
Berlin: Springer-Verlag.
117. O'Keefe, R. A. (1990). The Craft of Prolog. Cambridge, MA: MIT Press.
118. Pearl J. (1984), Heuristics: Intelligent Search Strategies for Computer Problem
Solving, Reading, MA: Addison-Wesley,
119. Pearl J. (1988), Probabilistic Reasoning in Intelligent Systems: Networks of
Plausible Inference. San Mateo, CA: Morgan Kaufmann.
120. Pereira F.C.N., Shieber S.M. (1987). Prolog and Natural Language Analysis. Menlo
Park, CA: CSLI Center for the Study of Language and Information,
121. Pereira F.C.N., Warren D.H.D. (1980). Definite clause grammars for language
analysis - a survey of the formalism and comparison with augmented transition
networks. Artificial Intelligence, 13: 231-278.
122. Pereira L.M., Pereira F., Warren D.H.D. (1978). User's Guide to DECsystcm-10
Prolog. University of Edinburgh: Department of Artificial Intelligence.
123. Pitrat J. (1977). A chess combination program which uses plans. Artificial
Intelligence, 8: 275-321.
124. Platt A., Schaeffer J., Pijls W. s de Bruin A. (1996). Best-first fixed-depth mimmax
algorithms. Artificial Intelligence, 87: 255-293.
125. Plotkin G. (1969). A note on inductive generalisation. In: Meltzer ., Michie D.
(eds). Machine Intelligence, 5. Edinburgh University Press.
126. Poole D., Mackworth A., Gaebel R. (1998). Computational Intelligence: A Logical
Approach. Oxford University Press.
127. Quinlan J.R. (1979). Discovering rules by induction from large collections of
examples. In: Michie D. (ed.). Expert Systems in the Microelectronic Age.
Edinburgh University Press.
128. Quinlan J.R. (1986). Induction of decision trees. Machine Learning, 1: 81-106.
129. Quinlan J.R. (1990). Learning logical definitions from relations. Machine
Learning, 5: 239-266.
130. Reiter J. (1980). AL/X: An Expert System Using Plausible Inference. Oxford:
Intelligent Terminals Ltd.
131. Robinson A.J. (1965). A machine-oriented logic based on the resolution principle.
JACM, 12: 23-41.
132. Ross P. (1989), Advanced Prolog Techniques and Examples. Harlow: AddisonWesley.
133. Russell S.J., Norvig P. (1995). Artificial Intelligence: A Modern Approach.
Englewood Cliffs, NJ: Prentice Hall.
134. Sacerdoti E.D. (1977). A Structure for Plans and Behauior. New York: Elsevier.
135. Sacks E.P., Doyle J. (1991). Prolegomena to any future qualitative physics. New
Jersey: Princeton University, Report CS-TR-314-91. . : Computational
Intelligence Journal.
136. Samnmt C, Banerji R. (1986). Learning concepts by asking questions. In:
Michalski R.S., Carbonell J., Mitchell T. (eds). Machine Learning: An Artificial
Intelligence Approach. Volume II. San Mateo, CA: Morgan Kaufmann,

616

137. Samuel A.L. (1959). Some studies in machine learning using the game of checkers.
IBM Journal of Research and Development, 3: 211-229. . : Feigenbaum
E.A., Feldman L (eds). Computers and Thought. McGraw-Hill, 1963.
138. Say A.C.C. (1998a). L'Hopital's filter for QSIM. IEEE Trans. Pattern Analysis and
Machine Intelligence, 20: 1-8.
139. Say A.C.C. (1998b). Improved infinity filtering in qualitative simulation. Proc,
Qualitative Reasoning Workshop 98, Menb Park, CA: AAAI Press.
140. Say A.C.C, Kuru S. (1993). Improved filtering for the QSIM algorithm. IEEE
Trans. Pattern Analysis and Machine Intelligence, 15: 967-971.
1-41. Say A.C.C., Kuru S. (1996). Qualitative system identification: deriving structure
from behavior. Artificial Intelligence, 83: 75-141.
142. Shafer G., Pearl J. (eds) (1990). Readings in Uncertain Reasoning. San Mateo, CA:
Morgan Kaufmann.
143. Shanahan M. (1997). Solving the Frame Problem: Mathematical Investigation of
the Common Sense Law of Inertia. MIT Press, Cambridge, MA.
144. Shannon C.E. (1950). Programming a computer for playing chess. Philosophical
Magazine, 41: 256-275.
145. Shapiro A. (1987). Structured Induction in Expert Systems, Glasgow: Turing
Institute Press, Addison-Wesley.
146. Shapiro E. (1983). Algorithmic Program Debugging. Cambridge, MA: MIT Press.
147. Shoham Y. (1994). Artificial Intelligence Techniques in Prolog. San Francisco, CA:
Morgan Kaufmann.
148. Shortliffe E. (1978). Computer-based Medical Consultations: MYCIN. Elsevier.
149. Shrager J., Langley P. (1990). Computational Models of Scientific Discovery and
Theory Formation. San Mateo, CA: Morgan Kaufmann.
150. SICStus Prolog Users Manual (1999). Stockholm: Swedish Institute of Computer
Science, http://www.si c s . s e / s i c s t u s . h t m l .
151. Slagle J.R. (1963). A heuristic program that solves symbolic integration problems
in freshman calculus. In: Feigenbaum E., Feldman J. (eds). Computers and
Thought. McGraw-Hill.
152. Stabler E.P. (1986). Object-oriented programming in Prolog. AJ Expert. (October
1986): 46-57.
153. Sterling L (1990), The Practice of Prolog. Cambridge, MA: MIT Press.
154. Sterling L., Shapiro E. (1994). The Art of Prolog, second edition. Cambridge, MA:
MIT Press.
155. Sutton R.S., Barto A.G. (1998). Reinforcement Learning: An Introduction,
Cambridge, MA: MIT Press.
156. Szpakowicz S. (1987). Logic grammars. BYTE. (August 1987): 185-195.
157. Tate A. (1977). Generating project networks. Proc. IJCAI 77. Cambridge, MA.
158. Touretzky D.S. (1986). The Mathematics of Inheritance Systems. Los Altos, CA:
Morgan Kaufmann.
159. van Emden M. (1981). Logic Programming Newsletter, 2.
160. van Emden M. (1982). Red and green cuts. Logic Programming Newsletter, 2.
161. Van Hentenryck P. (1989). Constraint Satisfaction in Logic Programming.
Cambridge, MA: MIT Press.
162. Varsek A. (1991). Qualitative model evolution. Proc. IJCAI-91, Sydney 1991.

617

163. Waldinger R.J. (1977). Achieving several goals simultaneously. In: Hlcock E.W.,
Michie D. (eds). Machine Intelligence 8. Chichester: Ellis Horwood. Distributed by
Wiley.
164. Warren D.H.D. (1974). WARPLAN: A System for Generating Plans. University of
Edinburgh: Department of Computational Logic, Memo 76.
165. Waterman D.A., Hayes-Roth F. (eds) (197S). Pattern-Directed Inference Systems.
London: Academic Press.
166. Weld D. (1994). An introduction to least commitment planning. AI Magazine, 15:
27-61.
167. Weld D.S., de Kleer J. (1990). Readings in Qualitative Reasoning about Physical
Systems, San Mateo, CA: Morgan Kaufmann.
168. Wilkins D.E. (1980). Using patterns and plans in chess. Artificial Intelligence, 14:
165-203.
169. Winston P.H. (1975). Learning structural descriptions from examples. In: Winston
P.H. (ed.). The Psychology of Computer Vision. McGraw-Hill.
170. Winston P,H. (1984). Artificial Intelligence, second edition. Reading, MA: AddisonWesley.
171. Winston P.H. (1992). Artificial Intelligence, third edition. Addison-Wesley.
172. Wirth N. (1976). Algorithms + Data Structures = Programs. Englewood Cliffs, NJ:
Prentice Hall.

618

2
2--, 215

HYPER
HYPothesis refinER, 446

ALO
Advice Language 0, 545
AVL-, 215; 221
AVL-, 222

ILP
Inductive Logic Programming, 446

BK
Background Knowledge, 447
BNF
Backus-Naur Form, 510
-, 225

C L P

Constraint Logic Programming, 324


Colinerauer, 18

D
D C G

,
Definite Clause Grammar, 510
DC- a, 510
de Morgan, 89

Kowalski, 18

LSS
Least Specific Specialization, 462

Michie, 2 2
-, 436

QDE
Qualitative Differential Equation, 457
R
Robinson, 73

E
EBG
Explanation-Based General Nation, 564

V
van Emden, 18

F
F-, 269
f-, 249

w
Warren, 18
Web-
, 21
Winograd, 15


, 480
, 102
, 480
, 480
, 480

, 480

, 506

, 103
, 341
, 39

17, 25
*, 289; 299
QSIM, 502
RBFS, 270
--, 538
, 579
, 303
, 304
, 422
, 232
, 255
, 193
quicksort, 194
, 255
--, 532; 537
--, 540
--, 538
--, 540

, 409
, 512
, 387; 388

, 176
, 50
, 29; 46
end_of_file, 138
nil, 198
stop, 139; 165

, 430
, 427
:

, 98
620

, 327; 358; 560

, 52
-
185
, 18

, 497
, 535
, 505
, 57

, 419
, 341

, 497

, 343
, 343
, 345
, 345
, 345
345
337- 341
, 343

Common Lisp, 25
SICStus Prolog, 306
predecessor, 69
, 54

, 243
, 15

, 161


, 395
, 27; 34; 57
"How?", 336
"Why?", 33S
- " 7 336
"", 336
, 486
, 486
, 486

, 302
, 261
, 261

, 157

, 136
, 426
, 331
, 331; 333
, 141
, 335
, 335

, 331

, 189

, 92

, 58

, 92
, 330; 359
, 522

, 344
, 344

, 201

, 139
, 125
, 47

, 131

, 330

, .993

, 458
, 458
, 447
, 411
, 411
, 463
, 447
, 411
, 248

, 524

, 456
, 532

, 32; 34
, 76

" ", 230


, 55

, 307
, 307
, 510
, 5/0

, 384
,192
AND/OR, 277; 331
, 208
, 103
, 280
, 211
, 449
, 450

, 143
, 433
, 89
, 384
climb, 63; 64
grasp, 63; 64
push, 63; 64
walk, 63
, 63; 229
, 403

, 277
, 50
, 156

, 93; 579
, 49; 192; 218
AND/OR, 284; 533
1, 218
2, 218
, 218
nil, 218
, 77; 197
-, 215
, 336; 367
, 532; 534
, 201
, 209
, 291; 534
, 215
, 198; 218
,280; 411
, 218

621

, 218
, 218
, 201; 215
, 26
, 516
, 199
, 544
, 524
, 342

, 113
, 113

, 73

, 57
, 317
, 88
tty
dynamic, 162
, 88

, 78
,
461
, 82

, 583
, 170

, 153

, 39
, 169
, 208
, 303

, 303

, 282
, 304; 310
, 302

, 31

, 483
, 483
, 483


, 451

, 87
, 332
, 78

flush,
141
, 357
, 34; 306

, 392

, 34
, 88

, 344
, 329
, 329
, 543
, 446; 564

, 42; 45; 56
, 535
Prolog, 56
, 45
, 42; 45; 56; 58
, 523
, ,523
, 535

, 18

, 515
, 330

, 591

, 482
, 383
, 281
, 111
, 62
622

, 283

, 63

, 283

, 535
MIN, 535
, 284; 533
, 284; 533

, 160

, 417

, 16

, 26
, 47
, 50

Gini, 430
, 251; 297
/, 58

, 16; 255

, 346
, 453
-
, 571
, 333

, 493
, 328
, 328;
376
, 562
, 560
,
333
,
334
, 338
Advice Language, 543
AL0, 546

, 522
, 454
-, 343
, 522

, 99; 327
, 308
, 19

, 177
, 335
, 177
, 177
, 428
, 335

, 143
, 441

, 31 7

, 341
, 183

, 406

, 249
, 179
, 52; 156
, 73
, 34; 134
, 134; 525

, 46
, 127

, 257
, 552
, 18

ASCII, 143

, 428
, 18
, 38; 175
, 175

, 146

, 32; 174

, 18

, 78; 181
, 52
, 53
, 53
, 31; 58

, 419
, 516
, 516
, 516
, 489

, 335

, 32
, 29; 57

, 290

, 49

623


, 463
, 540
, 337
, 429

, 410
, 281
, 565
, 281
, 440
, 163; 178; 188

, 463

, 450

,19; 73
, 446
, 73

, 156
, 143
, 385
, 10$
, 249

, 185

, 327

, 337
, 559
ebg, 568
, 559
, 559

null, 104
, 103
, 570
area, 571
EBG, 567
IDA*, 266
RBFS, 260
, 185
, 315
, 429
, 206
, 303
, 357

624

, 199
, 68
, 429
, 583

, 521
, 512
, 396'
, 62

, 15
, 63
, 22


, 167
, 441
, 577
, 441
, 438
, 438
, 410
, 98

, 317
, 185
, 479
, 481
, 317

, 490
, 501
, 478
, 478
, 481
, 318

, 174
, 45
, 170; 328
, 591
, 577
, 328

, 442
, 55
, 170

, 262

, 527
, 328

, 350
, 573

, 306
, 534
, 284; 533

, 303; 341; 496


, 48
, 321
Prolog, 178
, 492

, 409
, 172
, 559; 564
, 112

" , 88
"", 88
"", 88
, 78

, 408
, 409
, 422
, 446
, 447
, 408
, 422
, 447

, 419

, 72
, 144
, 571
, 49
, 45
, 45; 48

, 45
, 364
, 336
, 336
, 488
deriv, 497
mplus, 497
QDE, 497
sum, 497
, 321
, 303

, 452
, 309
, 490
, 313
, 263; 302
, 305
, 497
, 545
, 306

, 543
, 543
, 47
, 52; 156

is, 92
, 88
'"", 166
" \ = = " , 160
" = = " , W0
" > " , 285
"", 156
not, 165; 359
not , 130
,156
"<", 336
via, 299
,121; 164; 174
, 133; 174
, 133; 174
, 159
, 159
, 450

cone, 240
is, 92
SCANNING, 59
, 92
, 202; 217
, 138
, 87; 88
, 195
, 147
, 88
, 88
, 93
, 195
, 161
, 52
, 78
, 54; 524
, 93
, 93
, 484

625

,
156
, 203
, 203
, 138

, 422
, 478
, 412
, 422
, 422
, 564
, 412

, 57
, 94
, 88
,
100
, 55; 90
, 43
, 355
, 37
, 448
, 520
, 521

, 307
, 182; 183
, 183

"ff", 324

, 175

, 176
, 488
, 26; 29
a_kind__of, 351
accepts, 105; 108
actor, 524
add, 127; 202
add_at_end, 190
add23, 217; 221
addavl, 221
addleaf, 202
addroot, 205
adds, 387
adjacent, 210
alphabeta, 539
AND, 279
askable, 371
attack, 131
aunt, 34
avl, 224

626

born, 93
breadthfirst, 238
canget, 86
class, 127
combine, 221
cone, 79; 111
concat, 182
copyterm, 164
count, 151; 160
del, 82
deptime, 108
different, 33
digitsum, 154
dividelist, 86
enlarge, 156
expand, 297
fact, 334
female, 30
final, 104
flatten, 86
flight, 108
forwardfib, 188
goal, 257
grandchild, 34
grandparent, 28
gt, 192; 200
h, 257
hamiltonian, 210
happy, 34
hastwochildren, 34
impossible, 396
in, 198; 221
ins, 217
ins2, 221
instance_of, 351
isa, 349
jump, 119
knightpath, 120
length, 95; 101
lengthl, 95
linearize, 201
male, SO
max, 95; 126
maxelement, 201
member, 79; 179; 180; 210
memberl, 81
mini max, 536*
mother, 32
move, 64; 66; 513
moves, 536'
newbounds, 540
ngb, 179
no_attack, 131
noattack, 113; 234
not, 130

nqueens, 173
nthchild, 103
object, 570
offspring, 30
OR, 279
parent, 28
path, 209; 210
pathl, 210
permutation, 84; 111
powerset, 167
predecessor, 35; 68; 457
qsum, 483
regular, 56
reverse, 85; 190
route, 108
s, 256
safe, 115
set_difference, 131
shift, 85
silent, 104
sister, 33
sol, 118
solve, 232; 286
sort, 192
split, 196
starts, 146
sublist, 83
subset, 86
substitute, 157
subsumes, 159
sum, 152
suml, 153
timetable, 107
trans, 104
transfer, 108
transition, 108
translate, 86
, 30
, 458
, 30
, 161; 313
, 349
, 102
, 30
,192; 199

, 206

, 54; 56
, 55
, 49

, 121
, 129

, 433
, 433
, 434
, 433
, 541
, 32

, 437
, 263
, 541
, 440
, 259
, 247; 260

, 435
, 434
, 435
, 47

, 85

,181

Answer, 452
Solved, 253
, 176
, 176
, 182; 183
, 102

, 62; 121; 163


, 121
, 29
, 47

, 309

, 84
, 179
, 68
, 68

, 103
, 104

,
499

, 385
, 302; 383
, 107
, 385
, 404

627

, 383
, 109
, 404
, 397
, 404
, 284; 533

, 384

, 385
, 49
, 83

9, 459

, 94

,161
, 32

, 163
, 284
, 532
, 111
, 284
, 138
, 284

, 232

, 237
,
28 7
AND/OR, 285
, 353
, 238
, 425
, 108
, 125
, 212
, 39
, 247; 249; 460
" ",
425
, 425
, 245; 247
, 80

, 146
, 58

, 564

, 440
, 284; 533

628

, 67

, 40

, 440

, 137
, 137
, 137
, 137

, 63
, 137
, 536
, 152
, 31; 34
"if-then", 328
"-", 419
, 330
, 325; 544
, 290
, 422
, 328
, 37
, 71
, 53
, 169

accepts, 105
achieves, 395
actor, 524
addl, 469
ako, 474
all_different, 322
assert, 161
asserta, 161; 163
assertz, 161
atom, 151
backliteral, 450
best_search, 468
binary tree, 201
call, 165
canget, 64
checkmove, 552
hoose_attribute, 431
circuitja, 319
circuit_b, 319
circuitl, 485
complete, 456
compose, 524
consistent, 456
consult, 163
depth_first, 456
deriv, 497

dictionary, 201
domain, 321
driver, 144
e n d o f g a m e , 549
eval, 468
even, 470
evenlength, 55
ex, 449
father, 16
female, 446
fib, 186; 308
getsentence, 144
gives, 567
goal, 256'
grandfather, 16
ground, 159
has_daughter, 447; 450
indomain, 321
induce, 456
inducehyp, 468
initcounts, 469
insertsorted, 474
intrans_verb, 524
isjtrue, 336
iterdeep, 456
kingdiagfirst, 552
labeling, 322
legal, 552
length, 95
likes, 522; 567
lpatt, 552
male, 446
mate, 551
max_clause_length, 469
max_clauses, 461; 469
max_proof_length, 469
maxlist, 95
moves, 536
mplus, 497
name, 159
newroomsmaller, 551
nex, 450
nl, 139
nomove, 552
nonvar, 154
nospy, 177
not, 130
notrace, 177
odd, 470
oddlength, 85
okapproachedcsquare, 552
once, 164
order, 144
ordered, 96
paints, 522

palindrome, 85
parent, 16; 446
path, 471
prec, 311
precconstr, 312
precedenceconstr, 312
prolog_predicate, 450
prove, 452; 468
rectangle, 570
refine, 455; 468
refine_hyp, 456; 468
resource, 311; 316
retract, 161
rookdivides, 5S1
rookexposed, 551
rooklost, 551
rookmove, 552
roomgt2, 552
sad,567
satisfy, 431
schedule, 313
see, 137
select, 395
show_counts, 469
showhyp, 469
solution, 111
spy, 177
stalemate, 551
startclause, 469
start_hyp, 456
start_hyps, 468
subsum, 96
sum, 497
sumlist, 95
support/2, 474
tasks, 311
taxi, 144
terminallost, 533
terminalwon, 533
touch/2, 474
trace, 177
transition, 499
unifiable, 132
verb_phrase, 524
, 95; 136; 149
" = . , " , 156
arg, 156: 158; 184
atom, 150
atomic, 150
bagof, 165
call, 158
clause, 561
compile, 147
compound, 150
copy__term, 164

629

findall, 165
float, 150
functor, 156; 158; 184
get, 138
getO, 138
inf, 308
integer, 150
is, 307
maximize, 307
minimize, 307
name, 144
nonvar, 150
number, 150
put, 138
read, 138; 139
setof, 165; 180
sup, 308
var, 150
write, 138; 139
,
139
, 139
, 762; 590
, 154
, 162
, 590

, 460
, 323
, 552
, 148
, 590
, 446
, 447
, 26; 29; 34
, 462
, 527
, 73

,359

, 491
, 121; 133
, 384
, 64

AND/OR, 277
, 208
, 72
, 49
, 99
, 197
, 215
, 409
, 72

630

, 76
, 77
, 411
, 522
, 47

, 479
, 231

,
183
, 196
, 461
, 145

"-", 176
" + " , 176

, 359
, 140
, 357

, 446
, 446
, 565

, 532; 534
, 119
, 73

, 409

, 87
, 88

, 343

, 261
, 301

k- , 441
, 73
, 160
, 306
, 306
, 86; 198
, 78
, 149
, 342
, 58
addleaf, 202
ARCHES, 419
cone, 82
convert, 307
family, 37

HYPER, 446; 448


length, 94
MINIHYPER, 448; 454
schedule, 312
, 541
, 147

, 493
, 448
, 568
, 420
, 147
, 108
, 154
, 521
, 448
, 313
, 15
, 540
, 105

, 446
, 30/
, 16
, 301
, 446
-, 570
, 37
, 579
, 15

, 541
, 328

, 328

, 384
, 534
, 228; 277
, 235
, 37; 38
actor, 524
add_to_tail, 155
adds, 384
andor, 298
assign_processors, 315
bars, 141
between, 96
bubblesort, 193
can, 384
choose_attribute, 432
collect, 190
combine, 298
compute, 158
continue, 254; 298

count, 151
cube, 139
deletes, 384
depth_first_iterative_deepening, 237
depthfirstl, 2,37
depthfirst2, 236
dosquares, 165
ebg, 569
enlarge , 157
execute, 58; 59
expand, 251; 253
expandlist, 297
expert, 366; 377
explore, 366
fact, 332
fib/ 2, 308
fib2, 187
findl, 241
find2, 241
find3, 241
forwardfib, 188
gen, 118
getletters, 145
getreply, 369
getrest, 144
getsentence, 144
height, 201
inipurityl, 432
in, 198
inducejtrees, 431
InduceOneRule, 422
insert sort, 193
is_true, 333
learn, 426
makelist, 180
maketable, 163
maplist, 172
max, 190
merge, 175
move, 513
numbervars, 376
obtain, 158
path, 236
permutation, 84
plan, 396
playgame, 549
plural, 146
present, 366; 376
processftle, 142
prove, 560
prunetree, 439
quicksort, 194
quicksort2, 196
reverse, 184
satisfied, 403

631

schedule, 312
search, 146
send, 571
show, 206; 287; 298
show2, 299
showfile, 142
simplify, 155
sol, 118
solution, 112
solve. 234; 287
split, 129
squeeze, 143
subl, 189
sub2, 189
sub3, 189
substitute, 159
succlist, 254
sum list, 183
useranswer, 366; 369
value, 352
write, 139
writelist, 141
writelist2, 141
writenode, 299
, 92
asserta, 187
copy_term, 569
, 450
AND/OR,
, 334
, 182
, 307
, 183
, 377
, 331
, 15

, 54
, 261

, 56
, 56
, 52; 56; 156
, 137
, 123

, 298

, 209
, 210
, 211

632

, 160
, 306

, 144
, 84

, 570

, 15

, 429

, 328

, 463
, 292
, 322

Prolog, 171

, 179

, 206

, 108
, 261
, 262
, 262

, 45

, 302

, 259
, 207
, 259

, 478
, 482

findall, 167

, 208

, 151

, 395

, 147
, 393
, 51; 317
, 384

, 171
, 182

, 15
, 73

, 256

, 186

, 104
, 261
, 26; 349; 384
AND, 278; 280
OR, 280
, 102

Prolog , 65

, 56

, 340; 342
, 340
, 330
, 303
, 349
, 359
, 138
" ; : = " , 512
"-->", 512
"null", 426
, 138
, 143
, 511
, 47; 99
, 104
, 511

DEC-10, 20
, 20

, 482

AL/X,329
AL3, 329
CLP,305
CLP(B), 306
CLP(FD), 306
CLP(Q), 306; 310
CLP(R), 306
CLP(Z), 306
MYCIN, 329

Shrdlu, 15
DCG, 520
, 51
, 326
, 576
, 326

, 532
, 228

, 59

, 196

, 199
, 216
-, 215; 216

, 146
, 52,5
"", 525
"every", 526

, 440
, 534
, 425; 448
, 201
, 350; 524

, 172
, 172

, 519
, 522
, 522
, 517
, 341
, 543
, 543
, 544
, 303
, 45; 52; 73
, 73
, 461

, 174

, 318
, 318
, 52
, 5/

, 546

"More core needed", 69

633

"Stack overflow", 69

, 193
, 193
, 192

, 262
, 262
, 104; 492
, 63
, 63
, 63; 262
, 497
, 292
, 497
, 230\ 262
-, 262

, 501

, 391
, 163

, 88

, 343; 348
, 76: 192
, 384
, 76
, 294
, 76
, 181; 512
, 384
, 546
, 384
, 57; 58

, 417
, 567

, 306
, 93

, 333
, 176
, 301
, 164

ISO,18
ISO./IEC 13211-1, 44
Prolog, 148
Prolog, 18

, 173

634

, 535
, 211
, 231; 262

SLD, 73
, 48
, 98; 192
, 50; 197
CLP(FD), 322
, 172
, 102
, 327

, 48
, 571
, 308
, 349

, 173
, 64
, 338
, 32
, 51; 317; 482

, 405

, 374
, 18

product, 164
npoz3BefleHHfi , 163
, 544
, 163

, 31; 34
, 39
, 344
, 255; 256; 294
, 89

, 339
, 429
, 564; 565
, 49
rectangle, 56
, 53
, 192

, 137

, 88
askable, 369

, 49
, 49
, 49
, 57; 175; 306

, 413
, 434; 440
, 413
, 177; 364; 562
, 60
, 177
, 291
,177
, 49; 156

, 540
-, 458

, 232; 236; 287


, 542

, 78
, 82
, 169

, 301

AND, 279; 280; 284


OR, 279; 280; 284
, 103
, 532
, 229
, 450
, 450
, 229; 278
-, 233
, 15
, 73
, 18

, 161
, 192
, 161

, 121

QDE, 487
, 479

, 408

, .565

, 3/
, 52
, 450
, 171
, 171
, 170
, 525
, 451
, 462

, 337

, 137
user, 137
, 137
, 137
, 138
, 138
, 138
, 26; 34
, 186

, 73
, 408
, 73
,
584
, 45

, 136

, 68
, 563
, 55
, 336
, 262

, 436
, 344
, 429

, 65
, 511
, 350
, 48
, 208
digraph, 208
, 208
graph, 208
par, 51
point,- 49
point3, 49
rectangle, 56

635

seg, 49
seq, 51
state, 63
triangle, 49
, 77
, 49; 87

, 248
, 121
, 490
, 490
, 534
Cost, 463
, 536
, 263; 535
h, 289

, 76

, 535

, 29
assert, 161
consult, 146; 147
fail, 129; 165
functor, 184
getO, 143
getsentence, 144
in, 198
put, 143
read, 139
repeat, 165
retract, 161
see, 137
seen, 138
setof, 166
squeeze, 143
sum, 152
tab, 139
tell, 137
told, 138
treat, 142
true, 130;165
write, J39; 206
, 63
, 543
, 543
, 403
, 378

636

,
134
, 124

, 67
, 106

, 152

, 47
, 47
, 187; 308
, 47

, 136
, 136; 146

L-, 552
, 532
, 532

, 19

, 245

, 20
, 57

, 544

, 103; 108

, 543

, 457

, 542
, 169
, 178
, 200

,511
Advice Language 0, 545
AL0, 545
Fortran, 15

Lisp, 15
Mi 1 aimer, 15
Pascal, 61
Prolog, 18
Prolog , 74
, 15
, 411; 412; 447
, 15

,15
, 412
, 411; 412
, 15
Advice Language, 532; 543
, 305
, 18

637


PROLOG,
3-
.
,.
.
.. , . ,
..

"".
101509, , . , . 43, . I.
. . 090230 23.06.99
.
12.07.2004. 70x100/16.
Times. .
. . . 51,6. .-. . 41,6.
3000 . 180.
" "
,
.
197110, -, ., 15.