A predicate may have one or more delay clauses. They have to be textually before and consecutive with the normal clauses of the predicate they belong to. The simplest example for a delay clause is one that checks if a variable is instantiated:delay <Head> if <Body>.
The operational semantics of the delay clauses is as follows: when a procedure with delay clauses is called, then the delay clauses are executed before executing the procedure itself. If one of the delay clauses succeeds, the call is suspended, otherwise they are all tried in sequence and, if all delay clauses fail, the procedure is executed as usual.delay report_binding(X) if var(X). report_binding(X) :- printf("Variable has been bound to %w\n", [X]).
does not match the goal p(A, b) but it matches the goal p(a, b).delay p(a, X) if var(X).
delay integer_list(L) if var(L). delay integer_list([X|_]) if var(X). integer_list([]). integer_list([X|T]) :- integer(X), integer_list(T).
delay p(X, X, Y) if var(Y).
delay p(X) if compound(X), arg(1, X, Y), nonground(Y).
delay p(X) if nonground(2, X).
executed with the call ?- p(a, b) of course succeeds and the call delays forever, since no variable binding can wake it.delay p(X, Y) if X \== Y.