% Updated in Prova 1.90
% This example demonstrates the two ways "findall" can be used in Prova version 1.8 and above.
%
% If an untyped accumulator is used for collecting the list of predicate instantiations,
%    a classic "member" can be used to iterate over the list recursively.
% Alternatively, if an accumulator is of type java.util.List that is explicitely specified,
%    a built-in "element" allows iterating over the list non-deterministically and faster than
%    with "member".

:- eval(findall()).

findall() :-
	on_exception(java.lang.Throwable,on_any_exception()),
	test().

test() :-
	findall(
		[X1,X2],
		priority(X1,X2),
		Pairs),
	member(Pair,Pairs),
	println([Pair]).
test() :-
	findall(
		[X1,X2],
		priority(X1,X2),
		java.util.List.Pairs),
	element(Pair,java.util.List.Pairs),
	println([Pair]).

on_any_exception() :-
       exception(Ex),
       Ex.printStackTrace(),
       fail().

priority(a,b).
priority(b,c).

% General helper predicates
member(X,[X|Xs]).
member(X,[_|Xs]) :-
	member(X,Xs).
