-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
BinSearch6.tla.patch
60 lines (60 loc) · 2.03 KB
/
BinSearch6.tla.patch
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
1c1
< --------------------------- MODULE BinSearch5 ---------------------------------
---
> --------------------------- MODULE BinSearch6 ---------------------------------
4a5
> * Version 6. Check termination and progress.
47c48,51
< returnValue
---
> returnValue,
> \* The number of executed steps.
> \* @type: Int;
> nSteps
54a59
> /\ nSteps = 0
61,72c66,78
< LET mid == (low + high) \div 2 IN
< LET midVal == INPUT_SEQ[mid + 1] IN
< \//\ midVal < INPUT_KEY \* lines 9-10
< /\ low' = mid + 1
< /\ UNCHANGED <<high, returnValue, isTerminated>>
< \//\ midVal > INPUT_KEY \* lines 11-12
< /\ high' = mid -1
< /\ UNCHANGED <<low, returnValue, isTerminated>>
< \//\ midVal = INPUT_KEY \* lines 13-14
< /\ returnValue' = mid
< /\ isTerminated' = TRUE
< /\ UNCHANGED <<low, high>>
---
> /\ nSteps' = nSteps + 1
> /\ LET mid == (low + high) \div 2 IN
> LET midVal == INPUT_SEQ[mid + 1] IN
> \//\ midVal < INPUT_KEY \* lines 9-10
> /\ low' = mid + 1
> /\ UNCHANGED <<high, returnValue, isTerminated>>
> \//\ midVal > INPUT_KEY \* lines 11-12
> /\ high' = mid -1
> /\ UNCHANGED <<low, returnValue, isTerminated>>
> \//\ midVal = INPUT_KEY \* lines 13-14
> /\ returnValue' = mid
> /\ isTerminated' = TRUE
> /\ UNCHANGED <<low, high>>
76c82
< /\ UNCHANGED <<low, high>>
---
> /\ UNCHANGED <<low, high, nSteps>>
78c84
< UNCHANGED <<low, high, returnValue, isTerminated>>
---
> UNCHANGED <<low, high, returnValue, isTerminated, nSteps>>
113a120,128
> \* We know the exact number of steps to show termination.
> Termination ==
> (nSteps >= INT_WIDTH) => isTerminated
>
> \* By showing that the interval [low, high] is contracting,
> \* we can implicitly show termination too.
> Progress ==
> ~isTerminated' => (low' > low \/ high' < high)
>