Presentation is loading. Please wait.

Presentation is loading. Please wait.

Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15

Similar presentations


Presentation on theme: "Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15"— Presentation transcript:

1 Logic Programming Module 2AIT202 Website http://www.wombat.wmin.ac.uk/HSCS/UG/AI/AIT202/AIT202.html Lecturer: Dave Sharp Room: AG15 email: sharpd@wmin.ac.uk phone: 020 7911 5000 ex 4029

2 Assessment: Coursework 1 A sequence of practical exercises issued each week which have to be completed in that week and signed of at the tutorial15 % Coursework 2A practical assignment15% Examinations An observed practical examination (in Week 7)30% An unseen exam paper at the end of the module40%.

3 The Concept of Logic Programming Languages such as Pascal, Modula-2 and C are known as procedural languages or as imperative languages. A program consists of a sequence of instructions telling the computer how to solve your problem. This in turn means that you have to translate your problem into a form that can be represented in the syntax of the language you are using. Frequently you spend more time trying to manipulate the constucts of the language than on considering the problem you are trying to solve.

4 High Level Languages High level programming languages are simply abstractions, which allow us to perform our computing operations without considering the underlying mechanisms that exist at the processor level. One instruction in a high level language may replace ten or twenty at machine level. Once written in an imperative language the compiler will translate the program into machine code, appropriate for the processor we are using and we can use our program just as if we had written it in machine code in the first place.

5 The logic programming paradigm If we consider our program written in a high level imperative language we still find that the underlying number crunching concepts come through. Although we have a high level language, its number crunching roots keep intruding when we would rather be working at a problem oriented level. To avoid the programming errors we have decomposed the program development process into separate analysis, design and implementation stages using methodologies such as SSADM to allow us to work only at the problem solving level.

6 The logic programming paradigm In the case of logic programming we work with a language which is itself much closer to the problems solving level. Although, as in all languages, at the lowest level we are still number crunching, a logic programming language such as Prolog provides a very high level of abstraction so that our program is not a set of instructions, but simple a description of the real world situation in which we are working. We need to take much less account of how to solve the problem but only need to produce a full, logically sound and complete description of the system in which we are working.

7 The logic programming paradigm This technique we will be using is also known as declarative programming The logic programming language Prolog that will be used in this module is a very good example of this programming technique. Declarative programming using a language such as Prolog is particularly good for symbolic programming, that is to say programming using non-numeric data.

8 A Problem to consider Spend a little time considering how you would solve this program in or C++ or Visual Basic We have a set of facts about the games people play of the form  Jon plays rugby  Mary plays hockey... perhaps twenty such facts in all We have some more facts about peoples’ musical ability of the format  Anne plays the trumpet  Jon plays the violin... perhaps twenty more facts with some, but not all, of the people overlapping the list of sports players.

9 A Problem to consider We now decide that in our world the following definitions apply:  Anyone who plays a sport is a sportsman  Anyone who plays a musical instrument is a musician  Anyone who plays more than one sport is an allrounder  Anyone who plays more than one musical instrument is gifted  Anyone who is both an allrounder and gifted is brilliant

10 A Problem to consider The problem we want to solve is to be able to use our program to find solutions to problems such as:  who can play cricket?  who can play the violin?  who can play both rugby and the cello?  who is brilliant?  or even, find me a list of people who have the necessary skills to form a string quartet

11 Logic Programming in Prolog In Prolog we have a very simple syntax which allows us to represent the simple facts and rules which describe our little world. Prolog is basically and interpreted language and therefore once we have described our system we can simple put any of those questions to the Prolog interpreter. The interpreter, using predicate logic, will search for the answers to our questions, which can be derived from our description or the system.

12 Logic Programming in Prolog We do not have to tell it how to search all we need is our description of our system, which we might call a knowledge base, and the question to which we want an answer. The interpreter does the rest. In other words Prolog provides us with a higher level of abstraction towards the problem level. It frees us from the need to consider how to solve the problem and allows us to concentrate on the problem we want to solve.

13 Logic Programming in Prolog The two major differences you will notice when you try to implement an example in Prolog rather than a procedural language are.  The program will be much shorter  It will use far fewer program constructs and will therefore be easier to follow. There is a down side to this, although the program will be relatively short, it may still require some thought so don’t assume you can write 20 line of Prolog as quickly as 20 lines of C.

14 Logic Programming in Prolog Compared with most other languages, the syntax of Prolog uses very few constructs and symbols and therefore it takes very little time to learn enough to write simple programs. There are more complex features which have been built into Prolog to add to its usefulness and these do take a little more effort to master. The most important step in learning Prolog is to put your existing programming ideas to one side and learn to think and work in declarative terms.

15 Let’s look at a sample of Prolog code parent(dave,jon). parent(dave,anna). parent(jim,lauren). parent(sally,lauren). male(dave). male(jon). female(jane). female(anna). mother(X,Y):- parent(X,Y), female(X). The only symbols we have used that are part of Prolog are (),.:- By default Prolog regards anything which begins with a lower case letter as an atom (Atomic constant) and anything which begins with an upper case letter as a variable

16 Let’s look at a sample of Prolog code parent(dave,jon). parent(dave,anna). parent(jim,lauren). parent(sally,lauren). male(dave). male(jon). female(jane). female(anna). mother(X,Y):- parent(X,Y), female(X). The program consist of a number of clauses each ending with a full stop. The clause parent(dave,jon). Is used to represent a fact. The functor parent links the two arguments dave and jon. If you like it defines the connection between them. We decide what this clause means in our world

17 Let’s look at a sample of Prolog code parent(dave,jon). parent(dave,anna). parent(jim,lauren). parent(sally,lauren). male(dave). male(jon). female(jane). female(anna). mother(X,Y):- parent(X,Y), female(X). In the clause male(dave) The functor male defines a property or attribute of its argument dave. Again we decide what this clause means in our world

18 Let’s look at a sample of Prolog code parent(dave,jon). parent(dave,anna). parent(jim,lauren). parent(sally,lauren). male(dave). male(jon). female(jane). female(anna). mother(X,Y):- parent(X,Y), female(X). The clause mother(X,Y):- parent(X,Y), female(X). Defines a rule, if you like think of it as a generalisation. The functor mother defines the connection between the two arguments which are the variables X and Y.

19 Let’s look at a sample of Prolog code parent(dave,jon). parent(dave,anna). parent(jim,lauren). parent(sally,lauren). male(dave). male(jon). female(jane). female(anna). mother(X,Y):- parent(X,Y), female(X). The term mother(X,Y) is the head of the clause The terms parent(X,Y), female(X) are the body of the clause The head and body of the clause are separated by the symbol :-

20 Let’s look at a sample of Prolog code parent(dave,jon). parent(dave,anna). parent(jim,lauren). parent(sally,lauren). male(dave). male(jon). female(jane). female(anna). mother(X,Y):- parent(X,Y), female(X). The head and body of the clause are separated by the symbol :- Think of the symbol :- as meaning if The head of the clause is true if the body of the clause can be proved to be true. Another way of looking at it is to say that the body of the clause (if true) implies the head of the clause.

21 Let’s look at a sample of Prolog code parent(dave,jon). parent(dave,anna). parent(jim,lauren). parent(sally,lauren). male(dave). male(jon). female(jane). female(anna). mother(X,Y):- parent(X,Y), female(X). This is a simplified explanation but to summarise. Facts, things which are always true are represented by clauses which have a head and no body. Rules are represented by a clause which has a head and a body.

22 Let’s look at a sample of Prolog code parent(dave,jon). parent(dave,anna). parent(jim,lauren). parent(sally,lauren). male(dave). male(jon). female(jane). female(anna). mother(X,Y):- parent(X,Y), female(X). When we run our progame we load it into Prolog and then set goals, ( if you like ask it questions ). The interpreter tries to satisfy our goals ( answer our questions ) by matching them with the heads of the clauses in the program. You will probably understand the working of the interpreter much better if you think of it as a pattern matching process


Download ppt "Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15"

Similar presentations


Ads by Google