Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion modules/SequencesExt.tla
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---------------------------- MODULE SequencesExt ----------------------------
EXTENDS Sequences, Naturals, FiniteSets, FiniteSetsExt, Folds, Functions
EXTENDS Sequences, Naturals, FiniteSets, FiniteSetsExt, Folds, Functions, Bags
\* TLAPM does not play well with LOCAL INSTANCE, reinstate the following
\* when that issue is fixed.
\* LOCAL INSTANCE Sequences
Expand All @@ -8,6 +8,7 @@ EXTENDS Sequences, Naturals, FiniteSets, FiniteSetsExt, Folds, Functions
\* LOCAL INSTANCE FiniteSetsExt
\* LOCAL INSTANCE Folds
\* LOCAL INSTANCE Functions
\* LOCAL INSTANCE Bags
LOCAL INSTANCE TLC
(*************************************************************************)
(* Imports the definitions from the modules, but doesn't export them. *)
Expand All @@ -33,6 +34,13 @@ LOCAL INSTANCE TLC
ToSet(s) ==
{ s[i] : i \in DOMAIN s }

(**************************************************************************)
(* Convert a sequence to the bag (i.e. multi-set) of its elements. *)
(* Cf. standard module Bags.tla and community module BagsExt.tla. *)
(**************************************************************************)
ToBag(s) ==
[x \in Range(s) |-> Cardinality({i \in DOMAIN s : s[i] = x})]

(**************************************************************************)
(* Convert a set to some sequence that contains all the elements of the *)
(* set exactly once, and contains no other elements. *)
Expand Down
4 changes: 4 additions & 0 deletions tests/SequencesExtTests.tla
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ ASSUME(ToSet([i \in 1..10 |-> i]) = 1..10)
ASSUME(ToSet(Tail([i \in 1..10 |-> i])) = 2..10)
ASSUME(ToSet([i \in 0..9 |-> 42]) = {42})

ASSUME(ToBag(<<>>) = <<>>)
ASSUME(ToBag(<<1,2,1>>) = (1 :> 2) @@ (2 :> 1))
ASSUME(ToBag([i \in 1..10 |-> 42]) = (42 :> 10))

ASSUME(SetToSeq({}) = <<>>)
ASSUME(SetToSeq({1}) = <<1>>)
ASSUME(LET s == {"t","l","a","p","l","u","s"}
Expand Down
Loading