]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/basics/vectors.ma
basic lemmas
[helm.git] / matita / matita / lib / basics / vectors.ma
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic   
3     ||A||  Library of Mathematics, developed at the Computer Science 
4     ||T||  Department of the University of Bologna, Italy.           
5     ||I||                                                            
6     ||T||  
7     ||A||  
8     \   /  This file is distributed under the terms of the       
9      \ /   GNU General Public License Version 2   
10       V_____________________________________________________________*)
11
12 include "basics/finset.ma".
13
14 record Vector (A:Type[0]) (n:nat): Type[0] ≝ 
15 { vec :> list A;
16   len: length ? vec = n
17 }.
18
19 lemma Vector_eq : ∀A,n,v1,v2.
20   vec A n v1 = vec A n v2 → v1 = v2.
21 #A #n * #l1 #H1 * #l2 #H2 #eql1l2 generalize in match H1; 
22 -H1 >eql1l2 //
23 qed.
24
25 definition vec_tail ≝ λA.λn.λv:Vector A n.
26 mk_Vector A (pred n) (tail A v) ?.
27 >length_tail >(len A n v) //
28 qed.
29
30 definition vec_cons ≝ λA.λa.λn.λv:Vector A n.
31 mk_Vector A (S n) (cons A a v) ?.
32 normalize >(len A n v) //
33 qed.
34
35 definition vec_hd ≝ λA.λn.λv:Vector A (S n).
36 hd A v ?. elim v * [normalize #H destruct | #a #H #eq @a] 
37 qed.
38
39 lemma vec_expand: ∀A,n,v. 
40  v = vec_cons A (vec_hd A n v) n (vec_tail A (S n) v).
41 #A #n * #l cases l [normalize in ⊢ (%→?); #H destruct  |//]
42 qed.
43
44 definition vec_append ≝ λA.λn1,n2.λv1:Vector A n1.λv2: Vector A n2.
45 mk_Vector A (n1+n2) (v1@v2).
46
47 definition vec_map ≝ λA,B.λf:A→B.λn.λv:Vector A n.
48 mk_Vector B n (map ?? f v) 
49   (trans_eq … (length_map …) (len A n v)).
50
51 (* mapi: map with index to move in list.ma *)
52 let rec change_vec (A:Type[0]) (n:nat) on n ≝
53 match n return λn0.∀v:Vector A n0.A→nat→Vector A n0 with
54 [ O ⇒ λv,a,i.v
55 | S m ⇒ λv,a,i.
56   match i with
57   [ O ⇒ vec_cons A a m (vec_tail … v) 
58   | S j ⇒ vec_cons A (vec_hd A m v) m (change_vec A m (vec_tail … v) a j)
59   ]
60 ].
61
62 lemma nth_change_vec : ∀A,i,n,v,a,d. i < n →
63   nth i ? (change_vec A n v a i) d = a.
64 #A #i elim i
65   [#n #v #a #d #ltOn lapply v -v @(lt_O_n_elim n ltOn ??) //
66   |#m #Hind #n #v #a #d #Hlt 
67    lapply Hlt lapply v @(lt_O_n_elim … (ltn_to_ltO … Hlt)) 
68    #p #v #ltmp @Hind @le_S_S_to_le // 
69   ]
70 qed.
71
72 lemma nth_change_vec_neq : ∀A,j,i,n,v,a,d. i ≠ j →
73   nth j ? (change_vec A n v a i) d = nth j ? v d.
74 #A #j elim j
75   [#i * // #n #v #a #d cases i
76     [#H @False_ind @(absurd ?? H) //
77     |#i0 #_ >(vec_expand ?? v) in ⊢ (???%); //
78     ] 
79   |#m #Hind #i * // cases i // #i0 #n #v #a #d #neqim 
80    whd in ⊢(??%?); whd in match (tail ??); >Hind
81     [>(vec_expand ??v) in ⊢ (???%); // |@(not_to_not … neqim) // ]
82   ]
83 qed.
84
85 lemma change_vec_cons_tail :∀A,n,vA,a,b,i.
86   change_vec A (S n) (vec_cons ? a n vA) b (S i) =
87   vec_cons ? a n (change_vec A n vA b i).
88 #A #n #vA cases vA //
89 qed.
90
91 lemma vector_nil: ∀A.∀v:Vector A 0. 
92   v = mk_Vector A 0 (nil A) (refl ??).
93 #A * * // #a #tl normalize #H destruct
94 qed. 
95
96 lemma nth_default : ∀A,i,n.∀v:Vector A n.∀d1,d2. i < n →
97   nth i ? v d1 = nth i ? v d2.
98 #A #i elim i -i
99   [#n #v #d1 #d2 #ltOn lapply v @(lt_O_n_elim … ltOn)
100    -v #m #v >(vec_expand … v) //
101   |#i #Hind #n #v #d1 #d2 #ltn lapply ltn lapply v @(lt_O_n_elim … (ltn_to_ltO … ltn))
102    -v -ltn #m #v #ltim >(vec_expand … v) @(Hind m (vec_tail A (S m) v) d1 d2 ?) 
103    @le_S_S_to_le //
104   ]
105 qed.
106   
107 lemma eq_vec: ∀A,n.∀v1,v2:Vector A n.∀d. 
108   (∀i. i < n → nth i A v1 d = nth i A v2 d) → v1 = v2.
109 #A #n elim n -n 
110   [#v1 #v2 #H >(vector_nil A v1) >(vector_nil A v2) //
111   |#n #Hind #v1 #v2 #d #H >(vec_expand … v1) >(vec_expand … v2)
112    >(Hind (vec_tail … v1) (vec_tail … v2) d)
113     [cut (vec_hd A n v1 = vec_hd A n v2) //
114      cut (∀i,d1,d2. i < S n → nth i A v1 d1 = nth i A v2 d2)
115        [#i #d1 #d2 #Hi >(nth_default ????? d) // >(nth_default ???? d2 d) // @H //]
116      -H #H @(H 0) //  
117     |#i #ltin @(H (S i)) @le_S_S //
118     ]
119   ]
120 qed.
121
122 (*
123 lemma length_make_listi: ∀A,a,n,i. 
124   |make_listi A a n i| = n.
125 #A #a #n elim n // #m #Hind normalize //
126 qed.
127 definition change_vec ≝ λA,n,v,a,i.
128   make_veci A (λj.if (eqb i j) then a else (nth j A v a)) n 0.
129
130 let rec mapi (A,B:Type[0]) (f: nat → A → B) (l:list A) (i:nat) on l: list B ≝
131  match l with 
132  [ nil ⇒ nil ? 
133  | cons x tl ⇒ f i x :: (mapi A B f tl (S i))].
134  
135 lemma length_mapi: ∀A,B,l.∀f:nat→A→B.∀i. 
136   |mapi ?? f l i| = |l|.
137 #A #B #l #f elim l // #a #tl #Hind normalize //
138 qed.
139
140 let rec make_listi (A:Type[0]) (a:nat→A) (n,i:nat) on n : list A ≝
141 match n with
142 [ O ⇒ [ ]
143 | S m ⇒ a i::(make_listi A a m (S i))
144 ].
145
146 lemma length_make_listi: ∀A,a,n,i. 
147   |make_listi A a n i| = n.
148 #A #a #n elim n // #m #Hind normalize //
149 qed.
150
151 definition vec_mapi ≝ λA,B.λf:nat→A→B.λn.λv:Vector A n.λi.
152 mk_Vector B n (mapi ?? f v i) 
153   (trans_eq … (length_mapi …) (len A n v)).
154   
155 definition make_veci ≝ λA.λa:nat→A.λn,i.
156 mk_Vector A n (make_listi A a n i) (length_make_listi A a n i).
157 *)
158
159 let rec pmap A B C (f:A→B→C) l1 l2 on l1 ≝
160    match l1 with
161    [ nil ⇒ nil C
162    | cons a tlA ⇒ 
163      match l2 with
164      [nil ⇒ nil C
165      |cons b tlB ⇒ (f a b)::pmap A B C f tlA tlB
166      ]
167    ].
168
169 lemma length_pmap: ∀A,B,C.∀f:A→B→C.∀l1,l2.
170 length C (pmap  A B C f l1 l2) = 
171   min (length A l1) (length B l2).
172 #A #B #C #f #l1 elim l1 // #a #tlA #Hind #l2 cases l2 //
173 #b #tlB lapply (Hind tlB) normalize 
174 cases (true_or_false (leb (length A tlA) (length B tlB))) #H >H
175 normalize //
176 qed.
177
178 definition pmap_vec ≝ λA,B,C.λf:A→B→C.λn.λvA:Vector A n.λvB:Vector B n.
179 mk_Vector C n (pmap A B C f vA vB) ?.
180 >length_pmap >(len A n vA) >(len B n vB) normalize 
181 >(le_to_leb_true … (le_n n)) //
182 qed.
183
184 lemma pmap_vec_cons :∀A,B,C,f,n,vA,vB,a,b.
185   pmap_vec A B C f (S n) (vec_cons ? a n vA) (vec_cons ? b n vB) =
186   vec_cons ? (f a b) n (pmap_vec A B C f n vA vB).
187 // qed.
188
189 lemma pmap_change : ∀A,B,C,f,n,vA,vB,a,b,i.
190   pmap_vec A B C f n (change_vec ? n vA a i) (change_vec ? n vB b i) =
191   change_vec ? n (pmap_vec A B C f n vA vB) (f a b) i.
192 #A #B #C #f #n elim n //
193 #m #Hind #vA #vB #a #b >(vec_expand … vA) >(vec_expand … vB) * //
194 #i >pmap_vec_cons >pmap_vec_cons >change_vec_cons_tail @eq_f @Hind 
195 qed.