Top Banner
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog
74

Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

Apr 01, 2015

Download

Documents

Jennifer Camber
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

Lecturer: Dr. Abeer Mahmoud

Logic Programming in Prolog

Page 2: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

2

Outline

The course consists of taught material and practical work.

The taught material introduces the fundamentals of the Prolog programming language.

Practical work consists of programming exercises in Prolog.

o Early exercises are designed to develop a core understanding of programming in Prolog, especially:

the importance of unification in the understanding of Prolog; the nature of search in Prolog and alternative search

strategies; common recursive program structures, especially for list

processing; design of Prolog programs.

o Later exercises are designed to develop skills in applying Prolog in practical situations.

Page 3: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

3

Introduction to Prolog and Logic Programming. Prolog basic constructs:

o Facts, rules, knowledge base, goals, unification, and instantiation. Prolog syntax:

o characters, equality and arithmetic. Data Structures:

o Structures and trees, lists, strings. Control Structures:

o Backtracking, recursion, cut and failure. Input and output, assertions, consulting. Applications: Databases, Artificial Intelligence

o Games, natural language processing, meta-interpreters

Syllabus

Page 4: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

Text Book

Bratko, I., Prolog Programming for Artificial Intelligence (3rd edition), 2001.

Clocksin, W.F. and Mellish, C.S., Programming in Prolog: Using the ISO Standard (5th edition), 2003.

Sterling, L. and Shapiro, E., The Art of Prolog (Second edition), 1994.

Page 5: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

5

Software

• Visual Prolog

• Free Windows version available

• All programs must be coded in the lab

Page 6: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

6

Prolog When and Why???

• Prolog stands for programming in logic.• An idea emerged in 1970s to use logic as a

programming language.• The early developers are Robert Kowlaski, Maarten

van and Alain colmeraure.• Prolog introduces the descriptive or declarative view. • It alters the way of thinking about problems.• Programming in prolog is an exciting intellectual

challenge.

Page 7: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

7

Lecture 1

Page 8: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

8

Aim of this lecture

• What is logic programming?• Give some simple examples of Prolog

programs• Discuss the three basic constructs in Prolog:

– Facts– Rules– Queries

• Begin the systematic study of Prolog by defining terms, atoms, and variables

Page 9: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

9

Programming Techniques

1. Unstructured programming

2. procedural programming

3. modular programming and

4. Object-oriented programming

5. Logic Programming

Page 10: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

10

1-Unstructured Programming

• writing small and simple programs consisting only of one main program.

• The whole program is written in one partition, • when any part of the code is needed within

the same program again, it must be re-written.

• The program became insufficiently large.

Page 11: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

11

2-Procedural Programming

• Sequence of statements can be combined into one single piece of code called procedure.

• Procedures must be written within the same module as the main program, otherwise, the main program will not identify it.

• A procedure call is used to invoke the procedure.

• After the sequence is processed, the flow of the program proceeds right after the position where the call was done.

• To enable usage of general procedures or groups of procedures in other programs, they must be separately re-defined.

Page 12: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

12

3-Modular Programming

• In modular programming procedures of a common functionality are grouped together into separate modules.

• A program therefore no longer consists of only one single part. It is now divided into several smaller parts which interact through procedure calls and which form the whole program

• Each module can have its own data. This allows each module to manage an internal state, which is modified by calls to procedures of this module. However, there is only one state per module and each module exist at most once in the whole program.

Page 13: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

13

independent

Page 14: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

14

4- Object-Oriented Programming

• In object-oriented programming, data are gathered in a list of objects.

• Object-oriented programming (OOP) is a programming language model organized around "objects" rather than "actions" and data rather than logic

• Instead of calling a procedure, we send a message to the objects. Each object implements its own module allowing for example many lists of the same object to coexist.

• Each object is responsible to initialize and destroy itself correctly. Consequently, there is no longer the need to explicitly call a creation or termination procedure.

Page 15: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

15

5-Logic Programming: Prolog

• Prolog as a system for querying knowledge bases

Page 16: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

16

5-Logic Programming: Prolog

• Visual Prolog is a fifth-generation language that propels computer programming into a new dimension of ease and safety .

• Visual Prolog is a declarative language. This means, that given the necessary facts and rules,

• Visual Prolog will use deductive reasoning to solve your programming problems.

• Prolog was originally designed to be an AI language, and it is very well suited for expert systems, planning systems and similar AI applications.

Page 17: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

17

• Logic programming is a declarative style of programming. The programmer says what they want to compute, but does not explicitly specify how to compute it.

• It is up to the interpreter (compiler/implementation) to figure out how to perform the computation requested.

o Examples: Logic programming (Prolog), database query languages (SQL), functional programming (Haskell)

• In contrast, in a procedural style of programming, the program explicitly describes the individual steps of computation.

o Examples: Imperative programming (C), object-oriented programming (Java)

Declarative programming

Page 18: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

18

• Programming languages are of two kinds:– Procedural (BASIC, ForTran, C++, Pascal, Java);– Declarative (LISP, Prolog, ML).

• In procedural programming, we tell the computer how to solve a problem.

• In declarative programming, we tell the computer what problem we want solved.

• (However, in Prolog, we are often forced to give clues as to the solution method).

Page 19: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

19

Programming language

procedural Programming like C

declarative Programming like prolog

programs are made up not of commands to be executed, but of definitions and statements about the problem to be solved.

program is a collection of instructions for carrying out some computing task.

Page 20: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

20

What is Prolog?

• Prolog is a computer programming language that is used for solving

• problems involves objects and relationships between objects.

• Example:

• “John owns the book”

• Owns (john,book) relationship(object1,object2)

• The relationship has a specific order, johns own the book, but the book dose not owns john, and this relationship and its representation above called fact.

Page 21: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

21

How Does Prolog Differ From Other Languages?

Prolog program essentially consists of a list of logical statements, – either in the form of facts, such as:

“It is raining today.”– Or in the form of rules, such as:

“You will get wet if it is raining and you forget your umbrella”

Page 22: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

22

Components need to run a Prolog program

•a Prolog system (compiler or interpreter)includes

• an internal stack• a matching algorithm• a mechanism for placing and removing

items on and from the stack (scheduling algorithm).

Page 23: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

23

Prolog can make deductions

• Given the factsAnn eats apple.Tamer eats banana.

• And the ruleJenny eats anything if Tamer eats that thing.

• Prolog can deduce thatJenny eats banana.

• You can give the Prolog program a goal, for example:Find every thing that Ann eats.

• And Prolog will use its deductive ability to find all solutions to the problem.

Page 24: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

24

Prolog program execution is controlled automatically

• When a Visual Prolog program is executed, the system tries to find all possible sets of values that satisfy the given goal.

• During execution, results may be displayed, or the user may be prompted to type in some data.

• Visual Prolog uses a backtracking mechanism that, once one solution has been found, causes Prolog to re-evaluate any assumptions made to see if some new variable values will provide new solutions.

Page 25: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

25

What is Prolog used for?

• Good at– Grammars and Language processing,– Knowledge representation and reasoning,– Unification, – Pattern matching,– Planning and Search.

• i.e. Prolog is good at Symbolic AI.

• Poor at:– Repetitive number crunching,– Representing complex data structures,– Input/Output (interfaces).

Page 26: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

26

Prolog in English

Example Database:

John is the father of Jim.Jane is the mother of Jim.

Jack is the father of John.

Person 1 is a parent of Person 2 if Person 1 is the father of Person 2 or Person 1 is the mother of Person 2.

Person 1 is a grandparent of Person 2 if some Person 3 is a parent of Person 2 and

Person 1 is a parent of Person 3.

Example questions:

Who is Jim's father?Is Jane the mother of Fred?

Is Jane the mother of Jim? Does Jack have a grandchild?

Page 27: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

27

Prolog in Prolog

Example Database:

father( john, jim ).mother( jane, jim ).father( jack, john ).

parent( Person1, Person2 ) :- father( Person1,

Person2 ).parent( Person1, Person2 ) :-

mother( Person1, Person2 ).

grandparent( Person1, Person2 ) :- parent( Person3, Person2

), parent( Person1, Person3

).

Example questions:

?- father( Who, jim ).?- mother( jane, fred ).?- mother( jane, jim ).?- grandparent( jack, _ ).

Example Database:

John is the father of Jim.Jane is the mother of Jim.

Jack is the father of John.

Person 1 is a parent of Person 2 if Person 1 is the father of Person 2 or Person 1 is the mother of Person 2.

Person 1 is a grandparent of Person 2 if some Person 3 is a parent of Person 2 and

Person 1 is a parent of Person 3.

Example questions:

Who is Jim's father?Is Jane the mother of Fred?

Is Jane the mother of Jim? Does Jack have a grandchild?

Page 28: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

28

Knowledge Base 1

woman(mia).

woman(jody).

woman(yolanda).

playsAirGuitar(jody).

party.

Page 29: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

29

Knowledge Base 1

woman(mia).

woman(jody).

woman(yolanda).

playsAirGuitar(jody).

party.

?-

Page 30: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

30

Knowledge Base 1

woman(mia).

woman(jody).

woman(yolanda).

playsAirGuitar(jody).

party.

?- woman(mia).

Page 31: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

31

Knowledge Base 1

woman(mia).

woman(jody).

woman(yolanda).

playsAirGuitar(jody).

party.

?- woman(mia).

yes

?-

Page 32: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

32

Knowledge Base 1

woman(mia).

woman(jody).

woman(yolanda).

playsAirGuitar(jody).

party.

?- woman(mia).

yes

?- playsAirGuitar(jody).

Page 33: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

33

Knowledge Base 1

woman(mia).

woman(jody).

woman(yolanda).

playsAirGuitar(jody).

party.

?- woman(mia).

yes

?- playsAirGuitar(jody).

yes

?-

Page 34: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

34

Knowledge Base 1

woman(mia).

woman(jody).

woman(yolanda).

playsAirGuitar(jody).

party.

?- woman(mia).

yes

?- playsAirGuitar(jody).

yes

?- playsAirGuitar(mia).

no

Page 35: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

35

Knowledge Base 1

woman(mia).

woman(jody).

woman(yolanda).

playsAirGuitar(jody).

party.

?- tattoed(jody).

Page 36: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

36

Knowledge Base 1

woman(mia).

woman(jody).

woman(yolanda).

playsAirGuitar(jody).

party.

?- tattoed(jody).

no

?-

Page 37: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

37

Knowledge Base 1

woman(mia).

woman(jody).

woman(yolanda).

playsAirGuitar(jody).

party.

?- tattoed(jody).

ERROR: predicate tattoed/1 not defined.

?-

Page 38: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

38

Knowledge Base 1

woman(mia).

woman(jody).

woman(yolanda).

playsAirGuitar(jody).

party.

?- party.

Page 39: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

39

Knowledge Base 1

woman(mia).

woman(jody).

woman(yolanda).

playsAirGuitar(jody).

party.

?- party.

yes

?-

Page 40: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

40

Knowledge Base 1

woman(mia).

woman(jody).

woman(yolanda).

playsAirGuitar(jody).

party.

?- rockConcert.

Page 41: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

41

Knowledge Base 1

woman(mia).

woman(jody).

woman(yolanda).

playsAirGuitar(jody).

party.

?- rockConcert.

no

?-

Page 42: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

42

Knowledge Base 2

happy(yolanda).

listens2music(mia).

listens2music(yolanda):- happy(yolanda).

playsAirGuitar(mia):- listens2music(mia).

playsAirGuitar(yolanda):- listens2music(yolanda).

Page 43: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

43

Knowledge Base 2

happy(yolanda).

listens2music(mia).

listens2music(yolanda):- happy(yolanda).

playsAirGuitar(mia):- listens2music(mia).

playsAirGuitar(yolanda):- listens2music(yolanda).

fact

Page 44: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

44

Knowledge Base 2

happy(yolanda).

listens2music(mia).

listens2music(yolanda):- happy(yolanda).

playsAirGuitar(mia):- listens2music(mia).

playsAirGuitar(yolanda):- listens2music(yolanda).

factfact

Page 45: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

45

Knowledge Base 2

happy(yolanda).

listens2music(mia).

listens2music(yolanda):- happy(yolanda).

playsAirGuitar(mia):- listens2music(mia).

playsAirGuitar(yolanda):- listens2music(yolanda).

factfact

rule

Page 46: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

46

Knowledge Base 2

happy(yolanda).

listens2music(mia).

listens2music(yolanda):- happy(yolanda).

playsAirGuitar(mia):- listens2music(mia).

playsAirGuitar(yolanda):- listens2music(yolanda).

factfact

rulerule

Page 47: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

47

Knowledge Base 2

happy(yolanda).

listens2music(mia).

listens2music(yolanda):- happy(yolanda).

playsAirGuitar(mia):- listens2music(mia).

playsAirGuitar(yolanda):- listens2music(yolanda).

factfact

rulerule

rule

Page 48: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

48

Knowledge Base 2

happy(yolanda).

listens2music(mia).

listens2music(yolanda):- happy(yolanda).

playsAirGuitar(mia):- listens2music(mia).

playsAirGuitar(yolanda):- listens2music(yolanda).

head body

Page 49: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

49

Knowledge Base 2

happy(yolanda).

listens2music(mia).

listens2music(yolanda):- happy(yolanda).

playsAirGuitar(mia):- listens2music(mia).

playsAirGuitar(yolanda):- listens2music(yolanda).

?-

Page 50: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

50

Knowledge Base 2

happy(yolanda).

listens2music(mia).

listens2music(yolanda):- happy(yolanda).

playsAirGuitar(mia):- listens2music(mia).

playsAirGuitar(yolanda):- listens2music(yolanda).

?- playsAirGuitar(mia).

yes

?-

Page 51: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

51

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

Page 52: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

52

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.

Page 53: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

53

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

Page 54: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

54

Knowledge Base 3

happy(vincent).

listens2music(butch).

playsAirGuitar(vincent):- listens2music(vincent), happy(vincent).

playsAirGuitar(butch):- happy(butch).

playsAirGuitar(butch):- listens2music(butch).

Page 55: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

55

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

Page 56: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

56

Knowledge Base 3

happy(vincent).

listens2music(butch).

playsAirGuitar(vincent):- listens2music(vincent), happy(vincent).

playsAirGuitar(butch):- happy(butch).

playsAirGuitar(butch):- listens2music(butch).

?- playsAirGuitar(vincent).

no

?-

Page 57: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

57

Knowledge Base 3

happy(vincent).

listens2music(butch).

playsAirGuitar(vincent):- listens2music(vincent), happy(vincent).

playsAirGuitar(butch):- happy(butch).

playsAirGuitar(butch):- listens2music(butch).

?- playsAirGuitar(butch).

yes

?-

Page 58: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

58

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

Page 59: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

59

Prolog and Logic

• Clearly Prolog has something to do with logic

• Operators– Implication :-– Conjunction ,– Disjunction ;

• Use of modus ponens

• Negation

Page 60: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

60

Knowledge Base 4

woman(mia).woman(jody).woman(yolanda).

loves(vincent, mia).loves(marsellus, mia).loves(pumpkin, honey_bunny).loves(honey_bunny, pumpkin).

Page 61: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

61

Prolog Variables

woman(mia).woman(jody).woman(yolanda).

loves(vincent, mia).loves(marsellus, mia).loves(pumpkin, honey_bunny).loves(honey_bunny, pumpkin).

?- woman(X).

Page 62: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

62

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

Page 63: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

63

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;

Page 64: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

64

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

Page 65: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

65

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=yolanda

Page 66: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

66

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=yolanda;

no

Page 67: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

67

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

Page 68: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

68

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

yes

?-

Page 69: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

69

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

Page 70: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

70

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

?-

Page 71: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

71

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

Page 72: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

72

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

Page 73: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

73

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

?-

Page 74: Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.

74