Debian Codegraph @

src/Util/Tuple.v

raw
1 From Coq Require Import Morphisms.
2 From Coq Require Import PeanoNat.
3 From Coq Require Import Relation_Definitions.
4 From Coq Require Import List.
5 From Coq Require Import Lia.
6 Require Import Crypto.Util.Option.
7 Require Import Crypto.Util.Prod.
8 Require Import Crypto.Util.Tactics.DestructHead.
9 Require Import Crypto.Util.Tactics.BreakMatch.
10 Require Import Crypto.Util.Tactics.SpecializeBy.
11 Require Import Crypto.Util.Decidable.
12 Require Import Crypto.Util.ListUtil.
13 Require Export Crypto.Util.FixCoqMistakes.
14
15 Fixpoint tuple' T n : Type :=
16 match n with
17 | O => T
18 | S n' => (tuple' T n' * T)%type
19 end.
20
21 Definition tuple T n : Type :=
22 match n with
23 | O => unit
24 | S n' => tuple' T n'
25 end.
26
27 (** right-associated tuples *)
28 Fixpoint rtuple' T n : Type :=
29 match n with
30 | O => T
31 | S n' => (T * rtuple' T n')%type
32 end.
33
34 Definition rtuple T n : Type :=
35 match n with
36 | O => unit
37 | S n' => rtuple' T n'
38 end.
39
40 Fixpoint rsnoc' T n {struct n} : forall (x : rtuple' T n) (y : T), rtuple' T (S n)
41 := match n return forall (x : rtuple' T n) (y : T), rtuple' T (S n) with
42 | O => fun x y => (x, y)
43 | S n' => fun x y => (fst x, @rsnoc' T n' (snd x) y)
44 end.
45 Global Arguments rsnoc' {T n} _ _.
46 Definition rsnoc {T n} : rtuple T n -> T -> rtuple T (S n)
47 := match n with
48 | O => fun _ y => y
49 | S n' => @rsnoc' T n'
50 end.
51
52 Fixpoint cons' T n {struct n} : forall (y : T) (x : tuple' T n), tuple' T (S n)
53 := match n return forall (y : T) (x : tuple' T n), tuple' T (S n) with
54 | O => fun y x => (y, x)
55 | S n' => fun y x => (@cons' T n' y (fst x), snd x)
56 end.
57 Global Arguments cons' {T n} _ _.
58 Definition cons {T n} : T -> tuple T n -> tuple T (S n)
59 := match n with
60 | O => fun x _ => x
61 | S n' => @cons' T n'
62 end.
63
64 Fixpoint assoc_right' {n T} {struct n}
65 : tuple' T n -> rtuple' T n
66 := match n return tuple' T n -> rtuple' T n with
67 | 0 => fun x => x
68 | S n' => fun ts => let xs := @assoc_right' n' T (fst ts) in
69 let x := snd ts in
70 rsnoc' xs x
71 end.
72
73 Definition assoc_right {n T}
74 : tuple T n -> rtuple T n
75 := match n with
76 | 0 => fun x => x
77 | S n' => @assoc_right' n' T
78 end.
79
80 Fixpoint assoc_left' {n T} {struct n}
81 : rtuple' T n -> tuple' T n
82 := match n return rtuple' T n -> tuple' T n with
83 | 0 => fun x => x
84 | S n' => fun ts => let xs := @assoc_left' n' T (snd ts) in
85 let x := fst ts in
86 cons' x xs
87 end.
88
89 Definition assoc_left {n T}
90 : rtuple T n -> tuple T n
91 := match n with
92 | 0 => fun x => x
93 | S n' => @assoc_left' n' T
94 end.
95
96 Definition tl' {T n} : tuple' T (S n) -> tuple' T n := @fst _ _.
97 Definition tl {T n} : tuple T (S n) -> tuple T n :=
98 match n with
99 | O => fun _ => tt
100 | S n' => @tl' T n'
101 end.
102 Definition hd' {T n} : tuple' T n -> T :=
103 match n with
104 | O => fun x => x
105 | S n' => @snd _ _
106 end.
107 Definition hd {T n} : tuple T (S n) -> T := @hd' _ _.
108
109 Fixpoint to_list' {T} (n:nat) {struct n} : tuple' T n -> list T :=
110 match n with
111 | 0 => fun x => (x::nil)%list
112 | S n' => fun xs : tuple' T (S n') => let (xs', x) := xs in (x :: to_list' n' xs')%list
113 end.
114
115 Definition to_list {T} (n:nat) : tuple T n -> list T :=
116 match n with
117 | 0 => fun _ => nil
118 | S n' => fun xs : tuple T (S n') => to_list' n' xs
119 end.
120
121 Program Fixpoint from_list' {T} (y:T) (n:nat) (xs:list T) : length xs = n -> tuple' T n :=
122 match n return _ with
123 | 0 =>
124 match xs return (length xs = 0 -> tuple' T 0) with
125 | nil => fun _ => y
126 | _ => _ (* impossible *)
127 end
128 | S n' =>
129 match xs return (length xs = S n' -> tuple' T (S n')) with
130 | x :: xs' => fun _ => (from_list' x n' xs' _, y)
131 | _ => _ (* impossible *)
132 end
133 end.
134
135 Program Definition from_list {T} (n:nat) (xs:list T) : length xs = n -> tuple T n :=
136 match n return _ with
137 | 0 =>
138 match xs return (length xs = 0 -> tuple T 0) with
139 | nil => fun _ : 0 = 0 => tt
140 | _ => _ (* impossible *)
141 end
142 | S n' =>
143 match xs return (length xs = S n' -> tuple T (S n')) with
144 | x :: xs' => fun _ => from_list' x n' xs' _
145 | _ => _ (* impossible *)
146 end
147 end.
148
149 Lemma to_list_from_list : forall {T} (n:nat) (xs:list T) pf, to_list n (from_list n xs pf) = xs.
150 Proof using Type.
151 destruct xs as [|t xs]; simpl; intros; subst; auto.
152 generalize dependent t. simpl in *.
153 induction xs; simpl in *; intros; congruence.
154 Qed.
155
156 Lemma length_to_list' T n t : length (@to_list' T n t) = S n.
157 Proof using Type. induction n; simpl in *; trivial; destruct t; simpl; congruence. Qed.
158 #[global]
159 Hint Rewrite length_to_list' : distr_length.
160
161 Lemma length_to_list : forall {T} {n} (xs:tuple T n), length (to_list n xs) = n.
162 Proof using Type.
163 destruct n; [ reflexivity | apply length_to_list' ].
164 Qed.
165 #[global]
166 Hint Rewrite @length_to_list : distr_length.
167
168 Lemma from_list'_to_list' : forall T n (xs:tuple' T n),
169 forall x xs' pf, to_list' n xs = x :: xs' ->
170 from_list' x n xs' pf = xs.
171 Proof using Type.
172 induction n; intros xs x xs' pf H.
173 { simpl in *. injection H; clear H; intros; subst. congruence. }
174 { destruct xs eqn:Hxs;
175 destruct xs' eqn:Hxs';
176 injection H; clear H; intros; subst; try discriminate.
177 simpl. f_equal. eapply IHn. assumption. }
178 Qed.
179
180 Lemma from_list_to_list : forall {T n} (xs:tuple T n) pf, from_list n (to_list n xs) pf = xs.
181 Proof using Type.
182 destruct n as [|n]; auto; intros xs pf; simpl in *.
183 { destruct xs; auto. }
184 { destruct (to_list' n xs) eqn:H; try discriminate.
185 eapply from_list'_to_list'; assumption. }
186 Qed.
187
188 Lemma to_list_S {A n} (x : tuple A (S n)) (a:A)
189 : to_list (T:=A) (S (S n)) (x,a) = a :: to_list (S n) x.
190 Proof using Type. reflexivity. Qed.
191
192 Fixpoint curry'T (R T : Type) (n : nat) : Type
193 := match n with
194 | 0 => T -> R
195 | S n' => curry'T (T -> R) T n'
196 end.
197 Definition curryT (R T : Type) (n : nat) : Type
198 := match n with
199 | 0 => R
200 | S n' => curry'T R T n'
201 end.
202
203 Fixpoint uncurry' {R T n} : (tuple' T n -> R) -> curry'T R T n
204 := match n return (tuple' T n -> R) -> curry'T R T n with
205 | 0 => fun f x => f x
206 | S n' => fun f => @uncurry' (T -> R) T n' (fun xs x => f (xs, x))
207 end.
208
209 Definition uncurry {R T n} : (tuple T n -> R) -> curryT R T n
210 := match n return (tuple T n -> R) -> curryT R T n with
211 | 0 => fun f => f tt
212 | S n' => @uncurry' R T n'
213 end.
214
215 Fixpoint curry' {R T n} : curry'T R T n -> (tuple' T n -> R)
216 := match n return curry'T R T n -> (tuple' T n -> R) with
217 | 0 => fun f x => f x
218 | S n' => fun f xs_x => @curry' (T -> R) T n' f (fst xs_x) (snd xs_x)
219 end.
220
221 Definition curry {R T n} : curryT R T n -> (tuple T n -> R)
222 := match n return curryT R T n -> (tuple T n -> R) with
223 | 0 => fun r _ => r
224 | S n' => @curry' R T n'
225 end.
226
227 Fixpoint eta' {n A B} : (tuple' A n -> B) -> tuple' A n -> B
228 := match n with
229 | 0 => fun f => f
230 | S n' => fun (f : tuple' A n' * A -> B)
231 (xy : tuple' A n' * A)
232 => let '(x, y) := xy in
233 eta' (fun x => f (x, y)) x
234 end.
235
236 Definition eta {n A B} : (tuple A n -> B) -> tuple A n -> B
237 := match n with
238 | 0 => fun f => f
239 | S n' => @eta' n' A B
240 end.
241
242 Definition on_tuple {A B} (f:list A -> list B)
243 {n m:nat} (H:forall xs, length xs = n -> length (f xs) = m)
244 (xs:tuple A n) : tuple B m :=
245 from_list m (f (to_list n xs))
246 (H (to_list n xs) (length_to_list xs)).
247
248 Section map.
249 (* Put the types and function in the context, so that the fixpoint doesn't depend on them *)
250 Context {A B} (f:A -> B).
251
252 Fixpoint map' {n} : tuple' A n -> tuple' B n
253 := match n with
254 | 0 => f
255 | S n' => fun x : tuple' _ _ * _ => (@map' n' (fst x), f (snd x))
256 end.
257 End map.
258
259 Definition map {n A B} (f:A -> B) : tuple A n -> tuple B n
260 := match n with
261 | 0 => fun x => x
262 | S n' => map' f
263 end.
264
265 Definition on_tuple2 {A B C} (f : list A -> list B -> list C) {a b c : nat}
266 (Hlength : forall la lb, length la = a -> length lb = b -> length (f la lb) = c)
267 (ta:tuple A a) (tb:tuple B b) : tuple C c
268 := from_list c (f (to_list a ta) (to_list b tb))
269 (Hlength (to_list a ta) (to_list b tb) (length_to_list ta) (length_to_list tb)).
270
271 Definition map2 {n A B C} (f:A -> B -> C) (xs:tuple A n) (ys:tuple B n) : tuple C n
272 := on_tuple2 (map2 f) (fun la lb pfa pfb => eq_trans (@map2_length _ _ _ _ la lb) (eq_trans (f_equal2 _ pfa pfb) (Nat.min_idempotent _))) xs ys.
273
274 Lemma to_list'_ext {n A} (xs ys:tuple' A n) : to_list' n xs = to_list' n ys -> xs = ys.
275 Proof using Type.
276 induction n; simpl in *; [cbv; congruence|]; destruct_head' prod.
277 intro Hp; injection Hp; clear Hp; intros; subst; eauto using f_equal2.
278 Qed.
279
280 Lemma to_list_ext {n A} (xs ys:tuple A n) : to_list n xs = to_list n ys -> xs = ys.
281 Proof using Type.
282 destruct n; simpl in *; destruct_head unit; eauto using to_list'_ext.
283 Qed.
284
285 Lemma from_list'_complete {A n} (xs:tuple' A n) : exists x ls pf, xs = from_list' x n ls pf.
286 Proof using Type.
287 destruct n; simpl in xs.
288 { exists xs. exists nil. exists eq_refl. reflexivity. }
289 { destruct xs as [xs' x]. exists x. exists (to_list' _ xs'). eexists (length_to_list' _ _ _).
290 symmetry; eapply from_list'_to_list'. reflexivity. }
291 Qed.
292
293 Lemma from_list_complete {A n} (xs:tuple A n) : exists ls pf, xs = from_list n ls pf.
294 Proof using Type.
295 exists (to_list n xs). exists (length_to_list _). symmetry; eapply from_list_to_list.
296 Qed.
297
298 Ltac tuples_from_lists :=
299 repeat match goal with
300 | [xs:tuple ?A _ |- _] =>
301 let ls := fresh "l" xs in
302 destruct (from_list_complete xs) as [ls [? ?]]; subst
303 | [xs:tuple' ?A _ |- _] =>
304 let a := fresh A in
305 let ls := fresh "l" xs in
306 destruct (from_list'_complete xs) as [a [ls [? ?]]]; subst
307 end.
308
309 Lemma map_to_list {A B n ts} (f : A -> B)
310 : List.map f (@to_list A n ts) = @to_list B n (map f ts).
311 Proof using Type.
312 destruct n; simpl; [ reflexivity | ].
313 induction n; simpl in *; [ reflexivity | ].
314 destruct ts; simpl; congruence.
315 Qed.
316
317 Lemma to_list_map2 {A B C n xs ys} (f : A -> B -> C)
318 : List.map2 f (@to_list A n xs) (@to_list B n ys) = @to_list C n (map2 f xs ys).
319 Proof using Type.
320 tuples_from_lists; unfold map2, on_tuple2.
321 repeat (rewrite to_list_from_list || rewrite from_list_to_list).
322 reflexivity.
323 Qed.
324
325 Ltac tuple_maps_to_list_maps :=
326 try eapply to_list_ext;
327 tuples_from_lists;
328 repeat (rewrite <-@map_to_list || rewrite <-@to_list_map2 ||
329 rewrite @to_list_from_list || rewrite @from_list_to_list).
330
331 Lemma map_map2 {n A B C D} (f:A -> B -> C) (g:C -> D) (xs:tuple A n) (ys:tuple B n)
332 : map g (map2 f xs ys) = map2 (fun a b => g (f a b)) xs ys.
333 Proof using Type.
334 tuple_maps_to_list_maps; eauto using ListUtil.map_map2.
335 Qed.
336
337 Lemma map2_fst {n A B C} (f:A -> C) (xs:tuple A n) (ys:tuple B n)
338 : map2 (fun a b => f a) xs ys = map f xs.
339 Proof using Type.
340 tuple_maps_to_list_maps; eauto using ListUtil.map2_fst.
341 Qed.
342
343 Lemma map2_snd {n A B C} (f:B -> C) (xs:tuple A n) (ys:tuple B n)
344 : map2 (fun a b => f b) xs ys = map f ys.
345 Proof using Type.
346 tuple_maps_to_list_maps; eauto using ListUtil.map2_snd.
347 Qed.
348
349 Lemma map_id_ext {n A} (f : A -> A) (xs:tuple A n)
350 : (forall x, f x = x) -> map f xs = xs.
351 Proof using Type.
352 intros; tuple_maps_to_list_maps.
353 transitivity (List.map (fun x => x) lxs).
354 { eapply ListUtil.pointwise_map; cbv [pointwise_relation]; eauto. }
355 { eapply List.map_id. }
356 Qed.
357
358 Lemma map_id {n A} (xs:tuple A n)
359 : map (fun x => x) xs = xs.
360 Proof using Type. eapply map_id_ext; intros; reflexivity. Qed.
361
362 Lemma map2_map {n A B C A' B'} (f:A -> B -> C) (g:A' -> A) (h:B' -> B) (xs:tuple A' n) (ys:tuple B' n)
363 : map2 f (map g xs) (map h ys) = map2 (fun a b => f (g a) (h b)) xs ys.
364 Proof using Type.
365 tuple_maps_to_list_maps; eauto using ListUtil.map2_map.
366 Qed.
367
368 Lemma map_S' {n A B} (f:A -> B) (xs:tuple A (S n)) (x:A)
369 : map (n:=S (S n)) f (xs, x) = (map (n:=S n) f xs, f x).
370 Proof using Type.
371 tuple_maps_to_list_maps.
372 let lxs := match goal with lxs : list _ |- _ => lxs end in
373 destruct lxs as [|x' lxs']; [simpl in *; discriminate|].
374 let x0 := match goal with H : _ = _ |- _ => H end in
375 change ( f x :: List.map f (to_list (S n) (from_list (S n) (x' :: lxs') x0)) = f x :: to_list (S n) (map f (from_list (S n) (x' :: lxs') x0)) ).
376 tuple_maps_to_list_maps.
377 reflexivity.
378 Qed.
379
380 Definition map_S {n A B} (f:A -> B) (xs:tuple' A n) (x:A)
381 : map (n:=S (S n)) f (xs, x) = (map (n:=S n) f xs, f x) := map_S' _ _ _.
382
383 Lemma map2_S' {n A B C} (f:A -> B -> C) (xs:tuple A (S n)) (ys:tuple B (S n)) (x:A) (y:B)
384 : map2 (n:=S (S n)) f (xs, x) (ys, y) = (map2 (n:=S n) f xs ys, f x y).
385 Proof using Type.
386 tuple_maps_to_list_maps.
387 let lxs := match goal with lxs : list A |- _ => lxs end in
388 destruct lxs as [|x' lxs']; [simpl in *; discriminate|].
389 let lys := match goal with lxs : list B |- _ => lxs end in
390 destruct lys as [|y' lys']; [simpl in *; discriminate|].
391 change ( f x y :: List.map2 f (to_list (S n) (from_list (S n) (x' :: lxs') x1))
392 (to_list (S n) (from_list (S n) (y' :: lys') x0)) = f x y :: to_list (S n) (map2 f (from_list (S n) (x' :: lxs') x1) (from_list (S n) (y' :: lys') x0)) ).
393 tuple_maps_to_list_maps.
394 reflexivity.
395 Qed.
396
397 Definition map2_S {n A B C} (f:A -> B -> C) (xs:tuple' A n) (ys:tuple' B n) (x:A) (y:B)
398 : map2 (n:=S (S n)) f (xs, x) (ys, y) = (map2 (n:=S n) f xs ys, f x y) := map2_S' _ _ _ _ _.
399
400 Lemma map2_map_fst {n A B C A'} (f:A -> B -> C) (g:A' -> A) (xs:tuple A' n) (ys:tuple B n)
401 : map2 f (map g xs) ys = map2 (fun a b => f (g a) b) xs ys.
402 Proof using Type.
403 rewrite <- (map2_map f g (fun x => x)), map_id; reflexivity.
404 Qed.
405
406 Lemma map2_map_snd {n A B C B'} (f:A -> B -> C) (g:B' -> B) (xs:tuple A n) (ys:tuple B' n)
407 : map2 f xs (map g ys) = map2 (fun a b => f a (g b)) xs ys.
408 Proof using Type.
409 rewrite <- (map2_map f (fun x => x) g), map_id; reflexivity.
410 Qed.
411
412 Lemma map_map {n A B C} (g : B -> C) (f : A -> B) (xs:tuple A n)
413 : map g (map f xs) = map (fun x => g (f x)) xs.
414 Proof using Type. tuple_maps_to_list_maps; eapply List.map_map. Qed.
415
416 Section monad.
417 Context (M : Type -> Type) (bind : forall X Y, M X -> (X -> M Y) -> M Y) (ret : forall X, X -> M X).
418 Fixpoint lift_monad' {n A} {struct n}
419 : tuple' (M A) n -> M (tuple' A n)
420 := match n return tuple' (M A) n -> M (tuple' A n) with
421 | 0 => fun t => t
422 | S n' => fun xy => bind _ _ (@lift_monad' n' _ (fst xy)) (fun x' => bind _ _ (snd xy) (fun y' => ret _ (x', y')))
423 end.
424 Fixpoint push_monad' {n A} {struct n}
425 : M (tuple' A n) -> tuple' (M A) n
426 := match n return M (tuple' A n) -> tuple' (M A) n with
427 | 0 => fun t => t
428 | S n' => fun xy => (@push_monad' n' _ (bind _ _ xy (fun xy' => ret _ (fst xy'))),
429 bind _ _ xy (fun xy' => ret _ (snd xy')))
430 end.
431 Definition lift_monad {n A}
432 : tuple (M A) n -> M (tuple A n)
433 := match n return tuple (M A) n -> M (tuple A n) with
434 | 0 => ret _
435 | S n' => @lift_monad' n' A
436 end.
437 Definition push_monad {n A}
438 : M (tuple A n) -> tuple (M A) n
439 := match n return M (tuple A n) -> tuple (M A) n with
440 | 0 => fun _ => tt
441 | S n' => @push_monad' n' A
442 end.
443 End monad.
444 Local Notation option_bind
445 := (fun A B (x : option A) f => match x with
446 | Some x' => f x'
447 | None => None
448 end).
449 Definition lift_option {n A} (xs : tuple (option A) n) : option (tuple A n)
450 := lift_monad (fun T => option T) option_bind (fun T v => @Some T v) xs.
451 Definition push_option {n A} (xs : option (tuple A n)) : tuple (option A) n
452 := push_monad (fun T => option T) option_bind (fun T v => @Some T v) xs.
453
454 Lemma lift_push_option {n A} (xs : option (tuple A (S n))) : lift_option (push_option xs) = xs.
455 Proof using Type.
456 simpl in *.
457 induction n; [ reflexivity | ].
458 simpl in *; rewrite IHn; clear IHn.
459 destruct xs as [ [? ?] | ]; reflexivity.
460 Qed.
461
462 Lemma push_lift_option {n A} {xs : tuple (option A) (S n)} {v}
463 : lift_option xs = Some v <-> xs = push_option (Some v).
464 Proof using Type.
465 simpl in *.
466 induction n; [ reflexivity | ].
467 specialize (IHn (fst xs) (fst v)).
468 repeat first [ progress destruct_head_hnf' prod
469 | progress destruct_head_hnf' and
470 | progress destruct_head_hnf' iff
471 | progress destruct_head_hnf' option
472 | progress inversion_option
473 | progress inversion_prod
474 | progress subst
475 | progress break_match
476 | progress simpl in *
477 | progress specialize_by exact eq_refl
478 | reflexivity
479 | split
480 | intro ].
481 Qed.
482
483 Fixpoint fieldwise' {A B} (n:nat) (R:A -> B -> Prop) : tuple' A n -> tuple' B n -> Prop
484 := match n with
485 | 0 => R
486 | S n' => fun (x y : tuple' _ _ * _)
487 => @fieldwise' A B n' R (fst x) (fst y) /\ R (snd x) (snd y)
488 end.
489 Definition fieldwise {A B} (n:nat) (R:A -> B -> Prop) : tuple A n -> tuple B n -> Prop
490 := match n with
491 | 0 => fun _ _ => True
492 | S n' => fieldwise' n' R
493 end.
494
495 Local Ltac Equivalence_fieldwise'_t :=
496 let n := match goal with |- ?R (fieldwise' ?n _) => n end in
497 let IHn := fresh in
498 (* could use [dintuition] in 8.5 only, and remove the [destruct] *)
499 repeat match goal with
500 | [ H : Equivalence _ |- _ ] => destruct H
501 | [ |- Equivalence _ ] => constructor
502 end;
503 induction n as [|? IHn]; [solve [auto]|];
504 simpl; constructor; repeat intro; repeat intuition eauto.
505
506 Section Equivalence.
507 Context {A} {R:relation A}.
508 Global Instance Reflexive_fieldwise' {R_Reflexive:Reflexive R} {n:nat} : Reflexive (fieldwise' n R) | 5.
509 Proof using Type. Equivalence_fieldwise'_t. Qed.
510 Global Instance Symmetric_fieldwise' {R_Symmetric:Symmetric R} {n:nat} : Symmetric (fieldwise' n R) | 5.
511 Proof using Type. Equivalence_fieldwise'_t. Qed.
512 Global Instance Transitive_fieldwise' {R_Transitive:Transitive R} {n:nat} : Transitive (fieldwise' n R) | 5.
513 Proof using Type. Equivalence_fieldwise'_t. Qed.
514 Global Instance Equivalence_fieldwise' {R_equiv:Equivalence R} {n:nat} : Equivalence (fieldwise' n R).
515 Proof using Type. constructor; exact _. Qed.
516
517 Global Instance Reflexive_fieldwise {R_Reflexive:Reflexive R} : forall {n:nat}, Reflexive (fieldwise n R) | 5.
518 Proof using Type. destruct n; (repeat constructor || exact _). Qed.
519 Global Instance Symmetric_fieldwise {R_Symmetric:Symmetric R} : forall {n:nat}, Symmetric (fieldwise n R) | 5.
520 Proof using Type. destruct n; (repeat constructor || exact _). Qed.
521 Global Instance Transitive_fieldwise {R_Transitive:Transitive R} : forall {n:nat}, Transitive (fieldwise n R) | 5.
522 Proof using Type. destruct n; (repeat constructor || exact _). Qed.
523 Global Instance Equivalence_fieldwise {R_equiv:Equivalence R} : forall {n:nat}, Equivalence (fieldwise n R).
524 Proof using Type. constructor; exact _. Qed.
525 End Equivalence.
526
527 Arguments fieldwise' {A B n} _ _ _.
528 Arguments fieldwise {A B n} _ _ _.
529
530 Global Instance dec_fieldwise' {A B R} {H : forall a b, Decidable (R a b)} : forall {n}, DecidableRel (@fieldwise' A B n R) | 10.
531 Proof.
532 induction n; simpl @fieldwise'.
533 { exact _. }
534 { intros ??.
535 exact _. }
536 Defined.
537
538 Global Instance dec_fieldwise {A B R} {H : forall a b, Decidable (R a b)} : forall {n}, DecidableRel (@fieldwise A B n R) | 10.
539 Proof.
540 destruct n; unfold fieldwise; exact _.
541 Defined.
542
543 Global Instance dec_eq' {A} {HA : DecidableRel (@eq A)} : forall {n}, DecidableRel (@eq (tuple' A n)) | 10.
544 Proof.
545 induction n; typeclasses eauto.
546 Defined.
547
548 Global Instance dec_eq {A} {HA : DecidableRel (@eq A)} : forall {n}, DecidableRel (@eq (tuple A n)) | 10.
549 Proof.
550 destruct n; typeclasses eauto.
551 Defined.
552
553 Section fieldwise_map.
554 Local Arguments map : simpl never.
555 Lemma fieldwise'_map_eq {A A' B B' n} R (f:A -> A') (g:B -> B') (t1:tuple' A n) (t2:tuple' B n)
556 : fieldwise' R (map (n:=S n) f t1) (map (n:=S n) g t2)
557 = fieldwise' (fun x y => R (f x) (g y)) t1 t2.
558 Proof using Type.
559 induction n; [ reflexivity | ].
560 destruct t1, t2.
561 rewrite !map_S.
562 simpl @fieldwise'; rewrite IHn; reflexivity.
563 Qed.
564
565 Lemma fieldwise_map_eq {A A' B B' n} R (f:A -> A') (g:B -> B') (t1:tuple A n) (t2:tuple B n)
566 : fieldwise R (map f t1) (map g t2)
567 = fieldwise (fun x y => R (f x) (g y)) t1 t2.
568 Proof using Type.
569 destruct n; [ reflexivity | apply fieldwise'_map_eq ].
570 Qed.
571
572 Lemma fieldwise'_map_iff {A A' B B' n} R (f:A -> A') (g:B -> B') (t1:tuple' A n) (t2:tuple' B n)
573 : fieldwise' R (map (n:=S n) f t1) (map (n:=S n) g t2)
574 <-> fieldwise' (fun x y => R (f x) (g y)) t1 t2.
575 Proof using Type. rewrite fieldwise'_map_eq; reflexivity. Qed.
576
577 Lemma fieldwise_map_iff {A A' B B' n} R (f:A -> A') (g:B -> B') (t1:tuple A n) (t2:tuple B n)
578 : fieldwise R (map f t1) (map g t2)
579 <-> fieldwise (fun x y => R (f x) (g y)) t1 t2.
580 Proof using Type. rewrite fieldwise_map_eq; reflexivity. Qed.
581 End fieldwise_map.
582
583 Lemma fieldwise'_eq_iff {A n} x y : fieldwise' (@eq A) x y <-> @eq (tuple' A n) x y.
584 Proof using Type.
585 induction n as [|n IHn]; [ reflexivity | ].
586 simpl; rewrite IHn; split; [ destruct x, y | ]; simpl; intuition (subst; auto).
587 Qed.
588
589 Lemma fieldwise_eq_iff {A n} x y : fieldwise (@eq A) x y <-> @eq (tuple A n) x y.
590 Proof using Type.
591 destruct n; [ destruct x, y | apply fieldwise'_eq_iff ]; simpl.
592 tauto.
593 Qed.
594
595 Fixpoint fieldwiseb' {A B} (n:nat) (R:A->B->bool) (a:tuple' A n) (b:tuple' B n) {struct n} : bool.
596 destruct n; simpl @tuple' in *.
597 { exact (R a b). }
598 { exact (fieldwiseb' _ _ n R (fst a) (fst b) && R (snd a) (snd b))%bool. }
599 Defined.
600
601 Definition fieldwiseb {A B} (n:nat) (R:A->B->bool) (a:tuple A n) (b:tuple B n) : bool.
602 destruct n; simpl @tuple in *.
603 { exact true. }
604 { exact (fieldwiseb' _ R a b). }
605 Defined.
606
607 Arguments fieldwiseb' {A B n} _ _ _.
608 Arguments fieldwiseb {A B n} _ _ _.
609
610 Lemma fieldwiseb'_fieldwise' :forall {A B} n R Rb
611 (a:tuple' A n) (b:tuple' B n),
612 (forall a b, Rb a b = true <-> R a b) ->
613 (fieldwiseb' Rb a b = true <-> fieldwise' R a b).
614 Proof using Type.
615 intros A B n R Rb a b H.
616 revert n a b;
617 induction n; intros; simpl @tuple' in *;
618 simpl fieldwiseb'; simpl fieldwise'; auto.
619 cbv beta.
620 rewrite Bool.andb_true_iff.
621 f_equiv; auto.
622 Qed.
623
624 Lemma fieldwiseb_fieldwise :forall {A B} n R Rb
625 (a:tuple A n) (b:tuple B n),
626 (forall a b, Rb a b = true <-> R a b) ->
627 (fieldwiseb Rb a b = true <-> fieldwise R a b).
628 Proof using Type.
629 intros A B n R Rb a b H; destruct n; simpl @tuple in *;
630 simpl @fieldwiseb; simpl @fieldwise; try tauto.
631 auto using fieldwiseb'_fieldwise'.
632 Qed.
633
634 Lemma map_ext {A B} (f g : A -> B) n (t : tuple A n) :
635 (forall x : A, f x = g x) ->
636 map f t = map g t.
637 Proof using Type.
638 destruct n; [reflexivity|]; cbn in *.
639 induction n; cbn in *; intro H; auto; [ ].
640 rewrite IHn by assumption.
641 rewrite H; reflexivity.
642 Qed.
643
644 Lemma map_ext_In {A B} (f g : A -> B) n (t : tuple A n) :
645 (forall x, In x (to_list n t) -> f x = g x) ->
646 map f t = map g t.
647 Proof using Type.
648 destruct n; [reflexivity|]; cbn in *.
649 induction n; cbn in *; intro H; auto; [ ].
650 destruct t.
651 rewrite IHn by auto using in_cons.
652 rewrite H; auto using in_eq.
653 Qed.
654
655
656 Fixpoint from_list_default' {T} (d y:T) (n:nat) (xs:list T) : tuple' T n :=
657 match n return tuple' T n with
658 | 0 => y (* ignore high digits *)
659 | S n' =>
660 match xs return _ with
661 | x :: xs' => (from_list_default' d x n' xs', y)
662 | nil => (from_list_default' d d n' nil, y)
663 end
664 end.
665
666 Definition from_list_default {T} d (n:nat) (xs:list T) : tuple T n :=
667 match n return tuple T n with
668 | 0 => tt
669 | S n' =>
670 match xs return _ with
671 | x :: xs' => (from_list_default' d x n' xs')
672 | nil => (from_list_default' d d n' nil)
673 end
674 end.
675
676 Lemma from_list_default'_eq : forall {T} (d : T) xs n y pf,
677 from_list_default' d y n xs = from_list' y n xs pf.
678 Proof using Type.
679 induction xs as [|?? IHxs]; destruct n; intros; simpl in *;
680 solve [ congruence (* 8.5 *)
681 | erewrite IHxs; reflexivity ]. (* 8.4 *)
682 Qed.
683
684 Lemma from_list_default_eq : forall {T} (d : T) xs n pf,
685 from_list_default d n xs = from_list n xs pf.
686 Proof using Type.
687 induction xs; destruct n; intros; try solve [simpl in *; congruence].
688 apply from_list_default'_eq.
689 Qed.
690
691 Fixpoint function R T n : Type :=
692 match n with
693 | O => R
694 | S n' => T -> function R T n'
695 end.
696
697 Fixpoint apply' {R T} (n:nat) : (T -> function R T n) -> tuple' T n -> R :=
698 match n with
699 | 0 => id
700 | S n' => fun f x => apply' n' (f (snd x)) (fst x)
701 end.
702
703 Definition apply {R T} (n:nat) : function R T n -> tuple T n -> R :=
704 match n with
705 | O => fun r _ => r
706 | S n' => fun f x => apply' n' f x
707 end.
708
709 Lemma fieldwise_to_list_iff : forall {T T' n} R (s : tuple T n) (t : tuple T' n),
710 (fieldwise R s t <-> Forall2 R (to_list _ s) (to_list _ t)).
711 Proof using Type.
712 induction n as [|n IHn]; intros R s t; split; intros.
713 + constructor.
714 + cbv [fieldwise]. auto.
715 + destruct n; cbv [tuple to_list fieldwise] in *.
716 - cbv [to_list']; auto.
717 - simpl in *. destruct s,t; cbv [fst snd] in *.
718 constructor; intuition auto.
719 apply IHn; auto.
720 + destruct n; cbv [tuple to_list fieldwise] in *.
721 - cbv [fieldwise']; auto.
722 cbv [to_list'] in *; inversion H; auto.
723 - simpl in *. destruct s,t; cbv [fst snd] in *.
724 inversion H; subst.
725 split; try assumption.
726 apply IHn; auto.
727 Qed.
728
729 Lemma fieldwise_map_from_list_iff
730 {T0 T T'} R ls {n} pf1 pf2 (f : T0 -> T) (g : T0 -> T')
731 : ((fieldwise R (map f (from_list n ls pf1))
732 (map g (from_list n ls pf2)))
733 <-> List.Forall (fun x => R (f x) (g x)) ls).
734 Proof using Type.
735 split; intro H; revert H; revert dependent n;
736 (induction ls as [|x xs IHxs]; intro n; [ | specialize (IHxs (pred n)) ]);
737 destruct n as [|[|n]]; try destruct xs; simpl in *; auto;
738 try congruence; intros; destruct_head'_and; eauto.
739 { inversion H; auto. }
740 { inversion H; auto. }
741 Qed.
742
743 Fixpoint eta_tuple'_dep {T n} : forall (P : tuple' T n -> Type),
744 (forall ts : tuple' T n, P ts) -> forall ts : tuple' T n, P ts
745 := match n with
746 | 0 => fun P f => f
747 | S n' => fun P f (ts : tuple' T n' * T)
748 => let '(ts', x) := ts in
749 @eta_tuple'_dep T n' (fun ts' => P (ts', x)) (fun ts'' => f (ts'', x)) ts'
750 end.
751 Definition eta_tuple' {T n B} : (tuple' T n -> B) -> (tuple' T n -> B)
752 := @eta_tuple'_dep T n (fun _ => B).
753 Global Arguments eta_tuple' {T !n B} / _ _.
754 Definition eta_tuple_dep {T n} : forall (P : tuple T n -> Type),
755 (forall ts : tuple T n, P ts) -> forall ts : tuple T n, P ts
756 := match n with
757 | 0 => fun P f => f
758 | S n' => @eta_tuple'_dep T n'
759 end.
760 Global Arguments eta_tuple_dep {T !n} P / _ _.
761 Definition eta_tuple {T n B} : (tuple T n -> B) -> (tuple T n -> B)
762 := @eta_tuple_dep T n (fun _ => B).
763 Global Arguments eta_tuple {T !n B} / _ _.
764
765 Fixpoint eta_rtuple'_dep {T n} : forall (P : rtuple' T n -> Type),
766 (forall ts : rtuple' T n, P ts) -> forall ts : rtuple' T n, P ts
767 := match n with
768 | 0 => fun P f => f
769 | S n' => fun P f (ts : T * rtuple' T n')
770 => let '(x, ts') := ts in
771 @eta_rtuple'_dep T n' (fun ts' => P (x, ts')) (fun ts'' => f (x, ts'')) ts'
772 end.
773 Definition eta_rtuple' {T n B} : (rtuple' T n -> B) -> (rtuple' T n -> B)
774 := @eta_rtuple'_dep T n (fun _ => B).
775 Global Arguments eta_rtuple' {T !n B} / _ _.
776 Definition eta_rtuple_dep {T n} : forall (P : rtuple T n -> Type),
777 (forall ts : rtuple T n, P ts) -> forall ts : rtuple T n, P ts
778 := match n with
779 | 0 => fun P f => f
780 | S n' => @eta_rtuple'_dep T n'
781 end.
782 Global Arguments eta_rtuple_dep {T !n} P / _ _.
783 Definition eta_rtuple {T n B} : (rtuple T n -> B) -> (rtuple T n -> B)
784 := @eta_rtuple_dep T n (fun _ => B).
785 Global Arguments eta_rtuple {T !n B} / _ _.
786
787 Lemma strip_eta_tuple'_dep {T n} P (f : forall ts : tuple' T n, P ts) ts
788 : eta_tuple'_dep P f ts = f ts.
789 Proof using Type.
790 revert dependent P; induction n; simpl in *; [ reflexivity | intros ].
791 destruct_head prod.
792 rewrite IHn; reflexivity.
793 Qed.
794 Lemma strip_eta_tuple' {T n B} (f : tuple' T n -> B) ts
795 : eta_tuple' f ts = f ts.
796 Proof using Type. exact (strip_eta_tuple'_dep _ _ _). Qed.
797 Lemma strip_eta_tuple_dep {T n} P (f : forall ts : tuple T n, P ts) ts
798 : eta_tuple_dep P f ts = f ts.
799 Proof using Type. destruct n; [ reflexivity | exact (strip_eta_tuple'_dep _ _ _) ]. Qed.
800 Lemma strip_eta_tuple {T n B} (f : tuple T n -> B) ts
801 : eta_tuple f ts = f ts.
802 Proof using Type. exact (strip_eta_tuple_dep _ _ _). Qed.
803
804 Lemma strip_eta_rtuple'_dep {T n} P (f : forall ts : rtuple' T n, P ts) ts
805 : eta_rtuple'_dep P f ts = f ts.
806 Proof using Type.
807 revert dependent P; induction n; simpl in *; [ reflexivity | intros ].
808 destruct_head prod.
809 rewrite IHn; reflexivity.
810 Qed.
811 Lemma strip_eta_rtuple' {T n B} (f : rtuple' T n -> B) ts
812 : eta_rtuple' f ts = f ts.
813 Proof using Type. exact (strip_eta_rtuple'_dep _ _ _). Qed.
814 Lemma strip_eta_rtuple_dep {T n} P (f : forall ts : rtuple T n, P ts) ts
815 : eta_rtuple_dep P f ts = f ts.
816 Proof using Type. destruct n; [ reflexivity | exact (strip_eta_rtuple'_dep _ _ _) ]. Qed.
817 Lemma strip_eta_rtuple {T n B} (f : rtuple T n -> B) ts
818 : eta_rtuple f ts = f ts.
819 Proof using Type. exact (strip_eta_rtuple_dep _ _ _). Qed.
820
821 Definition append {A n} (a : A) : tuple A n -> tuple A (S n) :=
822 match n as n0 return (tuple A n0 -> tuple A (S n0)) with
823 | O => fun t => a
824 | S n' => fun t => (t,a)
825 end.
826
827 Lemma hd_append {A n} (x: tuple A n) (a:A) : hd (append a x) = a.
828 Proof using Type. destruct n; reflexivity. Qed.
829
830 Lemma tl_append {A n} (x: tuple A n) (a:A) : tl (append a x) = x.
831 Proof using Type. destruct n; try destruct x; reflexivity. Qed.
832
833 Lemma subst_append {A n} (x : tuple A (S n)) : x = append (hd x) (tl x).
834 Proof using Type. destruct n; try destruct x; reflexivity. Qed.
835
836 Lemma to_list_append {A n} (x : tuple A n) (a : A) :
837 to_list (S n) (append a x) = a :: to_list n x.
838 Proof using Type. destruct n; try destruct x; reflexivity. Qed.
839
840 Lemma from_list'_cons {A n} (a0 a1:A) (xs:list A) H :
841 from_list' a0 (S n) (a1::xs) H = append (n:=S n) a0 (from_list' a1 n xs (length_cons_full _ _ _ H)).
842 Proof using Type. simpl; rewrite <-!from_list_default'_eq with (d:=a0); eauto. Qed.
843
844 Lemma from_list_cons {A n}:
845 forall (xs : list A) a (H:length (a::xs)=S n),
846 from_list (S n) (a :: xs) H = append a (from_list n xs (length_cons_full _ _ _ H)).
847 Proof using Type.
848 destruct n; intros xs a H; destruct xs; distr_length; [reflexivity|].
849 cbv [from_list]; rewrite !from_list'_cons.
850 rewrite <-!from_list_default'_eq with (d:=a).
851 reflexivity.
852 Qed.
853
854 Lemma map_append' {A B n} (f:A->B) : forall (x:tuple' A n) (a:A),
855 map f (append (n:=S n) a x) = append (f a) (map (n:=S n) f x).
856 Proof using Type. reflexivity. Qed.
857
858 Lemma map_append {A B n} (f:A->B) : forall (x:tuple A n) (a:A),
859 map f (append a x) = append (f a) (map f x).
860 Proof using Type. destruct n; auto using map_append'. Qed.
861
862 Lemma map2_append n A B C f xs ys x y :
863 @map2 (S n) A B C f (append x xs) (append y ys)
864 = append (f x y) (map2 f xs ys).
865 Proof using Type. destruct n; [reflexivity|]. apply map2_S'. Qed.
866
867 Fixpoint nth_default {A m} (d:A) n : tuple A m -> A :=
868 match m, n with
869 | O, _ => fun _ => d
870 | S m', O => hd
871 | S m', S n' => fun x => nth_default d n' (tl x)
872 end.
873
874 Fixpoint left_tl {T n} : tuple T (S n) -> tuple T n :=
875 match n with
876 | O => fun _ => tt
877 | S n' => fun xs => append (hd xs) (left_tl (tl xs))
878 end.
879
880 Fixpoint left_hd {T n} : tuple T (S n) -> T :=
881 match n with
882 | O => fun x => x
883 | S n' => fun xs => left_hd (tl xs)
884 end.
885
886 Fixpoint left_append {T n} (x : T) : tuple T n -> tuple T (S n) :=
887 match n with
888 | O => fun _ => x
889 | S n' => fun xs => append (hd xs) (left_append x (tl xs))
890 end.
891
892 Lemma left_append_left_hd {T n} (xs : tuple T n) x :
893 left_hd (left_append x xs) = x.
894 Proof using Type. induction n; [reflexivity | apply IHn]. Qed.
895
896 Lemma left_append_left_tl {T n} (xs : tuple T n) x :
897 left_tl (left_append x xs) = xs.
898 Proof using Type.
899 induction n; [destruct xs; reflexivity|].
900 simpl. rewrite IHn.
901 symmetry; apply subst_append.
902 Qed.
903
904 Lemma left_append_append {T n} (xs : tuple T n) r l :
905 left_append l (append r xs) = append r (left_append l xs).
906 Proof using Type. destruct n; reflexivity. Qed.
907
908 Lemma left_tl_append {T n} (xs : tuple T (S n)) x:
909 left_tl (append x xs) = append x (left_tl xs).
910 Proof using Type. reflexivity. Qed.
911
912 Lemma left_hd_append {T n} (xs : tuple T (S n)) x:
913 left_hd (append x xs) = left_hd xs.
914 Proof using Type. reflexivity. Qed.
915
916 Lemma tl_left_append {T n} (xs : tuple T (S n)) x :
917 tl (left_append x xs) = left_append x (tl xs).
918 Proof using Type. destruct n; reflexivity. Qed.
919
920 Lemma hd_left_append {T n} (xs : tuple T (S n)) x :
921 hd (left_append x xs) = hd xs.
922 Proof using Type. destruct n; reflexivity. Qed.
923
924 Lemma map'_left_append {A B n} f xs x0 :
925 @map' A B f (S n) (left_append (n:=S n) x0 xs)
926 = left_append (n:=S n) (f x0) (map' f xs).
927 Proof using Type.
928 induction n; try reflexivity.
929 transitivity (map' f (tl (left_append x0 xs)), f (hd (left_append x0 xs))); [reflexivity|].
930 rewrite tl_left_append, IHn. reflexivity.
931 Qed.
932
933 Lemma map_left_append {A B n} f xs x0 :
934 @map (S n) A B f (left_append x0 xs)
935 = left_append (f x0) (map f xs).
936 Proof using Type.
937 destruct n; [ destruct xs; reflexivity| apply map'_left_append].
938 Qed.
939
940 Lemma subst_left_append {T n} (xs : tuple T (S n)) :
941 xs = left_append (left_hd xs) (left_tl xs).
942 Proof using Type.
943 induction n; [reflexivity|].
944 simpl tuple in *; destruct xs as [xs x0].
945 simpl; rewrite hd_append, tl_append.
946 rewrite <-IHn; reflexivity.
947 Qed.
948
949 (* map operation that carries state *)
950 (* first argument to f is index in tuple *)
951 Fixpoint mapi_with' {T A B n} i (f: nat->T->A->T*B) (start:T)
952 : tuple' A n -> T * tuple' B n :=
953 match n as n0 return (tuple' A n0 -> T * tuple' B n0) with
954 | O => fun ys => f i start ys
955 | S n' => fun ys =>
956 (fst (mapi_with' (S i) f (fst (f i start (hd ys))) (tl ys)),
957 (snd (mapi_with' (S i) f (fst (f i start (hd ys))) (tl ys)),
958 snd (f i start (hd ys))))
959 end.
960
961 Definition mapi_with {T A B n} (f: nat->T->A->T*B) (start:T)
962 : tuple A n -> T * tuple B n :=
963 match n as n0 return (tuple A n0 -> T * tuple B n0) with
964 | O => fun ys => (start, tt)
965 | S n' => fun ys => mapi_with' 0 f start ys
966 end.
967
968 Lemma mapi_with'_step {T A B n} i f start inp :
969 @mapi_with' T A B (S n) i f start inp =
970 (fst (mapi_with' (S i) f (fst (f i start (hd inp))) (tl inp)),
971 (snd (mapi_with'(S i) f (fst (f i start (hd inp))) (tl inp)), snd (f i start (hd inp)))).
972 Proof using Type. reflexivity. Qed.
973
974 Lemma mapi_with'_left_step {T A B n} f a0:
975 forall i start (xs : tuple' A n),
976 @mapi_with' T A B (S n) i f start (left_append (n:=S n) a0 xs)
977 = (fst (f (i + S n)%nat (fst (mapi_with' i f start xs)) a0),
978 left_append (n:=S n)
979 (snd (f (i + S n)%nat
980 (fst (mapi_with' i f start xs)) a0))
981 (snd (mapi_with' i f start xs))).
982 Proof using Type.
983 induction n as [|? IHn]; intros; [subst; simpl; repeat f_equal; lia|].
984 rewrite mapi_with'_step; autorewrite with cancel_pair.
985 rewrite tl_left_append, hd_left_append.
986 erewrite IHn by reflexivity; subst; autorewrite with cancel_pair.
987 match goal with |- context [(?xs ,?x0)] =>
988 change (xs, x0) with (append x0 xs) end.
989 rewrite <-left_append_append.
990 repeat (f_equal; try lia).
991 Qed.
992
993 Lemma mapi_with'_linvariant {T A B n} start f
994 (P : forall n, T -> tuple A n -> tuple B n -> Prop)
995 (P_holds : forall n st x0 xs ys,
996 (st = fst (mapi_with f start xs)) ->
997 (ys = snd (mapi_with f start xs)) ->
998 P n st xs ys ->
999 P (S n) (fst (f n st x0))
1000 (left_append x0 xs)
1001 (left_append (snd (f n st x0)) ys))
1002 (P_base : P 0%nat start tt tt) (inp : tuple A n):
1003 P n (fst (mapi_with f start inp)) inp (snd (mapi_with f start inp)).
1004 Proof using Type.
1005 induction n; [destruct inp; apply P_base |].
1006 rewrite (subst_left_append inp).
1007 cbv [mapi_with]. specialize (P_holds n).
1008 destruct n.
1009 { apply (P_holds _ inp tt tt (eq_refl _) (eq_refl _)).
1010 apply P_base. }
1011 { rewrite mapi_with'_left_step.
1012 autorewrite with cancel_pair natsimplify.
1013 apply P_holds; try apply IHn; reflexivity. }
1014 Qed.
1015
1016
1017 Fixpoint repeat {A} (a:A) n : tuple A n :=
1018 match n with
1019 | O => tt
1020 | S n' => append a (repeat a n')
1021 end.
1022
1023 Lemma map_repeat {A B} (f:A->B) (a: A) n :
1024 map f (repeat a n) = repeat (f a) n.
1025 Proof using Type.
1026 induction n; simpl repeat; [reflexivity|rewrite map_append; congruence].
1027 Qed.
1028
1029 Lemma to_list_repeat {A} (a:A) n : to_list _ (repeat a n) = List.repeat a n.
1030 Proof using Type. induction n as [|n]; [reflexivity|destruct n; simpl in *; congruence]. Qed.
1031
1032
1033 Local Ltac fieldwise_t :=
1034 repeat first [ progress simpl in *
1035 | apply conj
1036 | intro
1037 | progress subst
1038 | assumption
1039 | solve [ auto ]
1040 | match goal with
1041 | [ H : _ * _ |- _ ] => destruct H
1042 | [ H : _ /\ _ |- _ ] => destruct H
1043 | [ H : _ <-> _ |- _ ] => destruct H
1044 | [ H : False |- _ ] => exfalso; assumption
1045 | [ H : Forall2 _ (_::_) (_::_) |- _ ] => inversion H; clear H
1046 | [ |- Forall2 _ (_::_) (_::_) ] => constructor
1047 | [ H : _ \/ _ |- _ ] => destruct H
1048 | [ H : tuple' _ ?n, IH : forall x : tuple' _ ?n, _ |- _ ]
1049 => specialize (IH H)
1050 end ].
1051 Lemma fieldwise_In_to_list_repeat_l_iff {T T' n} R (s : tuple T n) (t : T')
1052 : fieldwise R (repeat t n) s <-> (forall x, List.In x (to_list _ s) -> R t x).
1053 Proof using Type.
1054 rewrite fieldwise_to_list_iff; destruct n as [|n]; [ | induction n ]; fieldwise_t.
1055 Qed.
1056 Lemma fieldwise_In_to_list_repeat_r_iff {T T' n} R (s : tuple T n) (t : T')
1057 : fieldwise R s (repeat t n) <-> (forall x, List.In x (to_list _ s) -> R x t).
1058 Proof using Type.
1059 rewrite fieldwise_to_list_iff; destruct n as [|n]; [ | induction n ]; fieldwise_t.
1060 Qed.
1061
1062 Global Instance map'_Proper : forall {n A B}, Proper (pointwise_relation _ eq ==> eq ==> eq) (fun f => @map' A B f n) | 10.
1063 Proof using Type.
1064 induction n as [|n IHn]; intros.
1065 { compute; intros; subst; auto. }
1066 { cbv [pointwise_relation Proper respectful] in *.
1067 intros f g Hfg x y ?; subst y.
1068 simpl; erewrite IHn by first [ eassumption | eauto ].
1069 congruence. }
1070 Qed.
1071
1072 Global Instance map_Proper : forall {n A B}, Proper (pointwise_relation _ eq ==> eq ==> eq) (@map n A B) | 10.
1073 Proof using Type.
1074 destruct n; intros; [ | apply map'_Proper ].
1075 { repeat (intros [] || intro); auto. }
1076 Qed.
1077
1078 Lemma fieldwise'_lift_and {n A B R1 R2} x y
1079 : (@fieldwise' A B n R1 x y /\ @fieldwise' A B n R2 x y)
1080 <-> (@fieldwise' A B n (fun a b => R1 a b /\ R2 a b) x y).
1081 Proof using Type.
1082 induction n as [|n IHn]; intros.
1083 { compute; intros; subst; auto. }
1084 { simpl; rewrite <- IHn; tauto. }
1085 Qed.
1086
1087 Lemma fieldwise_lift_and {n A B R1 R2} x y
1088 : (@fieldwise A B n R1 x y /\ @fieldwise A B n R2 x y)
1089 <-> (@fieldwise A B n (fun a b => R1 a b /\ R2 a b) x y).
1090 Proof using Type.
1091 destruct n; [ compute; tauto | apply fieldwise'_lift_and ].
1092 Qed.
1093
1094
1095 Lemma fieldwise'_Proper_gen_dep {A B A' B'} {RA RB : _ -> _ -> Prop} {R}
1096 {R_Proper_and : Proper (R ==> R ==> R) and}
1097 : forall {n},
1098 forall
1099 (f : A -> B -> Prop)
1100 (g : A' -> B' -> Prop)
1101 (Hfg : forall a a', RA a a' -> forall b b', RB b b' -> R (f a b) (g a' b'))
1102 x x'
1103 (Hx : fieldwise' RA x x')
1104 y y'
1105 (Hy : fieldwise' RB y y'),
1106 R (@fieldwise' A B n f x y) (@fieldwise' A' B' n g x' y').
1107 Proof using Type.
1108 induction n as [|n IHn].
1109 { compute; intros; subst; auto. }
1110 { cbv [pointwise_relation Proper respectful] in *.
1111 intros f g Hfg x y H x' y' H'.
1112 simpl in *; apply R_Proper_and; destruct H, H'; auto. }
1113 Qed.
1114
1115 Lemma fieldwise_Proper_gen_dep {A B A' B'} {RA RB : _ -> _ -> Prop} {R}
1116 {R_Proper_and : Proper (R ==> R ==> R) and}
1117 (R_True : R True True)
1118 : forall {n},
1119 forall
1120 (f : A -> B -> Prop)
1121 (g : A' -> B' -> Prop)
1122 (Hfg : forall a a', RA a a' -> forall b b', RB b b' -> R (f a b) (g a' b'))
1123 x x'
1124 (Hx : fieldwise RA x x')
1125 y y'
1126 (Hy : fieldwise RB y y'),
1127 R (@fieldwise A B n f x y) (@fieldwise A' B' n g x' y').
1128 Proof using Type.
1129 destruct n; intros; [ | eapply fieldwise'_Proper_gen_dep; eauto ].
1130 compute; trivial.
1131 Qed.
1132
1133 Global Instance fieldwise'_Proper_gen {A B RA RB R}
1134 {R_Proper_and : Proper (R ==> R ==> R) and}
1135 : forall {n}, Proper ((RA ==> RB ==> R) ==> fieldwise' RA ==> fieldwise' RB ==> R) (@fieldwise' A B n) | 10.
1136 Proof using Type.
1137 cbv [Proper respectful].
1138 eapply (@fieldwise'_Proper_gen_dep A B A B RA RB R); assumption.
1139 Qed.
1140
1141 Global Instance fieldwise_Proper_gen {A B RA RB R}
1142 {R_Proper_and : Proper (R ==> R ==> R) and}
1143 (R_True : R True True)
1144 : forall {n}, Proper ((RA ==> RB ==> R) ==> fieldwise RA ==> fieldwise RB ==> R) (@fieldwise A B n) | 10.
1145 Proof using Type.
1146 cbv [Proper respectful].
1147 eapply (@fieldwise_Proper_gen_dep A B A B RA RB R); assumption.
1148 Qed.
1149
1150 Global Instance fieldwise'_Proper_gen_eq {A B R}
1151 {R_Proper_and : Proper (R ==> R ==> R) and}
1152 : forall {n}, Proper (pointwise_relation _ (pointwise_relation _ R) ==> eq ==> eq ==> R) (@fieldwise' A B n) | 10.
1153 Proof using Type.
1154 cbv [pointwise_relation].
1155 repeat intro; subst; apply (@fieldwise'_Proper_gen A B eq eq); try assumption;
1156 repeat intro; subst; auto; reflexivity.
1157 Qed.
1158
1159 Global Instance fieldwise_Proper_gen_eq {A B R}
1160 {R_Proper_and : Proper (R ==> R ==> R) and}
1161 (R_True : R True True)
1162 : forall {n}, Proper (pointwise_relation _ (pointwise_relation _ R) ==> eq ==> eq ==> R) (@fieldwise A B n) | 10.
1163 Proof using Type.
1164 cbv [pointwise_relation].
1165 repeat intro; subst; apply (@fieldwise_Proper_gen A B eq eq); try assumption;
1166 repeat intro; subst; auto; reflexivity.
1167 Qed.
1168
1169 Lemma nth_default_to_list' {A} n (xs : tuple' A n) (d : A) :
1170 forall i, List.nth_default d (to_list' n xs) i = @nth_default A (S n) d i xs.
1171 Proof using Type.
1172 revert xs; induction n, i; cbn; break_match; intros;
1173 rewrite ?ListUtil.nth_default_cons_S; try reflexivity; rewrite ?IHn; eauto using ListUtil.nth_default_nil.
1174 Qed.
1175 Lemma nth_default_to_list {A} n (xs : tuple A n) (d : A) :
1176 forall i, List.nth_default d (to_list _ xs) i = nth_default d i xs.
1177 Proof using Type.
1178 destruct n; cbv [Tuple.tuple Tuple.to_list] in *.
1179 { destruct i; reflexivity. }
1180 eapply nth_default_to_list'.
1181 Qed.
1182
1183 From Coq Require Import SetoidList.
1184
1185 Global Instance fieldwise'_Proper
1186 : forall {n A B}, Proper (pointwise_relation _ (pointwise_relation _ impl) ==> eq ==> eq ==> impl) (@fieldwise' A B n) | 10.
1187 Proof using Type. intros; apply fieldwise'_Proper_gen_eq. Qed.
1188
1189 Global Instance fieldwise_Proper
1190 : forall {n A B}, Proper (pointwise_relation _ (pointwise_relation _ impl) ==> eq ==> eq ==> impl) (@fieldwise A B n) | 10.
1191 Proof using Type. intros; now apply fieldwise_Proper_gen_eq. Qed.
1192
1193 Global Instance fieldwise'_Proper_iff
1194 : forall {n A B}, Proper (pointwise_relation _ (pointwise_relation _ iff) ==> eq ==> eq ==> iff) (@fieldwise' A B n) | 10.
1195 Proof using Type. intros; apply fieldwise'_Proper_gen_eq. Qed.
1196
1197 Global Instance fieldwise_Proper_iff
1198 : forall {n A B}, Proper (pointwise_relation _ (pointwise_relation _ iff) ==> eq ==> eq ==> iff) (@fieldwise A B n) | 10.
1199 Proof using Type. intros; now apply fieldwise_Proper_gen_eq. Qed.
1200
1201 Global Instance fieldwise'_Proper_flip_impl
1202 : forall {n A B}, Proper (pointwise_relation _ (pointwise_relation _ (flip impl)) ==> eq ==> eq ==> flip impl) (@fieldwise' A B n) | 10.
1203 Proof using Type. intros; apply @fieldwise'_Proper_gen_eq; compute; tauto. Qed.
1204
1205 Global Instance fieldwise_Proper_flip_impl
1206 : forall {n A B}, Proper (pointwise_relation _ (pointwise_relation _ (flip impl)) ==> eq ==> eq ==> flip impl) (@fieldwise A B n) | 10.
1207 Proof using Type. intros; apply @fieldwise_Proper_gen_eq; compute; tauto. Qed.