-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIlist.v
1674 lines (1524 loc) · 57.1 KB
/
Ilist.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
(** Celia Picard with contributions by Ralph Matthes,
I.R.I.T., University of Toulouse and CNRS*)
(** provides an implementation of Ilist
It also provides various properties and lemmas about it. *)
Require Export Arith.
Require Import Utf8.
Require Import Setoid.
Require Import Morphisms.
Require Import List.
Require Import Fin.
Require Import ListEq.
Require Import Basics.
Require Import Tools.
Set Implicit Arguments.
Lemma decode_Fin_succ (n:nat)(f:Fin n): decode_Fin(succ f)=S(decode_Fin f).
Proof.
reflexivity.
Qed.
Create HintDb evalDecode_FinDb.
Hint Rewrite <- decode_Fin_match': evalDecode_FinDb.
Hint Rewrite decode_code1_Id: evalDecode_FinDb.
Hint Rewrite <- decode_Fin_get_cons: evalDecode_FinDb.
Hint Rewrite decode_Fin_succ: evalDecode_FinDb.
Ltac decode_Fin_get_cons_S := (apply eq_add_S || apply lt_S_n || apply le_S_n);
rewrite <- decode_Fin_get_cons.
Ltac evalDecode_Fin := repeat (autorewrite with evalDecode_FinDb || decode_Fin_get_cons_S).
Ltac decode_Fin_get_cons_S_Ass c := (apply eq_S in c || apply lt_n_S in c || apply le_n_S in c);
rewrite <- decode_Fin_get_cons in c.
Ltac evalDecode_Fin_Ass c := repeat (autorewrite with evalDecode_FinDb in c || decode_Fin_get_cons_S_Ass c).
Ltac treatFinPureDebug := (try (apply decode_Fin_unique));
evalDecode_Fin;
try (reflexivity || apply decode_Fin_inf_n).
Ltac treatFinPure := treatFinPureDebug; fail.
Ltac treatFinAss := (try (apply decode_Fin_unique));
evalDecode_Fin;
try (assumption || reflexivity || apply decode_Fin_inf_n);
fail.
Ltac treatFin c := (try (apply decode_Fin_unique));
let h:= fresh in assert (h := c);
evalDecode_Fin_Ass h;
evalDecode_Fin;
try (exact c || exact h || reflexivity || apply decode_Fin_inf_n);
clear h;
fail.
Ltac treatFinDebug c := (try (apply decode_Fin_unique));
let h:= fresh in assert (h := c);
evalDecode_Fin_Ass h;
evalDecode_Fin;
try (exact c || exact h || reflexivity || apply decode_Fin_inf_n).
(* let h:= fresh makes it clear that a fresh identifier has to be created *)
(* In this file we define functions and sets that will act like lists
without being lists *)
Section ilistn_def_tools.
Variable T: Set.
(* Definition of the function *)
Definition ilistn(n:nat) := Fin n -> T.
End ilistn_def_tools.
(* We define a pair (n, ilistn n) that mimics the behaviour of lists *)
Section ilist_def_tools.
(* Definition of the pair *)
Definition ilist(T:Set) := {n:nat & ilistn T n}.
(* Basic operations on ilist *)
(* important definition that is nothing but an abbreviation *)
Definition mkilist (T: Set)(n: nat)(i: ilistn T n): ilist T := existT (fun n => ilistn T n) n i.
Definition lgti(T: Set)(i: ilist T): nat:= projT1 i.
Definition fcti(T: Set)(i: ilist T): ilistn T (lgti i) := projT2 i.
Lemma lgti_mkilist (T: Set)(n: nat)(i: ilistn T n): lgti(mkilist i) = n.
Proof.
reflexivity.
Qed.
Create HintDb evalLgti.
Hint Rewrite lgti_mkilist: evalLgti.
(* Definition of the analogue for map on ilists *)
Definition imap (T U:Set) (f:T->U) (l:ilist T): ilist U :=
mkilist (fun i => f (fcti l i)).
(* Analogue for forall on ilists *)
Definition iall(T:Set)(p:T->Prop)(l: ilist T): Prop :=
forall i, p (fcti l i).
Lemma imap_lgti: forall (T U: Set)(i: ilist T)(f: T -> U),
lgti (imap f i) = lgti i.
Proof.
reflexivity.
Qed.
(* Definition of a more general relation on ilist *)
Inductive ilist_rel (T:Set) (R: T->T->Prop) (l1 l2: ilist T):Prop :=
is_ilist_rel: forall h: lgti l1 = lgti l2,
(forall f:Fin(lgti l1),
R (fcti l1 f) (fcti l2 (rewriteFins h f: Fin(lgti l2)))) -> ilist_rel R l1 l2.
Definition getNFin (n n': nat) (H: Fin n = Fin n'): nat := n'.
Section prLeq.
(* Proof that ilist_rel preserves equivalence *)
Lemma ilist_rel_refl: forall (T:Set) (R: T->T->Prop)(Rrefl: Reflexive R)(l: ilist T),
ilist_rel R l l.
Proof.
intros T R Rrefl i.
apply (is_ilist_rel R i i (refl_equal (lgti i))).
intros f.
reflexivity.
Qed.
Lemma ilist_rel_sym :
forall (T:Set) (R: T->T->Prop)(Rsym: Symmetric R)(l1 l2: ilist T),
ilist_rel R l1 l2 -> ilist_rel R l2 l1.
Proof.
intros T R R_sym l1 l2 [e H].
apply (is_ilist_rel R l2 l1 (sym_eq e)).
intro f.
assert (H1 : f = rewriteFins e (rewriteFins (eq_sym e) f)) by treatFinPure.
rewrite H1 at 1.
apply R_sym, H.
Qed.
Lemma ilist_rel_trans :
forall (T:Set) (R: T->T->Prop)(Rtrans: Transitive R)(l1 l2 l3: ilist T),
ilist_rel R l1 l2 -> ilist_rel R l2 l3 -> ilist_rel R l1 l3.
Proof.
intros T R R_trans l1 l2 l3 [e1 h1] [e2 h2].
apply (is_ilist_rel R l1 l3 (trans_eq e1 e2)).
intro f.
assert (h3 : rewriteFins (eq_trans e1 e2) f = rewriteFins e2 (rewriteFins e1 f)) by treatFinPure.
rewrite h3.
apply (R_trans _ _ _ (h1 f) (h2 (rewriteFins e1 f))).
Qed.
Add Parametric Relation(T: Set)(R: T -> T -> Prop)(RRel: Equivalence R):
(ilist T) (ilist_rel R)
reflexivity proved by (ilist_rel_refl _)
symmetry proved by (ilist_rel_sym _)
transitivity proved by (ilist_rel_trans _)
as ilist_relRel.
End prLeq.
Add Parametric Morphism (T: Set)(eqT: relation T)
(eqTR : Equivalence eqT)(p: T -> Prop)
(pM: Proper (eqT --> impl) p): (iall p)
with signature (ilist_rel eqT ==> impl)
as iallM.
Proof.
intros i1 i2 Hilist_rel H f.
apply (ilist_rel_sym _) in Hilist_rel.
destruct Hilist_rel as [Heq Hilist_rel].
assert (H' := H (rewriteFins Heq f)).
apply (pM _ _ (Hilist_rel f) H').
Qed.
Definition ilist_rel_lgti:
forall (T: Set)(R : T -> T -> Prop)(l1 l2: ilist T)(h:ilist_rel R l1 l2),
lgti l1= lgti l2.
Proof.
intros T R l1 l2 [e _].
assumption.
Defined.
Lemma ilist_rel_R_i: forall (T:Set)(R: T -> T -> Prop)
(l1 l2: ilist T)(h: lgti l1 = lgti l2)
(H : (∀f : Fin (lgti l1), R (fcti l1 f)
(fcti l2 (rewriteFins h f)))),
(forall (i: Fin (lgti l2)),
(R (fcti l1 (rewriteFins (sym_eq h) i)) (fcti l2 i))).
Proof.
intros T R l1 l2 e h f.
rewrite <- (rewriteFins_sym e f) at 2.
apply (h (rewriteFins (sym_eq e) f)).
Qed.
Lemma ilist_rel_mon (T: Set)(R1 R2: relation T)(HypR: subrelation R1 R2)(l1 l2: ilist T):
ilist_rel R1 l1 l2 -> ilist_rel R2 l1 l2.
Proof.
intros [H1 H2].
apply (is_ilist_rel _ _ _ H1).
intro i1.
apply HypR, H2.
Qed.
Section Bij_ilist_list.
Variable T: Set.
Variable comp : T -> T -> Prop.
Variable compRel: Equivalence comp.
Add Parametric Morphism (i: ilist T) : (fcti(T:= T) i)
with signature (eq(A:= Fin (lgti i))) ==> (comp)
as fctiM.
Proof.
reflexivity.
Qed.
(* translation from ilist to list *)
Definition ilist2list (i: ilist T) : list T :=
map (fcti i) (makeListFin (lgti i)).
Definition eq_comp_morph (n : nat) (f: Fin (n) -> T) :=
Proper (eq(A:= Fin (n)) ==> comp) f.
Definition fctiiM (i: ilist T):
Proper (eq(A:= Fin (lgti i)) ==> comp) (fcti i).
Proof.
red. intros f g H. rewrite H.
reflexivity.
Qed.
Definition iM (n: nat)(i: ilistn T n):
Proper (eq(A:= Fin n) ==> comp) i.
Proof.
red. intros j k H. rewrite H.
reflexivity.
Qed.
(* proof that this translation is correct *)
Lemma ilist2list_all_elem_ilist_in_list:
forall(i: ilist T), iall (fun x => In x (ilist2list i)) i.
Proof.
intros i f.
apply (in_map (fcti i)), all_Fin_n_in_makeListFin.
Qed.
Lemma ilist2list_all_elem_list_in_ilist:
forall (i: ilist T)(f: Fin(lgti i)), In(fcti i f) (ilist2list i).
Proof.
intros i f.
apply (in_map (fcti i)), all_Fin_n_in_makeListFin.
Qed.
Lemma ilist2list_nth:
forall (t: T)(i: ilist T)(n: nat)(h: n < lgti i),
fcti i (code_Fin1 h) = nth n (ilist2list i) (fcti i (code_Fin1 h)).
Proof.
intros t i n h.
unfold ilist2list.
rewrite map_nth, <- nth_makeListFin.
reflexivity.
Qed.
Lemma ilist2list_nth':
forall (i: ilist T)(n: nat)(d: T)(h: n < lgti i),
fcti i (code_Fin1 h) = nth n (ilist2list i) d.
Proof.
intros i n d h.
unfold ilist2list.
rewrite (nth_indep _ _ (fcti i (code_Fin1 h))).
- rewrite map_nth, <- nth_makeListFin.
reflexivity.
- rewrite map_length, makeListFin_nb_elem_ok.
assumption.
Qed.
Lemma length_ilist2list: forall (i: ilist T),
length (ilist2list i) = lgti i.
Proof.
intros i.
unfold ilist2list.
rewrite map_length.
apply (makeListFin_nb_elem_ok).
Qed.
Lemma ilist2list_nth2:
forall (i: ilist T)(f: Fin (lgti i)),
fcti i f = nth (decode_Fin f) (ilist2list i) (fcti i f).
Proof.
intros i f.
unfold ilist2list.
rewrite map_nth, <- (code1_decode_Id f), nth_makeListFin, code1_decode_Id at 1.
reflexivity.
Qed.
Lemma ilist2list_nth3:
forall (t: T)(i: ilist T)(f: Fin (lgti i)),
fcti i f = nth (decode_Fin f) (ilist2list i) t.
Proof.
intros t i f.
assert (H: decode_Fin f < length (ilist2list i)).
{ rewrite length_ilist2list.
apply decode_Fin_inf_n. }
rewrite (nth_indep (ilist2list i) t (fcti i f) H).
apply ilist2list_nth2.
Qed.
Definition Fin0_T : forall (f: Fin 0), T.
Proof.
intro f ; inversion f.
Defined.
Definition list2Fin_T: forall (l: list T)(f: Fin (length l)), T.
Proof.
intros [| h l] f.
- inversion f.
- exact (nth (decode_Fin f) (h :: l) h).
Defined.
Definition list2ilist (l: list T) : ilist T := mkilist (list2Fin_T l).
Lemma lgti_length : forall (l: list T),
length l = lgti (list2ilist l).
Proof.
reflexivity.
Qed.
Lemma list2ilist_lgti_cons: forall (a: T) (l: list T),
S (lgti (list2ilist l)) = lgti (list2ilist (a :: l)).
Proof.
reflexivity.
Qed.
(* proof that this translation is correct *)
Lemma list2ilist_all_elem_ilist_in_list:
forall (l: list T),iall (fun x => In x l) (list2ilist l).
Proof.
intros l f.
elim (gt_eq_gt_dec (decode_Fin f) 0) ; [intros [H|e] |intros a] ; try inversion H ;
destruct l as [| h l].
- inversion f.
- left.
rewrite (decode_Fin_0_first _ e).
reflexivity.
- inversion f.
- right.
inversion a as [e | n H e];
refine (match e in (_ = df) return
In match df with 0 => h | S m => nth m l h end l
with refl_equal => _ end) ;
apply (nth_In _ _), lt_S_n ;
rewrite e ;
apply decode_Fin_inf_n.
Qed.
Definition inf_length_lgti: forall (n: nat)(l: list T)(h: n < length l),
n < lgti (list2ilist l).
Proof.
intros n l h.
rewrite lgti_length in h.
assumption.
Defined.
Lemma list2ilist_nth:
forall (n: nat)(l: list T)(h: n < length l)(t: T),
comp (nth n l t) (fcti (list2ilist l) (code_Fin1 (inf_length_lgti l h))).
Proof.
destruct n as [|n]; intros [| hd l] h t.
- inversion h.
- cbn.
rewrite code_Fin1_Sn_0.
reflexivity.
- inversion h.
- cbn in h.
simpl nth.
assert (h':= lt_le_S _ _ (lt_S_n _ _ h)).
assert (H: decode_Fin (code_Fin1_Sn (lt_n_Sm_le _ (length l) (inf_length_lgti (hd :: l) h)))= S n).
{ rewrite (code_Fin1_Sn_proofirr _ h').
revert n h h'.
induction (length l) as [| ll IHll] ; intros n h h'.
- inversion h'.
- rewrite code_Fin1_Sn_S.
cbn.
f_equal.
set (h1 := le_S_n _ _ h').
destruct n as [|n].
+ rewrite code_Fin1_Sn_0.
reflexivity.
+ apply (IHll _ (lt_S_n _ _ h)).
}
cbn.
rewrite H.
apply (nth_indep_comp _ l t hd h').
Qed.
Lemma list2ilist_nth2:
forall (l: list T)(f: Fin (lgti (list2ilist l)))(t: T),
nth (decode_Fin f) l t = fcti (list2ilist l) f.
Proof.
intros [|hd l] f t.
- inversion f.
- cbn.
cbn in f.
elim (gt_eq_gt_dec (decode_Fin f) 0); [intros [H|e] | intros a].
+ inversion H.
+ rewrite e; reflexivity.
+ inversion a as [e| n H e] ; assert (h:= decode_Fin_inf_n f) ; rewrite <- e in h.
* destruct l as [|hhd l].
-- apply False_rec, (lt_irrefl _ h).
-- reflexivity.
* apply (nth_indep_comp _ _ _ _ (lt_S_n _ _ h)).
Qed.
Lemma lgti_list2ilist: forall (l: list T),
lgti (list2ilist l) = length l.
Proof.
reflexivity.
Qed.
Lemma list2ilist_ilist2list_id : forall (i: ilist T),
ilist_rel eq (list2ilist (ilist2list i)) i.
Proof.
intros i.
assert (h : lgti (list2ilist (ilist2list i)) = lgti i).
{ rewrite lgti_list2ilist.
apply length_ilist2list. }
apply (is_ilist_rel _ _ _ h).
intro f.
rewrite <- (list2ilist_nth2 _ f (fcti _ f)), (decode_Fin_match' f h), <- (ilist2list_nth3 _).
reflexivity.
Qed.
Lemma list2Fin_T_first: forall (l: list T)(a: T)(x: Fin (length l)),
list2Fin_T (a :: l) (first (length l)) = a.
Proof.
reflexivity.
Qed.
Lemma list2Fin_T_succ: forall (l: list T)(a: T)(x: Fin (length l)),
(list2Fin_T (a :: l) (succ x)) = (list2Fin_T l x).
Proof.
cbn.
induction l as [|hd l]; intros a f.
- inversion f.
- cbn.
set (d := decode_Fin f) ; assert (h1 := decode_Fin_inf_n f : d < (length (hd :: l))).
change (match d with 0 => hd | S m => nth m l a end = match d with 0 => hd | S m => nth m l hd end ).
revert h1 ; generalize d; clear d ; intros [|d] h1.
+ reflexivity.
+ apply nth_indep, lt_S_n, h1.
Qed.
Lemma list2Fin_T_succ_map:
forall (l: list T)(a: T)(lf: list (Fin (length l))),
map (fun x : Fin (length l) => list2Fin_T l x) lf =
map (fun x : Fin (length l) => list2Fin_T (a :: l) (succ x)) lf.
Proof.
intros l a ; induction lf as [| hd lf IH].
- reflexivity.
- change (list2Fin_T l hd :: map (fun x : Fin (length l) => list2Fin_T l x) lf = nth (decode_Fin hd) l a
:: map (fun x : Fin (length l) => list2Fin_T (a :: l) (succ x)) lf).
rewrite IH.
rewrite (list2ilist_nth2 l).
reflexivity.
Qed.
Lemma ilist2list_list2ilist_id: forall (l: list T), ilist2list (list2ilist l) = l.
Proof.
induction l as [|hd l].
- reflexivity.
- rewrite <- IHl at 2.
unfold ilist2list, list2ilist.
change (hd :: map (list2Fin_T (hd :: l)) (map (fun x => @succ _ x) (makeListFin (length l))) =
hd :: map (list2Fin_T l) (makeListFin (length l))).
rewrite map_map.
rewrite <- (list2Fin_T_succ_map l hd).
reflexivity.
Qed.
Definition ilistM (n: nat)(i: ilistn T n):
Proper (eq(A:= Fin n) ==> comp) i.
Proof.
red. intros j k H. rewrite H.
reflexivity.
Qed.
Definition ifilterB (P: T -> bool)(i: ilist T) :=
list2ilist (filter P (ilist2list i)).
End Bij_ilist_list.
Lemma list2Fin_T_map : forall (T U: Set)(l: list T)(f: T -> U)(i: Fin (length (map f l)))
(h: length (map f l) = length l),
list2Fin_T (map f l) i = f (list2Fin_T l (rewriteFins h i)).
Proof.
intros T U l f i h.
destruct l as [|t l].
- inversion i.
- cbn.
rewrite <- decode_Fin_match'.
elim (zerop (decode_Fin i)) ; intros a.
+ rewrite (decode_Fin_0_first _ a).
reflexivity.
+ inversion a ;
apply map_nth.
Qed.
Lemma list2Fin_T_map_bis : forall (T U: Set)(l: list T)(f: T -> U)(i: Fin (length (map f l))),
list2Fin_T (map f l) i = f (list2Fin_T l (rewriteFins (map_length f l) i)).
Proof.
intros T U l f i.
destruct l as [|t l].
- inversion i.
- cbn in *|-*.
rewrite <- decode_Fin_match'.
set (n := decode_Fin i) ;
change (match n with | 0 => f t | S m => nth m (map f l) (f t) end = f match n with
| 0 => t | S m => nth m l t end).
destruct n as [|n].
+ reflexivity.
+ apply map_nth.
Qed.
Lemma list2Fin_T_makeListFin: forall (T: Set)(l: ilist T)(i: Fin (length (makeListFin (lgti l))))
(h: length (makeListFin (lgti l)) = lgti l),
list2Fin_T (makeListFin (lgti l)) i = rewriteFins h i.
Proof.
intros T [[|n] l] i h ; cbn in *|-* ; clear l ;
apply decode_Fin_unique ; unfold rewriteFins ; rewrite <- decode_Fin_match.
- inversion i.
- set (d := decode_Fin i) ; assert (h1 := decode_Fin_inf_n i : d< _) ; assert (h2:= refl_equal _: d = decode_Fin i).
revert h1 h2 ; generalize d ; clear d ; intros [|d] h1 h2.
+ reflexivity.
+ assert (h3 : decode_Fin i > 0).
{ rewrite <- h2.
apply lt_0_Sn. }
assert (i' := get_cons i h3).
rewrite map_length, makeListFin_nb_elem_ok in i'.
apply lt_S_n in h1.
rewrite (nth_indep _ (first n) (succ i') h1) , map_nth.
rewrite map_length, makeListFin_nb_elem_ok in h1.
rewrite <- (nth_makeListFin_def h1).
change (S (decode_Fin (code_Fin1 h1)) = S d).
f_equal.
apply decode_code1_Id.
Qed.
Lemma list2Fin_T_makeListFin_bis: forall (T: Set)(l: ilist T)(i: Fin (length (makeListFin (lgti l)))),
list2Fin_T (makeListFin (lgti l)) i = rewriteFins (makeListFin_nb_elem_ok (lgti l)) i.
Proof.
intros T l i.
destruct l as [n l] ; cbn in *|-* ; clear l.
destruct n as [|n].
- inversion i.
- elim (zerop (decode_Fin i)) ; intros a.
+ rewrite (decode_Fin_0_first _ a).
simpl.
apply decode_Fin_unique.
unfold rewriteFins ; rewrite <- decode_Fin_match.
reflexivity.
+ simpl in *|-*.
inversion a as [H|m _ H] ;
[destruct n as [|n] | clear a ; revert m H; induction n as [|n IH]; intros m H] ;
try (rewrite (Fin_first_1 i) in H ; inversion H) ;
apply decode_Fin_unique ;
unfold rewriteFins ; rewrite <- decode_Fin_match.
* assumption.
* destruct m as [|m].
-- assumption.
-- simpl.
rewrite (nth_indep _ (first (S n)) (succ (first n))).
++ rewrite map_nth.
assert (H3 : decode_Fin i >= S (S m)).
{ rewrite H.
apply le_refl. }
simpl.
simpl in i.
set (i' := get_cons i (lt_le_trans _ _ _ (lt_0_Sn (S m)) H3) :
Fin (S (length (List.map (@succ (S n)) (List.map (@succ n) (makeListFin n)))))).
assert (H4 := decode_Fin_unique i (succ i') (decode_Fin_get_cons _ _)).
set (i'' := rewriteFins (eq_S _ _
(map_length (@succ (S n)) (List.map (@succ n) (makeListFin n)))) i').
assert (H5 : decode_Fin i'' = decode_Fin i').
{ unfold i'', rewriteFins ; apply (sym_eq (decode_Fin_match _ _ )). }
rewrite (IH i'').
** unfold rewriteFins ; rewrite <- decode_Fin_match.
transitivity (S(decode_Fin i')).
--- f_equal.
exact H5.
--- cbn.
rewrite H4.
reflexivity.
** symmetry.
transitivity (decode_Fin i').
--- exact H5.
--- apply eq_add_S; rewrite H, H4; reflexivity.
++ do 2 apply lt_S_n ; rewrite H.
apply decode_Fin_inf_n.
Qed.
Lemma list2ilist_app_lgti (T: Set)(t: T)(l1 l2 : list T) :
S (lgti (list2ilist (l1 ++ l2))) = lgti (list2ilist (l1 ++ t :: l2)).
Proof.
cbn.
do 2 rewrite app_length.
apply plus_n_Sm.
Qed.
Lemma list2ilist_ilist2list_id_bis(T: Set) : forall (i: ilist T),
ilist_rel eq i (list2ilist (ilist2list i)).
Proof.
intros i.
assert (h : lgti i = lgti (list2ilist (ilist2list i))).
{ rewrite lgti_list2ilist.
apply sym_eq, length_ilist2list. }
apply (is_ilist_rel _ _ _ h).
intro f.
cbn.
unfold ilist2list.
rewrite list2Fin_T_map_bis, list2Fin_T_makeListFin_bis.
f_equal.
apply decode_Fin_unique.
repeat rewrite <- decode_Fin_match'.
reflexivity.
Qed.
Section iappend.
Definition rightFin (n1 n2 : nat)(i: Fin (n1 + n2))(h: n1 <= decode_Fin i) : Fin n2.
Proof.
assert (h1 : decode_Fin i - n1 < n2).
{ apply (plus_lt_reg_l _ _ n1).
rewrite <- le_plus_minus ; try assumption.
apply decode_Fin_inf_n. }
exact (code_Fin1 h1).
Defined.
Lemma rightFin_decode_Fin : forall (n1 n2 :nat)(i: Fin (n1 + n2))(h: n1 <= decode_Fin i),
decode_Fin (rightFin _ _ h) + n1 = decode_Fin i.
Proof.
intros n1 n2 i h.
unfold rightFin.
rewrite decode_code1_Id.
rewrite plus_comm.
apply sym_eq, le_plus_minus,h.
Qed.
Definition iappend (X: Set)(l1 l2 : ilist X) : ilist X.
Proof.
destruct l1 as [n1 l1] ; destruct l2 as [n2 l2].
apply (existT (ilistn X) (n1 + n2)).
intro i.
elim (le_lt_dec n1 (decode_Fin i)); intros a.
- exact (l2 (rightFin _ _ a)).
- exact (l1 (code_Fin1 a)).
Defined.
Lemma iappend_lgti (X: Set)(l1 l2 : ilist X) : lgti (iappend l1 l2) = lgti l1 + lgti l2.
Proof.
destruct l1 as [n1 l1] ; destruct l2 as [n2 l2].
reflexivity.
Qed.
Lemma iappend_left (X: Set)(l1 l2 : ilist X)(i: Fin (lgti (iappend l1 l2)))
(h: decode_Fin i < lgti l1) : fcti (iappend l1 l2) i = fcti l1 (code_Fin1 h).
Proof.
destruct l1 as [n1 l1].
destruct l2 as [n2 l2].
cbn in *|-*.
unfold sumbool_rec, sumbool_rect.
elim (le_lt_dec n1 (decode_Fin i)) ; intros a.
- apply False_rec, (lt_irrefl n1), (le_lt_trans _ (decode_Fin i)) ; assumption.
- f_equal.
apply decode_Fin_unique.
do 2 rewrite decode_code1_Id.
reflexivity.
Qed.
Lemma iappend_right (X: Set)(l1 l2 : ilist X)(i: Fin (lgti(iappend l1 l2)))
(h: lgti l1 <= decode_Fin (rewriteFins (iappend_lgti _ _) i)) :
fcti (iappend l1 l2) i = fcti l2 (rightFin _ (rewriteFins (iappend_lgti _ _) i) h).
Proof.
destruct l1 as [n1 l1].
destruct l2 as [n2 l2].
cbn in *|-*.
unfold sumbool_rec, sumbool_rect.
elim (le_lt_dec n1 (decode_Fin i)) ; intros a.
- f_equal.
apply decode_Fin_unique.
unfold rightFin ; do 2 rewrite decode_code1_Id.
rewrite <- decode_Fin_match'.
reflexivity.
- apply False_rec, (lt_irrefl n1), (le_lt_trans _ (decode_Fin i)).
+ rewrite <- decode_Fin_match' in h.
assumption.
+ assumption.
Qed.
Lemma iappend_append (X: Set)(l1 l2 : ilist X) :
ilist_rel eq (iappend l1 l2) (list2ilist ((ilist2list l1) ++ (ilist2list l2))).
Proof.
assert (h1 : lgti (iappend l1 l2) = lgti (list2ilist (ilist2list l1 ++ ilist2list l2))).
{ rewrite lgti_list2ilist, app_length, length_ilist2list, length_ilist2list.
apply iappend_lgti. }
apply (is_ilist_rel _ _ _ h1).
intros i.
rewrite <- (list2ilist_nth2 _ _ (fcti (iappend l1 l2) i)), <- decode_Fin_match'.
elim (le_lt_dec (lgti l1) (decode_Fin i)) ; intros a.
- assert (b := a).
rewrite (decode_Fin_match' i (iappend_lgti l1 l2)) in b.
rewrite app_nth2 ; rewrite length_ilist2list ; try assumption.
rewrite (iappend_right _ _ _ b).
unfold rightFin, eq_ind, eq_rect.
set (h2 := plus_lt_reg_l _ _ _ (match le_plus_minus (lgti l1)
(decode_Fin (rewriteFins (iappend_lgti l1 l2) i)) b in _=y return y < lgti l1 + lgti l2 with
eq_refl => decode_Fin_inf_n (rewriteFins (iappend_lgti l1 l2) i) end)).
rewrite (decode_Fin_match' i (iappend_lgti l1 l2)).
apply ilist2list_nth'.
- rewrite (iappend_left _ _ _ a).
rewrite app_nth1.
+ apply ilist2list_nth'.
+ rewrite length_ilist2list ; assumption.
Qed.
Lemma append_iappend (T: Set)(l1 l2 : ilist T) :
ilist2list (iappend l1 l2) = (ilist2list l1) ++ (ilist2list l2).
Proof.
apply eq_nth_cor'.
- rewrite app_length.
do 3 rewrite length_ilist2list.
apply iappend_lgti.
- intros n d h1.
rewrite length_ilist2list in h1.
rewrite <- (ilist2list_nth' _ _ h1).
elim (le_lt_dec (lgti l1) n) ; intros b.
+ rewrite app_nth2; rewrite length_ilist2list; try assumption.
assert (h2 : n- lgti l1 < lgti l2).
* rewrite <- (minus_plus (lgti l1) (lgti l2)).
rewrite <- iappend_lgti.
apply le_S_gt.
rewrite minus_Sn_m ; try assumption.
apply minus_le_compat_r.
apply gt_le_S.
assumption.
* rewrite <- (ilist2list_nth' _ _ h2).
assert (h3 : lgti l1 ≤ decode_Fin (rewriteFins (iappend_lgti l1 l2) (code_Fin1 h1))).
{ rewrite <- decode_Fin_match', decode_code1_Id.
assumption. }
rewrite (iappend_right _ _ _ h3).
f_equal.
apply decode_Fin_unique.
unfold rightFin ; do 2 rewrite decode_code1_Id.
rewrite <- decode_Fin_match'.
rewrite decode_code1_Id.
reflexivity.
+ assert (c := b) ; rewrite <- (decode_code1_Id h1) in c.
rewrite (iappend_left _ _ _ c).
rewrite app_nth1.
* rewrite <- (ilist2list_nth' _ _ b).
f_equal.
apply decode_Fin_unique.
do 3 rewrite decode_code1_Id.
reflexivity.
* rewrite length_ilist2list; try assumption.
Qed.
Lemma iappend_ilist_rel (X: Set)(RelX : relation X)(ls ls' rs rs': ilist X):
ilist_rel RelX ls ls' -> ilist_rel RelX rs rs' ->
ilist_rel RelX (iappend ls rs) (iappend ls' rs').
Proof.
intros [h1 H1] [h2 H2].
assert (h3 : lgti (iappend ls rs) = lgti (iappend ls' rs')).
{ do 2 rewrite iappend_lgti.
rewrite h1, h2.
reflexivity. }
apply (is_ilist_rel _ _ _ h3).
intro i.
elim (le_lt_dec (lgti ls) (decode_Fin i)) ; intros a ; assert (b := a);
rewrite (decode_Fin_match' i h3), h1 in b.
- rewrite (decode_Fin_match' i (iappend_lgti ls rs)) in a.
rewrite (iappend_right _ _ _ a).
rewrite (decode_Fin_match' _ (iappend_lgti ls' rs')) in b.
rewrite (iappend_right _ _ _ b).
assert (H3 : rightFin (lgti rs') (rewriteFins (iappend_lgti ls' rs') (rewriteFins h3 i)) b =
rewriteFins h2 (rightFin (lgti rs) (rewriteFins (iappend_lgti ls rs) i) a)).
{ apply decode_Fin_unique.
rewrite <- decode_Fin_match'.
apply (plus_reg_l _ _ (lgti ls')).
rewrite plus_comm, rightFin_decode_Fin, <- decode_Fin_match', <- decode_Fin_match'.
rewrite <- h1.
rewrite plus_comm, rightFin_decode_Fin, <- decode_Fin_match'.
reflexivity.
}
rewrite H3.
apply H2.
- rewrite (iappend_left _ _ _ a).
rewrite (iappend_left _ _ _ b).
assert (H3 : code_Fin1 b = rewriteFins h1 (code_Fin1 a)) by treatFinPure.
rewrite H3.
apply H1.
Qed.
End iappend.
Section ilist_rel_ListEq.
(* equivalence between ilist_rel and ListEq *)
Lemma ilist_rel_ListEq_eq:
forall (T: Set)(eqT: relation T)(eqTR: Equivalence eqT)(i1 i2: ilist T),
ilist_rel eqT i1 i2 <-> ListEq eqT (ilist2list i1) (ilist2list i2).
Proof.
intros T eqT eqTRel i1 i2 ; split ; intros H ;
destruct i1 as [n i1]; destruct i2 as [n2 i2].
- inversion H as [e h] ; cbn in e, h.
assert (e' := e) ; revert i2 e H h ; rewrite <- e' ; intros i2 e H h ; clear e' n2.
assert (h' : forall f, eqT (i1 f) (i2 f)).
{ intro f.
rewrite (decode_Fin_unique _ _ (decode_Fin_match f e) : f = rewriteFins e f) at 2 ; apply h. }
apply ListEq_map_f_g, h'.
- unfold ilist2list in H.
cbn in H.
assert (H1 := ListEq_length H).
do 2 (rewrite map_length, makeListFin_nb_elem_ok in H1).
revert i2 H ; rewrite <- H1 ; intros i2 H ; clear H1 n2.
apply (is_ilist_rel _ _ _ (refl_equal n : lgti (existT _ _ i1) = lgti (existT _ _ i2))) ; cbn.
intro f.
apply (ListEq_eq eqTRel _ _ _ H _ (all_Fin_n_in_makeListFin f)).
Qed.
Lemma ListEq_ilist_rel_eq:
forall (T: Set)(eqT: relation T)(eqTR: Equivalence eqT)(l1 l2: list T),
ilist_rel eqT (list2ilist l1) (list2ilist l2) <-> ListEq eqT l1 l2.
Proof.
intros T eqT eqTR l1 l2.
rewrite <- (ilist2list_list2ilist_id l1), <- (ilist2list_list2ilist_id l2) at 2.
apply (ilist_rel_ListEq_eq eqTR).
Qed.
Lemma map_ext_in (T U : Set)(f1 f2 : T -> U)(l: list T) :
map f1 l = map f2 l -> forall t, In t l -> f1 t = f2 t.
Proof.
intros H1 t H2.
induction l as [|tt l IH].
- inversion H2.
- inversion H1.
destruct H2 as [H2|H2].
+ rewrite <- H2.
assumption.
+ apply IH ; assumption.
Qed.
Lemma ilist_rel_eq:
forall (T: Set)(i1 i2: ilist T), ilist_rel eq i1 i2 <-> ilist2list i1 = ilist2list i2.
Proof.
intros T i1 i2 ; split ; intros H ;
destruct i1 as [n i1]; destruct i2 as [n2 i2].
- inversion H as [e h] ; cbn in e, h.
assert (e' := e) ; revert i2 e H h ; rewrite <- e' ; intros i2 e H h ; clear e' n2.
unfold ilist2list.
cbn.
apply map_ext.
intro f.
rewrite (decode_Fin_unique _ _ (decode_Fin_match f e) : f = rewriteFins e f) at 2 ; apply h.
- unfold ilist2list in H.
simpl in H.
fold (mkilist i1) (mkilist i2).
assert (H1 : n = n2).
{ rewrite <- (makeListFin_nb_elem_ok n), <- (makeListFin_nb_elem_ok n2).
rewrite <- (map_length i1 (makeListFin n) : length (map _ _) = _), <- (map_length i2 (makeListFin n2)).
rewrite H.
reflexivity. }
revert i2 H ; rewrite <- H1 ; intros i2 H ; clear H1 n2.
apply (is_ilist_rel _ _ _ (refl_equal n : lgti (mkilist i1) = lgti (mkilist i2))) ; cbn.
intro f.
apply (map_ext_in _ _ _ H).
apply all_Fin_n_in_makeListFin.
Qed.
End ilist_rel_ListEq.
Add Parametric Morphism (T: Set)(eqT: relation T)
(eqTR: Equivalence eqT): (ilist2list(T:= T))
with signature (ilist_rel eqT) ==> (ListEq eqT)
as ilist2listM.
Proof.
intros x y h.
apply (ilist_rel_ListEq_eq eqTR x y).
assumption.
Qed.
Add Parametric Morphism (T: Set)(eqT: relation T)
(eqTR: Equivalence eqT): (list2ilist(T:= T))
with signature (ListEq eqT) ==> (ilist_rel eqT)
as list2ilistM.
Proof.
intros x y h.
apply (ListEq_ilist_rel_eq eqTR x y).
assumption.
Qed.
Lemma map_imap_ilist_list: forall (T U: Set)(i: ilist T)(f: T -> U)
(compT: relation T)(compTRel: Equivalence compT)
(compU: relation U)(compURel: Equivalence compU)
(fM: Proper (compT ==> compU) f),
ilist_rel compU (imap f i) (list2ilist (map f (ilist2list i))).
Proof.
intros T U i f compT compTRel compU compURel fM.
assert (H: lgti (imap f i) = lgti (list2ilist (map f (ilist2list i)))).
{ cbn.
rewrite map_length.
apply (sym_eq (length_ilist2list i)). }
apply (is_ilist_rel _ _ _ H); intro fi.
destruct i as [ [|n] i].
- inversion fi.
- simpl.
rewrite <- (code1_decode_Id fi) at 1.
elim (zerop (decode_Fin (rewriteFins H fi))); intros a.
+ refine (match (sym_eq a) in (_ = df) return
compU _ match df with 0 => f (i (first n)) | S m => _ end
with refl_equal => _ end).
rewrite <- (decode_Fin_match' _ H) in a.
rewrite code1_decode_Id, (decode_Fin_0_first _ a).
reflexivity.
+ inversion a as [e | m H1 e];
rewrite <- (decode_Fin_match' _ H) in e ; rewrite code1_decode_Id.
* destruct n as [|n].
-- rewrite (Fin_first_1 fi) in e.
inversion e.
-- rewrite (refl_equal _: 1 = decode_Fin (succ (first n))) in e.
apply decode_Fin_unique in e.
cbn ; rewrite e; reflexivity.
* assert (H2 :m < n).
{ apply (lt_S_n m n).
rewrite e.
apply decode_Fin_inf_n. }
rewrite (map_map_nth_comp compTRel compURel i fM).
destruct n as [|n].
-- inversion H2.
-- rewrite (nth_indep _ (first (S n)) (succ (first n))), map_nth, <- (nth_makeListFin_def H2).
++ rewrite (decode_Fin_unique _ _ (trans_eq
(eq_S _ _ (decode_code1_Id H2) : decode_Fin (succ _) = _) e)).
reflexivity.
++ rewrite map_length, makeListFin_nb_elem_ok.
assumption.
Qed.
Lemma list2Fin_T_succ_map_f:
forall (T U: Set) (l: list T)(a: T)(lf: list (Fin (length l)))(f: T -> U)
(compT: relation T)(compU: relation U)(compURel: Equivalence compU)
(fM: Proper (compT ==> compU) f),
ListEq compU (map (fun x : Fin (length l) => f( list2Fin_T l x)) lf)
(map (fun x : Fin (length l) => f(list2Fin_T (a :: l) (succ x))) lf).
Proof.
intros T U l a lf f compT compU compURel fM.
induction lf as [| hd lf IHlf].
- apply ListEq_nil.
- apply ListEq_cons.
+ rewrite list2Fin_T_succ.
reflexivity.
+ apply IHlf.
Qed.
Lemma imap_cons: forall (T U: Set)(a: T)(l: list T)(f: T -> U)
(compT: relation T)(compTRel: Equivalence compT)
(compU: relation U)(compURel: Equivalence compU)
(fM: Proper (compT ==> compU) f),
ListEq compU (ilist2list (imap f (list2ilist (a :: l))))
(f a :: (ilist2list (imap f (list2ilist l)))).
Proof.
intros T U a l f compT compTRel compU compURel fM .
rewrite (map_imap_ilist_list _ _ _ fM), ilist2list_list2ilist_id.
cbn.
apply ListEq_cons.
- reflexivity.
- do 2 (rewrite (map_map_ListEq compURel)).
unfold ilist2list, list2ilist.
cbn.
rewrite (list2Fin_T_succ_map_f l a _ compURel fM).
reflexivity.
Qed.
Lemma imap_map_list_ilist: forall (T U: Set)(l: list T)(f: T -> U)
(compT: relation T)(compTRel: Equivalence compT)
(compU: relation U)(compURel: Equivalence compU)
(fM: Proper (compT ==> compU) f),
ListEq compU (map f l) (ilist2list (imap f (list2ilist l))).
Proof.
intros T U l f compT compTRel compU compURel fM.
induction l as [|hd l IHl].
- reflexivity.
- rewrite (imap_cons _ _ _ _ _).
apply ListEq_cons.
+ reflexivity.
+ assumption.
Qed.
Definition ifold_left (A B : Set) (f : A → B → A)
(l : ilist B) (a0 : A) : A :=
fold_left f (ilist2list l) a0.
Section manip_ilist.
(* various definitions and functions for ilist manipulation *)
Definition singleton(T: Set) (t: T): ilist T :=