Introduction to Prolog

Slides:



Advertisements
Similar presentations
© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Advertisements

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Inference and Reasoning. Basic Idea Given a set of statements, does a new statement logically follow from this. For example If an animal has wings and.
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.
2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and.
© 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/
LING 388 Language and Computers Lecture 2 9/04/03 Sandiway FONG.
© 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.
LISP and PROLOG AI Programming Language Submitted To: Dr. Hesham El-Zoka Submitted By: Eng. Ismail Fathalla El-Gayar.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
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.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
LING 388: Language and Computers Sandiway Fong Lecture 4.
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.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Semantic Construction lecture 2. Semantic Construction Is there a systematic way of constructing semantic representation from a sentence of English? This.
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).
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
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.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
Introduction to Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
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.
ISBN Chapter 16 Logic Programming Languages.
Artificial Intelligence CS370D
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
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:... :-....
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
Instructor :Bayan Ghozlan الاستاذه : بيان غزلان.  البرولوغ Prolog هي لغة برمجة منطقية. وتعني برمجه المنطق programing language  تم اختراع اللغة بواسطة.
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
علمتني الثلوج أن أكون كبياض الثلج لا يحمل قلبي غير الحب والعطف والرحمة لا أسيئ الظن بالأخرين ولا أحكم عليهم من مظهرهم فإذا لم تعاشرهم فأنت تجهلهم !
Matching Matching and unification. Rules for matching terms. Examples of matching. –The most general instantiation. Computation using matching.
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
A Conjunction is a logical operator that connects two logical terms.
Unification, Recursion and Lists
Prolog syntax + Unification
Prolog Search Strategy
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Introduction to Prolog
Chapter 2: Prolog (Introduction and Basic Concepts)
Representations & Reasoning Systems (RRS) (2.2)
Presentation transcript:

Introduction to Prolog http://www.learnprolognow.org © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Chapter 1 Facts, Rules, and Queries http://www.learnprolognow.org © Patrick Blackburn, Johan Bos & Kristina Striegnitz

SWI Prolog A Free Software Prolog environment, licensed under the Lesser GNU public license. This popular interpreter was developed by Jan Wielemaker. Freely available Prolog interpreter Works with – Linux, – Windows, or – Mac OS

SWI Prolog This is the interpreter we used while developing this lecture. http://swish.swi-prolog.org/ There are many more Prolog interpreters

Lecture Theory – Prolog Syntax Exercises – Introduction to Prolog – Facts, Rules and Queries – Prolog Syntax Exercises – Exercises of LPN chapter 1 – Practical work

Aim of this lecture Give some simple examples of Prolog programs Discuss the three basic constructs in Prolog: – Facts – Rules – Queries

Aim of this lecture Introduce other concepts, such as – the role of logic – unification with the help of variables Begin the systematic study of Prolog by defining – terms – atoms, and – variables

Prolog "Programming with Logic" Very different from other programming languages – Declarative (not procedural) – Recursion (no “for” or “while” loops) – Relations (no functions) – Unification

Basic idea of Prolog Describe the situation of interest Ask a question – logically deduces new facts about the situation we described – gives us its deductions back as answers

Consequences Think declaratively, not procedurally High-level language – Challenging – Requires a different mindset High-level language – Not as efficient as, say, C – Good for rapid prototyping – Useful in many AI applications (knowledge representation, inference)

Facts The syntax of facts is the same as in the predicate calculus. Examples: fish(ray). % Ray is a fish. fish(salmon). % Salmon is a fish. red(car). % Car is red. likes(mary , john). % Mary likes John. factorial(3 , 6). % the factorial of 3 is 6

Prolog is conversational Here is an illustrative conversation with a Prolog system: ?- fish(ray). yes ?- fish(dog). no ?- fish(sardine). A sardine is a fish, but prolog doesn’t know that unless it is specified as a fact in the source file.

Multiple possible answers ?- fish(X). X = ray X = salmon Both ray and salmon are fish.

Inference Given the statement: Prove that “Fido will die.” ? “Fido is a dog” “All dogs are animals.” and “All animals will die.” Prove that “Fido will die.” ?

Inference “Fido is a dog” “All dogs are animals.” and “All animals will die.” dog(fido). animal(X):-dog(X). die(X):-animal(X). ?- die(fido). yes

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- woman(mia). ?- yes ?- ?- playsAirGuitar(jody). yes ?- ?- playsAirGuitar(mia). no

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- tattoed(jody). ERROR: predicate tattoed/1 not defined. ?-

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- party. yes ?-

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- rockConcert. no ?-

Knowledge Base 2 fact happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). fact rule rule rule head body

Knowledge Base 2 happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). ?- playsAirGuitar(mia). yes ?- ?- ?- playsAirGuitar(yolanda). yes

Clauses happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). There are five clauses in this knowledge base: two facts, and three rules. The end of a clause is marked with a full stop.

Predicates happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). There are three predicates in this knowledge base: happy, listens2music, and playsAirGuitar

Knowledge Base 3 happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).

Expressing Conjunction happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). The comma “," expresses conjunction in Prolog

Knowledge Base 3 happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). ?- playsAirGuitar(vincent). no ?-

Knowledge Base 3 happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). ?- playsAirGuitar(butch). yes ?-

Expressing Disjunction happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch); listens2music(butch).

Prolog and Logic Clearly Prolog has something to do with logic Operators

Knowledge Base 4 woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).

Prolog Variables woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X).

Variable Instantiation woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). X=mia

Asking Alternatives woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). X=mia; X=jody X=jody; X=yolanda X=yolanda; no

Knowledge Base 4 woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- loves(marsellus,X), woman(X). X=mia ?-

Knowledge Base 4 woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- loves(pumpkin,X), woman(X). no ?-

Knowledge Base 5 loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z). ?- jealous(marsellus,W). W=vincent; W=marsellus; no ?-

Prolog Syntax What exactly are facts, rules and queries built out of? Terms Terms Simple Terms Simple Terms Complex Terms Complex Terms Constants Constants Variables Variables Atoms Atoms Numbers Numbers

Atoms A sequence of characters of upper-case letters, lower-case letters, digits, or underscore, starting with a lowercase letter Examples: butch, big_kahuna_burger, playGuitar An arbitrary sequence of characters enclosed in single quotes Examples: 'Vincent', 'Five dollar shake', '@$%' A sequence of special characters Examples: : , ; . :-

Numbers Integers: 12, -34, 22342 Floats: 34573.3234, 0.3435

Variables A sequence of characters of upper-case letters, lower-case letters, digits, or underscore, starting with either an uppercase letter or an underscore Examples: X, Y, Variable, Vincent, _tag

Complex Terms Atoms, numbers and variables are building blocks for complex terms Complex terms are built out of a functor directly followed by a sequence of arguments Arguments are put in round brackets, separated by commas The functor must be an atom

Examples of complex terms Examples we have seen before: playsAirGuitar(jody) loves(vincent, mia) jealous(marsellus, W) Complex terms inside complex terms: hide(X,father(father(father(butch))))

Arity The number of arguments a complex term has is called its arity Examples: woman(mia) is a term with arity 1 loves(vincent,mia) has arity 2 father(father(butch)) arity 1

Arity is important In Prolog you can define two predicates with the same functor but with different arity Prolog would treat this as two different predicates In Prolog documentation arity of a predicate is usually indicated with the suffix "/" followed by a number to indicate the arity

Example of Arity This knowledge base defines happy/1 listens2music/1 happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). This knowledge base defines happy/1 listens2music/1 playsAirGuitar/1

Summary of this lecture Simple examples of Prolog programs Introduced three basic constructs in Prolog: Facts Rules Queries Discussed other concepts, such as the role of logic unification with the help of variables Definition of Prolog constructs: terms, atoms, and variables

Exercises Exercise 1.1 Which of the following sequences of characters are atoms, which are variables, and which are neither? vINCENT Footmassage Variable23 Variable2000 big_kahuna_burger ’big  kahuna  burger’ big  kahuna  burger ‘Jules’ _Jules ’_Jules’

Exercises Exercise 1.2 Which of the following sequences of characters are atoms, which are variables, which are complex terms, and which are not terms at all? Give the functor and arity of each complex term. loves(Vincent,mia) ’loves(Vincent,mia)’ Butch(boxer) boxer(Butch) and(big(burger),kahuna(burger)) and(big(X),kahuna(X)) _and(big(X),kahuna(X)) (Butch  kills  Vincent) kills(Butch  Vincent) kills(Butch,Vincent

Exercises Exercise 1.3 How many facts, rules, clauses, and predicates 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):-  father(X,Y).     father(Y,Z):-  man(Y),  son(Z,Y).     father(Y,Z):-  man(Y),  daughter(Z,Y).

Exercises Exercise 1.4 Represent the following in Prolog: Butch is a killer. Mia and Marsellus are married. Zed is dead. Marsellus kills everyone who gives Mia a footmassage. Mia loves everyone who is a good dancer. Jules eats anything that is nutritious or tasty.

Exercises Exercise 1.5 Suppose we are working with the following knowledge base: wizard(ron).      hasWand(harry).      quidditchPlayer(harry).      wizard(X):-  hasBroom(X),  hasWand(X).      hasBroom(X):-  quidditchPlayer(X).

Exercises Exercise 1.5 How does Prolog respond to the following queries? wizard(ron). witch(ron). wizard(hermione). witch(hermione). wizard(harry). wizard(Y). witch(Y).

Chapter 2  Unification http://www.learnprolognow.org © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Unification Recall previous example, where we said that Prolog unifies woman(X) with woman(mia) thereby instantiating the variable X with the atom mia.

Unification Working definition: Two terms unify if they are the same term or if they contain variables that can be uniformly instantiated with terms in such a way that the resulting terms are equal

Unification This means that: This also means that: mia and mia unify 42 and 42 unify woman(mia) and woman(mia) unify This also means that: vincent and mia do not unify woman(mia) and woman(jody) do not unify

Unification What about the terms: mia and X woman(Z) and woman(mia) loves(mia,X) and loves(X,vincent)

Instantiations When Prolog unifies two terms it performs all the necessary instantiations, so that the terms are equal afterwards This makes unification a powerful programming mechanism

Revised Definition If T1 and T2 are constants, then T1 and T2 unify if they are the same atom, or the same number. If T1 is a variable and T2 is any type of term, then T1 and T2 unify, and T1 is instantiated to T2. (and vice versa) If T1 and T2 are complex terms then they unify if: They have the same functor and arity, and all their corresponding arguments unify, and the variable instantiations are compatible.

Prolog unification ?- mia = mia. yes ?- mia = vincent. no ?-

Prolog unification ?- mia = X. X=mia yes ?-

How will Prolog respond? ?- X=mia, X=vincent. no ?- Why? After working through the first goal, Prolog has instantiated X with mia, so that it cannot unify it with vincent anymore. Hence the second goal fails.

Example with complex terms ?- k(s(g),Y) = k(X,t(k)). X=s(g) Y=t(k) yes ?-

Example with complex terms ?- k(s(g),t(k)) = k(X,t(Y)). X=s(g) Y=k yes ?-

One last example ?- loves(X,X) = loves(marsellus,mia).

Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?- vertical(line(point(1,1),point(1,3))). yes ?- ?- ?- vertical(line(point(1,1),point(3,2))). no ?-

Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?- horizontal(line(point(1,1),point(1,Y))). Y = 1; no ?-

Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?- horizontal(line(point(2,3),Point)). Point = point(_554,3); no ?-

Proof Search Now that we know about unification, we are in a position to learn how Prolog searches a knowledge base to see if a query is satisfied. In other words: we are ready to learn about proof search

Example f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y).

Example: search tree f(a). f(b). ?- k(Y). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- k(Y).

Example: search tree f(a). f(b). ?- k(Y). g(a). g(b). Y=X h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). ?- k(Y).

Example: search tree f(a). f(b). ?- k(Y). g(a). g(b). Y=X h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). X=a ?- g(a), h(a). ?- k(Y).

Example: search tree f(a). f(b). ?- k(Y). g(a). g(b). Y=X h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). X=a ?- g(a), h(a). ?- k(Y). ?- h(a).

Example: search tree † f(a). f(b). ?- k(Y). g(a). g(b). Y=X h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). X=a ?- g(a), h(a). ?- k(Y). ?- h(a). †

Example: search tree † f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). X=a X=b ?- g(a), h(a). ?- g(b), h(b). ?- k(Y). ?- h(a). †

Example: search tree † f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). X=a X=b ?- g(a), h(a). ?- g(b), h(b). ?- k(Y). ?- h(a). ?- h(b). †

Example: search tree † f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). X=a X=b ?- g(a), h(a). ?- g(b), h(b). ?- k(Y). Y=b ?- h(a). ?- h(b). †

Example: search tree † f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=X ?- f(X), g(X), h(X). X=a X=b ?- g(a), h(a). ?- g(b), h(b). ?- k(Y). Y=b; no ?- ?- h(a). ?- h(b). †

Exercise Exercise 2.1 Which of the following pairs of terms unify? Where relevant, give the variable instantiations that lead to successful unification. bread  =  bread ’Bread’  =  bread ’bread’  =  bread Bread  =  bread bread  =  sausage food(bread)  =  bread food(bread)  =  X food(X)  =  food(bread) food(bread,X)  =  food(Y,sausage) food(bread,X,beer)  =  food(Y,sausage,X) food(bread,X,beer)  =  food(Y,kahuna_burger) food(X)  =  X meal(food(bread),drink(beer))  =  meal(X,Y) meal(food(bread),X)  =  meal(X,drink(beer))

Exercise Exercise 2.2 We are working with the following knowledge base: house_elf(dobby). witch(hermione). witch(’McGonagall’). witch(rita_skeeter). magic(X):- house_elf(X). magic(X):- wizard(X). magic(X):- witch(X).

Exercise Which of the following queries are satisfied? Where relevant, give all the variable instantiations that lead to success. ?-  magic(house_elf). ?-  wizard(harry). ?-  magic(wizard). ?-  magic(’McGonagall’). ?-  magic(Hermione).