-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathFolds.tla
30 lines (27 loc) · 1.85 KB
/
Folds.tla
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
------------------------------- MODULE Folds -------------------------------
MapThenFoldSet(op(_,_), base, f(_), choose(_), S) ==
(******************************************************************************)
(* Starting from base, apply op to f(x), for all x \in S, by choosing the set *)
(* elements with `choose`. If there are multiple ways for choosing an element,*)
(* op should be associative and commutative. Otherwise, the result may depend *)
(* on the concrete implementation of `choose`. *)
(* *)
(* FoldSet, a simpler version for sets is contained in FiniteSetsEx. *)
(* FoldFunction, a simpler version for functions is contained in Functions. *)
(* FoldSequence, a simpler version for sequences is contained in SequencesExt.*)
(* *)
(* Example: *)
(* *)
(* MapThenFoldSet(LAMBDA x,y: x \cup y, *)
(* {}, *)
(* LAMBDA x: {{x}}, *)
(* LAMBDA set: CHOOSE x \in set: TRUE, *)
(* {1,2}) *)
(* = {{1},{2}} *)
(******************************************************************************)
LET iter[s \in SUBSET S] ==
IF s = {} THEN base
ELSE LET x == choose(s)
IN op(f(x), iter[s \ {x}])
IN iter[S]
=============================================================================