1 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
2 | (* Vector.ma: Fixed length polymorphic vectors, and routine operations on *) |
---|
3 | (* them. *) |
---|
4 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
5 | |
---|
6 | include "Util.ma". |
---|
7 | |
---|
8 | include "Nat.ma". |
---|
9 | include "List.ma". |
---|
10 | include "Cartesian.ma". |
---|
11 | include "Maybe.ma". |
---|
12 | include "Plogic/equality.ma". |
---|
13 | |
---|
14 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
15 | (* The datatype. *) |
---|
16 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
17 | |
---|
18 | ninductive Vector (A: Type[0]): Nat → Type[0] ≝ |
---|
19 | Empty: Vector A Z |
---|
20 | | Cons: ∀n: Nat. A → Vector A n → Vector A (S n). |
---|
21 | |
---|
22 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
23 | (* Syntax. *) |
---|
24 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
25 | |
---|
26 | notation "[[ list0 x sep ; ]]" |
---|
27 | non associative with precedence 90 |
---|
28 | for ${fold right @'vnil rec acc @{'vcons $x $acc}}. |
---|
29 | |
---|
30 | interpretation "Vector vnil" 'vnil = (Empty ?). |
---|
31 | interpretation "Vector vcons" 'vcons hd tl = (Cons ? ? hd tl). |
---|
32 | interpretation "Vector cons" 'cons hd tl = (Cons ? ? hd tl). |
---|
33 | |
---|
34 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
35 | (* Lookup. *) |
---|
36 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
37 | |
---|
38 | naxiom succ_less_than_injective: |
---|
39 | ∀m, n: Nat. |
---|
40 | less_than_p (S m) (S n) → m < n. |
---|
41 | |
---|
42 | naxiom nothing_less_than_Z: |
---|
43 | ∀m: Nat. |
---|
44 | ¬(m < Z). |
---|
45 | |
---|
46 | nlet rec get_index (A: Type[0]) (n: Nat) |
---|
47 | (v: Vector A n) (m: Nat) (lt: m < n) on m: A ≝ |
---|
48 | (match m with |
---|
49 | [ Z ⇒ |
---|
50 | match v return λx.λ_. Z < x → A with |
---|
51 | [ Empty ⇒ λabsd1: Z < Z. ? |
---|
52 | | Cons p hd tl ⇒ λprf1: Z < S p. hd |
---|
53 | ] |
---|
54 | | S o ⇒ |
---|
55 | (match v return λx.λ_. S o < x → A with |
---|
56 | [ Empty ⇒ λprf: S o < Z. ? |
---|
57 | | Cons p hd tl ⇒ λprf: S o < S p. get_index A p tl o ? |
---|
58 | ]) |
---|
59 | ]) lt. |
---|
60 | ##[ ncases (nothing_less_than_Z Z); #K; ncases (K absd1) |
---|
61 | ##| ncases (nothing_less_than_Z (S o)); #K; ncases (K prf) |
---|
62 | ##| napply succ_less_than_injective; nassumption |
---|
63 | ##] |
---|
64 | nqed. |
---|
65 | |
---|
66 | nlet rec get_index_weak (A: Type[0]) (n: Nat) |
---|
67 | (v: Vector A n) (m: Nat) on m ≝ |
---|
68 | match m with |
---|
69 | [ Z ⇒ |
---|
70 | match v with |
---|
71 | [ Empty ⇒ Nothing A |
---|
72 | | Cons p hd tl ⇒ Just A hd |
---|
73 | ] |
---|
74 | | S o ⇒ |
---|
75 | match v with |
---|
76 | [ Empty ⇒ Nothing A |
---|
77 | | Cons p hd tl ⇒ get_index_weak A p tl o |
---|
78 | ] |
---|
79 | ]. |
---|
80 | |
---|
81 | interpretation "Vector get_index" 'get_index v n = (get_index ? ? v n). |
---|
82 | |
---|
83 | nlet rec set_index (A: Type[0]) (n: Nat) (v: Vector A n) (m: Nat) (a: A) (lt: m < n) on m: Vector A n ≝ |
---|
84 | (match m with |
---|
85 | [ Z ⇒ |
---|
86 | match v return λx.λ_. Z < x → Vector A x with |
---|
87 | [ Empty ⇒ λabsd1: Z < Z. Empty A |
---|
88 | | Cons p hd tl ⇒ λprf1: Z < S p. (a :: tl) |
---|
89 | ] |
---|
90 | | S o ⇒ |
---|
91 | (match v return λx.λ_. S o < x → Vector A x with |
---|
92 | [ Empty ⇒ λprf: S o < Z. Empty A |
---|
93 | | Cons p hd tl ⇒ λprf: S o < S p. hd :: (set_index A p tl o a ?) |
---|
94 | ]) |
---|
95 | ]) lt. |
---|
96 | napply succ_less_than_injective. |
---|
97 | nassumption. |
---|
98 | nqed. |
---|
99 | |
---|
100 | nlet rec set_index_weak (A: Type[0]) (n: Nat) |
---|
101 | (v: Vector A n) (m: Nat) (a: A) on m ≝ |
---|
102 | match m with |
---|
103 | [ Z ⇒ |
---|
104 | match v with |
---|
105 | [ Empty ⇒ Nothing (Vector A n) |
---|
106 | | Cons o hd tl ⇒ Just (Vector A n) (? (Cons A o a tl)) |
---|
107 | ] |
---|
108 | | S o ⇒ |
---|
109 | match v with |
---|
110 | [ Empty ⇒ Nothing (Vector A n) |
---|
111 | | Cons p hd tl ⇒ |
---|
112 | let settail ≝ set_index_weak A p tl o a in |
---|
113 | match settail with |
---|
114 | [ Nothing ⇒ Nothing (Vector A n) |
---|
115 | | Just j ⇒ Just (Vector A n) (? (Cons A p hd j)) |
---|
116 | ] |
---|
117 | ] |
---|
118 | ]. |
---|
119 | //. |
---|
120 | nqed. |
---|
121 | |
---|
122 | nlet rec drop (A: Type[0]) (n: Nat) |
---|
123 | (v: Vector A n) (m: Nat) on m ≝ |
---|
124 | match m with |
---|
125 | [ Z ⇒ Just (Vector A n) v |
---|
126 | | S o ⇒ |
---|
127 | match v with |
---|
128 | [ Empty ⇒ Nothing (Vector A n) |
---|
129 | | Cons p hd tl ⇒ ? (drop A p tl o) |
---|
130 | ] |
---|
131 | ]. |
---|
132 | //. |
---|
133 | nqed. |
---|
134 | |
---|
135 | nlet rec split (A: Type[0]) (n,m: Nat) on m |
---|
136 | : Vector A (m+n) → (Vector A m) × (Vector A n) |
---|
137 | ≝ |
---|
138 | match m return λm. Vector A (m+n) → (Vector A m) × (Vector A n) with |
---|
139 | [ Z ⇒ λv.〈[[ ]], v〉 |
---|
140 | | S m' ⇒ λv. |
---|
141 | match v return λl.λ_:Vector A l.l = S (m' + n) → (Vector A (S m')) × (Vector A n) with |
---|
142 | [ Empty ⇒ λK.⊥ |
---|
143 | | Cons o he tl ⇒ λK. |
---|
144 | match split A n m' (tl⌈Vector A (m'+n)↦Vector A o⌉) with |
---|
145 | [ mk_Cartesian v1 v2 ⇒ 〈he::v1, v2〉 ]] (?: (S (m' + n)) = S (m' + n))]. |
---|
146 | //; ndestruct; //. |
---|
147 | nqed. |
---|
148 | |
---|
149 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
150 | (* Folds and builds. *) |
---|
151 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
152 | |
---|
153 | nlet rec fold_right (A: Type[0]) (B: Type[0]) (n: Nat) |
---|
154 | (f: A → B → B) (x: B) (v: Vector A n) on v ≝ |
---|
155 | match v with |
---|
156 | [ Empty ⇒ x |
---|
157 | | Cons n hd tl ⇒ f hd (fold_right A B n f x tl) |
---|
158 | ]. |
---|
159 | |
---|
160 | nlet rec fold_left (A: Type[0]) (B: Type[0]) (n: Nat) |
---|
161 | (f: A → B → A) (x: A) (v: Vector B n) on v ≝ |
---|
162 | match v with |
---|
163 | [ Empty ⇒ x |
---|
164 | | Cons n hd tl ⇒ f (fold_left A B n f x tl) hd |
---|
165 | ]. |
---|
166 | |
---|
167 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
168 | (* Maps and zips. *) |
---|
169 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
170 | |
---|
171 | nlet rec map (A: Type[0]) (B: Type[0]) (n: Nat) |
---|
172 | (f: A → B) (v: Vector A n) on v ≝ |
---|
173 | match v with |
---|
174 | [ Empty ⇒ Empty B |
---|
175 | | Cons n hd tl ⇒ (f hd) :: (map A B n f tl) |
---|
176 | ]. |
---|
177 | |
---|
178 | (* Should be moved into Plogic/equality.ma at some point. Only Type[2] version |
---|
179 | currently in there. |
---|
180 | *) |
---|
181 | nlemma eq_rect_Type0_r : |
---|
182 | ∀A: Type[0]. |
---|
183 | ∀a:A. |
---|
184 | ∀P: ∀x:A. eq ? x a → Type[0]. P a (refl A a) → ∀x: A.∀p:eq ? x a. P x p. |
---|
185 | #A a P H x p. |
---|
186 | ngeneralize in match H. |
---|
187 | ngeneralize in match P. |
---|
188 | ncases p. |
---|
189 | //. |
---|
190 | nqed. |
---|
191 | |
---|
192 | nlet rec zip_with (A: Type[0]) (B: Type[0]) (C: Type[0]) (n: Nat) |
---|
193 | (f: A → B → C) (v: Vector A n) (q: Vector B n) on v ≝ |
---|
194 | (match v return (λx.λr. x = n → Vector C x) with |
---|
195 | [ Empty ⇒ λ_. Empty C |
---|
196 | | Cons n hd tl ⇒ |
---|
197 | match q return (λy.λr. S n = y → Vector C (S n)) with |
---|
198 | [ Empty ⇒ ? |
---|
199 | | Cons m hd' tl' ⇒ |
---|
200 | λe: S n = S m. |
---|
201 | (f hd hd') :: (zip_with A B C n f tl ?) |
---|
202 | ] |
---|
203 | ]) |
---|
204 | (refl ? n). |
---|
205 | ## |
---|
206 | [ #e; |
---|
207 | ndestruct(e); |
---|
208 | ## |
---|
209 | | ndestruct(e); |
---|
210 | napply tl' |
---|
211 | ## |
---|
212 | ] |
---|
213 | nqed. |
---|
214 | |
---|
215 | ndefinition zip ≝ |
---|
216 | λA, B: Type[0]. |
---|
217 | λn: Nat. |
---|
218 | λv: Vector A n. |
---|
219 | λq: Vector B n. |
---|
220 | zip_with A B (Cartesian A B) n (mk_Cartesian A B) v q. |
---|
221 | |
---|
222 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
223 | (* Building vectors from scratch *) |
---|
224 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
225 | |
---|
226 | nlet rec replicate (A: Type[0]) (n: Nat) (h: A) on n ≝ |
---|
227 | match n return λn. Vector A n with |
---|
228 | [ Z ⇒ Empty A |
---|
229 | | S m ⇒ h :: (replicate A m h) |
---|
230 | ]. |
---|
231 | |
---|
232 | nlet rec append (A: Type[0]) (n: Nat) (m: Nat) |
---|
233 | (v: Vector A n) (q: Vector A m) on v ≝ |
---|
234 | match v return (λn.λv. Vector A (n + m)) with |
---|
235 | [ Empty ⇒ q |
---|
236 | | Cons o hd tl ⇒ hd :: (append A o m tl q) |
---|
237 | ]. |
---|
238 | |
---|
239 | interpretation "Vector append" 'append hd tl = (append ? ? hd tl). |
---|
240 | |
---|
241 | nlet rec scan_left (A: Type[0]) (B: Type[0]) (n: Nat) |
---|
242 | (f: A → B → A) (a: A) (v: Vector B n) on v ≝ |
---|
243 | a :: |
---|
244 | (match v with |
---|
245 | [ Empty ⇒ Empty A |
---|
246 | | Cons o hd tl ⇒ scan_left A B o f (f a hd) tl |
---|
247 | ]). |
---|
248 | |
---|
249 | nlet rec scan_right (A: Type[0]) (B: Type[0]) (n: Nat) |
---|
250 | (f: A → B → A) (b: B) (v: Vector A n) on v ≝ |
---|
251 | match v with |
---|
252 | [ Empty ⇒ ? |
---|
253 | | Cons o hd tl ⇒ f hd b :: (scan_right A B o f b tl) |
---|
254 | ]. |
---|
255 | //. |
---|
256 | nqed. |
---|
257 | |
---|
258 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
259 | (* Other manipulations. *) |
---|
260 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
261 | |
---|
262 | nlet rec length (A: Type[0]) (n: Nat) (v: Vector A n) on v ≝ |
---|
263 | match v with |
---|
264 | [ Empty ⇒ Z |
---|
265 | | Cons n hd tl ⇒ S $ length A n tl |
---|
266 | ]. |
---|
267 | |
---|
268 | nlet rec reverse (A: Type[0]) (n: Nat) |
---|
269 | (v: Vector A n) on v ≝ |
---|
270 | match v return (λm.λv. Vector A m) with |
---|
271 | [ Empty ⇒ Empty A |
---|
272 | | Cons o hd tl ⇒ ? (append A o ? (reverse A o tl) (Cons A Z hd (Empty A))) |
---|
273 | ]. |
---|
274 | nrewrite < (succ_plus ? ?). |
---|
275 | nrewrite > (plus_zero ?). |
---|
276 | //. |
---|
277 | nqed. |
---|
278 | |
---|
279 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
280 | (* Conversions to and from lists. *) |
---|
281 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
282 | |
---|
283 | nlet rec list_of_vector (A: Type[0]) (n: Nat) |
---|
284 | (v: Vector A n) on v ≝ |
---|
285 | match v return λn.λv. List A with |
---|
286 | [ Empty ⇒ ? (cic:/matita/ng/List/List.con(0,1,1) A) |
---|
287 | | Cons o hd tl ⇒ hd :: (list_of_vector A o tl) |
---|
288 | ]. |
---|
289 | //. |
---|
290 | nqed. |
---|
291 | |
---|
292 | nlet rec vector_of_list (A: Type[0]) (l: List A) on l ≝ |
---|
293 | match l return λl. Vector A (length A l) with |
---|
294 | [ Empty ⇒ ? (cic:/matita/ng/Vector/Vector.con(0,1,1) A) |
---|
295 | | Cons hd tl ⇒ ? (hd :: (vector_of_list A tl)) |
---|
296 | ]. |
---|
297 | nnormalize. |
---|
298 | //. |
---|
299 | //. |
---|
300 | nqed. |
---|
301 | |
---|
302 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
303 | (* Rotates and shifts. *) |
---|
304 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
305 | |
---|
306 | nlet rec rotate_left (A: Type[0]) (n: Nat) |
---|
307 | (m: Nat) (v: Vector A n) on m: Vector A n ≝ |
---|
308 | match m with |
---|
309 | [ Z ⇒ v |
---|
310 | | S o ⇒ |
---|
311 | match v with |
---|
312 | [ Empty ⇒ Empty A |
---|
313 | | Cons p hd tl ⇒ |
---|
314 | rotate_left A (S p) o (? (append A p ? tl (Cons A ? hd (Empty A)))) |
---|
315 | ] |
---|
316 | ]. |
---|
317 | nrewrite < (succ_plus ? ?). |
---|
318 | nrewrite > (plus_zero ?). |
---|
319 | //. |
---|
320 | nqed. |
---|
321 | |
---|
322 | ndefinition rotate_right ≝ |
---|
323 | λA: Type[0]. |
---|
324 | λn, m: Nat. |
---|
325 | λv: Vector A n. |
---|
326 | reverse A n (rotate_left A n m (reverse A n v)). |
---|
327 | |
---|
328 | ndefinition shift_left_1 ≝ |
---|
329 | λA: Type[0]. |
---|
330 | λn: Nat. |
---|
331 | λv: Vector A n. |
---|
332 | λa: A. |
---|
333 | match v with |
---|
334 | [ Empty ⇒ ? |
---|
335 | | Cons o hd tl ⇒ reverse A n (? (Cons A o a (reverse A o tl))) |
---|
336 | ]. |
---|
337 | //. |
---|
338 | nqed. |
---|
339 | |
---|
340 | ndefinition shift_right_1 ≝ |
---|
341 | λA: Type[0]. |
---|
342 | λn: Nat. |
---|
343 | λv: Vector A n. |
---|
344 | λa: A. |
---|
345 | reverse A n (shift_left_1 A n (reverse A n v) a). |
---|
346 | |
---|
347 | ndefinition shift_left ≝ |
---|
348 | λA: Type[0]. |
---|
349 | λn, m: Nat. |
---|
350 | λv: Vector A n. |
---|
351 | λa: A. |
---|
352 | iterate (Vector A n) (λx. shift_left_1 A n x a) v m. |
---|
353 | |
---|
354 | ndefinition shift_right ≝ |
---|
355 | λA: Type[0]. |
---|
356 | λn, m: Nat. |
---|
357 | λv: Vector A n. |
---|
358 | λa: A. |
---|
359 | iterate (Vector A n) (λx. shift_right_1 A n x a) v m. |
---|
360 | |
---|
361 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
362 | (* Lemmas. *) |
---|
363 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
364 | |
---|
365 | nlemma map_fusion: |
---|
366 | ∀A, B, C: Type[0]. |
---|
367 | ∀n: Nat. |
---|
368 | ∀v: Vector A n. |
---|
369 | ∀f: A → B. |
---|
370 | ∀g: B → C. |
---|
371 | map B C n g (map A B n f v) = map A C n (λx. g (f x)) v. |
---|
372 | #A B C n v f g. |
---|
373 | nelim v. |
---|
374 | nnormalize. |
---|
375 | @. |
---|
376 | #N H V H2. |
---|
377 | nnormalize. |
---|
378 | nrewrite > H2. |
---|
379 | @. |
---|
380 | nqed. |
---|
381 | |
---|
382 | nlemma length_correct: |
---|
383 | ∀A: Type[0]. |
---|
384 | ∀n: Nat. |
---|
385 | ∀v: Vector A n. |
---|
386 | length A n v = n. |
---|
387 | #A n v. |
---|
388 | nelim v. |
---|
389 | nnormalize. |
---|
390 | @. |
---|
391 | #N H V H2. |
---|
392 | nnormalize. |
---|
393 | nrewrite > H2. |
---|
394 | @. |
---|
395 | nqed. |
---|
396 | |
---|
397 | nlemma map_length: |
---|
398 | ∀A, B: Type[0]. |
---|
399 | ∀n: Nat. |
---|
400 | ∀v: Vector A n. |
---|
401 | ∀f: A → B. |
---|
402 | length A n v = length B n (map A B n f v). |
---|
403 | #A B n v f. |
---|
404 | nelim v. |
---|
405 | nnormalize. |
---|
406 | @. |
---|
407 | #N H V H2. |
---|
408 | nnormalize. |
---|
409 | nrewrite > H2. |
---|
410 | @. |
---|
411 | nqed. |
---|