**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)
- Known bugs and planned improvements (when)

`when`

)`when`

)- PREDICATE:
**when/2:** -
*Meta-predicate*with arguments:`when(?,goal)`

.**Usage:**`when(WakeupCond, Goal)`

*Description:*Delays / executes`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.*The following properties should hold at call time:*`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`

)

- REGTYPE:
**wakeup_exp/1:** -
**Usage:**`wakeup_exp(T)`

*Description:*`T`

is a legal expression for delaying goals.

`when`

)- Redundant conditions are not removed.
- Floundered goals are not appropriately printed.

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