Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

PENGENALAN PROLOG. OVERVIEW APA ITU PROLOG ? PROLOG vs PEMROGRAMAN KONVENTIONAL PROGRAM PROLOG QUERIES AMZI! PROLOG FUNGSI DAN PREDIKAT KESIMPULAN KASUS.

Presentasi serupa


Presentasi berjudul: "PENGENALAN PROLOG. OVERVIEW APA ITU PROLOG ? PROLOG vs PEMROGRAMAN KONVENTIONAL PROGRAM PROLOG QUERIES AMZI! PROLOG FUNGSI DAN PREDIKAT KESIMPULAN KASUS."— Transcript presentasi:

1 PENGENALAN PROLOG

2 OVERVIEW APA ITU PROLOG ? PROLOG vs PEMROGRAMAN KONVENTIONAL PROGRAM PROLOG QUERIES AMZI! PROLOG FUNGSI DAN PREDIKAT KESIMPULAN KASUS

3 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

4 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.

5 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?

6 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?

7 PROLOG Pada pemrograman konvensional : Programmer menginstruksi mesin BAGAIMANA cara memecahkan masalah dengan menampilkan urutan aksi-aksi Mesin membawa instruksi-instruksi yang spesifik

8 Pada pemrograman logika: Programmer mendefinisikan APA masalahnya, dengan menggunakan bahasa logika; Sistem menjalankan deduksi logika untuk menemukan jawaban dari masalah PROLOG VS CONVENTIONAL

9 PROLOG adalah spesifikasi masalah yang dapat dijalankan. Dengan alasan tersebut maka PROLOG disebut sebagai Very- High-Level programming.

10

11 PROLOG PROGRAMS Secara umum, PROLOG adalah deskripsi dari suatu dunia (yaitu kumpulan dari objek yang saling terhubung) yaang dituliskan dengan bahasa PROLOG.

12 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

13 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).

14 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

15 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.

16 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.

17 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

18 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

19 QUERIES Given the program in example 1, one may seek information by asking questions such as “What does Sue like ?”

20 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

21 QUERIES A query has a the form ?- A 1,…,A n, where each A i is an atomic formula. In This query, (A 1,…,A n ) is called a goal, and each A i 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 □.

22 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

23 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

24 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.

25 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

26 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

27 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).

28 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

29 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

30 WORKING WITH AMZI PROLOG Amzi! Standalone Executable Runtime (ARUN) The Amzi! runtime executes a Prolog load module (XPL file)

31 WORKING WITH AMZI PROLOG

32 PREDEF. FUNC. & PREDICATES Arithmetic functions : +- *///mod Arithmetic predicates : >==:=is

33 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

34 ?- 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

35 Two Term T 1 and T 2 are unifiable (written T 1 = T 2 ) 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

36 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

37 ?- 4-1 is 1+2 no ?- 4-1 =:= 1+2 yes ?- X+2 = 1+2 X = 1 ?- X + 2 =:= 1+2 no PREDEF. FUNC. & PREDICATES

38 SUMMARY for DETAIL INFORMATION do read( A BOOK OF PROLOG); write (‘YOU’LL FIND DETAIL’); write (‘INFORMATION OF PROLOG’); write( “ THESE ARE ONLY INTRODUCTION”)

39 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. 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 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

40 SOLUTION smart(X) :- teaches(X,Y),computing(Y). teaches(john,ma1).teaches(wife(john),sa1).mathematics(ma1).computing(sa1).

41 SOLUTION (cont) FACTS teaches(john,ma1). teaches(john,ma1). teaches(wife(john),sa1). teaches(wife(john),sa1). mathematics(ma1). mathematics(ma1). computing(sa1). computing(sa1).RULE smart(X) :- teaches(X,Y),computing(Y). smart(X) :- teaches(X,Y),computing(Y).

42 SOLUTION (cont) ATOMIC FORMULAs smart(X), teaches(X,Y), computing(Y). smart(X), teaches(X,Y), computing(Y).TERM Variabels : X, Y Variabels : X, Y Constants : john, ma1, sa1 Constants : john, ma1, sa1 Function : wife(john)function symbol : wife Function : wife(john)function symbol : wife PREDICATEs: smart, teaches, computing

43 SOLUTION (cont) To ask if there is anyone smart ? ?- smart(X) X = wife(john).

44 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. 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 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

45 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)

46 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

47 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

48 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.loves(X,husband(Y));loves(mother(Z),Z); b.loves(X,husband(X));loves(mother(Z),Z); c.mother(john);mother(sue); d.min(log(X),Y);min(U,exp(V)). e.X + 1;Y + 2;

49 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 c. no, because john is not unifiable with sue d. yes, U = log(X).Y = exp(V). e. no, because 1 is not unifiable with 2

50 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 !

51 SOLUTION S is a solution to the musicians problem if S is a band of three soloists, S is a band of three soloists, in which the first member X of S plays piano, and in which the first member X of S plays piano, and there are two ordered members Y, Z of S such that there are two ordered members Y, Z of S such that Y ’s name is John, Y plays saxophone, Y ’s name is John, Y plays saxophone, and Z comes from Australia, and and Z comes from Australia, and there are two ordered members Y1, Z1 of S such that there are two ordered members Y1, Z1 of S such that Y1 ’s name is Mark, Y1 comes from the US Y1 ’s name is Mark, Y1 comes from the US and Z1 plays violin, and and Z1 plays violin, and there is a member U of S who comes from Japan, and there is a member U of S who comes from Japan, and there is a member V of S whose name is Sam there is a member V of S whose name is Sam

52 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(N2,C2,I2), soloist(N3,C3,I3))). 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 ppt "PENGENALAN PROLOG. OVERVIEW APA ITU PROLOG ? PROLOG vs PEMROGRAMAN KONVENTIONAL PROGRAM PROLOG QUERIES AMZI! PROLOG FUNGSI DAN PREDIKAT KESIMPULAN KASUS."

Presentasi serupa


Iklan oleh Google