-
Notifications
You must be signed in to change notification settings - Fork 0
/
typ.agda
626 lines (557 loc) · 35.2 KB
/
typ.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
open import prelude
-- types
module core.typ where
data Typ : Set where
num : Typ
bool : Typ
unknown : Typ
_-→_ : Typ → Typ → Typ
_-×_ : Typ → Typ → Typ
infixr 25 _-→_
infixr 24 _-×_
module equality where
-- arrow type equality
-→-≡ : ∀ {τ₁ τ₂ τ₁′ τ₂′} → (τ₁ ≡ τ₁′) → (τ₂ ≡ τ₂′) → τ₁ -→ τ₂ ≡ τ₁′ -→ τ₂′
-→-≡ refl refl = refl
-- inverted arrow type equality
-→-inj : ∀ {τ₁ τ₂ τ₁′ τ₂′} → (τ₁ -→ τ₂ ≡ τ₁′ -→ τ₂′) → τ₁ ≡ τ₁′ × τ₂ ≡ τ₂′
-→-inj refl = ⟨ refl , refl ⟩
-- arrow type inequalities
-→-≢₁ : ∀ {τ₁ τ₂ τ₁′ τ₂′} → (τ₁ ≢ τ₁′) → τ₁ -→ τ₂ ≢ τ₁′ -→ τ₂′
-→-≢₁ τ₁≢τ₁′ refl = τ₁≢τ₁′ refl
-→-≢₂ : ∀ {τ₁ τ₂ τ₁′ τ₂′} → (τ₂ ≢ τ₂′) → τ₁ -→ τ₂ ≢ τ₁′ -→ τ₂′
-→-≢₂ τ₂≢τ₂′ refl = τ₂≢τ₂′ refl
-- product type equality
-×-≡ : ∀ {τ₁ τ₂ τ₁′ τ₂′} → (τ₁ ≡ τ₁′) → (τ₂ ≡ τ₂′) → τ₁ -× τ₂ ≡ τ₁′ -× τ₂′
-×-≡ refl refl = refl
-- inverted product type equality
-×-inj : ∀ {τ₁ τ₂ τ₁′ τ₂′} → (τ₁ -× τ₂ ≡ τ₁′ -× τ₂′) → τ₁ ≡ τ₁′ × τ₂ ≡ τ₂′
-×-inj refl = ⟨ refl , refl ⟩
-- product type inequalities
-×-≢₁ : ∀ {τ₁ τ₂ τ₁′ τ₂′} → (τ₁ ≢ τ₁′) → τ₁ -× τ₂ ≢ τ₁′ -× τ₂′
-×-≢₁ τ₁≢τ₁′ refl = τ₁≢τ₁′ refl
-×-≢₂ : ∀ {τ₁ τ₂ τ₁′ τ₂′} → (τ₂ ≢ τ₂′) → τ₁ -× τ₂ ≢ τ₁′ -× τ₂′
-×-≢₂ τ₂≢τ₂′ refl = τ₂≢τ₂′ refl
-- decidable equality
_≡τ?_ : (τ : Typ) → (τ′ : Typ) → Dec (τ ≡ τ′)
num ≡τ? num = yes refl
num ≡τ? bool = no (λ ())
num ≡τ? unknown = no (λ ())
num ≡τ? (_ -→ _) = no (λ ())
num ≡τ? (_ -× _) = no (λ ())
bool ≡τ? num = no (λ ())
bool ≡τ? bool = yes refl
bool ≡τ? unknown = no (λ ())
bool ≡τ? (_ -→ _) = no (λ ())
bool ≡τ? (_ -× _) = no (λ ())
unknown ≡τ? num = no (λ ())
unknown ≡τ? bool = no (λ ())
unknown ≡τ? unknown = yes refl
unknown ≡τ? (_ -→ _) = no (λ ())
unknown ≡τ? (_ -× _) = no (λ ())
(_ -→ _) ≡τ? num = no (λ ())
(_ -→ _) ≡τ? bool = no (λ ())
(_ -→ _) ≡τ? unknown = no (λ ())
(τ₁ -→ τ₂) ≡τ? (τ₁′ -→ τ₂′) with τ₁ ≡τ? τ₁′ | τ₂ ≡τ? τ₂′
... | yes refl | yes refl = yes refl
... | _ | no τ₂≢τ₂′ = no (-→-≢₂ τ₂≢τ₂′)
... | no τ₁≢τ₁′ | _ = no (-→-≢₁ τ₁≢τ₁′)
(_ -→ _) ≡τ? (_ -× _) = no (λ ())
(_ -× _) ≡τ? num = no (λ ())
(_ -× _) ≡τ? bool = no (λ ())
(_ -× _) ≡τ? unknown = no (λ ())
(_ -× _) ≡τ? (_ -→ _) = no (λ ())
(τ₁ -× τ₂) ≡τ? (τ₁′ -× τ₂′) with τ₁ ≡τ? τ₁′ | τ₂ ≡τ? τ₂′
... | yes refl | yes refl = yes refl
... | _ | no τ₂≢τ₂′ = no (-×-≢₂ τ₂≢τ₂′)
... | no τ₁≢τ₁′ | _ = no (-×-≢₁ τ₁≢τ₁′)
module base where
-- base types
data _base : (τ : Typ) → Set where
TBNum : num base
TBBool : bool base
-- decidable base
_base? : (τ : Typ) → Dec (τ base)
num base? = yes TBNum
bool base? = yes TBBool
unknown base? = no (λ ())
(_ -→ _) base? = no (λ ())
(_ -× _) base? = no (λ ())
-- base judgment equality
base-≡ : ∀ {τ} → (b₁ : τ base) → (b₂ : τ base) → b₁ ≡ b₂
base-≡ TBNum TBNum = refl
base-≡ TBBool TBBool = refl
module consistency where
open base
-- consistency
data _~_ : (τ₁ τ₂ : Typ) → Set where
TCUnknown : unknown ~ unknown
TCBase : {τ : Typ} → (b : τ base) → τ ~ τ
TCUnknownBase : {τ : Typ} → (b : τ base) → unknown ~ τ
TCBaseUnknown : {τ : Typ} → (b : τ base) → τ ~ unknown
TCArr : {τ₁ τ₂ τ₁′ τ₂′ : Typ}
→ (τ₁~τ₁′ : τ₁ ~ τ₁′)
→ (τ₂~τ₂′ : τ₂ ~ τ₂′)
→ τ₁ -→ τ₂ ~ τ₁′ -→ τ₂′
TCUnknownArr : {τ₁ τ₂ : Typ}
→ unknown ~ τ₁ -→ τ₂
TCArrUnknown : {τ₁ τ₂ : Typ}
→ τ₁ -→ τ₂ ~ unknown
TCProd : {τ₁ τ₂ τ₁′ τ₂′ : Typ}
→ (τ₁~τ₁′ : τ₁ ~ τ₁′)
→ (τ₂~τ₂′ : τ₂ ~ τ₂′)
→ τ₁ -× τ₂ ~ τ₁′ -× τ₂′
TCUnknownProd : {τ₁ τ₂ : Typ}
→ unknown ~ τ₁ -× τ₂
TCProdUnknown : {τ₁ τ₂ : Typ}
→ τ₁ -× τ₂ ~ unknown
-- inconsistency
_~̸_ : (τ₁ : Typ) → (τ₂ : Typ) → Set
τ₁ ~̸ τ₂ = ¬ (τ₁ ~ τ₂)
-- consistency reflexivity
~-refl : ∀ {τ} → τ ~ τ
~-refl {num} = TCBase TBNum
~-refl {bool} = TCBase TBBool
~-refl {unknown} = TCUnknown
~-refl {τ₁ -→ τ₂} = TCArr ~-refl ~-refl
~-refl {τ₁ -× τ₂} = TCProd ~-refl ~-refl
-- consistency symmetry
~-sym : ∀ {τ₁ τ₂} → τ₁ ~ τ₂ → τ₂ ~ τ₁
~-sym TCUnknown = TCUnknown
~-sym (TCBase b) = TCBase b
~-sym (TCUnknownBase b) = TCBaseUnknown b
~-sym (TCBaseUnknown b) = TCUnknownBase b
~-sym TCUnknownArr = TCArrUnknown
~-sym TCArrUnknown = TCUnknownArr
~-sym (TCArr τ₁~τ₁′ τ₂~τ₂′) = TCArr (~-sym τ₁~τ₁′) (~-sym τ₂~τ₂′)
~-sym (TCProd τ₁~τ₁′ τ₂~τ₂′) = TCProd (~-sym τ₁~τ₁′) (~-sym τ₂~τ₂′)
~-sym TCUnknownProd = TCProdUnknown
~-sym TCProdUnknown = TCUnknownProd
-- consistency with unknown type
~-unknown₁ : ∀ {τ} → unknown ~ τ
~-unknown₁ {num} = TCUnknownBase TBNum
~-unknown₁ {bool} = TCUnknownBase TBBool
~-unknown₁ {unknown} = TCUnknown
~-unknown₁ {_ -→ _} = TCUnknownArr
~-unknown₁ {_ -× _} = TCUnknownProd
~-unknown₂ : ∀ {τ} → τ ~ unknown
~-unknown₂ {num} = TCBaseUnknown TBNum
~-unknown₂ {bool} = TCBaseUnknown TBBool
~-unknown₂ {unknown} = TCUnknown
~-unknown₂ {_ -→ _} = TCArrUnknown
~-unknown₂ {_ -× _} = TCProdUnknown
-- consistency derivation equality
~-≡ : ∀ {τ₁ τ₂} → (τ₁~τ₂ : τ₁ ~ τ₂) → (τ₁~τ₂′ : τ₁ ~ τ₂) → τ₁~τ₂ ≡ τ₁~τ₂′
~-≡ TCUnknown TCUnknown = refl
~-≡ (TCBase b) (TCBase b′)
rewrite base-≡ b b′ = refl
~-≡ (TCUnknownBase b) (TCUnknownBase b′)
rewrite base-≡ b b′ = refl
~-≡ (TCBaseUnknown b) (TCBaseUnknown b′)
rewrite base-≡ b b′ = refl
~-≡ (TCArr τ₁~τ₁′ τ₂~τ₂′) (TCArr τ₁~τ₁′′ τ₂~τ₂′′)
rewrite ~-≡ τ₁~τ₁′ τ₁~τ₁′′ | ~-≡ τ₂~τ₂′ τ₂~τ₂′′ = refl
~-≡ TCUnknownArr TCUnknownArr = refl
~-≡ TCArrUnknown TCArrUnknown = refl
~-≡ (TCProd τ₁~τ₁′ τ₂~τ₂′) (TCProd τ₁~τ₁′′ τ₂~τ₂′′)
rewrite ~-≡ τ₁~τ₁′ τ₁~τ₁′′ | ~-≡ τ₂~τ₂′ τ₂~τ₂′′ = refl
~-≡ TCUnknownProd TCUnknownProd = refl
~-≡ TCProdUnknown TCProdUnknown = refl
-- inconsistency derivation equality
~̸-≡ : ∀ {τ₁ τ₂} → (τ₁~̸τ₂ : τ₁ ~̸ τ₂) → (τ₁~̸τ₂′ : τ₁ ~̸ τ₂) → τ₁~̸τ₂ ≡ τ₁~̸τ₂′
~̸-≡ τ₁~̸τ₂ τ₁~̸τ₂′ rewrite ¬-≡ τ₁~̸τ₂ τ₁~̸τ₂′ = refl
-- arrow type inconsistencies
-→-~̸₁ : ∀ {τ₁ τ₂ τ₁′ τ₂′} → τ₁ ~̸ τ₁′ → τ₁ -→ τ₂ ~̸ τ₁′ -→ τ₂′
-→-~̸₁ τ₁~̸τ₁′ = λ { (TCBase ()) ; (TCArr τ₁~τ₁′ _) → τ₁~̸τ₁′ τ₁~τ₁′ }
-→-~̸₂ : ∀ {τ₁ τ₂ τ₁′ τ₂′} → τ₂ ~̸ τ₂′ → τ₁ -→ τ₂ ~̸ τ₁′ -→ τ₂′
-→-~̸₂ τ₂~̸τ₂′ = λ { (TCBase ()) ; (TCArr _ τ₂~τ₂′) → τ₂~̸τ₂′ τ₂~τ₂′ }
-- decidable consistency
_~?_ : (τ : Typ) → (τ′ : Typ) → Dec (τ ~ τ′)
unknown ~? num = yes (TCUnknownBase TBNum)
unknown ~? bool = yes (TCUnknownBase TBBool)
unknown ~? unknown = yes TCUnknown
unknown ~? (_ -→ _) = yes TCUnknownArr
unknown ~? (_ -× _) = yes TCUnknownProd
num ~? num = yes (TCBase TBNum)
num ~? bool = no (λ ())
num ~? unknown = yes (TCBaseUnknown TBNum)
num ~? (_ -→ _) = no (λ ())
num ~? (_ -× _) = no (λ ())
bool ~? num = no (λ ())
bool ~? bool = yes (TCBase TBBool)
bool ~? unknown = yes (TCBaseUnknown TBBool)
bool ~? (_ -→ _) = no (λ ())
bool ~? (_ -× _) = no (λ ())
(_ -→ _) ~? num = no (λ ())
(_ -→ _) ~? bool = no (λ ())
(_ -→ _) ~? unknown = yes TCArrUnknown
(τ₁ -→ τ₂) ~? (τ₁′ -→ τ₂′) with τ₁ ~? τ₁′ | τ₂ ~? τ₂′
... | yes τ₁~τ₁′ | yes τ₂~τ₂′ = yes (TCArr τ₁~τ₁′ τ₂~τ₂′)
... | _ | no ¬τ₂~τ₂′ = no λ { (TCBase ()) ; (TCArr _ τ₂~τ₂′) → ¬τ₂~τ₂′ τ₂~τ₂′ }
... | no ¬τ₁~τ₁′ | _ = no λ { (TCBase ()) ; (TCArr τ₁~τ₁′ _) → ¬τ₁~τ₁′ τ₁~τ₁′ }
(_ -→ _) ~? (_ -× _) = no (λ ())
(_ -× _) ~? num = no (λ ())
(_ -× _) ~? bool = no (λ ())
(_ -× _) ~? unknown = yes TCProdUnknown
(_ -× _) ~? (_ -→ _) = no (λ ())
(τ₁ -× τ₂) ~? (τ₁′ -× τ₂′) with τ₁ ~? τ₁′ | τ₂ ~? τ₂′
... | yes τ₁~τ₁′ | yes τ₂~τ₂′ = yes (TCProd τ₁~τ₁′ τ₂~τ₂′)
... | _ | no ¬τ₂~τ₂′ = no λ { (TCBase ()) ; (TCProd _ τ₂~τ₂′) → ¬τ₂~τ₂′ τ₂~τ₂′ }
... | no ¬τ₁~τ₁′ | _ = no λ { (TCBase ()) ; (TCProd τ₁~τ₁′ _) → ¬τ₁~τ₁′ τ₁~τ₁′ }
module formalism where
data _~′_ : (τ₁ τ₂ : Typ) → Set where
TCUnknown1 : {τ : Typ} → unknown ~′ τ
TCUnknown2 : {τ : Typ} → τ ~′ unknown
TCRefl : {τ : Typ} → τ ~′ τ
TCArr : {τ₁ τ₁′ τ₂ τ₂′ : Typ}
→ (τ₁~τ₁′ : τ₁ ~′ τ₁′)
→ (τ₂~τ₂′ : τ₂ ~′ τ₂′)
→ τ₁ -→ τ₂ ~′ τ₁′ -→ τ₂′
TCProd : {τ₁ τ₁′ τ₂ τ₂′ : Typ}
→ (τ₁~τ₁′ : τ₁ ~′ τ₁′)
→ (τ₂~τ₂′ : τ₂ ~′ τ₂′)
→ τ₁ -× τ₂ ~′ τ₁′ -× τ₂′
~→~′ : ∀ {τ₁ τ₂ : Typ} → τ₁ ~ τ₂ → τ₁ ~′ τ₂
~→~′ TCUnknown = TCRefl
~→~′ (TCBase TBNum) = TCRefl
~→~′ (TCBase TBBool) = TCRefl
~→~′ (TCUnknownBase b) = TCUnknown1
~→~′ (TCBaseUnknown b) = TCUnknown2
~→~′ (TCArr τ₁~τ₁′ τ₂~τ₂′)
with τ₁~τ₁′′ ← ~→~′ τ₁~τ₁′
| τ₂~τ₂′′ ← ~→~′ τ₂~τ₂′ = TCArr τ₁~τ₁′′ τ₂~τ₂′′
~→~′ TCUnknownArr = TCUnknown1
~→~′ TCArrUnknown = TCUnknown2
~→~′ (TCProd τ₁~τ₁′ τ₂~τ₂′)
with τ₁~τ₁′′ ← ~→~′ τ₁~τ₁′
| τ₂~τ₂′′ ← ~→~′ τ₂~τ₂′ = TCProd τ₁~τ₁′′ τ₂~τ₂′′
~→~′ TCUnknownProd = TCUnknown1
~→~′ TCProdUnknown = TCUnknown2
~′→~ : ∀ {τ₁ τ₂ : Typ} → τ₁ ~′ τ₂ → τ₁ ~ τ₂
~′→~ {τ₂ = num} TCUnknown1 = TCUnknownBase TBNum
~′→~ {τ₂ = bool} TCUnknown1 = TCUnknownBase TBBool
~′→~ {τ₂ = unknown} TCUnknown1 = TCUnknown
~′→~ {τ₂ = _ -→ _} TCUnknown1 = TCUnknownArr
~′→~ {τ₂ = _ -× _} TCUnknown1 = TCUnknownProd
~′→~ {τ₁ = num} TCUnknown2 = TCBaseUnknown TBNum
~′→~ {τ₁ = bool} TCUnknown2 = TCBaseUnknown TBBool
~′→~ {τ₁ = unknown} TCUnknown2 = TCUnknown
~′→~ {τ₁ = _ -→ _} TCUnknown2 = TCArrUnknown
~′→~ {τ₁ = _ -× _} TCUnknown2 = TCProdUnknown
~′→~ {τ₁ = num} TCRefl = TCBase TBNum
~′→~ {τ₁ = bool} TCRefl = TCBase TBBool
~′→~ {τ₁ = unknown} TCRefl = TCUnknown
~′→~ {τ₁ = _ -→ _} TCRefl = TCArr (~′→~ TCRefl) (~′→~ TCRefl)
~′→~ {τ₁ = _ -× _} TCRefl = TCProd (~′→~ TCRefl) (~′→~ TCRefl)
~′→~ (TCArr τ₁~τ₁′ τ₂~τ₂′) = TCArr (~′→~ τ₁~τ₁′) (~′→~ τ₂~τ₂′)
~′→~ (TCProd τ₁~τ₁′ τ₂~τ₂′) = TCProd (~′→~ τ₁~τ₁′) (~′→~ τ₂~τ₂′)
~⇔~′ : ∀ {τ₁ τ₂ : Typ} → (τ₁ ~ τ₂) ⇔ (τ₁ ~′ τ₂)
~⇔~′ =
record
{ to = ~→~′
; from = ~′→~
}
module matched where
open equality
open consistency
-- matched arrow
data _▸_-→_ : (τ τ₁ τ₂ : Typ) → Set where
TMAUnknown : unknown ▸ unknown -→ unknown
TMAArr : {τ₁ τ₂ : Typ} → τ₁ -→ τ₂ ▸ τ₁ -→ τ₂
-- no matched
_!▸-→ : Typ → Set
τ !▸-→ = ¬ (∃[ τ₁ ] ∃[ τ₂ ] τ ▸ τ₁ -→ τ₂)
-- decidable matched arrow
_▸-→? : (τ : Typ) → Dec (∃[ τ₁ ] ∃[ τ₂ ] τ ▸ τ₁ -→ τ₂)
num ▸-→? = no (λ ())
bool ▸-→? = no (λ ())
unknown ▸-→? = yes ⟨ unknown , ⟨ unknown , TMAUnknown ⟩ ⟩
(τ₁ -→ τ₂) ▸-→? = yes ⟨ τ₁ , ⟨ τ₂ , TMAArr ⟩ ⟩
(τ₁ -× τ₂) ▸-→? = no (λ ())
-- matched arrow derivation equality
▸-→-≡ : ∀ {τ τ₁ τ₂} → (τ▸ : τ ▸ τ₁ -→ τ₂) → (τ▸′ : τ ▸ τ₁ -→ τ₂) → τ▸ ≡ τ▸′
▸-→-≡ TMAUnknown TMAUnknown = refl
▸-→-≡ TMAArr TMAArr = refl
-- matched arrow unicity
▸-→-unicity : ∀ {τ τ₁ τ₂ τ₃ τ₄} → (τ ▸ τ₁ -→ τ₂) → (τ ▸ τ₃ -→ τ₄) → τ₁ -→ τ₂ ≡ τ₃ -→ τ₄
▸-→-unicity TMAUnknown TMAUnknown = refl
▸-→-unicity TMAArr TMAArr = refl
-- no matched arrow derivation equality
!▸-→-≡ : ∀ {τ} → (τ!▸ : τ !▸-→) → (τ!▸′ : τ !▸-→) → τ!▸ ≡ τ!▸′
!▸-→-≡ τ!▸ τ!▸′ = ¬-≡ τ!▸ τ!▸′
-- only consistent types arrow match
▸-→→~ : ∀ {τ τ₁ τ₂} → τ ▸ τ₁ -→ τ₂ → τ ~ τ₁ -→ τ₂
▸-→→~ TMAUnknown = TCUnknownArr
▸-→→~ TMAArr = ~-refl
▸-→-~̸₁ : ∀ {τ τ₁ τ₂ τ₁′} → τ ▸ τ₁ -→ τ₂ → τ₁′ ~̸ τ₁ → τ ~̸ τ₁′ -→ τ₂
▸-→-~̸₁ TMAArr τ₁′~̸τ₁ (TCArr τ₁~τ₁′ _) = τ₁′~̸τ₁ (~-sym τ₁~τ₁′)
▸-→-~̸₁ TMAUnknown τ₁′~̸τ₁ TCUnknownArr = τ₁′~̸τ₁ ~-unknown₂
▸-→-~̸₂ : ∀ {τ τ₁ τ₂ τ₂′} → τ ▸ τ₁ -→ τ₂ → τ₂′ ~̸ τ₂ → τ ~̸ τ₁ -→ τ₂′
▸-→-~̸₂ TMAUnknown τ₂′~̸τ₂ TCUnknownArr = τ₂′~̸τ₂ ~-unknown₂
▸-→-~̸₂ TMAArr τ₂′~̸τ₂ (TCArr _ τ₂~τ₂′) = τ₂′~̸τ₂ (~-sym τ₂~τ₂′)
-- consistency with an arrow type implies existence of a matched arrow type
~→▸-→ : ∀ {τ τ₁ τ₂} → τ ~ τ₁ -→ τ₂ → ∃[ τ₁′ ] ∃[ τ₂′ ] τ ▸ τ₁′ -→ τ₂′
~→▸-→ (TCArr {τ₁ = τ₁} {τ₂ = τ₂} τ₁~ τ₂~) = ⟨ τ₁ , ⟨ τ₂ , TMAArr ⟩ ⟩
~→▸-→ TCUnknownArr = ⟨ unknown , ⟨ unknown , TMAUnknown ⟩ ⟩
~-▸-→→~ : ∀ {τ τ₁ τ₂ τ₁′ τ₂′} → τ ~ τ₁ -→ τ₂ → τ ▸ τ₁′ -→ τ₂′ → τ₁ -→ τ₂ ~ τ₁′ -→ τ₂′
~-▸-→→~ (TCArr τ₁~ τ₂~) TMAArr = TCArr (~-sym τ₁~) (~-sym τ₂~)
~-▸-→→~ TCUnknownArr TMAUnknown = TCArr ~-unknown₂ ~-unknown₂
-- matched product
data _▸_-×_ : (τ τ₁ τ₂ : Typ) → Set where
TMPUnknown : unknown ▸ unknown -× unknown
TMPProd : {τ₁ τ₂ : Typ} → τ₁ -× τ₂ ▸ τ₁ -× τ₂
-- no matched
_!▸-× : Typ → Set
τ !▸-× = ¬ (∃[ τ₁ ] ∃[ τ₂ ] τ ▸ τ₁ -× τ₂)
-- decidable matched product
_▸-×? : (τ : Typ) → Dec (∃[ τ₁ ] ∃[ τ₂ ] τ ▸ τ₁ -× τ₂)
num ▸-×? = no (λ ())
bool ▸-×? = no (λ ())
unknown ▸-×? = yes ⟨ unknown , ⟨ unknown , TMPUnknown ⟩ ⟩
(τ₁ -→ τ₂) ▸-×? = no (λ ())
(τ₁ -× τ₂) ▸-×? = yes ⟨ τ₁ , ⟨ τ₂ , TMPProd ⟩ ⟩
-- matched product derivation equality
▸-×-≡ : ∀ {τ τ₁ τ₂} → (τ▸ : τ ▸ τ₁ -× τ₂) → (τ▸′ : τ ▸ τ₁ -× τ₂) → τ▸ ≡ τ▸′
▸-×-≡ TMPUnknown TMPUnknown = refl
▸-×-≡ TMPProd TMPProd = refl
-- matched product unicity
▸-×-unicity : ∀ {τ τ₁ τ₂ τ₃ τ₄} → (τ ▸ τ₁ -× τ₂) → (τ ▸ τ₃ -× τ₄) → τ₁ -× τ₂ ≡ τ₃ -× τ₄
▸-×-unicity TMPUnknown TMPUnknown = refl
▸-×-unicity TMPProd TMPProd = refl
-- no matched product derivation equality
!▸-×-≡ : ∀ {τ} → (τ!▸ : τ !▸-×) → (τ!▸′ : τ !▸-×) → τ!▸ ≡ τ!▸′
!▸-×-≡ τ!▸ τ!▸′ = ¬-≡ τ!▸ τ!▸′
-- only consistent types product match
▸-×→~ : ∀ {τ τ₁ τ₂} → τ ▸ τ₁ -× τ₂ → τ ~ τ₁ -× τ₂
▸-×→~ TMPUnknown = TCUnknownProd
▸-×→~ TMPProd = ~-refl
▸-×-~̸₁ : ∀ {τ τ₁ τ₂ τ₁′} → τ ▸ τ₁ -× τ₂ → τ₁′ ~̸ τ₁ → τ ~̸ τ₁′ -× τ₂
▸-×-~̸₁ TMPProd τ₁′~̸τ₁ (TCProd τ₁~τ₁′ _) = τ₁′~̸τ₁ (~-sym τ₁~τ₁′)
▸-×-~̸₁ TMPUnknown τ₁′~̸τ₁ TCUnknownProd = τ₁′~̸τ₁ ~-unknown₂
▸-×-~̸₂ : ∀ {τ τ₁ τ₂ τ₂′} → τ ▸ τ₁ -× τ₂ → τ₂′ ~̸ τ₂ → τ ~̸ τ₁ -× τ₂′
▸-×-~̸₂ TMPUnknown τ₂′~̸τ₂ TCUnknownProd = τ₂′~̸τ₂ ~-unknown₂
▸-×-~̸₂ TMPProd τ₂′~̸τ₂ (TCProd _ τ₂~τ₂′) = τ₂′~̸τ₂ (~-sym τ₂~τ₂′)
-- consistency with an product type implies existence of a matched product type
~→▸-× : ∀ {τ τ₁ τ₂} → τ ~ τ₁ -× τ₂ → ∃[ τ₁′ ] ∃[ τ₂′ ] τ ▸ τ₁′ -× τ₂′
~→▸-× (TCProd {τ₁ = τ₁} {τ₂ = τ₂} τ₁~ τ₂~) = ⟨ τ₁ , ⟨ τ₂ , TMPProd ⟩ ⟩
~→▸-× TCUnknownProd = ⟨ unknown , ⟨ unknown , TMPUnknown ⟩ ⟩
~-▸-×→~ : ∀ {τ τ₁ τ₂ τ₁′ τ₂′} → τ ~ τ₁ -× τ₂ → τ ▸ τ₁′ -× τ₂′ → τ₁ -× τ₂ ~ τ₁′ -× τ₂′
~-▸-×→~ (TCProd τ₁~ τ₂~) TMPProd = TCProd (~-sym τ₁~) (~-sym τ₂~)
~-▸-×→~ TCUnknownProd TMPUnknown = TCProd ~-unknown₂ ~-unknown₂
module meet where
open base
open equality
open consistency
-- greatest lower bound (where the unknown type is the top of the imprecision partial order)
data _⊓_⇒_ : (τ₁ τ₂ τ : Typ) → Set where
TJBase : {τ : Typ} → (b : τ base) → τ ⊓ τ ⇒ τ
TJUnknown : unknown ⊓ unknown ⇒ unknown
TJUnknownBase : {τ : Typ} → (b : τ base) → unknown ⊓ τ ⇒ τ
TJBaseUnknown : {τ : Typ} → (b : τ base) → τ ⊓ unknown ⇒ τ
TJArr : {τ₁ τ₂ τ₁′ τ₂′ τ₁″ τ₂″ : Typ}
→ (τ₁⊓τ₁′ : τ₁ ⊓ τ₁′ ⇒ τ₁″)
→ (τ₂⊓τ₂′ : τ₂ ⊓ τ₂′ ⇒ τ₂″)
→ τ₁ -→ τ₂ ⊓ τ₁′ -→ τ₂′ ⇒ τ₁″ -→ τ₂″
TJUnknownArr : {τ₁ τ₂ : Typ}
→ unknown ⊓ τ₁ -→ τ₂ ⇒ τ₁ -→ τ₂
TJArrUnknown : {τ₁ τ₂ : Typ}
→ τ₁ -→ τ₂ ⊓ unknown ⇒ τ₁ -→ τ₂
TJProd : {τ₁ τ₂ τ₁′ τ₂′ τ₁″ τ₂″ : Typ}
→ (τ₁⊓τ₁′ : τ₁ ⊓ τ₁′ ⇒ τ₁″)
→ (τ₂⊓τ₂′ : τ₂ ⊓ τ₂′ ⇒ τ₂″)
→ τ₁ -× τ₂ ⊓ τ₁′ -× τ₂′ ⇒ τ₁″ -× τ₂″
TJUnknownProd : {τ₁ τ₂ : Typ}
→ unknown ⊓ τ₁ -× τ₂ ⇒ τ₁ -× τ₂
TJProdUnknown : {τ₁ τ₂ : Typ}
→ τ₁ -× τ₂ ⊓ unknown ⇒ τ₁ -× τ₂
-- decidable meet
_⊓?_ : (τ₁ : Typ) → (τ₂ : Typ) → Dec (∃[ τ ] τ₁ ⊓ τ₂ ⇒ τ)
num ⊓? num = yes ⟨ num , TJBase TBNum ⟩
num ⊓? bool = no λ()
num ⊓? unknown = yes ⟨ num , TJBaseUnknown TBNum ⟩
num ⊓? (_ -→ _) = no λ()
num ⊓? (_ -× _) = no λ()
bool ⊓? num = no λ()
bool ⊓? bool = yes ⟨ bool , TJBase TBBool ⟩
bool ⊓? unknown = yes ⟨ bool , TJBaseUnknown TBBool ⟩
bool ⊓? (_ -→ _) = no λ()
bool ⊓? (_ -× _) = no λ()
unknown ⊓? num = yes ⟨ num , TJUnknownBase TBNum ⟩
unknown ⊓? bool = yes ⟨ bool , TJUnknownBase TBBool ⟩
unknown ⊓? unknown = yes ⟨ unknown , TJUnknown ⟩
unknown ⊓? (τ₁ -→ τ₂) = yes ⟨ τ₁ -→ τ₂ , TJUnknownArr ⟩
unknown ⊓? (τ₁ -× τ₂) = yes ⟨ τ₁ -× τ₂ , TJUnknownProd ⟩
(τ₁ -→ τ₂) ⊓? num = no λ()
(τ₁ -→ τ₂) ⊓? bool = no λ()
(τ₁ -→ τ₂) ⊓? unknown = yes ⟨ τ₁ -→ τ₂ , TJArrUnknown ⟩
(τ₁ -→ τ₂) ⊓? (τ₁′ -→ τ₂′)
with τ₁ ⊓? τ₁′ | τ₂ ⊓? τ₂′
... | yes ⟨ τ , τ₁⊓τ₁′ ⟩ | yes ⟨ τ′ , τ₂⊓τ₂′′ ⟩ = yes ⟨ τ -→ τ′ , TJArr τ₁⊓τ₁′ τ₂⊓τ₂′′ ⟩
... | _ | no ¬τ₂⊓τ₂′ = no λ { ⟨ .(_ -→ _) , TJArr {τ₂″ = τ₂″} τ₁⊓τ₁′″ τ₂⊓τ₂′″ ⟩ → ¬τ₂⊓τ₂′ ⟨ τ₂″ , τ₂⊓τ₂′″ ⟩ }
... | no ¬τ₁⊓τ₁′ | _ = no λ { ⟨ .(_ -→ _) , TJArr {τ₁″ = τ₁″} τ₁⊓τ₁′″ τ₂⊓τ₂′″ ⟩ → ¬τ₁⊓τ₁′ ⟨ τ₁″ , τ₁⊓τ₁′″ ⟩ }
(τ₁ -→ τ₂) ⊓? (τ₁′ -× τ₂′) = no λ()
(τ₁ -× τ₂) ⊓? num = no λ()
(τ₁ -× τ₂) ⊓? bool = no λ()
(τ₁ -× τ₂) ⊓? unknown = yes ⟨ τ₁ -× τ₂ , TJProdUnknown ⟩
(τ₁ -× τ₂) ⊓? (τ₁′ -→ τ₂′) = no λ()
(τ₁ -× τ₂) ⊓? (τ₁′ -× τ₂′)
with τ₁ ⊓? τ₁′ | τ₂ ⊓? τ₂′
... | yes ⟨ τ , τ₁⊓τ₁′ ⟩ | yes ⟨ τ′ , τ₂⊓τ₂′′ ⟩ = yes ⟨ τ -× τ′ , TJProd τ₁⊓τ₁′ τ₂⊓τ₂′′ ⟩
... | _ | no ¬τ₂⊓τ₂′ = no λ { ⟨ .(_ -× _) , TJProd {τ₂″ = τ₂″} τ₁⊓τ₁′″ τ₂⊓τ₂′″ ⟩ → ¬τ₂⊓τ₂′ ⟨ τ₂″ , τ₂⊓τ₂′″ ⟩ }
... | no ¬τ₁⊓τ₁′ | _ = no λ { ⟨ .(_ -× _) , TJProd {τ₁″ = τ₁″} τ₁⊓τ₁′″ τ₂⊓τ₂′″ ⟩ → ¬τ₁⊓τ₁′ ⟨ τ₁″ , τ₁⊓τ₁′″ ⟩ }
-- meet of same type
⊓-refl : ∀ {τ} → τ ⊓ τ ⇒ τ
⊓-refl {num} = TJBase TBNum
⊓-refl {bool} = TJBase TBBool
⊓-refl {unknown} = TJUnknown
⊓-refl {τ₁ -→ τ₂}
with τ₁⊓τ₁ ← ⊓-refl {τ₁}
| τ₂⊓τ₂ ← ⊓-refl {τ₂}
= TJArr τ₁⊓τ₁ τ₂⊓τ₂
⊓-refl {τ₁ -× τ₂}
with τ₁⊓τ₁ ← ⊓-refl {τ₁}
| τ₂⊓τ₂ ← ⊓-refl {τ₂}
= TJProd τ₁⊓τ₁ τ₂⊓τ₂
-- meet is symmetric
⊓-sym : ∀ {τ₁ τ₂ τ} → τ₁ ⊓ τ₂ ⇒ τ → τ₂ ⊓ τ₁ ⇒ τ
⊓-sym (TJBase b) = TJBase b
⊓-sym TJUnknown = TJUnknown
⊓-sym (TJUnknownBase b) = TJBaseUnknown b
⊓-sym (TJBaseUnknown b) = TJUnknownBase b
⊓-sym TJUnknownArr = TJArrUnknown
⊓-sym TJArrUnknown = TJUnknownArr
⊓-sym (TJArr ⊓⇒τ₁″ ⊓⇒τ₂″) = TJArr (⊓-sym ⊓⇒τ₁″) (⊓-sym ⊓⇒τ₂″)
⊓-sym TJUnknownProd = TJProdUnknown
⊓-sym TJProdUnknown = TJUnknownProd
⊓-sym (TJProd ⊓⇒τ₁″ ⊓⇒τ₂″) = TJProd (⊓-sym ⊓⇒τ₁″) (⊓-sym ⊓⇒τ₂″)
-- meet with unknown
⊓-unknown₁ : ∀ {τ} → unknown ⊓ τ ⇒ τ
⊓-unknown₁ {num} = TJUnknownBase TBNum
⊓-unknown₁ {bool} = TJUnknownBase TBBool
⊓-unknown₁ {unknown} = TJUnknown
⊓-unknown₁ {_ -→ _} = TJUnknownArr
⊓-unknown₁ {_ -× _} = TJUnknownProd
⊓-unknown₂ : ∀ {τ} → τ ⊓ unknown ⇒ τ
⊓-unknown₂ {num} = TJBaseUnknown TBNum
⊓-unknown₂ {bool} = TJBaseUnknown TBBool
⊓-unknown₂ {unknown} = TJUnknown
⊓-unknown₂ {_ -→ _} = TJArrUnknown
⊓-unknown₂ {_ -× _} = TJProdUnknown
-- meet unicity
⊓-unicity : ∀ {τ₁ τ₂ τ τ′} → τ₁ ⊓ τ₂ ⇒ τ → τ₁ ⊓ τ₂ ⇒ τ′ → τ ≡ τ′
⊓-unicity (TJBase _) (TJBase _) = refl
⊓-unicity TJUnknown TJUnknown = refl
⊓-unicity (TJUnknownBase _) (TJUnknownBase _) = refl
⊓-unicity (TJBaseUnknown _) (TJBaseUnknown _) = refl
⊓-unicity TJUnknownArr TJUnknownArr = refl
⊓-unicity TJArrUnknown TJArrUnknown = refl
⊓-unicity (TJArr _ _) (TJBase ())
⊓-unicity (TJArr τ₁⊓τ₁′ τ₂⊓τ₂′) (TJArr τ₁⊓τ₁′′ τ₂⊓τ₂′′) = -→-≡ (⊓-unicity τ₁⊓τ₁′ τ₁⊓τ₁′′) (⊓-unicity τ₂⊓τ₂′ τ₂⊓τ₂′′)
⊓-unicity TJUnknownProd TJUnknownProd = refl
⊓-unicity TJProdUnknown TJProdUnknown = refl
⊓-unicity (TJProd _ _) (TJBase ())
⊓-unicity (TJProd τ₁⊓τ₁′ τ₂⊓τ₂′) (TJProd τ₁⊓τ₁′′ τ₂⊓τ₂′′) = -×-≡ (⊓-unicity τ₁⊓τ₁′ τ₁⊓τ₁′′) (⊓-unicity τ₂⊓τ₂′ τ₂⊓τ₂′′)
-- meet derivation equality
⊓-≡ : ∀ {τ₁ τ₂ τ} → (τ₁⊓τ₂ : τ₁ ⊓ τ₂ ⇒ τ) → (τ₁⊓τ₂′ : τ₁ ⊓ τ₂ ⇒ τ) → τ₁⊓τ₂ ≡ τ₁⊓τ₂′
⊓-≡ (TJBase b) (TJBase b′)
rewrite base-≡ b b′ = refl
⊓-≡ TJUnknown TJUnknown = refl
⊓-≡ (TJUnknownBase b) (TJUnknownBase b′)
rewrite base-≡ b b′ = refl
⊓-≡ (TJBaseUnknown b) (TJBaseUnknown b′)
rewrite base-≡ b b′ = refl
⊓-≡ (TJArr τ₁⊓τ₁′ τ₂⊓τ₂′) (TJArr τ₁⊓τ₁′′ τ₂⊓τ₂′′)
rewrite ⊓-≡ τ₁⊓τ₁′ τ₁⊓τ₁′′
| ⊓-≡ τ₂⊓τ₂′ τ₂⊓τ₂′′ = refl
⊓-≡ TJUnknownArr TJUnknownArr = refl
⊓-≡ TJArrUnknown TJArrUnknown = refl
⊓-≡ (TJProd τ₁⊓τ₁′ τ₂⊓τ₂′) (TJProd τ₁⊓τ₁′′ τ₂⊓τ₂′′)
rewrite ⊓-≡ τ₁⊓τ₁′ τ₁⊓τ₁′′
| ⊓-≡ τ₂⊓τ₂′ τ₂⊓τ₂′′ = refl
⊓-≡ TJUnknownProd TJUnknownProd = refl
⊓-≡ TJProdUnknown TJProdUnknown = refl
-- meet existence means that types are consistent
⊓→~ : ∀ {τ τ₁ τ₂} → τ₁ ⊓ τ₂ ⇒ τ → τ₁ ~ τ₂
⊓→~ (TJBase b) = TCBase b
⊓→~ TJUnknown = TCUnknown
⊓→~ (TJUnknownBase b) = TCUnknownBase b
⊓→~ (TJBaseUnknown b) = TCBaseUnknown b
⊓→~ TJUnknownArr = TCUnknownArr
⊓→~ TJArrUnknown = TCArrUnknown
⊓→~ (TJArr τ₁⊓τ₁′ τ₂⊓τ₂′) = TCArr (⊓→~ τ₁⊓τ₁′) (⊓→~ τ₂⊓τ₂′)
⊓→~ TJUnknownProd = TCUnknownProd
⊓→~ TJProdUnknown = TCProdUnknown
⊓→~ (TJProd τ₁⊓τ₁′ τ₂⊓τ₂′) = TCProd (⊓→~ τ₁⊓τ₁′) (⊓→~ τ₂⊓τ₂′)
-- consistent types have a meet
~→⊓ : ∀ {τ₁ τ₂} → τ₁ ~ τ₂ → ∃[ τ ] τ₁ ⊓ τ₂ ⇒ τ
~→⊓ TCUnknown = ⟨ unknown , TJUnknown ⟩
~→⊓ {τ₁ = τ } (TCBase b) = ⟨ τ , TJBase b ⟩
~→⊓ {τ₂ = τ₂} (TCUnknownBase b) = ⟨ τ₂ , TJUnknownBase b ⟩
~→⊓ {τ₁ = τ₁} (TCBaseUnknown b) = ⟨ τ₁ , TJBaseUnknown b ⟩
~→⊓ {τ₂ = τ₂} TCUnknownArr = ⟨ τ₂ , TJUnknownArr ⟩
~→⊓ {τ₁ = τ₁} TCArrUnknown = ⟨ τ₁ , TJArrUnknown ⟩
~→⊓ (TCArr τ₁~τ₁′ τ₂~τ₂′)
with ⟨ τ₁″ , ⊓⇒τ₁″ ⟩ ← ~→⊓ τ₁~τ₁′
| ⟨ τ₂″ , ⊓⇒τ₂″ ⟩ ← ~→⊓ τ₂~τ₂′
= ⟨ τ₁″ -→ τ₂″ , TJArr ⊓⇒τ₁″ ⊓⇒τ₂″ ⟩
~→⊓ {τ₂ = τ₂} TCUnknownProd = ⟨ τ₂ , TJUnknownProd ⟩
~→⊓ {τ₁ = τ₁} TCProdUnknown = ⟨ τ₁ , TJProdUnknown ⟩
~→⊓ (TCProd τ₁~τ₁′ τ₂~τ₂′)
with ⟨ τ₁″ , ⊓⇒τ₁″ ⟩ ← ~→⊓ τ₁~τ₁′
| ⟨ τ₂″ , ⊓⇒τ₂″ ⟩ ← ~→⊓ τ₂~τ₂′
= ⟨ τ₁″ -× τ₂″ , TJProd ⊓⇒τ₁″ ⊓⇒τ₂″ ⟩
-- meet nonexistence means types are inconsistent
¬⊓→~̸ : ∀ {τ₁ τ₂} → ¬ (∃[ τ ] τ₁ ⊓ τ₂ ⇒ τ) → τ₁ ~̸ τ₂
¬⊓→~̸ ¬τ₁⊓τ₂ = λ { τ₁~τ₂ → ¬τ₁⊓τ₂ (~→⊓ τ₁~τ₂) }
-- inconsistent types have no meet
~̸→¬⊓ : ∀ {τ₁ τ₂} → τ₁ ~̸ τ₂ → ¬ (∃[ τ ] τ₁ ⊓ τ₂ ⇒ τ)
~̸→¬⊓ τ₁~̸τ₂ = λ { ⟨ τ , τ₁⊓τ₂ ⟩ → τ₁~̸τ₂ (⊓→~ τ₁⊓τ₂) }
-- types are consistent with their meet
⊓⇒→~ : ∀ {τ₁ τ₂ τ} → τ₁ ⊓ τ₂ ⇒ τ → τ₁ ~ τ × τ₂ ~ τ
⊓⇒→~ (TJBase b) = ⟨ TCBase b , TCBase b ⟩
⊓⇒→~ TJUnknown = ⟨ TCUnknown , TCUnknown ⟩
⊓⇒→~ (TJUnknownBase b) = ⟨ TCUnknownBase b , TCBase b ⟩
⊓⇒→~ (TJBaseUnknown b) = ⟨ TCBase b , TCUnknownBase b ⟩
⊓⇒→~ (TJArr τ₁⊓τ₁′ τ₂⊓τ₂′)
with ⟨ τ₁~ , τ₁′~ ⟩ ← ⊓⇒→~ τ₁⊓τ₁′
| ⟨ τ₂~ , τ₂′~ ⟩ ← ⊓⇒→~ τ₂⊓τ₂′
= ⟨ TCArr τ₁~ τ₂~ , TCArr τ₁′~ τ₂′~ ⟩
⊓⇒→~ TJUnknownArr = ⟨ TCUnknownArr , TCArr ~-refl ~-refl ⟩
⊓⇒→~ TJArrUnknown = ⟨ TCArr ~-refl ~-refl , TCUnknownArr ⟩
⊓⇒→~ (TJProd τ₁⊓τ₁′ τ₂⊓τ₂′)
with ⟨ τ₁~ , τ₁′~ ⟩ ← ⊓⇒→~ τ₁⊓τ₁′
| ⟨ τ₂~ , τ₂′~ ⟩ ← ⊓⇒→~ τ₂⊓τ₂′
= ⟨ TCProd τ₁~ τ₂~ , TCProd τ₁′~ τ₂′~ ⟩
⊓⇒→~ TJUnknownProd = ⟨ TCUnknownProd , TCProd ~-refl ~-refl ⟩
⊓⇒→~ TJProdUnknown = ⟨ TCProd ~-refl ~-refl , TCUnknownProd ⟩
-- types are consistent with types consistent to their meet
⊓⇒-~→~ : ∀ {τ₁ τ₂ τ τ′} → τ₁ ⊓ τ₂ ⇒ τ → τ ~ τ′ → τ₁ ~ τ′ × τ₂ ~ τ′
⊓⇒-~→~ (TJBase b) τ~τ′ = ⟨ τ~τ′ , τ~τ′ ⟩
⊓⇒-~→~ TJUnknown τ~τ′ = ⟨ τ~τ′ , τ~τ′ ⟩
⊓⇒-~→~ (TJUnknownBase b) τ~τ′ = ⟨ ~-unknown₁ , τ~τ′ ⟩
⊓⇒-~→~ (TJBaseUnknown b) τ~τ′ = ⟨ τ~τ′ , ~-unknown₁ ⟩
⊓⇒-~→~ {τ = .(_ -→ _)} {unknown} (TJArr τ₁⊓τ₁′ τ₂⊓τ₂′) τ~τ′
= ⟨ TCArrUnknown , TCArrUnknown ⟩
⊓⇒-~→~ {τ = .(_ -→ _)} {τ₁″ -→ τ₂″} (TJArr τ₁⊓τ₁′ τ₂⊓τ₂′) (TCArr τ₁″~τ₁‴ τ₂″~τ₂‴)
with ⟨ τ₁~τ₁″ , τ₁′~τ₁″ ⟩ ← ⊓⇒-~→~ τ₁⊓τ₁′ τ₁″~τ₁‴
with ⟨ τ₂~τ₂″ , τ₂′~τ₂″ ⟩ ← ⊓⇒-~→~ τ₂⊓τ₂′ τ₂″~τ₂‴
= ⟨ TCArr τ₁~τ₁″ τ₂~τ₂″ , TCArr τ₁′~τ₁″ τ₂′~τ₂″ ⟩
⊓⇒-~→~ {τ = .(_ -→ _)} TJUnknownArr τ~τ′
= ⟨ ~-unknown₁ , τ~τ′ ⟩
⊓⇒-~→~ {τ = .(_ -→ _)} TJArrUnknown τ~τ′
= ⟨ τ~τ′ , ~-unknown₁ ⟩
⊓⇒-~→~ {τ = .(_ -× _)} {unknown} (TJProd τ₁⊓τ₁′ τ₂⊓τ₂′) τ~τ′
= ⟨ TCProdUnknown , TCProdUnknown ⟩
⊓⇒-~→~ {τ = .(_ -× _)} {τ′} (TJProd τ₁⊓τ₁′ τ₂⊓τ₂′) (TCProd τ₁″~τ₁‴ τ₂″~τ₂‴)
with ⟨ τ₁~τ₁″ , τ₁′~τ₁″ ⟩ ← ⊓⇒-~→~ τ₁⊓τ₁′ τ₁″~τ₁‴
with ⟨ τ₂~τ₂″ , τ₂′~τ₂″ ⟩ ← ⊓⇒-~→~ τ₂⊓τ₂′ τ₂″~τ₂‴
= ⟨ TCProd τ₁~τ₁″ τ₂~τ₂″ , TCProd τ₁′~τ₁″ τ₂′~τ₂″ ⟩
⊓⇒-~→~ {τ = .(_ -× _)} TJUnknownProd τ~τ′
= ⟨ ~-unknown₁ , τ~τ′ ⟩
⊓⇒-~→~ {τ = .(_ -× _)} TJProdUnknown τ~τ′
= ⟨ τ~τ′ , ~-unknown₁ ⟩
open equality public
open base public
open consistency public
open matched public
open meet public