/* A module that specifies and defines general purpose functions over sets.
All functions are explicit and executable. Where a non-executable condition adds value, it is included as a comment.
*/ moduleSet importsfrom Numeric all, fromSeqall, from Ord all exportsfunctions sum: setofreal +> real;
prod: setofreal +> real;
min[@a]: set1 of @a +> @a;
minWith[@a]: (@a * @a +> bool) +> set1 of @a +> @a;
max[@a]: set1 of @a +> @a;
maxWith[@a]: (@a * @a +> bool) +> set1 of @a +> @a;
toSeq[@a]: setof @a +> seqof @a;
xform[@a,@b]: (@a +> @b) * setof @a +> setof @b;
filter[@a]: (@a +> bool) +> setof @a +> setof @a;
fold[@a]: (@a * @a +> @a) * @a * setof @a +> @a;
fold1[@a]: (@a * @a +> @a) * set1 of @a +> @a;
pairwiseDisjoint[@a]: setofsetof @a +> bool;
isPartition[@a]: setofsetof @a * setof @a +> bool;
permutations[@a]: set1 of @a +> set1 ofseq1of @a;
xProduct[@a,@b]: setof @a * setof @b +> setof (@a * @b);
format[@a]: (@a +> seqofchar) * seqofchar * setof @a +> seqofchar
definitions
functions
-- The sum of a set of numerics.
sum: setofreal +> real
sum(s) == fold[real](Numeric`add,0,s);
-- The product of a set of numerics.
prod: setofreal +> real
prod(s) == fold[real](Numeric`mult,1,s);
-- The minimum of a set.
min[@a]: set1 of @a +> @a
min(s) == fold1[@a](Ord`min[@a], s) -- pre Type argument @a admits an order relation. postRESULTinset s andforall e inset s & RESULT <= e;
-- The minimum of a set with respect to a relation.
minWith[@a]: (@a * @a +> bool) +> set1 of @a +> @a
minWith(o)(s) == fold1[@a](Ord`minWith[@a](o), s) postRESULTinset s andforall e inset s & RESULT <= e;
-- The maximum of a set.
max[@a]: set1 of @a +> @a
max(s) == fold1[@a](Ord`max[@a], s) -- pre Type argument @a admits an order relation. postRESULTinset s andforall e inset s & RESULT >= e;
-- The maximum of a set with respect to a relation.
maxWith[@a]: (@a * @a +> bool) +> set1 of @a +> @a
maxWith(o)(s) == fold1[@a](Ord`maxWith[@a](o), s) postRESULTinset s andforall e inset s & RESULT >= e;
-- The sequence whose elements are those of a specified set, with no duplicates. -- No order is guaranteed in the resulting sequence.
toSeq[@a]: setof @a +> seqof @a
toSeq(s) == cases s:
{} -> [],
{x} -> [x],
t union u -> toSeq[@a](t) ^ toSeq[@a](u) end postlenRESULT = card s andelemsRESULT = s measure size; /* A simpler definition would be toSeq(s) == [ x | x in set s ] This would however assume an order relation on the argument type @a.
*/
-- Apply a function to all elements of a set. The result set may be smaller than the -- argument set if the function argument is not injective.
xform[@a,@b]: (@a+>@b) * setof @a +> setof @b
xform(f,s) == { f(e) | e inset s } post (forall e inset s & f(e) insetRESULT) and
(forall r insetRESULT & exists e inset s & f(e) = r);
-- Filter those elements of a set that satisfy a predicate.
filter[@a]: (@a +> bool) +> setof @a +> setof @a
filter(p)(s) == { x | x inset s & p(x) } post (forall x insetRESULT & p(x)) and (forall x inset s \ RESULT & not p(x));
-- Fold (iterate, accumulate, reduce) a binary function over a set. -- The function is assumed to be commutative and associative, and have an identity element.
fold[@a]: (@a * @a +> @a) * @a * setof @a +> @a
fold(f, e, s) == cases s:
{} -> e,
{x} -> x,
t union u -> f(fold[@a](f,e,t), fold[@a](f,e,u)) end --pre (forall x:@a & f(x,e) = x and f(e,x) = x) --and (forall x,y:@a & f(x, y) = f(y, x)) --and (forall x,y,z:@a & f(x,f(y,z)) = f(f(x,y),z)) measure size2;
-- Fold (iterate, accumulate, reduce) a binary function over a non-empty set. -- The function is assumed to be commutative and associative.
fold1[@a]: (@a * @a +> @a) * set1 of @a +> @a
fold1(f, s) == cases s:
{e} -> e,
t union u -> f(fold1[@a](f,t), fold1[@a](f,u)) end --pre (forall x,y:@a & f(x,y) = f(y,x)) --and (forall x,y,z:@a & f(x,f(y,z)) = f(f(x,y),z)) measure size1;
-- Are the members of a set of sets pairwise disjoint.
pairwiseDisjoint[@a]: setofsetof @a +> bool
pairwiseDisjoint(ss) == forall x,y inset ss & x<>y => x inter y = {};
-- Is a set of sets a partition of a set?
isPartition[@a]: setofsetof @a * setof @a +> bool
isPartition(ss,s) == pairwiseDisjoint[@a](ss) anddunion ss = s;
-- All (sequence) permutations of a set.
permutations[@a]: set1 of @a +> set1 ofseq1of @a
permutations(s) == cases s:
{e} -> {[e]},
- -> dunion { { [e]^tail | tail inset permutations[@a](s\{e}) } | e inset s } end post-- for a set of size n, there are n! permutations cardRESULT = prod({1,...,card s}) and forall sq insetRESULT & len sq = card s andelems sq = s measure size0;
-- The cross product of two sets.
xProduct[@a,@b]: setof @a * setof @b +> setof (@a * @b)
xProduct(s,t) == { mk_(x,y) | x inset s, y inset t } postcardRESULT = card s * card t;
-- Create a string presentation of a set.
format[@a]: (@a +> seqofchar) * seqofchar * setof @a +> seqofchar
format(f,sep,s) == cases s:
{} -> "",
{x} -> f(x),
t union u -> format[@a](f,sep,t) ^ sep ^ format[@a](f,sep,u) end measure size3;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.