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