# Set Operations

**Author(s):**Lena Flood.

This module implements set operations. Sets are just ordered lists.

## Usage and interface

**Library usage:**`:- use_module(library(sets)).`**Exports:****Imports:***System library modules:*`sort`.*Packages:*`prelude`,`nonpure`,`condcomp`,`assertions`,`basicmodes`.

## Documentation on exports

**Usage:**`insert(Set1,Element,Set2)`

It is true when Set2 is Set1 with Element inserted in it, preserving the order.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Set1 is currently a term which is not a free variable.

(term_typing:nonvar/1)Element is currently a term which is not a free variable.

(term_typing:var/1)Set2 is a free variable.

**Usage:**`ord_delete(Set0,X,Set)`

It succeeds if Set is Set0 without element X.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Set0 is currently a term which is not a free variable.

(term_typing:nonvar/1)X is currently a term which is not a free variable.

(term_typing:var/1)Set is a free variable.

**Usage:**`ord_member(X,Set)`

It succeeds if X is member of Set.

*The following properties should hold at call time:*

(term_typing:nonvar/1)X is currently a term which is not a free variable.

(term_typing:nonvar/1)Set is currently a term which is not a free variable.

**Usage:**`ord_test_member(Set,X,Result)`

If X is member of Set then Result=`yes`. Otherwise Result=`no`.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Set is currently a term which is not a free variable.

(term_typing:nonvar/1)X is currently a term which is not a free variable.

(term_typing:var/1)Result is a free variable.

**Usage:**`ord_subtract(Set1,Set2,Difference)`

It is true when Difference contains all and only the elements of Set1 which are not also in Set2.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Set1 is currently a term which is not a free variable.

(term_typing:nonvar/1)Set2 is currently a term which is not a free variable.

**Usage:**`ord_intersection(Set1,Set2,Intersection)`

It is true when Intersection is the ordered representation of Set1 and Set2, provided that Set1 and Set2 are ordered lists.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Set1 is currently a term which is not a free variable.

(term_typing:nonvar/1)Set2 is currently a term which is not a free variable.

**Usage:**`ord_intersection_diff(Set1,Set2,Intersect,NotIntersect)`

Intersect contains those elements which are both in Set1 and Set2, and NotIntersect those which are in Set1 but not in Set2.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Set1 is currently a term which is not a free variable.

(term_typing:nonvar/1)Set2 is currently a term which is not a free variable.

(term_typing:var/1)Intersect is a free variable.

(term_typing:var/1)NotIntersect is a free variable.

**Usage:**`ord_intersect(Xs,Ys)`

Succeeds when the two ordered lists have at least one element in common.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Xs is currently a term which is not a free variable.

(term_typing:nonvar/1)Ys is currently a term which is not a free variable.

**Usage:**`ord_subset(Xs,Ys)`

Succeeds when every element of Xs appears in Ys.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Xs is currently a term which is not a free variable.

(term_typing:nonvar/1)Ys is currently a term which is not a free variable.

**Usage:**`ord_subset_diff(Set1,Set2,Difference)`

It succeeds when every element of Set1 appears in Set2 and Difference has the elements of Set2 which are not in Set1.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Set1 is currently a term which is not a free variable.

(term_typing:nonvar/1)Set2 is currently a term which is not a free variable.

(term_typing:var/1)Difference is a free variable.

**Usage:**`ord_union(Set1,Set2,Union)`

It is true when Union is the union of Set1 and Set2. When some element occurs in both sets, Union retains only one copy.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Set1 is currently a term which is not a free variable.

(term_typing:nonvar/1)Set2 is currently a term which is not a free variable.

**Usage:**`ord_union_diff(Set1,Set2,Union,Difference)`

It succeeds when Union is the union of Set1 and Set2, and Difference is Set2 set-minus Set1.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Set1 is currently a term which is not a free variable.

(term_typing:nonvar/1)Set2 is currently a term which is not a free variable.

(term_typing:var/1)Union is a free variable.

(term_typing:var/1)Difference is a free variable.

**Usage:**`ord_union_symdiff(Set1,Set2,Union,Diff)`

It is true when Diff is the symmetric difference of Set1 and Set2, and Union is the union of Set1 and Set2.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Set1 is currently a term which is not a free variable.

(term_typing:nonvar/1)Set2 is currently a term which is not a free variable.

(term_typing:var/1)Union is a free variable.

(term_typing:var/1)Diff is a free variable.

**Usage:**`ord_union_change(Set1,Set2,Union)`

Union is the union of Set1 and Set2 and Union is different from Set2.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Set1 is currently a term which is not a free variable.

(term_typing:nonvar/1)Set2 is currently a term which is not a free variable.

(term_typing:var/1)Union is a free variable.

**Usage:**`merge(Set1,Set2,Union)`

See `ord_union/3`.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Set1 is currently a term which is not a free variable.

(term_typing:nonvar/1)Set2 is currently a term which is not a free variable.

**Usage:**`ord_disjoint(Set1,Set2)`

Set1 and Set2 have no element in common.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Set1 is currently a term which is not a free variable.

(term_typing:nonvar/1)Set2 is currently a term which is not a free variable.

**Usage:**`setproduct(Set1,Set2,Product)`

Product has all two element sets such that one element is in Set1 and the other in set2, except that if the same element belongs to both, then the corresponding one element set is in Product.

*The following properties should hold at call time:*

(term_typing:nonvar/1)Set1 is currently a term which is not a free variable.

(term_typing:nonvar/1)Set2 is currently a term which is not a free variable.

(term_typing:var/1)Product is a free variable.