1/*  Part of CLP(Q) (Constraint Logic Programming over Rationals)
    2
    3    Author:        Leslie De Koninck
    4    E-mail:        Leslie.DeKoninck@cs.kuleuven.be
    5    WWW:           http://www.swi-prolog.org
    6		   http://www.ai.univie.ac.at/cgi-bin/tr-online?number+95-09
    7    Copyright (C): 2006, K.U. Leuven and
    8		   1992-1995, Austrian Research Institute for
    9		              Artificial Intelligence (OFAI),
   10			      Vienna, Austria
   11
   12    This software is based on CLP(Q,R) by Christian Holzbaur for SICStus
   13    Prolog and distributed under the license details below with permission from
   14    all mentioned authors.
   15
   16    This program is free software; you can redistribute it and/or
   17    modify it under the terms of the GNU General Public License
   18    as published by the Free Software Foundation; either version 2
   19    of the License, or (at your option) any later version.
   20
   21    This program is distributed in the hope that it will be useful,
   22    but WITHOUT ANY WARRANTY; without even the implied warranty of
   23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   24    GNU General Public License for more details.
   25
   26    You should have received a copy of the GNU Lesser General Public
   27    License along with this library; if not, write to the Free Software
   28    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   29
   30    As a special exception, if you link this library with other files,
   31    compiled with a Free Software compiler, to produce an executable, this
   32    library does not by itself cause the resulting executable to be covered
   33    by the GNU General Public License. This exception does not however
   34    invalidate any other reasons why the executable file might be covered by
   35    the GNU General Public License.
   36*/
   37
   38:- module(clpcd_geler,
   39	[
   40	    geler/3,
   41	    project_nonlin/3,
   42	    collect_nonlin/3
   43	]).   44:- meta_predicate
   45	geler(+,?,0).   46
   47attr_unify_hook(g(CLP,goals(Gx),_),Y) :-
   48	!,
   49	(   var(Y),
   50	    (   get_attr(Y,clpcd_geler,g(A,B,C))
   51	    ->  ignore((CLP \== A,
   52                        throw(error(permission_error(
   53		                        'apply CLPCD constraints on',
   54                                        'variable of a different subdomain',Y),
   55		    context(_))))),
   56		(   % possibly mutual goals. these need to be run.
   57		    % other goals are run as well to remove redundant goals.
   58		    B = goals(Gy)
   59		->  Later = [Gx,Gy],
   60		    (   C = n
   61		    ->  del_attr(Y,clpcd_geler)
   62		    ;   put_attr(Y,clpcd_geler,g(CLP,n,C))
   63		    )
   64		;   % no goals in Y, so no mutual goals of X and Y, store
   65		    % goals of X in Y
   66		    % no need to run any goal.
   67		    Later = [],
   68		    put_attr(Y,clpcd_geler,g(CLP,goals(Gx),C))
   69		)
   70	    ;	Later = [],
   71		put_attr(Y,clpcd_geler,g(CLP,goals(Gx),n))
   72	    )
   73	;   nonvar(Y),
   74	    Later = [Gx]
   75	),
   76	maplist(call,Later).
   77attr_unify_hook(_,_). % no goals in X
   78
   79%
   80% called from project.pl
   81%
   82project_nonlin(_,Cvas,Reachable) :-
   83	collect_nonlin(Cvas,L,[]),
   84	sort(L,Ls),
   85	term_variables(Ls,Reachable).
   86	%put_attr(_,all_nonlin(Ls)).
   87
   88
   89collect_nonlin([]) --> [].
   90collect_nonlin([X|Xs]) -->
   91	(   { get_attr(X,clpcd_geler,g(_,goals(Gx),_)) }
   92	->  trans(Gx),
   93	    collect_nonlin(Xs)
   94	;   collect_nonlin(Xs)
   95	).
   96
   97% trans(Goals,OutList,OutListTail)
   98%
   99% transforms the goals (of the form run(Mutex,Goal)
  100% that are in Goals (in the conjunction form)
  101% that have not been run (Mutex = variable) into a readable output format
  102% and notes that they're done (Mutex = 'done'). Because of the Mutex
  103% variable, each goal is only added once (so not for each variable).
  104
  105trans((A,B)) -->
  106	trans(A),
  107	trans(B).
  108trans(run(Mutex,Gs)) -->
  109	(   { var(Mutex) }
  110	->  { Mutex = done },
  111	    transg(Gs)
  112	;   []
  113	).
  114
  115transg((A,B)) -->
  116	!,
  117	transg(A),
  118	transg(B).
  119transg(M:G) -->
  120	!,
  121	M:transg(G).
  122transg(G) --> [G].
  123
  124:- public run/2.  125:- meta_predicate run(?, 0 ).  126
  127% run(Mutex,G)
  128%
  129% Calls goal G if it has not yet run (Mutex is still variable)
  130% and stores that it has run (Mutex = done). This is done so
  131% that when X = Y and X and Y are in the same goal, that goal
  132% is called only once.
  133
  134run(Mutex,_) :- nonvar(Mutex), !.
  135run(Mutex,G) :-
  136	var(Mutex),
  137	Mutex = done,
  138	call(G).
  139
  140% geler(Vars,Goal)
  141%
  142% called by nf.pl when an unsolvable non-linear expression is found
  143% Vars contain the variables of the expression, Goal contains the predicate of
  144% nf.pl to be called when the variables are bound.
  145
  146geler(CLP,Vars,Goal) :-
  147	attach(Vars,CLP,run(_Mutex,Goal)).
  148	% one goal gets the same mutex on every var, so it is run only once
  149
  150% attach(Vars,Goal)
  151%
  152% attaches a new goal to be awoken when the variables get bounded.
  153% when the old value of the attribute goals = OldGoal, then the new value =
  154% (Goal,OldGoal)
  155
  156attach([],_,_).
  157attach([V|Vs],CLP,Goal) :-
  158	var(V),
  159	(   get_attr(V,clpcd_geler,g(A,B,C))
  160	->  (   CLP \== A
  161	    ->  throw(error(permission_error('apply CLP(Q) constraints on',
  162		    'CLP(R) variable',V),context(_)))
  163	    ;   (   B = goals(Goals)
  164	        ->  put_attr(V,clpcd_geler,g(A,goals((Goal,Goals)),C))
  165	        ;   put_attr(V,clpcd_geler,g(A,goals(Goal),C))
  166	        )
  167	    )
  168	;   put_attr(V,clpcd_geler,g(CLP,goals(Goal),n))
  169	),
  170	attach(Vs,CLP,Goal)