home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / pd3.lzh / SBPROLOG2.2 / MODLIB / MODLIB_SRC / $setof.P < prev    next >
Text File  |  1991-08-10  |  8KB  |  244 lines

  1. /************************************************************************
  2. *                                    *
  3. *    The SB-Prolog System                        *
  4. *    Copyright SUNY at Stony Brook, 1986                *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25.  
  26. /* setof : 'setof', 'bagof' and sorting. */
  27. /* This was taken from the C-Prolog system, and modified to use $findall,
  28.    instead of `record' */
  29.  
  30. $setof_export([$setof/3,$bagof/3,$findall/3,$sort/2,$keysort/3]).
  31.  
  32. /* $setof_use($meta,[$functor/3,$univ/2,$length/2]).
  33.    $setof_use($buff,[$alloc_perm/2,$alloc_heap/2,$trimbuff/3,$buff_code/4,
  34.         $symtype/2,
  35.         $substring/6,$subnumber/6,$subdelim/6,$conlength/2,
  36.         $pred_undefined/1, $hashval/3]).
  37.    $setof_use($bmeta,[$atom/1,$atomic/1,$integer/1,$number/1,$structure/1,
  38.     $functor0/2,$bldstr/3,$arg/3,$arity/2,$real/1,$floor/2]).
  39. */
  40.  
  41. $setof(X,P,Set) :-
  42.    $bagof(X,P,Bag),
  43.    $sort(Bag,Set0),
  44.    Set=Set0.
  45.  
  46. $bagof(X,P,Bag) :-
  47.    $excess_vars(P,X,[],L), $nonempty(L), !,
  48.    $univ(Key,[$|L]),
  49.    $findall(Key-X,P,Bags0),
  50.    $keysort(Bags0,Bags),
  51.    $pick(Bags,Key,Bag).
  52. $bagof(X,P,Bag) :-
  53.    $findall(X,P,Bag).
  54.  
  55. $nonempty([_|_]).
  56.  
  57. $pick(Bags,Key,Bag) :-
  58.    $nonempty(Bags),
  59.    $parade(Bags,Key1,Bag1,Bags1),
  60.    $decide(Key1,Bag1,Bags1,Key,Bag).
  61.  
  62. $parade([Item|L1],K,[X|B],L) :- $item(Item,K,X), !,
  63.    $parade(L1,K,B,L).
  64. $parade(L,K,[],L).
  65.  
  66. $item(K-X,K,X).
  67.  
  68. $decide(Key,Bag,Bags,Key,Bag) :- (Bags=[], ! ; true).
  69. $decide(_,_,Bags,Key,Bag) :- $pick(Bags,Key,Bag).
  70.  
  71. $excess_vars(T,X,L0,L) :- var(T), !,
  72.    ( $no_occurrence(T,X), !, $introduce(T,L0,L)
  73.    ; L = L0 ).
  74. $excess_vars(X^P,Y,L0,L) :- !, $excess_vars(P,(X,Y),L0,L).
  75. $excess_vars(setof(X,P,S),Y,L0,L) :- !, $excess_vars((P,S),(X,Y),L0,L).
  76. $excess_vars(bagof(X,P,S),Y,L0,L) :- !, $excess_vars((P,S),(X,Y),L0,L).
  77. $excess_vars(T,X,L0,L) :- $functor(T,_,N), $rem_excess_vars(N,T,X,L0,L).
  78.  
  79. $rem_excess_vars(0,_,_,L,L) :- !.
  80. $rem_excess_vars(N,T,X,L0,L) :-
  81.    $arg(N,T,T1),
  82.    $excess_vars(T1,X,L0,L1),
  83.    N1 is N-1,
  84.    $rem_excess_vars(N1,T,X,L1,L).
  85.  
  86. $introduce(X,L,L) :- $included(X,L), !.
  87. $introduce(X,L,[X|L]).
  88.  
  89. $included(X,L) :- $doesnt_include(L,X), !, fail.
  90. $included(X,L).
  91.  
  92. $doesnt_include([],X).
  93. $doesnt_include([Y|L],X) :- Y \== X, $doesnt_include(L,X).
  94.  
  95. $no_occurrence(X,Term) :- $contains(Term,X), !, fail.
  96. $no_occurrence(X,Term).
  97.  
  98. $contains(T,X) :- var(T), !, T == X.
  99. $contains(T,X) :- $functor(T,_,N), $upto(N,I), $arg(I,T,T1), $contains(T1,X).
  100.  
  101. $upto(N,N) :- N > 0.
  102. $upto(N,I) :- N > 0, N1 is N-1, $upto(N1,I).
  103.  
  104. /*---------------------------------------------------------------------------- */
  105. /* Sorting by bisecting and merging. */
  106.  
  107. $sort(L,R) :- $length(L,N), $sort(N,L,_,R1), R=R1.
  108.  
  109. $sort(2,[X1|L1],L,R) :- !, $comprises(L1,X2,L),
  110.     compare(Delta,X1,X2),
  111.   (Delta = (<) , !, R = [X1,X2]
  112.    ; Delta = (>) , !, R = [X2,X1]
  113.    ; R = [X2]
  114.   ).
  115. $sort(1,[X|L],L,[X]) :- !.
  116. $sort(0,L,L,[]) :- !.
  117. $sort(N,L1,L3,R) :-
  118.    N1 is N/2, N2 is N-N1,
  119.    $sort(N1,L1,L2,R1),
  120.    $sort(N2,L2,L3,R2),
  121.    $merge(R1,R2,R).
  122.  
  123. $merge([],R,R) :- !.
  124. $merge(R,[],R) :- !.
  125. $merge(R1,R2,[X|R]) :-
  126.    $comprises(R1,X1,R1a), $comprises(R2,X2,R2a),
  127.    compare(Delta,X1,X2),
  128.   (Delta = (<) , !, X = X1, $merge(R1a,R2,R)
  129.    ; Delta = (>) , !, X = X2, $merge(R1,R2a,R)
  130.    ; X = X1, $merge(R1a,R2a,R)
  131.   ).
  132.  
  133. $comprises([X|L],X,L).
  134.  
  135. /*------------------------------------------------------------------------ */
  136. /* Sorting on keys by bisecting and merging. */
  137.  
  138. $keysort(L,R) :- $length(L,N), $keysort(N,L,_,R1), R=R1.
  139.  
  140. $keysort(2,[X1|L1],L,R) :- !,
  141.    $comprises(L1,X2,L),
  142.    $compare_keys(Delta,X1,X2),
  143.   (Delta = (>) , !, R = [X2,X1] ; R = [X1,X2] ).
  144. $keysort(1,[X|L],L,[X]) :- !.
  145. $keysort(0,L,L,[]) :- !.
  146. $keysort(N,L1,L3,R) :-
  147.    N1 is N/2, N2 is N-N1,
  148.    $keysort(N1,L1,L2,R1),
  149.    $keysort(N2,L2,L3,R2),
  150.    $keymerge(R1,R2,R).
  151.  
  152. $keymerge([],R,R) :- !.
  153. $keymerge(R,[],R) :- !.
  154. $keymerge(R1,R2,[X|R]) :-
  155.    $comprises(R1,X1,R1a), $comprises(R2,X2,R2a),
  156.    $compare_keys(Delta,X1,X2),
  157.   (Delta = (>) , !, X = X2, $keymerge(R1,R2a,R)
  158.    ; X = X1, $keymerge(R1a,R2,R)
  159.   ).
  160.  
  161. $compare_keys(Delta,K1-X1,K2-X2) :- compare(Delta,K1,K2).
  162.  
  163. /*======================================================================*/
  164.  
  165. X^P :- call(P).
  166.  
  167. /*======================================================================*/
  168.  
  169.  
  170. $findall(T,Call,Result) :-
  171.     $alloc_heap(5000,Buff),
  172.     $findall_1(T,Call,Result,Buff).
  173.  
  174. $findall_1(T,Call,Result,Buff) :-
  175.     $copyterm([],Buff,8,4,_), /* init result list to empty */
  176.     $buff_code(Buff,0,2 /*pn*/ ,8), /* init where to put next answer */
  177.     $buff_code(Buff,4,2 /*pn*/ ,12), /* init first free place */
  178.     call(Call),
  179.     $buff_code(Buff,0,5 /*gn*/ ,Place), /* get where to put answer */
  180.     $buff_code(Buff,4,5 /*gn*/ ,Start), /* get first free place */
  181.     $copyterm([T],Buff,Place,Start,End),
  182.     Tailloc is Start+4,    
  183.     $buff_code(Buff,0,2 /*pn*/ ,Tailloc), /* where to put next answer */
  184.     $buff_code(Buff,4,2 /*pn*/ ,End), /* next first free place */
  185.     fail.
  186.  
  187. $findall_1(_,_,Result,Buff) :-
  188.     $buff_code(Buff,4,5 /*gn*/ ,Length), /* Length =\= 12 fail if [] */
  189.     $trimbuff(Length,Buff,1),
  190.     $buff_code(Buff,8,18 /*vtb*/ ,Result).
  191.  
  192.  
  193. /* This routine copies a term into a buffer. It is passed:
  194.     Term: the term to copy,
  195.     Buffer: the buffer to copy it into,
  196.     Worddisp: the word of the buffer in which to put the copy (or
  197.         a pointer to the copy.)
  198.     Start: the disp of the next free location in the buffer, before the 
  199.         copy is done.
  200.     End: (returned) the location of the first free location after the
  201.         copying.
  202.  
  203.     Variables are copied into the buffer and the copied variables are
  204.     pointed into the buffer and trailed. Thus later binding of these 
  205.     `outside' variables will cause the copied variables to be changed,
  206.     too. If, however, the $copyterm call is failed over, the variables
  207.     in the buffer will be ``disconnected'' from the outer variables.
  208.  
  209.     Copyterm is a prime candidate for moving down into the simulator as
  210.     a builtin written in C.
  211. */
  212.  
  213.  
  214. $copyterm(Term,Buff,Worddisp,Start,Start) :-
  215.     var(Term),!,$buff_code(Buff,Worddisp,17 /*pvar*/ ,Term).
  216.  
  217. $copyterm(Term,Buff,Worddisp,Start,Start) :-
  218.     $integer(Term),!,$buff_code(Buff,Worddisp,14 /*ptv*/ ,Term).
  219.  
  220. $copyterm(Term,Buff,Worddisp,Start,Start) :-
  221.     $atom(Term),!,$buff_code(Buff,Worddisp,14 /*ptv*/ ,Term).
  222.  
  223. $copyterm(Term,Buff,Worddisp,Start,End) :-
  224.     Term=[_|_],!,
  225.     $buff_code(Buff,Worddisp,16 /*ptl*/ ,Start), /* ptr to list rec */
  226.     Newstart is Start+8, /* reserve rec space */
  227.     $copyargs(Term,1,2,Buff,Start,Newstart,End).
  228.     
  229. $copyterm(Term,Buff,Worddisp,Start,End) :-
  230.     $structure(Term),!,
  231.     $buff_code(Buff,Worddisp,15 /*ptp*/ ,Start), /* ptr to str rec */
  232.     $buff_code(Buff,Start,0 /*ppsc*/ ,Term), /* rec psc ptr */
  233.     Argsloc is Start+4,
  234.     $arity(Term,Arity),Newstart is Argsloc+4*Arity, /* reserve rec space*/
  235.     $copyargs(Term,1,Arity,Buff,Argsloc,Newstart,End).
  236.     
  237. $copyargs(Term,Argno,Maxargs,Buff,Argloc,Start,End) :- 
  238.     Argno > Maxargs,
  239.      Start=End;
  240.     Argno =< Maxargs,
  241.      $arg(Argno,Term,Arg),$copyterm(Arg,Buff,Argloc,Start,Mid),
  242.      Nargno is Argno+1, Nargloc is Argloc+4,
  243.      $copyargs(Term,Nargno,Maxargs,Buff,Nargloc,Mid,End).
  244.