[comp.lang.prolog] SBProlog setof bug

kens@hpldola.HP.COM (Ken Shrum) (02/27/88)

I'm having a problem using setof in SBProlog.  Interactive queries work
fine, but non-direct calls fail in various ways.

Given:

test1(X, C, L) :-
	setof(X, C, L).

test2(L) :-
	setof(X, '$member'(X, [1]), L).

test3(L) :-
	call(setof(X, '$member'(X, [1]), L)).

| ?- setof(X, '$member'(X, [1]), L).

X = _
L = [1]
yes
| ?- test1(X, '$member'(X, [1]), L).

<same>

| ?- test2(L).

<bus error, core dumped>

| ?- test3(L).

no

Is this a known bug, and is there a fix?

	Ken Shrum
	hplabs!hpldola!kens

debray@arizona.edu (Saumya Debray) (03/01/88)

This may have been a problem in older versions of SB-Prolog.
(the current version is 2.3.1, and is available, as before,
by anonymous FTP from arizona.edu.  There are several large
files in tar format (*.tar) that should be copied in binary
mode.)  I ran the test programs on this version and found no
problems.  I've included the source for setof/bagof below,
for those people using older versions of SB-Prolog.  The code
should be copied to the file modlib/src/$setof.P, and compiled
into the file modlib/$setof .

Note: This version of setof uses the integer division operator //.
      Some older versions of SB-Prolog didn't have this operator.
      If the version you have is one such, it'd be a good idea to
      go and change the //'s to /'s (they occur on source lines
      133 and 170).
---------------------------- cut here ----------------------------
/************************************************************************
*									*
* The SB-Prolog System							*
* Copyright SUNY at Stony Brook, 1986; University of Arizona,1987	*
*									*
************************************************************************/

/*-----------------------------------------------------------------
SB-Prolog is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.  No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing.  Refer to the SB-Prolog General Public
License for full details.

Everyone is granted permission to copy, modify and redistribute
SB-Prolog, but only under the conditions described in the
SB-Prolog General Public License.   A copy of this license is
supposed to have been given to you along with SB-Prolog so you
can know your rights and responsibilities.  It should be in a
file named COPYING.  Among other things, the copyright notice
and this notice must be preserved on all copies. 
------------------------------------------------------------------ */
/* setof : 'setof', 'bagof' and sorting. */

$setof_export([$setof/3,$bagof/3,$findall/3,$sort/2,$keysort/3]).

/* $setof_use($meta,[$functor/3,$univ/2,$length/2]).
   $setof_use($buff,[$alloc_perm/2,$alloc_heap/2,$trimbuff/3,$buff_code/4,
		$symtype/2,
		$substring/6,$subnumber/6,$subdelim/6,$conlength/2,
		$pred_undefined/1, $hashval/3]).
   $setof_use($bmeta,[$atom/1,$atomic/1,$integer/1,$number/1,$structure/1,
	$functor0/2,$bldstr/3,$arg/3,$arity/2,$real/1,$floor/2]).
*/

$setof(X,P,Set) :-
   $bagof(X,P,Bag),
   $sort(Bag,Set0),
   Set=Set0.

$bagof(X,P,Bag) :-
   $excess_vars(P,X,[],L), $nonempty(L), !,
   $univ(Key,[$|L]),
   $findall(Key-X,P,Bags0),
   $keysort(Bags0,Bags),
   $pick(Bags,Key,Bag).
$bagof(X,P,Bag) :-
   $findall(X,P,Bag).

$nonempty([_|_]).

$pick(Bags,Key,Bag) :-
   $nonempty(Bags),
   $parade(Bags,Key1,Bag1,Bags1),
   $decide(Key1,Bag1,Bags1,Key,Bag).

$parade([Item|L1],K,[X|B],L) :- $item(Item,K,X), !,
   $parade(L1,K,B,L).
$parade(L,K,[],L).

$item(K-X,K,X).

$decide(Key,Bag,Bags,Key,Bag) :- (Bags=[], ! ; true).
$decide(_,_,Bags,Key,Bag) :- $pick(Bags,Key,Bag).

$excess_vars(T,X,L0,L) :- var(T), !,
   ( $no_occurrence(T,X), !, $introduce(T,L0,L)
   ; L = L0 ).
$excess_vars(X^P,Y,L0,L) :- !, $excess_vars(P,(X,Y),L0,L).
$excess_vars(setof(X,P,S),Y,L0,L) :- !, $excess_vars((P,S),(X,Y),L0,L).
$excess_vars(bagof(X,P,S),Y,L0,L) :- !, $excess_vars((P,S),(X,Y),L0,L).
$excess_vars(T,X,L0,L) :- $functor(T,_,N), $rem_excess_vars(N,T,X,L0,L).

$rem_excess_vars(0,_,_,L,L) :- !.
$rem_excess_vars(N,T,X,L0,L) :-
   arg(N,T,T1),
   $excess_vars(T1,X,L0,L1),
   N1 is N-1,
   $rem_excess_vars(N1,T,X,L1,L).

$introduce(X,L,L) :- $included(X,L), !.
$introduce(X,L,[X|L]).

$included(X,L) :- $doesnt_include(L,X), !, fail.
$included(X,L).

$doesnt_include([],X).
$doesnt_include([Y|L],X) :- Y \== X, $doesnt_include(L,X).

$no_occurrence(X,Term) :- $contains(Term,X), !, fail.
$no_occurrence(X,Term).

$contains(T,X) :- var(T), !, T == X.
$contains(T,X) :- $functor(T,_,N), $upto(N,I), arg(I,T,T1), $contains(T1,X).

$upto(N,N) :- N > 0.
$upto(N,I) :- N > 0, N1 is N-1, $upto(N1,I).

/*---------------------------------------------------------------------------- */
/* Sorting by bisecting and merging. */

$sort(L,R) :- $length(L,N), $sort(N,L,_,R1), R=R1.

$sort(N,U,L,R) :-
	N > 2 ->
		(N1 is N//2, N2 is N-N1,
		 $sort(N1,U,L2,R1),
		 $sort(N2,L2,L,R2),
		 $merge(R1,R2,R)
		) ;
		(N =:= 2 ->
			(U = [X1|L1],
			 L1 = [X2|L],
			 compare(Delta,X1,X2),
			 (Delta = (<) -> R = [X1,X2] ;
			  Delta = (>) -> R = [X2,X1] ;
			  		 R = [X2]
			 )
			) ;
			(N =:= 1 ->
				(U = [X|L], R = [X]) ;
				(U = L, R = [])		/* N == 0 */
			)
		 ).

$merge([],R,R) :- !.
$merge(R,[],R) :- !.
$merge(R1,R2,[X|R]) :-
   R1 = [X1|R1a], R2 = [X2|R2a],
   compare(Delta,X1,X2),
  (Delta = (<) , !, X = X1, $merge(R1a,R2,R)
   ; Delta = (>) , !, X = X2, $merge(R1,R2a,R)
   ; X = X1, $merge(R1a,R2a,R)
  ).

/*------------------------------------------------------------------------ */
/* Sorting on keys by bisecting and merging. */

$keysort(L,R) :- $length(L,N), $keysort(N,L,_,R1), R=R1.

$keysort(N,U,L,R) :-
	N > 2 ->
	     (N1 is N // 2, N2 is N-N1,
	      $keysort(N1,U,L2,R1),
   	      $keysort(N2,L2,L,R2),
   	      $keymerge(R1,R2,R)
	     ) ;
	     (N =:= 2 ->
	          (U = [X1|L1],
		   L1 = [X2|L],
		   $compare_keys(Delta,X1,X2),
		   (Delta = (>) -> R = [X2,X1] ; R = [X1,X2])
		  ) ;
		  (N =:= 1 ->
		  	(U = [X|L], R = [X]) ;
			(U = L, R = [])		/* N == 0 */
		  )
	     ).

$keymerge([],R,R) :- !.
$keymerge(R,[],R) :- !.
$keymerge(R1,R2,[X|R]) :-
   R1 = [X1|R1a], R2 = [X2|R2a],
   $compare_keys(Delta,X1,X2),
  (Delta = (>) , !, X = X2, $keymerge(R1,R2a,R)
   ; X = X1, $keymerge(R1a,R2,R)
  ).

$compare_keys(Delta,K1-X1,K2-X2) :- compare(Delta,K1,K2).

/*======================================================================*/

X^P :- call(P).

/*======================================================================*/


$findall(T,Call,Result) :-
	$alloc_heap(5000,Buff),
	$findall_1(T,Call,Result,Buff).

$findall_1(T,Call,Result,Buff) :-
	$copyterm([],Buff,8,4,_), /* init result list to empty */
	$buff_code(Buff,0,2 /*pn*/ ,8), /* init where to put next answer */
	$buff_code(Buff,4,2 /*pn*/ ,12), /* init first free place */
	call(Call),
	$buff_code(Buff,0,5 /*gn*/ ,Place), /* get where to put answer */
	$buff_code(Buff,4,5 /*gn*/ ,Start), /* get first free place */
	$copyterm([T],Buff,Place,Start,End),
	Tailloc is Start+4,	
	$buff_code(Buff,0,2 /*pn*/ ,Tailloc), /* where to put next answer */
	$buff_code(Buff,4,2 /*pn*/ ,End), /* next first free place */
	fail.

$findall_1(_,_,Result,Buff) :-
	$buff_code(Buff,4,5 /*gn*/ ,Length), /* Length =\= 12 fail if [] */
	$trimbuff(Length,Buff,1),
	$buff_code(Buff,8,18 /*vtb*/ ,Result).


/* This routine copies a term into a buffer. It is passed:
	Term: the term to copy,
	Buffer: the buffer to copy it into,
	Worddisp: the word of the buffer in which to put the copy (or
		a pointer to the copy.)
	Start: the disp of the next free location in the buffer, before the 
		copy is done.
	End: (returned) the location of the first free location after the
		copying.

	Variables are copied into the buffer and the copied variables are
	pointed into the buffer and trailed. Thus later binding of these 
	`outside' variables will cause the copied variables to be changed,
	too. If, however, the $copyterm call is failed over, the variables
	in the buffer will be ``disconnected'' from the outer variables.

	Copyterm is a prime candidate for moving down into the simulator as
	a builtin written in C.
*/


$copyterm(Term,Buff,Worddisp,Start,Start) :-
	var(Term),!,$buff_code(Buff,Worddisp,17 /*pvar*/ ,Term).

$copyterm(Term,Buff,Worddisp,Start,Start) :-
	number(Term),!,$buff_code(Buff,Worddisp,14 /*ptv*/ ,Term).

$copyterm(Term,Buff,Worddisp,Start,Start) :-
	$atom(Term),!,$buff_code(Buff,Worddisp,14 /*ptv*/ ,Term).

$copyterm(Term,Buff,Worddisp,Start,End) :-
	Term=[_|_],!,
	$buff_code(Buff,Worddisp,16 /*ptl*/ ,Start), /* ptr to list rec */
	Newstart is Start+8, /* reserve rec space */
	$copyargs(Term,1,2,Buff,Start,Newstart,End).
	
$copyterm(Term,Buff,Worddisp,Start,End) :-
	$structure(Term),!,
	$buff_code(Buff,Worddisp,15 /*ptp*/ ,Start), /* ptr to str rec */
	$buff_code(Buff,Start,0 /*ppsc*/ ,Term), /* rec psc ptr */
	Argsloc is Start+4,
	$arity(Term,Arity),Newstart is Argsloc+4*Arity, /* reserve rec space*/
	$copyargs(Term,1,Arity,Buff,Argsloc,Newstart,End).
	
$copyargs(Term,Argno,Maxargs,Buff,Argloc,Start,End) :- 
	Argno > Maxargs,
	 Start=End;
	Argno =< Maxargs,
	 arg(Argno,Term,Arg),$copyterm(Arg,Buff,Argloc,Start,Mid),
	 Nargno is Argno+1, Nargloc is Argloc+4,
	 $copyargs(Term,Nargno,Maxargs,Buff,Nargloc,Mid,End).

/* ------------------------------ $setof.P ------------------------------ */

-- 
Saumya Debray		CS Department, University of Arizona, Tucson

     internet:   debray@arizona.edu
     uucp:       {allegra, cmcl2, ihnp4} !arizona!debray