-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidTerm.v
451 lines (397 loc) · 14 KB
/
ValidTerm.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
From Coq Require Import Lia.
From Elo Require Import Core.
From Elo Require Import NoRef.
From Elo Require Import NoInit.
From Elo Require Import NoCR.
(* ------------------------------------------------------------------------- *)
(* valid-term *)
(* ------------------------------------------------------------------------- *)
(*
Enforces that:
- addresses are within the bounds of the memory.
- static blocks do not contain <init>s and <cr>s.
*)
Inductive valid_term (m : mem) : tm -> Prop :=
| vtm_unit : valid_term m <{unit }>
| vtm_nat : forall n, valid_term m <{nat n }>
| vtm_plus : forall t1 t2, valid_term m t1 ->
valid_term m t2 ->
valid_term m <{t1 + t2 }>
| vtm_monus : forall t1 t2, valid_term m t1 ->
valid_term m t2 ->
valid_term m <{t1 - t2 }>
| vtm_seq : forall t1 t2, valid_term m t1 ->
valid_term m t2 ->
valid_term m <{t1; t2 }>
| vtm_if : forall t1 t2 t3, no_inits t2 ->
no_crs t2 ->
no_inits t3 ->
no_crs t3 ->
valid_term m t1 ->
valid_term m t2 ->
valid_term m t3 ->
valid_term m <{if t1 then t2 else t3 end}>
| vtm_while : forall t1 t2, no_inits t1 ->
no_crs t1 ->
no_inits t2 ->
no_crs t2 ->
valid_term m t1 ->
valid_term m t2 ->
valid_term m <{while t1 do t2 end }>
| vtm_var : forall x, valid_term m <{var x }>
| vtm_fun : forall x Tx t, no_inits t ->
no_crs t ->
valid_term m t ->
valid_term m <{fn x Tx t }>
| vtm_call : forall t1 t2, valid_term m t1 ->
valid_term m t2 ->
valid_term m <{call t1 t2 }>
| vtm_ref : forall ad T, ad < #m ->
valid_term m <{&ad : T }>
| vtm_init : forall ad t T, ad < #m ->
valid_term m t ->
valid_term m <{init ad t : T}>
| vtm_new : forall T t, no_inits t ->
no_crs t ->
valid_term m t ->
valid_term m <{new t : T }>
| vtm_load : forall t, valid_term m t ->
valid_term m <{*t }>
| vtm_asg : forall t1 t2, valid_term m t1 ->
valid_term m t2 ->
valid_term m <{t1 := t2 }>
| vtm_acq : forall t1 x t2, no_inits t2 ->
no_crs t2 ->
valid_term m t1 ->
valid_term m t2 ->
valid_term m <{acq t1 x t2 }>
| vtm_cr : forall ad t, ad < #m ->
valid_term m t ->
valid_term m <{cr ad t }>
| vtm_spawn : forall t, no_inits t ->
no_crs t ->
valid_term m t ->
valid_term m <{spawn t }>
.
(* inversion --------------------------------------------------------------- *)
Local Ltac _vtm tt :=
match goal with
| H : valid_term _ <{unit }> |- _ => clear H
| H : valid_term _ <{nat _ }> |- _ => clear H
| H : valid_term _ <{_ + _ }> |- _ => tt H
| H : valid_term _ <{_ - _ }> |- _ => tt H
| H : valid_term _ <{_; _ }> |- _ => tt H
| H : valid_term _ <{if _ then _ else _ end}> |- _ => tt H
| H : valid_term _ <{while _ do _ end }> |- _ => tt H
| H : valid_term _ <{var _ }> |- _ => clear H
| H : valid_term _ <{fn _ _ _ }> |- _ => tt H
| H : valid_term _ <{call _ _ }> |- _ => tt H
| H : valid_term _ <{& _ : _ }> |- _ => tt H
| H : valid_term _ <{new _ : _ }> |- _ => tt H
| H : valid_term _ <{init _ _ : _ }> |- _ => tt H
| H : valid_term _ <{* _ }> |- _ => tt H
| H : valid_term _ <{_ := _ }> |- _ => tt H
| H : valid_term _ <{acq _ _ _ }> |- _ => tt H
| H : valid_term _ <{cr _ _ }> |- _ => tt H
| H : valid_term _ <{spawn _ }> |- _ => tt H
end.
Ltac inv_vtm := _vtm inv.
Ltac invc_vtm := _vtm invc.
(* lemmas ------------------------------------------------------------------ *)
Lemma vtm_from_base : forall m t,
no_refs t ->
no_inits t ->
no_crs t ->
valid_term m t.
Proof.
intros. induction t; invc_norefs; invc_noinits; invc_nocrs;
eauto using valid_term.
Qed.
Lemma vtm_insert_term : forall m t1 t2 ad t T,
valid_term m t1 ->
t1 --[e_insert ad t T]--> t2 ->
valid_term m t.
Proof.
intros. ind_tstep; invc_vtm; auto.
Qed.
Lemma vtm_write_term : forall m t1 t2 ad t,
valid_term m t1 ->
t1 --[e_write ad t]--> t2 ->
valid_term m t.
Proof.
intros. ind_tstep; invc_vtm; auto.
Qed.
Lemma vtm_insert_address : forall m t1 t2 ad t T,
valid_term m t1 ->
t1 --[e_insert ad t T]--> t2 ->
ad < #m.
Proof.
intros. ind_tstep; invc_vtm; auto.
Qed.
Lemma vtm_write_address : forall m t1 t2 ad t,
valid_term m t1 ->
t1 --[e_write ad t]--> t2 ->
ad < #m.
Proof.
intros. ind_tstep; repeat invc_vtm; auto.
Qed.
(* lemmas about no-init and no-cr ------------------------------------------ *)
(* spawn ------------------------ *)
Lemma noinit_spawn_term : forall ad m t1 t2 tid t,
valid_term m t1 ->
t1 --[e_spawn tid t]--> t2 ->
no_init ad t.
Proof.
intros. ind_tstep; invc_vtm; auto.
Qed.
Lemma nocr_spawn_term : forall ad m t1 t2 tid t,
valid_term m t1 ->
t1 --[e_spawn tid t]--> t2 ->
no_cr ad t.
Proof.
intros. ind_tstep; invc_vtm; auto.
Qed.
Corollary noinits_spawn_term : forall m t1 t2 tid t,
valid_term m t1 ->
t1 --[e_spawn tid t]--> t2 ->
no_inits t.
Proof.
unfold no_inits. eauto using noinit_spawn_term.
Qed.
Corollary nocrs_spawn_term : forall m t1 t2 tid t,
valid_term m t1 ->
t1 --[e_spawn tid t]--> t2 ->
no_crs t.
Proof.
unfold no_crs. eauto using nocr_spawn_term.
Qed.
(* bounds ----------------------- *)
Lemma noinit_from_vtm1 : forall m t,
valid_term m t ->
no_init (#m) t.
Proof.
intros. induction t; invc_vtm; auto using no_init.
Qed.
Lemma noinit_from_vtm2 : forall ad m t,
valid_term m t ->
#m < ad ->
no_init ad t.
Proof.
intros. induction t; invc_vtm; auto using no_init.
match goal with |- no_init ?ad1 <{init ?ad2 _ : _}> => nat_eq_dec ad1 ad2 end;
auto using no_init. lia.
Qed.
Lemma nocr_from_vtm1 : forall m t,
valid_term m t ->
no_cr (#m) t.
Proof.
intros. induction t; invc_vtm; auto using no_cr.
Qed.
Lemma nocr_from_vtm2 : forall ad m t,
valid_term m t ->
#m < ad ->
no_cr ad t.
Proof.
intros. induction t; invc_vtm; auto using no_cr.
match goal with |- no_cr ?ad1 <{cr ?ad2 _}> => nat_eq_dec ad1 ad2 end;
auto using no_cr. lia.
Qed.
(* values ----------------------- *)
Lemma noinit_from_value : forall m ad t,
valid_term m t ->
(* --- *)
value t ->
no_init ad t.
Proof.
intros * ? Hval. invc Hval; invc_vtm; auto using no_init.
Qed.
Lemma nocr_from_value : forall m ad t,
valid_term m t ->
(* --- *)
value t ->
no_cr ad t.
Proof.
intros * ? Hval. invc Hval; invc_vtm; auto using no_cr.
Qed.
Corollary noinits_from_value : forall m t,
valid_term m t ->
(* --- *)
value t ->
no_inits t.
Proof.
unfold no_inits. eauto using noinit_from_value.
Qed.
Corollary nocrs_from_value : forall m t,
valid_term m t ->
(* --- *)
value t ->
no_crs t.
Proof.
unfold no_crs. eauto using nocr_from_value.
Qed.
(* preservation lemmas ----------------------------------------------------- *)
Lemma vtm_subst : forall m t tx x,
no_inits t ->
no_crs t ->
value tx ->
(* --- *)
valid_term m t ->
valid_term m tx ->
valid_term m <{[x := tx] t}>.
Proof.
intros. induction t; invc_noinits; invc_nocrs; invc_vtm;
simpl; try destruct _str_eq_dec; auto using valid_term;
constructor;
eauto using noinits_from_value, noinits_subst;
eauto using nocrs_from_value, nocrs_subst.
Qed.
Lemma vtm_mem_add : forall m t c,
valid_term m t ->
valid_term (m +++ c) t.
Proof.
intros. induction t; invc_vtm; constructor; sigma; auto.
Qed.
Lemma vtm_mem_set : forall m t ad' t',
valid_term m t ->
valid_term m[ad'.t <- t'] t.
Proof.
intros. induction t; invc_vtm; constructor; sigma; auto.
Qed.
Lemma vtm_mem_acq : forall m t ad,
valid_term m t ->
valid_term m[ad.X <- true] t.
Proof.
intros. induction t; invc_vtm; constructor; sigma; auto.
Qed.
Lemma vtm_mem_rel : forall m t ad,
valid_term m t ->
valid_term m[ad.X <- false] t.
Proof.
intros. induction t; invc_vtm; constructor; sigma; auto.
Qed.
Lemma vtm_mem_region : forall m t ad R,
valid_term m t ->
valid_term m[ad.R <- R] t.
Proof.
intros. induction t; invc_vtm; constructor; sigma; auto.
Qed.
(* preservation ------------------------------------------------------------ *)
Local Ltac solve_vtm_preservation :=
intros; ind_tstep; repeat invc_vtm; repeat constructor; sigma;
auto using vtm_subst, vtm_mem_add, vtm_mem_set, vtm_mem_acq, vtm_mem_rel.
Lemma vtm_preservation_none : forall m t1 t2,
valid_term m t1 ->
t1 --[e_none]--> t2 ->
valid_term m t2.
Proof. solve_vtm_preservation. Qed.
Lemma vtm_preservation_alloc : forall m t1 t2 T,
valid_term m t1 ->
t1 --[e_alloc (#m) T]--> t2 ->
valid_term (m +++ (None, T, false, R_invalid)) t2.
Proof. solve_vtm_preservation. Qed.
Lemma vtm_preservation_insert : forall m t1 t2 ad t T,
valid_term m t1 ->
t1 --[e_insert ad t T]--> t2 ->
valid_term (m[ad.t <- t]) t2.
Proof. solve_vtm_preservation. Qed.
Lemma vtm_preservation_read : forall m t1 t2 ad t,
valid_term m t ->
(* --- *)
valid_term m t1 ->
t1 --[e_read ad t]--> t2 ->
valid_term m t2.
Proof. solve_vtm_preservation. Qed.
Lemma vtm_preservation_write : forall m t1 t2 ad t,
valid_term m t1 ->
t1 --[e_write ad t]--> t2 ->
valid_term (m[ad.t <- t]) t2.
Proof. solve_vtm_preservation. Qed.
Lemma vtm_preservation_acq : forall m t1 t2 ad t,
value t ->
valid_term m t ->
(* --- *)
valid_term m t1 ->
t1 --[e_acq ad t]--> t2 ->
valid_term m[ad.X <- true] t2.
Proof. solve_vtm_preservation. Qed.
Lemma vtm_preservation_rel : forall m t1 t2 ad,
valid_term m t1 ->
t1 --[e_rel ad]--> t2 ->
valid_term m[ad.X <- false] t2.
Proof. solve_vtm_preservation. Qed.
Lemma vtm_preservation_spawn : forall m t1 t2 tid t,
valid_term m t1 ->
t1 --[e_spawn tid t]--> t2 ->
valid_term m t2.
Proof. solve_vtm_preservation. Qed.
Lemma vtm_preservation_spawned : forall m t1 t2 tid t,
valid_term m t1 ->
t1 --[e_spawn tid t]--> t2 ->
valid_term m t.
Proof. solve_vtm_preservation. Qed.
(* ------------------------------------------------------------------------- *)
Corollary vtm_preservation_memory : forall m1 m2 ths1 ths2 tid e,
forall_threads ths1 (valid_term m1) ->
forall_memory m1 (valid_term m1) ->
m1 \ ths1 ~~[tid, e]~~> m2 \ ths2 ->
forall_memory m2 (valid_term m2).
Proof.
intros. invc_cstep; try invc_mstep; trivial; intros ? ? ?.
- upsilon. eauto using vtm_mem_add.
- upsilon; eauto using vtm_insert_term, vtm_mem_set.
- upsilon; eauto using vtm_write_term, vtm_mem_set.
- upsilon. eauto using vtm_mem_acq.
- upsilon. eauto using vtm_mem_rel.
Qed.
Corollary vtm_preservation_threads : forall m1 m2 ths1 ths2 tid e,
forall_memory m1 value ->
(* --- *)
forall_memory m1 (valid_term m1) ->
forall_threads ths1 (valid_term m1) ->
m1 \ ths1 ~~[tid, e]~~> m2 \ ths2 ->
forall_threads ths2 (valid_term m2).
Proof.
intros. invc_cstep; try invc_mstep.
- upsilon. eauto using vtm_preservation_none.
- upsilon; eauto using vtm_mem_add, vtm_preservation_alloc.
- upsilon; eauto using vtm_mem_set, vtm_preservation_insert.
- upsilon. eauto using vtm_preservation_read.
- upsilon; eauto using vtm_mem_set, vtm_preservation_write.
- upsilon; eauto using vtm_mem_acq, vtm_preservation_acq.
- upsilon; eauto using vtm_mem_rel, vtm_preservation_rel.
- upsilon; eauto using vtm_preservation_spawn, vtm_preservation_spawned.
Qed.
Theorem vtm_preservation_cstep : forall m1 m2 ths1 ths2 tid e,
forall_memory m1 value ->
(* --- *)
forall_program m1 ths1 (valid_term m1) ->
m1 \ ths1 ~~[tid, e]~~> m2 \ ths2 ->
forall_program m2 ths2 (valid_term m2).
Proof.
intros * ? [? ?] ?. split;
eauto using vtm_preservation_memory, vtm_preservation_threads.
Qed.
Theorem vtm_preservation_rstep : forall m1 m2 ths1 ths2 tid e,
forall_memory m1 value ->
(* --- *)
forall_program m1 ths1 (valid_term m1) ->
m1 \ ths1 ~~~[tid, e]~~> m2 \ ths2 ->
forall_program m2 ths2 (valid_term m2).
Proof.
intros. invc_rstep; eauto using vtm_preservation_cstep.
match goal with _ : _ \ _ ~~[_, _]~~> ?m \ ?ths |- _ =>
assert (forall_program m ths (valid_term m)) as [? ?]
by eauto using vtm_preservation_cstep
end.
split; repeat intro; repeat omicron; upsilon;
auto; eauto using vtm_mem_region.
Qed.
Theorem vtm_preservation_base : forall t,
no_refs t ->
no_inits t ->
no_crs t ->
(* --- *)
forall_program nil (base t) (valid_term nil).
Proof.
auto using forallprogram_base, vtm_from_base, valid_term.
Qed.