8.14 Coroutining

The coroutining facility can be accessed by a number of built-in predicates. This makes it possible to use coroutines in a dynamic way, without having to rely on block declarations:

Blocks Goal until the Condition is true, where Condition is a goal with the restricted syntax:

For example:

          | ?- when(((nonvar(X);?=(X,Y)),ground(T)), process(X,Y,T)).

Blocks Goal until nonvar(X) (see Meta Logic) holds. This is defined as if by:
          freeze(X, Goal) :- when(nonvar(X), Goal).


          :- block freeze(-, ?).
          freeze(_, Goal) :- Goal.

If some goal is blocked on the variable Var, or Var has attributes that can be interpreted as a goal (see Attributes), then that goal is unified with Goal. If no goals are blocked, Goal is unified with the atom true. If more than one goal is blocked, a conjunction is unified with Goal.
Constrains X and Y to represent different terms i.e. to be non-unifiable. Calls to dif/2 either succeed, fail, or are blocked depending on whether X and Y are sufficiently instantiated. It is defined as if by:
          dif(X, Y) :- when(?=(X,Y), X\==Y).

call_residue(:Goal,?Residue) obsolescent
The Goal is executed as if by call/1. If during the execution some attributes or blocked goals were attached to some variables, then Residue is unified with a list of VariableSet-Goal pairs, and those variables no longer have attributes or blocked goals attached to them. Otherwise, Residue is unified with the empty list [].

VariableSet is a set of variables such that when any of the variables is bound, Goal gets unblocked. Usually, a goal is blocked on a single variable, in which case VariableSet is a singleton.

Goal is an ordinary goal, sometimes module prefixed. For example:

          | ?- call_residue((dif(X,f(Y)), X=f(Z)), Res).
          X = f(Z),
          Res = [[Y,Z]-(prolog:dif(f(Z),f(Y)))]