-
Notifications
You must be signed in to change notification settings - Fork 2
/
finprod.v
2536 lines (2297 loc) · 76.9 KB
/
finprod.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 List.
Require Import NArith.
Require Import basics.
Require Import preord.
Require Import categories.
Require Import sets.
Require Import finsets.
Require Import esets.
Require Import effective.
Require Import directed.
Require Import plotkin.
Require Import joinable.
Require Import approx_rels.
Require Import cpo.
Require Import profinite.
Require Import atoms.
Require Import permutations.
(** * Finite products for type contexts
*)
(** First, a short development of cast morphisms. These are
useful for dealing with cases where we have types that
are provably equal, but not convertable.
*)
Section cast.
Variable hf:bool.
Variable A:Type.
Variable F:A -> PLT.PLT hf.
Definition cast_rel (x y:A) (H:x = y) : erel (F x) (F y) :=
esubset_dec
(PLT.prod (F x) (F y))
(fun xy => eq_rect x F (fst xy) y H ≥ snd xy)
(fun xy => eff_ord_dec _ (PLT.effective (F y)) _ _)
(eprod (eff_enum _ (PLT.effective (F x))) (eff_enum _ (PLT.effective (F y)))).
Lemma cast_rel_elem (x y:A) (H:x = y) a b :
(a,b) ∈ cast_rel x y H <-> eq_rect x F a y H ≥ b.
Proof.
unfold cast_rel. rewrite esubset_dec_elem.
- simpl. intuition.
apply eprod_elem. split; apply eff_complete.
- intros. destruct H0 as [[??][??]].
rewrite H4. rewrite H1.
generalize H0.
generalize (fst x0). generalize (fst y0).
case H. simpl. auto.
Qed.
Program Definition cast (x y:A) (H:x = y) : F x → F y :=
PLT.Hom hf (F x) (F y) (cast_rel x y H) _ _.
Next Obligation.
intros.
apply cast_rel_elem in H2. apply cast_rel_elem.
rewrite H1. rewrite H2.
case H. simpl. auto.
Qed.
Next Obligation.
repeat intro.
exists (eq_rect x F x0 y H). split.
- red; simpl; intros.
apply H0 in H1.
apply erel_image_elem in H1.
apply cast_rel_elem in H1. auto.
- apply erel_image_elem.
apply cast_rel_elem.
auto.
Qed.
Lemma cast_refl x :
cast x x (Logic.eq_refl x) ≈ id (F x).
Proof.
split; hnf; simpl; intros.
- destruct a.
apply cast_rel_elem in H. simpl in H.
apply ident_elem. auto.
- destruct a.
apply ident_elem in H.
apply cast_rel_elem. simpl. auto.
Qed.
Lemma cast_compose x y z H1 H2 :
cast y z H2 ∘ cast x y H1 ≈ cast x z (Logic.eq_trans H1 H2).
Proof.
split; hnf; simpl; intros.
- destruct a. apply PLT.compose_hom_rel in H.
destruct H as [q [??]].
simpl in *.
apply cast_rel_elem in H.
apply cast_rel_elem in H0.
apply cast_rel_elem.
rewrite H0. revert H.
case H2. simpl. auto.
- apply PLT.compose_hom_rel.
destruct a.
apply cast_rel_elem in H.
exists (eq_rect x F c y H1).
split.
+ apply cast_rel_elem. auto.
+ apply cast_rel_elem.
rewrite H.
case H2. simpl. auto.
Qed.
Lemma cast_iso1 x y H :
cast y x (eq_sym H) ∘ cast x y H ≈ id.
Proof.
rewrite cast_compose.
case H. simpl.
apply cast_refl.
Qed.
Lemma cast_iso2 x y H :
cast x y H ∘ cast y x (eq_sym H) ≈ id.
Proof.
rewrite cast_compose.
case H. simpl.
apply cast_refl.
Qed.
Hypothesis Adec : forall (x y:A), {x=y}+{x<>y}.
Lemma cast_dec_id : forall x (H:x=x), cast x x H ≈ id.
Proof.
intros.
replace H with (Logic.eq_refl x).
- apply cast_refl.
- apply (Eqdep_dec.UIP_dec Adec).
Qed.
End cast.
Arguments cast [hf] [A] F [x y] H.
Definition maybe A B (b:B) (f:A->B) (x:option A) : B :=
match x with
| None => b
| Some x => f x
end.
Arguments maybe [A B] b f x.
(** The input module type for contexts. Type [I] is the
index type, but type [A] are proxies for object language
types. The functon [F] interprets the types [A] as objects
of PLT.
*)
Module Type FINPROD_INPUT.
Parameter Inline A:Type.
Parameter Inline Adec : forall x y:A, {x=y}+{x<>y}.
Parameter Inline F: A -> PLT.
End FINPROD_INPUT.
(** This module type provides an object of contexts, which is
the universal object for finite collections of objects.
These are designed specifically to handle contexts of
typed λ-calculi.
*)
Module Type FINPROD.
Parameter Inline A:Type.
Parameter Inline Adec : forall x y:A, {x=y}+{x<>y}.
Parameter Inline F: A -> PLT.
Fixpoint lookup (i:atom) (l:list (atom*A)) : option A :=
match l with
| nil => None
| (i',a)::l' =>
match string_dec i' i with
| left Hi => Some a
| right _ => lookup i l'
end
end.
Lemma lookup_eq : forall i i' a ls,
i = i' ->
Some a = if string_dec i i' then Some a else lookup i' ls.
Proof.
intros.
destruct (string_dec i i'). reflexivity.
elim n. auto.
Defined.
Lemma lookup_neq : forall i i' a ls,
i <> i' ->
lookup i' ls = if string_dec i i' then Some a else lookup i' ls.
Proof.
intros.
destruct (string_dec i i'). elim H; auto.
auto.
Defined.
Definition ty (a:option A) : PLT := maybe 1 F a.
Parameter finprod : list (atom*A) -> PLT.
Parameter proj : forall ls i, finprod ls → ty (lookup i ls).
Parameter mk_finprod : forall ls (X:PLT),
(forall i, X → ty (lookup i ls)) -> X → finprod ls.
Definition bind ls i a : finprod ls × F a → finprod ((i,a)::ls) :=
mk_finprod ((i,a)::ls) (finprod ls × F a)
(fun i' =>
match string_dec i i' as Hi return
(finprod ls × F a) → ty (if Hi then Some a else lookup i' ls)
with
| left _ => π₂
| right _ => proj ls i' ∘ π₁
end).
Lemma unbind_lemma ls i i' : lookup i ls = None -> i = i' -> None = lookup i' ls.
Proof.
intuition; subst; auto.
Defined.
Definition unbind ls i a (Hi:lookup i ls = None) :
finprod ((i,a)::ls) → finprod ls :=
mk_finprod ls (finprod ((i,a)::ls))
(fun i' =>
match string_dec i i' as Hi return
ty (if Hi then Some a else lookup i' ls) → ty (lookup i' ls)
with
| left H => cast ty (unbind_lemma ls i i' Hi H) ∘ PLT.terminate _ _
| right _ => id
end ∘ proj ((i,a)::ls) i').
Axiom finprod_proj_commute : forall ls i X f,
proj ls i ∘ mk_finprod ls X f ≈ f i.
Axiom finprod_universal : forall ls X f (z:X → finprod ls),
(forall i, proj ls i ∘ z ≈ f i) -> z ≈ mk_finprod ls X f.
Axiom bind_unbind : forall ls i a Hi,
unbind ls i a Hi ∘ bind ls i a ≈ π₁.
Axiom proj_bind_neq : forall i a i' ls (Hneq:i <> i'),
proj ((i,a)::ls) i' ∘ bind ls i a
≈ cast ty (lookup_neq i i' a ls Hneq) ∘ proj ls i' ∘ π₁.
Axiom proj_bind_eq : forall i a i' ls (Heq:i = i'),
proj ((i,a)::ls) i' ∘ bind ls i a
≈ cast ty (lookup_eq i i' a ls Heq) ∘ π₂.
Axiom proj_bind : forall i a i' ls,
proj ((i,a)::ls) i' ∘ bind ls i a ≈
match string_dec i i' as H return
finprod ls × F a → ty (if H then Some a else lookup i' ls)
with
| left Heq => π₂
| right Hneq => proj ls i' ∘ π₁
end.
Axiom mk_finprod_compose_commute : forall ls X Y f (h:X → Y),
mk_finprod ls Y f ∘ h ≈
mk_finprod ls X (fun i => f i ∘ h).
End FINPROD.
Module finprod (FI:FINPROD_INPUT) <: FINPROD.
Include FI.
Fixpoint lookup (i:atom) (l:list (atom*A)) : option A :=
match l with
| nil => None
| (i',a)::l' =>
match string_dec i' i with
| left Hi => Some a
| right _ => lookup i l'
end
end.
Lemma lookup_app i l1 l2 :
lookup i (l1++l2) =
match lookup i l1 with
| None => lookup i l2
| Some a => Some a
end.
Proof.
induction l1; simpl; auto.
destruct a as [i' a].
destruct (string_dec i' i); auto.
Qed.
Lemma lookup_eq : forall i i' a ls,
i = i' ->
Some a = if string_dec i i' then Some a else lookup i' ls.
Proof.
intros.
destruct (string_dec i i'). reflexivity.
elim n. auto.
Defined.
Lemma lookup_neq : forall i i' a ls,
i <> i' ->
lookup i' ls = if string_dec i i' then Some a else lookup i' ls.
Proof.
intros.
destruct (string_dec i i'). elim H; auto.
auto.
Defined.
Definition ty (a:option A) : PLT := maybe 1 F a.
Module internals.
Inductive finprod_codom (avd:list atom) z i :=
| codom_avoid : In i avd -> finprod_codom avd z i
| codom_elem : ~In i avd -> ty z -> finprod_codom avd z i.
Arguments codom_avoid [avd z i] H.
Arguments codom_elem [avd z i] H x.
Definition finprod_elem (avd:list atom) ls :=
forall i, finprod_codom avd (lookup i ls) i.
Definition finprod_codom_ord avd z i (x y:finprod_codom avd z i) :=
match x, y with
| codom_avoid _, codom_avoid _ => True
| codom_elem _ a, codom_elem _ b => a ≤ b
| _, _ => False
end.
Program Definition finprod_codom_ord_mixin avd z i :
Preord.mixin_of (finprod_codom avd z i) :=
Preord.Mixin (finprod_codom avd z i) (finprod_codom_ord avd z i) _ _.
Next Obligation.
intros. red. destruct x; auto.
Qed.
Next Obligation.
intros. unfold finprod_codom_ord in *.
destruct x; destruct y; intuition.
destruct z0; auto.
transitivity c0; auto.
Qed.
Canonical Structure codom avd z i :=
Preord.Pack (finprod_codom avd z i) (finprod_codom_ord_mixin avd z i).
Definition codom_enum avd z i (n:N) : option (finprod_codom avd z i) :=
match In_dec string_dec i avd with
| left Hin => Some (codom_avoid Hin)
| right Hnin =>
match eff_enum _ (PLT.effective (ty z)) n with
| None => None
| Some x => Some (codom_elem Hnin x)
end
end.
Program Definition codom_eff avd z i : effective_order (codom avd z i)
:= EffectiveOrder (codom avd z i) _ (codom_enum avd z i) _.
Next Obligation.
intros. destruct x; destruct y.
- left; hnf; auto.
- right; intro H; elim H.
- right; intro H; elim H.
- destruct (eff_ord_dec _ (PLT.effective (ty z)) c c0).
left; auto. right; auto.
Qed.
Next Obligation.
intros. unfold codom_enum. destruct x.
- exists 0%N.
destruct (in_dec string_dec i avd). split; hnf; auto.
contradiction.
- destruct (in_dec string_dec i avd). contradiction.
destruct (eff_complete _ (PLT.effective (ty z)) c). exists x.
match goal with [|- match (match ?X with _ => _ end) with _ => _ end ] => destruct X end.
destruct H; split; auto.
auto.
Qed.
Definition codom_out avd z i (Hnin:~In i avd)
(x:codom avd z i) : ty z :=
match x with
| codom_avoid H => False_rect _ (Hnin H)
| codom_elem H x => x
end.
Program Definition codom_out' avd z i (Hnin:~In i avd) :
Preord.hom (codom avd z i) (ty z)
:=
Preord.Hom _ _ (codom_out avd z i Hnin) _.
Next Obligation.
repeat intro. destruct a. contradiction.
destruct b. contradiction.
simpl. auto.
Qed.
Program Definition codom_in' avd z i (Hnin:~In i avd) :
Preord.hom (ty z) (codom avd z i)
:= Preord.Hom _ _ (@codom_elem avd z i Hnin) _.
Next Obligation.
intros; auto.
Qed.
Lemma codom_has_normals avd z i : has_normals (codom avd z i) (codom_eff avd z i) false.
Proof.
repeat intro.
destruct (In_dec string_dec i avd).
- exists (@codom_avoid avd z i i0 :: nil).
split.
+ red; intros.
apply cons_elem. left.
destruct a.
* split; hnf; auto.
* contradiction.
+ split. red; auto.
repeat intro.
exists (@codom_avoid avd z i i0).
split.
* repeat intro.
destruct x. hnf; auto. contradiction.
* rewrite finsubset_elem.
split; auto.
** apply cons_elem; auto.
** destruct z0. hnf; auto. contradiction.
** intros. rewrite <- H0. auto.
- set (Z' := mub_closure (PLT.plotkin (ty z)) (image (codom_out' avd z i n) X)).
exists (image (codom_in' avd z i n) Z').
split.
+ red; intros.
apply image_axiom1'.
exists (codom_out' avd z i n a). split.
* simpl. unfold codom_out.
destruct a; auto. contradiction.
split; red; simpl; auto.
* unfold Z'.
apply mub_clos_incl.
apply image_axiom1'.
exists a. split; auto.
+ split. red; auto.
repeat intro.
destruct (mub_complete (PLT.plotkin (ty z)) (image (codom_out' avd z i n) M)
(codom_out' avd z i n z0)).
{ red; auto. }
{ repeat intro.
apply image_axiom2 in H0. destruct H0 as [q [??]].
rewrite H1. apply Preord.axiom.
apply H in H0.
rewrite finsubset_elem in H0. destruct H0; auto.
intros. rewrite <- H2; auto.
}
destruct H0.
exists (codom_in' avd z i n x).
split.
* repeat intro.
simpl.
destruct x0. contradiction.
apply H0.
apply image_axiom1'.
exists (codom_elem n0 c). split; auto.
* rewrite finsubset_elem. split.
** apply image_axiom1. unfold Z'.
apply mub_clos_mub with (image (codom_out' avd z i n) M); auto.
red; intros.
apply image_axiom2 in H2. destruct H2 as [q [??]].
apply H in H2.
rewrite finsubset_elem in H2.
*** destruct H2.
apply image_axiom2 in H2. destruct H2 as [q' [??]].
apply member_eq with q'; auto.
rewrite H3.
rewrite H5.
simpl. auto.
*** intros. rewrite <- H4; auto.
** simpl. simpl in H1.
destruct z0. contradiction.
auto.
** intros. rewrite <- H2. auto.
Qed.
Definition codom_plotkin avd z i : plotkin_order false (codom avd z i)
:= norm_plt (codom avd z i) (codom_eff avd z i) false (codom_has_normals avd z i).
Definition finprod_ord avd l (x y:finprod_elem avd l) :=
forall i, x i ≤ y i.
Program Definition finprod_ord_mixin avd l : Preord.mixin_of (finprod_elem avd l) :=
Preord.Mixin (finprod_elem avd l) (finprod_ord avd l) _ _.
Next Obligation.
intros. red. intro; auto.
Qed.
Next Obligation.
intros. red. intro; auto.
generalize (H i) (H0 i). eauto.
Qed.
Canonical Structure ord avd l :=
Preord.Pack (finprod_elem avd l) (finprod_ord_mixin avd l).
Definition finprod_dec l avd (x y:finprod_elem avd l) : {x≤y}+{x≰y}.
Proof.
unfold finprod_elem in *.
cut (forall l1 l2,
(forall i a, lookup i l1 = Some a -> x i ≤ y i) ->
(l1++l2)%list = l ->
{ forall i a, lookup i l2 = Some a -> x i ≤ y i} +
{ exists i , x i ≰ y i}).
{ intros.
destruct (X nil l); clear X; auto.
- simpl; intuition.
discriminate.
- left. intro.
generalize (o i). clear o.
destruct (x i); destruct (y i); intuition.
+ hnf. auto.
+ elim n; auto.
+ elim n; auto.
+ red; simpl.
unfold Preord.ord_op in H. simpl in H.
revert c c0 H.
simpl.
destruct (lookup i l).
* intros. eapply H; eauto.
* simpl; intros. hnf. auto.
- right. intro. hnf in H.
destruct e. apply H0. apply H.
}
intros l1 l2. revert l1. induction l2; simpl; intros.
{ left. intros. discriminate. }
subst l.
destruct a as [i a].
case_eq (x i); case_eq (y i); intros.
- destruct (IHl2 (l1++(i,a)::nil)%list); auto; clear IHl2.
+ intros.
rewrite lookup_app in H2.
generalize (H i2 a0).
destruct (lookup i2 l1); auto.
intros. simpl in H2.
destruct (string_dec i i2).
* subst i2; auto.
hnf. simpl. rewrite H1. rewrite H0. auto.
* discriminate.
+ rewrite app_ass; auto.
+ left; intros.
destruct (string_dec i i2).
* subst i2.
hnf. rewrite H1. rewrite H0. auto.
* apply o with a0; auto.
- contradiction.
- contradiction.
- destruct (eff_ord_dec _ (PLT.effective
(ty (lookup i (l1 ++ (i, a) :: l2))%list))
c0 c).
+ destruct (IHl2 (l1++(i,a)::nil)%list); auto; clear IHl2.
* intros.
rewrite lookup_app in H2.
generalize (H i0 a0).
destruct (lookup i0 l1); auto.
intros. simpl in H2.
destruct (string_dec i i0).
** subst i0; auto.
hnf. rewrite H1. rewrite H0. auto.
** discriminate.
* rewrite app_ass. auto.
* left. intros.
destruct (string_dec i i0).
** subst i0.
hnf. rewrite H1. rewrite H0. auto.
** apply o0 with a0; auto.
+ right. exists i.
hnf; intro.
hnf in H2.
rewrite H1 in H2. rewrite H0 in H2.
contradiction.
Qed.
Definition f_hd i a ls avd
(f:finprod_elem avd ((i,a)::ls)) : finprod_codom avd (Some a) i :=
match string_dec i i as Hi
return finprod_codom avd (if Hi then (Some a) else lookup i ls) i ->
finprod_codom avd (Some a) i
with
| left Hi => fun x => x
| right Hn => False_rect _ (Hn (Logic.eq_refl i))
end (f i).
Definition f_tl i a (ls:list (atom*A)) (avd:list atom)
(f:finprod_elem avd ((i,a)::ls)) : finprod_elem (i::avd) ls :=
fun i' =>
match f i' with
| codom_avoid H => @codom_avoid (i::avd) _ i' (or_intror H)
| codom_elem H x =>
match string_dec i i' as Hi return
ty (if Hi then Some a else lookup i' ls) ->
finprod_codom (i::avd) (lookup i' ls) i'
with
| left Hi => fun _ => @codom_avoid (i::avd) _ i' (or_introl Hi)
| right Hn => fun x' => @codom_elem (i::avd) _ i' (or_ind Hn H) x'
end x
end.
Definition f_cons i a (ls:list (atom*A)) (avd:list atom)
(h:finprod_codom avd (Some a) i)
(f:finprod_elem (i::avd) ls) : finprod_elem avd ((i,a)::ls) :=
fun i' =>
match in_dec string_dec i' avd with
| left Hin => codom_avoid Hin
| right Hnin => @codom_elem avd _ i' Hnin
match string_dec i i' as Hi
return ty (if Hi then Some a else lookup i' ls)
with
| left Hi =>
match h with
| codom_avoid H => False_rect _ (Hnin (eq_rect i (fun i => In i avd) H i' Hi))
| codom_elem H x => x
end
| right Hn =>
match f i' with
| codom_avoid H => False_rect _ (or_ind Hn Hnin H)
| codom_elem H x => x
end
end
end.
Lemma f_cons_mono i a ls avd
hd hd' (tl tl':ord (i::avd) ls) :
hd ≤ hd' ->
tl ≤ tl' ->
f_cons i a ls avd hd tl ≤ f_cons i a ls avd hd' tl'.
Proof.
repeat intro.
generalize (H0 i0). clear H0.
intro. hnf; simpl. hnf in H0.
unfold f_cons.
destruct (in_dec string_dec i0 avd). { auto. }
simpl.
destruct (tl i0).
- destruct (tl' i0).
+ simpl. destruct (string_dec i i0).
* subst i0.
destruct hd.
** elim n; auto.
** destruct hd'.
*** elim n; auto.
*** auto.
* elim (or_ind n0 n i1).
+ elim H0.
- destruct (tl' i0).
+ elim H0.
+ destruct (string_dec i i0); auto.
subst i0. elim n1; simpl; auto.
Qed.
Lemma f_cons_reflects1 i a ls avd
hd hd' (tl tl':ord (i::avd) ls) :
f_cons i a ls avd hd tl ≤ f_cons i a ls avd hd' tl' ->
hd ≤ hd'.
Proof.
intros. generalize (H i). clear H.
intro. hnf in H. hnf.
unfold f_cons in *. simpl in *.
destruct (in_dec string_dec i avd).
- destruct hd.
+ destruct hd'.
* auto.
* elim n; auto.
+ elim n; auto.
- revert H.
destruct (tl i).
+ destruct (tl' i).
* simpl.
destruct hd. contradiction.
destruct hd'. contradiction.
revert c c0. simpl.
destruct (string_dec i i); simpl; auto.
elim n2; auto.
* elim n0; simpl; auto.
+ elim n0; simpl; auto.
Qed.
Lemma f_cons_reflects2 i a ls avd
hd hd' (tl tl':ord (i::avd) ls) :
f_cons i a ls avd hd tl ≤ f_cons i a ls avd hd' tl' ->
tl ≤ tl'.
Proof.
repeat intro. generalize (H i0); clear H.
intro. hnf. hnf in H. unfold f_cons in *. simpl in *.
destruct (in_dec string_dec i0 avd).
- destruct (tl i0).
+ destruct (tl' i0). auto.
elim n; auto.
+ elim n; simpl; auto.
- destruct (tl i0).
+ destruct (tl' i0). auto.
elim n0; auto.
+ destruct (tl' i0).
* elim n0; auto.
* destruct (string_dec i i0).
** subst i0.
elim n0; simpl; auto.
** auto.
Qed.
Lemma f_cons_hd_tl i a ls avd
(f:ord avd ((i,a)::ls)) :
forall (hd:codom avd (Some a) i) (tl : ord (i::avd) ls),
hd ≈ f_hd i a ls avd f ->
tl ≈ f_tl i a ls avd f ->
f ≈ f_cons i a ls avd hd tl.
Proof.
intros.
cut (forall i',
finprod_codom_ord _ _ i' (f i') (f_cons i a ls avd hd tl i') /\
finprod_codom_ord _ _ i' (f_cons i a ls avd hd tl i') (f i')).
{ intros; split; intro; apply H1; auto. }
intro i'.
pose (string_dec i i').
destruct s.
- subst i'.
unfold f_cons, f_tl, f_hd, finprod_codom_ord in *. simpl in *.
revert H H0; simpl.
destruct hd.
+ destruct (f i).
* destruct (in_dec string_dec i avd); intuition.
* revert c; simpl.
destruct (string_dec i i).
** simpl.
intros. destruct H. elim H.
** elim n0; auto.
+ destruct (f i).
* contradiction.
* destruct (in_dec string_dec i avd). contradiction.
simpl.
revert c c0; simpl.
destruct (string_dec i i).
** simpl; intros.
destruct H0; auto.
** elim n2. auto.
- clear H. unfold f_tl in H0.
destruct H0. simpl in *.
generalize (H i') (H0 i'); clear H H0.
unfold finprod_codom_ord, f_cons; simpl.
destruct (in_dec string_dec i' avd).
+ destruct (f i'); simpl.
* intros. auto.
* contradiction.
+ destruct (f i'); simpl. contradiction.
revert c; simpl.
destruct (string_dec i i'); auto.
* elim n; auto.
* intros.
destruct (tl i').
** destruct i0; contradiction.
** split; auto.
Qed.
Fixpoint enum_finprod (ls:list (atom*A)) (avd:list atom) (z:N) :
option (finprod_elem avd ls) :=
match ls as ls' return option (finprod_elem avd ls') with
| nil => Some (fun i:atom =>
match in_dec string_dec i avd with
| left Hin => codom_avoid Hin
| right Hnin => @codom_elem avd None i Hnin tt
end)
| (i,a)::ls' =>
match in_dec string_dec i avd with
| left IN =>
match enum_finprod ls' (i::avd) z with
| None => None
| Some f => Some (f_cons i a ls' avd (codom_avoid IN) f)
end
| right NIN =>
let (p,q) := pairing.unpairing z in
match enum_finprod ls' (i::avd) q with
| None => None
| Some f =>
match eff_enum _ (PLT.effective (F a)) p with
| None => None
| Some xi => Some (f_cons i a ls' avd (@codom_elem avd (Some a) i NIN xi) f)
end
end
end
end.
Lemma enum_finprod_complete ls : forall avd (f:finprod_elem avd ls),
f ∈ (enum_finprod ls avd : eset (ord avd ls)).
Proof.
induction ls; intros.
exists 0%N.
simpl.
split; intro; hnf; simpl.
destruct (in_dec string_dec i avd).
destruct (f i); auto.
destruct (f i). contradiction.
hnf; auto.
destruct (in_dec string_dec i avd).
destruct (f i); auto.
destruct (f i). contradiction.
hnf; auto.
destruct a as [i a].
hnf. simpl.
destruct (in_dec string_dec i avd).
destruct (IHls (i::avd) (f_tl i a ls avd f)).
exists x.
simpl in *.
destruct (enum_finprod ls (i::avd) x).
apply f_cons_hd_tl.
unfold f_hd. simpl.
destruct (f i). clear.
simpl.
destruct (string_dec i i). split; hnf; auto.
elim n; auto.
contradiction.
auto. auto.
simpl in *.
case_eq (f_hd i a ls avd f); intros.
contradiction.
destruct (eff_complete _ (PLT.effective (F a)) c) as [q ?].
destruct (IHls (i::avd) (f_tl i a ls avd f)) as [p ?].
exists (pairing.pairing (q,p)).
rewrite pairing.unpairing_pairing.
destruct (enum_finprod ls (i::avd) p).
match goal with [|- match (match ?X with _ => _ end) with _ => _ end ] =>
set (X' := X); fold X' in H0
end.
cut (match X' with Some a' => c ≈ a' | None => False end); auto.
destruct X'; auto.
intros.
apply f_cons_hd_tl; auto.
rewrite H. auto.
auto.
Qed.
Program Definition finprod_effective avd ls : effective_order (ord avd ls) :=
EffectiveOrder (ord avd ls) (finprod_dec ls avd) (enum_finprod ls avd)
(enum_finprod_complete ls avd).
Program Definition f_cons' i a ls avd :
Preord.hom
(codom avd (Some a) i × ord (i::avd) ls)%cat_ob
(ord avd ((i,a)::ls))
:=
Preord.Hom _ _ (fun hf => f_cons i a ls avd (fst hf) (snd hf)) _.
Next Obligation.
intros. destruct H; apply f_cons_mono; auto.
Qed.
Program Definition f_hd' i a ls avd :
Preord.hom (ord avd ((i,a)::ls)) (codom avd (Some a) i)
:=
Preord.Hom _ _ (f_hd i a ls avd) _.
Next Obligation.
intros. unfold f_hd.
generalize (H i).
destruct (a0 i).
destruct (b i).
simpl. destruct (string_dec i i); auto.
elim n; auto.
contradiction.
destruct (b i).
intro. elim H0.
revert c c0; simpl.
destruct (string_dec i i); auto.
elim n1; auto.
Qed.
Program Definition f_tl' i a ls avd :
Preord.hom (ord avd ((i,a)::ls)) (ord (i::avd) ls)
:=
Preord.Hom _ _ (f_tl i a ls avd) _.
Next Obligation.
intros.
unfold f_tl. intro. simpl.
generalize (H i0).
destruct (a0 i0).
destruct (b i0).
auto. contradiction.
destruct (b i0). contradiction.
revert c c0; simpl.
destruct (string_dec i i0); auto.
Qed.
Lemma finprod_has_normals ls avd :
has_normals (ord avd ls) (finprod_effective avd ls) false.
Proof.
revert avd.
induction ls.
hnf; simpl; intros.
hnf. simpl; intros.
exists ((fun i:atom =>
match in_dec string_dec i avd with
| left Hin => codom_avoid Hin
| right Hnin => @codom_elem avd None i Hnin tt
end)::nil).
split.
red; intros.
apply cons_elem. left.
split; hnf; simpl; intros.
destruct (a i).
destruct (in_dec string_dec i avd). auto.
contradiction.
destruct (in_dec string_dec i avd).
contradiction.
hnf; auto.
destruct (a i).
destruct (in_dec string_dec i avd). auto.
contradiction.
destruct (in_dec string_dec i avd).
contradiction.
hnf; auto.
split. red; auto.
repeat intro.
exists (fun i:atom =>
match in_dec string_dec i avd with
| left Hin => codom_avoid Hin
| right Hnin => @codom_elem avd None i Hnin tt
end).
split.
repeat intro.
destruct (x i).
destruct (in_dec string_dec i avd).
auto.
contradiction.
destruct (in_dec string_dec i avd).
contradiction.
hnf; auto.
rewrite finsubset_elem. split.
apply cons_elem; auto.
repeat intro.
destruct (z i); destruct (in_dec string_dec i avd); try contradiction; hnf; auto.
intros. rewrite <- H0; auto.
intros. hnf; simpl; intros.
destruct a as [i a].
set (X' := image (f_tl' i a ls avd) X).
destruct (IHls (i::avd) X') as [Q' [??]]; auto.
set (A := image (f_hd' i a ls avd) X).
set (A' := mub_closure (codom_plotkin _ _ _) A).
set (Z := image (f_cons' i a ls avd) (finprod A' Q')).
exists Z.
split.
red; intros.
unfold Z.
apply image_axiom1'.
exists (f_hd' i a ls avd a0, f_tl' i a ls avd a0).
split; simpl.
apply f_cons_hd_tl; auto.
apply finsets.finprod_elem.
split.
unfold A'.
apply mub_clos_incl. unfold A.
apply image_axiom1'. exists a0.
split; simpl; auto.
apply H.
unfold X'.
apply image_axiom1'. exists a0.
simpl; auto.
split. red; auto.
repeat intro.
destruct H0.
destruct (H2 (f_tl i a ls avd z) (image (f_tl' i a ls avd) M))
as [q_tl [??]]; auto.
red; intros.
apply image_axiom2 in H3.
destruct H3 as [y [??]].
rewrite finsubset_elem.
apply H1 in H3.
rewrite finsubset_elem in H3.
destruct H3.
unfold Z in H3.
apply image_axiom2 in H3.
destruct H3 as [y' [??]].
destruct y'. apply finsets.finprod_elem in H3.
destruct H3. simpl in H6.
assert ((f_cons i a ls avd (f_hd i a ls avd y) (f_tl i a ls avd y) : ord _ _)
≈ f_cons i a ls avd c c0).
rewrite <- H6. symmetry.
apply f_cons_hd_tl; auto.
destruct H8.
apply f_cons_reflects2 in H8.
apply f_cons_reflects2 in H9.
split. rewrite H4.
simpl. apply member_eq with c0; auto.
destruct H4; auto. simpl in H4.
rewrite H4.
apply f_tl'_obligation_1. auto.
intros. rewrite <- H5; auto.