Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely...

28
programmation en logique

Transcript of Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely...

programmation en logique

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.

WAT A PROGRAM LOOKS LIKE/* At the Zoo */

elephant(george).elephant(mary).

panda(chi_chi).panda(ming_ming).

dangerous(X) :- big_teeth(X).dangerous(X) :- venomous(X).

guess(X, tiger) :- stripey(X), big_teeth(X), isaCat(X).guess(X, koala) :- arboreal(X), sleepy(X).guess(X, zebra) :- stripey(X), isaHorse(X).

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.

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) of arity 3.components (any terms)

It is possible to depict the term as a tree:

parents

roverfidospot

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 which information needs to be known for the property denoted by the predicate to be true.

Predicate Definitions

Predicate name

ClausesPredicate definitions consist of clauses.

= An individual definition (whether it be a fact or rule).

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

A body can be made up of any number of subgoals (calls to other predicates) and terms.

Arguments also consist of terms, which can be:Constants e.g. jane,Variables e.g. Person1, orCompound terms (explained in later lectures).

Predicate name Arguments

Terms: ConstantsConstants can either be:Numbers:

integers are the usual form (e.g. 1, 0, -1, etc), but floating-point numbers can also be used (e.g.

3.0E7)

Symbolic (non-numeric) constants:always start with a lower case alphabetic

character and contain any mixture of letters, digits, and underscores (but no spaces, punctuation, or an initial capital).

e.g. abc, big_long_constant, x4_3t).

String constants:are anything between single quotes e.g. ‘Like this’.

Terms: VariablesVariables always start with an upper

case alphabetic character or an underscore.

Other than the first character they can be made up of any mixture of letters, digits, and underscores.

e.g. X, ABC, _89two5, _very_long_variable

There are no “types” for variables (or constants) – a variable can take any value.

All Prolog variables have a “local” scope: they only keep the same value within a clause; the

same variable used outside of a clause does not inherit the value (this would be a “global” scope).

Aritygreetings is a predicate with no

arguments.The number of arguments a predicate

has is called its arity. The arity of greetings is zero =

greetings/0

The behaviour of predicates can be made more specific by including more arguments.greetings(hamish) = greetings/1

The predicate can then behave differently depending on the arguments passed to it.

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

Clauses: Facts and Rules

Head :- Body. This is a rule.

Head. This is a fact.

‘if’‘provided that’‘turnstile’

Full stop at the end.

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

(c) Sayborder(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)

SIGNIGICANT FEATURES

Intelligent systems : programs which perform useful tasks by using artificial intelligence techniques

Expert systems : intelligent systems which reproduce decision-making at the level of human expert

Natural language systems : which can analyze and respond to statements made in ordinary language as opposed to approved keywords or menu selections

Relational database systems

Advantages and Disadvantages:-

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. DISADVANTAGES:- -It can be very difficult to design a database that

accurately represents relationships. -Prolog is not best suited to solving complex

arithmetical computations. -Prolog programs are not best suited to the current

PC architecture (sequential execution) and are best optimised on parallel architectures (fifth generation computers).