🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #783

error in bagof/3?

#783
From: Torben [gidius Mogensen
Date: Mon, 09 Apr 1990 10:58
35 lines
1724 bytes
I recently had a very elusive bug in a prolog program I wrote with a
college. After lengthy searching, we tracked the bug to an unexpected
behaviour of bagof/3. We simplified the problem to this program:



q(X) :-
	G = [Y, [1,2]],
	bagof(W, (G = [W,L],member(W,L)),X).

member(X,[X|_]).
member(X,[_|L]) :- member(X,L).


We would expect "?- q(X)." to return the single solution "X = [1,2]",
but to our surprise we got the to solutions "X = [1]" and "X = [2]".
We tried this both in Sicstus prolog and C-prolog, with identical
results. Is this the correct behaviour? 

If we insert a "copy_term(G,G1)" in the beginning of the inner goal,
and use "G1 = [W,L]", we get the same result, so the problem is not
that we change binding of variables outside the "bagof". If we move
either "Y" or the list "[1,2]" inside the "bagof", we get the expected
solution.

To solve our immediate problem, we programmed our own version of
"bagof", using "recordz" etc. After this, our program not only
executed correctly, but also ran 10 times faster in the cases where
the earlier version also worked (this was in Sicstus prolog). Our
"bagof" was specialized to our specific use, but still it was
surprising to see such a large difference.

regards,

Torben Mogensen (torbenm@diku.dk)
Technical Details
Message-ID: <1990Apr9.105812.168@diku.dk>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!dkuug!freja.diku.dk!torbenm

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!dkuug!freja.diku.dk!torbenm
From: torbenm@diku.dk (Torben [gidius Mogensen)
Newsgroups: comp.lang.prolog
Subject: error in bagof/3?
Keywords: bagof
Message-ID: <1990Apr9.105812.168@diku.dk>
Date: 9 Apr 90 10:58:12 GMT
Organization: Department Of Computer Science, University Of Copenhagen
Lines: 35