-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafe_swap.adb
71 lines (60 loc) · 1.98 KB
/
safe_swap.adb
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
package body Safe_Swap with
SPARK_Mode => On
is
procedure Swap(A : in out Nat_Array; I, J : in Index) is
TMP : constant Natural := A(I);
Init : constant Nat_Array (A'Range):= A with Ghost;
Interm : Nat_Array (A'Range) with Ghost;
procedure Prove_Perm With
Ghost,
Global => (Init, Interm, I, J, A),
Pre => I in A'Range and then
J in A'Range and then
Is_Set(Init, I, Init(J), Interm) and then
Is_Set(Interm, J, Init(I), A),
Post => Is_Perm(Init, A)
is
begin
for E in Natural loop
Lemma_Occ_Set(Init, I, Init(J), E, Interm);
Lemma_Occ_Set(Interm, J, Init(I), E, A);
pragma Loop_Invariant
(for all F in Natural'First .. E =>
(Occ(A, F) = Occ(Init, F)));
end loop;
end Prove_Perm;
begin
A(I) := A(J);
pragma Assert (Is_Set(Init, I, Init(J), A));
Interm := A;
A(J) := TMP;
pragma Assert (Is_Set(Interm, J, Init(I), A));
Prove_Perm;
end Swap;
procedure Lemma_Occ_Eq (A, B: Nat_Array; E : Natural) is
begin
if A'Length = 0 then
return;
end if;
Lemma_Occ_Eq(Remove_Last(A), Remove_Last(B), E);
if A(A'Last) = E then
pragma Assert (B(B'Last) = E);
else
pragma Assert (B(B'Last) /= E);
end if;
end Lemma_Occ_Eq;
procedure Lemma_Occ_Set(A : Nat_Array; I : Index; V, E : Natural; R : Nat_Array) is
B : Nat_Array := Remove_Last(A);
begin
if A'Length = 0 then
return;
end if;
if I = A'Last then
Lemma_Occ_Eq(B, Remove_Last(R), E);
else
B(I) := V;
Lemma_Occ_Eq(Remove_Last(R), B, E);
Lemma_Occ_Set(Remove_Last(A), I, V, E, B);
end if;
end Lemma_Occ_Set;
end Safe_Swap;