26
Prolog programmation en logique

About prolog History Symbolic Programming Language Logic Programming Language Declarative Programming Language

Embed Size (px)

Citation preview

Page 1: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

Prologprogrammation en logique

Page 2: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

About prolog

HistorySymbolic Programming LanguageLogic Programming LanguageDeclarative Programming Language

Page 3: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

Prolog Program

A Prolog program is defined by a set of Predicates

Each Predicate has a unique Functor and Arity

parent(Parent, Child)

a predicate whose functor is “parent” with arity 2.

• Each Predicate is defined by a sequence of Clauses:

A Clause is either a Factor a Rule

Page 4: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

Defining relations by facts

parent( pam, bob). % Pam is a parent of Bob parent( tom, bob).

parent( tom, liz).

parent( bob, ann).

parent( bob, pat).

parent( pat, jim).

pam

lizbob

jim

patann

tom

Page 5: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

Term

• A Term is either: Atomic, Variable, or Compound Atomic terms can be either atoms or numbers

Atoms can be a sequence of alphanumeric (including ‘_’) characters starting with a lower case letter, or a sequence of any characters embedded in single quote marks

Variables start with a capital letter or _ character

Variables that occur with the same name in the same clause represent the same variable, except for “_” (The anonymous variable, which always represents a unique variable).

A compound term is a structure with a functor and N arguments

Page 6: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

Defining relations by rules

• Rules have:• A condition part (body)

• the right-hand side of the rule

• A conclusion part (head)• the left-hand side of the rule

• Syntax:concl(…) :- cond1(…), … , condN(…).

Page 7: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

Rules

• Define the “offspring” relation:• Fact: offspring( liz, tom).

• Rule: offspring( Y, X) :- parent( X, Y).

pam

lizbob

jim

patann

tom

X

Y

parent offspring

Page 8: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

• mother( X, Y) :- parent( X, Y), female( X).

• grandparent( X, Z) :-

parent( X, Y), parent( Y, Z).

parent

X

Y

mother

female

Z

parent

parent

X

Y grandparent

Page 9: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

• Facts: • declare things that are always true• facts are clauses that have a head and the empty body

• Rules: • declare things that are true depending on a given condition• rules have the head and the (non-empty) body

• Questions: • the user can ask the program what things are true• questions only have the body

• ?- parent( bob, pat).• ?- parent( bob, pat), parent( pat, jim).

pam

lizbob

jim

patann

tom

Page 10: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

BUILT-IN Predicate: consult/1

• consult(‘code.pl’).• reconsult(‘code.pl’).• [‘code.pl’].

Page 11: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

Structural Induction and Recursion

• e.g. simple arithmetic using compound terms to represent integers:• s(0). % 1

• s(s(0)). % 2

• s(s(s(0))). % 3

• …

is_number(0).

Is_number(s(N)):- number(N).

Page 12: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

• predecessor( X, Z):- parent( X, Z).• predecessor( X, Z):- parent( X, Y), predecessor( Y, Z).

parent

X

Y

predecessor

Z

parent

parent

X

Y predecessor

Y2

parent

parent

X

Y1

predecessor

Z

parent

Page 13: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

How Prolog works

• To answer a question, Prolog tries to satisfy all the goals.

• To satisfy a goal means to demonstrate that the goal is true, assuming that the relations in the program is true.

• Prolog accepts facts and rules as a set of axioms, and the user’s question as a conjectured theorem.

• For now; lets have a pragmatic view…

Page 14: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

• To find out when our question can be true; Prolog tries to prove it; to satisfy all the goals.

• To satisfy a goal means to demonstrate that the goal is true, assuming that the relations in the program are true.

• So our program -facts and rules we defined- would be axioms, and the user’s question is the conjectured theorem.

• A rough sketch of prolog’s algorithm would be:prove(Query):

if Query is empty succeed else choose it’s first goal, G.

for each clause C of the program whose head matches G:

make NewQuery from Query by replacing G with C’s body.

prove(NewQuery).

Page 15: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

And-Or Tree

• Note that in each step we are free to choose which clause to use (OR).

• When we choose the clause; we must satisfy all of it’s conditions (AND). OR

AND AND

OR OR OR OR OR

Page 16: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

parent( pam, bob).

parent( tom, bob).

parent( tom, liz).

parent( bob, ann).

parent( bob, pat).

parent( pat, jim).

predecessor( X, Z) :- parent( X, Z). % Rule pr1predecessor( X, Z) :- parent( X, Y), % Rule pr2 predecessor( Y, Z).

predecessor( tom, pat)

predecessor( bob, pat)

parent( tom, Y)predecessor( Y, pat)

parent( tom, pat)

parent( bob, pat)

no

By rule pr1 By rule pr2

By fact parent( tom, bob)Y = bob

By rule pr1

yes

?- predecessor( tom, pat).

Page 17: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

Trace & Notrace| ?- trace.

The debugger will first creep -- showing everything (trace)

(15 ms) ture.

{trace}

| ?- predecessor( tom, pat).

1 1 Call: predecessor(tom,pat) ?

2 2 Call: parent(tom,pat) ?

2 2 Fail: parent(tom,pat) ?

2 2 Call: parent(tom,_79) ?

2 2 Exit: parent(tom,bob) ?

3 2 Call: predecessor(bob,pat) ?

4 3 Call: parent(bob,pat) ?

4 3 Exit: parent(bob,pat) ?

3 2 Exit: predecessor(bob,pat) ?

1 1 Exit: predecessor(tom,pat) ?

true ?

| ?- notrace.

The debugger is switched off

true.

Page 18: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

Linked Lists

• Prolog allows a special syntax for lists:

[a,b,c] is a list of 3 elements

[ ] is a special atom indicating a list with 0 elements

• Internally, Prolog lists are regular Prolog terms with the functor ‘.’ (so called “dotted pairs”)

[a,b,c] = ‘.’(a, ‘.’(b, ‘.’(c, []))).

• The symbol | in a list indicates “rest of list”, or the term that is the 2nd argument of a dotted pair.

[a,b,c] = [a|[b,c]].

• [Head|Tail] is a common expression for dividing a list into

Page 19: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

% list(?List)list([]).list([_Head|Tail]):-

list(Tail).

Since Prolog is untyped, we don’t have to know anything about Head except that it is a term.

Example: list/1

Page 20: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

% member(?Element, ?List)member(Element, [Element|_Tail]).member(Element, [_Head|Tail]):-member(Element, Tail).

Example: member/2

Page 21: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

% delete(+Element, +List, -NewList)% delete/3 succeeds if NewList results from% removing one occurrence of Element from List.

delete(Element, [Element|Tail], Tail).delete(Element, [Head|Tail], [Head|NewTail]):-delete(Element, Tail, NewTail).

Example: delete/3

Page 22: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

% append(+List1, +List2, -List3)% append/3 succeds if List3 contains all the% elements of List1, followed by all the elements% of List2.

append([], List2, List2).append([Head|List1], List2, [Head|List3]):-

append(List1, List2, List3).

Example: append/3

Page 23: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

% "naive reverse": nreverse(+List, -ReversedList).

nreverse([], []).nreverse([Head|Tail], ReversedList):-

nreverse(Tail, ReversedTail),append(ReversedTail, [Head],

ReversedList).

Example: “naïve” reverse

Page 24: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

pure Prolog vs non-logical built-ins

• All the examples so far have been “pure Prolog”;

Contain no built-ins with non-logical side-effects

• Prolog has many built-in predicates: Type checking of terms

Arithmetic

Control execution

Input and output

Modify the program during execution

Perform aggregation operations

Use of non-logical built-in predicates usually effects the reversibility of your program.

Page 25: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

Type-checking Built-in Predicates

• var(X)– is true when X is an uninstantiated variable.

• nonvar(X)– is true when X is not a variable.

• atom(X)– is true when X is a symbolic constant.

• number(X)- is true when X is a number

• atomic(X)– is true when atom(X) or number(X).

• compound(X)- is true when X is a compound term.

Page 26: About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

Term constructor/selectors: functor/3, arg/3

functor(+Term, ?Functor, ?Arity)

% Find the Functor and Arity of Term

functor(?Term, +Functor, +Arity)

% Constructs a new Term with Functor and Arity

arg(+N, +Term, ?SubTerm)

% Unifies SubTerm with the Nth argument of Term