☰

*ON THIS PAGE*# Delaying predicates (when)

**Author(s):** Manuel Carro, Remy Haemmerle.## Usage and interface

## Documentation on exports

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

## Documentation on imports

This module has the following direct dependencies:

`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

**Library usage:**`:- use_module(library(when)).`**Exports:***Predicates:*`when/2`.*Regular Types:*`wakeup_exp/1`.

PREDICATEwhen/2

Usage:`when(WakeupCond,Goal)`

Delays / executes Goal according to WakeupCond given. The WakeupConds 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:*

(`when:wakeup_exp/1`)WakeupCond is a legal expression for delaying goals.

(`basic_props:cgoal/1`)Goal is a term which represents a goal, i.e., an atom or a structure.

REGTYPEwakeup_exp/1

Usage:`wakeup_exp(T)`

T is a legal expression for delaying goals.

*System library modules:*`terms_vars`,`sort`,`sets`.*Packages:*`prelude`,`initial`,`condcomp`,`assertions`,`assertions/assertions_basic`,`isomodes`.

Generated with LPdoc using Ciao