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

% Control of Search Size 
% (We use here Depth-First Search - Backtracking)
% 
% 2) The ordering of clauses in a predicate:

% Consider calling q:
a(X,Y) :- 
    q(X,Y),
    p(X).
% versus calling a reordered version qr:
b(X,Y) :-
    qr(X,Y),
    p(X).


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.

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

% Compare the time running
% ?- time(a(X,Y)).
% or
% ?- time(b(X,Y)).
% 
% Also, the order of solutions is different:
% ?- a(X,Y).
% we get:
% X = 4,
% Y = c ? ;
% X = 4,
% Y = d ? ;
% 
% and for: 
% ?- b(X,Y).
% we get: 
% X = 4,
% Y = d ? ;
% X = 4,
% Y = c ? ;


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