PENGENALAN PROLOG
description
Transcript of PENGENALAN PROLOG
PENGENALAN PROLOGPENGENALAN PROLOG
OVERVIEWOVERVIEW
APA ITU PROLOG ? PROLOG vs PEMROGRAMAN
KONVENTIONAL PROGRAM PROLOG QUERIES AMZI! PROLOG FUNGSI DAN PREDIKAT KESIMPULAN KASUS
APA ITU PROLOG ?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?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.
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?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
APA ITU PROLOG?APA ITU PROLOG?
PROLOGPROLOG
Pada pemrograman konvensional :
Programmer menginstruksi mesin BAGAIMANA cara memecahkan masalah dengan menampilkan urutan aksi-aksi
Mesin membawa instruksi-instruksi yang spesifik
Pada pemrograman logika:Programmer mendefinisikan
APA masalahnya, dengan menggunakan bahasa logika;
Sistem menjalankan deduksi logika untuk menemukan jawaban dari masalah
PROLOG VS CONVENTIONALPROLOG VS CONVENTIONAL
PROLOG VS PROLOG VS CONVENTIONALCONVENTIONAL
PROLOG adalah spesifikasi masalah yang dapat dijalankan.
Dengan alasan tersebut maka PROLOG disebut sebagai Very-High-Level programming.
Problem-oriented language level
Assembly language level
Operating System Machine level
Conventional Machine level
Microprogramming level
Digital Logic level
Level 5
Level 4
Level 3
Level 2
Level 1
Level 0
Translation (compiler)
Translation (assembler)
Partial Interpretation(Operating System)
Interpretation (microprogram)
Microprograms are directlyexecuted by the hardware
PROLOG PROGRAMSPROLOG PROGRAMS
Secara umum, PROLOG adalah deskripsi dari suatu dunia (yaitu kumpulan dari objek yang saling terhubung) yaang dituliskan dengan bahasa PROLOG.
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 PROGRAMSPROLOG PROGRAMS
PROLOG PROGRAMSPROLOG 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 PROGRAMSPROLOG 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 PROGRAMSPROLOG 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 PROGRAMSPROLOG 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 PROGRAMSPROLOG 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 PROGRAMSPROLOG 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
QUERIESQUERIES
Given the program in example 1, one may seek information by asking questions such as
“What does Sue like ?”
QUERIESQUERIES
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
QUERIESQUERIES
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 PROLOGWORKING 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 PROLOGWORKING 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 PROLOGWORKING 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 PROLOGWORKING 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 PROLOGWORKING WITH AMZI PROLOG
DEBUGGERThe 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 PROLOGWORKING WITH AMZI PROLOG
INTERMIXING INTERPRETED AND INTERMIXING INTERPRETED AND COMPILED CODECOMPILED 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 PROLOGWORKING 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 PROLOGWORKING 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 PROLOGWORKING WITH AMZI PROLOG
Amzi! Standalone Executable Runtime (ARUN)
The Amzi! runtime executes a Prolog load module (XPL file)
WORKING WITH AMZI PROLOGWORKING WITH AMZI PROLOG
PREDEF. FUNC. & PREDEF. FUNC. & PREDICATESPREDICATES
Arithmetic functions : + - * / // mod
Arithmetic predicates :< =< > >= =:= is
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 * 3is(X,+(Y,1)is equivalent to X is Y + 1
PREDEF. FUNC. & PREDEF. FUNC. & PREDICATESPREDICATES
?- 3 = 1 + 2noComment : 3 is not the same terms as 1 + 2
?- 3 is 1 + 2yesComment : 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. & PREDICATESPREDEF. 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. & PREDICATESPREDEF. 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. & PREDICATESPREDEF. FUNC. & PREDICATES
?- 4-1 is 1+2
no
?- 4-1 =:= 1+2
yes
?- X+2 = 1+2
X = 1
?- X + 2 =:= 1+2
no
PREDEF. FUNC. & PREDICATESPREDEF. FUNC. & PREDICATES
SUMMARYSUMMARY
for DETAIL INFORMATION do
read(A BOOK OF PROLOG); write(‘YOU’LL FIND DETAIL’); write(‘INFORMATION OF
PROLOG’);write(“THESE ARE ONLY INTRODUCTION”)
PROBLEM 1PROBLEM 1Translate the following sentences into a PROLOG program Translate the following sentences into a PROLOG program
::Everyone who teaches a computing unit is smart.Everyone who teaches a computing unit is smart.John teaches the unit MA1John teaches the unit MA1John’s wife teaches the unit SA1John’s wife teaches the unit SA1MA1 is a mathematics unitMA1 is a mathematics unitSA1 is a computing unitSA1 is a computing unit
From the above PROLOG program, identify the FACT, From the above PROLOG program, identify the FACT, RULES, TERMs, and ATOMIC FORMULA. Also list RULES, TERMs, and ATOMIC FORMULA. Also list VARIABVARIABLEsLEs, CONST, CONSTANTANT, FUNCTIONS, and , FUNCTIONS, and PREDICAPREDICATEsTEs..
Load the program to a PROLOG system and enter a Load the program to a PROLOG system and enter a query to ask if ANYONE IS SMART. What is the logical query to ask if ANYONE IS SMART. What is the logical meaning of the answermeaning of the answer
SOLUTIONSOLUTION
smart(X) :- teaches(X,Y),computing(Y).smart(X) :- teaches(X,Y),computing(Y).
teaches(john,ma1).teaches(john,ma1).
teaches(wife(john),sa1).teaches(wife(john),sa1).
mathematics(ma1).mathematics(ma1).
computing(sa1).computing(sa1).
SOLUTION (cont)SOLUTION (cont)
FACTSFACTS teaches(john,ma1).teaches(john,ma1). teaches(wife(john),sa1).teaches(wife(john),sa1). mathematics(ma1).mathematics(ma1). computing(sa1).computing(sa1).
RULERULE smart(X) :- smart(X) :-
teaches(X,Y),computing(Y).teaches(X,Y),computing(Y).
SOLUTION (cont)SOLUTION (cont)
ATOMIC FORMULAsATOMIC FORMULAs smart(X), teaches(X,Y), computing(Y).smart(X), teaches(X,Y), computing(Y).
TERMTERM VariabelsVariabels : X, Y : X, Y ConstantsConstants : john, ma1, sa1 : john, ma1, sa1 Function : Function : wife(john)wife(john) function function
symbol : symbol : wifewife
PREDICATEs: PREDICATEs: smart, teaches, computingsmart, teaches, computing
SOLUTION (cont)SOLUTION (cont)
To ask if there is anyone smart ?To ask if there is anyone smart ?
?- smart(X)?- smart(X)
X = wife(john).X = wife(john).
PROBLEM 2PROBLEM 2Consider the following English versionConsider the following English version
Every mother likes her child if her child is Every mother likes her child if her child is goodgood
Every mother is womanEvery mother is womanAnn is womanAnn is womanAnn’s husband is good.Ann’s husband is good.
Translate the above sentences into two different Translate the above sentences into two different PROLOG programs : one contains function symbols PROLOG programs : one contains function symbols and the other does not.and the other does not.
Load the program to a PROLOG system and enter a Load the program to a PROLOG system and enter a query to ask if there is any woman who likes query to ask if there is any woman who likes someone’s husband. What is the logical meaning of someone’s husband. What is the logical meaning of the answerthe answer
SOLUTION SOLUTION
Program 1Program 1likes(mother(X),X) :- good(X).likes(mother(X),X) :- good(X).woman(mother(X)).woman(mother(X)).woman(ann).woman(ann).good(husband(ann)).good(husband(ann)).
Program 2Program 2likes(X,Y) :- mother(X,Y), good(Y).likes(X,Y) :- mother(X,Y), good(Y).woman(Y) :- mother(X,Y).woman(Y) :- mother(X,Y).woman(ann).woman(ann).good(X) :- husband(X,ann)good(X) :- husband(X,ann)
SOLUTION (cont)SOLUTION (cont)
the program 1 is more expensive, because its the program 1 is more expensive, because its functions allow reference to a large number of functions allow reference to a large number of entities such as Ann’s husband, Ann’s mother, entities such as Ann’s husband, Ann’s mother, and Ann’s mother-in-law, whereas in program and Ann’s mother-in-law, whereas in program 2, the only entity that can be displayed is 2, the only entity that can be displayed is ann ann ..
Is there any woman who likes someone’s Is there any woman who likes someone’s husband ?husband ?
?- woman(X), likes(X,husband(Y)).?- woman(X), likes(X,husband(Y)).X = mother(husband(ann)).X = mother(husband(ann)).Y = annY = ann
SOLUTION (cont)SOLUTION (cont)?- woman(X), husband(Y,Z), likes(X,Y).?- woman(X), husband(Y,Z), likes(X,Y).nono
because the system is unable to find anyone’s because the system is unable to find anyone’s husbandhusband
to do this we must introduce new constant, say to do this we must introduce new constant, say ann_husband, & ann_mother_in_law ann_husband, & ann_mother_in_law
husband(ann_husband,ann).husband(ann_husband,ann).mother(ann_mother_in_law,ann_husband)mother(ann_mother_in_law,ann_husband)
?- woman(X), husband(Y,Z), likes(X,Y).?- woman(X), husband(Y,Z), likes(X,Y).X = ann_mother_in_lawX = ann_mother_in_lawY = ann_husbandY = ann_husbandZ = annZ = ann
SOLUTION (cont)SOLUTION (cont)
The unification process of PROLOG assumes every The unification process of PROLOG assumes every function is one-to-one, that is f(x) = f(y) only if function is one-to-one, that is f(x) = f(y) only if x = y. Consider the following pairs of x = y. Consider the following pairs of expressions and determine if they are unifiable. expressions and determine if they are unifiable. If so, Find appropriate variable substitution !If so, Find appropriate variable substitution !
a.a. loves(X,husband(Y));loves(X,husband(Y)); loves(mother(Z),Z);loves(mother(Z),Z);
b.b. loves(X,husband(X));loves(X,husband(X)); loves(mother(Z),Z);loves(mother(Z),Z);
c.c. mother(john);mother(john); mother(sue);mother(sue);
d.d. min(log(X),Y);min(log(X),Y); min(U,exp(V)).min(U,exp(V)).
e.e. X + 1;X + 1; Y + 2;Y + 2;
SOLUTION SOLUTION
a.a. ?- loves(X,husband(Y))=loves(mother(Z),Z).?- loves(X,husband(Y))=loves(mother(Z),Z).
X = mother(husband(H13))X = mother(husband(H13))
Y = H13Y = H13
Z = husband(H13)Z = husband(H13)
yesyes
b.b. nono, because husband(mother(Z)) in not unifiable , because husband(mother(Z)) in not unifiable with Zwith Z
c.c. nono, because john is not unifiable with sue, because john is not unifiable with sue
d.d. yesyes, , U = log(X).U = log(X).Y = exp(V).Y = exp(V).
e.e. nono, because 1 is not unifiable with 2, because 1 is not unifiable with 2
PROBLEMPROBLEM
Three musicians of a multinational band take Three musicians of a multinational band take turns playing solo in a piece of music; each turns playing solo in a piece of music; each plays only once. The pianist plays first. plays only once. The pianist plays first. John plays saxophone and plays before John plays saxophone and plays before Australian. Mark comes from United Australian. Mark comes from United States and plays before violinist. One States and plays before violinist. One soloist comes from Japan and one is Sam. soloist comes from Japan and one is Sam.
Find Out who comes from which country, Find Out who comes from which country, plays what instrument and in what order !plays what instrument and in what order !
SOLUTION SOLUTION
SS is a solution to the musicians problem ifis a solution to the musicians problem if
SS is a band of three soloists, is a band of three soloists,
in which the first member in which the first member XX of of SS plays piano, andplays piano, and there are two ordered members there are two ordered members Y, ZY, Z of of SS such that such that
YY’s name is John, ’s name is John, YY plays saxophone, plays saxophone,
and and ZZ comes from Australia, and comes from Australia, and
there are two ordered members there are two ordered members Y1, Z1Y1, Z1 of of SS such such thatthat
Y1Y1’s name is Mark, ’s name is Mark, Y1Y1 comes from the US comes from the US
and and Z1Z1 plays violin, and plays violin, and
there is a member there is a member UU of of SS who comes from Japan, who comes from Japan, and and
there is a member there is a member VV of of SS whose name is Sam whose name is Sam
SOLUTION (cont)SOLUTION (cont)musician_solution(S):-musician_solution(S):-
band_soloists(S),band_soloists(S),first(X,S),plays(X,piano),first(X,S),plays(X,piano),order_mbers(Y,Z,S),order_mbers(Y,Z,S),
named(Y,jhon),plays(Y,sax),named(Y,jhon),plays(Y,sax),country(Z,australia),country(Z,australia),
order_mbers(Y1,Z1,S),order_mbers(Y1,Z1,S),named(Y1,mark),plays(Z1,violin),named(Y1,mark),plays(Z1,violin),country(Y1,us),country(Y1,us),
member(U,S),country(U,japan),member(U,S),country(U,japan),member(V,S),named(V,sam).member(V,S),named(V,sam).
band_soloists(band(soloist(N1,C1,I1),band_soloists(band(soloist(N1,C1,I1), soloist(N2,C2,I2),soloist(N2,C2,I2), soloist(N3,C3,I3))).soloist(N3,C3,I3))).
named(soloist(N,_,_),N).named(soloist(N,_,_),N).country(soloist(_,C,_),C).country(soloist(_,C,_),C).plays(soloist(_,_,I),I).plays(soloist(_,_,I),I).first(X,band(X,_,_)).first(X,band(X,_,_)).order_mbers(X,Y,band(X,Y,Z)).order_mbers(X,Y,band(X,Y,Z)).order_mbers(X,Z,band(X,Y,Z)).order_mbers(X,Z,band(X,Y,Z)).order_mbers(Y,Z,band(X,Y,Z)).order_mbers(Y,Z,band(X,Y,Z)).member(X,band(X,Y,Z)).member(X,band(X,Y,Z)).member(Y,band(X,Y,Z)).member(Y,band(X,Y,Z)).member(Z,band(X,Y,Z)).member(Z,band(X,Y,Z)).