fact1(0,1). fact1(N,R) :- N>0, P is N-1, fact1(P,V), R is N*V. fact2(X,1) :- X=:=0. fact2(N,R) :- N>0, P is N-1, fact2(P,V), R is N*V. a(0,1). b(X,1):-X=0. c(X,1):-X=:=0. d(X,1):-X==0. if_1(T,P,Q):-T,!,P. if_1(T,P,Q):-Q. if_2(T,P,Q):-T,P,!. if_2(T,P,Q):-Q. if_3(T,P,Q):-!,T,P. if_3(T,P,Q):-Q. /* pour distinguer if_1 et if_2: (true,false,true) 1 et 3: (fail,fail,true) 2 et 3 idem */ p(1). p(2). p(3). p(4). p_aux(X,Y) :- p(X), p(Y), X\==Y, !. p2(X) :- p_aux(X,_). p2(X) :- p_aux(_,X). /*********/ p_auxb(X) :- p(X) , !. p2b(X):- p_auxb(X) ; p_auxb(X). /* recurrence*/ est_formule(X):-atomic(X),!. est_formule(neg(X)):-est_formule(X). est_formule(et(X,Y)):-est_formule(X),est_formule(Y). est_formule(ou(X,Y)):-est_formule(X),est_formule(Y). symboles(X,1,0,0,0):-atomic(X). symboles(neg(P),A,N,E,O):-symboles(P,A,M,E,O), N is M+1. symboles(et(P,Q),A,N,E,O):-symboles(P,Aa,Na,Ea,Oa), symboles(Q,Ab,Nb,Eb,Ob), A is Aa+Ab, N is Na+Nb, E is Ea+Eb+1,O is Oa+Ob. symboles(ou(P,Q),A,N,E,O):-symboles(P,Aa,Na,Ea,Oa), symboles(Q,Ab,Nb,Eb,Ob), A is Aa+Ab, N is Na+Nb, E is Ea+Eb,O is Oa+Ob+1. variables(X,[X]):-atomic(X),!. variables(neg(X),L):-variables(X,L). variables(et(X,Y), L):-variables(X,L1),variables(Y,L2),union(L1,L2,L). variables(ou(X,Y), L):-variables(X,L1),variables(Y,L2),union(L1,L2,L). combine([],[],[]). combine([X|L],[Y|G],[(X,Y)|H]) :- combine(L,G,H). assoc(X,[(X,Y)|_],Y):-!. assoc(X,[_|L],Y):-assoc(X,L,Y). combiner([],[]). combiner([X|L],[(X,Y)|R]):-combiner(L,R),(Y=1;Y=0). evaluer(X,A):-atomic(X), assoc(X,A,1). evaluer(neg(X),A):-not(evaluer(X,A)). evaluer(et(X,Y),A):-evaluer(X,A),evaluer(Y,A). evaluer(ou(X,Y),A):-(evaluer(X,A), !); evaluer(Y,A). /* sat(F,A):-variables(F,L), length(L,N), engendre(N,H), combine(L,H,A), evaluer(F,A,1).*/ satisfait(F,A):-variables(F,L), combiner(L,A), evaluer(F,A). /*ordre superieure*/ map2_somme([],[],[]):-!. map2_somme([],_,_):-write("Arguments non valides"), fail. map2_somme(_,[],_):-write("Arguments non valides"), fail. map2_somme([X|L],[Y|G],[Z|R]):-(integer(X),integer(Y)) -> (Z is X+Y, map2_somme(L,G,R)); (write("Arguments non valides"), fail). map2(_,[],[],[]):-!. map2(_,[],_,_):-write("Arguments non valides"), fail. map2(_,_,[],_):-write("Arguments non valides"), fail. map2(P,[X|L],[Y|G],[Z|H]):-(integer(X),integer(Y)) -> (Q=..[P,X,Y,Z],Q,map2(P,L,G,H)); (write("Arguments non valides"), fail). produit(X,Y,Z):- Z is X*Y. map3(_,[],[],[],[]):-!. map3(_,[],_,_,_):-write("Arguments non valides"), fail. map3(_,_,[],_,_):-write("Arguments non valides"), fail. map3(_,_,_,[],_):-write("Arguments non valides"), fail. map3(P,[X|L],[Y|G],[W|K],[Z|H]):-(integer(X),integer(Y),integer(W)) -> (Q=..[P,X,Y,W,Z],Q,map3(P,L,G,K,H)); (write("Arguments non valides"), fail). somme_3(A,B,C,D):-D is A+B+C. /*jeux*/ move([X|L],[SX|L1]):- SX is X+1, takeone(L,L1). move([X|L],[X|T]):-move(L,T). takeone([X|L],[P|L]):-X>0, P is X-1. takeone([X|L],[X|L1]):-takeone(L,L1). gagne(X):-move(X,Y),not(gagne(Y)),!. gagnebis([_,M,D]):- D mod 2 =\= 0,!; M mod 2 =\= 0.