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