Skip to content

Commit 254143f

Browse files
committed
SequencesExt!Zip
[Feature]
1 parent d077c5c commit 254143f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

modules/SequencesExt.tla

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,25 @@ FlattenSeq(seqs) ==
259259
LET flatten[i \in 1..Len(seqs)] ==
260260
IF i = 1 THEN seqs[i] ELSE flatten[i-1] \o seqs[i]
261261
IN flatten[Len(seqs)]
262+
263+
Zip(s, t) ==
264+
(**************************************************************************)
265+
(* A sequence where the i-th tuple contains the i-th element of s and *)
266+
(* t in this order. Not defined for Len(s) # Len(t) *)
267+
(* *)
268+
(* Examples: *)
269+
(* *)
270+
(* Zip(<< >>, << >>) = << >> *)
271+
(* Zip(<<"a">>, <<"b">>) = <<"a", "b">> *)
272+
(* Zip(<<1,3>>, <<2,4>>) = <<<<1>>, <<2>>, <<3>>, <<4>>>> *)
273+
(* FlattenSeq(Zip(<<1,3>>,<<2,4>>)) = <<1, 2, 3, 4>> *)
274+
(**************************************************************************)
275+
CASE Len(s) = Len(t) /\ Len(s) > 0 ->
276+
LET u[ i \in 1..Len(s) ] ==
277+
IF i = 1 THEN << <<s[i]>> >> \o << <<t[i]>> >>
278+
ELSE u[i-1] \o << <<s[i]>> >> \o << <<t[i]>> >>
279+
IN Last(u)
280+
[] Len(s) = Len(t) /\ Len(s) = 0 -> << <<>>, <<>> >>
281+
\* error "Zip: sequences must have same length"
282+
262283
=============================================================================

tests/SequencesExtTests.tla

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,19 @@ ASSUME FlattenSeq(<< <<1,2>>, << << 1, 2 >> >> >>) = << 1, 2, << 1, 2 >> >>
148148
ASSUME FlattenSeq("") = ""
149149
ASSUME FlattenSeq(<< <<"a">>, <<"b">> >>) = <<"a", "b">>
150150
ASSUME FlattenSeq(<<"a", "b">>) = "ab"
151+
152+
-----------------------------------------------------------------------------
153+
154+
ASSUME Zip(<<>>, <<>>) = << <<>>, <<>> >>
155+
ASSUME Zip(<< <<>> >>, <<1>>) = << << <<>> >>, <<1>> >>
156+
ASSUME Zip(<<1>>, << <<>> >>) = << <<1>>, << <<>> >> >>
157+
ASSUME Zip(<<2>>,<<2>>) = << <<2>>, <<2>> >>
158+
ASSUME Zip(<<2>>,<<3>>) = << <<2>>, <<3>> >>
159+
ASSUME Zip(<<2,3>>,<<1,4>>) = << <<2>>, <<1>>, <<3>>, <<4>> >>
160+
ASSUME Zip(<<2,3>>,<<2,3>>) = << <<2>>, <<2>>, <<3>>, <<3>> >>
161+
ASSUME Zip(<<1,3>>,<<2,4>>) = <<<<1>>, <<2>>, <<3>>, <<4>>>>
162+
ASSUME AssertEq(FlattenSeq(Zip(<<1,3>>,<<2,4>>)), <<1, 2, 3, 4>>)
163+
ASSUME Zip(<<"a", "c">>, <<"b", "d">>) = <<<<"a">>, <<"b">>, <<"c">>, <<"d">>>>
164+
ASSUME AssertEq(FlattenSeq(Zip(<<"a", "c">>, <<"b", "d">>)), <<"a", "b", "c", "d">>)
165+
151166
=============================================================================

0 commit comments

Comments
 (0)