:- dynamic(solution/1). :- use_module(library(reif)). :- use_module(library(iso_ext)). :- use_module(library(debug)). foo(1). foo(2). bar(3). findn(Vars0, Goal0, Result) :- retractall(solution(_)), assertz(solution([])), copy_term_nat(Goal0, Goal), term_variables(Goal, Vars), (collect_solutions(Vars, Goal); true), solution(Result). collect_solutions(Vars, Goal) :- call(Goal), write(Vars), retract(solution(S0)), assertz(solution([Vars | S0])), false.