# Aggregation

**URL:** <https://swi-prolog.discourse.group/t/aggregation/2122>\
**Category:** Help!\
**Created:** [April 8, 2020, 11:09am UTC](https://swi-prolog.discourse.group/t/aggregation/2122 "2020-04-08T11:09:41Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [April 8, 2020, 5:01pm UTC](https://swi-prolog.discourse.group/t/aggregation/2122/9 "2020-04-08T17:01:09Z")

</div>

> [@gavin.mendel.gleason](#):
>
> The semantics of setof, are in my opinion “wrong”, and I don’t think I’m alone in thinking this. It’s not sensible from the point of view of proving the set of results to conflate the empty set with failure.

Richard O’Keefe (_Craft of Prolog_ page 363) quotes Lee Naish in pointing out that `findall/3` can be used to define all sorts of nasty (non-logical) things such as `not/1` and `var/1`:

```prolog
not(Goal) :- findall(., Goal, []).
var(Var) :- findall(., (Var = a; Var = b), [_,_]).

```

If you want to conflate failure with `[]`, it’s easy enough:

```prolog
setof_or_nil(Template, Goal, Result) :-
    ( setof(Template, Goal, Result) -> true ; Result = [] ).

```

and it’s up to you to confirm that `Goal` and `Template` allow only sound results.

And if you don’t like the way existential variables are specified with `bagof` and `setof`, see [library(solution\_sequences)](https://www.swi-prolog.org/pldoc/man?section=solutionsequences). And [library(aggregate)](https://www.swi-prolog.org/pldoc/man?section=aggregate) gives some generalizations on bagof/setof.

---

_[View the full topic](https://swi-prolog.discourse.group/t/aggregation/2122)._
