Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!gatech!swrinde!howland.reston.ans.net!news.nic.surfnet.nl!sun4nl!sci.kun.nl!cs.kun.nl!markjan
From: markjan@cs.kun.nl (Mark-Jan Nederhof)
Subject: bagof vs. findall and variable results
Message-ID: <D7qu09.D33@sci.kun.nl>
Sender: news@sci.kun.nl (News owner)
Nntp-Posting-Host: zeus.cs.kun.nl
Organization: University of Nijmegen, The Netherlands
X-Newsreader: NN version 6.4.19
Date: Fri, 28 Apr 1995 11:45:44 GMT
Lines: 38

I noticed this (to me) unexpected behaviour of findall in sicstus2.1.9:

| ?- [library(lists)].
| ?- findall(X, member(X,[Y,Y,Z]), R).
R = [_A,_B,_C] ?

where I would expect R = [Y,Y,Z] ?
In other words, findall/3 renames the variables in the result-list.
On the other hand, bagof/3 does not:

| ?- bagof(X, member(X,[Y,Y,Z]), R).
R = [Y,Y,Z] ?

and neither does setof/3:

| ?- setof(X, member(X,[Y,Y,Z]), R).
R = [Y,Z] ?

Nothing in the manual seems to explain this. What accounts for the
different behaviours of findall, bagof, and setof w.r.t. variables in
the result list? Is it a bug, a feature? Is this behaviour specific
for sicstus2.1.9 or does it generally hold for Prolog implementations?

PS you may ask what I need this for. Well, I wanted to make a list of pairs
connecting variable occurrences in terms to types in some type system.
So, what I'd like to do for some Term (a Prolog term) is 
        bagof( (Var,Type), var_type_occurrence(Term, (Var,Type)), List ).
where var_type_occurrence nondeterministically finds variable
occurrences in a term and associates them to some type.
I don't want renaming of Var's in List because afterwards I need to 
be able to identify one occurrence of a particular variable with another.
By the way, bagof would not have been my first choice here because it may 
fail instead of yielding the empty list in case there are no variable
occurrences in Term. In this respect, findall seemed preferable.

Thanks for any help,
  Mark-Jan Nederhof
  Univ. Groningen
