Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.

Similar presentations


Presentation on theme: "CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany."— Presentation transcript:

1 CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany

2 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Arithmetic in Prolog Prolog provides a number of basic arithmetic tools Integer and real numbers 2 + 3 = 5 3 x 4 = 12 5 – 3 = 2 3 – 5 = -2 4  2 = 2 1 is the remainder when 7 is divided by 2 ?- 5 is 2+3. ?- 12 is 3  4. ?- 2 is 5-3. ?- -2 is 3-5. ?- 2 is 4/2. ?- 1 is mod(7,2). ArithmeticProlog

3 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example queries ?- 10 is 5+5. yes ?- 4 is 2+3. no ?- X is 3  4. X=12 yes ?- R is mod(7,2). R=1 yes

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

5 © Patrick Blackburn, Johan Bos & Kristina Striegnitz A closer look It is important to know that +, -, / and  do not carry out any arithmetic Expressions such as 3+2, 4-7, 5/5 are ordinary Prolog terms – Functor: +, -, /,  – Arity: 2 – Arguments: integers

6 © Patrick Blackburn, Johan Bos & Kristina Striegnitz A closer look ?- X = 3 + 2. X = 3+2 yes ?- 3 + 2 = X. X = 3+2 yes ?-

7 © Patrick Blackburn, Johan Bos & Kristina Striegnitz The is/2 predicate To force Prolog to actually evaluate arithmetic expressions, we have to use is just as we did in the other examples This is an instruction for Prolog to carry out calculations Because this is not an ordinary Prolog predicate, there are some restrictions

8 © Patrick Blackburn, Johan Bos & Kristina Striegnitz The is/2 predicate ?- X is 3 + 2. X = 5 yes ?- 3 + 2 is X. ERROR: is/2: Arguments are not sufficiently instantiated ?- Result is 2+2+2+2+2. Result = 10 yes ?- 3 is 2+H. ! ---------------------------------------- ! Error 22 : Instantiation Error ! Goal : 3 is 2 + _00065D44 | ?- H=3, Z is 3+H. H = 3, Z = 6

9 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Restrictions on use of is/2 We are free to use variables on the right hand side of the is predicate But when Prolog actually carries out the evaluation, the variables must be instantiated with a variable-free Prolog term This Prolog term must be an arithmetic expression

10 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Notation Two final remarks on arithmetic expressions – 3+2, 4/2, 4-5 are just ordinary Prolog terms in a user-friendly notation: 3+2 is really +(3,2) and so on. – Also the is predicate is a two-place Prolog predicate

11 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lists A list is a finite sequence of elements Examples of lists in Prolog: [mia, vincent, jules, yolanda] [mia, robber(honeybunny), X, 2, mia] [ ] [mia, [vincent, jules], [butch, friend(butch)]] [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]

12 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists List elements are enclosed in square brackets The length of a list is the number of elements it has All sorts of Prolog terms can be elements of a list There is a special list: the empty list [ ]

13 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail A non-empty list can be thought of as consisting of two parts – The head – The tail The head is the first item in the list The tail is everything else – The tail is the list that remains when we take the first element away – The tail of a list is always a list

14 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 1 [mia, vincent, jules, yolanda] Head: mia Tail: [vincent, jules, yolanda]

15 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 2 [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: [ ] Tail: [dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]

16 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 3 [dead(z)] Head: dead(z) Tail: [ ]

17 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and tail of empty list The empty list has neither a head nor a tail For Prolog, [ ] is a special simple list without any internal structure The empty list plays an important role in recursive predicates for list processing in Prolog

18 © Patrick Blackburn, Johan Bos & Kristina Striegnitz The built-in operator | Prolog has a special built-in operator | which can be used to decompose a list into its head and tail The | operator is a key tool for writing Prolog list manipulation predicates

19 © Patrick Blackburn, Johan Bos & Kristina Striegnitz The built-in operator | ?- [Head|Tail] = [mia, vincent, jules, yolanda]. Head = mia Tail = [vincent,jules,yolanda] yes ?-

20 © Patrick Blackburn, Johan Bos & Kristina Striegnitz The built-in operator | ?- [X|Y] = [mia, vincent, jules, yolanda]. X = mia Y = [vincent,jules,yolanda] yes ?-

21 © Patrick Blackburn, Johan Bos & Kristina Striegnitz The built-in operator | ?- [X|Y] = [ ]. no ?-

22 © Patrick Blackburn, Johan Bos & Kristina Striegnitz The built-in operator | ?- [X,Y|Tail] = [[ ], dead(Z), [2, [b,c]], [], Z, [2, [b,c]]]. X = [ ] Y = dead(z) Tail = [ [2, [b,c]], [ ], Z, [2, [b,c]] ] Z = _4543 yes ?-

23 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Anonymous variable Suppose we are interested in the second and fourth element of a list ?- [X1,X2,X3,X4|Tail] = [mia, vincent, marsellus, jody, yolanda]. X1 = mia X2 = vincent X3 = marsellus X4 = jody Tail = [yolanda] yes ?-

24 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Anonymous variables There is a simpler way of obtaining only the information we want: ?- [ _,X2, _,X4|_ ] = [mia, vincent, marsellus, jody, yolanda]. X2 = vincent X4 = jody yes ?- The underscore is the anonymous variable

25 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Member (of a List) One of the most basic things we would like to know is whether something is an element of a list or not So let`s write a predicate that when given a term X and a list L, tells us whether or not X belongs to L This predicate is usually called member/2

26 © Patrick Blackburn, Johan Bos & Kristina Striegnitz member/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(yolanda,[yolanda,trudy,vincent,jules]). yes ?- member(vincent,[yolanda,trudy,vincent,jules]). yes ?- member(zed,[yolanda,trudy,vincent,jules]). no ?-

27 © Patrick Blackburn, Johan Bos & Kristina Striegnitz member/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(X,[yolanda,trudy,vincent,jules]). X = yolanda; X = trudy; X = vincent; X = jules; no

28 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Arithmetic and Lists How long is a list? – The empty list has length: zero; – A non-empty list has length: one plus length of its tail.

29 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Length of a list in Prolog len([],0). len([_|L],N):- len(L,X), N is X + 1. ?- len([a,b,c,d,e,[a,x],t],X). X=7 yes ?-

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

31 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a Y=X

32 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a † Y=X

33 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a ?- g(b), h(b). X=b † Y=X

34 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a ?- g(b), h(b). X=b ?- h(b). † Y=X

35 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=b ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a ?- g(b), h(b). X=b ?- h(b). † Y=X

36 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: search tree 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). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a ?- g(b), h(b). X=b ?- h(b). † Y=X

37 We have to follow in the start the number of paths, equal to the number of formulae for the query. – In descend, we follow 2 paths as we have 2 formula for descend. – In k(X), we follow 1 path in the start as we have 1 formula.

38 Example Knowledge Base – R1

39 Search tree: related to R1 (query:p(X))

40 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example Knowledge Base – R2 loves(vincent,mia). loves(marsellus,mia). jealous(X,Y):- loves(X,Z), loves(Y,Z).

41 Search tree: related to R2 (query: jealous (X,Y) )

42 Example Knowledge Base – R3

43 Search tree: related to R3 (query: descend(Martha, laura))

44 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Recursive Definitions Prolog predicates can be defined recursively A predicate is recursively defined if one or more rules in its definition refers to itself

45 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 1: Eating isDigesting(X,Y):- justAte(X,Y). isDigesting(X,Y):- justAte(X,Z), isDigesting(Z,Y). justAte(mosquito,blood(john)). justAte(frog,mosquito). justAte(stork,frog). ?-

46 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Picture of the situation X Y justAte isDigesting

47 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Picture of the situation X Y justAte isDigesting X Z justAte isDigesting Y

48 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 1: Eating isDigesting(X,Y):- justAte(X,Y). isDigesting(X,Y):- justAte(X,Z), isDigesting(Z,Y). justAte(mosquito,blood(john)). justAte(frog,mosquito). justAte(stork,frog). ?- isDigesting(stork,mosquito).

49 Assignment Make a search tree for: isDigesting (stork, blood(john))

50 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another recursive definition p:- p. ?-

51 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another recursive definition p:- p. ?- p.

52 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Another recursive definition p:- p. ?- p. ERROR: out of memory

53 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 2: Decendant child(bridget,caroline). child(caroline,donna). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y).

54 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 2: Decendant child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y).

55 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 2: Decendant child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y). ?- descend(anna,donna). no ?-

56 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 2: Decendant child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y). descend(X,Y):- child(X,Z), child(Z,U), child(U,Y). ?-

57 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 2: Decendant child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), descend(Z,Y). ?-

58 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 2: Decendant child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), descend(Z,Y). ?- descend(anna,donna).

59 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree Draw search tree for ?- descend(anna,donna).

60 © Patrick Blackburn, Johan Bos & Kristina Striegnitz descend1.pl child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), descend(Z,Y). ?- descend(A,B). A=anna B=bridget

61 © Patrick Blackburn, Johan Bos & Kristina Striegnitz descend2.pl child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Z), descend(Z,Y). descend(X,Y):- child(X,Y). ?- descend(A,B). A=anna B=emily

62 © Patrick Blackburn, Johan Bos & Kristina Striegnitz descend3.pl child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- descend(Z,Y), child(X,Z). descend(X,Y):- child(X,Y). ?- descend(A,B). ERROR: OUT OF LOCAL STACK

63 © Patrick Blackburn, Johan Bos & Kristina Striegnitz descend4.pl child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- descend(Z,Y), child(X,Z). ?- descend(A,B).

64 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Recursing down lists The member/2 predicate works by recursively working its way down a list – doing something to the head, and then – recursively doing the same thing to the tail This technique is very common in Prolog and therefore very important that you master it So let`s look at another example!

65 © 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 a s, and – the second argument is a list of b s 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

66 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining a2b/2: step 1 Often the best away to solve such problems is to think about the simplest possible case Here it means: the empty list a2b([],[]).

67 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining a2b/2: step 2 Now think recursively! When should a2b/2 decide that two non- empty lists are a list of as and a list of bs of exactly the same length? a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2).

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

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

70 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Testing a2b/2 a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2). ?- a2b([a,t,a,a],[b,b,b,c]). no ?-

71 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Further investigating a2b/2 a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2). ?- a2b([a,a,a,a,a], X). X = [b,b,b,b,b] yes ?-

72 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Further investigating a2b/2 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 ?-

73 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparing Integers Some Prolog arithmetic predicates actually do carry out arithmetic by themselves These are the operators that compare integers

74 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparing Numbers x < y x  y x = y x  y x  y x > y X < Y X =< Y X =:= Y X =\= Y X >= Y X > Y ArithmeticProlog

75 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparison Operators Have the obvious meaning Force both left and right hand arguments to be evaluated ?- 2 < 4+1. yes ?- 4+3 > 5+5. no

76 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Comparison Operators Have the obvious meaning Force both left and right hand arguments to be evaluated ?- 4 = 4. yes ?- 2+2 = 4. no ?- 2+2 =:= 4. yes


Download ppt "CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany."

Similar presentations


Ads by Google