% 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(num_clph_clpn,[clph_clpn/2]).

:- use_module(library(lists),[append/3]).
:- use_module(predef,[predef_nat_bool_tc/4]).
:- use_module(utils,[tous_pos/2,suppress_empty_constraint/2,clause_head/2,clause_body/2,build_clause/3]).
:- use_module(library(terms),[term_variables/2]).
:- use_module(term_size,[term_size/2]).

clph_clpn(ClpHCl,ClpNCl) :-
	clause_head(ClpHCl,Hh),
	clause_body(ClpHCl,Bh),
	term_variables(Hh,VarsH),
	tous_pos(VarsH,Cs),
	tradnat_corps(Bh,Bn),
	suppress_empty_constraint(['$constraint'(Cs)|Bn],Body),
	build_clause(Hh,Body,ClpNCl).

tradnat_corps([],[]).
tradnat_corps([A|As],Ds) :-
	tradnat_elt(A,Bs),
	append(Bs,Cs,Ds),
	tradnat_corps(As,Cs).

tradnat_elt('$predef'('$num'([A])),['$constraint'([A|Cs])]):-
	!,predef_nat_bool_tc(A,Cs,_,_).
tradnat_elt('$predef'(A),['$predef'(A),'$constraint'(Cs)]):-
	!,predef_nat_bool_tc(A,Cs,_,_). %% '$bool' ???
tradnat_elt('$constraint'(Cpro),['$constraint'(Cnat)]):-
	!,tradnat_constraint(Cpro,Cnat).
tradnat_elt(A,['$constraint'(Cs),A]):-
	term_variables(A,Vs),
	tous_pos(Vs,Cs).

tradnat_constraint([],[]).
tradnat_constraint([Cp|Cps],Dns) :-
	tc(Cp,Cn),
	append(Cn,Cns,Dns),
	tradnat_constraint(Cps,Cns).

tc(X=Terme,[X=NormTerme|Cs]) :-
	term_size(Terme,NormTerme),
	term_variables2(NormTerme,X,Vars),
	tous_pos(Vars,Cs).

term_variables2(Z,_X,[]) :- Z==0,!.
term_variables2(NormTerme,X,[X|Vars]) :-
	term_variables(NormTerme,Vars).
