1/*  Part of Ciao Prolog compatibility library
    2
    3    Author:        Jan Wielemaker, Edison Mera
    4    E-mail:        J.Wielemaker@uva.nl, efmera@gmail.com
    5    WWW:           http://www.edisonm.com
    6    Copyright (C): 2010, University of Amsterdam and
    7                   2013, Process Design Center, Breda, The Netherlands.
    8    
    9    This program is free software; you can redistribute it and/or
   10    modify it under the terms of the GNU General Public License
   11    as published by the Free Software Foundation; either version 2
   12    of the License, or (at your option) any later version.
   13
   14    This program is distributed in the hope that it will be useful,
   15    but WITHOUT ANY WARRANTY; without even the implied warranty of
   16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17    GNU General Public License for more details.
   18
   19    You should have received a copy of the GNU General Public
   20    License along with this library; if not, write to the Free Software
   21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   22
   23    As a special exception, if you link this library with other files,
   24    compiled with a Free Software compiler, to produce an executable, this
   25    library does not by itself cause the resulting executable to be covered
   26    by the GNU General Public License. This exception does not however
   27    invalidate any other reasons why the executable file might be covered by
   28    the GNU General Public License.
   29*/
   30
   31:- module(aggregates,
   32	  [ setof/3,
   33	    bagof/3,
   34	    findall/3,
   35	    findall/4,
   36	    findnsols/4,     % +N, ?Template, :Generator, -List
   37	    findnsols/5,     % +N, ?Template, :Generator, -List, -Tail
   38	    (^)/2
   39	  ]).   40
   41:- meta_predicate
   42	findnsols(+, ?, :, -),
   43	findnsols(+, ?, :, -, ?).
 findnsols(+N, ?Template, :Generator, -List)
As findall/3, but generating at most N solutions of Generator. Thus, the length of List will not be greater than N. If N=<0, returns directly an empty list. This predicate is especially useful if Generator may have an infinite number of solutions.
Compatibility
- ciao
   54findnsols(N, Template, Generator, List) :-
   55	findnsols(N, Template, Generator, List, []).
 findnsols(+N, ?Template, :Generator, -List, -Tail)
As findnsols/4, but returning in Tail the tail of List.
Compatibility
- ciao
   63findnsols(N, Template, Generator, List, Tail) :-
   64	N > 0, !,
   65	findall(Template, maxsols(N, Generator), List, Tail).
   66findnsols(_, _, _, Tail, Tail).
   67
   68maxsols(N, Generator) :-
   69	State = count(0),
   70	Generator,
   71	arg(1, State, C0),
   72	C1 is C0+1,
   73	(   C1 == N
   74	->  !
   75	;   nb_setarg(1, State, C1)
   76	).
   77
   78(_X^Y) :- call(Y)