% Copyright (C) 2003 Fred Mesnard <fred@univ-reunion.fr>
%
% This file is part of cTI.
%
% cTI is free software; you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the
% Free Software Foundation; either version 2 of the License, or (at your
% option) any later version.
%
% cTI is distributed in the hope that it will be useful, but WITHOUT
% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
% FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
% for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
% USA.
%
% For the most up-to-date information see the cTI's web site:
% http://www.cs.unipr.it/cTI/

:- module(dc,[dc/3]).
		
:- use_module(library(clpb)).
:- use_module(library(lists),[sublist/2]).
:- use_module(bool_op,[]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  dc(Vars, Tb, Tb_Dc)
%%    Tb_Dc est la "+ grande"  disj de conj de vars  =>  Tb
%%    ie    est la partie >=0

dc(_,Tb,1) :-taut(Tb =:= 1,1),!.
dc(_,Tb,0) :-taut(Tb =:= 0,1),!.
dc([V|Vars],Tb,Tb_Dc) :-
	length([V|Vars],L),
        generer(1,L,[V|Vars],Tb,0,Tb_Dc).
	
generer(I,N,Vars,Tb,D1s,D2s) :-
	I =< N, !, J is I+1,
	findall(Vars-S,(length(L,I),sublist(L,Vars),pdt(L,S),taut((S =< Tb),1)),Disjs),
	filtrer(Disjs,Vars,D1s,D3s),
	generer(J,N,Vars,Tb,D3s,D2s).
generer(I,N,_,_,Ds,Ds):-I is N+1.

pdt([X],X) :- !.
pdt([X|Xs],X*S) :- pdt(Xs,S).

filtrer([],_Vars,D,D).
filtrer([Vars-D|Ds],Vars,Doks,Dnvx) :-
	(bool_op:satisfiable(Doks) ->
	    (taut((D =< Doks),1) ->
		filtrer(Ds,Vars,Doks,Dnvx)
	    ;
		filtrer(Ds,Vars,Doks+D,Dnvx))
	;
	    filtrer(Ds,Vars,D,Dnvx)).
	    





