In math, a k-combination of an n-element set is a set of all sets that take k element of the n-element set.
However, how can I compute this in TLA+?
I don't know how to compute (n, k), due to my poor algorithm knowledge.
However, I find an ugly way that can compute (n, 2) by using cartesian product.
Suppose the n-element set is X, so the following CombinationSeq2(X) computes the cartesian product of X and X. If X is {1, 2}, then the result is {<<1,1>>, <<1,2>>, <<2,1>>, <<2,2>>}, so we must use s[1] < s[2] to filter repeated sets, thus yielding the final result {<<1,2>>}.
CombinationSeq2(X) == {s \in X \X X: s[1] < s[2]}
Then I convert inner tuple to set by the following
Combination2(X) == { { s[1], s[2] } : s \in CombinationSeq2(X) }
However, the above solution is ugly:
- it do not support arbitrary k.
- it requires element of the set to have order. However, we don't need order here, telling equal or not is already enough.
I wonder is there any solution to do this? I added algorithm tag to this question because I believe if TLA+ don't support this, there should be some algorithm way to do this. If so, I need an idea here, so I can translate them into TLA+.
If efficiency is not a concern, then something like
{s \in SUBSET X: CARDINALITY s = k}should be correct. (I say "something like" because I'm kind of guessing at the syntax. ButCARDINALITYis supposed to be inFiniteSet.)All reasonably efficient algorithms that I'm aware of do process the set in some kind of implicit order.