-
Notifications
You must be signed in to change notification settings - Fork 0
/
space_efficiency_in_brr.v
1166 lines (1094 loc) · 41.4 KB
/
space_efficiency_in_brr.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
Set Bullet Behavior "Strict Subproofs".
Require Import PoplLibraries.Math.
Require Import PoplLibraries.Setoid_library.
Require Import PoplLibraries.Recursion.
Require Import PoplLibraries.Mutual_wf_fix.
(* This development is quite ad-hoc, but sufficient to prove gamma completeness for subtyping in the BRR system. *)
Inductive Ann : Set :=
| Opt : Ann
| Req : Ann.
Inductive GType : Set :=
| GDyn : GType
| GInt : GType
| GBool : GType
| GFun : GType -> GType -> GType
| GRec : list GType_mapping -> GType
| GRow : list GType_mapping -> GType
with GType_mapping : Set :=
| m_missing : GType_mapping
| m_map : Ann -> GType -> GType_mapping.
Definition GType_good_ind (P : GType -> Prop) (P0 : GType_mapping -> Prop)
(f : P GDyn)
(f0 : P GInt)
(f1 : P GBool)
(f2 : forall S_1 S_2,
P S_1 -> P S_2 -> P (GFun S_1 S_2))
(f3 : forall l,
Forall P0 l ->
P (GRec l))
(f4 : forall l,
Forall P0 l ->
P (GRow l))
(f5 : P0 m_missing)
(f6 : forall a s,
P s ->
P0 (m_map a s))
: forall s, P s.
refine (fix ind (s : GType) {struct s} : P s :=
let fix l_ind (l : list GType_mapping) {struct l} : Forall P0 l :=
_
in
match s with
| GDyn => f
| GInt => f0
| GBool => f1
| GFun dom cod => f2 dom cod (ind dom) (ind cod)
| GRec l => f3 l (l_ind l)
| GRow l => f4 l (l_ind l)
end).
destruct l;
econstructor.
destruct g.
eapply f5.
eapply f6.
eapply ind.
eapply l_ind.
Defined.
Fixpoint size_GT (x : GType) {struct x} : nat :=
match x with
| GFun dom cod => 1 + size_GT dom + size_GT cod
| GRec l => 1 + fold_right (plus) 0 (map size_GT_map l)
| GRow l => 1 + fold_right (plus) 0 (map size_GT_map l)
| _ => 1
end
with size_GT_map (x : GType_mapping) {struct x} : nat :=
(match x with
| m_map _ S' => size_GT S'
| _ => 0
end).
Fixpoint height_GT (x : GType) {struct x} : nat :=
match x with
| GFun dom cod => S (Nat.max (height_GT dom) (height_GT cod))
| GRec l => S ( fold_right (Nat.max) 0 (map height_GT_map l))
| GRow l => S ( fold_right (Nat.max) 0 (map height_GT_map l))
| _ => 0
end
with height_GT_map (x : GType_mapping) {struct x} : nat :=
(match x with
| m_map _ S' => (height_GT S') (* probably should not have the S *)
| _ => 0
end).
Theorem size_GT_map_ge1 : forall x,
0 <= size_GT_map x.
Proof with eauto with math.
destruct x; simpl...
Qed.
Theorem size_GT_ge1 : forall x,
1 <= size_GT x.
Proof with eauto with math.
destruct x; simpl...
Qed.
Theorem size_GT_gt0 : forall x,
0 < size_GT x.
Proof with eauto with math.
destruct x; simpl...
Qed.
Hint Resolve size_GT_map_ge1 : math.
Hint Resolve size_GT_gt0 : math.
Hint Resolve size_GT_ge1 : math.
Definition filter_error {A : Type} : list (option A) -> option (list A) :=
fun l =>
fold_right (fun hd tl =>
match hd, tl with
| Some hd, Some tl => Some (hd :: tl)
| _, _ => None
end) (Some []) l.
Inductive GAnn : Set :=
| Rec : GAnn
| Row : GAnn.
Definition g_meet_mapping_d (a_1 : GAnn) (M_2 : GType_mapping) : option GType_mapping :=
match a_1, M_2 with
| _, m_missing => Some m_missing
| Rec, m_map Opt _ => Some m_missing
| Row, m_map a S' => Some (m_map a S')
| _, _ => None
end.
Fixpoint g_meet (S_1 S_2 : GType) {struct S_1} : option GType :=
match S_1, S_2 with
| GInt, GInt => Some GInt
| GBool, GBool => Some GBool
| GFun S_11 S_12, GFun S_21 S_22 =>
match g_meet S_11 S_21, g_meet S_12 S_22 with
| Some S_dom, Some S_cod => Some (GFun S_dom S_cod)
| _ , _ => None
end
| GRec l_1, GRec l_2 =>
match filter_error (zip_fill (g_meet_mapping_d Rec) (g_meet_mapping_d Rec) g_meet_mapping l_1 l_2) with
| Some l => Some (GRec l)
| None => None
end
| GRow l_1, GRec l_2 =>
match filter_error (zip_fill (g_meet_mapping_d Row) (g_meet_mapping_d Rec) g_meet_mapping l_1 l_2) with
| Some l => Some (GRec l)
| None => None
end
| GRec l_1, GRow l_2 =>
match filter_error (zip_fill (g_meet_mapping_d Rec) (g_meet_mapping_d Row) g_meet_mapping l_1 l_2) with
| Some l => Some (GRec l)
| None => None
end
| GRow l_1, GRow l_2 =>
match filter_error (zip_fill (g_meet_mapping_d Row) (g_meet_mapping_d Row) g_meet_mapping l_1 l_2) with
| Some l => Some (GRow l)
| None => None
end
| _, _ => None
end
with g_meet_mapping (M_1 M_2 : GType_mapping) {struct M_1} : option GType_mapping :=
match M_1, M_2 with
| m_missing, m_missing => Some m_missing
| m_missing, m_map Opt _ => Some m_missing
| m_map Opt _, m_missing => Some m_missing
| m_map Req S_1, m_map _ S_2 => match g_meet S_1 S_2 with
| Some S_12 => Some (m_map Req S_12)
| None => None
end
| m_map _ S_1, m_map Req S_2 => match g_meet S_1 S_2 with
| Some S_12 => Some (m_map Req S_12)
| None => None
end
| _, _ => None
end
.
Definition height_oGT (x : option GType) : nat :=
match x with
| Some x => height_GT x
| None => 0
end.
Transparent height_oGT.
Lemma fold_right_max_le : (* move to math *)
forall l l0,
Forall2 le l l0 ->
fold_right Nat.max 0 l <= fold_right Nat.max 0 l0.
Proof with eauto with math.
intros.
induction H; simpl...
Qed.
Lemma filter_error_eq {A : Type} : forall l l0,
@filter_error A l = Some l0 ->
Forall2 (fun x y => x = Some y) l l0.
Proof with eauto with math.
induction l; intros; simpl in H; inversion H; subst...
destruct a; try congruence...
destruct (filter_error l) eqn:?; try congruence.
inversion H; subst.
specialize (IHl l1 eq_refl)...
Qed.
Definition height_oGT_map (x : option GType_mapping) : nat :=
match x with
| Some x => height_GT_map x
| None => 0
end.
Lemma height_l : forall (l: list (option GType_mapping)) l',
filter_error l = Some l' ->
fold_right (Nat.max) 0 (map height_GT_map l') = fold_right (Nat.max) 0 (map height_oGT_map l).
Proof with eauto with math.
induction l; intros;
simpl in H; inversion H...
destruct a; try congruence.
destruct (filter_error l)...
specialize (IHl _ eq_refl).
inversion H; subst; simpl...
Qed.
Lemma map_zipfill {A B C D: Type} : forall (l : list A) (l0 : list B) f g h z,
@map C D z (zip_fill f g h l l0) =
(zip_fill (fun x => z (f x)) (fun x => z (g x)) (fun x y => z (h x y)) l l0)
.
Proof with eauto with math.
induction l; induction l0; intros; simpl in *...
all: f_equal...
Qed.
Opaque Nat.max.
Fixpoint domain (x : GType) : nat :=
match x with
| GFun t_1 t_2 => (Nat.max (domain t_1) (domain t_2))
| GRec l => Nat.max ( length l) (fold_right (Nat.max) 0 (map domain_mapping l))
| GRow l => Nat.max ( length l) (fold_right (Nat.max) 0 (map domain_mapping l))
| _ => 0
end (* While we could likely make the bound a tiny bit lower, this would certainly simplify things *)
with domain_mapping (m : GType_mapping) : nat :=
match m with
| m_missing => 0
| m_map _ S' => domain S'
end.
Definition GType_good_ind_2 (P : GType -> Prop) (P0 : list GType_mapping -> Prop)
(f : P GDyn)
(f0 : P GInt)
(f1 : P GBool)
(f2 : forall S_1 S_2,
P S_1 -> P S_2 -> P (GFun S_1 S_2))
(f3 : forall l,
P0 l ->
P (GRec l))
(f4 : forall l,
P0 l ->
P (GRow l))
(f5 : P0 [])
(f6 : forall l,
P0 l ->
P0 (m_missing :: l))
(f7 : forall a s l,
P s ->
P0 l ->
P0 (m_map a s :: l) )
: forall s, P s.
refine (fix ind (s : GType) {struct s} : P s :=
let fix l_ind (l : list GType_mapping) {struct l} : P0 l :=
_
in
match s with
| GDyn => f
| GInt => f0
| GBool => f1
| GFun dom cod => f2 dom cod (ind dom) (ind cod)
| GRec l => f3 l (l_ind l)
| GRow l => f4 l (l_ind l)
end).
destruct l.
eassumption.
destruct g.
eapply f6.
eapply l_ind.
eapply f7.
eapply ind.
eapply l_ind.
Defined.
Lemma expand_max_2_succ : forall x y,
((Nat.max 2 x) ^ y) + ((Nat.max 2 x) ^ y) <= (Nat.max 2 x) ^ S (y).
Proof with eauto with math.
intros.
replace (Nat.max 2 x ^ y + Nat.max 2 x ^ y) with
(2 * Nat.max 2 x ^ y)...
eapply Nat.le_trans with (m := (Nat.max 2 x) * (Nat.max 2 x ^ y))...
destruct (Nat.max_spec 2 x)...
destruct H.
rewrite H0...
epose proof (Nat.mul_le_mono_l 2 x (x ^ y) _)...
Unshelve.
idtac...
Qed.
Lemma expand_max_n_succ : forall n x y,
2 <= n ->
((Nat.max n x) ^ y) + ((Nat.max n x) ^ y) <= (Nat.max n x) ^ S (y).
Proof with eauto with math.
intros.
replace (Nat.max n x ^ y + Nat.max n x ^ y) with
(2 * Nat.max n x ^ y)...
eapply Nat.le_trans with (m := (Nat.max n x) * (Nat.max n x ^ y))...
destruct (Nat.max_spec n x).
destruct H; destruct H0; try rewrite H0; subst...
epose proof (Nat.mul_le_mono_l 2 x (x ^ y) _)...
enough (2 <= Nat.max (S m) x)...
epose proof (Nat.mul_le_mono_l 2 (Nat.max (S m) x) (Nat.max (S m) x ^ y) _)...
destruct H0; try rewrite H1; subst...
epose proof (Nat.mul_le_mono_l 2 n (n ^ y) _)...
Unshelve.
all: idtac...
Qed.
Lemma expand_n_succ : forall n y,
2 <= n ->
(n ^ y) + (n ^ y) <= n ^ S (y).
Proof with eauto with math.
intros...
replace (n ^ y + n ^ y) with
(2 * n ^ y)...
eapply Nat.le_trans with (m := n * (n ^ y))...
epose proof (Nat.mul_le_mono_l 2 n (n ^ y) _)...
Unshelve.
all: idtac...
Qed.
Lemma expand_3_n : forall n,
3 * n = n + n + n.
Proof with eauto with math.
idtac...
Qed.
Lemma pow_gt1 : forall x y,
1 <= (S x) ^ (S ( y)).
Proof with eauto with math.
destruct x...
induction y...
simpl in *...
induction y;
simpl in *...
Qed.
Lemma pow_gtx : forall x y z,
z <= x ->
z <= ( x) ^ (S ( y)).
Proof with eauto with math.
destruct x...
induction y...
destruct z;
simpl in *...
destruct z;
simpl in *; intros...
specialize (IHy (S z))...
Qed.
Lemma forall_add_distr {A : Type}: forall f g (l : list A),
Forall (fun x => f x <= g x) l ->
fold_right (plus) 0 (map f l) <=
fold_right (plus) 0 (map g l).
Proof with eauto with math.
intros.
induction H...
simpl...
Qed.
Lemma forall_add_max {A : Type}: forall f k (l : list A),
Forall (fun x => f x <= k) l ->
fold_right (plus) 0 (map f l) <=
(length l) * k.
Proof with eauto with math.
intros.
induction H...
simpl...
Qed.
Hint Resolve pow_gt1: math.
Hint Resolve pow_gtx: math.
Hint Resolve Nat.pow_le_mono : math.
Lemma height_bound : forall S_1,
size_GT S_1 <= (S (2 + (domain S_1))) ^ S (height_GT S_1). (* (3 + domain S_1) ^ S (height_GT S_1) works for function types, but it is harder to prove for records/rows . The induction hypothesis looses too much info via max. *)
Proof with eauto with math.
intros.
eapply GType_good_ind with (s := S_1)
(P0 := fun x =>
size_GT_map x <=
S (2 + domain_mapping x) ^ S (height_GT_map x)).
all: try solve [simpl; eauto with math].
all: intros...
- cbn beta iota delta [size_GT].
eapply Nat.le_trans with (m := 1 + (S (2 + (domain S_0)) ^ S (height_GT S_0)) + (S (2 + (domain S_2)) ^ S (height_GT S_2)))...
clear H.
clear H0.
eapply Nat.le_trans with (m := 1 + S (2 + (domain (GFun S_0 S_2))) ^ (height_GT (GFun S_0 S_2)) + (S( 2 + (domain S_2)) ^ S (height_GT S_2)))...
epose proof (Nat.pow_le_mono
(S (2 + (domain S_0))) (S (height_GT S_0))
(S (2 + (domain (GFun S_0 S_2)))) (height_GT (GFun S_0 S_2)) _ _ _
)...
Unshelve.
2: shelve.
all: try solve [simpl; eauto with math].
eapply Nat.le_trans with (m := 1 + S (2 + (domain (GFun S_0 S_2))) ^ (height_GT (GFun S_0 S_2)) + (S(2 + (domain (GFun S_0 S_2))) ^ (height_GT (GFun S_0 S_2))))...
epose proof (Nat.pow_le_mono
(S (2 + (domain S_2))) (S (height_GT S_2))
(S (2 + (domain (GFun S_0 S_2)))) (height_GT (GFun S_0 S_2)) _ _ _
)...
Unshelve.
2: shelve.
all: try solve [simpl; eauto with math].
eapply Nat.le_trans with (m := 3 * (S(2 + (domain (GFun S_0 S_2))) ^ (height_GT (GFun S_0 S_2))))...
enough (1 <= (S(2 + (domain (GFun S_0 S_2))) ^ (height_GT (GFun S_0 S_2))))...
eapply Nat.le_trans with (m := S(2 + (domain (GFun S_0 S_2))) * (S(2 + (domain (GFun S_0 S_2))) ^ (height_GT (GFun S_0 S_2))))...
Unshelve.
idtac...
- cbn beta iota delta [ size_GT domain height_GT].
eapply Nat.le_trans with (m := 1 + (fold_right (plus) 0
(map (fun x => S (2 + domain_mapping x) ^ S (height_GT_map x)) l)))...
eapply forall_add_distr in H...
(* And this is the important step : *)
epose proof (forall_add_max (fun x => S (2 + domain_mapping x) ^ S (height_GT_map x))
(S (2 + fold_right Nat.max 0 (map domain_mapping l)) ^ S (fold_right Nat.max 0 (map height_GT_map l))) l _).
eapply forall_add_distr in H.
Unshelve.
2 : {
generalize dependent H.
induction l; intros...
inversion H; subst.
specialize (IHl H3).
econstructor...
- eapply Nat.pow_le_mono; simpl...
- eapply Forall_impl.
2: eapply IHl.
intros.
cbn beta iota in H0.
eapply Nat.le_trans;[ eapply H0|].
eapply Nat.pow_le_mono; simpl...
}
eapply Nat.le_trans with (m := 1 + length l *
S (2 + fold_right Nat.max 0 (map domain_mapping l)) ^ S (fold_right Nat.max 0 (map height_GT_map l)))...
eapply Nat.le_trans with (m := 1 + (2 + Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l))) *
S (2 + fold_right Nat.max 0 (map domain_mapping l)) ^ S (fold_right Nat.max 0 (map height_GT_map l)))...
eapply le_n_S...
eapply Nat.mul_le_mono_r...
eapply Nat.le_trans with (m := 1 + (2 + Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l))) *
S (2 + Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l))) ^ S (fold_right Nat.max 0 (map height_GT_map l)))...
eapply le_n_S...
eapply Nat.mul_le_mono_l...
eapply Nat.le_trans with (m := S (2 + Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l))) ^ S (fold_right Nat.max 0 (map height_GT_map l)) + (2 + Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l))) *
S (2 + Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l))) ^ S (fold_right Nat.max 0 (map height_GT_map l)))...
enough (1 <=S (2 + Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l))) ^ S (fold_right Nat.max 0 (map height_GT_map l)))...
- cbn beta iota delta [ size_GT domain height_GT].
eapply Nat.le_trans with (m := 1 + (fold_right (plus) 0
(map (fun x => S (2 + domain_mapping x) ^ S (height_GT_map x)) l)))...
eapply forall_add_distr in H...
(* And this is the important step : *)
epose proof (forall_add_max (fun x => S (2 + domain_mapping x) ^ S (height_GT_map x))
(S (2 + fold_right Nat.max 0 (map domain_mapping l)) ^ S (fold_right Nat.max 0 (map height_GT_map l))) l _).
eapply forall_add_distr in H.
Unshelve.
2 : {
generalize dependent H.
induction l; intros...
inversion H; subst.
specialize (IHl H3).
econstructor...
- eapply Nat.pow_le_mono; simpl...
- eapply Forall_impl.
2: eapply IHl.
intros.
cbn beta iota in H0.
eapply Nat.le_trans;[ eapply H0|].
eapply Nat.pow_le_mono; simpl...
}
eapply Nat.le_trans with (m := 1 + length l *
S (2 + fold_right Nat.max 0 (map domain_mapping l)) ^ S (fold_right Nat.max 0 (map height_GT_map l)))...
eapply Nat.le_trans with (m := 1 + (2 + Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l))) *
S (2 + fold_right Nat.max 0 (map domain_mapping l)) ^ S (fold_right Nat.max 0 (map height_GT_map l)))...
eapply le_n_S...
eapply Nat.mul_le_mono_r...
eapply Nat.le_trans with (m := 1 + (2 + Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l))) *
S (2 + Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l))) ^ S (fold_right Nat.max 0 (map height_GT_map l)))...
eapply le_n_S...
eapply Nat.mul_le_mono_l...
eapply Nat.le_trans with (m := S (2 + Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l))) ^ S (fold_right Nat.max 0 (map height_GT_map l)) + (2 + Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l))) *
S (2 + Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l))) ^ S (fold_right Nat.max 0 (map height_GT_map l)))...
enough (1 <=S (2 + Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l))) ^ S (fold_right Nat.max 0 (map height_GT_map l)))...
Qed.
Lemma height_meet : forall S_1 S_2,
height_oGT (g_meet S_1 S_2) <= Nat.max (height_GT S_1) (height_GT S_2).
Proof with eauto with math.
intros S_1.
eapply GType_good_ind with (s:=S_1) (P0 := fun M_1 => forall M_2,
match (g_meet_mapping M_1 M_2) with
| Some M' => height_GT_map M'
| None => 0
end <= Nat.max (height_GT_map M_1) (height_GT_map M_2)).
all: simpl; auto with math.
all: intros.
- destruct S_2; simpl...
- destruct S_2; simpl...
- destruct S_3; simpl...
destruct (g_meet S_0 S_3_1) eqn:?;
destruct (g_meet S_2 S_3_2) eqn:?; simpl...
specialize (H S_3_1).
specialize (H0 S_3_2).
rewrite Heqo in H.
rewrite Heqo0 in H0.
simpl in * ...
- destruct S_2; simpl...
+ rewrite <- Nat.succ_max_distr.
generalize dependent l0.
induction H...
* intros.
match goal with
| [ |- context[ match ?A with | Some _ => _ | None => _ end] ] =>
destruct A eqn:?
end;[|simpl]...
simpl.
eapply height_l in Heqo.
rewrite Heqo.
clear Heqo.
induction l0; simpl...
destruct a; simpl...
destruct a; simpl in *...
* intros.
specialize (IHForall (tl l0)).
clear H0.
generalize dependent l0.
induction l0; intros...
all: simpl in *.
-- destruct x; [ | destruct a]; simpl...
all: match goal with
| [ |- context[ match filter_error ?A with | Some _ => _ | None => _ end] ] =>
destruct (filter_error A) eqn:?
end...
simpl in *...
-- destruct (g_meet_mapping x a) eqn:?;[|simpl]...
match goal with
| [ |- context[ match filter_error ?A with | Some _ => _ | None => _ end] ] =>
destruct (filter_error A) eqn:?
end...
specialize (H a).
rewrite Heqo in H.
simpl in *...
+rewrite <- Nat.succ_max_distr.
generalize dependent l0.
induction H...
* intros.
match goal with
| [ |- context[ match ?A with | Some _ => _ | None => _ end] ] =>
destruct A eqn:?
end;[|simpl]...
simpl.
eapply height_l in Heqo.
rewrite Heqo.
clear Heqo.
induction l0; simpl...
destruct a; simpl...
destruct a; simpl in *...
* intros.
specialize (IHForall (tl l0)).
clear H0.
generalize dependent l0.
induction l0; intros...
all: simpl in *.
-- destruct x; [ | destruct a]; simpl...
all: match goal with
| [ |- context[ match filter_error ?A with | Some _ => _ | None => _ end] ] =>
destruct (filter_error A) eqn:?
end...
all: simpl in *...
-- destruct (g_meet_mapping x a) eqn:?;[|simpl]...
match goal with
| [ |- context[ match filter_error ?A with | Some _ => _ | None => _ end] ] =>
destruct (filter_error A) eqn:?
end...
specialize (H a).
rewrite Heqo in H.
simpl in *...
- destruct S_2; simpl...
+ rewrite <- Nat.succ_max_distr.
generalize dependent l0.
induction H...
* intros.
match goal with
| [ |- context[ match ?A with | Some _ => _ | None => _ end] ] =>
destruct A eqn:?
end;[|simpl]...
simpl.
eapply height_l in Heqo.
rewrite Heqo.
clear Heqo.
induction l0; simpl...
destruct a; simpl...
destruct a; simpl in *...
* intros.
specialize (IHForall (tl l0)).
clear H0.
generalize dependent l0.
induction l0; intros...
all: simpl in *.
-- destruct x; [ | destruct a]; simpl...
all: match goal with
| [ |- context[ match filter_error ?A with | Some _ => _ | None => _ end] ] =>
destruct (filter_error A) eqn:?
end...
simpl in *...
-- destruct (g_meet_mapping x a) eqn:?;[|simpl]...
match goal with
| [ |- context[ match filter_error ?A with | Some _ => _ | None => _ end] ] =>
destruct (filter_error A) eqn:?
end...
specialize (H a).
rewrite Heqo in H.
simpl in *...
+rewrite <- Nat.succ_max_distr.
generalize dependent l0.
induction H...
* intros.
match goal with
| [ |- context[ match ?A with | Some _ => _ | None => _ end] ] =>
destruct A eqn:?
end;[|simpl]...
simpl.
eapply height_l in Heqo.
rewrite Heqo.
clear Heqo.
induction l0; simpl...
destruct a; simpl...
destruct a; simpl in *...
* intros.
specialize (IHForall (tl l0)).
clear H0.
generalize dependent l0.
induction l0; intros...
all: simpl in *.
-- destruct x; [ | destruct a]; simpl...
all: match goal with
| [ |- context[ match filter_error ?A with | Some _ => _ | None => _ end] ] =>
destruct (filter_error A) eqn:?
end...
all: simpl in *...
-- destruct (g_meet_mapping x a) eqn:?;[|simpl]...
match goal with
| [ |- context[ match filter_error ?A with | Some _ => _ | None => _ end] ] =>
destruct (filter_error A) eqn:?
end...
specialize (H a).
rewrite Heqo in H.
simpl in *...
- destruct M_2; simpl...
destruct a; simpl...
- destruct a; destruct M_2...
destruct a; simpl...
all: specialize (H g); simpl in *.
all: destruct (g_meet s g); simpl in * ...
Qed.
Definition Ev := (GType * GType) % type.
Definition size_ev (x : Ev) :=
size_GT (fst x) + size_GT (snd x).
Definition height_ev (x : Ev) :=
Nat.max (height_GT (fst x)) (height_GT (snd x)).
Definition domain_ev (x : Ev) :=
Nat.max (domain (fst x)) (domain (snd x)).
Lemma ev_size_bound : forall x,
size_ev (x) <= 2 * S (2 + domain_ev x) ^ S (height_ev x). (* because we do size of each type *)
Proof with eauto with math.
intros.
unfold size_ev.
rewrite Nat.mul_succ_l.
rewrite Nat.mul_succ_l.
rewrite Nat.mul_0_l.
rewrite plus_O_n.
eapply Nat.add_le_mono.
all: unfold domain_ev.
all: unfold height_ev.
- eapply Nat.le_trans with (m := S (2 + (domain (fst x))) ^ S (height_GT (fst x)))...
eapply height_bound.
- eapply Nat.le_trans with (m := S (2 + (domain (snd x))) ^ S (height_GT (snd x)))...
eapply height_bound.
Qed.
Definition D (a : GAnn) : GType_mapping :=
match a with
| Rec => m_missing
| Row => m_map Opt GDyn
end.
(* NOTE: STRICT POSITIVITY RESTRICTIONS MAKE THIS A PROXY FOR THE PROOF ONLY, NOT A CORRECT DEFINITION*)
Inductive I : GType -> GType -> Ev -> Prop :=
| I_ii : I GInt GInt (GInt, GInt)
| I_bb : I GBool GBool (GBool, GBool)
| I_dd : I GDyn GDyn (GDyn, GDyn)
| I_id : I GInt GDyn (GInt, GInt)
| I_di : I GDyn GInt (GInt, GInt)
| I_bd : I GBool GDyn (GBool, GBool)
| I_db : I GDyn GBool (GBool, GBool)
| I_fd : forall S_1 S_2 ev,
I (GFun S_1 S_2) (GFun GDyn GDyn) ev ->
I (GFun S_1 S_2) GDyn ev
| I_df : forall S_3 S_4 ev,
I (GFun GDyn GDyn) (GFun S_3 S_4) ev ->
I GDyn (GFun S_3 S_4) ev
| I_ff : forall S_11 S_12 S_21 S_22 S'_11 S'_12 S'_21 S'_22,
I S_21 S_11 (S'_21, S'_11) ->
I S_12 S_22 (S'_12, S'_22) ->
I (GFun S_11 S_12) (GFun S_21 S_22) (GFun S'_11 S'_12, GFun S'_21 S'_22)
| I_red : forall l ev,
I (GRec l) (GRow []) ev ->
I (GRec l) GDyn ev
| I_rod : forall l ev,
I (GRow l) (GRow []) ev ->
I (GRow l) GDyn ev
| I_dre : forall l ev,
I (GRow []) (GRec l) ev ->
I GDyn (GRec l) ev
| I_dro : forall l ev,
I (GRow []) (GRow l) ev ->
I GDyn (GRow l) ev
| I_rere : forall l_1 l_2 l_3 l_4,
FAI Rec Rec l_1 l_2 (l_3, l_4) ->
I (GRec l_1) (GRec l_2) (GRec l_3, GRec l_4)
| I_rero : forall l_1 l_2 l_3 l_4,
FAI Rec Row l_1 l_2 (l_3, l_4) ->
I (GRec l_1) (GRow l_2) (GRec l_3, GRec l_4)
| I_rore : forall l_1 l_2 l_3 l_4,
FAI Row Rec l_1 l_2 (l_3, l_4) ->
I (GRow l_1) (GRec l_2) (GRow l_3, GRec l_4)
| I_roro : forall l_1 l_2 l_3 l_4,
FAI Row Row l_1 l_2 (l_3, l_4) ->
I (GRow l_1) (GRow l_2) (GRow l_3, GRow l_4)
with I_m : GType_mapping -> GType_mapping -> (GType_mapping * GType_mapping) % type -> Prop :=
| Im_mis : forall M,
I_m M m_missing (M, m_missing)
| Im_rr : forall S_1 S_2 S'_1 S'_2,
I S_1 S_2 (S'_1, S'_2) ->
I_m (m_map Req S_1) (m_map Req S_2) (m_map Req S'_1, m_map Req S'_2)
| Im_xo_def : forall a S_1 S_2 S'_1 S'_2,
I S_1 S_2 (S'_1, S'_2) ->
I_m (m_map a S_1) (m_map Opt S_2) (m_map a S_1, m_map Opt S'_2)
| Im_xo_undef : forall a S_1 S_2,
(* (forall ev, ~ I S_1 S_2 ev) strictly positive restriction impedes this, but would be sufficient
for the proof to just have the case and deal with it! *)
I_m (m_map a S_1) (m_map Opt S_2) (m_map a S_1, m_missing)
with FAI : GAnn -> GAnn -> list GType_mapping -> list GType_mapping -> (list GType_mapping * list GType_mapping) -> Prop :=
| FAI_nil : forall d_1 d_2,
FAI d_1 d_2 [] [] ([], [])
| FAI_cons : forall d_1 d_2 hd_1 hd_2 tl_1 tl_2 hd'_1 hd'_2 tl'_1 tl'_2,
I_m hd_1 hd_2 (hd'_1, hd'_2) ->
FAI d_1 d_2 tl_1 tl_2 (tl'_1, tl'_2) ->
FAI d_1 d_2 (hd_1 :: tl_1) (hd_2 :: tl_2) (hd'_1 :: tl'_1, hd'_2 :: tl'_2)
| FAI_nil_l : forall d_1 d_2 hd_2 tl_2 hd'_1 hd'_2 tl'_1 tl'_2,
I_m (D d_1) hd_2 (hd'_1, hd'_2) ->
FAI d_1 d_2 [] tl_2 (tl'_1, tl'_2) ->
FAI d_1 d_2 [] (hd_2 :: tl_2) (hd'_1 :: tl'_1, hd'_2 :: tl'_2)
| FAI_nil_r : forall d_1 d_2 hd_1 tl_1 hd'_1 hd'_2 tl'_1 tl'_2,
I_m hd_1 (D d_2) (hd'_1, hd'_2) ->
FAI d_1 d_2 tl_1 [] (tl'_1, tl'_2) ->
FAI d_1 d_2 (hd_1 :: tl_1) [] (hd'_1 :: tl'_1, hd'_2 :: tl'_2)
.
Scheme I_good_ind := Induction for I Sort Prop
with I_m_good_ind := Induction for I_m Sort Prop
with FAI_good_ind := Induction for FAI Sort Prop.
Lemma I_bound : forall S_1 S_2 ev,
I S_1 S_2 ev ->
height_ev ev <= Nat.max (height_GT S_1) (height_GT S_2).
Proof with eauto with math.
intros S_1 S_2 ev H.
eapply I_good_ind with (g := S_1) (g0 := S_2) (e := ev)
(P0 := fun m m0 p H =>
Nat.max (height_GT_map (fst p))
(height_GT_map (snd p)) <=
Nat.max (height_GT_map m)
(height_GT_map m0))
(P1 := fun d_1 d_2 l_1 l_2 ev f =>
Nat.max (S (fold_right Nat.max 0 (map height_GT_map (fst ev))))
(S (fold_right Nat.max 0 (map height_GT_map (snd ev)))) <=
Nat.max (S (fold_right Nat.max 0 (map height_GT_map l_1)))
(S (fold_right Nat.max 0 (map height_GT_map l_2))))
...
- simpl in *...
- intros.
unfold height_ev in *; simpl.
simpl in *.
eauto with math.
- simpl in *...
- simpl in *...
- destruct a; intros; simpl in *...
all: unfold height_ev in *; simpl.
all: simpl in *...
- destruct a; intros; simpl in *...
all: unfold height_ev in *; simpl.
all: simpl in *...
- intros; simpl in *...
- intros; destruct d_1; simpl in *...
- intros; destruct d_2; simpl in *...
Qed.
Lemma filter_error_length {A :Type}: forall l l0,
filter_error l = Some l0 ->
length l = @length A l0.
Proof with eauto with math.
induction l; simpl; intros...
all: inversion H...
destruct a...
destruct (filter_error l) eqn:?; try congruence.
inversion H; subst; simpl...
Qed.
Lemma domain_treelike_le : forall hd tl,
Nat.max (S (length tl)) (fold_right Nat.max 0 (map domain_mapping (hd :: tl)))
<= Nat.max (domain_mapping hd) ( S (Nat.max (length tl) (fold_right Nat.max 0 (map domain_mapping tl)))).
Proof with eauto with math.
intros.
simpl.
generalize dependent hd.
induction tl; simpl; intros...
Qed.
Lemma length_zip_fill{A B C : Type}: forall f g h l l0,
length (@zip_fill A B C f g h l l0) = Nat.max (length l) (length l0).
Proof with eauto with math.
induction l; induction l0; simpl in *...
specialize (IHl []); simpl in *...
specialize (IHl l0); simpl in *...
Qed.
Lemma meet_domain_bound : forall S_1 S_2 S_3,
g_meet S_1 S_2 = Some S_3 ->
domain S_3 <= Nat.max (domain S_1) (domain S_2).
Proof with eauto with math.
intros.
generalize dependent S_2.
generalize dependent S_1.
eapply GType_good_ind_2 with (s := S_3)
(P0 := fun l =>
forall l0 l1 a0 a1,
match filter_error (zip_fill (g_meet_mapping_d a0) (g_meet_mapping_d a1) g_meet_mapping l0 l1) with
| Some l2 => Some (match a0, a1 with
| Row, Row => GRow
| _, _ => GRec
end l2)
| None => None
end = Some (match a0, a1 with
| Row, Row => GRow
| _, _ => GRec
end l) ->
Nat.max (length l) (fold_right Nat.max 0 (map domain_mapping l)) <=
Nat.max (Nat.max (length l0) (fold_right Nat.max 0 (map domain_mapping l0)))
(Nat.max (length l1) (fold_right Nat.max 0 (map domain_mapping l1))))
.
all: try solve [destruct S_1; destruct S_2; simpl; intros; try congruence; inversion H; eauto with math].
- intros.
simpl.
destruct S_0; destruct S_4; simpl in *; try congruence; eauto with math.
all: repeat match goal with
| [ H : match ?A with
| Some _ => _
| None => _
end = _ |- _] =>
destruct A eqn:?; try congruence
end.
inversion H1; subst.
apply H in Heqo.
apply H0 in Heqo0...
- intros l H.
simpl.
destruct S_1; destruct S_2; simpl; try congruence.
all: try eapply H...
(* The rest are contradictions! *)
all: repeat match goal with
| [ |- context[match ?A with
| Some _ => _
| None => _
end]] =>
destruct A eqn:?; try congruence
end.
- intros l H.
simpl.
destruct S_1; destruct S_2; simpl; try congruence.
all: try eapply H...
(* The rest are contradictions! *)
all: repeat match goal with
| [ |- context[match ?A with
| Some _ => _
| None => _
end]] =>
destruct A eqn:?; try congruence
end.
- intros.
simpl...
- intros.
all: destruct l0;[remember [] as l0 |]; (destruct l1; [remember [] as l1 |]); try congruence.
(* We'll do cases separately as zip_fill behaves differently *)
+ subst; simpl in *...
destruct a0; destruct a1; simpl in *; try congruence.
+ subst; simpl in *...
all: repeat match goal with
| [ H : match ?A with
| Some _ => _
| None => _
end = _ |- _ ] =>
destruct A eqn:?; try congruence
end.
inversion Heqo; subst.
pose proof (length_zip_fill (g_meet_mapping_d a0) (g_meet_mapping_d a1) g_meet_mapping [] l1).
pose proof (filter_error_length _ _ Heqo1).
specialize (H [] l1 a0 a1).
simpl in H.
rewrite Heqo1 in H.
destruct a1; destruct a0; simpl in H0; inversion H0; subst.
all: specialize (H eq_refl); simpl in *...
+ subst; simpl in *...
all: repeat match goal with
| [ H : match ?A with
| Some _ => _
| None => _
end = _ |- _ ] =>
destruct A eqn:?; try congruence
end.
inversion Heqo; subst.
pose proof (length_zip_fill (g_meet_mapping_d a0) (g_meet_mapping_d a1) g_meet_mapping l0 []).
pose proof (filter_error_length _ _ Heqo1).
specialize (H l0 [] a0 a1).
simpl in H.
rewrite Heqo1 in H.
destruct a1; destruct a0; simpl in H0; inversion H0; subst.
all: specialize (H eq_refl); simpl in *...
+ subst; simpl in *...
all: repeat match goal with
| [ H : match ?A with
| Some _ => _
| None => _
end = _ |- _ ] =>
destruct A eqn:?; try congruence
end.
inversion Heqo; subst.
pose proof (length_zip_fill (g_meet_mapping_d a0) (g_meet_mapping_d a1) g_meet_mapping l0 l1).
pose proof (filter_error_length _ _ Heqo1).
specialize (H l0 l1 a0 a1).
simpl in H.
rewrite Heqo1 in H.
destruct a1; destruct a0; simpl in H0; inversion H0; subst.
all: specialize (H eq_refl); simpl in *...
- intros.
all: destruct l0;[remember [] as l0 |]; (destruct l1; [remember [] as l1 |]); try congruence.
(* We'll do cases separately as zip_fill behaves differently *)
+ subst; simpl in *...
destruct a0; destruct a1; simpl in *; try congruence.
+ subst; simpl in *...
all: repeat match goal with
| [ H : match ?A with
| Some _ => _
| None => _
end = _ |- _ ] =>
destruct A eqn:?; try congruence
end.
inversion Heqo; subst.
pose proof (length_zip_fill (g_meet_mapping_d a0) (g_meet_mapping_d a1) g_meet_mapping [] l1).
pose proof (filter_error_length _ _ Heqo1).
specialize (H0 [] l1 a0 a1).
simpl in H0.
rewrite Heqo1 in H0.
destruct a1; destruct a0; simpl in H1; inversion H1; subst.
all: specialize (H0 eq_refl); simpl in *...