Did you know ... | Search Documentation: |
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 browser-based 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 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 (a(X)
,
b(X)
) and the ones using this library side-by-side.
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.
b(X)
only for the top-10 a(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 non-deterministic relational predicates.
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).
Count | is either infinite ,
making this predicate equivalent to
call/1 or an integer. If Count <
1 this predicate fails immediately. |
This predicate is based on findall/3 and (thus) variables in answers are copied.