programmaing in logic apurva patil gayatri kharavlikar meenakshi borse manjiri bankar nikita alai...

Post on 17-Jan-2016

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

programmaing in logic

Apurva PatilGayatri KharavlikarMeenakshi BorseManjiri BankarNikita AlaiShruti Govilkar

PROgramming in LOGicEmphasis on what rather than

how

Problem in Declarative Form

Basic Machine

Logic Machine

Basic Machine

Logic Machine

LOGIC PROGRAMMING

science of reasoning algorithm = logic + control purely based on rules and facts

artificial intelligence declarative

HISTORYCreated in 1972 by Philippe Roussel

and Alain ColmerauerBased on Robert Kowalski’s procedural

interpretation of Horn clauses

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.

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.

…MOREClauses 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.

WHAT A PROGRAM LOOKS LIKE/* At the Zoo */ bigger(elephant, horse).bigger(elephant, horse).bigger(horse, donkey).bigger(donkey, dog).bigger(donkey, monkey).

-Query:?- bigger(donkey, dog).Yesis_bigger(X, Y) :- bigger(X, Z), is_bigger(Z, Y).

PROLOG IS A DECLARATIVE LANGUAGEClauses 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.

A Typical Prolog programCompute_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.

Fundamentals(absolute basics for writing Prolog Programs)

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

More facts

Predicate Interpretation

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

RulesStatements about objects and their

relationshipsExpess

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

Syntax<head> :- <body>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 ‘.’.

Question Answering in presence of rulesFacts

◦male (ram).◦male (shyam).◦female (sita).◦female (gita).◦parents (shyam, gita, ram).◦parents (sita, gita, ram).

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)

Complete Syntax of Terms Term

Constant VariableCompound Term

Atom Numberalpha17gross_payjohn_smithdyspepsia+=/=’12Q&A’

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

XGross_payDiagnosis_257_

Names an individual Stands for an individualunable to be named when program is written

Names an individualthat has parts

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

VariablesVariables always start with an

upper case alphabetic character or an underscore

◦?- likes (john,X).◦?- likes (john, Something).

But not ◦?- likes (john,something)

ConjunctionsUse ‘,’ 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).

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

ClausesPredicate definitions consist of clauses.

e.g. mother(jane,alan). = Fact parent(P1,P2):- mother(P1,P2). = Rule

A clause consists of a headand sometimes a body.

Facts don’t have a body because they are always true.

head body

ArgumentsA 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, orCompound terms

Predicate name Arguments

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

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=food2. Satisfy likes(john,food)

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 fails2. Return to marked place and try to resatisfy the first goal

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=tea2. Attempt to satisfy the likes(john,tea)

Backtracking (continued)

likes(mary,X),likes(john,X)

likes(mary,food)likes(mary,tea)likes(john,tea)likes(john,mary)

1. Second goal also suceeds2. Prolog notifies success and waits for a reply

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…

Example

elephant(george).elephant(mary).elephant(X) :- grey(X), mammal(X), hasTrunk(X).

Procedure for elephant

Predicate

Clauses

Rule

Facts

Example

?- elephant(george).

yes

?- elephant(jane).

no

Queries

Replies

Body of a (rule) clause contains goals.

likes(mary, X) :- human(X), honest(X).

Head Body

Goals

Interpretation of ClausesClauses can be given a declarative reading or a

procedural reading.

H :- G1, G2, …, Gn.

“That H is provable follows from goals G1, G2, …, Gn being provable.”

“To execute procedure H, the procedures called by goals G1, G2, …, Gn are executed first.”

Declarative reading:

Procedural reading:

Form of clause:

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.

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 representsone ‘direction’ of border: What about the other?

(a) Say border(kent, sussex).border(sussex, kent).

(b) Sayadjacent(X, Y) :- border(X, Y).adjacent(X, Y) :- border(Y, X).

Contd...

valid(X, Y) :- adjacent(X, Z), adjacent(Z, Y)

Now a somewhat strange type of discount ticket. For theticket to be valid, one must pass through an intermediate county.

A valid ticket between a start and end county obeys the following rule:

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)

How to save prolog file:Save as Filename.plConflict with PerlAlternative extension can be

filename.pro or filename.prNo capitalsC:/MyDocument //errorC:/mydocumentTo exit prolog- ‘halt’

Different editors:SWI Prolog

◦Extra libraries◦Built in predicates

GNU Prolog◦Support constraints logic

programmingVisual Prolog

◦A complete development environment for object oriented extension to prolog

And many more…

SIGNIFICANT FEATURESSimplicity

Regularity

Intelligent systems

Expert systems

Natural language systems

Relational database systems

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 logicMany algorithms become trivially

simple to implement

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

APPLICATIONS:

Target User:◦Expert Systems (Knowledge

Representation and Inferencing)◦Natural Language Processing◦Relational Databases◦Artificial intelligence

Thank u..!!

top related