setof(C,[A,B]^(my_module:my_goal(A,B,C)),Set)
Usually, this would be written
setof(C, A^B^(my_module:my_goal(A,B,C)), Set)
.
It’s easy to get the “exists” items wrong (and even easier if there are anonymous variables, or “joining” variables), so I would suggest:
my_goal(C) :- my_module:my_goal(_A,_B,C)
... setof(C, my_goal(C), Set)
(also, if you import my_goal
, then you don’t need my_module:...
)
As to differences between findall/3 and bagof/3 (besides the existential variables), here are examples where findall/3 breaks “pure logic” (e.g., you can define not/1 or var/1 using findall/3):
# Notes about _findall/3_
_This is companion information to the SWI-Prolog manual page of the [`findall/3`](https://eu.swi-prolog.org/pldoc/doc_for?object=findall/3) predicate._
## TOC
- [_findall/3_ is somewhat problematic!](#findall_is_somewhat_problematic)
- [Intro](#intro)
- [Where to find explainers](#where_to_find_explainers)
- [Some examples for _findall/3_](#some_examples_for_findall)
- [Standard behaviour](#standard_behaviour)
- [Bad style warning](#bad_style_warning)
- [What if there is no solution for the subgoal?](#what_if_there_is_no_solution_for_a_subgoal)
- [_findall/3_ always generates all solutions of subgoal, irrespective of the size of _Bag_](#findall_doesnt_care_about_bagsize)
- [Edge case: bad _Bag_](#edge_case_bad_bag)
- [_findall/3_ has no "caret syntax" to existentially quantify variables](#findall_has_no_caret_syntax)
- [_bagof/3_ without caret](#bagof_without_caret)
- [_bagof/3_ with caret](#bagof_with_caret)
- [_bagof/3_ with ineffective caret](#bagof_with_ineffective_caret)
- [_findall/3_ always behaves like the "existentially quantified" _bagof/3_](#findall_always_behaves_like_an_existentially_quantified_bagof)
This file has been truncated. show original
This doesn’t mean that findall/3 shouldn’t be used, but you need to be aware of the same kinds of things that cause problems with “not” or negation-as failure. I prefer to use this instead of findall:
( findall(C, my_goal(C), set) -> true ; C = [] )