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

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

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.

(**  * Joinable relations

     Joinable relations represent the compact elements of the
     continuous function space, and form the exponential object
     for profinite domains. 

     Here we carry out the techincal proofs required
     to show the joinable relations form an effective Plotkin preorder.
  *)


(**  This definition of joinable relations is a minor modification
     of a definition given by Samson Abramsky.  It is more obviously effective
     than the definition given by Carl Gunter in his dissertation.
  *)
Definition is_joinable_relation {A B:preord} hf (R:finset (A × B)) :=
  inh hf R /\
  forall (G:finset (A × B)) (HGinh:inh hf G), G ⊆ R ->
    forall x, minimal_upper_bound x (image π₁ G) ->
      exists y, (x,y) ∈ R /\ upper_bound y (image π₂ G).

Definition joinable_relation hf (A B:preord)
  := { R:finset (A × B) | is_joinable_relation hf R }.

Definition joinable_rel_ord hf (A B:preord) (R S:joinable_relation hf A B) :=
  forall a b, (a,b) ∈ proj1_sig R -> 
    exists a' b', (a',b') ∈ proj1_sig S /\ a' ≤ a /\ b ≤ b'.

Program Definition joinable_rel_ord_mixin hf (A B:preord) :
  Preord.mixin_of (joinable_relation hf A B) :=
  Preord.Mixin (joinable_relation hf A B) (joinable_rel_ord hf A B) _ _.
Next Obligation.
  repeat intro. exists a. exists b. split; auto.
Qed.
Next Obligation.
  repeat intro.
  destruct (H a b) as [a' [b' [?[??]]]]; auto.
  destruct (H0 a' b') as [a'' [b'' [?[??]]]]; auto.
  exists a''. exists b''. split; auto.
  split; etransitivity; eauto.
Qed.  

Canonical Structure joinable_rel_order hf (A B:preord) : preord :=
  Preord.Pack (joinable_relation hf A B) (joinable_rel_ord_mixin hf A B).

(** The ordering relation between joinable relations is decidable. *)
Lemma joinable_ord_dec
  hf
  (A B:preord)
  (HAeff : effective_order A)
  (HBeff : effective_order B)
  (R S:joinable_relation hf A B) :
  { R ≤ S }+{ R ≰ S }.
Proof.
  intros.
  destruct (finset_find_dec' (A×B)
    (fun z => exists w, w ∈ proj1_sig S /\ π₁#w ≤ π₁#z /\ π₂#z ≤ π₂#w))
    with (proj1_sig R).
  intros.
  destruct H0 as [w [??]]. exists w. intuition.
  rewrite <- H. auto.
  rewrite <- H. auto.
  intro.
  destruct (finset_find_dec (A×B)
    (fun w => π₁#w ≤ π₁#x /\ π₂#x ≤ π₂#w))
    with (proj1_sig S).
  intros.
  destruct H0 as [??].
  rewrite H in H0.
  rewrite H in H1.
  split; auto.
  intros.
  assert (Hdec : ord_dec (A × B)).
  constructor. intros.
  destruct x1 as [a b]. destruct y as [c d].
  destruct (eff_ord_dec A HAeff a c).
  destruct (eff_ord_dec B HBeff b d).
  left. split; auto.
  right. intros [??]. apply n; auto.
  right. intros [??]. apply n; auto.
  destruct (eff_ord_dec _ HAeff (π₁ # x0) (π₁ # x)).
  destruct (eff_ord_dec _ HBeff (π₂ # x) (π₂ # x0)).
  left. auto.
  right. intros [??]. apply n; auto.
  right. intros [??]. apply n; auto.
  destruct s as [z [?[??]]].
  left. exists z.  split; auto.
  right.
  intros [w [?[??]]].
  apply (n w); auto.
  destruct s as [z [??]].
  right.
  red. intros.
  destruct (H1 (fst z) (snd z)) as [p [q [?[??]]]].
  destruct z; auto.
  apply H0. exists (p,q). split; auto.
  left.
  repeat intro.
  destruct (e (a,b)) as [[p q] [?[??]]]; auto.
  exists p. exists q. split; auto.
Qed.

(** Given a finite relation, we can decide if it is joinable or not.
    If not, we can find evidence demonstrating where the relation
    is insufficently complete.
  *)
Lemma is_joinable_rel_dec0 hf
  (A B:preord)
  (HAeff : effective_order A)
  (HBeff : effective_order B)
  (HAplt : plotkin_order hf A)
  (R:finset (A × B)) (Rinh : inh hf R) :
  { is_joinable_relation hf R } +
  { exists (G:finset (A×B)), inh hf G /\ G ⊆ R /\ exists z : A,
     minimal_upper_bound z (image π₁ G) /\
     (forall q : B, (z, q) ∈ R -> ~ upper_bound q (image π₂ G)) }.
Proof.
  assert (Hdec : ord_dec (A × B)).
  constructor. intros.
  destruct x; destruct y.
  destruct (eff_ord_dec A HAeff c c1);
    destruct (eff_ord_dec B HBeff c0 c2).
  left. split; auto.
  right. intros [??]. apply n; auto.
  right. intros [??]. apply n; auto.
  right. intros [??]. apply n; auto.

  set (H:=I).

  set (P (G:finset (A×B)) := 
    inh hf G ->
    forall x:A, minimal_upper_bound x (image π₁ G) ->
      exists y, (x,y) ∈ R /\ upper_bound y (image π₂ G)).

  assert (Hrespects : forall x y:finset (A×B), x ≈ y -> P x -> P y).
  unfold P; intros x y ? ? Hy. intros.
  destruct H1 with x0; auto.
  apply inh_eq with y; auto.
  destruct H2.
  split.
  red; intros. apply H2.
  apply image_axiom2 in H4.
  destruct H4 as [q[??]].
  rewrite H5. apply image_axiom1.
  rewrite <- H0; auto.
  intros. apply H3.
  red; intros.
  apply image_axiom2 in H6.
  destruct H6 as [q [??]].
  apply H4.
  rewrite H7. apply image_axiom1. rewrite H0; auto.
  auto.
  destruct H3.
  exists x1. split; auto.
  red; intros.
  apply image_axiom2 in H5.
  destruct H5 as [a [??]].
  apply H4.
  rewrite H6.
  apply image_axiom1.
  rewrite H0. auto.

  destruct (finsubset_dec' (A × B) Hdec P) with R; auto.
  unfold P. intro G.
  destruct (inh_dec _ hf G).
  set (Q x0 := minimal_upper_bound x0 (image π₁ G) -> exists y, (x0,y) ∈ R /\ upper_bound y (image π₂ G)).
  destruct (finset_find_dec' A Q) with (mub_closure HAplt (image π₁ G)).
  unfold Q; intros.
  destruct H1 as [q [??]].
  revert H2. apply minimal_upper_bound_ok; auto.
  exists q; split; auto.
  apply member_eq with (x,q); auto.
  destruct H0; split; split; auto.

  intro x.
  unfold Q.
  destruct (mub_finset_dec hf A HAeff HAplt (image π₁ G) x).
  apply inh_image; auto.
  destruct (finset_find_dec (A×B) (fun q => fst q ≈ x /\ upper_bound (snd q) (image π₂ G))) with R.
  intros. destruct H1; split.
  rewrite <- H1.
  destruct H0 as [[??][??]]; auto.
  revert H2. apply upper_bound_ok.
  destruct H0 as [[??][??]]; split; auto.
  intros [a b]. simpl.
  destruct (eff_ord_dec A HAeff a x).
  destruct (eff_ord_dec A HAeff x a).
  destruct (upper_bound_dec B HBeff (image π₂ G) b).
  left. split; auto.
  right. intros [??]. apply n; auto.
  right. intros [[??]?]. apply n; auto.
  right. intros [[??]?]. apply n; auto.

  destruct s as [z [??]].
  destruct H1.
  destruct z as [a b]. simpl in H1, H2.
  left. intros.
  exists b. split.
  apply member_eq with (a,b); auto.
  destruct H1; split; split; auto.
  auto.
  right; intro.
  destruct H0. auto.
  destruct H0.
  apply n in H0.
  apply H0.
  split; auto.
  left. intro. elim n; auto.
  destruct s as [z [??]].
  right. intro.
  apply H1. intro.
  apply H2; auto.
  left. 
  intros HG. intros.
  apply q; auto.
  apply (mub_clos_mub HAplt (image π₁ G)) with (image π₁ G); auto.
  apply inh_image; auto.

  apply mub_clos_incl.
  left. intro. contradiction.
  left.
  split; auto.
  intros.
  apply p; auto.
  
  right.
  destruct e as [G [??]].
  destruct (inh_dec _ hf G).
  exists G. split; auto.
      
  set (Q x0 := minimal_upper_bound x0 (image π₁ G) -> exists y, (x0,y) ∈ R /\ upper_bound y (image π₂ G)).
  destruct (finset_find_dec' A Q) with (mub_closure HAplt (image π₁ G)).
  unfold Q; intros.
  destruct H3 as [q [??]].
  revert H4. apply minimal_upper_bound_ok; auto.
  exists q; split; auto.
  apply member_eq with (x,q); auto.
  destruct H2; split; split; auto.
  intro x.
  unfold Q.
  destruct (mub_finset_dec hf A HAeff HAplt (image π₁ G) x).
  apply inh_image; auto.
  destruct (finset_find_dec (A×B) (fun q => fst q ≈ x /\ upper_bound (snd q) (image π₂ G))) with R.
  intros. destruct H3; split.
  rewrite <- H3.
  destruct H2 as [[??][??]]; auto.
  revert H4. apply upper_bound_ok.
  destruct H2 as [[??][??]]; split; auto.
  intros [a b]. simpl.
  destruct (eff_ord_dec A HAeff a x).
  destruct (eff_ord_dec A HAeff x a).
  destruct (upper_bound_dec B HBeff (image π₂ G) b).
  left. split; auto.
  right. intros [??]. apply n; auto.
  right. intros [[??]?]. apply n; auto.
  right. intros [[??]?]. apply n; auto.
  
  destruct s as [z [??]].
  destruct H3.
  destruct z as [a b]. simpl in H3, H4.
  left. intros.
  exists b. split.
  apply member_eq with (a,b); auto.
  destruct H3; split; split; auto.
  auto.
  right; intro.
  destruct H2. auto.
  destruct H2.
  apply n in H2.
  apply H2.
  split; auto.
  left. intro. elim n; auto.
  destruct s as [z [??]].
  destruct (mub_finset_dec hf A HAeff HAplt (image π₁ G) z).
  apply inh_image; auto.
  split; auto.
  exists z. split; auto.
  repeat intro.
  apply H3. red. intros.
  exists q. split; auto.
  
  elim H3; red; intros. elim n; auto.
  elim H1. red; intros.
  apply q; auto.
  apply (mub_clos_mub HAplt (image π₁ G)) with (image π₁ G); auto.
  apply inh_image; auto.
  apply mub_clos_incl.
  elim H1.
  red; intro. contradiction.
Qed.

Lemma is_joinable_rel_dec hf
  (A B:preord)
  (HAeff : effective_order A)
  (HBeff : effective_order B)
  (HAplt : plotkin_order hf A)
  (R:finset (A × B)) (Rinh : inh hf R)  :
  { is_joinable_relation hf R } + { ~is_joinable_relation hf R }.
Proof.
  destruct (is_joinable_rel_dec0 hf A B HAeff HBeff HAplt R); auto.
  right; intro.
  destruct H.
  destruct e as [G [? [? [z [??]]]]].
  destruct (H0 G H1 H2 z H3) as [y [??]].
  apply (H4 y); auto.
Qed.

Lemma is_joinable_rel_dec' hf
  (A B:preord)
  (HAeff : effective_order A)
  (HBeff : effective_order B)
  (HAplt : plotkin_order hf A)
  (R:finset (A × B)) :
  { is_joinable_relation hf R } + { ~is_joinable_relation hf R }.
Proof.
  destruct (inh_dec _ hf R).
  destruct (is_joinable_rel_dec0 hf A B HAeff HBeff HAplt R); auto.
  right; intro.
  destruct H.
  destruct e as [G [? [? [z [??]]]]].
  destruct (H0 G H1 H2 z H3) as [y [??]].
  apply (H4 y); auto.
  right; intros [??]. contradiction.
Qed.

(** Joinable relations form an effective preorder.  We can enumerate
    the joinable relations by first enumerating all the finite
    relations between A and B and selecting those that are joinable.
  *)
Program Definition joinable_rel_effective hf
  (A B:preord)
  (HAeff : effective_order A)
  (HBeff : effective_order B)
  (HAplt : plotkin_order hf A) :
  effective_order (joinable_rel_order hf A B)
  := EffectiveOrder _ _ _ _.
Next Obligation.
  intros.
  apply joinable_ord_dec; auto.
Qed.
Next Obligation.
  intros.
  set (X := finsubsets (A×B) (eprod (eff_enum A HAeff) (eff_enum B HBeff))).
  exact (fun n => match X n with
           | None => None
           | Some a => 
               match is_joinable_rel_dec' hf A B HAeff HBeff HAplt a with
               | left H => Some (exist _ a H)
               | right _ => None
               end
           end).
Defined.
Next Obligation.
  intros.
  unfold joinable_rel_effective_obligation_2.
  red. simpl.
  destruct x as [x H].
  assert (x ∈ finsubsets (A×B) (eprod (eff_enum A HAeff) (eff_enum B HBeff))).
  apply finsubsets_complete.
  red; intros.
  destruct a as [a b].
  apply eprod_elem.
  split; apply eff_complete.
  destruct H0 as [n ?].
  exists n.
  destruct (finsubsets (A×B) (eprod (eff_enum A HAeff) (eff_enum B HBeff)) n).
  destruct (is_joinable_rel_dec' hf A B HAeff HBeff HAplt c).
  destruct H0.
  split; red; simpl; auto. red.
  simpl; intros.
  apply H0 in H2.
  exists a. exists b. split; auto.
  red; simpl; intros.
  apply H1 in H2.
  exists a. exists b. auto.
  apply n0; auto.
  split.
  destruct H.
  apply inh_eq with x; auto.
  intros.
  destruct H.
  destruct (H3 G) with x0 as [y [??]]; auto.
  rewrite H0; auto.
  exists y. split; auto.
  rewrite <- H0; auto.
  auto.
Qed.


(**  In this section, we show that, given an arbitrary finite relation M
     and an approximable relation R above M, we can "swell up" M into
     a joinable relation M' where M ⊆ M' ⊆ R.

     This is done by a recursive procedure that, at each step, asks
     if M is joinable.  If so, we are done.  If not, there is some
     subset of M lacking the necessary upper bound.  We add a new
     pair to M to fix the deficiency and recurse.  The termination
     of this procedure relies critically on the fact that the
     MUB closure of a finite set is finite in Plotkin orders.  Thus, at
     some point we will run out of elements that could possibly be added
     to M, and the procedure must terminate giving the desired joinable
     relation.

     Then we can then show that the set of joinable relations below some
     approximable relation is directed; this will be important later
     for the proof that currying is an approximable relation.
  *)
Section directed_joinables.
  Variable hf:bool.
  Variables A B:preord.
  Variable HAeff : effective_order A.
  Variable HAplt : plotkin_order hf A.
  Variable HBeff : effective_order B.
  Variable HBplt : plotkin_order hf B.

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

  Variable R:erel A B.
  Hypothesis HR : forall x x' y y', x ≤ x' -> y' ≤ y -> (x,y) ∈ R -> (x',y') ∈ R.
  Hypothesis HRdir : forall a, directed hf (erel_image A B OD R a).

  Section swell.

  Variable RS : finset (A×B).
  Hypothesis RSinh : inh hf RS.

  Let RS' := finprod
     (mub_closure HAplt (image π₁ RS))
     (mub_closure HBplt (image π₂ RS)).

  Section swell_relation.
    Variable G:finset (A×B).
    Hypothesis HG : G ⊆ R.
    Hypothesis HG' : G ⊆ RS'.

    Variable G0:finset (A×B).
    Hypothesis HG0 : G0 ⊆ G.
    Hypothesis G0inh : inh hf G0.

    Variable z:A.
    Hypothesis Hz : minimal_upper_bound z (image π₁ G0).

    Lemma swell_row :
      exists q,
        (z,q) ∈ RS' /\ (z,q) ∈ R /\
        upper_bound q (image π₂ G0).
    Proof.
      destruct (HRdir z (image π₂ G0)) as [q [??]].
      apply inh_image; auto.
      red; intros.
      apply image_axiom2 in H.
      destruct H as [q[??]].
      apply erel_image_elem.
      destruct q as [x y]. simpl in H0.
      apply HR with x y.      
      apply Hz. change x with (π₁#((x,y):A×B)).
      apply image_axiom1. auto.
      destruct H0; auto.
      apply HG; auto.
      apply erel_image_elem in H0.
      destruct (mub_complete HBplt (image π₂ G0) q) as [q0 [??]]; auto.
      apply inh_image; auto.
      exists q0. split.      
      unfold RS'. apply finprod_elem. split.
      apply (mub_clos_mub HAplt (image π₁ RS)) with (image π₁ G0); auto.
      apply inh_image. auto.
      red; intros.
      apply image_axiom2 in H3. destruct H3 as [y [??]].
      apply HG0 in H3.
      apply HG' in H3.
      unfold RS' in H3.
      destruct y as [x y].
      apply finprod_elem in H3.
      rewrite H4. simpl.
      destruct H3; auto.
      apply (mub_clos_mub HBplt (image π₂ RS)) with (image π₂ G0); auto.
      apply inh_image. auto.
      red; intros.
      apply image_axiom2 in H3. destruct H3 as [y [??]].
      apply HG0 in H3.
      apply HG' in H3.
      unfold RS' in H3.
      destruct y as [x y].
      apply finprod_elem in H3.
      rewrite H4. simpl.
      destruct H3; auto.
      split. apply HR with z q; auto.
      destruct H1; auto.
    Qed.

    Variable XS : finset (A×B).
    Hypothesis HXS : forall x, x ∈ XS <-> x ∈ RS' /\ x ∉ G.    

    Let ODAB := OrdDec (A×B) (eff_ord_dec _ (effective_prod HAeff HBeff)).

    Hypothesis noub : forall q, (z,q) ∈ G -> ~upper_bound q (image π₂ G0).

    Lemma swell_relation :
      exists G':finset (A×B), exists XS':finset (A×B),
        length XS' < length XS /\
        G ⊆ G' /\ G' ⊆ R /\ G' ⊆ RS' /\
        (forall x, x ∈ XS' <-> x ∈ RS' /\ x ∉ G').
    Proof.
      destruct swell_row as [r [?[??]]].
      exists ((z,r)::G)%list.
      exists (finset_remove ODAB XS (z,r)).
      split. apply finset_remove_length2.
      apply HXS. split; auto.
      red; intro. apply (noub r); auto.
      split. red; intros. apply cons_elem; auto.
      split. red; intros.
      apply cons_elem in H2. destruct H2.
      rewrite H2; auto. apply HG; auto.
      split. red; intros.
      apply cons_elem in H2. destruct H2.
      rewrite H2; auto. apply HG'; auto.
      intro. split; intros.
      apply finset_remove_elem in H2.
      destruct H2.
      apply HXS in H2. destruct H2.
      split. auto.
      intro.
      apply cons_elem in H5. destruct H5.
      apply H3; auto.
      apply H4; auto.
      apply finset_remove_elem.
      destruct H2.
      split.
      apply HXS.
      split; auto.
      intro. apply H3. apply cons_elem. auto.
      intro. apply H3. apply cons_elem. auto.
    Qed.
  End swell_relation.

  Lemma swell_inductive_step
    (G:finset (A×B))
    (Ginh : inh hf G)
    (HG : G ⊆ R) (HG': G ⊆ RS')
    (XS:finset (A×B))
    (HXS: forall x, x ∈ XS <-> x ∈ RS' /\ x ∉ G) :

    is_joinable_relation hf G \/
    exists G':finset (A×B), exists XS':finset (A×B),
        length XS' < length XS /\
        G ⊆ G' /\ G' ⊆ R /\ G' ⊆ RS' /\
        (forall x, x ∈ XS' <-> x ∈ RS' /\ x ∉ G').
  Proof.
    destruct (is_joinable_rel_dec0 hf A B HAeff HBeff HAplt G); auto.
    destruct e as [G0 [? [? [z [??]]]]].
    destruct swell_relation with G G0 z XS
      as [G' [XS' [?[?[?[??]]]]]]; auto.
    right.
    exists G'. exists XS'. intuition.
  Qed.

  Lemma swell_aux (n:nat) : forall
    (G:finset (A×B))
    (Ginh : inh hf G)
    (HG : G ⊆ R)
    (HG': G ⊆ RS')
    (XS : finset (A×B))
    (HXS: forall x, x ∈ XS <-> x ∈ RS' /\ x ∉ G)
    (Hn : n = length XS),
    exists G':finset (A×B),
      G ⊆ G' /\ G' ⊆ R /\ is_joinable_relation hf G'.
  Proof.      
    induction n using (well_founded_induction Wf_nat.lt_wf); intros.
    destruct (swell_inductive_step G Ginh HG HG' XS HXS).
    exists G. split; auto.
    red; auto.
    destruct H0 as [G' [XS' [?[?[?[??]]]]]].
    destruct (H (length XS')) with G' XS' as [G'' [?[??]]]; auto.
    rewrite Hn. auto.
    apply inh_sub with G; auto.
    exists G''. split; auto.
    red; intros. apply H5. apply H1. auto.
  Qed.

  Hypothesis HRS : RS ⊆ R.

  Lemma swell : exists G:finset (A×B),
    RS ⊆ G /\ G ⊆ R /\ is_joinable_relation hf G.
  Proof.
    assert (HRdec : forall x, { x ∉ RS }+{ ~x ∉ RS }).
    intro. destruct (eff_in_dec (effective_prod HAeff HBeff) RS x).
    right; auto. left; auto.
    set (XS := finsubset _ (fun x => x ∉ RS) HRdec RS').
    destruct (swell_aux (length XS)) with RS XS as [G [?[??]]]; auto.
    red; intros.
    unfold RS'.
    destruct a. apply finprod_elem.
    split.
    apply (mub_clos_incl HAplt (image π₁ RS)).
    change c with (π₁#((c,c0):A×B)).
    apply image_axiom1. auto.
    apply (mub_clos_incl HBplt (image π₂ RS)).
    change c0 with (π₂#((c,c0):A×B)).
    apply image_axiom1. auto.
    split; intros.    
    unfold XS in H.
    apply finsubset_elem in H; auto.
    intros. rewrite <- H0. auto.
    unfold XS.
    apply finsubset_elem; auto.
    intros. rewrite <- H0. auto.
    exists G. auto.
  Qed.

  End swell.

  Variable M:finset (joinable_rel_order hf A B).
  Hypothesis Minh : inh hf M.
  Hypothesis HM : forall S, S ∈ M -> proj1_sig S ⊆ R.
  Let RS := concat _ (List.map (fun R => proj1_sig R) M) : finset (A×B).

  Lemma RS_elem :
    forall a, a ∈ RS -> (exists S, S ∈ M /\ a ∈ proj1_sig S).
  Proof.
    intro a.
    unfold RS.
    clear -HM.
    induction M; simpl in *; intros.
    apply nil_elem in H. elim H.
    apply app_elem in H. destruct H.
    exists a0. split.
    exists a0; split; simpl; auto. auto.
    destruct IHc.
    intros. apply HM.
    apply cons_elem; auto.
    auto.
    exists x. 
    destruct H0.
    split; auto.
    apply cons_elem; auto.
  Qed.

  Lemma RS_elem' : forall S a, List.In S M -> a ∈ proj1_sig S -> a ∈ RS.
  Proof.
    intros. unfold RS.
    clear -M HM H H0.
    induction M; simpl. elim H.
    destruct H. subst a0.
    apply app_elem. left. auto.
    apply app_elem. right.
    apply IHc. intros.
    apply HM; auto.
    apply cons_elem; auto.
    auto.
  Qed.

  Theorem directed_joinables :
    exists Q, upper_bound Q M /\ proj1_sig Q ⊆ R.
  Proof.
    destruct (swell RS) as [G [?[??]]]; auto.
    generalize RS_elem'.
    revert M HM Minh RS.
    case_eq hf; intros; auto.
    destruct Minh as [S ?].
    assert (inh true (proj1_sig S)).
    destruct S.
    destruct i. auto.
    destruct H1 as [q [??]].
    destruct H2.
    destruct x.
    red.
    destruct H3.
    destruct (H3 c c0) as [m [n [?[??]]]]; auto.
    exists (m,n).
    apply H0 with q; auto.

    red; intros.
    apply RS_elem in H.
    destruct H as [S [??]].
    apply (HM S); auto.
    exists (exist _ G H1). split.
    red; intros.
    destruct H2 as [q [??]].
    rewrite H3.
    red; simpl; intros.
    red; simpl; intros.
    exists a. exists b. split; auto.
    apply H.
    apply RS_elem' with q; auto.
    simpl. auto.
  Qed.
End directed_joinables.

Section joinable_plt.
  Variable hf:bool.
  Variables A B:preord.
  Variable HAeff : effective_order A.
  Variable HAplt : plotkin_order hf A.
  Variable HBeff : effective_order B.
  Variable HBplt : plotkin_order hf B.

  (**  The intersection of an approximable relation with the cartesian
       product of MUB closed sets is a joinable relation.
    *)
  Lemma intersect_approx
    (R:A×B -> Prop)
    (Hdec : forall x, {R x}+{~R x})
    (HR : forall a a' b b', a ≤ a' -> b' ≤ b -> R (a,b) -> R (a',b'))
    (HRdir : forall a (M:finset B) (HMinh:inh hf M),
      (forall b, b ∈ M -> R (a,b)) ->
      exists z, R (a,z) /\ upper_bound z M)
    (P:finset A) (Q:finset B)
    (Hinh : if hf then exists x y, x ∈ P /\ y ∈ Q /\ R (x,y) else True)
    (HP:mub_closed hf A P) (HQ:mub_closed hf B Q) :
    is_joinable_relation hf (finsubset (A×B) R Hdec (finprod P Q)).
  Proof.
    assert (HR2 : forall x y : Preord_Eq (A × B), x ≈ y -> R x -> R y).
    intros.
    destruct x; destruct y.
    apply HR with c c0; auto.
    destruct H as [[??][??]]; auto.
    destruct H as [[??][??]]; auto.

    split.
    destruct hf; auto.
    destruct Hinh as [x [y [?[??]]]].
    red.
    exists (x,y).
    apply finsubset_elem.
    auto.
    split; auto.
    apply finprod_elem. split; auto.

    repeat intro. 
    assert (x ∈ P). apply HP with (image π₁ G); auto.
    apply inh_image; auto.
    red; intros.
    apply image_axiom2 in H1. destruct H1 as [y [??]].
    apply H in H1.
    apply finsubset_elem in H1.
    destruct H1.
    destruct y.
    apply finprod_elem in H1.
    destruct H1. simpl in H2.
    rewrite <- H2 in H1. auto.
    intros.
    destruct x0; destruct y0.
    eapply HR. 3: apply H4.
    destruct H3 as [[??][??]]; auto.
    destruct H3 as [[??][??]]; auto.

    destruct (HRdir x (image π₂ G)).
    apply inh_image; auto.
    intros. 
    apply image_axiom2 in H2.
    destruct H2 as [q [??]].
    generalize H2; intro H2'.
    apply H in H2.
    apply finsubset_elem in H2.
    destruct H2.
    destruct q.
    simpl in H3.
    apply HR with c c0; auto.
    apply H0.
    change c with (π₁#((c,c0):A×B)).
    apply image_axiom1. auto.
    intros. 
    destruct x0; destruct y.
    apply HR with c c0; auto.
    destruct H4 as [[??][??]]; auto.
    destruct H4 as [[??][??]]; auto.
    destruct H2.
    destruct (mub_complete HBplt (image π₂ G) x0) as [y [??]]; auto.
    apply inh_image; auto.
    exists y. split; auto.
    apply finsubset_elem.
    intros.
    destruct x1. destruct y0.
    apply HR with c c0; auto.
    destruct H6 as [[??][??]]; auto.
    destruct H6 as [[??][??]]; auto.
    split.
    apply finprod_elem; split; auto.
    apply (HQ (image π₂ G)); auto.
    apply inh_image; auto.
    red; intros.
    apply image_axiom2 in H6.
    destruct H6 as [q [??]].
    apply H in H6.
    apply finsubset_elem in H6.
    destruct H6.
    destruct q.
    apply finprod_elem in H6.
    destruct H6.
    rewrite H7; simpl; auto.
    intros.
    destruct x1; destruct y0.
    apply HR with c c0; auto.
    destruct H8 as [[??][??]]; auto.
    destruct H8 as [[??][??]]; auto.
    apply HR with x x0; auto.
    destruct H4; auto.
  Qed.

  Definition mkrel (R:joinable_relation hf A B) (x:A×B) :=
    exists a b, (a,b) ∈ proj1_sig R /\ a ≤ fst x /\ snd x ≤ b.

  Lemma mkrel_dec R : forall x, {mkrel R x}+{~mkrel R x}.
  Proof.
    intros.
    unfold mkrel.
    destruct (finset_find_dec (A×B) (fun y => fst y ≤ fst x /\ snd x ≤ snd y))
      with (proj1_sig R).
    intros.
    destruct H. destruct H0.
    destruct H; destruct H1.
    split; eauto.
    intros.
    destruct (eff_ord_dec A HAeff (fst x0) (fst x)).
    destruct (eff_ord_dec B HBeff (snd x) (snd x0)).
    left. split; auto.
    right; intros [??]; apply n; auto.
    right; intros [??]; apply n; auto.
    destruct s.
    left. exists (fst x0). exists (snd x0). auto.
    right; intros [a [b [?[??]]]].
    apply (n (a,b)); auto.
  Qed.

  Lemma mkrel_dir R : 
    forall a (M:finset B) (HMinh:inh hf M), (forall b, b ∈ M -> mkrel R (a,b)) ->
      exists z, mkrel R (a,z) /\ upper_bound z M.
  Proof.
    intros.
    set (P q := mkrel R q /\ snd q ∈ M /\ fst q ≤ a).
    assert (POK : forall x y, x ≈ y -> P x -> P y).
    clear. unfold P; intros.
    destruct H as [[??][??]].
    intuition.
    destruct H4 as [m [n [?[??]]]].
    exists m. exists n. intuition eauto.
    apply member_eq with (snd x); auto.
    eauto.
    assert (Pdec : forall q, { P q }+{ ~P q }).
    intro q.
    destruct (mkrel_dec R q).
    destruct (eff_in_dec HBeff M (snd q)).
    destruct (eff_ord_dec A HAeff (fst q) a).
    left. split; auto.
    right; intros [?[??]]; apply n; auto.
    right; intros [?[??]]; apply n; auto.
    right; intros [?[??]]; apply n; auto.

    set (R' := finsubset (A×B) (fun q => fst q ≤ a)
      (fun q => eff_ord_dec A HAeff (fst q) a) (proj1_sig R)).

    assert (R'inh : inh hf R').

    subst R'.
    clear -H HMinh.
    unfold mkrel in H.
    destruct hf; auto.
    destruct R.
    destruct i. simpl.
    destruct HMinh.
    destruct (H x0) as [p [q [??]]]; auto.
    simpl in *.
    exists (p,q).
    apply finsubset_elem; auto.
    intros.
    transitivity (fst x1); auto.
    destruct H3 as [[??][??]]; auto.
    split; auto.
    simpl. destruct H2; auto.

    destruct (mub_complete HAplt (image π₁ R') a).
    apply inh_image. auto.

    red; intros.
    apply image_axiom2 in H0.
    destruct H0. destruct H0.
    unfold R' in H0.
    apply finsubset_elem in H0.
    destruct H0.
    rewrite H1; auto.
    intros.
    transitivity (fst x1); auto.
    destruct H2 as [[??][??]]; auto.
    destruct H0.
    assert (is_joinable_relation hf (proj1_sig R)). apply proj2_sig.
    destruct H2 as [_ ?].
    destruct (H2 R') with x; auto.
    red; intros.
    unfold R' in H3.
    apply finsubset_elem in H3.
    destruct H3; auto.
    intros.
    transitivity (fst x0); auto.
    destruct H4 as [[??][??]]; auto.
    destruct H3.
    exists x0. split.
    exists x. exists x0.
    split; auto.
    red; intros.
    generalize (H x1 H5). intros.
    destruct H6 as [p [q [?[??]]]].
    simpl in H7, H8.
    transitivity q; auto.
    apply H4.
    change q with (π₂#((p,q):A×B)).
    apply image_axiom1.
    unfold R'.
    apply finsubset_elem; auto.
    intros.
    transitivity (fst x2); auto.
    destruct H9 as [[??][??]]; auto.
  Qed.

  Lemma mkrel_ord R : forall a a' b b', a ≤ a' -> b' ≤ b -> mkrel R (a,b) -> mkrel R (a',b').
  Proof.
    unfold mkrel; intros.
    destruct H1 as [p [q [?[??]]]].
    exists p. exists q. split; auto. split.
    transitivity a; auto.
    transitivity b; auto.
  Qed.

  Lemma mkrel_mono : forall R S, R ≤ S ->
    forall x, mkrel R x -> mkrel S x.
  Proof.
    unfold mkrel; intros.
    destruct H0 as [a [b [?[??]]]].
    destruct (H a b) as [a' [b' [?[??]]]]; auto.
    exists a'. exists b'. split; auto.
    split.
    transitivity a; auto.
    transitivity b; auto.
  Qed.

  Lemma mkrel_mono' : forall (R S:joinable_relation hf A B),
    (forall x, x ∈ proj1_sig R -> mkrel S x) -> R ≤ S.
  Proof.
    repeat intro.
    destruct (H (a,b)) as [a' [b' [?[??]]]]. auto.
    exists a'. exists b'. split; auto.
  Qed.

  (**  Joinable relations have normal sets.  In particular, the set of all
       joinable relations that are subsets of the cartesian product of two
       MUB-closed sets is a normal set.
    *)
  Section join_rel_normal.
    Variable (P:finset A).
    Variable (Q:finset B).
    Variable (HP:mub_closed hf A P).
    Variable (HQ:mub_closed hf B Q).

    Fixpoint select_jrels (l:finset (finset (A×B))) : finset (joinable_rel_order hf A B) :=
      match l with
      | nil => nil
      | cons R l' =>
         match is_joinable_rel_dec' hf A B HAeff HBeff HAplt R with
         | left H => cons (exist _ R H) (select_jrels l')
         | right _ => select_jrels l'
         end
      end.

    Lemma select_jrels_elem1 : forall R (H:is_joinable_relation hf R) XS,
      R ∈ XS -> (exist _ R H : joinable_rel_order hf A B) ∈ select_jrels XS.
    Proof.
      induction XS; simpl; intros.
      apply nil_elem in H0. elim H0.
      destruct (is_joinable_rel_dec' hf A B HAeff HBeff HAplt a).
      apply cons_elem in H0. destruct H0.
      apply cons_elem. left.
      split. red; simpl; intros.
      red; simpl; intros.
      exists a0. exists b. split; auto.
      destruct H0. apply H0; auto.
      red; simpl; intros.
      red; simpl; intros.
      exists a0. exists b. split; auto.
      destruct H0. apply H2; auto.
      apply cons_elem. right.
      apply IHXS. auto.

      apply cons_elem in H0.
      destruct H0.
      elim n.
      destruct H.
      split. apply inh_eq with R; auto.
      intros.
      destruct (e G) with x; auto.
      rewrite H0; auto.
      destruct H2.
      exists x0; split; auto.
      rewrite <- H0; auto.
      apply IHXS; auto.
    Qed.

    Lemma select_jrels_elem2 : forall R XS,
      R ∈ select_jrels XS -> exists R', R ≈ R' /\ proj1_sig R' ∈ XS.
    Proof.
      induction XS; simpl; intros.
      apply nil_elem in H. elim H.
      destruct (is_joinable_rel_dec' hf A B HAeff HBeff HAplt a).
      apply cons_elem in H.
      destruct H.
      exists (exist (is_joinable_relation hf) a i).
      split; auto.
      simpl. apply cons_elem; auto.
      destruct IHXS as [R' [??]]; auto.
      exists R'; split; auto.
      apply cons_elem; auto.
      destruct IHXS as [R' [??]]; auto.
      exists R'; split; auto.
      apply cons_elem; auto.
    Qed.      

    Definition all_jrels := select_jrels (list_finsubsets (finprod P Q)).

    Lemma all_jrels_complete : forall R,
      R ∈ all_jrels <->
        exists R', R ≈ R' /\ (proj1_sig R' ⊆ finprod P Q).
    Proof.
      intros. split; intros.
      unfold all_jrels in H.
      apply select_jrels_elem2 in H.
      destruct H as [R' [??]].
      exists R'. split; auto.
      apply list_finsubsets_correct in H0. auto.
      destruct H as [R' [??]].      
      unfold all_jrels.
      rewrite H.
      destruct R'.
      apply select_jrels_elem1.
      apply list_finsubsets_complete; auto.
      constructor.
      apply effective_prod; auto.
    Qed.

    Lemma all_jrels_inh' : forall x y, hf = true -> x ∈ P -> y ∈ Q -> exists R, R ∈ all_jrels.
    Proof.
      intros.
      set (R := ((x,y)::nil)%list).
      cut (is_joinable_relation hf R).
      intros HR. exists (exist _ R HR).
      apply all_jrels_complete.
      exists (exist _ R HR). split; auto.
      red; simpl; intros.
      unfold R in H2.
      apply cons_elem in H2. destruct H2.
      rewrite H2. apply finprod_elem; split; auto.
      apply nil_elem in H2. destruct H2.

      rewrite H. split.
      red. exists (x,y). unfold R.
      apply cons_elem; auto.
      intros.
      assert (forall z, z ∈ G <-> z ≈ (x,y)).
      intros. split; intros.
      apply H2 in H4.
      unfold R in H4.
      apply cons_elem in H4. destruct H4; auto.
      apply nil_elem in H4. elim H4.
      destruct HGinh.
      apply member_eq with x1; auto.
      apply H2 in H5.
      unfold R in H5.
      apply cons_elem in H5.
      destruct H5. rewrite H5. auto.
      apply nil_elem in H5. elim H5.

      exists y.
      split.
      apply member_eq with (x,y); auto.
      cut (x ≈ x0).
      intros [??]. split; split; auto.
      destruct H3.
      split.
      apply H3.
      change x with (π₁#((x,y):A×B)). apply image_axiom1.
      apply H4. auto.
      apply H5.
      red; intros.
      apply image_axiom2 in H6.
      destruct H6 as [?[??]].
      rewrite H7.
      apply H4 in H6.
      rewrite H6. simpl. auto.
      apply H3.
      change x with (π₁#((x,y):A×B)). apply image_axiom1.
      apply H4. auto.
      unfold R. apply cons_elem; auto.
      red; intros.
      apply image_axiom2 in H5. destruct H5 as [?[??]].
      apply H4 in H5.
      rewrite H6. rewrite H5. auto.
    Qed.

    Lemma all_jrels_inh : inh hf P -> inh hf Q -> inh hf all_jrels.
    Proof.
      generalize all_jrels_inh'.
      generalize all_jrels.
      destruct hf; auto.
      intros.
      destruct H0 as [x ?].
      destruct H1 as [y ?].
      apply (H x y); auto.
    Qed.

    Lemma joinable_rels_normal : 
      inh hf P -> inh hf Q ->
      normal_set (joinable_rel_order hf A B) (joinable_rel_effective hf A B HAeff HBeff HAplt) hf
        all_jrels.
    Proof.
      red. intros Pinh Qinh.
      split.

      apply all_jrels_inh; auto.

      red. simpl; intros.
      set (R := finsubset (A×B) (mkrel z) (mkrel_dec z) (finprod P Q)).
      assert (is_joinable_relation hf R).
      apply intersect_approx; auto.
      apply mkrel_ord.
      apply mkrel_dir.
      apply (inh_sub _ hf) in H; auto.
      
      unfold all_jrels in *. simpl in *.
      clear R Pinh Qinh.
      clear M Hinh.
      unfold mkrel.
      revert H.
      generalize (all_jrels_complete).
      unfold all_jrels.
      unfold select_jrels. simpl.
      generalize (fun x : joinable_relation hf A B =>
         joinable_rel_effective_obligation_1 hf A B HAeff HBeff HAplt x z).
      generalize (is_joinable_rel_dec' hf A B HAeff HBeff HAplt).
      revert z.
      case hf; simpl; intros; auto.
      destruct H0 as [x ?].
      apply finsubset_elem in H0.
      destruct H0.
      rewrite H in H0.
      destruct H0 as [R' [??]].
      rewrite H0 in H1.
      assert (inh true (proj1_sig R')).
      destruct R'. destruct i.
      auto.
      destruct H3 as [[p q] ?].
      exists p. exists q.
      generalize (H2 (p,q) H3); intros.
      apply finprod_elem in H4.
      destruct H4; split; auto.
      intros. rewrite <- H1; auto.

      exists (exist _ R H0).
      split.
      red; intros.
      generalize (H x H1).
      intros.
      apply finsubset_elem in H2.
      destruct H2; auto.
      apply all_jrels_complete in H2.
      destruct H2 as [R' [??]].
      rewrite H2 in H3.
      rewrite H2.
      red; simpl; intros.
      red; simpl; intros.
      exists a. exists b. split; auto.
      unfold R.
      apply finsubset_elem.
      clear. intros.
      destruct x. destruct y.
      apply mkrel_ord with c c0; auto.
      destruct H as [[??][??]]; auto.
      destruct H as [[??][??]]; auto.
      split; auto.
      eapply mkrel_mono with R'; auto.
      exists a. exists b. auto.
      clear. intros. rewrite <- H; auto.
      
      apply finsubset_elem.
      intros. rewrite <- H1. auto.
      split.
      apply all_jrels_complete.
      exists (exist _ R H0).
      split; auto.
      red; intros.
      simpl in H1.
      unfold R in H1.
      apply finsubset_elem in H1.
      destruct H1; auto.
      clear. intros.
      destruct x. destruct y.
      apply mkrel_ord with c c0; auto.
      destruct H as [[??][??]]; auto.
      destruct H as [[??][??]]; auto.
      red; simpl; intros.
      red; simpl; intros.
      unfold R in H1.
      apply finsubset_elem in H1.
      destruct H1; auto.
      clear. intros.
      destruct x. destruct y.
      apply mkrel_ord with c c0; auto.
      destruct H as [[??][??]]; auto.
      destruct H as [[??][??]]; auto.
    Qed.
  End join_rel_normal.

  (** The joinable relations have normal sets *)
  Lemma joinable_rel_has_normals : 
    has_normals (joinable_rel_order hf A B) (joinable_rel_effective hf A B HAeff HBeff HAplt) hf.
  Proof.
    red. intros M Minh.
    set (XS := (concat A (List.map (fun R => List.map (@fst _ _) (proj1_sig R)) M) : finset A)).
    set (YS := (concat B (List.map (fun R => List.map (@snd _ _) (proj1_sig R)) M) : finset B)).
    set (MS := mub_closure HAplt XS).
    set (NS := mub_closure HBplt YS).
    assert (HXYS : forall R x y, List.In R M -> (x,y) ∈ proj1_sig R -> x ∈ XS /\ y ∈ YS).
    subst XS YS.
    clear. induction M; simpl; intros.
    elim H. destruct H. subst R.
    split.
    apply app_elem.
    left.
    destruct H0 as [q [??]].
    exists (fst q).
    split; auto.
    apply List.in_map. auto.
    destruct H0 as [[??][??]]; split; auto.
    apply app_elem.
    left.
    destruct H0 as [q [??]].
    exists (snd q).
    split; auto.
    apply List.in_map. auto.
    destruct H0 as [[??][??]]; split; auto.
    split.
    apply app_elem. right.
    eapply IHM; eauto.
    apply app_elem. right.
    eapply IHM; eauto.

    exists (all_jrels MS NS).
    split.
    red; intros.
    destruct H as [q [??]].
    apply all_jrels_complete.
    exists q. split; auto.
    red; intros.
    destruct a0 as [x y].
    apply finprod_elem.
    split.
    unfold MS.
    apply mub_clos_incl.
    eapply HXYS; eauto.
    unfold NS.
    apply mub_clos_incl.
    eapply HXYS; eauto.

    assert (Xinh : inh hf XS).
    subst XS; clear -Minh.
    destruct hf; auto.
    destruct Minh as [x ?].
    destruct H as [? [? _]].
    simpl.
    destruct M. elim H.
    destruct c. simpl.
    destruct i.
    destruct i.
    destruct x2.
    exists c.
    apply app_elem. left.
    change (c ∈ image π₁ x1).
    change c with (π₁#((c,c0):(A×B))).
    apply image_axiom1. auto.
    assert (Yinh : inh hf YS).
    subst YS; clear -Minh.
    destruct hf; auto.
    destruct Minh as [x ?].
    destruct H as [? [? _]].
    simpl.
    destruct M. elim H.
    destruct c. simpl.
    destruct i.
    destruct i.
    destruct x2.
    exists c0.
    apply app_elem. left.
    change (c0 ∈ image π₂ x1).
    change c0 with (π₂#((c,c0):(A×B))).
    apply image_axiom1. auto.

    apply joinable_rels_normal.
    unfold MS. apply mub_clos_mub; auto.
    unfold NS. apply mub_clos_mub; auto.
    unfold MS. apply inh_sub with XS.
    apply mub_clos_incl. auto.
    unfold NS. apply inh_sub with YS.
    apply mub_clos_incl. auto.
  Qed.

  (** The joinable relations form a Plotkin order *)
  Definition joinable_rel_plt : plotkin_order hf (joinable_rel_order hf A B)
    := norm_plt 
         (joinable_rel_order hf A B)
         (joinable_rel_effective hf A B HAeff HBeff HAplt)
         hf
         (joinable_rel_has_normals).

End joinable_plt.