Skip to content

Commit 32d89c0

Browse files
committed
Add Java Module Override for SequencesExt!AllSubSeqs.
[Feature] Signed-off-by: Markus Alexander Kuppe <github.com@lemmster.de>
1 parent e6a3954 commit 32d89c0

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

modules/tlc2/overrides/SequencesExt.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,8 @@ public static Value removeFirstMatch(final Value s, final OpValue test) {
582582
public static Value Suffixes(final Value s) {
583583
final TupleValue seq = (TupleValue) s.toTuple();
584584
if (seq == null) {
585-
throw new EvalException(EC.TLC_MODULE_ARGUMENT_ERROR,
586-
new String[] { "first", "Suffixes", "sequence", Values.ppr(s.toString()) });
585+
throw new EvalException(EC.TLC_MODULE_ONE_ARGUMENT_ERROR,
586+
new String[] { "Suffixes", "sequence", Values.ppr(s.toString()) });
587587
}
588588

589589
final Value[] vals = new Value[seq.elems.length + 1];
@@ -605,4 +605,33 @@ public static Value Suffixes(final Value s) {
605605
// expensive: return new SetEnumValue(vals, false).normalize();
606606
return new SetEnumValue(vals, true);
607607
}
608+
609+
/*
610+
* AllSubSeqs(s) ==
611+
* { FoldFunction(Snoc, <<>>, [ i \in D |-> s[i] ]) : D \in SUBSET DOMAIN s }
612+
*/
613+
@TLAPlusOperator(identifier = "AllSubSeqs", module = "SequencesExt", warn = false)
614+
public static Value AllSubSeqs(final Value s) {
615+
final TupleValue seq = (TupleValue) s.toTuple();
616+
if (seq == null) {
617+
throw new EvalException(EC.TLC_MODULE_ONE_ARGUMENT_ERROR,
618+
new String[] { "AllSubSeqs", "sequence", Values.ppr(s.toString()) });
619+
}
620+
621+
final int n = seq.elems.length;
622+
final Value[] vals = new Value[(int) Math.pow(2, n)];
623+
624+
for (int i = 0; i < (1 << n); i++) {
625+
int k = 0;
626+
final Value[] subSeq = new Value[Long.bitCount(i)];
627+
for (int j = 0; j < n; j++) {
628+
if ((i & (1 << j)) != 0) {
629+
subSeq[k++] = seq.elems[j];
630+
}
631+
}
632+
vals[i] = new TupleValue(subSeq);
633+
}
634+
635+
return new SetEnumValue(vals, false);
636+
}
608637
}

tests/SequencesExtTests.tla

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ ASSUME AllSubSeqs(<<"a","c","b","d","b">>) =
243243
<<"c","b","d">>, <<"c","b","b">>, <<"b","d","b">>, <<"a","c","d","b">>, <<"a","c","b","d">>, <<"a","c","b","b">>, <<"a","b","d","b">>,
244244
<<"c","b","d","b">>, <<"a","c","b","d","b">> }
245245

246+
AllSubSeqsPure(s) ==
247+
{ FoldFunction(Snoc, <<>>, [ i \in D |-> s[i] ]) : D \in SUBSET DOMAIN s }
248+
249+
ASSUME \A s \in {<<>>, <<1>>, <<1,1>>, <<1,1,1>>, <<1,2,3,4>>, <<"a","c","b","d","b">>}:
250+
AllSubSeqs(s) = AllSubSeqsPure(s)
251+
246252
-----------------------------------------------------------------------------
247253

248254
ASSUME ReplaceFirstSubSeq(<<>>,<<>>,<<>>) = <<>>

0 commit comments

Comments
 (0)