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

root / plotkin.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
(* Copyright (c) 2014, Robert Dockins *)

Require Import List.

Require Import basics.
Require Import preord.
Require Import categories.
Require Import sets.
Require Import finsets.
Require Import esets.
Require Import effective.
Require Import directed.

(**  * Plotkin orders and normal sets.

     A plotkin order is a preorder where every conditionally-inhabited,
     bounded finite set has a minimal upper bound; and where every
     finite set has a finite MUB closure.
 
     The Plotkin orders are alternately characterized has having
     finite normal sets.  A set X is normal if, for every z,
     the set { x | x ∈ X ∧ x ≤ z } is h-directed.  A preorder is
     Plotkin iff every conditionally-inhabted finite set has an
     enclosing finite normal set.

     Demonstrating the existence of normal sets is generally easier
     than producing finite MUB closures, so that is our preferred
     method for demonstrating that an order is Plotkin.

     It might be better (following Gunter) to simply take the normal
     set definition as primary and drop the MUB closure definition
     altogether.  That might make other things more complicated,
     I'm not sure.  Anyway, it would mean major changes to difficult
     proofs, like those in joinable.v.
  *)


(**  A preorder is MUB complete if every bounded, h-inhabited finite
     set has a least upper bound below the given bound.
  *)
Definition is_mub_complete hf (A:preord) :=
  forall (M:finset A) (x:A), inh hf M -> upper_bound x M ->
    exists mub:A, minimal_upper_bound mub M /\ mub ≤ x.

(**  A set is MUB closed if it contains every MUB of every
     h-inhabited finite subset.
  *)
Definition mub_closed hf (A:preord) (X:finset A) :=
  forall M:finset A, inh hf M -> M ⊆ X ->
    forall x:A, minimal_upper_bound x M -> x ∈ X.

(**  A Plotkin order is MUB complete and has a finite MUB closure
     operation.  Note, we explicitly require mub_closure to be
     the smallest MUB closure operation.  It is thus uniquely
     determined.  In fact, this requirement is not strictly necessary;
     given an arbitrary MUB closure operation, we can compute the
     minimal one.
  *)
Record plotkin_order (hf:bool) (A:preord) :=
  PlotkinOrder
  { mub_complete : is_mub_complete hf A
  ; mub_closure : finset A -> finset A
  ; mub_clos_incl : forall M:finset A, M ⊆ mub_closure M
  ; mub_clos_mub : forall (M:finset A), mub_closed hf A (mub_closure M)
  ; mub_clos_smallest : forall (M X:finset A),
        M ⊆ X ->
        mub_closed hf A X -> 
        mub_closure M ⊆ X
  }.
Arguments mub_closure [hf] [A] p _.
Arguments mub_complete [hf] [A] p _ _ _ _.
Arguments mub_clos_incl [hf] [A] p _ _ _.
Arguments mub_clos_mub [hf] [A] p _ _ _ _ _ _.
Arguments mub_clos_smallest [hf] [A] p _ _ _ _ _ _.


(**  MUB-closure is actually a closure operation: it is
     monotone, inclusive and idempotent.
  *)
Lemma mub_clos_mono : forall hf A (H:plotkin_order hf A),
  forall (M N:finset A),
    M ⊆ N -> mub_closure H M ⊆ mub_closure H N.
Proof.
  intros.
  apply mub_clos_smallest; auto.
  apply incl_trans with finset_theory N; auto.
  apply mub_clos_incl.
  apply mub_clos_mub; eauto.
Qed.

Lemma mub_clos_idem : forall hf A (H:plotkin_order hf A), 
  forall (M:finset A),
    mub_closure H M ≈ mub_closure H (mub_closure H M).
Proof.
  intros. split.
  apply mub_clos_incl.
  apply mub_clos_smallest; auto.
  red; auto.
  apply mub_clos_mub; auto.
Qed.


(**  The empty preorder is Plotkin.
  *)
Program Definition empty_plotkin hf : plotkin_order hf emptypo :=
  PlotkinOrder hf emptypo _ (fun _ => nil) _ _ _.
Solve Obligations of empty_plotkin using (repeat intro; simpl in *; intuition).

(**  The unit preorder is Plotkin.
  *)
Program Definition unit_plotkin hf : plotkin_order hf unitpo :=
  PlotkinOrder hf _ _ (fun M => if hf then M else (tt::nil)) _ _ _.
Solve Obligations of unit_plotkin using (repeat intro; hnf; auto).
Next Obligation.
  repeat intro. exists tt.
  split; hnf; auto.
Qed.
Next Obligation.
  repeat intro.
  destruct hf.
  auto.
  destruct a. apply cons_elem; auto.
Qed.
Next Obligation.
  repeat intro.
  destruct hf.
  hnf in H. destruct H.
  destruct x0. destruct x. apply H0. auto.
  destruct x. apply cons_elem; auto.
Qed.
Next Obligation.
  repeat intro.
  destruct hf. apply H; auto.
  apply (H0 M); auto. red; auto.
  split; hnf; auto.
  repeat intro. hnf. auto.
Qed.
 
(**  When a preorder is effective Plotkin, it is decidable if an
     element is an upper bound or a minimal upper bound of a finite set.
  *)
Section dec_lemmas.
  Variable hf:bool.
  Variable A:preord.
  Variable Heff : effective_order A.
  Variable Hplt : plotkin_order hf A.

  Lemma upper_bound_dec : forall (M:finset A) (x:A),
    { upper_bound x M } + { ~upper_bound x M }.
  Proof.
    induction M; intros.
    left. red. intros. destruct H as [q [??]]. elim H.
    destruct (IHM x).
    destruct (eff_ord_dec A Heff a x).
    left.
    red. simpl; intros.
    destruct H as [q [??]].
    simpl in H. destruct H; subst.
    rewrite H0; auto.
    apply u. exists q; split; auto.
    right.
    intro. apply n.
    apply H.
    exists a. split; simpl; auto.
    right. intro.
    apply n.
    red; intros.
    apply H.
    destruct H0 as [q[??]].
    exists q; split; simpl; auto.
  Qed.

  Lemma mub_finset_dec : forall (M:finset A) (x:A) (Hinh:inh hf M),
    { minimal_upper_bound x M } + { ~minimal_upper_bound x M }.
  Proof.
    intros M x.
    destruct (upper_bound_dec M x).
    destruct (eff_in_dec Heff (mub_closure Hplt M) x).
    set (P b := upper_bound b M -> b ≤ x -> x ≤ b).
    destruct (finset_find_dec' A P) with (mub_closure Hplt M).
    subst P; simpl; intuition.
    rewrite <- H. apply H0.
    red; intros. rewrite H. apply H1. auto.
    rewrite H; auto.
    unfold P. simpl.
    intro b.
    destruct (upper_bound_dec M b).
    destruct (eff_ord_dec A Heff b x).
    destruct (eff_ord_dec A Heff x b).
    left. auto.
    right. intro.
    apply n. apply H; auto.
    left. intros. elim n. auto.
    left. intros. elim n. auto.

    destruct s.
    destruct a.
    red in H0.
    subst P. simpl in H0.
    right. intro.
    destruct H1.
    apply H0.
    intros.
    apply H2; auto.
    left.
    split; auto.
    intros.
    destruct (mub_complete Hplt M b) as [b0 [??]]; auto.
    transitivity b0; auto.
    apply p; auto.
    apply mub_clos_mub with M; auto.
    apply mub_clos_incl; auto.
    destruct H1; auto.
    transitivity b; auto.
    right. intro.
    apply n.
    apply mub_clos_mub with M; auto.
    apply mub_clos_incl; auto.
    right.
    intro. destruct H.
    apply n; auto.
  Qed.
End dec_lemmas.


Lemma upper_bound_ok : forall A (G:finset A) (x y:A),
  x ≈ y -> upper_bound x G -> upper_bound y G.
Proof.
  unfold upper_bound; intros.
  rewrite <- H. apply H0; auto.
Qed.

Lemma minimal_upper_bound_ok : forall A (G:finset A) (x y:A),
  x ≈ y -> minimal_upper_bound x G -> minimal_upper_bound y G.
Proof.
  unfold minimal_upper_bound. intros.
  destruct H0; split.
  eapply upper_bound_ok; eauto.
  intros. rewrite <- H. apply H1; auto.
  rewrite H; auto.
Qed.

(**  We introduce the alternate characterization of Plotkin orders
     and preorders posessing enough normal sets.  The Plotkin->normal
     direction of equivalance is easy, but the other direction is rather
     involved.
  *)
Section normal_sets.
  Variable A:preord.
  Variable Heff: effective_order A.
  Variable hf:bool.

  (**  A set X is normal if it is h-inhabited and, for abitrary z,
       the intersection of X with { x | x ≤ z } is directed.
    *)
  Definition normal_set (X:finset A) :=
    (inh hf X) /\
    forall z, directed hf 
      (finsubset A 
        (fun x => x ≤ z) 
        (fun x => eff_ord_dec A Heff x z)
        X).

  (**  A preorder "has" normal sets if every h-inhabited set is inclosed in
       some finite normal set.
    *)
  Definition has_normals :=
    forall (X:finset A) (Hinh:inh hf X),
      { Z:finset A | X ⊆ Z /\ normal_set Z }.
    

  (**  Plotkin orders have normal sets.
    *)
  Section plt_normal.
    Hypothesis Hplt : plotkin_order hf A.

    Lemma plt_has_normals : has_normals.
    Proof.
      red. intros X Xinh.
      exists (mub_closure Hplt X).
      split. apply mub_clos_incl.
      red; intros.
      split.
      apply inh_sub with X; auto.
      apply mub_clos_incl.
      red; simpl; intros.
      destruct (mub_complete Hplt M z); auto.
      red; intros.
      apply H in H0.
      apply finsubset_elem in H0.
      destruct H0; auto.
      intros. rewrite <- H1; auto.
      destruct H0.
      exists x. split; auto.
      destruct H0; auto.
      apply finsubset_elem.
      intros. rewrite <- H2; auto.
      split; auto.
      apply (mub_clos_mub Hplt X) with M; auto.
      red; intros.
      apply H in H2.
      apply finsubset_elem in H2.
      destruct H2; auto.
      intros. rewrite <- H3; auto.
    Qed.
  End plt_normal.

  (**  Given a finite subset X of a normal set Q, we can compute the (finite) set of
       all upper bounds of X that lie in Q.  Furthermore, for each upper bound of X,
       there is some upper bound of X below it in Q.
    *)
  Lemma normal_has_ubs Q :
    normal_set Q ->
    forall (X:finset A) (Hinh:inh hf X), X ⊆ Q ->
      { Y:finset A | Y ⊆ Q /\
        (forall y, y ∈ Y -> upper_bound y X) /\
        (forall z, upper_bound z X -> exists m, m ≤ z /\ m ∈ Y /\ upper_bound m X) }.
  Proof.
    intros. red in H.
    set (Y := finsubset A (fun x => upper_bound x X) (fun x => upper_bound_dec A Heff X x) Q).
    exists Y. split.
    unfold Y.
    red. intros.
    apply finsubset_elem in H1.
    destruct H1; auto.
    apply upper_bound_ok.
    split.
    intros.
    unfold Y in H1.
    apply finsubset_elem in H1.
    destruct H1; auto.
    apply upper_bound_ok.
    intros z Hz.
    destruct H as [HQ H].
    destruct (H z X); auto.
    red; intros.
    apply finsubset_elem.
    intros. rewrite <- H2; auto.
    split. apply H0; auto.
    apply Hz. auto.
    destruct H1.
    apply finsubset_elem in H2.
    destruct H2.
    exists x. intuition.
    unfold Y.
    apply finsubset_elem.
    apply upper_bound_ok.
    split; auto.
    intros. rewrite <- H3. auto.
  Qed.

  (**  Moreover, under the same conditions, we can calculate the set
       of minimal upper bounds of X.
    *)
  Section normal_mubs.
    Variable Q:finset A.
    Hypothesis H : normal_set Q.
    
    Variable X:finset A.
    Variable Hinh : inh hf X.
    Hypothesis H0 : X ⊆ Q.

    Let Y := proj1_sig (normal_has_ubs Q H X Hinh H0).
    Let H1 := proj1 (proj2_sig (normal_has_ubs Q H X Hinh H0)).
    Let H2 := proj2 (proj2_sig (normal_has_ubs Q H X Hinh H0)).

    Let P (x y:A) := (y ≤ x /\ x ≰ y).
    
    Lemma normal_mubs' : forall x, { z | z ∈ Y /\ P x z } + { forall z, z ∈ Y -> ~P x z }.
    Proof.
      intro x.
      apply (finset_find_dec A (P x)).
      clear; unfold P; intros.
      rewrite <- H. auto.
      unfold P.
      intro y.
      destruct (eff_ord_dec A Heff y x).
      destruct (eff_ord_dec A Heff x y).
      right. intros [??]. apply H4; auto.
      left. split; auto.
      right. intros [??]. apply n; auto.
    Qed.

    Lemma normal_sub_mub_dec : forall x, { minimal_upper_bound x X }+{~minimal_upper_bound x X}.
    Proof.
      intro x.
      destruct (normal_mubs' x).
      destruct s as [m [??]].
      red in H4.
      right. intro.
      destruct H4.
      apply H6.
      apply H5; auto.
      destruct H2. apply H2. auto.
      destruct (upper_bound_dec A Heff X x).
      left. red; intros.
      split; auto.
      intros.
      destruct H2.
      destruct (H6 b) as [m [?[??]]]; auto.
      destruct (eff_ord_dec A Heff x b); auto.
      elim (n m); auto.
      red. split; auto.
      transitivity b; auto.
      red; intros.
      apply n0.
      transitivity m; auto.
      right. intros [??]. contradiction.
    Qed.

    Lemma normal_has_mubs :
        { Y:finset A | Y ⊆ Q /\
          (forall y, y ∈ Y -> minimal_upper_bound y X) /\
          forall z, upper_bound z X -> exists m, m ≤ z /\ m ∈ Y /\ minimal_upper_bound m X }.
    Proof.
      exists (finsubset A (fun x => minimal_upper_bound x X) normal_sub_mub_dec Y).
      split.
      red; intros.
      apply finsubset_elem in H3.
      destruct H3.
      apply H1; auto.
      apply minimal_upper_bound_ok.
      split; intros.
      apply finsubset_elem in H3.
      destruct H3; auto.
      apply minimal_upper_bound_ok.
      destruct H2.
      destruct (H5 z) as [m [?[??]]]; auto.    
      cut (forall (Y1 Y2:finset A), (Y1++Y2)%list = Y -> forall m,
        (forall y, y ∈ Y1 -> y ≤ m -> m ≤ y) ->
        m ∈ Y2 -> m ≤ z -> exists m', m' ∈ Y2 /\ m' ≤ z /\ minimal_upper_bound m' X).
      intros.
      destruct (H9 nil Y) with m; auto.
      intros. destruct H10 as [?[??]]. elim H10.
      exists x. intuition.
      apply finsubset_elem.
      apply minimal_upper_bound_ok.
      split; auto.
      clear m H6 H7 H8.
      intros Y1 Y2. revert Y1. induction Y2; simpl; intros.
      rewrite <- List.app_nil_end in H6.
      destruct H8 as [?[??]].
      elim H8.
      destruct (eff_ord_dec A Heff a m).
      destruct (normal_mubs' a).
      destruct s as [m' [??]].
      destruct H11.
      assert (m' ∈ (Y2:finset A)).
      destruct H10 as [q [??]].
      rewrite <- H6 in H10.
      apply List.in_app_or in H10.
      destruct H10.
      elim H12.
      transitivity m; auto.
      apply H7; auto.
      exists q; split; auto.
      transitivity a; auto.
      destruct H10.
      subst q.
      elim H12. rewrite H13. auto.
      exists q; split; auto.

      destruct (IHY2 (Y1 ++ a::nil)%list) with m'.
      rewrite List.app_ass.
      simpl. auto.
      intros.
      destruct H14 as [p [??]].
      apply List.in_app_or in H14.
      destruct H14.
      transitivity a; auto.
      transitivity m; auto.
      apply H7; auto.
      exists p; split; auto.
      transitivity m'; auto.
      transitivity a; auto.
      simpl in H14. intuition subst.
      rewrite H16; auto.
      auto.
      transitivity m; auto.
      transitivity a; auto.
      exists x.
      intuition.
      destruct H2 as [p [??]]. exists p; split; simpl; auto.
      exists a. split.
      exists a; split; simpl; auto. split; auto.
      transitivity m; auto.
      split.
      apply H2.
      fold Y.
      rewrite <- H6.
      exists a; split; simpl; auto.
      apply List.in_or_app; auto.
      right; simpl; auto.
      intros.
      destruct (eff_ord_dec A Heff a b); auto.
      destruct (H5 b) as [q [??]]; auto.
      destruct H13.
      elim (n q); auto.
      split; auto.
      transitivity b; auto.
      intro.
      apply n0.
      transitivity q; auto.
      destruct (IHY2 (Y1++(a::nil))%list) with m.
      rewrite <- H6.
      rewrite List.app_ass; auto.
      intros.
      destruct H10 as [p [??]].
      apply List.in_app_or in H10.
      destruct H10.
      apply H7; auto.
      exists p; split; auto.
      simpl in H10; intuition subst.
      elim n. rewrite <- H12. auto.
      destruct H8 as [?[??]].
      destruct H8. subst a.
      elim n. destruct H10; auto.
      exists x; split; auto.
      auto.
      exists x; intuition.
      destruct H2 as [p [??]]. 
      exists p; split; simpl; auto.
    Qed.    
  End normal_mubs.

  (**  We can decide if a finite subset of a normal set is MUB closed.
    *)
  Lemma normal_sub_mub_closed_dec Q : normal_set Q ->
    forall (M:finset A), M ⊆ Q -> { mub_closed hf A M }+{ ~mub_closed hf A M }.
  Proof.
    intros HQ M HM. 
    unfold mub_closed.
    set (P' (N:finset A) := inh hf N -> N ⊆ M -> forall x, minimal_upper_bound x N -> x ∈ M).
    assert (forall x y, x ≈ y -> P' x -> P' y).
    clear. unfold P'. intros.
    apply H0.
    apply inh_eq with y; auto.
    rewrite H. auto.
    destruct H3. split.
    red; intros. apply H3.
    rewrite <- H; auto.
    intros. apply H4.
    red; intros. apply H5.
    rewrite H; auto.
    auto.
    destruct (finsubset_dec' A (OrdDec A (eff_ord_dec A Heff)) P') with M; auto.
    intro x.
    unfold P'.
    destruct (inh_dec A hf x).
    destruct (finset_find_dec' A
      (fun p:A => p ∈ M)) with x.
    intros. rewrite <- H0; auto.
    intros. apply finset_in_dec. 
    constructor. apply eff_ord_dec. auto.
    left. intros Hx ?.
    destruct s.
    destruct a.
    apply H0 in H1. elim H2; auto.
    destruct (normal_has_mubs Q HQ x) as [MUBS [?[??]]]; auto.
    red; intros. apply HM. apply m. auto.
    destruct (finset_find_dec' A (fun p => p ∈ M)) with MUBS.
    intros. rewrite <- H3; auto.
    intros. apply finset_in_dec. 
    constructor. apply eff_ord_dec. auto.
    right. intro.
    destruct s. destruct a. apply H5.
    apply H3; auto.
    left. intros _. intros.
    apply m0.
    destruct (H2 x0) as [x0' [?[??]]].
    destruct H4; auto.
    apply member_eq with x0'; auto.
    split; auto.
    destruct H4.
    apply H8; auto.
    destruct H7; auto.
    left; intro. contradiction.
    left.
    intros. 
    unfold P' in p.
    apply p with M0; auto.
    right. intro.
    destruct e as [X [??]].
    apply H2.
    red. intros.
    apply H0 with X; auto.
  Qed.    
 
  (** We can caluclate the (finite) set of all MUB closed finite subsets
      of a normal set.
    *)
  Lemma normal_set_mub_closed_sets Q : normal_set Q ->
    { CLS : finset (finset A) | 
      forall X, X ∈ CLS <-> (inh hf X /\ X ⊆ Q /\ mub_closed hf A X) }.
  Proof.        
    intros.
    set (SUBS := list_finsubsets Q).    
    assert (forall X, X ∈ SUBS -> X ⊆ Q).
    intros.
    unfold SUBS in H0.
    apply list_finsubsets_correct; auto.
    assert { XS:finset (finset A) | XS ⊆ SUBS /\ 
      forall X, X ∈ XS <-> (inh hf X /\ X ∈ SUBS /\ mub_closed hf A X) }.
    revert H0.
    generalize SUBS.
    clear SUBS.
    induction SUBS; intros.
    exists nil. split.
    red; auto.
    intuition.
    destruct H1 as [?[??]]. elim H1.
    destruct H1 as [?[??]]. elim H1.
    destruct IHSUBS as [XS [??]].
    intros. apply H0.
    destruct H1 as [q [??]]. exists q; split; simpl; auto.
    destruct (inh_dec A hf a).
    destruct (normal_sub_mub_closed_dec Q H a); auto.
    apply H0. exists a; split; simpl; auto.
    exists (a::XS)%list.
    split.
    red; intros.
    destruct H3 as [q [??]].
    destruct H3. subst q.
    exists a; split; simpl; auto.
    destruct (H1 a0).
    exists q; split; simpl; auto.
    destruct H5. exists x; split; simpl; auto.
    split; intros.
    destruct H3 as [q [??]].
    destruct H3. subst q.
    split. apply inh_eq with a; auto.
    split.
    exists a; split; simpl; auto.
    red. intros.
    rewrite H4. 
    apply (m M); auto.
    rewrite <- H4; auto.
    assert (X ∈ XS).
    exists q; split; simpl; auto.
    apply H2 in H5.
    destruct H5; split; auto.
    destruct H6; split; auto.
    destruct H6 as [q' [??]].
    exists q'; split; simpl; auto.
    destruct H3 as [HQ [??]].
    destruct H3 as [q [??]].
    destruct H3. subst q.
    exists a; split; simpl; auto.
    assert (X ∈ XS).
    apply H2.
    split; auto.
    split; auto.
    exists q; split; simpl; auto.
    destruct H6 as [q' [??]].
    exists q'; split; simpl; auto.
    exists XS.
    split.
    red; intros.
    apply H1 in H3.
    destruct H3 as [q [??]]. exists q; split; simpl; auto.
    split; intros.
    rewrite H2 in H3.
    destruct H3 as [HQ [??]]; split; auto. split; auto.
    destruct H3 as [q [??]]. exists q; split; simpl; auto.
    destruct H3 as [HQ [??]].
    destruct H3 as [q [??]].
    destruct H3. subst q.
    elim n; auto.
    red; intros.
    red in H4.
    rewrite <- H5.
    apply (H4 M); auto.
    rewrite H5; auto.
    rewrite H2. split; auto.
    split; auto.
    exists q; split; simpl; auto.
    exists XS.
    split.
    red; intros.
    apply H1 in H3.
    destruct H3 as [q [??]]. exists q; split; simpl; auto.
    split; intros.
    rewrite H2 in H3.
    destruct H3 as [HQ [??]]; split; auto. split; auto.
    destruct H3 as [q [??]]. exists q; split; simpl; auto.
    destruct H3 as [HQ [??]].
    destruct H3 as [q [??]].
    destruct H3. subst q.
    elim n; auto.
    apply inh_eq with X; auto.
    rewrite H2. split; auto.
    split; auto.
    exists q; split; simpl; auto.

    destruct X as [XS [??]].
    exists XS.
    intro X; split; intros.
    apply H2 in H3.
    destruct H3. split; auto.
    destruct H4; split; auto.
    destruct H3 as [?[??]].
    apply H2; split; auto. split; auto.
    apply list_finsubsets_complete; auto.
    constructor. apply (eff_ord_dec A Heff).
  Qed.

  Let OD := (OrdDec A (eff_ord_dec A Heff)).

  (**  The intersection of any two MUB closed sets is itself MUB closed.
    *)
  Lemma mub_closed_intersect : forall (X Y:finset A),
    mub_closed hf A X -> mub_closed hf A Y ->
    mub_closed hf A (fin_intersect A OD X Y).
  Proof.
    repeat intro.
    apply fin_intersect_elem.
    split.
    apply (H M); auto.
    red; intros.
    apply H2 in H4.
    apply fin_intersect_elem in H4.
    destruct H4; auto.
    apply (H0 M); auto.
    red; intros.
    apply H2 in H4.
    apply fin_intersect_elem in H4.
    destruct H4; auto.
  Qed.

  (**  Any normal set is mub closed.
    *)
  Lemma normal_set_mub_closed Q : normal_set Q -> mub_closed hf A Q.
  Proof.
    repeat intro.
    destruct (normal_has_mubs Q H M H0) as [MUBS [?[??]]]; auto.
    destruct (H5 x) as [m [?[??]]].
    destruct H2; auto.
    apply H3.
    apply member_eq with m; auto.
    split; auto.
    destruct H2. apply H9; auto.
    destruct H8; auto.
  Qed.

  (**  Given an h-inhabited finite subset of a normal set, we can compute
       the smallest MUB-closed superset.  This is done by taking the
       intersection of all the MUB-closed subsetsets of X that lie in Q.
    *)
  Lemma normal_set_mub_closure Q : normal_set Q ->
    forall (M:finset A) (Minh : inh hf M), M ⊆ Q ->
      { CL:finset A | M ⊆ CL /\ mub_closed hf A CL /\
          forall CL':finset A, M ⊆ CL' -> mub_closed hf A CL' -> CL ⊆ CL' }.
  Proof.
    intros.
    destruct (normal_set_mub_closed_sets Q H) as [CLS ?]; auto.
    assert (Hsubdec : forall X:finset A, {M⊆X}+{~(M ⊆ X)}).
    intros.
    destruct (finset_find_dec' A (fun z => z ∈ X)) with M.
    intros. rewrite <- H1; auto.
    apply finset_in_dec.
    constructor. apply eff_ord_dec; auto.
    destruct s as [z [??]].
    right. intro. apply H3 in H1.
    contradiction.
    left. red; auto.
    set (CLS' := finsubset (finset A) (fun X => M ⊆ X) Hsubdec CLS).
    exists (fin_list_intersect A OD CLS' Q).
    split.
    red; intros.
    apply fin_list_intersect_elem.
    split. apply H0; auto.
    intros.
    unfold CLS' in H2.
    apply finsubset_elem in H2.
    destruct H2.
    apply H3; auto.
    intros. rewrite <- H3; auto.
    split.
    cut (forall x, x ∈ CLS' -> mub_closed hf A x).
    generalize CLS'. clear -H.
    induction CLS'; intros.
    simpl.
    apply normal_set_mub_closed; auto.
    simpl.
    apply mub_closed_intersect.
    apply H0.
    exists a; split; simpl; auto.
    apply IHCLS'.
    intros. apply H0.
    destruct H1 as [q [??]]. exists q; split; simpl; auto.
    intros.
    unfold CLS' in H1.
    apply finsubset_elem in H1.
    destruct H1.
    apply i in H1.
    destruct H1 as [Hx [??]]; auto.
    intros. rewrite <- H2; auto.
    intros.
    red; intros.
    apply fin_list_intersect_elem in H3.
    destruct H3.
    assert (fin_intersect A OD CL' Q ∈ CLS').
    unfold CLS'.
    apply finsubset_elem.
    intros. rewrite <- H5; auto.
    split; auto.
    apply i.
    split.

    destruct hf; auto.
    red in Minh. simpl.
    destruct Minh as [x ?].
    exists x.
    apply fin_intersect_elem. split; auto.

    split; auto.
    red; intros.
    apply fin_intersect_elem in H5.
    destruct H5; auto.
    apply mub_closed_intersect; auto.
    apply normal_set_mub_closed; auto.
    red; intros.
    apply fin_intersect_elem.
    split; auto.
    apply H4 in H5.
    apply fin_intersect_elem in H5.
    destruct H5; auto.
  Qed.

  (**  In a MUB complete preorder, every MUB closed set is normal.
    *)
  Lemma mub_closed_normal_set : forall Q (HQ:inh hf Q),
    is_mub_complete hf A ->
    mub_closed hf A Q ->
    normal_set Q.
  Proof.
    intros. split; auto. repeat intro.
    set (Q' := finsubset A (fun x => x ≤ z) (fun x => eff_ord_dec A Heff x z) Q).
    destruct (H Q' z).
    apply inh_sub with M; auto.
    red; intros.
    unfold Q' in H2.
    apply finsubset_elem in H2. destruct H2; auto.
    intros. rewrite <- H3; auto.
    destruct H2.
    assert (x ∈ Q).
    apply (H0 Q'); auto.
    apply inh_sub with M; auto.
    unfold Q'; red; intros.
    apply finsubset_elem in H4. destruct H4; auto.
    intros. rewrite <- H5; auto.
    exists x. split; auto.
    red; intros.
    destruct H2.
    apply H2.
    unfold Q'.
    apply finsubset_elem.
    intros. rewrite <- H7; auto.
    split; auto.
    apply H1 in H5.
    apply finsubset_elem in H5. destruct H5; auto.
    intros. rewrite <- H7; auto.
    apply H1 in H5.
    apply finsubset_elem in H5. destruct H5; auto.
    intros. rewrite <- H7; auto.
    unfold Q'.
    apply finsubset_elem.
    intros. rewrite <- H5; auto.
    split; auto.
  Qed.

  Hypothesis Hnorm : has_normals.

  Lemma check_inh (X:finset A) : { X = nil /\ hf = true }+{ inh hf X }.
  Proof.
    destruct hf. simpl.
    destruct X. left; auto.
    right. exists c. apply cons_elem; auto.
    right. red. auto.
  Qed.

  (**  Define the MUB closure operation in a preorder with normal sets.
       Some slightly funny games are played here to ensure that the MUB
       closure operation is a total function even when hf = true.  In this
       case, the MUB closure of nil is nil; this works because nil is
       (vacuously) MUB closed when h = true.
    *)
  Definition norm_closure X :=
    match check_inh X with
    | left _ => nil
    | right Xinh =>
      match Hnorm X Xinh with
      | exist Q (conj HQ1 HQ2) => proj1_sig (normal_set_mub_closure Q HQ2 X Xinh HQ1)
      end
    end.

  (**  A preorder is Plotkin whenever it has normal sets.
    *)
  Program Definition norm_plt : plotkin_order hf A :=
    PlotkinOrder hf A _ norm_closure _ _ _.
  Next Obligation.
    red; intros.
    destruct (Hnorm M) as [Q [??]]; auto.
    destruct (normal_has_mubs Q H2 M H H1) as [MUBS [?[??]]].
    destruct (H5 x) as [m [?[??]]]; auto.
    exists m; split; auto.
  Qed.
  Next Obligation.
    repeat intro.
    unfold norm_closure.
    destruct (check_inh M).
    destruct a0. subst M; auto.
    destruct (Hnorm M i) as [Q [??]].
    destruct (normal_set_mub_closure Q n M i i0).
    simpl.
    destruct a0.
    apply H0. auto.
  Qed.    
  Next Obligation.
    repeat intro.
    unfold norm_closure in *.
    destruct (check_inh M).
    destruct a. subst.
    destruct H. apply H0 in H.
    apply nil_elem in H. elim H.
    destruct (Hnorm M i) as [Q [??]].
    destruct (normal_set_mub_closure Q n M i i0).
    simpl in *.
    destruct a. 
    destruct H3.
    apply H3 with M0; auto.
  Qed.    
  Next Obligation.
    repeat intro.
    unfold norm_closure in *.
    destruct (check_inh M).
    apply nil_elem in H1. elim H1.
    destruct (Hnorm M i) as [Q [??]].
    destruct (normal_set_mub_closure Q n M i i0).
    simpl in *.
    destruct a0 as [?[??]].
    apply H4; auto.
  Qed.    
End normal_sets.

(**  The product of two effective Plotkin orders has normal sets. *)
Lemma prod_has_normals hf (A B:preord)
  (HAeff:effective_order A)
  (HBeff:effective_order B)
  (HA:plotkin_order hf A)
  (HB:plotkin_order hf B) :
  has_normals (A×B) (effective_prod HAeff HBeff) hf.
Proof.
  red; intros.
  exists (finprod (mub_closure HA (image π₁ X))
                  (mub_closure HB (image π₂ X))).
  split.
  red; intros.
  destruct a.
  apply finprod_elem.
  split.
  apply mub_clos_incl.
  change c with (π₁#((c,c0):(A×B))).
  apply image_axiom1. auto.
  apply mub_clos_incl.
  change c0 with (π₂#((c,c0):(A×B))).
  apply image_axiom1. auto.
  apply mub_closed_normal_set.

  destruct hf; auto.
  destruct Hinh as [x ?].
  exists x.
  destruct x as [a b].
  apply finprod_elem.
  split; apply mub_clos_incl; auto.
  change a with (π₁#((a,b):A×B)).
  apply image_axiom1. auto.
  change b with (π₂#((a,b):A×B)).
  apply image_axiom1. auto.

  red. intros M x HMinh. intro.
  destruct x as [a b].
  destruct (mub_complete HA (image π₁ M) a).
  apply inh_image; auto.
  red; intros.
  apply image_axiom2 in H0.
  destruct H0 as [y [??]].
  apply H in H0.
  rewrite H1.
  destruct H0; auto.
  destruct (mub_complete HB (image π₂ M) b).
  apply inh_image; auto.
  red; intros.
  apply image_axiom2 in H1.
  destruct H1 as [y [??]].
  apply H in H1.
  rewrite H2.
  destruct H1; auto.
  exists (x,x0).
  destruct H0. destruct H1.
  split; [ | split; auto ].
  split.
  red; intros.
  split.
  apply H0.
  change (fst x1) with (π₁#x1). apply image_axiom1. auto.
  apply H1.
  change (snd x1) with (π₂#x1). apply image_axiom1. auto.
  intros.
  split.
  destruct H0. apply H6; auto.
  red; intros.
  apply image_axiom2 in H7.
  destruct H7 as [y [??]].
  apply H4 in H7.
  rewrite H8. destruct H7; auto.
  destruct H5; auto.
  destruct H1. apply H6.
  red; intros.
  apply image_axiom2 in H7.
  destruct H7 as [y [??]].
  apply H4 in H7.
  rewrite H8. destruct H7; auto.
  destruct H5; auto.

  red. intros M Minh. intros.
  destruct x.
  apply finprod_elem. split.
  apply (mub_clos_mub HA (image π₁ X) ) with (image π₁ M).
  apply inh_image; auto.

  red; intros.
  apply image_axiom2 in H1. destruct H1 as [y [??]].
  apply H in H1.
  destruct y.
  apply finprod_elem in H1.
  destruct H1.
  rewrite H2; auto.
  destruct H0; split.
  red; intros.
  apply image_axiom2 in H2. destruct H2 as [y [??]].
  apply H0 in H2.
  rewrite H3. destruct H2; auto.
  intros.
  destruct (H1 (b,c0)).
  red; intros.
  split.
  simpl.
  apply H2.
  change (fst x) with (π₁#x).
  apply image_axiom1. auto.
  simpl.
  apply H0 in H4.
  destruct H4; auto.
  split; auto.
  simpl in *. auto.

  apply (mub_clos_mub HB (image π₂ X)) with  (image π₂ M); auto.
  apply inh_image; auto.
  red; intros.
  apply image_axiom2 in H1. destruct H1 as [y [??]].
  apply H in H1.
  destruct y.
  apply finprod_elem in H1.
  destruct H1.
  rewrite H2; auto.
  destruct H0; split.
  red; intros.
  apply image_axiom2 in H2. destruct H2 as [y [??]].
  apply H0 in H2.
  rewrite H3. destruct H2; auto.
  intros.
  destruct (H1 (c,b)).
  red; intros.
  split.
  simpl.
  apply H0 in H4. destruct H4; auto.
  apply H2.
  change (snd x) with (π₂#x).
  apply image_axiom1. auto.
  split; auto.
  simpl in *. auto.
Qed.

(**  The product of two effective Plotkin orders is Plotkin. *)
Definition plotkin_prod hf (A B:preord)
  (HAeff:effective_order A) (HBeff:effective_order B)
  (HA:plotkin_order hf A) (HB:plotkin_order hf B)
  : plotkin_order hf (A×B)
  := norm_plt (A×B) (effective_prod HAeff HBeff) hf
         (prod_has_normals hf A B HAeff HBeff HA HB).


(** The disjoint union of two effective Plotkin orders has normal sets. *)
Lemma sum_has_normals hf (A B:preord)
  (HAeff:effective_order A)
  (HBeff:effective_order B)
  (HA:plotkin_order hf A)
  (HB:plotkin_order hf B) :
  has_normals (sum_preord A B) (effective_sum HAeff HBeff) hf.
Proof.
  hnf; intros.  
  set (L := left_finset A B X).
  set (R := right_finset A B X).
  destruct hf.
  
  case_eq L. intro.
  case_eq R. intro.
  elimtype False.
  destruct Hinh.
  destruct x.
  apply left_finset_elem in H1.
  unfold L in *.
  rewrite H in H1. apply nil_elem in H1. elim H1.
  apply right_finset_elem in H1.
  unfold R in *.
  rewrite H0 in H1. apply nil_elem in H1. elim H1.

  intros c l HR.
  destruct (plt_has_normals B HBeff true HB R) as [Z' [??]].
  hnf. exists c. rewrite HR. apply cons_elem; auto.
  exists (finsum nil Z').
  split.
  hnf. intros.
  rewrite (left_right_finset_finsum A B) in H2.
  destruct a.
  apply finsum_left_elem in H2.
  unfold L in H. rewrite H in H2.
  apply nil_elem in H2. elim H2.
  apply finsum_right_elem in H2.
  apply H0 in H2. 
  apply finsum_right_elem. auto.
  split.
  exists (inr c).
  apply finsum_right_elem. apply H0.
  rewrite HR. apply cons_elem; auto.
  repeat intro.
  destruct Hinh0 as [m Hm].
  destruct m as [m|m].
  apply H2 in Hm.
  apply finsubset_elem in Hm.
  destruct Hm.
  apply finsum_left_elem in H3.
  apply nil_elem in H3. elim H3.
  intros. rewrite <- H3; auto.
  generalize (H2 (inr m) Hm).  
  intros.
  apply finsubset_elem in H3.
  destruct H3.
  apply finsum_right_elem in H3.
  destruct z as [z|z]. elim H4.
  destruct H1.
  destruct (H5 z (right_finset A B M)) as [q [??]].
  exists m. apply right_finset_elem. auto.
  hnf; intros.
  apply finsubset_elem.
  intros. rewrite <- H7; auto.
  apply right_finset_elem in H6.
  apply H2 in H6.
  apply finsubset_elem in H6.
  destruct H6. split; auto.
  apply finsum_right_elem in H6.
  auto.
  intros. rewrite <- H7. auto.
  apply finsubset_elem in H7. destruct H7.
  exists (inr q). split.
  hnf; intros.
  destruct x.
  apply H2 in H9.
  apply finsubset_elem in H9.
  destruct H9. elim H10.
  intros. rewrite <- H10; auto.
  apply H6.
  apply right_finset_elem. auto.
  apply finsubset_elem.
  intros. rewrite <- H9; auto.
  split; auto.
  apply finsum_right_elem. auto.
  intros. rewrite <- H8. auto.
  intros. rewrite <- H4. auto.

  intros c l HL.
  case_eq R; intro.
  destruct (plt_has_normals A HAeff true HA L) as [Z' [??]].
  hnf. exists c. rewrite HL. apply cons_elem; auto.
  exists (finsum Z' nil).
  split.
  hnf. intros.
  rewrite (left_right_finset_finsum A B) in H2.
  destruct a.
  apply finsum_left_elem in H2.
  apply finsum_left_elem. auto.
  apply finsum_right_elem in H2.
  unfold R in H. rewrite H in H2.
  apply nil_elem in H2. elim H2.
  split.
  exists (inl c).
  apply finsum_left_elem. apply H0.
  rewrite HL. apply cons_elem; auto.
  repeat intro.
  destruct Hinh0 as [m Hm].
  destruct m as [m|m].
  generalize (H2 (inl m) Hm).  
  intros.
  apply finsubset_elem in H3.
  destruct H3.
  apply finsum_left_elem in H3.
  destruct z as [z|z]. 2: elim H4.
  destruct H1.
  destruct (H5 z (left_finset A B M)) as [q [??]].
  exists m. apply left_finset_elem. auto.
  hnf; intros.
  apply finsubset_elem.
  intros. rewrite <- H7; auto.
  apply left_finset_elem in H6.
  apply H2 in H6.
  apply finsubset_elem in H6.
  destruct H6. split; auto.
  apply finsum_left_elem in H6.
  auto.
  intros. rewrite <- H7. auto.
  apply finsubset_elem in H7. destruct H7.
  exists (inl q). split.
  hnf; intros.
  destruct x.
  apply H6.
  apply left_finset_elem. auto.
  apply H2 in H9.
  apply finsubset_elem in H9.
  destruct H9. elim H10.
  intros. rewrite <- H10; auto.
  apply finsubset_elem.
  intros. rewrite <- H9; auto.
  split; auto.
  apply finsum_left_elem. auto.
  intros. rewrite <- H8. auto.
  intros. rewrite <- H4. auto.
  apply H2 in Hm.
  apply finsubset_elem in Hm.
  destruct Hm.
  apply finsum_right_elem in H3.
  apply nil_elem in H3. elim H3.
  intros. rewrite <- H3; auto.

  intros l' HR.
  destruct (plt_has_normals A HAeff true HA L) as [ZL [??]].
  exists c. rewrite HL. apply cons_elem; auto.
  destruct (plt_has_normals B HBeff true HB R) as [ZR [??]].
  exists c0. rewrite HR. apply cons_elem; auto.
  exists (finsum ZL ZR).  
  split.
  hnf; intros.
  rewrite (left_right_finset_finsum A B) in H3.
  destruct a.
  apply finsum_left_elem in H3.
  apply H in H3.
  apply finsum_left_elem. auto.
  apply finsum_right_elem in H3.
  apply H1 in H3.
  apply finsum_right_elem. auto.
  hnf; intros.
  split.  
  exists (inl c).
  apply finsum_left_elem.
  apply H. rewrite HL. apply cons_elem; auto.
  repeat intro.
  destruct z as [z|z].
  destruct H0.
  destruct (H4 z (left_finset A B M)).
  destruct Hinh0.
  destruct x.
  exists c1.
  apply left_finset_elem. auto.
  apply H3 in H5.
  apply finsubset_elem in H5.
  destruct H5. elim H6.
  intros. rewrite <- H6; auto.
  hnf; simpl; intros.
  apply left_finset_elem in H5.
  apply H3 in H5.
  apply finsubset_elem in H5.
  destruct H5.
  apply finsubset_elem.
  intros. rewrite <- H7; auto.
  split; auto.
  apply finsum_left_elem in H5; auto.
  intros. rewrite <- H6; auto.
  destruct H5. exists (inl x).
  split.
  hnf; intros.
  destruct x0.
  apply H5.
  apply left_finset_elem. auto.
  apply H3 in H7.
  apply finsubset_elem in H7.
  destruct H7. elim H8.
  intros. rewrite <- H8; auto.
  apply finsubset_elem.
  intros. rewrite <- H7; auto.
  apply finsubset_elem in H6.
  destruct H6. split; auto.
  apply finsum_left_elem; auto.
  intros. rewrite <- H7; auto.
  destruct H2.
  destruct (H4 z (right_finset A B M)).
  destruct Hinh0 as [m Hm].
  destruct m.
  apply H3 in Hm.
  apply finsubset_elem in Hm.
  destruct Hm. elim H6.
  intros. rewrite <- H5; auto.
  exists c1. apply right_finset_elem; auto.
  hnf; intros.
  apply right_finset_elem in H5.
  apply finsubset_elem.
  intros. rewrite <- H6; auto.
  apply H3 in H5.
  apply finsubset_elem in H5.
  destruct H5. split; auto.
  apply finsum_right_elem in H5. auto.
  intros. rewrite <- H6; auto.
  exists (inr x).
  destruct H5. split.
  hnf; intros.
  destruct x0.
  apply H3 in H7.
  apply finsubset_elem in H7. destruct H7. elim H8.
  intros. rewrite <- H8; auto.
  apply H5. apply right_finset_elem; auto.
  apply finsubset_elem.
  intros. rewrite <- H7; auto.
  apply finsubset_elem in H6.
  destruct H6. split; auto.
  apply finsum_right_elem; auto.
  intros. rewrite <- H7; auto.
  
  destruct (plt_has_normals A HAeff false HA L) as [ZL [??]].
  hnf; auto.
  destruct (plt_has_normals B HBeff false HB R) as [ZR [??]].
  hnf; auto.
  exists (finsum ZL ZR).  
  split.
  hnf; intros.
  rewrite (left_right_finset_finsum A B) in H3.
  destruct a.
  apply finsum_left_elem in H3.
  apply H in H3.
  apply finsum_left_elem. auto.
  apply finsum_right_elem in H3.
  apply H1 in H3.
  apply finsum_right_elem. auto.
  hnf; intros.
  split.   hnf; auto.
  repeat intro.
  destruct z as [z|z].
  destruct H0.
  destruct (H4 z (left_finset A B M)).
  hnf; auto.
  hnf; simpl; intros.
  apply left_finset_elem in H5.
  apply H3 in H5.
  apply finsubset_elem in H5.
  destruct H5.
  apply finsubset_elem.
  intros. rewrite <- H7; auto.
  split; auto.
  apply finsum_left_elem in H5; auto.
  intros. rewrite <- H6; auto.
  destruct H5. exists (inl x).
  split.
  hnf; intros.
  destruct x0.
  apply H5.
  apply left_finset_elem. auto.
  apply H3 in H7.
  apply finsubset_elem in H7.
  destruct H7. elim H8.
  intros. rewrite <- H8; auto.
  apply finsubset_elem.
  intros. rewrite <- H7; auto.
  apply finsubset_elem in H6.
  destruct H6. split; auto.
  apply finsum_left_elem; auto.
  intros. rewrite <- H7; auto.
  destruct H2.
  destruct (H4 z (right_finset A B M)).
  hnf; auto.
  hnf; intros.
  apply right_finset_elem in H5.
  apply finsubset_elem.
  intros. rewrite <- H6; auto.
  apply H3 in H5.
  apply finsubset_elem in H5.
  destruct H5. split; auto.
  apply finsum_right_elem in H5. auto.
  intros. rewrite <- H6; auto.
  exists (inr x).
  destruct H5. split.
  hnf; intros.
  destruct x0.
  apply H3 in H7.
  apply finsubset_elem in H7. destruct H7. elim H8.
  intros. rewrite <- H8; auto.
  apply H5. apply right_finset_elem; auto.
  apply finsubset_elem.
  intros. rewrite <- H7; auto.
  apply finsubset_elem in H6.
  destruct H6. split; auto.
  apply finsum_right_elem; auto.
  intros. rewrite <- H7; auto.
Qed.

(** The disjoint union of two effective Plotkin orders is Plotkin. *)
Definition plotkin_sum hf (A B:preord)
  (HAeff:effective_order A) (HBeff:effective_order B)
  (HA:plotkin_order hf A) (HB:plotkin_order hf B)
  : plotkin_order hf (sum_preord A B)
  := norm_plt (sum_preord A B) 
         (effective_sum HAeff HBeff) hf
         (sum_has_normals hf A B HAeff HBeff HA HB).


(**  Next we show that adding a new bottom element to an effective
     Plotkin order yields another Plotkin order.
  *)
Fixpoint unlift_list {A} (x:list (option A)) :=
  match x with
  | nil => nil
  | None :: x' => unlift_list x'
  | Some a :: x' => a :: unlift_list x'
  end.

Lemma unlift_app A (l l':list (option A)) :
  unlift_list (l++l') = unlift_list l ++ unlift_list l'.
Proof.
  induction l; simpl; intuition.
  destruct a; simpl; auto.
  f_equal; auto.
Qed.

Lemma in_unlift A (l:list (option A)) x :
  In x (unlift_list l) <-> In (Some x) l.
Proof.
  induction l; simpl; intuition.
  destruct a; simpl in *.
  intuition subst; auto.
  right; auto.
  subst. simpl; auto.
  destruct a; simpl; auto.
Qed.

Lemma incl_unlift A (l l':list (option A)) :
  List.incl l l' -> List.incl (unlift_list l) (unlift_list l').
Proof.
  induction l; repeat intro; simpl in *; intuition.
  destruct a; simpl in *; intuition subst; auto.
  rewrite in_unlift.
  apply H; simpl; auto.
  rewrite in_unlift.
  rewrite in_unlift in H1.
  apply H; simpl; auto.
  apply IHl; auto.
  hnf; intros. apply H; simpl; auto.
Qed.

Definition lift_mub_closure hf (A:preord) (HA:plotkin_order hf A) (M:finset (lift A)) 
  : finset (lift A):=
  match unlift_list M with
  | nil => single None
  | X => None :: image (liftup A) (mub_closure HA X)
  end.

(** The lift preorder of had normal sets. *)
Lemma lift_has_normals hf1 hf2 (A:preord)
  (Heff:effective_order A)
  (Hplt:plotkin_order hf1 A) :
  has_normals (lift A) (effective_lift Heff) hf2.
Proof.
  red; intros.
  set (X' := unlift_list X : finset A).
  assert (forall a, a ∈ X' <-> Some a ∈ X).
  intro; split; intros.
  destruct H as [q [??]].
  apply in_unlift in H.
  exists (Some q). split; auto.
  destruct H as [q [??]].
  destruct q.
  exists c. split; auto.
  apply in_unlift. auto.
  destruct H0. elim H0.
  exists (lift_mub_closure hf1 A Hplt X).
  split.
  red; intros.
  unfold lift_mub_closure.
  case_eq (unlift_list X).
  intros.
  destruct H0 as [q [??]].
  destruct a.
  destruct q.
  assert (In c0 (unlift_list X)).
  apply in_unlift. auto.
  rewrite H1 in H3. elim H3.
  destruct H2. elim H2.
  apply single_axiom; auto.
  intros.
  destruct a.
  apply cons_elem. right.
  apply image_axiom1'.
  exists c0. split; auto.
  apply mub_clos_incl.
  destruct H0 as [q [??]].
  destruct q.
  exists c1. split; auto.
  rewrite <- H1.
  apply in_unlift. auto.
  destruct H2. elim H2.
  apply cons_elem; auto.

  hnf; intros.
  split.
  unfold lift_mub_closure.
  destruct hf2; simpl; auto.
  exists None.
  destruct (unlift_list X); simpl.
  apply single_axiom. auto.
  apply cons_elem; auto.
  
  repeat intro.
  case_eq (unlift_list M); intros.
  exists (None : lift A).
  split.
  hnf; intros.
  destruct x; auto.
  destruct H2 as [q [??]].
  destruct q.
  assert (In c0 (unlift_list M)).
  apply in_unlift. auto.
  rewrite H1 in H4. elim H4.
  destruct H3. elim H3.
  apply finsubset_elem.
  intuition. rewrite <- H2; auto.
  split; auto.
  unfold lift_mub_closure.
  destruct (unlift_list X); auto.
  apply single_axiom; auto.
  apply cons_elem; auto.
  red. simpl; auto.

  destruct z.
  destruct (mub_complete Hplt (unlift_list M) c0) as [ub [??]].
  rewrite H1.
  destruct hf1; simpl; auto.
  exists c. apply cons_elem; auto.
  hnf; intros.
  assert (Some x ∈ M).
  destruct H2 as [q [??]].
  apply in_unlift in H2.
  exists (Some q). split; auto.
  generalize H3; intros.
  apply H0 in H3.
  apply finsubset_elem in H3.
  destruct H3. auto.
  intros. rewrite <- H5; auto.
  exists (Some ub).
  split.
  hnf; intros.
  destruct H2.
  destruct x; auto.
  assert (c1 ∈ (unlift_list M : finset A)).
  destruct H4 as [q [??]].
  destruct q.
  exists c2.
  split; auto.
  apply in_unlift. auto.
  destruct H6. elim H6.
  apply H2 in H6. auto.
  red; simpl; auto.
  apply finsubset_elem.
  intros. rewrite <- H4; auto.
  split; auto.
  unfold lift_mub_closure.
  case_eq (unlift_list X).
  intros.
  assert (Some c ∈ M).
  exists (Some c); split; auto.
  apply in_unlift. rewrite H1. simpl; auto.
  apply H0 in H5.
  apply finsubset_elem in H5.
  destruct H5.
  unfold lift_mub_closure in H5.
  rewrite H4 in H5.
  apply single_axiom in H5. destruct H5. elim H5.
  intros. rewrite <- H6; auto.
  intros.
  apply cons_elem. right.
  apply image_axiom1'.
  exists ub. split; auto.
  rewrite <- H4.
  apply mub_clos_mub with (unlift_list M); auto.
  rewrite H1.
  destruct hf1; simpl; auto.
  exists c. apply cons_elem; auto.

  hnf; intros.
  assert (Some a ∈ M).
  destruct H5 as [q [??]].
  exists (Some q). split; auto.
  apply in_unlift; auto.
  apply H0 in H6.
  apply finsubset_elem in H6.
  destruct H6.
  unfold lift_mub_closure in H6.
  rewrite H4 in H6.
  rewrite <- H4 in H6.
  apply cons_elem in H6.
  destruct H6.
  destruct H6. elim H6.
  apply image_axiom2 in H6.
  destruct H6 as [y [??]].
  simpl in H8.
  apply member_eq with y; auto.
  intros. rewrite <- H7; auto.
  
  assert (Some c ∈ M).
  exists (Some c); split; auto.
  apply in_unlift. rewrite H1. simpl; auto.
  apply H0 in H2.
  apply finsubset_elem in H2.
  destruct H2. elim H3.
  intros. rewrite <- H3. auto.
Qed.

(** The lift preorder of an effective Plotkin order is Plotkin. *)
Definition lift_plotkin hf1 hf2 (A:preord)
  (Hplt:plotkin_order hf1 A) 
  (Heff:effective_order A)
  : plotkin_order hf2 (lift A)
  := norm_plt (lift A) (effective_lift Heff) hf2
         (lift_has_normals hf1 hf2 A Heff Hplt).