[Next][Prev] [_____] [Left] [Up] [Index] [Root]

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 = { a1, a2, ..., an } of elements belonging to an algebraic structure U, and an (associative) operator : U x U -> U, form the element ai1 ai2 ai3 ... ain, for some permutation i1, ..., in 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 a1, ..., an, the result is not unambiguously defined if the operation is not commutative on the arguments!

Example Set_Reduction (H9E14)

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]

Version: V2.19 of Mon Dec 17 14:40:36 EST 2012