Agile Software Development Lab 2008 Dr. Günter Kniesel, Daniel Speicher, Tobias Rho, Pascal Bihler Spring 2008 R O O T S Prolog - Part 1 Alexis Raptarchis.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic.
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
Chapter 11 :: Logic Languages
Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 1 Introduction to Prolog.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
4. PROLOG Data Objects And PROLOG Arithmetic
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.
Prolog: Unification & Recursion © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
CPSC 322 Introduction to Artificial Intelligence September 20, 2004.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 2 Theory –Unification –Unification in Prolog –Proof search Exercises –Correction exercises.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
PROLOG KAIST Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.
Formal Models of Computation Part II The Logic Model
CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.
LING 388: Language and Computers Sandiway Fong Lecture 6.
Introduction to AgentSpeak and Jason for Programming Multi-agent Systems (1) Dr Fuhua (Oscar) Lin SCIS Athabasca University June 19, 2009.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
Functions Reading: Chapter 6 (94 – 107) from the text book 1.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain).
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Introduction to Prolog
Introduction to Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
Knowledge Based Information System
Artificial Intelligence CS370D
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
23-Feb-16 Prolog II Unification and clause order.
C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Chapter 2 Syntax and meaning of prolog programs Part 1.
Done By :- -Nesreen Basem -Sara nabil -Rawan Prolog Program.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Instructor :Bayan Ghozlan الاستاذه : بيان غزلان.  البرولوغ Prolog هي لغة برمجة منطقية. وتعني برمجه المنطق programing language  تم اختراع اللغة بواسطة.
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
علمتني الثلوج أن أكون كبياض الثلج لا يحمل قلبي غير الحب والعطف والرحمة لا أسيئ الظن بالأخرين ولا أحكم عليهم من مظهرهم فإذا لم تعاشرهم فأنت تجهلهم !
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Unification, Recursion and Lists
Chapter 11 :: Logic Languages
Prolog Search Strategy
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Chapter 12 :: Logic Languages
Introduction to Prolog
Chapter 12 :: Logic Languages
PROLOG.
Presentation transcript:

Agile Software Development Lab 2008 Dr. Günter Kniesel, Daniel Speicher, Tobias Rho, Pascal Bihler Spring 2008 R O O T S Prolog - Part 1 Alexis Raptarchis Patrick Rypalla

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)2 Table of Contents 1. Facts, Rules and Queries a.Prolog examples b.Prolog Syntax c.Exercise 1 2. Matching a.Proof Search b.Exercise 2 3. Recursion a.Exercise 3 4. List 5. Arithmetic 6. Cuts and Negation 7. Meta-Predicates

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)3 Facts, Rules, and Queries l There are only three basic constructs in Prolog: facts, rules, and queries l Collection of facts and rules is called a knowledge base (KB) l Queries are questions about the information stored in the KB l Facts are used to state things that are unconditionally true of the domain of interest l We can ask Prolog whether Mia is a woman by posing a query l If we ask whether Jody is a woman Prolog will answer no, because Jody is not known to the KB woman(mia). ?- woman(mia). fact query query Prolog will answer: yes

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)4 Facts, Rules, and Queries l Rules state information that is conditionally true of the domain of interest playsAirGuitar(mia) :- listens2Music(mia). l The rule says that Mia plays air guitar if she listens to music l :- should be read as ``if'', or ``is implied by'‘ l In general rules say: if the body of the rule is true, then the head of the rule is true too The body can contain more then one fact, for example: playsAirGuitar(mia):- listens2music(mia), happy(mia). head body rule

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)5 Facts, Rules, and Queries Lets add a fact to our KB, namely listens2Music(mia). playsAirGuitar(mia) :- listens2Music(mia). l We will ask Prolog whether Mia plays air guitar ?- playsAirGuitar(mia). Remember playsAirGuitar(mia) Is not a fact in our KB l But Prolog will respond yes! Hence Prolog can use so called modus ponens to deduce facts from the KB l This new fact is not explicitly recorded in the knowledge base. It is only implicitly present fact rule

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)6 Facts, Rules, and Queries man(vincent). woman(mia). woman(jody). woman(yolanda). ?- woman(X). X = mia ?- ; X = jody; X = yolanda; no Prolog answers this query by working from top to bottom through the KB, trying to match the expression woman(X) with the information KB contains. Prolog instantiates X to mia, or that it binds X to mia ; means or, so this query means: are there any more women?

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)7 Facts, Rules, and Queries happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia),happy(mia). playsAirGuitar(yolanda):- listens2music(yolanda). ?- playsAirGuitar(mia). no ?- playsAirGuitar(yolanda). yes The facts and rules contained in a KB are called clauses. In this case the KB contains 5 clauses, namely 2 facts and 3 rules The facts and rules contained in a KB are called clauses. In this case the KB contains 5 clauses, namely 2 facts and 3 rules

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)8 Facts, Rules, and Queries woman(mia). woman(jody). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z). ?- loves(pumpkin,X), woman(X). no loves(marcellus,X),woman(X). X = mia ?- jealous(marsellus,W). W = vincent

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)9 Prolog Syntax l What exactly are facts, rules and queries built out of? Terms Simple TermsComplex Terms ConstantsVariables AtomsNumbers Terms Simple TermsComplex Terms ConstantsVariables AtomsNumbers

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)10 Prolog Syntax: Atoms and Variables l Atoms are sequence of characters of upper-case letters, lower-case letters, digits, or underscore, starting with a lowercase letter Examples: butch, mia, playGuitar l An arbitrary sequence of characters enclosed in single quotes Examples: 'Vincent', 'Five dollar shake', l A sequence of special characters Examples: :, ;. :- l Variables Same as Atoms, just starting with either an uppercase letter or an underscore l Examples: X, Y, Variable, Vincent, _tag

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)11 Prolog Syntax: Complex Terms l Operators uImplication :- uConjunction, uDisjunction ; l Complex Terms uAtoms, numbers and variables are building blocks for complex terms uComplex terms are built out of a functor directly followed by a sequence of arguments uArguments are put in round brackets, separated by commas uThe functor must be an atom l Examples we have seen before: uplaysAirGuitar(jody) uloves(vincent, mia) ujealous(marsellus, W) argument functor

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)12 Prolog Syntax: Arity l Complex terms inside complex terms: uhide(X,father(father(father(butch)))) l Functor is hide and it has two arguments: X and the complex term father(father(father(butch))) l The number of arguments a complex term has is called its arity uExamples: woman(mia) /1 is a term with arity 1 loves(vincent,mia) /2 has arity 2 father(father(butch)) /1 arity 1 l In Prolog documentation arity of a predicate is usually indicated with the suffix "/" followed by a number to indicate the arity

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)13 Exercise 1 l Which of the following sequences of characters are atoms and which are variables? uvINCENT uFootmassage uvariable23 ubig_kahuna_burger u'big kahuna burger' uJules How many facts, rules and clausesare there in the following knowledge base? What are the heads of the rules, and what are the goals they contain? woman(vincent). woman(mia). man(jules). person(X) :- man(X); woman(X). loves(X,Y) :- knows(Y,X). father(Y,Z) :- man(Y), son(Z,Y). father(Y,Z) :- man(Y), daughter(Z,Y).

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)14 Exercise 1 - Solution l Which of the following sequences of characters are atoms and which are variables? uvINCENTatom uFootmassagevariable uVariable23atom ubig_kahuna_burgeratom u'big kahuna burger‘atom uJulesvariable How many facts, rules and clauses are there in the following knowledge base? What are the heads of the rules, and what are the goals they contain? woman(vincent). woman(mia). man(jules). person(X) :- man(X); woman(X). loves(X,Y) :- knows(Y,X). father(Y,Z) :- man(Y), son(Z,Y). father(Y,Z) :- man(Y), daughter(Z,Y). 3 facts 4 rules 7 clauses

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)15 Matching lTlTwo terms match, if they are equal or if they contain variables that can be instantiated in such a way that the resulting terms are equal Exampels: l=l=(mia,mia) match, because they are the same atom lwlwoman(mia) = woman(mia)match, because they are the same complex term lmlmia = X match, because X can be instantiated to mia lHlHow about loves(vincent, X) and loves(X, mia)? no match, because its impossible to find an instantiation of X lAlAnd does kill(shoot(gun), stab(knife)) = kill (X, stab(Y)) match? X = shoot(gun) Y = knife yes

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)16 Matching l Definition of Matching 1. If term1 and term2 are constants, then term1 and term2 match if and only if they are the same atom, or the same number. 2. If term1 is a variable and term2 is any type of term, then term1 and term2 match, and term1 is instantiated to term2. 3. If term1 and term2 are complex terms, then they match if and only if: uThey have the same functor and arity. uAll their corresponding arguments match uand the variable instantiations are compatible. (I.e. it is not possible to instantiate variable X to mia, when matching one pair of arguments, and to then instantiate X to vincent, when matching another pair of arguments.) 4. Two terms match if and only if it follows from the previous three clauses that they match.

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)17 Matching - occurs check l Consider the following query: father(X) = X. Let`s try and instantiate X to father(father(butch)): father(father(father(butch))) = father(father(butch)) l Prolog is desperately trying to match these terms, but it won't succeed. X = father(father(father(father(father(father(father(father (father(father(father(father(father(father(father(father(…. l newer versions of Prolog can detect cycles in terms X = father(father(father(father(father(father(...)))))))))) yes l Now we know about matching uNext: we will learn how Prolog actually searches a KB to see if a query is satisfied èProof search

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)18 Proof Search f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=b; no ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). ?- g(b), h(b). X=b ?- h(b). † Y=X ?- g(a), h(a). X=a ?- h(a).

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)19 Proof Search loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=marsellus Y=vincent; X=vincent Y=marsellus; no ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia ?- loves(B,mia). A=marsellus C=mia B=vincent B=marsellus X=AY=B X = vincent Y = vincent; X = vincent Y = marsellus;

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)20 Exercise 2 l Which of the following pairs of terms match? Give the variable instantiations that lead to successful matching. ufood(bread,X,beer) = food(Y,sausage,X) ufood(bread,X,beer) = food(Y,kahuna_burger) umeal(food(bread),drink(beer)) = meal(X,Y) u?- loves(X,X) = loves(marsellus,mia). u?- k(s(g),Y) = k(X,t(k)). u?- k(s(g),t(k)) = k(X,t(Y)).

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)21 Exercise 2 - Solution l food(bread,X,beer) = food(Y,sausage,X) no X = beer and X = sausage l food(bread,X,beer) = food(Y,kahuna_burger) no Because we have 3 arguments on the left side but only 2 on the right l meal(food(bread),drink(beer)) = meal(X,Y) X = food(bread) Y = drink(beer) Yes

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)22 Exercise 2 - Solution l ?- loves(X,X) = loves(marsellus,mia). no X = marsellus and X = mia l ?- k(s(g),Y) = k(X,t(k)). X=s(g) Y=t(k) yes l ?- k(s(g),t(k)) = k(X,t(Y)). X=s(g) Y=k yes

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)23 Recursion l Prolog predicates can be defined recursively l A predicate is recursively defined if one or more rules in its definition refers to itself l Let’s take a look on two rules: descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), descend(Z,Y). l What does this say? 1.if Y is a child of X, then Y is a descendant of X 2.if Z is a child of X, and Y is a descendant of Z, then Y is a descendant of X

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)24 Recursion - descend child(martha, charlotte). child(charlotte, caroline). child(caroline, laura). child(laura, rose). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z),descend(Z,Y). ?- descend(martha, laura) yes child(martha,laura) descend(martha,laura) child (martha,Y) descend(Y,laura) child (martha,Y) descend(Y,laura) descend(charlotte,laura) † child(charlotte,laura) † child (charlotte,Y) descend(Y,laura) child (charlotte,Y) descend(Y,laura) descend(charlotte,laura) child(charlotte,laura)

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)25 Recursion - successor lSlSuppose we use the following way to write numerals: 1. 0 is a numeral. 2. If X is a numeral, then so is succ(X). lTlThat is, succ(X) represents the number obtained by adding one to the number represented by X lIlIt simply says that 0 is a numeral, and that all other numerals are built by stacking succ symbols in front. numeral(0). numeral(succ(X)) :- numeral(X). Exercise: What will happen if you ask: ?- numeral(X) X = 0 ; X = succ(0) ; X = succ(succ(0)) ; X = succ(succ(succ(0))) ; X = succ(succ(succ(succ(0)))) ; X = succ(succ(succ(succ(succ(0))))) ; X = succ(succ(succ(succ(succ(succ(0)))))) ;

Agile Software Development Lab Spring 2008 R O O T S Vortragstitel (in „Ansicht -> Master änderbar)26 Exercise 3 child(ron, hermione). child(hermione, draco). child(draco, harry). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z),descend(Z,Y). ?- descend(ron, harry) child(ron,harry) descend(ron,harry) child (ron,Y) descend(Y,harry) child (ron,Y) descend(Y,harry) descend(hermione,harry) † child(hermione,harry) † child (hermione,Y) descend(Y,harry) child (hermione,Y) descend(Y,harry) descend(draco,harry) child(draco,harry)