Top Banner
programmation en logique
28

Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

Dec 31, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

programmation en logique

Page 2: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

LOGIC PROGRAMMING

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

artificial intelligence declarative

Page 3: 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

Page 4: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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.

Page 5: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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.

Page 6: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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

Page 7: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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

Page 8: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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.

Page 9: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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

Page 10: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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

Page 11: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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

Page 12: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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

Page 13: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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

Page 14: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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

Page 15: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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

Page 16: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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.

Page 17: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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…

Page 18: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

Example

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

Procedure for elephant

Predicate

Clauses

Rule

Facts

Page 19: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

Example

?- elephant(george).

yes

?- elephant(jane).

no

Queries

Replies

Page 20: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

Clauses: Facts and Rules

Head :- Body. This is a rule.

Head. This is a fact.

‘if’‘provided that’‘turnstile’

Full stop at the end.

Page 21: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

Body of a (rule) clause contains goals.

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

Head Body

Goals

Page 22: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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:

Page 23: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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.

Page 24: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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

Page 25: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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:

Page 26: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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)

Page 27: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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

Page 28: Programmation en logique. LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative.

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