Tải bản đầy đủ (.pdf) (37 trang)

trí tuệ nhân tạo cao hoàng trứ chương ter prolog sinhvienzone com

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (239.96 KB, 37 trang )

ne

.C

om

Ho Chi Minh City University of Technology
Faculty of Computer Science and Engineering

nh
Vi
en

Zo

INTRODUCTION TO PROLOG
Huỳnh Tấn Đạt

Si

Email:
Home Page: />
SinhVienZone.com

/>

References

Si

nh


Vi
en

Zo

ne

.C

om

[1]. Ivan Bratko (1990), Prolog Programming For Artificial
Intelligence, Addition-Westley.
[2]. Nguyễn Thanh Tùng, Nguyễn Thị Trúc Viên (2007),
Thực Hành Ngôn Ngữ Lập Trình, (my web site).
[3]. B-Prolog – User‟s Guide.

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 2


Prolog

om

 PROgramming in LOGic


.C

 Alain Colmerauer & Philippe Roussel, 1972

ne

 A declarative programming language to describe

Si

nh
Vi
en

Zo

problems

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 3


Prolog

nh

Vi
en

Zo

ne

.C

om

 Irrefutable reasoning processes (Aristotle):
– Socrates is a man
man(socrates).
– All men are mortal
mortal(X) :- man(X).
=> Socrates is mortal
robber(jerry).
childof(tom, john).
rich(john).
rich(X) :- childof(X, Y), rich(Y).
rich(X) :- robber(X).

Si

– Jerry is a robber
– Tom is a child of John
– John is rich
– X is rich if X‟s father is rich or X is a robber
SinhVienZone.com

Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 4


Predicate
Predicate

om

Prolog program

ne

.C

Predicate


Clause

nh
Vi
en



Zo


Predicate

Clause

Predicate

Si

Predicate

 A predicate always returns true or false
SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 5


Clause

Zo

ne

.C

om


 Two types of clause:
– Fact
– Rule
 Clauses are separated by „.‟ character.

Si

nh
Vi
en

robber(jerry).
childof(tom, john).
rich(john).
rich(X) :- childof(X, Y), rich(Y).
rich(X) :- rober(X).

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

Facts
Rules

/>
Slide 6


sub-clauses


ne

.C

rich(X) :- childof(X, Y), rich(Y).
rich(X) :- rober(X).

om

Rule

nh
Vi
en

Zo

Predicate name and parameters

 A rule returns true if all sub-clauses return true

Si

 The clauses of a predicate should be consecutive.

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>

Slide 7


Clause

Zo

ne

.C

om

 Literals, or atoms, start with lower-case letters, or
enclose in quotes „ „
 Whereas variables start with Upper-case letters.
man(socrates).
mortal(X) :- man(X).

Si

nh
Vi
en

 The arguments in sub-clauses must be variables, not
expressions.
 Example: factorial(N – 1, R1). % Wrong!!!
factorial(0, 1).
factorial(N, R1) :- N1 is N – 1, factorial(N1, R1),

R is R1 * N.
SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 8


How Prolog answers questions?

Si

nh
Vi
en

Zo

ne

.C

om

 A question to Prolog is always a sequence of one or
more goals
 Prolog tries to satisfy all the goals by matching clauses
 Create the associations (unifications) between
arguments in the question and parameters declared in

the head of clauses
 Execute the sub-clauses of the current clauses
 If all sub-clauses are satisfied, the variables in question
will be bound to values => We have got a solution

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 9


.C
ne

nh
Vi
en

Zo

 Example:
man(socrates).
man(xeda).
king(xeda).
happy(X) :- man(X), king(X).

om


How Prolog answers questions?

Si

?- happy(xeda)
?- happy(Y)

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 10


.C
ne

nh
Vi
en

Zo

 Example:
man(socrates).
man(xeda).
king(xeda).
happy(X) :- man(X), king(X).


om

How Prolog answers questions?

Unify( happy(xeda), happy(X) )

Si

?- happy(xeda)

=> X = xeda

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 11


.C
ne

nh
Vi
en

Zo

 Example:

man(socrates).
man(xeda).
king(xeda).
happy(X) :- man(X), king(X).

om

How Prolog answers questions?

Unify( man(scocrates), man(X) )
=> fail!

Si

?- happy(xeda)

X = xeda

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 12


.C
ne

nh

Vi
en

Zo

 Example:
man(socrates).
man(xeda).
king(xeda).
happy(X) :- man(X), king(X).

om

How Prolog answers questions?

Unify( man(xeda), man(X) )
=> successful

Si

?- happy(xeda)

X = xeda

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 13



.C
ne

nh
Vi
en

Zo

 Example:
man(socrates).
man(xeda).
king(xeda).
happy(X) :- man(X), king(X).

om

How Prolog answers questions?

Unify( king(xeda), king(X) )
=> successful

Si

?- happy(xeda)

X = xeda


SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 14


.C
ne

Si

?- happy(xeda)
yes

nh
Vi
en

Zo

 Example:
man(socrates).
man(xeda).
king(xeda).
happy(X) :- man(X), king(X).

om


How Prolog answers questions?

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 15


.C
ne

Si

?- happy(Y)

nh
Vi
en

Zo

 Example:
man(socrates).
man(xeda).
king(xeda).
happy(X) :- man(X), king(X).

om


How Prolog answers questions?

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 16


.C
ne

nh
Vi
en

Zo

 Example:
man(socrates).
man(xeda).
king(xeda).
happy(X) :- man(X), king(X).

om

How Prolog answers questions?


Unify( happy(Y), happy(X) )

Si

?- happy(Y)

=> X = Y

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 17


.C
ne

nh
Vi
en

Zo

 Example:
man(socrates).
man(xeda).
king(xeda).
happy(X) :- man(X), king(X).


om

How Prolog answers questions?

?- happy(Y)

X = Y = socrates

Si

Unify( man(scocrates), man(X) )
=> successful

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 18


.C
ne

nh
Vi
en

Zo


 Example:
man(socrates).
man(xeda).
king(xeda).
happy(X) :- man(X), king(X).

om

How Prolog answers questions?

X = Y = socrates

Si

Unify( king(xeda), man(X) )
=> Fail
=> Backtrack!
?- happy(Y)
SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 19


.C
ne


nh
Vi
en

Zo

 Example:
man(socrates).
man(xeda).
king(xeda).
happy(X) :- man(X), king(X).

om

How Prolog answers questions?

Si

Backtrack!
X = Y = socrates
Release the bound value of X => X = Y

?- happy(Y)
SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 20



.C
ne

nh
Vi
en

Zo

 Example:
man(socrates).
man(xeda).
king(xeda).
happy(X) :- man(X), king(X).

om

How Prolog answers questions?

X = Y = xeda

Si

Unify( man(xeda), man(X) )

?- happy(Y)
SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT


/>
Slide 21


.C
ne

nh
Vi
en

Zo

 Example:
man(socrates).
man(xeda).
king(xeda).
happy(X) :- man(X), king(X).

om

How Prolog answers questions?

?- happy(Y)

X = Y = xeda

Si


Unify( king(xeda), king(X) )
=> successful

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 22


.C
ne
X = Y = xeda

Si

?- happy(Y)
Y = xeda

nh
Vi
en

Zo

 Example:
man(socrates).
man(xeda).
king(xeda).

happy(X) :- man(X), king(X).

om

How Prolog answers questions?

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 23


Controlling backtracking

?- p(X)

Si

p(X) :- q(X).
p(3).
q(1).
q(2).

nh
Vi
en

Zo


ne

.C

om

 Prolog automatically backtrack if this is necessary for
satisfying a goal
 The results may be different if we change the order of
clauses of a predicate.
 Example:

SinhVienZone.com
Faculty of Computer
Science and Engineering – HCMUT

/>
Slide 24


Controlling backtracking

p(X) :- q(X), !.
p(3).
q(2).
q(1).

Si


nh
Vi
en

p(X) :- q(X), !.
p(3).
q(1).
q(2).

Zo

ne

.C

om

 Uncontrolled backtracking may cause inefficiency in a
program.
 Cut mechanism is used to prevent the backtracking of
prolog at the point indicated by „CUT‟

p(3).
p(X) :- q(X), !.
q(1).
q(2).

?- p(X)
SinhVienZone.com
Faculty of Computer

Science and Engineering – HCMUT

/>
Slide 25


×