% This LaTeX document was generated using the LaTeX backend of PlDoc,
% The SWI-Prolog documentation system



\section{library(solution_sequences): Modify solution sequences}

\label{sec:solutionsequences}

\begin{tags}
\mtag{See also}- all solution predicates \predref{findall}{3}, \predref{bagof}{3} and \predref{setof}{3}. \\- \verb$library(aggregate)$
\end{tags}

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
\href{http://swish.swi-prolog.org}{SWISH} Prolog browser-based shell, which
can represent the solutions to a predicate as a table. Notably wrapping
a goal in \predref{distinct}{1} avoids duplicates in the result table and using
\predref{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 non-deterministic predicates are
composed from simpler non-deterministic 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 (\verb$a(X)$,
\verb$b(X)$) and the ones using this library side-by-side.

\begin{description}
    \item[Avoid duplicates of earlier steps] 

\begin{code}
  setof(X, a(X), Xs),               distinct(a(X)),
  member(X, Xs),                    b(X)
  b(X).
\end{code}

Note that the \predref{distinct}{1} based solution returns the first result
of \verb$distinct(a(X))$ immediately after \predref{a}{1} produces a result, while
the \predref{setof}{3} based solution will first compute all results of \predref{a}{1}.
    \item[Only try \const{b(X)} only for the top-10 \const{a(X)}] 

\begin{code}
  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)
\end{code}

Here we see power of composing primitives from this library and
staying within the paradigm of pure non-deterministic relational
predicates.
\end{description}

\vspace{0.7cm}

\begin{description}
    \predicate{distinct}{1}{:Goal}
\nodescription
    \predicate{distinct}{2}{?Witness, :Goal}
True if \arg{Goal} is true and no previous solution of \arg{Goal} bound
\arg{Witness} to the same value. As previous answers need to be
copied, equivalence testing is based on \textit{term variance} (\predref{\Sstructeq}{2}).
The variant \predref{distinct}{1} is equivalent to \verb$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.

\begin{code}
distinct(Goal) :-
    findall(Goal, Goal, List),
    list_to_set(List, Set),
    member(Goal, Set).
\end{code}

    \predicate{reduced}{1}{:Goal}
\nodescription
    \predicate{reduced}{3}{?Witness, :Goal, +Options}
Similar to \predref{distinct}{1}, but does not guarantee unique results in
return for using a limited amount of memory. Both \predref{distinct}{1} and
\predref{reduced}{1} create a table that block duplicate results. For
\predref{distinct}{1}, this table may get arbitrary large. In contrast,
\predref{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. \arg{Options}:

\begin{description}
    \termitem{size_limit}{+Integer}
Max number of elements kept in the table. Default is 10,000.
\end{description}

    \predicate{limit}{2}{+Count, :Goal}
Limit the number of solutions. True if \arg{Goal} is true, returning
at most \arg{Count} solutions. Solutions are returned as soon as they
become available.

    \predicate{offset}{2}{+Count, :Goal}
Ignore the first \arg{Count} solutions. True if \arg{Goal} is true and
produces more than \arg{Count} solutions. This predicate computes and
ignores the first \arg{Count} solutions.

    \predicate{call_nth}{2}{:Goal, ?Nth}
True when \arg{Goal} succeeded for the \arg{Nth} time. If \arg{Nth} is bound on entry,
the predicate succeeds deterministically if there are at least \arg{Nth}
solutions for \arg{Goal}.

    \predicate{order_by}{2}{+Spec, :Goal}
Order solutions according to \arg{Spec}. \arg{Spec} is a list of terms,
where each element is one of. The ordering of solutions of \arg{Goal}
that only differ in variables that are \textit{not} shared with \arg{Spec} is
not changed.

\begin{description}
    \termitem{asc}{Term}
Order solution according to ascending \arg{Term}
    \termitem{desc}{Term}
Order solution according to descending \arg{Term}
\end{description}

    \predicate[nondet]{group_by}{4}{+By, +Template, :Goal, -Bag}
Group bindings of \arg{Template} that have the same value for \arg{By}. This
predicate is almost the same as \predref{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.
\end{description}

