-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMachine.v
415 lines (335 loc) · 9.48 KB
/
Machine.v
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
From iVM Require Export Operations Binary.
From iVM Require Import DSet.
Require iVM.OpCodes.
Import DSetNotations.
Unset Suggest Proof Using.
Set Implicit Arguments.
Notation toB8 := (toBits 8).
Notation toB16 := (toBits 16).
Notation toB32 := (toBits 32).
Notation toB64 := (toBits 64).
Open Scope Z.
Open Scope vector.
Coercion bytesToBits : Bytes >-> Bits.
Coercion bitsToN : Bits >-> N.
(** Now we have coercions [Bytes >-> Bits >-> N >-> Z]
and also [nat >-> Z] and [option >-> bool >-> Prop]. *)
(* #[global] parameters! *)
Context
(available': B64 -> bool)
(allInputImages' : list (Image B8)).
Module concreteParameters <: MachineParameters.
Definition Addr := B64.
Instance H_eqdec : EqDec B64.
Proof.
typeclasses eauto.
Qed.
Definition available := available'.
Definition offset := fun (z: Z) (a: B64) => toB64 (z + a).
Instance offset_action : Z_action offset.
Proof.
unfold offset.
split; intros.
- cbn. apply toBits_ofN_bitsToN.
- apply toBits_cong.
repeat rewrite ofN_bitsToN.
rewrite fromBits_toBits_cong.
apply eq_cong.
lia.
Qed.
Definition Cell := B8.
Definition InputColor := B8.
Definition allInputImages := allInputImages'.
Definition OutputColor := (B64 * B64 * B64)%type.
Definition Char := B32.
Definition Byte := B8.
Definition Sample := B16.
End concreteParameters.
Module ConcreteCore := Core concreteParameters.
Export ConcreteCore.
(* Why is this needed? *)
#[global] Opaque loadMany.
#[global] Opaque load.
#[global] Opaque popMany.
(* TODO: Is there a more elegant way to achieve this? *)
Definition cells_to_bytes {n} : Cells n -> Bytes n := id.
Coercion cells_to_bytes : Cells >-> Bytes.
Section machine_section.
(** We leave these assumptions abstract in order improve proof search.
In Concete.v we have shown that they hold in our standard model. *)
Context {MP1: MachineParams1}.
Context {MP2: MachineParams2}.
Definition toBytes (n: nat) z := bitsToBytes (toBits (n * 8) z).
Coercion to_list : vector >-> list.
Definition pushZ (z: Z) : M unit :=
pushMany (toBytes 8 z).
Definition pop64 : M B64 :=
let* bytes := popMany 8 in
ret (bytes : B64).
Definition storeZ (n: nat) (a: Z) (x: Z) : M unit :=
storeMany (toB64 a) (toBytes n x).
Import OpCodes.
(* Without [noind] solving obligations seems to go on forever. *)
Equations(noind) oneStep' (op: Z) : M unit :=
oneStep' NOP := ret tt;
oneStep' JUMP :=
let* a := pop64 in
put' PC a;
oneStep' JZ_FWD :=
let* o := next 1 in
let* x := pop64 in
(if (decide (x = 0 :> Z))
then
let* pc := get' PC in
put' PC (offset o pc)
else
ret tt);
oneStep' JZ_BACK :=
let* o := next 1 in
let* x := pop64 in
(if (decide (x = 0 :> Z))
then
let* pc := get' PC in
put' PC (offset (-(1 + o)) pc)
else
ret tt);
oneStep' SET_SP :=
let* a := pop64 in
put' SP a;
oneStep' GET_PC =>
let* a := get' PC in
pushZ a;
oneStep' GET_SP :=
let* a := get' SP in
pushZ a;
oneStep' PUSH0 :=
pushZ 0;
oneStep' PUSH1 :=
let* x := next 1 in
pushZ x;
oneStep' PUSH2 :=
let* x := next 2 in
pushZ x;
oneStep' PUSH4 :=
let* x := next 4 in
pushZ x;
oneStep' PUSH8 :=
let* x := next 8 in
pushZ x;
oneStep' LOAD1 :=
let* a := pop64 in
let* x := loadMany 1 a in
pushZ x;
oneStep' LOAD2 :=
let* a := pop64 in
let* x := loadMany 2 a in
pushZ x;
oneStep' LOAD4 :=
let* a := pop64 in
let* x := loadMany 4 a in
pushZ x;
oneStep' LOAD8 :=
let* a := pop64 in
let* x := loadMany 8 a in
pushZ x;
oneStep' STORE1 :=
let* a := pop64 in
let* x := pop64 in
storeZ 1 a x;
oneStep' STORE2 :=
let* a := pop64 in
let* x := pop64 in
storeZ 2 a x;
oneStep' STORE4 :=
let* a := pop64 in
let* x := pop64 in
storeZ 4 a x;
oneStep' STORE8 :=
let* a := pop64 in
let* x := pop64 in
storeZ 8 a x;
oneStep' ADD :=
let* x := pop64 in
let* y := pop64 in
pushZ (x + y);
oneStep' MULT :=
let* x := pop64 in
let* y := pop64 in
pushZ (x * y);
oneStep' DIV :=
let* x := pop64 in
let* y := pop64 in
pushZ (if decide (x = 0 :> Z) then 0 else y / x);
oneStep' REM :=
let* x := pop64 in
let* y := pop64 in
pushZ (if decide (x = 0 :> Z) then 0 else y mod x);
oneStep' LT :=
let* x := pop64 in
let* y := pop64 in
pushZ (if decide (y < x) then -1 else 0);
oneStep' AND :=
let* x := pop64 in
let* y := pop64 in
pushZ (Vector.map2 andb x y : B64);
oneStep' OR :=
let* x := pop64 in
let* y := pop64 in
pushZ (Vector.map2 orb x y : B64);
oneStep' NOT :=
let* x := pop64 in
pushZ (Vector.map negb x : B64);
oneStep' XOR :=
let* x := pop64 in
let* y := pop64 in
pushZ (Vector.map2 xorb x y : B64);
oneStep' POW2 :=
let* x := pop64 in
pushZ (2 ^ x);
oneStep' READ_FRAME :=
let* i := pop64 in
let* pair := readFrame i in
pushZ (fst pair);;
pushZ (snd pair);
oneStep' READ_PIXEL :=
let* y := pop64 in
let* x := pop64 in
let* c := readPixel x y in
pushZ c;
oneStep' NEW_FRAME :=
let* r := pop64 in
let* h := pop64 in
let* w := pop64 in
newFrame w h r;
oneStep' SET_PIXEL :=
let* b := pop64 in
let* g := pop64 in
let* r := pop64 in
let* y := pop64 in
let* x := pop64 in
setPixel x y (r, g, b);
oneStep' ADD_SAMPLE :=
let* r := pop64 in
let* l := pop64 in
addSample (toB16 l) (toB16 r);
oneStep' PUT_CHAR :=
let* c := pop64 in
putChar (toB32 c);
oneStep' PUT_BYTE :=
let* b := pop64 in
putByte (toB8 b);
oneStep' _ := err.
Definition oneStep : M bool :=
let* op := next 1 in
match (op: Z) with
| EXIT => ret false
| _ => oneStep' op;; ret true
end.
(** ** Chaining steps *)
Definition chain (u v : M bool) :=
let* cont := u in
if cont then v else ret false.
Lemma true_chain (u: M bool) : chain (ret true) u = u.
Proof.
unfold chain. rewrite ret_bind. reflexivity.
Qed.
Lemma chain_true (u: M bool) : chain u (ret true) = u.
Proof.
unfold chain.
rewrite <- bind_ret.
apply bind_extensional.
intros [|]; reflexivity.
Qed.
Lemma chain_assoc (u v w : M bool) : chain (chain u v) w = chain u (chain v w).
Proof.
unfold chain.
smon_rewrite.
f_equal.
extensionality x.
destruct x; [reflexivity|].
rewrite ret_bind.
reflexivity.
Qed.
(** In other words, this defines a monoid (up to functional extensionality). *)
Lemma false_chain (u: M bool) : chain (ret false) u = ret false.
Proof.
unfold chain. rewrite ret_bind. reflexivity.
Qed.
Equations nSteps (n: nat) : M bool :=
nSteps 0 := ret true;
nSteps (S n) := chain oneStep (nSteps n).
Lemma nSteps_action (m n: nat) : nSteps (m + n) = chain (nSteps m) (nSteps n).
Proof.
revert n. induction m; intros n; simpl Nat.add; simp nSteps.
- rewrite true_chain. reflexivity.
- rewrite chain_assoc, IHm. reflexivity.
Qed.
(** *** Extra properties *)
Equations popN (n: nat) : M (vector B64 n) :=
popN 0 := ret [];
popN (S n) := let* h := pop64 in
let* r := popN n in
ret (h :: r).
Proposition popN_spec n :
popN n =
let* u := popMany (n * 8) in
ret (bytesToLongs u).
Proof.
induction n; simp popN.
- simp popMany. smon_rewrite.
- change (S n * 8)%nat with (S (S (S (S (S (S (S (S (n * 8)))))))))%nat.
setoid_rewrite IHn.
unfold pop64.
simp popMany.
smon_rewrite.
setoid_rewrite bytesToLongs_equation_2'.
reflexivity.
Qed.
(** Probably not safe to define as a global instance. *)
Instance disjoint_independent' u v (H: u # v) : Independent (MEM' u) (MEM' v).
Proof.
unfold MEM'.
apply
composite_independent_r,
separate_independent. (* TODO: Rename and move. *)
exact H.
Qed.
(* TODO: Safe as global instance? *)
#[global] Instance mem_point_sub {a u} (Ha: a ∈ u) :
(MEM'' a | MEM' u).
Proof.
unfold MEM', MEM''.
apply sublens_comp, pointLens_sublens.
exact Ha.
Qed.
Lemma mem_put_get'' {a u} (Ha: a ∈ u) f :
put' (MEM' u) f;;
get' (MEM'' a) =
put' (MEM' u) f;;
ret (f a Ha).
Proof.
unfold MEM', MEM''.
set (PL := pointLens _).
set (RL := restrLens _).
assert (PL|RL) as H;
[ apply pointLens_sublens; exact Ha | ].
setoid_rewrite <- (sublensFactor_spec H (fun _ _ => None)).
setoid_rewrite <- compositeLens_associative.
setoid_rewrite sub_put_get. f_equal.
extensionality x. destruct x. f_equal.
cbn. decided Ha. reflexivity.
Qed.
Proposition put_to_get
{A} (LA: Lens State A)
{X} (f g: A -> M X)
(H: forall a,
put' LA a;; f a =
put' LA a;; g a) :
let* a := get' LA in f a =
let* a := get' LA in g a.
Proof.
smon_ext' LA a.
setoid_rewrite lens_put_get.
exact (H a).
Qed.
End machine_section.