-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.tla
53 lines (46 loc) · 2.8 KB
/
utils.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-------------------------------- MODULE Utils -------------------------------
LOCAL INSTANCE Sequences
LOCAL INSTANCE Naturals
LOCAL INSTANCE FiniteSets
\* from the community modules
(***************************************************************************)
(* A function is injective iff it maps each element in its domain to a *)
(* distinct element. *)
(* *)
(* This definition is overridden by TLC in the Java class Functions.java *)
(* The operator is overridden by the Java method with the same name. *)
(***************************************************************************)
IsInjective(f) == \A a,b \in DOMAIN f : f[a] = f[b] => a = b
ToSet(s) ==
(*************************************************************************)
(* The image of the given sequence s. Cardinality(ToSet(s)) <= Len(s) *)
(* see https://en.wikipedia.org/wiki/Image_(mathematics) *)
(*************************************************************************)
{ s[i] : i \in DOMAIN s }
FlattenSeq(seqs) ==
(**************************************************************************)
(* A sequence of all elements from all sequences in the sequence seqs . *)
(* *)
(* Examples: *)
(* *)
(* FlattenSeq(<< <<1,2>>, <<1>> >>) = << 1, 2, 1 >> *)
(* FlattenSeq(<< <<"a">>, <<"b">> >>) = <<"a", "b">> *)
(* FlattenSeq(<< "a", "b" >>) = "ab" *)
(**************************************************************************)
IF Len(seqs) = 0 THEN seqs ELSE
\* Not via FoldSeq(\o, <<>>, seqs) here to support strings with TLC.
LET flatten[i \in 1..Len(seqs)] ==
IF i = 1 THEN seqs[i] ELSE flatten[i-1] \o seqs[i]
IN flatten[Len(seqs)]
SetToSeq(S) ==
(**************************************************************************)
(* Convert a set to some sequence that contains all the elements of the *)
(* set exactly once, and contains no other elements. *)
(**************************************************************************)
CHOOSE f \in [1..Cardinality(S) -> S] : IsInjective(f)
Remove(s, e) ==
(************************************************************************)
(* The sequence s with e removed or s iff e \notin Range(s) *)
(************************************************************************)
SelectSeq(s, LAMBDA t: t # e)
=============================================================================