# Logic Programming in Prolog

## Presentation on theme: "Logic Programming in Prolog"— Presentation transcript:

Logic Programming in Prolog
Lecturer: Dr. Abeer Mahmoud

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. 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. Later exercises are designed to develop skills in applying Prolog in practical situations.

Syllabus Introduction to Prolog and Logic Programming.
Prolog basic constructs: Facts, rules, knowledge base, goals, unification, and instantiation. Prolog syntax: characters, equality and arithmetic. Data Structures: Structures and trees, lists, strings. Control Structures: Backtracking, recursion, cut and failure. Input and output, assertions, consulting. Applications: Databases, Artificial Intelligence Games, natural language processing, meta-interpreters

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.

Software Visual Prolog Free Windows version available
All programs must be coded in the lab

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.

Lecture 1

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

Programming Techniques
Unstructured programming procedural programming modular programming and Object-oriented programming Logic Programming

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.

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.

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.

independent

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.

5-Logic Programming: Prolog
Prolog as a system for querying knowledge bases

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.

Declarative programming
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. 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. Examples: Imperative programming (C), object-oriented programming (Java)

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

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.

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.

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”

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

Prolog can make deductions
Given the facts Ann eats apple. Tamer eats banana. And the rule Jenny eats anything if Tamer eats that thing. Prolog can deduce that Jenny 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.

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.

What is Prolog used for? Good at Poor 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).

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? Facts Rules

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? Example Database: father( john, jim ). mother( jane, jim ). father( jack, john ). parent( Person1, Person2 ) :- father( 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, _ ).

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party.

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?-

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- woman(mia).

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- woman(mia). yes ?-

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- woman(mia). yes ?- playsAirGuitar(jody).

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- woman(mia). yes ?- playsAirGuitar(jody). ?-

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- woman(mia). yes ?- playsAirGuitar(jody). ?- playsAirGuitar(mia). no

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- tattoed(jody).

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- tattoed(jody). no ?-

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- tattoed(jody). ERROR: predicate tattoed/1 not defined. ?-

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- party.

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- party. yes ?-

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- rockConcert.

Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- rockConcert. no ?-

Knowledge Base 2 happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).

Knowledge Base 2 fact happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).

Knowledge Base 2 fact fact happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). fact

Knowledge Base 2 fact fact rule happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). fact rule

Knowledge Base 2 fact fact rule rule happy(yolanda).
listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). fact rule rule

Knowledge Base 2 fact fact rule rule rule happy(yolanda).
listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). fact rule rule rule

Knowledge Base 2 head body happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). head body

Knowledge Base 2 happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). ?-

Knowledge Base 2 happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). ?- playsAirGuitar(mia). yes ?-

Knowledge Base 2 happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). ?- playsAirGuitar(mia). yes ?- playsAirGuitar(yolanda).

Clauses There are five clauses in this knowledge base:
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.

Predicates There are three predicates in this knowledge base:
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

Knowledge Base 3 happy(vincent). listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).

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

Knowledge Base 3 happy(vincent). listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). ?- playsAirGuitar(vincent). no ?-

Knowledge Base 3 happy(vincent). listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). ?- playsAirGuitar(butch). yes ?-

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

Prolog and Logic Clearly Prolog has something to do with logic
Operators Implication :- Conjunction , Disjunction ; Use of modus ponens Negation

Knowledge Base 4 woman(mia). woman(jody). woman(yolanda).
loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).

Prolog Variables woman(mia). woman(jody). woman(yolanda).
loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X).

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

loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). X=mia;

loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). X=mia; X=jody

loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). X=mia; X=jody; X=yolanda

loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). X=mia; X=jody; X=yolanda; no

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

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

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

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

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

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

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

Thank you