Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programmaing in logic Apurva Patil Gayatri Kharavlikar Meenakshi Borse Manjiri Bankar Nikita Alai Shruti Govilkar.

Similar presentations


Presentation on theme: "Programmaing in logic Apurva Patil Gayatri Kharavlikar Meenakshi Borse Manjiri Bankar Nikita Alai Shruti Govilkar."— Presentation transcript:

1 programmaing in logic Apurva Patil Gayatri Kharavlikar Meenakshi Borse Manjiri Bankar Nikita Alai Shruti Govilkar

2 PROgramming in LOGic Emphasis on what rather than how Problem in Declarative Form Basic Machine Logic Machine Basic Machine Logic Machine

3 LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative

4 HISTORY Created in 1972 by Philippe Roussel and Alain Colmerauer Based on Robert Kowalski’s procedural interpretation of Horn clauses

5 TWO TYPES OF PROGRAMMING LANGUAGES Procedural (BASIC, Fortran, C++, Java) Declarative (LISP, Prolog) In procedural programming, we tell the computer how to solve a problem. In declarative programming, we tell the computer what problem we want solved.

6 INTRODUCTION TO PROLOG Prolog is the most widely used language to have been inspired by logic programming research. Some features: Prolog uses logical variables. These are not the same as variables in other languages. Programmers can use them as ‘holes’ in data structures that are gradually filled in as computation proceeds.

7 …MORE Clauses provide a convenient way to express case analysis and nondeterminism. Sometimes it is necessary to use control features that are not part of ‘logic’. A Prolog program can also be seen as a relational database containing rules as well as facts.

8 WHAT A PROGRAM LOOKS LIKE /* At the Zoo */ bigger(elephant, horse). bigger(horse, donkey). bigger(donkey, dog). bigger(donkey, monkey). -Query: ?- bigger(donkey, dog). Yes is_bigger(X, Y) :- bigger(X, Z), is_bigger(Z, Y).

9 PROLOG IS A DECLARATIVE LANGUAGE Clauses are statements about what is true about a problem, instead of instructions how to accomplish the solution. The Prolog system uses the clauses to work out how to accomplish the solution by searching through the space of possible solutions. Not all problems have pure declarative specifications. Sometimes extralogical statements are needed.

10 A Typical Prolog program Compute_length ([],0). Compute_length ([Head|Tail], Length):- Compute_length (Tail,Tail_length), Length is Tail_length+1. High level explanation: The length of a list is 1 plus the length of the tail of the list, obtained by removing the first element of the list. This is a declarative description of the computation.

11 Fundamentals (absolute basics for writing Prolog Programs)

12 Facts John likes Mary ◦ like(john,mary) Names of relationship and objects must begin with a lower-case letter. Relationship is written first (typically the predicate of the sentence). Objects are written separated by commas and are enclosed by a pair of round brackets. The full stop character ‘.’ must come at the end of a fact.

13 More facts PredicateInterpretation valuable(gold)Gold is valuable. owns(john,gold)John owns gold. father(john,mary)John is the father of Mary gives (john,book,mary)John gives the book to Mary

14 Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain). ◦ Generalizations  All men are mortal  mortal(X) :- man(X). ◦ Definitions  An animal is a bird if it has feathers  bird(X) :- animal(X), has_feather(X).

15 Syntax :- Read ‘:-’ as ‘if’. E.G. ◦ likes(john,X) :- likes(X,cricket). ◦ “John likes X if X likes cricket”. ◦ i.e., “John likes anyone who likes cricket”. Rules always end with ‘.’.

16 Question Answering in presence of rules Facts ◦ male (ram). ◦ male (shyam). ◦ female (sita). ◦ female (gita). ◦ parents (shyam, gita, ram). ◦ parents (sita, gita, ram).

17 Question Answering: Y/N type: is sita the sister of shyam? female(sita) parents(sita,M,F)parents(shyam,M,F) parents(sita,gita,ram) parents(shyam,gita,ram) success ?- sister_of (sita, shyam)

18 Complete Syntax of Terms Term Constant Variable Compound Term AtomNumber alpha17 gross_pay john_smith dyspepsia + =/= ’12Q&A’ 0 1 57 1.618 2.04e-27 -13.6 likes(john, mary) book(dickens, Z, cricket) f(x) [1, 3, g(a), 7, 9] -(+(15, 17), t) 15 + 17 - t X Gross_pay Diagnosis _257 _ Names an individual Stands for an individual unable to be named when program is written Names an individual that has parts

19 Compound Terms parents(spot, fido, rover) The parents of Spot are Fido and Rover. Functor (an atom) components (any terms) It is possible to depict the term as a tree: parents roverfidospot

20 Variables Variables always start with an upper case alphabetic character or an underscore ◦ ?- likes (john,X). ◦ ?- likes (john, Something). But not ◦ ?- likes (john,something)

21 Conjunctions Use ‘,’ and pronounce it as and. Example ◦ Facts:  likes(mary,food).  likes(mary,tea).  likes(john,tea).  likes(john,mary). ?-  likes(mary,X),likes(john,X).

22 Both facts and rules are predicate definitions. ‘Predicate’ is the name given to the word occurring before the bracket in a fact or rule: parent(jane,alan). By defining a predicate you are specifying relationship between objects. Predicate Definitions Predicate name

23 Clauses Predicate definitions consist of clauses. e.g. mother(jane,alan). = Fact parent(P1,P2):- mother(P1,P2). = Rule A clause consists of a head and sometimes a body. Facts don’t have a body because they are always true. head body

24 Arguments A predicate head consists of a predicate name and sometimes some arguments contained within brackets and separated by commas. mother(jane,alan). Arguments also consist of terms, which can be : Constants e.g. jane, Variables e.g. Person1, or Compound terms Predicate name Arguments

25 Arity The number of arguments a predicate has is called its arity. greetings is a predicate with no arguments. The arity of greetings is zero = greetings/0 By including more arguments predicates can be made more specific. greetings(hamish) = greetings/1 The predicate can then behave differently depending on the arguments passed to it.

26 Backtracking (an inherent property of prolog programming) likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john,tea) likes(john,mary) 1. First goal succeeds. X=food 2. Satisfy likes(john,food)

27 Backtracking (continued) Returning to a marked place and trying to resatisfy is called Backtracking likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john,tea) likes(john,mary) 1. Second goal fails 2. Return to marked place and try to resatisfy the first goal

28 Backtracking (continued) likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john,tea) likes(john,mary) 1. First goal succeeds again, X=tea 2. Attempt to satisfy the likes(john,tea)

29 Backtracking (continued) likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john,tea) likes(john,mary) 1. Second goal also suceeds 2. Prolog notifies success and waits for a reply

30 Structure of Programs Programs consist of procedures. Procedures consist of clauses. Each clause is a fact or a rule. Programs are executed by posing queries. An example…

31 Example elephant(george). elephant(mary). elephant(X) :- grey(X), mammal(X), hasTrunk(X). Procedure for elephant Predicate Clauses Rule Facts

32 Example ?- elephant(george). yes ?- elephant(jane). no Queries Replies

33 Body of a (rule) clause contains goals. likes(mary, X) :- human(X), honest(X). HeadBody Goals

34 Interpretation of Clauses Clauses can be given a declarative reading or a procedural reading. H :- G 1, G 2, …, G n. “That H is provable follows from goals G 1, G 2, …, G n being provable.” “To execute procedure H, the procedures called by goals G 1, G 2, …, G n are executed first.” Declarative reading: Procedural reading: Form of clause:

35 EXAMPLE berkshire wiltshire surrey hampshire sussex kent How to represent this relation? Note that borders are symmetric. (a) Representing a symmetric relation. (b) Implementing a strange ticket condition.

36 Contd... border(sussex, kent). border(sussex, surrey). border(surrey, kent). border(hampshire, sussex). border(hampshire, surrey). border(hampshire, berkshire). border(berkshire, surrey). border(wiltshire, hampshire). border(wiltshire, berkshire). This relation represents one ‘direction’ of border: What about the other? (a) Say border(kent, sussex). border(sussex, kent). (b) Say adjacent(X, Y) :- border(X, Y). adjacent(X, Y) :- border(Y, X).

37 Contd... valid(X, Y) :- adjacent(X, Z), adjacent(Z, Y) Now a somewhat strange type of discount ticket. For the ticket to be valid, one must pass through an intermediate county. A valid ticket between a start and end county obeys the following rule:

38 Contd... border(sussex, kent). border(sussex, surrey). border(surrey, kent). border(hampshire, sussex). border(hampshire, surrey). border(hampshire, berkshire). border(berkshire, surrey). border(wiltshire, hampshire). border(wiltshire, berkshire). adjacent(X, Y) :- border(X, Y). adjacent(X, Y) :- border(Y, X). ?- valid(wiltshire, sussex). ?- valid(wiltshire, kent). ?- valid(hampshire, hampshire). ?- valid(X, kent). ?- valid(sussex, X). ?- valid(X, Y). valid(X, Y) :- adjacent(X, Z), adjacent(Z, Y)

39 How to save prolog file: Save as Filename.pl Conflict with Perl Alternative extension can be filename.pro or filename.pr No capitals C:/MyDocument//error C:/mydocument To exit prolog- ‘halt’

40 Different editors: SWI Prolog ◦ Extra libraries ◦ Built in predicates GNU Prolog ◦ Support constraints logic programming Visual Prolog ◦ A complete development environment for object oriented extension to prolog And many more…

41 SIGNIFICANT FEATURES Simplicity Regularity Intelligent systems Expert systems Natural language systems Relational database systems

42 ADVANTAGES:- Logic based languages are able to represent the real world more accurately. Prolog is able to derive new rules from the existing rules contained within the knowledge base. Strong ties to formal logic Many algorithms become trivially simple to implement

43 DISADVANTAGES:- -It can be very difficult to design a database that accurately represents relationships. Prolog is not best suited to solving complex arithmetical computations. Complicated syntax Difficult to understand programs at first sight Prolog programs are not best suited to the current PC architecture (sequential execution) and are best optimised on parallel architectures (fifth generation computers).

44 APPLICATIONS: Target User: ◦ Expert Systems (Knowledge Representation and Inferencing) ◦ Natural Language Processing ◦ Relational Databases ◦ Artificial intelligence

45 Thank u..!!


Download ppt "Programmaing in logic Apurva Patil Gayatri Kharavlikar Meenakshi Borse Manjiri Bankar Nikita Alai Shruti Govilkar."

Similar presentations


Ads by Google