-
Notifications
You must be signed in to change notification settings - Fork 11
/
SemanticProperties.v
558 lines (494 loc) · 15.7 KB
/
SemanticProperties.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
(*! ORAAT | Properties of the semantics used in the one-rule-at-a-time theorem !*)
Require Export Koika.Common Koika.TypedSemantics.
Section Lists.
Lemma list_find_opt_app {A B} (f: A -> option B) (l l': list A) :
list_find_opt f (l ++ l') =
match list_find_opt f l with
| Some x => Some x
| None => list_find_opt f l'
end.
Proof.
induction l; cbn; intros.
- reflexivity.
- rewrite IHl. destruct (f a); reflexivity.
Qed.
Lemma find_none_notb {A B}:
forall (P: A -> option B) l,
(forall a, List.In a l -> P a = None) ->
list_find_opt P l = None.
Proof.
induction l; cbn; intros * Hnot.
- reflexivity.
- pose proof (Hnot a).
destruct (P a); firstorder discriminate.
Qed.
Lemma forallb_exists {A}:
forall f (l: list A),
forallb f l = false <->
exists x, List.In x l /\ f x = false.
Proof.
induction l; cbn; split.
- congruence.
- intros [x (? & ?)]; exfalso; assumption.
- intros H; repeat bool_step; destruct H.
+ exists a; eauto.
+ firstorder.
- intros [x [ [ ? | ? ] Hnx ] ]; subst; try rewrite Hnx.
+ reflexivity.
+ replace (forallb f l) with false by (symmetry; rewrite IHl; eauto);
bool_simpl; reflexivity.
Qed.
End Lists.
Section Logs.
Context {reg_t: Type}.
Context {R: reg_t -> Type}.
Context {REnv: Env reg_t}.
Notation Log := (@_Log reg_t R REnv).
Lemma getenv_logapp:
forall (l l': Log) idx,
REnv.(getenv) (log_app l l') idx =
REnv.(getenv) l idx ++ REnv.(getenv) l' idx.
Proof.
unfold log_app, map2; intros; rewrite getenv_create; reflexivity.
Qed.
Lemma log_find_empty {T} idx (f: @LogEntry (R idx) -> option T):
log_find (log_empty: Log) idx f = None.
Proof.
unfold log_find, log_empty; intros; rewrite getenv_create; reflexivity.
Qed.
Lemma log_find_create {T}:
forall fn idx (f: LogEntry (R idx) -> option T),
log_find (REnv.(create) fn) idx f =
list_find_opt f (fn idx).
Proof.
unfold log_find; intros; rewrite getenv_create; reflexivity.
Qed.
Lemma log_find_app {T} (l l': Log) reg (f: LogEntry (R reg) -> option T) :
log_find (log_app l l') reg f =
match log_find l reg f with
| Some x => Some x
| None => log_find l' reg f
end.
Proof.
unfold log_find, log_app, map2.
rewrite getenv_create.
rewrite list_find_opt_app.
reflexivity.
Qed.
Lemma log_cons_eq :
forall (log: Log) idx le,
REnv.(getenv) (log_cons idx le log) idx = List.cons le (REnv.(getenv) log idx).
Proof.
unfold log_cons; intros; rewrite get_put_eq; reflexivity.
Qed.
Lemma log_cons_neq :
forall (log: Log) idx idx' le,
idx <> idx' ->
REnv.(getenv) (log_cons idx' le log) idx = REnv.(getenv) log idx.
Proof.
unfold log_cons; intros; rewrite get_put_neq; eauto.
Qed.
Lemma log_find_cons_eq {T}:
forall (log: Log) idx le (f: _ -> option T),
log_find (log_cons idx le log) idx f =
match f le with
| Some v => Some v
| _ => log_find log idx f
end.
Proof.
unfold log_find; intros;
rewrite log_cons_eq; reflexivity.
Qed.
Lemma log_find_cons_neq {T}:
forall (log: Log) idx idx' le (f: _ -> option T),
idx <> idx' ->
log_find (log_cons idx' le log) idx f =
log_find log idx f.
Proof.
unfold log_find; intros;
rewrite log_cons_neq by eassumption; reflexivity.
Qed.
Lemma log_forallb_not_existsb (log: Log) reg (f: LogEntryKind -> Port -> bool) :
negb (log_existsb log reg f) = log_forallb log reg (fun k p => negb (f k p)).
Proof.
unfold log_existsb, log_forallb.
induction (getenv _ _ _); cbn.
- reflexivity.
- destruct a; cbn.
rewrite negb_orb, IHy.
reflexivity.
Qed.
Lemma log_existsb_log_cons_eq :
forall (log: Log) idx k p v f,
log_existsb (log_cons idx (LE k p v) log) idx f =
f k p || log_existsb log idx f.
Proof.
unfold log_existsb; intros; rewrite log_cons_eq; reflexivity.
Qed.
Lemma log_existsb_log_cons_neq :
forall (log: Log) idx idx' k p v f,
idx <> idx' ->
log_existsb (log_cons idx' (LE k p v) log) idx f =
log_existsb log idx f.
Proof.
unfold log_existsb; intros; rewrite log_cons_neq; eauto.
Qed.
Lemma log_existsb_empty :
forall idx f,
log_existsb (log_empty: Log) idx f = false.
Proof.
unfold log_existsb, log_empty; intros;
rewrite getenv_create; reflexivity.
Qed.
Lemma log_forallb_app:
forall (l l': Log) reg (f: LogEntryKind -> Port -> bool),
log_forallb (log_app l l') reg f =
log_forallb l reg f && log_forallb l' reg f.
Proof.
unfold log_forallb.
intros; rewrite getenv_logapp.
rewrite !forallb_app; reflexivity.
Qed.
Lemma log_existsb_app:
forall (l l': Log) reg (f: LogEntryKind -> Port -> bool),
log_existsb (log_app l l') reg f =
log_existsb l reg f || log_existsb l' reg f.
Proof.
unfold log_existsb, log_app; intros.
unfold map2; rewrite getenv_create.
rewrite existsb_app; reflexivity.
Qed.
Lemma log_app_assoc:
forall (l l' l'': Log),
log_app l (log_app l' l'') =
log_app (log_app l l') l''.
Proof.
unfold log_app, map2; intros.
apply create_funext; intros.
rewrite !getenv_create.
apply app_assoc.
Qed.
Lemma log_app_empty_l : forall (l: Log),
log_app l log_empty = l.
Proof.
intros.
apply equiv_eq.
unfold equiv, log_app, map2, log_empty; intros.
rewrite !getenv_create, app_nil_r.
reflexivity.
Qed.
Lemma log_app_empty_r : forall (l: Log),
log_app log_empty l = l.
Proof.
intros.
apply equiv_eq.
unfold equiv, log_app, map2, log_empty; intros.
rewrite !getenv_create.
reflexivity.
Qed.
End Logs.
Section LogMaps.
Context {reg_t: Type}.
Context {R1: reg_t -> Type}.
Context {R2: reg_t -> Type}.
Context {REnv: Env reg_t}.
Notation Log1 := (@_Log reg_t R1 REnv).
Notation Log2 := (@_Log reg_t R2 REnv).
Context (f: forall idx : reg_t, R1 idx -> R2 idx).
Lemma log_existsb_log_map_values :
forall (l1: Log1) idx pred,
log_existsb (log_map_values f l1 : Log2) idx pred =
log_existsb l1 idx pred.
Proof.
unfold log_existsb, log_map_values, log_map; intros; rewrite getenv_map.
induction (getenv _ _) as [| hd tl IH].
- reflexivity.
- destruct hd, kind; cbn; rewrite <- IH; reflexivity.
Qed.
Lemma log_map_values_empty :
log_map_values f (log_empty: Log1) = (log_empty: Log2).
Proof.
unfold log_app, log_map_values, log_map, log_empty; intros.
apply equiv_eq; intro; repeat rewrite ?getenv_map, ?getenv_map2, ?getenv_create.
reflexivity.
Qed.
Lemma log_map_values_cons :
forall (L: Log1) idx le,
log_map_values f (log_cons idx le L) =
log_cons idx (LogEntry_map (f idx) le) (log_map_values f L).
Proof.
unfold log_cons, log_map_values, log_map; intros.
apply equiv_eq; intro; repeat rewrite ?getenv_map.
destruct (let _ := REnv.(finite_keys) in eq_dec k idx); subst; cbn.
- rewrite !get_put_eq. cbn. reflexivity.
- rewrite !get_put_neq, !getenv_map; congruence.
Qed.
Lemma log_map_values_log_app :
forall (L l: Log1),
(log_app (log_map_values f L) (log_map_values f l) : Log2) =
log_map_values f (log_app L l).
Proof.
unfold log_app, log_map_values, log_map; intros.
apply equiv_eq; intro; repeat rewrite ?getenv_map, ?getenv_map2.
symmetry; apply map_app.
Qed.
Lemma may_read_log_map_values :
forall (l1: Log1) prt idx,
may_read (log_map_values f l1 : Log2) prt idx =
may_read l1 prt idx.
Proof.
unfold may_read; intros; rewrite !log_existsb_log_map_values; reflexivity.
Qed.
Lemma may_write_log_map_values :
forall (L1 l1: Log1) prt idx,
may_write (log_map_values f L1 : Log2) (log_map_values f l1 : Log2) prt idx =
may_write L1 l1 prt idx.
Proof.
unfold may_write; intros.
repeat setoid_rewrite log_map_values_log_app;
repeat setoid_rewrite log_existsb_log_map_values;
reflexivity.
Qed.
Lemma log_find_log_map_values (pred: forall {T}, LogEntry T -> option T):
forall (l1: Log1) idx,
(forall prt val,
@pred (R2 idx) {| kind := LogRead; port := prt; val := val |} =
match pred {| kind := LogRead; port := prt; val := val |} with
| Some v => Some (f idx v)
| None => None
end) ->
(forall prt val,
pred {| kind := LogWrite; port := prt; val := f idx val |} =
match pred {| kind := LogWrite; port := prt; val := val |} with
| Some v => Some (f idx v)
| None => None
end) ->
log_find (log_map_values f l1: Log2) idx pred =
match log_find l1 idx pred with
| Some v => Some (f idx v)
| None => None
end.
Proof.
unfold log_find, log_map_values, log_map, RLog_map, LogEntry_map; intros * Hr Hw.
rewrite !getenv_map; induction (getenv REnv l1 idx) as [ | hd tl ]; cbn.
- reflexivity.
- destruct hd, kind, port; cbn in *; auto.
all: rewrite IHtl, ?Hr, ?Hw.
all: destruct pred; reflexivity.
Qed.
Lemma latest_write_log_map_values :
forall (l1: Log1) idx,
latest_write (log_map_values f l1 : Log2) idx =
match latest_write l1 idx with
| Some v => Some (f idx v)
| None => None
end.
Proof.
unfold latest_write; intros;
apply log_find_log_map_values; reflexivity.
Qed.
Lemma latest_write0_log_map_values :
forall (l1: Log1) idx,
latest_write0 (log_map_values f l1 : Log2) idx =
match latest_write0 l1 idx with
| Some v => Some (f idx v)
| None => None
end.
Proof.
unfold latest_write0; intros.
apply log_find_log_map_values; destruct prt; reflexivity.
Qed.
Lemma commit_update_log_map_values :
forall (l1: Log1) (r: REnv.(env_t) (fun idx => R1 idx)),
commit_update (Environments.map REnv f r) (log_map_values f l1) =
Environments.map REnv f (commit_update r l1).
Proof.
unfold commit_update; intros; apply equiv_eq; intro.
repeat rewrite ?getenv_create, ?getenv_map.
rewrite latest_write_log_map_values; destruct latest_write; reflexivity.
Qed.
End LogMaps.
Section LatestWrites.
Context {reg_t: Type}.
Context {R: reg_t -> Type}.
Context {REnv: Env reg_t}.
Notation Log := (@_Log reg_t R REnv).
Lemma latest_write0_empty idx:
latest_write0 (log_empty: Log) idx = None.
Proof.
apply log_find_empty.
Qed.
Lemma latest_write0_app :
forall (sl sl': Log) (idx: reg_t),
latest_write0 (log_app sl sl') idx =
match latest_write0 sl idx with
| Some e => Some e
| None => latest_write0 sl' idx
end.
Proof.
unfold latest_write0; eauto using log_find_app.
Qed.
Lemma latest_write0_cons_eq :
forall (log: Log) idx le,
latest_write0 (log_cons idx le log) idx =
match le with
| LE LogWrite P0 v => Some v
| _ => latest_write0 log idx
end.
Proof.
unfold latest_write0; intros.
setoid_rewrite log_find_cons_eq; destruct le, kind, port; reflexivity.
Qed.
Lemma latest_write0_cons_neq :
forall (log: Log) idx idx' le,
idx <> idx' ->
latest_write0 (log_cons idx' le log) idx =
latest_write0 log idx.
Proof.
unfold latest_write0; intros.
setoid_rewrite log_find_cons_neq; auto.
Qed.
Lemma latest_write1_empty idx:
latest_write1 (log_empty: Log) idx = None.
Proof.
apply log_find_empty.
Qed.
Lemma latest_write1_app :
forall (sl sl': Log) idx,
latest_write1 (log_app sl sl') idx =
match latest_write1 sl idx with
| Some e => Some e
| None => latest_write1 sl' idx
end.
Proof.
unfold latest_write1; eauto using log_find_app.
Qed.
Lemma latest_write1_cons_eq :
forall (log: Log) idx le,
latest_write1 (log_cons idx le log) idx =
match le with
| LE LogWrite P1 v => Some v
| _ => latest_write1 log idx
end.
Proof.
unfold latest_write1; intros.
setoid_rewrite log_find_cons_eq; destruct le, kind, port; reflexivity.
Qed.
Lemma latest_write1_cons_neq :
forall (log: Log) idx idx' le,
idx <> idx' ->
latest_write1 (log_cons idx' le log) idx =
latest_write1 log idx.
Proof.
unfold latest_write1; intros.
setoid_rewrite log_find_cons_neq; auto.
Qed.
Lemma latest_write_empty idx:
latest_write (log_empty: Log) idx = None.
Proof.
apply log_find_empty.
Qed.
Lemma latest_write_app :
forall (sl sl': Log) idx,
latest_write (log_app sl sl') idx =
match latest_write sl idx with
| Some e => Some e
| None => latest_write sl' idx
end.
Proof.
unfold latest_write; eauto using log_find_app.
Qed.
Lemma latest_write_cons_eq :
forall (log: Log) idx le,
latest_write (log_cons idx le log) idx =
match le with
| LE LogWrite P v => Some v
| _ => latest_write log idx
end.
Proof.
unfold latest_write; intros.
setoid_rewrite log_find_cons_eq; destruct le, kind, port; reflexivity.
Qed.
Lemma latest_write_cons_neq :
forall (log: Log) idx idx' le,
idx <> idx' ->
latest_write (log_cons idx' le log) idx =
latest_write log idx.
Proof.
unfold latest_write1; intros.
setoid_rewrite log_find_cons_neq; auto.
Qed.
Ltac latest_write_t :=
unfold latest_write, latest_write0, latest_write1, log_find, log_existsb;
induction (getenv REnv _ _);
repeat match goal with
| _ => reflexivity || discriminate
| _ => progress (intros; cbn in * )
| [ H: LogEntry _ |- _ ] => destruct H as [ [ | ] [ | ] ]
| [ H: _ -> _ = _ |- _ ] => rewrite H by eauto
| _ => solve [eauto]
end.
Lemma latest_write_latest_write0 (l: Log) idx:
log_existsb l idx is_write1 = false ->
latest_write l idx = latest_write0 l idx.
Proof. latest_write_t. Qed.
Lemma latest_write_latest_write1 (l: Log) idx:
log_existsb l idx is_write0 = false ->
latest_write l idx = latest_write1 l idx.
Proof. latest_write_t. Qed.
Lemma latest_write_None (l: Log) idx:
log_existsb l idx is_write0 = false ->
log_existsb l idx is_write1 = false ->
latest_write l idx = None.
Proof. latest_write_t. Qed.
Lemma latest_write0_None (l: Log) idx:
log_existsb l idx is_write0 = false ->
latest_write0 l idx = None.
Proof. latest_write_t. Qed.
Lemma latest_write1_None (l: Log) idx:
log_existsb l idx is_write1 = false ->
latest_write1 l idx = None.
Proof. latest_write_t. Qed.
Lemma latest_write_None_latest_write0 (l: Log) idx :
latest_write l idx = None ->
latest_write0 l idx = None.
Proof. latest_write_t. Qed.
Lemma latest_write_None_latest_write1 (l: Log) idx :
latest_write l idx = None ->
latest_write1 l idx = None.
Proof. latest_write_t. Qed.
End LatestWrites.
Section CommitUpdates.
Context {reg_t: Type}.
Context {R: reg_t -> Type}.
Context {REnv: Env reg_t}.
Notation Log := (@_Log reg_t R REnv).
Context (r: REnv.(env_t) R).
Lemma commit_update_assoc:
forall (l l' : Log), commit_update (commit_update r l) l' = commit_update r (log_app l' l).
Proof.
unfold commit_update, log_app, map2, latest_write, log_find; intros.
apply create_funext; intros.
rewrite !getenv_create.
rewrite list_find_opt_app.
destruct list_find_opt; reflexivity.
Qed.
Lemma commit_update_empty:
commit_update r log_empty = r.
Proof.
intros; apply equiv_eq; intro.
unfold commit_update, log_empty, latest_write, log_find; rewrite !getenv_create.
reflexivity.
Qed.
Lemma getenv_commit_update :
forall (l: Log) idx,
REnv.(getenv) (commit_update r l) idx =
match latest_write l idx with
| Some v' => v'
| None => REnv.(getenv) r idx
end.
Proof.
unfold commit_update; intros; rewrite getenv_create.
reflexivity.
Qed.
End CommitUpdates.