Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!gatech!howland.reston.ans.net!nntp.crl.com!decwrl!tc.pw.com!Ferguson.tc.pw.com!Don_Ferguson
From: Don_Ferguson@notes.pw.com (Don Ferguson)
Subject: Re: bagof vs. findall and variable results
Message-ID: <Don_Ferguson.2.01CA5A40@notes.pw.com>
Sender: news@tc.pw.com
Nntp-Posting-Host: ferguson.tc.pw.com
Organization: Price Waterhouse
X-Newsreader: Trumpet for Windows [Version 1.0 Rev B]
References:  <D7qu09.D33@sci.kun.nl>
Date: Wed, 3 May 1995 01:28:41 GMT
Lines: 43

In article <D7qu09.D33@sci.kun.nl> markjan@cs.kun.nl (Mark-Jan Nederhof) writes:
>From: markjan@cs.kun.nl (Mark-Jan Nederhof)
>Subject: bagof vs. findall and variable results
>Date: Fri, 28 Apr 1995 11:45:44 GMT

>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?

Unlike setof and bagof, findall assumes unbound variables are 
existentially quantified.  Note that bagof returns the same result as 
findall if it is called withY and Z existentially quantified, as in:

| ?- bagof(X, (Y,Z)^member(X, [Y,Y,Z]), L).

L = [_A,_B,_C]

So the renaming of unbound variables in the findall must be done
for the purposes of quantifying the variables so that they will not retain
their bindings.

Sicstus behaves identically to Quintus Prolog in this respect.


