Você está na página 1de 46

BOOKLET

&

:

gtzortzi@cs.uoi.gr vchasani@cs.uoi.gr
:

ENOTHTA 1

1.1 Prolog
Prolog
. Horn
.

Prolog, .

. " george
(father) mary", Prolog :
father(george,mary).

. (parent), "
" "
" Prolog :
parent(X,):-father(,).
parent(X,):-mother(X,).
, . ":-"
.

1.2 Prolog
Prolog (interpreted).
, (queries),
. yes/no (/),
( ) .
,
";"

, Prolog, :

? ;

1.3
Prolog
.

george mary
george nick
peter marina
helen mary
H helen nick
ann marina

father(george,mary).
father(george,nick).
father(peter,marina).
mother(helen,mary).
mother(helen,nick).
mother(ann,marina).
parent(,):-father(,).
parent(,):-mother(,).


"" Prolog, .
, ,
:
george mary;
?- father(george,mary).
yes
() peter;
?- father(peter,).
X=marina
() george;
?- parent(george,).
X=mary;
X=nick;
.
X=mary ";" =nick.
, ;
?- mother(,).
X=helen,
Y=maria;
X=helen,
Y=nick;
X=ann,
Y=marina;
no
3


Prolog :
1. george marina;
?2. () marina;
?3. () paul;
?4. nick;
?5. Z, W o Z W;
?-

ENOTHTA 2 Prolog

2.1
. :
-
-
-
-
Prolog "%"
"/*" "*/".
(Terms)
(terms) Prolog
. ,
.

2.2
(simple terms) , .
, .
: 2, 3, 6.7, -3, -10 .
,
.
:

anna
x25
x_25
x_
x__3a
Tom

Anna
25x
25
_x
_xY3a
Tom

Prolog
"_".
"_".

Result
Object2
ListOfStrings
_a23

x
result
2object
listOfStrings
a_23
5

"_" .

. peter
, :
?- father(peter,_ ).

2.3 Prolog

Prolog.

. Prolog,
. = + 1,
Prolog.
Prolog, (typeless)
. ,
.

2.4
(compound terms), f(k1, k2,...,
kn). To f (functor) n k1, k2,..., kn
. .
( , ) .
:
- :
date(2,august,1997).
3 (, , ).
, .
- :
triangle(point(4,2),point(6,4),point(7,1)).
3.

.
:

2.5
horn (facts). ""
,
,
Prolog :
belongs_to(greece,europe).
belongs_to(italy,europe).
belongs_to(england,europe).
, belongs_to(greece,europe) (relationship)
belongs_to greece,europe.
, (predicate),
(arguments),
. (,
, ) . .

student(john,class(a),grade(b)). 3 student.

.

room(48,top_floor).

2, room. To
, .

day_sunny.


day_sunny.

. ,
, (atomic formula).
:

,
. .
,
.

2.6
(rules)
. :
:-1,2,3,...,Bn.

(n >= 1).

(head) .
. H ":-",
"". 1,2,...n (body)
.
"," (), .
:
( ) () .
:
brothers(,):-parent(,),parent(,).
(shared variable) .

2.7

(procedure). Prolog /
.
, ,
:
connected(a,b).
connected(b,c).
connected(X,Y):-connected(X,),connected(,Y).

To (definition)
"/" . ,
connected/2.

2.8
Prolog :
(declaratively) (proceduraly). , :-Q,R.
: : Q R
. : ,
Q R .

Prolog
: parent (.. parent(antonis,helen)), male (..
male(kostas)), female (.. female(anna)).

:
o
o
o
o
o
o
o
o

, father(X,Y), / .
, mother(X,Y), / .
, grandfather(X,Y), / .
, ancestor(X,Y), Y.
, sister(X,Y), / .
, aunt(X,Y), / .
, nephew(X,Y), X / Y.
married(X,Y), Y.

father(X,Y):-male(X),parent(X,Y).
mother(X,Y):-female(X),parent(X,Y).
grandfather(X,Y):-father(X,Z),parent(Z,Y).
ancestor(X,Y):-parent(X,Y).
ancestor(X,Y):-parent(X,Z),ancestor(Z,Y).
sibling(X,Y):-father(Z,X),father(Z,Y),X\=Y. %* *%
sister(X,Y):-sibling(X,Y),female(X).
aunt(X,Y):-sister(X,Z),parent(Z,Y).
nephew(X,Y):-male(X),parent(Z,X),sibling(Z,Y).
married(X,Y):-father(X,Z),mother(Y,Z).

antonis

katerina

george

father

mother

father

mother

helen

kostas

anna

dimitris

mother

father

mother

father

nick

mary2

mary

manolis
father

theano
mother

katerina2
manolis2

father

mother

dimitris2

1. Prolog

:
parent(antonis,helen).
male(kostas).
female(anna).

2. :

o
o
o
o
o
o
o
o
o

, father(X,Y), / .
, mother(X,Y), / .
, grandfather(X,Y), / .
, ancestor(X,Y), Y.
, sister(X,Y), / .
, brother(X,Y), X / .
, aunt(X,Y), / .
, nephew(X,Y), X / Y.
married(X,Y), Y.

10

ENOTHTA 3

3.1
Prolog
. : "?- ".
(call).
, :
. .
Prolog ,
.
() ,
.
, , (no).
.

:
big(bear).
big(elephant).
small(cat).
brown(bear).
gray(elephant).
black(cat).
:
?-brown(elephant).
no
,
.
?-big(elephant).
yes
.
?-big(X).
X=bear; X=elephant
bear .
, elephant .
.
(",") AND.

.
11

:
?-small(),gray().
no
,
cat. ,
. , gray
(cat). ,
.
:
?-big(X),gray(X).
X=elephant
?-big(X),black(Y).
X=bear Y=cat; X=elephant Y=cat

3.2 prolog
Prolog
.
.
, ,
, . ,
Prolog friend(,),
. , :
:
?- friend(X,nick).
:
?- friend(X,nick),friend(X,john).
3.2.1
H Prolog :
1.
(yes)
.
2. , , .
,

. H
.
.

12

3.2.2
,
, Prolog

.
,
, Prolog,
(most general unifier).
,
.
,
,

.

,

""
.
,


.
,
(fail).
(yes)
.
, .

.
(unification algorithm). :
- ,
.
- .
-

.
:

13

:
ge(,17) age(tom,17)
X=tom
age(,17) - age(tom,18)

ge(,17) age1(,17)

a(X,X) a(bob,)
X=Y=bob
a(X,X) a(bob,tom)
. .
a(f(1,),3)) a(f(,k),)
Y=1, X=k, =3
a(3,) a(Y,f(2,))
=3, X=f(2,3)
a(f(1,2),3) a(f(,2,3),)

.
3.2.3
:
greek(socrates).
human(turing).
human(socrates).
fallible():-human().
:
?- fallible(),greek(Y).
:
-
?-fallible().
14

4 . ?-fallible()

{=}. :
?-human(),greek().
2,
{Y=turing}.
human(Y) 3 . H
:
?-greek(turing).
H .

{Y=turing}. H
:
?-human(),greek(Y).
H 3. H
{Y=socrates} :
?-greek(socrates).
1
.
: Y=scrates.

(computation tree).

.

.
( )
.
.
.

15

,
(
). .

married(kiriakos,katerina).
child_of(eleni,kiriakos).
child_of(kostas,katerina).
parent(X,Y):-child_of(Y,X).
parent(X,Y):-child_of(Y,Z),are_married(Z,X).
are_married(X,Y):-married(X,Y).
are_married(X,Y):-married(Y,X).
() Prolog :
parent(Z,eleni),parent(Z,kostas).

16

married(kiriakos,katerina).
child_of(eleni,kiriakos).
child_of(kostas,katerina).
parent(X,Y):-child_of(Y,X).
parent(X,Y):-child_of(Y,Z),are_married(Z,X).
are_married(X,Y):-married(X,Y).
are_married(X,Y):-married(Y,X).
() Prolog
( ):
parent(Z,eleni),parent(Z,kostas).

17

ENOTHTA 4

4.1
(terms) Prolog
. , , (single)
(compound). , , .
, ,
. :
f(kl,k2,, kn). , .

, (records) Pascal, (structs)
C . .

4.2
Prolog
. ,
. ,
country :
, , .
country(Name,Population,Area,Language).
country
, ,
.
, :
student(Aem,name(f_name(F),l_name(L),father_name(Fa)),address(stre
et(S),number(N),city(C)),telephone(Tel),year_of_studies(No)).
:
student(84,name(f_name(george),l_name(nikolaou),father_name(nikos)
),address(street(ermou),number(60),city(thessaloniki)),telephone(2
23456), year_of_studies(4)).
student(89,name(f_name(nik),l_name(georgiou),father_name(petros)),
address(street(egnatia),number(210),city(thessaloniki)),telephone(
289478),year_of_studies(4)).
Prolog
.
Prolog
. ,
4 , :

18

?-student(,_,_,_,year_of_studies(4)).
=84;
=89;
no
:
?-student(,_,address(street(ermou),_,_),_,_)
=89;
=148;
no

4.3

, , . (x,y)
point(x,y). ,
:
triangle(point(4,2),point(6,4),point(7,l)).
.

4.4

.
Prolog . :
seq(rl,r2).
par(rl,r2).
par(rl,par(r2,r3)).
par(rl,seq(par(rl,r3),r4)).
seq per
.

19

4.5
.
"." ,
. ,
nil.
:
.(a,.(b,.(c,nil))): a, b c.
.(1,.(2,.(3,.(4,nil)))): .

()

fallen

evanescence

rock

2003

16

24

reload

metallica

metal

1997

13

25

no_need_to_argue

cranberries

rock

1999

18

24

demon_days

gorillaz

hip_hop

2005

16

24

master_of_puppets

metallica

metal

1990

17

24

war

u2

pop

1990

15

28

) , cd/4
.
) :

20

1. album_year(Album,Year), Album
Year.
:
?- album_year(X,2003).
X=album(fallen,evanescence)
2. album_category(Album,Category), Album
Category.
3. recent_album(Title,Year), Title
Year .
4. buy_only_used(Album,Amount), ( ) Amount
2 ( ) Album.
(: >=, =< .
.. =< X Y).
:
?- buy_only_used(X,25).
X=album(reload,metallica);
X=album(war,u2);
no

21

ENOTHTA 5

5.1
(recursion) Prolog.

. ,
.

.
,
.
: parent(X,),
( ) .
parent(john,george).
parent(john,nick).
parent(jim,bill).
parent(jim,jack).
parent(gregory,john).
parent(gregory,jim).
parent(bob,gregory).
parent(joseph,bob).
predecessor(,),
(, , - .) .

5.2
predecessor :
predecessor(,):-parent(,).
predecessor(,):-parent(,),parent(,).
predecessor(,):-parent(,1),parent(Yl,2),parent(2,).

22

, -...-- . ,
.
george,
:
?-predecessor(,george).
:
X=john;
X=gregory;
X=bob
X=joseph,
. :
predecessor(,):parent(,1),parent(Yl,Y2),parent(2,3),parent(3,).
( ,
parent(tom,joseph) parent).

5.3
predecessor
. :
, :
) ,
) .

23

,
predecessor .
.
predecessor:
predecessor(,):-parent(,).
predecessor(,):-parent(,),predecessor(,).

george, :
?-predecessor(,george).
:
=john;
X=gregory;
X=bob;
X=joseph
,
parent.
, .
, ,
. ,
predecessor, ,
.

24


:
next_to(a,b).
next_to(b,c).
next_to(d,e).
next_to(e,f).
,
:
next(X,Y) :- next_to(X,Y).
next(X,Y) :- next_to(Y,X).
same_side/2,
.
1 ()
.
.
same_side_to(X,Y) :- next_to(X,Y).
same_side_to(X,Y) :- next_to(X,Z), same_side_to(Z,Y).
.
same_side_to/2.
.
same_side(X,Y) :- same_side_to(X,Y).
same_side(X,Y) :- same_side_to(Y,X).
2 ()
same_side(X,Y) :- next(X,Y).
same_side(X,Y) :- next(X,Z), same_side(Z,Y).
.

, 'no' Prolog
( ).
:
?- same_side(a,d).
:
1) 1 : ?2) 2 : ?3) (','): ?4) 1 : ?5) 2 : ?6) (','): ?7) 1 : ?8) 2 : ?-

next(a,d) -> no
next(a,Z) -> Z = b
same_side(b,d)
next(b,d) -> no
next(b,Z) -> Z = c
same_side(c,d)
next(c,d) -> no
next(c,Z) -> Z = b
25

9) (','): ?- same_side(b,d)
(9) same_side(b,d) (3).
(3) (9) ' ( ).

M .
Cowley
Petherton
Hull

Kingswear

Mansfield

Leamington
Stockton
Romsey

Cork

Exeter

1.

/ Prolog.

road(X,Y), X Y .
.

direct_flight(X,Y), X Y
. .

2.

( ):

can_go_by_car(X,Y),
.

can_fly(X,Y),
X Y, .

can_travel(X,Y),
, ,
.

26

ENOTHTA 6

6.1
(lists) ,
Prolog.
. .
: , .
"[", "]"
",". , Prolog :
[dog,3,cat,5,elephant,ann,12,torn]
:
- ( ) []
- : (head)
(tail) , [H|T] ( H
T ).
[dog,3,cat,5,elephant,ann,12,torn] dog
[3,cat,5,elephant,ann,12,torn].
: .
, .
, :
[dog|[3,cat,5,elephant,ann,12,torn]]
[dog|[3|[cat,5,elephant,ann,12,torn]]]
[dog|[3|[cat|[5|[elephant|[ann|[12|[torn|[]]]]]]]]]

( )
.
"." (dot). :
.(,)
, [dog,3,cat,5] :
.(dog,.(3,.(cat,.(5,[]))))

6.2 ,
, ( )
.
. [dog,3,cat,5]
:

27

[a,b,c]
[1|]
[1,]
[[a,b],c]
[1,2|]
[,]
[X|]
[f(a,b),[a]]

a
1
1
[a,b]
1

f(a,b)

[b,c]

[]
[c]
[2|]
[]

[[a]]


.
.
, .
1
[a,b,c]
[a,b,c]
[a,b]
[a,b]
[f(1),k,[1]]
[f(1),k,[1]]
[[a]]

2
[a,b,c,d]
[,]
[a,]
[a|X]
[f(),,]
[f(),|]
[X|]

{X=b}
{X=[b]}
{X=l,Y=k,Z=[l]}
{X=l,Y=k,Z=[[1]]}
{X=[a],Y=[]}


. :
-
-
-
-
-
-
.
,
.
Prolog
, .
28

,
.

6.3
,
:
:
) , ) .
last . ,
. .
( / ),

, .
last(X,[]).
last(,[Head|Tail]):-last(,Tail).
:
?-last(3,[]).
no
?-last(2,[l,a2]).
no
?-last(X,[2,a,5,abc,7]).
X=7
?-last(c,[a,b,c]).
yes
:

29

6.4

, :
:
) ( ) ) .
member
, . .
( / ),
,,
.
member(,[|]).
member(,[Head|Tail]):-member(,Tail).
:
?-member(2,[2,3,4]).
yes
?-member(2,[c2,k4,g6]).
no
?-member(4,[1,3,4,5]).
yes
?-member(X,[a,b,c]).
X=a;
X=b;
X=c
:

30

ENOTHTA 7
,
:
) L L.
: append([],L,L).
) , : [|L1]. ,
[|L1] L2 [|L3] L3
L1 L2 (. ).

append .
. .
( ),
,
.
append([],L,L).
append([|L1],L2,[X|L3]):-append(L1,L2,L3).
:
?-append([a,b],[c,d],[a,b,c,d]).
yes
?-append([a,b,c],[1,2,3],L).
L=[a,b,c,1,2,3]
?-append([a,[b,c],d],[a,[],b],L).
L=[a,[b,c],d,a,[],b]
?-append(L1,L2,[a,b,c]).
L1=[]
L2=[a,b,c];
L1=[a]
L2=[b,c] ;
Ll=[a,b]
L2=[c];
Ll=[a,b,c]
L2=[];
no
31


1. first(X,L),
X L.
:
?- first(a,[a(3,5,6,6),3,2,9]).
no
?- first(2,[2,6,3,2,9]).
yes
?- first(X,[g,1,a,2]).
X=g
2. penult(X,L),
X L.
:
?- penult(6,[a(3,5,6,6),3,2,9]).
no
?- penult(2,[2,6,3,2,9]).
yes
?- penult(X,[g,1,a,2]).
X=a
3. subset(L1,L2),
L1 L2, L1
L2.
:
?subset([a,b,3,f(1)],[a,b(3,5,6,6),3,2,9,b,2,3,f(1),g]).
yes

32

?- subset([a,b,3,g],[a,b,3,g]).
yes
?- subset([a,b,3,g,2],[a,b,3,g]).
no
4. third(X,L),
X L.

33

ENOTHTA 8

8.1

:
.

( ):
add(List,Element,[Element|List]).
, List , Element
. :
?-add([a,b,c,d],q,L).
L=[q,a,b,c,d]
( )
, .
(. ):
1) List List1 List2
append/3.
2) Element
[Element|List2].
3) List1 [Element|List2]
NewList.

34

add. List ,
Element NewList .
add(List,Element,NewList):append(Listl,List2,List),append(Listl,[Element|List2],NewList).
:
add (
).
(:
, add,
).
:
?-add([1,2,3],7, [1,2,7,3]) .
yes
?-add([l,2,3],6,[1,2,3,4]).
no
?-add([a,b,c],1,L).
L=[l,a,b,c];
L=[a,l,b,c];
L=[a,b,l,c];
L=[a,b,c,1];
no
, add
append/3 :
) Element List
)
Element .
add(List,Element,[Element|List]).
add([Head|List1],Element,[Head|List2]):-add(List1,Element,List2).

8.2

:
1: List , append/3,
Element .
2: Element.
3: .

35

delete .
. .
.
.
delete(Element,List,NewList):append(Listl,[Element|List2],List),append(Listl,List2,NewList).
:
delete ,
.
.
(:
,
append).
:
?-delete(a,[a,b,c,d],L).
L=[b,c,d]
?-delete(b,[a,b,c],L).
L=[a,c]
?-delete(1,[2,1,3,4,1,1],L)
L=[2,3,4,1,1];
L=[2,1,3,4,1];
L=[2,1,3,4,1];
no
, delete
append/3, :
) Element

)
Element
36

delete([Element|List],Element,List).
delete([Head|List1],Element,[Head|List2]):delete(List1,Element,List2).

1. equals(L1,L2),
L1 L2 (
).
:
?- equals([a,b,3,g],[a,b,3,g]).
yes
?- equals([a,b,3,g],[b,3,g,a]).
no

2. replace(X,Y,List,Result),
X Y
List Result.
3. replace_all(X,Y,List,Result),
X Y List,
,
Result.

37

ENOTHTA 9
,
, .
'
,
.
1) , .
2) .
3) .
4)
append/3 (. ).

reverse/2 .
, 2 4
. ,
.
reverse([],[]).
reverse([Head|Tail],ReversedList):reverse(Tail,ReversedTail),append(ReversedTail,[Head],ReversedList
).
:
?-reverse([1,2,3,4],[4,3,1,2]).
no
?-reverse([1,2,3,4],[4,3,2,1]).
yes
?-reverse([a,b,c],L).
L=[c,b,a]

38

:
reverse
reverse append.
'
,
. .
, . ,
. :
reverse(List,ReversedList):-rev(List,ReversedList,[]).
rev([],List,List).
rev([Head|Tail],ReversedList,TempList):rev(Tail,ReversedList,[Head|TempList]).
[1,2,3] :
List
[1,2,3]
[2,3]
[3]
[]

TempList
[]
[1]
[2,1]
[3,2,1]

1. same_elements(L1,L2),
L1 L2 .
2. equal_size(L1,L2),
L1 L2 .

39

ENOTHTA 10

10.1
, Prolog
.
:
)
)
) is

10.2
Prolog :

>
<
>=
=<
=: =
=/ =

To
To
To
To


.
:
= =:= . , = ()
,
. , , . , =:=
.
.
?- 1+2 =:= 2+1.
yes
?- 1+2 = 2+1.
no
?- 1+ = +2.
=2
=1
,

40

1:
, sorted/1
, .
:
) ,
) :
1)
2)
2 sorted.
sorted:
sorted([]).
sorted([_]).
sorted([,|List]):-=<,sorted([Y|List]).
2:
, list_max/2
. :
) ,
) :
1)
2)

1 list_max.
,
max/3.
.
list_max max.
list_max([],).
list_max([X|Tail],):-list_max(Tail,M1),max(,M1,).
max(,,):->.
max(,,):-=<.

10.3
Prolog .
:

+
*
/
mod
^

41

10.4

=. ,
3+4, :
?- X=3+4
Prolog :
=3+4
=7 , = . ,
Prolog 3+4
+ 3 4.
, is.
: is
. :
?- is 3+4.
=7
?- 7 is 3+4.
yes
:

is.
is, Prolog
.
is
.
1:
, gcd/3
() .
:
) ,
) ,
. .
gcd . gcd.
gcd(X,X,X).
gcd(X,Y,D):-<,Y1 is Y-X,gcd(X,Y1,D).
gcd(X,Y,D):->=,X1 is X-,gcd(X1,Y,D).
:
?- gcd(4,12,D).
D=4

42

2:
, sum_list/2

. :
) , .
) , ,
.
, sum_list .
sum_list.
sum_list([],0).
sum_list([|],Sum):-sum_list(T,Suml),Sum is Suml+.
:
?- sum_list([1,2,3,4,5],).
=15
3:
, n_elem/3, n_elem(,L,),
- L.
:
) =1 , .
) >=2 1=-1, 1-
.
, n_elem .
.
n_elem(1,[|],).
n_elem(N,[|],):-1 is -1,n_elem(N1,T,X).
:
?- n_elem(3,[a,b,c,d,e],X).
X=c
:
n_elem,
. :
?- n_elem(N,[a,b,c,d,e],c).
: no
N1 is -1 : 1 .
,
:
n_elem(1,[|],).
n_elem(N,[|],):-n_elem(1,,), is 1+1.
:
?- n_elem(N,[a,b,c,d,e],c).
: =3
43

:
, is 1+1
Nl is -1 '
.

1. between(N1,N2,X),
[1,2] .
between(N,N,[N]).
between(N1,N2,[N1|T]):-N1<N2,NewN1 is N1+1,between(NewN1,N2,T).
2. length .
length([],0).
length([H|T],X):-length(T,X1),X is X1+1.

1. split(Number,List,Smaller,Larger),
List Smaller Larger,
() Number.
:
?- split(5,[6,3,2,9,5,10,11,1],Smaller,Larger).
Smaller=[3,2,1]
Larger=[6,9,5,10,11]
?- split(10,[6,3,2,9,5],Smaller,Larger).
Smaller=[6,3,2,9,5]
Larger=[]
2. o occurs(X,L,N) X
N L.
:
?- occurs(X,[1,2,1,4,5],2).
X=1
?- occurs(3,[3,5,2,4,3],N).
N=2

44

3. list_prod 2
( = ).
4. addlist 2
( = ).
5. o .
(.. ), .
.
katigorima(_,[],0).
katigorima(X,[Head|Tail],N):-Head=X,katigorima(X,Tail,N1),N
N1+1.
katigorima(X,[Head|Tail],N):-Head\=X,katigorima(X,Tail,N).

is

6. product/2
(..
[1,2,3] 1*2*3, 15).

45

1. ., ., . , ., . ,
, ' , . , 2006.

2. Prolog,
http://aibook.csd.auth.gr/download.asp?url=http://lpis.csd.auth.gr/prolog/Prolog_multi/prolo
g_main_guide.html

46

Você também pode gostar