Skip to content

Commit 3fb30f5

Browse files
committed
SequencesExt!Fold[Left|Right]Domain over domain of sequence.
#101 [Feature] Signed-off-by: Markus Alexander Kuppe <[email protected]>
1 parent 2cee5a3 commit 3fb30f5

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

modules/SequencesExt.tla

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,20 @@ FoldRight(op(_, _), seq, base) ==
332332
LAMBDA S : Min(S),
333333
DOMAIN seq)
334334

335+
FoldLeftDomain(op(_, _), base, seq) ==
336+
(***************************************************************************)
337+
(* FoldLeftDomain folds op on the domain of seq, i.e., the seq's indices, *)
338+
(* starting at the lowest index. *)
339+
(***************************************************************************)
340+
FoldLeft(op, base, [i \in DOMAIN seq |-> i])
341+
342+
FoldRightDomain(op(_, _), seq, base) ==
343+
(***************************************************************************)
344+
(* FoldRightDomain folds op on the domain of seq, i.e., the seq's indices, *)
345+
(* starting at the highest index. *)
346+
(***************************************************************************)
347+
FoldRight(op, [i \in DOMAIN seq |-> i], base)
348+
335349
-----------------------------------------------------------------------------
336350

337351
FlattenSeq(seqs) ==

modules/tlc2/overrides/SequencesExt.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,44 @@ public static Value foldRight(final OpValue op, final Value val, final Value bas
304304

305305
return args[1];
306306
}
307+
308+
@TLAPlusOperator(identifier = "FoldLeftDomain", module = "SequencesExt", warn = false)
309+
public static Value foldLeftDomain(final OpValue op, final Value base, final Value val) {
310+
final TupleValue tv = (TupleValue) val.toTuple();
311+
if (tv == null) {
312+
throw new EvalException(EC.TLC_MODULE_ARGUMENT_ERROR,
313+
new String[] { "third", "FoldLeftDomain", "sequence", Values.ppr(val.toString()) });
314+
}
315+
316+
final Value[] args = new Value[2];
317+
args[0] = base;
318+
319+
for (int i = 0; i < tv.size(); i++) {
320+
args[1] = IntValue.gen(i+1);
321+
args[0] = op.eval(args, EvalControl.Clear);
322+
}
323+
324+
return args[0];
325+
}
326+
327+
@TLAPlusOperator(identifier = "FoldRightDomain", module = "SequencesExt", warn = false)
328+
public static Value foldRightDomain(final OpValue op, final Value val, final Value base) {
329+
final TupleValue tv = (TupleValue) val.toTuple();
330+
if (tv == null) {
331+
throw new EvalException(EC.TLC_MODULE_ARGUMENT_ERROR,
332+
new String[] { "second", "FoldRightDomain", "sequence", Values.ppr(val.toString()) });
333+
}
334+
335+
final Value[] args = new Value[2];
336+
args[1] = base;
337+
338+
for (int i = tv.size() - 1; i >= 0; i--) {
339+
args[0] = IntValue.gen(i+1);
340+
args[1] = op.eval(args, EvalControl.Clear);
341+
}
342+
343+
return args[1];
344+
}
307345

308346
@Evaluation(definition = "ReplaceFirstSubSeq", module = "SequencesExt", warn = false, silent = true)
309347
public static Value replaceFirstSubSeq(final Tool tool, final ExprOrOpArgNode[] args, final Context c,

tests/SequencesExtTests.tla

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,8 @@ ASSUME AssertEq(RemoveFirstMatch(<<1,2>>, LAMBDA e: e = 1), <<2>>)
407407
ASSUME AssertEq(RemoveFirstMatch(<<1,2,1>>, LAMBDA e: e = 1), <<2,1>>)
408408
ASSUME AssertEq(RemoveFirstMatch(<<1,2,1,2>>, LAMBDA e: e = 2), <<1,1,2>>)
409409

410+
-----------------------------------------------------------------------------
411+
412+
ASSUME LET seq == <<"a","b","c","d","e">> IN AssertEq(FoldLeftDomain (LAMBDA acc, idx : acc \o seq[idx], "", seq), "abcde")
413+
ASSUME LET seq == <<"a","b","c","d","e">> IN AssertEq(FoldRightDomain(LAMBDA idx, acc : acc \o seq[idx], seq, ""), "edcba")
410414
=============================================================================

0 commit comments

Comments
 (0)