forked from strata-org/Strata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCmdEval.lean
More file actions
203 lines (181 loc) · 6.55 KB
/
Copy pathCmdEval.lean
File metadata and controls
203 lines (181 loc) · 6.55 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/-
Copyright Strata Contributors
SPDX-License-Identifier: Apache-2.0 OR MIT
-/
module
public import Strata.DL.Imperative.Cmd
public import Strata.DL.Imperative.EvalContext
namespace Imperative
open Std (ToFormat Format format)
public section
--------------------------------------------------------------------
/--
Symbolic simulation for an Imperative Command.
-/
def Cmd.eval [BEq P.Ident] [EC : EvalContext P S] (σ : S) (c : Cmd P) : Cmd P × S :=
match EC.lookupError σ with
| some _ => (c, σ)
| none =>
match c with
| .init x ty e md =>
match EC.lookup σ x with
| none =>
match e with
| .det expr =>
let (expr, σ) := EC.preprocess σ c expr
let expr := EC.eval σ expr
let σ := EC.update σ x ty expr
let c' := .init x ty (.det expr) md
(c', σ)
| .nondet =>
-- Unconstrained initialization - generate a fresh value
let (expr, σ) := EC.genFreeVar σ x ty
let σ := EC.update σ x ty expr
let c' := .init x ty .nondet md
(c', σ)
| some (xv, xty) => (c, EC.updateError σ (.InitVarExists (x, xty) xv))
| .set x e md =>
match EC.lookup σ x with
| none =>
match e with
| .det expr => (c, EC.updateError σ (.AssignVarNotExists x expr))
| .nondet => (c, EC.updateError σ (.HavocVarNotExists x))
| some (_xv, xty) =>
match e with
| .det expr =>
let (expr, σ) := EC.preprocess σ c expr
let expr := EC.eval σ expr
let σ := EC.update σ x xty expr
let c' := .set x (.det expr) md
(c', σ)
| .nondet =>
let (expr, σ) := EC.genFreeVar σ x xty
let σ := EC.update σ x xty expr
let c' := .set x .nondet (md.pushElem (.var x) (.expr expr))
(c', σ)
| .assert label e md =>
let (e, σ) := EC.preprocess σ c e
let e := EC.eval σ e
let assumptions := EC.getPathConditions σ
let c' := .assert label e md
let propType := convertMetaDataPropertyType md
match EC.denoteBool e with
| some true => -- Proved via evaluation.
(c', EC.deferObligation σ (ProofObligation.mk label propType assumptions e md))
| some false =>
if assumptions.isEmpty then
(c', EC.updateError σ (.AssertFail label e))
else
(c', EC.deferObligation σ (ProofObligation.mk label propType assumptions e md))
| none =>
(c', EC.deferObligation σ (ProofObligation.mk label propType assumptions e md))
| .assume label e md =>
let (e, σ) := EC.preprocess σ c e
let e := EC.eval σ e
let c' := .assume label e md
match EC.denoteBool e with
| some true => -- Satisified via evaluation.
(c', σ)
| some false =>
let σ := EC.addWarning σ (.AssumeFail label e)
(c', EC.addPathCondition σ [.assumption label e])
| none =>
(c', EC.addPathCondition σ [.assumption label e])
| .cover label e md =>
let (e, σ) := EC.preprocess σ c e
let e := EC.eval σ e
let assumptions := EC.getPathConditions σ
let c' := .cover label e md
(c', EC.deferObligation σ (ProofObligation.mk label .cover assumptions e md))
/--
Symbolic simulation for Imperative's Commands.
-/
def Cmds.eval [BEq P.Ident] [EvalContext P S] (σ : S) (cs : Cmds P) : Cmds P × S :=
match cs with
| [] => ([], σ)
| c :: crest =>
let (c, σ) := Cmd.eval σ c
let (crest, σ) := Cmds.eval σ crest
(c :: crest, σ)
--------------------------------------------------------------------
def stuck {P S} [EC : EvalContext P S] (σ : S) (message : String) : S :=
EC.updateError σ (.Misc message)
/--
Concrete execution for an Imperative Command.
This currently has substantial overlap with `eval`,
but it is likely to diverge further in the future,
especially when we add an oracle to make choices for non-deterministic elements.
-/
def Cmd.run {P S} [BEq P.Ident] [EC : EvalContext P S] (σ : S) (c : Cmd P) : S :=
match EC.lookupError σ with
| some _ => σ
| none =>
match c with
| .init x ty e _ =>
match EC.lookup σ x with
| none =>
match e with
| .det expr =>
let (expr, σ) := EC.preprocess σ c expr
let expr := EC.eval σ expr
EC.update σ x ty expr
| .nondet =>
-- Unconstrained initialization - generate a fresh value
-- Reading the value of this variable will cause execution to get stuck,
-- but this still allows the common pattern of initializing a variable
-- and then immediately overwriting it with a deterministic value.
let (expr, σ) := EC.genFreeVar σ x ty
EC.update σ x ty expr
| some (xv, xty) => EC.updateError σ (.InitVarExists (x, xty) xv)
| .set x e _ =>
match EC.lookup σ x with
| none =>
match e with
| .det expr => EC.updateError σ (.AssignVarNotExists x expr)
| .nondet => EC.updateError σ (.HavocVarNotExists x)
| some (_xv, xty) =>
match e with
| .det expr =>
let (expr, σ) := EC.preprocess σ c expr
let expr := EC.eval σ expr
EC.update σ x xty expr
| .nondet =>
-- See .init comment above
let (expr, σ) := EC.genFreeVar σ x xty
EC.update σ x xty expr
| .assert label e _ =>
let (e, σ) := EC.preprocess σ c e
let e := EC.eval σ e
match EC.denoteBool e with
| some true =>
σ
| some false =>
EC.updateError σ (.AssertFail label e)
| none =>
EC.updateError σ (.Misc f!"assert ({label}) condition did not reduce to bool")
| .assume label e _ =>
let (e, σ) := EC.preprocess σ c e
let e := EC.eval σ e
match EC.denoteBool e with
| some true =>
σ
| some false =>
EC.updateError σ (.Misc f!"assume ({label}) condition is false")
| none =>
EC.updateError σ (.Misc f!"assume ({label}) condition did not reduce to bool")
| .cover _ _ _ =>
-- In the future we can record when a cover is true
-- and assert it was hit at least once later on
EC.updateError σ (.Misc s!"cover is not yet supported")
/--
Concrete execution for Imperative's Commands.
-/
def Cmds.run [BEq P.Ident] [EvalContext P S] (σ : S) (cs : Cmds P) : S :=
match cs with
| [] => σ
| c :: crest =>
let σ := Cmd.run σ c
Cmds.run σ crest
---------------------------------------------------------------------
end -- public section
end Imperative