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
   45:- use_module(library(apply)).   46
   47:- meta_predicate
   48	geler(+,?,0).   49
   50attr_unify_hook(g(CLP,goals(Gx),_),Y) :-
   51	!,
   52	(   var(Y),
   53	    (   get_attr(Y,clpcd_geler,g(A,B,C))
   54	    ->  ignore((CLP \== A,
   55                        throw(error(permission_error(
   56		                        'apply CLPCD constraints on',
   57                                        'variable of a different subdomain',Y),
   58		    context(_))))),
   59		(   % possibly mutual goals. these need to be run.
   60		    % other goals are run as well to remove redundant goals.
   61		    B = goals(Gy)
   62		->  Later = [Gx,Gy],
   63		    (   C = n
   64		    ->  del_attr(Y,clpcd_geler)
   65		    ;   put_attr(Y,clpcd_geler,g(CLP,n,C))
   66		    )
   67		;   % no goals in Y, so no mutual goals of X and Y, store
   68		    % goals of X in Y
   69		    % no need to run any goal.
   70		    Later = [],
   71		    put_attr(Y,clpcd_geler,g(CLP,goals(Gx),C))
   72		)
   73	    ;	Later = [],
   74		put_attr(Y,clpcd_geler,g(CLP,goals(Gx),n))
   75	    )
   76	;   nonvar(Y),
   77	    Later = [Gx]
   78	),
   79	maplist(call,Later).
   80attr_unify_hook(_,_). % no goals in X
   81
   82%
   83% called from project.pl
   84%
   85project_nonlin(_,Cvas,Reachable) :-
   86	collect_nonlin(Cvas,L,[]),
   87	sort(L,Ls),
   88	term_variables(Ls,Reachable).
   89	%put_attr(_,all_nonlin(Ls)).
   90
   91
   92collect_nonlin([]) --> [].
   93collect_nonlin([X|Xs]) -->
   94	(   { get_attr(X,clpcd_geler,g(_,goals(Gx),_)) }
   95	->  trans(Gx),
   96	    collect_nonlin(Xs)
   97	;   collect_nonlin(Xs)
   98	).
   99
  100% trans(Goals,OutList,OutListTail)
  101%
  102% transforms the goals (of the form run(Mutex,Goal)
  103% that are in Goals (in the conjunction form)
  104% that have not been run (Mutex = variable) into a readable output format
  105% and notes that they're done (Mutex = 'done'). Because of the Mutex
  106% variable, each goal is only added once (so not for each variable).
  107
  108trans((A,B)) -->
  109	trans(A),
  110	trans(B).
  111trans(run(Mutex,Gs)) -->
  112	(   { var(Mutex) }
  113	->  { Mutex = done },
  114	    transg(Gs)
  115	;   []
  116	).
  117
  118transg((A,B)) -->
  119	!,
  120	transg(A),
  121	transg(B).
  122transg(M:G) -->
  123	!,
  124	M:transg(G).
  125transg(G) --> [G].
  126
  127:- public run/2.  128:- meta_predicate run(?, 0 ).  129
  130% run(Mutex,G)
  131%
  132% Calls goal G if it has not yet run (Mutex is still variable)
  133% and stores that it has run (Mutex = done). This is done so
  134% that when X = Y and X and Y are in the same goal, that goal
  135% is called only once.
  136
  137run(Mutex,_) :- nonvar(Mutex), !.
  138run(Mutex,G) :-
  139	var(Mutex),
  140	Mutex = done,
  141	call(G).
  142
  143% geler(Vars,Goal)
  144%
  145% called by nf.pl when an unsolvable non-linear expression is found
  146% Vars contain the variables of the expression, Goal contains the predicate of
  147% nf.pl to be called when the variables are bound.
  148
  149geler(CLP,Vars,Goal) :-
  150	attach(Vars,CLP,run(_Mutex,Goal)).
  151	% one goal gets the same mutex on every var, so it is run only once
  152
  153% attach(Vars,Goal)
  154%
  155% attaches a new goal to be awoken when the variables get bounded.
  156% when the old value of the attribute goals = OldGoal, then the new value =
  157% (Goal,OldGoal)
  158
  159attach([],_,_).
  160attach([V|Vs],CLP,Goal) :-
  161	var(V),
  162	(   get_attr(V,clpcd_geler,g(A,B,C))
  163	->  (   CLP \== A
  164	    ->  throw(error(permission_error('apply CLP(Q) constraints on',
  165		    'CLP(R) variable',V),context(_)))
  166	    ;   (   B = goals(Goals)
  167	        ->  put_attr(V,clpcd_geler,g(A,goals((Goal,Goals)),C))
  168	        ;   put_attr(V,clpcd_geler,g(A,goals(Goal),C))
  169	        )
  170	    )
  171	;   put_attr(V,clpcd_geler,g(CLP,goals(Goal),n))
  172	),
  173	attach(Vs,CLP,Goal)