Repository navigation
Expand file tree
/
Copy pathCore.lean
More file actions
219 lines (168 loc) · 8.37 KB
/
Copy pathCore.lean
File metadata and controls
219 lines (168 loc) · 8.37 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import Fe.Embed
/-!
# Fe model core
The monad that exported Fe functions are written in. It depends on Lean's standard library
only: the environment and the world are parameters, and each target supplies its own.
-/
namespace Fe
/-- How a run ends other than by returning to its caller. -/
inductive Exit (σ : Type) where
/-- A revert with its return data: an explicit revert, or a failed `assert!` with a message. -/
| revert (data : ByteArray)
/-- A trap with its panic code: checked arithmetic, a failed index check, an `assert!` without
a message. -/
| panic (code : Nat)
/-- A state change attempted where none is allowed. -/
| static
/-- An invalid instruction: `panic()` and `todo()`, which trap. -/
| trap
/-- A read past the end of the return data, where the EVM halts. -/
| oob
/-- The whole run ends successfully from inside a callee, with return data and the world as
it then is. -/
| stop (data : ByteArray) (world : σ)
/-- Whether an exit is a failure that discards the run's changes: a revert or a panic. -/
@[simp] def Exit.reverts {σ : Type} : Exit σ → Bool
| .revert _ | .panic _ => true
| .static | .trap | .oob | .stop _ _ => false
/-- How a run that unwinds ends: it returns `a`, or it reaches a successful halt with `data`,
which its caller makes once it has stored back the lane copies it holds. -/
inductive Unwound (α : Type) where
| ret (a : α)
| halt (data : ByteArray)
/-- The value an unwound run returned, if it returned. -/
def Unwound.ret? {α : Type} : Unwound α → Option α
| .ret a => some a
| .halt _ => none
/-- The data of the successful halt an unwound run reached; empty if it returned. -/
def Unwound.data {α : Type} : Unwound α → ByteArray
| .ret _ => .empty
| .halt data => data
@[simp] theorem Unwound.ret?_ret {α : Type} (a : α) : (Unwound.ret a).ret? = some a := rfl
@[simp] theorem Unwound.ret?_halt {α : Type} (data : ByteArray) :
(Unwound.halt data : Unwound α).ret? = none := rfl
@[simp] theorem Unwound.data_halt {α : Type} (data : ByteArray) :
(Unwound.halt data : Unwound α).data = data := rfl
/-- The model monad over an environment `ρ` and a world `σ`. A run that does not terminate is
`none`, the bottom of `Option`, so recursive definitions can use `partial_fixpoint`. -/
abbrev M (ρ σ : Type) := ReaderT ρ (StateT σ (ExceptT (Exit σ) Option))
namespace M
variable {ρ σ α β : Type}
/-- Run a computation: `none` if it diverges, otherwise how it exited, or its result and the
final world. -/
def exec (m : M ρ σ α) (env : ρ) (world : σ) : Option (Except (Exit σ) (α × σ)) :=
((m.run env).run world).run
def fail (f : Exit σ) : M ρ σ α := throw f
/-- A run that does not terminate. -/
def diverge : M ρ σ α := fun _ _ => (none : Option (Except (Exit σ) (α × σ)))
/-- End the whole run successfully with `data`. -/
def stop (data : ByteArray) : M ρ σ α := do fail (.stop data (← get))
def revert (data : ByteArray) : M ρ σ α := fail (.revert data)
def panic (code : Nat) : M ρ σ α := fail (.panic code)
@[simp] theorem exec_pure (a : α) (env : ρ) (w : σ) :
(pure a : M ρ σ α).exec env w = some (.ok (a, w)) := rfl
@[simp] theorem exec_fail (f : Exit σ) (env : ρ) (w : σ) :
(fail f : M ρ σ α).exec env w = some (.error f) := rfl
@[simp] theorem exec_revert (data : ByteArray) (env : ρ) (w : σ) :
(revert data : M ρ σ α).exec env w = some (.error (.revert data)) := rfl
@[simp] theorem exec_panic (code : Nat) (env : ρ) (w : σ) :
(panic code : M ρ σ α).exec env w = some (.error (.panic code)) := rfl
/-- What follows a branch follows each side of it. -/
theorem ite_bind (c : Prop) [Decidable c] (x y : M ρ σ α) (f : α → M ρ σ β) :
(if c then x else y) >>= f = if c then x >>= f else y >>= f := by
split <;> rfl
@[simp] theorem exec_diverge (env : ρ) (w : σ) :
(diverge : M ρ σ α).exec env w = none := rfl
@[simp] theorem exec_stop (data : ByteArray) (env : ρ) (w : σ) :
(stop data : M ρ σ α).exec env w = some (.error (.stop data w)) := rfl
@[simp] theorem exec_read (env : ρ) (w : σ) :
(read : M ρ σ ρ).exec env w = some (.ok (env, w)) := rfl
@[simp] theorem exec_get (env : ρ) (w : σ) :
(get : M ρ σ σ).exec env w = some (.ok (w, w)) := rfl
@[simp] theorem exec_set (env : ρ) (w w' : σ) :
(set w' : M ρ σ PUnit).exec env w = some (.ok (⟨⟩, w')) := rfl
@[simp] theorem exec_modify (f : σ → σ) (env : ρ) (w : σ) :
(modify f : M ρ σ PUnit).exec env w = some (.ok (⟨⟩, f w)) := rfl
/-- A bind runs its first part, and continues only from a result. -/
@[simp] theorem exec_bind (m : M ρ σ α) (k : α → M ρ σ β) (env : ρ) (w : σ) :
(m >>= k).exec env w =
match m.exec env w with
| none => none
| some (.error f) => some (.error f)
| some (.ok (a, w')) => (k a).exec env w' := by
simp only [exec, bind, ReaderT.bind, ReaderT.run, StateT.bind, StateT.run, ExceptT.bind,
ExceptT.run, ExceptT.mk, Option.bind]
cases h : m env w with
| none => rfl
| some r => cases r with
| error f => rfl
| ok p => rfl
/-- Running a conditional runs the branch taken. -/
theorem exec_ite (c : Prop) [Decidable c] (a b : M ρ σ α) (env : ρ) (w : σ) :
(if c then a else b).exec env w = if c then a.exec env w else b.exec env w := by
split <;> rfl
/-- A map runs its computation and applies the function to a result. -/
@[simp] theorem exec_map (f : α → β) (m : M ρ σ α) (env : ρ) (w : σ) :
(f <$> m).exec env w =
match m.exec env w with
| none => none
| some (.error e) => some (.error e)
| some (.ok (a, w')) => some (.ok (f a, w')) := by
rw [map_eq_pure_bind, exec_bind]
rfl
/-- Run `m`, giving a successful halt it reaches as `.halt data`, in the world it halted with:
a run that holds lane copies stores them back before it halts as recorded. -/
def unwind (m : M ρ σ α) : M ρ σ (Unwound α) := fun env w =>
match m.exec env w with
| none => none
| some (.ok (a, w')) => some (.ok (.ret a, w'))
| some (.error (.stop data w')) => some (.ok (.halt data, w'))
| some (.error f) => some (.error f)
@[simp] theorem exec_unwind (m : M ρ σ α) (env : ρ) (w : σ) :
(unwind m).exec env w =
match m.exec env w with
| none => none
| some (.ok (a, w')) => some (.ok (.ret a, w'))
| some (.error (.stop data w')) => some (.ok (.halt data, w'))
| some (.error f) => some (.error f) := rfl
/-! ## Runs that never stop from inside -/
/-- The computation never ends the run from inside: it returns, fails or diverges. A model
written without `stop` has this property, and a proof that inspects its outcomes needs it. -/
def NoStop (m : M ρ σ α) : Prop :=
∀ env w data w', m.exec env w ≠ some (.error (.stop data w'))
theorem NoStop.pure (a : α) : NoStop (pure a : M ρ σ α) := by
intro env w data w' h; simp at h
theorem NoStop.revert (data : ByteArray) : NoStop (revert data : M ρ σ α) := by
intro env w data' w' h; simp at h
theorem NoStop.panic (code : Nat) : NoStop (panic code : M ρ σ α) := by
intro env w data w' h; simp at h
theorem NoStop.fail_static : NoStop (fail .static : M ρ σ α) := by
intro env w data w' h; simp at h
theorem NoStop.fail_oob : NoStop (fail .oob : M ρ σ α) := by
intro env w data w' h; simp at h
theorem NoStop.read : NoStop (read : M ρ σ ρ) := by
intro env w data w' h; simp at h
theorem NoStop.get : NoStop (get : M ρ σ σ) := by
intro env w data w' h; simp at h
theorem NoStop.modify (f : σ → σ) : NoStop (modify f : M ρ σ PUnit) := by
intro env w data w' h; simp at h
theorem NoStop.bind {m : M ρ σ α} {k : α → M ρ σ β} (hm : NoStop m) (hk : ∀ a, NoStop (k a)) :
NoStop (m >>= k) := by
intro env w data w' h
rw [exec_bind] at h
cases hr : m.exec env w with
| none => rw [hr] at h; cases h
| some r =>
cases r with
| error f => rw [hr] at h; injection h with h; injection h with h; exact hm env w data w' (by rw [hr, h])
| ok p => rw [hr] at h; exact hk p.1 env p.2 data w' h
/-- An unwound run never stops: it gives the halt as its result. -/
theorem NoStop.unwind (m : M ρ σ α) : NoStop (unwind m) := by
intro env w data w' h
rw [exec_unwind] at h
split at h <;> simp_all
theorem NoStop.ite (c : Prop) [Decidable c] {a b : M ρ σ α} (ha : NoStop a) (hb : NoStop b) :
NoStop (if c then a else b) := by
split <;> assumption
end M
end Fe