-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathSequencesExt.tla
177 lines (146 loc) · 8.68 KB
/
SequencesExt.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
---------------------------- MODULE SequencesExt ----------------------------
LOCAL INSTANCE Sequences
LOCAL INSTANCE Naturals
LOCAL INSTANCE FiniteSets
LOCAL INSTANCE FiniteSetsExt
LOCAL INSTANCE Functions
(*************************************************************************)
(* Imports the definitions from the modules, but doesn't export them. *)
(*************************************************************************)
-----------------------------------------------------------------------------
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 }
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)
TupleOf(set, n) ==
(***************************************************************************)
(* TupleOf(s, 3) = s \X s \X s *)
(***************************************************************************)
[1..n -> set]
SeqOf(set, n) ==
(***************************************************************************)
(* All sequences up to length n with all elements in set. Includes empty *)
(* sequence. *)
(***************************************************************************)
UNION {[1..m -> set] : m \in 0..n}
BoundedSeq(S, n) ==
(***************************************************************************)
(* An alias for SeqOf to make the connection to Sequences!Seq, which is *)
(* the unbounded version of BoundedSeq. *)
(***************************************************************************)
SeqOf(S, n)
-----------------------------------------------------------------------------
Contains(s, e) ==
(**************************************************************************)
(* TRUE iff the element e \in ToSet(s). *)
(**************************************************************************)
\E i \in 1..Len(s) : s[i] = e
Reverse(s) ==
(**************************************************************************)
(* Reverse the given sequence s: Let l be Len(s) (length of s). *)
(* Equals a sequence s.t. << S[l], S[l-1], ..., S[1]>> *)
(**************************************************************************)
[ i \in 1..Len(s) |-> s[(Len(s) - i) + 1] ]
Remove(s, e) ==
(************************************************************************)
(* The sequence s with e removed or s iff e \notin Range(s) *)
(************************************************************************)
SelectSeq(s, LAMBDA t: t # e)
ReplaceAll(s, old, new) ==
(*************************************************************************)
(* Equals the sequence s except that all occurrences of element old are *)
(* replaced with the element new. *)
(*************************************************************************)
LET F[i \in 0..Len(s)] ==
IF i = 0 THEN << >>
ELSE IF s[i] = old THEN Append(F[i-1], new)
ELSE Append(F[i-1], s[i])
IN F[Len(s)]
-----------------------------------------------------------------------------
\* The operators below have been extracted from the TLAPS module
\* SequencesTheorems.tla as of 10/14/2019. The original comments have been
\* partially rewritten.
InsertAt(s, i, e) ==
(**************************************************************************)
(* Inserts element e at the position i moving the original element to i+1 *)
(* and so on. In other words, a sequence t s.t.: *)
(* /\ Len(t) = Len(s) + 1 *)
(* /\ t[i] = e *)
(* /\ \A j \in 1..(i - 1): t[j] = s[j] *)
(* /\ \A k \in (i + 1)..Len(s): t[k + 1] = s[k] *)
(**************************************************************************)
SubSeq(s, 1, i-1) \o <<e>> \o SubSeq(s, i, Len(s))
ReplaceAt(s, i, e) ==
(**************************************************************************)
(* Replaces the element at position i with the element e. *)
(**************************************************************************)
[s EXCEPT ![i] = e]
RemoveAt(s, i) ==
(**************************************************************************)
(* Replaces the element at position i shortening the length of s by one. *)
(**************************************************************************)
SubSeq(s, 1, i-1) \o SubSeq(s, i+1, Len(s))
-----------------------------------------------------------------------------
Cons(elt, seq) ==
(***************************************************************************)
(* Cons prepends an element at the beginning of a sequence. *)
(***************************************************************************)
<<elt>> \o seq
Front(s) ==
(**************************************************************************)
(* The sequence formed by removing its last element. *)
(**************************************************************************)
SubSeq(s, 1, Len(s)-1)
Last(s) ==
(**************************************************************************)
(* The last element of the sequence. *)
(**************************************************************************)
s[Len(s)]
-----------------------------------------------------------------------------
IsPrefix(s, t) ==
(**************************************************************************)
(* TRUE iff the sequence s is a prefix of the sequence t, s.t. *)
(* \E u \in Seq(Range(t)) : t = s \o u. In other words, there exists *)
(* a suffix u that with s prepended equals t. *)
(**************************************************************************)
DOMAIN s \subseteq DOMAIN t /\ \A i \in DOMAIN s: s[i] = t[i]
IsStrictPrefix(s,t) ==
(**************************************************************************)
(* TRUE iff the sequence s is a prefix of the sequence t and s # t *)
(**************************************************************************)
IsPrefix(s, t) /\ s # t
IsSuffix(s, t) ==
(**************************************************************************)
(* TRUE iff the sequence s is a suffix of the sequence t, s.t. *)
(* \E u \in Seq(Range(t)) : t = u \o s. In other words, there exists a *)
(* prefix that with s appended equals t. *)
(**************************************************************************)
IsPrefix(Reverse(s), Reverse(t))
IsStrictSuffix(s, t) ==
(**************************************************************************)
(* TRUE iff the sequence s is a suffix of the sequence t and s # t *)
(**************************************************************************)
IsSuffix(s,t) /\ s # t
-----------------------------------------------------------------------------
SeqMod(a, b) ==
(***************************************************************************)
(* Range(a % b) = 0..b-1, but DOMAIN seq = 1..Len(seq). *)
(* So to do modular arithmetic on sequences we need to *)
(* map 0 to b. *)
(***************************************************************************)
IF a % b = 0 THEN b ELSE a % b
ReduceSeq(op(_, _), seq, acc) ==
(***************************************************************************)
(* We can't just apply ReduceSet to the Range(seq) because the same *)
(* element might appear twice in the sequence. *)
(***************************************************************************)
ReduceSet(LAMBDA i, a: op(seq[i], a), DOMAIN seq, acc)
=============================================================================