-
Notifications
You must be signed in to change notification settings - Fork 1
/
SequencesExt.tla
459 lines (401 loc) · 24.6 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
---------------------------- MODULE SequencesExt ----------------------------
LOCAL INSTANCE Sequences
LOCAL INSTANCE Naturals
LOCAL INSTANCE FiniteSets
LOCAL INSTANCE FiniteSetsExt
LOCAL INSTANCE Functions
LOCAL INSTANCE Folds
LOCAL INSTANCE TLC
(*************************************************************************)
(* 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)
SetToSeqs(S) ==
(**************************************************************************)
(* Convert the set S to a set containing all sequences containing the *)
(* elements of S exactly once and no other elements. *)
(* Example: *)
(* SetToSeqs({}), {<<>>} *)
(* SetToSeqs({"t","l"}) = {<<"t","l">>, <<"l","t">>} *)
(**************************************************************************)
LET D == 1..Cardinality(S)
IN { f \in [D -> S] : \A i,j \in D : i # j => f[i] # f[j] }
SetToSortSeq(S, op(_,_)) ==
(**************************************************************************)
(* Convert a set to a sorted sequence that contains all the elements of *)
(* the set exactly once, and contains no other elements. *)
(**************************************************************************)
\* Not defined via CHOOSE like SetToSeq but with an additional conjunct,
\* because this variant works efficiently without a dedicated TLC override.
SortSeq(SetToSeq(S), op)
SetToAllKPermutations(S) ==
(**************************************************************************)
(* Convert the set S to a set containing all k-permutations of elements *)
(* of S for k \in 0..Cardinality(S). *)
(* Example: *)
(* SetToAllKPermutations({}) = {<<>>} *)
(* SetToAllKPermutations({"a"}) = {<<>>, <<"a">>} *)
(* SetToAllKPermutations({"a","b"}) = *)
(* {<<>>, <<"a">>, <<"b">>,<<"a","b">>, <<"b","a">>} *)
(**************************************************************************)
UNION { SetToSeqs(s) : s \in SUBSET S }
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. *)
(*************************************************************************)
[i \in 1 .. Len(s) |-> IF s[i] = old THEN new ELSE s[i]]
-----------------------------------------------------------------------------
SelectInSeq(seq, Test(_)) ==
(*************************************************************************)
(* Selects the index of the first element such that Test(seq[i]) is true *)
(* Equals 0 if Test(seq[i]) is FALSE for all elements. *)
(*************************************************************************)
LET I == { i \in 1..Len(seq) : Test(seq[i]) }
IN IF I # {} THEN Min(I) ELSE 0
SelectInSubSeq(seq, from, to, Test(_)) ==
(*************************************************************************)
(* Selects the index of the first element in seq such that Test(seq[i]) *)
(* is TRUE for this elements in from..to. Equals 0 if Test(seq[i]) is *)
(* FALSE for all elements. *)
(*************************************************************************)
SelectInSeq(SubSeq(seq, from, to), Test)
SelectLastInSeq(seq, Test(_)) ==
(*************************************************************************)
(* Selects the index of the last element such that Test(seq[i]) is true *)
(* Equals 0 if Test(seq[i]) is FALSE for all elements. *)
(*************************************************************************)
LET I == { i \in 1..Len(seq) : Test(seq[i]) }
IN IF I # {} THEN Max(I) ELSE 0
SelectLastInSubSeq(seq, from, to, Test(_)) ==
(*************************************************************************)
(* Selects the index of the last element in seq such that Test(seq[i]) *)
(* is TRUE for this elements in from..to. Equals 0 if Test(seq[i]) is *)
(* FALSE for all elements. *)
(*************************************************************************)
SelectLastInSeq(SubSeq(seq, from, to), Test)
-----------------------------------------------------------------------------
\* The operators below up to including IsStrictSuffix 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))
RemoveFirst(s, e) ==
(************************************************************************)
(* The sequence s with the first occurrence of e removed or s *)
(* iff e \notin Range(s) *)
(************************************************************************)
IF \E i \in 1..Len(s): s[i] = e
THEN RemoveAt(s, SelectInSeq(s, LAMBDA v: v = e))
ELSE s
RemoveFirstMatch(s, Test(_)) ==
(************************************************************************)
(* The sequence s with the first element removed s.t. Test(e) or s *)
(* iff e \notin Range(s) *)
(************************************************************************)
IF \E i \in 1..Len(s): Test(s[i])
THEN RemoveAt(s, SelectInSeq(s, Test))
ELSE 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. *)
(**************************************************************************)
Len(s) <= Len(t) /\ SubSeq(s, 1, Len(s)) = SubSeq(t, 1, Len(s))
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
-----------------------------------------------------------------------------
Prefixes(s) ==
(**************************************************************************)
(* The set of prefixes of the sequence s, including the empty sequence. *)
(**************************************************************************)
{ SubSeq(s, 1, l) : l \in 0..Len(s) } \* 0.. for <<>>
CommonPrefixes(S) ==
(**************************************************************************)
(* The set of all sequences that are prefixes of the set of sequences S. *)
(**************************************************************************)
LET P == UNION { Prefixes(seq) : seq \in S }
IN { prefix \in P : \A t \in S: IsPrefix(prefix, t) }
LongestCommonPrefix(S) ==
(**************************************************************************)
(* The longest common prefix of the sequences in the set S. *)
(**************************************************************************)
CHOOSE longest \in CommonPrefixes(S): \* there can only be one LCP => CHOOSE
\A other \in CommonPrefixes(S):
Len(other) <= Len(longest)
Suffixes(s) ==
(**************************************************************************)
(* The set of suffixes of the sequence s, including the empty sequence. *)
(**************************************************************************)
{ SubSeq(s, l, Len(s)) : l \in 1..Len(s) } \cup {<<>>}
-----------------------------------------------------------------------------
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
FoldSeq(op(_, _), base, seq) ==
(***************************************************************************)
(* An alias of FoldFunction that op on all elements of seq an arbitrary *)
(* order. The resulting function is: *)
(* op(f[i],op(f[j], ..., op(f[k],base) ...)) *)
(* *)
(* op must be associative and commutative, because we can not assume a *)
(* particular ordering of i, j, and k *)
(* *)
(* Example: *)
(* FoldSeq(LAMBDA x,y: {x} \cup y, {}, <<1,2,1>>) = Range(<<1,2,1>>) *)
(***************************************************************************)
FoldFunction(op, base, seq)
FoldLeft(op(_, _), base, seq) ==
(***************************************************************************)
(* FoldLeft folds op on all elements of seq from left to right, starting *)
(* with the first element and base. The resulting function is: *)
(* op(op(...op(base,f[0]), ...f[n-1]), f[n]) *)
(* *)
(* *)
(* Example: *)
(* LET cons(x,y) == <<x,y>> *)
(* IN FoldLeft(cons, 0, <<3,1,2>>) = << << <<0,3>>, 1>>, 2>> *)
(***************************************************************************)
MapThenFoldSet(LAMBDA x,y : op(y,x), base,
LAMBDA i : seq[i],
LAMBDA S : Max(S),
DOMAIN seq)
FoldRight(op(_, _), seq, base) ==
(***************************************************************************)
(* FoldRight folds op on all elements of seq from right to left, starting *)
(* with the last element and base. The resulting function is: *)
(* op(f[0],op(f[1], ..., op(f[n],base) ...)) *)
(* *)
(* *)
(* Example: *)
(* LET cons(x,y) == <<x,y>> *)
(* IN FoldRight(cons, <<3,1,2>>, 0 ) = << 3, << 1, <<2,0>> >> >> *)
(***************************************************************************)
MapThenFoldSet(op, base,
LAMBDA i : seq[i],
LAMBDA S : Min(S),
DOMAIN seq)
-----------------------------------------------------------------------------
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)]
Zip(s, t) ==
(**************************************************************************)
(* A sequence of pairs where the i-th pair is formed from the i-th *)
(* element of s and the i-th element of t. The length of the result *)
(* sequence is the minimum of the lengths of s and t. *)
(* *)
(* Examples: *)
(* *)
(* Zip(<< >>, << >>) = << >> *)
(* Zip(<<"a">>, <<"b">>) = << <<"a", "b">> >> *)
(* Zip(<<1, 3>>, <<2, 4>>) = <<<<1, 2>>, <<3, 4>>>> *)
(* FlattenSeq(Zip(<<1, 3>>, <<2, 4>>)) = <<1, 2, 3, 4>>>> *)
(* Zip(<< >>, <<1, 2, 3>>) = << >> *)
(* Zip(<<"a", "b", "c">>, <<1>>) = << <<"a", 1>> >> *)
(**************************************************************************)
LET l == IF Len(s) <= Len(t) THEN Len(s) ELSE Len(t)
IN [ i \in 1 .. l |-> <<s[i], t[i] >> ]
Interleave(s, t) ==
(**************************************************************************)
(* A sequence where the i-th tuple contains the i-th element of s and *)
(* t in this order. Not defined for Len(s) # Len(t) *)
(* *)
(* Examples: *)
(* *)
(* Interleave(<< >>, << >>) = << >> *)
(* Interleave(<<"a">>, <<"b">>) = <<"a", "b">> *)
(* Interleave(<<1,3>>, <<2,4>>) = <<<<1>>, <<2>>, <<3>>, <<4>>>> *)
(* FlattenSeq(Interleave(<<1,3>>,<<2,4>>)) = <<1, 2, 3, 4>> *)
(**************************************************************************)
CASE Len(s) = Len(t) /\ Len(s) > 0 ->
LET u[ i \in 1..Len(s) ] ==
IF i = 1 THEN << <<s[i]>> >> \o << <<t[i]>> >>
ELSE u[i-1] \o << <<s[i]>> >> \o << <<t[i]>> >>
IN Last(u)
[] Len(s) = Len(t) /\ Len(s) = 0 -> << <<>>, <<>> >>
\* error "Interleave: sequences must have same length"
SubSeqs(s) ==
(**************************************************************************)
(* The set of all subsequences of the sequence s . Note that the empty *)
(* sequence <<>> is defined to be a subsequence of any sequence. *)
(**************************************************************************)
{ SubSeq(s, i+1, j) : i, j \in 0..Len(s) }
IndexFirstSubSeq(s, t) ==
(**************************************************************************)
(* The (1-based) index of the beginning of the subsequence s of the *)
(* sequence t . If s appears in t multiple times, this equals the *)
(* lowest index. *)
(* For example: IndexFirstSubSeq(<<1>>, <<1,1,1>>) = 1 *)
(**************************************************************************)
LET last == CHOOSE i \in 0..Len(t) :
/\ s \in SubSeqs(SubSeq(t, 1, i))
/\ \A j \in 0..i-1 : s \notin SubSeqs(SubSeq(t, 1, j))
IN last - (Len(s) - 1)
ReplaceSubSeqAt(i, r, s, t) ==
(**************************************************************************)
(* The sequence t with its subsequence s at position i replaced by *)
(* the sequence r . *)
(**************************************************************************)
LET prefix == SubSeq(t, 1, i - 1)
suffix == SubSeq(t, i + Len(s), Len(t))
IN prefix \o r \o suffix
ReplaceFirstSubSeq(r, s, t) ==
(**************************************************************************)
(* The sequence t with its subsequence s replaced by the sequence r *)
(**************************************************************************)
IF s \in SubSeqs(t)
THEN ReplaceSubSeqAt(IndexFirstSubSeq(s, t), r, s, t)
ELSE t
ReplaceAllSubSeqs(r, s, t) ==
(**************************************************************************)
(* The sequence t with all subsequences s replaced by the sequence r *)
(* Overlapping replacements are disambiguated by choosing the occurrence *)
(* closer to the beginning of the sequence. *)
(* *)
(* Examples: *)
(* *)
(* ReplaceAllSubSeqs(<<>>,<<>>,<<>>) = <<>> *)
(* ReplaceAllSubSeqs(<<4>>,<<>>,<<>>) = <<4>> *)
(* ReplaceAllSubSeqs(<<2>>,<<3>>,<<1,3>>) = <<1,2>> *)
(* ReplaceAllSubSeqs(<<2,2>>,<<1,1>>,<<1,1,1>>) = <<2,2,1>> *)
(**************************************************************************)
CASE s = t -> r
[] r = s -> t \* TLC optimization
[] s # t /\ Len(s) = 0 ->
LET z == Interleave([i \in 1..Len(t) |-> r], [i \in 1..Len(t) |-> <<t[i]>>])
IN FlattenSeq(FlattenSeq(z))
[] s # t /\ Len(s) > 0 /\ s \in SubSeqs(t) ->
\* Not defined recursively to avoid infinite loops.
LET match(f) == { i \in 1..Len(f) : s = f[i] }
comp(p, q) == \A i \in 1..Len(p) : p[i] <= q[i]
\* TODO: Replace with Seq(Seq(Range(t))) once *total* Java module
\* override in place. The current override handles only the
\* case where the parameters are strings (hence Range("abc")
\* not a problem with TLC).
R == BoundedSeq(BoundedSeq(Range(t), Len(t)), Len(t))
\* A) Matches the input t.
S == { f \in R : FlattenSeq(f) = t }
\* B) Has the max number of matches...
T == { f \in S : \A g \in S :
Cardinality(match(g)) <= Cardinality(match(f)) }
\* C) ...of min (leftmost) matches.
u == CHOOSE f \in T :
\A g \in T : comp(
SetToSortSeq(match(f), <), SetToSortSeq(match(g), <))
IN FlattenSeq([i \in 1..Len(u) |-> IF s = u[i] THEN r ELSE u[i]])
[] OTHER -> t
=============================================================================