Presentation on theme: "Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog."— Presentation transcript:
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. oEarly 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. oLater exercises are designed to develop skills in applying Prolog in practical situations.
3 Introduction to Prolog and Logic Programming. Prolog basic constructs: oFacts, rules, knowledge base, goals, unification, and instantiation. Prolog syntax: ocharacters, equality and arithmetic. Data Structures: oStructures and trees, lists, strings. Control Structures: oBacktracking, recursion, cut and failure. Input and output, assertions, consulting. Applications: Databases, Artificial Intelligence oGames, natural language processing, meta-interpreters Syllabus
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.
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
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.
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 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. oExamples: 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. oExamples: Imperative programming (C), object- oriented programming (Java) Declarative programming
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 –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 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?
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?
28 Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
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.
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