Introduction to Prolog: Declarative Programming for AI Systems
Prolog is a declarative programming language with procedural elements, ideal for AI and knowledge-based systems. It focuses on logical descriptions of problems, allowing the computer to work out solutions. Prolog excels in search problems, uses backward chaining, and represents knowledge via facts, rules, and goals.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Pengantar Kecerdasan Buatan Pengantar Prolog
2 PROLOG PROLOG = Programmation en logique (Marseille, 1972) Declarative programming language with procedural elements Used for problems of AI / knowledge-based (expert) systems Motivation: reconcile use of logic as declarative knowledge representation with procedural representation of knowledge Strengths: Logical descriptions of problems, instead of HOW to solve them work out the solution let computer OSCAR KARNALIM, S.T., M.T.
3 PROLOG (Cont) Well-suited for problems involving search Simple programs can be understood by reading the code Backward chaining approach (goals, find facts: unification, substitution) Statement in PROLOG enclosed by a point . declarative Prolog-program = collection of clauses Facts Rules Goals (queries), shaped liked facts OSCAR KARNALIM, S.T., M.T.
4 Facts Facts describe properties of objects and relations between objects; comparable to tables in a relational database student Name interest Name Subject Hans Tina Lars Frida Hans Tina Lars Frida Math Datalogi Physics Math Prolog notation: Prolog notation: Prolog notation: student(hans). student(tina). student(lars). student(frida). interest(hans,math). interest(tina,datalogi). interest(lars,physics). interest(frida,math). <predicate_name>(arg1, arg2 ). OSCAR KARNALIM, S.T., M.T.
5 Rules Simple IF-THEN statements Every reasonable student is interested in math. interest(X,math) :- student(X). head body All specified conditions in the body (all clauses) must be true to make the predicate in the head true. Conjunctions (AND connected): mother(X,Person) :- parent(X,Person),sex(X,female). Disjunctions (OR connected): interest(X,prolog) :- interest(X,artificial_intelligence). interest(X,prolog) :- interest(X,logic). OSCAR KARNALIM, S.T., M.T.
6 Goals Goals are queries One ore more subgoals ?- student(thomas). => no Pattern matching: a fact matches a goal if Same predicate Same corresponding arguments. Goals can contain variables: ?- student(X). => X = hans ; => X = tina ; => X = lars ; => X = frida ; => no. OSCAR KARNALIM, S.T., M.T.
Prologs Inference mechanism Leftmost-depth-first search for solutions Matching: either two terms are identical, or they become identical by variable substitution (resolution based on pred.logic) Processing of subgoals from left to right Backtracking (from goal try to substitute variables into facts or rules) 1: 2: 3: 4: 5: ?- p(Z). s(a). s(b). q(a). p(X) :- s(X). p(Y) :- q(Y). p(Z) 5 4 s(Z) q(Z) 3 2 1 Z=a Z=b Z=a
Backward Chaining The Prolog interpreter uses backward chaining: starting from a goal (theorem) prove the goal by searching for rules whose head (action part) matches the goal Given are the following rules: X H OR 1 E A C F & C A, B facts B & E H F C 2 AND AND X prove H B E 3 4 5 F C B E C B AND X H A C B B
9 Rule s recursive Hindari rekursif kiri Correct (begin with simple cases): ancestor(X,Z):- parent(X,Z). Wrong: ancestor(X,Z):- ancestor(X,Y), parent(Y,Z). ancestor(X,Z):- parent(X,Y), ancestor(Y,Z). ancestor(X,Z):- parent(X,Z). OSCAR KARNALIM, S.T., M.T.
10 SWI PROLOG Install Open SWI PROLOG Menu File Consult Choose your prolog file (*.pro): contains facts and rules Write down your goal OSCAR KARNALIM, S.T., M.T.
11 Example File PROLOG likes(mary,food). likes(mary,wine). likes(john,wine). likes(john,mary). OSCAR KARNALIM, S.T., M.T.
12 Goal examples and Prolog s answers | ?- likes(mary,food). yes. | ?- likes(john,wine). yes. | ?- likes(john,food). no. | ?- likes(X,food). X= mary. OSCAR KARNALIM, S.T., M.T.
Marcuss Case Prove that: Marcus membenci Caesar Facts 1. Marcus adalah seorang manusia 2. Marcus orang Pompei 3. Marcus mencoba membunuh Caesar 4. Caesar adalah seorang penguasa Rules 1. Semua orang Pompei adalah orang Romawi 2. Semua orang Romawi setia pada Caesar atau membenci Caesar 3. Setiap orang setia pada minimal 1 orang 4. Orang hanya mencoba membunuh penguasa yang kepadanya mereka tidak setia
Translation into Predicate Logic (FOL) Facts: 1. man(marcus) 2. pompeian(marcus) 3. tryassassinate(marcus,caesar) 4. ruler(caesar) Rules: 1. x pompeian(x) roman(x) 2. x roman(x) loyalto(x, caesar) v hate(x,caesar) 3. x y loyalto(x, y) 4. x y man(x) ^ ruler(y) ^ tryassassinate(x,y) loyalto(x,y) Prove that: hate(marcus, caesar), use refutation/resolution.
Inference on Marcuss Case (1) Make CNF from rules 1. pompeian(x) v roman(x) 2. roman(x) v loyalto(x,caesar) v hate(x,caesar) 3. loyalto(x,y) 4. (man(x) ^ ruler(y) ^ tryassassinate(x,y)) v loyalto(x,y) man(x) v ruler(y) v tryassassinate(x,y) v loyalto(x,y) Apply substitutions into variable, and try to change rules into facts.
Inference on Marcuss Case (2) After the unification & substitions: 1. pompeian(marcus) v roman(marcus) 2. roman(marcus) v loyalto(marcus,caesar) v hate(marcus, caesar) 3. loyalto(marcus,caesar) 4. man(marcus) v ruler(caesar) v tryassassinate(marcus, caesar) v loyalto(marcus,caesar) 5. hate(marcus,caesar)
Inference on Marcuss Case (3) Inference tree, apply the resolution, try to make the KB empty , that s mean that you have to refute your assumption. R2 & 3: roman(marcus) v loyalto(marcus,caesar) v hate(marcus, caesar) R4: man(marcus) v ruler(caesar) v tryassassinate(marcus, caesar) v loyalto(marcus,caesar) R5: hate(marcus,caesar) F1:man(marcus) R1: pompeian(marcus) v roman(marcus) F2: pompeian(marcus) F4:ruler(caesar) F3: tryassassinate(marcus,caesar) { } { }
Use prolog to prove the Marcuss Case hate(marcus, caesar) man(marcus). pompeian(marcus). ruler(caesar). tryassassinate(marcus, caesar). roman(X) :- pompeian(X). hate(X, Y) :- man(X), ruler(Y), tryassassinate(X, Y). notloyalto(X, Y) :- man(X), ruler(Y), tryassassinate(X, Y).
Exercise 2: Family Relation (Kinship) George (L) x Mum (P) Spencer (L) x Kydd (P) Elizabeth (P) x Philip (L) Margaret (P) Diana (P) x Charles (L) Anne (P) x Mark (L) Andrew (L) x Sarah (P) Edward (L) William (L) Harry (L) Peter (L) Zara (P) Beatrice (P) Eugenie (L)
Exercise 2 (contd) Buatlah fakta (laki-laki, perempuan, ortu, menikah) dan aturan untuk: saudaraLaki(X,Y) X berjenis kelamin laki-laki, X dan Y adalah anak dari seseorang, X dan Y bukan orang yang sama saudaraPerempuan(X,Y) X berjenis kelamin perempuan, X dan Y adalah anak dari seseorang, X dan Y bukan orang yang sama bersaudaraKandung(X,Y) X dan Y memiliki ayah dan ibu yang sama, X dan Y bukan orang yang sama kakek(X,Y,Z) X adalah ayah dari Y, Y adalah ayah dari Z nenek(X,Y,Z) X adalah ibu dari Y, Y adalah ibu dari Z keponakan(X,Y) tidak membedakan jenis kelamin menantu(X,Y) tidak membedakan jenis kelamin sepupu(X,Y) tidak membedakan jenis kelamin
21 Referensi Russell, Stuart J. & Norvig, Peter. Artificial Intelligence: A Modern Approach (3rd ed.). Prentice Hall. 2009 Luger, George F. Artificial Intelligence: Structures and Strategies for Complex Problem Solving. 6th Edition. Addison Wesley. 2008. Watson, Mark., Practical Artificial Intelligence Programming in Java, Open Content Free eBook (CC License), 2005. Machine Learning : Tom Mitchel. OSCAR KARNALIM, S.T., M.T.