| 1 |
From Coq Require Import List. |
| 2 |
From Coq Require Import SetoidList. |
| 3 |
From Coq Require Import Lia. |
| 4 |
From Coq Require Import Peano_dec. |
| 5 |
From Coq Require Import ZArith. |
| 6 |
From Coq Require Import Arith. |
| 7 |
From Coq Require Import Morphisms. |
| 8 |
Require Import Crypto.Util.NatUtil. |
| 9 |
Require Import Crypto.Util.Pointed. |
| 10 |
Require Import Crypto.Util.Prod. |
| 11 |
Require Import Crypto.Util.Decidable. |
| 12 |
Require Crypto.Util.Option. |
| 13 |
Require Export Crypto.Util.FixCoqMistakes. |
| 14 |
Require Export Crypto.Util.Tactics.BreakMatch. |
| 15 |
Require Export Crypto.Util.Tactics.DestructHead. |
| 16 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 17 |
Require Import Crypto.Util.Tactics.RewriteHyp. |
| 18 |
Require Import Crypto.Util.Tactics.ConstrFail. |
| 19 |
Require Import Crypto.Util.Tactics.SplitInContext. |
| 20 |
Import ListNotations. |
| 21 |
Local Open Scope list_scope. |
| 22 |
|
| 23 |
Scheme Equality for list. |
| 24 |
|
| 25 |
Definition list_case |
| 26 |
{A} (P : list A -> Type) (N : P nil) (C : forall x xs, P (cons x xs)) |
| 27 |
(ls : list A) |
| 28 |
: P ls |
| 29 |
:= match ls return P ls with |
| 30 |
| nil => N |
| 31 |
| cons x xs => C x xs |
| 32 |
end. |
| 33 |
|
| 34 |
Definition list_case_nodep |
| 35 |
{A} (P : Type) (N : P) (C : A -> list A -> P) |
| 36 |
(ls : list A) |
| 37 |
: P |
| 38 |
:= match ls with |
| 39 |
| nil => N |
| 40 |
| cons x xs => C x xs |
| 41 |
end. |
| 42 |
|
| 43 |
|
| 44 |
Global Instance list_rect_Proper_dep_gen {A P} (RP : forall x : list A, P x -> P x -> Prop) |
| 45 |
: Proper (RP nil ==> forall_relation (fun x => forall_relation (fun xs => RP xs ==> RP (cons x xs))) ==> forall_relation RP) (@list_rect A P) | 10. |
| 46 |
Proof. |
| 47 |
cbv [forall_relation respectful]; intros N N' HN C C' HC ls. |
| 48 |
induction ls as [|l ls IHls]; cbn [list_rect]; |
| 49 |
repeat first [ apply IHls | apply HC | apply HN | progress intros | reflexivity ]. |
| 50 |
Qed. |
| 51 |
Global Instance list_rect_Proper_dep {A P} : Proper (eq ==> forall_relation (fun _ => forall_relation (fun _ => forall_relation (fun _ => eq))) ==> forall_relation (fun _ => eq)) (@list_rect A P) | 1. |
| 52 |
Proof. |
| 53 |
cbv [forall_relation respectful Proper]; intros; eapply (@list_rect_Proper_dep_gen A P (fun _ => eq)); cbv [forall_relation respectful]; intros; subst; eauto. |
| 54 |
Qed. |
| 55 |
Global Instance list_rect_arrow_Proper_dep {A P Q} : Proper ((eq ==> eq) ==> forall_relation (fun _ => forall_relation (fun _ => (eq ==> eq) ==> (eq ==> eq))) ==> forall_relation (fun _ => eq ==> eq)) (@list_rect A (fun x => P x -> Q x)) | 10. |
| 56 |
Proof. |
| 57 |
cbv [forall_relation respectful Proper]; intros; eapply (@list_rect_Proper_dep_gen A (fun x => P x -> Q x) (fun _ => eq ==> eq)%signature); intros; subst; eauto. |
| 58 |
Qed. |
| 59 |
Global Instance list_case_Proper_dep {A P} : Proper (eq ==> forall_relation (fun _ => forall_relation (fun _ => eq)) ==> forall_relation (fun _ => eq)) (@list_case A P) | 1. |
| 60 |
Proof. |
| 61 |
cbv [forall_relation]; intros N N' ? C C' HC ls; subst N'; revert N; destruct ls; eauto. |
| 62 |
Qed. |
| 63 |
Global Instance list_rect_Proper_gen {A P} R |
| 64 |
: Proper (R ==> (eq ==> eq ==> R ==> R) ==> eq ==> R) (@list_rect A (fun _ => P)) | 10. |
| 65 |
Proof. repeat intro; subst; apply (@list_rect_Proper_dep_gen A (fun _ => P) (fun _ => R)); cbv [forall_relation respectful] in *; eauto. Qed. |
| 66 |
Global Instance list_rect_Proper {A P} : Proper (eq ==> pointwise_relation _ (pointwise_relation _ (pointwise_relation _ eq)) ==> eq ==> eq) (@list_rect A (fun _ => P)). |
| 67 |
Proof. repeat intro; subst; apply (@list_rect_Proper_dep A (fun _ => P)); eauto. Qed. |
| 68 |
Global Instance list_rect_arrow_Proper {A P Q} |
| 69 |
: Proper ((eq ==> eq) ==> (eq ==> eq ==> (eq ==> eq) ==> eq ==> eq) ==> eq ==> eq ==> eq) |
| 70 |
(@list_rect A (fun _ => P -> Q)) | 10. |
| 71 |
Proof. eapply list_rect_Proper_gen. Qed. |
| 72 |
Global Instance list_case_Proper {A P} : Proper (eq ==> pointwise_relation _ (pointwise_relation _ eq) ==> eq ==> eq) (@list_case A (fun _ => P)). |
| 73 |
Proof. repeat intro; subst; apply (@list_case_Proper_dep A (fun _ => P)); eauto. Qed. |
| 74 |
|
| 75 |
Create HintDb distr_length discriminated. |
| 76 |
Create HintDb simpl_set_nth discriminated. |
| 77 |
Create HintDb simpl_update_nth discriminated. |
| 78 |
Create HintDb simpl_nth_default discriminated. |
| 79 |
Create HintDb simpl_nth_error discriminated. |
| 80 |
Create HintDb simpl_firstn discriminated. |
| 81 |
Create HintDb simpl_skipn discriminated. |
| 82 |
Create HintDb simpl_fold_right discriminated. |
| 83 |
Create HintDb simpl_fold_left discriminated. |
| 84 |
Create HintDb simpl_sum_firstn discriminated. |
| 85 |
Create HintDb push_map discriminated. |
| 86 |
Create HintDb push_combine discriminated. |
| 87 |
Create HintDb push_flat_map discriminated. |
| 88 |
Create HintDb push_rev discriminated. |
| 89 |
Create HintDb push_fold_right discriminated. |
| 90 |
Create HintDb push_fold_left discriminated. |
| 91 |
Create HintDb push_partition discriminated. |
| 92 |
Create HintDb pull_nth_error discriminated. |
| 93 |
Create HintDb push_nth_error discriminated. |
| 94 |
Create HintDb pull_nth_default discriminated. |
| 95 |
Create HintDb push_nth_default discriminated. |
| 96 |
Create HintDb pull_firstn discriminated. |
| 97 |
Create HintDb push_firstn discriminated. |
| 98 |
Create HintDb pull_skipn discriminated. |
| 99 |
Create HintDb push_skipn discriminated. |
| 100 |
Create HintDb push_sum discriminated. |
| 101 |
Create HintDb pull_update_nth discriminated. |
| 102 |
Create HintDb push_update_nth discriminated. |
| 103 |
Create HintDb znonzero discriminated. |
| 104 |
|
| 105 |
#[global] |
| 106 |
Hint Rewrite |
| 107 |
@app_length |
| 108 |
@rev_length |
| 109 |
@map_length |
| 110 |
@seq_length |
| 111 |
@fold_left_length |
| 112 |
@split_length_l |
| 113 |
@split_length_r |
| 114 |
@firstn_length |
| 115 |
@combine_length |
| 116 |
@prod_length |
| 117 |
: distr_length. |
| 118 |
|
| 119 |
#[global] |
| 120 |
Hint Rewrite |
| 121 |
rev_involutive |
| 122 |
: push_rev. |
| 123 |
|
| 124 |
Global Hint Extern 1 => progress autorewrite with distr_length in * : distr_length. |
| 125 |
Ltac distr_length := autorewrite with distr_length in *; |
| 126 |
try solve [simpl in *; intros; (idtac + exfalso); lia]. |
| 127 |
|
| 128 |
Module Export List. |
| 129 |
Local Set Implicit Arguments. |
| 130 |
Import ListNotations. |
| 131 |
(** From the 8.6 Standard Library *) |
| 132 |
|
| 133 |
Section Elts. |
| 134 |
Variable A : Type. |
| 135 |
|
| 136 |
(** Results about [nth_error] *) |
| 137 |
|
| 138 |
Lemma nth_error_In l n (x : A) : nth_error l n = Some x -> In x l. |
| 139 |
Proof using Type. |
| 140 |
revert n. induction l as [|a l IH]; intros [|n]; simpl; try easy. |
| 141 |
- injection 1; auto. |
| 142 |
- eauto. |
| 143 |
Qed. |
| 144 |
End Elts. |
| 145 |
|
| 146 |
Section Map. |
| 147 |
Variables (A : Type) (B : Type). |
| 148 |
Variable f : A -> B. |
| 149 |
|
| 150 |
Lemma map_nil : forall A B (f : A -> B), map f nil = nil. |
| 151 |
Proof using Type. reflexivity. Qed. |
| 152 |
Lemma map_cons (x:A)(l:list A) : map f (x::l) = (f x) :: (map f l). |
| 153 |
Proof using Type. |
| 154 |
reflexivity. |
| 155 |
Qed. |
| 156 |
Lemma map_repeat x n : map f (List.repeat x n) = List.repeat (f x) n. |
| 157 |
Proof using Type. induction n; simpl List.repeat; simpl map; congruence. Qed. |
| 158 |
End Map. |
| 159 |
#[global] |
| 160 |
Hint Rewrite @map_cons @map_nil @map_repeat : push_map. |
| 161 |
#[global] |
| 162 |
Hint Rewrite @map_app : push_map. |
| 163 |
|
| 164 |
Section FlatMap. |
| 165 |
Lemma flat_map_nil {A B} (f:A->list B) : List.flat_map f (@nil A) = nil. |
| 166 |
Proof. reflexivity. Qed. |
| 167 |
Lemma flat_map_cons {A B} (f:A->list B) x xs : |
| 168 |
(List.flat_map f (x::xs) = (f x++List.flat_map f xs))%list. |
| 169 |
Proof. reflexivity. Qed. |
| 170 |
End FlatMap. |
| 171 |
#[global] |
| 172 |
Hint Rewrite @flat_map_cons @flat_map_nil : push_flat_map. |
| 173 |
|
| 174 |
Lemma rev_cons {A} x ls : @rev A (x :: ls) = rev ls ++ [x]. Proof. reflexivity. Qed. |
| 175 |
#[global] |
| 176 |
Hint Rewrite @rev_cons : list. |
| 177 |
|
| 178 |
Section FoldRight. |
| 179 |
Context {A B} (f:B->A->A). |
| 180 |
Lemma fold_right_nil : forall {A B} (f:B->A->A) a, |
| 181 |
List.fold_right f a nil = a. |
| 182 |
Proof using Type. reflexivity. Qed. |
| 183 |
Lemma fold_right_cons : forall a b bs, |
| 184 |
fold_right f a (b::bs) = f b (fold_right f a bs). |
| 185 |
Proof using Type. reflexivity. Qed. |
| 186 |
Lemma fold_right_snoc a x ls: |
| 187 |
@fold_right A B f a (ls ++ [x]) = fold_right f (f x a) ls. |
| 188 |
Proof using Type. |
| 189 |
rewrite <-(rev_involutive ls), <-rev_cons. |
| 190 |
rewrite !fold_left_rev_right; reflexivity. |
| 191 |
Qed. |
| 192 |
End FoldRight. |
| 193 |
#[global] |
| 194 |
Hint Rewrite @fold_right_nil @fold_right_cons @fold_right_snoc : simpl_fold_right push_fold_right. |
| 195 |
|
| 196 |
Section Partition. |
| 197 |
Lemma partition_nil {A} (f:A->_) : partition f nil = (nil, nil). |
| 198 |
Proof. reflexivity. Qed. |
| 199 |
Lemma partition_cons {A} (f:A->_) x xs : partition f (x::xs) = |
| 200 |
if f x |
| 201 |
then (x :: (fst (partition f xs)), (snd (partition f xs))) |
| 202 |
else ((fst (partition f xs)), x :: (snd (partition f xs))). |
| 203 |
Proof. cbv [partition]; break_match; reflexivity. Qed. |
| 204 |
End Partition. |
| 205 |
#[global] |
| 206 |
Hint Rewrite @partition_nil @partition_cons : push_partition. |
| 207 |
|
| 208 |
Lemma in_seq len start n : |
| 209 |
In n (seq start len) <-> start <= n < start+len. |
| 210 |
Proof. |
| 211 |
revert start. induction len as [|len IHlen]; simpl; intros. |
| 212 |
- rewrite <- plus_n_O. split;[easy|]. |
| 213 |
intros (H,H'). apply (Nat.lt_irrefl _ (Nat.le_lt_trans _ _ _ H H')). |
| 214 |
- rewrite IHlen, <- plus_n_Sm; simpl; split. |
| 215 |
* intros [H|H]; subst; intuition auto with arith. |
| 216 |
* intros (H,H'). destruct (proj1 (Nat.lt_eq_cases _ _) H); intuition. |
| 217 |
Qed. |
| 218 |
|
| 219 |
Section Facts. |
| 220 |
|
| 221 |
Variable A : Type. |
| 222 |
|
| 223 |
Theorem length_zero_iff_nil (l : list A): |
| 224 |
length l = 0 <-> l=[]. |
| 225 |
Proof using Type. |
| 226 |
split; [now destruct l | now intros ->]. |
| 227 |
Qed. |
| 228 |
End Facts. |
| 229 |
|
| 230 |
Section Cutting. |
| 231 |
|
| 232 |
Variable A : Type. |
| 233 |
|
| 234 |
Local Notation firstn := (@firstn A). |
| 235 |
|
| 236 |
Lemma firstn_nil n: firstn n [] = []. |
| 237 |
Proof using Type. induction n; now simpl. Qed. |
| 238 |
|
| 239 |
Lemma firstn_cons n a l: firstn (S n) (a::l) = a :: (firstn n l). |
| 240 |
Proof using Type. now simpl. Qed. |
| 241 |
|
| 242 |
Lemma firstn_all l: firstn (length l) l = l. |
| 243 |
Proof using Type. induction l as [| ? ? H]; simpl; [reflexivity | now rewrite H]. Qed. |
| 244 |
|
| 245 |
Lemma firstn_all2 n: forall (l:list A), (length l) <= n -> firstn n l = l. |
| 246 |
Proof using Type. induction n as [|k iHk]. |
| 247 |
- intro l. inversion 1 as [H1|?]. |
| 248 |
rewrite (length_zero_iff_nil l) in H1. subst. now simpl. |
| 249 |
- destruct l as [|x xs]; simpl. |
| 250 |
* now reflexivity. |
| 251 |
* simpl. intro H. apply Peano.le_S_n in H. f_equal. apply iHk, H. |
| 252 |
Qed. |
| 253 |
|
| 254 |
Lemma firstn_O l: firstn 0 l = []. |
| 255 |
Proof using Type. now simpl. Qed. |
| 256 |
|
| 257 |
Lemma firstn_le_length n: forall l:list A, length (firstn n l) <= n. |
| 258 |
Proof using Type. |
| 259 |
induction n as [|k iHk]; simpl; [auto | destruct l as [|x xs]; simpl]. |
| 260 |
- auto with arith. |
| 261 |
- apply le_n_S, iHk. |
| 262 |
Qed. |
| 263 |
|
| 264 |
Lemma firstn_length_le: forall l:list A, forall n:nat, |
| 265 |
n <= length l -> length (firstn n l) = n. |
| 266 |
Proof using Type. induction l as [|x xs Hrec]. |
| 267 |
- simpl. intros n H. apply Nat.le_0_r in H. rewrite H. now simpl. |
| 268 |
- destruct n as [|n]. |
| 269 |
* now simpl. |
| 270 |
* simpl. intro H. apply le_S_n in H. now rewrite (Hrec n H). |
| 271 |
Qed. |
| 272 |
|
| 273 |
Lemma firstn_app n: |
| 274 |
forall l1 l2, |
| 275 |
firstn n (l1 ++ l2) = (firstn n l1) ++ (firstn (n - length l1) l2). |
| 276 |
Proof using Type. induction n as [|k iHk]; intros l1 l2. |
| 277 |
- now simpl. |
| 278 |
- destruct l1 as [|x xs]. |
| 279 |
* unfold List.firstn at 2, length. now rewrite 2!app_nil_l, Nat.sub_0_r. |
| 280 |
* rewrite <- app_comm_cons. simpl. f_equal. apply iHk. |
| 281 |
Qed. |
| 282 |
|
| 283 |
Lemma firstn_app_2 n: |
| 284 |
forall l1 l2, |
| 285 |
firstn ((length l1) + n) (l1 ++ l2) = l1 ++ firstn n l2. |
| 286 |
Proof using Type. induction n as [| k iHk];intros l1 l2. |
| 287 |
- unfold List.firstn at 2. rewrite <- plus_n_O, app_nil_r. |
| 288 |
rewrite firstn_app. rewrite Nat.sub_diag. |
| 289 |
unfold List.firstn at 2. rewrite app_nil_r. apply firstn_all. |
| 290 |
- destruct l2 as [|x xs]. |
| 291 |
* simpl. rewrite app_nil_r. apply firstn_all2. auto with arith. |
| 292 |
* rewrite firstn_app. assert (H0 : (length l1 + S k - length l1) = S k). |
| 293 |
auto with arith. |
| 294 |
rewrite H0, firstn_all2; [reflexivity | auto with arith]. |
| 295 |
Qed. |
| 296 |
|
| 297 |
Lemma firstn_firstn: |
| 298 |
forall l:list A, |
| 299 |
forall i j : nat, |
| 300 |
firstn i (firstn j l) = firstn (min i j) l. |
| 301 |
Proof using Type. induction l as [|x xs Hl]. |
| 302 |
- intros. simpl. now rewrite ?firstn_nil. |
| 303 |
- destruct i. |
| 304 |
* intro. now simpl. |
| 305 |
* destruct j. |
| 306 |
+ now simpl. |
| 307 |
+ simpl. f_equal. apply Hl. |
| 308 |
Qed. |
| 309 |
|
| 310 |
End Cutting. |
| 311 |
|
| 312 |
Lemma fold_right_rev_left A B (f : B -> A -> B) (l : list A) (i : B) |
| 313 |
: fold_left f (rev l) i = fold_right (fun x y => f y x) i l. |
| 314 |
Proof. |
| 315 |
now rewrite <- fold_left_rev_right, rev_involutive. |
| 316 |
Qed. |
| 317 |
|
| 318 |
|
| 319 |
Section FoldLeft. |
| 320 |
Context {A B} (f:A->B->A). |
| 321 |
Lemma fold_left_nil : forall {A B} (f:A->B->A) a, |
| 322 |
List.fold_left f nil a = a. |
| 323 |
Proof using Type. reflexivity. Qed. |
| 324 |
Lemma fold_left_cons : forall a b bs, |
| 325 |
fold_left f (b::bs) a = fold_left f bs (f a b). |
| 326 |
Proof using Type. reflexivity. Qed. |
| 327 |
Lemma fold_left_snoc a x ls: |
| 328 |
@fold_left A B f (ls ++ [x]) a = f (fold_left f ls a) x. |
| 329 |
Proof using Type. |
| 330 |
rewrite <-(rev_involutive ls), <-rev_cons. |
| 331 |
rewrite !fold_right_rev_left; reflexivity. |
| 332 |
Qed. |
| 333 |
End FoldLeft. |
| 334 |
#[global] |
| 335 |
Hint Rewrite @fold_left_nil @fold_left_cons @fold_left_snoc : simpl_fold_left push_fold_left. |
| 336 |
|
| 337 |
(** new operations *) |
| 338 |
Definition enumerate {A} (ls : list A) : list (nat * A) |
| 339 |
:= combine (seq 0 (length ls)) ls. |
| 340 |
|
| 341 |
Section map2. |
| 342 |
Context {A B C} |
| 343 |
(f : A -> B -> C). |
| 344 |
|
| 345 |
Fixpoint map2 (la : list A) (lb : list B) : list C := |
| 346 |
match la, lb with |
| 347 |
| nil, _ => nil |
| 348 |
| _, nil => nil |
| 349 |
| a :: la', b :: lb' |
| 350 |
=> f a b :: map2 la' lb' |
| 351 |
end. |
| 352 |
End map2. |
| 353 |
End List. |
| 354 |
|
| 355 |
#[global] |
| 356 |
Hint Rewrite @firstn_skipn : simpl_firstn. |
| 357 |
#[global] |
| 358 |
Hint Rewrite @firstn_skipn : simpl_skipn. |
| 359 |
#[global] |
| 360 |
Hint Rewrite @firstn_nil @firstn_cons @List.firstn_all @firstn_O @firstn_app_2 @List.firstn_firstn : push_firstn. |
| 361 |
#[global] |
| 362 |
Hint Rewrite @firstn_nil @firstn_cons @List.firstn_all @firstn_O @firstn_app_2 @List.firstn_firstn : simpl_firstn. |
| 363 |
#[global] |
| 364 |
Hint Rewrite @firstn_app : push_firstn. |
| 365 |
#[global] |
| 366 |
Hint Rewrite <- @firstn_cons @firstn_app @List.firstn_firstn : pull_firstn. |
| 367 |
#[global] |
| 368 |
Hint Rewrite @firstn_all2 @removelast_firstn @firstn_removelast using lia : push_firstn. |
| 369 |
#[global] |
| 370 |
Hint Rewrite @firstn_all2 @removelast_firstn @firstn_removelast using lia : simpl_firstn. |
| 371 |
|
| 372 |
Local Arguments value / _ _. |
| 373 |
Local Arguments error / _. |
| 374 |
|
| 375 |
Definition list_beq_hetero {A B} (f : A -> B -> bool) |
| 376 |
:= fix list_beq_hetero (l1 : list A) (l2 : list B) : bool |
| 377 |
:= match l1, l2 with |
| 378 |
| [], [] => true |
| 379 |
| [], _ | _, [] => false |
| 380 |
| x :: xs, y :: ys => f x y && list_beq_hetero xs ys |
| 381 |
end%bool. |
| 382 |
|
| 383 |
Definition sum_firstn l n := fold_right Z.add 0%Z (firstn n l). |
| 384 |
|
| 385 |
Definition sum xs := sum_firstn xs (length xs). |
| 386 |
|
| 387 |
(* xs[n] := f xs[n] *) |
| 388 |
Fixpoint update_nth {T} n f (xs:list T) {struct n} := |
| 389 |
match n with |
| 390 |
| O => match xs with |
| 391 |
| nil => nil |
| 392 |
| x'::xs' => f x'::xs' |
| 393 |
end |
| 394 |
| S n' => match xs with |
| 395 |
| nil => nil |
| 396 |
| x'::xs' => x'::update_nth n' f xs' |
| 397 |
end |
| 398 |
end. |
| 399 |
|
| 400 |
(* xs[n] := x *) |
| 401 |
Definition set_nth {T} n x (xs:list T) |
| 402 |
:= update_nth n (fun _ => x) xs. |
| 403 |
|
| 404 |
Definition splice_nth {T} n (x:T) xs := firstn n xs ++ x :: skipn (S n) xs. |
| 405 |
#[global] |
| 406 |
Hint Unfold splice_nth : core. |
| 407 |
|
| 408 |
Fixpoint take_while {T} (f : T -> bool) (ls : list T) : list T |
| 409 |
:= match ls with |
| 410 |
| nil => nil |
| 411 |
| cons x xs => if f x then x :: @take_while T f xs else nil |
| 412 |
end. |
| 413 |
|
| 414 |
Fixpoint drop_while {T} (f : T -> bool) (ls : list T) : list T |
| 415 |
:= match ls with |
| 416 |
| nil => nil |
| 417 |
| cons x xs => if f x then @drop_while T f xs else x :: xs |
| 418 |
end. |
| 419 |
Ltac boring := |
| 420 |
simpl; intuition auto with zarith datatypes; |
| 421 |
repeat match goal with |
| 422 |
| [ H : _ |- _ ] => rewrite H; clear H |
| 423 |
| [ |- context[match ?pf with end] ] => solve [ case pf ] |
| 424 |
| _ => progress autounfold in * |
| 425 |
| _ => progress autorewrite with core |
| 426 |
| _ => progress simpl in * |
| 427 |
| _ => progress intuition auto with zarith datatypes |
| 428 |
end; eauto. |
| 429 |
|
| 430 |
Ltac boring_list := |
| 431 |
repeat match goal with |
| 432 |
| _ => progress boring |
| 433 |
| _ => progress autorewrite with distr_length simpl_nth_default simpl_update_nth simpl_set_nth simpl_nth_error in * |
| 434 |
end. |
| 435 |
|
| 436 |
Section Relations. |
| 437 |
Fixpoint list_eq {A B} eq (x : list A) (y : list B) := |
| 438 |
match x, y with |
| 439 |
| [], [] => True |
| 440 |
| [], _ | _, [] => False |
| 441 |
| x :: xs, y :: ys => eq x y /\ list_eq eq xs ys |
| 442 |
end. |
| 443 |
|
| 444 |
Local Ltac t := |
| 445 |
repeat first [ progress cbn in * |
| 446 |
| progress break_match |
| 447 |
| intro |
| 448 |
| intuition congruence |
| 449 |
| progress destruct_head'_and |
| 450 |
| solve [ apply reflexivity |
| 451 |
| apply symmetry; eassumption |
| 452 |
| eapply transitivity; eassumption |
| 453 |
| eauto ] ]. |
| 454 |
|
| 455 |
Fixpoint list_eq_refl {T} {R} {Reflexive_R:@Reflexive T R} (ls : list T) : list_eq R ls ls. |
| 456 |
Proof. destruct ls; cbn; repeat split; auto. Defined. |
| 457 |
Global Instance Reflexive_list_eq {T} {R} {Reflexive_R:@Reflexive T R} |
| 458 |
: Reflexive (list_eq R) | 1 |
| 459 |
:= list_eq_refl. |
| 460 |
|
| 461 |
Lemma list_eq_sym {A B} {R1 R2 : _ -> _ -> Prop} (HR : forall v1 v2, R1 v1 v2 -> R2 v2 v1) |
| 462 |
: forall v1 v2, @list_eq A B R1 v1 v2 -> list_eq R2 v2 v1. |
| 463 |
Proof. induction v1; t. Qed. |
| 464 |
|
| 465 |
Lemma list_eq_trans {A B C} {R1 R2 R3 : _ -> _ -> Prop} |
| 466 |
(HR : forall v1 v2 v3, R1 v1 v2 -> R2 v2 v3 -> R3 v1 v3) |
| 467 |
: forall v1 v2 v3, @list_eq A B R1 v1 v2 -> @list_eq B C R2 v2 v3 -> @list_eq A C R3 v1 v3. |
| 468 |
Proof. induction v1; t. Qed. |
| 469 |
|
| 470 |
Global Instance Transitive_list_eq {T} {R} {Transitive_R:@Transitive T R} |
| 471 |
: Transitive (list_eq R) | 1 := list_eq_trans Transitive_R. |
| 472 |
|
| 473 |
Global Instance Symmetric_list_eq {T} {R} {Symmetric_R:@Symmetric T R} |
| 474 |
: Symmetric (list_eq R) | 1 := list_eq_sym Symmetric_R. |
| 475 |
|
| 476 |
Global Instance Equivalence_list_eq {T} {R} {Equivalence_R:@Equivalence T R} |
| 477 |
: Equivalence (list_eq R). Proof. split; exact _. Qed. |
| 478 |
End Relations. |
| 479 |
|
| 480 |
Definition list_leq_to_eq {A} {x y : list A} : x = y -> list_eq eq x y. |
| 481 |
Proof. destruct 1; reflexivity. Defined. |
| 482 |
|
| 483 |
Fixpoint list_eq_to_leq {A} {x y : list A} {struct x} : list_eq eq x y -> x = y. |
| 484 |
Proof. |
| 485 |
destruct x, y; cbn; try reflexivity; destruct 1; try apply f_equal2; eauto. |
| 486 |
Defined. |
| 487 |
|
| 488 |
Lemma list_leq_to_eq_refl {A x} : @list_leq_to_eq A x x eq_refl = reflexivity _. |
| 489 |
Proof. destruct x; cbn; reflexivity. Qed. |
| 490 |
|
| 491 |
Lemma list_eq_to_leq_refl {A x} : @list_eq_to_leq A x x (reflexivity _) = eq_refl. |
| 492 |
Proof. |
| 493 |
induction x as [|x xs IH]; cbn; rewrite ?IH; try reflexivity. |
| 494 |
Qed. |
| 495 |
|
| 496 |
Lemma list_leq_to_eq_to_leq {A x y} v : @list_eq_to_leq A x y (@list_leq_to_eq A x y v) = v. |
| 497 |
Proof. |
| 498 |
now subst; rewrite list_leq_to_eq_refl, list_eq_to_leq_refl. |
| 499 |
Qed. |
| 500 |
|
| 501 |
Lemma list_eq_to_leq_to_eq {A x y} v : @list_leq_to_eq A x y (@list_eq_to_leq A x y v) = v. |
| 502 |
Proof. |
| 503 |
revert y v. |
| 504 |
induction x as [|x xs IH], y as [|y ys]; try specialize (IH ys); cbn. |
| 505 |
1-3: intro H; destruct H; reflexivity. |
| 506 |
intro H; destruct H as [? H]; subst; specialize (IH H). |
| 507 |
cbv [list_leq_to_eq] in *; subst. |
| 508 |
break_innermost_match_hyps; subst; reflexivity. |
| 509 |
Qed. |
| 510 |
|
| 511 |
Lemma UIP_nil {A} (p q : @nil A = @nil A) : p = q. |
| 512 |
Proof. |
| 513 |
rewrite <- (list_leq_to_eq_to_leq p), <- (list_leq_to_eq_to_leq q); simpl; reflexivity. |
| 514 |
Qed. |
| 515 |
|
| 516 |
Lemma invert_list_eq {A x y} (p : @list_eq A A eq x y) : { pf : x = y | list_leq_to_eq pf = p }. |
| 517 |
Proof. eexists; apply list_eq_to_leq_to_eq. Qed. |
| 518 |
|
| 519 |
Lemma invert_eq_list {A x y} (p : x = y) : { pf : @list_eq A A eq x y | list_eq_to_leq pf = p }. |
| 520 |
Proof. eexists; apply list_leq_to_eq_to_leq. Qed. |
| 521 |
|
| 522 |
Ltac destr_list_eq H := |
| 523 |
lazymatch type of H with |
| 524 |
| True => first [ clear H | destruct H ] |
| 525 |
| False => destruct H |
| 526 |
| _ = _ /\ _ |
| 527 |
=> let H' := fresh in |
| 528 |
destruct H as [H' H]; |
| 529 |
destr_list_eq H |
| 530 |
| list_eq eq _ _ |
| 531 |
=> first [ apply list_eq_to_leq in H |
| 532 |
| let H' := fresh in |
| 533 |
rename H into H'; |
| 534 |
destruct (invert_list_eq H') as [H ?]; subst H' ] |
| 535 |
end. |
| 536 |
Ltac inversion_list_step := |
| 537 |
match goal with |
| 538 |
| [ H : nil = nil |- _ ] => clear H |
| 539 |
| [ H : cons _ _ = nil |- _ ] => solve [ inversion H ] |
| 540 |
| [ H : nil = cons _ _ |- _ ] => solve [ inversion H ] |
| 541 |
| [ H : nil = nil |- _ ] |
| 542 |
=> assert (eq_refl = H) by apply UIP_nil; subst H |
| 543 |
| [ H : cons _ _ = cons _ _ |- _ ] |
| 544 |
=> apply list_leq_to_eq in H; cbn [list_eq] in H; |
| 545 |
destr_list_eq H |
| 546 |
| [ H : cons _ _ = cons _ _ |- _ ] |
| 547 |
=> let H' := fresh in |
| 548 |
rename H into H'; |
| 549 |
destruct (invert_eq_list H') as [H ?]; subst H'; |
| 550 |
cbn [list_eq] in H; destr_list_eq H |
| 551 |
end. |
| 552 |
|
| 553 |
Ltac inversion_list := repeat inversion_list_step. |
| 554 |
|
| 555 |
Lemma list_bl_hetero {A B} {AB_beq : A -> B -> bool} {AB_R : A -> B -> Prop} |
| 556 |
(AB_bl : forall x y, AB_beq x y = true -> AB_R x y) |
| 557 |
: forall {x y}, |
| 558 |
list_beq_hetero AB_beq x y = true -> list_eq AB_R x y. |
| 559 |
Proof using Type. |
| 560 |
induction x, y; cbn in *; eauto; try congruence. |
| 561 |
rewrite Bool.andb_true_iff; intuition eauto. |
| 562 |
Qed. |
| 563 |
|
| 564 |
Lemma list_lb_hetero {A B} {AB_beq : A -> B -> bool} {AB_R : A -> B -> Prop} |
| 565 |
(AB_lb : forall x y, AB_R x y -> AB_beq x y = true) |
| 566 |
: forall {x y}, |
| 567 |
list_eq AB_R x y -> list_beq_hetero AB_beq x y = true. |
| 568 |
Proof using Type. |
| 569 |
induction x, y; cbn in *; rewrite ?Bool.andb_true_iff; intuition (congruence || eauto). |
| 570 |
Qed. |
| 571 |
|
| 572 |
Lemma list_beq_hetero_uniform {A : Type} A_beq {x y} |
| 573 |
: list_beq_hetero A_beq x y = @list_beq A A_beq x y. |
| 574 |
Proof. destruct x, y; cbn; reflexivity. Qed. |
| 575 |
|
| 576 |
Lemma list_bl_hetero_eq {A} |
| 577 |
{A_beq : A -> A -> bool} |
| 578 |
(A_bl : forall x y, A_beq x y = true -> x = y) |
| 579 |
{x y} |
| 580 |
: list_beq_hetero A_beq x y = true -> x = y. |
| 581 |
Proof using Type. rewrite list_beq_hetero_uniform; now apply internal_list_dec_bl. Qed. |
| 582 |
|
| 583 |
Lemma list_lb_hetero_eq {A} |
| 584 |
{A_beq : A -> A -> bool} |
| 585 |
(A_lb : forall x y, x = y -> A_beq x y = true) |
| 586 |
{x y} |
| 587 |
: x = y -> list_beq_hetero A_beq x y = true. |
| 588 |
Proof using Type. rewrite list_beq_hetero_uniform; now apply internal_list_dec_lb. Qed. |
| 589 |
|
| 590 |
Lemma eqlistA_bl {A eqA} {R : relation A} |
| 591 |
(H : forall x y : A, eqA x y = true -> R x y) |
| 592 |
: forall x y, list_beq A eqA x y = true -> eqlistA R x y. |
| 593 |
Proof. |
| 594 |
induction x, y; cbn; auto; try discriminate; constructor. |
| 595 |
all: rewrite Bool.andb_true_iff in *; destruct_head'_and; eauto. |
| 596 |
Qed. |
| 597 |
|
| 598 |
Lemma eqlistA_lb {A eqA} {R : relation A} |
| 599 |
(H : forall x y : A, R x y -> eqA x y = true) |
| 600 |
: forall x y, eqlistA R x y -> list_beq A eqA x y = true. |
| 601 |
Proof. |
| 602 |
induction x, y; cbn; auto; try discriminate; inversion 1; subst. |
| 603 |
all: rewrite Bool.andb_true_iff; eauto. |
| 604 |
Qed. |
| 605 |
|
| 606 |
Lemma nth_default_cons : forall {T} (x u0 : T) us, nth_default x (u0 :: us) 0 = u0. |
| 607 |
Proof. auto. Qed. |
| 608 |
|
| 609 |
#[global] |
| 610 |
Hint Rewrite @nth_default_cons : simpl_nth_default. |
| 611 |
#[global] |
| 612 |
Hint Rewrite @nth_default_cons : push_nth_default. |
| 613 |
|
| 614 |
Lemma nth_default_cons_S : forall {A} us (u0 : A) n d, |
| 615 |
nth_default d (u0 :: us) (S n) = nth_default d us n. |
| 616 |
Proof. boring. Qed. |
| 617 |
|
| 618 |
#[global] |
| 619 |
Hint Rewrite @nth_default_cons_S : simpl_nth_default. |
| 620 |
#[global] |
| 621 |
Hint Rewrite @nth_default_cons_S : push_nth_default. |
| 622 |
|
| 623 |
Lemma nth_default_nil : forall {T} n (d : T), nth_default d nil n = d. |
| 624 |
Proof. induction n; boring. Qed. |
| 625 |
|
| 626 |
#[global] |
| 627 |
Hint Rewrite @nth_default_nil : simpl_nth_default. |
| 628 |
#[global] |
| 629 |
Hint Rewrite @nth_default_nil : push_nth_default. |
| 630 |
|
| 631 |
Lemma nth_error_nil_error : forall {A} n, nth_error (@nil A) n = None. |
| 632 |
Proof. induction n; boring. Qed. |
| 633 |
|
| 634 |
#[global] |
| 635 |
Hint Rewrite @nth_error_nil_error : simpl_nth_error. |
| 636 |
|
| 637 |
Ltac nth_tac' := |
| 638 |
intros; simpl in *; unfold error,value in *; repeat progress (match goal with |
| 639 |
| [ |- context[nth_error nil ?n] ] => rewrite nth_error_nil_error |
| 640 |
| [ H: ?x = Some _ |- context[match ?x with Some _ => ?a | None => ?a end ] ] => destruct x |
| 641 |
| [ H: ?x = None _ |- context[match ?x with Some _ => ?a | None => ?a end ] ] => destruct x |
| 642 |
| [ |- context[match ?x with Some _ => ?a | None => ?a end ] ] => destruct x |
| 643 |
| [ |- context[match nth_error ?xs ?i with Some _ => _ | None => _ end ] ] => case_eq (nth_error xs i); intros |
| 644 |
| [ |- context[(if lt_dec ?a ?b then _ else _) = _] ] => destruct (lt_dec a b) |
| 645 |
| [ |- context[_ = (if lt_dec ?a ?b then _ else _)] ] => destruct (lt_dec a b) |
| 646 |
| [ H: context[(if lt_dec ?a ?b then _ else _) = _] |- _ ] => destruct (lt_dec a b) |
| 647 |
| [ H: context[_ = (if lt_dec ?a ?b then _ else _)] |- _ ] => destruct (lt_dec a b) |
| 648 |
| [ H: _ /\ _ |- _ ] => destruct H |
| 649 |
| [ H: Some _ = Some _ |- _ ] => injection H; clear H; intros; subst |
| 650 |
| [ H: None = Some _ |- _ ] => inversion H |
| 651 |
| [ H: Some _ = None |- _ ] => inversion H |
| 652 |
| [ |- Some _ = Some _ ] => apply f_equal |
| 653 |
end); eauto; try (autorewrite with list in *); try lia; eauto. |
| 654 |
Lemma nth_error_map {A B f n l} |
| 655 |
: nth_error (@map A B f l) n = option_map f (nth_error l n). |
| 656 |
Proof. revert n; induction l, n; nth_tac'. Qed. |
| 657 |
Lemma nth_error_map_ex : forall A B (f:A->B) i xs y, |
| 658 |
nth_error (map f xs) i = Some y -> |
| 659 |
exists x, nth_error xs i = Some x /\ f x = y. |
| 660 |
Proof. intros *; rewrite nth_error_map; edestruct nth_error; nth_tac'. Qed. |
| 661 |
|
| 662 |
Lemma nth_error_seq : forall i start len, |
| 663 |
nth_error (seq start len) i = |
| 664 |
if lt_dec i len |
| 665 |
then Some (start + i) |
| 666 |
else None. |
| 667 |
induction i as [|? IHi]; destruct len; nth_tac'; erewrite IHi; nth_tac'. |
| 668 |
Qed. |
| 669 |
|
| 670 |
Lemma nth_error_error_length : forall A i (xs:list A), nth_error xs i = None -> |
| 671 |
i >= length xs. |
| 672 |
Proof. |
| 673 |
induction i as [|? IHi]; destruct xs; nth_tac'; try match goal with H : _ |- _ => specialize (IHi _ H) end; lia. |
| 674 |
Qed. |
| 675 |
|
| 676 |
Lemma nth_error_value_length : forall A i (xs:list A) x, nth_error xs i = Some x -> |
| 677 |
i < length xs. |
| 678 |
Proof. |
| 679 |
induction i as [|? IHi]; destruct xs; nth_tac'; try match goal with H : _ |- _ => specialize (IHi _ _ H) end; lia. |
| 680 |
Qed. |
| 681 |
|
| 682 |
Lemma nth_error_length_error : forall A i (xs:list A), |
| 683 |
i >= length xs -> |
| 684 |
nth_error xs i = None. |
| 685 |
Proof. |
| 686 |
induction i as [|? IHi]; destruct xs; nth_tac'; rewrite IHi by lia; auto. |
| 687 |
Qed. |
| 688 |
Global Hint Resolve nth_error_length_error : core. |
| 689 |
#[global] |
| 690 |
Hint Rewrite @nth_error_length_error using lia : simpl_nth_error. |
| 691 |
|
| 692 |
Lemma map_nth_default : forall (A B : Type) (f : A -> B) n x y l, |
| 693 |
(n < length l) -> nth_default y (map f l) n = f (nth_default x l n). |
| 694 |
Proof. |
| 695 |
intros A B f n x y l H. |
| 696 |
unfold nth_default. |
| 697 |
erewrite map_nth_error. |
| 698 |
reflexivity. |
| 699 |
nth_tac'. |
| 700 |
let H0 := match goal with H0 : _ = None |- _ => H0 end in |
| 701 |
pose proof (nth_error_error_length A n l H0). |
| 702 |
lia. |
| 703 |
Qed. |
| 704 |
|
| 705 |
#[global] |
| 706 |
Hint Rewrite @map_nth_default using lia : push_nth_default. |
| 707 |
|
| 708 |
Ltac nth_tac := |
| 709 |
repeat progress (try nth_tac'; try (match goal with |
| 710 |
| [ H: nth_error (map _ _) _ = Some _ |- _ ] => destruct (nth_error_map_ex _ _ _ _ _ _ H); clear H |
| 711 |
| [ H: nth_error (seq _ _) _ = Some _ |- _ ] => rewrite nth_error_seq in H |
| 712 |
| [H: nth_error _ _ = None |- _ ] => specialize (nth_error_error_length _ _ _ H); intro; clear H |
| 713 |
end)). |
| 714 |
|
| 715 |
Lemma app_cons_app_app : forall T xs (y:T) ys, xs ++ y :: ys = (xs ++ (y::nil)) ++ ys. |
| 716 |
Proof. induction xs; boring. Qed. |
| 717 |
|
| 718 |
Lemma unfold_set_nth {T} n x |
| 719 |
: forall xs, |
| 720 |
@set_nth T n x xs |
| 721 |
= match n with |
| 722 |
| O => match xs with |
| 723 |
| nil => nil |
| 724 |
| x'::xs' => x::xs' |
| 725 |
end |
| 726 |
| S n' => match xs with |
| 727 |
| nil => nil |
| 728 |
| x'::xs' => x'::set_nth n' x xs' |
| 729 |
end |
| 730 |
end. |
| 731 |
Proof. |
| 732 |
induction n; destruct xs; reflexivity. |
| 733 |
Qed. |
| 734 |
|
| 735 |
Lemma simpl_set_nth_0 {T} x |
| 736 |
: forall xs, |
| 737 |
@set_nth T 0 x xs |
| 738 |
= match xs with |
| 739 |
| nil => nil |
| 740 |
| x'::xs' => x::xs' |
| 741 |
end. |
| 742 |
Proof. intro; rewrite unfold_set_nth; reflexivity. Qed. |
| 743 |
|
| 744 |
Lemma simpl_set_nth_S {T} x n |
| 745 |
: forall xs, |
| 746 |
@set_nth T (S n) x xs |
| 747 |
= match xs with |
| 748 |
| nil => nil |
| 749 |
| x'::xs' => x'::set_nth n x xs' |
| 750 |
end. |
| 751 |
Proof. intro; rewrite unfold_set_nth; reflexivity. Qed. |
| 752 |
|
| 753 |
#[global] |
| 754 |
Hint Rewrite @simpl_set_nth_S @simpl_set_nth_0 : simpl_set_nth. |
| 755 |
|
| 756 |
Lemma update_nth_ext {T} f g n |
| 757 |
: forall xs, (forall x, nth_error xs n = Some x -> f x = g x) |
| 758 |
-> @update_nth T n f xs = @update_nth T n g xs. |
| 759 |
Proof. |
| 760 |
induction n as [|n IHn]; destruct xs; simpl; intros H; |
| 761 |
try rewrite IHn; try rewrite H; |
| 762 |
try congruence; trivial. |
| 763 |
Qed. |
| 764 |
|
| 765 |
Global Instance update_nth_Proper {T} |
| 766 |
: Proper (eq ==> pointwise_relation _ eq ==> eq ==> eq) (@update_nth T). |
| 767 |
Proof. repeat intro; subst; apply update_nth_ext; trivial. Qed. |
| 768 |
|
| 769 |
Global Instance update_nth_Proper_eq {A} : Proper (eq ==> (eq ==> eq) ==> eq ==> eq) (@update_nth A) | 1. |
| 770 |
Proof. repeat intro; subst; apply update_nth_Proper; repeat intro; eauto. Qed. |
| 771 |
|
| 772 |
Lemma update_nth_id_eq_specific {T} f n |
| 773 |
: forall (xs : list T) (H : forall x, nth_error xs n = Some x -> f x = x), |
| 774 |
update_nth n f xs = xs. |
| 775 |
Proof. |
| 776 |
induction n as [|n IHn]; destruct xs; simpl; intros H; |
| 777 |
try rewrite IHn; try rewrite H; unfold value in *; |
| 778 |
try congruence; assumption. |
| 779 |
Qed. |
| 780 |
|
| 781 |
#[global] |
| 782 |
Hint Rewrite @update_nth_id_eq_specific using congruence : simpl_update_nth. |
| 783 |
|
| 784 |
Lemma update_nth_id_eq : forall {T} f (H : forall x, f x = x) n (xs : list T), |
| 785 |
update_nth n f xs = xs. |
| 786 |
Proof. intros; apply update_nth_id_eq_specific; trivial. Qed. |
| 787 |
|
| 788 |
#[global] |
| 789 |
Hint Rewrite @update_nth_id_eq using congruence : simpl_update_nth. |
| 790 |
|
| 791 |
Lemma update_nth_id : forall {T} n (xs : list T), |
| 792 |
update_nth n (fun x => x) xs = xs. |
| 793 |
Proof. intros; apply update_nth_id_eq; trivial. Qed. |
| 794 |
|
| 795 |
#[global] |
| 796 |
Hint Rewrite @update_nth_id : simpl_update_nth. |
| 797 |
|
| 798 |
Lemma nth_update_nth : forall m {T} (xs:list T) (n:nat) (f:T -> T), |
| 799 |
nth_error (update_nth m f xs) n = |
| 800 |
if eq_nat_dec n m |
| 801 |
then option_map f (nth_error xs n) |
| 802 |
else nth_error xs n. |
| 803 |
Proof. |
| 804 |
induction m as [|? IHm]. |
| 805 |
{ destruct n, xs; auto. } |
| 806 |
{ destruct xs, n; intros; simpl; auto; |
| 807 |
[ | rewrite IHm ]; clear IHm; |
| 808 |
edestruct eq_nat_dec; reflexivity. } |
| 809 |
Qed. |
| 810 |
|
| 811 |
#[global] |
| 812 |
Hint Rewrite @nth_update_nth : push_nth_error. |
| 813 |
#[global] |
| 814 |
Hint Rewrite <- @nth_update_nth : pull_nth_error. |
| 815 |
|
| 816 |
Lemma length_update_nth : forall {T} i f (xs:list T), length (update_nth i f xs) = length xs. |
| 817 |
Proof. |
| 818 |
induction i, xs; boring. |
| 819 |
Qed. |
| 820 |
|
| 821 |
#[global] |
| 822 |
Hint Rewrite @length_update_nth : distr_length. |
| 823 |
|
| 824 |
Lemma nth_set_nth : forall m {T} (xs:list T) (n:nat) x, |
| 825 |
nth_error (set_nth m x xs) n = |
| 826 |
if eq_nat_dec n m |
| 827 |
then (if lt_dec n (length xs) then Some x else None) |
| 828 |
else nth_error xs n. |
| 829 |
Proof. |
| 830 |
intros m T xs n x; unfold set_nth; rewrite nth_update_nth. |
| 831 |
destruct (nth_error xs n) eqn:?, (lt_dec n (length xs)) as [p|p]; |
| 832 |
rewrite <- nth_error_Some in p; |
| 833 |
solve [ reflexivity |
| 834 |
| exfalso; apply p; congruence ]. |
| 835 |
Qed. |
| 836 |
|
| 837 |
#[global] |
| 838 |
Hint Rewrite @nth_set_nth : push_nth_error. |
| 839 |
|
| 840 |
Lemma length_set_nth : forall {T} i x (xs:list T), length (set_nth i x xs) = length xs. |
| 841 |
Proof. intros; apply length_update_nth. Qed. |
| 842 |
|
| 843 |
#[global] |
| 844 |
Hint Rewrite @length_set_nth : distr_length. |
| 845 |
|
| 846 |
Lemma nth_error_length_exists_value : forall {A} (i : nat) (xs : list A), |
| 847 |
(i < length xs)%nat -> exists x, nth_error xs i = Some x. |
| 848 |
Proof. |
| 849 |
induction i, xs; boring; try lia. |
| 850 |
Qed. |
| 851 |
|
| 852 |
Lemma nth_error_length_not_error : forall {A} (i : nat) (xs : list A), |
| 853 |
nth_error xs i = None -> (i < length xs)%nat -> False. |
| 854 |
Proof. |
| 855 |
intros A i xs H H0. |
| 856 |
destruct (nth_error_length_exists_value i xs); intuition; congruence. |
| 857 |
Qed. |
| 858 |
|
| 859 |
Lemma nth_error_value_eq_nth_default : forall {T} i (x : T) xs, |
| 860 |
nth_error xs i = Some x -> forall d, nth_default d xs i = x. |
| 861 |
Proof. |
| 862 |
unfold nth_default; boring. |
| 863 |
Qed. |
| 864 |
|
| 865 |
#[global] |
| 866 |
Hint Rewrite @nth_error_value_eq_nth_default using eassumption : simpl_nth_default. |
| 867 |
|
| 868 |
Lemma skipn0 : forall {T} (xs:list T), skipn 0 xs = xs. |
| 869 |
Proof. auto. Qed. |
| 870 |
|
| 871 |
Lemma destruct_repeat : forall {A} xs y, (forall x : A, In x xs -> x = y) -> |
| 872 |
xs = nil \/ exists xs', xs = y :: xs' /\ (forall x : A, In x xs' -> x = y). |
| 873 |
Proof. |
| 874 |
destruct xs as [|? xs]; intros; try tauto. |
| 875 |
right. |
| 876 |
exists xs; split. |
| 877 |
+ f_equal; auto using in_eq. |
| 878 |
+ intros; auto using in_cons. |
| 879 |
Qed. |
| 880 |
|
| 881 |
Lemma splice_nth_equiv_update_nth : forall {T} n f d (xs:list T), |
| 882 |
splice_nth n (f (nth_default d xs n)) xs = |
| 883 |
if lt_dec n (length xs) |
| 884 |
then update_nth n f xs |
| 885 |
else xs ++ (f d)::nil. |
| 886 |
Proof. |
| 887 |
induction n, xs; boring_list; break_match; auto; lia. |
| 888 |
Qed. |
| 889 |
|
| 890 |
Lemma splice_nth_equiv_update_nth_update : forall {T} n f d (xs:list T), |
| 891 |
n < length xs -> |
| 892 |
splice_nth n (f (nth_default d xs n)) xs = update_nth n f xs. |
| 893 |
Proof. |
| 894 |
intros. |
| 895 |
rewrite splice_nth_equiv_update_nth; break_match; auto; lia. |
| 896 |
Qed. |
| 897 |
|
| 898 |
Lemma splice_nth_equiv_update_nth_snoc : forall {T} n f d (xs:list T), |
| 899 |
n >= length xs -> |
| 900 |
splice_nth n (f (nth_default d xs n)) xs = xs ++ (f d)::nil. |
| 901 |
Proof. |
| 902 |
intros. |
| 903 |
rewrite splice_nth_equiv_update_nth; break_match; auto; lia. |
| 904 |
Qed. |
| 905 |
|
| 906 |
Definition IMPOSSIBLE {T} : list T. exact nil. Qed. |
| 907 |
|
| 908 |
Ltac remove_nth_error := |
| 909 |
repeat match goal with |
| 910 |
| _ => exfalso; solve [ eauto using @nth_error_length_not_error ] |
| 911 |
| [ |- context[match nth_error ?ls ?n with _ => _ end] ] |
| 912 |
=> destruct (nth_error ls n) eqn:? |
| 913 |
end. |
| 914 |
|
| 915 |
Lemma update_nth_equiv_splice_nth: forall {T} n f (xs:list T), |
| 916 |
update_nth n f xs = |
| 917 |
if lt_dec n (length xs) |
| 918 |
then match nth_error xs n with |
| 919 |
| Some v => splice_nth n (f v) xs |
| 920 |
| None => IMPOSSIBLE |
| 921 |
end |
| 922 |
else xs. |
| 923 |
Proof. |
| 924 |
induction n as [|? IHn]; destruct xs; intros; |
| 925 |
autorewrite with simpl_update_nth simpl_nth_default in *; simpl in *; |
| 926 |
try (erewrite IHn; clear IHn); auto. |
| 927 |
repeat break_match; remove_nth_error; try reflexivity; try lia. |
| 928 |
Qed. |
| 929 |
|
| 930 |
Lemma splice_nth_equiv_set_nth : forall {T} n x (xs:list T), |
| 931 |
splice_nth n x xs = |
| 932 |
if lt_dec n (length xs) |
| 933 |
then set_nth n x xs |
| 934 |
else xs ++ x::nil. |
| 935 |
Proof. intros T n x xs; rewrite splice_nth_equiv_update_nth with (f := fun _ => x); auto. Qed. |
| 936 |
|
| 937 |
Lemma splice_nth_equiv_set_nth_set : forall {T} n x (xs:list T), |
| 938 |
n < length xs -> |
| 939 |
splice_nth n x xs = set_nth n x xs. |
| 940 |
Proof. intros T n x xs H; rewrite splice_nth_equiv_update_nth_update with (f := fun _ => x); auto. Qed. |
| 941 |
|
| 942 |
Lemma splice_nth_equiv_set_nth_snoc : forall {T} n x (xs:list T), |
| 943 |
n >= length xs -> |
| 944 |
splice_nth n x xs = xs ++ x::nil. |
| 945 |
Proof. intros T n x xs H; rewrite splice_nth_equiv_update_nth_snoc with (f := fun _ => x); auto. Qed. |
| 946 |
|
| 947 |
Lemma set_nth_equiv_splice_nth: forall {T} n x (xs:list T), |
| 948 |
set_nth n x xs = |
| 949 |
if lt_dec n (length xs) |
| 950 |
then splice_nth n x xs |
| 951 |
else xs. |
| 952 |
Proof. |
| 953 |
intros T n x xs; unfold set_nth; rewrite update_nth_equiv_splice_nth with (f := fun _ => x); auto. |
| 954 |
repeat break_match; remove_nth_error; trivial. |
| 955 |
Qed. |
| 956 |
|
| 957 |
Lemma combine_update_nth : forall {A B} n f g (xs:list A) (ys:list B), |
| 958 |
combine (update_nth n f xs) (update_nth n g ys) = |
| 959 |
update_nth n (fun xy => (f (fst xy), g (snd xy))) (combine xs ys). |
| 960 |
Proof. |
| 961 |
induction n as [|? IHn]; destruct xs, ys; simpl; try rewrite IHn; reflexivity. |
| 962 |
Qed. |
| 963 |
|
| 964 |
(* grumble, grumble, [rewrite] is bad at inferring the identity function, and constant functions *) |
| 965 |
Ltac rewrite_rev_combine_update_nth := |
| 966 |
let lem := match goal with |
| 967 |
| [ |- context[update_nth ?n (fun xy => (@?f xy, @?g xy)) (combine ?xs ?ys)] ] |
| 968 |
=> let f := match (eval cbv [fst] in (fun y x => f (x, y))) with |
| 969 |
| fun _ => ?f => f |
| 970 |
end in |
| 971 |
let g := match (eval cbv [snd] in (fun x y => g (x, y))) with |
| 972 |
| fun _ => ?g => g |
| 973 |
end in |
| 974 |
constr:(@combine_update_nth _ _ n f g xs ys) |
| 975 |
end in |
| 976 |
rewrite <- lem. |
| 977 |
|
| 978 |
Lemma combine_update_nth_l : forall {A B} n (f : A -> A) xs (ys:list B), |
| 979 |
combine (update_nth n f xs) ys = |
| 980 |
update_nth n (fun xy => (f (fst xy), snd xy)) (combine xs ys). |
| 981 |
Proof. |
| 982 |
intros ??? f xs ys. |
| 983 |
etransitivity; [ | apply combine_update_nth with (g := fun x => x) ]. |
| 984 |
rewrite update_nth_id; reflexivity. |
| 985 |
Qed. |
| 986 |
|
| 987 |
Lemma combine_update_nth_r : forall {A B} n (g : B -> B) (xs:list A) (ys:list B), |
| 988 |
combine xs (update_nth n g ys) = |
| 989 |
update_nth n (fun xy => (fst xy, g (snd xy))) (combine xs ys). |
| 990 |
Proof. |
| 991 |
intros ??? g xs ys. |
| 992 |
etransitivity; [ | apply combine_update_nth with (f := fun x => x) ]. |
| 993 |
rewrite update_nth_id; reflexivity. |
| 994 |
Qed. |
| 995 |
|
| 996 |
Lemma combine_set_nth : forall {A B} n (x:A) xs (ys:list B), |
| 997 |
combine (set_nth n x xs) ys = |
| 998 |
match nth_error ys n with |
| 999 |
| None => combine xs ys |
| 1000 |
| Some y => set_nth n (x,y) (combine xs ys) |
| 1001 |
end. |
| 1002 |
Proof. |
| 1003 |
intros A B n x xs ys; unfold set_nth; rewrite combine_update_nth_l. |
| 1004 |
nth_tac; |
| 1005 |
[ repeat rewrite_rev_combine_update_nth; apply f_equal2 |
| 1006 |
| assert (nth_error (combine xs ys) n = None) |
| 1007 |
by (apply nth_error_None; rewrite combine_length; lia * ) ]; |
| 1008 |
autorewrite with simpl_update_nth; reflexivity. |
| 1009 |
Qed. |
| 1010 |
|
| 1011 |
Lemma nth_error_value_In : forall {T} n xs (x:T), |
| 1012 |
nth_error xs n = Some x -> In x xs. |
| 1013 |
Proof. |
| 1014 |
induction n; destruct xs; nth_tac. |
| 1015 |
Qed. |
| 1016 |
|
| 1017 |
Lemma In_nth_error_value : forall {T} xs (x:T), |
| 1018 |
In x xs -> exists n, nth_error xs n = Some x. |
| 1019 |
Proof. |
| 1020 |
induction xs as [|?? IHxs]; nth_tac; destruct_head or; subst. |
| 1021 |
- exists 0; reflexivity. |
| 1022 |
- edestruct IHxs as [x0]; eauto. exists (S x0). eauto. |
| 1023 |
Qed. |
| 1024 |
|
| 1025 |
Lemma nth_value_index : forall {T} i xs (x:T), |
| 1026 |
nth_error xs i = Some x -> In i (seq 0 (length xs)). |
| 1027 |
Proof. |
| 1028 |
induction i as [|? IHi]; destruct xs; nth_tac; right. |
| 1029 |
rewrite <- seq_shift; apply in_map; eapply IHi; eauto. |
| 1030 |
Qed. |
| 1031 |
|
| 1032 |
Lemma nth_error_app : forall {T} n (xs ys:list T), nth_error (xs ++ ys) n = |
| 1033 |
if lt_dec n (length xs) |
| 1034 |
then nth_error xs n |
| 1035 |
else nth_error ys (n - length xs). |
| 1036 |
Proof. |
| 1037 |
induction n as [|n IHn]; destruct xs as [|? xs]; nth_tac; |
| 1038 |
rewrite IHn; destruct (lt_dec n (length xs)); trivial; lia. |
| 1039 |
Qed. |
| 1040 |
|
| 1041 |
Lemma nth_default_app : forall {T} n x (xs ys:list T), nth_default x (xs ++ ys) n = |
| 1042 |
if lt_dec n (length xs) |
| 1043 |
then nth_default x xs n |
| 1044 |
else nth_default x ys (n - length xs). |
| 1045 |
Proof. |
| 1046 |
intros T n x xs ys. |
| 1047 |
unfold nth_default. |
| 1048 |
rewrite nth_error_app. |
| 1049 |
destruct (lt_dec n (length xs)); auto. |
| 1050 |
Qed. |
| 1051 |
|
| 1052 |
#[global] |
| 1053 |
Hint Rewrite @nth_default_app : push_nth_default. |
| 1054 |
|
| 1055 |
Lemma combine_truncate_r : forall {A B} (xs : list A) (ys : list B), |
| 1056 |
combine xs ys = combine xs (firstn (length xs) ys). |
| 1057 |
Proof. |
| 1058 |
induction xs; destruct ys; boring. |
| 1059 |
Qed. |
| 1060 |
|
| 1061 |
Lemma combine_truncate_l : forall {A B} (xs : list A) (ys : list B), |
| 1062 |
combine xs ys = combine (firstn (length ys) xs) ys. |
| 1063 |
Proof. |
| 1064 |
induction xs; destruct ys; boring. |
| 1065 |
Qed. |
| 1066 |
|
| 1067 |
Lemma combine_app_samelength : forall {A B} (xs xs':list A) (ys ys':list B), |
| 1068 |
length xs = length ys -> |
| 1069 |
combine (xs ++ xs') (ys ++ ys') = combine xs ys ++ combine xs' ys'. |
| 1070 |
Proof. |
| 1071 |
induction xs, xs', ys, ys'; boring; lia. |
| 1072 |
Qed. |
| 1073 |
|
| 1074 |
Lemma map_fst_combine {A B} (xs:list A) (ys:list B) : List.map fst (List.combine xs ys) = List.firstn (length ys) xs. |
| 1075 |
Proof. |
| 1076 |
revert xs; induction ys; destruct xs; simpl; solve [ trivial | congruence ]. |
| 1077 |
Qed. |
| 1078 |
|
| 1079 |
Lemma map_snd_combine {A B} (xs:list A) (ys:list B) : List.map snd (List.combine xs ys) = List.firstn (length xs) ys. |
| 1080 |
Proof. |
| 1081 |
revert xs; induction ys; destruct xs; simpl; solve [ trivial | congruence ]. |
| 1082 |
Qed. |
| 1083 |
#[global] |
| 1084 |
Hint Rewrite @map_fst_combine @map_snd_combine : push_map. |
| 1085 |
|
| 1086 |
Lemma skipn_nil : forall {A} n, skipn n nil = @nil A. |
| 1087 |
Proof. destruct n; auto. Qed. |
| 1088 |
|
| 1089 |
#[global] |
| 1090 |
Hint Rewrite @skipn_nil : simpl_skipn. |
| 1091 |
#[global] |
| 1092 |
Hint Rewrite @skipn_nil : push_skipn. |
| 1093 |
|
| 1094 |
Lemma skipn_0 : forall {A} xs, @skipn A 0 xs = xs. |
| 1095 |
Proof. reflexivity. Qed. |
| 1096 |
|
| 1097 |
#[global] |
| 1098 |
Hint Rewrite @skipn_0 : simpl_skipn. |
| 1099 |
#[global] |
| 1100 |
Hint Rewrite @skipn_0 : push_skipn. |
| 1101 |
|
| 1102 |
Lemma skipn_cons_S : forall {A} n x xs, @skipn A (S n) (x::xs) = @skipn A n xs. |
| 1103 |
Proof. reflexivity. Qed. |
| 1104 |
|
| 1105 |
#[global] |
| 1106 |
Hint Rewrite @skipn_cons_S : simpl_skipn. |
| 1107 |
#[global] |
| 1108 |
Hint Rewrite @skipn_cons_S : push_skipn. |
| 1109 |
|
| 1110 |
Lemma skipn_app : forall {A} n (xs ys : list A), |
| 1111 |
skipn n (xs ++ ys) = skipn n xs ++ skipn (n - length xs) ys. |
| 1112 |
Proof. |
| 1113 |
induction n, xs, ys; boring. |
| 1114 |
Qed. |
| 1115 |
|
| 1116 |
#[global] |
| 1117 |
Hint Rewrite @skipn_app : push_skipn. |
| 1118 |
|
| 1119 |
Lemma skipn_skipn {A} n1 n2 (ls : list A) |
| 1120 |
: skipn n2 (skipn n1 ls) = skipn (n1 + n2) ls. |
| 1121 |
Proof. |
| 1122 |
revert n2 ls; induction n1, ls; |
| 1123 |
simpl; autorewrite with simpl_skipn; |
| 1124 |
boring. |
| 1125 |
Qed. |
| 1126 |
|
| 1127 |
#[global] |
| 1128 |
Hint Rewrite @skipn_skipn : simpl_skipn. |
| 1129 |
#[global] |
| 1130 |
Hint Rewrite <- @skipn_skipn : push_skipn. |
| 1131 |
#[global] |
| 1132 |
Hint Rewrite @skipn_skipn : pull_skipn. |
| 1133 |
|
| 1134 |
Lemma skipn_firstn {A} (ls : list A) n m |
| 1135 |
: skipn n (firstn m ls) = firstn (m - n) (skipn n ls). |
| 1136 |
Proof. |
| 1137 |
revert n m; induction ls, m, n; simpl; autorewrite with simpl_skipn simpl_firstn; boring_list. |
| 1138 |
Qed. |
| 1139 |
Lemma firstn_skipn_add {A} (ls : list A) n m |
| 1140 |
: firstn n (skipn m ls) = skipn m (firstn (m + n) ls). |
| 1141 |
Proof. |
| 1142 |
revert n m; induction ls, m; simpl; autorewrite with simpl_skipn simpl_firstn; boring_list. |
| 1143 |
Qed. |
| 1144 |
Lemma firstn_skipn_add' {A} (ls : list A) n m |
| 1145 |
: firstn n (skipn m ls) = skipn m (firstn (n + m) ls). |
| 1146 |
Proof. rewrite firstn_skipn_add; do 2 f_equal; auto with arith. Qed. |
| 1147 |
#[global] |
| 1148 |
Hint Rewrite <- @firstn_skipn_add @firstn_skipn_add' : simpl_firstn. |
| 1149 |
#[global] |
| 1150 |
Hint Rewrite <- @firstn_skipn_add @firstn_skipn_add' : simpl_skipn. |
| 1151 |
|
| 1152 |
Lemma firstn_app_inleft : forall {A} n (xs ys : list A), (n <= length xs)%nat -> |
| 1153 |
firstn n (xs ++ ys) = firstn n xs. |
| 1154 |
Proof. |
| 1155 |
induction n, xs, ys; boring; try lia. |
| 1156 |
Qed. |
| 1157 |
|
| 1158 |
#[global] |
| 1159 |
Hint Rewrite @firstn_app_inleft using solve [ distr_length ] : simpl_firstn. |
| 1160 |
#[global] |
| 1161 |
Hint Rewrite @firstn_app_inleft using solve [ distr_length ] : push_firstn. |
| 1162 |
|
| 1163 |
Lemma skipn_app_inleft : forall {A} n (xs ys : list A), (n <= length xs)%nat -> |
| 1164 |
skipn n (xs ++ ys) = skipn n xs ++ ys. |
| 1165 |
Proof. |
| 1166 |
induction n, xs, ys; boring; try lia. |
| 1167 |
Qed. |
| 1168 |
|
| 1169 |
#[global] |
| 1170 |
Hint Rewrite @skipn_app_inleft using solve [ distr_length ] : push_skipn. |
| 1171 |
|
| 1172 |
Lemma firstn_map : forall {A B} (f : A -> B) n (xs : list A), firstn n (map f xs) = map f (firstn n xs). |
| 1173 |
Proof. induction n, xs; boring. Qed. |
| 1174 |
|
| 1175 |
#[global] |
| 1176 |
Hint Rewrite @firstn_map : push_firstn. |
| 1177 |
#[global] |
| 1178 |
Hint Rewrite <- @firstn_map : pull_firstn. |
| 1179 |
|
| 1180 |
Lemma skipn_map : forall {A B} (f : A -> B) n (xs : list A), skipn n (map f xs) = map f (skipn n xs). |
| 1181 |
Proof. induction n, xs; boring. Qed. |
| 1182 |
|
| 1183 |
#[global] |
| 1184 |
Hint Rewrite @skipn_map : push_skipn. |
| 1185 |
#[global] |
| 1186 |
Hint Rewrite <- @skipn_map : pull_skipn. |
| 1187 |
|
| 1188 |
Lemma firstn_all : forall {A} n (xs:list A), n = length xs -> firstn n xs = xs. |
| 1189 |
Proof. |
| 1190 |
induction n, xs; boring; lia. |
| 1191 |
Qed. |
| 1192 |
|
| 1193 |
#[global] |
| 1194 |
Hint Rewrite @firstn_all using solve [ distr_length ] : simpl_firstn. |
| 1195 |
#[global] |
| 1196 |
Hint Rewrite @firstn_all using solve [ distr_length ] : push_firstn. |
| 1197 |
|
| 1198 |
Lemma skipn_all : forall {T} n (xs:list T), |
| 1199 |
(n >= length xs)%nat -> |
| 1200 |
skipn n xs = nil. |
| 1201 |
Proof. |
| 1202 |
induction n, xs; boring; lia. |
| 1203 |
Qed. |
| 1204 |
|
| 1205 |
#[global] |
| 1206 |
Hint Rewrite @skipn_all using solve [ distr_length ] : simpl_skipn. |
| 1207 |
#[global] |
| 1208 |
Hint Rewrite @skipn_all using solve [ distr_length ] : push_skipn. |
| 1209 |
|
| 1210 |
Lemma firstn_app_sharp : forall {A} n (l l': list A), |
| 1211 |
length l = n -> |
| 1212 |
firstn n (l ++ l') = l. |
| 1213 |
Proof. |
| 1214 |
intros. |
| 1215 |
rewrite firstn_app_inleft; auto using firstn_all; lia. |
| 1216 |
Qed. |
| 1217 |
|
| 1218 |
#[global] |
| 1219 |
Hint Rewrite @firstn_app_sharp using solve [ distr_length ] : simpl_firstn. |
| 1220 |
#[global] |
| 1221 |
Hint Rewrite @firstn_app_sharp using solve [ distr_length ] : push_firstn. |
| 1222 |
|
| 1223 |
Lemma skipn_app_sharp : forall {A} n (l l': list A), |
| 1224 |
length l = n -> |
| 1225 |
skipn n (l ++ l') = l'. |
| 1226 |
Proof. |
| 1227 |
intros. |
| 1228 |
rewrite skipn_app_inleft; try rewrite skipn_all; auto; lia. |
| 1229 |
Qed. |
| 1230 |
|
| 1231 |
#[global] |
| 1232 |
Hint Rewrite @skipn_app_sharp using solve [ distr_length ] : simpl_skipn. |
| 1233 |
#[global] |
| 1234 |
Hint Rewrite @skipn_app_sharp using solve [ distr_length ] : push_skipn. |
| 1235 |
|
| 1236 |
Lemma skipn_length : forall {A} n (xs : list A), |
| 1237 |
length (skipn n xs) = (length xs - n)%nat. |
| 1238 |
Proof. |
| 1239 |
induction n, xs; boring. |
| 1240 |
Qed. |
| 1241 |
|
| 1242 |
#[global] |
| 1243 |
Hint Rewrite @skipn_length : distr_length. |
| 1244 |
|
| 1245 |
Lemma length_cons : forall {T} (x:T) xs, length (x::xs) = S (length xs). |
| 1246 |
reflexivity. |
| 1247 |
Qed. |
| 1248 |
|
| 1249 |
#[global] |
| 1250 |
Hint Rewrite @length_cons : distr_length. |
| 1251 |
|
| 1252 |
Lemma length_cons_full {T} n (x:list T) (t:T) (H: length (t :: x) = S n) |
| 1253 |
: length x = n. |
| 1254 |
Proof. distr_length. Qed. |
| 1255 |
|
| 1256 |
Lemma cons_length : forall A (xs : list A) a, length (a :: xs) = S (length xs). |
| 1257 |
Proof. |
| 1258 |
auto. |
| 1259 |
Qed. |
| 1260 |
|
| 1261 |
Lemma length0_nil : forall {A} (xs : list A), length xs = 0%nat -> xs = nil. |
| 1262 |
Proof. |
| 1263 |
induction xs; boring; discriminate. |
| 1264 |
Qed. |
| 1265 |
|
| 1266 |
Lemma length_tl {A} ls : length (@tl A ls) = (length ls - 1)%nat. |
| 1267 |
Proof. destruct ls; cbn [tl length]; lia. Qed. |
| 1268 |
#[global] |
| 1269 |
Hint Rewrite @length_tl : distr_length. |
| 1270 |
|
| 1271 |
Lemma length_snoc {A : Type} (l : list A) a : length (l ++ [a]) = S (length l). |
| 1272 |
Proof. simpl_list; boring. Qed. |
| 1273 |
|
| 1274 |
#[global] |
| 1275 |
Hint Rewrite @length_snoc : distr_length. |
| 1276 |
|
| 1277 |
Lemma combine_cons : forall {A B} a b (xs:list A) (ys:list B), |
| 1278 |
combine (a :: xs) (b :: ys) = (a,b) :: combine xs ys. |
| 1279 |
Proof. reflexivity. Qed. |
| 1280 |
#[global] |
| 1281 |
Hint Rewrite @combine_cons : push_combine. |
| 1282 |
|
| 1283 |
Lemma firstn_combine : forall {A B} n (xs:list A) (ys:list B), |
| 1284 |
firstn n (combine xs ys) = combine (firstn n xs) (firstn n ys). |
| 1285 |
Proof. |
| 1286 |
induction n, xs, ys; boring. |
| 1287 |
Qed. |
| 1288 |
|
| 1289 |
#[global] |
| 1290 |
Hint Rewrite @firstn_combine : push_firstn. |
| 1291 |
#[global] |
| 1292 |
Hint Rewrite <- @firstn_combine : pull_firstn. |
| 1293 |
|
| 1294 |
Lemma combine_nil_r : forall {A B} (xs:list A), |
| 1295 |
combine xs (@nil B) = nil. |
| 1296 |
Proof. |
| 1297 |
induction xs; boring. |
| 1298 |
Qed. |
| 1299 |
#[global] |
| 1300 |
Hint Rewrite @combine_nil_r : push_combine. |
| 1301 |
|
| 1302 |
Lemma combine_snoc {A B} xs : forall ys x y, |
| 1303 |
length xs = length ys -> |
| 1304 |
@combine A B (xs ++ (x :: nil)) (ys ++ (y :: nil)) = combine xs ys ++ ((x, y) :: nil). |
| 1305 |
Proof. |
| 1306 |
induction xs; intros; destruct ys; distr_length; cbn; |
| 1307 |
try rewrite IHxs by lia; reflexivity. |
| 1308 |
Qed. |
| 1309 |
#[global] |
| 1310 |
Hint Rewrite @combine_snoc using (solve [distr_length]) : push_combine. |
| 1311 |
|
| 1312 |
Lemma skipn_combine : forall {A B} n (xs:list A) (ys:list B), |
| 1313 |
skipn n (combine xs ys) = combine (skipn n xs) (skipn n ys). |
| 1314 |
Proof. |
| 1315 |
induction n, xs, ys; boring. |
| 1316 |
rewrite combine_nil_r; reflexivity. |
| 1317 |
Qed. |
| 1318 |
|
| 1319 |
#[global] |
| 1320 |
Hint Rewrite @skipn_combine : push_skipn. |
| 1321 |
#[global] |
| 1322 |
Hint Rewrite <- @skipn_combine : pull_skipn. |
| 1323 |
|
| 1324 |
Lemma break_list_last: forall {T} (xs:list T), |
| 1325 |
xs = nil \/ exists xs' y, xs = xs' ++ y :: nil. |
| 1326 |
Proof. |
| 1327 |
destruct xs using rev_ind; auto. |
| 1328 |
right; do 2 eexists; auto. |
| 1329 |
Qed. |
| 1330 |
|
| 1331 |
Lemma break_list_first: forall {T} (xs:list T), |
| 1332 |
xs = nil \/ exists x xs', xs = x :: xs'. |
| 1333 |
Proof. |
| 1334 |
destruct xs; auto. |
| 1335 |
right; do 2 eexists; auto. |
| 1336 |
Qed. |
| 1337 |
|
| 1338 |
Lemma list012 : forall {T} (xs:list T), |
| 1339 |
xs = nil |
| 1340 |
\/ (exists x, xs = x::nil) |
| 1341 |
\/ (exists x xs' y, xs = x::xs'++y::nil). |
| 1342 |
Proof. |
| 1343 |
destruct xs as [|? xs]; auto. |
| 1344 |
right. |
| 1345 |
destruct xs using rev_ind. { |
| 1346 |
left; eexists; auto. |
| 1347 |
} { |
| 1348 |
right; repeat eexists; auto. |
| 1349 |
} |
| 1350 |
Qed. |
| 1351 |
|
| 1352 |
Lemma nil_length0 : forall {T}, length (@nil T) = 0%nat. |
| 1353 |
Proof. |
| 1354 |
auto. |
| 1355 |
Qed. |
| 1356 |
|
| 1357 |
#[global] |
| 1358 |
Hint Rewrite @nil_length0 : distr_length. |
| 1359 |
|
| 1360 |
Lemma nth_error_Some_nth_default : forall {T} i x (l : list T), (i < length l)%nat -> |
| 1361 |
nth_error l i = Some (nth_default x l i). |
| 1362 |
Proof. |
| 1363 |
intros ? ? ? ? i_lt_length. |
| 1364 |
destruct (nth_error_length_exists_value _ _ i_lt_length) as [k nth_err_k]. |
| 1365 |
unfold nth_default. |
| 1366 |
rewrite nth_err_k. |
| 1367 |
reflexivity. |
| 1368 |
Qed. |
| 1369 |
|
| 1370 |
Lemma update_nth_cons : forall {T} f (u0 : T) us, update_nth 0 f (u0 :: us) = (f u0) :: us. |
| 1371 |
Proof. reflexivity. Qed. |
| 1372 |
|
| 1373 |
#[global] |
| 1374 |
Hint Rewrite @update_nth_cons : simpl_update_nth. |
| 1375 |
|
| 1376 |
Lemma set_nth_cons : forall {T} (x u0 : T) us, set_nth 0 x (u0 :: us) = x :: us. |
| 1377 |
Proof. intros; apply update_nth_cons. Qed. |
| 1378 |
|
| 1379 |
#[global] |
| 1380 |
Hint Rewrite @set_nth_cons : simpl_set_nth. |
| 1381 |
|
| 1382 |
Lemma cons_update_nth : forall {T} n f (y : T) us, |
| 1383 |
y :: update_nth n f us = update_nth (S n) f (y :: us). |
| 1384 |
Proof. |
| 1385 |
induction n; boring. |
| 1386 |
Qed. |
| 1387 |
|
| 1388 |
#[global] |
| 1389 |
Hint Rewrite <- @cons_update_nth : simpl_update_nth. |
| 1390 |
|
| 1391 |
Lemma update_nth_nil : forall {T} n f, update_nth n f (@nil T) = @nil T. |
| 1392 |
Proof. |
| 1393 |
induction n; boring. |
| 1394 |
Qed. |
| 1395 |
|
| 1396 |
#[global] |
| 1397 |
Hint Rewrite @update_nth_nil : simpl_update_nth. |
| 1398 |
|
| 1399 |
Lemma cons_set_nth : forall {T} n (x y : T) us, |
| 1400 |
y :: set_nth n x us = set_nth (S n) x (y :: us). |
| 1401 |
Proof. intros; apply cons_update_nth. Qed. |
| 1402 |
|
| 1403 |
#[global] |
| 1404 |
Hint Rewrite <- @cons_set_nth : simpl_set_nth. |
| 1405 |
|
| 1406 |
Lemma set_nth_nil : forall {T} n (x : T), set_nth n x nil = nil. |
| 1407 |
Proof. intros; apply update_nth_nil. Qed. |
| 1408 |
|
| 1409 |
#[global] |
| 1410 |
Hint Rewrite @set_nth_nil : simpl_set_nth. |
| 1411 |
|
| 1412 |
Lemma skipn_nth_default : forall {T} n us (d : T), (n < length us)%nat -> |
| 1413 |
skipn n us = nth_default d us n :: skipn (S n) us. |
| 1414 |
Proof. |
| 1415 |
induction n as [|n IHn]; destruct us as [|? us]; intros d H; nth_tac. |
| 1416 |
rewrite (IHn us d) at 1 by lia. |
| 1417 |
nth_tac. |
| 1418 |
Qed. |
| 1419 |
|
| 1420 |
Lemma nth_default_out_of_bounds : forall {T} n us (d : T), (n >= length us)%nat -> |
| 1421 |
nth_default d us n = d. |
| 1422 |
Proof. |
| 1423 |
induction n as [|n IHn]; unfold nth_default; nth_tac; |
| 1424 |
let us' := match goal with us : list _ |- _ => us end in |
| 1425 |
destruct us' as [|? us]; nth_tac. |
| 1426 |
assert (n >= length us)%nat by lia. |
| 1427 |
pose proof (nth_error_length_error _ n us). |
| 1428 |
specialize_by_assumption. |
| 1429 |
rewrite_hyp * in *. |
| 1430 |
congruence. |
| 1431 |
Qed. |
| 1432 |
|
| 1433 |
#[global] |
| 1434 |
Hint Rewrite @nth_default_out_of_bounds using lia : simpl_nth_default. |
| 1435 |
|
| 1436 |
Ltac nth_error_inbounds := |
| 1437 |
match goal with |
| 1438 |
| [ |- context[match nth_error ?xs ?i with Some _ => _ | None => _ end ] ] => |
| 1439 |
case_eq (nth_error xs i); |
| 1440 |
match goal with |
| 1441 |
| [ |- forall _, nth_error xs i = Some _ -> _ ] => |
| 1442 |
let x := fresh "x" in |
| 1443 |
let H := fresh "H" in |
| 1444 |
intros x H; |
| 1445 |
repeat progress erewrite H; |
| 1446 |
repeat progress erewrite (nth_error_value_eq_nth_default i xs x); auto |
| 1447 |
| [ |- nth_error xs i = None -> _ ] => |
| 1448 |
let H := fresh "H" in |
| 1449 |
intros H; |
| 1450 |
destruct (nth_error_length_not_error _ _ H); |
| 1451 |
try solve [distr_length] |
| 1452 |
end; |
| 1453 |
idtac |
| 1454 |
end. |
| 1455 |
Ltac set_nth_inbounds := |
| 1456 |
match goal with |
| 1457 |
| [ |- context[set_nth ?i ?x ?xs] ] => |
| 1458 |
rewrite (set_nth_equiv_splice_nth i x xs); |
| 1459 |
destruct (lt_dec i (length xs)); |
| 1460 |
match goal with |
| 1461 |
| [ H : ~ (i < (length xs))%nat |- _ ] => destruct H |
| 1462 |
| [ H : (i < (length xs))%nat |- _ ] => try solve [distr_length] |
| 1463 |
end |
| 1464 |
end. |
| 1465 |
Ltac update_nth_inbounds := |
| 1466 |
match goal with |
| 1467 |
| [ |- context[update_nth ?i ?f ?xs] ] => |
| 1468 |
rewrite (update_nth_equiv_splice_nth i f xs); |
| 1469 |
destruct (lt_dec i (length xs)); |
| 1470 |
match goal with |
| 1471 |
| [ H : ~ (i < (length xs))%nat |- _ ] => destruct H |
| 1472 |
| [ H : (i < (length xs))%nat |- _ ] => remove_nth_error; try solve [distr_length] |
| 1473 |
end |
| 1474 |
end. |
| 1475 |
|
| 1476 |
Ltac nth_inbounds := nth_error_inbounds || set_nth_inbounds || update_nth_inbounds. |
| 1477 |
|
| 1478 |
Definition nth_dep {A} (ls : list A) (n : nat) (pf : n < length ls) : A. |
| 1479 |
Proof. |
| 1480 |
refine (match nth_error ls n as v return nth_error ls n = v -> A with |
| 1481 |
| Some v => fun _ => v |
| 1482 |
| None => fun bad => match _ : False with end |
| 1483 |
end eq_refl). |
| 1484 |
apply (proj1 (@nth_error_None _ _ _)) in bad; generalize dependent (length ls); clear. |
| 1485 |
abstract (intros; lia). |
| 1486 |
Defined. |
| 1487 |
|
| 1488 |
Lemma nth_error_nth_dep {A} ls n pf : nth_error ls n = Some (@nth_dep A ls n pf). |
| 1489 |
Proof. |
| 1490 |
unfold nth_dep. |
| 1491 |
generalize dependent (@nth_error_None A ls n). |
| 1492 |
edestruct nth_error; boring. |
| 1493 |
Qed. |
| 1494 |
|
| 1495 |
Lemma nth_default_nth_dep {A} d ls n pf : nth_default d ls n = @nth_dep A ls n pf. |
| 1496 |
Proof. |
| 1497 |
unfold nth_dep. |
| 1498 |
generalize dependent (@nth_error_None A ls n). |
| 1499 |
destruct (nth_error ls n) eqn:?; boring. |
| 1500 |
erewrite nth_error_value_eq_nth_default by eassumption; reflexivity. |
| 1501 |
Qed. |
| 1502 |
|
| 1503 |
Lemma nth_default_in_bounds : forall {T} (d' d : T) n us, (n < length us)%nat -> |
| 1504 |
nth_default d us n = nth_default d' us n. |
| 1505 |
Proof. |
| 1506 |
intros; now unshelve erewrite !nth_default_nth_dep. |
| 1507 |
Qed. |
| 1508 |
|
| 1509 |
Global Hint Resolve nth_default_in_bounds : simpl_nth_default. |
| 1510 |
|
| 1511 |
Lemma cons_eq_head : forall {T} (x y:T) xs ys, x::xs = y::ys -> x=y. |
| 1512 |
Proof. |
| 1513 |
intros; congruence. |
| 1514 |
Qed. |
| 1515 |
Lemma cons_eq_tail : forall {T} (x y:T) xs ys, x::xs = y::ys -> xs=ys. |
| 1516 |
Proof. |
| 1517 |
intros; congruence. |
| 1518 |
Qed. |
| 1519 |
|
| 1520 |
Lemma map_nth_default_always {A B} (f : A -> B) (n : nat) (x : A) (l : list A) |
| 1521 |
: nth_default (f x) (map f l) n = f (nth_default x l n). |
| 1522 |
Proof. |
| 1523 |
revert n; induction l; simpl; intro n; destruct n; [ try reflexivity.. ]. |
| 1524 |
nth_tac. |
| 1525 |
Qed. |
| 1526 |
|
| 1527 |
#[global] |
| 1528 |
Hint Rewrite @map_nth_default_always : push_nth_default. |
| 1529 |
|
| 1530 |
Lemma map_S_seq {A} (f:nat->A) len : forall start, |
| 1531 |
List.map (fun i => f (S i)) (seq start len) = List.map f (seq (S start) len). |
| 1532 |
Proof. induction len as [|len IHlen]; intros; simpl; rewrite ?IHlen; reflexivity. Qed. |
| 1533 |
|
| 1534 |
Lemma seq_snoc len : forall start, seq start (S len) = seq start len ++ ((start + len)%nat :: nil). |
| 1535 |
Proof. |
| 1536 |
induction len; intros. |
| 1537 |
{ cbv [seq app]. autorewrite with natsimplify; reflexivity. } |
| 1538 |
{ remember (S len); simpl seq. |
| 1539 |
rewrite (IHlen (S start)); subst; simpl seq. |
| 1540 |
rewrite Nat.add_succ_r; reflexivity. } |
| 1541 |
Qed. |
| 1542 |
|
| 1543 |
Lemma seq_len_0 a : seq a 0 = nil. Proof. reflexivity. Qed. |
| 1544 |
Lemma seq_add start a b : seq start (a + b) = seq start a ++ seq (start + a) b. |
| 1545 |
Proof. |
| 1546 |
revert start b; induction a as [|a IHa]; cbn; intros start b. |
| 1547 |
{ f_equal; lia. } |
| 1548 |
{ rewrite IHa; do 3 f_equal; lia. } |
| 1549 |
Qed. |
| 1550 |
|
| 1551 |
Lemma map_seq_ext {A} (f g : nat -> A) (n m k : nat) |
| 1552 |
(H : forall i : nat, n <= i <= m + k -> f i = g (i + (m - n))%nat) |
| 1553 |
(Hnm : n <= m) : |
| 1554 |
map f (seq n k) = map g (seq m k). |
| 1555 |
Proof. |
| 1556 |
generalize dependent m; generalize dependent n; induction k as [|k IHk]; intros; simpl. |
| 1557 |
- reflexivity. |
| 1558 |
- simpl; rewrite H by lia; replace (n + (m - n))%nat with m by lia. |
| 1559 |
rewrite (IHk (S n) (S m)); [reflexivity| |lia]. |
| 1560 |
intros; rewrite Nat.sub_succ; apply H; lia. Qed. |
| 1561 |
|
| 1562 |
Lemma map_seq_pred n m : |
| 1563 |
seq n m = map (fun i => (i - 1)%nat) (seq (S n) m). |
| 1564 |
Proof. rewrite <- map_id at 1; apply map_seq_ext; intros; lia. Qed. |
| 1565 |
|
| 1566 |
Lemma map_seq_succ n m : |
| 1567 |
seq (S n) m = map (fun i => (i + 1)%nat) (seq n m). |
| 1568 |
Proof. rewrite <- map_id at 1; symmetry; apply map_seq_ext; intros; lia. Qed. |
| 1569 |
|
| 1570 |
Lemma fold_right_and_Truth_forall_In_iff : forall {T} (l : list T) (P : T -> Prop) (Tr : Prop), |
| 1571 |
(Tr /\ forall x, In x l -> P x) <-> fold_right and Tr (map P l). |
| 1572 |
Proof. |
| 1573 |
induction l as [|?? IHl]; intros; simpl; try tauto. |
| 1574 |
rewrite <- IHl by assumption. |
| 1575 |
intuition (subst; auto). |
| 1576 |
Qed. |
| 1577 |
|
| 1578 |
Lemma fold_right_and_True_forall_In_iff : forall {T} (l : list T) (P : T -> Prop), |
| 1579 |
(forall x, In x l -> P x) <-> fold_right and True (map P l). |
| 1580 |
Proof. |
| 1581 |
intros; rewrite <- fold_right_and_Truth_forall_In_iff; tauto. |
| 1582 |
Qed. |
| 1583 |
|
| 1584 |
Lemma fold_right_invariant : forall {A B} P (f: A -> B -> B) l x, |
| 1585 |
P x -> (forall y, In y l -> forall z, P z -> P (f y z)) -> |
| 1586 |
P (fold_right f x l). |
| 1587 |
Proof. |
| 1588 |
induction l as [|a l IHl]; intros ? ? step; auto. |
| 1589 |
simpl. |
| 1590 |
apply step; try apply in_eq. |
| 1591 |
apply IHl; auto. |
| 1592 |
intros y in_y_l. |
| 1593 |
apply (in_cons a) in in_y_l. |
| 1594 |
auto. |
| 1595 |
Qed. |
| 1596 |
|
| 1597 |
Lemma fold_left_and_Truth_forall_In_iff : forall {T} (l : list T) (P : T -> Prop) (Tr : Prop), |
| 1598 |
(Tr /\ forall x, In x l -> P x) <-> fold_left and (map P l) Tr. |
| 1599 |
Proof. |
| 1600 |
induction l as [|?? IHl]; intros; simpl; try tauto. |
| 1601 |
rewrite <- IHl. |
| 1602 |
intuition (subst; auto). |
| 1603 |
Qed. |
| 1604 |
|
| 1605 |
Lemma fold_left_and_True_forall_In_iff : forall {T} (l : list T) (P : T -> Prop), |
| 1606 |
(forall x, In x l -> P x) <-> fold_left and (map P l) True. |
| 1607 |
Proof. |
| 1608 |
intros; rewrite <- fold_left_and_Truth_forall_In_iff; tauto. |
| 1609 |
Qed. |
| 1610 |
|
| 1611 |
Lemma fold_left_invariant : forall {A B} P (f: B -> A -> B) l x, |
| 1612 |
P x -> (forall y, In y l -> forall z, P z -> P (f z y)) -> |
| 1613 |
P (fold_left f l x). |
| 1614 |
Proof. |
| 1615 |
pose proof in_rev. |
| 1616 |
split_iff. |
| 1617 |
intros; rewrite <- fold_left_rev_right; eapply fold_right_invariant; |
| 1618 |
eauto. |
| 1619 |
Qed. |
| 1620 |
|
| 1621 |
Lemma In_firstn : forall {T} n l (x : T), In x (firstn n l) -> In x l. |
| 1622 |
Proof. |
| 1623 |
induction n; destruct l; boring. |
| 1624 |
Qed. |
| 1625 |
|
| 1626 |
Lemma In_skipn : forall {T} n l (x : T), In x (skipn n l) -> In x l. |
| 1627 |
Proof. |
| 1628 |
induction n; destruct l; boring. |
| 1629 |
Qed. |
| 1630 |
|
| 1631 |
Lemma In_firstn_skipn_split {T} n (x : T) |
| 1632 |
: forall l, In x l <-> In x (firstn n l) \/ In x (skipn n l). |
| 1633 |
Proof. |
| 1634 |
intro l; split; revert l; induction n; destruct l; boring. |
| 1635 |
match goal with |
| 1636 |
| [ IH : forall l, In ?x l -> _ \/ _, H' : In ?x ?ls |- _ ] |
| 1637 |
=> destruct (IH _ H') |
| 1638 |
end; auto. |
| 1639 |
Qed. |
| 1640 |
|
| 1641 |
Lemma firstn_firstn_min : forall {A} m n (l : list A), |
| 1642 |
firstn n (firstn m l) = firstn (min n m) l. |
| 1643 |
Proof. |
| 1644 |
induction m as [|? IHm]; destruct n; intros l; try lia; auto. |
| 1645 |
destruct l; auto. |
| 1646 |
simpl. |
| 1647 |
f_equal. |
| 1648 |
apply IHm; lia. |
| 1649 |
Qed. |
| 1650 |
|
| 1651 |
Lemma firstn_firstn : forall {A} m n (l : list A), (n <= m)%nat -> |
| 1652 |
firstn n (firstn m l) = firstn n l. |
| 1653 |
Proof. |
| 1654 |
intros A m n l H; rewrite firstn_firstn_min. |
| 1655 |
apply Nat.min_case_strong; intro; [ reflexivity | ]. |
| 1656 |
assert (n = m) by lia; subst; reflexivity. |
| 1657 |
Qed. |
| 1658 |
|
| 1659 |
#[global] |
| 1660 |
Hint Rewrite @firstn_firstn using lia : push_firstn. |
| 1661 |
|
| 1662 |
Lemma firstn_succ : forall {A} (d : A) n l, (n < length l)%nat -> |
| 1663 |
firstn (S n) l = (firstn n l) ++ nth_default d l n :: nil. |
| 1664 |
Proof. |
| 1665 |
intros A d; induction n as [|? IHn]; destruct l; rewrite ?(@nil_length0 A); intros; try lia. |
| 1666 |
+ rewrite nth_default_cons; auto. |
| 1667 |
+ simpl. |
| 1668 |
rewrite nth_default_cons_S. |
| 1669 |
rewrite <-IHn by (rewrite cons_length in *; lia). |
| 1670 |
reflexivity. |
| 1671 |
Qed. |
| 1672 |
|
| 1673 |
Lemma firstn_seq k a b |
| 1674 |
: firstn k (seq a b) = seq a (min k b). |
| 1675 |
Proof. |
| 1676 |
revert k a; induction b as [|? IHb], k; simpl; try reflexivity. |
| 1677 |
intros; rewrite IHb; reflexivity. |
| 1678 |
Qed. |
| 1679 |
#[global] |
| 1680 |
Hint Rewrite @firstn_seq : push_firstn. |
| 1681 |
|
| 1682 |
Lemma skipn_seq k a b |
| 1683 |
: skipn k (seq a b) = seq (k + a) (b - k). |
| 1684 |
Proof. |
| 1685 |
revert k a; induction b as [|? IHb], k; simpl; try reflexivity. |
| 1686 |
intros; rewrite IHb; simpl; f_equal; lia. |
| 1687 |
Qed. |
| 1688 |
|
| 1689 |
Lemma update_nth_out_of_bounds : forall {A} n f xs, n >= length xs -> @update_nth A n f xs = xs. |
| 1690 |
Proof. |
| 1691 |
induction n as [|n IHn]; destruct xs; simpl; try congruence; try lia; intros. |
| 1692 |
rewrite IHn by lia; reflexivity. |
| 1693 |
Qed. |
| 1694 |
|
| 1695 |
#[global] |
| 1696 |
Hint Rewrite @update_nth_out_of_bounds using lia : simpl_update_nth. |
| 1697 |
|
| 1698 |
|
| 1699 |
Lemma update_nth_nth_default_full : forall {A} (d:A) n f l i, |
| 1700 |
nth_default d (update_nth n f l) i = |
| 1701 |
if lt_dec i (length l) then |
| 1702 |
if (eq_nat_dec i n) then f (nth_default d l i) |
| 1703 |
else nth_default d l i |
| 1704 |
else d. |
| 1705 |
Proof. |
| 1706 |
induction n as [|n IHn]; (destruct l; simpl in *; [ intros i **; destruct i; simpl; try reflexivity; lia | ]); |
| 1707 |
intros i **; repeat break_match; subst; try destruct i; |
| 1708 |
repeat first [ progress break_match |
| 1709 |
| progress subst |
| 1710 |
| progress boring |
| 1711 |
| progress autorewrite with simpl_nth_default |
| 1712 |
| lia ]. |
| 1713 |
Qed. |
| 1714 |
|
| 1715 |
#[global] |
| 1716 |
Hint Rewrite @update_nth_nth_default_full : push_nth_default. |
| 1717 |
|
| 1718 |
Lemma update_nth_nth_default : forall {A} (d:A) n f l i, (0 <= i < length l)%nat -> |
| 1719 |
nth_default d (update_nth n f l) i = |
| 1720 |
if (eq_nat_dec i n) then f (nth_default d l i) else nth_default d l i. |
| 1721 |
Proof. intros; rewrite update_nth_nth_default_full; repeat break_match; boring. Qed. |
| 1722 |
|
| 1723 |
#[global] |
| 1724 |
Hint Rewrite @update_nth_nth_default using (lia || distr_length; lia) : push_nth_default. |
| 1725 |
|
| 1726 |
Lemma set_nth_nth_default_full : forall {A} (d:A) n v l i, |
| 1727 |
nth_default d (set_nth n v l) i = |
| 1728 |
if lt_dec i (length l) then |
| 1729 |
if (eq_nat_dec i n) then v |
| 1730 |
else nth_default d l i |
| 1731 |
else d. |
| 1732 |
Proof. intros; apply update_nth_nth_default_full; assumption. Qed. |
| 1733 |
|
| 1734 |
#[global] |
| 1735 |
Hint Rewrite @set_nth_nth_default_full : push_nth_default. |
| 1736 |
|
| 1737 |
Lemma set_nth_nth_default : forall {A} (d:A) n x l i, (0 <= i < length l)%nat -> |
| 1738 |
nth_default d (set_nth n x l) i = |
| 1739 |
if (eq_nat_dec i n) then x else nth_default d l i. |
| 1740 |
Proof. intros; apply update_nth_nth_default; assumption. Qed. |
| 1741 |
|
| 1742 |
#[global] |
| 1743 |
Hint Rewrite @set_nth_nth_default using (lia || distr_length; lia) : push_nth_default. |
| 1744 |
|
| 1745 |
Lemma nth_default_preserves_properties : forall {A} (P : A -> Prop) l n d, |
| 1746 |
(forall x, In x l -> P x) -> P d -> P (nth_default d l n). |
| 1747 |
Proof. |
| 1748 |
intros A P l n d H H0; rewrite nth_default_eq. |
| 1749 |
destruct (nth_in_or_default n l d); auto. |
| 1750 |
congruence. |
| 1751 |
Qed. |
| 1752 |
|
| 1753 |
Lemma nth_default_preserves_properties_length_dep : |
| 1754 |
forall {A} (P : A -> Prop) l n d, |
| 1755 |
(forall x, In x l -> n < (length l) -> P x) -> ((~ n < length l) -> P d) -> P (nth_default d l n). |
| 1756 |
Proof. |
| 1757 |
intros A P l n d H H0. |
| 1758 |
destruct (lt_dec n (length l)). |
| 1759 |
+ rewrite nth_default_eq; auto using nth_In. |
| 1760 |
+ rewrite nth_default_out_of_bounds by lia. |
| 1761 |
auto. |
| 1762 |
Qed. |
| 1763 |
|
| 1764 |
Lemma nth_error_first : forall {T} (a b : T) l, |
| 1765 |
nth_error (a :: l) 0 = Some b -> a = b. |
| 1766 |
Proof. |
| 1767 |
intros; simpl in *. |
| 1768 |
unfold value in *. |
| 1769 |
congruence. |
| 1770 |
Qed. |
| 1771 |
|
| 1772 |
Lemma nth_error_exists_first : forall {T} l (x : T) (H : nth_error l 0 = Some x), |
| 1773 |
exists l', l = x :: l'. |
| 1774 |
Proof. |
| 1775 |
induction l; try discriminate; intros x H; eexists. |
| 1776 |
apply nth_error_first in H. |
| 1777 |
subst; eauto. |
| 1778 |
Qed. |
| 1779 |
|
| 1780 |
Lemma list_elementwise_eq : forall {T} (l1 l2 : list T), |
| 1781 |
(forall i, nth_error l1 i = nth_error l2 i) -> l1 = l2. |
| 1782 |
Proof. |
| 1783 |
induction l1, l2; intros H; try reflexivity; |
| 1784 |
pose proof (H 0%nat) as Hfirst; simpl in Hfirst; inversion Hfirst. |
| 1785 |
f_equal. |
| 1786 |
apply IHl1. |
| 1787 |
intros i; specialize (H (S i)). |
| 1788 |
boring. |
| 1789 |
Qed. |
| 1790 |
|
| 1791 |
Lemma sum_firstn_all_succ : forall n l, (length l <= n)%nat -> |
| 1792 |
sum_firstn l (S n) = sum_firstn l n. |
| 1793 |
Proof. |
| 1794 |
unfold sum_firstn; intros. |
| 1795 |
autorewrite with push_firstn; reflexivity. |
| 1796 |
Qed. |
| 1797 |
|
| 1798 |
#[global] |
| 1799 |
Hint Rewrite @sum_firstn_all_succ using lia : simpl_sum_firstn. |
| 1800 |
|
| 1801 |
Lemma sum_firstn_all : forall n l, (length l <= n)%nat -> |
| 1802 |
sum_firstn l n = sum_firstn l (length l). |
| 1803 |
Proof. |
| 1804 |
unfold sum_firstn; intros. |
| 1805 |
autorewrite with push_firstn; reflexivity. |
| 1806 |
Qed. |
| 1807 |
|
| 1808 |
#[global] |
| 1809 |
Hint Rewrite @sum_firstn_all using lia : simpl_sum_firstn. |
| 1810 |
|
| 1811 |
Lemma sum_firstn_succ_default : forall l i, |
| 1812 |
sum_firstn l (S i) = (nth_default 0 l i + sum_firstn l i)%Z. |
| 1813 |
Proof. |
| 1814 |
unfold sum_firstn; induction l as [|a l IHl], i; |
| 1815 |
intros; autorewrite with simpl_nth_default simpl_firstn simpl_fold_right in *; |
| 1816 |
try reflexivity. |
| 1817 |
rewrite IHl; lia. |
| 1818 |
Qed. |
| 1819 |
|
| 1820 |
#[global] |
| 1821 |
Hint Rewrite @sum_firstn_succ_default : simpl_sum_firstn. |
| 1822 |
|
| 1823 |
Lemma sum_firstn_0 : forall xs, |
| 1824 |
sum_firstn xs 0 = 0%Z. |
| 1825 |
Proof. |
| 1826 |
destruct xs; reflexivity. |
| 1827 |
Qed. |
| 1828 |
|
| 1829 |
#[global] |
| 1830 |
Hint Rewrite @sum_firstn_0 : simpl_sum_firstn. |
| 1831 |
|
| 1832 |
Lemma sum_firstn_succ : forall l i x, |
| 1833 |
nth_error l i = Some x -> |
| 1834 |
sum_firstn l (S i) = (x + sum_firstn l i)%Z. |
| 1835 |
Proof. |
| 1836 |
intros; rewrite sum_firstn_succ_default. |
| 1837 |
erewrite nth_error_value_eq_nth_default by eassumption; reflexivity. |
| 1838 |
Qed. |
| 1839 |
|
| 1840 |
#[global] |
| 1841 |
Hint Rewrite @sum_firstn_succ using congruence : simpl_sum_firstn. |
| 1842 |
|
| 1843 |
Lemma sum_firstn_succ_cons : forall x xs i, |
| 1844 |
sum_firstn (x :: xs) (S i) = (x + sum_firstn xs i)%Z. |
| 1845 |
Proof. |
| 1846 |
unfold sum_firstn; simpl; reflexivity. |
| 1847 |
Qed. |
| 1848 |
|
| 1849 |
#[global] |
| 1850 |
Hint Rewrite @sum_firstn_succ_cons : simpl_sum_firstn. |
| 1851 |
|
| 1852 |
Lemma sum_firstn_nil : forall i, |
| 1853 |
sum_firstn nil i = 0%Z. |
| 1854 |
Proof. destruct i; reflexivity. Qed. |
| 1855 |
|
| 1856 |
#[global] |
| 1857 |
Hint Rewrite @sum_firstn_nil : simpl_sum_firstn. |
| 1858 |
|
| 1859 |
Lemma sum_firstn_succ_default_rev : forall l i, |
| 1860 |
sum_firstn l i = (sum_firstn l (S i) - nth_default 0 l i)%Z. |
| 1861 |
Proof. |
| 1862 |
intros; rewrite sum_firstn_succ_default; lia. |
| 1863 |
Qed. |
| 1864 |
|
| 1865 |
Lemma sum_firstn_succ_rev : forall l i x, |
| 1866 |
nth_error l i = Some x -> |
| 1867 |
sum_firstn l i = (sum_firstn l (S i) - x)%Z. |
| 1868 |
Proof. |
| 1869 |
intros; erewrite sum_firstn_succ by eassumption; lia. |
| 1870 |
Qed. |
| 1871 |
|
| 1872 |
Lemma sum_firstn_nonnegative : forall n l, (forall x, In x l -> 0 <= x)%Z |
| 1873 |
-> (0 <= sum_firstn l n)%Z. |
| 1874 |
Proof. |
| 1875 |
induction n as [|n IHn]; destruct l as [|? l]; autorewrite with simpl_sum_firstn; simpl; try lia. |
| 1876 |
{ specialize (IHn l). |
| 1877 |
destruct n; simpl; autorewrite with simpl_sum_firstn simpl_nth_default in *; |
| 1878 |
intuition auto with zarith. } |
| 1879 |
Qed. |
| 1880 |
|
| 1881 |
Global Hint Resolve sum_firstn_nonnegative : znonzero. |
| 1882 |
|
| 1883 |
Lemma sum_firstn_app : forall xs ys n, |
| 1884 |
sum_firstn (xs ++ ys) n = (sum_firstn xs n + sum_firstn ys (n - length xs))%Z. |
| 1885 |
Proof. |
| 1886 |
induction xs as [|a xs IHxs]; simpl. |
| 1887 |
{ intros ys n; autorewrite with simpl_sum_firstn; simpl. |
| 1888 |
f_equal; lia. } |
| 1889 |
{ intros ys [|n]; autorewrite with simpl_sum_firstn; simpl; [ reflexivity | ]. |
| 1890 |
rewrite IHxs; lia. } |
| 1891 |
Qed. |
| 1892 |
|
| 1893 |
Lemma sum_firstn_app_sum : forall xs ys n, |
| 1894 |
sum_firstn (xs ++ ys) (length xs + n) = (sum_firstn xs (length xs) + sum_firstn ys n)%Z. |
| 1895 |
Proof. |
| 1896 |
intros; rewrite sum_firstn_app; autorewrite with simpl_sum_firstn. |
| 1897 |
do 2 f_equal; lia. |
| 1898 |
Qed. |
| 1899 |
#[global] |
| 1900 |
Hint Rewrite @sum_firstn_app_sum : simpl_sum_firstn. |
| 1901 |
|
| 1902 |
Lemma sum_cons xs x : sum (x :: xs) = (x + sum xs)%Z. |
| 1903 |
Proof. reflexivity. Qed. |
| 1904 |
#[global] |
| 1905 |
Hint Rewrite sum_cons : push_sum. |
| 1906 |
|
| 1907 |
Lemma sum_nil : sum nil = 0%Z. |
| 1908 |
Proof. reflexivity. Qed. |
| 1909 |
#[global] |
| 1910 |
Hint Rewrite sum_nil : push_sum. |
| 1911 |
|
| 1912 |
Lemma sum_app x y : sum (x ++ y) = (sum x + sum y)%Z. |
| 1913 |
Proof. induction x; rewrite ?app_nil_l, <-?app_comm_cons; autorewrite with push_sum; lia. Qed. |
| 1914 |
#[global] |
| 1915 |
Hint Rewrite sum_app : push_sum. |
| 1916 |
|
| 1917 |
Lemma sum_rev x : sum (rev x) = sum x. |
| 1918 |
Proof. induction x; cbn [rev]; autorewrite with push_sum; lia. Qed. |
| 1919 |
#[global] |
| 1920 |
Hint Rewrite sum_rev : push_sum. |
| 1921 |
|
| 1922 |
Lemma nth_error_skipn : forall {A} n (l : list A) m, |
| 1923 |
nth_error (skipn n l) m = nth_error l (n + m). |
| 1924 |
Proof. |
| 1925 |
induction n as [|n IHn]; destruct l; boring. |
| 1926 |
apply nth_error_nil_error. |
| 1927 |
Qed. |
| 1928 |
#[global] |
| 1929 |
Hint Rewrite @nth_error_skipn : push_nth_error. |
| 1930 |
|
| 1931 |
Lemma nth_default_skipn : forall {A} (l : list A) d n m, nth_default d (skipn n l) m = nth_default d l (n + m). |
| 1932 |
Proof. |
| 1933 |
cbv [nth_default]; intros. |
| 1934 |
rewrite nth_error_skipn. |
| 1935 |
reflexivity. |
| 1936 |
Qed. |
| 1937 |
#[global] |
| 1938 |
Hint Rewrite @nth_default_skipn : push_nth_default. |
| 1939 |
|
| 1940 |
Lemma sum_firstn_skipn : forall l n m, sum_firstn l (n + m) = (sum_firstn l n + sum_firstn (skipn n l) m)%Z. |
| 1941 |
Proof. |
| 1942 |
induction m; intros. |
| 1943 |
+ rewrite sum_firstn_0. autorewrite with natsimplify. lia. |
| 1944 |
+ rewrite <-plus_n_Sm, !sum_firstn_succ_default. |
| 1945 |
rewrite nth_default_skipn. |
| 1946 |
lia. |
| 1947 |
Qed. |
| 1948 |
|
| 1949 |
Lemma nth_default_seq_inbounds d s n i (H:(i < n)%nat) : |
| 1950 |
List.nth_default d (List.seq s n) i = (s+i)%nat. |
| 1951 |
Proof. |
| 1952 |
progress cbv [List.nth_default]. |
| 1953 |
rewrite nth_error_seq. |
| 1954 |
break_innermost_match; solve [ trivial | lia ]. |
| 1955 |
Qed. |
| 1956 |
#[global] |
| 1957 |
Hint Rewrite @nth_default_seq_inbounds using lia : push_nth_default. |
| 1958 |
|
| 1959 |
Lemma sum_firstn_prefix_le' : forall l n m, (forall x, In x l -> (0 <= x)%Z) -> |
| 1960 |
(sum_firstn l n <= sum_firstn l (n + m))%Z. |
| 1961 |
Proof. |
| 1962 |
intros l n m H. |
| 1963 |
rewrite sum_firstn_skipn. |
| 1964 |
pose proof (sum_firstn_nonnegative m (skipn n l)) as Hskipn_nonneg. |
| 1965 |
match type of Hskipn_nonneg with |
| 1966 |
?P -> _ => assert P as Q; [ | specialize (Hskipn_nonneg Q); lia ] end. |
| 1967 |
intros x HIn_skipn. |
| 1968 |
apply In_skipn in HIn_skipn. |
| 1969 |
auto. |
| 1970 |
Qed. |
| 1971 |
|
| 1972 |
Lemma sum_firstn_prefix_le : forall l n m, (forall x, In x l -> (0 <= x)%Z) -> |
| 1973 |
(n <= m)%nat -> |
| 1974 |
(sum_firstn l n <= sum_firstn l m)%Z. |
| 1975 |
Proof. |
| 1976 |
intros l n m H H0. |
| 1977 |
replace m with (n + (m - n))%nat by lia. |
| 1978 |
auto using sum_firstn_prefix_le'. |
| 1979 |
Qed. |
| 1980 |
|
| 1981 |
Lemma sum_firstn_pos_lt_succ : forall l n m, (forall x, In x l -> (0 <= x)%Z) -> |
| 1982 |
(n < length l)%nat -> |
| 1983 |
(sum_firstn l n < sum_firstn l (S m))%Z -> |
| 1984 |
(n <= m)%nat. |
| 1985 |
Proof. |
| 1986 |
intros l n m H H0 H1. |
| 1987 |
destruct (le_dec n m); auto. |
| 1988 |
replace n with (m + (n - m))%nat in H1 by lia. |
| 1989 |
rewrite sum_firstn_skipn in H1. |
| 1990 |
rewrite sum_firstn_succ_default in *. |
| 1991 |
match goal with H : (?a + ?b < ?c + ?a)%Z |- _ => assert (H2 : (b < c)%Z) by lia end. |
| 1992 |
destruct (lt_dec m (length l)). { |
| 1993 |
rewrite skipn_nth_default with (d := 0%Z) in H2 by assumption. |
| 1994 |
replace (n - m)%nat with (S (n - S m))%nat in H2 by lia. |
| 1995 |
rewrite sum_firstn_succ_cons in H2. |
| 1996 |
pose proof (sum_firstn_nonnegative (n - S m) (skipn (S m) l)) as H3. |
| 1997 |
match type of H3 with |
| 1998 |
?P -> _ => assert P as Q; [ | specialize (H3 Q); lia ] end. |
| 1999 |
intros ? A. |
| 2000 |
apply In_skipn in A. |
| 2001 |
apply H in A. |
| 2002 |
lia. |
| 2003 |
} { |
| 2004 |
rewrite skipn_all, nth_default_out_of_bounds in H2 by lia. |
| 2005 |
rewrite sum_firstn_nil in H2; lia. |
| 2006 |
} |
| 2007 |
Qed. |
| 2008 |
|
| 2009 |
Definition NotSum {T} (xs : list T) (v : nat) := True. |
| 2010 |
|
| 2011 |
Ltac NotSum := |
| 2012 |
lazymatch goal with |
| 2013 |
| [ |- NotSum ?xs (length ?xs + _)%nat ] => fail |
| 2014 |
| [ |- NotSum _ _ ] => exact I |
| 2015 |
end. |
| 2016 |
|
| 2017 |
Lemma sum_firstn_app_hint : forall xs ys n, NotSum xs n -> |
| 2018 |
sum_firstn (xs ++ ys) n = (sum_firstn xs n + sum_firstn ys (n - length xs))%Z. |
| 2019 |
Proof. auto using sum_firstn_app. Qed. |
| 2020 |
|
| 2021 |
#[global] |
| 2022 |
Hint Rewrite sum_firstn_app_hint using solve [ NotSum ] : simpl_sum_firstn. |
| 2023 |
|
| 2024 |
|
| 2025 |
Lemma nth_default_map2 : forall {A B C} (f : A -> B -> C) ls1 ls2 i d d1 d2, |
| 2026 |
nth_default d (map2 f ls1 ls2) i = |
| 2027 |
if lt_dec i (min (length ls1) (length ls2)) |
| 2028 |
then f (nth_default d1 ls1 i) (nth_default d2 ls2 i) |
| 2029 |
else d. |
| 2030 |
Proof. |
| 2031 |
induction ls1 as [|a ls1 IHls1], ls2. |
| 2032 |
+ cbv [map2 length min]. |
| 2033 |
intros. |
| 2034 |
break_match; try lia. |
| 2035 |
apply nth_default_nil. |
| 2036 |
+ cbv [map2 length min]. |
| 2037 |
intros. |
| 2038 |
break_match; try lia. |
| 2039 |
apply nth_default_nil. |
| 2040 |
+ cbv [map2 length min]. |
| 2041 |
intros. |
| 2042 |
break_match; try lia. |
| 2043 |
apply nth_default_nil. |
| 2044 |
+ simpl. |
| 2045 |
destruct i. |
| 2046 |
- intros. rewrite !nth_default_cons. |
| 2047 |
break_match; auto; lia. |
| 2048 |
- intros d d1 d2. rewrite !nth_default_cons_S. |
| 2049 |
rewrite IHls1 with (d1 := d1) (d2 := d2). |
| 2050 |
repeat break_match; auto; lia. |
| 2051 |
Qed. |
| 2052 |
|
| 2053 |
Lemma map2_cons : forall A B C (f : A -> B -> C) ls1 ls2 a b, |
| 2054 |
map2 f (a :: ls1) (b :: ls2) = f a b :: map2 f ls1 ls2. |
| 2055 |
Proof. |
| 2056 |
reflexivity. |
| 2057 |
Qed. |
| 2058 |
|
| 2059 |
Lemma map2_nil_l : forall A B C (f : A -> B -> C) ls2, |
| 2060 |
map2 f nil ls2 = nil. |
| 2061 |
Proof. |
| 2062 |
reflexivity. |
| 2063 |
Qed. |
| 2064 |
|
| 2065 |
Lemma map2_nil_r : forall A B C (f : A -> B -> C) ls1, |
| 2066 |
map2 f ls1 nil = nil. |
| 2067 |
Proof. |
| 2068 |
destruct ls1; reflexivity. |
| 2069 |
Qed. |
| 2070 |
Local Hint Resolve map2_nil_r map2_nil_l : core. |
| 2071 |
|
| 2072 |
Ltac simpl_list_lengths := repeat match goal with |
| 2073 |
| H : context[length (@nil ?A)] |- _ => rewrite (@nil_length0 A) in H |
| 2074 |
| H : context[length (_ :: _)] |- _ => rewrite length_cons in H |
| 2075 |
| |- context[length (@nil ?A)] => rewrite (@nil_length0 A) |
| 2076 |
| |- context[length (_ :: _)] => rewrite length_cons |
| 2077 |
end. |
| 2078 |
|
| 2079 |
Section OpaqueMap2. |
| 2080 |
Local Opaque map2. |
| 2081 |
|
| 2082 |
Lemma map2_length : forall A B C (f : A -> B -> C) ls1 ls2, |
| 2083 |
length (map2 f ls1 ls2) = min (length ls1) (length ls2). |
| 2084 |
Proof. |
| 2085 |
induction ls1 as [|a ls1 IHls1], ls2; intros; try solve [cbv; auto]. |
| 2086 |
rewrite map2_cons, !length_cons, IHls1. |
| 2087 |
auto. |
| 2088 |
Qed. |
| 2089 |
Hint Rewrite @map2_length : distr_length. |
| 2090 |
|
| 2091 |
|
| 2092 |
Lemma map2_app : forall A B C (f : A -> B -> C) ls1 ls2 ls1' ls2', |
| 2093 |
(length ls1 = length ls2) -> |
| 2094 |
map2 f (ls1 ++ ls1') (ls2 ++ ls2') = map2 f ls1 ls2 ++ map2 f ls1' ls2'. |
| 2095 |
Proof. |
| 2096 |
induction ls1 as [|a ls1 IHls1], ls2; intros; rewrite ?map2_nil_r, ?app_nil_l; try congruence; |
| 2097 |
simpl_list_lengths; try lia. |
| 2098 |
rewrite <-!app_comm_cons, !map2_cons. |
| 2099 |
rewrite IHls1; auto. |
| 2100 |
Qed. |
| 2101 |
End OpaqueMap2. |
| 2102 |
#[global] |
| 2103 |
Hint Rewrite @map2_length : distr_length. |
| 2104 |
|
| 2105 |
Lemma firstn_update_nth {A} |
| 2106 |
: forall f m n (xs : list A), firstn m (update_nth n f xs) = update_nth n f (firstn m xs). |
| 2107 |
Proof. |
| 2108 |
induction m; destruct n, xs; |
| 2109 |
autorewrite with simpl_firstn simpl_update_nth; |
| 2110 |
congruence. |
| 2111 |
Qed. |
| 2112 |
|
| 2113 |
#[global] |
| 2114 |
Hint Rewrite @firstn_update_nth : push_firstn. |
| 2115 |
#[global] |
| 2116 |
Hint Rewrite @firstn_update_nth : pull_update_nth. |
| 2117 |
#[global] |
| 2118 |
Hint Rewrite <- @firstn_update_nth : pull_firstn. |
| 2119 |
#[global] |
| 2120 |
Hint Rewrite <- @firstn_update_nth : push_update_nth. |
| 2121 |
|
| 2122 |
Global Instance fold_right_Proper {A B} : Proper (pointwise_relation _ (pointwise_relation _ eq) ==> eq ==> eq ==> eq) (@fold_right A B) | 1. |
| 2123 |
Proof. |
| 2124 |
cbv [pointwise_relation]; intros f g Hfg x y ? ls ls' ?; subst y ls'; revert x. |
| 2125 |
induction ls as [|l ls IHls]; cbn [fold_right]; intro; rewrite ?IHls, ?Hfg; reflexivity. |
| 2126 |
Qed. |
| 2127 |
Global Instance fold_right_Proper_eq {A B} : Proper ((eq ==> eq ==> eq) ==> eq ==> eq ==> eq) (@fold_right A B) | 1. |
| 2128 |
Proof. cbv [respectful]; repeat intro; subst; apply fold_right_Proper; repeat intro; eauto. Qed. |
| 2129 |
|
| 2130 |
Global Instance fold_left_Proper {A B} : Proper (pointwise_relation _ (pointwise_relation _ eq) ==> eq ==> eq ==> eq) (@fold_left A B) | 1. |
| 2131 |
Proof. |
| 2132 |
repeat intro; rewrite <- !fold_left_rev_right; apply fold_right_Proper; cbv [pointwise_relation] in *; eauto; congruence. |
| 2133 |
Qed. |
| 2134 |
Global Instance fold_left_Proper_eq {A B} : Proper ((eq ==> eq ==> eq) ==> eq ==> eq ==> eq) (@fold_left A B) | 1. |
| 2135 |
Proof. cbv [respectful]; repeat intro; subst; apply fold_left_Proper; repeat intro; eauto. Qed. |
| 2136 |
|
| 2137 |
From Coq Require Import SetoidList. |
| 2138 |
Global Instance Proper_nth_default : forall A eq, |
| 2139 |
Proper (eq==>eqlistA eq==>Logic.eq==>eq) (nth_default (A:=A)). |
| 2140 |
Proof. |
| 2141 |
intros A ee x y H; subst; induction 1. |
| 2142 |
+ repeat intro; rewrite !nth_default_nil; assumption. |
| 2143 |
+ intros x1 y0 H2; subst; destruct y0; rewrite ?nth_default_cons, ?nth_default_cons_S; auto. |
| 2144 |
Qed. |
| 2145 |
|
| 2146 |
Lemma fold_right_andb_true_map_iff A (ls : list A) f |
| 2147 |
: List.fold_right andb true (List.map f ls) = true <-> forall i, List.In i ls -> f i = true. |
| 2148 |
Proof. |
| 2149 |
induction ls as [|a ls IHls]; simpl; [ | rewrite Bool.andb_true_iff, IHls ]; try tauto. |
| 2150 |
intuition (congruence || eauto). |
| 2151 |
Qed. |
| 2152 |
|
| 2153 |
Lemma fold_right_andb_true_iff_fold_right_and_True (ls : list bool) |
| 2154 |
: List.fold_right andb true ls = true <-> List.fold_right and True (List.map (fun b => b = true) ls). |
| 2155 |
Proof. |
| 2156 |
rewrite <- (map_id ls) at 1. |
| 2157 |
rewrite fold_right_andb_true_map_iff, fold_right_and_True_forall_In_iff; reflexivity. |
| 2158 |
Qed. |
| 2159 |
|
| 2160 |
Lemma fold_left_andb_true_map_iff A (ls : list A) f |
| 2161 |
: List.fold_left andb (List.map f ls) true = true <-> forall i, List.In i ls -> f i = true. |
| 2162 |
Proof. |
| 2163 |
rewrite <- fold_left_rev_right, <- map_rev; setoid_rewrite Bool.andb_comm. |
| 2164 |
rewrite fold_right_andb_true_map_iff. |
| 2165 |
setoid_rewrite <- in_rev; reflexivity. |
| 2166 |
Qed. |
| 2167 |
|
| 2168 |
Lemma fold_left_andb_true_iff_fold_left_and_True (ls : list bool) |
| 2169 |
: List.fold_left andb ls true = true <-> List.fold_left and (List.map (fun b => b = true) ls) True. |
| 2170 |
Proof. |
| 2171 |
rewrite <- (map_id ls) at 1. |
| 2172 |
rewrite fold_left_andb_true_map_iff, fold_left_and_True_forall_In_iff; reflexivity. |
| 2173 |
Qed. |
| 2174 |
|
| 2175 |
Lemma fold_right_andb_truth_map_iff A (ls : list A) f t |
| 2176 |
: List.fold_right andb t (List.map f ls) = true <-> (t = true /\ forall i, List.In i ls -> f i = true). |
| 2177 |
Proof. |
| 2178 |
induction ls as [|a ls IHls]; simpl; [ | rewrite Bool.andb_true_iff, IHls ]; try tauto. |
| 2179 |
intuition (congruence || eauto). |
| 2180 |
Qed. |
| 2181 |
|
| 2182 |
Lemma fold_right_andb_truth_iff_fold_right_and_Truth (ls : list bool) t |
| 2183 |
: List.fold_right andb t ls = true <-> List.fold_right and (t = true) (List.map (fun b => b = true) ls). |
| 2184 |
Proof. |
| 2185 |
rewrite <- (map_id ls) at 1. |
| 2186 |
rewrite fold_right_andb_truth_map_iff, fold_right_and_Truth_forall_In_iff; reflexivity. |
| 2187 |
Qed. |
| 2188 |
|
| 2189 |
Lemma fold_left_andb_truth_map_iff A (ls : list A) f t |
| 2190 |
: List.fold_left andb (List.map f ls) t = true <-> (t = true /\ forall i, List.In i ls -> f i = true). |
| 2191 |
Proof. |
| 2192 |
rewrite <- fold_left_rev_right, <- map_rev; setoid_rewrite Bool.andb_comm. |
| 2193 |
rewrite fold_right_andb_truth_map_iff. |
| 2194 |
setoid_rewrite <- in_rev; reflexivity. |
| 2195 |
Qed. |
| 2196 |
|
| 2197 |
Lemma fold_left_andb_truth_iff_fold_left_and_Truth (ls : list bool) t |
| 2198 |
: List.fold_left andb ls t = true <-> List.fold_left and (List.map (fun b => b = true) ls) (t = true). |
| 2199 |
Proof. |
| 2200 |
rewrite <- (map_id ls) at 1. |
| 2201 |
rewrite fold_left_andb_truth_map_iff, fold_left_and_Truth_forall_In_iff; reflexivity. |
| 2202 |
Qed. |
| 2203 |
|
| 2204 |
Lemma Forall2_forall_iff : forall {A B} (R : A -> B -> Prop) (xs : list A) (ys : list B) d1 d2, length xs = length ys -> |
| 2205 |
(Forall2 R xs ys <-> (forall i, (i < length xs)%nat -> R (nth_default d1 xs i) (nth_default d2 ys i))). |
| 2206 |
Proof. |
| 2207 |
intros A B R xs ys d1 d2 H; split; [ intros H0 i H1 | intros H0 ]. |
| 2208 |
|
| 2209 |
+ revert xs ys H H0 H1. |
| 2210 |
induction i as [|i IHi]; intros xs ys H H0 H1; destruct H0; distr_length; autorewrite with push_nth_default; auto. |
| 2211 |
eapply IHi; auto. lia. |
| 2212 |
+ revert xs ys H H0; induction xs as [|a xs IHxs]; intros ys H H0; destruct ys; distr_length; econstructor. |
| 2213 |
- specialize (H0 0%nat). |
| 2214 |
autorewrite with push_nth_default in *; auto. |
| 2215 |
apply H0; lia. |
| 2216 |
- apply IHxs; try lia. |
| 2217 |
intros i H1. |
| 2218 |
specialize (H0 (S i)). |
| 2219 |
autorewrite with push_nth_default in *; auto. |
| 2220 |
apply H0; lia. |
| 2221 |
Qed. |
| 2222 |
|
| 2223 |
Lemma Forall2_forall_iff' : forall {A} R (xs ys : list A) d, length xs = length ys -> |
| 2224 |
(Forall2 R xs ys <-> (forall i, (i < length xs)%nat -> R (nth_default d xs i) (nth_default d ys i))). |
| 2225 |
Proof. intros; apply Forall2_forall_iff; assumption. Qed. |
| 2226 |
|
| 2227 |
Lemma nth_default_firstn : forall {A} (d : A) l i n, |
| 2228 |
nth_default d (firstn n l) i = if le_dec n (length l) |
| 2229 |
then if lt_dec i n then nth_default d l i else d |
| 2230 |
else nth_default d l i. |
| 2231 |
Proof. |
| 2232 |
intros A d l i; induction n as [|n IHn]; break_match; autorewrite with push_nth_default; auto; try lia. |
| 2233 |
+ rewrite (firstn_succ d) by lia. |
| 2234 |
autorewrite with push_nth_default; repeat (break_match_hyps; break_match; distr_length); |
| 2235 |
rewrite Nat.min_l in * by lia; try lia. |
| 2236 |
- apply IHn; lia. |
| 2237 |
- replace i with n in * by lia. |
| 2238 |
rewrite Nat.sub_diag. |
| 2239 |
autorewrite with push_nth_default; auto. |
| 2240 |
+ rewrite nth_default_out_of_bounds; break_match_hyps; distr_length; auto; lia. |
| 2241 |
+ rewrite firstn_all2 by lia. |
| 2242 |
auto. |
| 2243 |
Qed. |
| 2244 |
#[global] |
| 2245 |
Hint Rewrite @nth_default_firstn : push_nth_default. |
| 2246 |
|
| 2247 |
Lemma nth_error_repeat {T} x n i v : nth_error (@repeat T x n) i = Some v -> v = x. |
| 2248 |
Proof. |
| 2249 |
revert n x v; induction i as [|i IHi]; destruct n; simpl in *; eauto; congruence. |
| 2250 |
Qed. |
| 2251 |
|
| 2252 |
#[global] |
| 2253 |
Hint Rewrite repeat_length : distr_length. |
| 2254 |
|
| 2255 |
Lemma repeat_spec_iff : forall {A} (ls : list A) x n, |
| 2256 |
(length ls = n /\ forall y, In y ls -> y = x) <-> ls = repeat x n. |
| 2257 |
Proof. |
| 2258 |
intros A ls x n; split; [ revert A ls x n | intro; subst; eauto using repeat_length, repeat_spec ]. |
| 2259 |
induction ls as [|a ls IHls], n; simpl; intros; intuition try congruence. |
| 2260 |
f_equal; auto. |
| 2261 |
Qed. |
| 2262 |
|
| 2263 |
Lemma repeat_spec_eq : forall {A} (ls : list A) x n, |
| 2264 |
length ls = n |
| 2265 |
-> (forall y, In y ls -> y = x) |
| 2266 |
-> ls = repeat x n. |
| 2267 |
Proof. |
| 2268 |
intros; apply repeat_spec_iff; auto. |
| 2269 |
Qed. |
| 2270 |
|
| 2271 |
Lemma tl_repeat {A} x n : tl (@repeat A x n) = repeat x (pred n). |
| 2272 |
Proof. destruct n; reflexivity. Qed. |
| 2273 |
|
| 2274 |
Lemma firstn_repeat : forall {A} x n k, firstn k (@repeat A x n) = repeat x (min k n). |
| 2275 |
Proof. induction n, k; boring. Qed. |
| 2276 |
|
| 2277 |
#[global] |
| 2278 |
Hint Rewrite @firstn_repeat : push_firstn. |
| 2279 |
|
| 2280 |
Lemma skipn_repeat : forall {A} x n k, skipn k (@repeat A x n) = repeat x (n - k). |
| 2281 |
Proof. induction n, k; boring. Qed. |
| 2282 |
|
| 2283 |
#[global] |
| 2284 |
Hint Rewrite @skipn_repeat : push_skipn. |
| 2285 |
|
| 2286 |
Global Instance Proper_map {A B} {RA RB} {Equivalence_RB:Equivalence RB} |
| 2287 |
: Proper ((RA==>RB) ==> eqlistA RA ==> eqlistA RB) (@List.map A B). |
| 2288 |
Proof. |
| 2289 |
repeat intro. |
| 2290 |
match goal with [H:eqlistA _ _ _ |- _ ] => induction H end; [reflexivity|]. |
| 2291 |
cbv [respectful] in *; econstructor; eauto. |
| 2292 |
Qed. |
| 2293 |
|
| 2294 |
Lemma pointwise_map {A B} : Proper ((pointwise_relation _ eq) ==> eq ==> eq) (@List.map A B). |
| 2295 |
Proof. |
| 2296 |
repeat intro; cbv [pointwise_relation] in *; subst. |
| 2297 |
match goal with [H:list _ |- _ ] => induction H as [|? IH IHIH] end; [reflexivity|]. |
| 2298 |
simpl. rewrite IHIH. congruence. |
| 2299 |
Qed. |
| 2300 |
|
| 2301 |
Lemma map_map2 {A B C D} (f:A -> B -> C) (g:C -> D) (xs:list A) (ys:list B) : List.map g (map2 f xs ys) = map2 (fun (a : A) (b : B) => g (f a b)) xs ys. |
| 2302 |
Proof. |
| 2303 |
revert ys; induction xs as [|a xs IHxs]; intros ys; [reflexivity|]. |
| 2304 |
destruct ys; [reflexivity|]. |
| 2305 |
simpl. rewrite IHxs. reflexivity. |
| 2306 |
Qed. |
| 2307 |
|
| 2308 |
Lemma map2_fst {A B C} (f:A -> C) (xs:list A) : forall (ys:list B), length xs = length ys -> |
| 2309 |
map2 (fun (a : A) (_ : B) => f a) xs ys = List.map f xs. |
| 2310 |
Proof. |
| 2311 |
induction xs as [|a xs IHxs]; intros ys **; [reflexivity|]. |
| 2312 |
destruct ys; [simpl in *; discriminate|]. |
| 2313 |
simpl. rewrite IHxs by eauto. reflexivity. |
| 2314 |
Qed. |
| 2315 |
|
| 2316 |
Lemma map2_flip {A B C} (f:A -> B -> C) (xs:list A) : forall (ys: list B), |
| 2317 |
map2 (fun b a => f a b) ys xs = map2 f xs ys. |
| 2318 |
Proof. |
| 2319 |
induction xs as [|a xs IHxs]; destruct ys; try reflexivity; []. |
| 2320 |
simpl. rewrite IHxs. reflexivity. |
| 2321 |
Qed. |
| 2322 |
|
| 2323 |
Lemma map2_snd {A B C} (f:B -> C) (xs:list A) : forall (ys:list B), length xs = length ys -> |
| 2324 |
map2 (fun (_ : A) (b : B) => f b) xs ys = List.map f ys. |
| 2325 |
Proof. intros. rewrite map2_flip. eauto using map2_fst. Qed. |
| 2326 |
|
| 2327 |
Lemma map2_map {A B C A' B'} (f:A -> B -> C) (g:A' -> A) (h:B' -> B) (xs:list A') (ys:list B') |
| 2328 |
: map2 f (List.map g xs) (List.map h ys) = map2 (fun a b => f (g a) (h b)) xs ys. |
| 2329 |
Proof. |
| 2330 |
revert ys; induction xs as [|a xs IHxs]; destruct ys; intros; try reflexivity; []. |
| 2331 |
simpl. rewrite IHxs. reflexivity. |
| 2332 |
Qed. |
| 2333 |
|
| 2334 |
Definition expand_list_helper {A} (default : A) (ls : list A) (n : nat) (idx : nat) : list A |
| 2335 |
:= nat_rect |
| 2336 |
(fun _ => nat -> list A) |
| 2337 |
(fun _ => nil) |
| 2338 |
(fun n' rec_call idx |
| 2339 |
=> cons (List.nth_default default ls idx) (rec_call (S idx))) |
| 2340 |
n |
| 2341 |
idx. |
| 2342 |
Definition expand_list {A} (default : A) (ls : list A) (n : nat) : list A |
| 2343 |
:= expand_list_helper default ls n 0. |
| 2344 |
|
| 2345 |
Lemma expand_list_helper_correct {A} (default : A) (ls : list A) (n idx : nat) (H : (idx + n <= length ls)%nat) |
| 2346 |
: expand_list_helper default ls n idx |
| 2347 |
= List.firstn n (List.skipn idx ls). |
| 2348 |
Proof. |
| 2349 |
cbv [expand_list_helper]; revert idx H. |
| 2350 |
induction n as [|n IHn]; cbn; intros. |
| 2351 |
{ reflexivity. } |
| 2352 |
{ rewrite IHn by lia. |
| 2353 |
erewrite (@skipn_nth_default _ idx ls) by lia. |
| 2354 |
reflexivity. } |
| 2355 |
Qed. |
| 2356 |
|
| 2357 |
Lemma expand_list_correct (n : nat) {A} (default : A) (ls : list A) (H : List.length ls = n) |
| 2358 |
: expand_list default ls n = ls. |
| 2359 |
Proof. |
| 2360 |
subst; cbv [expand_list]; rewrite expand_list_helper_correct by reflexivity. |
| 2361 |
rewrite skipn_0, firstn_all; reflexivity. |
| 2362 |
Qed. |
| 2363 |
|
| 2364 |
Ltac expand_lists _ := |
| 2365 |
let default_for A := |
| 2366 |
match goal with |
| 2367 |
| _ => (eval lazy in (_ : pointed A)) |
| 2368 |
| _ => constr_fail_with ltac:(fun _ => idtac "Warning: could not infer a default value for list type" A) |
| 2369 |
end in |
| 2370 |
let T := lazymatch goal with |- _ = _ :> ?T => T end in |
| 2371 |
let v := fresh in |
| 2372 |
evar (v : T); transitivity v; |
| 2373 |
[ subst v |
| 2374 |
| repeat match goal with |
| 2375 |
| [ H : @List.length ?A ?f = ?n |- context[?f] ] |
| 2376 |
=> let v := default_for A in |
| 2377 |
rewrite <- (@expand_list_correct n A v f H); |
| 2378 |
clear H |
| 2379 |
end; |
| 2380 |
lazymatch goal with |
| 2381 |
| [ H : List.length ?f = _ |- context[?f] ] |
| 2382 |
=> fail 0 "Could not expand list" f |
| 2383 |
| _ => idtac |
| 2384 |
end; |
| 2385 |
subst v; reflexivity ]. |
| 2386 |
|
| 2387 |
Lemma single_list_rect_to_match A (P:list A -> Type) (Pnil: P nil) (PS: forall a tl, P (a :: tl)) ls : |
| 2388 |
@list_rect A P Pnil (fun a tl _ => PS a tl) ls = match ls with |
| 2389 |
| cons a tl => PS a tl |
| 2390 |
| nil => Pnil |
| 2391 |
end. |
| 2392 |
Proof. destruct ls; reflexivity. Qed. |
| 2393 |
|
| 2394 |
Lemma partition_app A (f : A -> bool) (a b : list A) |
| 2395 |
: partition f (a ++ b) = (fst (partition f a) ++ fst (partition f b), |
| 2396 |
snd (partition f a) ++ snd (partition f b)). |
| 2397 |
Proof. |
| 2398 |
revert b; induction a, b; cbn; rewrite ?app_nil_r; eta_expand; try reflexivity. |
| 2399 |
rewrite !IHa; cbn; break_match; reflexivity. |
| 2400 |
Qed. |
| 2401 |
|
| 2402 |
Lemma flat_map_map A B C (f : A -> B) (g : B -> list C) (xs : list A) |
| 2403 |
: flat_map g (map f xs) = flat_map (fun x => g (f x)) xs. |
| 2404 |
Proof. induction xs; cbn; congruence. Qed. |
| 2405 |
Lemma flat_map_singleton A B (f : A -> B) (xs : list A) |
| 2406 |
: flat_map (fun x => cons (f x) nil) xs = map f xs. |
| 2407 |
Proof. induction xs; cbn; congruence. Qed. |
| 2408 |
Lemma flat_map_ext A B (f g : A -> list B) xs (H : forall x, In x xs -> f x = g x) |
| 2409 |
: flat_map f xs = flat_map g xs. |
| 2410 |
Proof. induction xs; cbn in *; [ reflexivity | rewrite IHxs; f_equal ]; intros; intuition auto. Qed. |
| 2411 |
Global Instance flat_map_Proper A B : Proper (pointwise_relation _ eq ==> eq ==> eq) (@flat_map A B). |
| 2412 |
Proof. repeat intro; subst; apply flat_map_ext; auto. Qed. |
| 2413 |
|
| 2414 |
Global Instance map_Proper_eq {A B} : Proper ((eq ==> eq) ==> eq ==> eq) (@List.map A B) | 1. |
| 2415 |
Proof. repeat intro; subst; apply pointwise_map; repeat intro; eauto. Qed. |
| 2416 |
Global Instance flat_map_Proper_eq {A B} : Proper ((eq ==> eq) ==> eq ==> eq) (@List.flat_map A B) | 1. |
| 2417 |
Proof. repeat intro; subst; apply flat_map_Proper; repeat intro; eauto. Qed. |
| 2418 |
Global Instance partition_Proper {A} : Proper (pointwise_relation _ eq ==> eq ==> eq) (@List.partition A). |
| 2419 |
Proof. |
| 2420 |
cbv [pointwise_relation]; intros f g Hfg ls ls' ?; subst ls'. |
| 2421 |
induction ls as [|l ls IHls]; cbn [partition]; rewrite ?IHls, ?Hfg; reflexivity. |
| 2422 |
Qed. |
| 2423 |
Global Instance partition_Proper_eq {A} : Proper ((eq ==> eq) ==> eq ==> eq) (@List.partition A) | 1. |
| 2424 |
Proof. repeat intro; subst; apply partition_Proper; repeat intro; eauto. Qed. |
| 2425 |
|
| 2426 |
Lemma partition_map A B (f : B -> bool) (g : A -> B) xs |
| 2427 |
: partition f (map g xs) = (map g (fst (partition (fun x => f (g x)) xs)), |
| 2428 |
map g (snd (partition (fun x => f (g x)) xs))). |
| 2429 |
Proof. induction xs; cbn; [ | rewrite !IHxs ]; break_match; reflexivity. Qed. |
| 2430 |
Lemma map_fst_partition A B (f : B -> bool) (g : A -> B) xs |
| 2431 |
: map g (fst (partition (fun x => f (g x)) xs)) = fst (partition f (map g xs)). |
| 2432 |
Proof. rewrite partition_map; reflexivity. Qed. |
| 2433 |
Lemma map_snd_partition A B (f : B -> bool) (g : A -> B) xs |
| 2434 |
: map g (snd (partition (fun x => f (g x)) xs)) = snd (partition f (map g xs)). |
| 2435 |
Proof. rewrite partition_map; reflexivity. Qed. |
| 2436 |
Lemma partition_In A (f:A -> bool) xs : forall x, @In A x xs <-> @In A x (if f x then fst (partition f xs) else snd (partition f xs)). |
| 2437 |
Proof. |
| 2438 |
intro x; destruct (f x) eqn:?; split; intros; repeat apply conj; revert dependent x; |
| 2439 |
(induction xs as [|x' xs IHxs]; cbn; [ | destruct (f x') eqn:?, (partition f xs) ]; cbn in *; subst; intuition (subst; auto)); |
| 2440 |
congruence. |
| 2441 |
Qed. |
| 2442 |
Lemma fst_partition_In A f xs : forall x, @In A x (fst (partition f xs)) <-> f x = true /\ @In A x xs. |
| 2443 |
Proof. |
| 2444 |
intro x; split; intros; repeat apply conj; revert dependent x; |
| 2445 |
(induction xs as [|x' xs IHxs]; cbn; [ | destruct (f x') eqn:?, (partition f xs) ]; cbn in *; subst; intuition (subst; auto)); |
| 2446 |
congruence. |
| 2447 |
Qed. |
| 2448 |
Lemma snd_partition_In A f xs : forall x, @In A x (snd (partition f xs)) <-> f x = false /\ @In A x xs. |
| 2449 |
Proof. |
| 2450 |
intro x; split; intros; repeat apply conj; revert dependent x; |
| 2451 |
(induction xs as [|x' xs IHxs]; cbn; [ | destruct (f x') eqn:?, (partition f xs) ]; cbn in *; subst; intuition (subst; auto)); |
| 2452 |
congruence. |
| 2453 |
Qed. |
| 2454 |
|
| 2455 |
Lemma list_rect_map A B P (f : A -> B) N C ls |
| 2456 |
: @list_rect B P N C (map f ls) = @list_rect A (fun ls => P (map f ls)) N (fun x xs rest => C (f x) (map f xs) rest) ls. |
| 2457 |
Proof. induction ls as [|x xs IHxs]; cbn; [ | rewrite IHxs ]; reflexivity. Qed. |
| 2458 |
Lemma flat_map_app A B (f : A -> list B) xs ys |
| 2459 |
: flat_map f (xs ++ ys) = flat_map f xs ++ flat_map f ys. |
| 2460 |
Proof. induction xs as [|x xs IHxs]; cbn; rewrite ?IHxs, <- ?app_assoc; reflexivity. Qed. |
| 2461 |
#[global] |
| 2462 |
Hint Rewrite flat_map_app : push_flat_map. |
| 2463 |
Lemma map_flat_map A B C (f : A -> list B) (g : B -> C) xs |
| 2464 |
: map g (flat_map f xs) = flat_map (fun x => map g (f x)) xs. |
| 2465 |
Proof. induction xs as [|x xs IHxs]; cbn; rewrite ?map_app; congruence. Qed. |
| 2466 |
|
| 2467 |
Lemma flat_map_rev A B (f : A -> list B) xs |
| 2468 |
: flat_map f (rev xs) = rev (flat_map (fun x => rev (f x)) xs). |
| 2469 |
Proof. |
| 2470 |
induction xs as [|x xs IHxs]; cbn; autorewrite with push_flat_map; rewrite ?rev_app_distr, ?IHxs, ?rev_involutive, ?app_nil_r; reflexivity. |
| 2471 |
Qed. |
| 2472 |
#[global] |
| 2473 |
Hint Rewrite flat_map_rev : push_flat_map. |
| 2474 |
|
| 2475 |
Lemma rev_flat_map A B (f : A -> list B) xs |
| 2476 |
: rev (flat_map f xs) = flat_map (fun x => rev (f x)) (rev xs). |
| 2477 |
Proof. rewrite flat_map_rev; setoid_rewrite rev_involutive; reflexivity. Qed. |
| 2478 |
#[global] |
| 2479 |
Hint Rewrite rev_flat_map : push_rev. |
| 2480 |
|
| 2481 |
Lemma combine_map_map A B C D (f : A -> B) (g : C -> D) xs ys |
| 2482 |
: combine (map f xs) (map g ys) = map (fun ab => (f (fst ab), g (snd ab))) (combine xs ys). |
| 2483 |
Proof. revert ys; induction xs, ys; cbn; congruence. Qed. |
| 2484 |
Lemma combine_map_l A B C (f : A -> B) xs ys |
| 2485 |
: @combine B C (map f xs) ys = map (fun ab => (f (fst ab), snd ab)) (combine xs ys). |
| 2486 |
Proof. rewrite <- combine_map_map with (f:=f) (g:=fun x => x), map_id; reflexivity. Qed. |
| 2487 |
Lemma combine_map_r A B C (f : B -> C) xs ys |
| 2488 |
: @combine A C xs (map f ys) = map (fun ab => (fst ab, f (snd ab))) (combine xs ys). |
| 2489 |
Proof. rewrite <- combine_map_map with (g:=f) (f:=fun x => x), map_id; reflexivity. Qed. |
| 2490 |
Lemma combine_same A xs |
| 2491 |
: @combine A A xs xs = map (fun x => (x, x)) xs. |
| 2492 |
Proof. induction xs; cbn; congruence. Qed. |
| 2493 |
Lemma if_singleton A (b:bool) (x y : A) : (if b then x::nil else y::nil) = (if b then x else y)::nil. |
| 2494 |
Proof. now case b. Qed. |
| 2495 |
Lemma flat_map_if_In A B (b : A -> bool) (f g : A -> list B) xs (b' : bool) |
| 2496 |
: (forall v, In v xs -> b v = b') -> flat_map (fun x => if b x then f x else g x) xs = if b' then flat_map f xs else flat_map g xs. |
| 2497 |
Proof. induction xs as [|x xs IHxs]; cbn; [ | intro H; rewrite IHxs, H by eauto ]; case b'; reflexivity. Qed. |
| 2498 |
Lemma flat_map_if_In_sumbool A B X Y (b : forall a : A, sumbool (X a) (Y a)) (f g : A -> list B) xs (b' : bool) |
| 2499 |
: (forall v, In v xs -> (if b v then true else false) = b') -> flat_map (fun x => if b x then f x else g x) xs = if b' then flat_map f xs else flat_map g xs. |
| 2500 |
Proof. |
| 2501 |
intro H; erewrite <- flat_map_if_In by refine H. |
| 2502 |
apply flat_map_Proper; [ intro | reflexivity ]; break_innermost_match; reflexivity. |
| 2503 |
Qed. |
| 2504 |
Lemma map_if_In A B (b : A -> bool) (f g : A -> B) xs (b' : bool) |
| 2505 |
: (forall v, In v xs -> b v = b') -> map (fun x => if b x then f x else g x) xs = if b' then map f xs else map g xs. |
| 2506 |
Proof. induction xs as [|x xs IHxs]; cbn; [ | intro H; rewrite IHxs, H by eauto ]; case b'; reflexivity. Qed. |
| 2507 |
Lemma map_if_In_sumbool A B X Y (b : forall a : A, sumbool (X a) (Y a)) (f g : A -> B) xs (b' : bool) |
| 2508 |
: (forall v, In v xs -> (if b v then true else false) = b') -> map (fun x => if b x then f x else g x) xs = if b' then map f xs else map g xs. |
| 2509 |
Proof. |
| 2510 |
intro H; erewrite <- map_if_In by refine H. |
| 2511 |
apply map_ext_in; intro; break_innermost_match; reflexivity. |
| 2512 |
Qed. |
| 2513 |
Lemma fold_right_map A B C (f : A -> B) xs (F : _ -> _ -> C) v |
| 2514 |
: fold_right F v (map f xs) = fold_right (fun x y => F (f x) y) v xs. |
| 2515 |
Proof. revert v; induction xs; cbn; intros; congruence. Qed. |
| 2516 |
Lemma fold_right_flat_map A B C (f : A -> list B) xs (F : _ -> _ -> C) v |
| 2517 |
: fold_right F v (flat_map f xs) = fold_right (fun x y => fold_right F y (f x)) v xs. |
| 2518 |
Proof. revert v; induction xs; cbn; intros; rewrite ?fold_right_app; congruence. Qed. |
| 2519 |
|
| 2520 |
Lemma fold_right_ext_in A B f g v xs : (forall x y, List.In x xs -> f x y = g x y) -> @fold_right A B f v xs = fold_right g v xs. |
| 2521 |
Proof. induction xs; cbn; intro H; rewrite ?H, ?IHxs; auto. Qed. |
| 2522 |
Lemma fold_right_ext A B f g v xs : (forall x y, f x y = g x y) -> @fold_right A B f v xs = fold_right g v xs. |
| 2523 |
Proof. intros; apply fold_right_ext_in; eauto. Qed. |
| 2524 |
|
| 2525 |
Lemma fold_right_id_ext A B f v xs : (forall x y, f x y = y) -> @fold_right A B f v xs = v. |
| 2526 |
Proof. induction xs; cbn; intro H; rewrite ?H; auto. Qed. |
| 2527 |
Lemma fold_left_map A B C f f' l a |
| 2528 |
: @fold_left A B f (@List.map C _ f' l) a = fold_left (fun x y => f x (f' y)) l a. |
| 2529 |
Proof. revert a; induction l; cbn [List.map List.fold_left]; auto. Qed. |
| 2530 |
Lemma fold_left_flat_map A B C (f : A -> list B) xs (F : _ -> _ -> C) v |
| 2531 |
: fold_left F (flat_map f xs) v = fold_left (fun x y => fold_left F (f y) x) xs v. |
| 2532 |
Proof. revert v; induction xs; cbn; intros; rewrite ?fold_left_app; congruence. Qed. |
| 2533 |
|
| 2534 |
Lemma fold_left_ext_in A B f g v xs : (forall x y, List.In y xs -> f x y = g x y) -> @fold_left A B f xs v = fold_left g xs v. |
| 2535 |
Proof. intros; rewrite <- !fold_left_rev_right; apply fold_right_ext_in; intros *; rewrite <- in_rev; eauto. Qed. |
| 2536 |
Lemma fold_left_ext A B f g v xs : (forall x y, f x y = g x y) -> @fold_left A B f v xs = fold_left g v xs. |
| 2537 |
Proof. intros; apply fold_left_ext_in; eauto. Qed. |
| 2538 |
|
| 2539 |
Lemma fold_left_id_ext A B f v xs : (forall x y, f x y = x) -> @fold_left A B f xs v = v. |
| 2540 |
Proof. induction xs; cbn; intro H; rewrite ?H; auto. Qed. |
| 2541 |
Lemma nth_error_repeat_alt {A} (v : A) n i |
| 2542 |
: nth_error (repeat v n) i = if dec (i < n)%nat then Some v else None. |
| 2543 |
Proof. |
| 2544 |
revert i; induction n as [|n IHn], i; cbn; try reflexivity. |
| 2545 |
cbn [nth_error]; rewrite IHn; do 2 edestruct dec; try reflexivity; lia. |
| 2546 |
Qed. |
| 2547 |
Lemma nth_default_repeat A (v:A) n (d:A) i : nth_default d (repeat v n) i = if dec (i < n)%nat then v else d. |
| 2548 |
Proof. |
| 2549 |
cbv [nth_default]; rewrite nth_error_repeat_alt; now break_innermost_match. |
| 2550 |
Qed. |
| 2551 |
#[global] |
| 2552 |
Hint Rewrite nth_default_repeat : push_nth_default simpl_nth_default. |
| 2553 |
Lemma fold_right_if_dec_eq_seq A start len i f (x v : A) |
| 2554 |
: ((start <= i < start + len)%nat -> f i v = x) |
| 2555 |
-> (forall j v, (i <> j)%nat -> f j v = v) |
| 2556 |
-> fold_right f v (seq start len) = if dec (start <= i < start + len)%nat then x else v. |
| 2557 |
Proof. |
| 2558 |
revert start v; induction len as [|len IHlen]; intros start v H H'; [ | rewrite seq_snoc, fold_right_app; cbn [fold_right] ]. |
| 2559 |
{ edestruct dec; try reflexivity; lia. } |
| 2560 |
{ destruct (dec (i = (start + len)%nat)); subst; [ | rewrite H' by lia ]; |
| 2561 |
rewrite IHlen; eauto; intros; clear IHlen; |
| 2562 |
repeat match goal with |
| 2563 |
| _ => reflexivity |
| 2564 |
| _ => lia |
| 2565 |
| _ => progress subst |
| 2566 |
| _ => progress specialize_by lia |
| 2567 |
| [ H : context[dec ?P] |- _ ] => destruct (dec P) |
| 2568 |
| [ |- context[dec ?P] ] => destruct (dec P) |
| 2569 |
| [ H : f _ _ = _ |- _ ] => rewrite H |
| 2570 |
| [ H : forall j, f j ?v = _ |- context[f _ ?v] ] => rewrite H |
| 2571 |
end. } |
| 2572 |
Qed. |
| 2573 |
|
| 2574 |
Lemma fold_left_if_dec_eq_seq A start len i f (x v : A) |
| 2575 |
: ((start <= i < start + len)%nat -> f v i = x) |
| 2576 |
-> (forall j v, (i <> j)%nat -> f v j = v) |
| 2577 |
-> fold_left f (seq start len) v = if dec (start <= i < start + len)%nat then x else v. |
| 2578 |
Proof. |
| 2579 |
revert start v; induction len as [|len IHlen]; intros start v H H'; [ | rewrite seq_snoc, fold_left_app; cbn [fold_left] ]. |
| 2580 |
{ edestruct dec; try reflexivity; lia. } |
| 2581 |
{ destruct (dec (i = (start + len)%nat)); subst; [ | rewrite H' by lia ]; |
| 2582 |
rewrite IHlen; eauto; intros; clear IHlen; |
| 2583 |
repeat match goal with |
| 2584 |
| _ => reflexivity |
| 2585 |
| _ => lia |
| 2586 |
| _ => progress subst |
| 2587 |
| _ => progress specialize_by lia |
| 2588 |
| [ H : context[dec ?P] |- _ ] => destruct (dec P) |
| 2589 |
| [ |- context[dec ?P] ] => destruct (dec P) |
| 2590 |
| [ H : f _ _ = _ |- _ ] => rewrite H |
| 2591 |
| [ H : forall j, f j ?v = _ |- context[f _ ?v] ] => rewrite H |
| 2592 |
end. } |
| 2593 |
Qed. |
| 2594 |
|
| 2595 |
Lemma fold_left_push A (x y : A) (f : A -> A -> A) |
| 2596 |
(f_assoc : forall x y z, f (f x y) z = f x (f y z)) |
| 2597 |
ls |
| 2598 |
: f x (fold_left f ls y) = fold_left f ls (f x y). |
| 2599 |
Proof. |
| 2600 |
revert x y; induction ls as [|l ls IHls]; cbn; [ reflexivity | ]. |
| 2601 |
intros; rewrite IHls; f_equal; auto. |
| 2602 |
Qed. |
| 2603 |
|
| 2604 |
Lemma fold_right_push A (x y : A) (f : A -> A -> A) |
| 2605 |
(f_assoc : forall x y z, f (f x y) z = f x (f y z)) |
| 2606 |
ls |
| 2607 |
: f (fold_right f x ls) y = fold_right f (f x y) ls. |
| 2608 |
Proof. |
| 2609 |
rewrite <- (rev_involutive ls), !fold_left_rev_right, fold_left_push with (f:=fun x y => f y x); auto. |
| 2610 |
Qed. |
| 2611 |
|
| 2612 |
Lemma nth_error_combine {A B} n (ls1 : list A) (ls2 : list B) |
| 2613 |
: nth_error (combine ls1 ls2) n = match nth_error ls1 n, nth_error ls2 n with |
| 2614 |
| Some v1, Some v2 => Some (v1, v2) |
| 2615 |
| _, _ => None |
| 2616 |
end. |
| 2617 |
Proof. |
| 2618 |
revert ls2 n; induction ls1 as [|l1 ls1 IHls1], ls2, n; cbn [combine nth_error]; try reflexivity; auto. |
| 2619 |
edestruct nth_error; reflexivity. |
| 2620 |
Qed. |
| 2621 |
|
| 2622 |
Lemma combine_repeat {A B} (a : A) (b : B) n : combine (repeat a n) (repeat b n) = repeat (a, b) n. |
| 2623 |
Proof. induction n; cbn; congruence. Qed. |
| 2624 |
|
| 2625 |
Lemma combine_rev_rev_samelength {A B} ls1 ls2 : length ls1 = length ls2 -> @combine A B (rev ls1) (rev ls2) = rev (combine ls1 ls2). |
| 2626 |
Proof. |
| 2627 |
revert ls2; induction ls1 as [|? ? IHls1], ls2; cbn in *; try congruence; intros. |
| 2628 |
rewrite combine_app_samelength, IHls1 by (rewrite ?rev_length; congruence); cbn [combine]. |
| 2629 |
reflexivity. |
| 2630 |
Qed. |
| 2631 |
|
| 2632 |
Lemma map_nth_default_seq {A} (d:A) n ls |
| 2633 |
: length ls = n -> List.map (List.nth_default d ls) (List.seq 0 n) = ls. |
| 2634 |
Proof. |
| 2635 |
intro; subst. |
| 2636 |
rewrite <- (List.rev_involutive ls); generalize (List.rev ls); clear ls; intro ls. |
| 2637 |
rewrite List.rev_length. |
| 2638 |
induction ls; cbn [length List.rev]; [ reflexivity | ]. |
| 2639 |
rewrite seq_snoc, List.map_app. |
| 2640 |
apply f_equal2; [ | cbn; rewrite nth_default_app, List.rev_length, Nat.sub_diag ]; |
| 2641 |
[ etransitivity; [ | eassumption ]; apply List.map_ext_in; intro; rewrite Lists.List.in_seq; |
| 2642 |
rewrite nth_default_app, List.rev_length; intros |
| 2643 |
| ]. |
| 2644 |
all: edestruct lt_dec; try (exfalso; lia). |
| 2645 |
all: reflexivity. |
| 2646 |
Qed. |
| 2647 |
|
| 2648 |
Lemma nth_error_firstn A ls n i |
| 2649 |
: List.nth_error (@List.firstn A n ls) i = if lt_dec i n then List.nth_error ls i else None. |
| 2650 |
Proof. |
| 2651 |
revert ls i; induction n, ls, i; cbn; try reflexivity; destruct lt_dec; try reflexivity; rewrite IHn. |
| 2652 |
all: destruct lt_dec; try reflexivity; lia. |
| 2653 |
Qed. |
| 2654 |
|
| 2655 |
Lemma nth_error_rev A n ls : List.nth_error (@List.rev A ls) n = if lt_dec n (length ls) then List.nth_error ls (length ls - S n) else None. |
| 2656 |
Proof. |
| 2657 |
destruct lt_dec; [ | rewrite nth_error_length_error; rewrite ?List.rev_length; try reflexivity; lia ]. |
| 2658 |
revert dependent n; induction ls as [|x xs IHxs]; cbn [length List.rev]; try reflexivity; intros; try lia. |
| 2659 |
{ rewrite ListUtil.nth_error_app, List.rev_length, Nat.sub_succ. |
| 2660 |
destruct lt_dec. |
| 2661 |
{ rewrite IHxs by lia. |
| 2662 |
rewrite <- (Nat.succ_pred_pos (length xs - n)) by lia. |
| 2663 |
cbn [List.nth_error]. |
| 2664 |
f_equal; lia. } |
| 2665 |
{ assert (n = length xs) by lia; subst. |
| 2666 |
rewrite Nat.sub_diag. |
| 2667 |
reflexivity. } } |
| 2668 |
Qed. |
| 2669 |
|
| 2670 |
Lemma concat_fold_right_app A ls |
| 2671 |
: @List.concat A ls = List.fold_right (@List.app A) nil ls. |
| 2672 |
Proof. induction ls; cbn; eauto. Qed. |
| 2673 |
|
| 2674 |
Lemma map_update_nth_ext {A B n} f1 f2 f3 ls1 ls2 |
| 2675 |
: map f3 ls1 = ls2 |
| 2676 |
-> (forall x, List.In x ls1 -> f3 (f2 x) = f1 (f3 x)) |
| 2677 |
-> map f3 (@update_nth A n f2 ls1) = @update_nth B n f1 ls2. |
| 2678 |
Proof. |
| 2679 |
revert ls1 ls2; induction n as [|n IHn], ls1 as [|x1 xs1], ls2 as [|x2 xs2]; cbn; intros H0 H1; try discriminate; try reflexivity. |
| 2680 |
all: inversion H0; clear H0; subst. |
| 2681 |
all: f_equal; eauto using or_introl. |
| 2682 |
Qed. |
| 2683 |
|
| 2684 |
Lemma push_f_list_rect {P P'} (f : P -> P') {A} Pnil Pcons Pcons' ls |
| 2685 |
(Hcons : forall x xs rec, f (Pcons x xs rec) |
| 2686 |
= Pcons' x xs (f rec)) |
| 2687 |
: f (list_rect (fun _ : list A => P) Pnil Pcons ls) |
| 2688 |
= list_rect |
| 2689 |
(fun _ => _) |
| 2690 |
(f Pnil) |
| 2691 |
Pcons' |
| 2692 |
ls. |
| 2693 |
Proof. |
| 2694 |
induction ls as [|x xs IHxs]; cbn [list_rect]; [ reflexivity | ]. |
| 2695 |
rewrite Hcons, IHxs; reflexivity. |
| 2696 |
Qed. |
| 2697 |
|
| 2698 |
Lemma eq_app_list_rect {A} (ls1 ls2 : list A) |
| 2699 |
: List.app ls1 ls2 = list_rect _ ls2 (fun x _ rec => x :: rec) ls1. |
| 2700 |
Proof. revert ls2; induction ls1, ls2; cbn; f_equal; eauto. Qed. |
| 2701 |
Lemma eq_flat_map_list_rect {A B} f (ls : list A) |
| 2702 |
: @flat_map A B f ls = list_rect _ nil (fun x _ rec => f x ++ rec) ls. |
| 2703 |
Proof. induction ls; cbn; eauto. Qed. |
| 2704 |
Lemma eq_partition_list_rect {A} f (ls : list A) |
| 2705 |
: @partition A f ls = list_rect _ (nil, nil) (fun x _ '(a, b) => bool_rect (fun _ => _) (x :: a, b) (a, x :: b) (f x)) ls. |
| 2706 |
Proof. induction ls; cbn; eauto. Qed. |
| 2707 |
Lemma eq_fold_right_list_rect {A B} f v (ls : list _) |
| 2708 |
: @fold_right A B f v ls = list_rect _ v (fun x _ rec => f x rec) ls. |
| 2709 |
Proof. induction ls; cbn; eauto. Qed. |
| 2710 |
Lemma eq_fold_left_list_rect {A B} f v (ls : list _) |
| 2711 |
: @fold_left A B f ls v = list_rect _ (fun v => v) (fun x _ rec v => rec (f v x)) ls v. |
| 2712 |
Proof. revert f v; induction ls; cbn; eauto. Qed. |
| 2713 |
Lemma eq_map_list_rect {A B} f (ls : list _) |
| 2714 |
: @List.map A B f ls = list_rect _ nil (fun x _ rec => f x :: rec) ls. |
| 2715 |
Proof. induction ls; cbn; eauto. Qed. |
| 2716 |
Lemma eq_flat_map_fold_right {A B} f (ls : list A) |
| 2717 |
: @flat_map A B f ls = fold_right (fun x y => f x ++ y) nil ls. |
| 2718 |
Proof. induction ls; cbn; eauto. Qed. |
| 2719 |
Lemma eq_flat_map_fold_left_gen {A B} f (ls : list A) ls' |
| 2720 |
: fold_left (fun x y => x ++ f y) ls ls' = ls' ++ @flat_map A B f ls. |
| 2721 |
Proof. revert ls'; induction ls; cbn; intros; rewrite ?app_nil_r, ?IHls, ?app_assoc; eauto. Qed. |
| 2722 |
Lemma eq_flat_map_fold_left {A B} f (ls : list A) |
| 2723 |
: @flat_map A B f ls = fold_left (fun x y => x ++ f y) ls nil. |
| 2724 |
Proof. rewrite eq_flat_map_fold_left_gen; reflexivity. Qed. |
| 2725 |
|
| 2726 |
Lemma map_repeat {A B} (f : A -> B) v k |
| 2727 |
: List.map f (List.repeat v k) = List.repeat (f v) k. |
| 2728 |
Proof. induction k; cbn; f_equal; assumption. Qed. |
| 2729 |
Lemma map_const {A B} (v : B) (ls : list A) |
| 2730 |
: List.map (fun _ => v) ls = List.repeat v (List.length ls). |
| 2731 |
Proof. induction ls; cbn; f_equal; assumption. Qed. |
| 2732 |
|
| 2733 |
Lemma Forall2_rev {A B R ls1 ls2} |
| 2734 |
: @List.Forall2 A B R ls1 ls2 |
| 2735 |
-> List.Forall2 R (rev ls1) (rev ls2). |
| 2736 |
Proof using Type. |
| 2737 |
induction 1; cbn [rev]; [ constructor | ]. |
| 2738 |
apply Forall2_app; auto. |
| 2739 |
Qed. |
| 2740 |
|
| 2741 |
Lemma Forall2_update_nth {A B f g n R ls1 ls2} |
| 2742 |
: @List.Forall2 A B R ls1 ls2 |
| 2743 |
-> (forall v1, nth_error ls1 n = Some v1 -> forall v2, nth_error ls2 n = Some v2 -> R v1 v2 -> R (f v1) (g v2)) |
| 2744 |
-> @List.Forall2 A B R (update_nth n f ls1) (update_nth n g ls2). |
| 2745 |
Proof using Type. |
| 2746 |
intro H; revert n; induction H, n; cbn [nth_error update_nth]. |
| 2747 |
all: repeat first [ progress intros |
| 2748 |
| progress specialize_by_assumption |
| 2749 |
| assumption |
| 2750 |
| match goal with |
| 2751 |
| [ |- List.Forall2 _ _ _ ] => constructor |
| 2752 |
| [ H : forall x, Some _ = Some x -> _ |- _ ] => specialize (H _ eq_refl) |
| 2753 |
| [ IH : forall n : nat, _, H : forall v1, nth_error ?l ?n = Some v1 -> _ |- _ ] => specialize (IH n H) |
| 2754 |
end ]. |
| 2755 |
Qed. |
| 2756 |
|
| 2757 |
Fixpoint remove_duplicates' {A} (beq : A -> A -> bool) (ls : list A) : list A |
| 2758 |
:= match ls with |
| 2759 |
| nil => nil |
| 2760 |
| cons x xs => if existsb (beq x) xs |
| 2761 |
then @remove_duplicates' A beq xs |
| 2762 |
else x :: @remove_duplicates' A beq xs |
| 2763 |
end. |
| 2764 |
Definition remove_duplicates {A} (beq : A -> A -> bool) (ls : list A) : list A |
| 2765 |
:= List.rev (remove_duplicates' beq (List.rev ls)). |
| 2766 |
Fixpoint find_duplicates' {A} (beq : A -> A -> bool) (ls : list A) : list A |
| 2767 |
:= match ls with |
| 2768 |
| nil => nil |
| 2769 |
| cons x xs => if existsb (beq x) xs |
| 2770 |
then x :: @find_duplicates' A beq xs |
| 2771 |
else @find_duplicates' A beq xs |
| 2772 |
end. |
| 2773 |
Definition find_duplicates {A} (beq : A -> A -> bool) (ls : list A) : list A |
| 2774 |
:= remove_duplicates beq (find_duplicates' beq ls). |
| 2775 |
|
| 2776 |
Lemma InA_remove_duplicates' |
| 2777 |
{A} (A_beq : A -> A -> bool) |
| 2778 |
(R : A -> A -> Prop) |
| 2779 |
{R_Transitive : Transitive R} |
| 2780 |
(A_bl : forall x y, A_beq x y = true -> R x y) |
| 2781 |
(ls : list A) |
| 2782 |
: forall x, InA R x (remove_duplicates' A_beq ls) <-> InA R x ls. |
| 2783 |
Proof using Type. |
| 2784 |
induction ls as [|x xs IHxs]; intro y; [ reflexivity | ]. |
| 2785 |
cbn [remove_duplicates']; break_innermost_match; |
| 2786 |
rewrite ?InA_cons, IHxs; [ | reflexivity ]. |
| 2787 |
split; [ now auto | ]. |
| 2788 |
intros [?|?]; subst; auto; []. |
| 2789 |
rewrite existsb_exists in *. |
| 2790 |
destruct_head'_ex; destruct_head'_and. |
| 2791 |
match goal with H : _ |- _ => apply A_bl in H end. |
| 2792 |
rewrite InA_alt. |
| 2793 |
eexists; split; [ | eassumption ]. |
| 2794 |
etransitivity; eassumption. |
| 2795 |
Qed. |
| 2796 |
|
| 2797 |
Lemma InA_remove_duplicates |
| 2798 |
{A} (A_beq : A -> A -> bool) |
| 2799 |
(R : A -> A -> Prop) |
| 2800 |
{R_Transitive : Transitive R} |
| 2801 |
(A_bl : forall x y, A_beq x y = true -> R x y) |
| 2802 |
(ls : list A) |
| 2803 |
: forall x, InA R x (remove_duplicates A_beq ls) <-> InA R x ls. |
| 2804 |
Proof using Type. |
| 2805 |
cbv [remove_duplicates]; intro. |
| 2806 |
rewrite InA_rev, InA_remove_duplicates', InA_rev; auto; reflexivity. |
| 2807 |
Qed. |
| 2808 |
|
| 2809 |
Lemma InA_eq_In_iff {A} x ls |
| 2810 |
: InA eq x ls <-> @List.In A x ls. |
| 2811 |
Proof using Type. |
| 2812 |
rewrite InA_alt. |
| 2813 |
repeat first [ progress destruct_head'_and |
| 2814 |
| progress destruct_head'_ex |
| 2815 |
| progress subst |
| 2816 |
| solve [ eauto ] |
| 2817 |
| apply conj |
| 2818 |
| progress intros ]. |
| 2819 |
Qed. |
| 2820 |
|
| 2821 |
Lemma NoDupA_eq_NoDup {A} ls |
| 2822 |
: @NoDupA A eq ls <-> NoDup ls. |
| 2823 |
Proof using Type. |
| 2824 |
split; intro H; induction H; constructor; eauto; |
| 2825 |
(idtac + rewrite <- InA_eq_In_iff + rewrite InA_eq_In_iff); assumption. |
| 2826 |
Qed. |
| 2827 |
|
| 2828 |
Lemma in_remove_duplicates' |
| 2829 |
{A} (A_beq : A -> A -> bool) (A_bl : forall x y, A_beq x y = true -> x = y) |
| 2830 |
(ls : list A) |
| 2831 |
: forall x, List.In x (remove_duplicates' A_beq ls) <-> List.In x ls. |
| 2832 |
Proof using Type. |
| 2833 |
intro x; rewrite <- !InA_eq_In_iff; apply InA_remove_duplicates'; eauto; exact _. |
| 2834 |
Qed. |
| 2835 |
|
| 2836 |
Lemma in_remove_duplicates |
| 2837 |
{A} (A_beq : A -> A -> bool) (A_bl : forall x y, A_beq x y = true -> x = y) |
| 2838 |
(ls : list A) |
| 2839 |
: forall x, List.In x (remove_duplicates A_beq ls) <-> List.In x ls. |
| 2840 |
Proof using Type. |
| 2841 |
intro x; rewrite <- !InA_eq_In_iff; apply InA_remove_duplicates; eauto; exact _. |
| 2842 |
Qed. |
| 2843 |
|
| 2844 |
Lemma NoDupA_remove_duplicates' {A} (A_beq : A -> A -> bool) |
| 2845 |
(R : A -> A -> Prop) |
| 2846 |
{R_Transitive : Transitive R} |
| 2847 |
(A_lb : forall x y, A_beq x y = true -> R x y) |
| 2848 |
(A_bl : forall x y, R x y -> A_beq x y = true) |
| 2849 |
(ls : list A) |
| 2850 |
: NoDupA R (remove_duplicates' A_beq ls). |
| 2851 |
Proof using Type. |
| 2852 |
induction ls as [|x xs IHxs]; [ now constructor | ]. |
| 2853 |
cbn [remove_duplicates']; break_innermost_match; [ assumption | constructor; auto ]; []. |
| 2854 |
intro H'. |
| 2855 |
cut (false = true); [ discriminate | ]. |
| 2856 |
match goal with H : _ = false |- _ => rewrite <- H end. |
| 2857 |
rewrite existsb_exists in *. |
| 2858 |
rewrite InA_remove_duplicates' in H' by eauto. |
| 2859 |
rewrite InA_alt in H'. |
| 2860 |
destruct_head'_ex; destruct_head'_and. |
| 2861 |
eauto. |
| 2862 |
Qed. |
| 2863 |
|
| 2864 |
Lemma NoDupA_remove_duplicates {A} (A_beq : A -> A -> bool) |
| 2865 |
(R : A -> A -> Prop) |
| 2866 |
{R_Equivalence : Equivalence R} |
| 2867 |
(A_lb : forall x y, A_beq x y = true -> R x y) |
| 2868 |
(A_bl : forall x y, R x y -> A_beq x y = true) |
| 2869 |
(ls : list A) |
| 2870 |
: NoDupA R (remove_duplicates A_beq ls). |
| 2871 |
Proof using Type. |
| 2872 |
cbv [remove_duplicates]. |
| 2873 |
apply NoDupA_rev; [ assumption | ]. |
| 2874 |
apply NoDupA_remove_duplicates'; auto; exact _. |
| 2875 |
Qed. |
| 2876 |
|
| 2877 |
Lemma NoDup_remove_duplicates' {A} (A_beq : A -> A -> bool) |
| 2878 |
(R : A -> A -> Prop) |
| 2879 |
(A_lb : forall x y, A_beq x y = true -> x = y) |
| 2880 |
(A_bl : forall x y, x = y -> A_beq x y = true) |
| 2881 |
(ls : list A) |
| 2882 |
: NoDup (remove_duplicates' A_beq ls). |
| 2883 |
Proof using Type. |
| 2884 |
apply NoDupA_eq_NoDup, NoDupA_remove_duplicates'; auto; exact _. |
| 2885 |
Qed. |
| 2886 |
|
| 2887 |
Lemma NoDup_remove_duplicates {A} (A_beq : A -> A -> bool) |
| 2888 |
(A_lb : forall x y, A_beq x y = true -> x = y) |
| 2889 |
(A_bl : forall x y, x = y -> A_beq x y = true) |
| 2890 |
(ls : list A) |
| 2891 |
: NoDup (remove_duplicates A_beq ls). |
| 2892 |
Proof using Type. |
| 2893 |
apply NoDupA_eq_NoDup, NoDupA_remove_duplicates; auto; exact _. |
| 2894 |
Qed. |
| 2895 |
|
| 2896 |
Lemma remove_duplicates'_eq_NoDupA {A} (A_beq : A -> A -> bool) |
| 2897 |
(R : A -> A -> Prop) |
| 2898 |
(A_lb : forall x y, A_beq x y = true -> R x y) |
| 2899 |
(ls : list A) |
| 2900 |
: NoDupA R ls -> remove_duplicates' A_beq ls = ls. |
| 2901 |
Proof using Type. |
| 2902 |
intro H; induction H as [|x xs H0 H1 IHxs]; [ reflexivity | ]. |
| 2903 |
cbn [remove_duplicates']. |
| 2904 |
rewrite IHxs. |
| 2905 |
repeat first [ break_innermost_match_step |
| 2906 |
| reflexivity |
| 2907 |
| progress destruct_head'_ex |
| 2908 |
| progress destruct_head'_and |
| 2909 |
| progress rewrite existsb_exists in * |
| 2910 |
| progress rewrite InA_alt in * |
| 2911 |
| match goal with |
| 2912 |
| [ H : ~(exists x, and _ _) |- _ ] |
| 2913 |
=> specialize (fun x H0 H1 => H (ex_intro _ x (conj H0 H1))) |
| 2914 |
end |
| 2915 |
| solve [ exfalso; eauto ] ]. |
| 2916 |
Qed. |
| 2917 |
|
| 2918 |
Lemma remove_duplicates_eq_NoDupA {A} (A_beq : A -> A -> bool) |
| 2919 |
(R : A -> A -> Prop) |
| 2920 |
{R_equiv : Equivalence R} |
| 2921 |
(A_lb : forall x y, A_beq x y = true -> R x y) |
| 2922 |
(ls : list A) |
| 2923 |
: NoDupA R ls -> remove_duplicates A_beq ls = ls. |
| 2924 |
Proof using Type. |
| 2925 |
cbv [remove_duplicates]; intro. |
| 2926 |
erewrite remove_duplicates'_eq_NoDupA by (eauto + apply NoDupA_rev; eauto). |
| 2927 |
rewrite rev_involutive; reflexivity. |
| 2928 |
Qed. |
| 2929 |
|
| 2930 |
Lemma remove_duplicates'_eq_NoDup {A} (A_beq : A -> A -> bool) |
| 2931 |
(A_lb : forall x y, A_beq x y = true -> x = y) |
| 2932 |
(ls : list A) |
| 2933 |
: NoDup ls -> remove_duplicates' A_beq ls = ls. |
| 2934 |
Proof using Type. |
| 2935 |
intro H; apply remove_duplicates'_eq_NoDupA with (R:=eq); eauto. |
| 2936 |
now apply NoDupA_eq_NoDup. |
| 2937 |
Qed. |
| 2938 |
|
| 2939 |
Lemma remove_duplicates_eq_NoDup {A} (A_beq : A -> A -> bool) |
| 2940 |
(A_lb : forall x y, A_beq x y = true -> x = y) |
| 2941 |
(ls : list A) |
| 2942 |
: NoDup ls -> remove_duplicates A_beq ls = ls. |
| 2943 |
Proof using Type. |
| 2944 |
intro H; apply remove_duplicates_eq_NoDupA with (R:=eq); eauto; try exact _. |
| 2945 |
now apply NoDupA_eq_NoDup. |
| 2946 |
Qed. |
| 2947 |
|
| 2948 |
Lemma eq_repeat_nat_rect {A} x n |
| 2949 |
: @List.repeat A x n |
| 2950 |
= nat_rect _ nil (fun k repeat_k => x :: repeat_k) n. |
| 2951 |
Proof using Type. induction n; cbn; f_equal; assumption. Qed. |
| 2952 |
|
| 2953 |
Lemma eq_firstn_nat_rect {A} n ls |
| 2954 |
: @List.firstn A n ls |
| 2955 |
= nat_rect |
| 2956 |
_ |
| 2957 |
(fun _ => nil) |
| 2958 |
(fun n' firstn_n' ls |
| 2959 |
=> match ls with |
| 2960 |
| nil => nil |
| 2961 |
| cons x xs => x :: firstn_n' xs |
| 2962 |
end) |
| 2963 |
n ls. |
| 2964 |
Proof using Type. revert ls; induction n, ls; cbn; f_equal; auto. Qed. |
| 2965 |
|
| 2966 |
Lemma eq_skipn_nat_rect {A} n ls |
| 2967 |
: @List.skipn A n ls |
| 2968 |
= nat_rect |
| 2969 |
_ |
| 2970 |
(fun ls => ls) |
| 2971 |
(fun n' skipn_n' ls |
| 2972 |
=> match ls with |
| 2973 |
| nil => nil |
| 2974 |
| cons x xs => skipn_n' xs |
| 2975 |
end) |
| 2976 |
n ls. |
| 2977 |
Proof using Type. revert ls; induction n, ls; cbn; f_equal; auto. Qed. |
| 2978 |
|
| 2979 |
Lemma eq_combine_list_rect {A B} xs ys |
| 2980 |
: @List.combine A B xs ys |
| 2981 |
= list_rect |
| 2982 |
_ |
| 2983 |
(fun _ => nil) |
| 2984 |
(fun x xs combine_xs ys |
| 2985 |
=> match ys with |
| 2986 |
| nil => nil |
| 2987 |
| y :: ys => (x, y) :: combine_xs ys |
| 2988 |
end) |
| 2989 |
xs ys. |
| 2990 |
Proof using Type. revert ys; induction xs, ys; cbn; f_equal; auto. Qed. |
| 2991 |
|
| 2992 |
Lemma eq_length_list_rect {A} xs |
| 2993 |
: @List.length A xs |
| 2994 |
= (list_rect _) |
| 2995 |
0%nat |
| 2996 |
(fun _ xs length_xs => S length_xs) |
| 2997 |
xs. |
| 2998 |
Proof using Type. induction xs; cbn; f_equal; auto. Qed. |
| 2999 |
|
| 3000 |
Lemma eq_rev_list_rect {A} xs |
| 3001 |
: @List.rev A xs |
| 3002 |
= (list_rect _) |
| 3003 |
nil |
| 3004 |
(fun x xs rev_xs => rev_xs ++ [x]) |
| 3005 |
xs. |
| 3006 |
Proof using Type. induction xs; cbn; f_equal; auto. Qed. |
| 3007 |
|
| 3008 |
Lemma eq_update_nth_nat_rect {A} n f xs |
| 3009 |
: @update_nth A n f xs |
| 3010 |
= (nat_rect _) |
| 3011 |
(fun xs => match xs with |
| 3012 |
| nil => nil |
| 3013 |
| x' :: xs' => f x' :: xs' |
| 3014 |
end) |
| 3015 |
(fun n' update_nth_n' xs |
| 3016 |
=> match xs with |
| 3017 |
| nil => nil |
| 3018 |
| x' :: xs' => x' :: update_nth_n' xs' |
| 3019 |
end) |
| 3020 |
n |
| 3021 |
xs. |
| 3022 |
Proof using Type. revert xs; induction n, xs; cbn; f_equal; auto. Qed. |
| 3023 |
|
| 3024 |
Lemma flat_map_const_nil {A B} ls : @flat_map A B (fun _ => nil) ls = nil. |
| 3025 |
Proof using Type. induction ls; cbn; auto. Qed. |
| 3026 |
|
| 3027 |
Lemma Forall_map_iff {A B} (f : A -> B) ls P |
| 3028 |
: Forall P (List.map f ls) <-> Forall (fun x => P (f x)) ls. |
| 3029 |
Proof. |
| 3030 |
induction ls as [|?? IH]; cbn [List.map]; split; intro H; inversion_clear H; constructor; split_iff; auto. |
| 3031 |
Qed. |
| 3032 |
|
| 3033 |
Lemma ForallOrdPairs_map_iff {A B} (f : A -> B) ls P |
| 3034 |
: ForallOrdPairs P (List.map f ls) <-> ForallOrdPairs (fun x y => P (f x) (f y)) ls. |
| 3035 |
Proof. |
| 3036 |
pose proof (@Forall_map_iff A B f) as HF. |
| 3037 |
induction ls as [|?? IH]; cbn [List.map]; split; intro H; inversion_clear H; constructor; split_iff; auto. |
| 3038 |
Qed. |
| 3039 |
|
| 3040 |
Lemma HdRel_map_iff {A B} (f : A -> B) R x xs |
| 3041 |
: HdRel R (f x) (List.map f xs) <-> HdRel (fun x y => R (f x) (f y)) x xs. |
| 3042 |
Proof. |
| 3043 |
destruct xs; split; intro H; inversion_clear H; constructor; auto. |
| 3044 |
Qed. |
| 3045 |
|
| 3046 |
Lemma Sorted_map_iff {A B} (f : A -> B) R ls |
| 3047 |
: Sorted R (List.map f ls) <-> Sorted (fun x y => R (f x) (f y)) ls. |
| 3048 |
Proof. |
| 3049 |
induction ls as [|?? IH]; cbn [List.map]; split; intro H; inversion_clear H; |
| 3050 |
constructor; split_iff; auto; now apply HdRel_map_iff. |
| 3051 |
Qed. |
| 3052 |
|
| 3053 |
Lemma In_nth_error_iff {A l x} |
| 3054 |
: In x l <-> exists n : nat, @nth_error A l n = Some x. |
| 3055 |
Proof. |
| 3056 |
split; [ now apply In_nth_error | intros [? ?]; eapply nth_error_In; eassumption ]. |
| 3057 |
Qed. |
| 3058 |
|
| 3059 |
Lemma fold_right_fun_apply {A B C} (ls : list B) (f : B -> C -> (A -> C)) init x |
| 3060 |
: fold_right (fun b F a => f b (F a) a) init ls x = fold_right (fun b c => f b c x) (init x) ls. |
| 3061 |
Proof. induction ls as [|?? IH]; cbn; now f_equal. Qed. |
| 3062 |
|
| 3063 |
Ltac make_fold_right_fun_apply ty := |
| 3064 |
multimatch ty with |
| 3065 |
| context[@fold_right ?AC ?B ?f ?init ?ls ?x] |
| 3066 |
=> let fv := fresh in |
| 3067 |
let b := fresh "b" in |
| 3068 |
let F := fresh "F" in |
| 3069 |
let a := fresh "a" in |
| 3070 |
let f := lazymatch |
| 3071 |
constr:( |
| 3072 |
fun b F a |
| 3073 |
=> match f b F a return _ with |
| 3074 |
| fv |
| 3075 |
=> ltac:(let fv := (eval cbv [fv] in fv) in |
| 3076 |
lazymatch (eval pattern a, (F a), b in fv) with |
| 3077 |
| ?f _ _ _ => refine (fun x y z => f z y x) |
| 3078 |
end) |
| 3079 |
end) with |
| 3080 |
| fun _ _ _ => ?f => (eval cbv beta in f) |
| 3081 |
| ?f => idtac "failed to eliminate the functional dependencies of" f; |
| 3082 |
fail 0 "failed to eliminate the functional dependencies of" f |
| 3083 |
end in |
| 3084 |
constr:(@fold_right_fun_apply _ _ _ ls f init x) |
| 3085 |
end. |
| 3086 |
|
| 3087 |
Ltac rewrite_fold_right_fun_apply := |
| 3088 |
match goal with |
| 3089 |
| [ H : ?T |- _ ] => let pf := make_fold_right_fun_apply T in |
| 3090 |
rewrite pf in H |
| 3091 |
| [ |- ?T ] => let pf := make_fold_right_fun_apply T in |
| 3092 |
rewrite pf |
| 3093 |
end. |
| 3094 |
|
| 3095 |
Lemma fold_left_fun_apply {A B C} (ls : list B) (f : C -> B -> (A -> C)) init x |
| 3096 |
: fold_left (fun F b a => f (F a) b a) ls init x = fold_left (fun b c => f b c x) ls (init x). |
| 3097 |
Proof. rewrite <- !fold_left_rev_right; now rewrite_fold_right_fun_apply. Qed. |
| 3098 |
|
| 3099 |
Ltac make_fold_left_fun_apply ty := |
| 3100 |
multimatch ty with |
| 3101 |
| context[@fold_left ?AC ?B ?f ?ls ?init ?x] |
| 3102 |
=> let fv := fresh in |
| 3103 |
let b := fresh "b" in |
| 3104 |
let F := fresh "F" in |
| 3105 |
let a := fresh "a" in |
| 3106 |
let f := lazymatch |
| 3107 |
constr:( |
| 3108 |
fun F b a |
| 3109 |
=> match f F b a return _ with |
| 3110 |
| fv |
| 3111 |
=> ltac:(let fv := (eval cbv [fv] in fv) in |
| 3112 |
lazymatch (eval pattern a, b, (F a) in fv) with |
| 3113 |
| ?f _ _ _ => refine (fun x y z => f z y x) |
| 3114 |
end) |
| 3115 |
end) with |
| 3116 |
| fun _ _ _ => ?f => (eval cbv beta in f) |
| 3117 |
| ?f => idtac "failed to eliminate the functional dependencies of" f; |
| 3118 |
fail 0 "failed to eliminate the functional dependencies of" f |
| 3119 |
end in |
| 3120 |
constr:(@fold_left_fun_apply _ _ _ ls f init x) |
| 3121 |
end. |
| 3122 |
|
| 3123 |
Ltac rewrite_fold_left_fun_apply := |
| 3124 |
match goal with |
| 3125 |
| [ H : ?T |- _ ] => let pf := make_fold_left_fun_apply T in |
| 3126 |
rewrite pf in H |
| 3127 |
| [ |- ?T ] => let pf := make_fold_left_fun_apply T in |
| 3128 |
rewrite pf |
| 3129 |
end. |
| 3130 |
|
| 3131 |
Definition span_cps' {A} (f : A -> bool) {T} (k : list A * list A -> T) |
| 3132 |
:= fix span_cps' (ls : list A) (prefix : list A) : T |
| 3133 |
:= match ls with |
| 3134 |
| nil => k (List.rev prefix, ls) |
| 3135 |
| x :: xs => if f x then span_cps' xs (x :: prefix) else k (List.rev prefix, ls) |
| 3136 |
end. |
| 3137 |
|
| 3138 |
Definition span_cps {A} (f : A -> bool) (ls : list A) {T} (k : list A * list A -> T) : T |
| 3139 |
:= span_cps' f k ls []. |
| 3140 |
|
| 3141 |
Definition span {A} (f : A -> bool) (ls : list A) : list A * list A |
| 3142 |
:= span_cps f ls id. |
| 3143 |
Definition takeWhile {A} (f : A -> bool) (ls : list A) : list A := fst (span f ls). |
| 3144 |
Definition dropWhile {A} (f : A -> bool) (ls : list A) : list A := snd (span f ls). |
| 3145 |
|
| 3146 |
Lemma span_cps'_id A f xs T k prefix |
| 3147 |
: @span_cps' A f T k xs prefix = k (List.rev prefix ++ fst (span f xs), snd (span f xs)). |
| 3148 |
Proof. |
| 3149 |
revert T k prefix; induction xs as [|?? IH]; intros; cbn; try (rewrite !IH; clear IH); cbv [id]; break_innermost_match; cbn [List.rev fst snd List.app]; rewrite ?List.app_nil_l, ?List.app_nil_r, <- ?List.app_assoc; try reflexivity. |
| 3150 |
Qed. |
| 3151 |
|
| 3152 |
Lemma span_cps_id A f xs T k |
| 3153 |
: @span_cps A f xs T k = k (span f xs). |
| 3154 |
Proof. cbv [span_cps]; rewrite span_cps'_id; destruct span; reflexivity. Qed. |
| 3155 |
|
| 3156 |
Lemma span_nil A f : @span A f nil = (nil, nil). |
| 3157 |
Proof. reflexivity. Qed. |
| 3158 |
Lemma span_cons A f x xs : @span A f (x :: xs) = if f x then let '(xs, ys) := span f xs in (x :: xs, ys) else (nil, x :: xs). |
| 3159 |
Proof. cbv [span span_cps]; cbn; rewrite !span_cps'_id; cbv [id]; reflexivity. Qed. |
| 3160 |
|
| 3161 |
Lemma span_app {A} f xs |
| 3162 |
: fst (@span A f xs) ++ snd (@span A f xs) = xs. |
| 3163 |
Proof. induction xs; rewrite ?span_nil, ?span_cons; break_innermost_match; boring. Qed. |
| 3164 |
|
| 3165 |
Lemma takeWhile_app_dropWhile {A} f xs |
| 3166 |
: @takeWhile A f xs ++ @dropWhile A f xs = xs. |
| 3167 |
Proof. apply span_app. Qed. |
| 3168 |
|
| 3169 |
Lemma filter_fst_span {A} f xs |
| 3170 |
: filter f (fst (@span A f xs)) = fst (@span A f xs). |
| 3171 |
Proof. induction xs; rewrite ?span_nil, ?span_cons; break_innermost_match; boring. Qed. |
| 3172 |
|
| 3173 |
Lemma filter_takeWhile {A} f xs |
| 3174 |
: filter f (@takeWhile A f xs) = @takeWhile A f xs. |
| 3175 |
Proof. apply filter_fst_span. Qed. |
| 3176 |
|
| 3177 |
Lemma hd_not_snd_span {A} f xs x |
| 3178 |
(H : nth_error (snd (@span A f xs)) 0 = Some x) |
| 3179 |
: f x = false. |
| 3180 |
Proof. |
| 3181 |
induction xs. |
| 3182 |
all: repeat first [ rewrite !span_nil in * |
| 3183 |
| rewrite !span_cons in * |
| 3184 |
| progress cbn in * |
| 3185 |
| progress cbv [fst snd] in * |
| 3186 |
| congruence |
| 3187 |
| solve [ auto ] |
| 3188 |
| break_innermost_match_hyps_step ]. |
| 3189 |
Qed. |
| 3190 |
|
| 3191 |
|
| 3192 |
Definition groupBy' {A} (f : A -> A -> bool) |
| 3193 |
:= fix groupBy' (ls : list A) (prefix : list A) : list (list A) |
| 3194 |
:= match ls with |
| 3195 |
| [] => [] |
| 3196 |
| x :: xs => span_cps' |
| 3197 |
(f x) (fun '(xs, ys) |
| 3198 |
=> (x :: xs) :: match ys with |
| 3199 |
| [] => [] |
| 3200 |
| _ => groupBy' ys [] |
| 3201 |
end) |
| 3202 |
xs prefix |
| 3203 |
end. |
| 3204 |
Definition groupBy {A} (f : A -> A -> bool) (ls : list A) : list (list A) |
| 3205 |
:= groupBy' f ls []. |
| 3206 |
|
| 3207 |
Definition span_cps'_rect A (f : A -> bool) T (k : list A * list A -> T) |
| 3208 |
(P : list A -> list A -> T -> Type) |
| 3209 |
(Pnil : forall prefix, P nil prefix (k (List.rev prefix, nil))) |
| 3210 |
(Pcons_true : forall x xs prefix rv, f x = true -> P xs (x :: prefix) rv -> P (x :: xs) prefix rv) |
| 3211 |
(Pcons_false : forall x ls prefix xs, f x = false -> ls = x :: xs -> P ls prefix (k (List.rev prefix, ls))) |
| 3212 |
: forall (ls : list A) (prefix : list A), P ls prefix (@span_cps' A f T k ls prefix). |
| 3213 |
Proof. |
| 3214 |
fix span_cps'_rect 1. |
| 3215 |
intro ls. |
| 3216 |
case_eq ls; [ | intros x xs ]; intros H prefix; cbn [span_cps']. |
| 3217 |
{ apply Pnil. } |
| 3218 |
{ destruct (f x) eqn:H'. |
| 3219 |
{ apply Pcons_true; [ apply H' | apply span_cps'_rect ]. } |
| 3220 |
{ generalize (Pcons_false x ls prefix xs H' H). |
| 3221 |
clear -H; subst ls; exact id. } } |
| 3222 |
Defined. |
| 3223 |
|
| 3224 |
Fixpoint concat_groupBy' {A f} ls prefix {struct ls} |
| 3225 |
: match ls with |
| 3226 |
| nil => List.concat (@groupBy' A f ls prefix) = nil |
| 3227 |
| x :: xs |
| 3228 |
=> List.concat (@groupBy' A f ls prefix) = x :: List.rev prefix ++ xs |
| 3229 |
end. |
| 3230 |
Proof. |
| 3231 |
specialize (concat_groupBy' A f). |
| 3232 |
destruct ls as [|x xs]. |
| 3233 |
{ reflexivity. } |
| 3234 |
{ cbn [groupBy']. |
| 3235 |
apply span_cps'_rect; cbn [concat List.rev List.app]; clear xs prefix. |
| 3236 |
{ intros; rewrite ?List.app_nil_l, ?List.app_nil_r; split; reflexivity. } |
| 3237 |
{ intros *; rewrite ?app_comm_cons, <- ?List.app_assoc; cbn [List.app]; trivial. } |
| 3238 |
{ intros x' ls xs' prefix' H H'. |
| 3239 |
specialize (concat_groupBy' ls); cbv beta iota in *. |
| 3240 |
subst ls. |
| 3241 |
rewrite concat_groupBy'; cbn. |
| 3242 |
reflexivity. } } |
| 3243 |
Qed. |
| 3244 |
|
| 3245 |
Lemma concat_groupBy A f ls |
| 3246 |
: List.concat (@groupBy A f ls) = ls. |
| 3247 |
Proof. |
| 3248 |
cbv [groupBy]; destruct ls as [|x xs]; [ reflexivity | apply (concat_groupBy' (x :: xs)) ]. |
| 3249 |
Qed. |
| 3250 |
|
| 3251 |
Lemma eq_filter_nil_Forall_iff {A} f (xs : list A) |
| 3252 |
: filter f xs = nil <-> Forall (fun x => f x = false) xs. |
| 3253 |
Proof. induction xs; cbn; break_innermost_match; boring; inversion_list; inversion_one_head Forall; congruence. Qed. |
| 3254 |
|
| 3255 |
Definition is_nil {A} (x : list A) : bool |
| 3256 |
:= match x with |
| 3257 |
| nil => true |
| 3258 |
| _ => false |
| 3259 |
end. |
| 3260 |
|
| 3261 |
Lemma is_nil_eq_nil_iff {A x} : @is_nil A x = true <-> x = nil. |
| 3262 |
Proof. destruct x; cbv; split; congruence. Qed. |
| 3263 |
|
| 3264 |
Lemma find_none_iff {A} (f : A -> bool) (xs : list A) : find f xs = None <-> forall x, In x xs -> f x = false. |
| 3265 |
Proof. |
| 3266 |
split; try apply find_none. |
| 3267 |
pose proof (find_some f xs) as H. |
| 3268 |
edestruct find; [ specialize (H _ eq_refl) | reflexivity ]. |
| 3269 |
destruct H as [H1 H2]. |
| 3270 |
intro H'; specialize (H' _ H1); congruence. |
| 3271 |
Qed. |
| 3272 |
|
| 3273 |
Lemma find_none_iff_nth_error {A} (f : A -> bool) (xs : list A) : find f xs = None <-> forall n a, nth_error xs n = Some a -> f a = false. |
| 3274 |
Proof. |
| 3275 |
rewrite find_none_iff. |
| 3276 |
setoid_rewrite In_nth_error_iff. |
| 3277 |
intuition (destruct_head'_ex; eauto). |
| 3278 |
Qed. |
| 3279 |
|
| 3280 |
Lemma find_some_iff {A} (f : A -> bool) (xs : list A) x |
| 3281 |
: find f xs = Some x |
| 3282 |
<-> exists n, nth_error xs n = Some x /\ f x = true /\ forall n', n' < n -> forall a, nth_error xs n' = Some a -> f a = false. |
| 3283 |
Proof. |
| 3284 |
induction xs as [|y xs IHxs]; cbn [find]. |
| 3285 |
all: repeat first [ apply conj |
| 3286 |
| progress intros |
| 3287 |
| progress destruct_head'_ex |
| 3288 |
| progress destruct_head'_and |
| 3289 |
| progress Option.inversion_option |
| 3290 |
| progress break_innermost_match_hyps |
| 3291 |
| progress subst |
| 3292 |
| progress cbn in * |
| 3293 |
| assumption |
| 3294 |
| exists 0; cbn; repeat split; try assumption; (idtac + (intros; exfalso)); lia |
| 3295 |
| progress break_innermost_match |
| 3296 |
| reflexivity |
| 3297 |
| congruence |
| 3298 |
| match goal with |
| 3299 |
| [ H : nth_error nil ?i = _ |- _ ] => is_var i; destruct i |
| 3300 |
| [ H : nth_error (cons _ _) ?i = _ |- _ ] => is_var i; destruct i |
| 3301 |
| [ H : ?T <-> _, H' : ?T |- _ ] => destruct H as [H _]; specialize (H H') |
| 3302 |
| [ H : S ?x < S ?y |- _ ] => assert (x < y) by lia; clear H |
| 3303 |
| [ H : forall a, Some _ = Some a -> _ |- _ ] => specialize (H _ eq_refl) |
| 3304 |
| [ H : forall a, Some a = Some _ -> _ |- _ ] => specialize (H _ eq_refl) |
| 3305 |
| [ H : forall n, n < S ?v -> @?P n |- _ ] |
| 3306 |
=> assert (forall n, n < v -> P (S n)) by (intros ? ?; apply H; lia); |
| 3307 |
specialize (H 0 ltac:(lia)) |
| 3308 |
end |
| 3309 |
| eexists (S _); cbn; repeat apply conj; [ eassumption | .. ]; try assumption; intros [|?] |
| 3310 |
| progress split_iff |
| 3311 |
| solve [ eauto with nocore ] |
| 3312 |
| progress specialize_by eauto ]. |
| 3313 |
Qed. |
| 3314 |
|
| 3315 |
Section find_index. |
| 3316 |
Context {A} (f : A -> bool). |
| 3317 |
|
| 3318 |
Definition find_index (xs : list A) : option nat |
| 3319 |
:= option_map (@fst _ _) (find (fun v => f (snd v)) (enumerate xs)). |
| 3320 |
|
| 3321 |
Lemma find_index_none_iff xs |
| 3322 |
: find_index xs = None <-> forall i a, nth_error xs i = Some a -> f a = false. |
| 3323 |
Proof using Type. |
| 3324 |
cbv [find_index enumerate]. |
| 3325 |
edestruct find eqn:H; cbn; [ split; [ congruence | ] | split; [ intros _ | reflexivity ] ]. |
| 3326 |
{ rewrite find_some_iff in H. |
| 3327 |
destruct H as [n H]; intro H'; specialize (H' n). |
| 3328 |
rewrite nth_error_combine, nth_error_seq in H. |
| 3329 |
break_innermost_match_hyps; destruct_head'_and; Option.inversion_option; subst; cbn in *. |
| 3330 |
specialize (H' _ eq_refl); congruence. } |
| 3331 |
{ rewrite find_none_iff_nth_error in H. |
| 3332 |
intros n a; specialize (H n (n, a)). |
| 3333 |
rewrite nth_error_combine, ListUtil.nth_error_seq in H. |
| 3334 |
edestruct lt_dec; [ | rewrite nth_error_length_error by lia; congruence ]. |
| 3335 |
edestruct nth_error eqn:?; intros; Option.inversion_option; subst; specialize (H eq_refl); assumption. } |
| 3336 |
Qed. |
| 3337 |
|
| 3338 |
Lemma find_index_some_iff xs n |
| 3339 |
: find_index xs = Some n |
| 3340 |
<-> ((exists x, nth_error xs n = Some x /\ f x = true) /\ forall n', n' < n -> forall a, nth_error xs n' = Some a -> f a = false). |
| 3341 |
Proof using Type. |
| 3342 |
cbv [find_index enumerate]. |
| 3343 |
edestruct find eqn:H; cbn; [ | split; [ congruence | ] ]. |
| 3344 |
{ rewrite find_some_iff in H. |
| 3345 |
destruct H as [n' H]. |
| 3346 |
rewrite nth_error_combine, ListUtil.nth_error_seq in H. |
| 3347 |
setoid_rewrite nth_error_combine in H. |
| 3348 |
setoid_rewrite ListUtil.nth_error_seq in H. |
| 3349 |
break_innermost_match_hyps; split; intro H'; destruct_head'_and; Option.inversion_option; subst; cbn in *. |
| 3350 |
all: repeat apply conj; eauto. |
| 3351 |
{ let H := match goal with H : forall n, n < _ -> _ |- _ => H end in |
| 3352 |
intros i H' a' H''; specialize (H i H' (i, a')); |
| 3353 |
rewrite H'' in H; |
| 3354 |
break_innermost_match_hyps; [ specialize (H eq_refl); assumption | ]. |
| 3355 |
rewrite nth_error_length_error in H'' by lia; congruence. } |
| 3356 |
{ match goal with |- Some ?n = Some ?m => destruct (lt_eq_lt_dec n m) end; destruct_head' sumbool; subst; try reflexivity. |
| 3357 |
all: match goal with H : forall n', n' < _ -> _ |- _ => specialize (H _ ltac:(eassumption)) end. |
| 3358 |
{ match goal with H : forall a, nth_error _ _ = Some _ -> _ |- _ => specialize (H _ ltac:(eassumption)); congruence end. } |
| 3359 |
{ destruct_head'_ex; destruct_head'_and. |
| 3360 |
break_innermost_match_hyps; try congruence; try lia. |
| 3361 |
Option.inversion_option; subst. |
| 3362 |
match goal with H : forall x, Some _ = Some x -> _ |- _ => specialize (H _ eq_refl) end. |
| 3363 |
cbn in *. |
| 3364 |
congruence. } } } |
| 3365 |
{ rewrite find_none_iff_nth_error in H. |
| 3366 |
intros [ [a [H0 H1]] ?]; specialize (H n (n, a)). |
| 3367 |
rewrite nth_error_combine, ListUtil.nth_error_seq, H0 in H. |
| 3368 |
break_innermost_match_hyps; [ | rewrite nth_error_length_error in H0 by lia; congruence ]. |
| 3369 |
specialize (H eq_refl); cbn in *. |
| 3370 |
congruence. } |
| 3371 |
Qed. |
| 3372 |
End find_index. |
| 3373 |
|
| 3374 |
Lemma fold_left_id {A B} init ls |
| 3375 |
: @fold_left A B (fun x _ => x) ls init = init. |
| 3376 |
Proof. |
| 3377 |
revert init; induction ls as [|x xs IHxs]; cbn [fold_left]; eauto. |
| 3378 |
Qed. |
| 3379 |
|
| 3380 |
Lemma fold_left_cons {B} init ls |
| 3381 |
: @fold_left _ B (fun xs x => cons x xs) ls init = List.rev ls ++ init. |
| 3382 |
Proof. |
| 3383 |
revert init; induction ls as [|x xs IHxs]; cbn [fold_left List.rev]; |
| 3384 |
intros; rewrite ?IHxs, ?List.app_nil_l, ?List.app_nil_r, <- ?List.app_assoc; |
| 3385 |
cbn [List.app]; reflexivity. |
| 3386 |
Qed. |
| 3387 |
|
| 3388 |
Lemma map_swap_combine {A B} ls1 ls2 |
| 3389 |
: List.map (fun xy => (snd xy, fst xy)) (List.combine ls2 ls1) |
| 3390 |
= @List.combine A B ls1 ls2. |
| 3391 |
Proof. |
| 3392 |
revert ls2; induction ls1 as [|x xs IHxs], ls2 as [|y ys]; cbn [List.combine List.map fst snd]; congruence. |
| 3393 |
Qed. |
| 3394 |
|
| 3395 |
Lemma fold_right_higher_order A B C f a ls (F : _ -> C) |
| 3396 |
: fold_right (fun x acc a => acc (f x a)) F ls a |
| 3397 |
= F (@fold_right A B f a (List.rev ls)). |
| 3398 |
Proof. |
| 3399 |
revert F a; induction ls; cbn; try reflexivity; intros. |
| 3400 |
rewrite IHls, fold_right_app; cbn. |
| 3401 |
reflexivity. |
| 3402 |
Qed. |
| 3403 |
|
| 3404 |
Lemma fold_right_rev_higher_order A B f a ls |
| 3405 |
: @fold_right A B f a (List.rev ls) |
| 3406 |
= fold_right (fun x acc a => acc (f x a)) id ls a. |
| 3407 |
Proof. symmetry; apply fold_right_higher_order. Qed. |
| 3408 |
|
| 3409 |
Lemma fold_left_higher_order A B C f a ls (F : _ -> C) |
| 3410 |
: fold_left (fun acc x a => acc (f a x)) ls F a |
| 3411 |
= F (@fold_left A B f (List.rev ls) a). |
| 3412 |
Proof. |
| 3413 |
revert F a; induction ls; cbn; try reflexivity; intros. |
| 3414 |
rewrite IHls, fold_left_app; cbn. |
| 3415 |
reflexivity. |
| 3416 |
Qed. |
| 3417 |
|
| 3418 |
Lemma fold_left_rev_higher_order A B f a ls |
| 3419 |
: @fold_left A B f (List.rev ls) a |
| 3420 |
= fold_left (fun acc x a => acc (f a x)) ls id a. |
| 3421 |
Proof. symmetry; apply fold_left_higher_order. Qed. |
| 3422 |
|
| 3423 |
(* This module is here because the equivalent standard library functions fail to be "reified by unfolding". *) |
| 3424 |
|
| 3425 |
Module Reifiable. |
| 3426 |
Section __. |
| 3427 |
Context {X : Type} |
| 3428 |
(eqb : X -> X -> bool) |
| 3429 |
(eqb_eq : forall x1 x2, eqb x1 x2 = true <-> x1 = x2). |
| 3430 |
|
| 3431 |
Definition existsb (f : X -> bool) (l : list X) : bool := |
| 3432 |
fold_right (fun x found => orb (f x) found) false l. |
| 3433 |
|
| 3434 |
Lemma reifiable_existsb_is_existsb : forall (f : X -> bool) (l : list X), |
| 3435 |
existsb f l = List.existsb f l. |
| 3436 |
Proof. reflexivity. Qed. |
| 3437 |
|
| 3438 |
Lemma existsb_eqb_true_iff : forall x l, existsb (eqb x) l = true <-> In x l. |
| 3439 |
Proof. |
| 3440 |
intros x l. rewrite reifiable_existsb_is_existsb. rewrite existsb_exists. split. |
| 3441 |
- intros [x0 [H1 H2]]. rewrite eqb_eq in H2. subst. assumption. |
| 3442 |
- intros H. exists x. split. |
| 3443 |
+ assumption. |
| 3444 |
+ apply eqb_eq. reflexivity. |
| 3445 |
Qed. |
| 3446 |
|
| 3447 |
Lemma existsb_eqb_false_iff : forall x l, existsb (eqb x) l = false <-> ~In x l. |
| 3448 |
Proof. |
| 3449 |
intros. rewrite <- existsb_eqb_true_iff. split. |
| 3450 |
- intros H. rewrite H. auto. |
| 3451 |
- intros H. destruct (existsb (eqb x) l). |
| 3452 |
+ exfalso. apply H. reflexivity. |
| 3453 |
+ reflexivity. |
| 3454 |
Qed. |
| 3455 |
|
| 3456 |
Definition nodupb (l : list X) := |
| 3457 |
fold_right (fun x l' => if (existsb (eqb x) l') then l' else (x :: l')) [] l. |
| 3458 |
|
| 3459 |
Lemma nodupb_in_iff (x : X) (l : list X) : In x l <-> In x (nodupb l). |
| 3460 |
Proof. |
| 3461 |
induction l as [|x' l' IHl']. |
| 3462 |
- reflexivity. |
| 3463 |
- simpl. destruct (existsb (eqb x') (nodupb l')) eqn:E. |
| 3464 |
+ rewrite existsb_eqb_true_iff in E. split. |
| 3465 |
-- intros [H|H]. |
| 3466 |
++ rewrite <- H. apply E. |
| 3467 |
++ rewrite <- IHl'. apply H. |
| 3468 |
-- intros H. right. rewrite IHl'. apply H. |
| 3469 |
+ rewrite existsb_eqb_false_iff in E. split. |
| 3470 |
-- intros [H|H]. |
| 3471 |
++ rewrite H. simpl. left. reflexivity. |
| 3472 |
++ simpl. right. rewrite <- IHl'. apply H. |
| 3473 |
-- simpl. intros [H|H]. |
| 3474 |
++ left. apply H. |
| 3475 |
++ right. rewrite IHl'. apply H. |
| 3476 |
Qed. |
| 3477 |
|
| 3478 |
Lemma nodupb_split (x : X) (l : list X) : In x l -> |
| 3479 |
exists l1 l2, nodupb l = l1 ++ [x] ++ l2 /\ ~ In x l1 /\ ~ In x l2. |
| 3480 |
Proof. |
| 3481 |
intros H. induction l as [| x' l']. |
| 3482 |
- simpl in H. destruct H. |
| 3483 |
- simpl in H. destruct H as [H|H]. |
| 3484 |
+ rewrite H. clear H. simpl. destruct (existsb (eqb x) (nodupb l')) eqn:E. |
| 3485 |
-- rewrite existsb_eqb_true_iff in E. rewrite <- nodupb_in_iff in E. |
| 3486 |
apply IHl' in E. apply E. |
| 3487 |
-- exists []. exists (nodupb l'). split. |
| 3488 |
++ rewrite app_nil_l. reflexivity. |
| 3489 |
++ split. |
| 3490 |
--- auto. |
| 3491 |
--- rewrite <- existsb_eqb_false_iff. apply E. |
| 3492 |
+ apply IHl' in H. clear IHl'. simpl. destruct (existsb (eqb x') (nodupb l')) eqn:E. |
| 3493 |
-- apply H. |
| 3494 |
-- destruct H as [l1 [l2 [H1 [H2 H3] ] ] ]. exists (x' :: l1). exists l2. split. |
| 3495 |
++ rewrite H1. reflexivity. |
| 3496 |
++ split. |
| 3497 |
--- simpl. intros [H|H]. |
| 3498 |
+++ rewrite H in *. rewrite H1 in E. apply existsb_eqb_false_iff in E. apply E. |
| 3499 |
repeat rewrite in_app_iff. right. left. simpl. left. reflexivity. |
| 3500 |
+++ apply H2. apply H. |
| 3501 |
--- apply H3. |
| 3502 |
Qed. |
| 3503 |
End __. |
| 3504 |
End Reifiable. |