[comp.lang.prolog] bagof/setof

bimbart@kulcs.uucp (Bart Demoen) (09/12/88)

In article <MATSC.88Sep7095859@skarabe.sics.se> Mats Carlson argues for
another behavior of bagof/setof

I am very glad with it: BIMprolog has always implemented bagof/setof with the
behavior Mats proposes (*) and I think it is very reasonable - although slowing
down some benchmarks


I still have a question:

database:

	p(1,a) .
	p(_,b) .
	p(2,c) .

query:

	?- setof(Y,p(X,Y),L) .

answers:

	SICSTUSprolog:
	
	X = 1 , L = [a,b]
	X = 2 , L = [b,c]


	BIMprolog:
	
	X = 1 , L = [a]
	X = _0 , L = [b]
	X = 2 , L = [c]


	CProlog:
	
	X = 1 , L = [a,b]
	X = 2 , L = [c]

my question is: which is more reasonable and/or useful and/or satisfying ?

(*) BIMprolog gives the answers in the order

	X = _49, L = [a,b] ;
	X = f(_141), L = [a,b] ;
	X = f(_165,_141), L = [a,b] ;
	X = f(_141,_141), L = [a,b] ;

(i.e. the last 2 solutions in a different order)
Mats, was that a misprint in your mail ?

lee@munnari.oz (Lee Naish) (09/14/88)

In article <1436@kulcs.kulcs.uucp> bimbart@kulcs.UUCP (Bart Demoen) writes:
>	p(1,a) .
>	p(_,b) .
>	p(2,c) .
>	?- setof(Y,p(X,Y),L) .
>
>	SICSTUSprolog:
>	X = 1 , L = [a,b]
>	X = 2 , L = [b,c]

"Logically", there are three answers (the last one containing two
constraints on X):
	X = 1 , L = [a,b];
	X = 2 , L = [b,c];
	X ~= 1, X ~= 2, L = [b].
Therefore, I think Sicstus gives the most "logical" set of solutions.
In other, less complex cases, setof generally returns what a logical
all solutions predicate returns except for answers which must contain
constraints.  It seems that Sicstus is more consistent than other
systems.  Does anyone know if it is completely consistent?
Perhaps there is another "logical" way of explaining the behaviour of
other systrems.  (NU-Prolog setof behaves the same as C-Prolog, and we
hope most Edinburgh systems, for compatability).

	lee

	lee

ok@quintus.uucp (Richard A. O'Keefe) (09/14/88)

In article <1436@kulcs.kulcs.uucp> bimbart@kulcs.UUCP (Bart Demoen) writes:
>database:
>	p(1, a).	p(_, b).	p(2, c).
>query:
>	?- setof(Y,p(X,Y),L) .
>answers:
>	SICSTUSprolog:
>	X = 1, L = [a,b] ;
>	X = 2, L = [b,c]
>
>	BIMprolog:
>	
>	X = 1, L = [a] ;
>	X = _0, L = [b] ;
>	X = 2, L = [c]
>
>	C Prolog:
>	
>	X = 1, L = [a,b] ;
>	X = 2, L = [c]
>
>My question is: which is more reasonable and/or useful and/or satisfying?

Quintus Prolog:
	X = _, L = [b] ;
	X = 1, L = [a] ;
	X = 2, L = [c]

Note that the bindings of X are enumerated in standard order (variables
precede numbers).  Within each binding for X, the elements of L are in
standard order.

I am able to say that C Prolog's behaviour is a bug;
that particular result is unintended.

There are three ground solutions:
	X = 1 -> L = [a,b] ;
	X = 2 -> L = [b,c] ;
	ground(X), X ~= 1, X ~= 2 -> L = [b]

SICStus Prolog gets two of them exactly right, but misses the third
case entirely.  BIMprolog and Quintus Prolog notice that there are
three cases, and their results cover p.

What this example illustrates is that to handle "all solutions" logically
you need
    - coroutining (to wait until X is sufficiently instantiated), or
    - constraints (to obtain the solution X~=1, X~=2, L=[b])

As things stand, setof(Template, Generator, Set) is only defined when all
the solutions of Generator found are ground.

matsc@sics.se (Mats Carlsson) (09/14/88)

Bart Demoen writes:
>I still have a question:
>database:  p(1,a). p(_,b). p(2,c). 
>query:     ?- setof(Y,p(X,Y),L).
>answers:
>...

As of version 0.6 #6, SICStus Prolog answers:

	X = _49, L = [b] ;
	X = 1, L = [a] ;
	X = 2, L = [c] ;

>(*) BIMprolog gives the answers in the order
>...
>(i.e. the last 2 solutions in a different order)
>Mats, was that a misprint in your mail ?

No.  I consider the order immaterial.  It depends on exactly how
you go about renaming variables.

jax@well.UUCP (Jack J. Woehr) (09/15/88)

	Uh, Rich, ole buddy, will you tell me when Quintus starts hiring
so I too can get a job that lets me spend all day and all night posting
to the net? :-)

**********
jax@well		programs(jax) :-
jax@chariot			uses_forth(vesta_technology),
JAX on GEnie			plays_with_Prolog(jax,when_boss_ain't_looking).