Go to the first, previous, next, last section, table of contents.


Delaying predicates (when)

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/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

Usage and interface (when)

Documentation on exports (when)

PREDICATE: when/2:

Meta-predicate with arguments: when(?,goal).

Usage: when(WakeupCond, Goal)

REGTYPE: wakeup_exp/1:

Usage: wakeup_exp(T)

Known bugs and planned improvements (when)


Go to the first, previous, next, last section, table of contents.