-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetaTheoryTyping.v
2766 lines (2639 loc) · 96.9 KB
/
MetaTheoryTyping.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
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 Import CpdtTactics.
From Coq Require Import Arith.PeanoNat.
From Coq Require Import Bool.Bool.
From Coq Require Import Lists.List.
Import ListNotations.
Require Import Maps.
Require Import SyntaxRuntime.
Require Import Typing.
Require Import OperationalSemantics.
Require Import MetaTheoryBase.
Local Set Warnings "-implicit-core-hint-db".
Local Set Warnings "-deprecated-focus".
Ltac inv H := inversion H; subst; clear H.
Ltac narrow_terms := try (match goal with
| [H : value _ |- _] => inv H
end);
try (match goal with
| [H : empty _ = Some _ |- _] => inv H
end);
try solve [match goal with
| [H : has_type _ _ _ _ _ |- _] => inv H
end].
Ltac ssame := subst; match goal with
| [ H : C _ _ _ _ = C _ _ _ _ |- _ ] => inversion H
end; subst.
Ltac ssame' := subst; match goal with
| [ H : C _ _ _ _ = C _ _ _ _ |- _ ] => inv H
end.
Lemma canonical_forms_fun : forall t T1 T2 E1 E2 ll,
has_type empty ll t (Arrow T1 T2 E1) E2 ->
value t ->
exists x u, t = t_abs x T1 u.
Proof using.
intros t T1 T2 E1 E2 ll HT HVal.
inversion HVal; intros; subst; try inversion HT; subst; auto; narrow_terms; eauto.
Qed.
Lemma canonical_forms_result : forall t E ll,
has_type empty ll t Result E ->
value t ->
exists r, t = t_result r.
Proof using.
destruct t; intros; try solve [inv H0; inv H].
eauto.
Qed.
Hint Resolve canonical_forms_result.
Lemma canonical_forms_label : forall t E T ll,
has_type empty ll t (Label T) E ->
value t ->
exists l, t = t_label l.
Proof using.
destruct t; intros; try solve [inv H0; inv H].
eauto.
Qed.
Hint Resolve canonical_forms_label.
Inductive dry_backend : backend -> Prop :=
| dry_backend_empty : dry_backend []
| dry_backend_cons : forall s b, dry_backend b -> value (get_payload (get_node s)) -> get_ostream s = [] -> dry_backend (s::b).
Hint Constructors dry_backend.
Inductive dry : config -> Prop :=
| dry_ : forall b rs v, dry_backend b -> value v -> dry (C b [] rs v).
Hint Constructors dry.
Lemma dry_backend_dist : forall b b1 b2,
b = b1 ++ b2 ->
dry_backend b ->
dry_backend b1 /\ dry_backend b2.
Proof using.
induction b; intros.
- assert (b1 = []). destruct b1. destruct b2. eauto. inv H. inv H.
assert (b2 = []). destruct b1. destruct b2. eauto. inv H. inv H.
crush.
- destruct b1; destruct b2; inv H.
+ split; eauto.
+ split; eauto.
constructor; inv H0; crush.
+ split.
constructor; inv H0; crush.
* apply IHb with (b3:=b1) (b4:=s0::b2) in H2; eauto; crush.
* inv H0. apply IHb with (b3:=b1) (b4:=s0::b2) in H2; eauto; crush.
Qed.
Lemma dry_no_in : forall b s n os,
dry_backend b ->
In s b ->
s = <<n; os>> ->
os = [].
Proof using.
induction b; intros.
- crush.
- crush.
+ inv H. auto.
+ inv H. eauto.
Qed.
Inductive next_reduction : term -> term -> Prop :=
| nr_app1 : forall t1 t2 t1', not (value t1) -> next_reduction t1 t1' -> next_reduction (t_app t1 t2) t1'
| nr_app2 : forall t1 t2 t2', value t1 -> not (value t2) -> next_reduction t2 t2' -> next_reduction (t_app t1 t2) t2'
| nr_app : forall t1 t2, value t1 -> value t2 -> next_reduction (t_app t1 t2) (t_app t1 t2)
| nr_var : forall x, next_reduction (t_var x) (t_var x)
| nr_ks_cons1 : forall t1 t2 t1', not (value t1) -> next_reduction t1 t1' -> next_reduction (t_ks_cons t1 t2) t1'
| nr_ks_cons2 : forall t1 t2 t2', value t1 -> not (value t2) -> next_reduction t2 t2' -> next_reduction (t_ks_cons t1 t2) t2'
| nr_downarrow : forall t t', not (value t) -> next_reduction t t' -> next_reduction (t_downarrow t) t'
| nr_downarrow_claim : forall t, value t -> next_reduction (t_downarrow t) (t_downarrow t)
| nr_emit_pfold1 : forall l t1 t2 t3 t', not (value t1) -> next_reduction t1 t' -> next_reduction (t_emit_pfold l t1 t2 t3) t'
| nr_emit_pfold2 : forall l t1 t2 t3 t', value t1 -> not (value t2) -> next_reduction t2 t' -> next_reduction (t_emit_pfold l t1 t2 t3) t'
| nr_emit_pfold3 : forall l t1 t2 t3 t', value t1 -> value t2 -> not (value t3) -> next_reduction t3 t' -> next_reduction (t_emit_pfold l t1 t2 t3) t'
| nr_emit_pfold : forall l t1 t2 t3, value t1 -> value t2 -> value t3 -> next_reduction (t_emit_pfold l t1 t2 t3) (t_emit_pfold l t1 t2 t3)
| nr_emit_pmap1 : forall l t1 t2 t', not (value t1) -> next_reduction t1 t' -> next_reduction (t_emit_pmap l t1 t2) t'
| nr_emit_pmap2 : forall l t1 t2 t', value t1 -> not (value t2) -> next_reduction t2 t' -> next_reduction (t_emit_pmap l t1 t2) t'
| nr_emit_pmap : forall l t1 t2, value t1 -> value t2 -> next_reduction (t_emit_pmap l t1 t2) (t_emit_pmap l t1 t2)
| nr_emit_add1 : forall l t1 t2 t', not (value t1) -> next_reduction t1 t' -> next_reduction (t_emit_add l t1 t2) t'
| nr_emit_add2 : forall l t1 t2 t', value t1 -> not (value t2) -> next_reduction t2 t' -> next_reduction (t_emit_add l t1 t2) t'
| nr_emit_add : forall l t1 t2, value t1 -> value t2 -> next_reduction (t_emit_add l t1 t2) (t_emit_add l t1 t2)
| nr_node1 : forall t1 t2 t3 t1', not (value t1) -> next_reduction t1 t1' -> next_reduction (t_node t1 t2 t3) t1'
| nr_node2 : forall t1 t2 t3 t2', value t1 -> not (value t2) -> next_reduction t2 t2' -> next_reduction (t_node t1 t2 t3) t2'
| nr_node3 : forall t1 t2 t3 t3', value t1 -> value t2 -> not (value t3) -> next_reduction t3 t3' -> next_reduction (t_node t1 t2 t3) t3'
| nr_na1 : forall t t', not (value t) -> next_reduction t t' -> next_reduction (t_na1 t) t'
| nr_na2 : forall t t', not (value t) -> next_reduction t t' -> next_reduction (t_na2 t) t'
| nr_na3 : forall t t', not (value t) -> next_reduction t t' -> next_reduction (t_na3 t) t'
| nr_na1_get : forall t, value t -> next_reduction (t_na1 t) (t_na1 t)
| nr_na2_get : forall t, value t -> next_reduction (t_na2 t) (t_na2 t)
| nr_na3_get : forall t, value t -> next_reduction (t_na3 t) (t_na3 t)
| nr_fix : forall t t' T, not (value t) -> next_reduction t t' -> next_reduction (t_fix T t) t'
| nr_fix_fix : forall t T, value t -> next_reduction (t_fix T t) (t_fix T t).
Hint Constructors next_reduction.
Ltac find_type := match goal with
| [H : has_type _ _ _ _ _ |- _] => inv H; eauto
| [H : config_has_type _ _ _ |- _] => inv H; find_type
end.
Ltac easy_wt := inversion WT; split; try split; crush; find_type.
Ltac can_fun :=
match goal with
| [H : has_type empty _ ?t (Arrow _ _ _) _, H' : value ?t |- _] =>
let x := fresh "x" in let u := fresh "u" in apply canonical_forms_fun in H; [|assumption]; destruct H as [x[u]]; subst t
end.
Ltac can_res :=
match goal with
| [H : has_type empty _ ?t Result _, H' : value ?t |- _] =>
let r := fresh "r" in apply canonical_forms_result in H; [|assumption]; destruct H as [r]; subst t
end.
Ltac can_lab :=
match goal with
| [H : has_type empty _ ?t (Label _) _, H' : value ?t |- _] =>
let l := fresh "l" in apply canonical_forms_label in H; [|assumption]; destruct H as [l]; subst t
end.
Lemma next_reduction_to_reduction' :
forall t rs b os t',
well_typed (C b os rs t) ->
next_reduction t t' ->
(exists t'' os', FRf t rs ==> FRt t'' os') \/ (exists l, t' = t_downarrow (t_label l) /\ not (exists v, In (l ->>> v) rs)).
Proof using.
intros t rs b os t' WT NR.
induction NR; subst.
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- inv WT. dtr. inv H3. inv H12. can_fun. left. eauto.
- inv WT; dtr. inv H1. inv H10. inv H4.
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- inv WT; dtr. inv H2. inv H11. can_lab.
destruct (result_in_dec rs l).
+ destruct H2. eauto.
+ right. eauto.
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- eauto.
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- eauto.
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- inv WT; dtr. inv H3. inv H12. can_res. can_res.
eauto.
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- inv WT; dtr. inv H2. inv H11. destruct t; narrow_terms. eauto.
- inv WT; dtr. inv H2. inv H11. destruct t; narrow_terms. eauto.
- inv WT; dtr. inv H2. inv H11. destruct t; narrow_terms; eauto.
- destruct IHNR; dtr; [try solve [easy_wt]| |]; try solve [eauto].
- eauto.
Unshelve.
auto.
auto.
Qed.
Lemma next_reduction_to_reduction :
forall t t' rs b os,
well_typed (C b os rs t) ->
next_reduction t t' ->
(exists t'' os', C b os rs t --> C b (os ++ os') rs t'') \/ (exists l, t' = t_downarrow (t_label l) /\ not (exists v, In (l ->>> v) rs)).
Proof using.
intros t t' rs0 b0 os0 WT NR.
apply next_reduction_to_reduction' with (t':=t') in WT; auto. destruct WT; dtr; eauto.
Qed.
Lemma term_to_next_reduction :
forall t b os rs, well_typed (C b os rs t) -> not (value t) -> exists t', next_reduction t t'.
Proof using.
induction t; intros b os rs WT; intros; eauto.
- destruct (value_dec t1); destruct (value_dec t2); eauto.
+ copy H1. eapply IHt2 in H1. destruct H1. eauto. easy_wt.
+ copy H0. eapply IHt1 in H0. destruct H0. eauto. easy_wt.
+ copy H0. eapply IHt1 in H0. destruct H0. eauto. easy_wt.
- crush.
- crush.
- crush.
- crush.
- destruct (value_dec t1); destruct (value_dec t2).
+ assert (value (t_ks_cons t1 t2)) by eauto; crush.
+ remember H1. clear Heqn. eapply IHt2 in H1. destruct H1. eauto. easy_wt.
+ remember H0. clear Heqn. eapply IHt1 in H0. destruct H0. eauto. easy_wt.
+ remember H0. clear Heqn. eapply IHt1 in H0. destruct H0. eauto. easy_wt.
- destruct (value_dec t).
+ assert (exists l, t = t_label l).
{
inversion WT; dtr. find_type; narrow_terms.
}
destruct H1. eauto.
+ remember H0. clear Heqn. eapply IHt in H0. destruct H0. eauto. easy_wt.
- destruct (value_dec t1).
+ destruct (value_dec t2).
* {
destruct (value_dec t3).
- eauto.
- remember H2. clear Heqn. eapply IHt3 in H2. destruct H2. eauto. easy_wt.
}
* remember H1. clear Heqn. eapply IHt2 in H1. destruct H1. eauto. easy_wt.
+ remember H0. clear Heqn. eapply IHt1 in H0. destruct H0. eauto. easy_wt.
- destruct (value_dec t1).
+ destruct (value_dec t2).
* eauto.
* remember H1. clear Heqn. eapply IHt2 in H1. destruct H1. eauto. easy_wt.
+ remember H0. clear Heqn. eapply IHt1 in H0. destruct H0. eauto. easy_wt.
- destruct (value_dec t1).
+ destruct (value_dec t2).
* eauto.
* remember H1. clear Heqn. eapply IHt2 in H1. destruct H1. eauto. easy_wt.
+ remember H0. clear Heqn. eapply IHt1 in H0. destruct H0. eauto. easy_wt.
- destruct (value_dec t1).
+ destruct (value_dec t2).
* destruct (value_dec t3); [exfalso; apply H; eauto|].
ceapply IHt3 in H2; dtr; eauto; easy_wt.
* ceapply IHt2 in H1; dtr; eauto; easy_wt.
+ ceapply IHt1 in H0; dtr; eauto; easy_wt.
- destruct (value_dec t).
+ eauto.
+ ceapply IHt in H0; dtr. eauto. easy_wt.
- destruct (value_dec t).
+ eauto.
+ ceapply IHt in H0; dtr. eauto. easy_wt.
- destruct (value_dec t).
+ eauto.
+ ceapply IHt in H0; dtr. eauto. easy_wt.
- destruct (value_dec t0).
+ eauto.
+ ceapply IHt in H0; dtr. eauto. easy_wt.
Qed.
Lemma well_typed_backend_dist' : forall b b' ll ll',
well_typed_backend ll ll' (b ++ b') ->
exists ll'', well_typed_backend ll'' ll' b /\ well_typed_backend ll ll'' b'.
Proof using.
induction b; intros.
- exists ll'; crush.
- simpl in *. inv H. apply IHb in H7; dtr. exi; eauto.
Qed.
Hint Resolve well_typed_backend_dist'.
Lemma well_typed_backend_empty : forall ll ll',
well_typed_backend ll ll' [] ->
ll = ll'.
Proof using.
intros; inv H; auto.
Qed.
Hint Resolve well_typed_backend_empty.
Lemma well_typed_ostream_empty : forall ll ll',
well_typed_ostream ll ll' [] ->
ll = ll'.
Proof using.
intros; inv H; auto.
Qed.
Hint Resolve well_typed_ostream_empty.
Lemma well_typed_backend_dist : forall b b' ll ll' ll'',
well_typed_backend ll'' ll' b ->
well_typed_backend ll ll'' b' ->
well_typed_backend ll ll' (b ++ b').
Proof using.
induction b; intros.
- replace ll' with ll'' in * by auto; eauto.
- simpl; destruct a; destruct n; inv H; econstructor; eauto.
Qed.
Hint Resolve well_typed_backend_dist.
Ltac wtbdist := match goal with
| [H : well_typed_backend _ _ (_ ++ _) |- _] => apply well_typed_backend_dist' in H; dtr
end.
Lemma graph_typing : forall b1 b2 os0 rs0 os t0 k t es,
well_typed (C (b1 ++ <<N k t es; os>> :: b2) os0 rs0 t0) ->
has_type empty (backend_types b2 (rstream_types rs0)) t Result false.
Proof using.
intros.
inv H.
dtr.
inv H.
wtbdist.
inv H2.
wtbt.
auto.
Qed.
Lemma well_typed_ostream_dist' : forall os os' ll ll',
well_typed_ostream ll ll' (os ++ os') ->
exists ll'', well_typed_ostream ll ll'' os /\ well_typed_ostream ll'' ll' os'.
Proof using.
induction os; intros.
- eauto.
- simpl in *; destruct a; inv H; apply IHos in H4; dtr; exists x; crush.
Qed.
Hint Resolve well_typed_ostream_dist'.
Lemma well_typed_ostream_dist : forall os os' ll ll' ll'',
well_typed_ostream ll ll'' os ->
well_typed_ostream ll'' ll' os' ->
well_typed_ostream ll ll' (os ++ os').
Proof using.
induction os; intros.
- inv H; subst; auto.
- simpl; destruct a; destruct o; constructor; inv H; eauto.
Qed.
Hint Resolve well_typed_ostream_dist.
Ltac wtosdist := match goal with
| [H : well_typed_ostream _ _ (_ ++ _) |- _] => apply well_typed_ostream_dist' in H; dtr
end.
Lemma graph_typing' : forall b1 n os l f v ks os' b2 os0 rs0 t0,
well_typed (C (b1 ++ <<n; os ++ l ->> pfold f v ks :: os'>> :: b2) os0 rs0 t0) ->
has_type empty (ostream_types os (backend_types b2 (rstream_types rs0))) v Result false.
Proof using.
intros.
inv H; dtr.
inv H.
wtbdist.
inv H2.
wtosdist.
inv H3.
inv H15.
wtbt.
wtost.
wtost.
auto.
Qed.
Lemma well_typed_top_ostream_dist : forall os os' ll ll' ll'',
well_typed_top_ostream ll ll'' os ->
well_typed_top_ostream ll'' ll' os' ->
well_typed_top_ostream ll ll' (os ++ os').
Proof using.
induction os; intros.
- inv H; auto.
- simpl; destruct a; destruct o; constructor; inv H; eauto.
Qed.
Hint Resolve well_typed_top_ostream_dist.
Lemma well_typed_top_ostream_dist' : forall os os' ll ll',
well_typed_top_ostream ll ll' (os ++ os') ->
exists ll'', well_typed_top_ostream ll ll'' os /\ well_typed_top_ostream ll'' ll' os'.
Proof using.
induction os; intros.
- eauto.
- simpl in *; destruct a; inv H; apply IHos in H6; dtr; exists x; crush.
Qed.
Hint Resolve well_typed_top_ostream_dist'.
Ltac wttosdist := match goal with
| [H : well_typed_top_ostream _ _ (_ ++ _) |- _] => apply well_typed_top_ostream_dist' in H; dtr
end.
Lemma graph_typing''' : forall b os l f v ks os' rs0 t0,
well_typed (C b (os ++ l ->> pfold f v ks :: os') rs0 t0) ->
has_type empty (ostream_types os (rstream_types rs0)) v Result false.
Proof using.
intros.
inv H; dtr.
inv H.
wttosdist.
inv H2; dtr; subst; auto.
Qed.
Lemma graph_typing'' : forall b1 n os l f v ks os' b2 os0 rs0 t0,
well_typed (C (b1 ++ <<n; os ++ l ->> pfold f v ks :: os'>> :: b2) os0 rs0 t0) ->
has_type empty (ostream_types os (backend_types b2 (rstream_types rs0))) f (Arrow Result (Arrow Result Result false) false) false.
Proof using.
intros.
inv H; dtr.
inv H.
wtbdist.
inv H2.
wtosdist.
inv H3.
inv H15.
wtbt.
wtost.
wtost.
auto.
Qed.
Lemma value_no_emit : forall v Gamma ll T E,
has_type Gamma ll v T E ->
value v ->
E = false.
Proof using.
induction v; intros; try solve [inv H0]; try solve [inv H; eauto]; eauto.
- inv H0. inv H. apply IHv1 in H6; auto. apply IHv2 in H9; auto. subst. auto.
- inv H0. inv H. apply IHv1 in H8; auto. apply IHv2 in H11; auto. apply IHv3 in H12; auto. subst. auto.
Qed.
Lemma emittability' : forall t T t' rs os ll,
has_type empty ll t T false ->
FRf t rs ==> FRt t' os ->
os = [].
Proof using.
induction t; intros T t' rs os ll HHT Hstep.
- inv Hstep.
- inv Hstep.
+ auto.
+ inv HHT.
eapply IHt1 in H0; eauto.
destruct E1; destruct E2; destruct E3; try solve [inv H4]; eauto.
+ inv HHT.
eapply IHt2 in H5; eauto.
destruct E1; destruct E2; destruct E3; try solve [inv H4]; eauto.
- exfalso; apply frontend_no_value in Hstep; eauto.
- exfalso; apply frontend_no_value in Hstep; eauto.
- exfalso; apply frontend_no_value in Hstep; eauto.
- exfalso; apply frontend_no_value in Hstep; eauto.
- inv Hstep.
+ inv HHT.
eapply IHt1 in H0; eauto.
destruct E1; destruct E2; try solve [inv H4]; eauto.
+ inv HHT.
eapply IHt2 in H5; eauto.
destruct E1; destruct E2; try solve [inv H4]; eauto.
- inv Hstep.
+ auto.
+ eapply IHt in H0; eauto.
inv HHT. eauto.
- inv Hstep; inv HHT.
- inv Hstep; inv HHT.
- inv Hstep; inv HHT.
- inv Hstep; inv HHT.
+ eapply IHt1 in H0; eauto.
destruct E1; destruct E2; destruct E3; try solve [inv H5]; eauto.
+ eapply IHt2 in H6; eauto.
destruct E1; destruct E2; destruct E3; try solve [inv H5]; eauto.
+ eapply IHt3 in H7; eauto.
destruct E1; destruct E2; destruct E3; try solve [inv H5]; eauto.
- inv Hstep; inv HHT; eauto.
- inv Hstep; inv HHT; eauto.
- inv Hstep; inv HHT; eauto.
- inv Hstep; inv HHT; eauto.
Qed.
Lemma list_not_cons_self : forall {A : Type} (x : A) xs,
x :: xs = xs -> False.
Proof using.
induction xs; crush.
Qed.
Hint Resolve list_not_cons_self.
Lemma list_not_cons_self' : forall {A : Type} (x : A) xs,
xs ++ [x] = xs -> False.
Proof using.
induction xs; crush.
Qed.
Hint Resolve list_not_cons_self'.
Lemma list_app_self_nil : forall {A : Type} (xs : list A) ys,
xs = xs ++ ys -> ys = [].
Proof using.
induction xs; crush.
Qed.
Hint Resolve list_app_self_nil.
Lemma emittability : forall t T b os rs t' os' ll,
has_type empty ll t T false ->
C b os rs t --> C b (os ++ os') rs t' ->
os' = [].
Proof using.
intros t T b os rs t' os' ll HHT Hstep.
inv Hstep; ssame'; try solve [exfalso; eauto]; try solve [eauto].
- eapply emittability' in H5; eauto.
apply List.app_inv_head in H0; subst; auto.
Qed.
Lemma nr_to_lafi : forall t l,
next_reduction t (t_downarrow (t_label l)) ->
lappears_free_in l t.
Proof using.
induction t; intros; inv H; try solve [apply IHt1 in H4; auto]; try solve [apply IHt2 in H5; auto]; try solve [apply IHt3 in H6; auto]; try solve [apply IHt in H2; auto].
- auto.
- apply IHt1 in H6; auto.
- apply IHt2 in H7; auto.
- apply IHt3 in H8; auto.
- apply IHt1 in H5; auto.
- apply IHt2 in H6; auto.
- apply IHt1 in H5; auto.
- apply IHt2 in H6; auto.
- apply IHt1 in H5; auto.
- apply IHt2 in H6; auto.
- apply IHt3 in H7; auto.
- apply IHt in H4; auto.
Qed.
Lemma dependent_load_after : forall k t es l c b1 b2 os os0 rs0 term0,
c = C (b1 ++ <<N k t es; os>> :: b2) os0 rs0 term0 ->
next_reduction t (t_downarrow (t_label l)) ->
well_typed c ->
(exists v, In (l ->>> v) rs0) \/ (exists n' op b2' b2'' os'', b2 = b2' ++ <<n'; os''>> :: b2'' /\ In (l ->> op) os'').
Proof using.
intros; subst.
inv H1; dtr.
inv H1.
wtbdist.
inv H3.
apply nr_to_lafi in H0.
eapply free_in_lcontext in H12; eauto.
dtr.
wtbt.
apply lcontext_in_or_b in H3; destruct H3; dtr.
- right. destruct x4. exists n, x6, x3, x5, l0. eauto.
- left. apply type_in_rstream in H3; auto.
Qed.
Lemma dependent_loadpfold_after : forall l' l c b1 b2 t1 t2 t3 n os os' os0 rs0 term0,
c = C (b1 ++ <<n; os ++ l' ->> pfold t1 t2 t3 :: os'>> :: b2) os0 rs0 term0 ->
next_reduction t2 (t_downarrow (t_label l)) ->
well_typed c ->
(exists v, In (l ->>> v) rs0) \/ (exists op, In (l ->> op) os) \/ (exists n' op b2' b2'' os'', b2 = b2' ++ <<n'; os''>> :: b2'' /\ In (l ->> op) os'').
Proof using.
intros; subst.
inv H1; dtr.
inv H1.
wtbdist.
inv H3.
wtosdist.
inv H4.
inv H16.
apply nr_to_lafi in H0.
eapply free_in_lcontext with (l:=l) in H17; eauto; dtr.
wtbt.
wtost.
wtost.
apply lcontext_in_or_os in H4; destruct H4; dtr; eauto.
- apply lcontext_in_or_b in H3; destruct H3; dtr; eauto.
+ right. right. destruct x2. exists n, x5, x1, x4, l0. eauto.
+ left. apply type_in_rstream in H3; auto.
Qed.
Lemma distinct_deduce1 : forall b l os os' rs,
distinct (backend_labels b ++ l :: ostream_labels os ++ ostream_labels os' ++ rstream_labels rs) ->
distinct (rstream_labels rs).
Proof using.
intros.
apply distinct_concat in H; dtr.
apply distinct_remove in H0; dtr.
apply distinct_concat in H0; dtr.
apply distinct_concat in H2; dtr.
auto.
Qed.
Hint Resolve distinct_deduce1.
Lemma distinct_deduce2 : forall l os os' rs,
distinct (l :: ostream_labels os ++ ostream_labels os' ++ rstream_labels rs) ->
distinct (rstream_labels rs).
Proof using.
intros.
apply distinct_remove in H; dtr.
apply distinct_concat in H; dtr.
apply distinct_concat in H1; dtr.
auto.
Qed.
Hint Resolve distinct_deduce2.
Lemma op_reduction_exists : forall c b b1 b2 rs0 os0 term0 k t es os l op,
well_typed c ->
c = C b os0 rs0 term0 ->
b = b1 ++ <<N k t es; l ->> op :: os>> :: b2 ->
exists c', C b os0 rs0 term0 --> c'.
Proof using.
intros c b b1 b2 rs0 os0 term0 k t es os l op WT Hceq Hbeq.
destruct op; subst. rename k0 into l0.
- destruct (List.in_dec Nat.eq_dec k l0).
+ eapply ex_intro; eapply S_PMap; eauto.
+ destruct b2.
* eapply ex_intro; eapply S_Last; eauto.
* destruct s. eapply ex_intro; eapply S_Prop; eauto; crush.
- destruct b2.
+ eapply ex_intro; eapply S_Last; eauto.
+ destruct s. eapply ex_intro; eapply S_Prop; eauto; crush.
- rename k0 into l0.
destruct (List.in_dec Nat.eq_dec k l0).
+ eapply ex_intro; eapply S_PFold; eauto.
Unshelve.
auto.
auto.
+ destruct b2.
* {
destruct (value_dec t1).
- assert (has_type empty (rstream_types rs0) t1 Result false) by (eapply graph_typing' with (os:=[]) (b2:=[]); eauto).
destruct t1; try solve [inv H]; try solve [inv H0].
eapply ex_intro; eapply S_Last; eauto.
Unshelve.
right; eauto.
- rename H into HNV. eapply term_to_next_reduction with (b:=[]) (os:=[]) (rs:=rs0) in HNV.
+ dtr. copy H. rename H0 into HNR. apply next_reduction_to_reduction' with (b:=[]) (os:=[]) (rs:=rs0) (t:=t1) in H.
* {
destruct H; dtr.
- exi. eapply S_LoadPFold with (b1:=b1) (b2:=[]) (os:=[]) (t1:=t1) (t1':=x0); eauto. crush.
assert (x1 = []).
{
eapply emittability' in H; eauto.
instantiate (1:=Result).
instantiate (1:=rstream_types rs0).
inv WT; dtr. inv H2. apply well_typed_backend_dist' in H9; dtr.
inv H3. inv H15. inv H9. wtbt. auto.
}
subst.
auto.
- subst. eapply dependent_loadpfold_after with (os:=[]) (b2:=[]) in HNR.
+ destruct HNR.
* exfalso; eauto.
* {
destruct H.
- dtr. exfalso. auto.
- dtr. exfalso. destruct x2; inv H.
}
+ instantiate (1:=term0).
instantiate (1:=os0).
instantiate (1:=os).
instantiate (1:=l0).
instantiate (1:=t0).
instantiate (1:=l).
instantiate (1:=N k t es).
instantiate (1:=b1).
auto.
+ auto.
}
* inv WT; dtr. inv H2. wtbdist. inv H3. inv H15. inv H9. split; try split; eauto.
crush. crush. eauto.
+ inv WT; dtr. split; try split; eauto. crush. crush. apply distinct_concat in H0; dtr.
eauto.
inv H1. wtbdist. inv H2. inv H14. inv H8. eauto.
}
* destruct s. eapply ex_intro; eapply S_Prop; eauto; crush.
Qed.
Lemma ll_update : forall t Gamma l T' ll T E,
has_type Gamma ll t T E ->
has_type Gamma (l #-> T'; ll) t T E.
Proof using.
induction t; intros; try solve [inv H; eauto].
- inv H. assert (l0 <> l) by (apply fresh_labels). constructor.
replace ((l0#->T';ll) l) with (ll l); auto.
rewrite NMaps.update_neq; auto.
Qed.
Hint Resolve ll_update.
Lemma ht_ostream_extension : forall os ll t T E,
has_type empty ll t T E ->
has_type empty (ostream_types os ll) t T E.
Proof using.
induction os; intros; auto.
destruct a; simpl.
apply IHos in H; eauto.
Qed.
Hint Resolve ht_ostream_extension.
Lemma ht_backend_extension : forall b ll t T E,
has_type empty ll t T E ->
has_type empty (backend_types b ll) t T E.
Proof using.
induction b; intros; auto.
destruct a; destruct n; simpl.
apply IHb in H; eauto.
Qed.
Hint Resolve ht_backend_extension.
Lemma ll_extract_ostream : forall os l T ll,
ostream_types os (l#->T;ll) = (l#->T;ostream_types os ll).
Proof using.
induction os; intros; auto.
Qed.
Lemma ll_extract_backend : forall b l T ll,
backend_types b (l#->T;ll) = (l#->T;backend_types b ll).
Proof using.
induction b; intros; auto.
destruct a; destruct n; simpl.
rewrite <- ll_extract_ostream. rewrite IHb. auto.
Qed.
Lemma ll_swap_ostream_backend : forall os b ll,
ostream_types os (backend_types b ll) = backend_types b (ostream_types os ll).
Proof using.
induction os; intros; auto.
destruct a; simpl. rewrite ll_extract_backend. crush.
Qed.
Lemma ll_swap_backend : forall b b' ll,
backend_types b (backend_types b' ll) = backend_types b' (backend_types b ll).
Proof using.
induction b; intros; auto.
destruct a; destruct n; simpl. rewrite IHb. rewrite ll_swap_ostream_backend. auto.
Qed.
Lemma ht_ostream_extract : forall os ll l T' t T E,
has_type empty (ostream_types os (l#->T';ll)) t T E ->
has_type empty (l#->T';ostream_types os ll) t T E.
Proof using.
induction os; intros; auto.
destruct a; simpl in *.
repeat (rewrite ll_extract_ostream in H). rename l0 into n.
replace (n #-> op_type o; l #-> T'; ostream_types os ll)
with (l#->T';n #-> op_type o; ostream_types os ll) in H.
auto.
apply NMaps.update_permute; apply fresh_labels.
Qed.
Lemma ht_ostream_extract' : forall os ll l T' t T E,
has_type empty (l#->T';ostream_types os ll) t T E ->
has_type empty (ostream_types os (l#->T';ll)) t T E.
Proof using.
induction os; intros; auto.
destruct a; simpl in *.
repeat (rewrite ll_extract_ostream). rename l0 into n.
replace (n #-> op_type o; l #-> T'; ostream_types os ll)
with (l#->T';n #-> op_type o; ostream_types os ll).
auto.
apply NMaps.update_permute; apply fresh_labels.
Qed.
Lemma ht_ostream_extension' : forall os l T' ll t T E,
has_type empty (ostream_types os ll) t T E ->
has_type empty (ostream_types os (l#->T';ll)) t T E.
Proof using.
intros.
apply ht_ostream_extract'. auto.
Qed.
Hint Resolve ht_ostream_extension'.
Lemma ht_backend_extract' : forall b ll l T' t T E,
has_type empty (l#->T';backend_types b ll) t T E ->
has_type empty (backend_types b (l#->T';ll)) t T E.
Proof using.
induction b; intros; auto.
destruct a; destruct n; simpl in *.
rewrite ll_extract_backend.
rewrite ll_extract_ostream.
auto.
Qed.
Lemma ht_backend_extension' : forall b l T' ll t T E,
has_type empty (backend_types b ll) t T E ->
has_type empty (backend_types b (l#->T';ll)) t T E.
Proof using.
intros.
apply ht_backend_extract'. auto.
Qed.
Hint Resolve ht_backend_extension'.
Lemma wt_operation_extension : forall ll l T op,
well_typed_operation ll op ->
well_typed_operation (l#->T;ll) op.
Proof using.
intros.
inv H; econstructor; eauto.
Qed.
Hint Resolve wt_operation_extension.
Lemma wt_ostream_build : forall os ll ll',
well_typed_ostream ll ll' os ->
well_typed_ostream ll (ostream_types os ll) os.
Proof using.
induction os; intros; auto.
destruct a; simpl in *.
wtost'. simpl in *. auto.
Qed.
Lemma wt_ostream_extension : forall os ll ll' l T,
well_typed_ostream ll ll' os ->
exists ll'', well_typed_ostream (l#->T;ll) ll'' os.
Proof using.
induction os; intros; eauto.
destruct a; simpl in *. inv H. eapply IHos with (l:=l) (T:=T) in H4; dtr.
wtost'. exi. econstructor; eauto.
eapply wt_ostream_build.
destruct o; simpl in *;
replace (l0 #-> Result; l #-> T; ll) with (l#->T;l0 #-> Result;ll); eauto; apply NMaps.update_permute; apply fresh_labels.
Qed.
Hint Resolve wt_ostream_extension.
Lemma wt_ostream_backend_extract' : forall b os ll ll' l T,
well_typed_ostream (l#->T;backend_types b ll) ll' os ->
exists ll'', well_typed_ostream (backend_types b (l#->T;ll)) ll'' os.
Proof using.
induction b; intros; eauto.
destruct a; destruct n; simpl in *.
wtost'.
rewrite ll_extract_backend.
rewrite ll_extract_ostream.
eauto.
Qed.
Lemma wt_ostream_backend_extension' : forall b os l T ll ll',
well_typed_ostream (backend_types b ll) ll' os ->
exists ll'', well_typed_ostream (backend_types b (l#->T;ll)) ll'' os.
Proof using.
intros.
eapply wt_ostream_extension with (l:=l) (T:=T) in H; dtr.
eapply wt_ostream_backend_extract'. eauto.
Qed.
Hint Resolve wt_ostream_backend_extension'.
Lemma wt_backend_build : forall b ll ll',
well_typed_backend ll ll' b ->
well_typed_backend ll (backend_types b ll) b.
Proof using.
induction b; intros; auto.
destruct a; destruct n; simpl in *.
wtbt'. simpl in *. auto.
Qed.
Lemma wt_backend_extension : forall b ll ll' l T,
well_typed_backend ll ll' b ->
exists ll'', well_typed_backend (l#->T;ll) ll'' b.
Proof using.
induction b; intros; eauto.
destruct a; destruct n; simpl in *.
inv H.
wtbt'. wtost'.
eapply IHb with (l:=l) (T:=T) in H; dtr.
exists (ostream_types l0 (backend_types b (l#->T;ll))).
econstructor. Focus 4.
- eauto.
- wtbt. wtost. eauto.
- wtbt. wtost. eauto.
- wtbt'. wtost'. apply wt_ostream_extension with (l:=l) (T:=T) in H; dtr.
eapply wt_ostream_build.
rewrite ll_extract_backend. eauto.
Qed.
Hint Resolve wt_backend_extension.
Lemma load_exists : forall c b b1 b2 rs0 os0 term0 k t es os,
well_typed c ->
c = C b os0 rs0 term0 ->
b = b1 ++ <<N k t es; os>> :: b2 ->
not (value t) ->
exists c', C b os0 rs0 term0 --> c'.
Proof using.
intros c b b1 b2 rs0 os0 term0 k t es os WT Hceq Hbeq HNV.
assert (has_type empty (backend_types b2 (rstream_types rs0)) t Result false) by (subst; eapply graph_typing; eauto).
destruct t; try solve [inv H; inv H3].
- eapply term_to_next_reduction in HNV; subst; dtr.
+ copy H0. rename H1 into HNR. eapply next_reduction_to_reduction' with (rs:=rs0) in H0.
* {
destruct H0; dtr.
- assert (x1 = []) by (eapply emittability'; eauto); subst.
exi. eapply S_Load; eauto.
- subst.
edestruct dependent_load_after with (c:=C (b1 ++ << N k (t_app t1 t2) es; os >> :: b2) os0 rs0 term0) (l:=x0); eauto; dtr.
+ exfalso; eauto.
+ apply List.in_split in H2; dtr.
subst.
replace ((b1 ++ << N k (t_app t1 t2) es; os >> :: x2 ++ << x; x5 ++ x0 ->> x1 :: x6 >> :: x3))
with (((b1 ++ << N k (t_app t1 t2) es; os >> :: x2) ++ << x; x5 ++ x0 ->> x1 :: x6 >> :: x3)) by crush.
destruct x. destruct x5; simpl.
* eapply op_reduction_exists; eauto. crush.
* destruct l. eapply op_reduction_exists; eauto. crush.
}
* instantiate (1:=os0).
instantiate (1:=b1 ++ << N k (t_app t1 t2) es; os >> :: b2).
inv WT; dtr.
split; try split; eauto.
inv H3.
exists Result, false.
econstructor.
eauto.
eauto.
wtbdist.
inv H4.
wtbt.
wtbt.
wtost.
wttost.
auto.
+ instantiate (1:=rs0).
instantiate (1:=os0).
instantiate (1:=b1 ++ << N k (t_app t1 t2) es; os >> :: b2).
inv WT; dtr.
split; try split; eauto.
inv H2.
exists Result, false.
econstructor.
eauto.
eauto.
wtbdist.
inv H3.
wtbt.
wtbt.
wtost.
wttost.
auto.
- exfalso. auto.
- eapply term_to_next_reduction in HNV; subst; dtr.
+ copy H0. rename H1 into HNR. eapply next_reduction_to_reduction' with (rs:=rs0) in H0.
* {
destruct H0; dtr.
- assert (x1 = []) by (eapply emittability'; eauto); subst.
exi. eapply S_Load; eauto.
- subst.
edestruct dependent_load_after with (c:=C (b1 ++ << N k (t_downarrow t) es; os >> :: b2) os0 rs0 term0) (l:=x0); eauto; dtr.
+ exfalso; eauto.
+ apply List.in_split in H2; dtr.
subst.
replace ((b1 ++ << N k (t_downarrow t) es; os >> :: x2 ++ << x; x5 ++ x0 ->> x1 :: x6 >> :: x3))
with (((b1 ++ << N k (t_downarrow t) es; os >> :: x2) ++ << x; x5 ++ x0 ->> x1 :: x6 >> :: x3)) by crush.
destruct x. destruct x5; simpl.
* eapply op_reduction_exists; eauto. crush.
* destruct l. eapply op_reduction_exists; eauto. crush.
}
* instantiate (1:=os0).
instantiate (1:=(b1 ++ << N k (t_downarrow t) es; os >> :: b2)).
inv WT; dtr.
split; try split; eauto.
inv H3.
exists Result, false.
econstructor.
eauto.
eauto.
wtbdist.
inv H4.
wtbt.
wtbt.
wtost.
wttost.
auto.
+ instantiate (1:=rs0).
instantiate (1:=os0).
instantiate (1:=(b1 ++ << N k (t_downarrow t) es; os >> :: b2)).
inv WT; dtr.
split; try split; eauto.
inv H2.
exists Result, false.
econstructor.
eauto.
eauto.
wtbdist.
inv H3.
wtbt.
wtbt.
wtost.
wttost.
auto.
- eapply term_to_next_reduction in HNV; subst; dtr.
+ copy H0. rename H1 into HNR. eapply next_reduction_to_reduction' with (rs:=rs0) in H0.
* {
destruct H0; dtr.
- assert (x1 = []) by (eapply emittability'; eauto); subst.
exi. eapply S_Load; eauto.
- subst.
edestruct dependent_load_after with (c:=C (b1 ++ << N k (t_na1 t) es; os >> :: b2) os0 rs0 term0) (l:=x0); eauto; dtr.
+ exfalso; eauto.
+ apply List.in_split in H2; dtr.
subst.
replace ((b1 ++ << N k (t_na1 t) es; os >> :: x2 ++ << x; x5 ++ x0 ->> x1 :: x6 >> :: x3))
with (((b1 ++ << N k (t_na1 t) es; os >> :: x2) ++ << x; x5 ++ x0 ->> x1 :: x6 >> :: x3)) by crush.