Author(s): Manuel Carro.
Version: 1.10#6 (2004/8/7, 21:46:39 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(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
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
Meta-predicate with arguments:
WakeupConds now acceptable are
Goalis delayed until
Goalis delayed until
Tis 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/2only fails it the
WakeupCondis not legally formed. If
WakeupCondis 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.
WakeupCondis a legal expression for delaying goals. (
Goalis a term which represents a goal, i.e., an atom or a structure. (
Tis a legal expression for delaying goals.
Go to the first, previous, next, last section, table of contents.