Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search.

Similar presentations


Presentation on theme: "© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search."— Presentation transcript:

1 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search

2 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Last Lecture In the previous lecture we have introduced: –recursion –declarative and procedural semantics –arithmetic operators –list, head & tail of a list, recursion on lists –predicates for collecting solutions

3 Aim of this lecture LPN Sect. 10.3 After this lecture, you should be able to explain the following key notions and their relation to Prolog: –“input/output” in Prolog –negation as failure –search algorithms

4 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Misc Info Prolog has many built-in predicates. See Prolog manual, e.g., list library documentation (Appendix 12)

5 “Input and output” in Prolog

6 Output Predicates do not return values. Variables are instantiated through unification. This is what gives Prolog its computation power! E.g., suppose you want to do something with the result of pairing the elements of a list with another element. Then don’t write doSomething(pair(L1,X,L2)) but:..... :- pair(L1,X,L2), doSomething(L2, L3). Here, L2 is taken as the “output” resulting from proving pair(L1,X,L2)

7 Input or Output When posing a query, often it is not fixed whether a parameter has to be “input” or “output”.

8 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: a2b/2 The predicate a2b/2 takes two lists as arguments and succeeds –if the first argument is a list of as, and –the second argument is a list of bs of exactly the same length ?- a2b([a,a,a,a],[b,b,b,b]). yes ?- a2b([a,a,a,a],[b,b,b]). no ?- a2b([a,c,a,a],[b,b,b,t]). no

9 © Patrick Blackburn, Johan Bos & Kristina Striegnitz a2b/2 a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2).

10 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Both Parameters “Input” a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2). ?- a2b([a,a,a],[b,b,b]). yes ?-

11 © Patrick Blackburn, Johan Bos & Kristina Striegnitz First Parameter “Input” Second Parameter “Output” a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2). ?- a2b([a,a,a,a,a], X). X = [b,b,b,b,b] yes ?-

12 © Patrick Blackburn, Johan Bos & Kristina Striegnitz a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2). ?- a2b(X,[b,b,b,b,b,b,b]). X = [a,a,a,a,a,a,a] yes ?- First Parameter “Output” Second Parameter “Input”

13 Input or Output Thus: When posing a query, often it is not fixed whether a parameter has to be “input” or “output”. But: Not in all cases can any parameter be used as input or as output. Sometimes parameters have to be instantiated.

14 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining predicates with arithmetic addThreeAndDouble(X, Y):- Y is 2 * (X+3). f(x) = 2(x+3)

15 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining predicates with arithmetic addThreeAndDouble(X, Y):- Y is 2 * (X+3). ?- addThreeAndDouble(1,Y). Y=8 yes ?- addThreeAndDouble(2,Y). Y=10 yes

16 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining predicates with arithmetic addThreeAndDouble(X, Y):- Y is 2 * (X+3). ?- addThreeAndDouble(X,10).

17 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining predicates with arithmetic addThreeAndDouble(X, Y):- Y is 2 * (X+3). ?- addThreeAndDouble(X,10). ERROR: is/2: Arguments are not sufficiently instantiated

18 Prolog Manual +Argument must be fully instantiated to a term that satisfies the required argument type. Think of the argument as input. - Argument must be unbound. Think of the argument as output. ?Argument must be bound to a partial term of the indicated type. Note that a variable is a partial term for any type. Think of the argument as either input or output or both input and output. E.g., flatten(+List1, ?List2)

19 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Meaning of Cut

20 © Patrick Blackburn, Johan Bos & Kristina Striegnitz The Cut Backtracking is a characteristic feature of Prolog But backtracking can lead to inefficiency: –Prolog can waste time exploring possibilities that lead nowhere –It would be nice to have some control The cut predicate !/0 offers a way to control backtracking

21 Warning! Only use cut when really needed! It affects Prolog’s procedural semantics, and can lead to difficult to understand code We also introduce it to explain negation as failure, which is used very often

22 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example of cut The cut is a Prolog predicate, so we can add it to rules in the body: – Example: p(X):- b(X), c(X), !, d(X), e(X). Cut is a goal that always succeeds Commits Prolog to the choices that were made since the parent goal was called

23 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Explaining the cut In order to explain the cut, we will –Look at a piece of cut-free Prolog code and see what it does in terms of backtracking –Add cuts to this Prolog code –Examine the same piece of code with added cuts and look how the cuts affect backtracking

24 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X).

25 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X).

26 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X).

27 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X).

28 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1 ?- p(X). ?- a(X). X=1

29 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X).?- b(X),c(X),d(X),e(X). X=1

30 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X).?- b(X),c(X),d(X),e(X). X=1

31 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X). ?- c(1),d(1),e(1). ?- b(X),c(X),d(X),e(X). X=1

32 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X). ?- d(1), e(1). ?- c(1),d(1),e(1). ?- b(X),c(X),d(X),e(X). X=1

33 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X). ?- d(1), e(1). ?- c(1),d(1),e(1). ?- b(X),c(X),d(X),e(X). X=1 †

34 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X). ?- d(1), e(1). ?- c(1),d(1),e(1). ?- b(X),c(X),d(X),e(X). X=1 X=2 †

35 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X). ?- d(1), e(1). ?- c(1),d(1),e(1).?- c(2),d(2),e(2). ?- b(X),c(X),d(X),e(X). X=1 X=2 †

36 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X). ?- d(2), e(2).?- d(1), e(1). ?- c(1),d(1),e(1).?- c(2),d(2),e(2). ?- b(X),c(X),d(X),e(X). X=1 X=2 †

37 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X). ?- d(2), e(2). ?- e(2). ?- d(1), e(1). ?- c(1),d(1),e(1).?- c(2),d(2),e(2). ?- b(X),c(X),d(X),e(X). X=1 X=2 †

38 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; X=2 ?- p(X). ?- a(X). ?- d(2), e(2). ?- e(2). ?- d(1), e(1). ?- c(1),d(1),e(1).?- c(2),d(2),e(2). ?- b(X),c(X),d(X),e(X). X=1 X=2 †

39 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; X=2; ?- p(X). ?- a(X).?- f(X). ?- d(2), e(2). ?- e(2). ?- d(1), e(1). ?- c(1),d(1),e(1).?- c(2),d(2),e(2). ?- b(X),c(X),d(X),e(X). X=1 X=2 †

40 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; X=2; X=3 ?- p(X). ?- a(X).?- f(X). ?- d(2), e(2). ?- e(2). ?- d(1), e(1). ?- c(1),d(1),e(1).?- c(2),d(2),e(2). ?- b(X),c(X),d(X),e(X). X=1 X=2 † X=3

41 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; X=2; X=3; no ?- p(X). ?- a(X).?- f(X). ?- d(2), e(2). ?- e(2). ?- d(1), e(1). ?- c(1),d(1),e(1).?- c(2),d(2),e(2). ?- b(X),c(X),d(X),e(X). X=1 X=2 † X=3

42 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Adding a cut Suppose we insert a cut in the second clause: If we now pose the same query we will get the following response: p(X):- b(X), c(X), !, d(X), e(X). ?- p(X). X=1; no

43 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X).

44 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X).

45 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3).

46 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). X=1 ?- p(X). ?- a(X). X=1 p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3).

47 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). X=1; ?- p(X). ?- a(X).?- b(X),c(X),!,d(X),e(X). X=1 p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3).

48 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). X=1; ?- p(X). ?- a(X).?- b(X),c(X),!,d(X),e(X). X=1 p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3).

49 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). X=1; ?- p(X). ?- a(X).?- b(X),c(X),!,d(X),e(X). X=1 p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- c(1),!,d(1),e(1). X=1

50 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). X=1; ?- p(X). ?- a(X).?- b(X),c(X),!,d(X),e(X). X=1 p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- c(1), !, d(1), e(1). X=1 ?- !, d(1), e(1).

51 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). X=1; ?- p(X). ?- a(X).?- b(X),c(X),!,d(X),e(X). X=1 p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- c(1), !, d(1), e(1). X=1 ?- !, d(1), e(1). ?- d(1), e(1). X X No backtracking on choices for b(X) and c(X) and not on choice for p(X).

52 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). X=1; no ?- p(X). ?- a(X).?- b(X),c(X),!,d(X),e(X). X=1 p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- c(1), !, d(1), e(1). X=1 ?- !, d(1), e(1). ?- d(1), e(1). †

53 © Patrick Blackburn, Johan Bos & Kristina Striegnitz What the cut does The cut only commits us to choices made since the parent goal was unified with the left-hand side of the clause containing the cut For example, in a rule of the form q:- p 1, …, p n, !, r 1, …, r n. when we reach the cut it commits us: –to this particular clause of q –to the choices made by p 1, …, p n –NOT to choices made by r 1, …, r n

54 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Using Cut

55 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Using Cut Consider the following predicate max/3 that succeeds if the third argument is the maximum of the first two max(X,Y,Y):- X =< Y. max(X,Y,X):- X>Y.

56 © Patrick Blackburn, Johan Bos & Kristina Striegnitz The max/3 predicate What is the problem? There is a potential inefficiency –Suppose it is called with ?- max(3,4,Y). –It will correctly unify Y with 4 –But when asked for more solutions, it will try to satisfy the second clause. This is completely pointless since these clauses are mutually exclusive! max(X,Y,Y):- X =< Y. max(X,Y,X):- X>Y.

57 © Patrick Blackburn, Johan Bos & Kristina Striegnitz max/3 with cut With the help of cut this is easy to fix Note how this works: –If the X =< Y succeeds, the cut commits us to this choice, and the second clause of max/3 is not considered –If the X =< Y fails, Prolog goes on to the second clause max(X,Y,Y):- X =< Y, !. max(X,Y,X):- X>Y.

58 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Green Cuts Cuts that do not change the meaning of a predicate are called green cuts The cut in max/3 is an example of a green cut: –the new code gives exactly the same answers as the old version, –but it is more efficient

59 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another max/3 with cut Why not remove the body of the second clause? After all, it is redundant. How good is it? max(X,Y,Y):- X =< Y, !. max(X,Y,X).

60 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another max/3 with cut Why not remove the body of the second clause? After all, it is redundant. How good is it? –okay max(X,Y,Y):- X =< Y, !. max(X,Y,X). ?- max(200,300,X). X=300 yes

61 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another max/3 with cut Why not remove the body of the second clause? After all, it is redundant. How good is it? –okay max(X,Y,Y):- X =< Y, !. max(X,Y,X). ?- max(400,300,X). X=400 yes

62 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another max/3 with cut Why not remove the body of the second clause? After all, it is redundant. How good is it? –oops…. max(X,Y,Y):- X =< Y, !. max(X,Y,X). ?- max(200,300,200). yes

63 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Revised max/3 with cut Unification after crossing the cut This does work max(X,Y,Z):- X =< Y, !, Y=Z. max(X,Y,X). ?- max(200,300,200). no Important: this is a very specific use of unification in the body of the rule. Do not use this if you don’t have to!

64 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Recall: simplifying allSame allSame([ ]). allSame([ _ ]). allSame([X,X|T]) :- allSame([X|T]). allSame([ ]). allSame([X]). allSame([H1| [H2|T] ]) :- H1=H2, allSame([H2|T]).

65 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Red Cuts Cuts that change the meaning of a predicate are called red cuts The cut in the revised max/3 is an example of a red cut: –If we take out the cut, we don’t get an equivalent program Programs containing red cuts –Are not fully declarative –Can be hard to read –Can lead to subtle programming mistakes

66 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Exercise Are the cuts in the programs below green or red? f(X,0):- X<3, !. f(X,2):- 3=<X, X<6, !. f(X,4):- 6=<X. member1(X,[X|_]):- !. member1(X,[_|L]):- member1(X,L).

67 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation as Failure

68 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another built-in predicate: fail/0 As the name suggests, this is a goal that will immediately fail when Prolog tries to prove it That may not sound too useful But in combination with cut it enables writing interesting programs

69 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Vincent and burgers The cut fail combination allows to code exceptions enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d).

70 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Vincent and burgers The cut fail combination allows to code exceptions enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). ?- enjoys(vincent,a). yes

71 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Vincent and burgers The cut fail combination allows to code exceptions enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). ?- enjoys(vincent,b). no What would the result of the query below be, if the first two clauses would be reversed?

72 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation as Failure (NAF) The cut-fail combination seems to be offering us some form of negation It is called negation as failure, and defined as follows: neg(Goal):- Goal, !, fail. neg(Goal).

73 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation in Logic The assertion ¬p specifies that ¬p is true, and therefore p is false E.g.: ¬p, ¬p → q |= q

74 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation in Prolog Negation in Prolog is different from negation in logic! In Prolog, negation is represented using the special predicate ‘not/1’

75 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). enjoys(vincent,X) :- burger(X), not(bigKahunaBurger(X)). not(bigKahunaBurger(X)) succeeds if bigKahunaBurger(X) cannot be derived, i.e., if bigKahunaBurger(X) fails Prolog uses “negation as failure”

76 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). enjoys(vincent,X) :- burger(X), not(bigKahunaBurger(X)). ?- enjoys(vincent,X).

77 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). enjoys(vincent,X):- burger(X), not(bigKahunaBurger(X)). ?- enjoys(vincent,X). X = a;

78 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). enjoys(vincent,X):- burger(X), not(bigKahunaBurger(X)). ?- enjoys(vincent,X). X = a; X = c;

79 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). enjoys(vincent,X):- burger(X), not(bigKahunaBurger(X)). ?- enjoys(vincent,X). X = a; X = c; X = d.

80 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Built-in NAF pred.: not or \+ Because negation as failure is so often used, there is no need to define it In standard Prolog the prefix operator not/1 or \+/1 means negation as failure So we could define Vincent`s preferences as follows: enjoys(vincent,X):- burger(X), not(bigKahunaBurger(X)). ?- enjoys(vincent,X). X=a; X=c; X=d. Important: use “not” instead of the cut and fail combination.

81 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation as failure and logic Negation as failure is not logical negation Changing the order of the goals in the vincent and burgers program gives a different behaviour: enjoys(vincent,X):- not(bigKahunaBurger(X)), burger(X). ?- enjoys(vincent,X). no not(bigKahunaBurger(X)) succeeds if bigKahunaBurger(X) cannot be derived, i.e., if there are no bigKahunaBurgers.

82 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation as failure and logic Negation as failure is not logical negation “not(p(X))” succeeds if p(X) cannot be derived – this is different from classical negation p, p → q |= ¬r ?- not(r). yes :- dynamic(r). p. q :- p.

83 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation as failure and logic Negation as failure is not logical negation “not” has a non-monotonic behavior: if formulas are added, it could be that it can no longer be derived p, p → q, r |= ¬r ?- not(r). no :- dynamic(r). p. r. q :- p.

84 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation as failure and closed world assumption (CWA) Closed world assumption: what is not currently known to be true, is false Example: if the train schedule does not include a train at 15:00 to A’dam, conclude that there will not be a train at 15:00 to A’dam NAF: if we cannot derive that something is true, conclude that it is not true

85 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Search

86 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Route Planning

87 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Robot Navigation http://www.ics.forth.gr/cvrl/

88 Unreal

89 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Search Problems Common structure: We are faced with an initial situation and we would like to achieve a certain goal. At any point in time we have different simple actions available to us (e.g. “turn left” vs. “turn right”). Executing a particular sequence of such actions may or may not achieve the goal. Search is the process of inspecting several such sequences and choosing a path that achieves the goal.

90 Deep Blue & Watson IBM Chess Computer (1997) IBM Jeopardy Computer (2011) http://mashable.com/2011/02/16/w atson-jeopardy-day-2/

91 © Patrick Blackburn, Johan Bos & Kristina Striegnitz State Space and Moves Navigation in Unreal Tournament: States are navigation points Navigation points can be connected to other navigation points (determine moves) Use search to determine how to get from the current navigation point to a desired one Moving from one navigation point to the next is associated with a cost (distance); Try to reach desired navigation point at minimal cost: optimisation problem.

92 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Searching the State Space The set of all possible sequences of legal moves form a tree Each branch (path from root to leaf) corresponds to a sequence of states (and thereby also a sequence of moves). Two basic ways of moving through such a tree: depth-first and breadth-first

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

94 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Depth-First solve_depthfirst(Node, TargetNode, SolPath) :- depthfirst([], Node, TargetNode, SolPath). depthfirst(Path, Node, Node, [Node|Path]). depthfirst(Path, Node, TargetNode, SolPath) :- move(Node, NextNode), depthfirst([Node|Path], NextNode, TargetNode, SolPath).

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

96 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Breadth-First Store a set of candidate paths If head of the first candidate path = target node then this path is a solution Otherwise remove first candidate path, generate the set of all one-step extensions of this path, add this set of extensions at the end of the candidate set, and execute breadth-first search on this updated set.

97 Summary of this Lecture We introduced the following key notions and their relation to Prolog: –“input & output” –negation as failure –search algorithms

98 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Organization Reading: LPN Sect. 10.3 from "Negation as failure is an impotant tool." to "The difference is crucial." Tomorrow “werkcollege” - Assignment 2 Next lecture (Koen Hindriks) - GOAL: introduction to agent programming


Download ppt "© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search."

Similar presentations


Ads by Google