Skip to content

Commit

Permalink
Rename operator IsASet to IsInjective (in TLA+ all things are sets).
Browse files Browse the repository at this point in the history
  • Loading branch information
lemmy committed Jun 28, 2019
1 parent 10b4f35 commit 9c9c445
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
10 changes: 5 additions & 5 deletions SequencesExt.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ private SequencesExt() {
// no-instantiation!
}

public static BoolValue IsASet(final Value val) {
public static BoolValue IsInjective(final Value val) {
if (val instanceof TupleValue) {
return isASetNonDestructive(((TupleValue) val).elems);
return isInjectiveNonDestructive(((TupleValue) val).elems);
} else {
final Value conv = val.toTuple();
if (conv == null) {
throw new EvalException(EC.TLC_MODULE_ONE_ARGUMENT_ERROR,
new String[] { "IsASet", "sequence", Values.ppr(val.toString()) });
}
return isASetDestructive(((TupleValue) conv).elems);
return isInjectiveDestructive(((TupleValue) conv).elems);
}
}

// O(n log n) runtime and O(1) space.
private static BoolValue isASetDestructive(final Value[] values) {
private static BoolValue isInjectiveDestructive(final Value[] values) {
Arrays.sort(values);
for (int i = 1; i < values.length; i++) {
if (values[i-1].equals(values[i])) {
Expand All @@ -66,7 +66,7 @@ private static BoolValue isASetDestructive(final Value[] values) {
// space is good enough. Sorting values in-place is a no-go because it
// would modify the TLA+ tuple. Elements can be any sub-type of Value, not
// just IntValue.
private static BoolValue isASetNonDestructive(final Value[] values) {
private static BoolValue isInjectiveNonDestructive(final Value[] values) {
for (int i = 0; i < values.length; i++) {
for (int j = i + 1; j < values.length; j++) {
if (values[i].equals(values[j])) {
Expand Down
7 changes: 3 additions & 4 deletions SequencesExt.tla
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ ToSet(s) ==
(*************************************************************************)
{ s[i] : i \in 1..Len(s) }

IsASet(s) ==
IsInjective(s) ==
(*************************************************************************)
(* TRUE iff the sequence s contains no duplicates where two elements *)
(* a, b of s are defined to be duplicates iff a = b. In other words, *)
(* Cardinality(ToSet(s)) = Len(s) *)
(* *)
(* This definition is overridden by TLC in the Java class *)
(* tlc2.module.SequencesExt. The operator is overridden by the Java *)
(* method with the same name. *)
(* This definition is overridden by TLC in the Java class SequencesExt. *)
(* The operator is overridden by the Java method with the same name. *)
(*************************************************************************)
\A i,j \in 1..Len(s): i # j => s[i] # s[j]

Expand Down
18 changes: 9 additions & 9 deletions SequencesExtTests.tla
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
------------------------- MODULE SequencesExtTests -------------------------
EXTENDS Sequences, SequencesExt, Naturals, TLC

ASSUME(IsASet(<<>>))
ASSUME(IsASet(<<1>>))
ASSUME(IsASet(<<1,2,3>>))
ASSUME(~IsASet(<<1,1>>))
ASSUME(~IsASet(<<1,1,2,3>>))
ASSUME(IsInjective(<<>>))
ASSUME(IsInjective(<<1>>))
ASSUME(IsInjective(<<1,2,3>>))
ASSUME(~IsInjective(<<1,1>>))
ASSUME(~IsInjective(<<1,1,2,3>>))

ASSUME(IsASet([i \in 1..10 |-> i]))
ASSUME(IsASet([i \in 1..10 |-> {i}]))
ASSUME(IsASet([i \in 1..10 |-> {i}]))
ASSUME(~IsASet([i \in 1..10 |-> {1,2,3}]))
ASSUME(IsInjective([i \in 1..10 |-> i]))
ASSUME(IsInjective([i \in 1..10 |-> {i}]))
ASSUME(IsInjective([i \in 1..10 |-> {i}]))
ASSUME(~IsInjective([i \in 1..10 |-> {1,2,3}]))

=============================================================================

0 comments on commit 9c9c445

Please sign in to comment.