Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions modules/SequencesExt.tla
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ SelectInSeq(seq, Test(_)) ==
(* FALSE for all elements. *)
(*************************************************************************)
SelectInSubSeq(seq, from, to, Test(_)) ==
SelectInSeq(SubSeq(seq, from, to), Test)
LET I == { i \in from..to : Test(seq[i]) }
IN IF I # {} THEN Min(I) ELSE 0

(*************************************************************************)
(* Selects the index of the last element such that Test(seq[i]) is true *)
Expand All @@ -183,7 +184,8 @@ SelectLastInSeq(seq, Test(_)) ==
(* FALSE for all elements. *)
(*************************************************************************)
SelectLastInSubSeq(seq, from, to, Test(_)) ==
SelectLastInSeq(SubSeq(seq, from, to), Test)
LET I == { i \in from..to : Test(seq[i]) }
IN IF I # {} THEN Max(I) ELSE 0

-----------------------------------------------------------------------------

Expand Down
20 changes: 20 additions & 0 deletions tests/SequencesExtTests.tla
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,26 @@ ASSUME AssertEq(SelectLastInSubSeq(<<>>, 1, Len(<<>>), Op), 0)
ASSUME AssertEq(SelectLastInSubSeq(<<1,1,2>> , 1, 3, LAMBDA e : e = 1), 2)
ASSUME AssertEq(SelectLastInSubSeq(<<1,1,2,2>>, 2, 4, LAMBDA e : e = 2), 4)

SelectInSubSeqPure(seq, from, to, Test(_)) ==
LET I == { i \in from..to : Test(seq[i]) }
IN IF I # {} THEN CHOOSE i \in I : \A j \in I : i <= j ELSE 0

SelectLastInSubSeqPure(seq, from, to, Test(_)) ==
LET I == { i \in from..to : Test(seq[i]) }
IN IF I # {} THEN CHOOSE i \in I : \A j \in I : i >= j ELSE 0

ASSUME AssertEq(SelectInSubSeq(<<1,1,2>>, 2, 3, LAMBDA e : e = 1), SelectInSubSeqPure(<<1,1,2>>, 2, 3, LAMBDA e : e = 1))
ASSUME AssertEq(SelectInSubSeq(<<1,1,2,2>>, 2, 4, LAMBDA e : e = 2), SelectInSubSeqPure(<<1,1,2,2>>, 2, 4, LAMBDA e : e = 2))
ASSUME AssertEq(SelectLastInSubSeq(<<1,1,2,2>>, 2, 4, LAMBDA e : e = 2), SelectLastInSubSeqPure(<<1,1,2,2>>, 2, 4, LAMBDA e : e = 2))

ASSUME \A seq \in BoundedSeq(1..3, 4):
\A from \in 1..Len(seq) + 1, to \in 0..Len(seq):
\A v \in 1..3:
/\ AssertEq(SelectInSubSeq(seq, from, to, LAMBDA e : e = v),
SelectInSubSeqPure(seq, from, to, LAMBDA e : e = v))
/\ AssertEq(SelectLastInSubSeq(seq, from, to, LAMBDA e : e = v),
SelectLastInSubSeqPure(seq, from, to, LAMBDA e : e = v))

-----------------------------------------------------------------------------

ASSUME AssertEq(Suffixes(<<>>), {<<>>})
Expand Down
Loading