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

( TEX )

: . .
: . .
. .

, 2009

1
. . . . . . . . . . . . . . . . . . .
2
1- . . . . . . .
2.1
. . . .
3
1- . . . .
4
. . . . . . . . . . . . . . .
5
. . . . . . . . .
6
. . . . . . . . . . . . . .
7
. . . . . . . . . . . . . . . . . .
7.1
. . . . . . . . . . . . . . . . .
8
modus tollens . . . . . . . .
8.1
modus tollens . . . . . . . . . . . . . .
8.2
. . . . . . . . . . . . . . .
9
. . . . . . . . . . . . . . .
9.1
. . . . . . . . .
9.2
SLD- . . . . . . . . . . . . .
10 SLD- . . . . . . . . . . . . . .
10.1

11 C . . . . . . . . . . . . . . . . . . .
12 . . . . . . . . . . . . . . . . . . .
12.1 SLDNF- . . . . . . . . . . .
12.2
not . . . . . . . . . . . . .

. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
.
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

II Prolog
13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13.1 . . . . . . . . . . .
14 O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15.2 . .
15.3 . . . . . . . . . . . .
15.4
16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

4
4
7
8
9
12
13
14
16
17
18
18
18
20
20
21
22
22
23
25
25
26

.
.
.
.
.
.
.
.
.

27
27
27
29
31
31
31
32
33
34

17
18
19

16.1
repeat. . . . . . . . . .
16.2
for. . . . . . . . . . .
. . . . .
read, write, nl . . . . . . . . .
asserta-setof . . . . . . . . . .
19.1 asserta/assertz, retract
19.2 findall, bagof/setoft . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

III C
20 . . . . . . . . . . . . .
21 O 1 . . . . . . . . . . . . . .
21.1 . . . . . . . . . . .
21.2 n- . . . . . . . . . .
21.3 . . .
21.4 . . . . . . . . . . .
21.5 . . . . . . . . . .
21.6 extend . . . . . . . . . . . .
22 O 2 . . . . . . . . . . . . . .
22.1 . . . . . . . . . . . .
22.2 . . . . . . . . .
22.3 . . . . . . . . . . . . . . .
23
23.1 . . . . . . . . . . . . . . . . .
24 . . . . . . . . . . . . . . . .
24.1 . . . . . . . . . . .
24.2 . . . . . . . . . . .
24.3 . . . . . . . . . . . . . .
24.4 . . . . . . . . . . . . . . . . .
25 . . . . . . . . . . . . . . . . . . . . . .
26 . . . . . . . . . . .
27 . . . . . . . . . . . . .
27.1 . . . . . . . . . . . . . . . . .
27.2 . . . . . . . . . . . . . . . . .
IV

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

35
35
36
38
39
39
40

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

42
42
45
45
45
45
46
46
46
47
47
48
49
50
51
52
52
52
52
53
55
58
61
61
62
63

28

29
30
31
32
33
34
35
36

, , . . . . . .
28.1 . . . . . . . .
28.2 . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

63
63
63
64
69
70
71
72
73
74
75

V
76
1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

I.

1.1

.
. ,

:

.

, .
1.
2. -

1.
2.

3.

+
1.

1.

2.
3. ( )
4.

I.

1.2

: . .
.

: .

:
.





.

.

,

:

.

:

.

, , ,
, . ,
.
1. :
5

I.

1.3





.
-. .
1. ,
.
2 ( -). .
.

I.

2.1

1-

1- . . . |=.
2. ( )
,
. ,
. , .
3. ( )
, ,
, . .
.
:
1. ( )
2. (, )
3. f (x1 , . . . xn ) .

I.
2.1

2.2

D. :
( )

ai :
a : {ai } D

(n)

f : {fi } (Dn D)

(n)

p : {Pi } (Dn {T, F})


.. = hD, a , f , p i
( { x1 , . . . xn })

{a1 , . . . an }
4. F |= F
.
5. F
.
6. B A
A |= B
A B

A1 , . . . An |= B A1 An |= B
3.
A |= B |= A B

I.

3.1

1-

1- . . (`) .
modus ponens.
7.
1.
A = {A0 , . . . , An }

2.
A

3.
0
4.

8. f {f0 , . . . , fn = f } fi
{f0 , . . . , fn = f }
9. , f

`f
10. f
`f
0 ` f

I.

3.2

11.
1.

A=
{, , (, ), , , , }


12. :
(a) ai M
(b) xi
(c) f (t1 . . . tn ), n;
ti
2.
P (t1 . . . tn ), P n;
ti

A
AB
AB
AB


F x , (x)F (x)F x
10

I.

3.3

3. :
p (q p)
(p (q r)) ((p q) (p r))
(p q) (q p)
:
(x)A(x) A(t)
A(t) (x)A(x)
4.
(modus ponens.):
A, A B ` B
(A x):
A B(x) ` A (x)B(x)
B(x) A ` (x)B(x) A
( )

11

I.

4.1

, .
. .

13. F |= F
.
14. F
.
15.

|= A ` A

1. :
@A((` A) (` A))
2. ():
` A |= A
4 ( ).

:
, A ` B ` A B
:
|= A ` A
:
, A ` B, A, ` A B
5 ( ).
|= A ` A
.
12

I.

5.1

. .
.

6 ( ).
|= A ` A
.
7 ( ). , , . |= F , F F ,
.
.
16. , , .. ,
.
:
( )
( )
8 (). ,
..
.
( ).
. .

13

I.

6.1

(, ). . .
17.
18.

A (B C) (A B) (A C)

(A B) A B
(A B) A B

(A B) = A B
(A B) = (A B) (A B)

()
(x1 . . . xn )F (x1 . . . xn )
{, }
F

()

, .

1.

2.

14

I.

()

6.2


A1 A2 An ;

1.

Ai {P (), P ()};

2.
3. ( )
4.
5.

A B1 Bn ;

A
=
B1 Bn
=
(B1 Bn )
(X Y =
=
(B Bn )

B1 Bn
A
A
(X Y ))
A

, . : ()
.

15

I.

7.1

. .
19.
= {X1 /t1 , . . . Xn /tn }

(i 6= j)Xi 6= Xj , ti 6= Xi
.
20. F (F )
(. ), Xi ti .
21. , F G, F =
G.
22. , .
, .
, , ,
, . .
:
= {x1 /t1 , . . . , xk /tk } = {y1/s1, . . . , yl /sl } . ,
.
{x1 /(t1 ), . . . , xk /(tk ), y1 /s1 , . . . , yn /sn }
xi = xi 1 <= i <= k, yj = sj ,
yj x1, . . . , xk, 1 <= j <= l.

,
, ,
, .
, ,
( ) = ( )

16

I.

7.2

23.
,

, =
:
24. (mgu) , . :
= mgu(F, G).
7.1

1. . . ,
2. . .
3. . :
(a)
(b)
(c)
.

17

I.

8.1

modus tollens

modus tollens. .
8.1

modus tollens
A B, B ` A
A B ,
B,
A

8.2


A B, B ` A

(L1 Ln ) A, (M1 Mk ) A ` (L1 Ln ) (M1 Mk )
L1 Ln , M1 Mk
, A .
: (L1 Ln ) A, (M1 Mk ) A
: (L1 Ln ) (M1 Mk )

(L1 Ln ) A , (M1 Mk ) A
` ((L1 Ln ) (M1 Mk ));
= mgu(A, B);
. . , , ,
.

.
A {B1 , Bn }
{B1 , Bn , A}
18

I.

8.2

A, A 

19

I.

9.1


. . SLD-.
9.1

25. ,
, .
:

26. (L-)
, .

.

27. (S-) .

28. S- (R-)
.
SL R - .

,
. .
SL-:
+:

:

.
SLD-.
20

I.
9.2

9.2

SLD-

29. SLD- ,
.
30.
A B1 Bn ;
A
=
B1 Bn
=
(B1 Bn )
(X Y =
=
(B Bn )

B1 Bn
A
A
(X Y ))
A

,
. : () .

21

I.

10

10.1

SLD-

SLD- . . .
.
31. SLD- ,
.
:
1. .
2. , , .
3. (modus tollens) :
.
.
4. . = .
. (. 61 ).
SLD-. .
32. ,
,
.
SLD- ,
.
10.1


, .
() 1
backtracking, .

.
.
22

I.

11

11.1

. .
. -.
:

.
.
: .
: .
SLD- . .
.
.
: .
: .
:
1. 1
2. , .

23

I.

11.2

:
ProLog . . :
:
(X)(Y )(((Z)A(X, Z) P (Z, Y )) P (X, Y )) A(X, Y )
:

A(X, Y ) A(X, Z) P (X, Z)
A(X, Y ) P (X, Y )
.

A(X, Y ) P (X, Y )
A(X, Y ) A(X, Z) P (X, Z)
1 .
:
P (a, b)
P (a, c)
P (c, d)
:
A(U, d)
Mercury . . . .
DataLog .

24

I.

12

12.1

. . SLDNF-.
. .

P ,

33. -

, P .
, P .
, ().
,
(CWA):
(A) ` (` A)

12.1
1
2
3
4

SLDNF-

p : p.
q : q.
q.
. CWA
p .

(` A) (N )n <= N
, n SLD- .
34. comp() CWA.
9 ( ). . A SLD , comp() |= A.
35. .
25

I.

12.2

(SLDNF-) SLD-:
A A.
A A
.
.

( A A ),
( )

(X)(Y )parent(X, Y ) (X)(Y )parent(X, Y )
12.2
1
2
3

not

not(Predicate):
Predicate,!,fail.
not(_).

26

II.

13.1

II

Prolog

13

.
:
, .
.


. , .
backtrackinga
,
.
13.1

:






:

::=
| "(" {","}")"
|
|
|
| "(" ")".
::= | .
::= .

::= .
27

II.

13.2

:


:
1.
2.
3.
.

28

II.

14

14.1

: , , , , ,
, .
:
1.
(a) ([A-Z-].*) 1 .
.
i. ([_]) .
1
2

has_child(X): parents(X, Y, Z).


has_child(X): parents(Y, X, Z).
m

1
2

has_child(X): parents(X, _, _).


has_child(X): parents(_, X, _).


.
backtracking.
.
(b)
i. . 1 1 .
[a-z-].*.
[^\<=>\-/]+ .
[].*?[] .
ii. ([0-9]+) .
, .

29

II.

14.2

2. f (t1 , . . . tn ), f n. : f /n.
:
1

(, (11, , 1989))
:



11

1989
. .

30

II.

15

15.1

. . .
.
15.1

: , , .
C:



(+, , , /)
()
15.2

=,
(. 88).
is, , ,

31

II.
15.3

15.2

op.
:
1

: op(<>, <>, <>)


[0, 255] ( ).
. :- 255.
:


fx; fy; xf; yf;

yfx; xfy; xfx;


xf; yf;

yfx; xfy; xfx;

fx; fy;
, :
x
y

yfx;

xfy;

xfx; xf; fx;
,
fy; yf;

(<==>).
(seq).
32

II.
15.4

15.3

, ,



1
2
3
4

resistance(par(X, Y), R) :
resistance(X, RX),
resistance(Y, RY),
R is RX RY / (RX + RY).
(lisp forewer):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

: ( resistance(par(X, Y), R),


,(
,(
resistance(X, RX),
resistance(Y, RY)
),
is (
R,
/(
(RX , RY)
+(RX , RY)
)
)
)
).

33

II.

16

16.1

. . for, repeat.
:
,
.
1
2
3
4
5

print(11).
print(X) :
X =< 10, write(X), X1 is X + 1, print(X1).
print :
print(1).
:

1
2

iloop :
iloop
.

.
.
1
2
3
4
5

print :
gen(1, X), write(X), fail.
gen(X, X).
gen(Y, X):
Y < 10, Y1 is Y + 1, gen(Y1, X).
:

1
2
3
4

iloop :
loop, fail
loop.
loop : loop.

34

II.
16.1

16.2

repeat.

. repeat . , .
repeat.
repeat : repeat.

1
2

:
read_dim:
repeat, read(X), X >= 3, X <= 6.

1
2
3
4
5

read_ans:
repeat, read(X), member(X, [yes, no]).
, , .
, repeat .
16.2

1
2
3

for.
for(A, A, _).
for(X, A, B):
A < B, A1 is A + 1, for(X, A1, B).

, , (member)
Python, ( ).
1
2
3
4
5
6
7

integer(A, A, [A]).
integer(A, B, [A|R]):
A < B, A1 is A + 1,
integer(A1, B, R).
for(X, A, B):
integer(A, B, L), member(X, L).
:

? for(X, 1, 10), write(X), fail.

35

II.

17

17.1

(cut). not.
. ,

.
36. , , .
:
,

.
1
2
3
4
5

C: P, Q, !, R, S.
% R1 %
C: T.
% R2
A: B, C, D.
R1:
(P, Q) !, :
(P, Q).
R1.
R2 backtracking
P, Q. R, S .
@(P, Q).

36

II.

17.2

( ):
. .
. .
:
( ).
father(X, Y): parent(X, Y), male(X).

m 1 . .
father(X, Y): parent(X, Y), male(X), !

.
numOfLegs(cetipede, X): !, X = 40.
numOfLegs(human, X): !, X = 2.
numOfLegs(_, 4).

1
2
3

. !-fail .
not(Predicate):
Predicate,!,fail.
not(_).

1
2
3

.
Q : if A then B, else C.

m
1
2

Q : A, !, B.
Q : C.

.
1
2
3

fact(1,1): !.
fact(N,F): N1 is N 1,
fact(N1,F1), F is F1N.

37

II.

18

18.1

: read, write, nl

: read, write, nl.


,
, - , .
- :
write(X)
X stdout.
read(X)
( ) stdin X.
nl
write("\n") .
:
1
2
3

main :
specialty(tonja, X),
write(X), nl, fail.

1
2
3
4
5

main :
wrire(),
read(Name), % write
specialty(Name, Spec),
write(Spec), nl, fail.
( 94):
see(fileName)
tll(fileName)
seeing(fileName); seeing(X)
telling(fileName); telling(X)

38

II.

19

19.1

asserta/assertz, retract, findall, bagof/setof

:
asserta/assertz, retract. . :
findall, bagof/setof.
C http://www.webauto.ru/forum08/ (, )
19.1

asserta/assertz, retract

( )
.
assert ,
assertz.
asserta / .
assertz / .
retract .
, , ,
, :
1
2
3

assert( fact( a ) )
%
assert( rule( X ): fact( X ) )
, :

1
2
3
4
5
6
7
8

? assert( fact( b ) ), assertz( fact( c ) ), assert(fact( d ) ), asserta( fact( a ) ).


Yes.
? fact( X ).
X = a;
X = b;
X = c;
X = d;
No.
,
, (
), , , .
39

II.
19.2

19.2

findall, bagof/setoft

findall/bagof setof. findall bagof


:
1. findall(X,pred(X,...),L).
X ,
pred(X,...) , X , .
L ,
pred.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

findall(X,P,L):
(
call(P), % P,
assertz(found(X)), %
fail % backtracking
);
collect(L).
collect(L):
(
retract(found(X)), %
L=[X|Rest], %
collect(Rest) %
);
L=[]. %

2. bagof(X,pred(X,...),L).
findall, .
3. setof(X,pred(X,...),L).
, ,
.

40

II.

19.3

c http://www.csupomona.edu/.../prolog_tutorial/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

p(1,3,5).
p(2,4,1).
p(3,5,2).
p(4,3,1).
p(5,2,4).
? bagof(Z,p(X,Y,Z),Bag).
Z = _G182 X = 1 Y = 3 Bag
Z = _G182 X = 2 Y = 4 Bag
Z = _G182 X = 3 Y = 5 Bag
Z = _G182 X = 4 Y = 3 Bag
Z = _G182 X = 5 Y = 2 Bag
No

=
=
=
=
=

[5]
[1]
[2]
[1]
[4]

;
;
;
;
;

? findall(Z,p(X,Y,Z),Bag).
Z = _G182 X = _G180 Y = _G181 Bag = [5, 1, 2, 1, 4] ;
No
? bagof(Z,X^Y^p(X,Y,Z),Bag).
Z = _G182 X = _G180 Y = _G181 Bag = [5, 1, 2, 1, 4] ;
No
? setof(Z,X^Y^p(X,Y,Z),Bag).
%$$
% (\exists X)(\exists Y)p(X, Y, Z)
%$$
Z = _G182 X = _G180 Y = _G181 Bag = [1, 2, 4, 5] ;
No
? bagof(Z,(p(X,Y,Z),Z>5),Bag).
No
? findall(Z,(p(X,Y,Z),Z>5),Bag).
Z = _G182 X = _G180 Y = _G181 Bag = []
Yes

41

III.

III
20

20.1

. . =..

( ).
, .
37.

(t, )

t ()
( (t, ))
.
:
[] .
.:
(1, (2, (3, ))) .(1, .(2, .(3, [])))
:
(1, (2, (3, ))) [1, 2, 3]
, :
[]: .
[]: = [].
.:
1

.(, ) = .

:
1

[ | ] = .

42

III.

20.2

:
[A, B|T ] .(A, .(B, T ))
,
[[1, [2]], 3, [4]]
.

: functor, arg =... =..,
(univ).
1

Term =.. L
, L , Term,
. :

1
2
3
4
5
6

? f ( , b) =.. L.
L = [f, a, b]
? =.. [ rectangle , 3, 5].
= rectangle( 3, 5)
? Z =.. [, X, f (X, Y)] .
Z = ( X, f(X,Y) )
,
, ,
.

43

III.

20.3

:
, .
1
2
3
4
5
6
7
8

enlarge( Fig, F, Figl) :


Fig =.. [Type | Parameters],
multiplylist( Parameters, F, Parameters1 ) ,
Figl =.. [Type | Parametersl ].
multiplylist( [], _, [] ).
multiplylist( [X | L], F, [X1 | L1]) :
X1 is F X, multiplylist( L, F, L1 ).

, .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

% substitute ( Subterm, Term, Subterml, Terml) :


% Subterm
% Term
% Subterml, Terml
% 1.
substitute{ Term, Term, Term1, Term1) : !.
% 2. ,
% Term atomic
substitute( _, Term, _, Term) :
atomic(Term) , !.
% 3.
substitute( Sub, Term, Sub1, Term1) :
Term =.. [F|Args], %
substlist( Sub, Args, Sub1 Args1), %
Terml =.. [F|Args1].
substlist( _, [], _, []) .
substlist( Sub, [Term|Terms], Subl, [Terml|Terms1]) :
Substitute( Sub, Term, Sub1, Term1),
Substlist( Sub, Terms, Sub1, Terms1).
? substitute( sin(x), 2sin(x)f(sin(x)), t, F).
F = 2tf(t)

44

III.

21

21.1

: , n- ,
, .

: , n-
, , .
21.1
1
2
3
4

% Prolog
length([], 0).
length([_|T], N):
length(T, N1), N is N + 1.
21.2

1
2
3
4


: func length(list(T)) = int.
: mode length(in) = out is det.
length([]) = 0.
length([_|T]) = length(T) + 1.

1
2
3
4

n-

getN([Elem|_], 1, Elem).
getN([_|List], N, Elem):
M is N 1,
getN(List, M, Elem).

21.3

1
2
3
4
5

% Prolog
member(X, [X|_]).

member(X, L): remove(X, L, _).

member(X, [_|T]):
member(X, T).

1
2
3
4
5

: pred member(T, list(T)).


: mode member(in, in) = is semidet.
: mode member(out, in) = is nondet.
member(X, [X|_]).
member(X, [_|T]): member(X, T).

45

III.
21.4

21.2

append(A, B, C), C = A [B]


1
2
3
4
5

% Prolog
append([], X, [X]).
append([_|T], Y, [_|A]) :
append(T, Y, A).
21.5

1
2
3
4
5

: pred append(list(T), T, list(T)).


: mode append(in, in, out) = is det.
: mode append(out, out, in) = is multi.
append([], X, [X]).
append([_,T],Y,[_,L]): append(T,Y,L).

1
2
3
4
5

: pred extend(list(T), list(T), list(T)).


: mode extend(in, in, out) = is det.
: mode extend(out, out, in) = is multi.
extend([], X, X).
extend([_,T],Y,[_,L]): extend(T,Y,L).

extend(A, B, C), C = A B
1
2
3
4
5

% Prolog
extend([], X, X).
extend([_|T], Y, [_|A]) :
extend(T, Y, A).
21.6

extend

extend(A, B, C), C = A B
extend([foo1], [foo2], C)
extend(X, Y, [1,2,3])
rem_first(N, L, R):- extend(X, L, R), length(X, N).
last(X, L):- extend(_, [X], L).
next(A, B, L):- extend(_, [A,B|_], L).
sublist(R, L):- extend(_, T, L), extend(R, _, T).

46

III.

22

22.1

: , ,

: ,
, .
22.1

1
2
3

remove(X, [X|T], T).


remove(X, [H|T], [H|R]):
remove(X, T, R).

1
2
3
4
5
6
7

: pred remove(T, list(T), list(T),).


: mode remove(in,in,out) is nondet.
: mode remove(out,in,out) is nondet.
: mode remove(in,out,in,) is multi.
remove(X, [X|T], T).
remove(X, [H|T], [H|R])
: remove(X, T, R).

:
( )
1
2
3

? remove(, [, , , ], X)
X = [, , ]
X = [, , ]


1
2
3
4

? remove(X,[, , ], L)
X =[], L =[, ];
X =[], L =[, ];
X =[], L =[, ];
member

member(X, L): remove(X, L, _).

47

III.
22.2
1
2
3

22.2

sublist(L, R:
append(_, T, L),
append(R, _, T).

48

III.
22.3

1
2
3
4

22.3

permute([], []).
permute(L, [X|T]):
remove(X, L, R),
permute(R, T).

1
2
3
4
5
6
7
8
9

:
1
2
3
4

([X], [X]).
([X|L], R) :
(L, R1),
(X, R, R1).

:
:
:
:

pred permute(list(T), list(T)).


mode permute(in, in) is semidet.
mode permute(in, out) is nondet.
mode permute(out, in) is nondet.

permute([], []).
permute(L, [X|T]):
remove(X, L, R).
permute(R, T).

.
, (. 1) SLD-.

49

III.

23

23.1

. . .
38.

(t, )

t ()
( (t, ))
.
39. ,
.
Python ++, PHP. :

.
1

[[101,212],[333,411]]
m

1
2
3
4

[
e(1,1,101), e(1,2,212),
e(2,1,333), e(2,2,411),
]

50

III.
23.1

23.2

1.
2. n-
3.
4.
5.
6.
7.
, . (e(1, \_)):
1
2
3
4
5
6

rem_first([], []).
rem_first([e(1, _)| X1 ], X2 ):
rem_first(X1, X2).
rem_first([e(A1, B1)| X1 ], [e(A2, B2)| X2 ]):
X2 is X1 1,
rem_first(X1, X2).
:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

c2o([], []).
c2o(ListCommon, ListOdering):
c2o_util(ListCommon, 1, ListOdering).
c2o_util([X], N, [e(N, X)]).
c2o_util([X|ListCommon], N, [e(N, X)|ListOdering]):
N1 is N + 1,
c2o_util(ListCommon, N1, ListOdering).

? c2o([blonde, brunette , brown, red], X), write(X), fail.


[e(1,blonde),e(2,brunette),e(3,brown),e(4,red)]
? c2o(X, [e(1,blonde),e(2,brunette),e(3,brown),e(4,red)]), write(X), fail.
[blonde,brunette,brown,red]

51

III.

24

24.1

. . . .
24.1

40. L=< L2 , L1 > < L2 , L1 >


, L1 L2 .
/ -.
L L A/A

L L|A/A
24.2

( ):
. ,
.

. . .
. (> 1) . .
. ( )

24.3

(). , 1.
.

52

III.
24.4

24.2

1
2
3
4

length([X], 1).
length([_|X], N):
length(X, N1),
N is N1 + 1.

1
2
3
4
5

length(L, N): length(L, 0, N).


length([], N, N).
length([X|T], S, N):
S1 is S + 1,
length(T, S1, N).


1
2
3
4

reverse([], []).
reverse([X|T], R):
reverse(T, T1),
extend(T1, [X] R).


1
2
3

reverse(L, R): reverse(L, [], R).


reverse([], R, R).
reverse([X|T], L, R): reverse(T, [X|L], R).


1
2
3
4

%%
reverse(L, R): rev(L, R/[]).
rev([], R/R).
rev([X|T], R/L): reverse(T, R/[X|L]).

53

III.

24.3


1
2
3

extend([], X, X).
extend([X|T], L, [X|R]):
extend(T, L, R).

X Y = (X T ) + (T Y )
1
2
3
4

extend(X/T, Y/T, X/Y).


? extend([1,2|T]/T, [3,4|Y]/Y, R).
R = [1,2,3,4|Y]/Y

54

III.

25

25.1

. , .
41.

(t, )
.
t ().
.
.
.
.
- , .
42. ()

(t, l, r)
.
t ().
l .
r .
43.
x (t, l, r) (x == t) (x l) (x r)
44. , ,

(x, l, r)
.
x .
l . l < x
r . r > x
55

III.

25.2

:
in( X, S ). X S.
1
2
3
4
5

in(X,t(X,_,_)).
in(X, t(_,L,_) ) :
in( X, L) .
in( X, t(_,_,R):
in ( X, R) .
X .

1
2
3
4
5
6
7

in( X, t ( X, _, _) ).
in( X, t( Root, Left, Right) ) :
gt(Root, X), % , X
in( X, Left}. %
in( X, t( Root, Left, Right} ) :
gt( X, Root}, % X ,
in( X, Right). %
:

add( S, X, S1 ). X. S S1.
1
2
3
4
5
6
7
8

addleaf( nil, X, t(X, nil, nil)).


addleaf( t(X, Left, Right), X, t(X, Left, Right)).
addleaf( t(Root, Left, Right), X, t(Root, Left1, Right)):
gt(Root, X),
addleaf( Left, X, Left1).
addleaf( t(Root, Left, Right), X, t(Root, Left, Right1)):
gt(X, Root),
addleaf( Right, X, Right1).

56

III.

25.3

del( S, X, S1). X S
S1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14

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


del( t(X,Left, nil), X, Left ).
del( t(X,nil, Right), X, Right ).
del( t(X,Left, Right), X, t(Y,Left, Right) ):
delmin( Right, Y, Right1 ).
del( t(Root,Left, Right), X, t(Root,Left1, Right) ):
gt(Root, X),
del( Left, Y, Left1 ).
del( t(Root,Left, Right), X, t(Root,Left, Right1) ):
gt(X, Root),
del( Right, Y, Right1 ).
delmin(t(Y, nil, R), Y, R).
delmin(t(Root,Left, Right), Y, t(Root,Left1, Right)):
delmin(Left, Y, Left1).

, , : X D
X . X
:
D > X
D < X
203/205 .
1
2
3
4
5
6
7
8

add(Tree, X, NewTree): addroot(Tree, X, NewTree).


add(t(Y, L, R), X, t(Y, L1, R)): gt(Y, X), add(L, X, L1).
add(t(Y, L, R), X, t(Y, L, R1)): gt(X, Y), add(R, X, R1).
addroot(nil, X, t(X, nil, nil)).
addroot(t(Y, L, R), X, t(X, L1, t(Y, L2, R))):
gt(Y, X), addroot(L, X, t(X, L1, L2)).
addroot(t(Y, L, R), X, t(X, t(Y, L, R1), R2)):
gt(Y, X), addroot(R, X, t(X, R1, R2)).

57

III.

26

26.1

. .
() ,
.
:
AVL-
- .

<?> 1.
45. AVL

(t, l, r)
.
t ().
l AVL-.
r AVL-.
|h(l) h(r)| 6 1, h()
46. h()
h() = 0
h((t, l, r)) = max(h(l), h(r)) + 1
AVL- t(Root, Left, Right)/Height

<?> 2. ?

58

III.

26.2

:
, .
,
.
, , ,
.
O(n), . ,
, O(log2(n)),
. ,
, , .
, .
, . .
, , .
. .

59

III.

26.3

<?> 3. ?

60

III.

27

27.1

. ( , ).
47. hU, V i
U
V U U ( )
.
. ,
, . +
, .
48. G =< U, V > a U b U {xi }ni=0 , ,
xi U,
x0 = a
xn = b
ihXi1 , xi i V
1
2
3
4
5
6
7
8
9

%
door(a, b).
door(b, c). door(b, d).
door(c, d).
door(d, e). door(d, f).
door(f, g).
door(h, i).
%
move(X, Y): door(X, Y), door(Y, X).

27.1

lmax b:
T IM E = O(lmax )
SP ACE = O(blmax )

, . . .
61

III.

1
2
3
4
5
6
7

27.2

search_depth(A,B,P):
depth([A], B, L), reverse(L, P).
depth([X|T], X, [X|T]).
depth(P, F, L):
prolong(P, P1), depth(P1, F, L).
prolong([X| T], [Y, X |T])
move(X, Y), not(member(Y, [X | T])).

27.2

l b:
T IM E = O(bl )
SP ACE = O(bl )
, .

1. ,
2. ( ) .
3. ,
. .
.
, , -
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

search_bdth(X, Y, P):
bdth([[X]], Y, P), reverse(P, L).
bdth([[X|T]|_], X, [X|T]).
bdth([P|QI], X, R):
findall(Z, prolong(P, Z), T),
append(QI, T, QO),!,
%% append(T, QI, QO)
/
==>
.
/
bdth(QO, X, R).
bdth([_|T], Y, L):
bdth(T, Y, L).
62

IV.

IV
28

28.1

. .
-.
28.1


.
.
backtracking.

,
28.2

, ,


,



.

.

<?> 4. ?
63

IV.

29

29.1


, , N 2 1

, , N 2 1.

<?> 5. !

64

IV.

29.2

N N N , ,
.
x y ,
.
x1 , y1 , x2 , y2 , ..., xN , yN , [1..N ] ,
i j :
xi 6= xj ( );
yi 6= yj ( );
|xi xj | 6= |yi yj | ( ).


1
2
3
4
5

queens(N,YsXs) :
range(1,N, Ns),
length(Xs, N), members(Xs,Ns),
length(Ys, N), members(Ys,Ns),
safe(YsXs).
N 2N . N = 8 1014 . ,
x x1 , x2 , ..., xN
1, 2, ..., N . ,
x y. :
(y1,y2,...,yN), 1<yi<N , i j :
yi =/= yj |yi - yj| =/= | i-j |.


1
2
3
4
5

queens(N,Ys) :
range(1,N, Ns),
length(Ys, N),
members(Ys, Ns),
safe(Ys).
NN ( N=8 -107) , , . , y y1,y2,...,yN
65

IV.

29.3

1,2,...,N , .
(y1,y2,...,yn), 1<yi<N, |yi - yj| =/= | i-j | i =/=
j.
, , , yi =/= yj . .
1
2
3
4
5

queens(N,Ys) :
range(1,N, Ns),
length(Ys, N),
selects(Ys, Ns),
safe(Ys).
NN N!, N=8 40 320
. .

1
2
3
4
5
6
7
8

safe([Y|Ys]) : noattack(Y,Ys), safe(Ys).


safe([]).
noattack(Y,Ys) : noattack(Y,Ys,1).
noattack(_,[],_).
noattack(Q,[Y|Ys],D) :
abs(QY) =\= D,
D1 is D+1,
noattack(Q,Ys,D1).
N=8 , N=10 , N=12 - .
- .
. , , .
, .
selects safe place_queens.
Board, .

1
2
3
4
5
6
7

queens(N,Ys) :
range(1,N,Ns),
length(Ys,N),
place_queens(Ys,Ns,[]).
place_queens([Y|Ys], Ns, Board) :
select(Y, Ns, Ns1),
noattack(Y, Board),
66

IV.
8
9

29.4

place_queens(Ys, Ns1, [Y|Board]).


place_queens([],_,_).
20 N=8,
N!0.7.
, place_queens .
select,
, noattack. . , ,
. ,
. [1] /- .
, Us Ds,
, "". . Ys
,
Us Ds. ,
, ( ). place_queens
""Us Ds
.

1
2
3
4
5
6
7
8
9
10

queens(N,Ys) :
range(1,N, Ns),
length(Ys, N),
place_queens(Ns,Ys,_,_).
place_queens([N|Ns], Ys, Us, [D|Ds]):
place1(N, Ys, Us, [D|Ds]),
place_queens(Ns, Ys, [U|Us], Ds).
place_queens([], _, _, _).
place1(N,[N|_], [N|_],[N|_]).
place1(N,[_|Cs],[_|Us], [_|Ds]): place1(N,Cs,Us,Ds).
2-3 , . , , . . ,
, (constraints propagation).
.
- , . - , -
67

IV.

29.5

. " "(forward checking).


( : ).
, [1,2,...N].
,
, .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

queens(N, Queens) :
range(1,N, Ns),
init_vars(Ns, Ns, V),
place_queens(V,Queens).
init_vars([X|Xs],Ys,[X:Ys|V]) :
init_vars(Xs,Ys,V).
init_vars([], _, []).
place_queens([X:Ys|V],[XY|Qs]) :
member(Y, Ys),
prune(V, X, Y, V1),
place_queens(V1,Qs).
place_queens([],[]).
prune([Q:Ys|Qs], X,Y, [Q:Ys1|Ps]) :
sublist(noattacks(X,Y,Q), Ys, Ys1),
Ys1 \== [],
prune(Qs, X,Y, Ps).
prune([], _,_, []).
noattacks(X1,Y1,X2,Y2) :
Y1 \== Y2,
abs(Y2 Y1) =\= abs(X2 X1).
sublist - filter. , . ,

findall(T, (member(T,Ys),noattacks(X,Y,Q,T)) , Ys1)


Ys1 =
[] . , prune . ,
.
, prune. , ,
, , .
, ,
, , . , ,
68

IV.

30.1

. , , . ,
X-Y.
1
2
3
4
5
6

place_queens(V, [XY|Qs]) :
queen_to_place(V,X:Ys,V1),
member(Y, Ys),
prune(V1, X, Y, V2),
place_queens(V2,Qs).
place_queens([], []).
queen_to_place
.

queen_to_place([V|Vs],V,Vs).
. " ", , .

1
2
3
4
5
6
7
8

queen_to_place([V|Vs], Q, Vx) : select_boundest(Vs, V, Q, Vx).


select_boundest([], Q, Q, []).
select_boundest([X|Xs], Q, Qm, [Q|R]) :
shorter(X, Q), !,
select_boundest(Xs, X, Qm, R).
select_boundest([X|Xs], Q, Qm, [X|R]) :
select_boundest(Xs, Q, Qm, R).
shorter(_:L1, _:L2) : length(L1,N1), length(L2,N2), N1<N2.

.
-
. , N . ,
N!, ,
.

30

. . .

69

IV.

31

31.1

. .
.

70

IV.

32

32.1

. .

71

IV.

33

33.1

72

IV.

34

34.1

- .
- .

73

IV.

35

35.1

. . .

74

IV.

36

36.1

. -.
.

75

V
1

<?> 1. Mercury ?
<?> 2. ?
<?> 3. ?

76

1
2
3
4

LATEX :
$\rho \gamma \LaTeX$ :
\begin{python}
print "$\\Delta\\alpha$\n\n"
\end{python}
: Tue Jun 30 11:51:32 2009
: win32 nt

77

8
48

78