Download presentation
Presentation is loading. Please wait.
1
Logic Programming in Prolog
Lecturer: Dr. Abeer Mahmoud
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. 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.
3
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
4
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.
5
Software Visual Prolog Free Windows version available
All programs must be coded in the lab
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.
7
Lecture 1
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
9
Programming Techniques
Unstructured programming procedural programming modular programming and Object-oriented programming Logic Programming
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.
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.
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.
13
independent
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.
15
5-Logic Programming: Prolog
Prolog as a system for querying knowledge bases
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.
17
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)
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).
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.
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.
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”
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).
23
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.
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.
25
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).
26
Prolog in English Facts Rules Example Database: Example questions:
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
27
Prolog in Prolog Example Database: 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? 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, _ ).
28
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party.
29
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?-
30
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- woman(mia).
31
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- woman(mia). yes ?-
32
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- woman(mia). yes ?- playsAirGuitar(jody).
33
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- woman(mia). yes ?- playsAirGuitar(jody). ?-
34
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- woman(mia). yes ?- playsAirGuitar(jody). ?- playsAirGuitar(mia). no
35
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- tattoed(jody).
36
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- tattoed(jody). no ?-
37
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- tattoed(jody). ERROR: predicate tattoed/1 not defined. ?-
38
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- party.
39
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- party. yes ?-
40
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- rockConcert.
41
Knowledge Base 1 woman(mia). woman(jody). woman(yolanda).
playsAirGuitar(jody). party. ?- rockConcert. no ?-
42
Knowledge Base 2 happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).
43
Knowledge Base 2 fact happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).
44
Knowledge Base 2 fact fact happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). fact
45
Knowledge Base 2 fact fact rule happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). fact rule
46
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
47
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
48
Knowledge Base 2 head body happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). head body
49
Knowledge Base 2 happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). ?-
50
Knowledge Base 2 happy(yolanda). listens2music(mia).
listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). ?- playsAirGuitar(mia). yes ?-
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).
52
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.
53
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
54
Knowledge Base 3 happy(vincent). listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).
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
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 ?-
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 ?-
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).
59
Prolog and Logic Clearly Prolog has something to do with logic
Operators Implication :- Conjunction , Disjunction ; Use of modus ponens Negation
60
Knowledge Base 4 woman(mia). woman(jody). woman(yolanda).
loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).
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).
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
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;
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
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
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
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).
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 ?-
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).
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 ?-
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).
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).
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 ?-
74
Thank you
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.