Skip to content

Commit 857abc3

Browse files
committed
Delegate from SequencesExt!FoldSeq to Functions!FoldFunction instead
of `SequencesExt!FoldLeft`. The difference is that the base element is the second and not the first operand of the operation op(_,_). The (iteration) order is determined by the order of the sequence's (seq) domain, which is 1..Len(seq) in both cases. [Bug]
1 parent d32e013 commit 857abc3

File tree

3 files changed

+8
-27
lines changed

3 files changed

+8
-27
lines changed

modules/tlc2/overrides/Functions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public static Value foldFunction(final OpValue op, final Value base, final Appli
114114
@TLAPlusOperator(identifier = "FoldFunctionOnSet", module = "Functions", warn = false)
115115
public static Value foldFunctionOnSet(final OpValue op, final Value base, final Applicable fun, final Enumerable subdomain) {
116116

117+
// FoldFunction base is second operand.
117118
final Value[] args = new Value[2];
118119
args[1] = base;
119120

modules/tlc2/overrides/SequencesExt.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,12 @@
3737
import tlc2.util.Context;
3838
import tlc2.value.IValue;
3939
import tlc2.value.Values;
40-
import tlc2.value.impl.Applicable;
4140
import tlc2.value.impl.BoolValue;
42-
import tlc2.value.impl.Enumerable;
4341
import tlc2.value.impl.OpValue;
4442
import tlc2.value.impl.SetEnumValue;
4543
import tlc2.value.impl.StringValue;
4644
import tlc2.value.impl.TupleValue;
4745
import tlc2.value.impl.Value;
48-
import tlc2.value.impl.ValueEnumeration;
4946
import tlc2.value.impl.ValueVec;
5047

5148
public final class SequencesExt {
@@ -97,29 +94,6 @@ public String longestCommonPrefix(String[] strs) {
9794
}
9895
return prefix;
9996
}
100-
/*
101-
*/
102-
@TLAPlusOperator(identifier = "FoldFunction", module = "Functions", warn = false)
103-
public static Value foldFunction(final OpValue op, final Value base, final Applicable fun) {
104-
return foldFunctionOnSet(op, base, fun, (Enumerable) fun.getDomain());
105-
}
106-
107-
@TLAPlusOperator(identifier = "FoldFunctionOnSet", module = "Functions", warn = false)
108-
public static Value foldFunctionOnSet(final OpValue op, final Value base, final Applicable fun, final Enumerable subdomain) {
109-
110-
final Value[] args = new Value[2];
111-
args[1] = base;
112-
113-
final ValueEnumeration ve = subdomain.elements();
114-
115-
Value v = null;
116-
while ((v = ve.nextElement()) != null) {
117-
args[0] = fun.select(v);
118-
args[1] = op.apply(args, EvalControl.Clear);
119-
}
120-
121-
return args[1];
122-
}
12397

12498
@TLAPlusOperator(identifier = "LongestCommonPrefix", module = "SequencesExt", warn = false)
12599
public static Value longestCommonPrefix(final Value val) {
@@ -195,7 +169,7 @@ private static Value longestCommonPrefix(final StringValue first, final ValueVec
195169

196170
@TLAPlusOperator(identifier = "FoldSeq", module = "SequencesExt", warn = false)
197171
public static Value foldSeq(final OpValue op, final Value base, final TupleValue tv) {
198-
return foldLeft(op, base, tv);
172+
return Functions.foldFunction(op, base, tv);
199173
}
200174

201175
@TLAPlusOperator(identifier = "FoldLeft", module = "SequencesExt", warn = false)
@@ -207,6 +181,7 @@ public static Value foldLeft(final OpValue op, final Value base, final Value val
207181
new String[] { "FoldLeft", "sequence", Values.ppr(val.toString()) });
208182
}
209183

184+
// FoldLeft base is left (first) operand.
210185
final Value[] args = new Value[2];
211186
args[0] = base;
212187

@@ -228,6 +203,7 @@ public static Value foldRight(final OpValue op, final Value val, final Value bas
228203
new String[] { "FoldRight", "sequence", Values.ppr(val.toString()) });
229204
}
230205

206+
// FoldRight base is right (second) operand.
231207
final Value[] args = new Value[2];
232208
args[1] = base;
233209

tests/SequencesExtTests.tla

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ ASSUME FoldRight(-, [n \in 1..25 |-> n], 1) = 12
9999
ASSUME FoldRight(+, [n \in 1..250 |-> n], 1) = 31376
100100
ASSUME FoldRight(-, [n \in 1..250 |-> n], 1) = -124
101101

102+
ASSUME FoldSeq( LAMBDA e, acc: acc + 1, 0, <<1,1,1,1,1>>) = 5
103+
ASSUME FoldLeft( LAMBDA acc, e: acc + 1, 0, <<1,1,1,1,1>>) = 5
104+
ASSUME FoldRight(LAMBDA e, acc: acc + 1, <<1,1,1,1,1>>, 0) = 5
105+
102106
-----------------------------------------------------------------------------
103107

104108
ASSUME LongestCommonPrefix({<<>>}) = <<>>

0 commit comments

Comments
 (0)