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

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

Require Import Setoid.
Require Import List.

Require Import basics.
Require Import categories.
Require Import preord.
Require Import sets.
Require Import finsets.
Require Import esets.
Require Import effective.
Require Import plotkin.
Require Import profinite.
Require Import embed.
Require Import directed.
Require Import cont_functors.
Require Import cpo.


(**  * Bilimits and fixpoints of continuous functors.

       Here we construct the colimit of directed systems.
       The carrier of the bilimit is the disjoint union
       of the directed system.  The order on the bilimit
       elements holds if there exists some larger poset
       into which both elements can be embedded where
       the ordering holds.
  *)
Section bilimit.
  Variable hf:bool.
  Variable I:directed_preord.
  Variable DS:directed_system I (EMBED hf).


  Record limset :=
    LimSet
    { idx : I
    ; elem : ds_F DS idx
    }.

  Definition limset_order (x y:limset) :=
    exists k (Hxk : idx x ≤ k) (Hyk : idx y ≤ k),
      ds_hom DS (idx x) k Hxk (elem x) 
      ≤ ds_hom DS (idx y) k Hyk (elem y).


  (**  This lemma shows that if two elements compare in any larger poset,
       the compare in _every_ larger poset.
    *)
  Lemma limset_order_exists_forall x y :
    limset_order x y <->
    forall k (Hxk : idx x ≤ k) (Hyk : idx y ≤ k),
      ds_hom DS (idx x) k Hxk (elem x) 
      ≤ ds_hom DS (idx y) k Hyk (elem y).
  Proof.
    split; intros.
    destruct H as [k' [Hxk' [Hyk' ?]]].
    destruct (choose_ub I k k') as [q [??]].
    assert (Hxq : idx x ≤ q).
    transitivity k; auto.
    assert (Hyq : idx y ≤ q).
    transitivity k; auto.
    assert (ds_hom DS (idx x) q Hxq (elem x) ≤
            ds_hom DS (idx y) q Hyq (elem y)).
    rewrite <- (ds_compose DS (idx x) k' q Hxk' H1 Hxq).
    rewrite <- (ds_compose DS (idx y) k' q Hyk' H1 Hyq).
    simpl. apply embed_mono. auto.

    rewrite <- (ds_compose DS (idx x) k q Hxk H0 Hxq) in H2.
    rewrite <- (ds_compose DS (idx y) k q Hyk H0 Hyq) in H2.
    simpl in H2.

    apply embed_reflects in H2. auto.

    destruct (choose_ub I (idx x) (idx y)) as [k [??]].
    exists k, H0, H1.
    apply H.    
  Qed.

  Lemma limset_order_refl (x:limset) : limset_order x x.
  Proof.
    exists (idx x). exists (ord_refl _ _).  exists (ord_refl _ _). auto.
  Qed.

  Lemma limset_order_trans (x y z:limset) : 
    limset_order x y -> limset_order y z -> limset_order x z.
  Proof.
    intros [k1 [Hxk1 [Hyk1 ?]]].
    intros [k2 [Hyk2 [Hzk2 ?]]].
    destruct (choose_ub I k1 k2) as [k [??]].
    assert (Hxk : idx x ≤ k). transitivity k1; auto.
    assert (Hyk : idx y ≤ k). transitivity k1; auto.
    assert (Hzk : idx z ≤ k). transitivity k2; auto.
    exists k. exists Hxk. exists Hzk.
    transitivity (ds_hom DS (idx y) k Hyk (elem y)).
    rewrite <- (ds_compose DS (idx x) k1 k Hxk1 H1 Hxk).
    rewrite <- (ds_compose DS (idx y) k1 k Hyk1 H1 Hyk).
    simpl. apply embed_mono. auto.
    rewrite <- (ds_compose DS (idx y) k2 k Hyk2 H2 Hyk).
    rewrite <- (ds_compose DS (idx z) k2 k Hzk2 H2 Hzk).
    simpl. apply embed_mono. auto.
  Qed.

  Definition limord_mixin :=
    Preord.Mixin limset limset_order limset_order_refl limset_order_trans.

  Canonical Structure limord := Preord.Pack limset limord_mixin.

  (**  The order is decidable.
    *)
  Lemma limset_order_dec (x y:limset) :
    { limset_order x y } + { ~limset_order x y }.
  Proof.
    destruct (choose_ub I (idx x) (idx y)) as [k [??]].
    destruct (eff_ord_dec _ (PLT.effective (ds_F DS k))
                (ds_hom DS (idx x) k H (elem x))
                (ds_hom DS (idx y) k H0 (elem y))).
    left.
    exists k. exists H. exists H0. auto.
    right. intro.
    rewrite limset_order_exists_forall in H1.
    apply n. apply H1.
  Qed.

  (**  Furthermore, all the elements can be enumerated.
    *)
  Definition limset_enum : eset limord :=
    fun n => let (p,q) := pairing.unpairing n in
         match eff_enum _ (dir_effective I) p with
         | Some i =>
              match eff_enum _ (PLT.effective (ds_F DS i)) q with
              | Some x => Some (LimSet i x)
              | None => None
              end
         | None => None
         end.

  (** Thus, the bilimit is an effective order. *)
  Program Definition limord_effective : effective_order limord :=
    EffectiveOrder limord limset_order_dec limset_enum _.
  Next Obligation.
    intros [i x].
    generalize (eff_complete _ (dir_effective I) i).
    intros [p ?].
    case_eq (eff_enum I (dir_effective I) p); intros.
    2: rewrite H0 in H; elim H.
    rewrite H0 in H.
    destruct H.
    set (x' := ds_hom DS i c H x).
    assert (x' ∈ eff_enum _ (PLT.effective (ds_F DS c))).
    apply eff_complete.
    destruct H2 as [q ?].
    case_eq (eff_enum _ (PLT.effective (ds_F DS c)) q); intros.
    2: rewrite H3 in H2; elim H2.
    rewrite H3 in H2.
    exists (pairing.pairing (p,q)).
    unfold limset_enum.
    rewrite pairing.unpairing_pairing.
    rewrite H0. rewrite H3.
    split.
    exists c. simpl. exists H. exists (ord_refl _ _).
    rewrite (ds_ident DS c (ord_refl _ _)). simpl. auto.
    exists c. simpl. exists (ord_refl _ _). exists H.
    rewrite (ds_ident DS c (ord_refl _ _)). simpl. auto.
  Qed.

  (**  Moreover, the bilimit has normal sets.
    *)
  Lemma limord_has_normals : has_normals limord limord_effective hf.
  Proof.
    hnf. intros.
    destruct (choose_ub_set I (map idx X)) as [k ?].
    
    assert { M | X ≈ map (LimSet k) M }.
    clear Hinh.
    induction X.
    exists nil. simpl; auto.
    destruct IHX as [M ?].
    hnf; intros. apply u.
    simpl. apply cons_elem; auto.
    assert (Hak : idx a ≤ k).
    apply u. simpl. apply cons_elem; auto.
    exists (ds_hom DS (idx a) k Hak (elem a) :: M).
    split. hnf; simpl; intros.
    apply cons_elem in H.
    destruct H. rewrite H.
    apply cons_elem. left.
    split.
    exists k; simpl. exists Hak. exists (ord_refl _ _).
    rewrite (ds_ident DS k (ord_refl _ _)).
    simpl. auto.
    exists k; simpl. exists (ord_refl _ _). exists Hak.
    rewrite (ds_ident DS k (ord_refl _ _)).
    simpl. auto.
    apply cons_elem. right.
    rewrite <- e; auto.
    hnf; intros.
    simpl in H.
    apply cons_elem in H.
    destruct H.
    rewrite H.
    apply cons_elem. left.
    split.
    exists k; simpl. exists (ord_refl _ _). exists Hak.
    rewrite (ds_ident DS k (ord_refl _ _)).
    simpl. auto.
    exists k; simpl. exists Hak. exists (ord_refl _ _).
    rewrite (ds_ident DS k (ord_refl _ _)).
    simpl. auto.
    apply cons_elem. right.
    rewrite e; auto.
    
    destruct X0 as [M HM].
    exists (map (LimSet k) (mub_closure (PLT.plotkin (ds_F DS k)) M)).
    split.
    hnf; intros.
    rewrite HM in H.
    destruct H as [q [??]].
    apply in_map_iff in H.
    destruct H as [q' [??]].
    subst q.
    assert (q' ∈ mub_closure (PLT.plotkin (ds_F DS k)) M).
    apply mub_clos_incl. exists q'; auto.
    destruct H as [x [??]].
    rewrite H0.
    exists (LimSet k x).
    split.
    apply in_map. auto.
    split.
    exists k; simpl.
    exists (ord_refl _ _ ).
    exists (ord_refl _ _ ).
    apply embed_mono; auto.
    exists k; simpl.
    exists (ord_refl _ _ ).
    exists (ord_refl _ _ ).
    apply embed_mono; auto.
    split.
    revert Hinh. 
    unfold inh.
    pattern hf at 1 2. case hf; auto.
    intros [x ?].
    rewrite HM in H.
    destruct H as [x' [??]].
    apply in_map_iff in H.
    destruct H as [q [??]].
    subst x'.
    assert (q ∈ mub_closure (PLT.plotkin (ds_F DS k)) M).
    apply mub_clos_incl. exists q; split; auto.
    destruct H as [q' [??]].
    exists (LimSet k q').
    exists (LimSet k q').
    split; auto.
    apply in_map. auto.

    intros.
    apply prove_directed.
    generalize (refl_equal hf).
    pattern hf at 2. case hf; intros. 
    pattern hf at 1. rewrite H; auto.
    pattern hf at 1. rewrite H.
    destruct (choose_ub I (idx z) k) as [k' [Hzk' Hkk']].
    generalize (embed_directed0 (ds_hom DS k k' Hkk')
        (ds_hom DS (idx z) k' Hzk' (elem z))).
    rewrite H at 1.
    intros [q ?].
    destruct (mub_complete (PLT.plotkin (ds_F DS k)) nil q) as [q' [??]].
    hnf. pattern hf at 1. rewrite H. auto.
    hnf; simpl; intros. apply nil_elem in H1. elim H1.
    assert (q' ∈ mub_closure (PLT.plotkin (ds_F DS k)) M).
    apply mub_clos_mub with nil; auto.
    hnf. pattern hf at 1. rewrite H. auto.

    hnf; intros. apply nil_elem in H3; elim H3.
    destruct H3 as [q'' [??]].
    exists (LimSet k q'').
    apply finsubset_elem.
    intros. rewrite <- H5; auto.
    split.
    exists (LimSet k q'').
    split; auto.
    apply in_map. auto.
    exists k'. simpl. exists Hkk'. exists Hzk'.
    transitivity (ds_hom DS k k' Hkk' q); auto.
    apply embed_mono.
    rewrite <- H4; auto.

    intros.
    apply finsubset_elem in H.
    apply finsubset_elem in H0.
    destruct H. destruct H0.
    destruct H as [x' [??]].
    destruct H0 as [y' [??]].
    apply in_map_iff in H.
    destruct H as [x'' [??]].
    apply in_map_iff in H0.
    destruct H0 as [y'' [??]].
    subst x' y'.
    rewrite H3 in H1. rewrite H4 in H2.
    destruct H1 as [k' [Hkk' [Hzk' ?]]]. simpl in *.

    destruct H2 as [l [Hl1 [Hl2 ?]]]. simpl in *.
    destruct (choose_ub I k' l) as [m [Hm1 Hm2]].
    assert (Hkm : k ≤ m). 
    transitivity k'; auto.
    assert (Hzm : idx z ≤ m).
    transitivity k'; auto.

    destruct (embed_directed2 (ds_hom DS k m Hkm)
      (ds_hom DS (idx z) m Hzm (elem z)))
      with x'' y'' as [c [?[??]]]; auto.
    rewrite <- (ds_compose DS k k' m Hkk' Hm1 Hkm).
    rewrite <- (ds_compose DS (idx z) k' m Hzk' Hm1 Hzm).
    simpl. apply embed_mono. auto.
    rewrite <- (ds_compose DS k l m Hl1 Hm2 Hkm).
    rewrite <- (ds_compose DS (idx z) l m Hl2 Hm2 Hzm).
    simpl. apply embed_mono. auto.

    destruct (mub_complete (PLT.plotkin (ds_F DS k))
      (x''::y''::nil) c) as [c' [??]].
    apply elem_inh with x''. apply cons_elem; auto.
    hnf; simpl; intros.
    apply cons_elem in H8. destruct H8.
    rewrite H8; auto.
    apply cons_elem in H8. destruct H8.
    rewrite H8; auto.
    apply nil_elem in H8. elim H8.
    assert (c' ∈ mub_closure (PLT.plotkin (ds_F DS k)) M).
    apply mub_clos_mub with (x''::y''::nil); auto.

    apply elem_inh with x''. apply cons_elem; auto.
    hnf; simpl; intros.
    apply cons_elem in H10. destruct H10.
    rewrite H10; auto.
    exists x''; split; auto.
    apply cons_elem in H10. destruct H10.
    rewrite H10; auto.
    exists y''; split; auto.
    apply nil_elem in H10. elim H10.
    destruct H10 as [c'' [??]].
    exists (LimSet k c'').
    split.
    rewrite H3.
    exists k'. simpl. exists Hkk'. exists Hkk'.
    apply embed_mono; auto.
    rewrite <- H11.
    destruct H8.
    apply H8. apply cons_elem; auto.
    split.
    rewrite H4.
    exists k'. simpl. exists Hkk'. exists Hkk'.
    apply embed_mono; auto.
    rewrite <- H11.
    destruct H8.
    apply H8.
    apply cons_elem. right.
    apply cons_elem. auto.
    apply finsubset_elem.
    intros. rewrite <- H12; auto.
    split; auto.
    exists (LimSet k c'').
    split; auto.
    apply in_map. auto.
    exists m. simpl.
    exists Hkm. exists Hzm.
    transitivity (ds_hom DS k m Hkm c); auto.
    apply embed_mono.
    rewrite <- H11; auto.
    intros. rewrite <- H1. auto.
    intros. rewrite <- H1. auto.
  Qed.

  (**  Altogether, this makes the bilimit a plotkin order.
    *)
  Definition limord_plotkin : plotkin_order hf limord :=
    norm_plt limord limord_effective hf limord_has_normals.

  Definition bilimit : PLT.ob hf :=
    PLT.Ob hf limset (PLT.Class _ _ limord_mixin limord_effective limord_plotkin).
  

  (**  Here we define the spokes of the colimit cocone.
    *)
  Program Definition limset_spoke (i:I) : hom (EMBED hf) (ds_F DS i) bilimit
    := Embedding hf (ds_F DS i : ob (EMBED hf)) bilimit
    (fun x => LimSet i x) _ _ _ _.
  Next Obligation.
    intros. hnf. simpl. exists i. exists (ord_refl _ _). exists (ord_refl _ _).
    rewrite (ds_ident DS i (ord_refl I i)). simpl. auto.
  Qed.
  Next Obligation.
    intros. 
    destruct H as [k [Hki [Hki' ?]]]. simpl in *.
    rewrite <- (ds_compose DS i k k Hki' (ord_refl I k) Hki) in H.
    rewrite (ds_ident DS k (ord_refl I k)) in H.
    simpl in H.
    apply embed_reflects in H. auto.
  Qed.
  Next Obligation.
    intros.
    destruct (choose_ub I i (idx y)) as [k [??]].
    generalize (refl_equal hf). pattern hf at 2. case hf.
    intros. pattern hf at 1. rewrite H1. auto.
    intros. pattern hf at 1. rewrite H1.
    generalize (embed_directed0 (ds_hom DS i k H) 
      (ds_hom DS (idx y) k H0 (elem y))).
    pattern hf at 1. rewrite H1.
    intros [q ?].
    exists q.
    hnf; simpl.
    exists k. exists H. exists H0.
    auto.
  Qed.
  Next Obligation.
    simpl; intros.
    destruct H as [k1 [Hik1 [Hyk1 ?]]]. simpl in *.
    destruct H0 as [k2 [Hik2 [Hyk2 ?]]]. simpl in *.
    destruct (choose_ub I k1 k2) as [k [??]].
    assert (i ≤ k). transitivity k1; auto.
    assert (idx y ≤ k). transitivity k1; auto.
    destruct (embed_directed2 (ds_hom DS i k H3)
      (ds_hom DS (idx y) k H4 (elem y)) a b) as [q [?[??]]].
    rewrite <- (ds_compose DS i k1 k Hik1 H1 H3).
    rewrite <- (ds_compose DS (idx y) k1 k Hyk1 H1 H4).
    simpl. apply embed_mono. auto.
    rewrite <- (ds_compose DS i k2 k Hik2 H2 H3).
    rewrite <- (ds_compose DS (idx y) k2 k Hyk2 H2 H4).
    simpl. apply embed_mono. auto.
    exists q. split; auto.
    exists k. simpl.
    exists H3. exists H4. auto.
  Qed.

  Lemma limset_spoke_commute : forall i j (Hij:i≤j),
     limset_spoke i ≈ limset_spoke j ∘ ds_hom DS i j Hij.
  Proof.
    intros; split; hnf; simpl; intros.
    exists j. simpl. exists Hij. exists (ord_refl _ _).
    rewrite (ds_ident DS j (ord_refl _ _)). simpl; auto.
    exists j. simpl. exists (ord_refl _ _). exists Hij.
    rewrite (ds_ident DS j (ord_refl _ _)). simpl; auto.
  Qed.

  Definition bilimit_cocone : cocone DS :=
    Cocone DS bilimit limset_spoke limset_spoke_commute.

  Section bilimit_univ.
    Variable YC:cocone DS.

    Program Definition limord_univ : bilimit ⇀ YC :=
      Embedding hf bilimit (YC : ob (EMBED hf))
        (fun x => let (i,x') := x in cocone_spoke YC i x')
        _ _ _ _.
    Next Obligation.
      simpl; intros.
      destruct a as [i a].
      destruct a' as [i' a'].
      hnf in H. simpl in H.
      destruct H as [k [Hk1 [Hk2 ?]]].
      generalize (cocone_commute YC i k Hk1); intros.
      rewrite H0; clear H0.
      generalize (cocone_commute YC i' k Hk2); intros.
      rewrite H0. simpl.
      apply embed_mono. auto.
    Qed.
    Next Obligation.
      simpl; intros.
      destruct a as [i a].
      destruct a' as [i' a'].
      destruct (choose_ub I i i') as [k [Hk1 Hk2]].
      exists k. simpl. exists Hk1. exists Hk2.
      apply (embed_reflects (cocone_spoke YC k)).
      apply (use_ord H).
      destruct (cocone_commute YC i k Hk1); intros.
      apply H1; auto.
      destruct (cocone_commute YC i' k Hk2); intros.
      apply H0; auto.
    Qed.
    Next Obligation.      
      intro.
      generalize (refl_equal hf).
      pattern hf at 2.  case hf; intros.
      pattern hf at 1. rewrite H. auto.
      pattern hf at 1. rewrite H.
      destruct (choose_ub_set I nil) as [i _].
      generalize (embed_directed0 (cocone_spoke YC i) y).
      pattern hf at 1. rewrite H.
      intros [x ?].
      exists (LimSet i x). auto.
    Qed.
    Next Obligation.
      intros.
      destruct a as [i a].
      destruct b as [j b].
      destruct (choose_ub I i j) as [k [Hk1 Hk2]].
      rewrite (cocone_commute YC i k Hk1) in H.
      rewrite (cocone_commute YC j k Hk2) in H0.
      destruct (embed_directed2 (cocone_spoke YC k) y
        (ds_hom DS i k Hk1 a)
        (ds_hom DS j k Hk2 b)) as [c [?[??]]]; auto.
      exists (LimSet k c).
      split; auto.
      split.
      exists k. simpl.
      exists Hk1. exists (ord_refl _ _).
      rewrite (ds_ident DS k (ord_refl _ _)).
      simpl. auto.
      exists k. simpl.
      exists Hk2. exists (ord_refl _ _).
      rewrite (ds_ident DS k (ord_refl _ _)).
      simpl. auto.
    Qed.

    Lemma limord_univ_commute i :
      cocone_spoke YC i ≈ limord_univ ∘ limset_spoke i.
    Proof.
      cut (forall x,
        cocone_spoke YC i x ≈ limord_univ (limset_spoke i x)).
      intros. split; intro x; destruct (H x); auto.
      simpl; intros; auto.
    Qed.

    Lemma limord_univ_uniq (f:bilimit ⇀ YC) :
      (forall i, cocone_spoke YC i ≈ f ∘ limset_spoke i) ->
      f ≈ limord_univ.
    Proof.
      intros.
      cut (forall x, f x ≈ limord_univ x).
      intros. split; intro x; destruct (H0 x); auto.
      simpl. intros.
      destruct x as [i x].
      rewrite H. simpl. auto.
    Qed.
  End bilimit_univ.

  Definition bilimit_construction : directed_colimit DS bilimit_cocone :=
    DirectedColimit DS bilimit_cocone
      limord_univ
      limord_univ_commute
      limord_univ_uniq.

  Program Definition ep_set : dir_preord I → (PLT.hom_ord hf bilimit bilimit) :=
    Preord.Hom _ _ 
        (fun i => embed (embed_ep_pair (limset_spoke i)) ∘
                  project (embed_ep_pair (limset_spoke i))) 
        _.
  Next Obligation.
    hnf; simpl; intros.
    hnf; simpl; intros.
    destruct a0 as [x y].
    apply PLT.compose_hom_rel in H0 as [z [??]].
    apply PLT.compose_hom_rel.
    exists (ds_hom DS a b H z).
    split; simpl.
    simpl in *.
    apply project_rel_elem.
    apply project_rel_elem in H0.
    rewrite <- H0.
    generalize (limset_spoke_commute a b H).
    intro. 
    destruct H2. apply H3.
    apply embed_rel_elem.
    simpl in H1.
    apply embed_rel_elem in H1.
    rewrite H1.
    generalize (limset_spoke_commute a b H).
    intros [??]. apply H2.
  Qed.

  Program Definition Iset : Preord.carrier (cl_eset (directed_hf_cl hf) I)
    := exist _ (eff_enum I (dir_effective I)) _.
  Next Obligation.
    hnf. simpl; intros.
    destruct (choose_ub_set I M). exists x. split; auto.
    apply eff_complete.
  Qed.

  Lemma bilimit_cpo_colimit : forall x y:bilimit,
    y ≤ x ->
    exists i a, 
      y ≤ limset_spoke i a /\
      limset_spoke i a ≤ x.
  Proof.
    intros. destruct H as [k [Hk1 [Hk2 ?]]].
    exists k. exists (ds_hom DS (idx x) k Hk2 (elem x)).
    split.
    exists k. simpl. exists Hk1. exists (ord_refl _ _). 
    rewrite (ds_ident DS k (ord_refl _ _)); simpl; auto.
    exists k. simpl. exists (ord_refl _ _). exists Hk2.
    rewrite (ds_ident DS k (ord_refl _ _)); simpl; auto.
  Qed.

  Lemma bilimit_cpo_colimit1 :
    id(bilimit) ≤ ∐(image ep_set Iset).
  Proof.
    hnf. intros [x y] ?.
    simpl in H.
    apply approx_rels.ident_elem in H.
    simpl.
    apply union_axiom.
    destruct (bilimit_cpo_colimit x y) as [i [a [??]]]; auto.
    exists (PLT.hom_rel (ep_set#i)).
    split.
    apply image_axiom1'.
    exists (ep_set#i).
    split; auto.
    apply image_axiom1'.
    exists i. split; auto.
    apply eff_complete.
    simpl.
    apply PLT.compose_hom_rel.
    exists a. split.
    apply project_rel_elem. auto.
    apply embed_rel_elem. auto.
  Qed.

  Lemma bilimit_cpo_colimit2 :
    id(bilimit) ≈ ∐(image ep_set Iset).
  Proof.
    split. apply bilimit_cpo_colimit1.
    apply CPO.sup_is_least.
    hnf. simpl; intros.
    apply image_axiom2 in H. destruct H as [i [??]].
    rewrite H0.
    unfold ep_set. simpl.
    apply (ep_ident hf _ _ _ _ 
      (embed_func_is_ep_pair hf _ _ (limset_spoke i))).
  Qed.

End bilimit.


(**  The following two constructions show that a cocone is a colimit
     in EMBED iff every element can be factored through a spoke of the cocone.

     Passing thorough these constructions is generally the easiest way to
     demonstrate that an embedding functor is continuous.
  *)
Section colimit_decompose.
  Variable hf:bool.
  Variable I:directed_preord.
  Variable DS : directed_system I (EMBED hf).
  Variable CC : cocone DS.
  Variable Hcolimit : directed_colimit DS CC.

  Lemma colimit_decompose : forall x:cocone_point CC,
    { i:I & { a:ds_F DS i | cocone_spoke CC i a ≈ x }}.
  Proof.
    intros.
    set (y := colim_univ Hcolimit (bilimit_cocone hf I DS) x).
    exists (idx _ _ _ y).    
    exists (elem _ _ _ y).
    split.
    apply (embed_reflects 
      (colim_univ Hcolimit (bilimit_cocone hf I DS))).
    generalize (colim_commute Hcolimit (bilimit_cocone hf I DS) (idx _ _ _ y)).
    simpl. intros.
    transitivity (limset_spoke hf I DS (idx _ _ _ y) (elem hf I DS y)).
    rewrite H. auto.
    transitivity y; auto.
    destruct y; simpl; auto.
    apply (embed_reflects 
      (colim_univ Hcolimit (bilimit_cocone hf I DS))).
    generalize (colim_commute Hcolimit (bilimit_cocone hf I DS) (idx _ _ _ y)).
    simpl. intros.
    transitivity (limset_spoke hf I DS (idx _ _ _ y) (elem hf I DS y)).
    transitivity y; auto.
    destruct y; simpl; auto.
    rewrite H; auto.
  Qed.
End colimit_decompose.

Section colimit_decompose2.
  Variable hf:bool.
  Variable I:directed_preord.
  Variable DS : directed_system I (EMBED hf).
  Variable CC : cocone DS.
  Hypothesis decompose : forall x:cocone_point CC,
    { i:I & { a:ds_F DS i | cocone_spoke CC i a ≈ x }}.

  Definition decompose_univ_func (YC:cocone DS) (x:cocone_point CC) :=
    cocone_spoke YC 
       (projT1 (decompose x))
       (projT1 (projT2 (decompose x))).

  Program Definition decompose_univ (YC:cocone DS) : CC ⇀ YC :=
    Embedding hf (cocone_point CC: ob (EMBED hf)) 
                 (cocone_point YC : ob (EMBED hf))
                 (decompose_univ_func YC) _ _ _ _.
  Next Obligation.
    unfold decompose_univ_func. intros.
    destruct (decompose a) as [i [q ?]]. simpl.
    destruct (decompose a') as [j [q' ?]]. simpl.
    destruct (choose_ub I i j) as [k [??]].
    rewrite (cocone_commute YC i k H0).
    rewrite (cocone_commute YC j k H1).
    simpl.
    apply embed_mono.
    apply (embed_reflects (cocone_spoke CC k)).
    apply (use_ord H).
    rewrite <- e.
    rewrite (cocone_commute CC i k H0). auto.
    rewrite <- e0.
    rewrite (cocone_commute CC j k H1). auto.
  Qed.
  Next Obligation.
    unfold decompose_univ_func. intros.
    destruct (decompose a) as [i [q ?]]. simpl in *.
    destruct (decompose a') as [j [q' ?]]. simpl in *.
    destruct (choose_ub I i j) as [k [??]].
    rewrite (cocone_commute YC i k H0) in H.
    rewrite (cocone_commute YC j k H1) in H.
    simpl in H.
    apply embed_reflects in H.
    rewrite <- e.
    rewrite <- e0.
    rewrite (cocone_commute CC i k H0).
    rewrite (cocone_commute CC j k H1).
    simpl. apply embed_mono; auto.
  Qed.
  Next Obligation.
    intros.
    unfold decompose_univ_func.
    destruct hf; simpl; auto.
    destruct (choose_ub_set I nil) as [i ?].
    destruct (embed_directed0 (cocone_spoke YC i) y).
    exists (cocone_spoke CC i x).
    destruct (decompose (cocone_spoke CC i x)) as [m [z' ?]]. simpl.
    rewrite <- H.    
    destruct (choose_ub I m i) as [k [??]].
    rewrite (cocone_commute YC m k H0).
    rewrite (cocone_commute YC i k H1).
    simpl. apply embed_mono.
    rewrite (cocone_commute CC m k H0) in e.
    rewrite (cocone_commute CC i k H1) in e.
    simpl in e. 
    destruct e.
    apply (embed_reflects (cocone_spoke CC k)) in H2. auto.
  Qed.
  Next Obligation.
    unfold decompose_univ_func. intros.
    destruct (decompose a) as [i [q ?]]. simpl in *.
    destruct (decompose b) as [j [q' ?]]. simpl in *.
    destruct (choose_ub I i j) as [k [??]].
    rewrite (cocone_commute YC i k H1) in H.
    rewrite (cocone_commute YC j k H2) in H0.
    simpl in H, H0.
    destruct (embed_directed2 (cocone_spoke YC k) y
      (ds_hom DS i k H1 q)
      (ds_hom DS j k H2 q')) as [z [?[??]]]; auto.
    exists (cocone_spoke CC k z).
    split.
    destruct (decompose (cocone_spoke CC k z)) as [m [z' ?]]. simpl.
    rewrite <- H3.

    destruct (choose_ub I m k) as [k' [??]].
    rewrite (cocone_commute YC m k' H6).
    rewrite (cocone_commute YC k k' H7).
    simpl. apply embed_mono.
    destruct e1.
    rewrite (cocone_commute CC m k' H6) in H8.
    rewrite (cocone_commute CC k k' H7) in H8.
    simpl in H8.
    apply (embed_reflects (cocone_spoke CC k')) in H8. auto.

    split.
    rewrite <- e.
    rewrite (cocone_commute CC i k H1).
    simpl. apply embed_mono. auto.
    rewrite <- e0.
    rewrite (cocone_commute CC j k H2).
    simpl. apply embed_mono. auto.
  Qed.

  Program Definition decompose_is_colimit : directed_colimit DS CC :=
    DirectedColimit DS CC decompose_univ _ _.
  Next Obligation.
    intros. apply embed_lift'. simpl.
    unfold decompose_univ_func. simpl. intro x.
    destruct (decompose (cocone_spoke CC i x)) as [j [x' ?]]. simpl.
    destruct (choose_ub I i j) as [k [??]].
    rewrite (cocone_commute YC i k H).
    rewrite (cocone_commute YC j k H0).
    rewrite (cocone_commute CC i k H) in e.
    rewrite (cocone_commute CC j k H0) in e.
    simpl in e. 
    destruct e; split; simpl.
    apply embed_reflects in H2. apply embed_mono; auto.
    apply embed_reflects in H1. apply embed_mono; auto.
  Qed.    
  Next Obligation.
    simpl; intros.
    intros. apply embed_lift'. simpl.
    unfold decompose_univ_func. simpl. intro x.
    destruct (decompose x) as [i [x' ?]]. simpl.
    rewrite <- e.
    rewrite (H i).
    simpl; auto.
  Qed.
End colimit_decompose2.



(** With the bilimit in hand, we can construct the least
    fixpoint of continuous functors in the embeddings
    over the category of partial plotkin orders by appeal
    to the general fixpoint construction for continuous functors.
 *)
Section fixpoint.
  Variable F:functor (EMBED true) (EMBED true).
  Hypothesis Fcontinuous : continuous_functor F.

  Definition fixpoint : ob (EMBED true) :=
    cont_functors.fixpoint
           PPLT_EMBED_initialized
           F 
           (bilimit_cocone true).

  Definition fixpoint_initial : Alg.initial_alg (EMBED true) F :=
    cont_functors.fixpoint_initial
           PPLT_EMBED_initialized
           F 
           (bilimit_cocone true)
           (bilimit_construction true)
           Fcontinuous.

  Definition fixpoint_iso : F fixpoint ↔ fixpoint :=
    cont_functors.fixpoint_iso
           PPLT_EMBED_initialized
           F 
           (bilimit_cocone true)
           (bilimit_construction true)
           Fcontinuous.
End fixpoint.


Require Import Arith.

(** We can also build fixpoints in the category of total
    Plotkin orders by requiring a little more data.
    This proof is a modification of Theorem 79 from
    Carl Gunter's PhD thesis (CMU, 1985).
 *)
Section total_fixpoint.
  Variable F:functor (EMBED false) (EMBED false).
  Hypothesis Fcontinuous : continuous_functor F.

  Variable A:ob PLT.
  Variable h:A ⇀ F A.

  Fixpoint iterF (x:nat) : ob PLT :=
    match x with
    | O => A
    | S x' => F (iterF x')
    end.

  Fixpoint injectA (j:nat) : A ⇀ iterF j :=
    match j as j' return A ⇀ iterF j' with
    | 0 => id
    | S n => F·(injectA n) ∘ h
    end.

  Fixpoint iter_hom (i:nat) : forall (j:nat) (Hij:i <= j), iterF i ⇀ iterF j :=
    match i as i' return forall (j:nat) (Hij:i' <= j), iterF i' ⇀ iterF j with
    | O => fun j Hij => injectA j
    | S i' => fun j =>
        match j as j' return forall (Hij:S i' <= j'), iterF (S i') ⇀ iterF j' with
        | O => fun Hij => False_rect _ (HSle0 i' Hij) (* impossible case *)
        | S j' => fun Hij => F·(iter_hom i' j' (gt_S_le i' j' Hij))
        end
    end.

  Lemma iter_hom_proof_irr i : forall j H1 H2,
    iter_hom i j H1 ≈ iter_hom i j H2.
  Proof.
    induction i; simpl; intros; auto.
    destruct j.
    elimtype False. inversion H1.
    apply Functor.respects.
    apply IHi.
  Qed.

  Program Definition kleene_chain_alt : directed_system nat_dirord (EMBED false) :=
    DirSys nat_dirord _ iterF iter_hom _ _.
  Next Obligation.      
    induction i; simpl; intros.
    auto.
    apply Functor.ident; auto.
  Qed.
  Next Obligation.
    induction i. simpl; intros.
    clear Hij Hik.
    revert k Hjk; induction j; simpl; intros.
    apply cat_ident1.
    destruct k.
    elimtype False. inversion Hjk.
    simpl.
    rewrite (@cat_assoc (EMBED false)).
    apply cat_respects; auto.
    symmetry. apply Functor.compose.
    symmetry. apply IHj.
    
    intros. destruct j.
    elimtype False. inversion Hij.
    destruct k.
    elimtype False. inversion Hjk.
    simpl.
    rewrite <- (Functor.compose F _ _ _ (iter_hom j k (gt_S_le j k Hjk))).
    reflexivity. auto.
  Qed.

  Definition fixpoint_alt : ob PLT := bilimit false nat_dirord kleene_chain_alt.
  
  Let BL := Fcontinuous
               nat_dirord kleene_chain_alt
               (bilimit_cocone false nat_dirord kleene_chain_alt)
               (bilimit_construction false nat_dirord kleene_chain_alt).

  Program Definition cocone_plus1 : cocone (dir_sys_app kleene_chain_alt F)
    := Cocone (dir_sys_app kleene_chain_alt F) fixpoint_alt
      (fun i => cocone_spoke (bilimit_cocone false nat_dirord kleene_chain_alt) (S i)) _.
  Next Obligation.
    simpl; intros.
    assert (Hij' : S i <= S j). auto with arith.
    rewrite (cocone_commute (bilimit_cocone false nat_dirord kleene_chain_alt) (S i) (S j) Hij').
    simpl. apply cat_respects; auto.
    apply Functor.respects.
    apply iter_hom_proof_irr.
  Qed.

  Program Definition cocone_minus1 : cocone kleene_chain_alt
    := Cocone kleene_chain_alt (F fixpoint_alt) 
           (fun i => 
              F·(cocone_spoke (bilimit_cocone false nat_dirord kleene_chain_alt) i) ∘
              iter_hom i (S i) (le_S i i (le_refl i)))
           _.
  Next Obligation.
    simpl. intros.
    assert (i <= S j). auto with arith.
    rewrite <- (@cat_assoc (EMBED false)).
    rewrite (kleene_chain_alt_obligation_2 i j (S j) Hij (le_S j j (le_refl j)) H).
    destruct i. simpl.

    rewrite Functor.ident; auto.
    rewrite (@cat_ident2 (EMBED false)).
    rewrite (@cat_assoc (EMBED false)).
    apply cat_respects; auto.
    apply Functor.compose.
    rewrite (limset_spoke_commute false nat_dirord kleene_chain_alt 0%nat j Hij).
    simpl. auto.

    simpl.
    etransitivity.
    symmetry.
    apply (Functor.compose F) with 
      (f:=limset_spoke false nat_dirord kleene_chain_alt (S i))
      (g:=iter_hom i (S i) (gt_S_le i (S i) (le_S (S i) (S i) (le_refl (S i)))))
      (h:=limset_spoke false nat_dirord kleene_chain_alt j ∘
          iter_hom i j (gt_S_le i j H)).
    2: apply (Functor.compose F); auto.
    assert (i <= j) by auto with arith.
    rewrite <- (limset_spoke_commute false nat_dirord kleene_chain_alt i j).
    rewrite <- (limset_spoke_commute false nat_dirord kleene_chain_alt i (S i)).
    auto.
  Qed.

  Definition fixpoint_alt_in : F fixpoint_alt ⇀ fixpoint_alt :=
    colim_univ BL cocone_plus1.
  Definition fixpoint_alt_out : fixpoint_alt ⇀ F fixpoint_alt :=
    limord_univ false nat_dirord kleene_chain_alt cocone_minus1.

  Lemma fixpoint_alt_in_out : fixpoint_alt_in ∘ fixpoint_alt_out ≈ id.
  Proof.
    transitivity (limord_univ false nat_dirord kleene_chain_alt 
      (bilimit_cocone false nat_dirord kleene_chain_alt)).
    apply (limord_univ_uniq false nat_dirord kleene_chain_alt
      (bilimit_cocone false nat_dirord kleene_chain_alt)).
    simpl; intros. unfold fixpoint_alt_in. unfold fixpoint_alt_out.
    rewrite <- (@cat_assoc (EMBED false)).
    rewrite <- (limord_univ_commute false nat_dirord kleene_chain_alt cocone_minus1 i).
    simpl.
    generalize (colim_commute BL cocone_plus1 i). simpl. intros.
    transitivity (limset_spoke false nat_dirord kleene_chain_alt (S i)
      ∘ iter_hom i (S i) (le_S i i (le_refl i))).
    apply (limset_spoke_commute false nat_dirord kleene_chain_alt i (S i)).
    rewrite (@cat_assoc (EMBED false)).
    apply cat_respects; auto.
    symmetry.
    apply (limord_univ_uniq false nat_dirord kleene_chain_alt
      (bilimit_cocone false nat_dirord kleene_chain_alt)).
    simpl; intros.  
    symmetry. apply cat_ident2.
  Qed.

  Lemma fixpoint_alt_out_in : fixpoint_alt_out ∘ fixpoint_alt_in ≈ id.
  Proof.
    transitivity (colim_univ BL 
        (cocone_app (bilimit_cocone false nat_dirord kleene_chain_alt) F)).
    apply (colim_uniq BL (cocone_app (bilimit_cocone false nat_dirord kleene_chain_alt) F)).
    simpl. unfold fixpoint_alt_out. unfold fixpoint_alt_in.
    intros.
    generalize (colim_commute BL cocone_plus1 i).
    simpl. intros. 
    rewrite <- (@cat_assoc (EMBED false)).
    rewrite <- H.
    generalize (limord_univ_commute false nat_dirord kleene_chain_alt
      cocone_minus1 (S i)). simpl. intros.
    rewrite <- H0.
    apply Functor.compose.
    apply (limset_spoke_commute false nat_dirord kleene_chain_alt i (S i)).
    symmetry. 
    apply (colim_uniq BL (cocone_app (bilimit_cocone false nat_dirord kleene_chain_alt) F)).
    simpl; intros.
    symmetry. apply cat_ident2.
  Qed.

  Definition fixpoint_embed : A ⇀ fixpoint_alt :=
    limset_spoke false nat_dirord kleene_chain_alt 0%nat.
  
  Definition fixpoint_alt_iso : F fixpoint_alt ↔ fixpoint_alt :=
    Isomorphism (EMBED false) (F fixpoint_alt) fixpoint_alt
       fixpoint_alt_in
       fixpoint_alt_out
       fixpoint_alt_out_in
       fixpoint_alt_in_out.

End total_fixpoint.