Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 1 Introduction to Prolog.

Similar presentations


Presentation on theme: "© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 1 Introduction to Prolog."— Presentation transcript:

1 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 1 Introduction to Prolog

2 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Learn Prolog Now! http://www.learnprolognow.org/ Important: be aware that there are different versions of the LPN book: the online html version, the hard copy book, and online pdf. They differ in particular wrt exercises and their numbering. When referring to exercises from the LPN book, I refer to the online html version as can be found on www.learnprolognow.org.

3 © Patrick Blackburn, Johan Bos & Kristina Striegnitz SWI Prolog Freely available Prolog interpreter Works with –Linux, –Windows, or –Mac OS There are many more Prolog interpreters Not all are ISO compliant

4 Aim of this lecture LPN Ch. 1-3 After this lecture, you should be able to explain the meaning of the following key notions and their relation to Prolog: –logic programming –facts, rules and queries (horn clauses) –unification –(most general) unifier (mgu) –backtracking, depth-first search, forward & backward chaining Important: I will not discuss everything from the book in the lectures, but it is to be studied for the exam (to the extent indicated on blackboard)! I strongly advise you to read the relevant parts of the book before the practical sessions.

5 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Introduction

6 Prolog Colmerauer & Roussel, 1972 Kowalski, Predicate Logic as Programming Language, In Proceedings IFIP Congress, pp. 569-574. 1974.

7 Grace Hopper 1906-1992 “Mother of COBOL” (1959) programs should be written in a language that is close to English rather than in machine code

8 Prolog (2)

9 © Patrick Blackburn, Johan Bos & Kristina Striegnitz removeThrees in Java static void removeThrees (ArrayList list) { Iterator iterator = list.iterator(); while (iterator.hasNext()) { if (iterator.next() == 3) { iterator.remove(); } } Procedural language: step by step instructions describing what should be computed

10 © Patrick Blackburn, Johan Bos & Kristina Striegnitz removeThrees in Prolog removeThrees([],[]). removeThrees([3|T],L) :- removeThrees(T,L). removeThrees([H|T],[H|T2]) :- not(H = 3), removeThrees(T,T2). What are the differences with Java?

11 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Prolog "Programming with Logic" Declarative: separation of knowledge base and inference engine Very different from other (procedural) programming languages Good for knowledge-rich tasks, logical reasoning Initially, small programming tasks to practice specific skills. More elaborate programs as part of GOAL in the 2 nd half of the course, and in Q4.

12 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Why Learn Prolog?

13 It is the knowledge representation language of GOAL, which will be used to program UT2004 agents in Q4 Different programming paradigm It is a challenge! One of the most well-known AI languages Learn fundamental programming concepts, in particular recursion and recursive data structures WARNING: it seems easy at first

14 Prolog Development Center advanced scheduling systems and speech based applications for manufacturing, retail and service businesses, larger public institutions, airlines and airports http://www.pdc.dk/

15 Visual Prolog support industrial strength programming of complex knowledge emphasized problems. combining the very best features of logical, functional and object-oriented programming paradigms in a consistent and elegant way. http://www.visual-prolog.com/

16 Clarissa at ISS fully voice-operated procedure browser, enabling astronauts to be more efficient with their hands and eyes and to give full attention to the task while they navigate through the procedure using spoken commands http://ti.arc.nasa.gov/project/clarissa/

17 OntoDLV OntoDLV is an intelligent ally for quickly and easily developing powerful knowledge-based applications and decision support systems (e.g. Planning, Customer Profiling or Workforce Scheduling) that otherwise require complex and labourious programming. http://www.exeura.com/

18 Ontological Logic Programming Murat Sensoy, Geeth de Mel, Wamberto W. Vasconcelos and Timothy J. Norman, "Position Paper: Ontological Logic Programming", Proceedings of the 3rd International Workshop on Semantic Sensor Networks (SSN10), 2010.

19 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Facts, Rules and Queries

20 Alle mensen zijn sterfelijk m(socrates). s(X) :- m(X). M(socrates), ∀ x(M(x) → S(x)) fact rule Variables capitalized, predicates lower case Implication ( :- ) from right to left Each clause is terminated with a full stop. Clauses are implicitly universally quantified. horn clauses

21 Head and Body of Rule m(socrates). s(X) :- m(X). fact rule Variables capitalized, predicates lower case Implication ( :- ) from right to left Each clause is terminated with a full stop. Clauses are implicitly universally quantified. body head

22 Is socrates sterfelijk? m(socrates). s(X) :- m(X). M(socrates), ∀ x(M(x) → S(x)) |= S(socrates) “?-” is the SWI-Prolog prompt asking whether a goal formula logically follows from the knowledge base ?- s(socrates). query

23 Is socrates sterfelijk? m(socrates). s(X) :- m(X).  ∀  →  “?-” is the SWI-Prolog prompt asking whether a goal formula logically follows from the knowledge base ?- s(socrates). yes ?-

24 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Knowledge Base 4 woman(mia). woman(jody). woman(yolanda).

25 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Prolog Variables woman(mia). woman(jody). woman(yolanda). ?- woman(X).

26 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Variable Instantiation woman(mia). woman(jody). woman(yolanda). ?- woman(X). X=mia returning variable instantiations instead of “only” whether a goal follows from the KB or not, is what gives Prolog its programming power!

27 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Asking Alternatives woman(mia). woman(jody). woman(yolanda). ?- woman(X). X=mia;

28 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Asking Alternatives woman(mia). woman(jody). woman(yolanda). ?- woman(X). X=mia; X=jody

29 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Asking Alternatives woman(mia). woman(jody). woman(yolanda). ?- woman(X). X=mia; X=jody; X=yolanda

30 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Asking Alternatives woman(mia). woman(jody). woman(yolanda). ?- woman(X). X=mia; X=jody; X=yolanda; no

31 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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). The comma “," expresses conjunction in Prolog

32 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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). how many variable instantiations does Prolog return for W?

33 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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

34 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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

35 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Syntax

36 A Few Remarks Be careful: terminology in Learn Prolog Now differs (somewhat) from standard terminology in predicate logic (in particular atom and term)! Prolog atoms (constants) start with lower case, but can also be an arbitrary sequence of characters enclosed in single quotes Examples: 'Vincent', 'Five dollar shake', '@$%’ Variables start with upper case or underscore

37 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Prolog Syntax What exactly are facts, rules and queries built out of? Terms Simple TermsComplex Terms ConstantsVariables AtomsNumbers Terms Simple TermsComplex Terms ConstantsVariables AtomsNumbers

38 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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

39 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Arity This knowledge base defines –happy/1 –listens2music/1 –playsAirGuitar/1 happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).

40 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Unification

41 From Redeneren en Logica: A subsitution that makes two literals equal is called a unifier. Searching such a substitution is unification. A most general unifier (mgu) is a unifier θ for which it holds that every unifier is an “extension” of θ. Unification is a fundamental computation mechanism in Prolog! The unifier resulting from unification is what gives Prolog its programming power.

42 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Unification 1.If T 1 and T 2 are constants, then T 1 and T 2 unify if they are the same atom, or the same number. 2.If T 1 is a variable and T 2 is any type of term, then T 1 and T 2 unify, and T 1 is instantiated to T 2. (and vice versa) 3.If T 1 and T 2 are complex terms then they unify if: a)They have the same functor and arity, and b)all their corresponding arguments unify, and c)the variable instantiations are compatible.

43 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Exercise: unification Which of the following pairs of terms unify? Which variable instantiations does unification give rise to? 1. ‘Bread’ = bread. 2. ‘bread’ = bread. 3. food(X) = food(bread). 4. meal(food(X),Y) = meal(food(bread),Z).

44 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Exercise: unification ?- ’Bread’ = bread. no ?- ’bread’ = bread. yes ?- food(X) = food(bread). X = bread. ?- meal(food(X),Y) = meal(food(bread),Z). X = bread, Y = Z. <= most general unifier!

45 © Patrick Blackburn, Johan Bos & Kristina Striegnitz ?- k(s(g),Y) = k(X,t(k)). Prolog unification: =/2

46 © Patrick Blackburn, Johan Bos & Kristina Striegnitz ?- k(s(g),Y) = k(X,t(k)). X=s(g) Y=t(k) yes ?- Prolog unification: =/2 Important: The equals sign (=) is Prolog’s unification operator. Do not confuse it with operators for comparing integers which look or sound similar, to be introduced later!

47 int myInt; myInt = 42; Unification is not Assignment The result of assignment is the assignment of a value to a variable. variablevalue myInt(X) = myInt(42). AssignmentUnification term The result of unification is an mgu or failure if no mgu exists.

48 int myInt; myInt = 42; Unification is not Assignment The result of assignment is the assignment of a value to a variable. variablevalue MyInt = 42. AssignmentUnification term The result of unification is an mgu or failure if no mgu exists.

49 int myInt; myInt = 3; myInt = 42; Unification is not Assignment The result of assignment is the assignment of a value to a variable. MyInt = 3, MyInt = 42. AssignmentUnification The result of unification is an mgu or failure if no mgu exists.

50 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Programming with Unification

51 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))).

52 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?-

53 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 ?-

54 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 ?-

55 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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(2,Y))). Y = 1; no ?- Prolog unifies the query with the second clause in the program and returns a unifier as the answer.

56 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 ?-

57 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Proof Search Intro

58 Logic & Prolog Kowalski, Predicate Logic as Programming Language, In Proceedings IFIP Congress, pp. 569-574. 1974. LogicProlog Sentences in predicate logic (horn clauses) Program Derivation (using resolution and unification) Computation Proof proceduresExecutors of logic programs

59 Resolution & Proof Search (1) Predicate logic: ¬B ∨ ¬C ∨ A ¬D ∨ B ¬E ∨ C D E ¬A (¬ conclusion) ¬B ∨ ¬C ∨ A & ¬A = ¬B ∨ ¬C ¬B ∨ ¬C & ¬D ∨ B = ¬C ∨ ¬D ¬C ∨ ¬D & D = ¬C ¬C & ¬E ∨ C = ¬E ¬E & E = ☐ Prolog program: a :- b, c. b :- d. c :- e. d. e. ?- a. ➟ [a] (list of query goals) [b,c] (a :- b, c.) [d,c](b :- d.) [c](d.) [e](c :- e.) true.(e.)

60 Resolution & Proof Search (2) ResolutionProof search in Prolog find two clauses that can be resolved find a rule with head that unifies with current goal calculate resolventupdate goal list by replacing unified goal with body of rule repeat But: Prolog fixes order in which resolvents are computed. This can influence the result of proof search! That is, procedural meaning can differ from declarative (logical) meaning !

61 Why learn about proof search? declarative vs. procedural meaning of programs writing correct programs debugging Prolog does it for me, so why should I know how it works?

62 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Proof Search

63 Key Notions Prolog uses backward chaining, i.e., it only computes if a query is made! linear (top-to-bottom) search for unifying facts or heads of rules depth-first search strategy backtracking if goal fails Important: you should be able to explain using these notions how Prolog searches for a proof of a query!

64 Backward Chaining Backward chaining: ?- a. true Forward chaining: b. c. a. Prolog uses backward chaining: only computes if a query is made! Alternative is forward chaining, i.e., take the facts and keep on firing rules until no rule can be fired anymore. a :- b, c. b :- d. c :- e. d. e.

65 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Depth-First Search Depth-first traversal: A→B→D→E→C→F→G

66 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Breadth-First Search Breadth-first traversal: A→B→C→D→E→F→G

67 Depth-first Search If [q1, …, qn] is the list of query goals, then select the head of the list, i.e., q1 to search for a matching clause. If q1 :- p1, p2, …,pm is the selected unified clause, then update list of query goals to [p1, p2, …, pm, q2, …, qn] and find a matching clause for p1 a :- b, c. b :- d. c :- e. d. e. a.a. b.b.c. d.e.

68 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Linear Search & Backtracking f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y).

69 © Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Linear Search & Backtracking

70 © Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 Linear Search & Backtracking Variables of rules are renamed (_G34) at each unification with a goal query. most general unifier

71 © Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- g(a), h(a). _G34 = a ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 Linear Search & Backtracking Linear search: f(_G34) is first unified with f(a) (the top-most clause that unifies). Resulting unifier is applied to remaining goals.

72 © Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- g(a), h(a). ?- h(a). ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 _G34 = a Linear Search & Backtracking

73 © Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- g(a), h(a). ?- h(a). † ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 _G34 = a Linear Search & Backtracking h(a) cannot be derived. Prolog backtracks to previous choice point.

74 © Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- g(a), h(a). ?- h(a). ?- g(b), h(b). _G34 = b † ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 _G34 = a Linear Search & Backtracking

75 © Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- g(a), h(a). ?- h(a). ?- g(b), h(b). ?- h(b). † _G34 = b ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 _G34 = a Linear Search & Backtracking

76 © Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=b ?- k(Y). ?- g(a), h(a). ?- h(a). ?- g(b), h(b). ?- h(b). † _G34 = b ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 _G34 = a Linear Search & Backtracking

77 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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). ?- g(a), h(a). ?- h(a). ?- g(b), h(b). ?- h(b). † _G34 = b ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 _G34 = a Linear Search & Backtracking using semicolon: ask Prolog to backtrack.

78 © Patrick Blackburn, Johan Bos & Kristina Striegnitz loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). Trace Demo

79 © Patrick Blackburn, Johan Bos & Kristina Striegnitz f(1). f(2). f(3). p(3,4). q(4). r(3). r(4). r(5). g(X) :- p(X,Y), q(Y), r(X). h(X) :- f(X). h(X) :- g(X). ?- h(X). X = 1 ; X = 2 ; X = 3 ; no. Exercise 1 Output does not correspond to program. What’s wrong?

80 © Patrick Blackburn, Johan Bos & Kristina Striegnitz f(1). f(2). f(3). p(3,4). q(4). r(3). r(4). r(5). g(X) :- p(X,Y), q(Y), r(X). h(X) :- f(X). h(X) :- g(X). ?- h(X). X = 1 ; X = 2 ; X = 3 ; X = 3. Exercise 1 Asking Prolog to backtrack gives another substitution X = 3, resulting from unification with the second rule for h(X).

81 © Patrick Blackburn, Johan Bos & Kristina Striegnitz f(3). p(3,4). q(4). r(3). r(4). r(5). g(X) :- p(X,Y), q(Y), r(X). h(X) :- f(X). h(X) :- g(X). [trace] ?- h(X). Call: (7) h(_G335) ? creep Call: (8) g(_G335) ? creep Call: (9) p(_G335, _L191) ? creep Exit: (9) p(3, 4) ? creep Call: (9) q(4) ? creep Exit: (9) q(4) ? creep Call: (9) r(3) ? creep Exit: (9) r(3) ? creep Exit: (8) g(3) ? creep Exit: (7) h(3) ? creep X = 3. Exercise 2 Output does not correspond to program. What’s wrong?

82 © Patrick Blackburn, Johan Bos & Kristina Striegnitz f(3). p(3,4). q(4). r(3). r(4). r(5). g(X) :- p(X,Y), q(Y), r(X). h(X) :- f(X). h(X) :- g(X). [trace] ?- h(X). Call: (7) h(_G335) ? creep Call: (8) f(_G335) ? creep Exit: (8) f(3) ? creep Exit: (7) h(3) ? creep X = 3 ; Redo: (7) h(_G335) ? creep Call: (8) g(_G335) ? creep Call: (9) p(_G335, _L191) ? creep Exit: (9) p(3, 4) ? creep..... Exercise 2 Linear search, so first unify h(X) with the head of the first rule for h(X), resulting in the goal f(_G335).

83 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y).

84 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7

85 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). ?- loves(_G7,mia). _G5 =vincent _G6 =mia ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7

86 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=vincent Y=vincent ?- jealous(X,Y). ?- loves(_G7,mia). _G5 =vincent _G6 =mia ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7 _G7=vincent

87 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=vincent Y=vincent; X=vincent Y=marsellus ?- jealous(X,Y). ?- loves(_G7,mia). _G5 =vincent _G6 =mia ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7 _G7=vincent _G7=marsellus

88 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=vincent Y=vincent; X=vincent Y=marsellus; ?- loves(_G7,mia). _G5=marsellus _G6=mia _G7=vincent _G7=marsellus ?- jealous(X,Y). ?- loves(_G7,mia). _G5 =vincent _G6 =mia ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7

89 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). …. X=vincent Y=marsellus; X=marsellus Y=vincent _G7=vincent ?- loves(_G7,mia). _G5=marsellus _G6=mia _G7=vincent _G7=marsellus ?- jealous(X,Y). ?- loves(_G7,mia). _G5 =vincent _G6 =mia ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7

90 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). …. X=marsellus Y=vincent; X=marsellus Y=marsellus _G7=marsellus _G7=vincent ?- loves(_G7,mia). _G5=marsellus _G6=mia _G7=vincent _G7=marsellus ?- jealous(X,Y). ?- loves(_G7,mia). _G5 =vincent _G6 =mia ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7

91 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). …. X=marsellus Y=vincent; X=marsellus Y=marsellus; no _G7=marsellus _G7=vincent ?- loves(_G7,mia). _G5=marsellus _G6=mia _G7=vincent _G7=marsellus ?- jealous(X,Y). ?- loves(_G7,mia). _G5 =vincent _G6 =mia ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7

92 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Summary of this lecture Introduced the following key notions and their relation to Prolog: –logic programming –facts, rules and queries (horn clauses) –unification –(most general) unifier (mgu) –backtracking, depth-first search, forward & backward chaining

93 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Tips Practical Session add comments to your code!! think before you ask the Prolog interpreter! remember to add a dot (‘.’) after each clause (fact or rule) you need to create and load a knowledge base (Prolog program) before you can query Prolog if you change the program, you need to reload it in Prolog. Use the command ‘make.’ for this. Prolog gives warning if clauses for a single predicate are not grouped in the program. Best practice: group clauses defining the same predicate.

94 Organization Reading: LPN Ch. 1-2 Tomorrow practical session - work on LPN Sect. 1.4, LPN Sect. 1.3, LPN Sect. 2.3, 2.4 Next lecture –Recursion –Lists –Arithmetic


Download ppt "© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 1 Introduction to Prolog."

Similar presentations


Ads by Google