Repository navigation
Expand file tree
/
Copy pathCountCalls.lean
More file actions
123 lines (108 loc) · 5.1 KB
/
Copy pathCountCalls.lean
File metadata and controls
123 lines (108 loc) · 5.1 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import Extracted
import CIL.Verification
import CIL.MethodIdentity
import CIL.StructuralLabels
import CIL.Safety.LoopScaffold
open CIL.Safety
open CIL (W32)
namespace ExampleProof
bind_method countCalls countBody := "System.UInt32 Examples.Loops::CountCalls(System.UInt32)"
bind_callee decrement decrementBody :=
"System.UInt32 Examples.Loops::Decrement(System.UInt32)" from countBody
bind_call decrementSite := decrement from countBody
def decrementContract : MethodContract W32 W32 where
args x := [.scalar (.i32 x)]
results v := [.scalar (.i32 v)]
pre _ _ := True
footprint _ := []
post x _ _ v := v = x - 1
theorem decrement_holds : decrementContract.Holds Extracted.program decrement := by
rintro x m -
refine MethodContract.Holds.intro m _ ?_ (Confined.refl _ m) rfl
cil_checked_steps
bind_entry entry (x : W32) := countBody with [.scalar (.i32 x)] from Memory.empty
allocating Memory.empty_wellFormed
bind_loop loop := countBody
/-- The remaining count and the calls made so far, held by the frame's two locals. -/
private abbrev State (remaining counted : Reference) :=
FrameState [] Memory.empty [.word32, .word32] (W32 × W32)
(fun _ => [.bytes .word32 remaining, .bytes .word32 counted])
fun values => [some (.i32 values.1), some (.i32 values.2)]
/-- The loop test, with the remaining count on the stack. -/
bind_cutpoint header (remaining counted : Reference) (state : State remaining counted) :=
countBody at loop.latch matching (· matches .brtrue _)
with entry.frame remaining counted, [.scalar (.i32 state.values.1)], state.memory
/-- `count + remaining` is the input. -/
private def invariant {remaining counted : Reference} (value : W32)
(state : State remaining counted) : Prop :=
state.values.2 + state.values.1 = value
/-- Uses only `decrementContract` at the call site; the helper body is never unfolded. -/
theorem terminates (holds : decrementContract.Holds Extracted.program decrement)
(args : List CIL.Safety.Value) (value : W32) {remaining counted : Reference}
(state : State remaining counted) (valid : invariant value state) :
LoopTerminates Extracted.program countCalls args
(fun _ values => values = [.scalar (.i32 value)]) (header remaining counted state) := by
apply loop_terminates_induction Extracted.program countCalls args (header remaining counted)
(invariant value) (fun s => s.values.1.toNat) _ ?_ state valid
rintro ⟨⟨x, count⟩, m, frame⟩ total recurse
dsimp only [invariant] at total recurse
by_cases zero : x = 0
· subst x
have same : count = value := by simpa using total
subst same
cil_loop_finish
· cil_loop_advance
-- The callee may change only its empty footprint, so both locals survive the call.
cil_loop_call holds x at decrementSite
cil_loop_recurse recurse (x - 1, count + 1)
case invariant =>
show count + 1 + (x - 1) = value
rw [← total]
bv_omega
case measure =>
show (x - 1).toNat < x.toNat
have positive : 0 < x.toNat := by
have : x.toNat ≠ 0 := fun h => zero (BitVec.eq_of_toNat_eq h)
omega
rw [BitVec.toNat_sub_of_le]
· simp; omega
· change 1 ≤ x.toNat
omega
/-- `CountCalls` returns its input for every `uint`, given only the helper's contract. -/
theorem count_calls (holds : decrementContract.Holds Extracted.program decrement) :
CIL.Safety.Ensures Extracted.program countCalls
(fun args initial => ∃ x : W32, args = [.scalar (.i32 x)] ∧ initial = Memory.empty)
(fun args _ _ values => values = args) := by
apply ensures_of_loop
intro args initial permitted
obtain ⟨x, rfl, rfl⟩ := permitted
obtain ⟨remaining, counted, entered, enter⟩ := entry.checked x
have started :
LoopTerminates Extracted.program countCalls [.scalar (.i32 x)]
(fun _ values => values = [.scalar (.i32 x)])
⟨0, entry.frame remaining counted, [], entered⟩ := by
cil_loop_recurse (terminates holds [.scalar (.i32 x)] x) (x, 0)
case invariant => simp [invariant]
exact ⟨countBody, entry.frame remaining counted, entered, countCalls.found, rfl, enter.setup,
started⟩
theorem correctness : CIL.Safety.Ensures Extracted.program countCalls
(fun args initial => ∃ x : W32, args = [.scalar (.i32 x)] ∧ initial = Memory.empty)
(fun args _ _ values => values = args) :=
count_calls decrement_holds
#print axioms decrement_holds
#print axioms terminates
#print axioms count_calls
#print axioms correctness
/-- From empty memory, every count is returned unchanged. -/
def countCallsContract : MethodContract W32 where
args input := [.scalar (.i32 input)]
pre _ memory := memory = Memory.empty
footprint _ := []
post input _ _ values := values = [.scalar (.i32 input)]
/-- `correctness` as `countCallsContract`, which also states that no caller storage changes. -/
theorem holds : countCallsContract.Holds Extracted.program countCalls := by
rintro value _ rfl
obtain ⟨fuel, final, values, ran, result⟩ := correctness _ _ ⟨value, rfl, rfl⟩
exact ⟨fuel, (final, values), ran, by constructor <;> simp [Memory.empty], values, rfl, result⟩
#print axioms holds
end ExampleProof