-
Notifications
You must be signed in to change notification settings - Fork 3
/
AGT.agda
1770 lines (1557 loc) · 76.1 KB
/
AGT.agda
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
module AGT where
open import Agda.Primitive renaming (_⊔_ to _⊍_)
open import Types
open import Labels
open import Pow2
open import Data.Product using (_×_; proj₁; proj₂; Σ; Σ-syntax)
renaming (_,_ to ⟨_,_⟩)
open import Data.Bool using (Bool; true; false)
open import Data.Nat using (ℕ; zero; suc; _≤_; _+_; _*_; z≤n; s≤s) renaming (_⊔_ to _∨_)
open import Data.Nat.Properties
using (⊔-mono-≤; ⊔-monoˡ-≤; ⊔-monoʳ-≤; ≤-trans; ≤-refl; ≤-reflexive;
m≤m⊔n; m≤n⊔m; ⊔-assoc; ⊔-comm; ≤-step; ⊔-idem;
⊔-identityˡ; ⊔-identityʳ; +-mono-≤; *-monoʳ-≤)
open import Data.Nat.Solver
open import Data.Sum using (_⊎_; inj₁; inj₂)
open import Data.Empty using (⊥; ⊥-elim)
open import Relation.Binary.PropositionalEquality
using (_≡_;_≢_; refl; trans; sym; cong; cong₂; cong-app)
open import Relation.Nullary using (¬_; Dec; yes; no)
open import Relation.Nullary.Negation using (contradiction)
data SType : Set where
`_ : Base → SType
_⇒_ : SType → SType → SType
_`×_ : SType → SType → SType
_`⊎_ : SType → SType → SType
to-type : SType → Type
to-type (` ι) = (` ι)
to-type (S ⇒ T) = to-type S ⇒ to-type T
to-type (S `× T) = to-type S `× to-type T
to-type (S `⊎ T) = to-type S `⊎ to-type T
data _⌢_ : SType → SType → Set where
base⌢ : ∀{ι : Base} → (` ι) ⌢ (` ι)
fun⌢ : ∀{A B A' B'}
-------------------
→ (A ⇒ B) ⌢ (A' ⇒ B')
pair⌢ : ∀{A B A' B'}
-------------------
→ (A `× B) ⌢ (A' `× B')
sum⌢ : ∀{A B A' B'}
-------------------
→ (A `⊎ B) ⌢ (A' `⊎ B')
{- Concretization -}
data Conc : Type → SType → Set where
c-base : ∀{ι} → Conc (` ι) (` ι)
c-fun : ∀{T₁ T₂ : Type} {S₁ S₂ : SType}
→ Conc T₁ S₁ → Conc T₂ S₂
-------------------------
→ Conc (T₁ ⇒ T₂) (S₁ ⇒ S₂)
c-pair : ∀{T₁ T₂ : Type} {S₁ S₂ : SType}
→ Conc T₁ S₁ → Conc T₂ S₂
-------------------------
→ Conc (T₁ `× T₂) (S₁ `× S₂)
c-sum : ∀{T₁ T₂ : Type} {S₁ S₂ : SType}
→ Conc T₁ S₁ → Conc T₂ S₂
-------------------------
→ Conc (T₁ `⊎ T₂) (S₁ `⊎ S₂)
c-unk : ∀{S} → Conc ⋆ S
infix 6 _`⊑_
data _`⊑_ : Type → Type → Set where
prec : ∀{A B}
→ (∀{S} → Conc A S → Conc B S)
----------------------------
→ A `⊑ B
conc : (A : Type) → Σ[ S ∈ SType ] Conc A S
conc ⋆ = ⟨ ` 𝔹 , c-unk ⟩
conc (` ι) = ⟨ ` ι , c-base ⟩
conc (A ⇒ B) with conc A | conc B
... | ⟨ A' , ca ⟩ | ⟨ B' , cb ⟩ =
⟨ A' ⇒ B' , c-fun ca cb ⟩
conc (A `× B) with conc A | conc B
... | ⟨ A' , ca ⟩ | ⟨ B' , cb ⟩ =
⟨ A' `× B' , c-pair ca cb ⟩
conc (A `⊎ B) with conc A | conc B
... | ⟨ A' , ca ⟩ | ⟨ B' , cb ⟩ =
⟨ A' `⊎ B' , c-sum ca cb ⟩
prec-unk-inv : ∀{A}
→ ⋆ `⊑ A
------
→ A ≡ ⋆
prec-unk-inv {⋆} (prec f) = refl
prec-unk-inv {` ι} (prec f) with f {` ι ⇒ ` ι} c-unk
... | ()
prec-unk-inv {A ⇒ A₁} (prec f) with f {` Nat} c-unk
... | ()
prec-unk-inv {A `× A₁} (prec f) with f {` Nat} c-unk
... | ()
prec-unk-inv {A `⊎ A₁} (prec f) with f {` Nat} c-unk
... | ()
prec-base-inv : ∀{A ι}
→ ` ι `⊑ A
---------------
→ A ≡ ` ι ⊎ A ≡ ⋆
prec-base-inv {⋆} (prec f) = inj₂ refl
prec-base-inv {` ι} {ι'} (prec f) with f {` ι'} c-base
... | c-base = inj₁ refl
prec-base-inv {A ⇒ A₁} {ι} (prec f) with f {` ι} c-base
... | ()
prec-base-inv {A `× A₁} {ι} (prec f) with f {` ι} c-base
... | ()
prec-base-inv {A `⊎ A₁} {ι} (prec f) with f {` ι} c-base
... | ()
prec-fun-inv : ∀{A₁ A₂ B₁ B₂}
→ (A₁ ⇒ A₂) `⊑ (B₁ ⇒ B₂)
-----------------------
→ (A₁ `⊑ B₁) × (A₂ `⊑ B₂)
prec-fun-inv {A₁}{A₂}{B₁}{B₂} (prec f) =
⟨ prec g , prec h ⟩
where
g : {S : SType} → Conc A₁ S → Conc B₁ S
g ca with conc A₂
... | ⟨ A₂' , ca2 ⟩ with f (c-fun ca ca2)
... | c-fun a b = a
h : {S : SType} → Conc A₂ S → Conc B₂ S
h ca with conc A₁
... | ⟨ A' , ca1 ⟩ with f (c-fun ca1 ca )
... | c-fun a b = b
prec-left-fun-inv : ∀{A₁ A₂ B}
→ (A₁ ⇒ A₂) `⊑ B
-----------------------
→ (Σ[ B₁ ∈ Type ] Σ[ B₂ ∈ Type ] (B ≡ B₁ ⇒ B₂) × (A₁ `⊑ B₁) × (A₂ `⊑ B₂))
⊎ B ≡ ⋆
prec-left-fun-inv {A₁} {A₂} {⋆} (prec f) = inj₂ refl
prec-left-fun-inv {A₁} {A₂} {` ι} (prec f)
with conc A₁ | conc A₂
... | ⟨ A₁' , ca1 ⟩ | ⟨ A₂' , ca2 ⟩
with f (c-fun ca1 ca2)
... | ()
prec-left-fun-inv {A₁} {A₂} {B₁ ⇒ B₂} (prec f) with prec-fun-inv (prec f)
... | ⟨ a1b1 , a2b2 ⟩ =
inj₁ ⟨ B₁ , ⟨ B₂ , ⟨ refl , ⟨ a1b1 , a2b2 ⟩ ⟩ ⟩ ⟩
prec-left-fun-inv {A₁} {A₂} {B `× B₁} (prec f)
with conc A₁ | conc A₂
... | ⟨ A₁' , ca1 ⟩ | ⟨ A₂' , ca2 ⟩
with f (c-fun ca1 ca2)
... | ()
prec-left-fun-inv {A₁} {A₂} {B `⊎ B₁} (prec f)
with conc A₁ | conc A₂
... | ⟨ A₁' , ca1 ⟩ | ⟨ A₂' , ca2 ⟩
with f (c-fun ca1 ca2)
... | ()
prec-pair-inv : ∀{A₁ A₂ B₁ B₂}
→ (A₁ `× A₂) `⊑ (B₁ `× B₂)
-----------------------
→ (A₁ `⊑ B₁) × (A₂ `⊑ B₂)
prec-pair-inv {A₁}{A₂}{B₁}{B₂} (prec f) =
⟨ prec g , prec h ⟩
where
g : {S : SType} → Conc A₁ S → Conc B₁ S
g ca with conc A₂
... | ⟨ A₂' , ca2 ⟩ with f (c-pair ca ca2)
... | c-pair a b = a
h : {S : SType} → Conc A₂ S → Conc B₂ S
h ca with conc A₁
... | ⟨ A' , ca1 ⟩ with f (c-pair ca1 ca )
... | c-pair a b = b
prec-left-pair-inv : ∀{A₁ A₂ B}
→ (A₁ `× A₂) `⊑ B
-----------------------
→ (Σ[ B₁ ∈ Type ] Σ[ B₂ ∈ Type ] (B ≡ B₁ `× B₂) × (A₁ `⊑ B₁) × (A₂ `⊑ B₂))
⊎ B ≡ ⋆
prec-left-pair-inv {A₁} {A₂} {⋆} (prec f) = inj₂ refl
prec-left-pair-inv {A₁} {A₂} {` ι} (prec f)
with conc A₁ | conc A₂
... | ⟨ A₁' , ca1 ⟩ | ⟨ A₂' , ca2 ⟩
with f (c-pair ca1 ca2)
... | ()
prec-left-pair-inv {A₁} {A₂} {B ⇒ B₁} (prec f)
with conc A₁ | conc A₂
... | ⟨ A₁' , ca1 ⟩ | ⟨ A₂' , ca2 ⟩
with f (c-pair ca1 ca2)
... | ()
prec-left-pair-inv {A₁} {A₂} {B₁ `× B₂} (prec f) with prec-pair-inv (prec f)
... | ⟨ a1b1 , a2b2 ⟩ =
inj₁ ⟨ B₁ , ⟨ B₂ , ⟨ refl , ⟨ a1b1 , a2b2 ⟩ ⟩ ⟩ ⟩
prec-left-pair-inv {A₁} {A₂} {B `⊎ B₁} (prec f)
with conc A₁ | conc A₂
... | ⟨ A₁' , ca1 ⟩ | ⟨ A₂' , ca2 ⟩
with f (c-pair ca1 ca2)
... | ()
prec-sum-inv : ∀{A₁ A₂ B₁ B₂}
→ (A₁ `⊎ A₂) `⊑ (B₁ `⊎ B₂)
-----------------------
→ (A₁ `⊑ B₁) × (A₂ `⊑ B₂)
prec-sum-inv {A₁}{A₂}{B₁}{B₂} (prec f) =
⟨ prec g , prec h ⟩
where
g : {S : SType} → Conc A₁ S → Conc B₁ S
g ca with conc A₂
... | ⟨ A₂' , ca2 ⟩ with f (c-sum ca ca2)
... | c-sum a b = a
h : {S : SType} → Conc A₂ S → Conc B₂ S
h ca with conc A₁
... | ⟨ A' , ca1 ⟩ with f (c-sum ca1 ca )
... | c-sum a b = b
prec-left-sum-inv : ∀{A₁ A₂ B}
→ (A₁ `⊎ A₂) `⊑ B
-----------------------
→ (Σ[ B₁ ∈ Type ] Σ[ B₂ ∈ Type ] (B ≡ B₁ `⊎ B₂) × (A₁ `⊑ B₁) × (A₂ `⊑ B₂))
⊎ B ≡ ⋆
prec-left-sum-inv {A₁} {A₂} {⋆} (prec f) = inj₂ refl
prec-left-sum-inv {A₁} {A₂} {` ι} (prec f)
with conc A₁ | conc A₂
... | ⟨ A₁' , ca1 ⟩ | ⟨ A₂' , ca2 ⟩
with f (c-sum ca1 ca2)
... | ()
prec-left-sum-inv {A₁} {A₂} {B ⇒ B₁} (prec f)
with conc A₁ | conc A₂
... | ⟨ A₁' , ca1 ⟩ | ⟨ A₂' , ca2 ⟩
with f (c-sum ca1 ca2)
... | ()
prec-left-sum-inv {A₁} {A₂} {B `× B₁} (prec f)
with conc A₁ | conc A₂
... | ⟨ A₁' , ca1 ⟩ | ⟨ A₂' , ca2 ⟩
with f (c-sum ca1 ca2)
... | ()
prec-left-sum-inv {A₁} {A₂} {B₁ `⊎ B₂} (prec f) with prec-sum-inv (prec f)
... | ⟨ a1b1 , a2b2 ⟩ =
inj₁ ⟨ B₁ , ⟨ B₂ , ⟨ refl , ⟨ a1b1 , a2b2 ⟩ ⟩ ⟩ ⟩
le-implies-prec : ∀ {A B} → A ⊑ B → B `⊑ A
le-implies-prec unk⊑ = prec (λ {S} _ → c-unk)
le-implies-prec base⊑ = prec (λ {S} z → z)
le-implies-prec (fun⊑ le₁ le₂)
with le-implies-prec le₁ | le-implies-prec le₂
... | prec imp1 | prec imp2 =
prec λ { (c-fun x y) → c-fun (imp1 x) (imp2 y) }
le-implies-prec (pair⊑ le₁ le₂)
with le-implies-prec le₁ | le-implies-prec le₂
... | prec imp1 | prec imp2 =
prec λ { (c-pair x y) → c-pair (imp1 x) (imp2 y) }
le-implies-prec (sum⊑ le₁ le₂)
with le-implies-prec le₁ | le-implies-prec le₂
... | prec imp1 | prec imp2 =
prec λ { (c-sum x y) → c-sum (imp1 x) (imp2 y) }
prec-implies-le : ∀{A B} → A `⊑ B → B ⊑ A
prec-implies-le {⋆} {B} (prec f) with prec-unk-inv (prec f)
... | eq rewrite eq = unk⊑
prec-implies-le {` ι} {B} (prec f) with prec-base-inv (prec f)
... | inj₁ eq rewrite eq = base⊑
... | inj₂ eq rewrite eq = unk⊑
prec-implies-le {A₁ ⇒ A₂} {B} (prec f) with prec-left-fun-inv (prec f)
... | inj₁ ⟨ B₁ , ⟨ B₂ , ⟨ eq , ⟨ a1b1 , a2b2 ⟩ ⟩ ⟩ ⟩ rewrite eq =
fun⊑ (prec-implies-le a1b1) (prec-implies-le a2b2)
... | inj₂ eq rewrite eq = unk⊑
prec-implies-le {A₁ `× A₂} {B} (prec f) with prec-left-pair-inv (prec f)
... | inj₁ ⟨ B₁ , ⟨ B₂ , ⟨ eq , ⟨ a1b1 , a2b2 ⟩ ⟩ ⟩ ⟩ rewrite eq =
pair⊑ (prec-implies-le a1b1) (prec-implies-le a2b2)
... | inj₂ eq rewrite eq = unk⊑
prec-implies-le {A₁ `⊎ A₂} {B} (prec f) with prec-left-sum-inv (prec f)
... | inj₁ ⟨ B₁ , ⟨ B₂ , ⟨ eq , ⟨ a1b1 , a2b2 ⟩ ⟩ ⟩ ⟩ rewrite eq =
sum⊑ (prec-implies-le a1b1) (prec-implies-le a2b2)
... | inj₂ eq rewrite eq = unk⊑
data _~'_ : Type → Type → Set where
cons : ∀ {A B : Type} {S : SType}
→ Conc A S → Conc B S
-------------------
→ A ~' B
cons-implies-ceq : ∀ {A B} → A ~ B → A ~' B
cons-implies-ceq {.⋆}{B} unk~L with conc B
... | ⟨ B' , cb ⟩ = cons c-unk cb
cons-implies-ceq {A}{⋆} unk~R with conc A
... | ⟨ A' , ca ⟩ = cons ca c-unk
cons-implies-ceq base~ = cons c-base c-base
cons-implies-ceq {A₁ ⇒ A₂}{B₁ ⇒ B₂} (fun~ cns₁ cns₂)
with cons-implies-ceq cns₁ | cons-implies-ceq cns₂
... | cons{S = S₁} c1 c2 | cons{S = S₂} c3 c4 =
cons (c-fun c2 c3) (c-fun c1 c4)
cons-implies-ceq {A₁ `× A₂}{B₁ `× B₂} (pair~ cns₁ cns₂)
with cons-implies-ceq cns₁ | cons-implies-ceq cns₂
... | cons{S = S₁} c1 c2 | cons{S = S₂} c3 c4 =
cons (c-pair c1 c3) (c-pair c2 c4)
cons-implies-ceq {A₁ `⊎ A₂}{B₁ `⊎ B₂} (sum~ cns₁ cns₂)
with cons-implies-ceq cns₁ | cons-implies-ceq cns₂
... | cons{S = S₁} c1 c2 | cons{S = S₂} c3 c4 =
cons (c-sum c1 c3) (c-sum c2 c4)
ceq-implies-cons : ∀ {A B} → A ~' B → A ~ B
ceq-implies-cons {.(` _)} {.(` _)} (cons {S = .(` _)} c-base c-base) = base~
ceq-implies-cons {.(` _)} {.⋆} (cons {S = .(` _)} c-base c-unk) = unk~R
ceq-implies-cons (cons {S = .(_ ⇒ _)} (c-fun as as₁) (c-fun bs bs₁)) =
fun~ (ceq-implies-cons (cons bs as)) (ceq-implies-cons (cons as₁ bs₁))
ceq-implies-cons (cons {S = .(_ ⇒ _)} (c-fun as as₁) c-unk) = unk~R
ceq-implies-cons (cons {S = .(_ `× _)} (c-pair as as₁) (c-pair bs bs₁)) =
pair~ (ceq-implies-cons (cons as bs)) (ceq-implies-cons (cons as₁ bs₁))
ceq-implies-cons (cons {S = .(_ `× _)} (c-pair as as₁) c-unk) = unk~R
ceq-implies-cons (cons {S = .(_ `⊎ _)} (c-sum as as₁) (c-sum bs bs₁)) =
sum~ (ceq-implies-cons (cons as bs)) (ceq-implies-cons (cons as₁ bs₁))
ceq-implies-cons (cons {S = .(_ `⊎ _)} (c-sum as as₁) c-unk) = unk~R
ceq-implies-cons (cons {S = S} c-unk bs) = unk~L
{- Abstraction -}
data AllFuns : (SType → Set) → Set where
funs : ∀{P}
→ (∀{T : SType} → P T → Σ[ T₁ ∈ SType ] Σ[ T₂ ∈ SType ]
T ≡ T₁ ⇒ T₂)
-----------------------------------------------------
→ AllFuns P
data AllPairs : (SType → Set) → Set where
pairs : ∀{P}
→ (∀{T : SType} → P T → Σ[ T₁ ∈ SType ] Σ[ T₂ ∈ SType ]
T ≡ T₁ `× T₂)
-----------------------------------------------------
→ AllPairs P
data AllSums : (SType → Set) → Set where
sums : ∀{P}
→ (∀{T : SType} → P T → Σ[ T₁ ∈ SType ] Σ[ T₂ ∈ SType ]
T ≡ T₁ `⊎ T₂)
-----------------------------------------------------
→ AllSums P
data Dom : (SType → Set) → SType → Set where
in-dom : ∀{P : (SType → Set)} {T₁ T₂}
→ P (T₁ ⇒ T₂)
---------------------------------------------
→ Dom P T₁
data Cod : (SType → Set) → SType → Set where
in-cod : ∀{P} {T₁ T₂}
→ P (T₁ ⇒ T₂)
---------------------------------------------
→ Cod P T₂
data Proj₁ : (SType → Set) → SType → Set where
in-proj₁ : ∀{P : (SType → Set)} {T₁ T₂}
→ P (T₁ `× T₂)
---------------------------------------------
→ Proj₁ P T₁
data Proj₂ : (SType → Set) → SType → Set where
in-proj₂ : ∀{P : (SType → Set)} {T₁ T₂}
→ P (T₁ `× T₂)
---------------------------------------------
→ Proj₂ P T₂
data In₁ : (SType → Set) → SType → Set where
in-in₁ : ∀{P : (SType → Set)} {T₁ T₂}
→ P (T₁ `⊎ T₂)
---------------------------------------------
→ In₁ P T₁
data In₂ : (SType → Set) → SType → Set where
in-in₂ : ∀{P : (SType → Set)} {T₁ T₂}
→ P (T₁ `⊎ T₂)
---------------------------------------------
→ In₂ P T₂
data Abs : (SType → Set) → Type → Set₁ where
abs-base : ∀{P : SType → Set} {ι : Base}
→ P (` ι)
→ (∀{T : SType} → P T → T ≡ ` ι)
-------------------------------
→ Abs P (` ι)
abs-fun : ∀{P : SType → Set}{A B : Type}
→ AllFuns P
→ Abs (Dom P) A
→ Abs (Cod P) B
----------------------
→ Abs P (A ⇒ B)
abs-pair : ∀{P : SType → Set}{A B : Type}
→ AllPairs P
→ Abs (Proj₁ P) A
→ Abs (Proj₂ P) B
----------------------
→ Abs P (A `× B)
abs-sum : ∀{P : SType → Set}{A B : Type}
→ AllSums P
→ Abs (In₁ P) A
→ Abs (In₂ P) B
----------------------
→ Abs P (A `⊎ B)
abs-any : ∀{P : SType → Set} {S T : SType}
→ ¬ (S ⌢ T)
→ P S → P T
---------------
→ Abs P ⋆
abs-non-empty : ∀{P : SType → Set}{A : Type}
→ Abs P A
→ Σ[ T ∈ SType ] P T
abs-non-empty {P} {` ι} (abs-base x x₁) = ⟨ ` ι , x ⟩
abs-non-empty {P} {⋆} (abs-any{T = T} x x₁ x₂) = ⟨ T , x₂ ⟩
abs-non-empty {P} {_} (abs-fun x abs₁ abs₂)
with abs-non-empty abs₁
... | ⟨ T₁ , in-dom {T₂ = T₂'} PT₁T₂' ⟩ =
⟨ (T₁ ⇒ T₂') , PT₁T₂' ⟩
abs-non-empty {P} {_} (abs-pair x abs₁ abs₂)
with abs-non-empty abs₁
... | ⟨ T₁ , in-proj₁ {T₂ = T₂'} PT₁T₂' ⟩ =
⟨ (T₁ `× T₂') , PT₁T₂' ⟩
abs-non-empty {P} {_} (abs-sum x abs₁ abs₂)
with abs-non-empty abs₁
... | ⟨ T₁ , in-in₁ {T₂ = T₂'} PT₁T₂' ⟩ =
⟨ (T₁ `⊎ T₂') , PT₁T₂' ⟩
_⊆_ : (SType → Set) → (SType → Set) → Set
P ⊆ P' = ∀{T : SType} → P T → P' T
_⇔_ : (SType → Set) → (SType → Set) → Set
P ⇔ P' = P ⊆ P' × P' ⊆ P
dom-subset : ∀{P Q : SType → Set}
→ P ⊆ Q
-------------
→ Dom P ⊆ Dom Q
dom-subset pq (in-dom x) = in-dom (pq x)
proj₁-subset : ∀{P Q : SType → Set}
→ P ⊆ Q
-------------
→ Proj₁ P ⊆ Proj₁ Q
proj₁-subset pq (in-proj₁ x) = in-proj₁ (pq x)
in₁-subset : ∀{P Q : SType → Set}
→ P ⊆ Q
-------------
→ In₁ P ⊆ In₁ Q
in₁-subset pq (in-in₁ x) = in-in₁ (pq x)
cod-subset : ∀{P Q : SType → Set}
→ P ⊆ Q
-------------
→ Cod P ⊆ Cod Q
cod-subset pq (in-cod x) = in-cod (pq x)
proj₂-subset : ∀{P Q : SType → Set}
→ P ⊆ Q
-------------
→ Proj₂ P ⊆ Proj₂ Q
proj₂-subset pq (in-proj₂ x) = in-proj₂ (pq x)
in₂-subset : ∀{P Q : SType → Set}
→ P ⊆ Q
-------------
→ In₂ P ⊆ In₂ Q
in₂-subset pq (in-in₂ x) = in-in₂ (pq x)
dom-equiv : ∀{P Q : SType → Set}
→ P ⇔ Q
-------------
→ Dom P ⇔ Dom Q
dom-equiv pq = ⟨ (dom-subset (proj₁ pq)) , (dom-subset (proj₂ pq)) ⟩
cod-equiv : ∀{P Q : SType → Set}
→ P ⇔ Q
-------------
→ Cod P ⇔ Cod Q
cod-equiv pq = ⟨ (cod-subset (proj₁ pq)) , (cod-subset (proj₂ pq)) ⟩
proj₁-equiv : ∀{P Q : SType → Set}
→ P ⇔ Q
-----------------
→ Proj₁ P ⇔ Proj₁ Q
proj₁-equiv pq = ⟨ (proj₁-subset (proj₁ pq)) , (proj₁-subset (proj₂ pq)) ⟩
proj₂-equiv : ∀{P Q : SType → Set}
→ P ⇔ Q
-------------
→ Proj₂ P ⇔ Proj₂ Q
proj₂-equiv pq = ⟨ (proj₂-subset (proj₁ pq)) , (proj₂-subset (proj₂ pq)) ⟩
in₁-equiv : ∀{P Q : SType → Set}
→ P ⇔ Q
-----------------
→ In₁ P ⇔ In₁ Q
in₁-equiv pq = ⟨ (in₁-subset (proj₁ pq)) , (in₁-subset (proj₂ pq)) ⟩
in₂-equiv : ∀{P Q : SType → Set}
→ P ⇔ Q
-------------
→ In₂ P ⇔ In₂ Q
in₂-equiv pq = ⟨ (in₂-subset (proj₁ pq)) , (in₂-subset (proj₂ pq)) ⟩
allfuns-equiv : ∀{P Q : SType → Set}
→ AllFuns P → P ⇔ Q
--------------------
→ AllFuns Q
allfuns-equiv{P}{Q} (funs f) p-q = (funs G)
where
G : {T : SType} →
Q T → Σ-syntax SType (λ T₁ → Σ-syntax SType (λ T₂ → T ≡ (T₁ ⇒ T₂)))
G {T} qt with f {T} ((proj₂ p-q) qt)
... | ⟨ T₁ , ⟨ T₂ , eq ⟩ ⟩ rewrite eq =
⟨ T₁ , ⟨ T₂ , refl ⟩ ⟩
allpairs-equiv : ∀{P Q : SType → Set}
→ AllPairs P → P ⇔ Q
--------------------
→ AllPairs Q
allpairs-equiv{P}{Q} (pairs f) p-q = (pairs G)
where
G : {T : SType} →
Q T → Σ-syntax SType (λ T₁ → Σ-syntax SType (λ T₂ → T ≡ (T₁ `× T₂)))
G {T} qt with f {T} ((proj₂ p-q) qt)
... | ⟨ T₁ , ⟨ T₂ , eq ⟩ ⟩ rewrite eq =
⟨ T₁ , ⟨ T₂ , refl ⟩ ⟩
allsums-equiv : ∀{P Q : SType → Set}
→ AllSums P → P ⇔ Q
--------------------
→ AllSums Q
allsums-equiv{P}{Q} (sums f) p-q = (sums G)
where
G : {T : SType} →
Q T → Σ-syntax SType (λ T₁ → Σ-syntax SType (λ T₂ → T ≡ (T₁ `⊎ T₂)))
G {T} qt with f {T} ((proj₂ p-q) qt)
... | ⟨ T₁ , ⟨ T₂ , eq ⟩ ⟩ rewrite eq =
⟨ T₁ , ⟨ T₂ , refl ⟩ ⟩
abs-equiv : ∀{P Q : SType → Set}{A : Type}
→ Abs P A → P ⇔ Q
-----------------
→ Abs Q A
abs-equiv (abs-base x x₁) p-q =
abs-base (proj₁ p-q x) (λ {T} z → x₁ (proj₂ p-q z))
abs-equiv{P}{Q} (abs-fun{A = A}{B = B} allf abs-dom-p abs-cod-p) p-q =
let dp⇔dq = dom-equiv p-q in
let cp⇔cq = cod-equiv p-q in
abs-fun (allfuns-equiv allf p-q) (abs-equiv abs-dom-p (dom-equiv p-q))
(abs-equiv abs-cod-p (cod-equiv p-q) )
abs-equiv{P}{Q} (abs-pair{A = A}{B = B} allf abs-dom-p abs-cod-p) p-q =
let dp⇔dq = proj₁-equiv p-q in
let cp⇔cq = proj₂-equiv p-q in
abs-pair (allpairs-equiv allf p-q) (abs-equiv abs-dom-p (proj₁-equiv p-q))
(abs-equiv abs-cod-p (proj₂-equiv p-q) )
abs-equiv{P}{Q} (abs-sum{A = A}{B = B} allf abs-dom-p abs-cod-p) p-q =
let dp⇔dq = in₁-equiv p-q in
let cp⇔cq = in₂-equiv p-q in
abs-sum (allsums-equiv allf p-q) (abs-equiv abs-dom-p (in₁-equiv p-q))
(abs-equiv abs-cod-p (in₂-equiv p-q) )
abs-equiv (abs-any x x₁ x₂) p-q =
abs-any x (proj₁ p-q x₁) (proj₁ p-q x₂)
conc-abs-sound : ∀{P : SType → Set}{A : Type}
→ Abs P A
----------
→ P ⊆ Conc A
conc-abs-sound (abs-base p-i p-base) {T} pt
rewrite p-base {T} pt = c-base
conc-abs-sound (abs-fun allfun abs-a abs-b) pt
with allfun
... | funs af
with af pt
... | ⟨ T₁ , ⟨ T₂ , eq ⟩ ⟩ rewrite eq =
let ih1 = conc-abs-sound abs-a in
let ih2 = conc-abs-sound abs-b in
c-fun (ih1 (in-dom pt)) (ih2 (in-cod pt))
conc-abs-sound (abs-pair all abs-a abs-b) pt
with all
... | pairs af
with af pt
... | ⟨ T₁ , ⟨ T₂ , eq ⟩ ⟩ rewrite eq =
let ih1 = conc-abs-sound abs-a in
let ih2 = conc-abs-sound abs-b in
c-pair (ih1 (in-proj₁ pt)) (ih2 (in-proj₂ pt))
conc-abs-sound (abs-sum all abs-a abs-b) pt
with all
... | sums af
with af pt
... | ⟨ T₁ , ⟨ T₂ , eq ⟩ ⟩ rewrite eq =
let ih1 = conc-abs-sound abs-a in
let ih2 = conc-abs-sound abs-b in
c-sum (ih1 (in-in₁ pt)) (ih2 (in-in₂ pt))
conc-abs-sound (abs-any x x₁ x₂) pt = c-unk
c-any-base : ∀{A ι}
→ Conc A (` ι)
→ A ≡ (` ι) ⊎ A ≡ ⋆
c-any-base c-base = inj₁ refl
c-any-base c-unk = inj₂ refl
c-any-fun : ∀{A T₁ T₂}
→ Conc A (T₁ ⇒ T₂)
→ (Σ[ A₁ ∈ Type ] Σ[ A₂ ∈ Type ] A ≡ A₁ ⇒ A₂ × Conc A₁ T₁ × Conc A₂ T₂)
⊎ A ≡ ⋆
c-any-fun (c-fun{T₁}{T₂} c c₁) =
inj₁ ⟨ T₁ , ⟨ T₂ , ⟨ refl , ⟨ c , c₁ ⟩ ⟩ ⟩ ⟩
c-any-fun c-unk = inj₂ refl
c-any-pair : ∀{A T₁ T₂}
→ Conc A (T₁ `× T₂)
→ (Σ[ A₁ ∈ Type ] Σ[ A₂ ∈ Type ] A ≡ A₁ `× A₂ × Conc A₁ T₁ × Conc A₂ T₂)
⊎ A ≡ ⋆
c-any-pair (c-pair{T₁}{T₂} c c₁) =
inj₁ ⟨ T₁ , ⟨ T₂ , ⟨ refl , ⟨ c , c₁ ⟩ ⟩ ⟩ ⟩
c-any-pair c-unk = inj₂ refl
c-any-sum : ∀{A T₁ T₂}
→ Conc A (T₁ `⊎ T₂)
→ (Σ[ A₁ ∈ Type ] Σ[ A₂ ∈ Type ] A ≡ A₁ `⊎ A₂ × Conc A₁ T₁ × Conc A₂ T₂)
⊎ A ≡ ⋆
c-any-sum (c-sum{T₁}{T₂} c c₁) =
inj₁ ⟨ T₁ , ⟨ T₂ , ⟨ refl , ⟨ c , c₁ ⟩ ⟩ ⟩ ⟩
c-any-sum c-unk = inj₂ refl
conc-sh-cons : ∀{A T₁ T₂}
→ Conc A T₁ → Conc A T₂
-----------------------
→ A ≡ ⋆ ⊎ (T₁ ⌢ T₂)
conc-sh-cons c-base c-base = inj₂ base⌢
conc-sh-cons (c-fun a-t1 a-t3) (c-fun a-t2 a-t4) = inj₂ fun⌢
conc-sh-cons (c-pair a-t1 a-t3) (c-pair a-t2 a-t4) = inj₂ pair⌢
conc-sh-cons (c-sum a-t1 a-t3) (c-sum a-t2 a-t4) = inj₂ sum⌢
conc-sh-cons c-unk a-t2 = inj₁ refl
abs-optimal : ∀ {P : SType → Set} {A A' : Type}
→ (Σ[ T ∈ SType ] P T)
→ P ⊆ Conc A → Abs P A'
-------------------------
→ A ⊑ A'
abs-optimal ⟨ T , pt ⟩ p-ca (abs-base p-i all-base)
with pt
... | pt'
rewrite all-base pt
with c-any-base (p-ca pt')
... | inj₁ eq rewrite eq = Refl⊑
... | inj₂ eq rewrite eq = unk⊑
abs-optimal{P = P} ⟨ T , pt ⟩ p-ca (abs-fun{A = A}{B = B} allf abs-p1-b1 abs-p2-b2)
with allf
... | funs af
with af pt
... | ⟨ T₁ , ⟨ T₂ , eq ⟩ ⟩ rewrite eq
with c-any-fun (p-ca pt)
... | inj₁ ⟨ A₁ , ⟨ A₂ , ⟨ a=a12 , ⟨ c1 , c2 ⟩ ⟩ ⟩ ⟩ rewrite a=a12 =
let ih1 = abs-optimal ⟨ T₁ , in-dom pt ⟩ domP⊆ca1 abs-p1-b1 in
let ih2 = abs-optimal ⟨ T₂ , in-cod pt ⟩ codP⊆ca2 abs-p2-b2 in
fun⊑ ih1 ih2
where domP⊆ca1 : Dom P ⊆ Conc A₁
domP⊆ca1 {T'} (in-dom {T₂ = T₂} PT'⇒T2)
with p-ca PT'⇒T2
... | c-fun c-a1t' c-a2t2 = c-a1t'
codP⊆ca2 : Cod P ⊆ Conc A₂
codP⊆ca2 {T'} (in-cod {T₁ = T₁} PT₁⇒T')
with p-ca PT₁⇒T'
... | c-fun c1 c2 = c2
... | inj₂ a=unk rewrite a=unk =
unk⊑
abs-optimal{P = P} ⟨ T , pt ⟩ p-ca (abs-pair{A = A}{B = B} all abs-p1-b1 abs-p2-b2)
with all
... | pairs ap
with ap pt
... | ⟨ T₁ , ⟨ T₂ , eq ⟩ ⟩ rewrite eq
with c-any-pair (p-ca pt)
... | inj₁ ⟨ A₁ , ⟨ A₂ , ⟨ a=a12 , ⟨ c1 , c2 ⟩ ⟩ ⟩ ⟩ rewrite a=a12 =
let ih1 = abs-optimal ⟨ T₁ , in-proj₁ pt ⟩ domP⊆ca1 abs-p1-b1 in
let ih2 = abs-optimal ⟨ T₂ , in-proj₂ pt ⟩ codP⊆ca2 abs-p2-b2 in
pair⊑ ih1 ih2
where domP⊆ca1 : Proj₁ P ⊆ Conc A₁
domP⊆ca1 {T'} (in-proj₁ {T₂ = T₂} PT'⇒T2)
with p-ca PT'⇒T2
... | c-pair c-a1t' c-a2t2 = c-a1t'
codP⊆ca2 : Proj₂ P ⊆ Conc A₂
codP⊆ca2 {T'} (in-proj₂ {T₁ = T₁} PT₁⇒T')
with p-ca PT₁⇒T'
... | c-pair c1 c2 = c2
... | inj₂ a=unk rewrite a=unk =
unk⊑
abs-optimal{P = P} ⟨ T , pt ⟩ p-ca (abs-sum{A = A}{B = B} all abs-p1-b1 abs-p2-b2)
with all
... | sums ap
with ap pt
... | ⟨ T₁ , ⟨ T₂ , eq ⟩ ⟩ rewrite eq
with c-any-sum (p-ca pt)
... | inj₁ ⟨ A₁ , ⟨ A₂ , ⟨ a=a12 , ⟨ c1 , c2 ⟩ ⟩ ⟩ ⟩ rewrite a=a12 =
let ih1 = abs-optimal ⟨ T₁ , in-in₁ pt ⟩ domP⊆ca1 abs-p1-b1 in
let ih2 = abs-optimal ⟨ T₂ , in-in₂ pt ⟩ codP⊆ca2 abs-p2-b2 in
sum⊑ ih1 ih2
where domP⊆ca1 : In₁ P ⊆ Conc A₁
domP⊆ca1 {T'} (in-in₁ {T₂ = T₂} PT'⇒T2)
with p-ca PT'⇒T2
... | c-sum c-a1t' c-a2t2 = c-a1t'
codP⊆ca2 : In₂ P ⊆ Conc A₂
codP⊆ca2 {T'} (in-in₂ {T₁ = T₁} PT₁⇒T')
with p-ca PT₁⇒T'
... | c-sum c1 c2 = c2
... | inj₂ a=unk rewrite a=unk =
unk⊑
abs-optimal ⟨ T , pt ⟩ p-ca (abs-any a b c )
with conc-sh-cons (p-ca b) (p-ca c)
... | inj₁ A≡⋆ rewrite A≡⋆ =
unk⊑
... | inj₂ x =
contradiction x a
all-funs-conc⇒ : ∀{A B} → AllFuns (Conc (A ⇒ B))
all-funs-conc⇒{A}{B} = funs f
where f : {T : SType} → Conc (A ⇒ B) T →
Σ-syntax SType (λ T₁ → Σ-syntax SType (λ T₂ → T ≡ (T₁ ⇒ T₂)))
f {.(_ ⇒ _)} (c-fun{S₁ = S₁}{S₂ = S₂} c c₁) = ⟨ S₁ , ⟨ S₂ , refl ⟩ ⟩
all-pairs-conc× : ∀{A B} → AllPairs (Conc (A `× B))
all-pairs-conc×{A}{B} = pairs f
where f : {T : SType} → Conc (A `× B) T →
Σ-syntax SType (λ T₁ → Σ-syntax SType (λ T₂ → T ≡ (T₁ `× T₂)))
f {.(_ `× _)} (c-pair{S₁ = S₁}{S₂ = S₂} c c₁) = ⟨ S₁ , ⟨ S₂ , refl ⟩ ⟩
all-sums-conc⊎ : ∀{A B} → AllSums (Conc (A `⊎ B))
all-sums-conc⊎{A}{B} = sums f
where f : {T : SType} → Conc (A `⊎ B) T →
Σ-syntax SType (λ T₁ → Σ-syntax SType (λ T₂ → T ≡ (T₁ `⊎ T₂)))
f {.(_ `⊎ _)} (c-sum{S₁ = S₁}{S₂ = S₂} c c₁) = ⟨ S₁ , ⟨ S₂ , refl ⟩ ⟩
dom-conc⇒⊆ : ∀{A B} → Dom (Conc (A ⇒ B)) ⊆ Conc A
dom-conc⇒⊆ (in-dom (c-fun x x₁)) = x
proj₁-conc×⊆ : ∀{A B} → Proj₁ (Conc (A `× B)) ⊆ Conc A
proj₁-conc×⊆ (in-proj₁ (c-pair x x₁)) = x
in₁-conc⊎⊆ : ∀{A B} → In₁ (Conc (A `⊎ B)) ⊆ Conc A
in₁-conc⊎⊆ (in-in₁ (c-sum x x₁)) = x
cod-conc⇒⊆ : ∀{A B} → Cod (Conc (A ⇒ B)) ⊆ Conc B
cod-conc⇒⊆ (in-cod (c-fun x x₁)) = x₁
proj₂-conc×⊆ : ∀{A B} → Proj₂ (Conc (A `× B)) ⊆ Conc B
proj₂-conc×⊆ (in-proj₂ (c-pair x x₁)) = x₁
in₂-conc⊎⊆ : ∀{A B} → In₂ (Conc (A `⊎ B)) ⊆ Conc B
in₂-conc⊎⊆ (in-in₂ (c-sum x x₁)) = x₁
conc-dom⇒⊆ : ∀{A B} → Conc A ⊆ Dom (Conc (A ⇒ B))
conc-dom⇒⊆ {ι}{B} c-base with conc B
... | ⟨ B' , x ⟩ = in-dom (c-fun c-base x)
conc-dom⇒⊆ {B = B} (c-fun c c₁) with conc B
... | ⟨ B' , x ⟩ = in-dom (c-fun (c-fun c c₁) x)
conc-dom⇒⊆ {B = B} (c-pair c c₁) with conc B
... | ⟨ B' , x ⟩ = in-dom (c-fun (c-pair c c₁) x)
conc-dom⇒⊆ {B = B} (c-sum c c₁) with conc B
... | ⟨ B' , x ⟩ = in-dom (c-fun (c-sum c c₁) x)
conc-dom⇒⊆ {B = B} c-unk with conc B
... | ⟨ B' , x ⟩ = in-dom (c-fun c-unk x)
conc-proj₁×⊆ : ∀{A B} → Conc A ⊆ Proj₁ (Conc (A `× B))
conc-proj₁×⊆ {ι}{B} c-base with conc B
... | ⟨ B' , x ⟩ = in-proj₁ (c-pair c-base x)
conc-proj₁×⊆ {B = B} (c-fun c c₁) with conc B
... | ⟨ B' , x ⟩ = in-proj₁ (c-pair (c-fun c c₁) x)
conc-proj₁×⊆ {B = B} (c-pair c c₁) with conc B
... | ⟨ B' , x ⟩ = in-proj₁ (c-pair (c-pair c c₁) x)
conc-proj₁×⊆ {B = B} (c-sum c c₁) with conc B
... | ⟨ B' , x ⟩ = in-proj₁ (c-pair (c-sum c c₁) x)
conc-proj₁×⊆ {B = B} c-unk with conc B
... | ⟨ B' , x ⟩ = in-proj₁ (c-pair c-unk x)
conc-in₁⊎⊆ : ∀{A B} → Conc A ⊆ In₁ (Conc (A `⊎ B))
conc-in₁⊎⊆ {ι}{B} c-base with conc B
... | ⟨ B' , x ⟩ = in-in₁ (c-sum c-base x)
conc-in₁⊎⊆ {B = B} (c-fun c c₁) with conc B
... | ⟨ B' , x ⟩ = in-in₁ (c-sum (c-fun c c₁) x)
conc-in₁⊎⊆ {B = B} (c-pair c c₁) with conc B
... | ⟨ B' , x ⟩ = in-in₁ (c-sum (c-pair c c₁) x)
conc-in₁⊎⊆ {B = B} (c-sum c c₁) with conc B
... | ⟨ B' , x ⟩ = in-in₁ (c-sum (c-sum c c₁) x)
conc-in₁⊎⊆ {B = B} c-unk with conc B
... | ⟨ B' , x ⟩ = in-in₁ (c-sum c-unk x)
conc-cod⇒⊆ : ∀{A B} → Conc B ⊆ Cod (Conc (A ⇒ B))
conc-cod⇒⊆ {A} {.(` _)} c-base with conc A
... | ⟨ A' , x ⟩ = in-cod (c-fun x c-base)
conc-cod⇒⊆ {A} {.(_ ⇒ _)} (c-fun cb cb₁) with conc A
... | ⟨ A' , x ⟩ = in-cod (c-fun x (c-fun cb cb₁))
conc-cod⇒⊆ {A} {.(_ `× _)} (c-pair cb cb₁) with conc A
... | ⟨ A' , x ⟩ = in-cod (c-fun x (c-pair cb cb₁))
conc-cod⇒⊆ {A} {.(_ `⊎ _)} (c-sum cb cb₁) with conc A
... | ⟨ A' , x ⟩ = in-cod (c-fun x (c-sum cb cb₁))
conc-cod⇒⊆ {A} {.⋆} c-unk with conc A
... | ⟨ A' , x ⟩ = in-cod (c-fun x c-unk)
conc-proj₂×⊆ : ∀{A B} → Conc B ⊆ Proj₂ (Conc (A `× B))
conc-proj₂×⊆ {A} {.(` _)} c-base with conc A
... | ⟨ A' , x ⟩ = in-proj₂ (c-pair x c-base)
conc-proj₂×⊆ {A} {.(_ ⇒ _)} (c-fun cb cb₁) with conc A
... | ⟨ A' , x ⟩ = in-proj₂ (c-pair x (c-fun cb cb₁))
conc-proj₂×⊆ {A} {.(_ `× _)} (c-pair cb cb₁) with conc A
... | ⟨ A' , x ⟩ = in-proj₂ (c-pair x (c-pair cb cb₁))
conc-proj₂×⊆ {A} {.(_ `⊎ _)} (c-sum cb cb₁) with conc A
... | ⟨ A' , x ⟩ = in-proj₂ (c-pair x (c-sum cb cb₁))
conc-proj₂×⊆ {A} {.⋆} c-unk with conc A
... | ⟨ A' , x ⟩ = in-proj₂ (c-pair x c-unk)
conc-in₂⊎⊆ : ∀{A B} → Conc B ⊆ In₂ (Conc (A `⊎ B))
conc-in₂⊎⊆ {A} {.(` _)} c-base with conc A
... | ⟨ A' , x ⟩ = in-in₂ (c-sum x c-base)
conc-in₂⊎⊆ {A} {.(_ ⇒ _)} (c-fun cb cb₁) with conc A
... | ⟨ A' , x ⟩ = in-in₂ (c-sum x (c-fun cb cb₁))
conc-in₂⊎⊆ {A} {.(_ `× _)} (c-pair cb cb₁) with conc A
... | ⟨ A' , x ⟩ = in-in₂ (c-sum x (c-pair cb cb₁))
conc-in₂⊎⊆ {A} {.(_ `⊎ _)} (c-sum cb cb₁) with conc A
... | ⟨ A' , x ⟩ = in-in₂ (c-sum x (c-sum cb cb₁))
conc-in₂⊎⊆ {A} {.⋆} c-unk with conc A
... | ⟨ A' , x ⟩ = in-in₂ (c-sum x c-unk)
dom-conc⇒⇔ : ∀{A B} → Dom (Conc (A ⇒ B)) ⇔ Conc A
dom-conc⇒⇔ = ⟨ dom-conc⇒⊆ , conc-dom⇒⊆ ⟩
proj₁-conc×⇔ : ∀{A B} → Proj₁ (Conc (A `× B)) ⇔ Conc A
proj₁-conc×⇔ = ⟨ proj₁-conc×⊆ , conc-proj₁×⊆ ⟩
in₁-conc⊎⇔ : ∀{A B} → In₁ (Conc (A `⊎ B)) ⇔ Conc A
in₁-conc⊎⇔ = ⟨ in₁-conc⊎⊆ , conc-in₁⊎⊆ ⟩
cod-conc⇒⇔ : ∀{A B} → Cod (Conc (A ⇒ B)) ⇔ Conc B
cod-conc⇒⇔ = ⟨ cod-conc⇒⊆ , conc-cod⇒⊆ ⟩
proj₂-conc×⇔ : ∀{A B} → Proj₂ (Conc (A `× B)) ⇔ Conc B
proj₂-conc×⇔ = ⟨ proj₂-conc×⊆ , conc-proj₂×⊆ ⟩
in₂-conc⊎⇔ : ∀{A B} → In₂ (Conc (A `⊎ B)) ⇔ Conc B
in₂-conc⊎⇔ = ⟨ in₂-conc⊎⊆ , conc-in₂⊎⊆ ⟩
Sym⇔ : ∀{P Q} → P ⇔ Q → Q ⇔ P
Sym⇔ pq = ⟨ (proj₂ pq) , (proj₁ pq) ⟩
{-
Corollary abs-optimimal and conc-abs-sound:
α(γ(A)) = A
-}
conc-abs-id : ∀{A B : Type}{P : SType → Set}
→ Abs (Conc A) B
-------------------
→ A ≡ B
conc-abs-id {A}{B}{P} abs-conc-ab =
let A⊑B = (abs-optimal {Conc A}{A}{B} (conc A) (λ {T} z → z)) abs-conc-ab in
let B⊑A = prec-implies-le (prec (conc-abs-sound abs-conc-ab)) in
AntiSym⊑ A⊑B B⊑A
conc-abs-id2 : ∀{A : Type}{P : SType → Set}
→ Abs (Conc A) A
conc-abs-id2 {⋆} {P} = abs-any{S = ` Nat}{T = ` 𝔹} (λ ()) c-unk c-unk
conc-abs-id2 {` x} {P} = abs-base c-base G
where G : {T : SType} → Conc (` x) T → T ≡ (` x)
G {.(` _)} c-base = refl
conc-abs-id2 {A ⇒ B} {P} =
let x1 = Sym⇔ (dom-conc⇒⇔ {A} {B}) in
let ih1 = conc-abs-id2 {A} {P} in
let y1 = abs-equiv ih1 x1 in
let x2 = Sym⇔ (cod-conc⇒⇔ {A} {B}) in
let ih2 = conc-abs-id2 {B} {P} in
let y2 = abs-equiv ih2 x2 in
abs-fun all-funs-conc⇒ y1 y2
conc-abs-id2 {A `× B} {P} =
let x1 = Sym⇔ (proj₁-conc×⇔ {A} {B}) in
let ih1 = conc-abs-id2 {A} {P} in
let y1 = abs-equiv ih1 x1 in
let x2 = Sym⇔ (proj₂-conc×⇔ {A} {B}) in
let ih2 = conc-abs-id2 {B} {P} in
let y2 = abs-equiv ih2 x2 in
abs-pair all-pairs-conc× y1 y2
conc-abs-id2 {A `⊎ B} {P} =
let x1 = Sym⇔ (in₁-conc⊎⇔ {A} {B}) in
let ih1 = conc-abs-id2 {A} {P} in
let y1 = abs-equiv ih1 x1 in
let x2 = Sym⇔ (in₂-conc⊎⇔ {A} {B}) in
let ih2 = conc-abs-id2 {B} {P} in
let y2 = abs-equiv ih2 x2 in
abs-sum all-sums-conc⊎ y1 y2
{-
Def. of interior based on Prop 15 and a little subsequent reasoning.
-}
data L (P : SType → SType → Set) (G₁ : Type) (G₂ : Type) : SType → Set where
leftp : ∀{T₁ T₂ : SType}
→ Conc G₁ T₁ → Conc G₂ T₂ → P T₁ T₂
-------------------------------------
→ L P G₁ G₂ T₁
data R (P : SType → SType → Set) (G₁ : Type) (G₂ : Type) : SType → Set where
rightp : ∀{T₁ T₂ : SType}
→ Conc G₁ T₁ → Conc G₂ T₂ → P T₁ T₂
-------------------------------------
→ R P G₁ G₂ T₂
data Interior {n : Level} (P : SType → SType → Set)
: Type → Type → Type → Type → Set₁ where
inter : ∀{G₁ G₂ G₃ G₄}
→ Abs (L P G₁ G₂) G₃
→ Abs (R P G₁ G₂) G₄
----------------------
→ Interior P G₁ G₂ G₃ G₄
L⇒-intro : ∀{P : SType → SType → Set}{G₁₁ G₁₂ G₂₁ G₂₂ T₁ T₂}
→ (∀{T₁ T₂ T₃ T₄ : SType} → P T₁ T₃ → P T₂ T₄ → P (T₁ ⇒ T₂) (T₃ ⇒ T₄))
→ L P G₁₁ G₂₁ T₁ → L P G₁₂ G₂₂ T₂
→ L P (G₁₁ ⇒ G₁₂) (G₂₁ ⇒ G₂₂) (T₁ ⇒ T₂)
L⇒-intro p (leftp x x₁ x₂) (leftp x₃ x₄ x₅) =
leftp (c-fun x x₃) (c-fun x₁ x₄) (p x₂ x₅)
L⇒-elim : ∀{P : SType → SType → Set}{G₁₁ G₁₂ G₂₁ G₂₂ T₁ T₂}
→ (∀{T₁ T₂ T₃ T₄ : SType} → P (T₁ ⇒ T₂) (T₃ ⇒ T₄) → P T₁ T₃ × P T₂ T₄)
→ L P (G₁₁ ⇒ G₁₂) (G₂₁ ⇒ G₂₂) (T₁ ⇒ T₂)
→ L P G₁₁ G₂₁ T₁ × L P G₁₂ G₂₂ T₂
L⇒-elim p (leftp (c-fun x x₄) (c-fun x₁ x₃) x₂) =
⟨ (leftp x x₁ (proj₁ (p x₂))) , leftp x₄ x₃ (proj₂ (p x₂)) ⟩
data STypeEq (A : SType) (B : SType) : Set where
stype-eq : A ≡ B → STypeEq A B
L=→cc : ∀{G₁ G₂ T} → L STypeEq G₁ G₂ T → Conc G₁ T × Conc G₂ T
L=→cc (leftp x x₁ (stype-eq refl)) = ⟨ x , x₁ ⟩
cc→L= : ∀{G₁ G₂ T} → Conc G₁ T → Conc G₂ T → L STypeEq G₁ G₂ T
cc→L= g1t g2t = leftp g1t g2t (stype-eq refl)
L=→R= : ∀{G₁ G₂ T} → L STypeEq G₁ G₂ T → R STypeEq G₁ G₂ T
L=→R= (leftp x x₁ (stype-eq refl)) = rightp x x₁ (stype-eq refl)
R=→L= : ∀{G₁ G₂ T} → R STypeEq G₁ G₂ T → L STypeEq G₁ G₂ T
R=→L= (rightp x x₁ (stype-eq refl)) = leftp x x₁ (stype-eq refl)
L=⇔R= : ∀{G₁ G₂} → R STypeEq G₁ G₂ ⇔ L STypeEq G₁ G₂
L=⇔R= = ⟨ R=→L= , L=→R= ⟩
cct-consis : ∀{G1 G2 T} → Conc G1 T → Conc G2 T → G1 ~ G2
cct-consis c-base c-base = base~
cct-consis c-base c-unk = unk~R
cct-consis (c-fun c1t c1t₁) (c-fun c2t c2t₁) =
fun~ (cct-consis c2t c1t) (cct-consis c1t₁ c2t₁)
cct-consis (c-fun c1t c1t₁) c-unk = unk~R
cct-consis (c-pair c1t c1t₁) (c-pair c2t c2t₁) =
pair~ (cct-consis c1t c2t) (cct-consis c1t₁ c2t₁)
cct-consis (c-pair c1t c1t₁) c-unk = unk~R
cct-consis (c-sum c1t c1t₁) (c-sum c2t c2t₁) =
sum~ (cct-consis c1t c2t) (cct-consis c1t₁ c2t₁)
cct-consis (c-sum c1t c1t₁) c-unk = unk~R
cct-consis c-unk c2t = unk~L
cct-c⊔' : ∀{G1 G2 T} {c : G1 ~ G2} → (c1 : Conc G1 T) → (c2 : Conc G2 T)
→ Conc ((G1 ⊔ G2){c}) T
cct-c⊔' {` ι}{` ι}{c = c} c-base c-base with (` ι `⊔ ` ι){c}
... | ⟨ T , ⟨ ⟨ base⊑ , base⊑ ⟩ , b ⟩ ⟩ = c-base
cct-c⊔' {` ι}{⋆}{c = c} c-base c-unk with (` ι `⊔ ⋆){c}
... | ⟨ T , ⟨ ⟨ base⊑ , unk⊑ ⟩ , b ⟩ ⟩ = c-base
cct-c⊔'{c = fun~ c1 c2} (c-fun c1t c1t₁) (c-fun c2t c2t₁) =
c-fun (cct-c⊔' {c = c1} c2t c1t) (cct-c⊔' {c = c2} c1t₁ c2t₁)
cct-c⊔'{c = unk~R} (c-fun c1t c1t₁) c-unk = c-fun c1t c1t₁
cct-c⊔'{c = pair~ c1 c2} (c-pair c1t c1t₁) (c-pair c2t c2t₁) =
c-pair (cct-c⊔' {c = c1} c1t c2t) (cct-c⊔' {c = c2} c1t₁ c2t₁)
cct-c⊔'{c = unk~R} (c-pair c1t c1t₁) c-unk = c-pair c1t c1t₁
cct-c⊔'{c = sum~ c1 c2} (c-sum c1t c1t₁) (c-sum c2t c2t₁) =
c-sum (cct-c⊔' {c = c1} c1t c2t) (cct-c⊔' {c = c2} c1t₁ c2t₁)
cct-c⊔'{c = unk~R} (c-sum c1t c1t₁) c-unk = c-sum c1t c1t₁
cct-c⊔'{⋆}{G2}{c = unk~L} c-unk c2t with (⋆ `⊔ G2){unk~L}
... | ⟨ T , ⟨ ⟨ x , y ⟩ , b ⟩ ⟩ = c2t
cct-c⊔' {⋆} {⋆} {c = unk~R {⋆}} c-unk c-unk = c-unk
cct-c⊔ : ∀{G1 G2 T} → (c1 : Conc G1 T) → (c2 : Conc G2 T)
→ Conc ((G1 ⊔ G2){cct-consis c1 c2}) T
cct-c⊔ c-base c-base = c-base
cct-c⊔ c-base c-unk = c-base
cct-c⊔ (c-fun c1t c1t₁) (c-fun c2t c2t₁) =
c-fun (cct-c⊔ c2t c1t) (cct-c⊔ c1t₁ c2t₁)
cct-c⊔ (c-fun c1t c1t₁) c-unk = c-fun c1t c1t₁
cct-c⊔ (c-pair c1t c1t₁) (c-pair c2t c2t₁) =
c-pair (cct-c⊔ c1t c2t) (cct-c⊔ c1t₁ c2t₁)
cct-c⊔ (c-pair c1t c1t₁) c-unk = c-pair c1t c1t₁
cct-c⊔ (c-sum c1t c1t₁) (c-sum c2t c2t₁) =
c-sum (cct-c⊔ c1t c2t) (cct-c⊔ c1t₁ c2t₁)
cct-c⊔ (c-sum c1t c1t₁) c-unk = c-sum c1t c1t₁
cct-c⊔ c-unk c2t = c2t
c⊔-cct : ∀{G1 G2 T c} → Conc ((G1 ⊔ G2){c}) T
→ (Conc G1 T × Conc G2 T)
c⊔-cct {.⋆} {G2} {T} {unk~L} ct = ⟨ c-unk , ct ⟩
c⊔-cct {G1} {.⋆} {T} {unk~R} ct = ⟨ ct , c-unk ⟩
c⊔-cct {.(` _)} {.(` _)} {T} {base~} ct = ⟨ ct , ct ⟩