[Next] [Prev] [_____] [Left] [Up] [Index] [Root]
Reduction and Iteration over Sets

Reduction and Iteration over Sets

Both enumerated and indexed sets allow enumeration of their elements; formal sets do not. For indexed sets the enumeration will occur according to the order given by the indexing.

Instead of using a loop to apply the same binary associative operator to all elements of an enumerated or indexed set, it is in certain cases possible to use the reduction operator &.

x in S
Enumerate the elements of an enumerated or indexed set S. This can be used in loops, as well as in the set and sequence constructors.
& o S : Op, SetIndx -> Elt
& o S : Op, SetEnum -> Elt
Given an enumerated or indexed set S = { a_1, a_2, ..., a_n } of elements belonging to an algebraic structure U, and an (associative) operator o : U x U -> U, form the element a_(i_1) o a_(i_2) o a_(i_3) o ... o a_(i_n), for some permutation i_1, ..., i_n of 1, ..., n.

Currently, the following operators may be used to reduce enumerated sets: +, *, and, or, join, meet and +, *, and, or to reduce indexed sets. An error will occur if the operator is not defined on U.

If S contains a single element a, then the value returned is a. If S is the null set (empty and no universe specified) or S is empty with universe U (and the operation is defined in U), then the result (or error) depends on the operation and upon U. The following table defines the return value:

EMPTY NULL &+ U!0 error &* U!1 error &and true true &or false false &join empty null &meet error error

Warning: since the reduction may take place in an arbitrary order on the arguments a_1, ..., a_n, the result is not unambiguously defined if the operation is not commutative on the arguments!

Example Set_Reduction (H7E14)

The function choose defined below takes a set S and an integer k as input, and produces a set of all subsets of S with cardinality k.

> function choose(S, k)
>    if k eq 0 then
>       return { { } };
>    else
>       return &join{ { s join { x } : s in choose(S diff { x }, k-1) } : x in S };
>    end if;  
> end function;
So, for example:

>  S := { 1, 2, 3, 4 };   
> choose(S, 2);  
{ 
       { 1, 3 },
       { 1, 4 },
       { 2, 4 },
       { 2, 3 },
       { 1, 2 },
       { 3, 4 }
 }
Try to guess what happens if k < 0.
[Next] [Prev] [_____] [Left] [Up] [Index] [Root]