Académique Documents
Professionnel Documents
Culture Documents
FI 3 - 2002
Programmation Logique
2002
Organisation du module
Enseignement
Cours/TD
50%
TP sur machine
50%
Langage support PROLOG
valuations
Programmation Logique
NJ - 2002 - 2/127
Introduction
Programmation Logique
2002
Classes de langage
Langages impratifs
contrle
total du flot dexcution
Langages
fonctionnels
Programmation Logique
NJ - 2002 - 4/127
PROgrammation en LOGique
exemple : Prolog
Programmation Logique
NJ - 2002 - 5/127
Historique
1930
1965
1970
1972
1977
1980
1990
Programmation Logique
NJ - 2002 - 6/127
Plan du cours
Calcul propositionnel
Calcul des prdicats
Le langage PROLOG
PROLOG avanc
Programmation Logique
NJ - 2002 - 7/127
Programmation Logique
NJ - 2002 - 8/127
Rappels de Logique
1. Calcul propositionnel
2. Calcul des prdicats
Programmation Logique
2002
1. Calcul propositionnel
Aspects syntaxiques
Aspects smantiques
Aspects dductifs
Programmation Logique
NJ - 2002 - 10/127
Calcul propositionnel
Donnes
Formules
Programmation Logique
NJ - 2002 - 11/127
Calcul propositionnel
Logique bi-value
faux (0)
vrai (1)
Notion dinterprtation
( p) 0 , 1
extension un oprateur
extension une formule
Programmation Logique
NJ - 2002 - 12/127
Calcul propositionnel
0 1
0 0 0
1 0 1
0 1
0 1 1
1 0 1
Programmation Logique
0 1
0 0 1
1 1 1
0 1
0 1 0
1 0 1
NJ - 2002 - 13/127
Calcul propositionnel
Formules particulires
p ( p) (p p)
0
1
1
1 0
1
Programmation Logique
0 1
0 0 1
1 1 1
NJ - 2002 - 14/127
Calcul propositionnel
? ( p q) (p q)
p q p
0
0
1
1
0
1
0
1
1
1
0
0
pq
(pq)
( p q)
1
0
1
0
0
0
0
1
1
1
1
0
1
1
1
0
1
1
1
1
0 1
0 0 0
1 0 1
Programmation Logique
0 1
0 0 1
1 1 1
0 1
0 1 0
1 0 1
NJ - 2002 - 15/127
Calcul propositionnel
Catgories de formules
Formules inconsistantes
Formules consistantes
, ( F ) 1
Programmation Logique
NJ - 2002 - 16/127
Calcul propositionnel
Formules particulires
, ( F ) ( H )
Programmation Logique
(F) (H)
NJ - 2002 - 17/127
Calcul propositionnel
tauto
F F
F
tauto
( F G ) (G F )
GF
tauto 1
tauto
Proprits de et
F G
tauto
F G
F G
tauto
GF
F F
tauto
( F G ) tauto
F G
( F G ) tauto
F G
lois de De Morgan
associativit
distributivit (dans les 2 sens)
lments neutres (0 pour et 1 pour )
lments absorbants (1 pour et 0 pour )
Programmation Logique
NJ - 2002 - 18/127
Calcul propositionnel
Formes normales
Programmation Logique
NJ - 2002 - 19/127
Calcul propositionnel
Formes normales
Programmation Logique
NJ - 2002 - 20/127
Calcul propositionnel
Aspects dductifs
Programmation Logique
2002
Calcul propositionnel
Consquence logique
G ssi
(F1 Fn) G
Notion de rfutation
Programmation Logique
G ssi
F 1 Fn G
est inconsistante
NJ - 2002 - 22/127
Calcul propositionnel
Systme formel
Donnes
ensemble
ensemble
ensemble
ensemble
V de symboles
F de formules (F V*)
A daxiomes (A F)
fini de rgles de dduction
un
un
un
un
Programmation Logique
NJ - 2002 - 23/127
Calcul propositionnel
nonc dductible
ou Ai
Programmation Logique
NJ - 2002 - 24/127
Calcul propositionnel
modus ponens
modus tollens
p, p q
p q, q
syllogisme
p q, q r
Programmation Logique
p r
NJ - 2002 - 25/127
Calcul propositionnel
A alors
A alors
Programmation Logique
NJ - 2002 - 26/127
Calcul propositionnel
Dfinitions
littral positif ou ngatif
une clause est une disjonction de littraux
la rsolvante de C1 = l C1 et de C2 = l C2 est
C1 C2
Principe de rsolution
l C1 , l C2
rso
C1 C2
Programmation Logique
NJ - 2002 - 27/127
Calcul propositionnel
C1 C2
(l C1) ( l C2 ) (C1 C2 )
l est vrai
ncessairement C2 vrai et donc (C1 C2 ) aussi
l est vrai
ncessairement C1 vrai et donc (C1 C2 ) aussi
0 1
0 1 1
1 0 1
Programmation Logique
0 1
0 0 1
1 1 1
0 1
0 0 0
1 0 1
NJ - 2002 - 28/127
Calcul propositionnel
reso
ssi S {C}
Rappel
Programmation Logique
reso
Calcul propositionnel
Principe de rsolution
Programmation Logique
NJ - 2002 - 30/127
Aspects syntaxiques
Aspects smantiques
Aspects dductifs
Programmation Logique
NJ - 2002 - 31/127
Modliser
Programmation Logique
NJ - 2002 - 32/127
Une modlisation
x, chandelle( x ) claire ( x )
x, chandelle( x ) claireMal ( x )
x, claire ( x ) claireMal ( x )
Programmation Logique
NJ - 2002 - 33/127
Syntaxe
des connecteurs (, , , et )
des quantificateurs ( et )
Programmation Logique
NJ - 2002 - 34/127
Vocabulaire
Les termes
Les atomes
Programmation Logique
NJ - 2002 - 35/127
Formules
(F)
(F) (G) et (F) (G)
(F) (G) et (F) (G)
x (F) et x (G)
Programmation Logique
NJ - 2002 - 36/127
Programmation Logique
NJ - 2002 - 37/127
Occurrence libre
Programmation Logique
NJ - 2002 - 38/127
Programmation Logique
NJ - 2002 - 39/127
Aspects smantiques
Programmation Logique
2002
Programmation Logique
NJ - 2002 - 41/127
Exemple dinterprtation
E = { anne, bernard, }
M = est la mre de
Q = est un parent de
R = est le grand-pre de
P = est le pre de
Programmation Logique
d = didier e= ric
NJ - 2002 - 42/127
Modle
Programmation Logique
NJ - 2002 - 43/127
Preuve et dmonstration
Programmation Logique
NJ - 2002 - 44/127
forme clausale
thorme de Herbrand
Programmation Logique
NJ - 2002 - 45/127
Transformation de formule
Programmation Logique
NJ - 2002 - 46/127
Programmation Logique
NJ - 2002 - 47/127
x F
xy F yx F
xy F yx F
x F x H x F H
x F x H x F H
si H ne contient aucune occurrence de x
x F H
x F H
Programmation Logique
x F H
x F H
x H H
x H H
NJ - 2002 - 48/127
Inversion de et de
Skolemisation
Lorsquon a
xy f ( x ) y
g x f ( x ) g ( x )
Skolemisation = expliciter limplicite
Programmation Logique
NJ - 2002 - 49/127
x y z
p ( x , y ) q ( z , y , x )
p(a, y1 )
Programmation Logique
; q( z1 , y2 , a )
NJ - 2002 - 50/127
Univers de Herbrand
H i H i 1 f n (t1 , , tn )
Programmation Logique
NJ - 2002 - 51/127
Thorme de Herbrand
Programmation Logique
NJ - 2002 - 52/127
A quoi a sert ?
Validation de raisonnement
Programmation Logique
NJ - 2002 - 53/127
Rsolution du problme
Dmarche gnrale
Notations
Programmation Logique
NJ - 2002 - 54/127
vo
vo
, xx,y
p,y(,ypxp,(,
)vo
((y(y
(hhy(h(x)(x)x))h( x )
f , x, p( x, f ( x )) h( x ) vo( f ( x )) h( x )
P2 les dragons verts peuvent voler
x, ve( x ) vo( x )
x, ve( x ) vo( x )
p
y
y
x)( x)
)rve
x,xp ,(A
p
,)(pve
y(,
yB
y )
(
r,(prxy())y,) x
()x(r )x(
)y )ve
, xA
, y,
(x,ve
y)x))(y
ve
( x()
(ve
(ve
les x
C les dragons verts sont heureux
x, ve( x ) h( x )
ngation
Programmation Logique
ve(ve(xx())x
ve
xx,,x,
)
hh((xx))
NJ - 2002 - 55/127
Forme clausale
f , x, p( x, f ( x )) h( x ) vo( f ( x )) h( x )
x, ve( x ) vo( x )
x, y,
x, ve( x ) h( x )
p( x1 , f ( x1 )) h( x1 )
vo( f ( x2 )) h( x2 )
ve( x3 ) vo( x3 )
p( y1 , x4 ) ve( y1 ) ve( x4 )
p( y2 , x5 ) r ( y2 ) ve( x5 )
Programmation Logique
ve(a )
h (a )
NJ - 2002 - 56/127
Validation du raisonnement
Univers de Herbrand
H0 = {a}
H = {a, , fn(a)}
Programmation Logique
NJ - 2002 - 57/127
Mise en uvre
p( x1 , f ( x1 )) h( x1 )
vo( f ( x2 )) h( x2 )
ve( x3 ) vo( x3 )
p( y1 , x4 ) ve( y1 ) ve( x4 )
p( y2 , x5 ) r ( y2 ) ve( x5 )
h (a )
h( a ) vo( f ( a ))
x2 a
h (a )
ve(a )
vo( f (a )) ve( f (a ))
x3 f ( a )
x1 a
x4 f ( a )
clause vide
p( y1 , f (xa ))
ve(xy1) f(ave
( f ( a ))
a
) h (a )
y1 x1
vo
(( af())
(fve
)) ve
a( a) ))
veh((fa()a ))
h(a ) p( a, f (a )) vo(pf((aa, ))f (a
h (a )
Programmation Logique
ve(a )
NJ - 2002 - 58/127
Rsolution
h (a )
h (a )
vo( f ( a )) ve( f (a ))
h(a ) vo( f ( a ))
p( a, f ( a )) ve( a ) ve( f ( a ))
h(a ) p( a, f (a ))
ve(a )
Programmation Logique
NJ - 2002 - 59/127
y1 x1
x1 a
Programmation Logique
NJ - 2002 - 60/127
Programmation Logique
NJ - 2002 - 61/127
Programmer en logique ?
Un petit exemple
Forme clausale
mere(a,b)
pere(c,b)
pere(d,a)
pere(e,c)
Programmation Logique
NJ - 2002 - 62/127
Programmer en logique ?
On part de grand-pere(x,b)
on obtient pere(x, c)
si x=e unification avec pere(e,c)
on obtient
QED
Programmation Logique
NJ - 2002 - 63/127
Programmer en logique ?
On a russi calculer un x
Unification = calcul
Calcul = programmation
Programmation Logique
NJ - 2002 - 64/127
Le langage PROLOG
1.
2.
3.
4.
5.
6.
Premiers pas
Arithmtique
Listes
Le contrle PROLOG
Termes structurs
Mta-interprtation de PROLOG
Programmation Logique
2002
Le langage
Elles permettent
Programmation Logique
NJ - 2002 - 66/127
Le langage
Univers PROLOG
Programmation Logique
NJ - 2002 - 67/127
Le langage
Syntaxe de PROLOG
Considrons lnonc
Prolog
homme(socrate).
mortel(X) :- homme(X).
?- mortel(socrate).
Programmation Logique
NJ - 2002 - 68/127
Le langage
La famille
masculin(tom).
masculin(tim).
masculin(bob).
masculin(jim).
feminin(pam).
feminin(liz).
feminin(ann).
feminin(pat).
enfant(bob,pam).
enfant(bob,tom).
enfant(liz,tom).
enfant(ann,bob).
enfant(pat,bob).
enfant(tim,liz).
enfant(jim,pat).
Programmation Logique
NJ - 2002 - 69/127
Le langage
masculin(tom).
masculin(tim).
masculin(bob).
masculin(jim).
Premires requtes
feminin(pam).
feminin(liz).
feminin(ann).
feminin(pat).
enfant(bob,pam).
enfant(bob,tom).
enfant(liz,tom).
enfant(ann,bob).
enfant(pat,bob).
enfant(tim,liz).
enfant(jim,pat).
;
;
No
Programmation Logique
NJ - 2002 - 70/127
Rgles LOGIQUE
abcd
Requte PROLOG
?- q.
Requte LOGIQUE
q
Unification
Faire correspondre requte et tte de rgle
Programmation Logique
NJ - 2002 - 71/127
Le langage
masculin(tom).
masculin(tim).
masculin(bob).
masculin(jim).
criture de prdicats
feminin(pam).
feminin(liz).
feminin(ann).
feminin(pat).
X=pam
X=tom
enfant(bob,pam).
enfant(bob,tom).
enfant(liz,tom).
enfant(ann,bob).
enfant(pat,bob).
enfant(tim,liz).
enfant(jim,pat).
chec
X=tom
Plus gnralement
pere(X,Y) :enfant(Y,X),
masculin(X).
crire le prdicat mere/2
Programmation Logique
NJ - 2002 - 72/127
Le langage
grand-pere/2
frere/2
tante/2
cousin/2
Programmation Logique
NJ - 2002 - 73/127
Le langage
PROLOG
Intrts
facilit de comprhension
facilit dcriture
Programmation Logique
NJ - 2002 - 74/127
Le langage
masculin(tom).
masculin(tim).
masculin(bob).
masculin(jim).
feminin(pam).
feminin(liz).
feminin(ann).
feminin(pat).
retour
Considrons la requte
?- frere(bob,liz).
Z=pam
Z = tom
chec
X = bob, Y = liz
Yes
enfant(bob,pam).
enfant(bob,tom).
enfant(liz,tom).
enfant(ann,bob).
enfant(pat,bob).
enfant(tim,liz).
enfant(jim,pat).
frere(X,Y) :masculin(X),
enfant(X,Z),
enfant(Y,Z).
Programmation Logique
NJ - 2002 - 75/127
Le langage
Quelques proprits
Programmation Logique
NJ - 2002 - 76/127
Le langage
Rcursivit
Programmation Logique
ascendant(X,Y) :enfant(Y,X).
Condition darrt
en premier
ascendant(X,Z) :enfant(Z,Y),
ascendant(X,Y).
Information discriminante
en premier
NJ - 2002 - 77/127
Le langage
PROLOG = logique ?
rcursion droite
fermeture(X,Y) :- r(X,Y).
fermeture(X,Z) :- r(X,Y), fermeture(Y,Z).
rcursion gauche
fermeture(X,Y) :- r(X,Y).
fermeture(X,Z) :- fermeture(Y,Z), r(X,Y).
Programmation Logique
NJ - 2002 - 78/127
Le langage
2. Arithmtique
?- X = 1 + 2.
X= 1+2
+(1,2)
Un oprateur spcial is
?- X is 1 + 2.
X=3
?- X is 1 + Y.
{INSTANTIATION ERROR}
Programmation Logique
NJ - 2002 - 79/127
Le langage
Oprateurs arithmtiques
> et <
>= et =<
=:= galit numrique
=\= disgalit numrique
attention : les deux arguments
doivent tre instancis lappel
Programmation Logique
NJ - 2002 - 80/127
Le langage
Proprits du PGCD D de X et Y
X=Y
pgcd(X, X
Y,X
D)).:- X
=:= Y , D is X.
pgcd(X, Y , D) :X<Y,
pgcd(X,Y-X,D).
Y1
is Y - X,
pgcd(X,Y1,D).
pgcd(X, Y, D) :X > Y,
pgcd(Y,X,D).
Programmation Logique
NJ - 2002 - 81/127
Le langage
Attention
Programmation Logique
NJ - 2002 - 82/127
Le langage
3. Listes
La liste vide : []
Les listes non vides :
Le constructeur de listes : |
[a, b, c]
[a, [b, c], d]
[Tete | Reste]
[a] [a | [] ]
[a, b] [a | [b |[]]] [a | [b] ]
|=:
reprsentation arborescente
Programmation Logique
NJ - 2002 - 83/127
Le langage
?- [1,2,3,4] = [H|T].
Programmation Logique
NJ - 2002 - 84/127
Le langage
Prdicats de base
Ys ne sert rien
Y ne sert rien
Programmation Logique
NJ - 2002 - 85/127
Le langage
Programmation Logique
NJ - 2002 - 86/127
Le langage
Programmation Logique
NJ - 2002 - 87/127
Le langage
Programmation Logique
NJ - 2002 - 88/127
Le langage
Programmation Logique
NJ - 2002 - 89/127
Le langage
A vous de jouer !
Les mutants
mutant(Nouveau) :animal(A1), animal(A2),
A1 \== A2,
name(A1,L1),
name(A2,L2),
append(_, [X|Xs], L1),
append([X|Xs], Fin, L2),
append(L1, Fin, Mutant),
name(Nouveau,Mutant).
Programmation Logique
NJ - 2002 - 90/127
Le langage
4. Le contrle PROLOG
Contrle du backtrack
Programmation Logique
NJ - 2002 - 91/127
Le langage
?- f(1,Y), 2 < Y.
f(X, 0) :- X < 3.
f(X, 2) :- 3 =< X, X < 6.
f(X, 4) :- 6 =< X.
Dmonstration de f(1,Y)
Premire rgle : Y = 0
Dmonstration de 2 < Y :- chec
Deuxime rgle : chec
Troisime rgle : chec
Programmation Logique
NJ - 2002 - 92/127
Le langage
La coupure : !
?- f(7,Y).
Programmation Logique
NJ - 2002 - 93/127
Le langage
Si alors sinon
?- f(1,2).
Programmation Logique
NJ - 2002 - 94/127
Le langage
Un usage dlicat
Programmation Logique
NJ - 2002 - 95/127
Le langage
p(a,b).
p(a,b) :- true.
Programmation Logique
NJ - 2002 - 96/127
Le langage
Application : ngation
different(X, Y) :X = Y, !, fail.
different(X, Y).
Programmation Logique
NJ - 2002 - 97/127
Le langage
?- q(X), p(X).
?- p(X), q(X).
Programmation Logique
NJ - 2002 - 98/127
Le langage
not(X)
Programmation Logique
NJ - 2002 - 99/127
Le langage
Typage en Prolog
var/1, nonvar/1
compound/1
ground/1
Programmation Logique
NJ - 2002 - 100/127
Le langage
5. Termes structurs
Notion de foncteur
famille(
indiv(tom, fox, date(7, mai, 1950), travail(emn, 7850)),
indiv(ann, fox, date(9, mai, 1951), sans-emploi),
[ indiv(pat, fox, date(5, mai, 1973), sans-emploi),
indiv(jim, fox, date(5, mai, 1973), sans-emploi) ])
Programmation Logique
NJ - 2002 - 101/127
Le langage
Utilisation de lunification
famille(
indiv(tom, fox, date(7, mai, 1950), travail(emn, 7850)),
indiv(ann, fox, date(9, mai, 1951), sans-emploi),
[ indiv(pat, fox, date(5, mai, 1973), sans-emploi),
indiv(jim, fox, date(5, mai, 1973), sans-emploi) ])
famille(_,_,[_,_,_]).
Programmation Logique
NJ - 2002 - 102/127
Le langage
Outils de manipulation
functor/3
?- functor(date(9, janvier, 1973), F, A)
F = date, A = 3
arg/3
?- arg(3, date(9, janvier, 1973), F)
F = 1973
=../2
?- X =.. [date, 9, janvier, 1973]
X = date(9, janvier, 1973)
Programmation Logique
NJ - 2002 - 103/127
Le langage
Oprateurs
Programmation Logique
NJ - 2002 - 104/127
Le langage
Fonctionnement
se lit
a+(b/c)
Programmation Logique
NJ - 2002 - 105/127
Le langage
6. Mta-interprtation
Mta-programmation
Programmation Logique
NJ - 2002 - 106/127
Le langage
Mta-interprtation
Programmation Logique
NJ - 2002 - 107/127
Le langage
?- clause(append(X,Y,Z), Corps).
X = [], Y = _A1, Z=_A2, Corps = true ? ;
X = [_A1|_A2], Y = _A3, Z = [_A1|_A4],
Corps = append(_A2, _A3, _A4)
?;
no
Programmation Logique
NJ - 2002 - 108/127
Le langage
Un premier interprte
prouve( true ).
prouve( (A,B) ) :prouve(A),
prouve(B).
prouve( But ) :clause(But, Corps),
prouve(Corps).
Programmation Logique
NJ - 2002 - 109/127
Le langage
Amliorations
Programmation Logique
NJ - 2002 - 110/127
Le langage
Algorithme dunification
unification( X, Y) :var(X), !, Y = X.
% nooccur(X,Y)
unification( X, Y) :var(Y), !, X = Y.
% nooccur(Y,X)
NJ - 2002 - 111/127
Le langage
Le test doccurrence
Programmation Logique
NJ - 2002 - 112/127
Le langage
nooccur/2
noccur( _, Y) :ground(Y).
noccur( X, Y) :var(Y), X \== Y.
noccur( X, Y) :Y =.. [_|Arguments],
nooccurListe(X, Arguments).
nooccurListe(_, []).
nooccurListe(X, [Y|Ys]) :nooccur(X,Y),
nooccurListe(X,Ys).
Programmation Logique
NJ - 2002 - 113/127
PROLOG avanc
1.
2.
3.
4.
Structures incompltes
Listes de solutions
Modifications de la base
Indexation de clauses
Programmation Logique
2002
Prolog avanc
1. Structures incompltes
Concatnation
append([], Ys, Ys).
append([X|Xs], Ys, [X|Zs]) :append(Xs,Ys,Zs).
Notoirement inefficace !
Le problme
pas de pointeur sur le dernier lment
pas de possibilit de modification dune variable logique
uniquement instanciation dune inconnue
Programmation Logique
NJ - 2002 - 115/127
Prolog avanc
Programmation Logique
NJ - 2002 - 116/127
Prolog avanc
Ls - Xs
Xs - Rs
abc 123???
123???
123???
???
Ls - Rs
abc123???
???
Merci lunification !
Programmation Logique
NJ - 2002 - 117/127
Prolog avanc
2. Listes de solutions
pere(tarzan, fils).
pere(tarzan, fille).
age(fils, 5).
age(fille, 2).
age(tarzan, 25).
Programmation Logique
NJ - 2002 - 118/127
Prolog avanc
--> [b,a]
--> L=[b] Y=1 ;
L=[a] Y=2
--> [a,b]
NJ - 2002 - 119/127
Prolog avanc
3. Modifications de la base
assert/1
asserta/1
assertz/1
retract/1
retractall/1
Programmation Logique
NJ - 2002 - 120/127
Prolog avanc
4. Indexation de clause
Programmation Logique
NJ - 2002 - 121/127
Bilan
1. Cours
2. Travaux pratiques
3. A quoi a sert ?
Programmation Logique
2002
Bilan
Bilan du cours
Rappels de logique
Le langage PROLOG
Subtilits du langage
Programmation Logique
NJ - 2002 - 123/127
Bilan
Bilan des TP
TP-5 (not)
Programmation Logique
NJ - 2002 - 124/127
Bilan
Systmes experts
cf. TP
Programmation Logique
NJ - 2002 - 125/127
Bilan
Programmation Logique
NJ - 2002 - 126/127
Bilan
Utilis pour
Dassault lectronique
ECRC (Centre de recherche commun Bull, Siemens)
Programmation Logique
NJ - 2002 - 127/127