Skip to content

Instantly share code, notes, and snippets.

@llaine
Last active October 19, 2015 13:52
Show Gist options
  • Select an option

  • Save llaine/15f4928924a9cc06cbde to your computer and use it in GitHub Desktop.

Select an option

Save llaine/15f4928924a9cc06cbde to your computer and use it in GitHub Desktop.
TD de prolog
% http://dept-info.labri.u-bordeaux.fr/~musumbu/Expert/td2.pdf
/*
***************** Les faits *******************
Arguemnts du predicat bio :
bio(enfant, sexe, annee_naissance, annee_mort, pere, mere)
*/
bio(louis13, h, 1601, 1643, henri4, marie_medicis).
bio(elisabeth_France, f, 1603, 1644, henri4, marie_medicis).
bio(marie_therese_Autriche, f, 1638, 1683, philippe4, elisabeth_france).
bio(louis14, h, 1638, 1715, louis13, anne_autriche).
bio(grand_dauphin, h, 1661, 1711, louis14, marie_therese_autriche).
bio(louis_bourbon, h, 1682, 1712, grand_dauphin, marie_anne_baviere).
bio(philippe5, h, 1683, 1746, grand_dauphin, marie_anne_baviere).
bio(louis15, h, 1710, 1774, louis_bourbon, marie_adelaide_savoie).
bio(louis_dauphin, h, 1729, 1765, louis15, marie_leczcynska).
bio(louis16, h, 1754, 1793, louis_dauphin, marie_josephe_saxe).
bio(louis18, h, 1755, 1824, louis_dauphin, marie_josephe_saxe).
bio(charles10, h, 1757, 1836, louis_dauphin, marie_josephe_saxe).
bio(clotilde, f, 1759, 1802, louis_dauphin, marie_josephe_saxe).
bio(louis17, h, 1785, 1795, louis16, marie_antoinette).
bio(philippe1, h, 1640, 1701, louis13, anne_autriche).
bio(philippe2, h, 1674, 1723, philippe1, charlotte_baviere).
bio(louis_orleans, h, 1703, 1752, philippe, francoise_marie_bourbon).
bio(louis_philippe, h, 1725, 1785, louis_orleans, augusta_marie_bade).
bio(philippe_egalite, h, 1747, 1793, louis_philippe,
louise_henriette_bourbon_conti).
bio(louis_philippe1, h, 1773, 1850, philippe_egalite,
louise_marie_adelaide_bourbon_penthievre).
bio(louis_philippe1, h, 1773, 1850, philippe_egalite,
louise_marie_adelaide_bourbon_penthievre).
bio(louis_orleans, h, 1670, 1752, louis14, madame_de_pompadour).
bio(irene_orleanss, f, 1734, 1772, louis_orleans, marie_sophie_de_catalagne).
bio(henri_bourbon, h, 1713, 1764, inconnu, francoise_marie_bourbon).
bio(irene_orleans, f, 1734, 1772, louis_orleans, marie_sophie_de_catalagne).
% Bio tout simple
/***************** Les regles *******************/
/*enfant(enfant,parent)*/
/*petit_tenfant(petit_enfant,grand-parent)*/
% Exercice 1 :
% Soit le père ou la mère.
enfant(X, Y):- bio(X, _, _, _, Y, _); bio(X, _, _, _, _, Y).
% Exercice 2 :
% 2.1
prince(X):- bio(X, 'h', _, _, _, _).
% 2.2
princesse(X):- bio(X, 'f', _, _, _, _).
% Exo 3
frere(X, Y):- (enfant(X, Z), enfant(Y, Z)), (prince(X), prince(Y)), not(X=Y).
soeur(X, Y):- (enfant(X, Z), enfant(Y, Z)), princesse(X), not(X=Y).
parent(X, Y):- enfant(Y, X).
marie_de(X, Y):- (enfant(Z, X), enfant(Z, Y)), not(X=Y).
petit_tenfant(X, Y):- enfant(Z, Y), parent(Z, X).
cousin(X, Y):- parent(Z, X), parent(W, Y), (frere(Z, W); soeur(Z, W)).
% TD 2 :
% 1.1
princesansdescendant(Prince):- prince(Prince), not(parent(Prince, _)).
% 1.2
enfantUnique(Individu):-
(prince(Individu); princesse(Individu)),
(not(frere(Individu, _)),
not(soeur(Individu, _))).
% 1.4
polygame(Individu):- marie_de(Individu, Y), marie_de(Individu, Z), not(Z=Y).
% 1.5
enfant_cache(P, X):-
bio(X, _, _, _, inconnu, _);
bio(X, _, _, _, _, inconnu).
% 6.a
aine(P1, P2, LAINE):-
(bio(P1, _, Born1, _, _, _), bio(P2, _, Born2, _, _, _)),
((Born1<Born2, LAINE=P1);LAINE=P2).
% 6.b
cadet(P1, P2, Cadet):-(bio(P1, _, Born1, _, _, _), bio(P2, _, Born2, _, _, _)),
((Born1>Born2, Cadet=P1);Cadet=P2).
a_survecu(P1, P2, Last):-
(bio(P1, _, _, Dead1, _, _),
bio(P2, _, _, Dead2, _, _)),
(frere(P1, P2);soeur(P1, P2)),
((Dead1<Dead2, Last=P2); Last=P1).
disparu_avant(P1, P2, Last):-
(bio(P1, _, _, Dead1, _, _),
bio(P2, _, _, Dead2, _, _)),
(frere(P1, P2);soeur(P1, P2)),
((Dead1>Dead2, Last=P2); Last=P1).
age(X, AGE):- bio(X, _, Naiss, Mort, _, _), AGE is Mort - Naiss.
% Exercice
premier(L, R):-
length(L, Taille),
(Taille=0 -> R=[]; nth0(0, L, R)).
dernier(L, R):-
length(L, Taille),
(Taille=0 -> R=[]; last(L, R)).
element(X, L):- member(X, L).
supprime1(X, [X|L], L).
supprime1(X, [A|L], [A|B]):- supprime1(X, L, B).
test([X|L]):-
write(X),
test(L).
supprime_all(X, L, R):- delete(L, X, R).
suppDoublon([], []).
suppDoublon([X|L], [X|B]):-
supprime_all(X, L, R),
suppDoublon(R, B).
%% Version sans les fonction déjà fait
% Premier exercice
premier([], R):-!,R=[].
premier([X|_], R):-R = X.
% Dernier exercice
dernier([X], R):-!, R=X.
dernier([], R):-!,R=[].
dernier([_|L], R):- dernier(L, R).
element(E, [X|_]):-E = X,!,true,!.
element(_, []):-!,false.
element(X, [_|Q]):-element(X, Q).
% Cette fonction, supprime un élement d'une liste.
% SI la liste est vide on s'arrête
supprime1(_, [], _):-!.
% Si X est équivalent à la tête de la liste,
% on continue
supprime1(X, [X|Q], Q):-!.
% Si X n'est pas égale à T, alors la tête est ajouté à R
% et on rappelle la fonction.
% Sinon on rappelle la fonction quand mêmle
supprime1(X, [T|Q], [T|R]):-X \== T, supprime1(X, Q, R).
% TD 5
% 5.1
% genere_liste(Nombre, ListeRetour):- numlist(1, Nombre, ListeRetour).
% Ajoute à la tête de la liste L, X.
push(X, L, [X|L]).
genere_liste_2(0, []):-!.
genere_liste_2(Nombre, [Nombre|ListeRetour]):-
X is Nombre - 1,
genere_liste_2(X, ListeRetour).
genere_liste(Nombre, X):-
genere_liste_2(Nombre, L), reverse(L, X).
/*
push(Nombre, ListeRetour, L),
X is Nombre - 1,
genere_liste_2(X, L).
*/
% 5.2
if_then_else(Predicat, Q, R):-(Predicat -> Q; R).
ifthenelse(P, Q, R):- P, !, Q; R.
% 5.2.2
transport():-if_then_else(neige, tram, if_then_else(pluie, voiture, velo)).
transport(X, R):-
ifthenelse(X=neige, R=tram, ifthenelse(X=pluie,R=voiture, R=velo)).
% TD 6.1
process:-
repeat,
read(X),read(Y),
(atom(X),!;
Z is X*Y,
write(Z),nl,fail).
/*
1) tell(nomFichier), permet d'appeller un fichier du répertoire courant
see(nomFichier), permet de lire un fichier
2)
Les résultats correspondent à la function process, qui va
pour toute les lignes du fichiers, lire les premiers atom
et multiplier l'un par l'autre.
Enfin on va écrire le résultat de chaque ligne à l'aide de la commande write(X) combiné
avec la commande tell(user).
*/
/*
***************** Les faits *******************
Arguemnts du predicat bio :
bio(enfant, sexe, annee_naissance, annee_mort, pere, mere)
*/
bio(louis13, h, 1601, 1643, henri4, marie_medicis).
bio(elisabeth_France, f, 1603, 1644, henri4, marie_medicis).
bio(marie_therese_Autriche, f, 1638, 1683, philippe4, elisabeth_france).
bio(louis14, h, 1638, 1715, louis13, anne_autriche).
bio(grand_dauphin, h, 1661, 1711, louis14, marie_therese_autriche).
bio(louis_bourbon, h, 1682, 1712, grand_dauphin, marie_anne_baviere).
bio(philippe5, h, 1683, 1746, grand_dauphin, marie_anne_baviere).
bio(louis15, h, 1710, 1774, louis_bourbon, marie_adelaide_savoie).
bio(louis_dauphin, h, 1729, 1765, louis15, marie_leczcynska).
bio(louis16, h, 1754, 1793, louis_dauphin, marie_josephe_saxe).
bio(louis18, h, 1755, 1824, louis_dauphin, marie_josephe_saxe).
bio(charles10, h, 1757, 1836, louis_dauphin, marie_josephe_saxe).
bio(clotilde, f, 1759, 1802, louis_dauphin, marie_josephe_saxe).
bio(louis17, h, 1785, 1795, louis16, marie_antoinette).
bio(philippe1, h, 1640, 1701, louis13, anne_autriche).
bio(philippe2, h, 1674, 1723, philippe1, charlotte_baviere).
bio(louis_orleans, h, 1703, 1752, philippe, francoise_marie_bourbon).
bio(louis_philippe, h, 1725, 1785, louis_orleans, augusta_marie_bade).
bio(philippe_egalite, h, 1747, 1793, louis_philippe,
louise_henriette_bourbon_conti).
bio(louis_philippe1, h, 1773, 1850, philippe_egalite,
louise_marie_adelaide_bourbon_penthievre).
bio(louis_philippe1, h, 1773, 1850, philippe_egalite,
louise_marie_adelaide_bourbon_penthievre).
push(X, L, [X|L]).
ifthenelse(P, Q, R):- P, !, Q; R.
age(Personne, Age):-bio(Personne, _, N, D, _, _), Age is D-N.
/*
plusJeune(X):-
bio(X, _, _, _, _, _),
age(X, AgeX),
forall(
(bio(Y, _, _, _, _, _), age(Y, AgeY)),
AgeX =< AgeY
).
*/
% Lis le contenu d'un fichier.
foo:-
repeat,
read(P),
(atom(P),!;
write(P), nl, fail).
plusJeune:-
forall(
bio(Nom, _, _, _, _, _),
(
age(Nom, Age),
Y is Age,
)
).
bar(X, R):- findall(X, plusJeune(X), R).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment