Debian Codegraph @

src/Util/CPSUtil.v

raw
1 From Coq Require Import List. Import ListNotations.
2 From Coq Require Import ZArith Lia.
3 Require Import Crypto.Util.Tactics.BreakMatch.
4 Require Import Crypto.Util.Tactics.DestructHead.
5 Require Import Crypto.Util.ListUtil.
6 Require Import Crypto.Util.IdfunWithAlt.
7 Require Import Crypto.Util.LetIn.
8 Require Crypto.Util.Tuple. Local Notation tuple := Tuple.tuple.
9 Local Open Scope Z_scope.
10
11 Create HintDb push_id discriminated.
12 Create HintDb uncps discriminated.
13
14 Lemma push_id {A} (a:A) : id a = a. reflexivity. Qed.
15 #[global]
16 Hint Rewrite @push_id : push_id.
17
18 Definition id_with_alt_cps' {R A} (value value_for_alt : (A -> A) -> A) (f : A -> R) : R
19 := dlet z := id_with_alt (value id) (value_for_alt id) in
20 f z.
21 Definition id_with_alt_cps'' {R A} (value value_for_alt : (A -> R) -> R) (f : A -> R) : R
22 := id_with_alt (value f) (value_for_alt f).
23 Definition id_with_alt_cps {R A} (value value_for_alt : forall R, (A -> R) -> R) (f : A -> R) : R
24 := id_with_alt_cps' (value _) (value_for_alt _) f.
25 Definition id_with_alt_cps'_correct {R A} value value_for_alt f
26 : @id_with_alt_cps' R A value value_for_alt f = f (id_with_alt (value id) (value_for_alt id))
27 := eq_refl.
28 #[global]
29 Hint Rewrite @id_with_alt_cps'_correct : uncps.
30 Definition id_with_alt_cps''_correct {R A} value value_for_alt f
31 : @id_with_alt_cps'' R A value value_for_alt f = id_with_alt (value f) (value_for_alt f)
32 := eq_refl.
33 #[global]
34 Hint Rewrite @id_with_alt_cps''_correct : uncps.
35 Definition id_with_alt_cps_correct {R A} value value_for_alt f
36 : @id_with_alt_cps R A value value_for_alt f = f (id_with_alt (value _ id) (value_for_alt _ id))
37 := eq_refl.
38 #[global]
39 Hint Rewrite @id_with_alt_cps_correct : uncps.
40 Definition id_with_alt_cps'_correct_gen {R A} (value value_for_alt : forall R, (A -> R) -> R) f
41 : @id_with_alt_cps' R A (value _) (value_for_alt _) f = f (id_with_alt (value _ id) (value_for_alt _ id))
42 := eq_refl.
43 #[global]
44 Hint Rewrite @id_with_alt_cps'_correct_gen : uncps.
45 Definition id_with_alt_cps''_correct_gen {R A} (value value_for_alt : forall R, (A -> R) -> R) f
46 (Hvalue : value _ f = f (value _ id))
47 : @id_with_alt_cps'' R A (value _) (value_for_alt _) f = f (id_with_alt (value _ id) (value_for_alt _ id)).
48 Proof.
49 cbv [id_with_alt_cps'' id_with_alt]; assumption.
50 Defined.
51 #[global]
52 Hint Rewrite @id_with_alt_cps''_correct_gen : uncps.
53
54 Definition id_tuple'_with_alt_cps' {R A n}
55 (value value_for_alt : (Tuple.tuple' A n -> Tuple.tuple' A n) -> Tuple.tuple' A n)
56 (f : Tuple.tuple' A n -> R)
57 : R
58 := dlet z := id_tuple'_with_alt (value id) (value_for_alt id) in
59 f z.
60 Definition id_tuple_with_alt_cps' {R A n}
61 (value value_for_alt : (Tuple.tuple A n -> Tuple.tuple A n) -> Tuple.tuple A n)
62 (f : Tuple.tuple A n -> R)
63 : R
64 := dlet z := id_tuple_with_alt (value id) (value_for_alt id) in
65 f z.
66
67 Definition id_tuple'_with_alt_cps'_correct {R A n} value value_for_alt f
68 : @id_tuple'_with_alt_cps' R A n value value_for_alt f = f (id_tuple'_with_alt (value id) (value_for_alt id))
69 := eq_refl.
70 #[global]
71 Hint Rewrite @id_tuple'_with_alt_cps'_correct : uncps.
72
73 Definition id_tuple_with_alt_cps'_correct {R A n} value value_for_alt f
74 : @id_tuple_with_alt_cps' R A n value value_for_alt f = f (id_tuple_with_alt (value id) (value_for_alt id))
75 := eq_refl.
76 #[global]
77 Hint Rewrite @id_tuple_with_alt_cps'_correct : uncps.
78
79 Definition id_tuple'_with_alt_cps'' {R A n}
80 (value value_for_alt : (Tuple.tuple' A n -> R) -> R)
81 (f : Tuple.tuple' A n -> R)
82 : R
83 := id_with_alt (value f) (value_for_alt f).
84 Definition id_tuple_with_alt_cps'' {R A n}
85 (value value_for_alt : (Tuple.tuple A n -> R) -> R)
86 (f : Tuple.tuple A n -> R)
87 : R
88 := id_with_alt (value f) (value_for_alt f).
89
90 Definition id_tuple'_with_alt_cps''_correct {R A n} value value_for_alt f
91 : @id_tuple'_with_alt_cps'' R A n value value_for_alt f = id_with_alt (value f) (value_for_alt f)
92 := eq_refl.
93 #[global]
94 Hint Rewrite @id_tuple'_with_alt_cps''_correct : uncps.
95
96 Definition id_tuple_with_alt_cps''_correct {R A n} value value_for_alt f
97 : @id_tuple_with_alt_cps'' R A n value value_for_alt f = id_with_alt (value f) (value_for_alt f)
98 := eq_refl.
99 #[global]
100 Hint Rewrite @id_tuple_with_alt_cps''_correct : uncps.
101
102 Definition id_tuple'_with_alt_cps''_correct_gen {R A n}
103 (value value_for_alt : forall R, (_ -> R) -> R) f
104 (Hvalue : value _ f = f (value _ id))
105 : @id_tuple'_with_alt_cps'' R A n (value _) (value_for_alt _) f = f (id_tuple'_with_alt (value _ id) (value_for_alt _ id)).
106 Proof.
107 autorewrite with uncps.
108 rewrite ?unfold_id_tuple'_with_alt, ?unfold_id_tuple_with_alt, ?unfold_id_with_alt, Hvalue.
109 reflexivity.
110 Qed.
111 #[global]
112 Hint Rewrite @id_tuple'_with_alt_cps''_correct_gen : uncps.
113
114 Definition id_tuple_with_alt_cps''_correct_gen {R A n}
115 (value value_for_alt : forall R, (_ -> R) -> R) f
116 (Hvalue : value _ f = f (value _ id))
117 : @id_tuple_with_alt_cps'' R A n (value _) (value_for_alt _) f = f (id_tuple_with_alt (value _ id) (value_for_alt _ id)).
118 Proof.
119 autorewrite with uncps.
120 rewrite ?unfold_id_tuple_with_alt, ?unfold_id_tuple_with_alt, ?unfold_id_with_alt, Hvalue.
121 reflexivity.
122 Qed.
123 #[global]
124 Hint Rewrite @id_tuple_with_alt_cps''_correct_gen : uncps.
125
126 Definition id_tuple'_with_alt_cps {R A n}
127 (value value_for_alt : forall R, (Tuple.tuple' A n -> R) -> R)
128 (f : Tuple.tuple' A n -> R)
129 : R
130 := id_tuple'_with_alt_cps' (value _) (value_for_alt _) f.
131 Definition id_tuple_with_alt_cps {R A n}
132 (value value_for_alt : forall R, (Tuple.tuple A n -> R) -> R)
133 (f : Tuple.tuple A n -> R)
134 : R
135 := id_tuple_with_alt_cps' (value _) (value_for_alt _) f.
136
137 Definition id_tuple'_with_alt_cps_correct {R A n} value value_for_alt f
138 : @id_tuple'_with_alt_cps R A n value value_for_alt f = f (id_tuple'_with_alt (value _ id) (value_for_alt _ id))
139 := eq_refl.
140 #[global]
141 Hint Rewrite @id_tuple'_with_alt_cps_correct : uncps.
142
143 Definition id_tuple_with_alt_cps_correct {R A n} value value_for_alt f
144 : @id_tuple_with_alt_cps R A n value value_for_alt f = f (id_tuple_with_alt (value _ id) (value_for_alt _ id))
145 := eq_refl.
146 #[global]
147 Hint Rewrite @id_tuple_with_alt_cps_correct : uncps.
148
149 Lemma update_nth_id {T} i (xs:list T) : ListUtil.update_nth i id xs = xs.
150 Proof.
151 revert xs; induction i; destruct xs; simpl; solve [ trivial | congruence ].
152 Qed.
153
154 Lemma map_fst_combine {A B} (xs:list A) (ys:list B) : List.map fst (List.combine xs ys) = List.firstn (length ys) xs.
155 Proof.
156 revert xs; induction ys; destruct xs; simpl; solve [ trivial | congruence ].
157 Qed.
158
159 Lemma map_snd_combine {A B} (xs:list A) (ys:list B) : List.map snd (List.combine xs ys) = List.firstn (length xs) ys.
160 Proof.
161 revert xs; induction ys; destruct xs; simpl; solve [ trivial | congruence ].
162 Qed.
163
164 Lemma nth_default_seq_inbouns d s n i (H:(i < n)%nat) :
165 List.nth_default d (List.seq s n) i = (s+i)%nat.
166 Proof.
167 progress cbv [List.nth_default].
168 rewrite ListUtil.nth_error_seq.
169 break_innermost_match; solve [ trivial | lia ].
170 Qed.
171
172 Lemma mod_add_mul_full a b c k m : m <> 0 -> c mod m = k mod m ->
173 (a + b * c) mod m = (a + b * k) mod m.
174 Proof.
175 intros; rewrite Z.add_mod, Z.mul_mod by auto.
176 match goal with H : _ mod _ = _ mod _ |- _ => rewrite H end.
177 rewrite <-Z.mul_mod, <-Z.add_mod by auto; reflexivity.
178 Qed.
179
180 Fixpoint map_cps {A B} (g : A->B) ls
181 {T} (f:list B->T):=
182 match ls with
183 | nil => f nil
184 | a :: t => map_cps g t (fun r => f (g a :: r))
185 end.
186 Lemma map_cps_correct {A B} g ls: forall {T} f,
187 @map_cps A B g ls T f = f (map g ls).
188 Proof. induction ls as [|?? IHls]; simpl; intros; rewrite ?IHls; reflexivity. Qed.
189 #[global]
190 Hint Rewrite @map_cps_correct : uncps.
191
192 Fixpoint map_cps2 {A B} (g : A->forall T, (B -> T) -> T) ls
193 {T} (f:list B->T) : T:=
194 match ls with
195 | nil => f nil
196 | a :: t => map_cps2 g t (fun r => g a _ (fun ga => f (ga :: r)))
197 end.
198 Lemma map_cps2_correct {A B} g g' (Hg : forall T k a, g a T k = k (g' a)) ls : forall {T} f,
199 @map_cps2 A B g ls T f = f (map g' ls).
200 Proof. induction ls as [|?? IHls]; simpl; intros; rewrite ?IHls, ?Hg; reflexivity. Qed.
201
202 Definition firstn_cps {A} (n:nat) (l:list A) {T} (f:list A->T) :=
203 match n with
204 | O => f nil
205 | S n' => match l with
206 | nil => f nil
207 | a :: l' => f (a :: firstn n' l')
208 end
209 end.
210 Lemma firstn_cps_correct {A} n l T f :
211 @firstn_cps A n l T f = f (firstn n l).
212 Proof. induction n; destruct l; reflexivity. Qed.
213 #[global]
214 Hint Rewrite @firstn_cps_correct : uncps.
215
216 Fixpoint flat_map_cps_specialized {T A B} (g:A->(list B->T)->T) (ls : list A) (f:list B->T) :=
217 match ls with
218 | nil => f nil
219 | (x::tl)%list => g x (fun r => flat_map_cps_specialized g tl (fun rr => f (r ++ rr))%list)
220 end.
221
222 Definition flat_map_cps {A B} (g:A->forall {T}, (list B->T)->T) (ls : list A) {T} (f:list B->T)
223 := @flat_map_cps_specialized T A B (fun a => @g a T) ls f.
224 Lemma unfold_flat_map_cps {A B} (g:A->forall {T}, (list B->T)->T) (ls : list A) {T} (f:list B->T)
225 : @flat_map_cps A B g ls T f
226 = match ls with
227 | nil => f nil
228 | (x::tl)%list => g x (fun r => flat_map_cps g tl (fun rr => f (r ++ rr))%list)
229 end.
230 Proof. destruct ls; reflexivity. Qed.
231 Lemma flat_map_cps_correct {A B} (g:A->forall {T}, (list B->T)->T) ls :
232 forall {T} (f:list B->T),
233 (forall x T h, @g x T h = h (g x id)) ->
234 @flat_map_cps A B g ls T f = f (List.flat_map (fun x => g x id) ls).
235 Proof.
236 induction ls as [|?? IHls]; intros T f H; [reflexivity|].
237 rewrite unfold_flat_map_cps. simpl @flat_map.
238 rewrite H; erewrite IHls by eassumption.
239 reflexivity.
240 Qed.
241 #[global]
242 Hint Rewrite @flat_map_cps_correct using (intros; autorewrite with uncps; auto): uncps.
243
244 Fixpoint from_list_default'_cps {A} (d y:A) n xs {T}:
245 (Tuple.tuple' A n -> T) -> T:=
246 match n as n0 return ((Tuple.tuple' A n0 ->T) ->T) with
247 | O => fun f => f y
248 | S n' => fun f =>
249 match xs with
250 | nil => from_list_default'_cps d d n' nil (fun r => f (r, y))
251 | x :: xs' => from_list_default'_cps d x n' xs' (fun r => f (r, y))
252 end
253 end.
254 Lemma from_list_default'_cps_correct {A} n : forall d y l {T} f,
255 @from_list_default'_cps A d y n l T f = f (Tuple.from_list_default' d y n l).
256 Proof.
257 induction n as [|? IHn]; intros; simpl; [reflexivity|].
258 break_match; subst; apply IHn.
259 Qed.
260 Definition from_list_default_cps {A} (d:A) n (xs:list A) {T} :
261 (Tuple.tuple A n -> T) -> T:=
262 match n as n0 return ((Tuple.tuple A n0 ->T) ->T) with
263 | O => fun f => f tt
264 | S n' => fun f =>
265 match xs with
266 | nil => from_list_default'_cps d d n' nil f
267 | x :: xs' => from_list_default'_cps d x n' xs' f
268 end
269 end.
270 Lemma from_list_default_cps_correct {A} n : forall d l {T} f,
271 @from_list_default_cps A d n l T f = f (Tuple.from_list_default d n l).
272 Proof.
273 destruct n; intros; simpl; [reflexivity|].
274 break_match; auto using from_list_default'_cps_correct.
275 Qed.
276 #[global]
277 Hint Rewrite @from_list_default_cps_correct : uncps.
278 Fixpoint to_list'_cps {A} n
279 {T} (f:list A -> T) : Tuple.tuple' A n -> T :=
280 match n as n0 return (Tuple.tuple' A n0 -> T) with
281 | O => fun x => f [x]
282 | S n' => fun (xs: Tuple.tuple' A (S n')) =>
283 let (xs', x) := xs in
284 to_list'_cps n' (fun r => f (x::r)) xs'
285 end.
286 Lemma to_list'_cps_correct {A} n: forall t {T} f,
287 @to_list'_cps A n T f t = f (Tuple.to_list' n t).
288 Proof.
289 induction n; simpl; intros; [reflexivity|].
290 destruct_head prod. apply IHn.
291 Qed.
292 Definition to_list_cps' {A} n {T} (f:list A->T)
293 : Tuple.tuple A n -> T :=
294 match n as n0 return (Tuple.tuple A n0 ->T) with
295 | O => fun _ => f nil
296 | S n' => to_list'_cps n' f
297 end.
298 Definition to_list_cps {A} n t {T} f :=
299 @to_list_cps' A n T f t.
300 Lemma to_list_cps_correct {A} n t {T} f :
301 @to_list_cps A n t T f = f (Tuple.to_list n t).
302 Proof. cbv [to_list_cps to_list_cps' Tuple.to_list]; break_match; auto using to_list'_cps_correct. Qed.
303 #[global]
304 Hint Rewrite @to_list_cps_correct : uncps.
305
306 Definition on_tuple_cps {A B} (d:B) (g:list A ->forall {T},(list B->T)->T) {n m}
307 (xs : Tuple.tuple A n) {T} (f:tuple B m ->T) :=
308 to_list_cps n xs (fun r => g r (fun rr => from_list_default_cps d m rr f)).
309 Lemma on_tuple_cps_correct {A B} d (g:list A -> forall {T}, (list B->T)->T)
310 {n m} xs {T} f
311 (Hg : forall x {T} h, @g x T h = h (g x id)) : forall H,
312 @on_tuple_cps A B d g n m xs T f = f (@Tuple.on_tuple A B (fun x => g x id) n m H xs).
313 Proof.
314 cbv [on_tuple_cps Tuple.on_tuple]; intros H.
315 rewrite to_list_cps_correct, Hg, from_list_default_cps_correct.
316 rewrite (Tuple.from_list_default_eq _ _ _ (H _ (Tuple.length_to_list _))).
317 reflexivity.
318 Qed. #[global] Hint Rewrite @on_tuple_cps_correct using (intros; autorewrite with uncps; auto): uncps.
319
320 Fixpoint update_nth_cps {A} n (g:A->A) xs {T} (f:list A->T) :=
321 match n with
322 | O =>
323 match xs with
324 | [] => f []
325 | x' :: xs' => f (g x' :: xs')
326 end
327 | S n' =>
328 match xs with
329 | [] => f []
330 | x' :: xs' => update_nth_cps n' g xs' (fun r => f (x' :: r))
331 end
332 end.
333 Lemma update_nth_cps_correct {A} n g: forall xs T f,
334 @update_nth_cps A n g xs T f = f (update_nth n g xs).
335 Proof. induction n; intros; simpl; break_match; try apply IHn; reflexivity. Qed.
336 #[global]
337 Hint Rewrite @update_nth_cps_correct : uncps.
338
339 Fixpoint combine_cps {A B} (la :list A) (lb : list B)
340 {T} (f:list (A*B)->T) :=
341 match la with
342 | nil => f nil
343 | a :: tla =>
344 match lb with
345 | nil => f nil
346 | b :: tlb => combine_cps tla tlb (fun lab => f ((a,b)::lab))
347 end
348 end.
349 Lemma combine_cps_correct {A B} la: forall lb {T} f,
350 @combine_cps A B la lb T f = f (combine la lb).
351 Proof.
352 induction la; simpl combine_cps; simpl combine; intros;
353 try break_match; try apply IHla; reflexivity.
354 Qed.
355 #[global]
356 Hint Rewrite @combine_cps_correct: uncps.
357
358 (* differs from fold_right_cps in that the functional argument `g` is also a CPS function *)
359 Local Set Universe Polymorphism.
360 Definition fold_right_cps2_specialized_step
361 (fold_right_cps2_specialized
362 : forall {T A B} (g : B -> A -> (A->T)->T) (a0 : A) (l : list B) (f : A -> T), _)
363 {T A B} (g : B -> A -> (A->T)->T) (a0 : A) (l : list B) (f : A -> T) :=
364 match l with
365 | nil => f a0
366 | b :: tl => fold_right_cps2_specialized g a0 tl (fun r => g b r f)
367 end.
368 Fixpoint fold_right_cps2_specialized {T A B} (g : B -> A -> (A->T)->T) (a0 : A) (l : list B) (f : A -> T) :=
369 @fold_right_cps2_specialized_step (@fold_right_cps2_specialized) T A B g a0 l f.
370 Definition fold_right_cps2 {A B} (g : B -> A -> forall {T}, (A->T)->T) (a0 : A) (l : list B) {T} (f : A -> T) :=
371 @fold_right_cps2_specialized T A B (fun b a => @g b a T) a0 l f.
372 Local Unset Universe Polymorphism.
373 Lemma unfold_fold_right_cps2 {A B} (g : B -> A -> forall {T}, (A->T)->T) (a0 : A) (l : list B) {T} (f : A -> T)
374 : @fold_right_cps2 A B g a0 l T f
375 = match l with
376 | nil => f a0
377 | b :: tl => fold_right_cps2 g a0 tl (fun r => g b r f)
378 end.
379 Proof. destruct l; reflexivity. Qed.
380 Lemma fold_right_cps2_correct {A B} g a0 l : forall {T} f,
381 (forall b a T h, @g b a T h = h (@g b a A id)) ->
382 @fold_right_cps2 A B g a0 l T f = f (List.fold_right (fun b a => @g b a A id) a0 l).
383 Proof.
384 induction l as [|?? IHl]; intros T f H; [reflexivity|].
385 rewrite unfold_fold_right_cps2. simpl fold_right.
386 rewrite H; erewrite IHl by eassumption.
387 rewrite H; reflexivity.
388 Qed.
389 #[global]
390 Hint Rewrite @fold_right_cps2_correct using (intros; autorewrite with uncps; auto): uncps.
391
392 Definition fold_right_no_starter {A} (f:A->A->A) ls : option A :=
393 match ls with
394 | nil => None
395 | cons x tl => Some (List.fold_right f x tl)
396 end.
397 Lemma fold_right_min ls x :
398 x = List.fold_right Z.min x ls
399 \/ List.In (List.fold_right Z.min x ls) ls.
400 Proof.
401 induction ls; intros; simpl in *; try tauto.
402 match goal with |- context [Z.min ?x ?y] =>
403 destruct (Z.min_spec x y) as [[? Hmin]|[? Hmin]]
404 end; rewrite Hmin; tauto.
405 Qed.
406 Lemma fold_right_no_starter_min ls : forall x,
407 fold_right_no_starter Z.min ls = Some x ->
408 List.In x ls.
409 Proof.
410 cbv [fold_right_no_starter]; intros; destruct ls; try discriminate.
411 inversion H; subst; clear H.
412 destruct (fold_right_min ls z);
413 simpl List.In; tauto.
414 Qed.
415 Fixpoint fold_right_cps {A B} (g:B->A->A) (a0:A) (l:list B) {T} (f:A->T) :=
416 match l with
417 | nil => f a0
418 | cons a tl => fold_right_cps g a0 tl (fun r => f (g a r))
419 end.
420 Lemma fold_right_cps_correct {A B} g a0 l: forall {T} f,
421 @fold_right_cps A B g a0 l T f = f (List.fold_right g a0 l).
422 Proof. induction l as [|? l IHl]; intros; simpl; rewrite ?IHl; auto. Qed.
423 #[global]
424 Hint Rewrite @fold_right_cps_correct : uncps.
425
426 Definition fold_right_no_starter_cps {A} g ls {T} (f:option A->T) :=
427 match ls with
428 | nil => f None
429 | cons x tl => f (Some (List.fold_right g x tl))
430 end.
431 Lemma fold_right_no_starter_cps_correct {A} g ls {T} f :
432 @fold_right_no_starter_cps A g ls T f = f (fold_right_no_starter g ls).
433 Proof.
434 cbv [fold_right_no_starter_cps fold_right_no_starter]; break_match; reflexivity.
435 Qed.
436 #[global]
437 Hint Rewrite @fold_right_no_starter_cps_correct : uncps.
438
439 Import Tuple.
440
441 Module Tuple.
442 Fixpoint map_cps {A B n} (g : A->B) (t : tuple A n) {T} :
443 (tuple B n->T) -> T:=
444 match n return tuple A n -> (tuple B n -> T) -> T with
445 | O => fun _ f => f tt
446 | S n' => fun t f => map_cps g (tl t) (fun r => f (append (g (hd t)) r))
447 end t.
448 Lemma map_cps_correct {A B n} g t: forall {T} f,
449 @map_cps A B n g t T f = f (map g t).
450 Proof.
451 induction n; simpl map_cps; intros; try destruct t;
452 [|rewrite IHn, <-map_append,<-subst_append]; reflexivity.
453 Qed. #[global] Hint Rewrite @map_cps_correct : uncps.
454
455 Fixpoint map2_cps {n A B C} (g:A->B->C) (xs:tuple A n) (ys:tuple B n) {T} :
456 (tuple C n->T) -> T :=
457 match n return tuple _ n -> tuple _ n -> (tuple C n -> T) -> T with
458 | O => fun _ _ f => f tt
459 | S n' => fun xs ys f =>
460 map2_cps g (tl xs) (tl ys) (fun zs => f (append (g (hd xs) (hd ys)) zs))
461 end xs ys.
462 Lemma map2_cps_correct {n A B C} g xs ys : forall {T} f,
463 @map2_cps n A B C g xs ys T f = f (map2 g xs ys).
464 Proof.
465 induction n; simpl map2_cps; intros; try destruct xs, ys;
466 [|rewrite IHn, <-map2_append,<-!subst_append]; reflexivity.
467 Qed. #[global] Hint Rewrite @map2_cps_correct : uncps.
468
469 Section internal_mapi_with_cps.
470 (* We define fixpoints with fewer parameters to the internal [fix] to allow unfolding to partially specialize them *)
471 Context {R T A B : Type}
472 (f: nat->T->A->(T*B->R)->R).
473
474 Fixpoint mapi_with'_cps_specialized {n} i
475 (start:T)
476 : Tuple.tuple' A n -> (T * tuple' B n -> R) -> R :=
477 match n as n0 return (tuple' A n0 -> (T * tuple' B n0->R)->R) with
478 | O => fun ys ret => f i start ys ret
479 | S n' => fun ys ret =>
480 f i start (hd ys) (fun sb =>
481 mapi_with'_cps_specialized (S i) (fst sb) (tl ys)
482 (fun r => ret (fst r, (snd r, snd sb))))
483 end.
484 End internal_mapi_with_cps.
485
486 Definition mapi_with'_cps {T A B n} i
487 (f: nat->T->A->forall {R}, (T*B->R)->R) (start:T)
488 : Tuple.tuple' A n -> forall {R}, (T * tuple' B n -> R) -> R
489 := fun ts {R} => @mapi_with'_cps_specialized R T A B (fun n t a => @f n t a R) n i start ts.
490
491 Definition mapi_with_cps {S A B n}
492 (f: nat->S->A->forall {T}, (S*B->T)->T) (start:S) (ys:tuple A n) {T}
493 : (S * tuple B n->T)->T :=
494 match n as n0 return (tuple A n0 -> (S * tuple B n0->T)->T) with
495 | O => fun ys ret => ret (start, tt)
496 | S n' => fun ys ret => mapi_with'_cps 0%nat f start ys ret
497 end ys.
498
499 Lemma unfold_mapi_with'_cps {T A B n} i
500 (f: nat->T->A->forall {R}, (T*B->R)->R) (start:T)
501 : @mapi_with'_cps T A B n i f start
502 = match n as n0 return (tuple' A n0 -> forall {R}, (T * tuple' B n0->R)->R) with
503 | O => fun ys T ret => f i start ys ret
504 | S n' => fun ys T ret =>
505 f i start (hd ys) (fun sb =>
506 mapi_with'_cps (S i) f (fst sb) (tl ys)
507 (fun r => ret (fst r, (snd r, snd sb))))
508 end.
509 Proof. destruct n; reflexivity. Qed.
510
511 Lemma mapi_with'_cps_correct {S A B n} : forall i f start xs T ret,
512 (forall i s a R (ret:_->R), f i s a R ret = ret (f i s a _ id)) ->
513 @mapi_with'_cps S A B n i f start xs T ret = ret (mapi_with' i (fun i s a => f i s a _ id) start xs).
514 Proof. induction n as [|n IHn]; intros i f start xs T ret H; simpl; rewrite H, ?IHn by assumption; reflexivity. Qed.
515 Lemma mapi_with_cps_correct {S A B n} f start xs T ret
516 (H:forall i s a R (ret:_->R), f i s a R ret = ret (f i s a _ id))
517 : @mapi_with_cps S A B n f start xs T ret = ret (mapi_with (fun i s a => f i s a _ id) start xs).
518 Proof. destruct n; simpl; rewrite ?mapi_with'_cps_correct by assumption; reflexivity. Qed.
519 #[global]
520 Hint Rewrite @mapi_with_cps_correct @mapi_with'_cps_correct
521 using (intros; autorewrite with uncps; auto): uncps.
522
523 Section internal_mapi_with_cps2.
524 (* We define fixpoints with fewer parameters to the internal [fix] to allow unfolding to partially specialize them *)
525 Context {R T A B : Type}
526 (f: nat->T->A->(T*B->R)->R).
527
528 Fixpoint mapi_with'_cps2_specialized {n} i
529 (start:T)
530 : Tuple.tuple' A n -> (T * tuple' B n -> R) -> R :=
531 match n as n0 return (tuple' A n0 -> (T * tuple' B n0->R)->R) with
532 | O => fun ys ret => f i start ys ret
533 | S n' => fun ys ret =>
534 f i start (hd ys) (fun sb =>
535 mapi_with'_cps2_specialized (S i) (fst sb) (tl ys)
536 (fun r => ret (fst r, (snd r, snd sb))))
537 end.
538 End internal_mapi_with_cps2.
539
540 Definition mapi_with'_cps2 {T A B n} i
541 (f: nat->T->A->forall {R}, (T*B->R)->R) (start:T)
542 : Tuple.tuple' A n -> forall {R}, (T * tuple' B n -> R) -> R
543 := fun ts {R} => @mapi_with'_cps2_specialized R T A B (fun n t a => @f n t a R) n i start ts.
544
545 Definition mapi_with_cps2 {S A B n}
546 (f: nat->S->A->forall {T}, (S*B->T)->T) (start:S) (ys:tuple A n) {T}
547 : (S * tuple B n->T)->T :=
548 match n as n0 return (tuple A n0 -> (S * tuple B n0->T)->T) with
549 | O => fun ys ret => ret (start, tt)
550 | S n' => fun ys ret => mapi_with'_cps2 0%nat f start ys ret
551 end ys.
552
553 Lemma unfold_mapi_with'_cps2 {T A B n} i
554 (f: nat->T->A->forall {R}, (T*B->R)->R) (start:T)
555 : @mapi_with'_cps2 T A B n i f start
556 = match n as n0 return (tuple' A n0 -> forall {R}, (T * tuple' B n0->R)->R) with
557 | O => fun ys T ret => f i start ys ret
558 | S n' => fun ys T ret =>
559 f i start (hd ys) (fun sb =>
560 mapi_with'_cps2 (S i) f (fst sb) (tl ys)
561 (fun r => ret (fst r, (snd r, snd sb))))
562 end.
563 Proof. destruct n; reflexivity. Qed.
564
565 Lemma mapi_with'_cps2_correct {S A B n} : forall i f start xs T ret,
566 (forall i s a R (ret:_->R), f i s a R ret = ret (f i s a _ id)) ->
567 @mapi_with'_cps2 S A B n i f start xs T ret = ret (mapi_with' i (fun i s a => f i s a _ id) start xs).
568 Proof. induction n as [|n IHn]; intros i f start xs T ret H; simpl; rewrite H, ?IHn by assumption; reflexivity. Qed.
569 Lemma mapi_with_cps2_correct {S A B n} f start xs T ret
570 (H:forall i s a R (ret:_->R), f i s a R ret = ret (f i s a _ id))
571 : @mapi_with_cps2 S A B n f start xs T ret = ret (mapi_with (fun i s a => f i s a _ id) start xs).
572 Proof. destruct n; simpl; rewrite ?mapi_with'_cps2_correct by assumption; reflexivity. Qed.
573 #[global]
574 Hint Rewrite @mapi_with_cps2_correct @mapi_with'_cps2_correct
575 using (intros; autorewrite with uncps; auto): uncps.
576
577 Fixpoint left_append_cps {A n} (x:A) (xs:tuple A n) {R} :
578 (tuple A (S n) -> R) -> R :=
579 match
580 n as n0 return (tuple A n0 -> (tuple A (S n0) -> R) -> R)
581 with
582 | 0%nat => fun _ f => f x
583 | S n' =>
584 fun xs f =>
585 left_append_cps x (tl xs) (fun r => f (append (hd xs) r))
586 end xs.
587 Lemma left_append_cps_correct A n x xs R f :
588 @left_append_cps A n x xs R f = f (left_append x xs).
589 Proof.
590 induction n; [reflexivity|].
591 simpl left_append. simpl left_append_cps.
592 rewrite IHn. reflexivity.
593 Qed.
594
595 Definition tl_cps {A n} (xs : tuple A (S n)) {R} :
596 (tuple A n -> R) -> R :=
597 match
598 n as n0 return (tuple A (S n0) -> (tuple A n0 -> R) -> R)
599 with
600 | 0%nat => fun _ f => f tt
601 | S n' => fun xs f => f (fst xs)
602 end xs.
603 Lemma tl_cps_correct A n xs R f :
604 @tl_cps A n xs R f = f (tl xs).
605 Proof. destruct n; reflexivity. Qed.
606
607 Definition hd_cps {A n} (xs : tuple A (S n)) {R} :
608 (A -> R) -> R :=
609 match
610 n as n0 return (tuple A (S n0) -> (A -> R) -> R)
611 with
612 | 0%nat => fun x f => f x
613 | S n' => fun xs f => f (snd xs)
614 end xs.
615 Lemma hd_cps_correct A n xs R f :
616 @hd_cps A n xs R f = f (hd xs).
617 Proof. destruct n; reflexivity. Qed.
618
619 Fixpoint left_tl_cps {A n} (xs : tuple A (S n)) {R} :
620 (tuple A n -> R) -> R :=
621 match
622 n as n0 return (tuple A (S n0) -> (tuple A n0 -> R) -> R)
623 with
624 | 0%nat => fun _ f => f tt
625 | S n' =>
626 fun xs f =>
627 tl_cps xs (fun xtl => hd_cps xs (fun xhd =>
628 left_tl_cps xtl (fun r => f (append xhd r))))
629 end xs.
630 Lemma left_tl_cps_correct A n xs R f :
631 @left_tl_cps A n xs R f = f (left_tl xs).
632 Proof.
633 induction n; [reflexivity|].
634 simpl left_tl. simpl left_tl_cps.
635 rewrite IHn. reflexivity.
636 Qed.
637
638 Fixpoint left_hd_cps {A n} (xs : tuple A (S n)) {R} :
639 (A -> R) -> R :=
640 match
641 n as n0 return (tuple A (S n0) -> (A -> R) -> R)
642 with
643 | 0%nat => fun x f => f x
644 | S n' =>
645 fun xs f => tl_cps xs (fun xtl => left_hd_cps xtl f)
646 end xs.
647 Lemma left_hd_cps_correct A n xs R f :
648 @left_hd_cps A n xs R f = f (left_hd xs).
649 Proof.
650 induction n; [reflexivity|].
651 simpl left_hd. simpl left_hd_cps.
652 rewrite IHn. reflexivity.
653 Qed.
654
655 Lemma In_left_hd {A} n (p : tuple A (S n))
656 : In (left_hd p) (to_list _ p).
657 Proof.
658 simpl in *.
659 induction n as [|n IHn].
660 { left; reflexivity. }
661 { destruct p; simpl in *; right; auto. }
662 Qed.
663
664 Lemma In_to_list_left_tl {A} n (p : tuple A (S n)) x
665 : In x (to_list n (left_tl p)) -> In x (to_list (S n) p).
666 Proof.
667 simpl in *.
668 remember (to_list n (left_tl p)) as ls eqn:H.
669 intro Hin; revert Hin H.
670 revert n p.
671 induction ls as [|l ls IHls], n.
672 { simpl; intros ? []. }
673 { simpl; intros ? []. }
674 { simpl; congruence. }
675 { simpl; intros [? p] [H0|H1] H; simpl in *;
676 setoid_rewrite to_list_append in H;
677 inversion H; clear H; subst; auto. }
678 Qed.
679
680 Lemma to_list_left_append {A} {n} (p:tuple A n) x
681 : (to_list _ (left_append x p)) = to_list n p ++ x :: nil.
682 Proof.
683 destruct n as [|n]; simpl in *; [ reflexivity | ].
684 induction n as [|n IHn]; simpl in *; [ reflexivity | ].
685 destruct p; simpl in *; rewrite IHn; simpl; reflexivity.
686 Qed.
687 End Tuple.
688 #[global]
689 Hint Rewrite @Tuple.map_cps_correct @Tuple.left_append_cps_correct @Tuple.left_tl_cps_correct @Tuple.left_hd_cps_correct @Tuple.tl_cps_correct @Tuple.hd_cps_correct : uncps.
690 #[global]
691 Hint Rewrite @Tuple.mapi_with_cps_correct @Tuple.mapi_with'_cps_correct @Tuple.mapi_with_cps2_correct @Tuple.mapi_with'_cps2_correct
692 using (intros; autorewrite with uncps; auto): uncps.