/* 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 exportsfunctions sum: setofreal +> real
prod: setofreal +> real
min: setofreal +> real
max: setofreal +> real
toSeq[@a]: setof @a +> seqof @a
xform[@a,@b]: (@a +> @b) * setof @a +> setof @b
fold[@a]: (@a * @a +> @a) * @a * setof @a +> @a
fold1[@a]: (@a * @a +> @a) * setof @a +> @a
pairwiseDisjoint[@a]: setofsetof @a +> bool
isPartition[@a]: setofsetof @a * setof @a +> bool
permutations[@a]: setof @a +> setofseq1of @a
xProduct[@a,@b]: setof @a * setof @b +> setof (@a * @b)
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 of numerics.
min: setofreal +> real
min(s) == fold1[real](Numeric`min, s) pre s <> {} postRESULTinset s andforall e inset s & RESULT <= e;
-- The maximum of a set of numerics.
max: setofreal +> real
max(s) == fold1[real](Numeric`max, s) pre 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 andforall e inset s & Seq`inSeq[@a](e,RESULT);
-- 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);
-- 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 (exists x:@a & forall y:@a & f(x,y) = y and f(y,x) = y) --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) * setof @a +> @a
fold1(f, s) == cases s:
{e} -> e,
t union u -> f(fold1[@a](f,t), fold1[@a](f,u)) end pre s <> {} --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 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]: setof @a +> setofseq1of @a
permutations(s) == cases s:
{e} -> {[e]},
- -> dunion { { [e]^tail | tail inset permutations[@a](s\{e}) } | e inset s } end pre s <> {} 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 size;
-- 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;
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.