View source with raw comments or as raw
    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(+, 0),
   42    suspend_list(+, 0),
   43    trigger(+, 0),
   44    trigger_disj(+, 0),
   45    trigger_conj(+, +, 0).

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.

    ...
    when(ground(Expr), 0 =:= Expr),
author
- Tom Schrijvers (initial implementation)
- Jan Wielemaker */
 when(+Condition, :Goal)
Execute Goal when Condition is satisfied. I.e., Goal is executed as by call/1 if Condition is true when when/2 is called. Otherwise Goal is delayed until Condition becomes true. Condition is one of the following:

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).
 $eval_when_condition(+Condition, -Optimised)
C-building block defined in pl-attvar.c. It pre-processes the when-condition, checks it for errors (instantiation errors, domain-errors and cyclic terms) and simplifies it. Notably, it removes already satisfied conditions from Condition, unifying Optimised to true if there is no need to suspend. Nested disjunctions are reported as or(List).
   98trigger_first(true, Goal) :-
   99    !,
  100    call(Goal).
  101trigger_first(nonvar(X), Goal) :-
  102    !,
  103    '$suspend'(X, when, trigger_nonvar(X, Goal)).
  104trigger_first(Cond, Goal) :-
  105    trigger(Cond, Goal).
  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,Goal).
  115trigger(or(GL),Goal) :-
  116    trigger_disj(GL, check_disj(_DisjID,GL,Goal)).
  117
  118trigger_nonvar(X, Goal) :-
  119    (   nonvar(X)
  120    ->  call(Goal)
  121    ;   '$suspend'(X, when, trigger_nonvar(X, Goal))
  122    ).
 trigger_ground(@Term, :Goal)
Trigger Goal when Term becomes ground. The current implementation uses nonground/2, waiting for an arbitrary variable and re-check Term when this variable is bound. Previous version used term_variables and suspended on a term constructed from these variables. It is clear that either approach performs better on certain types of terms. The term_variables/2 based approach wins on large terms that are almost ground. Possibly we need a nonground that also returns the number of tests performed and switch to the term_variables/2 based approach if this becomes large.
  136trigger_ground(X, Goal) :-
  137    (   nonground(X, V)
  138    ->  '$suspend'(V, when, trigger_ground(X, Goal))
  139    ;   call(Goal)
  140    ).
  141
  142trigger_determined(X, Y, Goal) :-
  143    unifiable(X, Y, Unifier),
  144    !,
  145    (   Unifier == []
  146    ->  call(Goal)
  147    ;   put_attr(Det, when, det(trigger_determined(X,Y,Goal))),
  148        suspend_list(Unifier, wake_det(Det))
  149    ).
  150trigger_determined(_, _, Goal) :-
  151    call(Goal).
  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(Goal)
  161    ;
  162            true
  163    ).
  164
  165trigger_conj(G1,G2,Goal) :-
  166    trigger(G1, trigger(G2,Goal)).
  167
  168trigger_disj([],_).
  169trigger_disj([H|T], G) :-
  170    trigger(H, G),
  171    trigger_disj(T, G).
 check_disj(DisjVar, Disj, Goal)
If there is a disjunctive condition, we share a variable between the disjunctions. If the goal is fired due to one of the conditions, the shared variable is bound to (-). Note that this implies that the attributed variable is left in place. The predicate when_goal//1 skips such goals on behalf of copy_term/3.
  182check_disj(Disj,_,Goal) :-
  183    (   Disj == (-)
  184    ->  true
  185    ;   Disj = (-),
  186        call(Goal)
  187    ).
  188
  189suspend_list([],_Goal).
  190suspend_list([V=W|Unifier],Goal) :-
  191    '$suspend'(V, when, Goal),
  192    (   var(W)
  193    ->  '$suspend'(W, when, Goal)
  194    ;   true
  195    ),
  196    suspend_list(Unifier,Goal).
  197
  198attr_unify_hook(call(Goal), Other) :-
  199    (   get_attr(Other, when, call(GOTher))
  200    ->  del_attr(Other, when),
  201        Goal, GOTher
  202    ;   Goal
  203    ).
  204
  205
  206%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  207attribute_goals(V) -->
  208    { get_attr(V, when, Attr) },
  209    when_goals(Attr).
  210
  211when_goals(det(trigger_determined(X, Y, G))) -->
  212    when_trigger_goal(?=(X,Y), G).
  213when_goals(call(Conj)) -->
  214    when_conj_goals(Conj).
  215
  216when_conj_goals((A,B)) -->
  217    !,
  218    when_conj_goals(A),
  219    when_conj_goals(B).
  220when_conj_goals(when:G) -->
  221    when_goal(G).
  222
  223when_goal(trigger_nonvar(X, G)) -->
  224    when_trigger_goal(nonvar(X), G).
  225when_goal(trigger_ground(X, G)) -->
  226    when_trigger_goal(ground(X), G).
  227when_goal(wake_det(_)) -->
  228    [].
  229
  230when_trigger_goal(Cond, when:trigger(InnerCond, InnerGoal)) -->
  231    !,
  232    when_trigger_goal((Cond, InnerCond), InnerGoal).
  233when_trigger_goal(_, when:check_disj(Disj, _, _)) -->
  234    { Disj == (-) },
  235    !,
  236    [].
  237when_trigger_goal(_, when:check_disj(-, OrList, InnerGoal)) -->
  238    { OrList \== [] },
  239    !,
  240    { or_list(OrList, OrCond) },
  241    when_trigger_goal(OrCond, InnerGoal).
  242when_trigger_goal(Cond, Goal) -->
  243    [ when(Cond, Goal) ].
  244
  245or_list([H], HCond) :-
  246    !,
  247    or_list_member(H, HCond).
  248or_list([H|T], (HCond;OT)) :-
  249    or_list_member(H, HCond),
  250    or_list(T, OT).
  251
  252or_list_member(or(OrList), OrCond) :-
  253    !,
  254    or_list(OrList, OrCond).
  255or_list_member((L, R), (LCond, RCond)) :-
  256    !,
  257    or_list_member(L, LCond),
  258    or_list_member(R, RCond).
  259or_list_member(Cond, Cond).
  260
  261:- multifile sandbox:safe_meta_predicate/1.  262
  263sandbox:safe_meta_predicate(when:when/2)