From ok@atlas.otago.ac.nz Mon Feb 19 00:46:40 2001
Received: from atlas.otago.ac.nz (atlas.otago.ac.nz [139.80.32.250])
	by swi.psy.uva.nl (8.11.2/8.11.2) with ESMTP id f1INkaZ18350
	for <prolog@swi.psy.uva.nl>; Mon, 19 Feb 2001 00:46:37 +0100 (MET)
Received: (from ok@localhost)
	by atlas.otago.ac.nz (8.9.3/8.9.3) id MAA32110;
	Mon, 19 Feb 2001 12:46:19 +1300 (NZDT)
Date: Mon, 19 Feb 2001 12:46:19 +1300 (NZDT)
From: "Richard A. O'Keefe" <ok@atlas.otago.ac.nz>
Message-Id: <200102182346.MAA32110@atlas.otago.ac.nz>
To: 50000481@plink.cityu.edu.hk, ra27946@SCS.UBBCluj.Ro
Subject: Re: [SWIPL] using bagof
Cc: prolog@swi.psy.uva.nl

Rastei Amelia Viorela  <ra27946@SCS.UBBCluj.Ro> wrote:

    Prolog has two built-in predicates designed to collect together objects
    resulting from successful computations: 
	
Three.
	
	bagof(Things, Condition, Bag)
	setof(Things, Condition, Bag)
	
The first argument is not a collection of things, it is a template
exhibiting the shape of ONE thing.  To call the second argument a
"condition" may be misleading, it is a GENERATOR of results.  And
the last argument of setof/3 is not a Bag, it is a Set.   So

    findall(Template, Goal, List)
    bagof(Template, Generator, Bag)
    setof(Template, Generator, Set)

Existential quantifiers in the Goal of a findall/3 have no special effect,
but the use of existential quantifiers in the Generator of a bagof/3 or
setof/3 call is often essential.

Examples such as

    height(Node, H) :- setof(Z, ht(Node, Z), Set), max(Set, 0, H).
	'setof' computes a Set of distinct results. 
	
completely miss the point that has Cecilia Wong confused.

Suppose we have

    p(a, 1, y).
    p(a, 2, x).
    p(b, 1, v).
    p(b, 2, w).

?- findall(X, p(a,N,X), Xs).
 => var(X), var(N), Xs = [y,x].

?- setof(X, N^p(a,N,X), Xs).		N is existentially quantified
 => var(X), var(N), Xs = [x,y].		Xs is sorted with duplicates removed

?- setof(X, p(a,N,X), Xs).		N is ***SOLVED FOR***
 => var(X), N = 1, Xs = [x]
  | var(X), N = 2, Xs = [y]

It is this last use that could not possibly work in a reasonable way
if setof/3 were ever allowed to return an empty list.

?- setof(N-Xs, setof(X, p(a,N,X), Xs), Answers).
 => var(N), var(Xs), var(X),
    Answers = [1-[x],2-[y]].

and this nested use of setof/3 depends on variables not in the template
and not existentially quantified being SOLVED FOR by the inner call to
setof/3, hence relies on setof/3 never returning an empty list.

The Quintus library includes

    setofall(Template, Generator, Set)
    bagofall(Template, Generator, Bag)

which check that the Generator has no free variables, and in that case
*are* willing to return an empty Set or Bag.  That's the equivalent of
not/1 checking for a ground goal.  Just as \+ /1, which does not wait
for its argument to be ground nor check that it is ground, is known to
be logically unsound, so setof/3 and bagof/3 would be logically unsound
if they returned an empty list without checking that their Generator has
no free variables.

