.TH sofs 3erl "stdlib 5.2" "Ericsson AB" "Erlang Module Definition" .SH NAME sofs \- Functions for manipulating sets of sets. .SH DESCRIPTION .LP This module provides operations on finite sets and relations represented as sets\&. Intuitively, a set is a collection of elements; every element belongs to the set, and the set contains every element\&. .LP The data representing \fIsofs\fR\& as used by this module is to be regarded as opaque by other modules\&. In abstract terms, the representation is a composite type of existing Erlang terms\&. See note on data types\&. Any code assuming knowledge of the format is running on thin ice\&. .LP Given a set A and a sentence S(x), where x is a free variable, a new set B whose elements are exactly those elements of A for which S(x) holds can be formed, this is denoted B = {x in A : S(x)}\&. Sentences are expressed using the logical operators "for some" (or "there exists"), "for all", "and", "or", "not"\&. If the existence of a set containing all the specified elements is known (as is always the case in this module), this is denoted B = {x : S(x)}\&. .RS 2 .TP 2 * The \fIunordered set\fR\& containing the elements a, b, and c is denoted {a, b, c}\&. This notation is not to be confused with tuples\&. .RS 2 .LP The \fIordered pair\fR\& of a and b, with first \fIcoordinate\fR\& a and second coordinate b, is denoted (a, b)\&. An ordered pair is an \fIordered set\fR\& of two elements\&. In this module, ordered sets can contain one, two, or more elements, and parentheses are used to enclose the elements\&. .RE .RS 2 .LP Unordered sets and ordered sets are orthogonal, again in this module; there is no unordered set equal to any ordered set\&. .RE .LP .TP 2 * The \fIempty set\fR\& contains no elements\&. .RS 2 .LP Set A is \fIequal\fR\& to set B if they contain the same elements, which is denoted A = B\&. Two ordered sets are equal if they contain the same number of elements and have equal elements at each coordinate\&. .RE .RS 2 .LP Set B is a \fIsubset\fR\& of set A if A contains all elements that B contains\&. .RE .RS 2 .LP The \fIunion\fR\& of two sets A and B is the smallest set that contains all elements of A and all elements of B\&. .RE .RS 2 .LP The \fIintersection\fR\& of two sets A and B is the set that contains all elements of A that belong to B\&. .RE .RS 2 .LP Two sets are \fIdisjoint\fR\& if their intersection is the empty set\&. .RE .RS 2 .LP The \fIdifference\fR\& of two sets A and B is the set that contains all elements of A that do not belong to B\&. .RE .RS 2 .LP The \fIsymmetric difference\fR\& of two sets is the set that contains those element that belong to either of the two sets, but not both\&. .RE .RS 2 .LP The \fIunion\fR\& of a collection of sets is the smallest set that contains all the elements that belong to at least one set of the collection\&. .RE .RS 2 .LP The \fIintersection\fR\& of a non-empty collection of sets is the set that contains all elements that belong to every set of the collection\&. .RE .LP .TP 2 * The \fICartesian product\fR\& of two sets X and Y, denoted X x Y, is the set {a : a = (x, y) for some x in X and for some y in Y}\&. .RS 2 .LP A \fIrelation\fR\& is a subset of X x Y\&. Let R be a relation\&. The fact that (x, y) belongs to R is written as x R y\&. As relations are sets, the definitions of the last item (subset, union, and so on) apply to relations as well\&. .RE .RS 2 .LP The \fIdomain\fR\& of R is the set {x : x R y for some y in Y}\&. .RE .RS 2 .LP The \fIrange\fR\& of R is the set {y : x R y for some x in X}\&. .RE .RS 2 .LP The \fIconverse\fR\& of R is the set {a : a = (y, x) for some (x, y) in R}\&. .RE .RS 2 .LP If A is a subset of X, the \fIimage\fR\& of A under R is the set {y : x R y for some x in A}\&. If B is a subset of Y, the \fIinverse image\fR\& of B is the set {x : x R y for some y in B}\&. .RE .RS 2 .LP If R is a relation from X to Y, and S is a relation from Y to Z, the \fIrelative product\fR\& of R and S is the relation T from X to Z defined so that x T z if and only if there exists an element y in Y such that x R y and y S z\&. .RE .RS 2 .LP The \fIrestriction\fR\& of R to A is the set S defined so that x S y if and only if there exists an element x in A such that x R y\&. .RE .RS 2 .LP If S is a restriction of R to A, then R is an \fIextension\fR\& of S to X\&. .RE .RS 2 .LP If X = Y, then R is called a relation \fIin\fR\& X\&. .RE .RS 2 .LP The \fIfield\fR\& of a relation R in X is the union of the domain of R and the range of R\&. .RE .RS 2 .LP If R is a relation in X, and if S is defined so that x S y if x R y and not x = y, then S is the \fIstrict\fR\& relation corresponding to R\&. Conversely, if S is a relation in X, and if R is defined so that x R y if x S y or x = y, then R is the \fIweak\fR\& relation corresponding to S\&. .RE .RS 2 .LP A relation R in X is \fIreflexive\fR\& if x R x for every element x of X, it is \fIsymmetric\fR\& if x R y implies that y R x, and it is \fItransitive\fR\& if x R y and y R z imply that x R z\&. .RE .LP .TP 2 * A \fIfunction\fR\& F is a relation, a subset of X x Y, such that the domain of F is equal to X and such that for every x in X there is a unique element y in Y with (x, y) in F\&. The latter condition can be formulated as follows: if x F y and x F z, then y = z\&. In this module, it is not required that the domain of F is equal to X for a relation to be considered a function\&. .RS 2 .LP Instead of writing (x, y) in F or x F y, we write F(x) = y when F is a function, and say that F maps x onto y, or that the value of F at x is y\&. .RE .RS 2 .LP As functions are relations, the definitions of the last item (domain, range, and so on) apply to functions as well\&. .RE .RS 2 .LP If the converse of a function F is a function F\&', then F\&' is called the \fIinverse\fR\& of F\&. .RE .RS 2 .LP The relative product of two functions F1 and F2 is called the \fIcomposite\fR\& of F1 and F2 if the range of F1 is a subset of the domain of F2\&. .RE .LP .TP 2 * Sometimes, when the range of a function is more important than the function itself, the function is called a \fIfamily\fR\&\&. .RS 2 .LP The domain of a family is called the \fIindex set\fR\&, and the range is called the \fIindexed set\fR\&\&. .RE .RS 2 .LP If x is a family from I to X, then x[i] denotes the value of the function at index i\&. The notation "a family in X" is used for such a family\&. .RE .RS 2 .LP When the indexed set is a set of subsets of a set X, we call x a \fIfamily of subsets\fR\& of X\&. .RE .RS 2 .LP If x is a family of subsets of X, the union of the range of x is called the \fIunion of the family\fR\& x\&. .RE .RS 2 .LP If x is non-empty (the index set is non-empty), the \fIintersection of the family\fR\& x is the intersection of the range of x\&. .RE .RS 2 .LP In this module, the only families that are considered are families of subsets of some set X; in the following, the word "family" is used for such families of subsets\&. .RE .LP .TP 2 * A \fIpartition\fR\& of a set X is a collection S of non-empty subsets of X whose union is X and whose elements are pairwise disjoint\&. .RS 2 .LP A relation in a set is an \fIequivalence relation\fR\& if it is reflexive, symmetric, and transitive\&. .RE .RS 2 .LP If R is an equivalence relation in X, and x is an element of X, the \fIequivalence class\fR\& of x with respect to R is the set of all those elements y of X for which x R y holds\&. The equivalence classes constitute a partitioning of X\&. Conversely, if C is a partition of X, the relation that holds for any two elements of X if they belong to the same equivalence class, is an equivalence relation induced by the partition C\&. .RE .RS 2 .LP If R is an equivalence relation in X, the \fIcanonical map\fR\& is the function that maps every element of X onto its equivalence class\&. .RE .LP .TP 2 * Relations as defined above (as sets of ordered pairs) are from now on referred to as \fIbinary relations\fR\&\&. .RS 2 .LP We call a set of ordered sets (x[1], \&.\&.\&., x[n]) an \fI(n-ary) relation\fR\&, and say that the relation is a subset of the Cartesian product X[1] x \&.\&.\&. x X[n], where x[i] is an element of X[i], 1 <= i <= n\&. .RE .RS 2 .LP The \fIprojection\fR\& of an n-ary relation R onto coordinate i is the set {x[i] : (x[1], \&.\&.\&., x[i], \&.\&.\&., x[n]) in R for some x[j] in X[j], 1 <= j <= n and not i = j}\&. The projections of a binary relation R onto the first and second coordinates are the domain and the range of R, respectively\&. .RE .RS 2 .LP The relative product of binary relations can be generalized to n-ary relations as follows\&. Let TR be an ordered set (R[1], \&.\&.\&., R[n]) of binary relations from X to Y[i] and S a binary relation from (Y[1] x \&.\&.\&. x Y[n]) to Z\&. The \fIrelative product\fR\& of TR and S is the binary relation T from X to Z defined so that x T z if and only if there exists an element y[i] in Y[i] for each 1 <= i <= n such that x R[i] y[i] and (y[1], \&.\&.\&., y[n]) S z\&. Now let TR be a an ordered set (R[1], \&.\&.\&., R[n]) of binary relations from X[i] to Y[i] and S a subset of X[1] x \&.\&.\&. x X[n]\&. The \fImultiple relative product\fR\& of TR and S is defined to be the set {z : z = ((x[1], \&.\&.\&., x[n]), (y[1],\&.\&.\&.,y[n])) for some (x[1], \&.\&.\&., x[n]) in S and for some (x[i], y[i]) in R[i], 1 <= i <= n}\&. .RE .RS 2 .LP The \fInatural join\fR\& of an n-ary relation R and an m-ary relation S on coordinate i and j is defined to be the set {z : z = (x[1], \&.\&.\&., x[n], y[1], \&.\&.\&., y[j-1], y[j+1], \&.\&.\&., y[m]) for some (x[1], \&.\&.\&., x[n]) in R and for some (y[1], \&.\&.\&., y[m]) in S such that x[i] = y[j]}\&. .RE .LP .TP 2 * The sets recognized by this module are represented by elements of the relation Sets, which is defined as the smallest set such that: .RS 2 .TP 2 * For every atom T, except \&'_\&', and for every term X, (T, X) belongs to Sets (\fIatomic sets\fR\&)\&. .LP .TP 2 * ([\&'_\&'], []) belongs to Sets (the \fIuntyped empty set\fR\&)\&. .LP .TP 2 * For every tuple T = {T[1], \&.\&.\&., T[n]} and for every tuple X = {X[1], \&.\&.\&., X[n]}, if (T[i], X[i]) belongs to Sets for every 1 <= i <= n, then (T, X) belongs to Sets (\fIordered sets\fR\&)\&. .LP .TP 2 * For every term T, if X is the empty list or a non-empty sorted list [X[1], \&.\&.\&., X[n]] without duplicates such that (T, X[i]) belongs to Sets for every 1 <= i <= n, then ([T], X) belongs to Sets (\fItyped unordered sets\fR\&)\&. .LP .RE .RS 2 .LP An \fIexternal set\fR\& is an element of the range of Sets\&. .RE .RS 2 .LP A \fItype\fR\& is an element of the domain of Sets\&. .RE .RS 2 .LP If S is an element (T, X) of Sets, then T is a \fIvalid type\fR\& of X, T is the type of S, and X is the external set of S\&. \fIfrom_term/2\fR\& creates a set from a type and an Erlang term turned into an external set\&. .RE .RS 2 .LP The sets represented by Sets are the elements of the range of function Set from Sets to Erlang terms and sets of Erlang terms: .RE .RS 2 .TP 2 * Set(T,Term) = Term, where T is an atom .LP .TP 2 * Set({T[1], \&.\&.\&., T[n]}, {X[1], \&.\&.\&., X[n]}) = (Set(T[1], X[1]), \&.\&.\&., Set(T[n], X[n])) .LP .TP 2 * Set([T], [X[1], \&.\&.\&., X[n]]) = {Set(T, X[1]), \&.\&.\&., Set(T, X[n])} .LP .TP 2 * Set([T], []) = {} .LP .RE .RS 2 .LP When there is no risk of confusion, elements of Sets are identified with the sets they represent\&. For example, if U is the result of calling \fIunion/2\fR\& with S1 and S2 as arguments, then U is said to be the union of S1 and S2\&. A more precise formulation is that Set(U) is the union of Set(S1) and Set(S2)\&. .RE .LP .RE .LP The types are used to implement the various conditions that sets must fulfill\&. As an example, consider the relative product of two sets R and S, and recall that the relative product of R and S is defined if R is a binary relation to Y and S is a binary relation from Y\&. The function that implements the relative product, \fIrelative_product/2\fR\&, checks that the arguments represent binary relations by matching [{A,B}] against the type of the first argument (Arg1 say), and [{C,D}] against the type of the second argument (Arg2 say)\&. The fact that [{A,B}] matches the type of Arg1 is to be interpreted as Arg1 representing a binary relation from X to Y, where X is defined as all sets Set(x) for some element x in Sets the type of which is A, and similarly for Y\&. In the same way Arg2 is interpreted as representing a binary relation from W to Z\&. Finally it is checked that B matches C, which is sufficient to ensure that W is equal to Y\&. The untyped empty set is handled separately: its type, [\&'_\&'], matches the type of any unordered set\&. .LP A few functions of this module (\fIdrestriction/3\fR\&, \fIfamily_projection/2\fR\&, \fIpartition/2\fR\&, \fIpartition_family/2\fR\&, \fIprojection/2\fR\&, \fIrestriction/3\fR\&, \fIsubstitution/2\fR\&) accept an Erlang function as a means to modify each element of a given unordered set\&. Such a function, called SetFun in the following, can be specified as a functional object (fun), a tuple \fI{external, Fun}\fR\&, or an integer: .RS 2 .TP 2 * If SetFun is specified as a fun, the fun is applied to each element of the given set and the return value is assumed to be a set\&. .LP .TP 2 * If SetFun is specified as a tuple \fI{external, Fun}\fR\&, Fun is applied to the external set of each element of the given set and the return value is assumed to be an external set\&. Selecting the elements of an unordered set as external sets and assembling a new unordered set from a list of external sets is in the present implementation more efficient than modifying each element as a set\&. However, this optimization can only be used when the elements of the unordered set are atomic or ordered sets\&. It must also be the case that the type of the elements matches some clause of Fun (the type of the created set is the result of applying Fun to the type of the given set), and that Fun does nothing but selecting, duplicating, or rearranging parts of the elements\&. .LP .TP 2 * Specifying a SetFun as an integer I is equivalent to specifying \fI{external, fun(X) -> element(I, X) end}\fR\&, but is to be preferred, as it makes it possible to handle this case even more efficiently\&. .LP .RE .LP Examples of SetFuns: .LP .nf fun sofs:union/1 fun(S) -> sofs:partition(1, S) end {external, fun(A) -> A end} {external, fun({A,_,C}) -> {C,A} end} {external, fun({_,{_,C}}) -> C end} {external, fun({_,{_,{_,E}=C}}) -> {E,{E,C}} end} 2 .fi .LP The order in which a SetFun is applied to the elements of an unordered set is not specified, and can change in future versions of this module\&. .LP The execution time of the functions of this module is dominated by the time it takes to sort lists\&. When no sorting is needed, the execution time is in the worst case proportional to the sum of the sizes of the input arguments and the returned value\&. A few functions execute in constant time: \fIfrom_external/2\fR\&, \fIis_empty_set/1\fR\&, \fIis_set/1\fR\&, \fIis_sofs_set/1\fR\&, \fIto_external/1\fR\& \fItype/1\fR\&\&. .LP The functions of this module exit the process with a \fIbadarg\fR\&, \fIbad_function\fR\&, or \fItype_mismatch\fR\& message when given badly formed arguments or sets the types of which are not compatible\&. .LP When comparing external sets, operator \fI==/2\fR\& is used\&. .SH DATA TYPES .nf \fBanyset()\fR\& = ordset() | a_set() .br .fi .RS .LP Any kind of set (also included are the atomic sets)\&. .RE .nf \fBbinary_relation()\fR\& = relation() .br .fi .RS .LP A binary relation\&. .RE .nf \fBexternal_set()\fR\& = term() .br .fi .RS .LP An external set\&. .RE .nf \fBfamily()\fR\& = a_function() .br .fi .RS .LP A family (of subsets)\&. .RE .nf \fBa_function()\fR\& = relation() .br .fi .RS .LP A function\&. .RE .nf \fBordset()\fR\& .br .fi .RS .LP An ordered set\&. .RE .nf \fBrelation()\fR\& = a_set() .br .fi .RS .LP An n-ary relation\&. .RE .nf \fBa_set()\fR\& .br .fi .RS .LP An unordered set\&. .RE .nf \fBset_of_sets()\fR\& = a_set() .br .fi .RS .LP An unordered set of unordered sets\&. .RE .nf \fBset_fun()\fR\& = .br integer() >= 1 | .br {external, fun((external_set()) -> external_set())} | .br fun((anyset()) -> anyset()) .br .fi .RS .LP A SetFun\&. .RE .nf \fBspec_fun()\fR\& = .br {external, fun((external_set()) -> boolean())} | .br fun((anyset()) -> boolean()) .br .fi .nf \fBtype()\fR\& = term() .br .fi .RS .LP A type\&. .RE .nf .B tuple_of(T) .br .fi .RS .LP A tuple where the elements are of type \fIT\fR\&\&. .RE .SH EXPORTS .LP .nf .B a_function(Tuples) -> Function .br .fi .br .nf .B a_function(Tuples, Type) -> Function .br .fi .br .RS .LP Types: .RS 3 Function = a_function() .br Tuples = [tuple()] .br Type = type() .br .RE .RE .RS .LP Creates a function\&. \fIa_function(F, T)\fR\& is equivalent to \fIfrom_term(F, T)\fR\& if the result is a function\&. If no type is explicitly specified, \fI[{atom, atom}]\fR\& is used as the function type\&. .RE .LP .nf .B canonical_relation(SetOfSets) -> BinRel .br .fi .br .RS .LP Types: .RS 3 BinRel = binary_relation() .br SetOfSets = set_of_sets() .br .RE .RE .RS .LP Returns the binary relation containing the elements (E, Set) such that Set belongs to \fISetOfSets\fR\& and E belongs to Set\&. If \fISetOfSets\fR\& is a partition of a set X and R is the equivalence relation in X induced by \fISetOfSets\fR\&, then the returned relation is the canonical map from X onto the equivalence classes with respect to R\&. .LP .nf 1> Ss = sofs:from_term([[a,b],[b,c]]), CR = sofs:canonical_relation(Ss), sofs:to_external(CR)\&. [{a,[a,b]},{b,[a,b]},{b,[b,c]},{c,[b,c]}] .fi .RE .LP .nf .B composite(Function1, Function2) -> Function3 .br .fi .br .RS .LP Types: .RS 3 Function1 = Function2 = Function3 = a_function() .br .RE .RE .RS .LP Returns the composite of the functions \fIFunction1\fR\& and \fIFunction2\fR\&\&. .LP .nf 1> F1 = sofs:a_function([{a,1},{b,2},{c,2}]), F2 = sofs:a_function([{1,x},{2,y},{3,z}]), F = sofs:composite(F1, F2), sofs:to_external(F)\&. [{a,x},{b,y},{c,y}] .fi .RE .LP .nf .B constant_function(Set, AnySet) -> Function .br .fi .br .RS .LP Types: .RS 3 AnySet = anyset() .br Function = a_function() .br Set = a_set() .br .RE .RE .RS .LP Creates the function that maps each element of set \fISet\fR\& onto \fIAnySet\fR\&\&. .LP .nf 1> S = sofs:set([a,b]), E = sofs:from_term(1), R = sofs:constant_function(S, E), sofs:to_external(R)\&. [{a,1},{b,1}] .fi .RE .LP .nf .B converse(BinRel1) -> BinRel2 .br .fi .br .RS .LP Types: .RS 3 BinRel1 = BinRel2 = binary_relation() .br .RE .RE .RS .LP Returns the converse of the binary relation \fIBinRel1\fR\&\&. .LP .nf 1> R1 = sofs:relation([{1,a},{2,b},{3,a}]), R2 = sofs:converse(R1), sofs:to_external(R2)\&. [{a,1},{a,3},{b,2}] .fi .RE .LP .nf .B difference(Set1, Set2) -> Set3 .br .fi .br .RS .LP Types: .RS 3 Set1 = Set2 = Set3 = a_set() .br .RE .RE .RS .LP Returns the difference of the sets \fISet1\fR\& and \fISet2\fR\&\&. .RE .LP .nf .B digraph_to_family(Graph) -> Family .br .fi .br .nf .B digraph_to_family(Graph, Type) -> Family .br .fi .br .RS .LP Types: .RS 3 Graph = digraph:graph() .br Family = family() .br Type = type() .br .RE .RE .RS .LP Creates a family from the directed graph \fIGraph\fR\&\&. Each vertex a of \fIGraph\fR\& is represented by a pair (a, {b[1], \&.\&.\&., b[n]}), where the b[i]:s are the out-neighbors of a\&. If no type is explicitly specified, [{atom, [atom]}] is used as type of the family\&. It is assumed that \fIType\fR\& is a valid type of the external set of the family\&. .LP If G is a directed graph, it holds that the vertices and edges of G are the same as the vertices and edges of \fIfamily_to_digraph(digraph_to_family(G))\fR\&\&. .RE .LP .nf .B domain(BinRel) -> Set .br .fi .br .RS .LP Types: .RS 3 BinRel = binary_relation() .br Set = a_set() .br .RE .RE .RS .LP Returns the domain of the binary relation \fIBinRel\fR\&\&. .LP .nf 1> R = sofs:relation([{1,a},{1,b},{2,b},{2,c}]), S = sofs:domain(R), sofs:to_external(S)\&. [1,2] .fi .RE .LP .nf .B drestriction(BinRel1, Set) -> BinRel2 .br .fi .br .RS .LP Types: .RS 3 BinRel1 = BinRel2 = binary_relation() .br Set = a_set() .br .RE .RE .RS .LP Returns the difference between the binary relation \fIBinRel1\fR\& and the restriction of \fIBinRel1\fR\& to \fISet\fR\&\&. .LP .nf 1> R1 = sofs:relation([{1,a},{2,b},{3,c}]), S = sofs:set([2,4,6]), R2 = sofs:drestriction(R1, S), sofs:to_external(R2)\&. [{1,a},{3,c}] .fi .LP \fIdrestriction(R, S)\fR\& is equivalent to \fIdifference(R, restriction(R, S))\fR\&\&. .RE .LP .nf .B drestriction(SetFun, Set1, Set2) -> Set3 .br .fi .br .RS .LP Types: .RS 3 SetFun = set_fun() .br Set1 = Set2 = Set3 = a_set() .br .RE .RE .RS .LP Returns a subset of \fISet1\fR\& containing those elements that do not give an element in \fISet2\fR\& as the result of applying \fISetFun\fR\&\&. .LP .nf 1> SetFun = {external, fun({_A,B,C}) -> {B,C} end}, R1 = sofs:relation([{a,aa,1},{b,bb,2},{c,cc,3}]), R2 = sofs:relation([{bb,2},{cc,3},{dd,4}]), R3 = sofs:drestriction(SetFun, R1, R2), sofs:to_external(R3)\&. [{a,aa,1}] .fi .LP \fIdrestriction(F, S1, S2)\fR\& is equivalent to \fIdifference(S1, restriction(F, S1, S2))\fR\&\&. .RE .LP .nf .B empty_set() -> Set .br .fi .br .RS .LP Types: .RS 3 Set = a_set() .br .RE .RE .RS .LP Returns the untyped empty set\&. \fIempty_set()\fR\& is equivalent to \fIfrom_term([], [\&'_\&'])\fR\&\&. .RE .LP .nf .B extension(BinRel1, Set, AnySet) -> BinRel2 .br .fi .br .RS .LP Types: .RS 3 AnySet = anyset() .br BinRel1 = BinRel2 = binary_relation() .br Set = a_set() .br .RE .RE .RS .LP Returns the extension of \fIBinRel1\fR\& such that for each element E in \fISet\fR\& that does not belong to the domain of \fIBinRel1\fR\&, \fIBinRel2\fR\& contains the pair (E, \fIAnySet\fR\&)\&. .LP .nf 1> S = sofs:set([b,c]), A = sofs:empty_set(), R = sofs:family([{a,[1,2]},{b,[3]}]), X = sofs:extension(R, S, A), sofs:to_external(X)\&. [{a,[1,2]},{b,[3]},{c,[]}] .fi .RE .LP .nf .B family(Tuples) -> Family .br .fi .br .nf .B family(Tuples, Type) -> Family .br .fi .br .RS .LP Types: .RS 3 Family = family() .br Tuples = [tuple()] .br Type = type() .br .RE .RE .RS .LP Creates a family of subsets\&. \fIfamily(F, T)\fR\& is equivalent to \fIfrom_term(F, T)\fR\& if the result is a family\&. If no type is explicitly specified, \fI[{atom, [atom]}]\fR\& is used as the family type\&. .RE .LP .nf .B family_difference(Family1, Family2) -> Family3 .br .fi .br .RS .LP Types: .RS 3 Family1 = Family2 = Family3 = family() .br .RE .RE .RS .LP If \fIFamily1\fR\& and \fIFamily2\fR\& are families, then \fIFamily3\fR\& is the family such that the index set is equal to the index set of \fIFamily1\fR\&, and \fIFamily3\fR\&[i] is the difference between \fIFamily1\fR\&[i] and \fIFamily2\fR\&[i] if \fIFamily2\fR\& maps i, otherwise \fIFamily1[i]\fR\&\&. .LP .nf 1> F1 = sofs:family([{a,[1,2]},{b,[3,4]}]), F2 = sofs:family([{b,[4,5]},{c,[6,7]}]), F3 = sofs:family_difference(F1, F2), sofs:to_external(F3)\&. [{a,[1,2]},{b,[3]}] .fi .RE .LP .nf .B family_domain(Family1) -> Family2 .br .fi .br .RS .LP Types: .RS 3 Family1 = Family2 = family() .br .RE .RE .RS .LP If \fIFamily1\fR\& is a family and \fIFamily1\fR\&[i] is a binary relation for every i in the index set of \fIFamily1\fR\&, then \fIFamily2\fR\& is the family with the same index set as \fIFamily1\fR\& such that \fIFamily2\fR\&[i] is the domain of \fIFamily1[i]\fR\&\&. .LP .nf 1> FR = sofs:from_term([{a,[{1,a},{2,b},{3,c}]},{b,[]},{c,[{4,d},{5,e}]}]), F = sofs:family_domain(FR), sofs:to_external(F)\&. [{a,[1,2,3]},{b,[]},{c,[4,5]}] .fi .RE .LP .nf .B family_field(Family1) -> Family2 .br .fi .br .RS .LP Types: .RS 3 Family1 = Family2 = family() .br .RE .RE .RS .LP If \fIFamily1\fR\& is a family and \fIFamily1\fR\&[i] is a binary relation for every i in the index set of \fIFamily1\fR\&, then \fIFamily2\fR\& is the family with the same index set as \fIFamily1\fR\& such that \fIFamily2\fR\&[i] is the field of \fIFamily1\fR\&[i]\&. .LP .nf 1> FR = sofs:from_term([{a,[{1,a},{2,b},{3,c}]},{b,[]},{c,[{4,d},{5,e}]}]), F = sofs:family_field(FR), sofs:to_external(F)\&. [{a,[1,2,3,a,b,c]},{b,[]},{c,[4,5,d,e]}] .fi .LP \fIfamily_field(Family1)\fR\& is equivalent to \fIfamily_union(family_domain(Family1), family_range(Family1))\fR\&\&. .RE .LP .nf .B family_intersection(Family1) -> Family2 .br .fi .br .RS .LP Types: .RS 3 Family1 = Family2 = family() .br .RE .RE .RS .LP If \fIFamily1\fR\& is a family and \fIFamily1\fR\&[i] is a set of sets for every i in the index set of \fIFamily1\fR\&, then \fIFamily2\fR\& is the family with the same index set as \fIFamily1\fR\& such that \fIFamily2\fR\&[i] is the intersection of \fIFamily1\fR\&[i]\&. .LP If \fIFamily1\fR\&[i] is an empty set for some i, the process exits with a \fIbadarg\fR\& message\&. .LP .nf 1> F1 = sofs:from_term([{a,[[1,2,3],[2,3,4]]},{b,[[x,y,z],[x,y]]}]), F2 = sofs:family_intersection(F1), sofs:to_external(F2)\&. [{a,[2,3]},{b,[x,y]}] .fi .RE .LP .nf .B family_intersection(Family1, Family2) -> Family3 .br .fi .br .RS .LP Types: .RS 3 Family1 = Family2 = Family3 = family() .br .RE .RE .RS .LP If \fIFamily1\fR\& and \fIFamily2\fR\& are families, then \fIFamily3\fR\& is the family such that the index set is the intersection of \fIFamily1\fR\&:s and \fIFamily2\fR\&:s index sets, and \fIFamily3\fR\&[i] is the intersection of \fIFamily1\fR\&[i] and \fIFamily2\fR\&[i]\&. .LP .nf 1> F1 = sofs:family([{a,[1,2]},{b,[3,4]},{c,[5,6]}]), F2 = sofs:family([{b,[4,5]},{c,[7,8]},{d,[9,10]}]), F3 = sofs:family_intersection(F1, F2), sofs:to_external(F3)\&. [{b,[4]},{c,[]}] .fi .RE .LP .nf .B family_projection(SetFun, Family1) -> Family2 .br .fi .br .RS .LP Types: .RS 3 SetFun = set_fun() .br Family1 = Family2 = family() .br .RE .RE .RS .LP If \fIFamily1\fR\& is a family, then \fIFamily2\fR\& is the family with the same index set as \fIFamily1\fR\& such that \fIFamily2\fR\&[i] is the result of calling \fISetFun\fR\& with \fIFamily1\fR\&[i] as argument\&. .LP .nf 1> F1 = sofs:from_term([{a,[[1,2],[2,3]]},{b,[[]]}]), F2 = sofs:family_projection(fun sofs:union/1, F1), sofs:to_external(F2)\&. [{a,[1,2,3]},{b,[]}] .fi .RE .LP .nf .B family_range(Family1) -> Family2 .br .fi .br .RS .LP Types: .RS 3 Family1 = Family2 = family() .br .RE .RE .RS .LP If \fIFamily1\fR\& is a family and \fIFamily1\fR\&[i] is a binary relation for every i in the index set of \fIFamily1\fR\&, then \fIFamily2\fR\& is the family with the same index set as \fIFamily1\fR\& such that \fIFamily2\fR\&[i] is the range of \fIFamily1\fR\&[i]\&. .LP .nf 1> FR = sofs:from_term([{a,[{1,a},{2,b},{3,c}]},{b,[]},{c,[{4,d},{5,e}]}]), F = sofs:family_range(FR), sofs:to_external(F)\&. [{a,[a,b,c]},{b,[]},{c,[d,e]}] .fi .RE .LP .nf .B family_specification(Fun, Family1) -> Family2 .br .fi .br .RS .LP Types: .RS 3 Fun = spec_fun() .br Family1 = Family2 = family() .br .RE .RE .RS .LP If \fIFamily1\fR\& is a family, then \fIFamily2\fR\& is the restriction of \fIFamily1\fR\& to those elements i of the index set for which \fIFun\fR\& applied to \fIFamily1\fR\&[i] returns \fItrue\fR\&\&. If \fIFun\fR\& is a tuple \fI{external, Fun2}\fR\&, then \fIFun2\fR\& is applied to the external set of \fIFamily1\fR\&[i], otherwise \fIFun\fR\& is applied to \fIFamily1\fR\&[i]\&. .LP .nf 1> F1 = sofs:family([{a,[1,2,3]},{b,[1,2]},{c,[1]}]), SpecFun = fun(S) -> sofs:no_elements(S) =:= 2 end, F2 = sofs:family_specification(SpecFun, F1), sofs:to_external(F2)\&. [{b,[1,2]}] .fi .RE .LP .nf .B family_to_digraph(Family) -> Graph .br .fi .br .nf .B family_to_digraph(Family, GraphType) -> Graph .br .fi .br .RS .LP Types: .RS 3 Graph = digraph:graph() .br Family = family() .br GraphType = [digraph:d_type()] .br .RE .RE .RS .LP Creates a directed graph from family \fIFamily\fR\&\&. For each pair (a, {b[1], \&.\&.\&., b[n]}) of \fIFamily\fR\&, vertex a and the edges (a, b[i]) for 1 <= i <= n are added to a newly created directed graph\&. .LP If no graph type is specified, \fIdigraph:new/0\fR\& is used for creating the directed graph, otherwise argument \fIGraphType\fR\& is passed on as second argument to \fIdigraph:new/1\fR\&\&. .LP It F is a family, it holds that F is a subset of \fIdigraph_to_family(family_to_digraph(F), type(F))\fR\&\&. Equality holds if \fIunion_of_family(F)\fR\& is a subset of \fIdomain(F)\fR\&\&. .LP Creating a cycle in an acyclic graph exits the process with a \fIcyclic\fR\& message\&. .RE .LP .nf .B family_to_relation(Family) -> BinRel .br .fi .br .RS .LP Types: .RS 3 Family = family() .br BinRel = binary_relation() .br .RE .RE .RS .LP If \fIFamily\fR\& is a family, then \fIBinRel\fR\& is the binary relation containing all pairs (i, x) such that i belongs to the index set of \fIFamily\fR\& and x belongs to \fIFamily\fR\&[i]\&. .LP .nf 1> F = sofs:family([{a,[]}, {b,[1]}, {c,[2,3]}]), R = sofs:family_to_relation(F), sofs:to_external(R)\&. [{b,1},{c,2},{c,3}] .fi .RE .LP .nf .B family_union(Family1) -> Family2 .br .fi .br .RS .LP Types: .RS 3 Family1 = Family2 = family() .br .RE .RE .RS .LP If \fIFamily1\fR\& is a family and \fIFamily1\fR\&[i] is a set of sets for each i in the index set of \fIFamily1\fR\&, then \fIFamily2\fR\& is the family with the same index set as \fIFamily1\fR\& such that \fIFamily2\fR\&[i] is the union of \fIFamily1\fR\&[i]\&. .LP .nf 1> F1 = sofs:from_term([{a,[[1,2],[2,3]]},{b,[[]]}]), F2 = sofs:family_union(F1), sofs:to_external(F2)\&. [{a,[1,2,3]},{b,[]}] .fi .LP \fIfamily_union(F)\fR\& is equivalent to \fIfamily_projection(fun sofs:union/1, F)\fR\&\&. .RE .LP .nf .B family_union(Family1, Family2) -> Family3 .br .fi .br .RS .LP Types: .RS 3 Family1 = Family2 = Family3 = family() .br .RE .RE .RS .LP If \fIFamily1\fR\& and \fIFamily2\fR\& are families, then \fIFamily3\fR\& is the family such that the index set is the union of \fIFamily1\fR\&:s and \fIFamily2\fR\&:s index sets, and \fIFamily3\fR\&[i] is the union of \fIFamily1\fR\&[i] and \fIFamily2\fR\&[i] if both map i, otherwise \fIFamily1\fR\&[i] or \fIFamily2\fR\&[i]\&. .LP .nf 1> F1 = sofs:family([{a,[1,2]},{b,[3,4]},{c,[5,6]}]), F2 = sofs:family([{b,[4,5]},{c,[7,8]},{d,[9,10]}]), F3 = sofs:family_union(F1, F2), sofs:to_external(F3)\&. [{a,[1,2]},{b,[3,4,5]},{c,[5,6,7,8]},{d,[9,10]}] .fi .RE .LP .nf .B field(BinRel) -> Set .br .fi .br .RS .LP Types: .RS 3 BinRel = binary_relation() .br Set = a_set() .br .RE .RE .RS .LP Returns the field of the binary relation \fIBinRel\fR\&\&. .LP .nf 1> R = sofs:relation([{1,a},{1,b},{2,b},{2,c}]), S = sofs:field(R), sofs:to_external(S)\&. [1,2,a,b,c] .fi .LP \fIfield(R)\fR\& is equivalent to \fIunion(domain(R), range(R))\fR\&\&. .RE .LP .nf .B from_external(ExternalSet, Type) -> AnySet .br .fi .br .RS .LP Types: .RS 3 ExternalSet = external_set() .br AnySet = anyset() .br Type = type() .br .RE .RE .RS .LP Creates a set from the external set \fIExternalSet\fR\& and the type \fIType\fR\&\&. It is assumed that \fIType\fR\& is a valid type of \fIExternalSet\fR\&\&. .RE .LP .nf .B from_sets(ListOfSets) -> Set .br .fi .br .RS .LP Types: .RS 3 Set = a_set() .br ListOfSets = [anyset()] .br .RE .RE .RS .LP Returns the unordered set containing the sets of list \fIListOfSets\fR\&\&. .LP .nf 1> S1 = sofs:relation([{a,1},{b,2}]), S2 = sofs:relation([{x,3},{y,4}]), S = sofs:from_sets([S1,S2]), sofs:to_external(S)\&. [[{a,1},{b,2}],[{x,3},{y,4}]] .fi .RE .LP .nf .B from_sets(TupleOfSets) -> Ordset .br .fi .br .RS .LP Types: .RS 3 Ordset = ordset() .br TupleOfSets = tuple_of(anyset()) .br .RE .RE .RS .LP Returns the ordered set containing the sets of the non-empty tuple \fITupleOfSets\fR\&\&. .RE .LP .nf .B from_term(Term) -> AnySet .br .fi .br .nf .B from_term(Term, Type) -> AnySet .br .fi .br .RS .LP Types: .RS 3 AnySet = anyset() .br Term = term() .br Type = type() .br .RE .RE .RS .LP Creates an element of Sets by traversing term \fITerm\fR\&, sorting lists, removing duplicates, and deriving or verifying a valid type for the so obtained external set\&. An explicitly specified type \fIType\fR\& can be used to limit the depth of the traversal; an atomic type stops the traversal, as shown by the following example where \fI"foo"\fR\& and \fI{"foo"}\fR\& are left unmodified: .LP .nf 1> S = sofs:from_term([{{"foo"},[1,1]},{"foo",[2,2]}], [{atom,[atom]}]), sofs:to_external(S)\&. [{{"foo"},[1]},{"foo",[2]}] .fi .LP \fIfrom_term\fR\& can be used for creating atomic or ordered sets\&. The only purpose of such a set is that of later building unordered sets, as all functions in this module that \fIdo\fR\& anything operate on unordered sets\&. Creating unordered sets from a collection of ordered sets can be the way to go if the ordered sets are big and one does not want to waste heap by rebuilding the elements of the unordered set\&. The following example shows that a set can be built "layer by layer": .LP .nf 1> A = sofs:from_term(a), S = sofs:set([1,2,3]), P1 = sofs:from_sets({A,S}), P2 = sofs:from_term({b,[6,5,4]}), Ss = sofs:from_sets([P1,P2]), sofs:to_external(Ss)\&. [{a,[1,2,3]},{b,[4,5,6]}] .fi .LP Other functions that create sets are \fIfrom_external/2\fR\& and \fIfrom_sets/1\fR\&\&. Special cases of \fIfrom_term/2\fR\& are \fIa_function/1,2\fR\&, \fIempty_set/0\fR\&, \fIfamily/1,2\fR\&, \fIrelation/1,2\fR\&, and \fIset/1,2\fR\&\&. .RE .LP .nf .B image(BinRel, Set1) -> Set2 .br .fi .br .RS .LP Types: .RS 3 BinRel = binary_relation() .br Set1 = Set2 = a_set() .br .RE .RE .RS .LP Returns the image of set \fISet1\fR\& under the binary relation \fIBinRel\fR\&\&. .LP .nf 1> R = sofs:relation([{1,a},{2,b},{2,c},{3,d}]), S1 = sofs:set([1,2]), S2 = sofs:image(R, S1), sofs:to_external(S2)\&. [a,b,c] .fi .RE .LP .nf .B intersection(SetOfSets) -> Set .br .fi .br .RS .LP Types: .RS 3 Set = a_set() .br SetOfSets = set_of_sets() .br .RE .RE .RS .LP Returns the intersection of the set of sets \fISetOfSets\fR\&\&. .LP Intersecting an empty set of sets exits the process with a \fIbadarg\fR\& message\&. .RE .LP .nf .B intersection(Set1, Set2) -> Set3 .br .fi .br .RS .LP Types: .RS 3 Set1 = Set2 = Set3 = a_set() .br .RE .RE .RS .LP Returns the intersection of \fISet1\fR\& and \fISet2\fR\&\&. .RE .LP .nf .B intersection_of_family(Family) -> Set .br .fi .br .RS .LP Types: .RS 3 Family = family() .br Set = a_set() .br .RE .RE .RS .LP Returns the intersection of family \fIFamily\fR\&\&. .LP Intersecting an empty family exits the process with a \fIbadarg\fR\& message\&. .LP .nf 1> F = sofs:family([{a,[0,2,4]},{b,[0,1,2]},{c,[2,3]}]), S = sofs:intersection_of_family(F), sofs:to_external(S)\&. [2] .fi .RE .LP .nf .B inverse(Function1) -> Function2 .br .fi .br .RS .LP Types: .RS 3 Function1 = Function2 = a_function() .br .RE .RE .RS .LP Returns the inverse of function \fIFunction1\fR\&\&. .LP .nf 1> R1 = sofs:relation([{1,a},{2,b},{3,c}]), R2 = sofs:inverse(R1), sofs:to_external(R2)\&. [{a,1},{b,2},{c,3}] .fi .RE .LP .nf .B inverse_image(BinRel, Set1) -> Set2 .br .fi .br .RS .LP Types: .RS 3 BinRel = binary_relation() .br Set1 = Set2 = a_set() .br .RE .RE .RS .LP Returns the inverse image of \fISet1\fR\& under the binary relation \fIBinRel\fR\&\&. .LP .nf 1> R = sofs:relation([{1,a},{2,b},{2,c},{3,d}]), S1 = sofs:set([c,d,e]), S2 = sofs:inverse_image(R, S1), sofs:to_external(S2)\&. [2,3] .fi .RE .LP .nf .B is_a_function(BinRel) -> Bool .br .fi .br .RS .LP Types: .RS 3 Bool = boolean() .br BinRel = binary_relation() .br .RE .RE .RS .LP Returns \fItrue\fR\& if the binary relation \fIBinRel\fR\& is a function or the untyped empty set, otherwise \fIfalse\fR\&\&. .RE .LP .nf .B is_disjoint(Set1, Set2) -> Bool .br .fi .br .RS .LP Types: .RS 3 Bool = boolean() .br Set1 = Set2 = a_set() .br .RE .RE .RS .LP Returns \fItrue\fR\& if \fISet1\fR\& and \fISet2\fR\& are disjoint, otherwise \fIfalse\fR\&\&. .RE .LP .nf .B is_empty_set(AnySet) -> Bool .br .fi .br .RS .LP Types: .RS 3 AnySet = anyset() .br Bool = boolean() .br .RE .RE .RS .LP Returns \fItrue\fR\& if \fIAnySet\fR\& is an empty unordered set, otherwise \fIfalse\fR\&\&. .RE .LP .nf .B is_equal(AnySet1, AnySet2) -> Bool .br .fi .br .RS .LP Types: .RS 3 AnySet1 = AnySet2 = anyset() .br Bool = boolean() .br .RE .RE .RS .LP Returns \fItrue\fR\& if \fIAnySet1\fR\& and \fIAnySet2\fR\& are equal, otherwise \fIfalse\fR\&\&. The following example shows that \fI==/2\fR\& is used when comparing sets for equality: .LP .nf 1> S1 = sofs:set([1\&.0]), S2 = sofs:set([1]), sofs:is_equal(S1, S2)\&. true .fi .RE .LP .nf .B is_set(AnySet) -> Bool .br .fi .br .RS .LP Types: .RS 3 AnySet = anyset() .br Bool = boolean() .br .RE .RE .RS .LP Returns \fItrue\fR\& if \fIAnySet\fR\& appears to be an unordered set, and \fIfalse\fR\& if \fIAnySet\fR\& is an ordered set or an atomic set or any other term\&. Note that the test is shallow and this function will return \fItrue\fR\& for any term that coincides with the representation of an unordered set\&. See also note on data types\&. .RE .LP .nf .B is_sofs_set(Term) -> Bool .br .fi .br .RS .LP Types: .RS 3 Bool = boolean() .br Term = term() .br .RE .RE .RS .LP Returns \fItrue\fR\& if \fITerm\fR\& appears to be an unordered set, an ordered set, or an atomic set, otherwise \fIfalse\fR\&\&. Note that this function will return \fItrue\fR\& for any term that coincides with the representation of a \fIsofs\fR\& set\&. See also note on data types\&. .RE .LP .nf .B is_subset(Set1, Set2) -> Bool .br .fi .br .RS .LP Types: .RS 3 Bool = boolean() .br Set1 = Set2 = a_set() .br .RE .RE .RS .LP Returns \fItrue\fR\& if \fISet1\fR\& is a subset of \fISet2\fR\&, otherwise \fIfalse\fR\&\&. .RE .LP .nf .B is_type(Term) -> Bool .br .fi .br .RS .LP Types: .RS 3 Bool = boolean() .br Term = term() .br .RE .RE .RS .LP Returns \fItrue\fR\& if term \fITerm\fR\& is a type\&. .RE .LP .nf .B join(Relation1, I, Relation2, J) -> Relation3 .br .fi .br .RS .LP Types: .RS 3 Relation1 = Relation2 = Relation3 = relation() .br I = J = integer() >= 1 .br .RE .RE .RS .LP Returns the natural join of the relations \fIRelation1\fR\& and \fIRelation2\fR\& on coordinates \fII\fR\& and \fIJ\fR\&\&. .LP .nf 1> R1 = sofs:relation([{a,x,1},{b,y,2}]), R2 = sofs:relation([{1,f,g},{1,h,i},{2,3,4}]), J = sofs:join(R1, 3, R2, 1), sofs:to_external(J)\&. [{a,x,1,f,g},{a,x,1,h,i},{b,y,2,3,4}] .fi .RE .LP .nf .B multiple_relative_product(TupleOfBinRels, BinRel1) -> BinRel2 .br .fi .br .RS .LP Types: .RS 3 TupleOfBinRels = tuple_of(BinRel) .br BinRel = BinRel1 = BinRel2 = binary_relation() .br .RE .RE .RS .LP If \fITupleOfBinRels\fR\& is a non-empty tuple {R[1], \&.\&.\&., R[n]} of binary relations and \fIBinRel1\fR\& is a binary relation, then \fIBinRel2\fR\& is the multiple relative product of the ordered set (R[i], \&.\&.\&., R[n]) and \fIBinRel1\fR\&\&. .LP .nf 1> Ri = sofs:relation([{a,1},{b,2},{c,3}]), R = sofs:relation([{a,b},{b,c},{c,a}]), MP = sofs:multiple_relative_product({Ri, Ri}, R), sofs:to_external(sofs:range(MP))\&. [{1,2},{2,3},{3,1}] .fi .RE .LP .nf .B no_elements(ASet) -> NoElements .br .fi .br .RS .LP Types: .RS 3 ASet = a_set() | ordset() .br NoElements = integer() >= 0 .br .RE .RE .RS .LP Returns the number of elements of the ordered or unordered set \fIASet\fR\&\&. .RE .LP .nf .B partition(SetOfSets) -> Partition .br .fi .br .RS .LP Types: .RS 3 SetOfSets = set_of_sets() .br Partition = a_set() .br .RE .RE .RS .LP Returns the partition of the union of the set of sets \fISetOfSets\fR\& such that two elements are considered equal if they belong to the same elements of \fISetOfSets\fR\&\&. .LP .nf 1> Sets1 = sofs:from_term([[a,b,c],[d,e,f],[g,h,i]]), Sets2 = sofs:from_term([[b,c,d],[e,f,g],[h,i,j]]), P = sofs:partition(sofs:union(Sets1, Sets2)), sofs:to_external(P)\&. [[a],[b,c],[d],[e,f],[g],[h,i],[j]] .fi .RE .LP .nf .B partition(SetFun, Set) -> Partition .br .fi .br .RS .LP Types: .RS 3 SetFun = set_fun() .br Partition = Set = a_set() .br .RE .RE .RS .LP Returns the partition of \fISet\fR\& such that two elements are considered equal if the results of applying \fISetFun\fR\& are equal\&. .LP .nf 1> Ss = sofs:from_term([[a],[b],[c,d],[e,f]]), SetFun = fun(S) -> sofs:from_term(sofs:no_elements(S)) end, P = sofs:partition(SetFun, Ss), sofs:to_external(P)\&. [[[a],[b]],[[c,d],[e,f]]] .fi .RE .LP .nf .B partition(SetFun, Set1, Set2) -> {Set3, Set4} .br .fi .br .RS .LP Types: .RS 3 SetFun = set_fun() .br Set1 = Set2 = Set3 = Set4 = a_set() .br .RE .RE .RS .LP Returns a pair of sets that, regarded as constituting a set, forms a partition of \fISet1\fR\&\&. If the result of applying \fISetFun\fR\& to an element of \fISet1\fR\& gives an element in \fISet2\fR\&, the element belongs to \fISet3\fR\&, otherwise the element belongs to \fISet4\fR\&\&. .LP .nf 1> R1 = sofs:relation([{1,a},{2,b},{3,c}]), S = sofs:set([2,4,6]), {R2,R3} = sofs:partition(1, R1, S), {sofs:to_external(R2),sofs:to_external(R3)}\&. {[{2,b}],[{1,a},{3,c}]} .fi .LP \fIpartition(F, S1, S2)\fR\& is equivalent to \fI{restriction(F, S1, S2), drestriction(F, S1, S2)}\fR\&\&. .RE .LP .nf .B partition_family(SetFun, Set) -> Family .br .fi .br .RS .LP Types: .RS 3 Family = family() .br SetFun = set_fun() .br Set = a_set() .br .RE .RE .RS .LP Returns family \fIFamily\fR\& where the indexed set is a partition of \fISet\fR\& such that two elements are considered equal if the results of applying \fISetFun\fR\& are the same value i\&. This i is the index that \fIFamily\fR\& maps onto the equivalence class\&. .LP .nf 1> S = sofs:relation([{a,a,a,a},{a,a,b,b},{a,b,b,b}]), SetFun = {external, fun({A,_,C,_}) -> {A,C} end}, F = sofs:partition_family(SetFun, S), sofs:to_external(F)\&. [{{a,a},[{a,a,a,a}]},{{a,b},[{a,a,b,b},{a,b,b,b}]}] .fi .RE .LP .nf .B product(TupleOfSets) -> Relation .br .fi .br .RS .LP Types: .RS 3 Relation = relation() .br TupleOfSets = tuple_of(a_set()) .br .RE .RE .RS .LP Returns the Cartesian product of the non-empty tuple of sets \fITupleOfSets\fR\&\&. If (x[1], \&.\&.\&., x[n]) is an element of the n-ary relation \fIRelation\fR\&, then x[i] is drawn from element i of \fITupleOfSets\fR\&\&. .LP .nf 1> S1 = sofs:set([a,b]), S2 = sofs:set([1,2]), S3 = sofs:set([x,y]), P3 = sofs:product({S1,S2,S3}), sofs:to_external(P3)\&. [{a,1,x},{a,1,y},{a,2,x},{a,2,y},{b,1,x},{b,1,y},{b,2,x},{b,2,y}] .fi .RE .LP .nf .B product(Set1, Set2) -> BinRel .br .fi .br .RS .LP Types: .RS 3 BinRel = binary_relation() .br Set1 = Set2 = a_set() .br .RE .RE .RS .LP Returns the Cartesian product of \fISet1\fR\& and \fISet2\fR\&\&. .LP .nf 1> S1 = sofs:set([1,2]), S2 = sofs:set([a,b]), R = sofs:product(S1, S2), sofs:to_external(R)\&. [{1,a},{1,b},{2,a},{2,b}] .fi .LP \fIproduct(S1, S2)\fR\& is equivalent to \fIproduct({S1, S2})\fR\&\&. .RE .LP .nf .B projection(SetFun, Set1) -> Set2 .br .fi .br .RS .LP Types: .RS 3 SetFun = set_fun() .br Set1 = Set2 = a_set() .br .RE .RE .RS .LP Returns the set created by substituting each element of \fISet1\fR\& by the result of applying \fISetFun\fR\& to the element\&. .LP If \fISetFun\fR\& is a number i >= 1 and \fISet1\fR\& is a relation, then the returned set is the projection of \fISet1\fR\& onto coordinate i\&. .LP .nf 1> S1 = sofs:from_term([{1,a},{2,b},{3,a}]), S2 = sofs:projection(2, S1), sofs:to_external(S2)\&. [a,b] .fi .RE .LP .nf .B range(BinRel) -> Set .br .fi .br .RS .LP Types: .RS 3 BinRel = binary_relation() .br Set = a_set() .br .RE .RE .RS .LP Returns the range of the binary relation \fIBinRel\fR\&\&. .LP .nf 1> R = sofs:relation([{1,a},{1,b},{2,b},{2,c}]), S = sofs:range(R), sofs:to_external(S)\&. [a,b,c] .fi .RE .LP .nf .B relation(Tuples) -> Relation .br .fi .br .nf .B relation(Tuples, Type) -> Relation .br .fi .br .RS .LP Types: .RS 3 N = integer() .br Type = N | type() .br Relation = relation() .br Tuples = [tuple()] .br .RE .RE .RS .LP Creates a relation\&. \fIrelation(R, T)\fR\& is equivalent to \fIfrom_term(R, T)\fR\&, if T is a type and the result is a relation\&. If \fIType\fR\& is an integer N, then \fI[{atom, \&.\&.\&., atom}])\fR\&, where the tuple size is N, is used as type of the relation\&. If no type is explicitly specified, the size of the first tuple of \fITuples\fR\& is used if there is such a tuple\&. \fIrelation([])\fR\& is equivalent to \fIrelation([], 2)\fR\&\&. .RE .LP .nf .B relation_to_family(BinRel) -> Family .br .fi .br .RS .LP Types: .RS 3 Family = family() .br BinRel = binary_relation() .br .RE .RE .RS .LP Returns family \fIFamily\fR\& such that the index set is equal to the domain of the binary relation \fIBinRel\fR\&, and \fIFamily\fR\&[i] is the image of the set of i under \fIBinRel\fR\&\&. .LP .nf 1> R = sofs:relation([{b,1},{c,2},{c,3}]), F = sofs:relation_to_family(R), sofs:to_external(F)\&. [{b,[1]},{c,[2,3]}] .fi .RE .LP .nf .B relative_product(ListOfBinRels) -> BinRel2 .br .fi .br .nf .B relative_product(ListOfBinRels, BinRel1) -> BinRel2 .br .fi .br .RS .LP Types: .RS 3 ListOfBinRels = [BinRel, \&.\&.\&.] .br BinRel = BinRel1 = BinRel2 = binary_relation() .br .RE .RE .RS .LP If \fIListOfBinRels\fR\& is a non-empty list [R[1], \&.\&.\&., R[n]] of binary relations and \fIBinRel1\fR\& is a binary relation, then \fIBinRel2\fR\& is the relative product of the ordered set (R[i], \&.\&.\&., R[n]) and \fIBinRel1\fR\&\&. .LP If \fIBinRel1\fR\& is omitted, the relation of equality between the elements of the Cartesian product of the ranges of R[i], range R[1] x \&.\&.\&. x range R[n], is used instead (intuitively, nothing is "lost")\&. .LP .nf 1> TR = sofs:relation([{1,a},{1,aa},{2,b}]), R1 = sofs:relation([{1,u},{2,v},{3,c}]), R2 = sofs:relative_product([TR, R1]), sofs:to_external(R2)\&. [{1,{a,u}},{1,{aa,u}},{2,{b,v}}] .fi .LP Notice that \fIrelative_product([R1], R2)\fR\& is different from \fIrelative_product(R1, R2)\fR\&; the list of one element is not identified with the element itself\&. .RE .LP .nf .B relative_product(BinRel1, BinRel2) -> BinRel3 .br .fi .br .RS .LP Types: .RS 3 BinRel1 = BinRel2 = BinRel3 = binary_relation() .br .RE .RE .RS .LP Returns the relative product of the binary relations \fIBinRel1\fR\& and \fIBinRel2\fR\&\&. .RE .LP .nf .B relative_product1(BinRel1, BinRel2) -> BinRel3 .br .fi .br .RS .LP Types: .RS 3 BinRel1 = BinRel2 = BinRel3 = binary_relation() .br .RE .RE .RS .LP Returns the relative product of the converse of the binary relation \fIBinRel1\fR\& and the binary relation \fIBinRel2\fR\&\&. .LP .nf 1> R1 = sofs:relation([{1,a},{1,aa},{2,b}]), R2 = sofs:relation([{1,u},{2,v},{3,c}]), R3 = sofs:relative_product1(R1, R2), sofs:to_external(R3)\&. [{a,u},{aa,u},{b,v}] .fi .LP \fIrelative_product1(R1, R2)\fR\& is equivalent to \fIrelative_product(converse(R1), R2)\fR\&\&. .RE .LP .nf .B restriction(BinRel1, Set) -> BinRel2 .br .fi .br .RS .LP Types: .RS 3 BinRel1 = BinRel2 = binary_relation() .br Set = a_set() .br .RE .RE .RS .LP Returns the restriction of the binary relation \fIBinRel1\fR\& to \fISet\fR\&\&. .LP .nf 1> R1 = sofs:relation([{1,a},{2,b},{3,c}]), S = sofs:set([1,2,4]), R2 = sofs:restriction(R1, S), sofs:to_external(R2)\&. [{1,a},{2,b}] .fi .RE .LP .nf .B restriction(SetFun, Set1, Set2) -> Set3 .br .fi .br .RS .LP Types: .RS 3 SetFun = set_fun() .br Set1 = Set2 = Set3 = a_set() .br .RE .RE .RS .LP Returns a subset of \fISet1\fR\& containing those elements that gives an element in \fISet2\fR\& as the result of applying \fISetFun\fR\&\&. .LP .nf 1> S1 = sofs:relation([{1,a},{2,b},{3,c}]), S2 = sofs:set([b,c,d]), S3 = sofs:restriction(2, S1, S2), sofs:to_external(S3)\&. [{2,b},{3,c}] .fi .RE .LP .nf .B set(Terms) -> Set .br .fi .br .nf .B set(Terms, Type) -> Set .br .fi .br .RS .LP Types: .RS 3 Set = a_set() .br Terms = [term()] .br Type = type() .br .RE .RE .RS .LP Creates an unordered set\&. \fIset(L, T)\fR\& is equivalent to \fIfrom_term(L, T)\fR\&, if the result is an unordered set\&. If no type is explicitly specified, \fI[atom]\fR\& is used as the set type\&. .RE .LP .nf .B specification(Fun, Set1) -> Set2 .br .fi .br .RS .LP Types: .RS 3 Fun = spec_fun() .br Set1 = Set2 = a_set() .br .RE .RE .RS .LP Returns the set containing every element of \fISet1\fR\& for which \fIFun\fR\& returns \fItrue\fR\&\&. If \fIFun\fR\& is a tuple \fI{external, Fun2}\fR\&, \fIFun2\fR\& is applied to the external set of each element, otherwise \fIFun\fR\& is applied to each element\&. .LP .nf 1> R1 = sofs:relation([{a,1},{b,2}]), R2 = sofs:relation([{x,1},{x,2},{y,3}]), S1 = sofs:from_sets([R1,R2]), S2 = sofs:specification(fun sofs:is_a_function/1, S1), sofs:to_external(S2)\&. [[{a,1},{b,2}]] .fi .RE .LP .nf .B strict_relation(BinRel1) -> BinRel2 .br .fi .br .RS .LP Types: .RS 3 BinRel1 = BinRel2 = binary_relation() .br .RE .RE .RS .LP Returns the strict relation corresponding to the binary relation \fIBinRel1\fR\&\&. .LP .nf 1> R1 = sofs:relation([{1,1},{1,2},{2,1},{2,2}]), R2 = sofs:strict_relation(R1), sofs:to_external(R2)\&. [{1,2},{2,1}] .fi .RE .LP .nf .B substitution(SetFun, Set1) -> Set2 .br .fi .br .RS .LP Types: .RS 3 SetFun = set_fun() .br Set1 = Set2 = a_set() .br .RE .RE .RS .LP Returns a function, the domain of which is \fISet1\fR\&\&. The value of an element of the domain is the result of applying \fISetFun\fR\& to the element\&. .LP .nf 1> L = [{a,1},{b,2}]\&. [{a,1},{b,2}] 2> sofs:to_external(sofs:projection(1,sofs:relation(L)))\&. [a,b] 3> sofs:to_external(sofs:substitution(1,sofs:relation(L)))\&. [{{a,1},a},{{b,2},b}] 4> SetFun = {external, fun({A,_}=E) -> {E,A} end}, sofs:to_external(sofs:projection(SetFun,sofs:relation(L)))\&. [{{a,1},a},{{b,2},b}] .fi .LP The relation of equality between the elements of {a,b,c}: .LP .nf 1> I = sofs:substitution(fun(A) -> A end, sofs:set([a,b,c])), sofs:to_external(I)\&. [{a,a},{b,b},{c,c}] .fi .LP Let \fISetOfSets\fR\& be a set of sets and \fIBinRel\fR\& a binary relation\&. The function that maps each element \fISet\fR\& of \fISetOfSets\fR\& onto the image of \fISet\fR\& under \fIBinRel\fR\& is returned by the following function: .LP .nf images(SetOfSets, BinRel) -> Fun = fun(Set) -> sofs:image(BinRel, Set) end, sofs:substitution(Fun, SetOfSets). .fi .LP External unordered sets are represented as sorted lists\&. So, creating the image of a set under a relation R can traverse all elements of R (to that comes the sorting of results, the image)\&. In \fIimage/2\fR\&, \fIBinRel\fR\& is traversed once for each element of \fISetOfSets\fR\&, which can take too long\&. The following efficient function can be used instead under the assumption that the image of each element of \fISetOfSets\fR\& under \fIBinRel\fR\& is non-empty: .LP .nf images2(SetOfSets, BinRel) -> CR = sofs:canonical_relation(SetOfSets), R = sofs:relative_product1(CR, BinRel), sofs:relation_to_family(R). .fi .RE .LP .nf .B symdiff(Set1, Set2) -> Set3 .br .fi .br .RS .LP Types: .RS 3 Set1 = Set2 = Set3 = a_set() .br .RE .RE .RS .LP Returns the symmetric difference (or the Boolean sum) of \fISet1\fR\& and \fISet2\fR\&\&. .LP .nf 1> S1 = sofs:set([1,2,3]), S2 = sofs:set([2,3,4]), P = sofs:symdiff(S1, S2), sofs:to_external(P)\&. [1,4] .fi .RE .LP .nf .B symmetric_partition(Set1, Set2) -> {Set3, Set4, Set5} .br .fi .br .RS .LP Types: .RS 3 Set1 = Set2 = Set3 = Set4 = Set5 = a_set() .br .RE .RE .RS .LP Returns a triple of sets: .RS 2 .TP 2 * \fISet3\fR\& contains the elements of \fISet1\fR\& that do not belong to \fISet2\fR\&\&. .LP .TP 2 * \fISet4\fR\& contains the elements of \fISet1\fR\& that belong to \fISet2\fR\&\&. .LP .TP 2 * \fISet5\fR\& contains the elements of \fISet2\fR\& that do not belong to \fISet1\fR\&\&. .LP .RE .RE .LP .nf .B to_external(AnySet) -> ExternalSet .br .fi .br .RS .LP Types: .RS 3 ExternalSet = external_set() .br AnySet = anyset() .br .RE .RE .RS .LP Returns the external set of an atomic, ordered, or unordered set\&. .RE .LP .nf .B to_sets(ASet) -> Sets .br .fi .br .RS .LP Types: .RS 3 ASet = a_set() | ordset() .br Sets = tuple_of(AnySet) | [AnySet] .br AnySet = anyset() .br .RE .RE .RS .LP Returns the elements of the ordered set \fIASet\fR\& as a tuple of sets, and the elements of the unordered set \fIASet\fR\& as a sorted list of sets without duplicates\&. .RE .LP .nf .B type(AnySet) -> Type .br .fi .br .RS .LP Types: .RS 3 AnySet = anyset() .br Type = type() .br .RE .RE .RS .LP Returns the type of an atomic, ordered, or unordered set\&. .RE .LP .nf .B union(SetOfSets) -> Set .br .fi .br .RS .LP Types: .RS 3 Set = a_set() .br SetOfSets = set_of_sets() .br .RE .RE .RS .LP Returns the union of the set of sets \fISetOfSets\fR\&\&. .RE .LP .nf .B union(Set1, Set2) -> Set3 .br .fi .br .RS .LP Types: .RS 3 Set1 = Set2 = Set3 = a_set() .br .RE .RE .RS .LP Returns the union of \fISet1\fR\& and \fISet2\fR\&\&. .RE .LP .nf .B union_of_family(Family) -> Set .br .fi .br .RS .LP Types: .RS 3 Family = family() .br Set = a_set() .br .RE .RE .RS .LP Returns the union of family \fIFamily\fR\&\&. .LP .nf 1> F = sofs:family([{a,[0,2,4]},{b,[0,1,2]},{c,[2,3]}]), S = sofs:union_of_family(F), sofs:to_external(S)\&. [0,1,2,3,4] .fi .RE .LP .nf .B weak_relation(BinRel1) -> BinRel2 .br .fi .br .RS .LP Types: .RS 3 BinRel1 = BinRel2 = binary_relation() .br .RE .RE .RS .LP Returns a subset S of the weak relation W corresponding to the binary relation \fIBinRel1\fR\&\&. Let F be the field of \fIBinRel1\fR\&\&. The subset S is defined so that x S y if x W y for some x in F and for some y in F\&. .LP .nf 1> R1 = sofs:relation([{1,1},{1,2},{3,1}]), R2 = sofs:weak_relation(R1), sofs:to_external(R2)\&. [{1,1},{1,2},{2,2},{3,1},{3,3}] .fi .RE .SH "SEE ALSO" .LP \fIdict(3erl)\fR\&, \fIdigraph(3erl)\fR\&, \fIorddict(3erl)\fR\&, \fIordsets(3erl)\fR\&, \fIsets(3erl)\fR\&