Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

Presentation on theme: "Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog."— Presentation transcript:

Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog

2 acknowledgement Patrick Blackburn, Johan Bos & Kristina Striegnitz (lecture notes on logic programming in prolog)

3 Lecture 3

4 Quiz Define the predicate parent_of Sibling (person have the same parent) Grandparent_of Greatgrandparent_of

5 exercise Convert the following paragraph into fact or rule: 1. a person may be a smart buyer for something if the person is a smart and he likes the thing and the thing is valuable and the thing has a discount. 2. Bob likes all kind of game. Football is a game. Anything anyone plays and not killed by is a game.

6 a person may be a smart buyer for something if the person is a smart and he likes the thing and the thing is valuable and the thing has a discount. smart(ahmed). smart(nada). smart(noha). smart(islam). valuable(gold). valuable(books). valuable(leather-jackets). discount(books). discount(perfumes). Smart-buyer(X,Y):- smart(X),likes(X,Y),valuable(Y), discount(Y). likes(ahmed, leather-jackets). likes(ahmed, sun-glasses). likes(soha,sun-glasses). likes(islam,books). likes(nada, perfume).

7 Bob likes all kind of game. Football is a game. Anything anyone plays and not killed by is a game. plays(player1,football). plays(player2,tennis). plays(player3,handball). plays(player4,car-racing). likes(‘Bob’, X):-game(X). game(Anything):- plays(_,Anything),not-killed(Anything) not-killed(football). not-killed(tennis). not-killed(handball).

8 Prolog Syntax What exactly are facts, rules and queries built out of? Terms Simple TermsComplex Terms ConstantsVariables AtomsNumbers Terms Simple TermsComplex Terms ConstantsVariables AtomsNumbers

9 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: :, ;. :-

10 Numbers Integers: 12, -34, 22342 Floats: 34573.3234

11 Variables If we want to get more interest information about fact or rule, we can use variable to get more than Yes/No answer. 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

12 variable name must begin with capital letter. *using variable we can get all possible answer about a particular fact or rule. Variables

13 Variables Always begin with a capital letter –?- likes (john,X). –?- likes (john, Something). But not –?- likes (john,something)

14 Example of usage of variable Facts: likes(john,flowers). likes(john,mary). likes(paul,mary). Question: ?- likes(john,X) Answer: X=flowers and wait ; mary ; no

15 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

16 Complex terms are of the form: functor (argument,..., argument). Functors have to be atoms. Arguments can be any kind of Prolog term, e.g., complex terms. date April131988 date(“Aprile”,13,1988)

17 Examples of complex terms Examples we have seen before: –playsAirGuitar(jody) –loves(vincent, mia) –jealous(marsellus, W)

18 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

19 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

20 Example of 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).

21 summary data type Prolog supports the following data type to define program entries. 1.Integer: to define numerical value like 1, 20, 0,-3,-50, ect. 2.Real: to define the decimal value like 2.4, 3.0, 5,-2.67, ect. 3.Char: to define single character, the character can be of type small letter or capital letter or even of type integer under one condition it must be surrounded by single quota. For example, ‘a’,’C’,’123’. 4.string : to define a sequence of character like “good” i.e define word or statement entries the string must be surrounded by double quota for example “computer”, “134”, “a”. The string can be of any length and type. 5.Symbol: anther type of data type to define single character or sequence of character but it must begin with small letter and don’t surround with single quota or double quota.

22 summary program structure Prolog program structure consists of five segments, not all of them must appear in each program. The following segment must be included in each program predicates, clauses, and goal. 1.Domains: define global parameter used in the program. I= integer C= char S = string R = real 2.Data base: define internal data base generated by the program Database Greater (integer) 3.Predicates: define rule and fact used in the program. Mark(symbol,integer).

23 summary program structure Prolog program structure consists of five segments, not all of them must appear in each program. The following segment must be included in each program predicates, clauses, and goal. 4. Clauses: define the body of the program.. For the above predicates the clauses portion may contain Mark (a, 20). 5.Goal: can be internal or external, internal goal written after clauses portion, external goal supported by the prolog compiler if the program syntax is correct

24 summary mathematical and logical operation

25 summary Other mathematical function

27 write prolog program to read integer value and print it. Example

28 write prolog program that take two integer input us integer and print the greater. Example

29 1.write prolog program that read any phrase then print it. 2.write prolog program that read an integer number then print it after multiplying it by 5. assignment

30 Discuss unification in Prolog –Show how Prolog unification differs from standard unification Explain the search strategy that Prolog uses when it tries to deduce new information from old, using modus ponens

31 Unification Recall previous example, where we said that Prolog unifies woman(X) with woman(mia) thereby instantiating the variable X with the atom mia.

32 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

33 Unification This 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

34 Unification What about the terms: mia and X

35 Unification What about the terms: mia and X woman(Z) and woman(mia)

36 Unification What about the terms: mia and X woman(Z) and woman(mia) loves(mia,X) and loves(X,vincent)

37 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 demonstrated through the use of evidence, supported with an example; specifically defined object through the replacement of some variables with values (Computer Programming)

38 Revised Definition 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.

39 Revised Definition 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)

40 Revised Definition 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.

41 Prolog unification ?- mia = mia. yes ?-

42 Prolog unification ?- mia = mia. yes ?- mia = vincent. no ?-

43 Prolog unification ?- mia = X. X=mia yes ?-

44 How will Prolog respond? ?- X=mia, X=vincent.

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

46 Example with complex terms ?- k(s(g),Y) = k(X,t(k)).

47 Example with complex terms ?- k(s(g),Y) = k(X,t(k)). X=s(g) Y=t(k) yes ?-

48 Example with complex terms ?- k(s(g),t(k)) = k(X,t(Y)).

49 Example with complex terms ?- k(s(g),t(k)) = k(X,t(Y)). X=s(g) Y=k yes ?-

50 book(“prolog”,”A.I”,”Gourge”,10,2000). book(“c++”,”programming”,”Rintice Hill”,5,2001). book(“Expert system”,”A.I”,”Daniel”,5,1994). book1(X,Y,A,b,C). book(A,B,C,D)

51 One last example ?- loves(X,X) = loves(marsellus,mia).

52 Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))).

53 Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?-

54 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 ?-

55 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 ?-

56 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 ?-

57 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 ?-

58 Exercise: unification 1.b

59 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

60 Example f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y).

61 Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y).

62 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). Y=X

63 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). X=a Y=X

64 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

65 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

66 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

67 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

68 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

69 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

70 Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y).

71 Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y).

72 Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). ?- loves(A,C), loves(B,C). X=AY=B

73 Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia X=AY=B

74 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(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia B=vincent X=AY=B

75 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(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia B=vincent B=marsellus X=AY=B

76 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(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia ?- loves(B,mia). A=marsellus C=mia B=vincent B=marsellus X=AY=B

77 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 ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia ?- loves(B,mia). A=marsellus C=mia B=vincent B=marsellus X=AY=B

78 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 ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia ?- loves(B,mia). A=marsellus C=mia B=vincent B=marsellus X=AY=B

79 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 ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia ?- loves(B,mia). A=marsellus C=mia B=vincent B=marsellus X=AY=B

80 Exercises

81 Trace the following goal to find the value of X,Y,W,Z. Fact Mark(a,10). Mark(b,20). Mark(c,30). Goal: Mark(X,Y),Mark(W,Z).

82 Summary of this lecture In this lecture we have –defined unification –looked at the difference between standard unification and Prolog unification –introduced search trees

83 Thank You