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

I

Prolog

...

1. Prolog
2. Prolog
3. , ,
4. :
5.
6.
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( tom, bob).

parent; tom bob


. ,
, , tom, .
, . 1.1,
Prolog:
parent(
parent(
parent(
parent(
parent(
parent(

pam,
tom,
tom,
bob,
bob,
pat,

bob).
bob).
liz).
ann).
pat).
jim).

,
parent. , parent( tom, bob)
parent.
. .

pam

tom

bob

liz

pat

ann

jim

. 1.1.

Prolog
parent, ,
? Prolog, :
?- parent( bob, pat).

, , , Prolog :
yes

:
?- parent( liz, pat).

Prolog :
no

,
. no
?- parent( tom, ben).

.
, . , ?
?- parent( X, liz).

Prolog yes no, X,


. :
X = tom

, , Prolog :
?- parent( bob, X).

. Prolog
:

1. Prolog

27

X = ann

(
), Prolog :
X = pat

, Prolog
no, .
,
? :
X Y, , X Y.

Prolog :
?- parent( X, Y).

Prolog .
, Prolog ( )
. :
X =
Y =
X =
Y =
X =
Y =
...

pam
bob;
tom
bob;
tom
liz;

, <Enter>
.
, ,
, , ,
( ). grandparent,
(. 1.2).
1. ? ,
Y.
2. Y? , X.
X
parent
Y

grandparent

parent
jim

. 1.2. grandparent,

parent

Prolog
:
?- parent( Y, jim), parent( X, Y).

:
X = bob
Y = pat

28

I. Prolog

: X Y, :
parent( Y, jim) parent( X, Y)

,
:
parent( X, Y) parent( Y, jim)

, Prolog, :
?- parent( X, Y), parent( Y, jim).

.
, , :
?- parent( tom, X), parent( X, Y).

Prolog :
X
Y
X
Y

=
=
=
=

bob
ann;
bob
pat

, , . .
1. (X)?
2. ( ) X ?
Prolog :
?- parent( X, ann), parent( X, pat).

:
X = bob

.
 Prolog , parent,
n- , .
 Prolog , .
 Prolog .
.
 ( ) , ( tom ann), ( X Y). , , .
.
 . ,
parent( X, ann), parent( X, pat)

:
X
X .

(goal) ,
Prolog , .
 , , . -

1. Prolog

29

, .
.
 , Prolog , ( ).

1.1. , parent,
(. . 1.1), , Prolog
?
) ?- parent( jim, X).
) ?- parent( X, jim).
) ?- parent( pam, X), parent( X, pat).
) ?- parent( pam, X), parent( X, Y), parent( Y, jim).
1.2. Prolog
parent.
) ?
) ?
) ?

1.2.
.
, parent. , :
female( pam).
male( tom).
male( bob).
female( liz).
female( pat).
female( ann).
male( jim).

male female.
( ). parent
. , ,
.
: . ,
, . :
sex( pam, feminine).
sex( tom, masculine).
sex( bob, masculine).

offspring
(), parent. offspring
, parent,
offspring, ,
, :
offspring( liz, tom).

30

I. Prolog

offspring , ,
parent parent .
:
X Y,
Y X,
X Y.

Prolog. Prolog, , :
offspring( Y, X) :- parent( X, Y).

:
X Y,
X Y,
Y X.

Prolog,
offspring( Y, X) :- parent( X, Y).

. .
,
parent( tom, liz).

,
. , ,
, .
, :
 ( );
 ( ).
, , ,
, , , .
offspring( Y, X)
144424443

:- parent( X, Y).
1442443

parent( X, Y) ,
offspring( Y, X).
Prolog . , :
?- offspring( liz, tom).

,
offspring. ,
X Y; , liz tom. liz tom,
Y liz, X tom.
( X Y), :
X = tom Y = liz

,
:
offspring( liz, tom) :- parent( tom, liz).


parent( tom, liz)

Prolog ,
. :
offspring( liz, tom)

1. Prolog

31

:
parent( tom, liz)

, () , . ,
,
Prolog yes.
. mother
:

X
X
X

X Y,
Y,
Y
- .

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

;
, .
, parent, offspring mother,
, . 1.3. . , .. . ( ) .
.

. , ,
. : , , ,
. . 1.3, grandparent Prolog :
grandparent( X, Z) :- parent( X, Y), parent( Y, Z).
female
X

X
parent

offspring
Y

parent

X
mother

parent

grandparent

parent
Z

. 1.3. , offspring, mother


grandparent

.
Prolog .

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

32

I. Prolog

grandparent( X, Z) :parent( X, Y),


parent( Y, Z).

sister (. 1.4) :
X Y
X Y,
1) X Y
2) X - .

Z
parent

female

parent

Y
sister

. 1.4.
sister

, . 1.4, Prolog
:
sister( X, Y) :- parent( Z, X), parent( Z, Y), female(X).

, X Y
. : Z X, Z Y. , : Z1 X, Z2 Y Z1 Z2.
:
?- sister( ann, pat).

yes, (. . 1.1).
, sister , ,
. ,
, :
?- sister( X, pat).

Prolog , .
X = ann;
X = pat

, ?! -, sister. ,
, Prolog .
, X Y , X Y. , Prolog
( ) , X Y ,
, , ,
.
, , X Y . , , , Prolog different
different( X, Y)

1. Prolog

33

, X Y .
sister :
sister( X, Y) :parent( Z, X),
parent( Z, Y),
female( X),
different( X, Y).

.
 Prolog , .
 Prolog : , .
 ,
.
 ,
.
 , , .
 Prolog .
, .
.
 , .
. () .
 .
.
 , , . , .
,
hasachild( X) :- parent( X, Y).

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

1.3. Prolog.
) , , ( happy
).
) X, X , , X
( hastwochildren).
1.4. grandchild parent. : grandparent (. . 1.3).
1.5. aunt( X, Y) parent sister.
, aunt (), . 1.3.

34

I. Prolog

1.3.

predecessor (). parent. .
() ,
. , X Z,
X Z,
. 1.5. , . 1.1,
.
X
parent

X
predecessor

parent

Z
parent

predecessor

parent

. 1.5. predecessor: ) X
Z; ) X Z

:
X Z,
X - Z,
X - Z.

Prolog :
predecessor( X, Z) :parent( X, Z).

, , ,
. . 1.6. , predecessor
:
predecessor( X, Z) :parent( X, Z).
predecessor( X, Z) :parent( X, Y),
parent( Y, Z).
predecessor( X, Z) :parent( X, Y1),
parent( Y1, Y2),
parent( Y2, Z).
predecessor( X, Z) :parent( X, Y1),

1. Prolog

35

parent( Y1, Y2),


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

Y1

Y1

parent

parent
Y

predecessor
parent

parent
Z

predecessor
Y2

Y2

Y3

predecessor

parent

. 1.6. ,

, ,
. ,
.
predecessor
. ,
. , predecessor . . 1.7 :
X Z,
X - Z,
Y,
1) X - Y
2) Y - Z.

Prolog, , .
predecessor( X, Z) :parent( X, Y),
predecessor( Y, Z).

, predecessor,
: , . , , .
predecessor( X, Z) :parent( X, Z).
predecessor( X, Z) :parent( X, Y),
predecessor( Y, Z).

, predecessor
.

36

I. Prolog

, , , , . .
;
, , . 1.7.
, Prolog
. ,
.
Prolog. Prolog - .
X
parent
Y

predecessor
predecessor

...

. 1.7. predecessor

, Prolog
, . , ?
?- predecessor( pam, X).
X = bob;
X = ann;
X = pat;
X = jim

Prolog, ,
predecessor parent.
: Prolog ?
, Prolog, . ,
. 1.1.
, :
-, , -, , .
1.1. family
parent(
parent(
parent(
parent(
parent(

pam,
tom,
tom,
bob,
bob,

bob).
bob).
liz).
ann).
pat).

1. Prolog

37

parent( pat, jim).


female( pam).
male( tom).
male( bob).
female( liz).
female( ann).
female( pat).
male( jim).

% -
% -

offspring( Y, X) :parent( X, Y).

% Y X,
% X Y

mother( X, Y) :parent( X, Y),


female( X).

% X Y,
% X Y
% X -

grandparent( X, Z) :parent( X, Y),


parent( Y, Z).

% X Z,
% X Y
% Y Z

sister( X, Y) :parent( Z, X),


parent( Z, Y),
female( X),
different( X, Y).

% X Y,

predecessor( X, Z) :parent( X, Z).

% pr1: X - Z

predecessor( X, Z) :parent( X, Y),


predecessor( Y, Z)

% pr2: X - Z

% X Y
% X -
% X Y

, 1.1, :
parent, male, female, predecessor .. , predecessor . ,
predecessor.
, .
.
1.1 , predecessor,
, pr1 pr2, .
.
Prolog.
, .
Prolog /* */. , Prolog
:
/* - */

, , %. , %
, .
% -

1.6. predecessor:
predecessor( X, Z) :parent( X, Z).

38

I. Prolog

predecessor( X, Z) :parent( Y, Z),


predecessor( X, Y).

predecessor ?
, . 1.7,
.

1.4.
Prolog

Prolog. Prolog . , Prolog
. ?
, ,
, . , : ,
, . ,
Prolog ( ), . ,
. Prolog - , , no.
, Prolog : Prolog , , ; Prolog
, .. , .
Prolog . , .
.
- .

:
.

, ,
:
X, X - , X .

Prolog,
.
fallible( X) :- man( X).
man( socrates).
?- fallible( socrates).
yes

%
%
%
%


-
?

, (. 1.1), .
?- predecessor( tom, pat).

, parent( bob, pat).


pr1, ,
predecessor( bob, pat). , ,
, . , , :

1. Prolog

39

parent( bob, pat) ==> predecessor( bob, pat)

: parent( bob, pat)


predecessor( bob, pat), pr1. , , parent( tom, bob).
predecessor( bob, pat), ,
predecessor( tom, pat), pr2. ,
predecessor( tom, pat) .
:
parent( bob, pat) ==> predecessor( bob, pat)
parent( tom, bob) predecessor( bob, pat) ==> predecessor( tom, pat)

, , , , .. , . .
, Prolog .
Prolog , , . , , ,
, , . :
?- predecessor( tom, pat).

Prolog . , . ,
pr1 pr2. ,
predecessor. , .
, pr1 pr2,
Prolog. ,
:
predecessor( X, Z) :- parent( X, Z).

predecessor( tom, pat), :


X = tom, Z = pat

predecessor( tom, pat) :


parent( tom, pat)

,
, . 1.8. , parent( tom, pat), . Prolog
,

predecessor( tom, pat). pr2 :


predecessor( X, Z) :parent( X, Y),
predecessor( Y, Z).

, X Z :
X = tom, Z = pat

Y . predecessor( tom, pat)


:
parent( tom, Y),
predecessor( Y, pat)

40

I. Prolog

. 1.9,
, . 1.8.
predecessor( tom, pat)

pr1

parent( tom, pat)

pr2
parent( tom, Y)
predecessor( Y, pat)

no

. 1.8.
; , , , ,

. 1.9. ,
. 1.8

Prolog
, . , . Y
bob. , ,
predecessor( bob, pat)

pr1. ,
() . Prolog . ,
pr1 :
predecessor( X', Z') :parent( X', Z').

predecessor
( bob, pat), :
X' = bob,
Z' = pat

:
parent( bob, pat)

, . , . 1.10.
(. . 1.10) .
, . () , . ,
( ) -, yes. yes,
. Prolog
, .
Prolog , . , , Prolog
.

1. Prolog

41

. 1.10. predecessor( tom,


pat). ,

1.7. , Prolog
, 1.1.
. 1.81.10.
?
) ?- parent( pam, bob).
) ?- mother( pam, bob).
) ?- grandparent( pam, ann).
) ?- grandparent( bob, jim).

1.5.

, , .
, ,
Prolog,
,

 .
, .
,
. , , , , Prolog.

42

I. Prolog

Prolog
. Prolog
, .
, ( )
. ,

. , , , .
Prolog.
Prolog , C Pascal. , ,
.
,
,
,
.
Prolog , .

 Prolog
.
 , : ,
.

n- , , , .
 ,
.
 , ,
. Prolog , .
 Prolog , ,
,
, , , . Prolog ,
, .
 Prolog: .
. .
 :
, , , ;
, ;
, ;
;

1. Prolog

43

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


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

44

I. Prolog