/
rules_unfold.v
1766 lines (1470 loc) · 57.1 KB
/
rules_unfold.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
(*
Copyright 2014 Cornell University
Copyright 2015 Cornell University
Copyright 2016 Cornell University
Copyright 2017 Cornell University
This file is part of VPrl (the Verified Nuprl project).
VPrl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
VPrl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with VPrl. If not, see <http://www.gnu.org/licenses/>.
Websites: http://nuprl.org/html/verification/
http://nuprl.org/html/Nuprl2Coq
https://github.com/vrahli/NuprlInCoq
Authors: Vincent Rahli
*)
Require Export sequents2.
Require Export sequents_lib.
Require Export sequents_tacs.
Require Export per_props_equality.
Require Export sequents_equality.
Require Export lsubst_hyps.
Require Export per_can.
Definition get_abstraction_name_op {o} (op : @Opid o) : option opname :=
match op with
| Can _ => None
| NCan _ => None
| Exc => None
| Abs abs => Some (opabs_name abs)
end.
Definition get_abstraction_name {o} (t : @NTerm o) : option opname :=
match t with
| vterm _ => None
| sterm _ => None
| oterm op _ => get_abstraction_name_op op
end.
Definition maybe_unfold {o} lib abstractions (t : @NTerm o) :=
match get_abstraction_name t with
| Some name =>
if in_deq _ String.string_dec name abstractions then
match unfold lib t with
| Some u => u
| None => t
end
else t
| None => t
end.
Fixpoint unfold_abstractions {o} lib abstractions (t : @NTerm o) :=
match t with
| vterm v => vterm v
| sterm f => sterm f
| oterm op bs =>
maybe_unfold lib abstractions (oterm op (map (unfold_abstractions_b lib abstractions) bs))
end
with unfold_abstractions_b {o} lib abstractions (b : @BTerm o) :=
match b with
| bterm vs t => bterm vs (unfold_abstractions lib abstractions t)
end.
Hint Resolve matching_entry_implies_matching_bterms : slow.
Lemma unfold_abs_some_implies_unfold_abs_entry_some {o} :
forall lib abs bs (t : @NTerm o),
unfold_abs lib abs bs = Some t
-> {entry : library_entry & unfold_abs_entry entry abs bs = Some t}.
Proof.
induction lib; introv h; simpl in *; tcsp.
remember (unfold_abs_entry a abs bs) as uae; symmetry in Hequae.
destruct uae; ginv.
exists a; auto.
Qed.
Lemma free_vars_unfold_abs_entry {o} :
forall entry abs (bs : list (@BTerm o)) t,
unfold_abs_entry entry abs bs = Some t
-> subset (free_vars t) (free_vars_bterms bs).
Proof.
introv h; destruct entry; simpl in *; boolvar; ginv.
pose proof (subvars_free_vars_mk_instance vars bs rhs) as q.
unfold free_vars_bterms.
rw subvars_eq in q.
apply q; eauto 2 with slow.
Qed.
Lemma free_vars_unfold_abs {o} :
forall lib abs (bs : list (@BTerm o)) t,
unfold_abs lib abs bs = Some t
-> subset (free_vars t) (free_vars_bterms bs).
Proof.
introv h; apply unfold_abs_some_implies_unfold_abs_entry_some in h; exrepnd.
eapply free_vars_unfold_abs_entry; eauto.
Qed.
Lemma unfold_abstractions_preserves_free_vars {o} :
forall lib abs (t : @NTerm o),
subset (free_vars (unfold_abstractions lib abs t)) (free_vars t).
Proof.
nterm_ind t as [v|f|op bs ind] Case; introv i; simpl in *; auto.
Case "oterm".
unfold maybe_unfold in i; simpl in *.
destruct op; simpl in *; auto; allrw lin_flat_map; exrepnd;
allrw in_map_iff; exrepnd; subst; simpl in *.
- destruct a as [l t]; simpl in *.
apply in_remove_nvars in i0; repnd.
applydup ind in i1.
applydup i3 in i2.
eexists; dands; eauto.
simpl.
apply in_remove_nvars; dands; auto.
- destruct a as [l t]; simpl in *.
apply in_remove_nvars in i0; repnd.
applydup ind in i1.
applydup i3 in i2.
eexists; dands; eauto.
simpl.
apply in_remove_nvars; dands; auto.
- destruct a as [l t]; simpl in *.
apply in_remove_nvars in i0; repnd.
applydup ind in i1.
applydup i3 in i2.
eexists; dands; eauto.
simpl.
apply in_remove_nvars; dands; auto.
- boolvar.
+ rename o0 into a.
remember (unfold_abs lib a (map (unfold_abstractions_b lib abs) bs)) as uop.
symmetry in Hequop; destruct uop.
* apply free_vars_unfold_abs in Hequop.
apply Hequop in i.
unfold free_vars_bterms in i.
apply lin_flat_map in i; exrepnd.
apply in_map_iff in i1; exrepnd; subst.
destruct a0 as [vs t]; simpl in *.
apply in_remove_nvars in i0; repnd.
applydup ind in i1.
applydup i3 in i2.
eexists; dands; eauto.
simpl.
apply in_remove_nvars; dands; auto.
* simpl in *.
apply lin_flat_map in i; exrepnd.
apply in_map_iff in i1; exrepnd; subst; simpl in *.
destruct a0 as [vs t]; simpl in *.
apply in_remove_nvars in i0; repnd.
applydup ind in i1.
applydup i3 in i2.
eexists; dands; eauto.
simpl.
apply in_remove_nvars; dands; auto.
+ simpl in *.
apply lin_flat_map in i; exrepnd.
apply in_map_iff in i1; exrepnd; subst; simpl in *.
destruct a as [vs t]; simpl in *.
apply in_remove_nvars in i0; repnd.
applydup ind in i1.
applydup i3 in i2.
eexists; dands; eauto.
simpl.
apply in_remove_nvars; dands; auto.
Qed.
Lemma unfold_abs_implies_find_entry {o} :
forall lib abs bs (t : @NTerm o),
unfold_abs lib abs bs = Some t
->
{abs' : opabs
& {vars : list sovar_sig
& {rhs : SOTerm
& {correct : correct_abs abs' vars rhs
& matching_entry abs abs' vars bs
# t = mk_instance vars bs rhs
# find_entry lib abs bs = Some (lib_abs abs' vars rhs correct)}}}}.
Proof.
induction lib; introv h; simpl in *; ginv.
destruct a; simpl in *; boolvar; ginv; tcsp.
eexists; eexists; eexists; eexists; dands; eauto.
Qed.
Lemma unfold_abstractions_preserves_nt_wf {o} :
forall lib abs (t : @NTerm o),
nt_wf t
-> nt_wf (unfold_abstractions lib abs t).
Proof.
nterm_ind t as [v|f|op bs ind] Case; introv wf; simpl in *; auto.
Case "oterm".
unfold maybe_unfold; simpl in *.
destruct op; simpl in *; auto; allrw @nt_wf_oterm_iff; repnd; dands; auto;
allrw map_map; unfold compose in *; simpl in *; tcsp.
- rewrite <- wf0.
apply eq_maps; introv i.
destruct x; unfold num_bvars; simpl; auto.
- introv i.
apply in_map_iff in i; exrepnd; subst.
destruct a as [l t]; simpl in *.
applydup wf in i1.
allrw @bt_wf_iff.
eapply ind; eauto.
- rewrite <- wf0.
apply eq_maps; introv i.
destruct x; unfold num_bvars; simpl; auto.
- introv i.
apply in_map_iff in i; exrepnd; subst.
destruct a as [l t]; simpl in *.
applydup wf in i1.
allrw @bt_wf_iff.
eapply ind; eauto.
- rewrite <- wf0.
apply eq_maps; introv i.
destruct x; unfold num_bvars; simpl; auto.
- introv i.
apply in_map_iff in i; exrepnd; subst.
destruct a as [l t]; simpl in *.
applydup wf in i1.
allrw @bt_wf_iff.
eapply ind; eauto.
- boolvar.
+ rename o0 into a.
remember (unfold_abs lib a (map (unfold_abstractions_b lib abs) bs)) as uop.
symmetry in Hequop; destruct uop.
* apply unfold_abs_implies_find_entry in Hequop; exrepnd; subst.
apply nt_wf_eq.
eapply wf_mk_instance; eauto.
introv i.
apply in_map_iff in i; exrepnd; subst.
destruct a0 as [vs t]; simpl in *.
applydup wf in i1.
apply bt_wf_eq.
allrw @bt_wf_iff.
eapply ind; eauto.
* allrw @nt_wf_oterm_iff.
allrw map_map; unfold compose; simpl.
allrw <- .
dands.
{ apply eq_maps; introv i; destruct x; unfold num_bvars; simpl; auto. }
introv i; allrw in_map_iff; exrepnd; subst.
applydup wf in i1.
destruct a0 as [vs t].
allrw @bt_wf_iff.
eapply ind; eauto.
+ allrw @nt_wf_oterm_iff.
allrw map_map; unfold compose; simpl.
allrw <- .
dands.
{ apply eq_maps; introv i; destruct x; unfold num_bvars; simpl; auto. }
introv i; allrw in_map_iff; exrepnd; subst.
applydup wf in i1.
destruct a as [vs t].
allrw @bt_wf_iff.
eapply ind; eauto.
Qed.
Hint Resolve unfold_abstractions_preserves_nt_wf : slow.
Lemma isprog_unfold_abstractions {o} :
forall lib abs (t : @NTerm o),
isprog t
-> isprog (unfold_abstractions lib abs t).
Proof.
introv isp.
allrw @isprog_eq.
destruct isp as [cl wf].
split; eauto 2 with slow;[].
pose proof (unfold_abstractions_preserves_free_vars lib abs t) as h.
rewrite cl in h.
apply closed_iff.
introv i; apply h in i; simpl in i; tcsp.
Qed.
Definition unfold_abstractions_c {o} lib abs (t : @CTerm o) : CTerm :=
let (a,p) := t in
exist isprog (unfold_abstractions lib abs a) (isprog_unfold_abstractions lib abs a p).
(*Lemma lsubst_aux_unfold_abstractions {o} :
forall lib abs (t : @NTerm o) s,
cl_sub s
-> lsubst_aux (unfold_abstractions lib abs t) s
= unfold_abstractions lib abs (lsubst_aux t s).
Proof.
nterm_ind t as [v|f|op bs ind] Case; introv cls; simpl in *; tcsp.
-
Qed.*)
(*Lemma lsubstc_unfold_abstractions {o} :
forall lib abs (t : @NTerm o) w s c (w' : wf_term t) (c' : cover_vars t s),
lsubstc (unfold_abstractions lib abs t) w s c
= unfold_abstractions_c lib abs (lsubstc t w' s c').
Proof.
introv.
apply cterm_eq; simpl.
Qed.*)
Lemma isprogram_lsubst_aux_implies_subvars_free_vars_dom_sub {o} :
forall (t : @NTerm o) sub,
cl_sub sub
-> isprogram (lsubst_aux t sub)
-> subvars (free_vars t) (dom_sub sub).
Proof.
introv cls isp; apply subvars_eq.
destruct isp as [cl wf].
introv i.
pose proof (cl_lsubst_aux_trivial2 t sub) as q; autodimp q hyp.
rw cl in q.
symmetry in q.
rw @nil_remove_nvars_iff in q.
applydup q in i; auto.
Qed.
Hint Resolve isprogram_lsubst_aux_implies_subvars_free_vars_dom_sub : slow.
(* This improves [reduces_to_implies_approx_open] by only requiring
the term to be well-formed and not necessarily closed *)
Lemma reduces_to_implies_approx_open2 {o} :
forall lib (t x : @NTerm o),
wf_term t
-> reduces_to lib t x
-> approx_open lib x t # approx_open lib t x.
Proof.
introv wf r.
applydup @reduces_to_preserves_wf in r; auto.
dands; apply approx_open_simpler_equiv; unfold simpl_olift;
dands; eauto 2 with slow; introv psub isp1 isp2.
- repeat (rewrite cl_lsubst_lsubst_aux; eauto 2 with slow;[]).
rewrite cl_lsubst_lsubst_aux in isp1; eauto 2 with slow.
rewrite cl_lsubst_lsubst_aux in isp2; eauto 2 with slow.
pose proof (reduces_to_lsubst_aux lib t x sub) as q.
repeat (autodimp q hyp); eauto 3 with slow.
exrepnd.
apply reduces_to_implies_approx in q1; auto; repnd.
applydup @approx_relates_only_progs in q2; repnd.
eapply approx_trans;[|eauto].
eapply approx_alpha_rw_l_aux;[eauto|].
apply approx_refl; auto.
- repeat (rewrite cl_lsubst_lsubst_aux; eauto 2 with slow;[]).
rewrite cl_lsubst_lsubst_aux in isp1; eauto 2 with slow.
rewrite cl_lsubst_lsubst_aux in isp2; eauto 2 with slow.
pose proof (reduces_to_lsubst_aux lib t x sub) as q.
repeat (autodimp q hyp); eauto 3 with slow.
exrepnd.
apply reduces_to_implies_approx in q1; auto; repnd.
applydup @approx_relates_only_progs in q2; repnd.
eapply approx_trans;[eauto|].
eapply approx_alpha_rw_l_aux;[apply alpha_eq_sym;eauto|].
apply approx_refl; auto.
Qed.
Lemma approx_star_preserves_reduces_to_left {o} :
forall lib (a b c : @NTerm o),
wf_term a
-> reduces_to lib a b
-> approx_star lib a c
-> approx_star lib b c.
Proof.
introv ispa r apr.
allrw @approx_star_iff_approx_open.
eapply approx_open_trans;[|eauto].
apply reduces_to_implies_approx_open2 in r; tcsp.
Qed.
Lemma approx_star_preserves_reduces_to_right {o} :
forall lib (a b c : @NTerm o),
wf_term a
-> reduces_to lib a b
-> approx_star lib c a
-> approx_star lib c b.
Proof.
introv ispa r apr.
allrw @approx_star_iff_approx_open.
eapply approx_open_trans;[eauto|].
apply reduces_to_implies_approx_open2 in r; tcsp.
Qed.
Lemma sub_find_nrut_sub_some_implies {o} :
forall (sub : @Sub o) l t v,
nrut_sub l sub
-> sub_find sub v = Some t
-> {a : get_patom_set o & t = mk_utoken a}.
Proof.
induction sub; introv nrut sf; simpl in *; ginv.
destruct a; simpl in *; boolvar; ginv; tcsp.
- apply nrut_sub_cons in nrut; exrepnd; subst.
eexists; eauto.
- apply nrut_sub_cons in nrut; exrepnd; subst.
eapply IHsub; eauto.
Qed.
Lemma matching_bterms_map_unfold_abstractions_b {o} :
forall vars lib abs (bs : list (@BTerm o)),
matching_bterms vars (map (unfold_abstractions_b lib abs) bs)
<-> matching_bterms vars bs.
Proof.
introv.
unfold matching_bterms.
allrw map_map; unfold compose.
split; intro h; repnd; dands; auto; allrw; apply eq_maps;
introv i; destruct x; unfold num_bvars; simpl; auto.
Qed.
Hint Rewrite @matching_bterms_map_unfold_abstractions_b : slow.
Lemma matching_entry_map_unfold_abstractions_b {o} :
forall abs opabs vars lib' abs' (bs : list (@BTerm o)),
matching_entry abs opabs vars (map (unfold_abstractions_b lib' abs') bs)
<-> matching_entry abs opabs vars bs.
Proof.
introv; unfold matching_entry.
split; intro h; repnd; dands; autorewrite with slow in *; auto.
Qed.
Hint Rewrite @matching_entry_map_unfold_abstractions_b : slow.
Lemma find_entry_map_unfold_abstractions_b_eq {o} :
forall lib abs (bs : list (@BTerm o)) lib' abs',
find_entry lib abs (map (unfold_abstractions_b lib' abs') bs)
= find_entry lib abs bs.
Proof.
induction lib; introv; simpl; auto.
destruct a; boolvar; auto; allrw @not_matching_entry_iff;
allrw @matching_entry_map_unfold_abstractions_b; tcsp.
destruct n.
allrw @matching_entry_map_unfold_abstractions_b; tcsp.
Qed.
Hint Rewrite @find_entry_map_unfold_abstractions_b_eq : slow.
Lemma matching_bterm_lsubst_bterms_aux {o} :
forall vars (bs : list (@BTerm o)) sub,
matching_bterms vars (lsubst_bterms_aux bs sub)
<-> matching_bterms vars bs.
Proof.
introv.
unfold matching_bterms, lsubst_bterms_aux.
allrw map_map; unfold compose.
split; intro h; repnd; dands; auto; allrw; apply eq_maps;
introv i; destruct x; unfold num_bvars; simpl; auto.
Qed.
Hint Rewrite @matching_bterm_lsubst_bterms_aux : slow.
Lemma matching_entry_lsubst_bterms_aux {o} :
forall abs opabs vars sub (bs : list (@BTerm o)),
matching_entry abs opabs vars (lsubst_bterms_aux bs sub)
<-> matching_entry abs opabs vars bs.
Proof.
introv; unfold matching_entry.
split; intro h; repnd; dands; auto; autorewrite with slow in *; auto.
Qed.
Hint Rewrite @matching_entry_lsubst_bterms_aux : slow.
Lemma find_entry_lsubst_bterms_aux {o} :
forall lib abs sub (bs : list (@BTerm o)),
find_entry lib abs (lsubst_bterms_aux bs sub)
= find_entry lib abs bs.
Proof.
induction lib; introv; simpl; auto.
destruct a; boolvar; auto; allrw @not_matching_entry_iff;
allrw @matching_entry_lsubst_bterms_aux; tcsp.
destruct n.
allrw @matching_entry_lsubst_bterms_aux; tcsp.
Qed.
Hint Rewrite @find_entry_lsubst_bterms_aux : slow.
Lemma alpha_eq_lsubst_mk_abs_subst2 {o} :
forall rhs vars (bs1 bs2 : list (@BTerm o)),
length vars = length bs1
-> length vars = length bs2
-> matching_bterms vars bs1
-> matching_bterms vars bs2
-> socovered rhs vars
-> map num_bvars bs1 = map num_bvars bs2
-> (forall b1 b2, LIn (b1,b2) (combine bs1 bs2) -> alpha_eq_bterm b1 b2)
-> alpha_eq (mk_instance vars bs1 rhs) (mk_instance vars bs2 rhs).
Proof.
introv lbs1 lbs2 m1 m2 cov eqmaps imp.
apply alphaeq_eq.
apply mk_instance_alpha_congr; auto.
unfold bin_rel_bterm.
unfold binrel_list.
applydup map_eq_length_eq in eqmaps; dands; auto.
introv i.
apply alphaeqbt_eq.
apply imp.
apply in_nth_combine; auto.
Qed.
Lemma length_lsubst_bterms_aux {o} :
forall (bs : list (@BTerm o)) sub,
length (lsubst_bterms_aux bs sub) = length bs.
Proof.
introv; unfold lsubst_bterms_aux; autorewrite with slow; auto.
Qed.
Hint Rewrite @length_lsubst_bterms_aux : slow.
Lemma matching_entry_implies_eq_lengths {o} :
forall abs1 abs2 vars (bs : list (@BTerm o)),
matching_entry abs1 abs2 vars bs
-> length vars = length bs.
Proof.
introv m; unfold matching_entry in m; repnd.
unfold matching_bterms in m.
match goal with
| [ H : map ?a ?b = map ?c ?d |- _ ] =>
assert (length (map a b) = length (map c d)) as q; allrw; auto
end.
autorewrite with slow in *; auto.
Qed.
Hint Resolve matching_entry_implies_eq_lengths : slow.
Hint Rewrite @fold_lsubst_bterms_aux : slow.
Lemma unfold_abs_map_unfold_abstractions_b_none_implies {o} :
forall lib abs lib' abs' (bs : list (@BTerm o)),
unfold_abs lib abs (map (unfold_abstractions_b lib' abs') bs) = None
-> unfold_abs lib abs bs = None.
Proof.
induction lib; introv h; simpl in *; tcsp.
remember (unfold_abs_entry a abs (map (unfold_abstractions_b lib' abs') bs)) as q.
symmetry in Heqq; destruct q; ginv.
apply IHlib in h; allrw.
destruct a; simpl in *; boolvar; auto; ginv.
allrw @not_matching_entry_iff.
destruct n.
autorewrite with slow in *; auto.
Qed.
Lemma implies_unfold_abs_map_unfold_abstractions_b_none {o} :
forall lib abs lib' abs' (bs : list (@BTerm o)),
unfold_abs lib abs bs = None
-> unfold_abs lib abs (map (unfold_abstractions_b lib' abs') bs) = None.
Proof.
induction lib; introv h; simpl in *; tcsp.
remember (unfold_abs_entry a abs bs) as q.
symmetry in Heqq; destruct q; ginv.
eapply IHlib in h.
allrw.
destruct a; simpl in *; boolvar; auto; ginv.
allrw @not_matching_entry_iff.
destruct n.
autorewrite with slow in *; auto.
Qed.
Lemma implies_unfold_abs_lsubst_bterms_aux_none {o} :
forall lib abs sub (bs : list (@BTerm o)),
unfold_abs lib abs bs = None
-> unfold_abs lib abs (lsubst_bterms_aux bs sub) = None.
Proof.
induction lib; introv h; simpl in *; tcsp.
remember (unfold_abs_entry a abs bs) as q.
symmetry in Heqq; destruct q; ginv.
eapply IHlib in h.
allrw.
destruct a; simpl in *; boolvar; auto; ginv.
allrw @not_matching_entry_iff.
destruct n.
autorewrite with slow in *; auto.
Qed.
Lemma lsubst_aux_nrut_sub_unfold_abstractions {o} :
forall lib abs (t : @NTerm o) sub l,
nrut_sub l sub
-> alpha_eq
(lsubst_aux (unfold_abstractions lib abs t) sub)
(unfold_abstractions lib abs (lsubst_aux t sub)).
Proof.
nterm_ind t as [v|f|op bs ind] Case; introv nrut; simpl in *; tcsp.
- Case "vterm".
remember (sub_find sub v) as sf; symmetry in Heqsf; destruct sf; simpl in *; tcsp.
eapply sub_find_nrut_sub_some_implies in Heqsf;[|eauto]; exrepnd; subst; simpl; tcsp.
- Case "oterm".
unfold maybe_unfold; simpl.
dopid op as [can|ncan|exc|abs'] SCase; simpl in *; tcsp.
+ allrw map_map; unfold compose.
apply alpha_eq_oterm_combine; autorewrite with slow; dands; auto.
introv i.
rewrite <- map_combine in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a as [vs t]; simpl.
apply alpha_eq_bterm_congr.
eapply ind; eauto 2 with slow.
+ allrw map_map; unfold compose.
apply alpha_eq_oterm_combine; autorewrite with slow; dands; auto.
introv i.
rewrite <- map_combine in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a as [vs t]; simpl.
apply alpha_eq_bterm_congr.
eapply ind; eauto 2 with slow.
+ allrw map_map; unfold compose.
apply alpha_eq_oterm_combine; autorewrite with slow; dands; auto.
introv i.
rewrite <- map_combine in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a as [vs t]; simpl.
apply alpha_eq_bterm_congr.
eapply ind; eauto 2 with slow.
+ boolvar.
* remember (unfold_abs lib abs' (map (unfold_abstractions_b lib abs) bs)) as u.
symmetry in Hequ; destruct u.
{
apply unfold_abs_implies_find_entry in Hequ; exrepnd; subst.
autorewrite with slow in *.
erewrite find_entry_implies_unfold_abs;
[|rewrite <- Hequ1; autorewrite with slow in *; auto].
eapply alpha_eq_trans;
[apply alpha_eq_sym;
apply alpha_eq_lsubst_aux_mk_instance|];
autorewrite with slow in *;
eauto 3 with slow.
apply alpha_eq_lsubst_mk_abs_subst2;
autorewrite with slow in *; eauto 2 with slow.
{ unfold lsubst_bterms_aux; allrw map_map; unfold compose.
apply eq_maps; introv i; destruct x; unfold num_bvars; simpl; auto. }
introv i.
unfold lsubst_bterms_aux in i.
allrw map_map; unfold compose in i.
rewrite <- map_combine in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a as [vs t]; simpl.
apply alpha_eq_bterm_congr.
eapply ind; eauto 2 with slow.
}
{
apply unfold_abs_map_unfold_abstractions_b_none_implies in Hequ.
rewrite implies_unfold_abs_map_unfold_abstractions_b_none;
[|autorewrite with slow;rewrite implies_unfold_abs_lsubst_bterms_aux_none;auto].
simpl.
allrw map_map; unfold compose.
apply alpha_eq_oterm_combine; autorewrite with slow; dands; auto.
introv i.
rewrite <- map_combine in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a as [vs t]; simpl.
apply alpha_eq_bterm_congr.
eapply ind; eauto 2 with slow.
}
* simpl.
allrw map_map; unfold compose.
apply alpha_eq_oterm_combine; autorewrite with slow; dands; auto.
introv i.
rewrite <- map_combine in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a as [vs t]; simpl.
apply alpha_eq_bterm_congr.
eapply ind; eauto 2 with slow.
Qed.
Lemma approx_star_unfold_abstractions_left {o} :
forall lib abs (t : @NTerm o),
nt_wf t
-> approx_star lib (unfold_abstractions lib abs t) t.
Proof.
nterm_ind1s t as [v|f|op bs ind] Case; introv wf; simpl; auto;
try (complete (apply approx_star_refl; auto)).
Case "oterm".
allrw @nt_wf_oterm_iff; repnd.
unfold maybe_unfold; simpl.
destruct op; simpl in *.
- econstructor; autorewrite with slow;[| |apply approx_open_refl]; auto.
apply lblift_sub_eq; autorewrite with slow; dands; auto.
introv i.
rewrite <- map_combine_left in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a as [l t]; simpl in *.
eexists; eexists; eexists; dands; try (apply alphaeqbt_refl).
left; dands; tcsp; try (intro xx; inversion xx).
applydup wf in i0.
allrw @bt_wf_iff.
eapply ind; eauto 2 with slow.
- econstructor; autorewrite with slow;[| |apply approx_open_refl]; auto.
apply lblift_sub_eq; autorewrite with slow; dands; auto.
introv i.
rewrite <- map_combine_left in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a as [l t]; simpl in *.
eexists; eexists; eexists; dands; try (apply alphaeqbt_refl).
destruct (@dec_op_eq_fresh o (NCan n)) as [d|d]; ginv.
+ right.
pose proof (exists_nrut_sub l (get_utokens (unfold_abstractions lib abs t) ++ get_utokens t)) as q.
exrepnd.
exists sub; dands; auto.
repeat (rewrite cl_lsubst_lsubst_aux; eauto 2 with slow;[]).
eapply approx_star_alpha_fun_l;
[|apply alpha_eq_sym;eapply lsubst_aux_nrut_sub_unfold_abstractions;eauto].
applydup wf in i0.
allrw @bt_wf_iff.
eapply ind; eauto 2 with slow.
{ rewrite simple_osize_lsubst_aux; eauto 2 with slow. }
{ apply implies_wf_lsubst_aux; eauto 2 with slow. }
+ left; dands; tcsp; try (intro xx; inversion xx).
applydup wf in i0.
allrw @bt_wf_iff.
eapply ind; eauto 2 with slow.
- econstructor; autorewrite with slow;[| |apply approx_open_refl]; auto.
apply lblift_sub_eq; autorewrite with slow; dands; auto.
introv i.
rewrite <- map_combine_left in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a as [l t]; simpl in *.
eexists; eexists; eexists; dands; try (apply alphaeqbt_refl).
left; dands; tcsp; try (intro xx; inversion xx).
applydup wf in i0.
allrw @bt_wf_iff.
eapply ind; eauto with slow.
- rename o0 into a.
boolvar.
+ remember (unfold_abs lib a (map (unfold_abstractions_b lib abs) bs)) as ua.
symmetry in Hequa; destruct ua.
* apply (approx_star_preserves_reduces_to_left
_ (oterm (Abs a) (map (unfold_abstractions_b lib abs) bs)));
[|apply reduces_to_if_step; csunf; simpl; unfold compute_step_lib; allrw; auto|].
{
apply wf_term_eq.
apply nt_wf_oterm_iff.
allrw map_map; unfold compose; simpl.
allrw <- .
dands.
- apply eq_maps; introv i; destruct x; unfold num_bvars; simpl; auto.
- introv i; allrw in_map_iff; exrepnd; subst.
destruct a0 as [vs t].
applydup wf in i1.
allrw @bt_wf_iff; eauto 2 with slow.
}
econstructor; autorewrite with slow;[| |apply approx_open_refl]; auto.
apply lblift_sub_eq; autorewrite with slow; dands; auto.
introv i.
rewrite <- map_combine_left in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a0 as [vs t]; simpl in *.
eexists; eexists; eexists; dands; try (apply alphaeqbt_refl).
left; dands; tcsp; try (intro xx; inversion xx).
applydup wf in i0.
allrw @bt_wf_iff.
eapply ind; eauto with slow.
* econstructor; autorewrite with slow;[| |apply approx_open_refl]; auto.
apply lblift_sub_eq; autorewrite with slow; dands; auto.
introv i.
rewrite <- map_combine_left in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a0 as [vs t]; simpl in *.
eexists; eexists; eexists; dands; try (apply alphaeqbt_refl).
left; dands; tcsp; try (intro xx; inversion xx).
applydup wf in i0.
allrw @bt_wf_iff.
eapply ind; eauto with slow.
+ econstructor; autorewrite with slow;[| |apply approx_open_refl]; auto.
apply lblift_sub_eq; autorewrite with slow; dands; auto.
introv i.
rewrite <- map_combine_left in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a0 as [vs t]; simpl in *.
eexists; eexists; eexists; dands; try (apply alphaeqbt_refl).
left; dands; tcsp; try (intro xx; inversion xx).
applydup wf in i0.
allrw @bt_wf_iff.
eapply ind; eauto with slow.
Qed.
Lemma lblift_sub_approx_star_refl {o} :
forall op lib (bs : list (@BTerm o)),
(forall b, LIn b bs -> bt_wf b)
-> lblift_sub op (approx_star lib) bs bs.
Proof.
introv imp.
apply lblift_sub_eq; dands; auto.
introv i.
apply in_combine_same in i; repnd; subst.
unfold blift_sub.
destruct b2 as [l t].
applydup imp in i0.
allrw @bt_wf_iff.
eexists; eexists; eexists; dands; try (apply alphaeqbt_refl).
destruct (dec_op_eq_fresh op) as [d|d]; subst; tcsp.
- right.
pose proof (exists_nrut_sub l (get_utokens t ++ get_utokens t)) as q.
exrepnd.
exists sub; dands; auto.
apply approx_star_refl.
rewrite cl_lsubst_lsubst_aux; eauto 2 with slow;[].
apply implies_wf_lsubst_aux; eauto 2 with slow.
- left; dands; tcsp; try (intro xx; inversion xx).
apply approx_star_refl; eauto 2 with slow.
Qed.
Lemma map_combine_right :
forall {T1 T2 T3} (f : T2 -> T3) (l1 : list T1) (l2 : list T2),
map (fun x => (fst x, f (snd x))) (combine l1 l2)
= combine l1 (map f l2).
Proof.
induction l1; introv; allsimpl; auto.
destruct l2; allsimpl; auto.
rw IHl1; auto.
Qed.
Lemma approx_star_unfold_abstractions_right {o} :
forall lib abs (t : @NTerm o),
nt_wf t
-> approx_star lib t (unfold_abstractions lib abs t).
Proof.
nterm_ind1s t as [v|f|op bs ind] Case; introv wf; simpl; auto;
try (complete (apply approx_star_refl; auto)).
Case "oterm".
allrw @nt_wf_oterm_iff; repnd.
unfold maybe_unfold; simpl.
destruct op; simpl in *.
- econstructor; autorewrite with slow;[| |apply approx_open_refl];
autorewrite with slow; auto;
[|apply nt_wf_oterm_iff; allrw map_map; unfold compose; simpl; allrw <-;dands;
[apply eq_maps; introv i; destruct x; unfold num_bvars; simpl; auto
|introv i; apply in_map_iff in i; exrepnd; subst; destruct a as [l t]; simpl;
applydup wf in i1; allrw @bt_wf_iff; eauto 2 with slow]
].
apply lblift_sub_eq; autorewrite with slow; dands; auto.
introv i.
rewrite <- map_combine_right in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a as [l t]; simpl in *.
eexists; eexists; eexists; dands; try (apply alphaeqbt_refl).
left; dands; tcsp; try (intro xx; inversion xx).
applydup wf in i0.
allrw @bt_wf_iff.
eapply ind; eauto 2 with slow.
- econstructor; autorewrite with slow;[| |apply approx_open_refl];
autorewrite with slow; auto;
[|apply nt_wf_oterm_iff; allrw map_map; unfold compose; simpl; allrw <-;dands;
[apply eq_maps; introv i; destruct x; unfold num_bvars; simpl; auto
|introv i; apply in_map_iff in i; exrepnd; subst; destruct a as [l t]; simpl;
applydup wf in i1; allrw @bt_wf_iff; eauto 2 with slow]
].
apply lblift_sub_eq; autorewrite with slow; dands; auto.
introv i.
rewrite <- map_combine_right in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a as [l t]; simpl in *.
eexists; eexists; eexists; dands; try (apply alphaeqbt_refl).
destruct (@dec_op_eq_fresh o (NCan n)) as [d|d]; ginv.
+ right.
pose proof (exists_nrut_sub l (get_utokens t ++ get_utokens (unfold_abstractions lib abs t))) as q.
exrepnd.
exists sub; dands; auto.
repeat (rewrite cl_lsubst_lsubst_aux; eauto 2 with slow;[]).
eapply approx_star_alpha_fun_r;
[|apply alpha_eq_sym;eapply lsubst_aux_nrut_sub_unfold_abstractions;eauto].
applydup wf in i0.
allrw @bt_wf_iff.
eapply ind; eauto 2 with slow.
{ rewrite simple_osize_lsubst_aux; eauto 2 with slow. }
{ apply implies_wf_lsubst_aux; eauto 2 with slow. }
+ left; dands; tcsp; try (intro xx; inversion xx).
applydup wf in i0.
allrw @bt_wf_iff.
eapply ind; eauto 2 with slow.
- econstructor; autorewrite with slow;[| |apply approx_open_refl];
autorewrite with slow; auto;
[|apply nt_wf_oterm_iff; allrw map_map; unfold compose; simpl; allrw <-;dands;
[apply eq_maps; introv i; destruct x; unfold num_bvars; simpl; auto
|introv i; apply in_map_iff in i; exrepnd; subst; destruct a as [l t]; simpl;
applydup wf in i1; allrw @bt_wf_iff; eauto 2 with slow]
].
apply lblift_sub_eq; autorewrite with slow; dands; auto.
introv i.
rewrite <- map_combine_right in i.
apply in_map_iff in i; exrepnd; ginv.
apply in_combine_same in i1; repnd; subst.
destruct a as [l t]; simpl in *.
eexists; eexists; eexists; dands; try (apply alphaeqbt_refl).
left; dands; tcsp; try (intro xx; inversion xx).
applydup wf in i0.
allrw @bt_wf_iff.
eapply ind; eauto 2 with slow.
- rename o0 into a.
boolvar.
+ remember (unfold_abs lib a (map (unfold_abstractions_b lib abs) bs)) as ua.
symmetry in Hequa; destruct ua.