-
Notifications
You must be signed in to change notification settings - Fork 2
/
euclid.tla
84 lines (67 loc) · 2.11 KB
/
euclid.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
------------------------------- MODULE euclid -------------------------------
(* for functions print, .., and assert *)
EXTENDS Naturals, TLC
(* parameterize the algorithm by K, make concrete before checking model *)
\* CONSTANT K
(* PlusCal options (-termination) *)
(* helpers *)
Divides(i,j) == \E k \in 0..j : j = i * k
\*Divides(i,j) == j % i = 0
IsGCD(i,j,k) == Divides(i,j) /\ Divides(i,k) /\ \A r \in 0..j \cup 0..k : Divides(r,j) /\ Divides(r,k) => Divides(r,i)
THEOREM GCD1 == \A x \in Nat \ {0}: IsGCD(x,x,x)
(*
--algorithm euclid {
variables m=30, n=18, u=m, v=n, v_ini=v;
{
bela: while (u # 0) {
iff: if (u < v)
u := v || v := u;
tmp: u := u - v;
};
print <<m,v_ini,"have gdc",v>>;
(* correctness condition *)
assert IsGCD(v,m,n)
}
}
*)
\* BEGIN TRANSLATION
VARIABLES m, n, u, v, v_ini, pc
vars == << m, n, u, v, v_ini, pc >>
Init == (* Global variables *)
/\ m = 30
/\ n = 18
/\ u = m
/\ v = n
/\ v_ini = v
/\ pc = "bela"
bela == /\ pc = "bela"
/\ IF u # 0
THEN /\ pc' = "iff"
ELSE /\ PrintT(<<m,v_ini,"have gdc",v>>)
/\ Assert(IsGCD(v,m,n),
"Failure of assertion at line 29, column 5.")
/\ pc' = "Done"
/\ UNCHANGED << m, n, u, v, v_ini >>
iff == /\ pc = "iff"
/\ IF u < v
THEN /\ /\ u' = v
/\ v' = u
ELSE /\ TRUE
/\ UNCHANGED << u, v >>
/\ pc' = "tmp"
/\ UNCHANGED << m, n, v_ini >>
tmp == /\ pc = "tmp"
/\ u' = u - v
/\ pc' = "bela"
/\ UNCHANGED << m, n, v, v_ini >>
Next == bela \/ iff \/ tmp
\/ (* Disjunct to prevent deadlock on termination *)
(pc = "Done" /\ UNCHANGED vars)
Spec == /\ Init /\ [][Next]_vars
/\ WF_vars(Next)
Termination == <>(pc = "Done")
\* END TRANSLATION
=============================================================================
\* Modification History
\* Last modified Thu Jan 05 18:44:44 CET 2017 by bela
\* Created Mon Mar 09 16:07:32 CET 2015 by bela