Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!gatech!howland.reston.ans.net!news.sprintlink.net!EU.net!sun4nl!sci.kun.nl!cs.kun.nl!markjan
From: markjan@cs.kun.nl (Mark-Jan Nederhof)
Subject: Re: bagof vs. findall and variable results
Message-ID: <D81qA1.9n2@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
References: <D7qu09.D33@sci.kun.nl> <Don_Ferguson.2.01CA5A40@notes.pw.com>
Date: Thu, 4 May 1995 08:58:48 GMT
Lines: 41

In <Don_Ferguson.2.01CA5A40@notes.pw.com> Don_Ferguson@notes.pw.com (Don Ferguson) writes:

 > >| ?- [library(lists)].
 > >| ?- findall(X, member(X,[Y,Y,Z]), R).
 > >R = [_A,_B,_C] ?
 >
 > [...]
 >
 > Unlike setof and bagof, findall assumes unbound variables are
 > existentially quantified.

My thanks to Don Ferguson and others who have helped me to understand what
happens with unbound variables in findall. I describe some of the
misunderstandings I had before posting my question. Perhaps I'm helping
some novices like me.

First I thought that the existential quantification only pertained to
the variables *textually* occurring in the goal (i.e. 2nd argument of
findall). This is not true:

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

gives the same result for R as the above.

Next I thought that the property of being unbound was something detected
at the beginning of the execution of the goal. This is also not true:

| ?- Y=Z, findall(X, (Y=a, X=Z), R).
R = [a], Z = Y ?

So although Z is unbound at first, it becomes bound later, resulting in
R = [a], rather than R = [_A].

So to me it seems that *after* a solution has been found for the goal,
all unbound variables in the template are (consistently) replaced by
fresh variables.

(I trust the experts will correct me if I told an untruth.)

Mark-Jan Nederhof
Univ. Groningen
