Vous êtes sur la page 1sur 10

Département Informatique

3ème année licence


Module : Programmation logique

Série de travaux dirigés n°1

La forme clausale

1-

a. P(x) xQ(x)  P(y) xQ(x) chgt variable


 x (P(y) Q(x)) c.5

b. (xP(x) xP(x))  Q  x (P(x) P(x))  Q d.5


 x ((P(x) P(x))  Q) c.4

c. xP(x) xQ(x,z) xyR(x,y) 


x(P(x) xQ(x,z)) xyR(x,y)  c.5
x(P(x) uQ(u,z)) xyR(x,y)  chgt var.
x u (P(x) Q(u,z)) xyR(x,y)  c.8
x ( u (P(x) Q(u,z)) xyR(x,y) )  d.3
x ( u (P(x) Q(u,z)) vyR(v,y) )  chgt. var.
x v ( u (P(x) Q(u,z))  yR(v,y) )  d.1
x v u ((P(x) Q(u,z))  yR(v,y) )  d.4
x v u y ((P(x) Q(u,z))  R(v,y) )  d.2

2-

a. (xP(x) xP(x))  Q  x ((P(x) P(x))  Q) 


(P(a) P(a))  Q  skolémisation
(P(a) P(a))  Q  sup. 
VQ  Q simplification

b. x v u y ((P(x) Q(u,z))  R(v,y) ) 


v u ((P(a) Q(u,z))  R(v,f(u,v)) )  skolem
(P(a) Q(u,z))  R(v,f(u,v)) etc. (FNC)

3-
P1 un dragon est heureux si tous ses enfants peuvent voler
x (y p(x,y)vo(y))  h(x)
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)


P3 un dragon est vert s’il a au moins un parent vert ou rose

x yp( y, x)  ve( y)  r ( y)   ve( x)


xy p( y, x)  ve( y)  ve( x)   p( y, x)  r ( y)  ve( x) 

Abidet-Bahi H.
Département Informatique
3ème année licence
Module : Programmation logique

C les dragons verts sont heureux


x ve( x)  h( x)
x ve( x)  h( x)

Forme clausale

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)
ve(a)
h(a)

Abidet-Bahi H.
Département Informatique
3ème année licence
Module : Programmation logique

Série de travaux dirigés n°2

Unification et résolution

1.  = { a/Z , f(a) / X , g(Y)/U}

2.
(a)  = { Z / X , f(a) / Z , on ne peut pas unifier f(a) et g(a,f(a))
(b)  = {g(X,Y)/Z , g(V , W) /X , f(g(V,W))/Y}

3.
a.
Res(a,b) = p(f(a)) = d a/X
Res (c,d)=  a/Z

b.
C1 = Res(c,d) = p(f(b), f(Y))  ¬p(b, Y) { b/X}
C2 = Res(C1,e) = p(f(b), f(b)) { b/Y}
C3 = Res(C2,b) = q(a,f(b)) { b/Y}
C4 = Res(C3,f) = q(f(a),f(a)) { a/X , f(b) / Y}
C5 =  {a/Z , f(a) /Y}

4.
a) S1 = {X (P(X)  Q(X)), X P(X)} ; F1 = {X Q(X) }
E=S1¬ F1
C0 = ¬ F1 = ¬(X Q(X)) =  X ¬Q(X) = ¬Q(a)
C1= X (P(X)  Q(X)) = P(X)  Q(X) = ¬ P(X)  Q(X)
C2 = X P(X) = P(X)
µ1={ a/X}
C3=res(µ1.C1, C0) = ¬ P(a)
C4=res(µ1.C2,C3) =  donc S1 |- F1

b) S2 = { X (P(X)  Q(X)), X P(X) } ; F2 = { X Q(X) }


E=S2¬ F2
C0 = ¬ F2 = ¬(X Q(X)) =  X ¬Q(X) = ¬Q(X)
C1= X (P(X)  Q(X)) = P(a)  Q(a) = ¬ P(a)  Q(a)
C2 = X P(X) = P(b)

µ1={ a/X}
C3=res(µ1.C1, C0) = ¬ P(a)
On ne peut plus calculer de résolvantes donc S2 |-/ F2

5.

Rq. Les variables sont en minuscule et les constantes en Majuscule.

- C1 = Pyramide(C)
- C2 = ¬ Cube(t)  Gauchede(C, t)
- C3 = Cube(A)

Abidet-Bahi H.
Département Informatique
3ème année licence
Module : Programmation logique

- C4 = Pyramide(B)
- C5 = Gauchede(A , B)
- C6 = ¬Gauchede(u, v)  ¬Gauchede(v,w)  Gauchede(u,w)
- C0 = ¬Pyramide(x)  ¬ Gauchede(C, x)]

a = Res(2,3) = Gauchede(C,A)
b = Res(a,6) =¬Gauchede(A,w)  Gauchede(C,w)
c = Res(b, 5) = Gauchede(C,B)
d = Res(c, C0) = ¬Pyramide(B)
e = Res(d,C4) = 

Abidet-Bahi H.
Département Informatique
3ème année licence
Module : Programmation logique

Série de travaux dirigés n°3 :

Création de règles et de requêtes en Prolog

1. Ecrivez en Prolog les faits suivants :


- chien(rex). - jouer(ali,samia,football).
- pere(omar,zina). - posseder(samia,livre)

2. Posez les questions suivantes :


?-posseder(omar,livre). ?- pere(X,ali).
?-femme(samia). ?- jouer(ali,X,football).
?- père(omar,ali). ?- jouer(X,Y,football).

3.
- lire(nabil,X) :- livre(X), theme(X,voitures)
- parents(Y,Z,X) :-mere(Y,X),père(Z,X).

4.
(a) ? animal(mamal,X,_,_).
(b) ?animal(mamal,X,carnivore,_).
(c) ? animal(mamal,X,_,stripes).
(d) ?animal(reptile,X,_mane).

5.

nationalité(X,P) :- personne(X),pays(P),ne(X,P).
Vous pouvez proposer une autre solution plus élaborée en tenant compte des classes des mots.

immigrer(X) :- personne(X), pays(Y),pays(Z),diff(Y,Z),nee(X,Y),habiter(X,Z).


on peut aussi proposer une définition de immgrer à 3 arguments.

immigrer(X,algerie,angleterre) :- personne(X), nee(X,algerie), habiter(X,angleterre).


On peut proposer le prédicat immiger_alg_ang(X) avec la même définition.

6.
1. ?enfant(bob,X).masculin(X).
2. mere(X,Y) :- enfant(Y,X),feminin(X).
grand_pere(X,Y) :- père(X,Z),enfant(Y,Z).
frere(X,Y) :- enfant(X,Z),enfant(Y,Z).
tante(X,Y) :- feminin(X),enfant(X,Z),enfant(P,Z),enfant(Y,P).

7. On se propose de définir un prédicat permettant de colorier la carte suivante :


/* prédicat permettant de colorier une carte */

couleur(vert).

Abidet-Bahi H.
Département Informatique
3ème année licence
Module : Programmation logique

couleur(jaune).

couleur(rouge).

coloriage(C1,C2,C3,C4):-couleur(C1),couleur(C2),couleur(C3),couleur(C4),

C1\==C2, C1\==C3, C1\==C4, C2\==C3, C3\==C4.

coloriage_instance(C1, C2, C3, C4) :- couleur(C1),couleur(C2),C1 \== C2,couleur(C3),C1 \== C3,C2 \==
C3,couleur(C4),C1 \== C4, C3 \== C4.

Abidet-Bahi H.
Département Informatique
3ème année licence
Module : Programmation logique

Série de travaux dirigés n°4 :

Prolog et graphe de résolution

1.Soit la base de connaissances suivante :

habite(jean,paris).
habite(hans,munich).
habite(juan,madrid).
capitale(paris).
capitale(madrid).
habite-capitale(Qui) :-
habite(Qui,Qqpart),capitale(Qqpart).

a) Quelles seraient dans l’ordre les réponses à la question : ?- habite-capitale(Qui).


b) Construire le graphe de résolution relatif à cette question.
c) Construire l’arbre ET/OU relatif à la question.

2. Construire le graphe de résolution pour répondre à la question : ?- oncle(x,nabil).


Faits et règles de départ en Prolog.

frère(ali,nabil).
frère(omar,sami).

père(omar,ali),
père(omar,nabil).

oncle(z,y) :- père(x,y) , frère(x,z).

Abidet-Bahi H.
Département Informatique
3ème année licence
Module : Programmation logique

Corrigé :

oncle(X,nabil) oncle(Z1,Y1) père(X1,Y1) frere(X1,Z1)

Z1|X
Y1|nabil

père(X1,nabil) frere(X1,X)
père(omar,ali) père(omar,nabil)

X1|omar

echec frere(omar,X)

frere(ali,nabil) frere(omar,sami)
X|sami

echec

Abidet-Bahi H.
Département Informatique
3ème année licence
Module : Programmation logique

Série de travaux dirigés n°5


La récursivité et les listes

1.
factoriel(0,1).
Factoriel(N, Fact) :- M is N – 1,
factoriel(M, M_Fact),
Fact is N * M_Fact.

2.
fib(0,0).
fib(1,1).
fib(X,Y) :- X>1,
X2 is X – 2 , fib(X2, Y2),
X1 is X – 1 , fib(X1, Y1),
Y is Y1 + Y2.

3.
maximum_no([],Max).

maximum_no([H|T],Max):- H>Max, N = H, maximum_no(T,N).

maximum_no(L,Max):- maximum_no(L,Max).

4.
Suppression d’un élément d’une liste : efface/3
efface(_, [], []).
efface(X,[X|Ys], Ys).
efface(X,[Y|Ys], [Y|Zs]) :- X \== Y,
efface(X,Ys,Zs)

ou

remove_at(Elem,[Elem|Reste],1,Reste).
remove_at(Elem,[Y|Reste],Position,[Y|Ys]) :- Position > 1,
K1 is Position - 1, remove_at(Elem,Reste,K1,Ys).

Exemple d'utilisation:
%?- remove_at(X,[a,b,c,d],2,R).
%X = b
%R = [a,c,d]

5. Ecrire le prédicat qui insère un élément dans une liste.

%Insert an element at a given position into a list

Abidet-Bahi H.
Département Informatique
3ème année licence
Module : Programmation logique

% The first element in the list is number 1.

% insert_at(X,L,K,R) :- X is inserted into the list L such that it


% occupies position K. The result is the list R.
% (element,list,integer,list) (?,?,+,?)

insert_at(X,L,K,R) :- remove_at(X,R,K,L).

6. Ecrire le prédicat qui inverse les éléments d’une liste.

%Reverse a list.

% my_reverse(L1,L2) :- L2 is the list obtained from L1 by reversing


% the order of the elements.
% (list,list) (?,?)

% Note: reverse(+List1, -List2) is predefined

my_reverse(L1,L2) :- my_rev(L1,L2,[]).

my_rev([],L2,L2) :- !.
my_rev([X|Xs],L2,Acc) :- my_rev(Xs,L2,[X|Acc]).

7. Ecrire en Prolog le prédicat compresser permettant de supprimer des doublons consécutifs


dans une liste L pour obtenir une liste L1. Remarque: L’ordre des éléments doit être respecté.
Exemple:

?- compresser([a,a,a,a,b,c,c,a,a,d,e,e,e,e],L1).
L1 = [a,b,c,a,d,e].

%Eliminate consecutive duplicates of list elements.

Compresser([],[]).
Compresser([X],[X]).
Compresser([X,X|L1],L2) :-compresser([X|L1],L2).
Compresser([X,Y|L3],[X|L2]) :- X\=Y,compresser([Y|L3],L2).

7. Les Mutants
non_vide([_|_]).
mutant(S) :- animal(D), animal(F), append(Debut,Milieu,D),
non_vide(Debut), non_vide(Milieu), append(Milieu,_,F), append(Debut,F,M), name(S,M).
animal("alligator").
animal("lapin").
animal("tortue").
animal("pintade").
animal("cheval").

Abidet-Bahi H.

Vous aimerez peut-être aussi