1/* Part of SWI-Prolog 2 3 Author: Tom Schrijvers, K.U.Leuven 4 E-mail: Tom.Schrijvers@cs.kuleuven.ac.be 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2004-2016, K.U.Leuven 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without 10 modification, are permitted provided that the following conditions 11 are met: 12 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in 18 the documentation and/or other materials provided with the 19 distribution. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 POSSIBILITY OF SUCH DAMAGE. 33*/ 34 35:- module(when, 36 [ when/2 % +Condition, :Goal 37 ]). 38:- set_prolog_flag(generate_debug_info, false). 39 40:- meta_predicate 41 when( , ), 42 suspend_list( , ), 43 trigger( , ), 44 trigger_disj( , ), 45 trigger_conj( , , ).
nonvar(X)
ground(X)
For example (note the order a
and b
are written):
?- when(nonvar(X), writeln(a)), writeln(b), X = x. b a X = x
84when(Condition, Goal) :-
85 '$eval_when_condition'(Condition, Optimised),
86 trigger_first(Optimised, Goal).
true
if there is no need to suspend. Nested
disjunctions are reported as or(List)
.98trigger_first(true, Goal) :- 99 !, 100 call(). 101trigger_first(nonvar(X), Goal) :- 102 !, 103 '$suspend'(X, when, trigger_nonvar(X, Goal)). 104trigger_first(Cond, Goal) :- 105 trigger(Cond, ). 106 107trigger(nonvar(X),Goal) :- 108 trigger_nonvar(X,Goal). 109trigger(ground(X),Goal) :- 110 trigger_ground(X,Goal). 111trigger(?=(X,Y),Goal) :- 112 trigger_determined(X,Y,Goal). 113trigger((G1,G2),Goal) :- 114 trigger_conj(G1,G2,). 115trigger(or(GL),Goal) :- 116 trigger_disj(GL, check_disj(_DisjID,GL,Goal)). 117 118trigger_nonvar(X, Goal) :- 119 ( nonvar(X) 120 -> call() 121 ; '$suspend'(X, when, trigger_nonvar(X, Goal)) 122 ).
136trigger_ground(X, Goal) :- 137 ( nonground(X, V) 138 -> '$suspend'(V, when, trigger_ground(X, Goal)) 139 ; call() 140 ). 141 142trigger_determined(X, Y, Goal) :- 143 unifiable(X, Y, Unifier), 144 !, 145 ( Unifier == [] 146 -> call() 147 ; put_attr(Det, when, det(trigger_determined(X,Y,Goal))), 148 suspend_list(Unifier, wake_det(Det)) 149 ). 150trigger_determined(_, _, Goal) :- 151 call(). 152 153 154wake_det(Det) :- 155 ( var(Det) -> 156 get_attr(Det,when,Attr), 157 del_attr(Det,when), 158 Det = (-), 159 Attr = det(Goal), 160 call() 161 ; 162 true 163 ). 164 165trigger_conj(G1,G2,Goal) :- 166 trigger(G1, trigger(G2,)). 167 168trigger_disj([],_). 169trigger_disj([H|T], G) :- 170 trigger(H, ), 171 trigger_disj(T, ).
183check_disj(Disj,_,Goal) :- 184 ( Disj == (-) 185 -> true 186 ; Disj = (-), 187 call() 188 ). 189 190suspend_list([],_Goal). 191suspend_list([V=W|Unifier],Goal) :- 192 '$suspend'(V, when, Goal), 193 ( var(W) 194 -> '$suspend'(W, when, Goal) 195 ; true 196 ), 197 suspend_list(Unifier,). 198 199attr_unify_hook(call(Goal), Other) :- 200 ( get_attr(Other, when, call(GOTher)) 201 -> del_attr(Other, when), 202 , 203 ; 204 ). 205 206 207%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 208attribute_goals(V) --> 209 { get_attr(V, when, Attr) }, 210 when_goals(Attr). 211 212when_goals(det(trigger_determined(X, Y, G))) --> 213 !, 214 ( { disj_goal(G, Disj, DG) } 215 -> disj_or(Disj, DG) 216 ; { G = when:trigger(C, Goal) } 217 -> [ when((?=(X,Y),C), Goal) ] 218 ; [ when(?=(X,Y), G) ] 219 ). 220when_goals(call(Conj)) --> 221 when_conj_goals(Conj). 222 223when_conj_goals((A,B)) --> 224 !, 225 when_conj_goals(A), 226 when_conj_goals(B). 227when_conj_goals(when:G) --> 228 when_goal(G). 229 230when_goal(trigger_nonvar(X, G)) --> 231 ( { disj_goal(G, Disj, DG) } 232 -> disj_or(Disj, DG) 233 ; { G = when:trigger(C, Goal) } 234 -> [ when((nonvar(X),C), Goal) ] 235 ; [ when(nonvar(X),G) ] 236 ). 237when_goal(trigger_ground(X, G)) --> 238 ( { disj_goal(G, Disj, DG) } 239 -> disj_or(Disj, DG) 240 ; { G = when:trigger(C, Goal) } 241 -> [ when((ground(X),C), Goal) ] 242 ; [ when(ground(X),G) ] 243 ). 244when_goal(wake_det(_)) --> 245 []. 246 247disj_goal(when:check_disj(X, _, _), [], -) :- X == (-). 248disj_goal(when:check_disj(-, Or, DG), Or, DG). 249 250disj_or([], _) --> []. 251disj_or(List, DG) --> 252 { or_list(List, Or) }, 253 [when(Or, DG)]. 254 255or_list([H], H) :- !. 256or_list([H|T], (H;OT)) :- 257 or_list(T, OT). 258 259:- multifile sandbox:safe_meta_predicate/1. 260 261sandbox:safe_meta_predicate(when:when/2)
Conditional coroutining
This library implements the when/2 constraint, delaying a goal until its arguments are sufficiently instantiated. For example, the following delayes the execution of =:=/2 until the expression is instantiated.