% Figure 7.4   An implementation of the findall relation,
% renamed findall1, because findall is built-in in SWI Prolog


findall1( X, Goal, Xlist)  :-
  call( Goal),                         % Find a solution
  assertz( queue(X) ),                 % Assert it
  fail;                                % Try to find more solutions
  assertz( queue(bottom) ),            % Mark end of solutions
  collect( Xlist).                     % Collect the solutions 

collect( L)  :-
  retract( queue(X) ), !,              % Retract next solution
  ( X == bottom, !, L = []             % End of solutions?
    ;
    L = [X | Rest], collect( Rest) ).  % Otherwise collect the rest

% age relation for examples
age( peter,7).
age( ann,5).
age( pat,8).
age( tom,5).

