%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%  The Prolog to {log} interface %%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%            November 2001 - by Gianfranco Rossi
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%This file contains the Prolog code that allows a Prolog program
%to use the {log} facilities for set/bag definition and manipulation
%without leaving the Prolog execution environment.
%Basically, this code takes care of consulting the {log} interpreter
%and provides a (Prolog) predicate for calling any {log} goal G, 
%possibly including {log} set constraints.
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

:- module(prolog2setlog,[
             setlog_call/1,
             setlog_call/2,
             setlog_consult/1,
             setlog_clause/1,
             user_h/0,
             user_help/0,
             setlog_help/0,
             setlog/0
   ]).

:- multifile no_interactive_mode/0.
:- dynamic no_interactive_mode/0.

setlog_call(Goal) :-
       setlog_call(Goal,_).
setlog_call(Goal,Constr_ext) :-
       copy_term(Goal,NewGoal),
       setlog:solve(NewGoal,Constr),
       setlog:postproc(Constr,Constr_ext),
       setlog:postproc(NewGoal,NewGoal_ext),
       setlog:postproc(Goal,Goal_ext),
       Goal_ext = NewGoal_ext.

setlog_consult(lib) :- !,
       assert(no_interactive_mode),
       setlog_call(consult('setlog_lib.txt'),_),
       retract(no_interactive_mode).
setlog_consult(File) :- 
       assert(no_interactive_mode),
       setlog_call(consult(File),_),
       retract(no_interactive_mode).

setlog_consult_int :-
       setlog_consult_int('setlog.pl').
setlog_consult_int(File) :-
       assert(no_interactive_mode),
       consult(File),
       retract(no_interactive_mode).

setlog_load_int :-
       setlog_load_int('setlog.ql').
setlog_load_int(File) :-
       assert(no_interactive_mode),
       ensure_loaded(File),
       retract(no_interactive_mode).

setlog_clause(Cl) :-
       setlog_call(assert(Cl),_).
      
user_h :- user_help.
user_help :- 
   nl,
   write('%%%%%%%% Prolog predicates for accessing {log} facilities %%%%%%%%%'),
   nl, nl,
   write('   -   setlog_call(G), setlog_call(G,C): to call a {log} '), nl,
   write('       goal G, possibly getting an (output) {log} constraint C'), nl,
   write('   -   setlog_consult(lib): to consult the {log} library '),nl,
   write('       file ''setlog.lib.txt'' '), nl,
   write('   -   setlog_consult(F): to consult any {log} file F '),nl,
   write('   -   setlog/0:  to enter the {log} interactive environment'),nl, 
   write('   -   setlog_help/0: to get help information about {log}'), nl.

setlog_help :- setlog_call(help).

:- setlog_consult_int.


