% mvlpl07.pl
%
% ******************************************************************

% ## 8.0 Base de connaissance pour l'exemple de Lauritzen

% ******************************************************************

taille(9).

%

cert2(T,L) :- cert(T,A),
              (is_list(A) -> A = [_,L]; % extraction du max
              %true ->
               A = L).

%  # 8.1 certitudes fournies

cert(smok,8).
cert(asia,9).

%  # 8.3 règles sur les certitudes

% si X vient de Y

cert(T,R) :- dep(A,D), member(T,D), cert2(A,I),
    % affiche le chemin parcouru (eventuellement incomplet si relancÅ par ;)
    write(A),write(' : '),write(I),write(' '),
    cert_quand(T,A,J),taille(M), i(I,J,M,K), l(I,K,M,L),
    (L == K -> R = L;
     %true ->
     R = [K,L]).

cert(inter(X,Y),I)  :- indep(X,Y),taille(T),cert(X,J),cert(Y,K),i(J,K,T,I).
cert(inter(X,Y),I)  :- cert_quand(X,Y,J),cert(Y,K),taille(T),i(J,K,T,I).

cert(X,Y,K) :- indep(X,Y),!,cert(X,I),cert(Y,J),taille(T),i(I,J,T,K).
cert(X,Y,K) :- listedep([X],L),member(Y,L),!,cert_quand(Y,X,K).
cert(X,Y,K) :- cert_quand(X,Y,K).

%  # 8.4 certitudes conditionnelles fournies

cert_quand(inter(X,inter(Y,Z)),K) :-
           not indep(Y,Z),
           cert(X,I),
           cert(inter(Y,Z),J),
           taille(T),
           c(I,J,T,K),!.

cert_quand(inter(X,inter(Y,Z)),K) :-
           cert_quand(inter(Z,inter(X,Y)),K).

cert_quand(tubr,asia,2).
cert_quand(lung,smok,6).
cert_quand(bron,smok,8).
cert_quand(uuuu,tubr,9).
cert_quand(uuuu,lung,9).
cert_quand(dysp,uuuu,2).
cert_quand(dysp,bron,8).
cert_quand(xray,dysp,8).

%  # 8.5 règles sur les certitudes conditionnelles

cert_quand(X,inter(Y,Z),K) :-
           indep(X,Z),
           cert(inter(Y,Z),I),
           cert(inter(Z,inter(X,Y)),J),
           taille(T),
           c(I,J,T,K).

cert_quand(X,inter(Y,Z),K) :-
           indep(Y,Z),
           cert(inter(X,Z),I),
           cert(inter(Y,inter(X,Z)),J),
           taille(T),
           c(I,J,T,K).

%  # 8.6 gestion de l'indépendance via les listes de dépendance dans le graphe

%
%
%
%     asia                            heavy-smoker
%     (asia)                            |  (smoker)
%      |                                |       |
%      |                                |       |
%     tuberculosis             lung cancer      bronchitis
%     (tubr)                   (lung)           bron)
%      |                        |               |
%      +------- tubr or lung ---+               |
%               (uuuu)                          |
%                 |                             |
%                 |                             |
%                 +------------ dyspnoea  ------+
%                                (dysp)
%                                  |
%                                  |
%                                  |
%                                  |
%                               hospitalisation
%                               (hosp)
%
%
%

dep(asia,[tubr]).
dep(smok,[lung,bron]).
dep(tubr,[uuuu]).
dep(lung,[uuuu]).
dep(bron,[dysp]).
dep(uuuu,[dysp]).
dep(dysp,[xray]).

% deux éléments X et Y sont indépendants si X n'est pas dans la liste de
% dépendance de Y (ou inversement)


listedep([],[]).

listedep([T|Q],L) :- dep(T,L1),
                     listedep(L1,L2),
                     listedep(Q,L3),
                     append([T],L2,L4),
                     append(L3,L4,L).

listedep([T|Q],L) :- listedep(Q,L1),
                     append([T],L1,L).

otredep(X,L,L1) :- listedep(L,L2),append([X],L2,L1),!.

indep(X,Y) :- dep(X,L),
              otredep(X,L,L1),
              member(Y,L1),!,
              fail.
indep(X,Y) :- dep(Y,L),
              otredep(Y,L,L1),
              member(X,L1),!,
              fail.
indep(X,inter(Z,Y)):- indep(X,Y),asserta(indep(X,inter(Z,Y))).
indep(X,inter(Y,Z)):- indep(X,Y),asserta(indep(X,inter(Y,Z))).
indep(X,Y) :- asserta(indep(X,Y)).