-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalAsGlobal.v
More file actions
3703 lines (3389 loc) · 116 KB
/
LocalAsGlobal.v
File metadata and controls
3703 lines (3389 loc) · 116 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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Coq.Lists.List.
Require Coq.Program.Equality.
Require Coq.Init.Specif.
Require Coq.Logic.FunctionalExtensionality.
Module Type SemanticInterface.
Import Coq.Logic.FunctionalExtensionality.
(* The type of state. *)
Parameter S : Type.
(* The semantic domain *)
Parameter D : Type -> Type.
(* The algebra for the signature *)
Parameter retD: forall {A}, A -> D A.
Parameter failD : forall {A}, D A.
Parameter orD : forall {A}, D A -> D A -> D A.
Parameter getD : forall {A}, (S -> D A) -> D A.
Parameter putD : forall {A}, S -> D A -> D A.
(* State Laws *)
Parameter get_get_G_D:
forall {A} (k: S -> S -> D A),
getD (fun s => getD (fun s' => k s s'))
=
getD (fun s => k s s).
Parameter get_put_G_D:
forall {A} (p: D A),
getD (fun s => putD s p)
=
p.
Parameter put_get_G_D:
forall {A} (s: S) (k: S -> D A),
putD s (getD k)
=
putD s (k s).
Parameter put_put_G_D:
forall {A} (s s': S) (p: D A),
putD s (putD s' p)
=
putD s' p.
(* Nondeterminism Laws *)
Parameter or1_fail_G_D:
forall {A} (q: D A),
orD failD q
=
q.
Parameter or2_fail_G_D:
forall {A} (p: D A),
orD p failD
=
p.
Parameter or_or_G_D:
forall {A} (p q r: D A),
orD (orD p q) r
=
orD p (orD q r).
(* Global State Law *)
Parameter put_or_G_D:
forall {A} (p q: D A) (s: S),
orD (putD s p) q
=
putD s (orD p q).
(* Further laws governing the interaction of state and nondeterminism *)
Parameter put_ret_or_G_D:
forall {A} (v: S) (w: A) (q: D A),
putD v (orD (retD w) q)
=
orD (putD v (retD w)) (putD v q).
Parameter put_or_comm_G_D:
forall {A} (p q : D A) (t u : S -> S),
getD (fun s => orD (orD (putD (t s) p) (putD (u s) q)) (putD s failD))
=
getD (fun s => orD (orD (putD (u s) q) (putD (t s) p)) (putD s failD)).
End SemanticInterface.
Module Type Syntax (Sem: SemanticInterface).
Import Sem.
Import Coq.Lists.List.
Import Coq.Program.Equality.
Import Coq.Logic.FunctionalExtensionality.
Import Coq.Program.Basics.
Import Coq.Init.Specif.
(* We start out by proving some useful lemmas about the semantic domain. *)
Lemma or_comm_ret_G_D:
forall {A} (x y: A),
orD (retD x) (retD y) = orD (retD y) (retD x).
Proof.
intros.
rewrite <- (get_put_G_D (orD (retD x) (retD y))).
rewrite <- (get_put_G_D (orD (retD y) (retD x))).
assert (H : forall (a b : A),
(fun s => putD s (orD (retD a) (retD b)))
=
(fun s => orD (orD (putD s (retD a)) (putD s (retD b))) (putD s failD))).
- intros; apply functional_extensionality; intro s.
rewrite put_ret_or_G_D.
rewrite <- (or2_fail_G_D (putD s (retD b))) at 1.
rewrite (put_or_G_D (retD b) failD s).
rewrite put_ret_or_G_D.
rewrite or_or_G_D.
reflexivity.
- rewrite (H x y); rewrite (H y x).
apply put_or_comm_G_D.
Qed.
Lemma get_put_G_D':
forall {A} (p: S -> D A),
getD (fun s => putD s (p s))
=
getD p.
Proof.
intros.
assert (H : (fun s => putD s (p s)) = (fun s => putD s (getD (fun s0 => p s0)))).
- apply functional_extensionality; intro s.
rewrite put_get_G_D.
reflexivity.
- rewrite H.
rewrite get_put_G_D.
auto.
Qed.
Lemma get_or_G_D:
forall {A} (p: S -> D A) (q: D A),
getD (fun s => orD (p s) q)
=
orD (getD p) q.
Proof.
intros.
rewrite <- (get_put_G_D (orD (getD p) q)).
assert (H : (fun s => putD s (orD (getD p) q))
= (fun s => putD s (orD (p s) q))).
- apply functional_extensionality; intro s.
rewrite <- put_or_G_D.
rewrite put_get_G_D.
rewrite put_or_G_D.
reflexivity.
- rewrite H.
rewrite get_put_G_D'.
reflexivity.
Qed.
Lemma get_ret_or_G_D:
forall {A} (f : S -> A) (p : S -> D A),
getD (fun s => orD (retD (f s)) (p s))
=
orD (getD (fun s => retD (f s))) (getD p).
Proof.
intros.
rewrite <- get_put_G_D' at 1.
cut ((fun s => putD s (orD (retD (f s)) (p s)))
=
(fun s => putD s (orD (retD (f s)) (getD p)))).
intro H. rewrite H.
rewrite get_put_G_D'.
rewrite get_or_G_D.
reflexivity.
apply functional_extensionality; intro s.
rewrite put_ret_or_G_D.
rewrite <- put_get_G_D.
rewrite <- put_ret_or_G_D.
reflexivity.
Qed.
Lemma get_const:
forall {A} (p : D A),
getD (fun s => p) = p.
Proof.
intros.
rewrite <- get_put_G_D'.
apply get_put_G_D.
Qed.
(* Programs -- the free monad over the signature of fail + or + get + put.
These programs are closed, i.e. they contain no free variables.
*)
Inductive Prog : Type -> Type :=
| Return : forall {A}, A -> Prog A
| Fail : forall {A}, Prog A
| Or : forall {A}, Prog A -> Prog A -> Prog A
| Get : forall {A}, (S -> Prog A) ->Prog A
| Put : forall {A}, S -> Prog A ->Prog A.
Fixpoint bind {A B} (p: Prog A) : (A -> Prog B) -> Prog B :=
match p in (Prog A) return ((A -> Prog B) -> Prog B) with
| Return v => (fun f => f v)
| Fail => (fun f => Fail)
| Or p1 p2 => (fun f => Or (bind p1 f) (bind p2 f))
| Get p => (fun f => Get (fun s => bind (p s) f))
| Put v p => (fun f => Put v (bind p f))
end.
Lemma bind_bind :
forall {A B C} (p: Prog A) (k1: A -> Prog B) (k2: B -> Prog C),
bind (bind p k1) k2 = bind p (fun x => bind (k1 x) k2).
Proof.
intros A B C p;
induction p; intros; simpl; auto.
- rewrite IHp1, IHp2; auto.
- f_equal; apply functional_extensionality; intros; rewrite H; auto.
- rewrite IHp; auto.
Qed.
Lemma bind_return:
forall {A} (p: Prog A),
bind p (fun x => Return x) = p.
Proof.
intros Ap;
induction p; intros; simpl; auto.
- rewrite IHp1, IHp2; auto.
- f_equal; apply functional_extensionality; intros; rewrite H; auto.
- rewrite IHp; auto.
Qed.
Lemma bind_return':
forall {A X} (p: X -> Prog A),
(fun e => bind (p e) (fun x => Return x)) = (fun e => p e).
Proof.
intros.
apply functional_extensionality; intro e0.
apply bind_return.
Qed.
Lemma bind_or_left:
forall {A B} (p1 p2: Prog A) (f: A -> Prog B),
bind (Or p1 p2) f = Or (bind p1 f) (bind p2 f).
Proof.
auto.
Qed.
Lemma bind_if:
forall {A B} (p: bool) (m n: Prog A) (k: A -> Prog B),
bind (if p then m else n) k
=
if p then bind m k else bind n k.
Proof.
intros; destruct p; auto.
Qed.
Lemma bind_left_zero:
forall {A B} (f: A -> Prog B),
bind Fail f = Fail.
Proof.
auto.
Qed.
(* Programs with free variables *)
(* Environment as a heterogeneous list *)
Inductive Env : list Type -> Type :=
| Nil : Env nil
| Cons : forall {A L}, A -> Env L -> Env (A :: L).
Definition tail_ {E} (env: Env E) :=
match env in (Env E) return (match E with nil => unit | X::Xs => Env Xs end) with
| Nil => tt
| Cons x xs => xs
end.
Definition tail {C E} (env: Env (C :: E)): Env E := tail_ env.
Definition head_ {E} (env: Env E) :=
match env in (Env E) return (match E with nil => unit | X::Xs => X end) with
| Nil => tt
| Cons x xs => x
end.
Definition head {C E} (env: Env (C :: E)): C := head_ env.
Lemma cons_head_tail:
forall {A E} (env: Env (A::E)),
Cons (head env) (tail env) = env.
Proof.
intros. dependent destruction env. auto.
Qed.
(* We represent open programs (programs with free variables) abstractly
as a function that produces a closed program when given an environment
*)
Definition OProg (E: list Type) (A: Type): Type := Env E -> Prog A.
Definition comap {A E E'} (p: OProg E A) : (Env E' -> Env E) -> OProg E' A :=
fun f env => p (f env).
(* Filling in the first free variable *)
Definition cpush {A C E} (c: C) (p: OProg (C :: E) A): OProg E A :=
comap p (fun env => Cons c env).
(* Ignoring the first free variable *)
Definition clift {A C E} (p: OProg E A): OProg (C :: E) A := comap p tail.
Lemma cpush_clift :
forall {A C E} (p: OProg E A) (c: C),
cpush c (clift p) = p.
Proof.
intros; unfold cpush, clift, comap; simpl; auto.
Qed.
Definition obind {E A B} (p: OProg E A) : (A -> OProg E B) -> OProg E B :=
fun k env => bind (p env) (fun x => k x env).
Lemma obind_from_bind:
forall {E A B} (p: OProg E A) (k: A -> OProg E B),
(fun env => bind (p env) (fun x => k x env))
=
obind p k.
Proof.
auto.
Qed.
Lemma obind_obind:
forall {A B C E} (p: OProg E A) (k1: A -> OProg E B) (k2: B -> OProg E C),
obind (obind p k1) k2 = obind p (fun x => obind (k1 x) k2).
Proof.
intros.
unfold obind.
apply functional_extensionality; intro env.
apply bind_bind.
Qed.
Lemma obind_cpush:
forall {E A B C} {a: C} {p: OProg (C :: E) A} {k: A -> OProg (C::E) B},
obind (cpush a p) (fun x => cpush a (k x))
=
cpush a (obind p k).
Proof.
intros; unfold obind, cpush, comap; auto.
Qed.
(* Contexts for open programs.
Predicates like (S -> bool) indicate control flow choices based on information
that is not statically available.
Context E1 A E2 B:
(E1, A) is the environment and return type of the subprogram that is
expected in the hole.
(E2, B) is the environment and return type of the whole
program,
So this context can be said to construct an open program that returns
a value of type B given an E2 environment, from an open program that
returns a value of type A given an E1 environment
Note that this context type has no support for monadic bind, and therefore it
deviates from the presentation in the paper.
Further on, we introduce a data type BContext which also supports binds.
*)
Inductive Context : list Type -> Type -> list Type -> Type -> Type :=
| CHole : forall {E A }, Context E A E A
| COr1 : forall {E1 E2 A B }, Context E1 A E2 B ->
OProg E2 B ->
Context E1 A E2 B
| COr2 : forall {E1 E2 A B }, OProg E2 B ->
Context E1 A E2 B ->
Context E1 A E2 B
| CPut : forall {E1 E2 A B }, (Env E2 -> S) ->
Context E1 A E2 B ->
Context E1 A E2 B
| CGet : forall {E1 E2 A B }, (S -> bool) ->
(Context E1 A (S::E2) B) ->
(S -> OProg E2 B) ->
Context E1 A E2 B
| CDelay : forall {E1 E2 A B }, (Env E2 -> bool) ->
Context E1 A E2 B ->
OProg E2 B ->
Context E1 A E2 B
| CPush : forall {E1 E2 A B C}, C ->
Context E1 A (C::E2) B ->
Context E1 A E2 B
| CLift : forall {E1 E2 A B C}, Context E1 A E2 B ->
Context E1 A (C::E2) B.
(* Applying a context to a program: produce a complete program from a program
containing a hole (a context) and a program to be filled into the hole.
*)
Fixpoint appl {E1 E2: list Type} {A B: Type} (c: Context E1 A E2 B) : OProg E1 A -> OProg E2 B :=
match c in (Context E1 A E2 B) return (OProg E1 A -> OProg E2 B) with
| CHole => (fun p env => p env)
| COr1 c q => (fun p env => Or (appl c p env) (q env))
| COr2 p c => (fun q env => Or (p env) (appl c q env))
| CPut v c => (fun p env => Put (v env) (appl c p env))
| CGet t c q => (fun p env => Get (fun s => if t s then cpush s (appl c p) env else q s env))
| CDelay t c q => (fun p => (fun env => if t env then appl c p env else q env))
| CPush x c => (fun p env => cpush x (appl c p) env)
| CLift c => (fun p env => clift (appl c p) env)
end.
(* Applying a context to a context, aka composing contexts *)
Fixpoint applC {E1 E2 E3: list Type} {A B C: Type} (c: Context E2 B E3 C) : Context E1 A E2 B -> Context E1 A E3 C:=
match c in (Context E2 B E3 C) return (Context E1 A E2 B -> Context E1 A E3 C) with
| CHole => (fun d => d)
| COr1 c q => (fun d => COr1 (applC c d) q)
| COr2 p c => (fun d => COr2 p (applC c d))
| CPut v c => (fun d => CPut v (applC c d))
| CGet t c q => (fun d => CGet t (applC c d) q)
| CDelay t c q => (fun d => CDelay t (applC c d) q)
| CPush x c => (fun d => CPush x (applC c d))
| CLift c => (fun d => CLift (applC c d))
end.
(* Alternative zipper-based context.
Whereas the Context datatype offers a top-down view of a program-with-hole,
the ZContext datatype offers a bottom-up view of a program-with-hole.
ZContext E1 A E2 B:
the hole has environment and type (E1,A); the ZContext
transforms a program of that type into a program of type B,
given environment E2.
*)
Inductive ZContext : list Type -> Type -> list Type -> Type -> Type :=
| ZTop : forall {E A},
ZContext E A E A
| ZOr1 : forall {E1 E2 A B},
ZContext E1 A E2 B -> OProg E1 A -> ZContext E1 A E2 B
| ZOr2 : forall {E1 E2 A B},
ZContext E1 A E2 B -> OProg E1 A -> ZContext E1 A E2 B
| ZPut : forall {E1 E2 A B},
ZContext E1 A E2 B -> (Env E1 -> S) -> ZContext E1 A E2 B
| ZGet : forall {E1 E2 A B},
ZContext E1 A E2 B -> (S -> bool) -> (S -> OProg E1 A) -> ZContext (S::E1) A E2 B
| ZDelay : forall {E1 E2 A B},
ZContext E1 A E2 B -> (Env E1 -> bool) -> OProg E1 A -> ZContext E1 A E2 B
| ZPush : forall {E1 E2 A B C},
ZContext E1 A E2 B -> C -> ZContext (C :: E1) A E2 B
| ZLift : forall {E1 E2 A B C},
ZContext (C::E1) A E2 B -> ZContext E1 A E2 B.
Fixpoint zappl {E1 E2: list Type} {A B: Type} (z: ZContext E1 A E2 B) : OProg E1 A -> OProg E2 B :=
match z in (ZContext E1 A E2 B) return (OProg E1 A -> OProg E2 B) with
| ZTop => (fun p env => p env)
| ZOr1 z q => (fun p => zappl z (fun env => Or (p env) (q env)))
| ZOr2 z p => (fun q => zappl z (fun env => Or (p env) (q env)))
| ZPut z v => (fun p => zappl z (fun env => Put (v env) (p env)))
| ZGet z t q => (fun p =>
zappl z (fun env =>
Get (fun s => if t s
then (cpush s p) env
else q s env)))
| ZDelay z t q => (fun p => zappl z (fun env => if t env then p env else q env))
| ZPush z c => (fun p => zappl z (cpush c p))
| ZLift z => (fun p => zappl z (clift p))
end.
(* Functionality for transforming between zipper contexts and regular contexts. *)
Fixpoint toZContext_ {E1 E2 A B} (c: Context E1 A E2 B):
(forall {E3 C}, ZContext E2 B E3 C-> ZContext E1 A E3 C) :=
match c in (Context E1 A E2 B) return ((forall {E3 C}, ZContext E2 B E3 C-> ZContext E1 A E3 C)) with
| CHole => (fun _ _ z => z)
| COr1 c p => (fun _ _ z => toZContext_ c (ZOr1 z p))
| COr2 p c => (fun _ _ z => toZContext_ c (ZOr2 z p))
| CPut s c => (fun _ _ z => toZContext_ c (ZPut z s))
| CGet t c p => (fun _ _ z => toZContext_ c (ZGet z t p))
| CDelay t c p => (fun _ _ z => toZContext_ c (ZDelay z t p))
| CPush a c => (fun _ _ z => toZContext_ c (ZPush z a))
| CLift c => (fun _ _ z => toZContext_ c (ZLift z))
end.
Definition toZContext {E1 E2 A B} (c: Context E1 A E2 B): ZContext E1 A E2 B := toZContext_ c ZTop.
Lemma zappl_toZContext_:
forall {E1 E2 A B} (c: Context E1 A E2 B) {E3 C} (z: ZContext E2 B E3 C) (p: OProg E1 A),
zappl (toZContext_ c z) p = zappl z (appl c p).
Proof.
intros E1 E2 A B c; induction c; intros E3 D z prog; simpl; auto; rewrite IHc; auto.
Qed.
Lemma zappl_toZContext:
forall {E1 E2 A B} (c: Context E1 A E2 B) (p: OProg E1 A),
zappl (toZContext c) p = appl c p.
Proof.
intros; unfold toZContext; rewrite zappl_toZContext_; simpl; reflexivity.
Qed.
Fixpoint fromZContext_ {E2 E3: list Type} {B C: Type} (z: ZContext E2 B E3 C) : forall {E1 A}, Context E1 A E2 B -> Context E1 A E3 C :=
match z in (ZContext E2 B E3 C) return (forall {E1 A}, Context E1 A E2 B -> Context E1 A E3 C) with
| ZTop => (fun _ _ c => c)
| ZOr1 z q => (fun _ _ c => fromZContext_ z (COr1 c q))
| ZOr2 z p => (fun _ _ c => fromZContext_ z (COr2 p c))
| ZPut z v => (fun _ _ c => fromZContext_ z (CPut v c))
| ZGet z t q => (fun _ _ c => fromZContext_ z (CGet t c q))
| ZDelay z t q => (fun _ _ c => fromZContext_ z (CDelay t c q))
| ZPush z x => (fun _ _ c => fromZContext_ z (CPush x c))
| ZLift z => (fun _ _ c => fromZContext_ z (CLift c))
end.
Definition fromZContext {E1 E2: list Type} {A B: Type} (z: ZContext E1 A E2 B) : Context E1 A E2 B :=
fromZContext_ z CHole.
Lemma appl_fromZContext_:
forall {B C E2 E3} (z: ZContext E2 B E3 C) {E1 A} (c: Context E1 A E2 B) (p: OProg E1 A),
appl (fromZContext_ z c) p = zappl z (appl c p).
Proof.
intros B C E2 E3 z; induction z; intros E0 A0 c0 p; auto; simpl; rewrite IHz; auto.
Qed.
Lemma appl_fromZContext:
forall {A B E1 E2} (z: ZContext E1 A E2 B) (p: OProg E1 A),
appl (fromZContext z) p = zappl z p.
Proof.
intros; unfold fromZContext; rewrite appl_fromZContext_; auto.
Qed.
Lemma toZContext_fromZContext_:
forall {E2 E3 B C} (z: ZContext E2 B E3 C) {E1 A} (c: Context E1 A E2 B),
toZContext (fromZContext_ z c) = toZContext_ c z.
Proof.
intros E2 E3 B C z; induction z; intros E0 A0 c0; simpl; auto; rewrite IHz; auto.
Qed.
Lemma toZContext_fromZContext:
forall {E1 E2 A B} (z: ZContext E1 A E2 B),
toZContext (fromZContext z) = z.
Proof.
intros; unfold fromZContext; apply toZContext_fromZContext_.
Qed.
Lemma invert_zappl_zput:
forall {E1 E2 A B} (z: ZContext E1 A E2 B) (v: Env E1 -> S) (q: OProg E1 A),
zappl z (fun env => Put (v env) (q env))
=
zappl (ZPut z v) q.
Proof.
intros; simpl; unfold cpush, clift, comap, head, tail, head_, tail_; auto.
Qed.
Lemma invert_zappl_zor1:
forall {E1 E2 A B} (z: ZContext E1 A E2 B) (p q: OProg E1 A),
zappl z (fun env => Or (p env) (q env))
=
zappl (ZOr1 z q) p.
Proof.
intros; simpl; unfold cpush, clift, comap, head, tail, head_, tail_; auto.
Qed.
Lemma invert_zappl_zor2:
forall {E1 E2 A B} (z: ZContext E1 A E2 B) (p q: OProg E1 A),
zappl z (fun env => Or (p env) (q env))
=
zappl (ZOr2 z p) q.
Proof.
intros; simpl; unfold cpush, clift, comap, head, tail, head_, tail_; auto.
Qed.
Lemma invert_zappl_zget:
forall {E1 E2 A B} (z: ZContext E1 A E2 B) (p: S -> OProg E1 A),
zappl z (fun env => Get (fun s => p s env))
=
zappl (ZGet z (fun _ => true) (fun _ _ => Fail)) (fun env' => clift (p (head env')) env').
Proof.
intros; simpl; unfold cpush, clift, comap, head, tail, head_, tail_; auto.
Qed.
Lemma invert_zappl_zget_or:
forall {E1 E2 A B} (z: ZContext E1 A E2 B) (p q: S -> OProg E1 A),
zappl z (fun env => Get (fun s => Or (p s env) (q s env)))
=
zappl (ZOr1 (ZGet z (fun _ => true) (fun _ _ => Fail)) (fun env' => clift (q (head env')) env')) (fun env' => clift (p (head env')) env').
Proof.
intros; simpl; unfold cpush, clift, comap, head, tail, head_, tail_; auto.
Qed.
Lemma invert_zappl_zdelay:
forall {E1 E2 A B} (z: ZContext E1 A E2 B) (b: Env E1 -> bool) (p q: OProg E1 A),
zappl z (fun env => if (b env) then (p env) else (q env))
=
zappl (ZDelay z b q) (fun env => p env).
Proof.
intros; simpl; unfold cpush, clift, comap, head, tail, head_, tail_; auto.
Qed.
Lemma invert_zappl_zpush:
forall {E1 E2 A B C}
(z: ZContext E1 A E2 B) (x: C) (p: OProg (C::E1) A),
zappl z (cpush x p)
=
zappl (ZPush z x) p.
Proof.
intros; simpl; unfold cpush, clift, comap, head, tail, head_, tail_; auto.
Qed.
(* The semantic function for global, i.e. non-backtracking, state *)
Fixpoint run {A} (p : Prog A) : D A :=
match p with
| Return x => retD x
| Fail => failD
| Or p q => orD (run p) (run q)
| Get p => getD (fun s => run (p s))
| Put s p => putD s (run p)
end.
Lemma run_ret: forall {A} (x: A), run (Return x) = retD x.
Proof. auto. Qed.
Lemma run_fail: forall {A}, @run A Fail = failD.
Proof. auto. Qed.
Lemma run_or : forall {A} (p q : Prog A), run (Or p q) = orD (run p) (run q).
Proof. auto. Qed.
Lemma run_get: forall {A} (p: S -> Prog A), run (Get p) = getD (fun s => run (p s)).
Proof. auto. Qed.
Lemma run_put:forall {A} (s: S) (p: Prog A), run (Put s p) = putD s (run p).
Proof. auto. Qed.
(* Derived definitions for open programs *)
Definition orun {A E} (p: OProg E A) (env: Env E) : D A :=
run (p env).
Lemma orun_fail:
forall {A E},
@orun A E (fun env => Fail) = fun _ => failD.
Proof.
intros. apply functional_extensionality. intro env.
unfold orun.
auto.
Qed.
Lemma orun_or:
forall {A E} (p q: OProg E A),
orun (fun env => Or (p env) (q env)) = fun env => orD (orun p env) (orun q env).
Proof.
intros; apply functional_extensionality; intro env; unfold orun. auto.
Qed.
Lemma orun_get:
forall {A E} (p: S -> OProg E A),
orun (fun env => Get (fun s => p s env)) = fun env => getD (fun s => orun (p s) env).
Proof.
intros; apply functional_extensionality; intro env; unfold orun; auto.
Qed.
Lemma orun_put:
forall {E A} (s: Env E -> S) (p: OProg E A),
orun (fun env => Put (s env) (p env)) = fun env => putD (s env) (orun p env).
Proof.
intros; apply functional_extensionality; intro env; unfold orun; auto.
Qed.
(* We will prove some laws for Prog equipped with run.
The first step is to prove that these laws hold at least at the top level of auto
program (ie empty context).
These proofs will be generalized further on.
*)
Lemma get_get_G':
forall {A} (k: S -> S -> Prog A),
run (Get (fun s => Get (fun s' => k s s')))
=
run (Get (fun s => k s s)).
Proof.
intros A k. simpl. apply get_get_G_D.
Qed.
Lemma get_put_G':
forall {A} (q: Prog A),
run (Get (fun s => Put s q))
=
run q.
Proof.
intros.
apply get_put_G_D.
Qed.
Lemma put_get_G':
forall {A} (s: S) (p: S -> Prog A),
run (Put s (Get p))
=
run (Put s (p s)).
Proof.
intros. simpl.
apply (put_get_G_D s (fun s => run (p s))).
Qed.
Lemma put_put_G':
forall {A} (v1 v2: S) (q: Prog A),
run (Put v1 (Put v2 q))
=
run (Put v2 q).
Proof.
intros; repeat rewrite run_put; apply put_put_G_D.
Qed.
Lemma put_or_G':
forall {A} (p q: Prog A) (s: S),
run (Or (Put s p) q)
=
run (Put s (Or p q)).
Proof.
intros; apply put_or_G_D.
Qed.
Lemma get_or_G' : forall {A : Type} (p : S -> Prog A) (q : Prog A),
run (Get (fun s => Or (p s) q))
=
run (Or (Get p) q).
Proof.
intros; apply get_or_G_D.
Qed.
Lemma or_fail':
forall {A} (q: Prog A),
run (Or Fail q)
=
run q.
Proof.
intros; apply or1_fail_G_D.
Qed.
Lemma fail_or':
forall {A} (q: Prog A),
run (Or q Fail)
=
run q.
Proof.
intros; apply or2_fail_G_D.
Qed.
Lemma or_or':
forall {A} (p q r: Prog A),
run (Or (Or p q) r)
=
run (Or p (Or q r)).
Proof.
intros; apply or_or_G_D.
Qed.
(* Lemma to help generalize proofs for empty contexts to proofs for arbitrary contexts. *)
Lemma meta_G:
forall {A B E1 E2} {X} (p q: X -> Prog A)
(meta_G': forall (x: X), run (p x) = run (q x))
(f: Env E1 -> X)
(c: Context E1 A E2 B),
orun (appl c (fun env => p (f env)))
=
orun (appl c (fun env => q (f env))).
Proof.
intros.
induction c; simpl.
- unfold orun.
apply functional_extensionality.
simpl.
intro env0.
apply meta_G'.
- repeat rewrite orun_or. rewrite (IHc p q); auto.
- repeat rewrite orun_or. rewrite (IHc p q); auto.
- repeat rewrite orun_put. rewrite (IHc p q); auto.
- repeat rewrite orun_get.
apply functional_extensionality; intro env.
f_equal. apply functional_extensionality; intro s; destruct (b s).
+ unfold cpush. unfold comap.
assert (H : forall r,
orun (fun env => appl c r (Cons s env))
=
fun env => orun (appl c r) (Cons s env)).
{ auto. }
rewrite H.
rewrite IHc with (q:=q).
auto.
apply meta_G'.
+ reflexivity.
- change (orun ?f) with (fun env => orun f env);
apply functional_extensionality; intro env0.
change (orun (fun env => if b env then ?f env else ?g env) env0) with
(orun (fun env => if b env0 then f env else g env) env0).
destruct (b env0).
+ rewrite IHc with (q:=q).
auto.
apply meta_G'.
+ reflexivity.
- unfold cpush, comap.
change (orun (fun env => appl c0 ?r (Cons c env)))
with (fun env => orun (appl c0 r) (Cons c env)).
rewrite IHc with (q:=q); auto.
- unfold clift, comap.
change (orun (fun env => appl c ?r (tail env)))
with (fun env => orun (appl c r) (@tail C _ env)).
rewrite IHc with (q:=q); auto.
Qed.
(* Now we can generalize to arbitrary (but bind-free) contexts. *)
Lemma get_get_G:
forall {A B E1 E2} (c: Context E1 A E2 B) (k: S -> S -> OProg E1 A),
orun (appl c (fun env => Get (fun s1 => Get (fun s2 => k s1 s2 env))))
=
orun (appl c (fun env => Get (fun s1 => k s1 s1 env))).
Proof.
intros A B E1 E2 c k.
apply (@meta_G A B E1 E2 (S -> S -> Prog A)
(fun k => Get (fun s1 => Get (fun s2 => k s1 s2)))
(fun k => Get (fun s1 => k s1 s1))
get_get_G'
(fun env s1 s2 => k s1 s2 env)
).
Qed.
Lemma get_put_G:
forall {E1 E2 A B} (c: Context E1 A E2 B) (k: OProg E1 A),
orun (appl c (fun env => Get (fun s => Put s (k env))))
=
orun (appl c k).
Proof.
intros.
apply (@meta_G A B E1 E2 (Prog A)
(fun q => Get (fun s => Put s q))
(fun q => q)
get_put_G'
(fun env => k env)
).
Qed.
Lemma put_get_G:
forall {A B E1 E2} (c: Context E1 A E2 B) (k: S -> OProg E1 A) (v: Env E1 -> S),
orun (appl c (fun env => Put (v env) (Get (fun s => k s env))))
=
orun (appl c (fun env => Put (v env) ( k (v env) env))).
Proof.
intros A B E1 E2 c k v;
apply (@meta_G A B E1 E2 (S * (S -> Prog A))
(fun x => Put (fst x) (Get (snd x)))
(fun x => Put (fst x) (snd x (fst x)))
(fun x => put_get_G' (fst x) (snd x))
(fun env => (v env, fun s => k s env))
).
Qed.
Lemma put_put_G:
forall {E1 E2 A B} (c: Context E1 A E2 B) (v1 v2: Env E1 -> S) (k: OProg E1 A),
orun (appl c (fun env => Put (v1 env) (Put (v2 env) (k env))))
=
orun (appl c (fun env => Put (v2 env) (k env))).
Proof.
intros.
apply (@meta_G A B E1 E2 ((S * S) * (Prog A))
(fun t => Put (fst (fst t)) (Put (snd (fst t)) (snd t)))
(fun t => Put (snd (fst t)) (snd t))
(fun t => put_put_G' (fst (fst t)) (snd (fst t)) (snd t))
(fun env => ((v1 env, v2 env), k env))
).
Qed.
Lemma put_or_G:
forall {E1 E2 A B} (c: Context E1 A E2 B) (p: OProg E1 A) (q: OProg E1 A) (s: Env E1 -> S),
orun (appl c (fun env => Or (Put (s env) (p env)) (q env)))
=
orun (appl c (fun env => Put (s env) (Or (p env) (q env)))).
Proof.
intros.
apply (@meta_G A B E1 E2 ((Prog A * Prog A) * S)
(fun t => Or (Put (snd t) (fst (fst t))) (snd (fst t)))
(fun t => Put (snd t) (Or (fst (fst t)) (snd (fst t))))
(fun t => put_or_G' (fst (fst t)) (snd (fst t)) (snd t))
(fun env => ((p env, q env), s env))
).
Qed.
Lemma get_or_G:
forall {E1 E2 A B}
(c: Context E1 A E2 B) (p: S -> OProg E1 A) (q: OProg E1 A),
orun (appl c (fun env => Get (fun s =>
Or ((fun s' => p s' env) s) (q env))))
=
orun (appl c (fun env => Or (Get (fun s =>
((fun s' => p s' env) s))) (q env))).
Proof.
intros.
apply (@meta_G A B E1 E2 ((S -> Prog A) * Prog A)
(fun t => Get (fun s => Or (fst t s) (snd t)))
(fun t => Or (Get (fun s => (fst t s))) (snd t))
(fun t => get_or_G' (fst t) (snd t))
(fun env => ((fun s => p s env), q env))).
Qed.
Lemma or_fail:
forall {E1 E2 A B} (c: Context E1 A E2 B) (q: OProg E1 A),
orun (appl c (fun env => Or Fail (q env)))
=
orun (appl c q).
Proof.
intros.
apply (@meta_G A B E1 E2 (Prog A)
(fun t => Or Fail t)
(fun t => t)
(fun t => or_fail' t)
(fun env => q env)
).
Qed.
Lemma fail_or:
forall {E1 E2 A B} (c: Context E1 A E2 B) (q: OProg E1 A),
orun (appl c (fun env => Or (q env) Fail))
=
orun (appl c q).
Proof.
intros.
apply (@meta_G A B E1 E2 (Prog A)
(fun t => Or t Fail)
(fun t => t)
(fun t => fail_or' t)
(fun env => q env)
).
Qed.
Lemma or_or:
forall {E1 E2 A B} (c: Context E1 A E2 B) (p q r: OProg E1 A),
orun (appl c (fun env => Or (Or (p env) (q env)) (r env)))
=
orun (appl c (fun env => Or (p env) (Or (q env) (r env)))).
Proof.
intros.
apply (@meta_G A B E1 E2 ((Prog A * Prog A) * Prog A)
(fun t => Or (Or (fst (fst t)) (snd (fst t))) (snd t))
(fun t => Or (fst (fst t)) (Or (snd (fst t)) (snd t)))
(fun t => or_or' (fst (fst t)) (snd (fst t)) (snd t))
(fun env => ((p env, q env), r env))
).
Qed.
Lemma put_ret_or_G:
forall {A} (v: S) (w: A) (q: Prog A),
run (Put v (Or (Return w) q))
=
run (Or (Put v (Return w)) (Put v q)).
Proof.
intros.
apply put_ret_or_G_D.
Qed.
(* The function trans takes a program p and produces a program p' such that
the result of running p under local state semantics is the same as the result
of running p' under global state semantics.
*)
Fixpoint trans {A} (p: Prog A): Prog A :=
match p with
| Return x => Return x
| Or p q => Or (trans p) (trans q)
| Fail => Fail
| Get p => Get (fun s => trans (p s))
| Put s p => Get (fun s0 => Or (Put s (trans p)) (Put s0 Fail))
end.
Definition otrans {E A} (p: OProg E A) : OProg E A :=
fun env => trans (p env).
(* Translating contexts *)
Fixpoint transC {E1 E2: list Type} {A B: Type} (c: Context E1 A E2 B) : Context E1 A E2 B :=
match c in (Context E1 A E2 B) return (Context E1 A E2 B) with
| CHole => CHole
| COr1 c q => COr1 (transC c) (otrans q)
| COr2 p c => COr2 (otrans p) (transC c)
| CPut v c => CGet (fun s => true)
(COr1
(CPut (fun env => v (tail env))
(CLift (transC c)))
(fun env => Put (head env) Fail))
(fun s env => Fail)
| CGet t c q => CGet t (transC c) (fun s => otrans (q s))
| CDelay t c q => CDelay t (transC c) (otrans q)
| CPush x c => CPush x (transC c)
| CLift c => CLift (transC c)
end.
Lemma otrans_cpush:
forall {A C E1} (p: OProg (C::E1) A) (x: C),
cpush x (otrans p) = otrans (cpush x p).
Proof.
intros A C E p x; unfold cpush, comap, otrans; auto.
Qed.
Lemma otrans_clift: