Author(s): Manuel Carro.
Version: 1.10#7 (2006/4/26, 19:22:13 CEST)
Version of last change: 1.7#3 (2000/7/21, 11:54:59 CEST)
when/2
delays a predicate until some condition in its variable is met. For example, we may want to find out the maximum of two numbers, but we are not sure when they will be instantiated. We can write the standard
max/3
predicate (but changing its name to
gmax/3
to denote that the first and second arguments must be ground) as
gmax(X, Y, X):- X > Y, !. gmax(X, Y, Y):- X =< Y.
and then define a 'safe'
max/3
as
max(X, Y, Z):- when((ground(X),ground(Y)), gmax(X, Y, Z)).
which can be called as follows:
?- max(X, Y, Z) , Y = 0, X = 8. X = 8, Y = 0, Z = 8 ? yes
Alternatively,
max/3
could have been defined as
max(X, Y, Z):- when(ground((X, Y)), gmax(X, Y, Z)).
with the same effects as above. More complex implementations are possible. Look, for example, at the max.pl
implementation under the when
library directory, where a
max/3
predicate is implemented which waits on all the arguments until there is enough information to determine their values:
?- use_module(library('when/max')). yes ?- max(X, Y, Z), Z = 5, Y = 4. X = 5, Y = 4, Z = 5 ? yes
when
)when
)
Meta-predicate with arguments: when(?,goal)
.
Usage: when(WakeupCond, Goal)
Goal
according to WakeupCond
given. The WakeupCond
s now acceptable are ground(T)
(
Goal
is delayed until T
is ground), nonvar(T)
(
Goal
is delayed until T
is not a variable), and conjunctions and disjunctions of conditions:
wakeup_exp(ground(_1)). wakeup_exp(nonvar(_1)). wakeup_exp((C1,C2)) :- wakeup_exp(C1), wakeup_exp(C2). wakeup_exp((C1;C2)) :- wakeup_exp(C1), wakeup_exp(C2).
when/2
only fails it the WakeupCond
is not legally formed. If WakeupCond
is met at the time of the call no delay mechanism is involved -- but there exists a time penalty in the condition checking.
In case that an instantiation fires the execution of several predicates, the order in which these are executed is not defined.
WakeupCond
is a legal expression for delaying goals.
(when:wakeup_exp/1
)
Goal
is a term which represents a goal, i.e., an atom or a structure.
(basic_props:callable/1
)
Usage: wakeup_exp(T)
T
is a legal expression for delaying goals.
when
)Go to the first, previous, next, last section, table of contents.