 Documentation
 Reference manual
 The SWIProlog library
 library(aggregate): Aggregation operators on backtrackable predicates
 library(ansi_term): Print decorated text to ANSI consoles
 library(apply): Apply predicates on a list
 library(assoc): Association lists
 library(broadcast): Broadcast and receive event notifications
 library(charsio): I/O on Lists of Character Codes
 library(check): Consistency checking
 library(clpb): CLP(B): Constraint Logic Programming over Boolean Variables
 library(clpfd): CLP(FD): Constraint Logic Programming over Finite Domains
 library(clpqr): Constraint Logic Programming over Rationals and Reals
 library(csv): Process CSV (CommaSeparated Values) data
 library(dcg/basics): Various general DCG utilities
 library(dcg/high_order): High order grammar operations
 library(debug): Print debug messages and test assertions
 library(dicts): Dict utilities
 library(error): Error generating support
 library(gensym): Generate unique identifiers
 library(intercept): Intercept and signal interface
 library(iostream): Utilities to deal with streams
 library(lists): List Manipulation
 library(main): Provide entry point for scripts
 library(nb_set): Nonbacktrackable set
 library(www_browser): Activating your Webbrowser
 library(occurs): Finding and counting subterms
 library(option): Option list processing
 library(optparse): command line parsing
 library(ordsets): Ordered set manipulation
 library(pairs): Operations on keyvalue lists
 library(persistency): Provide persistent dynamic predicates
 library(pio): Pure I/O
 library(predicate_options): Declare optionprocessing of predicates
 library(prolog_pack): A package manager for Prolog
 library(prolog_xref): Prolog crossreferencer data collection
 library(quasi_quotations): Define Quasi Quotation syntax
 library(random): Random numbers
 library(readutil): Read utilities
 library(record): Access named fields in a term
 library(registry): Manipulating the Windows registry
 library(settings): Setting management
 library(simplex): Solve linear programming problems
 library(solution_sequences): Modify solution sequences
 library(thread_pool): Resource bounded thread management
 library(ugraphs): Unweighted Graphs
 library(url): Analysing and constructing URL
 library(varnumbers): Utilities for numbered terms
 library(yall): Lambda expressions
 The SWIProlog library
 Packages
 Reference manual
A.42 library(solution_sequences): Modify solution sequences
The meta predicates of this library modify the sequence of solutions of a goal. The modifications and the predicate names are based on the classical database operations DISTINCT, LIMIT, OFFSET, ORDER BY and GROUP BY.
These predicates were introduced in the context of the SWISH Prolog browserbased shell, which can represent the solutions to a predicate as a table. Notably wrapping a goal in distinct/1 avoids duplicates in the result table and using order_by/2 produces a nicely ordered table.
However, the predicates from this library can also be used to stay
longer within the clean paradigm where nondeterministic predicates are
composed from simpler nondeterministic predicates by means of
conjunction and disjunction. While evaluating a conjunction, we might
want to eliminate duplicates of the first part of the conjunction. Below
we give both the classical solution for solving variations of (a(X)
,
b(X)
) and the ones using this library sidebyside.
 Avoid duplicates of earlier steps

setof(X, a(X), Xs), distinct(a(X)), member(X, Xs), b(X) b(X).
Note that the distinct/1 based solution returns the first result of
distinct(a(X))
immediately after a/1 produces a result, while the setof/3 based solution will first compute all results of a/1.  Only try
b(X)
only for the top10a(X)

setof(X, a(X), Xs), limit(10, order_by([desc(X)], a(X))), reverse(Xs, Desc), b(X) first_max_n(10, Desc, Limit), member(X, Limit), b(X)
Here we see power of composing primitives from this library and staying within the paradigm of pure nondeterministic relational predicates.
 distinct(:Goal)
 distinct(?Witness, :Goal)
 True if Goal is true and no previous solution of Goal
bound
Witness to the same value. As previous answers need to be
copied, equivalence testing is based on term variance (=@=/2).
The variant distinct/1
is equivalent to
distinct(Goal,Goal)
.If the answers are ground terms, the predicate behaves as the code below, but answers are returned as soon as they become available rather than first computing the complete answer set.
distinct(Goal) : findall(Goal, Goal, List), list_to_set(List, Set), member(Goal, Set).
 reduced(:Goal)
 reduced(?Witness, :Goal, +Options)
 Similar to distinct/1,
but does not guarantee unique results in return for using a limited
amount of memory. Both distinct/1
and
reduced/1
create a table that block duplicate results. For
distinct/1,
this table may get arbitrary large. In contrast,
reduced/1
discards the table and starts a new one of the table size exceeds a
specified limit. This filter is useful for reducing the number of
answers when processing large or infinite long tail distributions. Options:
 size_limit(+Integer)
 Max number of elements kept in the table. Default is 10,000.
 limit(+Count, :Goal)
 Limit the number of solutions. True if Goal is true,
returning at most Count solutions. Solutions are returned as
soon as they become available.
Count is either infinite
, making this predicate equivalent to call/1 or an integer. If Count < 1 this predicate fails immediately.  offset(+Count, :Goal)
 Ignore the first Count solutions. True if Goal is true and produces more than Count solutions. This predicate computes and ignores the first Count solutions.
 call_nth(:Goal, ?Nth)
 True when Goal succeeded for the Nth time. If Nth is bound on entry, the predicate succeeds deterministically if there are at least Nth solutions for Goal.
 order_by(+Spec, :Goal)
 Order solutions according to Spec. Spec is a list
of terms, where each element is one of. The ordering of solutions of Goal
that only differ in variables that are not shared with Spec
is not changed.
 asc(Term)
 Order solution according to ascending Term
 desc(Term)
 Order solution according to descending Term
 [nondet]group_by(+By, +Template, :Goal, Bag)
 Group bindings of Template that have the same value for By. This predicate is almost the same as bagof/3, but instead of specifying the existential variables we specify the free variables. It is provided for consistency and complete coverage of the common database vocabulary.