Coroutining deals with having Prolog goals scheduled for execution as soon as some conditions is fullfilled. In Prolog the most commonly used conditions is the instantiation (binding) of a variable. Scheduling a goal to execute immediately after a variable is bound allows may be used to avoid instantiation errors for some built-in predicates (e.g. arithmetic), do work lazy, prevent the binding of a variable to a particular value, etc. Using freeze/2 for example we can define a variable can only be assigned an even number:
?- freeze(X, X mod 2 =:= 0), X = 3 No |
freeze, so get_attr(Var, freeze, AttVal)
can be used to find out whether and which goals are delayed on Var.
true.
?=(X, Y), nonvar(X), ground(X),
,(Cond1, Cond2) or ;(Cond1,
Cond2). See also freeze/2
and dif/2.
The implementation can deal with cyclic terms.
The when/2
predicate is realised using attributed variable associated with the
module when. It is defined in the autoload library
library(when).
dif(X, Y) :- when(?=(X, Y), X \== Y). See also ?=/2.
The implementation can deal with cyclic terms.
The dif/2
predicate is realised using attributed variable associated with the
module dif. It is defined in the autoload library
library(dif).