@@ -7,60 +7,60 @@ LOCAL INSTANCE Bags
7
7
LOCAL INSTANCE Integers
8
8
LOCAL INSTANCE Folds
9
9
10
+ (* ***********************************************************************)
11
+ (* Add an element x to bag B. *)
12
+ (* Same as B (+) SetToBag({x}). *)
13
+ (*********************************************************************** *)
10
14
BagAdd ( B , x ) ==
11
- (* ***********************************************************************)
12
- (* Add an element x to bag B. *)
13
- (* Same as B (+) SetToBag({x}). *)
14
- (*********************************************************************** *)
15
15
IF x \in DOMAIN B
16
16
THEN [ e \in DOMAIN B |-> IF e = x THEN B [ e ] + 1 ELSE B [ e ] ]
17
17
ELSE [ e \in DOMAIN B \union { x } |-> IF e = x THEN 1 ELSE B [ e ] ]
18
18
19
+ (* ***********************************************************************)
20
+ (* Remove an element x from bag B. *)
21
+ (* Same as B (-) SetToBag({x}). *)
22
+ (*********************************************************************** *)
19
23
BagRemove ( B , x ) ==
20
- (* ***********************************************************************)
21
- (* Remove an element x from bag B. *)
22
- (* Same as B (-) SetToBag({x}). *)
23
- (*********************************************************************** *)
24
24
IF x \in DOMAIN B
25
25
THEN IF B [ x ] = 1
26
26
THEN [ e \in DOMAIN B \ { x } |-> B [ e ] ]
27
27
ELSE [ e \in DOMAIN B |-> IF e = x THEN B [ e ] - 1 ELSE B [ e ] ]
28
28
ELSE B
29
29
30
+ (* ***********************************************************************)
31
+ (* Remove all copies of an element x from bag B. *)
32
+ (*********************************************************************** *)
30
33
BagRemoveAll ( B , x ) ==
31
- (* ***********************************************************************)
32
- (* Remove all copies of an element x from bag B. *)
33
- (*********************************************************************** *)
34
34
[ e \in DOMAIN B \ { x } |-> B [ e ] ]
35
35
36
+ (* **********************************************************************)
37
+ (* Fold operation op over the images through f of all elements of bag *)
38
+ (* B, starting from base. The parameter choose indicates the order in *)
39
+ (* which elements of the bag are processed; all replicas of an element *)
40
+ (* are processed consecutively. *)
41
+ (* *)
42
+ (* Examples: *)
43
+ (* MapThenFoldBag(LAMBDA x,y : x+y, *)
44
+ (* 0, *)
45
+ (* LAMBDA x: 1, *)
46
+ (* LAMBDA B : CHOOSE x \in DOMAIN B : TRUE, *)
47
+ (* (1 :> 2) @@ (2 :> 1) @@ (3 :> 3)) = 6 *)
48
+ (* *)
49
+ (* MapThenFoldBag(LAMBDA x,y : x \o y, *)
50
+ (* << >>, *)
51
+ (* LAMBDA x: << x >>, *)
52
+ (* LAMBDA S: CHOOSE x \in S : \A y \in S : x <= y, *)
53
+ (* (1 :> 2) @@ (2 :> 1) @@ (3 :> 3)) *)
54
+ (* = <<1,1,2,3,3,3>> *)
55
+ (* *)
56
+ (* The fifth argument represents the bag {| 1, 1, 2, 3, 3, 3 |}. *)
57
+ (* The first example counts the elements of the bag. *)
58
+ (* The second example computes a sorted sequence of all elements *)
59
+ (* of the bag. It is brittle because concatenation of sequences is *)
60
+ (* non-commutative, and the result therefore relies on the *)
61
+ (* definition of MapThenFoldSet. *)
62
+ (********************************************************************** *)
36
63
MapThenFoldBag ( op ( _ , _ ) , base , f ( _ ) , choose ( _ ) , B ) ==
37
- (* **********************************************************************)
38
- (* Fold operation op over the images through f of all elements of bag *)
39
- (* B, starting from base. The parameter choose indicates the order in *)
40
- (* which elements of the bag are processed; all replicas of an element *)
41
- (* are processed consecutively. *)
42
- (* *)
43
- (* Examples: *)
44
- (* MapThenFoldBag(LAMBDA x,y : x+y, *)
45
- (* 0, *)
46
- (* LAMBDA x: 1, *)
47
- (* LAMBDA B : CHOOSE x \in DOMAIN B : TRUE, *)
48
- (* (1 :> 2) @@ (2 :> 1) @@ (3 :> 3)) = 6 *)
49
- (* *)
50
- (* MapThenFoldBag(LAMBDA x,y : x \o y, *)
51
- (* << >>, *)
52
- (* LAMBDA x: << x >>, *)
53
- (* LAMBDA S: CHOOSE x \in S : \A y \in S : x <= y, *)
54
- (* (1 :> 2) @@ (2 :> 1) @@ (3 :> 3)) *)
55
- (* = <<1,1,2,3,3,3>> *)
56
- (* *)
57
- (* The fifth argument represents the bag {| 1, 1, 2, 3, 3, 3 |}. *)
58
- (* The first example counts the elements of the bag. *)
59
- (* The second example computes a sorted sequence of all elements *)
60
- (* of the bag. It is brittle because concatenation of sequences is *)
61
- (* non-commutative, and the result therefore relies on the *)
62
- (* definition of MapThenFoldSet. *)
63
- (********************************************************************** *)
64
64
LET handle ( x ) == \* handle all occurrences of x in B
65
65
LET pow [ n \in Nat \ { 0 } ] ==
66
66
op ( IF n = 1 THEN base ELSE pow [ n - 1 ] , f ( x ) )
@@ -69,28 +69,28 @@ BagRemoveAll(B,x) ==
69
69
LAMBDA S : CHOOSE x \in S : TRUE ,
70
70
DOMAIN B )
71
71
72
+ (* ***********************************************************************)
73
+ (* Fold op over all elements of bag B, starting with value base. *)
74
+ (* *)
75
+ (* Example: *)
76
+ (* FoldBag(LAMBDA x,y : x+y, *)
77
+ (* 0, *)
78
+ (* (1 :> 2) @@ (2 :> 1) @@ (3 :> 3)) = 13 *)
79
+ (* The third argument represents the bag {| 1, 1, 2, 3, 3, 3 |}. *)
80
+ (*********************************************************************** *)
72
81
FoldBag ( op ( _ , _ ) , base , B ) ==
73
- (* ***********************************************************************)
74
- (* Fold op over all elements of bag B, starting with value base. *)
75
- (* *)
76
- (* Example: *)
77
- (* FoldBag(LAMBDA x,y : x+y, *)
78
- (* 0, *)
79
- (* (1 :> 2) @@ (2 :> 1) @@ (3 :> 3)) = 13 *)
80
- (* The third argument represents the bag {| 1, 1, 2, 3, 3, 3 |}. *)
81
- (*********************************************************************** *)
82
82
MapThenFoldBag ( op , base , LAMBDA x : x , LAMBDA S : CHOOSE x \in S : TRUE , B )
83
83
84
+ (* ***********************************************************************)
85
+ (* Compute the sum of the elements of B. *)
86
+ (*********************************************************************** *)
84
87
SumBag ( B ) ==
85
- (* ***********************************************************************)
86
- (* Compute the sum of the elements of B. *)
87
- (*********************************************************************** *)
88
88
FoldBag ( LAMBDA x , y : x + y , 0 , B )
89
89
90
+ (* ***********************************************************************)
91
+ (* Compute the product of the elements of B. *)
92
+ (*********************************************************************** *)
90
93
ProductBag ( B ) ==
91
- (* ***********************************************************************)
92
- (* Compute the product of the elements of B. *)
93
- (*********************************************************************** *)
94
94
FoldBag ( LAMBDA x , y : x * y , 1 , B )
95
95
96
96
=============================================================================
0 commit comments