2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog %W% %G%
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: setof.pl *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: set predicates *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
/**
|
|
|
|
* @file setof.yap
|
|
|
|
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
|
|
|
|
* @date Thu Nov 19 10:45:32 2015
|
|
|
|
*
|
|
|
|
* @brief Setof and friends.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-20 22:36:16 +00:00
|
|
|
|
|
|
|
:- system_module( '$_setof', [(^)/2,
|
|
|
|
all/3,
|
|
|
|
bagof/3,
|
|
|
|
findall/3,
|
|
|
|
findall/4,
|
|
|
|
setof/3], []).
|
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
/**
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
@defgroup Sets Collecting Solutions to a Goal
|
|
|
|
@ingroup builtins
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
When there are several solutions to a goal, if the user wants to collect all
|
|
|
|
the solutions he may be led to use the data base, because backtracking will
|
|
|
|
forget previous solutions.
|
|
|
|
|
|
|
|
YAP allows the programmer to choose from several system
|
|
|
|
predicates instead of writing his own routines. findall/3 gives you
|
|
|
|
the fastest, but crudest solution. The other built-in predicates
|
|
|
|
post-process the result of the query in several different ways:
|
|
|
|
|
2016-01-20 22:36:16 +00:00
|
|
|
@{
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
*/
|
|
|
|
|
2014-04-09 12:39:29 +01:00
|
|
|
:- use_system_module( '$_boot', ['$catch'/3]).
|
|
|
|
|
|
|
|
:- use_system_module( '$_errors', ['$do_error'/2]).
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
% this is used by the all predicate
|
|
|
|
|
|
|
|
:- op(50,xfx,same).
|
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
|
2016-01-20 22:36:16 +00:00
|
|
|
%% @pred ^/2
|
|
|
|
%
|
|
|
|
% The "existential quantifier" symbol is only significant to bagof
|
|
|
|
% and setof, which it stops binding the quantified variable.
|
|
|
|
% op(200, xfy, ^) is defined during bootstrap.
|
|
|
|
|
2001-10-30 16:42:05 +00:00
|
|
|
_^Goal :-
|
2001-04-09 20:54:03 +01:00
|
|
|
'$execute'(Goal).
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
/** @pred findall( _T_,+ _G_,- _L_) is iso
|
2014-09-15 19:10:49 +01:00
|
|
|
|
2016-01-20 22:36:16 +00:00
|
|
|
findall/3 is a simplified version of bagof which has an implicit
|
|
|
|
existential quantifier on every variable.
|
2014-09-15 19:10:49 +01:00
|
|
|
|
|
|
|
Unifies _L_ with a list that contains all the instantiations of the
|
|
|
|
term _T_ satisfying the goal _G_.
|
|
|
|
|
|
|
|
With the following program:
|
|
|
|
|
|
|
|
~~~~~
|
|
|
|
a(2,1).
|
|
|
|
a(1,1).
|
|
|
|
a(2,2).
|
|
|
|
~~~~~
|
|
|
|
the answer to the query
|
|
|
|
|
|
|
|
~~~~~
|
|
|
|
findall(X,a(X,Y),L).
|
|
|
|
~~~~~
|
|
|
|
would be:
|
|
|
|
|
|
|
|
~~~~~
|
|
|
|
X = _32
|
|
|
|
Y = _33
|
|
|
|
L = [2,1,2];
|
|
|
|
no
|
|
|
|
~~~~~
|
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
|
2014-09-15 19:10:49 +01:00
|
|
|
*/
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
findall(Template, Generator, Answers) :-
|
2016-05-19 08:47:40 +01:00
|
|
|
must_be_of_type( list_or_partial_list, Answers ),
|
|
|
|
'$findall'(Template, Generator, [], Answers).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
% If some answers have already been found
|
2016-05-19 08:47:40 +01:00
|
|
|
/** @pred findall( ?Key, +Goal, +InitialSolutions, -Solutions )
|
2014-09-15 19:10:49 +01:00
|
|
|
|
|
|
|
Similar to findall/3, but appends all answers to list _L0_.
|
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
|
2014-09-15 19:10:49 +01:00
|
|
|
*/
|
2001-04-09 20:54:03 +01:00
|
|
|
findall(Template, Generator, Answers, SoFar) :-
|
2016-05-19 08:47:40 +01:00
|
|
|
must_be_of_type( list_or_partial_list, Answers ),
|
|
|
|
'$findall'(Template, Generator, SoFar, Answers).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
% starts by calling the generator,
|
|
|
|
% and recording the answers
|
2006-08-23 13:12:14 +01:00
|
|
|
'$findall'(Template, Generator, SoFar, Answers) :-
|
2006-09-28 18:24:40 +01:00
|
|
|
nb:nb_queue(Ref),
|
2006-08-23 13:12:14 +01:00
|
|
|
(
|
|
|
|
'$execute'(Generator),
|
2006-09-28 18:24:40 +01:00
|
|
|
nb:nb_queue_enqueue(Ref, Template),
|
2013-02-15 16:31:49 +00:00
|
|
|
fail
|
2006-08-23 13:12:14 +01:00
|
|
|
;
|
2013-02-15 16:31:49 +00:00
|
|
|
nb:nb_queue_close(Ref, Answers, SoFar)
|
2006-08-23 13:12:14 +01:00
|
|
|
).
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
% findall_with_key is very similar to findall, but uses the SICStus
|
|
|
|
% algorithm to guarantee that variables will have the same names.
|
|
|
|
%
|
2006-08-23 13:12:14 +01:00
|
|
|
'$findall_with_common_vars'(Template, Generator, Answers) :-
|
2006-09-28 18:24:40 +01:00
|
|
|
nb:nb_queue(Ref),
|
2006-08-23 13:12:14 +01:00
|
|
|
(
|
|
|
|
'$execute'(Generator),
|
2006-09-28 18:24:40 +01:00
|
|
|
nb:nb_queue_enqueue(Ref, Template),
|
2006-08-23 13:12:14 +01:00
|
|
|
fail
|
|
|
|
;
|
2006-09-28 18:24:40 +01:00
|
|
|
nb:nb_queue_close(Ref, Answers, []),
|
2006-08-23 13:12:14 +01:00
|
|
|
'$collect_with_common_vars'(Answers, _)
|
|
|
|
).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
|
2006-08-22 17:12:46 +01:00
|
|
|
'$collect_with_common_vars'([], _).
|
|
|
|
'$collect_with_common_vars'([Key-_|Answers], VarList) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
'$variables_in_term'(Key, _, VarList),
|
2006-08-22 17:12:46 +01:00
|
|
|
'$collect_with_common_vars'(Answers, VarList).
|
2015-04-13 13:28:17 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
% This is the setof predicate
|
2015-04-13 13:28:17 +01:00
|
|
|
/** @pred setof( _X_,+ _P_,- _B_) is iso
|
2014-09-15 19:10:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
Similar to `bagof( _T_, _G_, _L_)` but sorts list
|
|
|
|
_L_ and keeping only one copy of each element. Again, assuming the
|
|
|
|
same clauses as in the examples above, the reply to the query
|
|
|
|
|
|
|
|
~~~~~
|
|
|
|
setof(X,a(X,Y),L).
|
|
|
|
~~~~~
|
|
|
|
would be:
|
|
|
|
|
|
|
|
~~~~~
|
|
|
|
X = _32
|
|
|
|
Y = 1
|
|
|
|
L = [1,2];
|
|
|
|
X = _32
|
|
|
|
Y = 2
|
|
|
|
L = [2];
|
|
|
|
no
|
|
|
|
~~~~~
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2014-09-15 19:10:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2001-04-09 20:54:03 +01:00
|
|
|
setof(Template, Generator, Set) :-
|
2015-12-15 09:28:43 +00:00
|
|
|
|
2012-03-06 14:41:23 +00:00
|
|
|
( '$is_list_or_partial_list'(Set) ->
|
2010-11-21 21:49:13 +00:00
|
|
|
true
|
|
|
|
;
|
|
|
|
'$do_error'(type_error(list,Set), setof(Template, Generator, Set))
|
|
|
|
),
|
2001-12-28 16:11:09 +00:00
|
|
|
'$bagof'(Template, Generator, Bag),
|
2001-04-09 20:54:03 +01:00
|
|
|
'$sort'(Bag, Set).
|
|
|
|
|
|
|
|
% And this is bagof
|
|
|
|
|
|
|
|
% Either we have excess of variables
|
2002-01-09 17:19:36 +00:00
|
|
|
% and we need to find the solutions for each instantiation
|
2001-04-09 20:54:03 +01:00
|
|
|
% of these variables
|
2001-12-28 16:11:09 +00:00
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
/** @pred bagof( _T_,+ _G_,- _L_) is iso
|
2014-09-15 19:10:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
For each set of possible instances of the free variables occurring in
|
|
|
|
_G_ but not in _T_, generates the list _L_ of the instances of
|
|
|
|
_T_ satisfying _G_. Again, assuming the same clauses as in the
|
|
|
|
examples above, the reply to the query
|
|
|
|
|
|
|
|
~~~~~
|
|
|
|
bagof(X,a(X,Y),L).
|
|
|
|
|
|
|
|
would be:
|
|
|
|
X = _32
|
|
|
|
Y = 1
|
|
|
|
L = [2,1];
|
|
|
|
X = _32
|
|
|
|
Y = 2
|
|
|
|
L = [2];
|
|
|
|
no
|
|
|
|
~~~~~
|
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
|
2014-09-15 19:10:49 +01:00
|
|
|
*/
|
2015-12-15 09:28:43 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
bagof(Template, Generator, Bag) :-
|
2012-03-06 14:41:23 +00:00
|
|
|
( '$is_list_or_partial_list'(Bag) ->
|
2010-11-21 21:49:13 +00:00
|
|
|
true
|
|
|
|
;
|
|
|
|
'$do_error'(type_error(list,Bag), bagof(Template, Generator, Bag))
|
|
|
|
),
|
2001-12-28 16:11:09 +00:00
|
|
|
'$bagof'(Template, Generator, Bag).
|
2010-11-21 21:49:13 +00:00
|
|
|
|
2001-12-28 16:11:09 +00:00
|
|
|
'$bagof'(Template, Generator, Bag) :-
|
2013-01-15 11:17:56 +00:00
|
|
|
'$free_variables_in_term'(Template^Generator, StrippedGenerator, Key),
|
|
|
|
%format('TemplateV=~w v=~w ~w~n',[TemplateV,Key, StrippedGenerator]),
|
|
|
|
( Key \== '$' ->
|
2006-08-23 13:12:14 +01:00
|
|
|
'$findall_with_common_vars'(Key-Template, StrippedGenerator, Bags0),
|
2005-11-26 02:57:25 +00:00
|
|
|
'$keysort'(Bags0, Bags),
|
|
|
|
'$pick'(Bags, Key, Bag)
|
|
|
|
;
|
2006-08-23 13:12:14 +01:00
|
|
|
'$findall'(Template, StrippedGenerator, [], Bag0),
|
2005-11-26 02:57:25 +00:00
|
|
|
Bag0 \== [],
|
|
|
|
Bag = Bag0
|
|
|
|
).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
% picks a solution attending to the free variables
|
|
|
|
'$pick'([K-X|Bags], Key, Bag) :-
|
|
|
|
'$parade'(Bags, K, Bag1, Bags1),
|
|
|
|
'$decide'(Bags1, [X|Bag1], K, Key, Bag).
|
|
|
|
|
|
|
|
'$parade'([K-X|L1], Key, [X|B], L) :- K == Key, !,
|
|
|
|
'$parade'(L1, Key, B, L).
|
|
|
|
'$parade'(L, _, [], L).
|
|
|
|
|
|
|
|
%
|
|
|
|
% The first argument to decide gives if solutions still left;
|
|
|
|
% The second gives the solution currently found;
|
|
|
|
% The third gives the free variables that are supposed to be bound;
|
|
|
|
% The fourth gives the free variables being currently used.
|
|
|
|
% The fifth outputs the current solution.
|
|
|
|
%
|
2010-03-15 14:18:25 +00:00
|
|
|
'$decide'([], Bag, Key0, Key, Bag) :- !,
|
|
|
|
Key0=Key.
|
2001-04-09 20:54:03 +01:00
|
|
|
'$decide'(_, Bag, Key, Key, Bag).
|
|
|
|
'$decide'(Bags, _, _, Key, Bag) :-
|
|
|
|
'$pick'(Bags, Key, Bag).
|
|
|
|
|
|
|
|
% as an alternative to setof you can use the predicate all(Term,Goal,Solutions)
|
|
|
|
% But this version of all does not allow for repeated answers
|
2015-04-13 13:28:17 +01:00
|
|
|
% if you want them use findall
|
|
|
|
/** @pred all( _T_,+ _G_,- _L_)
|
2014-09-15 19:10:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
Similar to `findall( _T_, _G_, _L_)` but eliminate
|
|
|
|
repeated elements. Thus, assuming the same clauses as in the above
|
|
|
|
example, the reply to the query
|
|
|
|
|
|
|
|
~~~~~
|
|
|
|
all(X,a(X,Y),L).
|
|
|
|
~~~~~
|
|
|
|
would be:
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2014-09-15 19:10:49 +01:00
|
|
|
~~~~~
|
|
|
|
X = _32
|
|
|
|
Y = _33
|
|
|
|
L = [2,1];
|
|
|
|
no
|
|
|
|
~~~~~
|
|
|
|
|
|
|
|
Note that all/3 will fail if no answers are found.
|
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
|
2014-09-15 19:10:49 +01:00
|
|
|
*/
|
2014-02-09 10:47:44 +00:00
|
|
|
all(T, G same X,S) :- !, all(T same X,G,Sx), '$$produce'(Sx,S,X).
|
2015-04-13 13:28:17 +01:00
|
|
|
all(T,G,S) :-
|
2004-04-22 04:23:12 +01:00
|
|
|
'$init_db_queue'(Ref),
|
2016-04-14 12:00:09 +01:00
|
|
|
( catch(G, Error,'$clean_findall'(Ref,Error) ),
|
2004-04-22 04:23:12 +01:00
|
|
|
'$execute'(G),
|
|
|
|
'$db_enqueue'(Ref, T),
|
|
|
|
fail
|
|
|
|
;
|
|
|
|
'$$set'(S,Ref)
|
|
|
|
).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
% $$set does its best to preserve space
|
2015-04-13 13:28:17 +01:00
|
|
|
'$$set'(S,R) :-
|
2004-05-13 21:54:58 +01:00
|
|
|
'$$build'(S0,_,R),
|
2007-01-25 22:11:55 +00:00
|
|
|
S0 = [_|_],
|
2004-04-22 21:06:24 +01:00
|
|
|
S = S0.
|
2004-04-22 04:23:12 +01:00
|
|
|
|
2004-04-27 16:03:06 +01:00
|
|
|
'$$build'(Ns,S0,R) :- '$db_dequeue'(R,X), !,
|
|
|
|
'$$build2'(Ns,S0,R,X).
|
|
|
|
'$$build'([],_,_).
|
2004-04-22 04:23:12 +01:00
|
|
|
|
2004-04-27 16:03:06 +01:00
|
|
|
'$$build2'([X|Ns],Hash,R,X) :-
|
2004-05-13 21:54:58 +01:00
|
|
|
'$$new'(Hash,X), !,
|
|
|
|
'$$build'(Ns,Hash,R).
|
2006-03-24 16:26:31 +00:00
|
|
|
'$$build2'(Ns,Hash,R,_) :-
|
2004-04-27 16:03:06 +01:00
|
|
|
'$$build'(Ns,Hash,R).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2004-05-13 21:54:58 +01:00
|
|
|
'$$new'(V,El) :- var(V), !, V = n(_,El,_).
|
2015-04-13 13:28:17 +01:00
|
|
|
'$$new'(n(R,El0,L),El) :-
|
2004-05-13 21:54:58 +01:00
|
|
|
compare(C,El0,El),
|
|
|
|
'$$new'(C,R,L,El).
|
|
|
|
|
|
|
|
'$$new'(=,_,_,_) :- !, fail.
|
|
|
|
'$$new'(<,R,_,El) :- '$$new'(R,El).
|
|
|
|
'$$new'(>,_,L,El) :- '$$new'(L,El).
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
'$$produce'([T1 same X1|Tn],S,X) :- '$$split'(Tn,T1,X1,S1,S2),
|
|
|
|
( S=[T1|S1], X=X1;
|
|
|
|
!, produce(S2,S,X) ).
|
|
|
|
|
|
|
|
'$$split'([],_,_,[],[]).
|
|
|
|
'$$split'([T same X|Tn],T,X,S1,S2) :- '$$split'(Tn,T,X,S1,S2).
|
|
|
|
'$$split'([T1 same X|Tn],T,X,[T1|S1],S2) :- '$$split'(Tn,T,X,S1,S2).
|
|
|
|
'$$split'([T1|Tn],T,X,S1,[T1|S2]) :- '$$split'(Tn,T,X,S1,S2).
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/**
|
|
|
|
@}
|
|
|
|
*/
|