-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathUtils.tla
28 lines (25 loc) · 1.16 KB
/
Utils.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
------------------------------- MODULE Utils -------------------------------
EXTENDS Integers, Sequences, FiniteSets, TLC, Functions, SequencesExt
\* No support for RECURSIVE in PlusPy.
IsSimpleCycle(S, r) ==
(* View r as a graph s.t. S is the graph's set of vertices and there
exists an edge from s to t iff f[s] = t. IsFiniteSet(DOMAIN r). *)
LET
F[ i \in 1..Cardinality(S) ] ==
IF i = 1 THEN << CHOOSE s \in S : TRUE >>
ELSE LET seq == F[i-1]
head == Head(seq)
IN << r[head] >> \o F[i-1]
IN Range(F[Cardinality(S)]) = S
\* SimpleCycle is a recursive variant of the predicate IsSimpleCycle above. It
\* does not work with PlusPy, but is orders of magnitude faster when evaluated
\* by TLC.
SimpleCycle(S) ==
LET sts == LET SE == INSTANCE SequencesExt IN SE!SetToSeq(S)
RECURSIVE SimpleCycle(_,_,_)
SimpleCycle(seq, prefix, i) ==
IF i = Len(seq)
THEN prefix @@ (seq[i] :> seq[1])
ELSE SimpleCycle(seq, prefix @@ (seq[i] :> seq[i+1]), i+1)
IN SimpleCycle(sts, sts[1] :> sts[2], 2)
=============================================================================