Presentation is loading. Please wait.

Presentation is loading. Please wait.

programmation en logique

Similar presentations


Presentation on theme: "programmation en logique"— Presentation transcript:

1 programmation en logique

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

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

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

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

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

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

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

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

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

11 Predicate Definitions
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 name

12 Clauses Predicate 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 head and sometimes a body. Facts don’t have a body because they are always true. head body

13 Arguments mother(jane,alan).
A 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, or Compound terms (explained in later lectures). Predicate name Arguments

14 Terms: Constants Constants 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’.

15 Terms: Variables Variables 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).

16 Arity The number of arguments a predicate has is called its arity.
greetings 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.

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

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

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

20 Clauses: Facts and Rules
‘if’ ‘provided that’ ‘turnstile’ Head :- Body. This is a rule. Head. This is a fact. Full stop at the end.

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

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

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

24 Contd... This relation represents one ‘direction’ of border:
What about the other? (a) Say border(kent, sussex). border(sussex, kent). 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). (b) Say adjacent(X, Y) :- border(X, Y). adjacent(X, Y) :- border(Y, X). (c) Say border(X, Y) :- border(Y, X).

25 Contd... 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: valid(X, Y) :- adjacent(X, Z), adjacent(Z, Y)

26 Contd... valid(X, Y) :- adjacent(X, Z), adjacent(Z, Y)
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(X, Y) :- adjacent(X, Z), adjacent(Z, Y) ?- valid(wiltshire, sussex). ?- valid(wiltshire, kent). ?- valid(hampshire, hampshire). ?- valid(X, kent). ?- valid(sussex, X). ?- valid(X, Y).

27 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

28 Advantages and Disadvantages:-
-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).


Download ppt "programmation en logique"

Similar presentations


Ads by Google