Skip to content

Commit 9017c0d

Browse files
Improve compatibility of CommunityModules and TLAPS. (#28)
- Add SequencesExt!Cons operator - Move SequencesExt!IsInjective to Functions!IsInjective - Move SequencesExtTheorems* to TLAPS distribution [Refactor]
1 parent d613a96 commit 9017c0d

11 files changed

Lines changed: 124 additions & 190 deletions

.github/workflows/main.yml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,6 @@ jobs:
2020
- name: Get current date
2121
id: date
2222
run: echo "::set-output name=date::$(date +'%Y%m%d%H%M')"
23-
# Do not download and install TLAPS over and over again.
24-
- uses: actions/cache@v1
25-
id: cache
26-
with:
27-
path: tlaps/
28-
key: tlaps1.4.5
29-
- name: Get TLAPS
30-
if: steps.cache.outputs.cache-hit != 'true' # see actions/cache above
31-
run: wget https://tla.msr-inria.inria.fr/tlaps/dist/current/tlaps-1.4.5-x86_64-linux-gnu-inst.bin
32-
- name: Install TLAPS
33-
if: steps.cache.outputs.cache-hit != 'true' # see actions/cache above
34-
run: |
35-
chmod +x tlaps-1.4.5-x86_64-linux-gnu-inst.bin
36-
./tlaps-1.4.5-x86_64-linux-gnu-inst.bin -d tlaps
37-
- name: Run TLAPS
38-
run: tlaps/bin/tlapm --cleanfp -I tlaps/ -I modules/ modules/SequencesExtTheorems_proofs.tla
3923
- name: Build with Ant
4024
run: ant -noinput -buildfile build.xml -Dtimestamp=${{steps.date.outputs.date}}
4125
- name: Copied CommunityModules-SomeDate.jar to CommunityModules.jar

modules/Functions.tla

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,18 @@ Inverse(f,S,T) == [t \in T |-> CHOOSE s \in S : t \in Range(f) => f[s] = t]
2727

2828

2929
(***************************************************************************)
30-
(* A map is an injection iff each element in the domain maps to a distinct *)
31-
(* element in the range. *)
30+
(* A function is injective iff it maps each element in its domain to a *)
31+
(* distinct element. *)
32+
(* *)
33+
(* This definition is overridden by TLC in the Java class SequencesExt. *)
34+
(* The operator is overridden by the Java method with the same name. *)
3235
(***************************************************************************)
33-
Injection(S,T) == { M \in [S -> T] : \A a,b \in S : M[a] = M[b] => a = b }
36+
IsInjective(f) == \A a,b \in DOMAIN f : f[a] = f[b] => a = b
37+
38+
(***************************************************************************)
39+
(* Set of injections between two sets. *)
40+
(***************************************************************************)
41+
Injection(S,T) == { M \in [S -> T] : IsInjective(M) }
3442

3543

3644
(***************************************************************************)
@@ -57,7 +65,7 @@ ExistsBijection(S,T) == Bijection(S,T) # {}
5765

5866
=============================================================================
5967
\* Modification History
60-
\* Last modified Wed Jul 10 20:32:37 CEST 2013 by merz
68+
\* Last modified Sun Dec 27 09:38:06 CET 2020 by merz
6169
\* Last modified Wed Jun 05 12:14:19 CEST 2013 by bhargav
6270
\* Last modified Fri May 03 12:55:35 PDT 2013 by tomr
6371
\* Created Thu Apr 11 10:30:48 PDT 2013 by tomr

modules/SequencesExt.tla

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,6 @@ ToSet(s) ==
1818
(*************************************************************************)
1919
{ s[i] : i \in DOMAIN s }
2020

21-
IsInjective(s) ==
22-
(*************************************************************************)
23-
(* TRUE iff the sequence s contains no duplicates where two elements *)
24-
(* a, b of s are defined to be duplicates iff a = b. In other words, *)
25-
(* Cardinality(ToSet(s)) = Len(s) *)
26-
(* *)
27-
(* This definition is overridden by TLC in the Java class SequencesExt. *)
28-
(* The operator is overridden by the Java method with the same name. *)
29-
(* *)
30-
(* Also see Functions!Injective operator. *)
31-
(*************************************************************************)
32-
\A i, j \in DOMAIN s: (s[i] = s[j]) => (i = j)
33-
3421
SetToSeq(S) ==
3522
(**************************************************************************)
3623
(* Convert a set to some sequence that contains all the elements of the *)
@@ -121,6 +108,12 @@ RemoveAt(s, i) ==
121108

122109
-----------------------------------------------------------------------------
123110

111+
Cons(elt, seq) ==
112+
(***************************************************************************)
113+
(* Cons prepends an element at the beginning of a sequence. *)
114+
(***************************************************************************)
115+
<<elt>> \o seq
116+
124117
Front(s) ==
125118
(**************************************************************************)
126119
(* The sequence formed by removing its last element. *)

modules/SequencesExtTheorems.tla

Lines changed: 0 additions & 27 deletions
This file was deleted.

modules/SequencesExtTheorems_proofs.tla

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 Microsoft Research. All rights reserved.
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10+
* of the Software, and to permit persons to whom the Software is furnished to do
11+
* so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20+
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*
23+
* Contributors:
24+
* Markus Alexander Kuppe - initial API and implementation
25+
******************************************************************************/
26+
package tlc2.overrides;
27+
28+
import java.util.Arrays;
29+
30+
import tlc2.output.EC;
31+
import tlc2.tool.EvalException;
32+
import tlc2.value.Values;
33+
import tlc2.value.impl.BoolValue;
34+
import tlc2.value.impl.SetEnumValue;
35+
import tlc2.value.impl.TupleValue;
36+
import tlc2.value.impl.Value;
37+
38+
public final class Functions {
39+
40+
private Functions() {
41+
// no-instantiation!
42+
}
43+
44+
@TLAPlusOperator(identifier = "IsInjective", module = "Functions", warn = false)
45+
public static BoolValue IsInjective(final Value val) {
46+
if (val instanceof TupleValue) {
47+
return isInjectiveNonDestructive(((TupleValue) val).elems);
48+
} else {
49+
final Value conv = val.toTuple();
50+
if (conv == null) {
51+
throw new EvalException(EC.TLC_MODULE_ONE_ARGUMENT_ERROR,
52+
new String[] { "IsInjective", "sequence", Values.ppr(val.toString()) });
53+
}
54+
return isInjectiveDestructive(((TupleValue) conv).elems);
55+
}
56+
}
57+
58+
// O(n log n) runtime and O(1) space.
59+
private static BoolValue isInjectiveDestructive(final Value[] values) {
60+
Arrays.sort(values);
61+
for (int i = 1; i < values.length; i++) {
62+
if (values[i-1].equals(values[i])) {
63+
return BoolValue.ValFalse;
64+
}
65+
}
66+
return BoolValue.ValTrue;
67+
}
68+
69+
// Assume small arrays s.t. the naive approach with O(n^2) runtime but O(1)
70+
// space is good enough. Sorting values in-place is a no-go because it
71+
// would modify the TLA+ tuple. Elements can be any sub-type of Value, not
72+
// just IntValue.
73+
private static BoolValue isInjectiveNonDestructive(final Value[] values) {
74+
for (int i = 0; i < values.length; i++) {
75+
for (int j = i + 1; j < values.length; j++) {
76+
if (values[i].equals(values[j])) {
77+
return BoolValue.ValFalse;
78+
}
79+
}
80+
}
81+
return BoolValue.ValTrue;
82+
}
83+
}

modules/tlc2/overrides/SequencesExt.java

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
* Contributors:
2525
* Markus Alexander Kuppe - initial API and implementation
2626
******************************************************************************/
27-
import java.util.Arrays;
2827

2928
import tlc2.output.EC;
3029
import tlc2.tool.EvalException;
@@ -40,46 +39,6 @@ private SequencesExt() {
4039
// no-instantiation!
4140
}
4241

43-
@TLAPlusOperator(identifier = "IsInjective", module = "SequencesExt", warn = false)
44-
public static BoolValue IsInjective(final Value val) {
45-
if (val instanceof TupleValue) {
46-
return isInjectiveNonDestructive(((TupleValue) val).elems);
47-
} else {
48-
final Value conv = val.toTuple();
49-
if (conv == null) {
50-
throw new EvalException(EC.TLC_MODULE_ONE_ARGUMENT_ERROR,
51-
new String[] { "IsInjective", "sequence", Values.ppr(val.toString()) });
52-
}
53-
return isInjectiveDestructive(((TupleValue) conv).elems);
54-
}
55-
}
56-
57-
// O(n log n) runtime and O(1) space.
58-
private static BoolValue isInjectiveDestructive(final Value[] values) {
59-
Arrays.sort(values);
60-
for (int i = 1; i < values.length; i++) {
61-
if (values[i-1].equals(values[i])) {
62-
return BoolValue.ValFalse;
63-
}
64-
}
65-
return BoolValue.ValTrue;
66-
}
67-
68-
// Assume small arrays s.t. the naive approach with O(n^2) runtime but O(1)
69-
// space is good enough. Sorting values in-place is a no-go because it
70-
// would modify the TLA+ tuple. Elements can be any sub-type of Value, not
71-
// just IntValue.
72-
private static BoolValue isInjectiveNonDestructive(final Value[] values) {
73-
for (int i = 0; i < values.length; i++) {
74-
for (int j = i + 1; j < values.length; j++) {
75-
if (values[i].equals(values[j])) {
76-
return BoolValue.ValFalse;
77-
}
78-
}
79-
}
80-
return BoolValue.ValTrue;
81-
}
82-
8342
/*
8443
* Improve carbon footprint of SequencesExt!SetToSeq. Convert SetEnumValue to
8544
* TupleValue (linear in the number of elements) instead of generating the set

modules/tlc2/overrides/TLCOverrides.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public class TLCOverrides implements ITLCOverrides {
3434
public Class[] get() {
3535
try {
3636
Json.resolves();
37-
return new Class[] { TLCExt.class, IOUtils.class, SVG.class, SequencesExt.class, Json.class, Bitwise.class, FiniteSetsExt.class };
37+
return new Class[] { TLCExt.class, IOUtils.class, SVG.class, SequencesExt.class, Json.class, Bitwise.class, FiniteSetsExt.class, Functions.class };
3838
} catch (NoClassDefFoundError e) {
3939
System.out.println(
4040
"com.fasterxml.jackson dependencies of Json overrides not found, Json module won't work unless "
4141
+ "the libraries in the lib/ folder of the CommunityModules have been added to the classpath of TLC.");
4242
}
43-
return new Class[] { TLCExt.class, IOUtils.class, SVG.class, SequencesExt.class, Bitwise.class, FiniteSetsExt.class };
43+
return new Class[] { TLCExt.class, IOUtils.class, SVG.class, SequencesExt.class, Bitwise.class, FiniteSetsExt.class, Functions.class };
4444
}
4545
}

tests/AllTests.tla

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ EXTENDS SequencesExtTests,
99
JsonTests,
1010
BitwiseTests,
1111
IOUtilsTests,
12-
FiniteSetsExtTests
12+
FiniteSetsExtTests,
13+
FunctionsTests
1314

1415
===========================================

tests/FunctionsTests.tla

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
------------------------- MODULE FunctionsTests -------------------------
2+
EXTENDS Functions, Naturals, TLC, TLCExt, FiniteSets
3+
4+
ASSUME(IsInjective(<<>>))
5+
ASSUME(IsInjective(<<1>>))
6+
ASSUME(IsInjective(<<1,2,3>>))
7+
ASSUME(~IsInjective(<<1,1>>))
8+
ASSUME(~IsInjective(<<1,1,2,3>>))
9+
10+
ASSUME(IsInjective([i \in 1..10 |-> i]))
11+
ASSUME(IsInjective([i \in 1..10 |-> {i}]))
12+
ASSUME(IsInjective([i \in 1..10 |-> {i}]))
13+
ASSUME(~IsInjective([i \in 1..10 |-> {1,2,3}]))
14+
15+
ASSUME(AssertError("The argument of IsInjective should be a sequence, but instead it is:\n{}", IsInjective({})))
16+
ASSUME(AssertError("The argument of IsInjective should be a sequence, but instead it is:\n[a: 1, b: 2]", IsInjective([a: 1, b: 2])))
17+
ASSUME(AssertError("The argument of IsInjective should be a sequence, but instead it is:\n(0 :> 0 @@ 1 :> 1 @@ 2 :> 2)", IsInjective([i \in 0..2 |-> i])))
18+
19+
=============================================================================

0 commit comments

Comments
 (0)