:- module(_,_,[]).

% Control of Search Size 
% (We use here Depth-First Search - Backtracking)
% 
% 1) The ordering of literals in the body of a clause:

% Consider: 
a(X,Y) :- 
    p(X),   % We execute p first, 
    q(X,Y). % then q
% versus: 
b(X,Y) :-
    q(X,Y), % We execute q first,
    p(X).   % then p

p(4).
p(5).

q(1, a) :- lots_of_computing.
q(2, b) :- lots_of_computing.
q(4, c) :- lots_of_computing.
q(4, d) :- lots_of_computing.

% Compare the time running
% ?- time(a(X,Y)).
% or
% ?- time(b(X,Y)).
% 
% Note that optimal order depends on the variable 
% instantiation mode. E.g., for q(X,d), p(X), this 
% order is better than p(X), q(X,d).


:- use_module(library(system),[pause/1]).
lots_of_computing :- pause(3).
