-
Notifications
You must be signed in to change notification settings - Fork 2
/
finord.v
2246 lines (2091 loc) · 69.6 KB
/
finord.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
(* Copyright (c) 2014, Robert Dockins *)
Require Import Setoid.
Require Import NArith.
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.
(* FIXME move this *)
Lemma conj_dec (P Q:Prop) :
{P}+{~P} ->
{Q}+{~Q} ->
{ P /\ Q } + { ~(P /\ Q) }.
Proof.
intros. destruct H. destruct H0.
left; split; auto.
right; intros [??]; auto.
right; intros [??]; auto.
Qed.
Local Open Scope N_scope.
Lemma N2_eq_dec : forall (x y:N*N), { x = y }+{x <> y}.
Proof.
decide equality; apply N_eq_dec.
Qed.
Definition N2 := Ndisc_ord × Ndisc_ord.
Canonical Structure N2dec := OrdDec N2
(eff_ord_dec _ (effective_prod effective_Nord effective_Nord)).
Lemma N2in : forall (l:finset N2) x y, In (x,y) l <-> (x,y) ∈ l.
Proof.
intuition.
exists (x,y). split; auto.
destruct H as [q [??]].
replace (x,y) with q; auto.
destruct q.
destruct H0.
destruct H0.
hnf in H0. hnf in H2. simpl in *.
subst. auto.
Qed.
Notation N := Ndisc_ord.
Lemma finord_ord_dec1 (Y:finset N2) : forall (X:finset N2),
{ forall a b, (a,b) ∈ X -> (a,b) ∈ Y } +
{ exists a b, (a,b) ∈ X /\ ~(a,b) ∈ Y }.
Proof.
induction X; intros.
left; simpl; intuition.
apply nil_elem in H. elim H.
destruct IHX.
destruct a.
destruct (in_dec N2_eq_dec (c,c0) Y).
left; simpl; intuition.
apply cons_elem in H.
destruct H. rewrite H.
apply N2in. auto.
apply m; auto.
right; eauto.
exists c. exists c0. split; simpl; auto.
apply cons_elem; auto.
intro. apply n. apply N2in. auto.
right. destruct e as [p [q [??]]]; simpl; eauto.
exists p. exists q. split; auto.
apply cons_elem; auto.
Qed.
Lemma finord_ord_dec2 (Y:finset N2) : forall (X:finset N2),
{ forall a b, (a,b) ∈ X -> (a,a) ∈ Y -> (b,b) ∈ Y -> (a,b) ∈ Y } +
{ exists a b, (a,b) ∈ X /\ (a,a) ∈ Y /\ (b,b) ∈ Y /\ (a,b) ∉ Y }.
Proof.
induction X; intros.
left; simpl; intuition.
apply nil_elem in H. elim H.
destruct IHX.
destruct a.
destruct (in_dec N2_eq_dec (c,c0) Y).
left.
simpl; intuition.
apply cons_elem in H. destruct H.
rewrite H; auto. apply N2in; auto.
apply m; auto.
destruct (in_dec N2_eq_dec (c,c) Y).
destruct (in_dec N2_eq_dec (c0,c0) Y).
right.
exists c. exists c0.
split.
apply cons_elem; auto.
split. apply N2in; auto.
split. apply N2in; auto.
intro. apply n. apply N2in; auto.
left; intros.
apply cons_elem in H. destruct H.
elim n0.
apply N2in.
apply member_eq with (b,b); auto.
destruct H as [[??][??]]; split; split; simpl in *; auto.
apply m; auto.
left; intros.
apply cons_elem in H. destruct H.
elim n0.
apply N2in.
apply member_eq with (a,a); auto.
destruct H as [[??][??]]; split; split; simpl in *; auto.
apply m; auto.
right.
destruct e as [p [q [?[??]]]].
exists p. exists q; intuition.
apply cons_elem; auto.
Qed.
Lemma preord_graph_dec1 : forall (G H:list (N*N)),
{ forall x y, In (x,y) G -> In (x,x) H }
+
{ ~forall x y, In (x,y) G -> In (x,x) H }.
Proof.
induction G; simpl; intros.
left; intuition.
destruct (IHG H).
destruct a as [x y].
destruct (in_dec N2_eq_dec (x,x) H).
left; simpl; intuition.
inversion H1; subst; auto.
eapply i; eauto.
right; repeat intro.
apply n. eapply H0; eauto.
right; repeat intro.
apply n; intros.
eapply H0; eauto.
Qed.
Lemma preord_graph_dec2 : forall (G H:list (N*N)),
{ forall x y, In (x,y) G -> In (y,y) H }
+
{ ~forall x y, In (x,y) G -> In (y,y) H }.
Proof.
induction G; simpl; intros.
left; intuition.
destruct (IHG H).
destruct a as [x y].
destruct (in_dec N2_eq_dec (y,y) H).
left; simpl; intuition.
inversion H1; subst; auto.
eapply i; eauto.
right; repeat intro.
apply n. eapply H0; eauto.
right; repeat intro.
apply n; intros.
eapply H0; eauto.
Qed.
Lemma preord_graph_dec3 : forall (G H I:list (N*N)),
{ forall x y z, In (x,y) G -> In (y,z) H -> In (x,z) I }
+
{ ~forall x y z, In (x,y) G -> In (y,z) H -> In (x,z) I }.
Proof.
induction G.
left; simpl; intuition.
induction H.
left; simpl; intuition.
intro I.
destruct (IHG (a0::H) I).
clear IHG.
destruct (IHlist I).
destruct a as [x y].
destruct a0 as [y' z].
destruct (in_dec N2_eq_dec (x,z) I).
left.
simpl; intuition. inversion H2; inversion H0; subst; auto.
eapply i0; simpl; eauto.
eapply i; simpl; eauto.
eapply i; simpl; eauto.
destruct (N_eq_dec y y'). subst y'.
right; repeat intro.
apply n. eapply H0; simpl; eauto.
left. simpl; intuition.
inversion H2; inversion H0; subst.
elim n0; auto.
eapply i0; simpl; eauto.
eapply i; simpl; eauto.
eapply i0; simpl; eauto.
right; repeat intro.
apply n.
intros. eapply H0; simpl; eauto.
right; repeat intro.
apply n.
intros. eapply H0; simpl; eauto.
Qed.
Lemma preord_graph_dec4 : forall (hf:bool) (G:list (N*N)),
{ if hf then G <> nil else True }
+
{ ~if hf then G <> nil else True }.
Proof.
destruct hf; auto.
destruct G; simpl; auto.
left. discriminate.
Qed.
Definition preord_graph (hf:bool) (G:finset N2) :=
(forall x y, (x,y) ∈ G -> (x,x) ∈ G) /\
(forall x y, (x,y) ∈ G -> (y,y) ∈ G) /\
(forall x y z, (x,y) ∈ G -> (y,z) ∈ G -> (x,z) ∈ G) /\
(if hf then exists x y, (x,y) ∈ G else True).
Lemma preord_graph_dec : forall hf G, { preord_graph hf G } + { ~preord_graph hf G }.
Proof.
intros hf G.
destruct (preord_graph_dec1 G G).
destruct (preord_graph_dec2 G G).
destruct (preord_graph_dec3 G G G).
destruct (preord_graph_dec4 hf G).
left. red; auto.
split; intros.
apply N2in. eapply i. apply N2in. eauto.
split; intros.
apply N2in. eapply i0. apply N2in. eauto.
split; intros.
apply N2in. eapply i1; apply N2in; eauto.
destruct hf; auto.
destruct G. elim y; auto.
destruct c.
exists c. exists c0. apply cons_elem; auto.
right; intros [?[?[??]]]; apply n; auto.
destruct hf; auto. destruct G; auto.
destruct H2 as [p [q ?]]. apply nil_elem in H2. auto.
discriminate.
right; intros [?[?[??]]]; apply n; auto.
intros. apply N2in. eapply H1; apply N2in; eauto.
right; intros [?[?[??]]]; apply n; auto.
intros. apply N2in. eapply H0; apply N2in; eauto.
right; intros [?[?[??]]]; apply n; auto.
intros. apply N2in. eapply H; apply N2in; eauto.
Qed.
Lemma preord_graph_intersect (hf:bool) (X Y:finset N2) :
(if hf then exists a, a ∈ X /\ a ∈ Y else True) ->
preord_graph hf X -> preord_graph hf Y ->
preord_graph hf (fin_intersect N2 N2dec X Y).
Proof.
repeat intro.
destruct H0 as [?[?[??]]].
destruct H1 as [?[?[??]]].
split; intros.
apply fin_intersect_elem in H8.
apply fin_intersect_elem. destruct H8; split; auto.
eapply H0; eauto.
eapply H1; eauto.
split; intros.
apply fin_intersect_elem in H8.
apply fin_intersect_elem. destruct H8; split; auto.
eapply H2; eauto.
eapply H5; eauto.
split.
intros.
apply fin_intersect_elem in H8.
apply fin_intersect_elem in H9.
apply fin_intersect_elem.
destruct H8. destruct H9.
split; eauto.
destruct hf; auto.
destruct H as [a ?].
destruct a.
exists c. exists c0.
apply fin_intersect_elem.
destruct H. split; auto.
Qed.
Lemma preord_graph_ok hf : forall x y:finset N2, x ≈ y -> preord_graph hf x -> preord_graph hf y.
Proof.
unfold preord_graph.
intros. destruct H0 as [?[?[??]]].
repeat split; intros.
rewrite <- H. eapply H0. rewrite H. eauto.
rewrite <- H. eapply H1. rewrite H. eauto.
rewrite <- H. eapply H2; rewrite H; eauto.
destruct hf; auto.
destruct H3 as [p [q ?]]. exists p. exists q. rewrite <- H; auto.
Qed.
Definition finrel_image (R:finset N2) (x:N) : finset N :=
image π₂ (finsubset N2 (fun q => π₁#q = x) (fun q => N_eq_dec (π₁#q) x) R).
Definition finrel_inv_image (R:finset N2) (y:N) : finset N :=
image π₁ (finsubset N2 (fun q => π₂#q = y) (fun q => N_eq_dec (π₂#q) y) R).
Lemma finrel_image_elem : forall R x y,
y ∈ finrel_image R x <-> (x,y) ∈ R.
Proof.
unfold finrel_image. intros.
split; intros.
apply image_axiom2 in H. destruct H as [[p q] [??]].
apply finsubset_elem in H.
destruct H. simpl in *.
subst x.
apply member_eq with (p,q); auto.
split; split; auto.
simpl; intros.
subst x.
destruct H1 as [[??][??]]; auto.
apply image_axiom1'.
exists (x,y). split; simpl; auto.
apply finsubset_elem.
simpl; intros. subst x.
destruct H0 as [[??][??]]; auto.
split; auto.
Qed.
Lemma finrel_inv_image_elem : forall R x y,
x ∈ finrel_inv_image R y <-> (x,y) ∈ R.
Proof.
unfold finrel_inv_image. intros.
split; intros.
apply image_axiom2 in H. destruct H as [[p q] [??]].
apply finsubset_elem in H.
destruct H. simpl in *.
subst y.
apply member_eq with (p,q); auto.
split; split; auto.
simpl; intros.
subst y.
destruct H1 as [[??][??]]; auto.
apply image_axiom1'.
exists (x,y). split; simpl; auto.
apply finsubset_elem.
simpl; intros. subst y.
destruct H0 as [[??][??]]; auto.
split; auto.
Qed.
Definition finapprox_rel (hf:bool) (G1 G2 R:finset N2) :=
(forall x y, (x,y) ∈ R -> (x,x) ∈ G1 /\ (y,y) ∈ G2) /\
(forall x x' y y', (x,x') ∈ G1 -> (y',y) ∈ G2 -> (x,y) ∈ R -> (x',y') ∈ R) /\
(forall x y1 y2, (x,y1) ∈ R -> (x,y2) ∈ R ->
exists y3, (y1,y3) ∈ G2 /\ (y2,y3) ∈ G2 /\ (x,y3) ∈ R) /\
(if hf then True else forall x, (x,x) ∈ G1 -> exists y, (x,y) ∈ R).
Definition finrel_compose (G2 S R:finset N2) : finset N2 :=
image (mk_pair (π₁ ∘ π₁) (π₂ ∘ π₂))
(finsubset (N2×N2) (fun q => (fst (snd q),snd (fst q)) ∈ G2)
(fun q => finset_in_dec _ (OrdDec _ (eff_ord_dec _
(effective_prod effective_Nord effective_Nord)))
G2 (fst (snd q),snd (fst q)))
(finprod R S)).
Lemma finrel_compose_elem hf (G1 G2 G3 S R:finset N2) :
finapprox_rel hf G1 G2 R ->
finapprox_rel hf G2 G3 S ->
forall x z,
(x,z) ∈ finrel_compose G2 S R
<->
(exists y,
(x,y) ∈ R /\ (y,z) ∈ S).
Proof.
intros; split; intros.
unfold finrel_compose in H1.
apply image_axiom2 in H1.
destruct H1 as [[p q] [??]].
simpl in H2.
apply finsubset_elem in H1.
destruct H1 as [??]. simpl in H3.
apply finprod_elem in H1.
destruct H1.
exists (fst q).
split.
destruct H as [?[??]].
apply H5 with (fst p) (snd p).
replace x with (fst p).
destruct p.
apply H in H1. simpl. destruct H1; auto.
destruct H2 as [[??][??]]; auto.
auto.
destruct p; auto.
destruct H0 as [?[??]].
apply H5 with (fst q) (snd q).
destruct q. simpl in *.
apply H0 in H4.
destruct H4; auto.
replace z with (snd q).
destruct q. simpl in *.
apply H0 in H4.
destruct H4; auto.
destruct H2 as [?[??]]; auto.
destruct q; auto.
simpl; intros.
eapply member_eq. 2: apply H4.
destruct H3 as [[[??][??]][[??][??]]]; split; split; auto.
destruct H1 as [y [??]].
unfold finrel_compose.
apply image_axiom1'.
exists ((x,y),(y,z)).
split; auto.
apply finsubset_elem.
simpl; intros.
eapply member_eq. 2: apply H4.
destruct H3 as [[[??][??]][[??][??]]]; split; split; auto.
split; simpl.
apply finprod_elem. split; auto.
destruct H as [?[??]].
apply H in H1. destruct H1; auto.
Qed.
Lemma finapprox_compose hf (G1 G2 G3 S R:finset N2) :
finapprox_rel hf G1 G2 R ->
finapprox_rel hf G2 G3 S ->
finapprox_rel hf G1 G3 (finrel_compose G2 S R).
Proof.
intros.
generalize H; intro H'.
generalize H0; intro H0'.
destruct H as [HR1 [HR2 [HR3 HR4]]].
destruct H0 as [HS1 [HS2 [HS3 HS4]]].
split; intros.
rewrite finrel_compose_elem in H; eauto.
destruct H as [y' [??]].
apply HR1 in H.
apply HS1 in H0.
intuition.
split; intros.
rewrite finrel_compose_elem in H1; eauto.
rewrite finrel_compose_elem; eauto.
destruct H1 as [q [??]].
exists q; split; auto.
eapply HR2; eauto.
apply HR1 in H1. destruct H1; auto.
eapply HS2; eauto.
apply HR1 in H1. destruct H1; auto.
split; intros.
rewrite finrel_compose_elem in H; eauto.
rewrite finrel_compose_elem in H0; eauto.
destruct H as [q1 [??]].
destruct H0 as [q2 [??]].
destruct (HR3 x q1 q2) as [q3 [?[??]]]; auto.
destruct (HS3 q3 y1 y2) as [y3 [?[??]]]; auto.
apply HS2 with q1 y1; auto.
apply HS1 in H1. destruct H1; auto.
apply HS2 with q2 y2; auto.
apply HS1 in H2. destruct H2; auto.
exists y3. split; auto. split; auto.
rewrite finrel_compose_elem; eauto.
destruct hf; auto.
intros.
destruct (HR4 x) as [y ?]; auto.
destruct (HS4 y) as [z ?]; auto.
apply HR1 in H0. destruct H0; auto.
exists z.
rewrite finrel_compose_elem; eauto.
Qed.
Record fin_ep_pair hf (G1 G2 E P:finset N2) :=
FinEP
{ finep_approx1 : finapprox_rel hf G1 G2 E
; finep_approx2 : finapprox_rel hf G2 G1 P
; finep_id1 : forall x y, (x,y) ∈ finrel_compose G2 P E <-> (y,x) ∈ G1
; finep_id2 : forall x y, (x,y) ∈ finrel_compose G1 E P -> (y,x) ∈ G2
}.
Definition finord hf := { G:finset N2 | preord_graph hf G }.
Definition finord_ord hf (X Y:finord hf) : Prop :=
exists E P, fin_ep_pair hf (proj1_sig X) (proj1_sig Y) E P.
Definition finrel_inv (G:finset N2) : finset N2 :=
image (mk_pair π₂ π₁) G.
Lemma finrel_inv_elem G : forall x y, (x,y) ∈ finrel_inv G <-> (y,x) ∈ G.
Proof.
intros; split; intros.
unfold finrel_inv in H.
apply image_axiom2 in H. destruct H as [q [??]].
destruct q. simpl in H0.
apply member_eq with (c,c0); auto.
destruct H0 as [[??][??]]; split; split; auto.
unfold finrel_inv.
apply image_axiom1'.
exists (y,x). split; auto.
Qed.
Lemma finapprox_rel_dec hf G1 G2 R :
{ finapprox_rel hf G1 G2 R }+{ ~finapprox_rel hf G1 G2 R }.
Proof.
unfold finapprox_rel. apply conj_dec.
destruct (finset_find_dec' N2 (fun q => (fst q, fst q) ∈ G1 /\ (snd q, snd q) ∈ G2)) with R.
intros.
destruct H as [[??][??]].
destruct H0; split; auto.
apply member_eq with (fst x, fst x); auto.
split; split; auto.
apply member_eq with (snd x, snd x); auto.
split; split; auto.
intros. apply conj_dec; apply eff_in_dec; apply effective_prod; apply effective_Nord.
destruct s as [z [??]].
right; repeat intro.
apply H0. auto.
left; intros.
apply (a (x,y)); auto.
apply conj_dec.
destruct (finset_find_dec' N2 (fun q => forall y' y, (y',y) ∈ G2 -> (fst q, y) ∈ R ->
(snd q, y') ∈ R)) with G1.
intros. apply member_eq with (snd x, y'); auto.
destruct H as [[??][??]]; split; split; auto.
apply H0 with y0; auto.
apply member_eq with (fst y, y0); auto.
destruct H as [[??][??]]; split; split; auto.
intros [x x']. simpl.
destruct (finset_find_dec' N2 (fun q => (x, snd q) ∈ R -> (x', fst q) ∈ R)) with G2.
intros.
apply member_eq with (x',fst x0).
destruct H as [[??][??]]; split; split; auto.
apply H0.
apply member_eq with (x,snd y); auto.
destruct H as [[??][??]]; split; split; auto.
intros.
destruct (finset_in_dec N2 N2dec R (x, snd x0)).
destruct (finset_in_dec N2 N2dec R (x',fst x0)).
left; auto.
right. repeat intro.
apply n. apply H; auto.
left. intro. contradiction.
destruct s as [z [??]].
right; repeat intro.
apply H0; intros.
eapply H1; eauto.
destruct z; auto.
left; intros.
apply (m (y',y)); auto.
destruct s as [z [??]].
right; repeat intro. apply H0; intros.
eapply H1; eauto.
destruct z; auto.
left; intros.
eapply (m (x,x')); eauto.
apply conj_dec.
destruct (finset_find_dec' N2 (fun q => forall y2, (fst q, y2) ∈ R ->
exists y3, (snd q, y3) ∈ G2 /\ (y2,y3) ∈ G2 /\ (fst q, y3) ∈ R)) with R.
intros.
destruct (H0 y2) as [y3 [?[??]]].
apply member_eq with (fst y,y2); auto.
destruct H as [[??][??]]; split; split; auto.
exists y3. split; auto.
apply member_eq with (snd x,y3); auto.
destruct H as [[??][??]]; split; split; auto.
split; auto.
apply member_eq with (fst x,y3); auto.
destruct H as [[??][??]]; split; split; auto.
intros [x y1]. simpl.
destruct (finset_find_dec' N
(fun y2 => exists y3, (y1,y3) ∈ G2 /\ (y2,y3) ∈ G2 /\ (x,y3) ∈ R)) with (finrel_image R x).
intros.
destruct H0 as [y3 [?[??]]]. exists y3; split; auto. split; auto.
apply member_eq with (x0,y3); auto.
destruct H; split; split; auto.
intros y2.
destruct (finset_find_dec N
(fun y3 => (y2,y3) ∈ G2 /\ (x,y3) ∈ R)) with (finrel_image G2 y1).
intros.
destruct H0; split; auto.
apply member_eq with (y2,x0); auto.
destruct H; split; split; auto.
apply member_eq with (x,x0); auto.
destruct H; split; split; auto.
intro y3.
destruct (finset_in_dec N2 N2dec G2 (y2,y3)).
destruct (finset_in_dec N2 N2dec R (x,y3)).
left; auto.
right; intros [??]; auto.
right; intros [??]; auto.
destruct s as [y3 [?[??]]].
left. exists y3. split; auto.
apply finrel_image_elem in H. auto.
right; repeat intro.
destruct H as [y3 [?[??]]].
apply (n y3); auto.
apply finrel_image_elem; auto.
destruct s as [z [??]].
right. repeat intro.
apply H0; intros.
apply H1. apply finrel_image_elem in H. auto.
left. intros.
apply (e y2). apply finrel_image_elem; auto.
destruct s as [z [??]].
right; repeat intro.
apply H0. intros.
eapply H1; eauto.
left; intros.
apply (e (x,y1)); auto.
destruct hf; auto.
destruct (finset_find_dec' N2
(fun q => fst q ≈ snd q -> exists y, (fst q, y) ∈ R)) with G1.
intros. destruct H0 as [q ?].
destruct H as [[??][??]]; destruct H1; split; eauto.
exists q.
apply member_eq with (fst x, q); auto.
destruct H as [[??][??]].
destruct H1; split; split; simpl; auto.
intros [x x'].
destruct (N_eq_dec x x').
subst x'.
destruct (finset_find_dec N
(fun q => True)) with (finrel_image R x); auto.
destruct s as [z [??]].
left; simpl; intros.
exists z. apply finrel_image_elem in H. auto.
right; repeat intro.
destruct H; auto.
simpl in H.
apply (n x0); auto. apply finrel_image_elem; auto.
left; intros.
simpl in H. elim n. destruct H; auto.
destruct s as [z [??]].
right. repeat intro. apply H0.
intros. apply (H1 (fst z)).
apply member_eq with z; auto.
destruct H2. split; split; auto.
left. intros.
apply (e (x,x)); auto.
Qed.
Lemma fin_ep_pair_dec hf G1 G2 E P :
{ fin_ep_pair hf G1 G2 E P } + { ~fin_ep_pair hf G1 G2 E P }.
Proof.
destruct (finapprox_rel_dec hf G1 G2 E).
destruct (finapprox_rel_dec hf G2 G1 P).
destruct (finord_ord_dec1 (finrel_inv G1) (finrel_compose G2 P E)).
destruct (finord_ord_dec1 (finrel_compose G2 P E) (finrel_inv G1)).
destruct (finord_ord_dec1 (finrel_inv G2) (finrel_compose G1 E P)).
left.
constructor; auto.
intros. split; intros.
apply finrel_inv_elem.
apply m; auto.
apply m0.
apply finrel_inv_elem. auto.
intros.
apply finrel_inv_elem. apply m1. auto.
right; intros [????].
destruct e as [a [b [??]]].
apply H0.
apply finrel_inv_elem. auto.
right; intros [????].
destruct e as [a [b [??]]].
apply H0.
apply finep_id3; auto.
apply finrel_inv_elem. auto.
right; intros [????].
destruct e as [a [b [??]]].
apply H0. apply finrel_inv_elem.
apply finep_id3; auto.
right; intros [????]. auto.
right; intros [????]. auto.
Qed.
Lemma finapprox_rel_ok hf X Y a b :
a ≈ b -> finapprox_rel hf X Y a -> finapprox_rel hf X Y b.
Proof.
unfold finapprox_rel; intros.
destruct H0 as [?[?[??]]].
split; intros.
apply H0; auto. rewrite H; auto.
split; intros.
rewrite <- H. eapply H1; eauto.
rewrite H; auto.
split; intros.
destruct (H2 x y1 y2) as [y3 [?[??]]].
rewrite H; auto.
rewrite H; auto.
exists y3. intuition.
rewrite <- H; auto.
destruct hf; auto.
intros. destruct (H3 x); auto.
exists x0. rewrite <- H; auto.
Qed.
Lemma fin_ep_pair_ok : forall hf X Y E E' P P',
P ≈ P' -> E ≈ E' ->
fin_ep_pair hf X Y E P ->
fin_ep_pair hf X Y E' P'.
Proof.
intros.
destruct H1; constructor.
apply finapprox_rel_ok with E; auto.
apply finapprox_rel_ok with P; auto.
intros.
split; intros.
rewrite <- finep_id3.
rewrite finrel_compose_elem in H1; eauto.
rewrite finrel_compose_elem; eauto.
destruct H1 as [q [??]]. exists q; split; auto.
rewrite H0; auto.
rewrite H; auto.
apply finapprox_rel_ok with E; eauto.
apply finapprox_rel_ok with P; eauto.
rewrite <- finep_id3 in H1.
rewrite finrel_compose_elem in H1; eauto.
rewrite finrel_compose_elem; eauto.
destruct H1 as [q [??]]. exists q; split; auto.
rewrite <- H0; auto.
rewrite <- H; auto.
apply finapprox_rel_ok with E; eauto.
apply finapprox_rel_ok with P; eauto.
intros.
apply finep_id4.
rewrite finrel_compose_elem in H1; eauto.
rewrite finrel_compose_elem; eauto.
destruct H1 as [q [??]]. exists q; split; auto.
rewrite H; auto.
rewrite H0; auto.
apply finapprox_rel_ok with P; eauto.
apply finapprox_rel_ok with E; eauto.
Qed.
Lemma finord_ord_dec hf (X Y:finord hf) :
{ finord_ord hf X Y } + { ~finord_ord hf X Y }.
Proof.
unfold finord_ord.
destruct (finset_find_dec (finset N2)
(fun E => exists P, fin_ep_pair hf (proj1_sig X) (proj1_sig Y) E P))
with (list_finsubsets (finprod (image π₁ (proj1_sig X)) (image π₂ (proj1_sig Y)))).
intros. destruct H0 as [P ?]. exists P.
apply fin_ep_pair_ok with x P; auto.
intro E.
destruct (finset_find_dec (finset N2)
(fun P => fin_ep_pair hf (proj1_sig X) (proj1_sig Y) E P))
with (list_finsubsets (finprod (image π₂ (proj1_sig Y)) (image π₁ (proj1_sig X)))).
intros.
apply fin_ep_pair_ok with E x; auto.
intro P.
apply fin_ep_pair_dec.
destruct s as [P [??]].
left. exists P. auto.
right; intro.
destruct H as [P ?].
apply (n P); auto.
apply list_finsubsets_complete; auto.
apply N2dec.
red; intros.
destruct H.
destruct finep_approx4 as [?[?[??]]].
destruct a.
apply H in H0.
apply finprod_elem.
destruct H0.
split; apply image_axiom1'.
exists (c,c). split; auto.
exists (c0,c0). split; auto.
destruct s as [E [??]].
left. exists E. auto.
right; intro.
destruct H as [E [P ?]].
apply (n E); eauto.
apply list_finsubsets_complete; auto.
apply N2dec.
red; intros.
destruct H.
destruct finep_approx3 as [?[?[??]]].
destruct a.
apply H in H0.
apply finprod_elem.
destruct H0.
split; apply image_axiom1'.
exists (c,c). split; auto.
exists (c0,c0). split; auto.
Qed.
Lemma fin_ep_pair_compose hf (G1 G2 G3 E E' P P':finset N2) :
preord_graph hf G1 ->
preord_graph hf G2 ->
preord_graph hf G3 ->
fin_ep_pair hf G1 G2 E P ->
fin_ep_pair hf G2 G3 E' P' ->
fin_ep_pair hf G1 G3 (finrel_compose G2 E' E) (finrel_compose G2 P P').
Proof.
intros HG1 HG2 HG3 HEP1 HEP2.
split.
apply finapprox_compose; auto.
destruct HEP1; auto.
destruct HEP2; auto.
apply finapprox_compose; auto.
destruct HEP2; auto.
destruct HEP1; auto.
intros. split; intros.
destruct HEP1; destruct HEP2.
rewrite finrel_compose_elem in H; eauto.
2: eapply finapprox_compose; eauto.
2: eapply finapprox_compose; eauto.
destruct H as [q [??]].
rewrite finrel_compose_elem in H; eauto.
destruct H as [m [??]].
rewrite finrel_compose_elem in H0; eauto.
destruct H0 as [n [??]].
assert ((n,m) ∈ G2).
apply finep_id5.
rewrite finrel_compose_elem; eauto.
apply finep_id3.
rewrite finrel_compose_elem; eauto.
exists m. split; auto.
destruct finep_approx4 as [?[?[??]]].
apply H5 with n y; auto.
apply H4 in H2. destruct H2; auto.
destruct HEP1; destruct HEP2.
rewrite finrel_compose_elem; eauto.
2: eapply finapprox_compose; eauto.
2: eapply finapprox_compose; eauto.
apply finep_id3 in H.
rewrite finrel_compose_elem in H; eauto.
destruct H as [q [??]].
assert ((q,q) ∈ G2).
destruct finep_approx3 as [?[?[??]]].
apply H1 in H. destruct H. auto.
apply finep_id5 in H1.
rewrite finrel_compose_elem in H1; eauto.
destruct H1 as [p [??]].
exists p. split.
rewrite finrel_compose_elem; eauto.
rewrite finrel_compose_elem; eauto.
intros.
destruct HEP1; destruct HEP2.
rewrite finrel_compose_elem in H; eauto.
2: eapply finapprox_compose; eauto.
2: eapply finapprox_compose; eauto.
destruct H as [q [??]].
apply finep_id6.
rewrite finrel_compose_elem in H; eauto.
rewrite finrel_compose_elem in H0; eauto.
destruct H as [m [??]].
destruct H0 as [n [??]].
rewrite finrel_compose_elem; eauto.
exists n. split; auto.
destruct finep_approx6 as [?[?[??]]].
apply H4 with x m.
apply H3 in H. destruct H; auto.
apply finep_id4.
rewrite finrel_compose_elem; eauto.
auto.
Qed.
Lemma fin_ep_pair_refl hf (G:finset N2) :
preord_graph hf G ->
fin_ep_pair hf G G (finrel_inv G) (finrel_inv G).
Proof.
intros [?[?[??]]].
assert (finapprox_rel hf G G (finrel_inv G)).
repeat split; intros; eauto.
apply -> finrel_inv_elem in H3.
eapply H0; eauto.
apply -> finrel_inv_elem in H3.
eapply H; eauto.
apply -> finrel_inv_elem in H5.
apply <- finrel_inv_elem.
apply H1 with y; auto.
apply H1 with x; auto.
apply -> finrel_inv_elem in H3.
apply -> finrel_inv_elem in H4.
exists x. split; auto. split; auto.
apply <- finrel_inv_elem.
eapply H0; eauto.
destruct hf; auto.
intros.
exists x.
apply <- finrel_inv_elem. auto.
assert (forall x y : N,
(x, y) ∈ finrel_compose G (finrel_inv G) (finrel_inv G) <-> (y, x) ∈ G).
intros; split; intros.
rewrite finrel_compose_elem in H4; eauto.
destruct H4 as [q [??]].
apply -> finrel_inv_elem in H4.
apply -> finrel_inv_elem in H5.
apply H1 with q; auto.
rewrite finrel_compose_elem; eauto.
exists y. split.
apply <- finrel_inv_elem. auto.
apply <- finrel_inv_elem.
eapply H; eauto.
apply FinEP; auto.
intros. apply H4; auto.
Qed.
Program Definition finord_preord_mixin hf : Preord.mixin_of (finord hf) :=
Preord.Mixin (finord hf) (finord_ord hf) _ _.
Next Obligation.
intros.
exists (finrel_inv (proj1_sig x)).
exists (finrel_inv (proj1_sig x)).
apply fin_ep_pair_refl.
apply proj2_sig.
Qed.
Next Obligation.
intros.
destruct H as [E [P ?]].
destruct H0 as [E' [P' ?]].
exists (finrel_compose (proj1_sig y) E' E).
exists (finrel_compose (proj1_sig y) P P').
apply fin_ep_pair_compose; auto.
apply proj2_sig.
apply proj2_sig.
apply proj2_sig.
Qed.
Canonical Structure finord_preord hf := Preord.Pack (finord hf) (finord_preord_mixin hf).
Definition finord_enum hf : eset (finord_preord hf) :=
fun n =>
let X := finsubsets _ (eff_enum _ (effective_prod effective_Nord effective_Nord)) in
match X n with
| None => None
| Some G => match preord_graph_dec hf G with
| left H => Some (exist _ G H)
| right _ => None
end
end.
Lemma finapprox_rel_ok' hf X Y X' Y' a :
X ≈ X' -> Y ≈ Y' -> finapprox_rel hf X Y a -> finapprox_rel hf X' Y' a.
Proof.
intros.
destruct H1 as [?[?[??]]].
repeat split; intros.
apply H1 in H5. destruct H5.
rewrite <- H. auto.
apply H1 in H5. destruct H5.
rewrite <- H0. auto.
apply H2 with x y; auto.
rewrite H; auto. rewrite H0; auto.
destruct (H3 x y1 y2) as [y3 [?[??]]]; auto.
exists y3.
split. rewrite <- H0; auto.
split. rewrite <- H0; auto.
auto.
destruct hf; auto.
intros. destruct (H4 x).
rewrite H; auto.
exists x0. auto.
Qed.
Lemma fin_ep_pair_ok' : forall hf X X' Y Y' E P,
X ≈ X' -> Y ≈ Y' ->
fin_ep_pair hf X Y E P ->
fin_ep_pair hf X' Y' E P.
Proof.
repeat intro.
destruct H1; split.
eapply finapprox_rel_ok'; eauto.
eapply finapprox_rel_ok'; eauto.
intros.
rewrite <- H.
rewrite <- finep_id3.
split; intros.
rewrite finrel_compose_elem in H1; eauto.
rewrite finrel_compose_elem; eauto.
eapply finapprox_rel_ok'. 3: apply finep_approx3.
eauto. eauto.
eapply finapprox_rel_ok'. 3: apply finep_approx4.
auto. eauto.
rewrite finrel_compose_elem in H1; eauto.
rewrite finrel_compose_elem; eauto.
eapply finapprox_rel_ok'. 3: apply finep_approx3.
eauto. eauto.
eapply finapprox_rel_ok'. 3: apply finep_approx4.
auto. eauto.
intros.
rewrite finrel_compose_elem in H1; eauto.
destruct H1 as [q [??]].
rewrite <- H0. apply finep_id4.
rewrite finrel_compose_elem; eauto.
eapply finapprox_rel_ok'. 3: apply finep_approx4.
eauto. eauto.
eapply finapprox_rel_ok'. 3: apply finep_approx3.
auto. eauto.
Qed.
Program Definition finord_preord_eff hf : effective_order (finord_preord hf) :=