-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHF.v
More file actions
1457 lines (1212 loc) · 33.4 KB
/
HF.v
File metadata and controls
1457 lines (1212 loc) · 33.4 KB
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
(** Set theory without infinity axiom: hereditarily finite sets *)
Require Export Setoid Morphisms.
Require Import List.
Require Import Bool.
Lemma in_map_inv : forall (A B:Set) (f:A->B) x l,
In x (map f l) -> exists2 y, In y l & x = f y.
Proof.
induction l; simpl in |- *; intros.
elim H.
destruct H.
exists a; auto.
elim IHl; intros; trivial.
exists x0; auto.
Qed.
(*****************)
Inductive hf : Set := HF (elts : list hf).
Definition hf_elts (x:hf) := let (lx) := x in lx.
Lemma hf_elts_ext : forall x, HF (hf_elts x) = x.
destruct x; reflexivity.
Qed.
Lemma hf_ind2 : forall P : hf -> Prop,
(forall l, (forall x, In x l -> P x) -> P (HF l)) ->
forall x, P x.
Proof.
intros P Pnode.
fix hf_ind 1.
destruct x.
apply Pnode.
induction elts; simpl in |- *; intros.
elim H.
destruct H.
elim H.
apply hf_ind.
apply IHelts; trivial.
Qed.
(*****************)
Definition forall_elt P x :=
fold_right (fun y b => P y && b) true (hf_elts x).
Definition exists_elt P x :=
negb (forall_elt (fun x => negb (P x)) x).
Lemma forall_elt_case :
forall P x,
forall_elt P x = true /\ (forall y, In y (hf_elts x) -> P y = true) \/
forall_elt P x = false /\ (exists2 y, In y (hf_elts x) & P y = false).
Proof.
unfold forall_elt in |- *.
destruct x as (lx); simpl.
induction lx; simpl in |- *; intros.
left.
split; trivial.
destruct 1.
elimtype (P a = true \/ P a = false); intros.
rewrite H; simpl in |- *.
elim IHlx; intros.
left.
destruct H0; split; trivial.
destruct 1; auto.
elim H2; auto.
right.
destruct H0.
split; auto.
destruct H1.
exists x; auto.
rewrite H; simpl in |- *.
right.
split; auto.
exists a; auto.
destruct (P a); auto.
Qed.
Lemma forall_elt_true_intro :
forall P x,
(forall y, In y (hf_elts x) -> P y = true) ->
forall_elt P x = true.
intros.
elim forall_elt_case with P x; intros.
destruct H0; trivial.
destruct H0.
destruct H1.
rewrite (H _ H1) in H2; discriminate.
Qed.
Lemma forall_elt_false_intro :
forall P y x,
In y (hf_elts x) ->
P y = false ->
forall_elt P x = false.
intros.
elim forall_elt_case with P x; intros.
destruct H1.
rewrite (H2 _ H) in H0; discriminate.
destruct H1; trivial.
Qed.
Lemma forall_elt_true_elim :
forall P y x,
forall_elt P x = true ->
In y (hf_elts x) ->
P y = true.
intros.
elim forall_elt_case with P x; intros.
destruct H1; auto.
destruct H1.
rewrite H1 in H; discriminate.
Qed.
Lemma forall_elt_false_elim :
forall P x,
forall_elt P x = false ->
exists2 y, In y (hf_elts x) & P y = false.
intros.
elim forall_elt_case with P x; intros.
destruct H0.
rewrite H0 in H; discriminate.
destruct H0; trivial.
Qed.
Lemma exists_elt_true_intro :
forall P y x,
In y (hf_elts x) ->
P y = true ->
exists_elt P x = true.
intros.
unfold exists_elt in |- *.
rewrite forall_elt_false_intro with (fun x => negb (P x)) y x; trivial.
rewrite H0; trivial.
Qed.
Lemma exists_elt_true_elim :
forall P x,
exists_elt P x = true ->
exists2 y, In y (hf_elts x) & P y = true.
intros.
elim forall_elt_false_elim with (fun x => negb (P x)) x; intros.
exists x0; trivial.
rewrite <- (negb_elim (P x0)).
rewrite H1; trivial.
fold (negb true) in |- *.
apply negb_sym; trivial.
symmetry in |- *; trivial.
Qed.
(** Equality *)
Fixpoint eq_hf (x y: hf) {struct x} : bool :=
forall_elt (fun x1 => exists_elt (fun y1 => eq_hf x1 y1) y) x &&
forall_elt (fun y1 => exists_elt (fun x1 => eq_hf x1 y1) x) y.
Definition Eq_hf x y := eq_hf x y = true.
Lemma eq_hf_intro :
forall x y,
(forall x', In x' (hf_elts x) ->
exists2 y', In y' (hf_elts y) & Eq_hf x' y') ->
(forall y', In y' (hf_elts y) ->
exists2 x', In x' (hf_elts x) & Eq_hf x' y') ->
Eq_hf x y.
Proof.
unfold Eq_hf.
destruct x as (lx); simpl; intros.
apply andb_true_intro; split; apply forall_elt_true_intro; intros.
elim H with (1 := H1); intros.
apply exists_elt_true_intro with x; auto.
elim H0 with (1 := H1); intros.
apply exists_elt_true_intro with x; auto.
Qed.
Lemma eq_hf_elim1 :
forall x' x y,
Eq_hf x y ->
In x' (hf_elts x) ->
exists2 y', In y' (hf_elts y) & Eq_hf x' y'.
Proof.
unfold Eq_hf.
destruct x as (lx); simpl in |- *; intros.
elim andb_prop with (1 := H); clear H; intros.
specialize forall_elt_true_elim with (1 := H) (2 := H0); intros.
apply exists_elt_true_elim with (P := fun y' => eq_hf x' y').
trivial.
Qed.
Lemma eq_hf_elim2 :
forall y' x y,
Eq_hf x y ->
In y' (hf_elts y) ->
exists2 x', In x' (hf_elts x) & Eq_hf x' y'.
Proof.
unfold Eq_hf.
destruct x as (lx); destruct y as (ly); simpl in |- *; intros.
elim andb_prop with (1 := H); clear H; intros.
specialize forall_elt_true_elim with (1 := H1) (2 := H0); intros.
change lx with (hf_elts (HF lx)).
apply exists_elt_true_elim with (P := fun x1 => eq_hf x1 y').
trivial.
Qed.
Instance eq_hf_refl : Reflexive Eq_hf.
red; intro.
elim x using hf_ind2; intros.
apply eq_hf_intro; intros.
exists x'; auto.
exists y'; auto.
Qed.
Instance eq_hf_sym : Symmetric Eq_hf.
red; intros x.
elim x using hf_ind2; destruct y; intros.
apply eq_hf_intro; intros.
elim eq_hf_elim2 with (1 := H0) (2 := H1); intros.
exists x0; auto.
elim eq_hf_elim1 with (1 := H0) (2 := H1); intros.
exists x0; auto.
Qed.
Instance eq_hf_trans : Transitive Eq_hf.
red; intros x.
elim x using hf_ind2; destruct y; destruct z; intros.
apply eq_hf_intro; intros.
elim eq_hf_elim1 with (1 := H0) (2 := H2); intros.
elim eq_hf_elim1 with (1 := H1) (2 := H3); intros.
exists x1; eauto.
elim eq_hf_elim2 with (1 := H1) (2 := H2); intros.
elim eq_hf_elim2 with (1 := H0) (2 := H3); intros.
exists x1; eauto.
Qed.
Instance eq_hf_equiv : Equivalence Eq_hf. (* why is it needed? *)
constructor; auto with *.
Qed.
Instance eq_hf_morph : Proper (Eq_hf ==> Eq_hf ==> @eq bool) eq_hf.
repeat red; intros.
apply bool_1.
fold (Eq_hf x x0) (Eq_hf y y0).
rewrite H; rewrite H0; reflexivity.
Qed.
(** Membership *)
Definition in_hf (x y: hf) : bool := exists_elt (fun y1 => eq_hf x y1) y.
Definition In_hf x y := in_hf x y = true.
Lemma in_hf_intro : forall x x' y,
Eq_hf x x' ->
In x' (hf_elts y) ->
In_hf x y.
intros.
unfold In_hf, in_hf.
apply exists_elt_true_intro with x'; auto.
Qed.
Lemma in_hf_elim : forall x y,
In_hf x y ->
exists2 x', In x' (hf_elts y) & Eq_hf x x'.
unfold In_hf, in_hf.
intros.
apply exists_elt_true_elim with (1:=H).
Qed.
Lemma in_hf_reg_l: forall a a' b,
Eq_hf a a' -> In_hf a b -> In_hf a' b.
intros.
elim in_hf_elim with (1:=H0); intros.
apply in_hf_intro with x; trivial.
transitivity a; trivial; symmetry; trivial.
Qed.
Lemma in_hf_reg_r :
forall a x y,
Eq_hf x y ->
In_hf a x ->
In_hf a y.
intros.
elim in_hf_elim with (1:=H0); intros.
elim eq_hf_elim1 with (1:=H) (2:=H1); intros.
apply in_hf_intro with x1; trivial.
transitivity x0; trivial.
Qed.
Instance In_hf_morph : Proper (Eq_hf ==> Eq_hf ==> iff) In_hf.
split; intros.
apply in_hf_reg_l with x; trivial.
apply in_hf_reg_r with x0; trivial.
symmetry in H, H0.
apply in_hf_reg_l with y; trivial.
apply in_hf_reg_r with y0; trivial.
Qed.
Instance in_hf_morph : Proper (Eq_hf ==> Eq_hf ==> @eq _) in_hf.
repeat red; intros.
apply bool_1.
fold (In_hf x x0) (In_hf y y0).
rewrite H; rewrite H0; reflexivity.
Qed.
Lemma In_hf_head : forall x y l,
Eq_hf x y ->
In_hf x (HF (y::l)).
intros.
apply in_hf_intro with y; simpl; auto.
Qed.
Lemma In_hf_tail : forall x y l,
In_hf x (HF l) ->
In_hf x (HF (y::l)).
intros.
elim in_hf_elim with (1:=H); intros.
apply in_hf_intro with x0; simpl; auto.
Qed.
Lemma In_hf_elim : forall x y l,
In_hf x (HF (y::l)) ->
Eq_hf x y \/ In_hf x (HF l).
Proof.
intros.
elim in_hf_elim with (1:=H); simpl; intros.
destruct H0.
subst x0; auto.
right; apply in_hf_intro with x0; trivial.
Qed.
Lemma In_app_left : forall x l1 l2,
In_hf x (HF l1) ->
In_hf x (HF (l1 ++ l2)).
Proof.
induction l1; simpl; intros.
inversion H.
elim In_hf_elim with (1:=H); intros.
apply In_hf_head; trivial.
apply In_hf_tail; auto.
Qed.
Lemma In_app_right : forall x l1 l2,
In_hf x (HF l2) ->
In_hf x (HF (l1 ++ l2)).
Proof.
induction l1; simpl; intros; auto.
apply In_hf_tail; auto.
Qed.
Lemma In_app_elim : forall x l1 l2,
In_hf x (HF (l1 ++ l2)) ->
In_hf x (HF l1) \/ In_hf x (HF l2).
induction l1; simpl; intros; auto.
elim In_hf_elim with (1:=H); intros.
left.
apply In_hf_head; trivial.
elim IHl1 with (1:=H0); intros; auto.
left.
apply In_hf_tail; trivial.
Qed.
Definition incl_hf x y :=
forall_elt (fun x1 => in_hf x1 y) x.
Definition Incl_hf x y := forall a, In_hf a x -> In_hf a y.
Instance incl_hf_morph : Proper (Eq_hf ==> Eq_hf ==> iff) Incl_hf.
unfold Incl_hf; split; intros.
rewrite <- H0; rewrite <- H in H2; auto.
rewrite H0; rewrite H in H2; auto.
Qed.
Lemma incl_hf_sound : forall x y,
incl_hf x y = true -> Incl_hf x y.
unfold incl_hf, Incl_hf.
intros.
elim in_hf_elim with (1:=H0); intros.
specialize forall_elt_true_elim with (1:=H) (2:=H1); intros.
apply in_hf_reg_l with x0; trivial.
symmetry; trivial.
Qed.
Lemma incl_hf_complete : forall x y,
Incl_hf x y -> incl_hf x y = true.
unfold incl_hf, Incl_hf; intros.
apply forall_elt_true_intro; intros.
apply H.
apply in_hf_intro with y0; trivial; reflexivity.
Qed.
Lemma Eq_hf_intro : forall x y,
Incl_hf x y -> Incl_hf y x -> Eq_hf x y.
intros.
apply eq_hf_intro; intros.
apply in_hf_elim.
apply H.
apply in_hf_intro with x'; trivial; reflexivity.
elim (in_hf_elim y' x); intros.
exists x0; trivial; symmetry; trivial.
apply H0.
apply in_hf_intro with y'; trivial; reflexivity.
Qed.
Lemma Eq_hf_cons : forall x1 x2 l1 l2,
Eq_hf x1 x2 ->
Eq_hf (HF l1) (HF l2) ->
Eq_hf (HF (x1::l1)) (HF (x2::l2)).
Proof.
intros.
apply Eq_hf_intro; red; simpl in |- *; intros.
elim In_hf_elim with (1:=H1); intros.
apply In_hf_head; transitivity x1; trivial.
apply In_hf_tail; apply in_hf_reg_r with (HF l1); trivial.
elim In_hf_elim with (1:=H1); intros.
apply In_hf_head; transitivity x2; trivial.
symmetry; trivial.
apply In_hf_tail; apply in_hf_reg_r with (HF l2); trivial.
symmetry; trivial.
Qed.
Lemma Eq_hf_split : forall l1 l1' l2 l2',
Eq_hf (HF l1) (HF l1') ->
Eq_hf (HF l2) (HF l2') ->
Eq_hf (HF(l1++l2)) (HF(l1'++l2')).
intros.
apply Eq_hf_intro; red; intros.
elim In_app_elim with (1:=H1); intros.
apply In_app_left.
apply in_hf_reg_r with (HF l1); auto.
apply In_app_right.
apply in_hf_reg_r with (HF l2); auto.
elim In_app_elim with (1:=H1); intros.
apply In_app_left.
apply in_hf_reg_r with (HF l1'); auto.
symmetry; auto.
apply In_app_right.
apply in_hf_reg_r with (HF l2'); auto.
symmetry; auto.
Qed.
Lemma hf_size_ind : forall (P:hf->Type) x,
P (HF nil) ->
(forall x' y,
In_hf x' x ->
Incl_hf y x ->
P y -> P (HF(x'::hf_elts y))) ->
P x.
Proof.
destruct x as (x).
intro Pnil.
elim x; intros; trivial.
change l with (hf_elts (HF l)).
apply X0.
apply In_hf_head; reflexivity.
red; intros; apply In_hf_tail; trivial.
apply X; intros.
apply X0; trivial.
apply In_hf_tail; trivial.
red; intros; apply In_hf_tail; auto.
Qed.
(** Cancelling redundancies *)
Lemma cancel_repeat : forall a l,
In_hf a (HF l) -> Eq_hf (HF(a::l)) (HF l).
intros.
apply Eq_hf_intro; red; intros.
elim In_hf_elim with (1:=H0); intros; auto.
rewrite H1; trivial.
apply In_hf_tail; trivial.
Qed.
Definition cons_hf x l := if in_hf x (HF l) then l else x :: l.
Lemma cons_hf_cons :
forall x l, Eq_hf (HF(cons_hf x l)) (HF(cons x l)).
unfold cons_hf.
intros.
case_eq (in_hf x (HF l)); intro.
symmetry; apply cancel_repeat; trivial.
reflexivity.
Qed.
Lemma In_hf_head_hf : forall x y l,
Eq_hf x y ->
In_hf x (HF (cons_hf y l)).
Proof.
intros.
rewrite cons_hf_cons.
apply In_hf_head; trivial.
Qed.
Lemma In_hf_tail_hf : forall x y l,
In_hf x (HF l) ->
In_hf x (HF (cons_hf y l)).
Proof.
intros.
rewrite cons_hf_cons.
apply In_hf_tail; trivial.
Qed.
Lemma In_hf_elim_hf : forall x y l,
In_hf x (HF (cons_hf y l)) ->
Eq_hf x y \/ In_hf x (HF l).
intros.
rewrite cons_hf_cons in H.
apply In_hf_elim; trivial.
Qed.
Lemma Eq_hf_cons_hf : forall x1 x2 l1 l2,
Eq_hf x1 x2 ->
Eq_hf (HF l1) (HF l2) ->
Eq_hf (HF (cons_hf x1 l1)) (HF (cons_hf x2 l2)).
Proof.
intros.
do 2 rewrite cons_hf_cons.
apply Eq_hf_cons; trivial.
Qed.
Fixpoint app_hf (l1 l2:list hf) {struct l1} : list hf :=
match l1 with
nil => l2
| cons x l' => cons_hf x (app_hf l' l2)
end.
Lemma In_app_hf_left : forall x l1 l2,
In_hf x (HF l1) ->
In_hf x (HF (app_hf l1 l2)).
Proof.
induction l1; simpl; intros.
inversion H.
elim In_hf_elim with (1:=H); intros.
apply In_hf_head_hf; trivial.
apply In_hf_tail_hf; auto.
Qed.
Lemma In_app_hf_right : forall x l1 l2,
In_hf x (HF l2) ->
In_hf x (HF (app_hf l1 l2)).
Proof.
induction l1; simpl; intros; auto.
apply In_hf_tail_hf; auto.
Qed.
Lemma In_app_hf_elim : forall x l1 l2,
In_hf x (HF (app_hf l1 l2)) ->
In_hf x (HF l1) \/ In_hf x (HF l2).
induction l1; simpl; intros; auto.
elim In_hf_elim_hf with (1:=H); intros.
left.
apply In_hf_head; trivial.
elim IHl1 with (1:=H0); intros; auto.
left.
apply In_hf_tail; trivial.
Qed.
Definition fold_set (X:Type) (f:hf->X->X) (x:hf) (acc:X) : X :=
let fix F (l:list hf) : X :=
match l with
nil => acc
| cons x l' => if in_hf x (HF l') then F l' else f x (F l')
end in
F (hf_elts x).
Lemma fold_set_unfold : forall X f x l acc,
fold_set X f (HF(x::l)) acc =
if in_hf x (HF l) then fold_set X f (HF l) acc
else f x (fold_set X f (HF l) acc).
reflexivity.
Qed.
Lemma fold_set_ind : forall X (P:hf->X->Prop) f x acc,
P (HF nil) acc ->
(forall x' y acc, In_hf x' x -> In_hf x' y -> P y acc ->
P (HF(x'::hf_elts y)) acc) ->
(forall x' y acc, In_hf x' x -> ~ In_hf x' y -> P y acc ->
P (HF(x'::hf_elts y)) (f x' acc)) ->
P x (fold_set X f x acc).
Proof.
destruct x.
induction elts; simpl; intros; auto.
rewrite fold_set_unfold.
case_eq (in_hf a (HF elts)); intro.
change elts with (hf_elts (HF elts)).
apply H0; trivial.
apply In_hf_head; reflexivity.
simpl in IHelts; apply (IHelts acc); intros; trivial.
apply H0; trivial.
apply In_hf_tail; trivial.
apply H1; trivial.
apply In_hf_tail; trivial.
change elts with (hf_elts (HF elts)).
apply H1; trivial.
apply In_hf_head; reflexivity.
unfold In_hf; rewrite H2; discriminate.
simpl in IHelts; apply (IHelts acc); intros; trivial.
apply H0; trivial.
apply In_hf_tail; trivial.
apply H1; trivial.
apply In_hf_tail; trivial.
Qed.
Fixpoint canonical (x:hf) : hf :=
HF (fold_set _ (fun y cl => canonical y :: cl) x nil).
Lemma canonical_correct : forall x, Eq_hf (canonical x) x.
Proof.
intro.
elim x using hf_ind2; intros.
unfold canonical in |- *; fold canonical in |- *.
induction l; simpl in |- *; intros.
compute; reflexivity.
rewrite fold_set_unfold.
case_eq (in_hf a (HF l)); intro.
apply eq_hf_trans with (HF l); auto with *.
symmetry; apply cancel_repeat; trivial.
apply Eq_hf_cons; auto with *.
Qed.
Hint Resolve In_hf_head In_hf_head_hf In_hf_head_hf In_hf_tail_hf.
(** Notations *)
Notation "{ l }" := (HF l) (at level 0, l at level 99).
Notation EMPTY := {nil}.
Notation ONE := {EMPTY::nil}.
Notation TWO := {EMPTY::ONE::nil}.
Infix ":::" := cons_hf (at level 60, right associativity).
Infix "+++" := app_hf (at level 60, right associativity).
(* *)
Notation "x ∈ y" := (In_hf x y) (at level 60).
Notation "x == y" := (Eq_hf x y) (at level 70).
Notation morph1 := (Proper (Eq_hf ==> Eq_hf)).
Notation morph2 := (Proper (Eq_hf ==> Eq_hf ==> Eq_hf)).
Notation morph3 := (Proper (Eq_hf ==> Eq_hf ==> Eq_hf ==> Eq_hf)).
(** Set theoretical operators *)
Definition empty := HF nil.
Definition singl x := HF (x:::nil).
Definition pair x y := HF (x:::y:::nil).
Definition union (x:hf) :=
HF (fold_set _ (fun y l => hf_elts y+++l) x nil).
Definition power (x:hf) :=
HF (fold_set _
(fun y pow p => pow p ++ pow (y::p)) x
(fun p => HF (rev p) :: nil) nil).
Definition subset (x:hf) (P:hf->bool) :=
HF (fold_set _ (fun y l => if P y then y :: l else l) x nil).
Definition repl (x:hf) (f:hf->hf) :=
HF (map f (hf_elts x)).
Instance singl_morph : morph1 singl.
Proof.
unfold singl in |- *; do 2 red; intros.
apply Eq_hf_cons_hf; trivial.
reflexivity.
Qed.
Instance pair_morph : morph2 pair.
Proof.
unfold pair in |- *; do 3 red; intros.
repeat apply Eq_hf_cons_hf; trivial.
reflexivity.
Qed.
Definition eq_hf_fun (x:hf) (f1 f2:hf->hf) :=
forall y1 y2, y1 ∈ x -> y1 == y2 -> f1 y1 == f2 y2.
Lemma eq_hf_fun_sym : forall x f1 f2,
eq_hf_fun x f1 f2 -> eq_hf_fun x f2 f1.
Proof.
unfold eq_hf_fun in |- *; intros.
symmetry in |- *.
apply H.
apply in_hf_reg_l with y1; trivial.
symmetry in |- *; trivial.
Qed.
Lemma eq_hf_fun_trans : forall x f1 f2 f3,
eq_hf_fun x f1 f2 -> eq_hf_fun x f2 f3 -> eq_hf_fun x f1 f3.
Proof.
unfold eq_hf_fun in |- *; intros.
transitivity (f2 y1); auto.
apply H; trivial.
reflexivity.
Qed.
Lemma eq_hf_fun_left : forall x f1 f2,
eq_hf_fun x f1 f2 -> eq_hf_fun x f1 f1.
Proof.
unfold eq_hf_fun in |- *; intros.
transitivity (f2 y2); auto.
symmetry in |- *.
apply H.
apply in_hf_reg_l with y1; trivial.
reflexivity.
Qed.
Definition hf_pred_morph x P :=
forall y1 y2, y1 ∈ x -> y1 == y2 -> P y1 = true -> P y2 = true.
Definition eq_hf_pred (x:hf) (f1 f2:hf->bool) :=
forall y1 y2, y1 ∈ x -> y1 == y2 -> f1 y1 = f2 y2.
(* *)
Lemma empty_elim : forall x, ~ x ∈ empty.
compute in |- *; intros; discriminate.
Qed.
Lemma empty_ext : forall a, (forall x, ~ x ∈ a) -> a == empty.
intros.
apply Eq_hf_intro; red; intros.
elim H with a0; trivial.
elim empty_elim with a0; trivial.
Qed.
Lemma singl_intro : forall x y, x == y -> x ∈ singl y.
unfold singl in |- *; auto.
Qed.
Lemma singl_elim : forall x a, x ∈ singl a -> x == a.
unfold singl in |- *; intros.
elim In_hf_elim_hf with (1 := H); intros; auto.
discriminate H0.
Qed.
Lemma singl_ext :
forall y x,
x ∈ y ->
(forall z, z ∈ y -> z == x) ->
y == singl x.
Proof.
intros; apply Eq_hf_intro; red; intros.
apply singl_intro; auto.
apply in_hf_reg_l with x; trivial.
symmetry in |- *; apply singl_elim; trivial.
Qed.
Lemma pair_elim : forall x a b,
x ∈ pair a b -> x == a \/ x == b.
intros.
unfold pair in H.
elim In_hf_elim_hf with (1 := H); intros; auto.
right.
elim In_hf_elim_hf with (1 := H0); intros; auto.
discriminate H1.
Qed.
Lemma pair_intro1 : forall x a b, x == a -> x ∈ pair a b.
Proof.
unfold pair;auto.
Qed.
Lemma pair_intro2 : forall x a b, x == b -> x ∈ pair a b.
Proof.
unfold pair;auto.
Qed.
Lemma union_intro : forall x y z, x ∈ y -> y ∈ z -> x ∈ union z.
Proof.
intros x y z H.
unfold union.
pattern z, (fold_set _ (fun y0 l => hf_elts y0 +++ l) z nil).
apply fold_set_ind; intros.
discriminate.
apply H2.
rewrite cancel_repeat in H3; auto.
elim In_hf_elim with (1:=H3); intros.
apply In_app_hf_left.
rewrite hf_elts_ext.
rewrite <- H4; trivial.
apply In_app_hf_right.
rewrite hf_elts_ext in H4.
auto.
Qed.
Lemma union_elim : forall x z, x ∈ union z -> exists2 y, x ∈ y & y ∈ z.
unfold union.
intros x z.
pattern z, (fold_set _ (fun y l => hf_elts y +++ l) z nil).
apply fold_set_ind; intros.
discriminate.
destruct H1; trivial.
exists x0; trivial.
rewrite cancel_repeat; auto.
elim In_app_hf_elim with (1:=H2); intros.
rewrite hf_elts_ext in H3.
exists x'; trivial.
apply In_hf_head; reflexivity.
elim H1 with (1:=H3); intros.
exists x0; trivial.
apply In_hf_tail.
rewrite hf_elts_ext; trivial.
Qed.
Lemma union_ext :
forall u z,
(forall x y, x ∈ y -> y ∈ z -> x ∈ u) ->
(forall x, x ∈ u -> exists2 y, x ∈ y & y ∈ z) ->
u == union z.
intros.
apply Eq_hf_intro; red; intros.
elim H0 with (1:=H1); intros.
apply union_intro with x; trivial.
elim union_elim with (1:=H1); intros.
eauto.
Qed.
Instance union_morph : morph1 union.
do 2 red; intros.
apply union_ext; intros.
apply union_intro with y0; trivial.
rewrite H; trivial.
elim union_elim with (1 := H0); intros.
exists x1; trivial.
rewrite <- H; trivial.
Qed.
Lemma union_singl : forall x, union (singl x) == x.
intros.
symmetry; apply union_ext; intros.
apply singl_elim in H0.
rewrite <- H0; trivial.
exists x; trivial.
apply singl_intro; reflexivity.
Qed.
(** power properties *)
Lemma power_intro :
forall x y, Incl_hf x y -> x ∈ power y.
unfold power.
set (g := fun y0 (pow:list hf ->list hf) p => pow p ++ pow (y0 :: p)).
set (h := fun p => HF(rev p) :: nil).
intros x y.
assert (forall x l, Incl_hf (HF (rev l)) x ->
Incl_hf x (HF(rev l ++ hf_elts y)) ->
x ∈ HF(fold_set _ g y h l)).
clear x.
pattern y, (fold_set _ g y h).
apply fold_set_ind; intros.
unfold h; simpl.
apply In_hf_head.
apply Eq_hf_intro; trivial.
red; intros.
elim In_app_elim with (1:=H0 _ H1); intros; auto.
elim empty_elim with a; trivial.
apply H1; trivial.
red; intros.
elim In_app_elim with (1:=H3 _ H4); intros.
apply In_app_left; trivial.
apply In_app_right.
rewrite hf_elts_ext in H5.
elim In_hf_elim with (1:=H5); intros; trivial.
rewrite H6; trivial.
unfold g; simpl.
case_eq (in_hf x' x); intros.
apply In_app_right.
apply H1.
red; simpl; intros.
elim In_app_elim with (1:=H5); intros; auto.
elim In_hf_elim with (1:=H6); simpl; intros.
rewrite H7; trivial.
elim empty_elim with a; trivial.
simpl.
rewrite app_ass; simpl.
trivial.
apply In_app_left.
apply H1; trivial.
red; intros.
elim In_app_elim with (1:=H3 _ H5); simpl; intros.
apply In_app_left; trivial.
apply In_app_right.
elim In_hf_elim with (1:=H6); intros; auto.
rewrite H7 in H5; unfold In_hf in H5; rewrite H5 in H4; discriminate.
intro; apply H; simpl.
red; intros.
elim empty_elim with a; trivial.
rewrite hf_elts_ext; trivial.
Qed.
Lemma power_elim : forall x y z, x ∈ power y -> z ∈ x -> z ∈ y.
unfold power.
set (g := fun y0 (pow:list hf ->list hf) p => pow p ++ pow (y0 :: p)).
set (h := fun p => HF(rev p) :: nil).
intros x y.
assert (forall l,
x ∈ HF(fold_set _ g y h l) ->
forall z, z ∈ x -> ~ z ∈ (HF(rev l)) -> z ∈ y).
pattern y, (fold_set _ g y h).
apply fold_set_ind; intros.
unfold h in H.
elim H1.
apply in_hf_reg_r with (2:=H0).
apply singl_elim.
assumption.
apply In_hf_tail.
rewrite hf_elts_ext.
eauto.
unfold g in H2.
elim In_app_elim with (1:=H2);intros.
apply In_hf_tail; rewrite hf_elts_ext.
apply H1 with l; trivial.
case_eq (eq_hf z x'); intro.
apply In_hf_head; trivial.
apply In_hf_tail; rewrite hf_elts_ext.
apply H1 with (1:=H5); trivial.
simpl; red; intros; apply H4.
elim In_app_elim with (1:=H7); intros; trivial.
replace (eq_hf z x') with true in H6; try discriminate.
symmetry; apply singl_elim.
assumption.
intros.
apply H with (l:=@nil hf); trivial.
red; intros; discriminate.
Qed.
Lemma power_ext :
forall p a,
(forall x, (forall y, y ∈ x -> y ∈ a) -> x ∈ p) ->
(forall x y, x ∈ p -> y ∈ x -> y ∈ a) ->
p == power a.
intros.