Standard Prolog includes a few facilities that support working with bags and
sets, represented as lists - e.g. the set { x, y, z } is represented by the
list [x, y, z], the bag { x, y, x } is represented by the list [x, y, x], and
the empty set/bag is represented by []. Standard Prolog includes built-in
predicates bagof/3, findall/3, and
setof/3 for creating a bag or set of instances of a template
satisfying some goal. (See [ Deremsart, Ed-Dbali, and Cervoni, 1996 ] for
formal definitions of these predicates, which PrologJ supports as defined there
in addition to the additional predicates described below.)
The PrologJ set processing extension adds a number of built-in predicates for manipulating sets. (The predicates that accept sets as input(s) also accept bag as input(s).)
filter_set(SetOrBag, Template, Predicate, Result)SetOrBag is a set or bag represented as a (possibly empty)
listTemplate is a term that is unified with each successive
element of SetOrBag in turn.Predicate is called (as by once/1) once for
each successive element of SetOrBag in turn, provided
unification with Template is successful. (Typically,
Template and Predicate have one or more
variables in common.)Result is unified with a list consisting of each element
of SetOrBag that successfully unified with
Template and then satisfied Predicate.
(If there is no such element, Result is unified with
the empty list.) The elements in this list occur in the same
order in which they appeared in BagOrSet, and
duplicate elements are preserved. The predicate
filter_set/4 succeeds just when this unification is
successful. It is not redoable.
filter_set/4 should only be used with crisp sets.
Either fuzzy_filter_set/4 or fuzzy_filter_set/5
should be used with fuzzy sets.
filter_set([ color(apple, red), color(banana, yellow), color(tomato, red) ], color(Fruit, Color), Color = red, Result).unifies Result with
[ color(apple, red), color(tomato, red) ]
| Conditions | Error Term |
|---|---|
SetOrBag is a variable |
instantiation_error |
SetOrBag is neither a variable nor a proper list
nor the empty list |
type_error(list, SetOrBag) |
SetOrBag is a list that contains an element that
is a variable |
instantiation_error |
SetOrBag is a list that contains an element such
that, after it is unified with Template,
Predicate is a variable |
instantiation_error |
SetOrBag is a list that contains an element such
that, after it is unified with Template,
Predicate is neither a variable nor an atom nor a
compound term |
type_error(callable, Predicate) |
Result is neither a variable, nor a proper or
partial list, nor the empty list |
type_error(list, Result) |
findset(Term, Goal, Set)findall/3, except that the result
is guaranteed to be a set; if it is a bag, duplicates are eliminated to
make it a set. The result set is in the order determined by the
term_precedes relationship, as with the ISO Prolog predicate
setof/3. The error cases are the same as specified in the ISO
standard for findall/3.set_difference(SetOrBag1, SetOrBag2, Set12)SetOrBag1 and SetOrBag1 are sets or bags
represented as (possibly empty) lists. However, if either is a bag, it
is converted to a set by eliminating duplicate elements. Set12 is unified with a list that corresponds to the
difference of the sets represented by the first and second arguments.
An element appears in the result if it appears in SetOrBag1,
but not in SetOrBag2. An element appears in the result
just once, regardless of how many times it may appear in the
first input argument. The result is always in the
order determined by the term_precedes relationship, as with the
ISO Prolog predicate setof/3.
set_difference([ bananas, figs, apples ], [ apples, cumquats, figs ], Set12).
Unifies Set12 with
[ bananas ]
| Conditions | Error Term |
|---|---|
SetOrBag1 is a variable |
instantiation_error |
SetOrBag1 is neither a variable nor a proper list
nor the empty list |
type_error(list, SetOrBag1) |
SetOrBag1 is a list that contains an element that
is a variable |
instantiation_error |
SetOrBag2 is a variable |
instantiation_error |
SetOrBag2 is neither a variable nor a proper list
nor the empty list |
type_error(list, SetOrBag2) |
SetOrBag2 is a list that contains an element that
is a variable |
instantiation_error |
Set12 is neither a variable, nor a proper or
partial list, nor the empty list |
type_error(list, Result) |
set_intersection(SetOrBag1, SetOrBag2, Set12)SetOrBag1 and SetOrBag1 are sets or bags
represented as (possibly empty) lists. However, if either is a bag, it
is converted to a set by eliminating duplicate elements. Set12 is unified with a list that corresponds to the
intersection of the sets represented by the first and second arguments.
An element appears in the result if it appears in both of
SetOrBag1 and SetOrBag2. An
element appears in the result just once, regardless of how many times
it may appear in the input arguments. The result is always in the
order determined by the term_precedes relationship, as with the
ISO Prolog predicate setof/3.
set_intersection([ bananas, figs, apples ], [ apples, cumquats, figs ], Set12).
Unifies Set12 with
[ apples, figs ]
| Conditions | Error Term |
|---|---|
SetOrBag1 is a variable |
instantiation_error |
SetOrBag1 is neither a variable nor a proper list
nor the empty list |
type_error(list, SetOrBag1) |
SetOrBag1 is a list that contains an element that
is a variable |
instantiation_error |
SetOrBag2 is a variable |
instantiation_error |
SetOrBag2 is neither a variable nor a proper list
nor the empty list |
type_error(list, SetOrBag2) |
SetOrBag2 is a list that contains an element that
is a variable |
instantiation_error |
Set12 is neither a variable, nor a proper or
partial list, nor the empty list |
type_error(list, Result) |
set_union(SetOrBag1, SetOrBag2, Set12)SetOrBag1 and SetOrBag1 are sets or bags
represented as (possibly empty) lists. However, if either is a bag, it
is converted to a set by eliminating duplicate elements. Set12 is unified with a list
that corresponds to the union of the sets represented by the first
and second arguments. An element appears in the result if it appears
in either of SetOrBag1 or SetOrBag2. An
element appears in the result just once, regardless of how many times
it may appear in either of the input arguments, or if it appears in
both. The result is always in the order determined by the
term_precedes relationship, as with the ISO Prolog predicate
setof/3.
set_union([ bananas, figs, apples ], [ apples, cumquats, figs ], Set12).
Unifies Set12 with
[ apples, bananas, cumquats, figs ]
| Conditions | Error Term |
|---|---|
SetOrBag1 is a variable |
instantiation_error |
SetOrBag1 is neither a variable nor a proper list
nor the empty list |
type_error(list, SetOrBag1) |
SetOrBag1 is a list that contains an element that
is a variable |
instantiation_error |
SetOrBag2 is a variable |
instantiation_error |
SetOrBag2 is neither a variable nor a proper list
nor the empty list |
type_error(list, SetOrBag2) |
SetOrBag2 is a list that contains an element that
is a variable |
instantiation_error |
Set12 is neither a variable, nor a proper or
partial list, nor the empty list |
type_error(list, Result) |