A Coq library for domain theory (http://rwd.rdockins.name/domains/)

root / skiy.v

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
(* Copyright (c) 2014, Robert Dockins *)

Require Import String.
Require Import List.
Require Import Arith.
Require Import Omega.


Require Import basics.
Require Import preord.
Require Import categories.
Require Import sets.
Require Import finsets.
Require Import esets.
Require Import effective.
Require Import directed.
Require Import plotkin.
Require Import joinable.
Require Import approx_rels.
Require Import cpo.
Require Import profinite.
Require Import profinite_adj.
Require Import strict_utils.
Require Import fixes.
Require Import flat.

(** * Soundness and adequacy simply-typed SKI with booleans and fixpoints.

    The adequacy proof goes via a standard logical-relations argument.
    As a corollary of the main logical-relations lemma, we show that
    nonvalues are denoted by ⊥.
  *)


(**  We have arrow types and a single base type of booleans. *)
Inductive ty :=
  | ty_bool
  | ty_arrow : ty -> ty -> ty.

Delimit Scope ty_scope with ty.
Notation "2" := ty_bool : ty_scope.
Notation "x ⇒ y" := (ty_arrow (x)%ty (y)%ty) : ty_scope.
Bind Scope ty_scope with ty.

Delimit Scope ski_scope with ski.
Open Scope ski_scope.

(**  Terms are boolean constants, the standard combinators S, K and I,
     and an IF/THEN/ELSE combinator; and applications.  We also have
     the call-by-value fixpoint combinator Y.  As usual for the CBV
     fixpoint operator, it only calculates fixpoints at function types.
  *)
Inductive term : ty -> Type :=
  | tbool : forall b:bool,
                term 2

  | tapp : forall σ₁ σ₂,
                term (σ₁ ⇒ σ₂) ->
                term σ₁ ->
                term σ₂

  | tI : forall σ,
                term (σ ⇒ σ)

  | tK : forall σ₁ σ₂,
                term (σ₁ ⇒ σ₂ ⇒ σ₁)

  | tS : forall σ₁ σ₂ σ₃,
                term ((σ₁ ⇒ σ₂ ⇒ σ₃) ⇒ (σ₁ ⇒ σ₂) ⇒ σ₁ ⇒ σ₃)

  | tIF : forall σ,
                term (2 ⇒ σ ⇒ σ ⇒ σ)

  | tY : forall σ₁ σ₂,
                term ( ((σ₁ ⇒ σ₂) ⇒ (σ₁ ⇒ σ₂)) ⇒ (σ₁ ⇒ σ₂) ).

Arguments tapp [_ _] _ _.
Notation "x • y" := (tapp x y) 
  (at level 52, left associativity, format "x • y") : ski_scope.

(**  The operational semantics is given in a big-step style, with the specification
     of redexes split out into a separate relation.
  *)
Inductive redex : forall σ₁ σ₂, term (σ₁ ⇒ σ₂) -> term σ₁ -> term σ₂ -> Prop :=
  | redex_I : forall σ x,
                  redex _ _ (tI σ) x x
  | redex_K : forall σ₁ σ₂ x y,
                  redex σ₂ σ₁ (tK σ₁ σ₂ • y) x y
  | redex_S : forall σ₁ σ₂ σ₃ f g x,
                  redex _ _ (tS σ₁ σ₂ σ₃ • f • g)
                            x
                            ((f•x)•(g•x))
  | redex_IFtrue : forall σ th el,
                  redex _ _ (tIF σ • tbool true • th) el th
  | redex_IFfalse : forall σ th el,
                  redex _ _ (tIF σ • tbool false • th) el el

  | redex_Y : forall σ₁ σ₂ (f:term ((σ₁ ⇒ σ₂) ⇒ (σ₁ ⇒ σ₂))) x,
                  redex _ _ (tY σ₁ σ₂ • f) x
                            (f•(tY σ₁ σ₂ • f)•x).

Inductive eval : forall τ, term τ -> term τ -> Prop :=
  | ebool : forall b, eval 2 (tbool b) (tbool b)
  | eI   : forall σ, eval _ (tI σ) (tI _)
  | eK   : forall σ₁ σ₂, eval _ (tK σ₁ σ₂) (tK _ _)
  | eS   : forall σ₁ σ₂ σ₃, eval _ (tS σ₁ σ₂ σ₃) (tS _ _ _)
  | eIF  : forall σ, eval _ (tIF σ) (tIF σ)
  | eY   : forall σ₁ σ₂, eval _ (tY σ₁ σ₂) (tY _ _)
  | eapp1 : forall σ₁ σ₂ m₁ m₂ n₁ n₂ r z,
             eval (σ₁ ⇒ σ₂) m₁ n₁ ->
             eval σ₁ m₂ n₂ ->
             redex σ₁ σ₂ n₁ n₂ r ->
             eval σ₂ r z ->
             eval σ₂ (m₁ • m₂) z
  | eapp2 : forall σ₁ σ₂ m₁ m₂ n₁ n₂,
             eval (σ₁ ⇒ σ₂) m₁ n₁ ->
             eval σ₁ m₂ n₂ ->
             ~(exists r, redex σ₁ σ₂ n₁ n₂ r) ->
             eval σ₂ (m₁ • m₂) (n₁ • n₂).


(**  Syntactic types have decicable equality, which
     implies injectivity for dependent pairs with
     (syntactic) types as the type being depended upon.
  *)
Lemma inj_pair2_ty : forall (F:ty -> Type) τ x y,
  existT F τ x = existT F τ y -> x = y.
Proof.
  intros.
  apply Eqdep_dec.inj_pair2_eq_dec in H. auto.
  decide equality.
Qed.

Ltac inj_ty :=
  repeat match goal with
           [ H : existT _ _ _ = existT _ _ _ |- _ ] =>
             apply inj_pair2_ty in H
           end.

Ltac inv H :=
  inversion H; subst; inj_ty; repeat subst.


(** Values are terms that evaluate to themselves.
  *)
Definition value σ (t:term σ) := eval _ t t.
Arguments value [σ] t.

(**  Here are some basic techincal results  
     about the operational semantics.
  *)
Lemma eval_value τ x y :
  eval τ x y -> value y.
Proof.
  intro H. induction H.
  apply ebool.
  apply eI.
  apply eK.
  apply eS.
  apply eIF.
  apply eY.
  auto.
  apply eapp2; auto.
Qed.

Lemma redex_eq τ₁ τ₂ x y z1 z2 :
  redex τ₁ τ₂ x y z1 ->
  redex τ₁ τ₂ x y z2 ->
  z1 = z2.
Proof.
  intros; inv H; inv H; inv H0; auto.
Qed.

Lemma eval_eq τ x y1 y2 :
  eval τ x y1 -> eval τ x y2 -> y1 = y2.
Proof.
  intro H. revert y2.
  induction H.

  intros. inv H. auto.
  intros. inv H. auto.
  intros. inv H. auto.
  intros. inv H. auto.
  intros. inv H. auto.
  intros. inv H. auto.

  intros. inv H3.
  apply IHeval1 in H9.
  apply IHeval2 in H10.
  subst n₁0 n₂0.
  assert (r = r0).
  eapply redex_eq; eauto.
  subst r0.
  apply IHeval3; auto.
  apply IHeval1 in H9.
  apply IHeval2 in H10.
  subst n₁0 n₂0.
  elim H11; eauto.

  intros. inv H2.
  apply IHeval1 in H8.
  apply IHeval2 in H9.
  subst n₁0 n₂0.
  elim H1. eauto.
  f_equal; auto.
Qed.

Lemma eval_trans τ x y z :
  eval τ x y -> eval τ y z -> eval τ x z.
Proof.
  intros.
  replace z with y; auto.
  eapply eval_eq with y; auto.
  eapply eval_value; eauto.
Qed.

Lemma eval_app_congruence σ₁ σ₂ : forall x x' y y' z,
  (forall q, eval _ x q -> eval _ x' q) ->
  (forall q, eval _ y q -> eval _ y' q) ->
  eval _ (@tapp σ₁ σ₂ x y) z ->
  eval _ (@tapp σ₁ σ₂ x' y') z.
Proof.
  intros.
  inv H1.
  apply H in H7.
  apply H0 in H8.
  eapply eapp1; eauto.
  apply eapp2; auto.
Qed.

Lemma eval_no_redex : forall σ₁ σ₂ x x',
  eval σ₂ x x' ->
  forall m₁ m₂ n₁ n₂ r,
    x' = @tapp σ₁ σ₂ m₁ m₂ ->
    eval _ m₁ n₁ -> eval _ m₂ n₂ -> redex _ _ n₁ n₂ r -> False.
Proof.
  do 5 intro. induction H; intros; try discriminate; subst.
  eapply IHeval3; eauto.
  inv H2.
  assert (m₂0 = n₂0).
  eapply eval_eq; eauto.
  apply eval_trans with m₂0; auto.
  assert (m₁0 = n₁0).
  eapply eval_eq; eauto.
  apply eval_trans with m₁0; auto.
  subst.
  apply H1. eauto.
Qed.


Lemma value_app_inv σ₁ σ₂ x y :
  value (@tapp σ₁ σ₂ x y) ->
  value x /\ value y.
Proof.
  intros. inv H.
  elimtype False.
  eapply eval_no_redex.
  apply H8. reflexivity. eauto. eauto. eauto.
  split; auto.
Qed.  

(*
Lemma eval_app_inv σ₁ σ₂ x y z :
  eval _ (@tapp σ₁ σ₂ x y) z ->
  exists x', exists y',
    eval _ x x' /\ eval _ y y' /\
    eval _ (x' • y') z.
Proof.
  intros. inv H.
  exists n₁. exists n₂.
  intuition.
  eapply eapp1.
  eapply eval_value; eauto.
  eapply eval_value; eauto.
  eauto. auto.
  exists n₁. exists n₂.
  intuition.
  apply eapp2.
  eapply eval_value; eauto.
  eapply eval_value; eauto.
  auto.
Qed.
*)

(**  "Inert" terms are those that will not evaluate if applied to
     one more argument.  We prove that every term at function type
     is either intert or forms a redex if applied to another term.
  *)
Inductive inert : forall σ₁ σ₂, term (σ₁ ⇒ σ₂) -> Prop :=
  | inert_K : forall σ₁ σ₂,
                  inert _ _ (tK σ₁ σ₂)
  | inert_S1 : forall σ₁ σ₂ σ₃,
                  inert _ _ (tS σ₁ σ₂ σ₃)
  | inert_S2 : forall σ₁ σ₂ σ₃ x,
                  inert _ _ (tS σ₁ σ₂ σ₃ • x)
  | inert_IF1 : forall σ,
                  inert _ _ (tIF σ)
  | inert_IF2 : forall σ x,
                  inert _ _ (tIF σ • x)
  | inert_Y : forall σ₁ σ₂,
                  inert _ _ (tY σ₁ σ₂).


Fixpoint tmsize τ (x:term τ) : nat :=
  match x with
  | tapp σ₁ σ₂ a b => (1 + tmsize _ a + tmsize _ b)%nat
  | _ => 1%nat
  end.

Lemma redex_inert_false : forall σ₁ σ₂ f g r,
  redex σ₁ σ₂ f g r ->
  inert σ₁ σ₂ f ->
  False.
Proof.
  intros. inv H; inv H0.
Qed.

Lemma redex_or_inert' n : 
  forall τ (x:term τ) σ₁ σ₂ (f:term (σ₁ ⇒ σ₂))
    (Hτ : τ = (σ₁ ⇒ σ₂)%ty)
    (Hx : eq_rect τ term x _ Hτ = f)
    (Hsz : tmsize τ x = n),
    value f ->
    (forall g, exists r, redex σ₁ σ₂ f g r) \/ inert σ₁ σ₂ f.
Proof.
  induction n using (well_founded_induction lt_wf).
  intros τ x. rename H into Hind.
  destruct x; intros; try discriminate.

  subst σ₂. simpl in *. subst n f.
  destruct (value_app_inv _ _ _ _ H).
  assert (Hx1:tmsize _ x1 < S (tmsize _ x1 + tmsize _ x2)).
  omega.
  generalize (Hind (tmsize _ x1) Hx1 _ _ _ _ x1
    (refl_equal _) (refl_equal _) (refl_equal _) H0).
  intros. destruct H2. 
  destruct (H2 x2).
  elimtype False. eapply eval_no_redex.
  apply H. reflexivity. apply H0. apply H1. eauto.
  inv H2. 
  left; intros. econstructor. econstructor.
  right. constructor.
  left; intros. econstructor. econstructor.
  right. constructor.
  inj_ty. subst x1.
  destruct (value_app_inv _ _ _ _ H0).
  inv H4.
  left; intros. destruct b.
  econstructor. econstructor.
  econstructor. econstructor.
  simpl in *.
  inv H13.
  elimtype False. eapply eval_no_redex.
  apply H13. reflexivity. eauto. eauto. eauto.
  assert (Hm₁ : tmsize _ m₁ <
         S (S (S (S (tmsize (σ₁ ⇒ ty_bool) m₁ + tmsize σ₁ m₂ + tmsize σ₂0 x2))))).
  omega.
  destruct (value_app_inv _ _ _ _ H4).
  generalize (Hind _ Hm₁ _ _ _ _ m₁ 
    (refl_equal _) (refl_equal _) (refl_equal _) H5).
  intros. destruct H11. destruct (H11 m₂).
  elimtype False. eapply eval_no_redex.
  apply H4. reflexivity. eauto. eauto. eauto.
  inv H11.
  inv H6.
  assert (Hm₁ : tmsize _ m₁ <
         S (S (S (S (tmsize (σ₁ ⇒ ty_bool) m₁ + tmsize σ₁ m₂ + tmsize σ₂0 x2))))).
  omega.
  destruct (value_app_inv _ _ _ _ H4).
  generalize (Hind _ Hm₁ _ _ _ _ m₁
    (refl_equal _) (refl_equal _) (refl_equal _) H5).
  intros. destruct H13. destruct (H13 m₂).
  elimtype False. eapply eval_no_redex.
  apply H4. reflexivity. eauto. eauto. eauto.
  inv H13.

  left; intros. econstructor. econstructor.

  inv Hτ.
  replace Hτ with (refl_equal (σ₂ ⇒ σ₂)%ty). simpl.
  left; intros. econstructor. econstructor.
  apply Eqdep_dec.UIP_dec. decide equality.

  inv Hτ. 
  replace Hτ with (refl_equal (σ₁0 ⇒ σ₂ ⇒ σ₁0)%ty). simpl.
  right. constructor.
  apply Eqdep_dec.UIP_dec. decide equality.
  
  inv Hτ.
  replace Hτ with (refl_equal ((σ₁ ⇒ σ₂ ⇒ σ₃) ⇒ (σ₁ ⇒ σ₂) ⇒ σ₁ ⇒ σ₃)%ty).
  simpl.
  right. constructor.
  apply Eqdep_dec.UIP_dec. decide equality.

  inv Hτ.
  replace Hτ with (refl_equal (ty_bool ⇒ σ ⇒ σ ⇒ σ)%ty).
  simpl.
  right. constructor.
  apply Eqdep_dec.UIP_dec. decide equality.

  inv Hτ.
  replace Hτ with (refl_equal (((σ₁ ⇒ σ₂) ⇒ σ₁ ⇒ σ₂) ⇒ σ₁ ⇒ σ₂)%ty).
  simpl.
  right. constructor.
  apply Eqdep_dec.UIP_dec. decide equality.
Qed.

Lemma redex_or_inert : 
  forall σ₁ σ₂ (f:term (σ₁ ⇒ σ₂)),
    value f ->
    (forall g, exists r, redex σ₁ σ₂ f g r) \/ inert σ₁ σ₂ f.
Proof.
  intros. 
  apply (redex_or_inert' (tmsize _ f) _ f _ _ f (refl_equal _));
    simpl; auto.
Qed.

Lemma canonical_bool : forall x,
  eval 2 x x -> 
  x = tbool true \/ x = tbool false.
Proof.
  intros. inv H.
  destruct b; auto.

  elimtype False.
  eapply eval_no_redex.
  apply H6. reflexivity. eauto. eauto. eauto.
  inv H1. clear H1.
  destruct (redex_or_inert _ _ m₁); auto.
  elim H5; apply H0.
  inv H0.
Qed.


(**  Types are interpreted as pointed domains.  Booleans
     are the flat domain over booleans and the arrow type
     is the lifted strict function space.
  *)
Fixpoint tydom (τ:ty) : ∂PLT :=
  match τ with
  | ty_bool => flat enumbool
  | ty_arrow τ₁ τ₂ => colift (tydom τ₁ ⊸ tydom τ₂)
  end.

(**  Here we define the semantics of the Y combinator.
  *)
Section Ydefn.
  Variables σ₁ σ₂:ty.

  Definition Ybody
    : U (colift (tydom (σ₁ ⇒ σ₂) ⊸ tydom (σ₁ ⇒ σ₂)))
       → PLT.exp (U (tydom (σ₁ ⇒ σ₂))) (U (tydom (σ₁ ⇒ σ₂)))

       (*w : U (colift (tydom (σ₁ ⇒ σ₂) ⊸ tydom (σ₁ ⇒ σ₂))) *)
    := PLT.curry (*x:U (tydom (σ₁ ⇒ σ₂)))*) (strict_curry' (*y:U tydom σ₁ *)

                                                          (* w *)    (* x *)    (*y*)
        (strict_app' ∘ 〈strict_app' ∘ 〈π₁ ∘ π₁, π₂ ∘ π₁〉, π₂〉)
       ).

  Lemma Ybody_unroll : forall Γ 
    (f:Γ → U (tydom ((σ₁ ⇒ σ₂) ⇒ (σ₁ ⇒ σ₂))))
    (x:Γ → U (tydom σ₁)),

    semvalue x ->

    let Yf := (fixes Ybody) ∘ f in

    strict_app' ∘ 〈Yf, x〉 ≈
    strict_app' ∘ 〈strict_app' ∘ 〈f,Yf〉 , x〉.
  Proof.
    intros. unfold Yf at 1.
    rewrite fixes_unroll. unfold Ybody at 1.
    rewrite PLT.curry_apply2.
    rewrite <- (cat_assoc PLT).
    rewrite strict_curry_app2'.
    rewrite (PLT.pair_compose_commute false).
    rewrite <- (cat_assoc PLT).
    apply cat_respects. auto.
    rewrite (PLT.pair_compose_commute false).
    rewrite <- (cat_assoc PLT).
    rewrite (PLT.pair_compose_commute false).
    rewrite <- (cat_assoc PLT).
    rewrite PLT.pair_commute1.        
    rewrite PLT.pair_commute2.
    rewrite PLT.pair_commute1.        
    rewrite <- (cat_assoc PLT).
    rewrite PLT.pair_commute1.        
    rewrite PLT.pair_commute2.
    rewrite (cat_ident2 PLT).    
    apply PLT.pair_eq. auto. auto.
    auto.
  Qed.

  Definition Ysem Γ 
    : Γ → U (tydom (((σ₁ ⇒ σ₂) ⇒ (σ₁ ⇒ σ₂)) ⇒ (σ₁ ⇒ σ₂)))
    := strict_curry' (fixes Ybody ∘ π₂).
End Ydefn.

Notation "'Λ' f" := (strict_curry' f) : ski_scope.

(**  The denotation of terms.  The denotation of
     the S, K and I combinators a straightforward interpretation of the
     usual lambda term into the strict lambda and strict application 
     denotation functions.
  *)
Fixpoint denote (τ:ty) (m:term τ) : 1 → U (tydom τ) :=
  match m in term τ return 1 → U (tydom τ) with
  | tbool b => flat_elem' b
  | tapp σ₁ σ₂ m₁ m₂ => strict_app' ∘ 〈〚m₁〛,〚m₂〛〉
  | tI σ => Λ(π₂)
  | tK σ₁ σ₂ => Λ(Λ(π₂ ∘ π₁))
  | tS σ₁ σ₂ σ₃ => Λ(Λ(Λ(
                     strict_app' ∘
                       〈 strict_app' ∘ 〈π₂ ∘ π₁ ∘ π₁, π₂〉
                       , strict_app' ∘ 〈π₂ ∘ π₁, π₂〉
                       〉
                      )))
  | tIF σ => Λ(flat_cases' (fun b:bool =>
                if b then Λ(Λ(π₂ ∘ π₁))
                     else Λ(Λ(π₂))
                ))
  | tY σ₁ σ₂ => Ysem σ₁ σ₂ 1
  end
 where "〚 m 〛" := (denote _ m) : ski_scope.


(**  This mutual induction shows that operational
     values have semantic values as denotations,
     and that inert operational values yield 
     semantic values when applied to any semantic value.
  *)
Lemma value_inert_semvalue : forall n,
  (forall σ x,
    tmsize _ x = n ->
    eval σ x x -> semvalue 〚x〛) /\
  (forall σ₁ σ₂ x (y:1 → U (tydom σ₁)),
    tmsize _ x = n ->
    value x ->
    inert σ₁ σ₂ x ->
    semvalue y ->
    semvalue (strict_app' ∘ 〈〚x〛, y〉)).
Proof.
  intro n. induction n using (well_founded_induction lt_wf).

  split; intros. 
  inv H1; simpl.
  apply flat_elem'_semvalue.

  apply strict_curry'_semvalue.  
  apply strict_curry'_semvalue.  
  apply strict_curry'_semvalue.  
  apply strict_curry'_semvalue.  
  unfold Ysem. apply strict_curry'_semvalue.

  elimtype False.
  eapply eval_no_redex.
  apply H8. reflexivity. eauto. eauto. eauto.

  inv H3. clear H3.
  destruct (redex_or_inert _ _ m₁); auto.
  elim H7; auto.
  simpl in H.
  assert (Hm1 : (tmsize _ m₁) < S (tmsize _ m₁ + tmsize _ m₂)).
  omega.
  destruct (H _ Hm1).
  apply H3; auto.
  clear H2 H3.
  assert (Hm2 : (tmsize _ m₂) < S (tmsize _ m₁ + tmsize _ m₂)).
  omega.
  destruct (H _ Hm2).
  apply H2; auto.


  inv H2; simpl.

  rewrite strict_curry_app'; auto.
  apply strict_curry'_semvalue2.

  rewrite strict_curry_app'; auto.
  apply strict_curry'_semvalue2.

  rewrite strict_curry_app'; auto.
  rewrite strict_curry_app2'; auto.
  apply strict_curry'_semvalue2.
  destruct (value_app_inv _ _ _ _ H1); auto.
  simpl in H.
  assert (tmsize _ x0 < S (S (tmsize _ x0))). omega.
  destruct (H _ H5). apply (H6 _ x0); auto.

  rewrite strict_curry_app'; auto.
  destruct (flat_elem_canon enumbool y H3) as [b ?].
  rewrite H0.
  simpl.
  rewrite flat_cases_elem'.
  destruct b.
  apply strict_curry'_semvalue2.
  apply strict_curry'_semvalue2.

  rewrite strict_curry_app'; auto.
  destruct (value_app_inv _ _ _ _ H1); auto.
  destruct (canonical_bool x0); auto; subst x0; simpl.
  rewrite flat_cases_elem'.
  rewrite strict_curry_app2'; auto.
  apply strict_curry'_semvalue2.
  rewrite flat_cases_elem'.
  rewrite strict_curry_app2'; auto.
  apply strict_curry'_semvalue2.

  destruct (value_app_inv _ _ _ _ H1); auto.
  simpl in H.
  assert (tmsize _ x0 < S (S (tmsize _ x0))). omega.
  destruct (H _ H5). apply (H6 _ x0); auto.
  
  unfold Ysem.
  rewrite strict_curry_app'; auto.
  rewrite (fixes_unroll _ _ (Ybody σ₁0 σ₂0)).
  unfold Ybody at 1.
  rewrite PLT.curry_apply2.
  rewrite <- (cat_assoc PLT).
  rewrite <- (cat_assoc PLT).
  apply strict_curry'_semvalue2.
Qed.

Lemma value_semvalue : forall σ (x:term σ),
  value x -> semvalue 〚x〛.
Proof.
  intros. destruct (value_inert_semvalue (tmsize _ x)); auto.
Qed.

Lemma inert_semvalue σ₁ σ₂ x y :    
  value x -> inert σ₁ σ₂ x -> semvalue y ->
  semvalue (strict_app' ∘ 〈〚x〛, y 〉).
Proof.
  intros.
  destruct (value_inert_semvalue (tmsize _ x)).
  apply H3; auto.
Qed.

Hint Resolve value_semvalue.


(**  Now we can show the soundness of redexes.
  *)
Lemma redex_soundness : forall σ₁ σ₂ x y z,
  value x ->
  value y ->
  redex σ₁ σ₂ x y z ->
  strict_app' ∘ 〈〚x〛,〚y〛〉 ≈ 〚z〛.
Proof.
  intros. inv H1.

  inv H1. simpl.
  rewrite strict_curry_app'; auto.
  rewrite PLT.pair_commute2. auto.
  
  simpl.
  rewrite strict_curry_app'; auto.
  rewrite strict_curry_app2'; auto.
  rewrite <- (cat_assoc PLT).
  rewrite PLT.pair_commute1.
  rewrite PLT.pair_commute2. auto.
  destruct (value_app_inv _ _ _ _ H); auto.
  
  destruct (value_app_inv _ _ _ _ H).
  destruct (value_app_inv _ _ _ _ H2). clear H4.
  simpl.
  rewrite strict_curry_app'; auto.
  rewrite strict_curry_app2'; auto.
  rewrite strict_curry_app2'; auto.
  repeat rewrite <- (cat_assoc PLT).
  rewrite (PLT.pair_compose_commute false).
  repeat rewrite <- (cat_assoc PLT).
  rewrite (PLT.pair_compose_commute false).
  repeat rewrite <- (cat_assoc PLT).
  repeat rewrite PLT.pair_commute1.
  repeat rewrite PLT.pair_commute2.
  rewrite (PLT.pair_compose_commute false).
  repeat rewrite <- (cat_assoc PLT).
  repeat rewrite PLT.pair_commute1.
  repeat rewrite PLT.pair_commute2.
  auto.
  apply (value_semvalue _ g); auto.
  apply (value_semvalue _ f); auto.
  
  destruct (value_app_inv _ _ _ _ H). clear H2.
  simpl.
  rewrite strict_curry_app'; auto.
  rewrite flat_cases_elem'.
  rewrite strict_curry_app2'; auto.
  rewrite strict_curry_app2'; auto.
  repeat rewrite <- (cat_assoc PLT).
  repeat rewrite PLT.pair_commute1.
  repeat rewrite PLT.pair_commute2. auto.
  apply flat_elem'_semvalue.
  
  destruct (value_app_inv _ _ _ _ H). clear H2.
  inv H1.
  simpl.
  rewrite strict_curry_app'; auto.
  rewrite flat_cases_elem'.
  rewrite strict_curry_app2'; auto.
  rewrite strict_curry_app2'; auto.
  repeat rewrite PLT.pair_commute2. auto.
  apply flat_elem'_semvalue.

  destruct (value_app_inv _ _ _ _ H). clear H1 H2.
  simpl.    
  unfold Ysem.
  rewrite strict_curry_app'; auto.
  rewrite fixes_unroll at 1. unfold Ybody at 1.
  rewrite PLT.curry_apply2.
  rewrite <- (cat_assoc PLT).
  rewrite PLT.pair_commute2.
  rewrite <- (cat_assoc PLT).
  rewrite strict_curry_app2'; auto.
  rewrite <- (cat_assoc PLT).
  apply cat_respects. auto.
  rewrite (PLT.pair_compose_commute false).
  rewrite PLT.pair_commute2.
  apply PLT.pair_eq. 2: auto.
  rewrite <- (cat_assoc PLT).
  apply cat_respects. auto.
  rewrite (PLT.pair_compose_commute false).
  apply PLT.pair_eq.
  rewrite <- (cat_assoc PLT).
  rewrite PLT.pair_commute1.
  rewrite (cat_assoc PLT).
  rewrite PLT.pair_commute1.
  apply cat_ident2.
  rewrite <- (cat_assoc PLT).
  rewrite PLT.pair_commute1.
  rewrite (cat_assoc PLT).
  rewrite PLT.pair_commute2.
  rewrite <- (cat_assoc PLT).
  rewrite PLT.pair_commute2.
  auto.
  apply (value_semvalue _ f). auto.
Qed.

(** This leads easily into the soundness of evaluation.
  *)
Lemma soundness : forall τ (m z:term τ),
  eval τ m z -> 〚m〛≈〚z〛.
Proof.
  intros. induction H; simpl; auto.
  rewrite IHeval1.
  rewrite IHeval2.
  rewrite <- IHeval3.
  apply redex_soundness.
  eapply eval_value; eauto.
  eapply eval_value; eauto.
  auto.
  rewrite IHeval1.
  rewrite IHeval2.
  auto.
Qed.


(** * The logical relations lemma

    Now define the logical relation for adequacy.  It is defined
    by induction on the structure of types, in a standard way.    
  *)
Fixpoint LR (τ:ty) : 
  term τ -> (1 → U (tydom τ)) -> Prop :=
  match τ as τ' return 
    term τ' -> (1 → U (tydom τ')) -> Prop
  with
  | ty_bool => fun m h => exists b:bool,
        m = tbool b /\ h ≈ flat_elem' b
  | ty_arrow σ₁ σ₂ => fun m h =>
        forall n h',
          LR σ₁ n h' -> value n -> semvalue h' ->
          semvalue (strict_app' ∘ 〈h, h'〉) ->
          exists z,
            eval _ (m • n) z /\
            LR σ₂ z (strict_app' ∘ 〈h, h'〉)
  end.

Lemma LR_equiv τ : forall m h h',
  h ≈ h' -> LR τ m h -> LR τ m h'.
Proof.
  induction τ; simpl. intros.
  destruct H0 as [b [??]]. exists b; split; auto.
  rewrite <- H; auto.
  simpl; intros.
  destruct (H0 n h'0 H1 H2 H3) as [z [??]]; auto.
  revert H4. apply semvalue_equiv.
  apply cat_respects; auto.
  apply PLT.pair_eq; auto.
  exists z.
  split; auto.
  revert H6. apply IHτ2.
  apply cat_respects; auto.
  apply PLT.pair_eq; auto.
Qed.

(**  Now we need a host of auxilary definitions to state
     the main lemmas regarding the logical relation.  These
     definitions allow us to apply an arbitrary number of
     arguments to a syntactic term and to the denotation of terms.
  *)
Fixpoint lrtys (ts:list ty) (z:ty) :=
  match ts with
  | nil => z
  | t::ts' => (t ⇒ (lrtys ts' z))%ty
  end.

Fixpoint lrsyn (ts:list ty) : Type :=
  match ts with
  | nil => unit
  | t::ts' => prod (lrsyn ts') (term t)
  end.

Fixpoint lrsem (ts:list ty) : Type :=
  match ts with
  | nil => unit
  | t::ts' => prod (lrsem ts') (1 → U (tydom t))
  end.

Fixpoint lrhyps (ls:list ty) : lrsyn ls -> lrsem ls -> Prop :=
  match ls with
  | nil => fun _ _ => True
  | t::ts => fun xs ys =>
    (eval _ (snd xs) (snd xs) /\ semvalue (snd ys)) /\
    LR t (snd xs) (snd ys) /\ lrhyps ts (fst xs) (fst ys)
  end.

Fixpoint lrapp (ls:list ty) z : lrsyn ls -> term (lrtys ls z) -> term z :=
  match ls as ls' return lrsyn ls' -> term (lrtys ls' z) -> term z with
  | nil => fun _ m => m
  | t::ts => fun xs m => lrapp ts _ (fst xs) (m • (snd xs))
  end.

Fixpoint lrsemapp (ls:list ty) z :
  lrsem ls -> (1 → U (tydom (lrtys ls z))) -> (1 → U (tydom z)) :=
  match ls as ls' return
    lrsem ls' -> (1 → U (tydom (lrtys ls' z)))  -> (1 → U (tydom z))
  with
  | nil => fun _ h => h
  | t::ts => fun ys h => lrsemapp ts _ (fst ys) (strict_app' ∘ 〈h, snd ys〉)
  end.

Lemma eval_lrapp_congruence ls : forall xs τ m m' z,
  (forall q, eval _ m q -> eval _ m' q) ->
  eval τ (lrapp ls τ xs m) z ->
  eval τ (lrapp ls τ xs m') z.
Proof.
  induction ls; simpl; intros.
  apply H. auto.
  fold lrtys in *.

  revert H0. apply IHls.
  intros.
  inv H0.
  apply H in H6.
  eapply eapp1; eauto.
  eapply eapp2; eauto.
Qed.

Lemma lrsemapp_equiv ls : forall τ ys h h',
  h ≈ h' -> lrsemapp ls τ ys h ≈ lrsemapp ls τ ys h'.
Proof.
  induction ls; simpl; intros; auto.
  apply IHls.
  apply cat_respects; auto.
  apply PLT.pair_eq; auto.
Qed.

Lemma semvalue_lrsemapp_out ls : forall τ ys h,
   semvalue (lrsemapp ls τ ys h) -> semvalue h.
Proof.
  induction ls; simpl; intros; auto.
  apply IHls in H.
  apply semvalue_app_out1' in H. auto.
Qed.

(**  This fact is important in the base cases of the fundamental lemma; it allows
     unwind a stack of applications.
  *)
Lemma LR_under_apply ls :
   forall (τ : ty) (m z0 : term (lrtys ls τ)) (xs : lrsyn ls) 
     (ys : lrsem ls) (h : 1 → U (tydom (lrtys ls τ))),
   eval (lrtys ls τ) m z0 ->
   lrhyps ls xs ys ->
   semvalue (lrsemapp ls τ ys h) ->
   LR (lrtys ls τ) z0 h ->
   exists z : term τ,
     eval τ (lrapp ls τ xs m) z /\ LR τ z (lrsemapp ls τ ys h).
Proof.
  induction ls; simpl; intros.
  exists z0. split; auto.
  destruct xs as [xs x].
  destruct ys as [ys y]. simpl in *.
  destruct H0 as [[??][??]].
  destruct (H2 x y) as [z1 [??]]; auto.
  apply semvalue_lrsemapp_out in H1. auto.

  generalize (IHls τ (tapp z0 x) z1 xs ys (strict_app' ∘ PLT.pair h y)
     H6 H5 H1 H7).
  intros [q [??]].
  exists q; split; auto.
  revert H8.
  apply eval_lrapp_congruence. intro.
  apply eval_app_congruence; auto.
  fold lrtys. intros.
  apply eval_trans with z0; auto.
Qed.


(**  If a sup is a semantic value, then there is some element of the
     set is a semantic value.
  *)
Lemma semvalue_sup (B:∂PLT) (XS:dirset (PLT.homset_cpo _ 1 (U B))) : 
  semvalue (∐XS) -> exists x, x ∈ XS /\ semvalue x.
Proof.
  intros.
  destruct (H tt) as [q ?].
  simpl in H0.
  apply union_axiom in H0.
  destruct H0 as [q' [??]].
  apply image_axiom2 in H0.
  destruct H0 as [q'' [??]].
  simpl in *.
  exists q''. split; auto.
  red; intro. destruct g.
  exists q. rewrite <- H2; auto.
Qed.

(**  The logical relation is admissible.  This is key to the
     fundamental lemma case for Y.
  *)
Lemma LR_admissible τ : 
  forall m (XS:dirset (PLT.homset_cpo _ 1 (U (tydom τ)))),
  semvalue (∐XS) ->
  (forall x, x ∈ XS -> semvalue x -> LR τ m x) -> LR τ m (∐XS).
Proof.
  induction τ; simpl. intros.

  apply semvalue_sup in H. destruct H as [x [??]].
  destruct (H0 x) as [b [??]]; auto.
  subst m. exists b. split; auto.
  split.
  apply CPO.sup_is_least.
  hnf; simpl; intros.
  destruct (proj2_sig XS (x::x0::nil)). hnf; auto.
  hnf; intros. apply cons_elem in H4.
  destruct H4. rewrite H4. auto.
  apply cons_elem in H4.
  destruct H4. rewrite H4. auto.
  apply nil_elem in H4. elim H4.
  destruct H4.
  assert (x1 ≈ x).  
  assert (x ≤ x1). apply H4. apply cons_elem; auto.
  split; auto.
  hnf; intros.
  rewrite H3 in H6.
  assert ((tt,Some b : U (flat enumbool)) ∈ PLT.hom_rel x1).
  apply H6.
  unfold flat_elem'.
  apply PLT.compose_hom_rel. exists (Some tt).
  split. simpl. apply adj_unit_rel_elem. auto.
  apply U_hom_rel. right.
  exists tt. exists b. split; auto.
  apply PLT.compose_hom_rel; auto.
  exists tt. split.
  simpl. apply eprod_elem. split; simpl.
  apply eff_complete. apply single_axiom; auto.
  simpl. apply single_axiom; auto.
  destruct H3. apply H9.
  destruct a. unfold flat_elem'.
  apply PLT.compose_hom_rel.
  exists (Some tt). split.
  simpl. apply adj_unit_rel_elem; simpl; auto.
  destruct c; auto.
  apply U_hom_rel.
  destruct c0; auto. right.
  exists tt. exists c0. split; auto.
  apply PLT.compose_hom_rel.
  exists tt.
  split. simpl.
  apply eprod_elem. split.
  apply eff_complete. apply single_axiom; auto.
  simpl. apply single_axiom; auto.
  cut (c0 = b). intros. subst c0; auto.
  destruct c.
  destruct (PLT.hom_directed _ _ _ x1 tt ((Some c0::Some b::nil))).
  hnf; auto.
  red; intros.
  apply cons_elem in H10. destruct H10. rewrite H10.
  apply erel_image_elem. auto.
  apply cons_elem in H10. destruct H10. rewrite H10.
  apply erel_image_elem. auto.
  apply nil_elem in H10. elim H10.
  destruct H10.
  assert (Some c0 ≤ x2).
  apply H10. apply cons_elem; auto.
  assert (Some (b:enumbool) ≤ x2).
  apply H10. apply cons_elem. right. apply cons_elem; auto.
  destruct x2. hnf in H12. hnf in H13.
  subst c0. subst b. auto.
  elim H12.

  rewrite <- H3. rewrite <- H6.
  apply H4.
  apply cons_elem. right.
  apply cons_elem. auto.
  apply CPO.sup_is_ub. rewrite <- H3. auto.

  simpl; intros.
  set (g := (postcompose _ strict_app' ∘ pair_left (U (tydom (τ1 ⇒ τ2))) h')).
  assert (strict_app' ∘ PLT.pair (∐XS) h' ≈ g (∐XS)).
  simpl; auto.
  assert (strict_app' ∘ PLT.pair (∐XS) h' ≈ ∐(image g XS)).
  rewrite H5.
  apply CPO.continuous_sup'.
  apply continuous_sequence.
  apply postcompose_continuous.
  apply pair_left_continuous.

  assert (exists q, q ∈ XS /\
    semvalue (strict_app' ∘ PLT.pair q h')).
  rewrite H6 in H4.
  destruct (H4 tt) as [q ?].
  simpl.
  simpl in H7.
  apply union_axiom in H7.
  destruct H7 as [q' [??]].
  apply image_axiom2 in H7.
  destruct H7 as [q'' [??]].
  apply image_axiom2 in H7.
  destruct H7 as [q''' [??]].
  exists q'''. split; auto.
  rewrite H9 in H8.
  rewrite H10 in H8.
  red; intros.
  exists q. auto.
  destruct H7 as [q [??]].
  assert (semvalue q).
  apply semvalue_app_out1' in H8. auto.
  destruct (H0 q H7 H9 n h' H1 H2 H3 H8) as [z [??]].
  exists z. split; auto.
  cut (LR τ2 z (∐(image g XS))).
  apply LR_equiv; auto.
  apply IHτ2; auto.
  rewrite <- H6. auto.

  intros.
  apply image_axiom2 in H12. destruct H12 as [y [??]].
  rewrite H14 in H13.
  simpl in H13.
  assert (semvalue y).
  apply semvalue_app_out1' in H13. auto.
  destruct (H0 y H12 H15 n h' H1 H2 H3) as [z' [??]]; auto.
  assert (z = z').
  eapply eval_eq; eauto. subst z'.
  revert H17.
  apply LR_equiv; auto.
Qed.

(**  Here we prove fundamental lemma case for he main body
     of Y combinator. This proof goes by Scott induction.
  *)
Lemma LR_Ybody σ₁ σ₂
  (f:term ((σ₁ ⇒ σ₂) ⇒ σ₁ ⇒ σ₂)) hf :
  LR _ f hf -> value f -> semvalue hf ->

  forall (x:term σ₁) hx,
    LR σ₁ x hx -> value x -> semvalue hx ->
    semvalue (strict_app' ∘ 〈fixes (Ybody σ₁ σ₂) ∘ hf, hx〉) ->
    exists z:term σ₂,
      eval _ (tY σ₁ σ₂ • f • x) z /\
      LR _ z (strict_app' ∘ 〈fixes (Ybody σ₁ σ₂) ∘ hf, hx〉).
Proof.
  intros Hf1 Hf2 Hf3. unfold fixes.
  apply scott_induction.
  split.

  intros.
  apply semvalue_app_out1' in H2.
  destruct (H2 tt) as [q ?].
  apply (PLT.compose_hom_rel _ _ _ _ hf ⊥ tt (Some q)) in H3.
  destruct H3 as [?[??]].  
  elimtype False. revert H4.
  clear. simpl bottom.
  intros.
  unfold plt_hom_adj' in H4; simpl in H4.
  apply PLT.compose_hom_rel in H4.
  destruct H4 as [?[??]].
  simpl in H. apply adj_unit_rel_elem in H.
  apply U_hom_rel in H0.
  destruct H0. discriminate.
  destruct H0 as [? [? [?[??]]]].
  subst x. inv H2.
  simpl in H0.
  apply union_axiom in H0.
  destruct H0 as [?[??]].
  apply image_axiom2 in H0.
  destruct H0 as [?[??]].
  apply empty_elem in H0. elim H0.

  intros.
  set (g := (postcompose _ strict_app' 
            ∘ pair_left (U (tydom (σ₁ ⇒ σ₂))) hx
            ∘ precompose _ hf)).
  assert (strict_app' ∘ PLT.pair (∐XS ∘ hf) hx ≈ g (∐XS)). auto.
  assert (strict_app' ∘ PLT.pair (∐XS ∘ hf) hx ≈ ∐(image g XS)).
  rewrite H5.

  apply CPO.continuous_sup'.
  apply continuous_sequence.
  apply continuous_sequence.
  apply postcompose_continuous.
  apply pair_left_continuous.
  apply precompose_continuous.

  assert (exists q, q ∈ XS /\
    semvalue (strict_app' ∘ PLT.pair (q ∘ hf) hx)).
  rewrite H6 in H4.
  apply semvalue_sup in H4.
  destruct H4 as [q [??]].
  apply image_axiom2 in H4 as [q' [??]].
  exists q'. split; auto.
  simpl in H8. rewrite <- H8. auto.

  destruct H7 as [q [??]].
  destruct (H0 q H7 x hx H1 H2 H3) as [z [??]]; auto.
  exists z.
  split; auto.
  cut (LR σ₂ z (∐(image g XS))).  
  apply LR_equiv; auto.
  apply LR_admissible.
  rewrite <- H6; auto.
  intros.
  apply image_axiom2 in H11. destruct H11 as [y [??]].
  simpl in H13. rewrite H13 in H12.
  destruct (H0 y H11 x hx) as [z' [??]]; auto.
  assert (z = z'). eapply eval_eq; eauto. subst z'.
  revert H15; apply LR_equiv; auto.  

  intros.
  destruct (H0 x0 hx H1 H2 H3) as [z [??]]. rewrite H; auto.
  exists z; split; auto.
  revert H6. apply LR_equiv. rewrite <- H; auto.

  intros.
  simpl in H3. unfold fixes_step in H3.
  unfold Ybody in H3.
  rewrite PLT.curry_apply2 in H3.
  rewrite <- (cat_assoc PLT) in H3.
  rewrite (PLT.pair_compose_commute false) in H3.
  rewrite strict_curry_app2' in H3.
  rewrite <- (cat_assoc PLT) in H3.
  rewrite (PLT.pair_compose_commute false) in H3.
  rewrite PLT.pair_commute2 in H3.  
  rewrite <- (cat_assoc PLT) in H3.
  rewrite (PLT.pair_compose_commute false) in H3.
  rewrite <- (cat_assoc PLT) in H3.
  rewrite PLT.pair_commute1 in H3.
  rewrite PLT.pair_commute1 in H3.
  rewrite <- (cat_assoc PLT) in H3.
  rewrite PLT.pair_commute1 in H3.
  rewrite PLT.pair_commute2 in H3.
  rewrite (cat_ident2 PLT) in H3.    

  simpl in Hf1.
  destruct (Hf1 (tapp (tY _ _) f) (x ∘ hf)) as [q [??]]; auto.
  apply eapp2. apply eY. auto.
  intros [? Hr]; inv Hr.
  apply semvalue_app_out1' in H3.
  apply semvalue_app_out2' in H3. auto.
  apply semvalue_app_out1' in H3. auto.
  destruct (H5 x0 hx) as [q' [?]]; auto.
  exists q'. split.
  eapply eapp1.
  apply eapp2. apply eY. eauto.
  intros [? Hr]; inv Hr. eauto.
  apply redex_Y.
  revert H6. apply eval_app_congruence; intros; auto.
  apply eval_trans with q; auto.
  revert H7. apply LR_equiv.
  simpl. unfold fixes_step. unfold Ybody.
  symmetry.

  rewrite PLT.curry_apply2.
  rewrite <- (cat_assoc PLT).
  rewrite (PLT.pair_compose_commute false).
  rewrite strict_curry_app2'.
  rewrite <- (cat_assoc PLT).
  rewrite (PLT.pair_compose_commute false).
  rewrite PLT.pair_commute2.
  rewrite <- (cat_assoc PLT).
  rewrite (PLT.pair_compose_commute false).
  rewrite <- (cat_assoc PLT).
  rewrite PLT.pair_commute1.
  rewrite PLT.pair_commute1.
  rewrite <- (cat_assoc PLT).
  rewrite PLT.pair_commute1.
  rewrite PLT.pair_commute2.
  rewrite (cat_ident2 PLT).
  auto.
  apply semvalue_app_out2' in H3. auto.
  auto.
Qed.

(**  Now we can prove the fundamental lemma cases for
     each of the system combinators.
  *)

Lemma LR_I σ : LR _ (tI σ) 〚tI σ〛.
Proof.
  simpl. intros.
  exists n. split.
  eapply eapp1.
  apply eI. apply H0.
  apply redex_I. auto.
  revert H. apply LR_equiv. rewrite strict_curry_app'.
  rewrite PLT.pair_commute2; auto. auto.
Qed.

Lemma LR_K σ₁ σ₂ : LR _ (tK σ₁ σ₂) 〚tK σ₁ σ₂〛.
Proof.
  simpl. intros.

  exists (tapp (tK _ _) n). split.
  apply eapp2. apply eK. auto.
  intros [? Hr]. inv Hr.
  intros.
  exists n. split.
  eapply eapp1. 
  apply eapp2. apply eK. eauto.
  intros [? Hr]. inv Hr. eauto.
  apply redex_K. auto.
  revert H.
  apply LR_equiv.
  rewrite strict_curry_app'.
  rewrite strict_curry_app2'.
  rewrite <- (cat_assoc PLT).
  rewrite PLT.pair_commute1.
  rewrite PLT.pair_commute2.
  auto. auto. auto.
Qed.

Lemma LR_S σ₁ σ₂ σ₃ : LR _ (tS σ₁ σ₂ σ₃) 〚tS σ₁ σ₂ σ₃〛.
Proof.
  simpl; intros.
  exists (tapp (tS _ _ _) n). split.
  apply eapp2. apply eS. auto.
  intros [? Hr]; inversion Hr.
  intros.
  exists (tapp (tapp (tS _ _ _) n) n0). split.
  apply eapp2. apply eapp2. apply eS. auto.
  intros [? Hr]; inversion Hr. auto.
  intros [? Hr]; inversion Hr. intros.

  assert ( (strict_app'
           ∘ PLT.pair
               (strict_app'
                ∘ PLT.pair
                    (strict_app'
                     ∘ PLT.pair
                         (strict_curry'
                            (strict_curry'
                               (strict_curry'
                                  (strict_app'
                                   ∘ PLT.pair
                                       (strict_app'
                                        ∘ PLT.pair (π₂ ∘ π₁ ∘ π₁) π₂)
                                       (strict_app'
                                        ∘ PLT.pair (π₂ ∘ π₁) π₂))))) h')
                    h'0) h'1)
     ≈
     strict_app' ∘ PLT.pair
       (strict_app' ∘ PLT.pair h' h'1)
       (strict_app' ∘ PLT.pair h'0 h'1)).
  clear -H9 H5 H1.

  rewrite strict_curry_app'; auto.
  rewrite strict_curry_app2'; auto.
  rewrite strict_curry_app2'; auto.
  rewrite <- (cat_assoc PLT).
  apply cat_respects. auto.
  rewrite <- (cat_assoc PLT).
  rewrite (PLT.pair_compose_commute false).
  apply PLT.pair_eq.
  rewrite <- (cat_assoc PLT).
  rewrite (PLT.pair_compose_commute false).
  apply cat_respects. auto.
  apply PLT.pair_eq.
  rewrite <- (cat_assoc PLT).
  rewrite <- (cat_assoc PLT).
  rewrite PLT.pair_commute1.
  rewrite PLT.pair_commute1.
  rewrite PLT.pair_commute2.
  auto.
  rewrite PLT.pair_commute2.
  auto.
  rewrite <- (cat_assoc PLT).
  apply cat_respects; auto.
  rewrite (PLT.pair_compose_commute false).
  apply PLT.pair_eq; auto.
  rewrite <- (cat_assoc PLT).
  rewrite PLT.pair_commute1.
  rewrite PLT.pair_commute2.
  auto.
  rewrite PLT.pair_commute2.
  auto.

  rewrite H11 in H10.

  destruct (H3 n1 h'1) as [z0 [??]]; auto; clear H3.
  apply semvalue_app_out2' in H10. auto.
  
  destruct (H n1 h'1) as [z1 [??]]; auto; clear H.
  apply semvalue_app_out1' in H10. auto.
  
  destruct (H14 z0 (strict_app' ∘ PLT.pair h'0 h'1)) as [z2 [??]]; auto; clear H14.
  eapply eval_value; eauto.
  apply semvalue_app_out2' in H10. auto.
  exists z2. split.
  eapply eapp1. 
  apply eapp2. apply eapp2. apply eS. eauto.
  intros [? Hr]; inv Hr. eauto.
  intros [? Hr]; inv Hr. eauto.
  apply redex_S.
  revert H.
  apply eval_app_congruence.
  intros. apply eval_trans with z1; auto.
  intros. apply eval_trans with z0; auto.
  revert H15. apply LR_equiv. auto.
Qed.

Lemma LR_Y σ₁ σ₂ : LR _ (tY σ₁ σ₂) 〚tY σ₁ σ₂〛.
Proof.
  simpl; intros.
  exists (tapp (tY _ _) n).
  split. apply eapp2. apply eY. auto.
  intros [? Hr]; inv Hr.
  intros.  
  unfold Ysem in H6.
  rewrite strict_curry_app' in H6.
  rewrite <- (cat_assoc PLT) in H6.
  rewrite PLT.pair_commute2 in H6.
  destruct (LR_Ybody σ₁ σ₂ n h' H H0 H1 n0 h'0 H3 H4 H5 H6) as [z [??]].
  exists z. split; auto.
  revert H8. apply LR_equiv.
  unfold Ysem.
  rewrite strict_curry_app'.
  rewrite <- (cat_assoc PLT).
  rewrite PLT.pair_commute2.
  auto.
  auto.
  auto.
Qed.

Lemma LR_IF σ : LR _ (tIF σ) 〚tIF σ〛.
Proof.
  simpl. intros.
  exists (tapp (tIF σ) n). split. apply eapp2.
  apply eIF. auto.
  intros [? Hr]. inv Hr.
  intros.
  exists (tapp (tapp (tIF σ) n) n0). split. apply eapp2. apply eapp2.
  apply eIF. auto.
  intros [? Hr]. inv Hr. auto.
  intros [? Hr]. inv Hr.
  intros.
  destruct H as [b [??]]. subst n.
  destruct b.
  exists n0. split.
  eapply eapp1. apply eapp2. apply eapp2.
  apply eIF. apply ebool.
  intros [? Hr]. inv Hr. eauto.
  intros [? Hr]. inv Hr. eauto.
  econstructor. auto.
  revert H3. apply LR_equiv.
  rewrite H11.
  rewrite strict_curry_app'; auto.
  rewrite flat_cases_elem'.
  rewrite strict_curry_app2'; auto.
  rewrite strict_curry_app2'; auto.
  rewrite <- (cat_assoc PLT).
  rewrite PLT.pair_commute1.
  rewrite PLT.pair_commute2. auto.
  apply flat_elem'_semvalue.  
  exists n1.
  split.
  eapply eapp1. apply eapp2. apply eapp2.
  apply eIF. apply ebool.
  intros [? Hr]. inv Hr. eauto.
  intros [? Hr]. inv Hr. eauto.
  econstructor. auto.
  revert H7. apply LR_equiv.
  rewrite H11.
  rewrite strict_curry_app'; auto.
  rewrite flat_cases_elem'.
  rewrite strict_curry_app2'; auto.
  rewrite strict_curry_app2'; auto.
  rewrite PLT.pair_commute2. auto.
  apply flat_elem'_semvalue.  
Qed.

(**  Now the fundamental lemma follows by induction on terms.
  *)
Lemma fundamental_lemma : forall σ (n:term σ) ls τ m xs ys
  (Hσ : σ = lrtys ls τ),
  eq_rect σ term n (lrtys ls τ) Hσ = m ->
  lrhyps ls xs ys ->
  semvalue (lrsemapp ls τ ys 〚m〛) ->
  exists z,
    eval _ (lrapp ls τ xs m) z /\
    LR τ z (lrsemapp ls τ ys 〚m〛).
Proof.
  induction n; intros.

  (* bool case *)
  destruct ls; simpl in *. subst τ.
  simpl in H. subst m.
  exists (tbool b).
  split. apply ebool.
  simpl.
  exists b. split; auto.
  inv Hσ.

  (* application case *)
  subst σ₂. simpl in H. subst m.

  destruct (IHn2 nil σ₁ n2 tt tt (Logic.refl_equal _) (Logic.refl_equal _) I)
    as [q2[??]]; auto.
  simpl.
  apply semvalue_lrsemapp_out in H1. simpl in H1.
  apply semvalue_app_out2' in H1. auto.

  destruct (IHn1 (σ₁::ls) _ n1 (xs, q2) (ys, denote σ₁ q2)
    (Logic.refl_equal _) (Logic.refl_equal _)) as [q1 [??]].
  simpl; intuition. eapply eval_value; eauto.
  apply value_semvalue; auto.  eapply eval_value; eauto.
  revert H2. apply LR_equiv.
  simpl. simpl in H. apply soundness; auto.
  simpl. revert H1.
  apply semvalue_equiv.
  apply lrsemapp_equiv.
  simpl. apply cat_respects; auto.
  apply PLT.pair_eq; auto.
  apply soundness; auto.
  exists q1. split.
  revert H3.
  simpl. apply eval_lrapp_congruence.
  intro q. apply eval_app_congruence; intros; auto.
  apply eval_trans with q2; auto.
  revert H4.
  apply LR_equiv. simpl.
  apply lrsemapp_equiv.
  simpl. apply cat_respects; auto.
  apply PLT.pair_eq; auto.
  symmetry.
  apply soundness; auto.
  
  (* I case *)
  cut (exists z : term (σ ⇒ σ),
     eval (σ ⇒ σ) (tI σ) z /\ LR (σ ⇒ σ) z (denote _ (tI _))).
  revert H.
  generalize (tI σ).
  generalize Hσ.
  rewrite Hσ. intro H.
  replace H with (refl_equal (lrtys ls τ)). simpl. intros.
  subst t. clear H Hσ. 
  destruct H3 as [z0 [??]].
  eapply LR_under_apply; eauto.
  apply Eqdep_dec.UIP_dec. decide equality.

  exists (tI σ). split. apply eI. apply LR_I.

  (* K case *)
  cut (exists z,
     eval _ (tK σ₁ σ₂) z /\ LR _ z (denote _ (tK _ _))).
  revert H.
  generalize (tK σ₁ σ₂).
  generalize Hσ.
  rewrite Hσ. intro H.
  replace H with (refl_equal (lrtys ls τ)). simpl. intros.
  subst t. clear H Hσ. 
  destruct H3 as [z0 [??]].
  eapply LR_under_apply; eauto.
  apply Eqdep_dec.UIP_dec. decide equality.

  exists (tK _ _). split. apply eK. apply LR_K.

  (* S case *)
  cut (exists z,
     eval _ (tS σ₁ σ₂ σ₃) z /\ LR _ z (denote _ (tS _ _ _))).
  revert H.
  generalize (tS σ₁ σ₂ σ₃).
  generalize Hσ.
  rewrite Hσ. intro H.
  replace H with (refl_equal (lrtys ls τ)). simpl. intros.
  subst t. clear H Hσ. 
  destruct H3 as [z0 [??]].
  eapply LR_under_apply; eauto.
  apply Eqdep_dec.UIP_dec. decide equality.

  exists (tS _ _ _).
  split. simpl. apply eS. apply LR_S.

  (* IF case *)
  cut (exists z,
     eval _ (tIF σ) z /\ LR _ z (denote _ (tIF _))).
  revert H.
  generalize (tIF σ).
  generalize Hσ.
  rewrite Hσ. intro H.
  replace H with (refl_equal (lrtys ls τ)). simpl. intros.
  subst t. clear H Hσ. 
  destruct H3 as [z0 [??]].
  eapply LR_under_apply; eauto.
  apply Eqdep_dec.UIP_dec. decide equality.

  exists (tIF σ). split. apply eIF.
  apply LR_IF.

  (* Y case *)
  cut (exists z,
     eval _ (tY σ₁ σ₂) z /\ LR _ z (denote _ (tY σ₁ σ₂))).
  revert H.
  generalize (tY σ₁ σ₂).
  generalize Hσ.
  rewrite Hσ. intro H.
  replace H with (refl_equal (lrtys ls τ)). simpl. intros.
  subst t. clear H Hσ. 
  destruct H3 as [z0 [??]].
  eapply LR_under_apply; eauto.
  apply Eqdep_dec.UIP_dec. decide equality.

  exists (tY σ₁ σ₂). split. apply eY. apply LR_Y.
Qed.

(**  A specialization of the fundamental lemma to empty contexts
  *)
Lemma fundamental_lemma' : forall τ (m:term τ),
  semvalue 〚m〛 ->
  exists z, eval τ m z /\ LR τ z 〚m〛.
Proof.
  intros.
  apply (fundamental_lemma τ m nil τ m tt tt (refl_equal _) (refl_equal _) I).
  simpl. auto.
Qed.

(** * Contextual equivalance and the adequacy theorem.

     Now we define contextual equivalance.  Contexts here are
     given in "inside-out" form, which makes the induction in the
     adequacy proof significantly easier.
  *)
Inductive context τ : ty -> Type :=
  | cxt_top : context τ τ
  | cxt_appl : forall σ₁ σ₂,
                    term σ₁ ->
                    context τ σ₂ ->
                    context τ (σ₁ ⇒ σ₂)
  | cxt_appr : forall σ₁ σ₂,
                    term (σ₁ ⇒ σ₂) ->
                    context τ σ₂ ->
                    context τ σ₁.

Fixpoint plug τ σ (C:context τ σ) : term σ -> term τ :=
  match C in context _ σ return term σ -> term τ with
  | cxt_top => fun x => x
  | cxt_appl σ₁ σ₂ t C' => fun x => plug τ _ C' (tapp x t)
  | cxt_appr σ₁ σ₂ t C' => fun x => plug τ _ C' (tapp t x)
  end.

Definition cxt_eq τ σ (m n:term σ):=
  forall (C:context τ σ) (z:term τ),
    eval τ (plug τ σ C m) z <-> eval τ (plug τ σ C n) z.

(**  Adequacy means that terms with equivalant denotations
     are contextually equivalant in any boolean context.
  *)
Theorem adequacy : forall τ (m n:term τ),
  〚m〛≈〚n〛 -> cxt_eq 2 τ m n.
Proof.
  intros. intro.
  revert n m H.
  induction C.

  simpl; intros.
  split; intros.
  destruct (fundamental_lemma' _ m) as [zm [??]].
  simpl.
  apply semvalue_equiv with (denote _ z).
  symmetry. apply soundness. auto.
  apply value_semvalue. eapply eval_value; eauto.
  destruct (fundamental_lemma' _ n) as [zn [??]].
  simpl.
  apply semvalue_equiv with (denote _ z).
  symmetry. rewrite <- H.
  apply soundness. auto.
  apply value_semvalue. eapply eval_value; eauto.
  destruct H2 as [b [??]].
  destruct H4 as [b' [??]].
  simpl in *.
  rewrite H in H5. rewrite H5 in H6.
  assert (b = b').
  apply flat_elem'_inj in H6. auto.
  exact tt.
  subst b'.
  subst zm zn.
  assert (z = (tbool b)).
  eapply eval_eq; eauto.
  subst z. auto.

  destruct (fundamental_lemma' _ m) as [zm [??]].
  simpl.
  apply semvalue_equiv with (denote _ z).
  symmetry. rewrite H. apply soundness. auto.
  apply value_semvalue. eapply eval_value; eauto.
  destruct (fundamental_lemma' _ n) as [zn [??]].
  simpl.
  apply semvalue_equiv with (denote _ z).
  symmetry. apply soundness. auto.
  apply value_semvalue. eapply eval_value; eauto.
  destruct H2 as [b [??]].
  destruct H4 as [b' [??]].
  simpl in *.
  rewrite H in H5. rewrite H5 in H6.
  assert (b = b').
  apply flat_elem'_inj in H6. auto.
  exact tt.
  subst b'.
  subst zm zn.
  assert (z = (tbool b)).
  eapply eval_eq; eauto.
  subst z. auto.

  simpl. intros.
  apply IHC. simpl.
  apply cat_respects; auto.
  apply PLT.pair_eq; auto.

  simpl; intros.
  apply IHC. simpl.
  apply cat_respects; auto.
  apply PLT.pair_eq; auto.
Qed.

(** Every term fails to evaluate iff the denotation is bottom. *)
Corollary denote_bottom_nonvalue : forall τ (m:term τ),
  (~exists z, eval τ m z) <-> 〚m〛 ≈ ⊥.
Proof.
  intros. split; intro.

  split. 2: apply bottom_least.
  hnf. intros [u x] Hx. destruct x.
  elimtype False.
  destruct (fundamental_lemma' τ m) as [z [??]].
  red; intros. destruct g. simpl.
  exists c. auto.
  elim H. eauto.
  apply PLT.compose_hom_rel.    
  simpl. exists None.
  split.
  apply adj_unit_rel_elem. hnf; auto.
  apply U_hom_rel. auto.

  intros [z ?].
  assert (denote τ z ≈ ⊥).
  rewrite <- soundness; eauto.
  assert (value z).
  eapply eval_value; eauto.
  apply value_semvalue in H2.
  hnf in H2.
  destruct (H2 tt) as [x ?].
  destruct H1. apply H1 in H3.
  simpl bottom in H3.
  apply (PLT.compose_hom_rel) in H3.
  destruct H3 as [q [??]].
  apply U_hom_rel in H5.
  destruct H5.
  inversion H5.
  destruct H5 as [q' [?[??]]].
  simpl in H5.
  apply union_axiom in H5.
  destruct H5 as [?[??]].
  apply image_axiom2 in H5. destruct H5 as [? [??]].
  apply empty_elem in H5. elim H5.
Qed.   

(** These should print "Closed under the global context", meaning these
    theorems hold without the use of any axioms.
  *)
Print Assumptions adequacy.
Print Assumptions denote_bottom_nonvalue.