Download presentation
Presentation is loading. Please wait.
1
Introduction to Prolog
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
2
Chapter 1 Facts, Rules, and Queries
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
3
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
4
SWI Prolog This is the interpreter we used while developing this lecture. There are many more Prolog interpreters
5
Lecture Theory – Prolog Syntax Exercises – Introduction to Prolog
– Facts, Rules and Queries – Prolog Syntax Exercises – Exercises of LPN chapter 1 – Practical work
6
Aim of this lecture Give some simple examples of Prolog programs
Discuss the three basic constructs in Prolog: – Facts – Rules – Queries
7
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
8
Prolog "Programming with Logic"
Very different from other programming languages – Declarative (not procedural) – Recursion (no “for” or “while” loops) – Relations (no functions) – Unification
9
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
10
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)
11
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
12
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.
13
Multiple possible answers
?- fish(X). X = ray X = salmon Both ray and salmon are fish.
14
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.” ?
15
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
16
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- woman(mia). ?- yes ?- ?- playsAirGuitar(jody). yes ?- ?- playsAirGuitar(mia). no
17
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- tattoed(jody). ERROR: predicate tattoed/1 not defined. ?-
18
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- party. yes ?-
19
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- rockConcert. no ?-
20
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
21
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
22
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.
23
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
24
Knowledge Base 3 happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).
25
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
26
Knowledge Base 3 happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). ?- playsAirGuitar(vincent). no ?-
27
Knowledge Base 3 happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). ?- playsAirGuitar(butch). yes ?-
28
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).
29
Prolog and Logic Clearly Prolog has something to do with logic
Operators
30
Knowledge Base 4 woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).
31
Prolog Variables woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X).
32
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
33
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
34
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 ?-
35
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 ?-
36
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 ?-
37
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
38
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: : , ; :-
39
Numbers Integers: 12, -34, 22342 Floats: ,
40
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
41
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
42
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))))
43
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
44
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
45
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
46
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
47
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’
48
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
49
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).
50
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.
51
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).
52
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).
53
Chapter 2 Unification © Patrick Blackburn, Johan Bos & Kristina Striegnitz
54
Unification Recall previous example, where we said that Prolog unifies woman(X) with woman(mia) thereby instantiating the variable X with the atom mia.
55
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
56
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
57
Unification What about the terms: mia and X woman(Z) and woman(mia)
loves(mia,X) and loves(X,vincent)
58
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
59
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.
60
Prolog unification ?- mia = mia. yes ?- mia = vincent. no ?-
61
Prolog unification ?- mia = X. X=mia yes ?-
62
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.
63
Example with complex terms
?- k(s(g),Y) = k(X,t(k)). X=s(g) Y=t(k) yes ?-
64
Example with complex terms
?- k(s(g),t(k)) = k(X,t(Y)). X=s(g) Y=k yes ?-
65
One last example ?- loves(X,X) = loves(marsellus,mia).
66
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 ?-
67
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 ?-
68
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 ?-
69
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
70
Example f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X).
?- k(Y).
71
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).
72
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).
73
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).
74
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).
75
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). †
76
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). †
77
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). †
78
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). †
79
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). †
80
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))
81
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).
82
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).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.