Program PROLOG

advertisement
PENGENALAN PROLOG
OVERVIEW








APA ITU PROLOG ?
PROLOG vs PEMROGRAMAN KONVENTIONAL
PROGRAM PROLOG
QUERIES
AMZI! PROLOG
FUNGSI DAN PREDIKAT
KESIMPULAN
KASUS
APA ITU PROLOG ?
PROLOG singkatan dari
“ Programmation en Logique “,
dalam bahasa French
“ Programming in Logic “,
dalam bahasa English
PROLOG adalah sistem pemrograman dimana
bahasa logika digunakan sebagai bahasa
pemrograman
APA ITU PROLOG?
Pada dasarnya sistem bekerja sebagai
berikut :
 User menginputkan deskripsi masalah
yang dituliskan di bahasa PROLOG.
 PROLOG menterjemahkan kemudian
melakukan penelusuran secara logika
untuk menemukan jawaban dari masalah
tersebut.
APA ITU PROLOG?
PROLOG dapat digunakan sebagai:
 Software Tool untuk pengembangan
kecerdasan sistem (seperti sistem pakar
dan sistem robot kontrol);
 Secara sederhana sebagai bahasa
pemrograman umum dengan mekanisme
yang kuat untuk pemecahan masalah.
APA ITU PROLOG?
Diperkenalkan pada tahun 1973,
Oleh Alain Colmerauer dan timnya
d’Intelligence Artificielle de l’Université
d’Aix-Marseille
Purposed of Translating Natural Languages
PROLOG
Pada pemrograman konvensional :
 Programmer menginstruksi mesin
BAGAIMANA cara memecahkan
masalah dengan menampilkan
urutan aksi-aksi
 Mesin membawa instruksi-instruksi
yang spesifik
PROLOG VS CONVENTIONAL
Pada pemrograman logika:
 Programmer mendefinisikan APA
masalahnya, dengan menggunakan
bahasa logika;
 Sistem menjalankan deduksi logika
untuk menemukan jawaban dari
masalah
PROLOG VS CONVENTIONAL
PROLOG adalah spesifikasi masalah
yang dapat dijalankan.
Dengan alasan tersebut maka
PROLOG disebut sebagai VeryHigh-Level programming.
Lev el 5
P ro b lem -o rien te d la n g ua g e le v e l
T ra n s la tion (c o m p iler)
Lev el 4
A s s e m b ly la n g ua g e le v e l
T ra n s la tion (a s s e m b le r)
Lev el 3
O pe ra ting S y s te m M a c hin e le v e l
P a rtia l In te rp reta tio n
(O pe ra ting S y s te m )
Lev el 2
C on v e ntio n al M a c h in e le v el
In te rpre ta tion (m ic ro p ro gra m )
Lev el 1
M ic ro p ro gra m m in g lev e l
M ic ro p ro gra m s are d irec tly
e x ec u te d b y th e h a rd w a re
Lev el 0
D ig ita l L o g ic le v e l
PROLOG PROGRAMS
Secara umum, PROLOG adalah
deskripsi dari suatu dunia (yaitu
kumpulan dari objek yang saling
terhubung) yaang dituliskan dengan
bahasa PROLOG.
PROLOG PROGRAMS
Contoh 1. Dunia dari Ann & Sue dideskripsikan
sebagai berikut:
Ann likes every toy she plays with.
Doll is a toy.
Snoopy is a toy.
Ann plays with Snoopy.
Sue likes everything Ann likes
PROLOG PROGRAMS
Deskripsi di atas diterjemahkan ke program PROLOG.
likes(ann,X) :- toy(X),
plays(ann, X).
toy(doll).
toy(snoopy).
plays(ann,snoopy).
likes(sue,Y):- likes(ann, Y).
PROLOG PROGRAMS
Program PROLOG :
 FACT merepresentasikan suatu bagian dari informasi
yang diasumsikan true;
 RULE merepresentasikan pernyataan kondisi
 VARIABLE merepresentasikan elemen yang tidak
spesifik
 CONSTANT merepresentasikan elemen yang spesifik
 PREDICATE merepresentasikan hubungan antara
elemen atau properti dari klas elemen-elemen
 PROCEDURE adalah kumpulan dari klausa-klausa yang
memiliki predikat yang sama
PROLOG PROGRAMS
Pada umumnya, PROLOG memiliki aturan sintaks :
 Simbol definisi dimulai dengan HURUF tunggal
atau sebuah KATA (huruf kecil/besar, angka,
underscores
 Variabel yang dimulai dengan HURUF BESAR
atau UNDERSCORE. Variabel tanpa nama
diijinkan dan direpresentasikan oleh SATU
UNDERSCORE. Klausa adalah formula yang
spesial dan menggabungkan sejumlah formula
elemen yang disebut dengan formula atomik.
PROLOG PROGRAMS
likes(sue,husband(ann)),



husband adalah bukan konstanta ataupun variabel
tetapi simbol fungsi.
Simbol fungsi merepresentasikan elemen secara tidak
langsung melalui elemen lain husband(ann ) yang
merujuk ke elemen lain, tidak dengan elemennya tapi
relasinya ke ann.
Dengan sintaks , term tidak pernah berdiri sendiri tapi
selalu dengan beberapa formula atomik.
PROLOG PROGRAMS
Term adalah variabel atau konstanta atau ekspresi
dari bentuk f (T1,T2,…,Tn) dimana f adalah
simbol fungsi dan T1, T2, …, Tn (N > 0) adalah
term. Simbol f disebut functor, n adalah arity,
dan T1, T2, …, Tn adalah argumen-argumen dari
term.
 Formula atomik adalah ekspresi dari bentuk
p(T1, T2, …, Tn), dimana p adlah simbol
predikat dan T1, T2, …, Tn adalah term.
Jika n = 0 maka tidak ada argumen, sehingga
yang di dalam kurung diabaikan

PROLOG PROGRAMS
Program PROLOG adalah kumpulan klausa yang terbatas
dengan bentuk :
A :- B1, …, Bn
Dimana n ≥ 0 dan A dan Bi…Bn adalah formula atomik.
Klausa diatas dibaca
“ A if B1 and … and Bn“.
Formula atomik A disebut kepala dari klausa dan (B1,…
Bn) disebut badan klausa. Jika n = 0, maka klausa
dengan simbol “:-“ diabaikan, disebut unit clause
QUERIES
Given the program in example 1, one
may seek information by asking
questions such as
“What does Sue like ?”
QUERIES
In PROLOG, the above query is written
as follows :
?- likes(sue,X).
PROLOG, applying logical deduction to
find an answer for the query. PROLOG’s
answer will be :
X = snoopy
QUERIES
A query has a the form ?- A1,…,An,
where each Ai is an atomic formula. In
This query, (A1,…,An) is called a goal,
and each Ai is subgoal. If n = 1, then
the goal is called a simple goal. If n =
0, we have an empty goal,which is
denoted by □.
WORKING WITH AMZI PROLOG
Amzi! Listener (ALIS)
 a Prolog interpreter
 The listener directly executes
source code, which is stored in a
plain text file (PRO file), or
typed in directly at the ?- prompt
WORKING WITH AMZI PROLOG
Enter and edit your source code (using File/New to
create a new file)
 Consult your source code file into the listener (using
Listener/Consult)
 Issue Prolog queries to test your code and use the
listener debugger, as needed (using Listener/Debug
on)
 Respond to runtime errors by either continuing or
failing
 Modify your source code to fix the problems
 Reconsult your source code file into the listener (using
Listener/Reconsult, which will automatically save all
modified source files/windows)
 Goto step 3 until your module works

WORKING WITH AMZI PROLOG
There are three types of errors.
 Regular or "soft" errors can be captured and handled by
Prolog or C/C++ error handlers. If you have not defined a
handler for the error (the normal case), the standard error
handler will display a message and present you with a
number of options. Normally, you will either fail the
current predicate, continue execution or reset the listener.
The most common soft error is a read error, which means
the syntax is incorrect. The most common causes of read
errors are a missing period (.) at the end of the clause or
term, and mismatched parentheses, dollar signs or quotes.
WORKING WITH AMZI PROLOG
The second type of error is a fatal error. Fatal
errors are most commonly caused by running
out of stack space or memory. You can modify
the sizes of the stacks by using initialization
files (.INI files).
 The final type of error is a catastrophic error
which is an internal Amzi! Prolog error. Report
any of these to technical support

WORKING WITH AMZI PROLOG
DEBUGGER
 The Amzi! debugger is used to find and
identify errors.
 It works on interpreted source code.
 The debugger can only be invoked from,
and used within, the listener. In order to
step through code, you need to consult
the source code form of the module
WORKING WITH AMZI PROLOG
INTERMIXING INTERPRETED AND
COMPILED CODE
The Amzi! listener differs from other Prolog listeners
because, can consult both source code files (*.PRO) and
compiled object code (*.PLM) files. This allows, to keep
code under development in source form, and debugged code
in compiled form.
 The Listener is the heart of the Amzi! development tools. It
is where you develop, test and debug your Prolog code. The
text file ENV.PRO is consulted automatically for you when
the listener starts up, which initializes various flags and
options.
 ENV.PRO in turn loads UTIL.PLM which contains a
number of useful predicates like member and append (see
UTIL.PRO for a complete list).

WORKING WITH AMZI PROLOG
Amzi! Compiler (ACMP)
 Compile source files
(*.PRO) into object files
(*.PLM),
 Compiled code runs at
least 10 times faster than
interpreted code
WORKING WITH AMZI PROLOG
Amzi! Linker (ALNK)
 Once an application has been debugged, the
object files (PLM) are linked into a single
executable load module (XPL file) that can be
embedded in a C/C++, Visual Basic or other
program, or run as a standalone program
 The linker automatically links in a copy of the
standard Amzi! library AMZILIB.PLM into
each XPL file
WORKING WITH AMZI PROLOG
Amzi! Standalone Executable Runtime
(ARUN)
 The Amzi! runtime executes a Prolog
load module (XPL file)
WORKING WITH AMZI PROLOG
PREDEF. FUNC. & PREDICATES
Arithmetic functions :
+
*
/
//
mod
Arithmetic predicates :
<
=< >
>= =:= is
PREDEF. FUNC. & PREDICATES
PROLOG allows arithmetic terms and
formulas to be written in infix order,
instead of prefix order.
Example.
+(1,*(2,3)) is equivalent to 1 + 2 * 3
is(X,+(Y,1)is equivalent to X is Y + 1
PREDEF. FUNC. & PREDICATES
?- 3 = 1 + 2
no
Comment : 3 is not the same terms as 1 + 2
?- 3 is 1 + 2
yes
Comment : 3 is the value of 1 + 2.
The Predicate symbols “=“ does not represent the normal
“equality” but it represent a special relation in
PROLOG called “unifiability”
PREDEF. FUNC. & PREDICATES
Two Term T1 and T2 are unifiable (written T1 = T2)
if they can be made identical by a substitution of
their variables with appropriate terms
Ex.
?- X + 2 = 1 + Y
X = 1
Y = 2
PREDEF. FUNC. & PREDICATES
The Predicate “ = “ unifies its two
arguments with no arithmetic
computation involved
 The predicate “ is “ unifies its first
argument with the (computed) value of
its second argument

PREDEF. FUNC. & PREDICATES
?- 4-1
no
?- 4-1
yes
?- X+2
X = 1
?- X +
no
is 1+2
=:= 1+2
= 1+2
2 =:= 1+2
SUMMARY
for DETAIL INFORMATION do
read(A BOOK OF PROLOG);
write(‘YOU’LL FIND DETAIL’);
write(‘INFORMATION OF PROLOG’);
write(“THESE ARE ONLY INTRODUCTION”)
PROBLEM 1
Translate the following sentences into a PROLOG program :
Everyone who teaches a computing unit is smart.
John teaches the unit MA1
John’s wife teaches the unit SA1
MA1 is a mathematics unit
SA1 is a computing unit


From the above PROLOG program, identify the FACT, RULES,
TERMs, and ATOMIC FORMULA. Also list VARIABLEs,
CONSTANT, FUNCTIONS, and PREDICATEs.
Load the program to a PROLOG system and enter a query to
ask if ANYONE IS SMART. What is the logical meaning of the
answer
SOLUTION
smart(X) :- teaches(X,Y),computing(Y).
teaches(john,ma1).
teaches(wife(john),sa1).
mathematics(ma1).
computing(sa1).
SOLUTION (cont)
FACTS
 teaches(john,ma1).
 teaches(wife(john),sa1).
 mathematics(ma1).
 computing(sa1).
RULE
 smart(X) :- teaches(X,Y),computing(Y).
SOLUTION (cont)
ATOMIC FORMULAs
 smart(X), teaches(X,Y), computing(Y).
TERM
 Variabels : X, Y
 Constants : john, ma1, sa1
 Function : wife(john) function symbol : wife
PREDICATEs: smart, teaches, computing
SOLUTION (cont)
To ask if there is anyone smart ?
?- smart(X)
X = wife(john).
PROBLEM 2
Consider the following English version
Every mother likes her child if her child is good
Every mother is woman
Ann is woman
Ann’s husband is good.


Translate the above sentences into two different PROLOG
programs : one contains function symbols and the other does
not.
Load the program to a PROLOG system and enter a query to
ask if there is any woman who likes someone’s husband. What is
the logical meaning of the answer
SOLUTION
Program 1
likes(mother(X),X) :- good(X).
woman(mother(X)).
woman(ann).
good(husband(ann)).
Program 2
likes(X,Y) :- mother(X,Y), good(Y).
woman(Y) :- mother(X,Y).
woman(ann).
good(X) :- husband(X,ann)
SOLUTION (cont)
the program 1 is more expensive, because its functions
allow reference to a large number of entities such as
Ann’s husband, Ann’s mother, and Ann’s mother-in-law,
whereas in program 2, the only entity that can be
displayed is ann .
Is there any woman who likes someone’s husband ?
?- woman(X), likes(X,husband(Y)).
X = mother(husband(ann)).
Y = ann
SOLUTION (cont)
?- woman(X), husband(Y,Z), likes(X,Y).
no
because the system is unable to find anyone’s husband
to do this we must introduce new constant, say ann_husband, &
ann_mother_in_law
husband(ann_husband,ann).
mother(ann_mother_in_law,ann_husband)
?- woman(X), husband(Y,Z), likes(X,Y).
X = ann_mother_in_law
Y = ann_husband
Z = ann
SOLUTION (cont)
The unification process of PROLOG assumes every function is
one-to-one, that is f(x) = f(y) only if x = y. Consider the
following pairs of expressions and determine if they are
unifiable. If so, Find appropriate variable substitution !
a.
b.
c.
d.
e.
loves(X,husband(Y));
loves(X,husband(X));
mother(john);
min(log(X),Y);
X + 1;
loves(mother(Z),Z);
loves(mother(Z),Z);
mother(sue);
min(U,exp(V)).
Y + 2;
SOLUTION
a.
?- loves(X,husband(Y))=loves(mother(Z),Z).
X = mother(husband(H13))
Y = H13
Z = husband(H13)
yes
b.
no, because husband(mother(Z)) in not unifiable with Z
no, because john is not unifiable with sue
yes, U = log(X). Y = exp(V).
no, because 1 is not unifiable with 2
c.
d.
e.
PROBLEM
Three musicians of a multinational band take turns
playing solo in a piece of music; each plays only once.
The pianist plays first. John plays saxophone and plays
before Australian. Mark comes from United States and
plays before violinist. One soloist comes from Japan
and one is Sam.
Find Out who comes from which country, plays what
instrument and in what order !
SOLUTION
S is a solution to the musicians problem if
S is a band of three soloists,
in which the first member X of S plays piano, and
there are two ordered members Y, Z of S such that
Y’s name is John, Y plays saxophone,
and Z comes from Australia, and
there are two ordered members Y1, Z1 of S such that
Y1’s name is Mark, Y1 comes from the US
and Z1 plays violin, and
there is a member U of S who comes from Japan, and
there is a member V of S whose name is Sam
SOLUTION (cont)
musician_solution(S):band_soloists(S),
first(X,S),plays(X,piano),
order_mbers(Y,Z,S),
named(Y,jhon),plays(Y,sax),
country(Z,australia),
order_mbers(Y1,Z1,S),
named(Y1,mark),plays(Z1,violin),
country(Y1,us),
member(U,S),country(U,japan),
member(V,S),named(V,sam).
band_soloists(band(soloist(N1,C1,I1),
soloist(N2,C2,I2),
soloist(N3,C3,I3))).
named(soloist(N,_,_),N).
country(soloist(_,C,_),C).
plays(soloist(_,_,I),I).
first(X,band(X,_,_)).
order_mbers(X,Y,band(X,Y,Z)).
order_mbers(X,Z,band(X,Y,Z)).
order_mbers(Y,Z,band(X,Y,Z)).
member(X,band(X,Y,Z)).
member(Y,band(X,Y,Z)).
member(Z,band(X,Y,Z)).
Download