Skip to content

Commit ec5c155

Browse files
add ToBag operator
1 parent ff09fe4 commit ec5c155

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

modules/SequencesExt.tla

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---------------------------- MODULE SequencesExt ----------------------------
2-
EXTENDS Sequences, Naturals, FiniteSets, FiniteSetsExt, Folds, Functions
2+
EXTENDS Sequences, Naturals, FiniteSets, FiniteSetsExt, Folds, Functions, Bags
33
\* TLAPM does not play well with LOCAL INSTANCE, reinstate the following
44
\* when that issue is fixed.
55
\* LOCAL INSTANCE Sequences
@@ -8,6 +8,7 @@ EXTENDS Sequences, Naturals, FiniteSets, FiniteSetsExt, Folds, Functions
88
\* LOCAL INSTANCE FiniteSetsExt
99
\* LOCAL INSTANCE Folds
1010
\* LOCAL INSTANCE Functions
11+
\* LOCAL INSTANCE Bags
1112
LOCAL INSTANCE TLC
1213
(*************************************************************************)
1314
(* Imports the definitions from the modules, but doesn't export them. *)
@@ -33,6 +34,12 @@ LOCAL INSTANCE TLC
3334
ToSet(s) ==
3435
{ s[i] : i \in DOMAIN s }
3536

37+
(**************************************************************************)
38+
(* Convert a sequence to the bag of its elements. *)
39+
(**************************************************************************)
40+
ToBag(s) ==
41+
[x \in Range(s) |-> Cardinality({i \in DOMAIN s : s[i] = x})]
42+
3643
(**************************************************************************)
3744
(* Convert a set to some sequence that contains all the elements of the *)
3845
(* set exactly once, and contains no other elements. *)

tests/SequencesExtTests.tla

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ ASSUME(ToSet([i \in 1..10 |-> i]) = 1..10)
1212
ASSUME(ToSet(Tail([i \in 1..10 |-> i])) = 2..10)
1313
ASSUME(ToSet([i \in 0..9 |-> 42]) = {42})
1414

15+
ASSUME(ToBag(<<>>) = <<>>)
16+
ASSUME(ToBag(<<1,2,1>>) = (1 :> 2) @@ (2 :> 1))
17+
ASSUME(ToBag([i \in 1..10 |-> 42]) = (42 :> 10))
18+
1519
ASSUME(SetToSeq({}) = <<>>)
1620
ASSUME(SetToSeq({1}) = <<1>>)
1721
ASSUME(LET s == {"t","l","a","p","l","u","s"}

0 commit comments

Comments
 (0)