forked from mit-pdos/perennial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransitions.v
548 lines (478 loc) · 17.2 KB
/
Transitions.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
From Coq Require Import RelationClasses.
From Coq Require Import Setoid.
From Coq Require Import ZArith.
(* make sure tactics use ssreflect rewrite *)
From Coq Require Import ssreflect.
From stdpp Require Import base.
Set Implicit Arguments.
Generalizable All Variables.
Class GenPred T Σ (pred: Σ -> T -> Prop) := genNext : Z -> forall (s:Σ), option {x:T | pred s x}.
Arguments GenPred : clear implicits.
Notation GenType T Σ := (GenPred T Σ (fun _ _ => True)).
Definition fallback_genPred T Σ p : GenPred T Σ p (* | 99 *) := fun _ _ => None.
#[global]
Instance bool_GenType Σ : GenType bool Σ := fun z _ => Some (exist _ (z mod 2 =? 0)%Z I).
Class GenBool T Σ (b: Σ -> T -> bool) := genNextBool : Z -> forall (s:Σ), option {x:T | b s x = true}.
Arguments GenBool : clear implicits.
(* use as dummy generator, non-executable *)
Definition fallback_genBool T Σ b : GenBool T Σ b (*| 99*) := fun _ _ => None.
Section transition.
Context {Σ:Type}.
Inductive transition : Type -> Type :=
| runF {T} (f: Σ -> Σ * T) : transition T
| suchThat {T} (pred: Σ -> T -> Prop) {gen:GenPred T Σ pred} : transition T
| suchThatBool {T} (b: Σ -> T -> bool) {gen:GenBool T Σ b}: transition T
| bind {T T'} (x: transition T') (rx: T' -> transition T) : transition T
.
Arguments suchThat {T} pred {gen}.
Arguments suchThatBool {T} b {gen}.
Definition ret {T} (v:T): transition T :=
runF (fun s => (s, v)).
Definition fmap {T1 T2} (f: T1 -> T2) (r: transition T1): transition T2 :=
bind r (fun x => ret (f x)).
Definition r_mbind : MBind transition :=
fun {A B} rx r => bind r rx.
Definition r_mret : MRet transition := @ret.
Definition r_fmap : FMap transition := @fmap.
Definition reads {T} (f: Σ -> T): transition T :=
runF (fun s => (s, f s)).
Definition modify (f: Σ -> Σ): transition unit :=
runF (fun s => (f s, tt)).
Definition any T {gen:GenType T Σ}: transition T :=
suchThat (fun _ _ => True).
Arguments any T {gen}.
Definition undefined {T}: transition T :=
suchThat (gen:=fun _ _ => None) (fun _ _ => False).
(* transitions still form a Kleene algebra *)
Definition plus {T} (r1 r2: transition T): transition T :=
bind (any bool) (fun b => if b then r1 else r2).
(* assert produces undefined behavior if [pred] does not hold of the state *)
(* like check, but over state instead of for a proposition *)
Definition assert (pred: Σ -> Prop): transition unit :=
suchThat (gen:=fun _ _ => None) (fun s _ => pred s).
Definition ifThenElse P `{!Decision P} {T} (tr1 tr2: transition T): transition T :=
match decide P with
| left _ => tr1
| right _ => tr2
end.
Arguments ifThenElse P {_} {T}.
Definition check P `{!Decision P}: transition unit :=
ifThenElse P (ret tt) (undefined).
Definition checkNot P `{!Decision P}: transition unit :=
ifThenElse P (undefined) (ret tt).
Definition when P `{!Decision P} (r: transition unit): transition unit :=
ifThenElse P r (ret tt).
Definition unless P `{!Decision P} (r: transition unit): transition unit :=
ifThenElse P (ret tt) (r).
Definition unwrap {T} (mx: option T): transition T :=
match mx with
| Some x => ret x
| None => undefined
end.
Theorem ifThenElse_if P `{!Decision P} {T} (r1 r2: transition T) :
P ->
ifThenElse P r1 r2 = r1.
Proof.
intros H.
unfold ifThenElse.
destruct (decide P); auto || contradiction.
Qed.
Theorem ifThenElse_else P `{!Decision P} {T} (r1 r2: transition T) :
~P ->
ifThenElse P r1 r2 = r2.
Proof.
intros H.
unfold ifThenElse.
destruct (decide P); auto || contradiction.
Qed.
Definition next_hint (hints: list Z): Z * list Z :=
match hints with
| z::hints' => (z, hints')
| nil => (0%Z, nil)
end.
Fixpoint interpret (hints: list Z) {T} (tr: transition T): Σ -> option (list Z * Σ * T) :=
match tr with
| runF f => fun s => let (s', v) := f s in
Some (hints, s', v)
| bind r1 rx => fun s => match interpret hints r1 s with
| Some (hints', s', v) =>
interpret hints' (rx v) s'
| None => None
end
| suchThat pred => let (hint, hints') := next_hint hints in
fun s => match genNext hint s with
| Some (exist _ x _) => Some (hints', s, x)
| None => None
end
| suchThatBool b => let (hint, hints') := next_hint hints in
fun s => match genNextBool hint s with
| Some (exist _ x _) => Some (hints', s, x)
| None => None
end
end.
End transition.
Arguments transition Σ T : clear implicits.
Arguments suchThat {Σ T} pred {gen}.
Arguments suchThatBool {Σ T} b {gen}.
Arguments any {Σ} T {gen}.
Arguments check {Σ} P {_}.
Arguments when {Σ} P {_}.
Module relation.
Section state.
Context (Σ:Type).
Definition t T := Σ -> Σ -> T -> Prop.
Inductive runF {T} (f:Σ -> Σ * T): t T :=
| runF_runs : forall s s' v,
f s = (s', v) ->
runF f s s' v.
Theorem runF_runs_to {T} (f: Σ -> Σ * T) s :
runF f s (fst (f s)) (snd (f s)).
Proof.
econstructor.
destruct (f s); auto.
Qed.
Inductive suchThatBool {T} (b: Σ -> T -> bool): t T :=
| suchThatBool_runs : forall s x,
b s x = true ->
suchThatBool b s s x
.
Inductive suchThat {T} (pred: Σ -> T -> Prop): t T :=
| suchThat_runs : forall s x,
pred s x ->
suchThat pred s s x
.
Inductive bind {T1 T2} (r: t T1) (rx: T1 -> t T2): t T2 :=
| bind_runs : forall s1 s2 x s3 x',
r s1 s2 x ->
(rx x) s2 s3 x' ->
bind r rx s1 s3 x'
.
Definition ret {T} (v:T): t T :=
runF (fun s => (s, v)).
Theorem inv_runF {T} (f:Σ -> Σ*T) :
forall s1 s2 v', runF f s1 s2 v' ->
s2 = fst (f s1) /\ v' = snd (f s1).
Proof.
intros.
inversion H; subst.
destruct (f s1); inversion H0; subst; auto.
Qed.
Theorem inv_ret {T} (v:T) :
forall s1 s2 v', ret v s1 s2 v' ->
s2 = s1 /\ v' = v.
Proof.
intros.
apply inv_runF in H; intuition subst.
Qed.
Theorem inv_suchThatBool {T} (b:Σ -> T -> bool) :
forall s1 s2 v', suchThatBool b s1 s2 v' ->
s2 = s1 /\ b s1 v' = true.
Proof.
intros.
inversion H; subst; eauto.
Qed.
Theorem inv_suchThat {T} (pred:Σ -> T -> Prop) :
forall s1 s2 v', suchThat pred s1 s2 v' ->
s2 = s1 /\ pred s1 v'.
Proof.
intros.
inversion H; subst; eauto.
Qed.
Definition fmap {T1 T2} (f: T1 -> T2) (r: t T1): t T2 :=
bind r (fun x => ret (f x)).
Fixpoint denote {T} (tr: transition Σ T): t T :=
match tr with
| Transitions.runF f => runF f
| Transitions.suchThat pred => suchThat pred
| Transitions.suchThatBool b => suchThatBool b
| Transitions.bind r rx => bind (denote r) (fun x => denote (rx x))
end.
Theorem inv_undefined {T} s1 s2 (x: T):
denote undefined s1 s2 x -> False.
Proof. by inversion 1. Qed.
Theorem inv_bind_undefined T (r: unit -> t T) :
forall s1 s2 v,
bind (denote undefined) r s1 s2 v ->
False.
Proof.
inversion 1; subst.
eapply inv_undefined; eauto.
Qed.
Theorem suchThat_gen_run {T} (pred: Σ -> T -> Prop) {gen: GenPred T Σ pred} (hint:Z) :
forall s v H,
gen hint s = Some (exist _ v H) ->
suchThat pred s s v.
Proof.
intros.
simpl.
constructor; eauto.
Qed.
Theorem suchThat_gen0 {T} (pred: Σ -> T -> Prop) {gen: GenPred T Σ pred} :
forall s v H,
gen 0%Z s = Some (exist _ v H) ->
suchThat pred s s v.
Proof.
intros.
eapply suchThat_gen_run; eauto.
Qed.
Instance requiv {T}: Equiv (t T) :=
fun r1 r2 => forall s1 s2 v, r1 s1 s2 v <-> r2 s1 s2 v.
Definition wf_preserved {T} (tr: transition Σ T) (wf: Σ -> Prop) :=
forall s1 s2 x,
wf s1 -> denote tr s1 s2 x ->
wf s2.
Ltac inv H :=
lazymatch type of H with
| ret _ _ _ _ => apply inv_ret in H; destruct H; subst
| _ => inversion H; subst; clear H
end.
Theorem interpret_sound {T} (tr: transition Σ T):
forall hints s s' hints' v,
interpret hints tr s = Some (hints', s', v) ->
denote tr s s' v.
Proof.
induction tr; simpl; intros.
- destruct_with_eqn (f s).
inv H.
eauto using runF_runs.
- destruct_with_eqn (next_hint hints).
destruct (genNext z s); [ | congruence ].
destruct s0.
inv H.
eauto using suchThat_runs.
- destruct_with_eqn (next_hint hints).
destruct (genNextBool z s); [ | congruence ].
destruct s0.
inv H.
eauto using suchThatBool_runs.
- destruct_with_eqn (interpret hints tr s); [ | congruence ].
destruct p as [[hints'' s''] v'].
eapply IHtr in Heqo.
eapply H in H0.
econstructor; eauto.
Qed.
Global Instance equiv_Equiv T : Equivalence (@requiv T).
Proof.
constructor.
- firstorder.
- firstorder.
- intros r1 r2 r3.
unfold requiv; intros Heq1 Heq2 **.
rewrite Heq1 //.
Qed.
Theorem bind_id {T1 T2} (f: T1 -> t T2) x :
bind (ret x) f ≡ f x.
Proof.
intros s1 s2 v.
split; intros.
- inv H. inv H0.
auto.
- econstructor; eauto.
econstructor; eauto.
Qed.
Theorem bind_id' {T} (r: t T) :
bind r (fun x => ret x) ≡ r.
Proof.
intros s1 s2 v.
split; intros.
- inv H. inv H1.
auto.
- econstructor; eauto.
econstructor; eauto.
Qed.
Theorem bind_bind {T1 T2 T3} (r1: t T1) (r2: T1 -> t T2) (r3: T2 -> t T3) :
forall s1 s2 v, bind (bind r1 r2) r3 s1 s2 v -> bind r1 (fun x => bind (r2 x) r3) s1 s2 v.
Proof.
intros s1 s2 v H.
inv H.
inv H0.
eauto using bind_runs.
Qed.
Theorem bind_assoc {T1 T2 T3} (r1: t T1) (r2: T1 -> t T2) (r3: T2 -> t T3) :
bind (bind r1 r2) r3 ≡ bind r1 (fun x => bind (r2 x) r3).
Proof.
intros s1 s2 v.
split; intros.
- eauto using bind_bind.
- inv H.
inv H1.
eauto using bind_runs.
Qed.
Theorem fmap_ret T1 T2 (f: T1 -> T2) x :
fmap f (ret x) ≡ ret (f x).
Proof.
intros s1 s2 v.
split; intros.
- inv H.
inv H0.
inv H1.
econstructor; eauto.
- inv H.
econstructor; eauto.
{ econstructor; eauto. }
{ econstructor; eauto. }
Qed.
Global Instance bind_respects_equiv {T1 T2} :
Proper (requiv ==> pointwise_relation _ requiv ==> requiv) (@bind T1 T2).
Proof.
unfold Proper, respectful, pointwise_relation.
intros r1 r1' Heq1 r2 r2' Heq2.
intros s1 s2 v.
split; intros.
- inv H.
econstructor; eauto.
{ eapply Heq1; eauto. }
{ eapply Heq2; eauto. }
- inv H.
econstructor; eauto.
{ eapply Heq1; eauto. }
{ eapply Heq2; eauto. }
Qed.
Theorem bind_runF T1 T2 (f: Σ -> Σ * T1) (r: T1 -> t T2) :
forall s1 s2 v,
r (f s1).2 (f s1).1 s2 v ->
bind (runF f) r s1 s2 v.
Proof.
intros.
destruct_with_eqn (f s1); simpl in H.
eapply bind_runs; eauto.
econstructor; eauto.
Qed.
Theorem inv_bind_runF T1 T2 (f: Σ -> Σ * T1) (r: T1 -> t T2) :
forall s1 s2 v,
bind (runF f) r s1 s2 v ->
r (f s1).2 (f s1).1 s2 v.
Proof.
intros.
inv H.
inv H0.
replace (f s1); auto.
Qed.
Theorem bind_suchThat T1 T2 (pred: Σ -> T1 -> Prop) (r: T1 -> t T2) :
forall s1 s2 v x,
pred s1 x -> r x s1 s2 v ->
bind (suchThat pred) r s1 s2 v.
Proof.
intros.
econstructor; eauto using suchThat_runs.
Qed.
Theorem inv_bind_suchThat T1 T2 (pred: Σ -> T1 -> Prop) (r: T1 -> t T2) :
forall s1 s2 v,
bind (suchThat pred) r s1 s2 v ->
exists x, pred s1 x /\ r x s1 s2 v.
Proof.
intros.
inv H.
inv H0.
eauto.
Qed.
Theorem inv_bind_suchThatBool T1 T2 (b: Σ -> T1 -> bool) (r: T1 -> t T2) :
forall s1 s2 v,
bind (suchThatBool b) r s1 s2 v ->
exists x, b s1 x = true /\ r x s1 s2 v.
Proof.
intros.
inv H.
inv H0.
eauto.
Qed.
Theorem bind_runF_runF T1 T2 (f: Σ -> Σ * T1) (rx: T1 -> Σ -> Σ * T2) :
bind (runF f) (fun x => runF (rx x)) ≡
runF (fun s => let '(s', x) := f s in
rx x s').
Proof.
intros s1 s2 v.
split; intros.
- inv H.
inv H0.
inv H1.
econstructor.
replace (f s1).
auto.
- inv H.
destruct_with_eqn (f s1).
inv Heqp.
eauto using bind_runs, runF_runs.
Qed.
End state.
End relation.
Arguments ifThenElse {Σ} P {_} {T} tr1 tr2.
Arguments ifThenElse_if {Σ} P {_} {T} r1 r2.
Arguments ifThenElse_else {Σ} P {_} {T} r1 r2.
Ltac monad_simpl :=
unfold check, when;
repeat match goal with
| |- relation.bind (relation.bind _ _) _ ?s1 ?s2 ?v =>
apply relation.bind_assoc
| |- relation.bind (relation.runF _) _ ?s1 ?s2 ?v =>
apply relation.bind_runF
| |- relation.runF _ ?s1 ?s2 ?v =>
solve [ econstructor; eauto ]
| [ H: ?mx = Some ?x |- context[unwrap ?mx] ] =>
rewrite H; cbn [unwrap fst snd]
| [ |- context[relation.denote (ifThenElse ?P _ _)] ] =>
rewrite -> (ifThenElse_if P) by eauto; cbn [relation.denote ret undefined]
| [ |- context[relation.denote (ifThenElse ?P _ _)] ] =>
rewrite -> (ifThenElse_else P) by eauto; cbn [relation.denote ret undefined]
end.
Ltac monad_inv :=
repeat match goal with
| [ H: (_, _) = (_, _) |- _ ] =>
inversion H; subst; clear H
| [ H: relation.denote undefined ?s1 ?s2 ?v |- _ ] =>
apply relation.inv_undefined in H; exfalso; apply H
| [ H: relation.bind (relation.denote undefined) _ ?s1 ?s2 ?v |- _ ] =>
apply relation.inv_bind_undefined in H; exfalso; apply H
| [ H: relation.bind (relation.runF _) _ ?s1 ?s2 ?v |- _ ] =>
apply relation.inv_bind_runF in H
| [ H: relation.runF _ ?s1 ?s2 ?v |- _ ] =>
apply relation.inv_runF in H; simpl in H; destruct H; subst
| [ H: relation.suchThat (fun _ _ => False) ?s1 ?s2 ?v |- _ ] =>
inversion H; contradiction
| [ H: relation.suchThat _ ?s1 ?s2 ?v |- _ ] =>
apply relation.inv_suchThat in H; destruct H; subst
| [ H: relation.suchThatBool (fun _ _ => false) ?s1 ?s2 ?v |- _ ] =>
inversion H; contradiction
| [ H: relation.suchThatBool _ ?s1 ?s2 ?v |- _ ] =>
apply relation.inv_suchThatBool in H; destruct H; subst
| [ H: relation.bind (relation.bind _ _) _ ?s1 ?s2 ?v |- _ ] =>
apply relation.bind_bind in H
| [ H: relation.bind (relation.suchThat ?pred) _ ?s1 ?s2 ?v |- _ ] =>
let pred := (eval hnf in pred) in
let x := lazymatch pred with
| fun _ x => _ => fresh x
(* Hacky support for closures that use patterns on their first arg, see Coq bug #13959 *)
| fun '(_,_) x => _ => fresh x
(* fallback to a "random" name *)
| _ => fresh
end in
let H' := fresh in
apply relation.inv_bind_suchThat in H;
destruct H as [x [H' H]]
| [ H: relation.bind (relation.suchThatBool ?b) _ ?s1 ?s2 ?v |- _ ] =>
let pred := (eval hnf in pred) in
let x := lazymatch pred with
| fun _ x => _ => fresh x
(* Hacky support for closures that use patterns on their first arg, see Coq bug #13959 *)
| fun '(_,_) x => _ => fresh x
(* fallback to a "random" name *)
| _ => fresh
end in
let H' := fresh in
apply relation.inv_bind_suchThatBool in H;
destruct H as [x [H' H]]
| [ H: ?mx = Some ?x, H': context[unwrap ?mx] |- _ ] =>
rewrite H in H'; cbn [fst snd unwrap] in H'
| [ H: context[relation.denote (check ?P)] |- _ ] =>
unfold check in H
| [ H: context[relation.denote (when ?P _)] |- _ ] =>
unfold when in H
| [ H: context[relation.denote (ret _)] |- _ ] =>
unfold ret in H
| [ H: context[relation.denote (runF ?x)] |- _ ] =>
change (relation.denote (runF x)) with (relation.runF x) in H
| [ H: context[relation.denote (ifThenElse ?P _ _)] |- _ ] =>
rewrite -> (ifThenElse_if P) in H by eauto; cbn [relation.denote ret undefined] in H
| [ H: context[relation.denote (ifThenElse ?P _ _)] |- _ ] =>
rewrite -> (ifThenElse_else P) in H by eauto; cbn [relation.denote ret undefined] in H
end.