-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFCL.v
More file actions
5442 lines (5153 loc) · 225 KB
/
Copy pathFCL.v
File metadata and controls
5442 lines (5153 loc) · 225 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Import PeanoNat.
Require Import Coq.Relations.Relation_Operators.
Require Import Coq.Arith.Wf_nat.
Require Import Coq.Wellfounded.Inverse_Image.
Require Import Coq.Wellfounded.Lexicographic_Product.
From mathcomp Require Import all_ssreflect.
Require Import PreOrders.
Require Import Types.
Require Import Cover.
Set Bullet Behavior "Strict Subproofs".
Delimit Scope it_scope with IT.
Open Scope it_scope.
Import EqNotations.
Reserved Notation "[ 'FCL' Gamma |- M : A ]" (at level 0, M at level 50).
Reserved Notation "M @ N" (at level 50, left associativity).
Definition context_of (combT: finType) (ctorT: ctor) (_: phantom Type (Finite.sort combT)) (_: phantom Type (PreOrdered.sort ctorT)) :=
{ffun combT -> @IT ctorT}.
Notation Ctxt Combinator Constructor :=
(context_of _ _ (Phantom Type Combinator) (Phantom Type Constructor)).
Section FCL.
Variable Combinator: finType.
Variable Constructor: ctor.
Inductive Term : Type :=
| Var: Combinator -> Term
| App: Term -> Term -> Term.
Notation "M @ N" := (App M N) : it_scope.
Inductive FCL (Gamma: Ctxt Combinator Constructor): Term -> @IT Constructor -> Prop :=
| FCL__Var: forall c, [FCL Gamma |- Var c : Gamma c ]
| FCL__MP: forall M N A B, [FCL Gamma |- M : A -> B ] -> [FCL Gamma |- N : A] -> [FCL Gamma |- M @ N : B ]
| FCL__Sub: forall M A B, [FCL Gamma |- M : A] -> [bcd A <= B] -> [FCL Gamma |- M : B]
where "[ 'FCL' Gamma |- M : A ]" := (FCL Gamma M A) : it_scope.
(** Enable mathcomp functionalities on terms **)
Section TermMathcompInstances.
Fixpoint Term2tree (M: Term):
GenTree.tree Combinator :=
match M with
| Var c => GenTree.Node 0 [:: GenTree.Leaf c]
| M @ N => GenTree.Node 1 [:: Term2tree M; Term2tree N]
end.
Fixpoint tree2Term (t: GenTree.tree Combinator): option Term :=
match t with
| GenTree.Node n args =>
match n, args with
| 0, [:: GenTree.Leaf c] => Some (Var c)
| 1, [:: t1; t2] =>
if tree2Term t1 is Some M then
if tree2Term t2 is Some N
then Some (M @ N)
else None
else None
| _, _ => None
end
| _ => None
end.
Lemma pcan_Termtree: pcancel Term2tree tree2Term.
Proof. by elim => //= ? -> ? ->. Qed.
Definition Term_eqMixin := PcanEqMixin pcan_Termtree.
Canonical Term_eqType := EqType Term Term_eqMixin.
Definition Term_choiceMixin := PcanChoiceMixin pcan_Termtree.
Canonical Term_choiceType := ChoiceType Term Term_choiceMixin.
Definition Term_countMixin := PcanCountMixin pcan_Termtree.
Canonical Term_countType := CountType Term Term_countMixin.
End TermMathcompInstances.
Definition FCL_inv {Gamma} {M} {B} (prf: [FCL Gamma |- M : B]) :=
fun (X : Ctxt Combinator Constructor -> Term -> @IT Constructor -> Prop) =>
let diag M B :=
match M return Prop with
| Var c =>
(X Gamma (Var c) (Gamma c) ->
(forall A, [FCL Gamma |- Var c : A] -> [bcd A <= B] -> X Gamma (Var c) B) ->
X Gamma M B)%type
| M @ N =>
((forall A, [FCL Gamma |- M : (A -> B)%IT] -> [FCL Gamma |- N : A] -> X Gamma (M @ N) B) ->
(forall A, [FCL Gamma |- M @ N : A] -> [bcd A <= B] -> X Gamma (M @ N) B) ->
X Gamma (M @ N) B)%type
end in
match prf in [FCL _ |- M : B] return diag M B with
| FCL__Var c => fun kv _ => kv
| FCL__MP M N A B prf1 prf2 => fun kmp _ => kmp A prf1 prf2
| FCL__Sub (Var c) A B prf1 prf2 => fun _ ksub => ksub A prf1 prf2
| FCL__Sub (M @ N) A B prf1 prf2 => fun _ ksub => ksub A prf1 prf2
end.
Definition revApply M Ns : Term := foldr (fun N M => M @ N) M Ns.
End FCL.
Arguments Term [Combinator].
Arguments Var [Combinator].
Arguments App [Combinator].
Hint Constructors Term.
Arguments FCL [Combinator Constructor].
Arguments FCL__Var [Combinator Constructor] [Gamma c].
Arguments FCL__MP [Combinator Constructor] [Gamma M N] A [B].
Arguments FCL__Sub [Combinator Constructor] [Gamma M] A [B].
Hint Constructors FCL.
Arguments revApply [Combinator].
Notation "M @ N" := (App M N) : it_scope.
Notation "[ 'FCL' Gamma |- M : A ]" := (FCL Gamma M A) : it_scope.
Section FCLProperties.
Variable Combinator: finType.
Variable Constructor: ctor.
Implicit Type c: Combinator.
Implicit Type A B C: @IT Constructor.
Implicit Type Gamma: @Ctxt Combinator Constructor.
Lemma FCL_Var_le: forall Gamma c A, [FCL Gamma |- (Var c) : A] -> [bcd (Gamma c) <= A].
Proof.
move => Gamma c A.
move M__eq: (Var c) => M prf.
move: M__eq.
elim: M A / prf.
- by move => ? [] ->.
- by discriminate.
- move => M A B prf IH le_prf M__eq.
by apply: BCD__Trans; first by apply: IH.
Qed.
Lemma FCL_MP_inv: forall Gamma M N B, [FCL Gamma |- M @ N : B] -> exists A, [FCL Gamma |- M : A -> B] /\ [FCL Gamma |- N : A].
Proof.
move => Gamma M N B.
move MN__eq: (M @ N) => MN prf.
move: MN__eq.
elim: MN B / prf => //.
- move => M__tmp N__tmp A B prf1 _ prf2 _ [] -> ->.
by (exists A); split.
- move => [] // M__tmp N__tmp B1 B2 _ IH le_prf MN__eq.
case: (IH MN__eq) => A [] prf1 prf2.
exists A; split => //.
apply: (FCL__Sub (A -> B1)) => //.
by apply: BCD__Sub.
Qed.
Lemma FCL_normalized_ind:
forall Gamma (P : Term -> @IT Constructor -> Prop),
(forall c, P (Var c) (Gamma c)) ->
(forall c A, P (Var c) (Gamma c) -> [bcd (Gamma c) <= A] -> P (Var c) A) ->
(forall M N A B, [FCL Gamma |- M : (A -> B)] -> P M (A -> B) -> [FCL Gamma |- N : A] -> P N A -> P (M @ N) B) ->
forall M A, [FCL Gamma |- M : A] -> P M A.
Proof.
move => Gamma P IH__Var IH__Sub IH__MP.
elim.
- move => c A /FCL_Var_le.
apply: IH__Sub.
by apply: IH__Var.
- move => M IH__M N IH__N B /FCL_MP_inv [] A [] prf1 prf2.
by apply: (IH__MP M N A B prf1 (IH__M (A -> B) prf1) prf2 (IH__N A prf2)).
Qed.
Lemma FCL__II:
forall Gamma M A B, [FCL Gamma |- M : A] -> [FCL Gamma |- M : B] -> [FCL Gamma |- M : A \cap B].
Proof.
move => Gamma.
elim.
- move => c A B /FCL_Var_le prf1 /FCL_Var_le prf2.
apply: (FCL__Sub (Gamma c)) => //.
by apply: BCD__Glb.
- move => M IH__M N IH__N B1 B2 /FCL_MP_inv [] A1 [] prf1__M prf1__N /FCL_MP_inv [] A2 [] prf2__M prf2__N.
apply: (FCL__MP (A1 \cap A2)).
+ apply: (FCL__Sub ((A1 -> B1) \cap (A2 -> B2))).
* by apply: IH__M.
* by apply: bcd__Arr.
+ by apply: IH__N.
Qed.
Lemma FCL__Omega: forall Gamma M, [FCL Gamma |- M : Omega].
Proof.
move => Gamma.
elim.
- move => c.
by apply: FCL__Sub.
- move => M IH__M N IH__N.
apply: (FCL__MP Omega) => //.
by apply: (FCL__Sub Omega).
Qed.
Lemma FCL__weaken: forall Gamma1 Gamma2 M A,
(forall c, [bcd (Gamma2 c) <= (Gamma1 c)]) ->
[FCL Gamma1 |- M : A] -> [FCL Gamma2 |- M : A].
Proof.
move => Gamma1 Gamma2 M A weaken prf.
elim /FCL_normalized_ind: M A / prf.
- move => c.
by apply: FCL__Sub; last by apply weaken.
- move => c A _ /(BCD__Trans _ (weaken c)) res.
by apply: FCL__Sub; last by exact res.
- by move => ? ? A *; apply: (FCL__MP A).
Qed.
Lemma FCL__App: forall Gamma M Ns m,
seq.size Ns = seq.size m.1 ->
(forall n, [FCL Gamma |- (nth M Ns n) : nth (mkArrow m) m.1 n]) ->
[FCL Gamma |- revApply M Ns : m.2].
Proof.
move => Gamma M Ns.
move: M.
elim: Ns.
- move => M [] srcs tgt /=.
move => /(fun prf => @Logic.eq_sym _ _ _ prf) /size0nil -> prf.
by apply: (prf 0).
- move => N Ns IH M [] [] // src srcs tgt /= [] size__eq prfs.
rewrite /revApply /= -/(revApply M Ns).
apply: (FCL__MP src).
+ apply: (IH M (srcs, src -> tgt)) => //.
move => n.
by exact (prfs n.+1).
+ by exact (prfs 0).
Qed.
Lemma FCL__invApp: forall Gamma M Ns tgt,
[FCL Gamma |- revApply M Ns : tgt] ->
exists srcs,
seq.size Ns = seq.size srcs /\
(forall n, [FCL Gamma |- (nth M Ns n) : nth (mkArrow (srcs, tgt)) srcs n]).
Proof.
move => Gamma M Ns.
move: M.
elim: Ns.
- move => M tgt /= prf.
exists [::]; split => //.
move => n.
by rewrite nth_default //= nth_default.
- move => N Ns IH M tgt /FCL_MP_inv [] src [].
move => /(IH M (src -> tgt)) [] srcs [] size__eq prf1 prf2.
exists [:: src & srcs]; split.
+ by rewrite /= size__eq.
+ by case.
Qed.
Fixpoint unapply (M: Term) : (Combinator * seq Term) :=
match M with
| Var c => (c, [::])
| M @ N => let (c, Ns) := unapply M in (c, [:: N & Ns])
end.
Lemma revapply_unapply: cancel (fun cNs => revApply (Var cNs.1) cNs.2) unapply.
Proof. move => [] ?; by elim => //= N Ns ->. Qed.
Lemma unapply_revapply: cancel unapply (fun cNs => revApply (Var cNs.1) cNs.2).
Proof. by (elim => //= M; case: (unapply M) => c Ns /= ->). Qed.
Definition minimalArrowTgt (A B: @IT Constructor): @IT Constructor :=
\bigcap_(A_i <- if (subtype_machine _ [tgt_for_srcs_gte B in
cast (Omega -> Omega \times Omega) A])
is exist [check_tgt Delta] _ then Delta else [::]) A_i.
Fixpoint minimalType (Gamma: Ctxt Combinator Constructor) (M: Term): @IT Constructor :=
match M with
| Var c => Gamma c
| M @ N => minimalArrowTgt (minimalType Gamma M) (minimalType Gamma N)
end.
Lemma minimalArrowType_le: forall A B, [bcd A <= B -> minimalArrowTgt A B].
Proof.
move => A B.
apply /subty__sound.
rewrite /minimalArrowTgt.
move: (subtype_machine _ [tgt_for_srcs_gte B in cast (B -> minimalArrowTgt A B) A]) => [].
case.
- move => b /SubtypeMachine_inv.
by case: (cast (B -> minimalArrowTgt A B) A).
- move => Delta prf1.
move: (subtype_machine _ [subty \bigcap_(A_i <- Delta) A_i of minimalArrowTgt A B]) => [].
case; last first.
+ move => ? /SubtypeMachine_inv.
by case: (minimalArrowTgt A B).
+ move => r prf2.
case isOmega__tgt: (isOmega (minimalArrowTgt A B)).
* rewrite -(orTb r) -isOmega__tgt /minimalArrowTgt.
by apply: step__Arr; first by exact prf1.
* have cast__eq: (cast (Omega -> Omega \times Omega) A) =
(cast (B -> minimalArrowTgt A B) A).
{ move: isOmega__tgt.
clear...
move: (minimalArrowTgt A B) => C.
move => isOmega__C.
elim: A; by rewrite /cast /= isOmega__C. }
rewrite -(orbT false) -isOmega__tgt.
apply: (step__Arr (Delta := Delta) (r := true)).
** move: prf1.
by rewrite -cast__eq.
** move: prf1.
rewrite -cast__eq.
move => prf1.
move: (subtype_machine Constructor [ tgt_for_srcs_gte B in cast (Omega -> Omega \times Omega) A]) => [] res1 res2.
move: (Types.Semantics_functional _ _ _ _ prf1 res2) => <-.
by apply: subty_complete.
Qed.
Lemma minimalType_sound: forall Gamma M, FCL Gamma M (minimalType Gamma M).
Proof.
move => Gamma.
elim => //= M IH1 N IH2.
apply: FCL__MP; last by exact IH2.
apply: FCL__Sub; first by exact IH1.
by apply: minimalArrowType_le.
Qed.
Lemma minimalArrowType_minimal: forall A B C,
[bcd A <= B -> C] -> [bcd (minimalArrowTgt A B) <= C].
Proof.
move => A B C /subty_complete /SubtypeMachine_inv.
move => /(fun prf => prf (fun i r =>
match i, r with
| [subty A of B -> C], Return true => [bcd (minimalArrowTgt A B) <= C]
| _, _ => True
end)) res.
apply: res.
case isOmega__C: (isOmega C).
- move => /= *.
apply /subty__sound.
by apply: subty__Omega.
- move => Delta [] //= prf1 prf2.
apply /subty__sound.
have cast__eq: (cast (Omega -> Omega \times Omega) A = cast (B -> C) A).
{ move: isOmega__C.
clear...
move => isOmega__C.
by elim: A; rewrite /cast /= isOmega__C. }
rewrite /minimalArrowTgt cast__eq.
move: (subtype_machine Constructor [ tgt_for_srcs_gte B in cast (B -> C) A]) => [] res1 res2.
by move: (Types.Semantics_functional _ _ _ _ prf1 res2) => <-.
Qed.
Lemma minimalType_minimal: forall Gamma M A, FCL Gamma M A -> [bcd (minimalType Gamma M) <= A].
Proof.
move => Gamma M A prf.
elim /FCL_normalized_ind: M A / prf => //= M N A B _ IH1 _ IH2.
apply: minimalArrowType_minimal.
apply: BCD__Trans; first by exact IH1.
by apply: BCD__Sub.
Qed.
Definition typeCheck Gamma (M: Term) (A: IT): bool :=
checkSubtypes (minimalType Gamma M) A.
Lemma fclP : forall Gamma M A, reflect [FCL Gamma |- M : A] (typeCheck Gamma M A).
Proof.
move => Gamma M A.
case checks: (typeCheck Gamma M A).
- constructor.
apply: FCL__Sub; first by apply: minimalType_sound.
by apply /subtypeMachineP.
- constructor.
move => prf.
move: (minimalType_minimal _ _ _ prf) => /subtypeMachineP.
move: checks.
rewrite /typeCheck.
by move => ->.
Qed.
End FCLProperties.
Arguments FCL_Var_le [Combinator Constructor].
Arguments FCL_MP_inv [Combinator Constructor].
Arguments FCL_normalized_ind [Combinator Constructor].
Arguments FCL__II [Combinator Constructor].
Arguments FCL__Omega [Combinator Constructor].
Arguments FCL__App [Combinator Constructor].
Arguments FCL__invApp [Combinator Constructor].
Arguments unapply [Combinator].
Arguments revapply_unapply [Combinator].
Arguments unapply_revapply [Combinator].
Arguments minimalType [Combinator Constructor].
Arguments minimalArrowTgt [Constructor].
Arguments minimalArrowType_le [Constructor].
Arguments minimalType_sound [Combinator Constructor].
Arguments minimalArrowType_minimal [Constructor].
Arguments minimalType_minimal [Combinator Constructor].
Arguments typeCheck [Combinator Constructor].
Section ConstructorSum.
Variable Constructor1 Constructor2: ctor.
Definition ctorSum_countType: countType :=
sum_countType Constructor1 Constructor2.
Definition leq_sum (l r: ctorSum_countType): bool :=
match l, r with
| inl a, inl b => [ctor a <= b]
| inr a, inr b => [ctor a <= b]
| _, _ => false
end.
Lemma leq_sum_refl: PreOrdered.preorder_reflexive ctorSum_countType leq_sum.
Proof.
move => [] c; by apply: preorder_reflexive.
Qed.
Lemma leq_sum_trans: PreOrdered.preorder_transitive ctorSum_countType leq_sum.
Proof.
move => [] c1 [] c2 [] c3 //=.
- by apply: preorder_transitive.
- by rewrite andbF.
- by rewrite andbF.
- by apply: preorder_transitive.
Qed.
Definition sum_preOrderedMixin :=
PreOrdered.Mixin ctorSum_countType
leq_sum leq_sum_refl leq_sum_trans.
Canonical sum_preOrderedType := Eval hnf in PreOrderedType ctorSum_countType sum_preOrderedMixin.
End ConstructorSum.
Module SplitTypeUniverse.
Definition classify (Constructor: ctor): Type := (@IT Constructor -> bool)%type.
Definition dist_arr_partition
(Constructor: ctor)
(inPartition : classify Constructor): Type :=
forall A B, inPartition (A -> B) = inPartition A && inPartition B.
Definition dist_inter_partition
(Constructor: ctor)
(inPartition : classify Constructor): Type :=
forall A B, inPartition (A \cap B) = inPartition A && inPartition B.
Definition omega_partition
(Constructor: ctor)
(inPartition: classify Constructor): Type :=
inPartition Omega.
Definition st_irrel_partition
(Constructor: ctor)
(inPartition1 : classify Constructor)
(inPartition2 : classify Constructor): Type :=
forall A B C, inPartition1 A -> inPartition2 B -> inPartition1 C -> [bcd (A \cap B) <= C] -> [bcd A <= C].
Record mixin_of (Constructor: ctor): Type :=
Mixin {
inPartition1 : classify Constructor;
inPartition2 : classify Constructor;
_: dist_arr_partition Constructor inPartition1;
_: dist_arr_partition Constructor inPartition2;
_: dist_inter_partition Constructor inPartition1;
_: dist_inter_partition Constructor inPartition2;
_: omega_partition Constructor inPartition1;
_: omega_partition Constructor inPartition2;
_: st_irrel_partition Constructor inPartition1 inPartition2;
_: st_irrel_partition Constructor inPartition2 inPartition1
}.
Section ClassDef.
Record class_of (C: Type) :=
Class {
base: PreOrdered.class_of C;
mixin: mixin_of (PreOrdered.Pack C base)
}.
Local Coercion base : class_of >-> PreOrdered.class_of.
Structure type := Pack { sort : Type; _ : class_of sort }.
Local Coercion sort : type >-> Sortclass.
Variables (T: Type) (splitUniverse: type).
Definition class := let: Pack _ c as splitUniverse' := splitUniverse return class_of splitUniverse' in c.
Definition clone c of phant_id class c := @Pack T c.
Let xT := let: Pack T _ := splitUniverse in T.
Notation xclass := (class : class_of xT).
Definition pack b0 (m0: mixin_of (PreOrdered.Pack T b0)) :=
fun bT b & phant_id (PreOrdered.class bT) b =>
fun m & phant_id m0 m => Pack T (@Class T b m).
Definition eqType := Eval hnf in @Equality.Pack splitUniverse xclass.
Definition choiceType := Eval hnf in @Choice.Pack splitUniverse xclass.
Definition countType := Eval hnf in @Countable.Pack splitUniverse xclass.
Definition preOrdered := Eval hnf in @PreOrdered.Pack splitUniverse xclass.
End ClassDef.
Module Import Exports.
Coercion base : class_of >-> PreOrdered.class_of.
Coercion mixin: class_of >-> mixin_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion countType : type >-> Countable.type.
Canonical countType.
Coercion preOrdered : type >-> PreOrdered.type.
Canonical preOrdered.
Notation splitTypeUniverse := type.
Notation SplitTypeUniverseMixin := Mixin.
Notation SplitTypeUniverseType C m := (@pack C _ m _ _ id _ id).
Notation "[ 'splitTypeUniverse' 'of' T 'for' C ]" :=
(@clone splitTypeUniverse C _ idfun id) (at level 0, format "[ 'splitTypeUniverse' 'of' T 'for' C ]") : form_scope.
Notation "[ 'splitTypeUniverse' 'of' C ]" :=
(@clone splitTypeUniverse C _ id id) (at level 0, format "[ 'splitTypeUniverse' 'of' C ]") : form_scope.
End Exports.
End SplitTypeUniverse.
Export SplitTypeUniverse.Exports.
Definition inPartition1 (U: splitTypeUniverse) := @SplitTypeUniverse.inPartition1 _ (SplitTypeUniverse.class U).
Arguments inPartition1 [U].
Definition inPartition2 (U: splitTypeUniverse) := @SplitTypeUniverse.inPartition2 _ (SplitTypeUniverse.class U).
Arguments inPartition2 [U].
Lemma dist_arr_partition1 U: SplitTypeUniverse.dist_arr_partition _ (@inPartition1 U).
Proof. by case U => ? [] ? []. Qed.
Arguments dist_arr_partition1 [U].
Lemma dist_arr_partition2 U: SplitTypeUniverse.dist_arr_partition _ (@inPartition2 U).
Proof. by case U => ? [] ? []. Qed.
Arguments dist_arr_partition2 [U].
Lemma dist_inter_partition1 U: SplitTypeUniverse.dist_inter_partition _ (@inPartition1 U).
Proof. by case U => ? [] ? []. Qed.
Arguments dist_inter_partition1 [U].
Lemma dist_inter_partition2 U: SplitTypeUniverse.dist_inter_partition _ (@inPartition2 U).
Proof. by case U => ? [] ? []. Qed.
Arguments dist_inter_partition2 [U].
Lemma omega_partition1 U: SplitTypeUniverse.omega_partition _ (@inPartition1 U).
Proof. by case U => ? [] ? []. Qed.
Arguments omega_partition1 [U].
Lemma omega_partition2 U: SplitTypeUniverse.omega_partition _ (@inPartition2 U).
Proof. by case U => ? [] ? []. Qed.
Arguments omega_partition2 [U].
Lemma st_irrel_partition1 U: SplitTypeUniverse.st_irrel_partition _ (@inPartition1 U) (@inPartition2 U).
Proof. by case U => ? [] ? []. Qed.
Arguments st_irrel_partition1 [U].
Lemma st_irrel_partition2 U: SplitTypeUniverse.st_irrel_partition _ (@inPartition2 U) (@inPartition1 U).
Proof. by case U => ? [] ? []. Qed.
Arguments st_irrel_partition2 [U].
Module SplitContextPair.
Definition pure_context
(Combinator: finType)
(Constructor: ctor)
(Gamma: Ctxt Combinator Constructor)
(inPartition: SplitTypeUniverse.classify Constructor): Type :=
forall c, inPartition (Gamma c).
Record mixin_of (Combinator: finType) (U: splitTypeUniverse): Type :=
Mixin {
ctxt1: Ctxt Combinator U;
ctxt2: Ctxt Combinator U;
_: pure_context _ _ ctxt1 (@inPartition1 _);
_: pure_context _ _ ctxt2 (@inPartition2 _)
}.
Section ClassDef.
Structure class_of (Combinator: Type) (Constructor: Type) :=
Class {
combinator_base: Finite.class_of Combinator;
universe_base: SplitTypeUniverse.class_of Constructor;
mixin: mixin_of (@Finite.Pack Combinator combinator_base)
(SplitTypeUniverse.Pack Constructor universe_base)
}.
Local Coercion combinator_base : class_of >-> Finite.class_of.
Local Coercion universe_base : class_of >-> SplitTypeUniverse.class_of.
Structure type := Pack { combinator_sort : Type; constructor_sort: Type; _ : class_of combinator_sort constructor_sort }.
Variables (Combinator: Type) (Constructor: Type) (splitCtxts: type).
Definition class := let: Pack _ _ c as splitCtor' := splitCtxts return class_of (combinator_sort splitCtor')
(constructor_sort splitCtor')
in c.
Definition clone c of phant_id class c := @Pack Combinator Constructor c.
Let xCombinator := (let: Pack Combinator _ _ := splitCtxts in Combinator).
Let xConstructor := (let: Pack _ Constructor _ := splitCtxts in Constructor).
Notation xclass := (class : class_of xCombinator xConstructor).
Definition pack b0 b1 (m0: mixin_of (@Finite.Pack Combinator b0) (SplitTypeUniverse.Pack Constructor b1)) :=
fun bCombinator bcomb & phant_id (Finite.class bCombinator) bcomb =>
fun bConstructor bctor & phant_id (SplitTypeUniverse.class bConstructor) bctor =>
fun m & phant_id m0 m => Pack Combinator Constructor (@Class Combinator Constructor bcomb bctor m).
Definition combEqType := Eval hnf in @Equality.Pack xCombinator xclass.
Definition combChoiceType := Eval hnf in @Choice.Pack xCombinator xclass.
Definition combCountType := Eval hnf in @Countable.Pack xCombinator xclass.
Definition finType := Eval hnf in @Finite.Pack xCombinator xclass.
Definition ctorEqType := Eval hnf in @Equality.Pack xConstructor (SplitTypeUniverse.base _ xclass).
Definition ctorCountType := Eval hnf in @Countable.Pack xConstructor (SplitTypeUniverse.base _ xclass).
Definition preOrdered := Eval hnf in @PreOrdered.Pack xConstructor xclass.
Definition splitTypeUniverse := (SplitTypeUniverse.Pack xConstructor xclass).
End ClassDef.
Module Import Exports.
Coercion universe_base : class_of >-> SplitTypeUniverse.class_of.
Coercion mixin: class_of >-> mixin_of.
Coercion preOrdered : type >-> PreOrdered.type.
Canonical preOrdered.
Coercion splitTypeUniverse: type >-> SplitTypeUniverse.type.
Canonical splitTypeUniverse.
Coercion finType: type >-> Finite.type.
Canonical finType.
Notation splitCtxtPair := type.
Notation SplitCtxtPairMixin := Mixin.
Notation SplitCtxtPairType Combinator Constructor m := (@pack Combinator Constructor _ _ m _ _ id _ _ id m id).
Notation "[ 'splitCtxtPair' 'of' Combinator 'and' Constructor 'for' splitCtxts ]" :=
(@clone Combinator Constructor splitCtxts _ idfun)
(at level 0, format "[ 'splitCtxtPair' 'of' Combinator 'and' Constructor 'for' splitCtxts ]") : form_scope.
Notation "[ 'splitCtxtPair' 'of' Combinator 'and' Constructor ]" :=
(@clone Combinator Constructor _ _ id) (at level 0, format "[ 'splitCtxtPair' 'of' Combinator 'and' Constructor ]") : form_scope.
End Exports.
End SplitContextPair.
Export SplitContextPair.Exports.
Definition ctxt1 (Gammas: splitCtxtPair) :=
@SplitContextPair.ctxt1 _ _ (@SplitContextPair.class Gammas).
Definition ctxt2 (Gammas: splitCtxtPair) :=
@SplitContextPair.ctxt2 _ _ (@SplitContextPair.class Gammas).
Lemma pure_context1:
forall (Gammas: splitCtxtPair),
SplitContextPair.pure_context _ _ (ctxt1 Gammas) (@inPartition1 Gammas).
Proof. by case => [] ? ? [] ? ? []. Qed.
Lemma pure_context2:
forall (Gammas: splitCtxtPair),
SplitContextPair.pure_context _ _ (ctxt2 Gammas) (@inPartition2 Gammas).
Proof. by case => [] ? ? [] ? ? []. Qed.
Fixpoint isArrow {C: ctor} (A: @IT C): bool :=
match A with
| _ -> _ => true
| A \cap B => isArrow A || isArrow B
| _ => false
end.
Module ITHomomorphism.
Definition subtype_hom (Constructor1 Constructor2: ctor) (f: @IT Constructor1 -> @IT Constructor2): Type :=
forall A B, [bcd A <= B] <-> [bcd (f A) <= (f B)].
Definition arrow_hom (Constructor1 Constructor2: ctor) (f: @IT Constructor1 -> @IT Constructor2): Type :=
forall A B, f (A -> B) = (f A -> f B).
Definition inter_hom (Constructor1 Constructor2: ctor) (f: @IT Constructor1 -> @IT Constructor2): Type :=
forall A B, f (A \cap B) = (f A \cap f B).
Definition omega_hom (Constructor1 Constructor2: ctor) (f: @IT Constructor1 -> @IT Constructor2): Type :=
f Omega = Omega.
Definition arrow_preimage (Constructor1 Constructor2: ctor) (f: @IT Constructor1 -> @IT Constructor2): Type :=
forall A, isArrow (f A) -> isArrow A.
Record mixin_of (Domain Range: ctor): Type :=
Mixin {
map_types: @IT Domain -> @IT Range;
_: subtype_hom _ _ map_types;
_: arrow_hom _ _ map_types;
_: inter_hom _ _ map_types;
_: omega_hom _ _ map_types;
_: arrow_preimage _ _ map_types
}.
Section ClassDef.
Structure class_of (Domain Range: Type) :=
Class {
dom_base: PreOrdered.class_of Domain;
rng_base: PreOrdered.class_of Range;
mixin: mixin_of (PreOrdered.Pack Domain dom_base)
(PreOrdered.Pack Range rng_base)
}.
Structure type := Pack { domain_sort : Type; range_sort: Type; _ : class_of domain_sort range_sort }.
Variables (Domain Range: Type) (itHom: type).
Definition class := let: Pack _ _ c as itHom' := itHom return class_of (domain_sort itHom')
(range_sort itHom')
in c.
Definition clone c of phant_id class c := @Pack Domain Range c.
Let xDomain := let: Pack Domain _ _ := itHom in Domain.
Let xRange := let: Pack _ Range _ := itHom in Range.
Notation xclass := (class : class_of xDomain xRange).
Definition pack b0 b1 (m0: mixin_of (@PreOrdered.Pack Domain b0) (@PreOrdered.Pack Range b1)) :=
fun bDomain bdom & phant_id (PreOrdered.class bDomain) bdom =>
fun bRange brng & phant_id (PreOrdered.class bRange) brng =>
fun m & phant_id m0 m => Pack Domain Range (@Class Domain Range bdom brng m).
Definition domain := Eval hnf in @PreOrdered.Pack xDomain (dom_base _ _ xclass).
Definition range := Eval hnf in @PreOrdered.Pack xRange (rng_base _ _ xclass).
End ClassDef.
Module Import Exports.
Coercion class: type >-> class_of.
Coercion mixin: class_of >-> mixin_of.
Coercion map_types: mixin_of >-> Funclass.
Notation itHom := type.
Notation ITHomMixin := Mixin.
Notation ITHomType Domain Range m := (@pack Domain Range _ _ m _ _ id _ _ id _ id).
Notation "[ 'itHom' 'of' Domain '*' Range 'for' hom ]" :=
(@clone Domain Range hom _ idfun)
(at level 0, format "[ 'itHom' 'of' Domain '*' Range 'for' hom ]") : form_scope.
Notation "[ 'itHom' 'of' Domain '*' Range ]" :=
(@clone Domain Range _ _ id) (at level 0, format "[ 'itHom' 'of' Domain '*' Range ]") : form_scope.
End Exports.
End ITHomomorphism.
Export ITHomomorphism.Exports.
Definition domain_base (f: itHom): ctor :=
@PreOrdered.Pack _ (@ITHomomorphism.dom_base _ _ (@ITHomomorphism.class f)).
Definition range_base (f: itHom): ctor :=
@PreOrdered.Pack _ (@ITHomomorphism.rng_base _ _ (@ITHomomorphism.class f)).
Lemma subtype_hom: forall (f: itHom), @ITHomomorphism.subtype_hom _ _ f.
Proof. by move => [] ? ? [] ? ? []. Qed.
Lemma arrow_hom: forall (f: itHom), @ITHomomorphism.arrow_hom _ _ f.
Proof. by move => [] ? ? [] ? ? []. Qed.
Lemma inter_hom: forall (f: itHom), @ITHomomorphism.inter_hom _ _ f.
Proof. by move => [] ? ? [] ? ? []. Qed.
Lemma omega_hom: forall (f: itHom), @ITHomomorphism.omega_hom _ _ f.
Proof. by move => [] ? ? [] ? ? []. Qed.
Lemma arrow_preimage: forall (f: itHom), @ITHomomorphism.arrow_preimage _ _ f.
Proof. by move => [] ? ? [] ? ? []. Qed.
Section ContextCoproduct.
Variables (Constructor1 Constructor2: ctor).
Definition LiftedConstructor: ctor := sum_preOrderedType
(diag_preOrderedType [countType of bool])
(sum_preOrderedType Constructor1 Constructor2).
Fixpoint lift {C: ctor}
(lift_ctor: C -> sum_preOrderedType Constructor1 Constructor2)
(isLeft: bool)
(A: @IT C) {struct A}: @IT LiftedConstructor :=
match A with
| Omega => Omega
| Ctor c A => Ctor (inr (lift_ctor c)) (lift lift_ctor isLeft A)
| Arrow A B => Arrow (lift lift_ctor isLeft A) (lift lift_ctor isLeft B)
| Prod A B => Ctor (inl isLeft) (Prod (lift lift_ctor isLeft A) (lift lift_ctor isLeft B))
| Inter A B => Inter (lift lift_ctor isLeft A) (lift lift_ctor isLeft B)
end.
Definition lift1 := lift inl true.
Definition lift2 := lift inr false.
Lemma lift_arrow_hom {C: ctor}: forall lift_ctor isLeft, ITHomomorphism.arrow_hom C _ (lift lift_ctor isLeft).
Proof. done. Qed.
Lemma lift_inter_hom {C: ctor}: forall lift_ctor isLeft, ITHomomorphism.inter_hom C _ (lift lift_ctor isLeft).
Proof. done. Qed.
Lemma lift_arrow_preimage {C: ctor}: forall lift_ctor isLeft, ITHomomorphism.arrow_preimage C _ (lift lift_ctor isLeft).
Proof.
move => ? ?.
elim => // A1 IH1 A2 IH2 /orP [].
- by move => /IH1 /= ->.
- move => /IH2 /= ->.
by rewrite orbT.
Qed.
Lemma lift_omega_hom {C: ctor}: forall lift_ctor isLeft, ITHomomorphism.omega_hom C _ (lift lift_ctor isLeft).
Proof. done. Qed.
Fixpoint unlift {C: ctor}
(unlift_ctor: sum_preOrderedType Constructor1 Constructor2 -> option C)
(A: @IT LiftedConstructor) {struct A}: option (@IT C) :=
match A with
| Omega => Some Omega
| Ctor (inl _) (Prod A B) =>
obind (fun A => omap (Prod A) (unlift unlift_ctor B))
(unlift unlift_ctor A)
| Ctor (inr c) A =>
obind (fun c => omap (Ctor c) (unlift unlift_ctor A))
(unlift_ctor c)
| Arrow A B =>
obind (fun A => omap (Arrow A) (unlift unlift_ctor B))
(unlift unlift_ctor A)
| Inter A B =>
obind (fun A => omap (Inter A) (unlift unlift_ctor B))
(unlift unlift_ctor A)
| _ => None
end.
Lemma unlift_lift {C: ctor}: forall lift_ctor isLeft unlift_ctor,
pcancel lift_ctor unlift_ctor ->
pcancel (@lift C lift_ctor isLeft) (unlift unlift_ctor).
Proof.
move => lift_ctor isLeft unlift_ctor unlift_lift_ctor.
elim => //.
- move => ? ? /=.
by rewrite unlift_lift_ctor /= => ->.
- by move => /= ? -> ? ->.
- by move => /= ? -> ? ->.
- by move => /= ? -> ? ->.
Qed.
Definition unlift_ctor1: sum_preOrderedType Constructor1 Constructor2 -> option Constructor1 :=
(fun c => if c is inl x then Some x else None).
Definition unlift1 := unlift unlift_ctor1.
Definition unlift_ctor2: sum_preOrderedType Constructor1 Constructor2 -> option Constructor2 :=
(fun c => if c is inr x then Some x else None).
Definition unlift2 := unlift unlift_ctor2.
Lemma unlift_ctor1_inl: pcancel inl unlift_ctor1.
Proof. done. Qed.
Lemma unlift_ctor2_inr: pcancel inr unlift_ctor2.
Proof. done. Qed.
Lemma lift_map_bigcap {C: ctor}:
forall lift_ctor isLeft (Delta: seq (@IT C)),
lift lift_ctor isLeft (\bigcap_(A_i <- Delta) A_i) =
\bigcap_(A_i <- map (lift lift_ctor isLeft) Delta) A_i.
Proof.
move => lift_ctor isLeft.
elim => // A1.
case => // A2 Delta IH.
by rewrite bigcap_cons [X in _ = X]bigcap_cons -/(map _) -IH.
Qed.
Lemma lift_cast_ctor {C: ctor}:
forall lift_ctor isLeft,
(forall c1 c2, [ctor c1 <= c2] = [ctor (lift_ctor c1) <= (lift_ctor c2)]) ->
forall d (B A: @IT C), cast (lift lift_ctor isLeft (Ctor d B)) (lift lift_ctor isLeft A) =
map (lift lift_ctor isLeft) (cast (Ctor d B) A).
Proof.
move => lift_ctor isLeft lift_ctor_hom d B.
elim => //.
- move => c A _.
rewrite /cast /=.
move: (lift_ctor_hom c d).
rewrite [X in if X then _ else _]/[ctor _ <= _] /=.
by case: [ctor c <= d] => <-.
- move => A1 IH1 A2 IH2.
by rewrite cast_inter // map_cat -IH1 -IH2 cast_inter.
Qed.
Lemma isOmega_lift {C: ctor}: forall lift_ctor isLeft (A: @IT C), isOmega A = isOmega (lift lift_ctor isLeft A).
Proof.
move => lift_ctor isLeft.
by elim => //= ? -> ? ->.
Qed.
Lemma lift_cast_arr {C: ctor}:
forall lift_ctor isLeft,
forall (B1 B2 A: @IT C), cast (lift lift_ctor isLeft (B1 -> B2)) (lift lift_ctor isLeft A) =
(map (fun AB => (lift lift_ctor isLeft AB.1, lift lift_ctor isLeft AB.2)) (cast (B1 -> B2) A)).
Proof.
move => lift_ctor isLeft B1 B2.
move: (fun A1 A2 => cast_inter _ A1 A2 (B1 -> B2)).
move: (fun A1 A2 => cast_inter _ A1 A2 (lift lift_ctor isLeft (B1 -> B2))).
rewrite /cast -isOmega_lift /=.
case isOmega__B2: (isOmega B2) => //.
move => cast_eq1 cast_eq2.
elim => //= A1 IH1 A2 IH2.
by rewrite cast_eq1 // cast_eq2 // IH1 IH2 map_cat.
Qed.
Lemma lift_cast_prod {C: ctor}:
forall lift_ctor isLeft,
forall (B1 B2 A: @IT C), cast (lift lift_ctor isLeft (B1 \times B2)) (lift lift_ctor isLeft A) =
(map (fun AB => (Prod (lift lift_ctor isLeft AB.1)
(lift lift_ctor isLeft AB.2))) (cast (B1 \times B2) A)).
Proof.
move => lift_ctor isLeft B1 B2.
elim => //.
- move => A1 _ A2 _ /=.
by rewrite /cast /= preorder_reflexive.
- move => A1 IH1 A2 IH2 /=.
by rewrite cast_inter // IH1 IH2 -map_cat cast_inter.
Qed.
Lemma cast_bigcap_cat {C: ctor}:
forall (A: @IT C) Delta1 Delta2,
~~isOmega A ->
cast A (\bigcap_(A_i <- Delta1 ++ Delta2) A_i) =
cast A (\bigcap_(A_i <- Delta1) A_i) ++ cast A (\bigcap_(A_i <- Delta2) A_i).
Proof.
move => A.
elim.
- move => Delta2 /negbTE.
by rewrite /cast /= => ->.
- move => B1 Delta1 IH Delta2 disprf.
move: IH (IH Delta2 disprf) => _.
case: Delta1.
+ case: Delta2.
* by rewrite /cast (negbTE disprf) /= cats0.
* move => ? ? /=.
by rewrite cast_inter.
+ move => ? ? /=.
rewrite cast_inter // cast_inter // => ->.
by rewrite catA.
Qed.
Lemma lift_cast_inter_prod {C: ctor}:
forall lift_ctor isLeft,
forall (B1 B2 A: @IT C),
cast (lift lift_ctor isLeft B1 \times lift lift_ctor isLeft B2)
(\bigcap_(A__i <- cast (@Ctor LiftedConstructor (inl isLeft)
(lift lift_ctor isLeft B1 \times lift lift_ctor isLeft B2))
(lift lift_ctor isLeft A)) A__i) =
map (fun AB => (lift lift_ctor isLeft AB.1,
lift lift_ctor isLeft AB.2))
(cast (B1 \times B2) A).
Proof.
move => lift_ctor isLeft B1 B2.
elim => //.
- move => A1 _ A2 _.
by rewrite /cast /= preorder_reflexive.
- move => A1 IH1 A2 IH2.
by rewrite /= cast_inter // cast_inter // cast_bigcap_cat // IH1 IH2 map_cat.
Qed.
Lemma map_nilp {a b: Type}: forall (f: a -> b) xs, nilp (map f xs) = nilp xs.
Proof. by move => ? []. Qed.
Lemma lift_subtype_hom {C: ctor}:
forall lift_ctor isLeft unlift_ctor,
(forall c1 c2, [ctor c1 <= c2] = [ctor (lift_ctor c1) <= (lift_ctor c2)]) ->
pcancel lift_ctor unlift_ctor ->
ITHomomorphism.subtype_hom C _ (lift lift_ctor isLeft).
Proof.
move => lift_ctor isLeft unlift_ctor lift_hom unlift_lift_ctor A B.
move: (total _ [subty A of B]).
move => /(Types.Domain_ind _ (fun i =>
match i with
| [ subty A of B] =>
[ bcd (A) <= B] <->
[ bcd (lift lift_ctor isLeft A) <= lift lift_ctor isLeft B]
| [ tgt_for_srcs_gte B1 in Delta] =>
forall Delta',
Types.Semantics [tgt_for_srcs_gte B1 in Delta] [check_tgt Delta'] <->
Types.Semantics [tgt_for_srcs_gte (lift lift_ctor isLeft B1) in
(map (fun AB => (lift lift_ctor isLeft AB.1,
lift lift_ctor isLeft AB.2)) Delta)]
[check_tgt (map (lift lift_ctor isLeft) Delta')]
end)) res.
apply: res; move: A B => _ _.
- by move => ?; split => /=.
- move => A d B _ IH.
split.
+ move => /subty_complete /SubtypeMachine_inv.
move => /(fun prf => prf (fun i r =>
match i, r with
| [subty A of Ctor d B], Return true =>
[bcd (lift lift_ctor isLeft A) <= lift lift_ctor isLeft (Ctor d B)]
| _, _ => True
end)) res.
apply: res.
case; last by rewrite andbF.
move => /subty__sound /IH /subty_complete prf.
case canCast: (~~(nilp (cast (Ctor d B) A))) => //=.
apply: subty__sound.
have: (true = ~~(nilp (cast (lift lift_ctor isLeft (Ctor d B)) (lift lift_ctor isLeft A))) && true).
{ move: canCast.
by rewrite lift_cast_ctor // map_nilp => ->. }
move => ->.
apply: step__Ctor.
by rewrite lift_cast_ctor // -lift_map_bigcap.
+ move => /= /subty_complete /SubtypeMachine_inv.
move => /(fun prf => prf (fun i r =>
match i, r with
| [subty A of Ctor d B], Return true =>
if (unlift unlift_ctor A) is Some A
then if (unlift unlift_ctor (Ctor d B)) is Some B
then [bcd A <= B]
else True
else True
| _, _ => True
end)).
rewrite unlift_lift //= unlift_lift_ctor /= unlift_lift //=.
move => res.
apply: res.
case; last by rewrite andbF.
rewrite lift_cast_ctor // map_nilp -lift_map_bigcap.
move => /subty__sound /IH /subty_complete prf.
case canCast: (~~(nilp (cast (Ctor d B) A))) => //=.
apply /subty__sound.
rewrite -canCast -[X in Return X]andbT.
by apply: step__Ctor.
- move => A B1 B2 _ IH1 _ IH2.
split.
+ move => /subty_complete /SubtypeMachine_inv.
move => /(fun prf => prf (fun i r =>
match i, r with
| [subty A of B1 -> B2], Return true =>
[bcd (lift lift_ctor isLeft A) <= lift lift_ctor isLeft (B1 -> B2)]
| _, _ => True
end)) res.
apply: res.
move => Delta.
case isOmega__B2: (isOmega B2) => /=.
* move => *.
apply /subty__sound.
apply: subty__Omega.
by rewrite /= -isOmega_lift isOmega__B2.
* case => // prf1 prf2.
apply: subty__sound.
rewrite -[X in Return X]orFb -isOmega__B2 (isOmega_lift lift_ctor isLeft).
apply: (step__Arr (Delta := map (lift lift_ctor isLeft) Delta)).
** rewrite lift_cast_arr.
by apply /IH1.
** rewrite -lift_map_bigcap.
apply /subty_complete.
apply /IH2 => //.
by apply /subty__sound.
+ move => /= /subty_complete /SubtypeMachine_inv.
move => /(fun prf => prf (fun i r =>
match i, r with
| [subty A of B], Return true =>
if (unlift unlift_ctor A) is Some A
then if (unlift unlift_ctor B) is Some B
then [bcd A <= B]