Skip to content

Commit 50999d7

Browse files
konnovlemmy
authored andcommitted
Align SequencesExt!Zip with the usual definition in PLs.
[Refactor]
1 parent 4e523a6 commit 50999d7

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

modules/SequencesExt.tla

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,23 @@ FlattenSeq(seqs) ==
283283
IF i = 1 THEN seqs[i] ELSE flatten[i-1] \o seqs[i]
284284
IN flatten[Len(seqs)]
285285

286+
Zip(s, t) ==
287+
(**************************************************************************)
288+
(* A sequence where the i-th tuple contains the i-th element of s and *)
289+
(* t in this order. Not defined for Len(s) # Len(t) *)
290+
(* *)
291+
(* Examples: *)
292+
(* *)
293+
(* Zip(<< >>, << >>) = << >> *)
294+
(* Zip(<<"a">>, <<"b">>) = <<"a", "b">> *)
295+
(* Zip(<<1, 3>>, <<2, 4>>) = <<<<1, 2>>, <<3, 4>>>> *)
296+
(* FlattenSeq(Zip(<<1, 3>>, <<2, 4>>)) = <<1, 2, 3, 4>>>> *)
297+
(**************************************************************************)
298+
CASE Len(s) = Len(t) /\ Len(s) > 0 ->
299+
[ i \in DOMAIN s |-> <<s[i], t[i]>> ]
300+
[] Len(s) = Len(t) /\ Len(s) = 0 -> << >>
301+
\* error "Zip: sequences must have same length"
302+
286303
Interleave(s, t) ==
287304
(**************************************************************************)
288305
(* A sequence where the i-th tuple contains the i-th element of s and *)
@@ -303,7 +320,6 @@ Interleave(s, t) ==
303320
[] Len(s) = Len(t) /\ Len(s) = 0 -> << <<>>, <<>> >>
304321
\* error "Interleave: sequences must have same length"
305322

306-
307323
SubSeqs(s) ==
308324
(**************************************************************************)
309325
(* The set of all subsequences of the sequence s . Note that the empty *)

tests/SequencesExtTests.tla

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ ASSUME AssertEq(FlattenSeq(Interleave(<<"a", "c">>, <<"b", "d">>)), <<"a", "b",
182182

183183
-----------------------------------------------------------------------------
184184

185+
ASSUME Zip(<<>>, <<>>) = << >>
186+
ASSUME Zip(<< <<>> >>, <<1>>) = << << <<>>, 1>> >>
187+
ASSUME Zip(<<1>>, << <<>> >>) = << <<1, <<>> >> >>
188+
ASSUME Zip(<<2>>, <<2>>) = << <<2, 2>> >>
189+
ASSUME Zip(<<2>>, <<3>>) = << <<2, 3>> >>
190+
ASSUME Zip(<<2, 3>>, <<1, 4>>) = << <<2, 1>>, <<3, 4>> >>
191+
ASSUME Zip(<<2, 3>>, <<2, 3>>) = << <<2, 2>>, <<3, 3>> >>
192+
ASSUME Zip(<<1, 3>>, <<2, 4>>) = << <<1, 2>>, <<3, 4>> >>
193+
ASSUME AssertEq(FlattenSeq(Zip(<<1, 3>>, <<2, 4>>)), <<1, 2, 3, 4>>)
194+
ASSUME Zip(<<"a", "c">>, <<"b", "d">>) = << <<"a", "b">>, <<"c", "d">> >>
195+
ASSUME AssertEq(FlattenSeq(Zip(<<"a", "c">>, <<"b", "d">>)), <<"a", "b", "c", "d">>)
196+
197+
-----------------------------------------------------------------------------
198+
185199
ASSUME SubSeqs(<<>>) = {<<>>}
186200
ASSUME SubSeqs(<<1>>) = {<<>>, <<1>>}
187201
ASSUME SubSeqs(<<1,1>>) = {<<>>, <<1>>, <<1,1>>}

0 commit comments

Comments
 (0)