/************************************************************************ * * * The SB-Prolog System * * Copyright SUNY at Stony Brook, 1986 * * * ************************************************************************/ /*----------------------------------------------------------------- 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. */ /* This was taken from the C-Prolog system, and modified to use $findall, instead of `record' */ $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(2,[X1|L1],L,R) :- !, $comprises(L1,X2,L), compare(Delta,X1,X2), (Delta = (<) , !, R = [X1,X2] ; Delta = (>) , !, R = [X2,X1] ; R = [X2] ). $sort(1,[X|L],L,[X]) :- !. $sort(0,L,L,[]) :- !. $sort(N,L1,L3,R) :- N1 is N/2, N2 is N-N1, $sort(N1,L1,L2,R1), $sort(N2,L2,L3,R2), $merge(R1,R2,R). $merge([],R,R) :- !. $merge(R,[],R) :- !. $merge(R1,R2,[X|R]) :- $comprises(R1,X1,R1a), $comprises(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) ). $comprises([X|L],X,L). /*------------------------------------------------------------------------ */ /* Sorting on keys by bisecting and merging. */ $keysort(L,R) :- $length(L,N), $keysort(N,L,_,R1), R=R1. $keysort(2,[X1|L1],L,R) :- !, $comprises(L1,X2,L), $compare_keys(Delta,X1,X2), (Delta = (>) , !, R = [X2,X1] ; R = [X1,X2] ). $keysort(1,[X|L],L,[X]) :- !. $keysort(0,L,L,[]) :- !. $keysort(N,L1,L3,R) :- N1 is N/2, N2 is N-N1, $keysort(N1,L1,L2,R1), $keysort(N2,L2,L3,R2), $keymerge(R1,R2,R). $keymerge([],R,R) :- !. $keymerge(R,[],R) :- !. $keymerge(R1,R2,[X|R]) :- $comprises(R1,X1,R1a), $comprises(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) :- $integer(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).