Skip to content

Commit 03b0838

Browse files
muenchnerkindllemmy
authored andcommitted
Add BagsExt module.
[Feature]
1 parent e081b9a commit 03b0838

File tree

4 files changed

+96
-8
lines changed

4 files changed

+96
-8
lines changed

modules/BagsExt.tla

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
------------------------------ MODULE BagsExt ------------------------------
2+
(***************************************************************************)
3+
(* Additional operators on bags (multisets). *)
4+
(***************************************************************************)
5+
6+
LOCAL INSTANCE Bags
7+
LOCAL INSTANCE Integers
8+
LOCAL INSTANCE Folds
9+
10+
BagAdd(B, x) ==
11+
(************************************************************************)
12+
(* Add an element x to bag B. *)
13+
(* Same as B (+) SetToBag({x}). *)
14+
(************************************************************************)
15+
IF x \in DOMAIN B
16+
THEN [e \in DOMAIN B |-> IF e=x THEN B[e]+1 ELSE B[e]]
17+
ELSE [e \in DOMAIN B \union {x} |-> IF e=x THEN 1 ELSE B[e]]
18+
19+
BagRemove(B,x) ==
20+
(************************************************************************)
21+
(* Remove an element x from bag B. *)
22+
(* Same as B (-) SetToBag({x}). *)
23+
(************************************************************************)
24+
IF x \in DOMAIN B
25+
THEN IF B[x] = 1
26+
THEN [e \in DOMAIN B \ {x} |-> B[e]]
27+
ELSE [e \in DOMAIN B |-> IF e=x THEN B[e]-1 ELSE B[e]]
28+
ELSE B
29+
30+
FoldBag(op(_,_), base, B) ==
31+
(************************************************************************)
32+
(* Fold op over all elements of bag B, starting with value base. *)
33+
(* *)
34+
(* Example: *)
35+
(* FoldBag(LAMBDA x,y : x+y, *)
36+
(* 0, *)
37+
(* (1 :> 2) @@ (2 :> 1) @@ (3 :> 2)) = 10 *)
38+
(* The third argument represents the bag {| 1, 1, 2, 3, 3 |}. *)
39+
(************************************************************************)
40+
LET repl(x) ==
41+
(* replicate operation op for all occurrences of x in B *)
42+
LET it[n \in Nat \ {0}] ==
43+
IF n = 1 THEN x ELSE op(x, it[n-1])
44+
IN it[B[x]]
45+
IN MapThenFoldSet(op, base, repl,
46+
LAMBDA S : CHOOSE x \in S : TRUE,
47+
DOMAIN B)
48+
49+
SumBag(B) ==
50+
(************************************************************************)
51+
(* Compute the sum of the elements of B. *)
52+
(************************************************************************)
53+
FoldBag(LAMBDA x,y : x+y, 0, B)
54+
55+
ProductBag(B) ==
56+
(************************************************************************)
57+
(* Compute the product of the elements of B. *)
58+
(************************************************************************)
59+
FoldBag(LAMBDA x,y : x*y, 1, B)
60+
61+
=============================================================================

modules/Folds.tla

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ MapThenFoldSet(op(_,_), base, f(_), choose(_), S) ==
1414
(* Example: *)
1515
(* *)
1616
(* MapThenFoldSet(LAMBDA x,y: x \cup y, *)
17-
(* {1,2}, *)
17+
(* {}, *)
1818
(* LAMBDA x: {{x}}, *)
1919
(* LAMBDA set: CHOOSE x \in set: TRUE, *)
20-
(* S) *)
20+
(* {1,2}) *)
2121
(* = {{1},{2}} *)
2222
(******************************************************************************)
2323
LET iter[s \in SUBSET S] ==
@@ -27,9 +27,4 @@ MapThenFoldSet(op(_,_), base, f(_), choose(_), S) ==
2727
IN iter[S]
2828

2929

30-
31-
3230
=============================================================================
33-
\* Modification History
34-
\* Last modified Fri Apr 02 13:54:18 CEST 2021 by marty
35-
\* Created Tue Mar 30 19:20:49 CEST 2021 by marty

tests/AllTests.tla

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ EXTENDS SequencesExtTests,
1414
CSVTests,
1515
CombinatoricsTests,
1616
FoldsTests,
17-
GraphsTests
17+
GraphsTests,
18+
BagsExtTests
1819

1920
===========================================

tests/BagsExtTests.tla

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---- CONFIG BagsExtTests ----
2+
INIT Init
3+
NEXT Next
4+
=====
5+
6+
---------------------------- MODULE BagsExtTests ----------------------------
7+
EXTENDS Integers, TLC, Bags, BagsExt
8+
9+
ASSUME LET T == INSTANCE TLC IN T!PrintT("BagsExtTests")
10+
11+
-----------------------------------------------------------------------------
12+
13+
ASSUME LET B == SetToBag({1,2}) IN BagAdd(B,2) = B (+) SetToBag({2})
14+
ASSUME LET B == SetToBag({1,2}) IN BagAdd(B,3) = B (+) SetToBag({3})
15+
ASSUME LET B == SetToBag({1,2}) IN BagRemove(B,2) = B (-) SetToBag({2})
16+
ASSUME LET B == SetToBag({1,2}) IN
17+
/\ BagRemove(B,3) = B (-) SetToBag({3})
18+
/\ BagRemove(B,3) = B
19+
20+
-----------------------------------------------------------------------------
21+
22+
ASSUME FoldBag(LAMBDA x,y : x+y, 0, (1:>2) @@ (2:>1) @@ (3:>2)) = 10
23+
ASSUME FoldBag(LAMBDA x,y : x+y, 0, (1:>2) @@ (-2:>1)) = 0
24+
25+
ASSUME SumBag((1:>2) @@ (2:>1) @@ (3:>2)) = 10
26+
ASSUME SumBag((1:>2) @@ (-2:>1)) = 0
27+
28+
ASSUME ProductBag((1:>2) @@ (-2:>1)) = -2
29+
ASSUME ProductBag((1:>2) @@ (0:>1) @@ (10000:>1)) = 0
30+
31+
=============================================================================

0 commit comments

Comments
 (0)